From 9c56b147ee830894ded7e3aa86b8ada64cf2943c Mon Sep 17 00:00:00 2001 From: snowleo Date: Sun, 27 Nov 2011 08:18:41 +0100 Subject: [PATCH] Enchantment Multiplier in future Worth config --- .../earth2me/essentials/settings/Worth.java | 5 + .../essentials/storage/BukkitConstructor.java | 60 +++++++++- .../essentials/storage/EnchantmentLevel.java | 57 ++++++++++ .../essentials/storage/YamlStorageWriter.java | 103 +++++++++++------- 4 files changed, 187 insertions(+), 38 deletions(-) create mode 100644 Essentials/src/com/earth2me/essentials/storage/EnchantmentLevel.java diff --git a/Essentials/src/com/earth2me/essentials/settings/Worth.java b/Essentials/src/com/earth2me/essentials/settings/Worth.java index 939cdd883..707f907cf 100644 --- a/Essentials/src/com/earth2me/essentials/settings/Worth.java +++ b/Essentials/src/com/earth2me/essentials/settings/Worth.java @@ -1,5 +1,6 @@ package com.earth2me.essentials.settings; +import com.earth2me.essentials.storage.EnchantmentLevel; import com.earth2me.essentials.storage.MapKeyType; import com.earth2me.essentials.storage.MapValueType; import com.earth2me.essentials.storage.StorageObject; @@ -8,6 +9,7 @@ import java.util.Map; import lombok.Data; import lombok.EqualsAndHashCode; import org.bukkit.Material; +import org.bukkit.enchantments.Enchantment; import org.bukkit.material.MaterialData; @@ -21,6 +23,9 @@ public class Worth implements StorageObject @MapKeyType(MaterialData.class) @MapValueType(Double.class) private Map buy = new HashMap(); + @MapKeyType(EnchantmentLevel.class) + @MapValueType(Double.class) + private Map enchantmentMultiplier = new HashMap(); public Worth() { diff --git a/Essentials/src/com/earth2me/essentials/storage/BukkitConstructor.java b/Essentials/src/com/earth2me/essentials/storage/BukkitConstructor.java index 6d7066c58..c48204a06 100644 --- a/Essentials/src/com/earth2me/essentials/storage/BukkitConstructor.java +++ b/Essentials/src/com/earth2me/essentials/storage/BukkitConstructor.java @@ -71,6 +71,10 @@ public class BukkitConstructor extends Constructor { mat = Material.matchMaterial(split[0]); } + if (mat == null) + { + return null; + } byte data = 0; if (split.length == 2 && NUMPATTERN.matcher(split[1]).matches()) { @@ -105,6 +109,10 @@ public class BukkitConstructor extends Constructor { mat = Material.matchMaterial(split2[0]); } + if (mat == null) + { + return null; + } short data = 0; if (split2.length == 2 && NUMPATTERN.matcher(split2[1]).matches()) { @@ -135,7 +143,8 @@ public class BukkitConstructor extends Constructor { enchantment = Enchantment.getByName(split3[0].toUpperCase(Locale.ENGLISH)); } - if (enchantment == null) { + if (enchantment == null) + { continue; } int level = enchantment.getStartLevel(); @@ -143,11 +152,60 @@ public class BukkitConstructor extends Constructor { level = Integer.parseInt(split3[1]); } + if (level < enchantment.getStartLevel()) + { + level = enchantment.getStartLevel(); + } + if (level > enchantment.getMaxLevel()) + { + level = enchantment.getMaxLevel(); + } stack.addUnsafeEnchantment(enchantment, level); } } return stack; } + if (node.getType().equals(EnchantmentLevel.class)) + { + final String val = (String)constructScalar((ScalarNode)node); + if (val.isEmpty()) + { + return null; + } + final String[] split = val.split("[:+',;.]", 2); + if (split.length == 0) + { + return null; + } + Enchantment enchant; + if (NUMPATTERN.matcher(split[0]).matches()) + { + final int typeId = Integer.parseInt(split[0]); + enchant = Enchantment.getById(typeId); + } + else + { + enchant = Enchantment.getByName(split[0].toUpperCase(Locale.ENGLISH)); + } + if (enchant == null) + { + return null; + } + int level = enchant.getStartLevel(); + if (split.length == 2 && NUMPATTERN.matcher(split[1]).matches()) + { + level = Integer.parseInt(split[1]); + } + if (level < enchant.getStartLevel()) + { + level = enchant.getStartLevel(); + } + if (level > enchant.getMaxLevel()) + { + level = enchant.getMaxLevel(); + } + return new EnchantmentLevel(enchant, level); + } return super.construct(node); } } diff --git a/Essentials/src/com/earth2me/essentials/storage/EnchantmentLevel.java b/Essentials/src/com/earth2me/essentials/storage/EnchantmentLevel.java new file mode 100644 index 000000000..0a0299ed3 --- /dev/null +++ b/Essentials/src/com/earth2me/essentials/storage/EnchantmentLevel.java @@ -0,0 +1,57 @@ +package com.earth2me.essentials.storage; + +import java.util.Map.Entry; +import org.bukkit.enchantments.Enchantment; + + +public class EnchantmentLevel implements Entry +{ + private Enchantment enchantment; + private int level; + + public EnchantmentLevel(Enchantment enchantment, int level) + { + this.enchantment = enchantment; + this.level = level; + } + + public Enchantment getEnchantment() + { + return enchantment; + } + + public void setEnchantment(Enchantment enchantment) + { + this.enchantment = enchantment; + } + + public int getLevel() + { + return level; + } + + public void setLevel(int level) + { + this.level = level; + } + + @Override + public Enchantment getKey() + { + return enchantment; + } + + @Override + public Integer getValue() + { + return level; + } + + @Override + public Integer setValue(Integer v) + { + int t = level; + level = v; + return t; + } +} diff --git a/Essentials/src/com/earth2me/essentials/storage/YamlStorageWriter.java b/Essentials/src/com/earth2me/essentials/storage/YamlStorageWriter.java index c260b3bde..48540483d 100644 --- a/Essentials/src/com/earth2me/essentials/storage/YamlStorageWriter.java +++ b/Essentials/src/com/earth2me/essentials/storage/YamlStorageWriter.java @@ -24,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 @@ -45,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()) @@ -54,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)) { @@ -85,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); @@ -109,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); @@ -131,7 +131,7 @@ public class YamlStorageWriter implements IStorageWriter } return commentPresent; } - + private void writeCollection(final Collection data, final int depth) throws IllegalAccessException { writer.println(); @@ -162,7 +162,7 @@ public class YamlStorageWriter implements IStorageWriter } writer.println(); } - + private void writeMap(final Map data, final int depth) throws IllegalArgumentException, IllegalAccessException { writer.println(); @@ -199,7 +199,7 @@ public class YamlStorageWriter implements IStorageWriter } } } - + private void writeIndention(final int depth) { for (int i = 0; i < depth; i++) @@ -207,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) @@ -219,40 +219,30 @@ public class YamlStorageWriter implements IStorageWriter } else if (data instanceof Material) { - writer.println(data.toString().toLowerCase(Locale.ENGLISH)); + writeMaterial(data); + writer.println(); } else if (data instanceof MaterialData) { - final MaterialData matData = (MaterialData)data; - writer.println(matData.getItemType().toString().toLowerCase(Locale.ENGLISH) - + (matData.getData() > 0 ? ":" + matData.getData() : "")); + writeMaterialData(data); + writer.println(); } else if (data instanceof ItemStack) { - final ItemStack itemStack = (ItemStack)data; - 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 entry : itemStack.getEnchantments().entrySet()) - { - writer.print(' '); - writer.print(entry.getKey().getName().toLowerCase(Locale.ENGLISH)); - writer.print(':'); - writer.print(entry.getValue()); - } + writeItemStack(data); + writer.println(); + } + else if (data instanceof EnchantmentLevel) + { + writeEnchantmentLevel(data); + writer.println(); } else { throw new UnsupportedOperationException(); } } - + private void writeKey(final Object data) { if (data instanceof String || data instanceof Boolean || data instanceof Number) @@ -271,20 +261,59 @@ public class YamlStorageWriter implements IStorageWriter } else if (data instanceof Material) { - writer.print(data.toString().toLowerCase(Locale.ENGLISH)); + writeMaterial(data); } else if (data instanceof MaterialData) { - final MaterialData matData = (MaterialData)data; - writer.print(matData.getItemType().toString().toLowerCase(Locale.ENGLISH) - + (matData.getData() > 0 ? ":" + matData.getData() : "")); + writeMaterialData(data); + } + else if (data instanceof EnchantmentLevel) + { + writeEnchantmentLevel(data); } else { throw new UnsupportedOperationException(); } } - + + private void writeMaterial(final Object data) + { + writer.print(data.toString().toLowerCase(Locale.ENGLISH)); + } + + private void writeMaterialData(final Object data) + { + final MaterialData matData = (MaterialData)data; + writeMaterial(matData.getItemType()); + if (matData.getData() > 0) + { + writer.print(':'); + writer.print(matData.getData()); + } + } + + private void writeItemStack(final Object data) + { + final ItemStack itemStack = (ItemStack)data; + writeMaterialData(itemStack.getData()); + writer.print(' '); + writer.print(itemStack.getAmount()); + for (Entry entry : itemStack.getEnchantments().entrySet()) + { + writer.print(' '); + writeEnchantmentLevel(entry); + } + } + + private void writeEnchantmentLevel(Object data) + { + final Entry enchLevel = (Entry)data; + writer.print(enchLevel.getKey().getName().toLowerCase(Locale.ENGLISH)); + writer.print(':'); + writer.print(enchLevel.getValue()); + } + private void writeLocation(final Location entry, final int depth) { writer.println();