CB#1518 B#1042

Support for Enchantments & Removed broken BedFix (in cb now)
This commit is contained in:
snowleo 2011-11-27 06:00:58 +01:00
parent d5c852b79d
commit f250a107e4
16 changed files with 124 additions and 68 deletions

View file

@ -57,7 +57,7 @@ import org.bukkit.scheduler.BukkitScheduler;
public class Essentials extends JavaPlugin implements IEssentials public class Essentials extends JavaPlugin implements IEssentials
{ {
public static final int BUKKIT_VERSION = 1512; public static final int BUKKIT_VERSION = 1518;
private static final Logger LOGGER = Logger.getLogger("Minecraft"); private static final Logger LOGGER = Logger.getLogger("Minecraft");
private transient ISettings settings; private transient ISettings settings;
private final transient TNTExplodeListener tntListener = new TNTExplodeListener(this); private final transient TNTExplodeListener tntListener = new TNTExplodeListener(this);

View file

@ -2,7 +2,9 @@ package com.earth2me.essentials;
import static com.earth2me.essentials.I18n._; import static com.earth2me.essentials.I18n._;
import java.io.*; import java.io.*;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -10,6 +12,7 @@ import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.Server; import org.bukkit.Server;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.util.config.Configuration; import org.bukkit.util.config.Configuration;
@ -224,13 +227,25 @@ public class EssentialsConf extends Configuration
public ItemStack getItemStack(final String path) public ItemStack getItemStack(final String path)
{ {
return new ItemStack( final ItemStack stack = new ItemStack(
Material.valueOf(getString(path + ".type", "AIR")), Material.valueOf(getString(path + ".type", "AIR")),
getInt(path + ".amount", 1), getInt(path + ".amount", 1),
(short)getInt(path + ".damage", 0)/* (short)getInt(path + ".damage", 0));
List<String> enchants = getKeys(path + ".enchant");
for (String enchant : enchants)
{
Enchantment enchantment = Enchantment.getByName(enchant);
if (enchantment == null) {
continue;
}
int level = getInt(path+ ".enchant."+enchant, enchantment.getStartLevel());
stack.addUnsafeEnchantment(enchantment, level);
}
return stack;
/*
* , * ,
* (byte)getInt(path + ".data", 0) * (byte)getInt(path + ".data", 0)
*/); */
} }
public void setProperty(final String path, final ItemStack stack) public void setProperty(final String path, final ItemStack stack)
@ -239,6 +254,16 @@ public class EssentialsConf extends Configuration
map.put("type", stack.getType().toString()); map.put("type", stack.getType().toString());
map.put("amount", stack.getAmount()); map.put("amount", stack.getAmount());
map.put("damage", stack.getDurability()); map.put("damage", stack.getDurability());
Map<Enchantment, Integer> enchantments = stack.getEnchantments();
if (!enchantments.isEmpty())
{
Map<String, Integer> enchant = new HashMap<String, Integer>();
for (Map.Entry<Enchantment, Integer> entry : enchantments.entrySet())
{
enchant.put(entry.getKey().getName(), entry.getValue());
}
map.put("enchant", enchant);
}
// getData().getData() is broken // getData().getData() is broken
//map.put("data", stack.getDurability()); //map.put("data", stack.getDurability());
setProperty(path, map); setProperty(path, map);

View file

@ -57,7 +57,8 @@ public class EssentialsEntityListener extends EntityListener
ItemStack hand = player.getItemInHand(); ItemStack hand = player.getItemInHand();
if (hand != null && hand.getType() == Material.MILK_BUCKET) { if (hand != null && hand.getType() == Material.MILK_BUCKET) {
((Animals)eDefend).setAge(-24000); ((Animals)eDefend).setAge(-24000);
player.setItemInHand(new ItemStack(Material.BUCKET, hand.getAmount())); hand.setType(Material.BUCKET);
player.setItemInHand(hand);
player.updateInventory(); player.updateInventory();
event.setCancelled(true); event.setCancelled(true);
} }

View file

@ -1,6 +1,5 @@
package com.earth2me.essentials; package com.earth2me.essentials;
import com.earth2me.essentials.craftbukkit.BedLocationFix;
import static com.earth2me.essentials.I18n._; import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.textreader.IText; import com.earth2me.essentials.textreader.IText;
import com.earth2me.essentials.textreader.KeywordReplacer; import com.earth2me.essentials.textreader.KeywordReplacer;
@ -17,7 +16,6 @@ import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.Server; import org.bukkit.Server;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerLoginEvent.Result; import org.bukkit.event.player.PlayerLoginEvent.Result;
import org.bukkit.event.player.*; import org.bukkit.event.player.*;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@ -234,7 +232,7 @@ public class EssentialsPlayerListener extends PlayerListener
{ {
Location loc = user.getHome(user.getLocation()); Location loc = user.getHome(user.getLocation());
if (loc == null) { if (loc == null) {
loc = BedLocationFix.getBedSpawnLocation(user); loc = user.getBedSpawnLocation();
} }
if (loc != null) { if (loc != null) {
user.setCompassTarget(loc); user.setCompassTarget(loc);

View file

@ -20,6 +20,7 @@ public class FakeInventory implements Inventory
continue; continue;
} }
this.items[i] = new ItemStack(items[i].getTypeId(), items[i].getAmount(), items[i].getDurability()); this.items[i] = new ItemStack(items[i].getTypeId(), items[i].getAmount(), items[i].getDurability());
this.items[i].addEnchantments(items[i].getEnchantments());
} }
} }

View file

@ -34,7 +34,7 @@ public final class InventoryWorkaround
{ {
continue; continue;
} }
if (item.getTypeId() == cItem.getTypeId() && (!forceAmount || item.getAmount() == cItem.getAmount()) && (!forceDurability || cItem.getDurability() == item.getDurability())) if (item.getTypeId() == cItem.getTypeId() && (!forceAmount || item.getAmount() == cItem.getAmount()) && (!forceDurability || cItem.getDurability() == item.getDurability()) && cItem.getEnchantments().equals(item.getEnchantments()))
{ {
return i; return i;
} }
@ -56,7 +56,7 @@ public final class InventoryWorkaround
{ {
continue; continue;
} }
if (item.getTypeId() == cItem.getTypeId() && cItem.getAmount() < cItem.getType().getMaxStackSize() && (!forceDurability || cItem.getDurability() == item.getDurability())) if (item.getTypeId() == cItem.getTypeId() && cItem.getAmount() < cItem.getType().getMaxStackSize() && (!forceDurability || cItem.getDurability() == item.getDurability()) && cItem.getEnchantments().equals(item.getEnchantments()))
{ {
return i; return i;
} }
@ -102,9 +102,10 @@ public final class InventoryWorkaround
if (combined[j] == null) if (combined[j] == null)
{ {
combined[j] = new ItemStack(items[i].getType(), items[i].getAmount(), items[i].getDurability()); combined[j] = new ItemStack(items[i].getType(), items[i].getAmount(), items[i].getDurability());
combined[j].addEnchantments(items[i].getEnchantments());
break; break;
} }
if (combined[j].getTypeId() == items[i].getTypeId() && (!forceDurability || combined[j].getDurability() == items[i].getDurability())) if (combined[j].getTypeId() == items[i].getTypeId() && (!forceDurability || combined[j].getDurability() == items[i].getDurability()) && combined[j].getEnchantments().equals(items[i].getEnchantments()))
{ {
combined[j].setAmount(combined[j].getAmount() + items[i].getAmount()); combined[j].setAmount(combined[j].getAmount() + items[i].getAmount());
break; break;
@ -143,7 +144,9 @@ public final class InventoryWorkaround
// More than a single stack! // More than a single stack!
if (item.getAmount() > item.getType().getMaxStackSize()) if (item.getAmount() > item.getType().getMaxStackSize())
{ {
cinventory.setItem(firstFree, new ItemStack(item.getTypeId(), item.getType().getMaxStackSize(), item.getDurability())); ItemStack stack = new ItemStack(item.getTypeId(), item.getType().getMaxStackSize(), item.getDurability());
stack.addEnchantments(item.getEnchantments());
cinventory.setItem(firstFree, stack);
item.setAmount(item.getAmount() - item.getType().getMaxStackSize()); item.setAmount(item.getAmount() - item.getType().getMaxStackSize());
} }
else else
@ -257,9 +260,10 @@ public final class InventoryWorkaround
if (combined[j] == null) if (combined[j] == null)
{ {
combined[j] = new ItemStack(items[i].getType(), items[i].getAmount(), items[i].getDurability()); combined[j] = new ItemStack(items[i].getType(), items[i].getAmount(), items[i].getDurability());
combined[j].addEnchantments(items[i].getEnchantments());
break; break;
} }
if (combined[j].getTypeId() == items[i].getTypeId() && (!forceDurability || combined[j].getDurability() == items[i].getDurability())) if (combined[j].getTypeId() == items[i].getTypeId() && (!forceDurability || combined[j].getDurability() == items[i].getDurability()) && combined[j].getEnchantments().equals(items[i].getEnchantments()))
{ {
combined[j].setAmount(combined[j].getAmount() + items[i].getAmount()); combined[j].setAmount(combined[j].getAmount() + items[i].getAmount());
break; break;
@ -318,14 +322,18 @@ public final class InventoryWorkaround
final int maxStackSize = itm.getType().getMaxStackSize(); final int maxStackSize = itm.getType().getMaxStackSize();
final int stacks = itm.getAmount() / maxStackSize; final int stacks = itm.getAmount() / maxStackSize;
final int leftover = itm.getAmount() % maxStackSize; final int leftover = itm.getAmount() % maxStackSize;
Item[] itemStacks = new Item[stacks + (leftover > 0 ? 1 : 0)]; final Item[] itemStacks = new Item[stacks + (leftover > 0 ? 1 : 0)];
for (int i = 0; i < stacks; i++) for (int i = 0; i < stacks; i++)
{ {
itemStacks[i] = loc.getWorld().dropItem(loc, new ItemStack(itm.getType(), maxStackSize, itm.getDurability())); final ItemStack stack = new ItemStack(itm.getType(), maxStackSize, itm.getDurability());
stack.addEnchantments(itm.getEnchantments());
itemStacks[i] = loc.getWorld().dropItem(loc, stack);
} }
if (leftover > 0) if (leftover > 0)
{ {
itemStacks[stacks] = loc.getWorld().dropItem(loc, new ItemStack(itm.getType(), leftover, itm.getDurability())); final ItemStack stack = new ItemStack(itm.getType(), leftover, itm.getDurability());
stack.addEnchantments(itm.getEnchantments());
itemStacks[stacks] = loc.getWorld().dropItem(loc, stack);
} }
return itemStacks; return itemStacks;
} }

View file

@ -4,6 +4,7 @@ import static com.earth2me.essentials.I18n._;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
import lombok.Delegate; import lombok.Delegate;

View file

@ -4,7 +4,6 @@ import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.Trade; import com.earth2me.essentials.Trade;
import com.earth2me.essentials.User; import com.earth2me.essentials.User;
import com.earth2me.essentials.Util; import com.earth2me.essentials.Util;
import com.earth2me.essentials.craftbukkit.BedLocationFix;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import org.bukkit.Location; import org.bukkit.Location;
@ -45,7 +44,7 @@ public class Commandhome extends EssentialsCommand
try try
{ {
if ("bed".equalsIgnoreCase(homeName)) { if ("bed".equalsIgnoreCase(homeName)) {
final Location bed = BedLocationFix.getBedSpawnLocation(player); final Location bed = player.getBedSpawnLocation();
if (bed != null) if (bed != null)
{ {
user.getTeleport().teleport(bed, charge); user.getTeleport().teleport(bed, charge);
@ -58,7 +57,7 @@ public class Commandhome extends EssentialsCommand
final List<String> homes = player.getHomes(); final List<String> homes = player.getHomes();
if (homes.isEmpty() && player.equals(user)) if (homes.isEmpty() && player.equals(user))
{ {
final Location loc = BedLocationFix.getBedSpawnLocation(player); final Location loc = player.getBedSpawnLocation();
if (loc == null) if (loc == null)
{ {
if (ess.getSettings().spawnIfNoHome()) if (ess.getSettings().spawnIfNoHome())

View file

@ -149,6 +149,7 @@ public class Commandsell extends EssentialsCommand
} }
} }
//TODO: Prices for Enchantments
final ItemStack ris = new ItemStack(is.getType(), amount, is.getDurability()); final ItemStack ris = new ItemStack(is.getType(), amount, is.getDurability());
InventoryWorkaround.removeItem(user.getInventory(), true, ris); InventoryWorkaround.removeItem(user.getInventory(), true, ris);
user.updateInventory(); user.updateInventory();

View file

@ -1,27 +0,0 @@
package com.earth2me.essentials.craftbukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.craftbukkit.entity.CraftPlayer;
import org.bukkit.entity.Player;
public class BedLocationFix
{
/*
* Adds missing null pointer check to getHandle().getBed()
*/
public static Location getBedSpawnLocation(final Player player)
{
final CraftPlayer cplayer = (CraftPlayer)player;
final World world = player.getServer().getWorld(cplayer.getHandle().spawnWorld);
if (world != null && cplayer.getHandle().getBed() != null)
{
return new Location(world, cplayer.getHandle().getBed().x, cplayer.getHandle().getBed().y, cplayer.getHandle().getBed().z);
}
else
{
return null;
}
}
}

View file

@ -76,7 +76,9 @@ public class SignTrade extends EssentialsSign
amount -= amount % trade.getItemStack().getAmount(); amount -= amount % trade.getItemStack().getAmount();
if (amount > 0) if (amount > 0)
{ {
final Trade store = new Trade(new ItemStack(player.getItemInHand().getTypeId(), amount, player.getItemInHand().getDurability()), ess); final ItemStack stack = new ItemStack(player.getItemInHand().getTypeId(), amount, player.getItemInHand().getDurability());
stack.addEnchantments(player.getItemInHand().getEnchantments());
final Trade store = new Trade(stack, ess);
addAmount(sign, 2, store, ess); addAmount(sign, 2, store, ess);
store.charge(player); store.charge(player);
return store; return store;

View file

@ -1,10 +1,12 @@
package com.earth2me.essentials.storage; package com.earth2me.essentials.storage;
import java.util.Locale;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.material.MaterialData; import org.bukkit.material.MaterialData;
import org.yaml.snakeyaml.constructor.Constructor; import org.yaml.snakeyaml.constructor.Constructor;
@ -83,7 +85,7 @@ public class BukkitConstructor extends Constructor
{ {
return null; return null;
} }
final String[] split1 = val.split("\\W", 2); final String[] split1 = val.split("\\W");
if (split1.length == 0) if (split1.length == 0)
{ {
return null; return null;
@ -109,11 +111,42 @@ public class BukkitConstructor extends Constructor
data = Short.parseShort(split2[1]); data = Short.parseShort(split2[1]);
} }
int size = mat.getMaxStackSize(); int size = mat.getMaxStackSize();
if (split1.length == 2 && NUMPATTERN.matcher(split1[1]).matches()) if (split1.length > 1 && NUMPATTERN.matcher(split1[1]).matches())
{ {
size = Integer.parseInt(split1[1]); size = Integer.parseInt(split1[1]);
} }
return new ItemStack(mat, size, data); final ItemStack stack = new ItemStack(mat, size, data);
if (split1.length > 2)
{
for (int i = 2; i < split1.length; i++)
{
final String[] split3 = split1[0].split("[:+',;.]", 2);
if (split3.length != 2)
{
continue;
}
Enchantment enchantment;
if (NUMPATTERN.matcher(split3[0]).matches())
{
final int enchantId = Integer.parseInt(split3[0]);
enchantment = Enchantment.getById(enchantId);
}
else
{
enchantment = Enchantment.getByName(split3[0].toUpperCase(Locale.ENGLISH));
}
if (enchantment == null) {
continue;
}
int level = enchantment.getStartLevel();
if (NUMPATTERN.matcher(split3[1]).matches())
{
level = Integer.parseInt(split3[1]);
}
stack.addUnsafeEnchantment(enchantment, level);
}
}
return stack;
} }
return super.construct(node); return super.construct(node);
} }

View file

@ -5,6 +5,7 @@ import java.lang.reflect.Field;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.logging.Level; import java.util.logging.Level;
@ -12,6 +13,7 @@ import java.util.logging.Logger;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.material.MaterialData; import org.bukkit.material.MaterialData;
import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.Yaml;
@ -228,9 +230,22 @@ public class YamlStorageWriter implements IStorageWriter
else if (data instanceof ItemStack) else if (data instanceof ItemStack)
{ {
final ItemStack itemStack = (ItemStack)data; final ItemStack itemStack = (ItemStack)data;
writer.println(itemStack.getType().toString().toLowerCase() writer.print(itemStack.getType().toString().toLowerCase(Locale.ENGLISH));
+ (itemStack.getDurability() > 0 ? ":" + itemStack.getDurability() : "")
+ " " + itemStack.getAmount()); if (itemStack.getDurability() > 0)
{
writer.print(':');
writer.print(itemStack.getDurability());
}
writer.print(' ');
writer.print(itemStack.getAmount());
for (Entry<Enchantment, Integer> entry : itemStack.getEnchantments().entrySet())
{
writer.print(' ');
writer.print(entry.getKey().getName().toLowerCase(Locale.ENGLISH));
writer.print(':');
writer.print(entry.getValue());
}
} }
else else
{ {

View file

@ -3,7 +3,6 @@ package com.earth2me.essentials.spawn;
import static com.earth2me.essentials.I18n._; import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.IEssentials; import com.earth2me.essentials.IEssentials;
import com.earth2me.essentials.User; import com.earth2me.essentials.User;
import com.earth2me.essentials.craftbukkit.BedLocationFix;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.bukkit.Location; import org.bukkit.Location;
@ -32,7 +31,7 @@ public class EssentialsSpawnPlayerListener extends PlayerListener
Location home = user.getHome(user.getLocation()); Location home = user.getHome(user.getLocation());
if (home == null) if (home == null)
{ {
home = BedLocationFix.getBedSpawnLocation(user); home = user.getBedSpawnLocation();
} }
if (home != null) if (home != null)
{ {
@ -52,7 +51,7 @@ public class EssentialsSpawnPlayerListener extends PlayerListener
{ {
final User user = ess.getUser(event.getPlayer()); final User user = ess.getUser(event.getPlayer());
if (!user.isNew() || BedLocationFix.getBedSpawnLocation(user) != null) if (!user.isNew() || user.getBedSpawnLocation() != null)
{ {
return; return;
} }

Binary file not shown.

Binary file not shown.