[trunk] Worth/Sell: Support for double values as prices and more important: support for data items.

the yaml structure has changed, there is a fallback to the old structure.

This code is untested.

git-svn-id: https://svn.java.net/svn/essentials~svn/trunk@1211 e251c2fe-e539-e718-e476-b85c1f46cddb
This commit is contained in:
snowleo 2011-04-16 06:28:56 +00:00
parent cf144b1a64
commit c849bf7fe9
4 changed files with 24 additions and 8 deletions

View file

@ -2,6 +2,7 @@ package com.earth2me.essentials;
import java.io.File; import java.io.File;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.bukkit.inventory.ItemStack;
public class Worth implements IConf public class Worth implements IConf
@ -16,14 +17,26 @@ public class Worth implements IConf
config.load(); config.load();
} }
public int getPrice(String id) public double getPrice(ItemStack itemStack)
{ {
return config.getInt("worth-" + id, 0); double result = config.getDouble("worth."+itemStack.getType().toString().toLowerCase()+"."+itemStack.getData().getData(), Double.NaN);
if (Double.isNaN(result)) {
result = config.getDouble("worth."+itemStack.getType().toString().toLowerCase(), Double.NaN);
}
if (Double.isNaN(result)) {
result = config.getDouble("worth-"+itemStack.getTypeId(), 0.0);
}
return result;
} }
public void setPrice(String id, int price) public void setPrice(ItemStack itemStack, double price)
{ {
config.setProperty("worth-" + id, price); if (itemStack.getType().getData() == null) {
config.setProperty("worth." + itemStack.getType().toString().toLowerCase(), price);
} else {
config.setProperty("worth." + itemStack.getType().toString().toLowerCase()+"."+itemStack.getData().getData(), price);
}
config.removeProperty("worth-"+itemStack.getTypeId());
config.save(); config.save();
} }

View file

@ -2,6 +2,7 @@ package com.earth2me.essentials.commands;
import org.bukkit.Server; import org.bukkit.Server;
import com.earth2me.essentials.Essentials; import com.earth2me.essentials.Essentials;
import com.earth2me.essentials.InventoryWorkaround;
import com.earth2me.essentials.User; import com.earth2me.essentials.User;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@ -24,7 +25,7 @@ public class Commandsell extends EssentialsCommand
int id = is.getTypeId(); int id = is.getTypeId();
int amount = 0; int amount = 0;
if (args.length > 0) amount = Integer.parseInt(args[0].replaceAll("[^0-9]", "")); if (args.length > 0) amount = Integer.parseInt(args[0].replaceAll("[^0-9]", ""));
int worth = Essentials.getWorth().getPrice(String.valueOf(id)); double worth = Essentials.getWorth().getPrice(is);
boolean stack = args.length > 0 && args[0].endsWith("s"); boolean stack = args.length > 0 && args[0].endsWith("s");
boolean requireStack = parent.getConfiguration().getBoolean("trade-in-stacks-" + id, false); boolean requireStack = parent.getConfiguration().getBoolean("trade-in-stacks-" + id, false);
@ -34,6 +35,8 @@ public class Commandsell extends EssentialsCommand
int max = 0; int max = 0;
for (ItemStack s : user.getInventory().all(is).values()) for (ItemStack s : user.getInventory().all(is).values())
{ {
if (s.getDurability() != is.getDurability())
continue;
max += s.getAmount(); max += s.getAmount();
} }
@ -54,7 +57,7 @@ public class Commandsell extends EssentialsCommand
} }
user.charge(this); user.charge(this);
user.getInventory().removeItem(new ItemStack(id, amount)); InventoryWorkaround.removeItem(user.getInventory(), true, new ItemStack(is.getType(), amount, is.getDurability()));
user.updateInventory(); user.updateInventory();
user.giveMoney(worth * amount); user.giveMoney(worth * amount);
user.sendMessage("§7Sold for §c$" + (worth * amount) + "§7 (" + amount + " items at $" + worth + " each)"); user.sendMessage("§7Sold for §c$" + (worth * amount) + "§7 (" + amount + " items at $" + worth + " each)");

View file

@ -23,7 +23,7 @@ public class Commandsetworth extends EssentialsCommand
return; return;
} }
ItemStack stack = ItemDb.get(args[0]); ItemStack stack = ItemDb.get(args[0]);
Essentials.getWorth().setPrice(Integer.toString(stack.getTypeId()), Integer.parseInt(args[1])); Essentials.getWorth().setPrice(stack, Integer.parseInt(args[1]));
user.charge(this); user.charge(this);
user.sendMessage("§7Worth value set"); user.sendMessage("§7Worth value set");
} }

View file

@ -39,7 +39,7 @@ public class Commandworth extends EssentialsCommand
amount = 64; amount = 64;
} }
int worth = Essentials.getWorth().getPrice(String.valueOf(id)); double worth = Essentials.getWorth().getPrice(is);
user.charge(this); user.charge(this);
user.sendMessage("§7Stack of " + id + " worth §c$" + (worth * amount) + "§7 (" + amount + " item(s) at $" + worth + " each)"); user.sendMessage("§7Stack of " + id + " worth §c$" + (worth * amount) + "§7 (" + amount + " item(s) at $" + worth + " each)");