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 static final int BUKKIT_VERSION = 1512;
public static final int BUKKIT_VERSION = 1518;
private static final Logger LOGGER = Logger.getLogger("Minecraft");
private transient ISettings settings;
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 java.io.*;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -10,6 +12,7 @@ import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.config.Configuration;
@ -224,13 +227,25 @@ public class EssentialsConf extends Configuration
public ItemStack getItemStack(final String path)
{
return new ItemStack(
final ItemStack stack = new ItemStack(
Material.valueOf(getString(path + ".type", "AIR")),
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)
*/);
*/
}
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("amount", stack.getAmount());
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
//map.put("data", stack.getDurability());
setProperty(path, map);

View File

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

View File

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

View File

@ -20,6 +20,7 @@ public class FakeInventory implements Inventory
continue;
}
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;
}
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;
}
@ -56,7 +56,7 @@ public final class InventoryWorkaround
{
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;
}
@ -102,9 +102,10 @@ public final class InventoryWorkaround
if (combined[j] == null)
{
combined[j] = new ItemStack(items[i].getType(), items[i].getAmount(), items[i].getDurability());
combined[j].addEnchantments(items[i].getEnchantments());
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());
break;
@ -143,7 +144,9 @@ public final class InventoryWorkaround
// More than a single stack!
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());
}
else
@ -257,9 +260,10 @@ public final class InventoryWorkaround
if (combined[j] == null)
{
combined[j] = new ItemStack(items[i].getType(), items[i].getAmount(), items[i].getDurability());
combined[j].addEnchantments(items[i].getEnchantments());
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());
break;
@ -318,14 +322,18 @@ public final class InventoryWorkaround
final int maxStackSize = itm.getType().getMaxStackSize();
final int stacks = 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++)
{
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)
{
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;
}

View File

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

View File

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

View File

@ -1,10 +1,12 @@
package com.earth2me.essentials.storage;
import java.util.Locale;
import java.util.regex.Pattern;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import org.bukkit.material.MaterialData;
import org.yaml.snakeyaml.constructor.Constructor;
@ -83,7 +85,7 @@ public class BukkitConstructor extends Constructor
{
return null;
}
final String[] split1 = val.split("\\W", 2);
final String[] split1 = val.split("\\W");
if (split1.length == 0)
{
return null;
@ -109,11 +111,42 @@ public class BukkitConstructor extends Constructor
data = Short.parseShort(split2[1]);
}
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]);
}
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);
}

View File

@ -5,6 +5,7 @@ import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Collection;
import java.util.Collections;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.logging.Level;
@ -12,6 +13,7 @@ import java.util.logging.Logger;
import java.util.regex.Pattern;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import org.bukkit.material.MaterialData;
import org.yaml.snakeyaml.Yaml;
@ -22,12 +24,12 @@ public class YamlStorageWriter implements IStorageWriter
private transient static final Pattern NON_WORD_PATTERN = Pattern.compile("\\W");
private transient final PrintWriter writer;
private transient static final Yaml YAML = new Yaml();
public YamlStorageWriter(final PrintWriter writer)
{
this.writer = writer;
}
public void save(final StorageObject object)
{
try
@ -43,7 +45,7 @@ public class YamlStorageWriter implements IStorageWriter
Logger.getLogger(YamlStorageWriter.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void writeToFile(final Object object, final int depth, final Class clazz) throws IllegalAccessException
{
for (Field field : clazz.getDeclaredFields())
@ -52,7 +54,7 @@ public class YamlStorageWriter implements IStorageWriter
if (Modifier.isPrivate(modifier) && !Modifier.isTransient(modifier) && !Modifier.isStatic(modifier))
{
field.setAccessible(true);
final Object data = field.get(object);
if (writeKey(field, depth, data))
{
@ -83,7 +85,7 @@ public class YamlStorageWriter implements IStorageWriter
}
}
}
private boolean writeKey(final Field field, final int depth, final Object data)
{
final boolean commentPresent = writeComment(field, depth);
@ -107,7 +109,7 @@ public class YamlStorageWriter implements IStorageWriter
}
return false;
}
private boolean writeComment(final Field field, final int depth)
{
final boolean commentPresent = field.isAnnotationPresent(Comment.class);
@ -129,7 +131,7 @@ public class YamlStorageWriter implements IStorageWriter
}
return commentPresent;
}
private void writeCollection(final Collection<Object> data, final int depth) throws IllegalAccessException
{
writer.println();
@ -160,7 +162,7 @@ public class YamlStorageWriter implements IStorageWriter
}
writer.println();
}
private void writeMap(final Map<Object, Object> data, final int depth) throws IllegalArgumentException, IllegalAccessException
{
writer.println();
@ -197,7 +199,7 @@ public class YamlStorageWriter implements IStorageWriter
}
}
}
private void writeIndention(final int depth)
{
for (int i = 0; i < depth; i++)
@ -205,7 +207,7 @@ public class YamlStorageWriter implements IStorageWriter
writer.print(" ");
}
}
private void writeScalar(final Object data)
{
if (data instanceof String || data instanceof Boolean || data instanceof Number)
@ -228,16 +230,29 @@ public class YamlStorageWriter implements IStorageWriter
else if (data instanceof ItemStack)
{
final ItemStack itemStack = (ItemStack)data;
writer.println(itemStack.getType().toString().toLowerCase()
+ (itemStack.getDurability() > 0 ? ":" + itemStack.getDurability() : "")
+ " " + itemStack.getAmount());
writer.print(itemStack.getType().toString().toLowerCase(Locale.ENGLISH));
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
{
throw new UnsupportedOperationException();
}
}
private void writeKey(final Object data)
{
if (data instanceof String || data instanceof Boolean || data instanceof Number)
@ -269,7 +284,7 @@ public class YamlStorageWriter implements IStorageWriter
throw new UnsupportedOperationException();
}
}
private void writeLocation(final Location entry, final int depth)
{
writer.println();

View File

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

Binary file not shown.

Binary file not shown.