Skip to content
Browse files

Added item and level prices for all PurchasableMagicSigns.

  • Loading branch information...
1 parent 2114d03 commit d02bda2a0eeb29aff447fd416494472eb9561832 @Philipp15b committed Apr 5, 2012
View
2 pom.xml
@@ -25,7 +25,7 @@
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
- <version>1.1-R4</version>
+ <version>1.2.5-R0.1</version>
</dependency>
<dependency>
View
3 src/main/java/de/philworld/bukkit/magicsigns/MagicSignsListener.java
@@ -77,8 +77,7 @@ public void onPlayerInteract(PlayerInteractEvent event) {
MSMsg.NOT_ENOUGH_MONEY.send(event.getPlayer());
return;
} else {
- MSMsg.PAID_SIGN.send(event.getPlayer(),
- Double.toString(pSign.getPrice()));
+ MSMsg.PAID_SIGN.send(event.getPlayer());
}
}
View
1 src/main/java/de/philworld/bukkit/magicsigns/config/ConfigurationBase.java
@@ -5,7 +5,6 @@
import java.util.logging.Logger;
import org.bukkit.configuration.ConfigurationSection;
-import org.bukkit.util.config.ConfigurationNode;
import de.philworld.bukkit.magicsigns.MagicSigns;
View
171 src/main/java/de/philworld/bukkit/magicsigns/economy/Price.java
@@ -0,0 +1,171 @@
+package de.philworld.bukkit.magicsigns.economy;
+
+import org.bukkit.Material;
+import org.bukkit.entity.Player;
+import org.bukkit.inventory.ItemStack;
+
+import de.philworld.bukkit.magicsigns.MagicSigns;
+import de.philworld.bukkit.magicsigns.util.InventoryUtil;
+
+public abstract class Price {
+
+ public static Price valueOf(String text) throws IllegalArgumentException,
+ NumberFormatException {
+ // the price is an item
+ if (text.startsWith("i:")) {
+ String[] result = text.split("i:");
+ if (result.length != 2)
+ throw new IllegalArgumentException(
+ "Invalid price format for an item! Format: `i:ITEMNAME`");
+ return Price.Item.valueOf(result[1]);
+ } else if (text.startsWith("lvl:")) {
+ String[] result = text.split("i:");
+ if (result.length != 2)
+ throw new IllegalArgumentException(
+ "Invalid price format for levels! Format: `lvl:10`");
+ return Price.Level.valueOf(result[1]);
+ } else { // its just money
+ return Price.VaultEconomy.valueOf(text);
+ }
+ }
+
+ /**
+ * Returns if the player can pay this price.
+ *
+ * @param p
+ * The player
+ * @return True if the player can pay this, else false
+ */
+ public abstract boolean has(Player p);
+
+ /**
+ * Withdraw an amount from a player.
+ *
+ * @param p
+ * The player
+ * @return True if the transaction succeeded, else false.
+ */
+ public abstract boolean withdrawPlayer(Player p);
+
+ /**
+ * A price that uses Vault's economy.
+ *
+ */
+ public static class VaultEconomy extends Price {
+
+ public static VaultEconomy valueOf(String text)
+ throws IllegalArgumentException, NumberFormatException {
+ double money = Double.parseDouble(text);
+ if (money < 0)
+ throw new IllegalArgumentException(
+ "The price may not be lower than zero!");
+ return new Price.VaultEconomy(money);
+ }
+
+ private final double price;
+
+ public VaultEconomy(double price) {
+ this.price = price;
+ }
+
+ public double getPrice() {
+ return price;
+ }
+
+ @Override
+ public boolean has(Player p) {
+ return MagicSigns.economy.has(p.getName(), price);
+ }
+
+ @Override
+ public boolean withdrawPlayer(Player p) {
+ if (MagicSigns.economy != null) {
+ if (has(p)) {
+ if (MagicSigns.economy.withdrawPlayer(p.getName(), price)
+ .transactionSuccess()) {
+ return true;
+ }
+ }
+ return false;
+ }
+ return true;
+ }
+
+ }
+
+ /**
+ * A price that uses items.
+ *
+ */
+ public static class Item extends Price {
+
+ public static Item valueOf(String text) throws NumberFormatException {
+ String[] result = text.split(":");
+ Material material = Material.getMaterial(result[0]);
+ int amount;
+ if (result.length == 1)
+ amount = 1;
+ else
+ amount = Integer.parseInt(result[1]);
+ return new Item(material, amount);
+ }
+
+ private final Material material;
+ private final int amount;
+
+ public Item(Material material, int amount) {
+ this.material = material;
+ this.amount = amount;
+ }
+
+ public ItemStack getItems() {
+ return new ItemStack(material, amount);
+ }
+
+ @Override
+ public boolean has(Player p) {
+ return p.getInventory().contains(material, amount);
+ }
+
+ @SuppressWarnings("deprecation")
+ @Override
+ public boolean withdrawPlayer(Player p) {
+ if (has(p)) {
+ InventoryUtil.removeItems(p.getInventory(), material, amount);
+ p.updateInventory();
+ return true;
+ }
+ return false;
+ }
+
+ }
+
+ public static class Level extends Price {
+
+ public static Level valueOf(String text) throws NumberFormatException {
+ return new Level(Integer.valueOf(text));
+ }
+
+ private final int level;
+
+ public Level(int level) {
+ this.level = level;
+ }
+
+ @Override
+ public boolean has(Player p) {
+ return p.getLevel() >= level;
+ }
+
+ @Override
+ public boolean withdrawPlayer(Player p) {
+ if (has(p)) {
+ p.setLevel(p.getLevel() - level);
+ return true;
+ }
+ return false;
+ }
+
+ }
+
+}
View
1 src/main/java/de/philworld/bukkit/magicsigns/signs/MagicSign.java
@@ -6,7 +6,6 @@
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.event.block.SignChangeEvent;
import org.bukkit.event.player.PlayerInteractEvent;
-import org.bukkit.util.config.ConfigurationNode;
import de.philworld.bukkit.magicsigns.InvalidSignException;
import de.philworld.bukkit.magicsigns.config.ConfigurationBase;
View
30 src/main/java/de/philworld/bukkit/magicsigns/signs/PurchasableMagicSign.java
@@ -4,15 +4,15 @@
import org.bukkit.entity.Player;
import de.philworld.bukkit.magicsigns.InvalidSignException;
-import de.philworld.bukkit.magicsigns.MagicSigns;
+import de.philworld.bukkit.magicsigns.economy.Price;
/**
* This is a {@link MagicSign} that allows to be payed for usage. The price has
* to be on the last line of the sign.
*/
public abstract class PurchasableMagicSign extends MagicSign {
- protected double price = 0;
+ protected final Price price;
/**
* Creates a new purchasable magic sign by setting the price of this sign.
@@ -27,11 +27,13 @@ public PurchasableMagicSign(Block sign, String[] lines)
super(sign, lines);
if (!lines[3].isEmpty()) {
- price = Double.parseDouble(lines[3]);
- if (price < 0) {
- throw new InvalidSignException(
- "The sign price may not be lower than zero!");
+ try {
+ price = Price.valueOf(lines[3]);
+ } catch (IllegalArgumentException e) {
+ throw new InvalidSignException(e.getMessage());
}
+ } else {
+ price = null;
}
}
@@ -40,7 +42,7 @@ public PurchasableMagicSign(Block sign, String[] lines)
*
* @return The price.
*/
- public double getPrice() {
+ public Price getPrice() {
return price;
}
@@ -53,16 +55,10 @@ public double getPrice() {
* found, else false.
*/
public boolean withdrawPlayer(Player p) {
- if (MagicSigns.economy != null) {
- if (MagicSigns.economy.has(p.getName(), price)) {
- if (MagicSigns.economy.withdrawPlayer(p.getName(), price)
- .transactionSuccess()) {
- return true;
- }
- }
- return false;
- }
- return true;
+ if (price != null)
+ return price.withdrawPlayer(p);
+ else
+ return true;
}
}
View
37 src/main/java/de/philworld/bukkit/magicsigns/util/InventoryUtil.java
@@ -0,0 +1,37 @@
+package de.philworld.bukkit.magicsigns.util;
+
+import org.bukkit.Material;
+import org.bukkit.inventory.Inventory;
+import org.bukkit.inventory.ItemStack;
+
+public class InventoryUtil {
+
+ /**
+ * Removes a specific amount of items from an inventory.
+ *
+ * @param inv
+ * The inventory
+ * @param type
+ * The material
+ * @param amount
+ * The amount
+ */
+ public static void removeItems(Inventory inv, Material type,
+ int amount) {
+ for (ItemStack is : inv.getContents()) {
+ if (is != null && is.getType() == type) {
+ int newamount = is.getAmount() - amount;
+ if (newamount > 0) {
+ is.setAmount(newamount);
+ break;
+ } else {
+ inv.remove(is);
+ amount = -newamount;
+ if (amount == 0)
+ break;
+ }
+ }
+ }
+ }
+
+}
View
4 src/main/java/de/philworld/bukkit/magicsigns/util/MSMsg.java
@@ -14,8 +14,8 @@
LEVEL_ADDED(ChatColor.GREEN + "You have been taught % levels."),
TELEPORT_SUCCESS(ChatColor.GREEN + "You have been teleported."),
INVENTORY_CLEARED(ChatColor.GREEN + "Your inventory has been cleared."),
- NOT_ENOUGH_MONEY("You don't have enough money for this!"),
- PAID_SIGN("You paid % to use this sign.");
+ NOT_ENOUGH_MONEY("You can't pay this sign. Make sure you have enough money, items or levels."),
+ PAID_SIGN("You paid this sign.");
private String msg;

0 comments on commit d02bda2

Please sign in to comment.
Something went wrong with that request. Please try again.