Enchantements are now stored in one class

This commit is contained in:
snowleo 2011-12-04 22:28:29 +01:00
parent c8814bd70e
commit f12e9f6dfb
3 changed files with 116 additions and 129 deletions

View File

@ -0,0 +1,77 @@
package com.earth2me.essentials;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.regex.Pattern;
import org.bukkit.enchantments.Enchantment;
public class Enchantments
{
private static final transient Pattern NUMPATTERN = Pattern.compile("\\d+");
private static final Map<String, Enchantment> ENCHANTMENTS = new HashMap<String, Enchantment>();
static
{
ENCHANTMENTS.put("alldamage", Enchantment.DAMAGE_ALL);
ENCHANTMENTS.put("alldmg", Enchantment.DAMAGE_ALL);
ENCHANTMENTS.put("sharpness", Enchantment.DAMAGE_ALL);
ENCHANTMENTS.put("arthropodsdamage", Enchantment.DAMAGE_ARTHROPODS);
ENCHANTMENTS.put("ardmg", Enchantment.DAMAGE_ARTHROPODS);
ENCHANTMENTS.put("baneofarthropods", Enchantment.DAMAGE_ARTHROPODS);
ENCHANTMENTS.put("undeaddamage", Enchantment.DAMAGE_UNDEAD);
ENCHANTMENTS.put("smite", Enchantment.DAMAGE_UNDEAD);
ENCHANTMENTS.put("digspeed", Enchantment.DIG_SPEED);
ENCHANTMENTS.put("efficiency", Enchantment.DIG_SPEED);
ENCHANTMENTS.put("durability", Enchantment.DURABILITY);
ENCHANTMENTS.put("dura", Enchantment.DURABILITY);
ENCHANTMENTS.put("unbreaking", Enchantment.DURABILITY);
ENCHANTMENTS.put("fireaspect", Enchantment.FIRE_ASPECT);
ENCHANTMENTS.put("fire", Enchantment.FIRE_ASPECT);
ENCHANTMENTS.put("knockback", Enchantment.KNOCKBACK);
ENCHANTMENTS.put("blockslootbonus", Enchantment.LOOT_BONUS_BLOCKS);
ENCHANTMENTS.put("fortune", Enchantment.LOOT_BONUS_BLOCKS);
ENCHANTMENTS.put("mobslootbonus", Enchantment.LOOT_BONUS_MOBS);
ENCHANTMENTS.put("mobloot", Enchantment.LOOT_BONUS_MOBS);
ENCHANTMENTS.put("looting", Enchantment.LOOT_BONUS_MOBS);
ENCHANTMENTS.put("oxygen", Enchantment.OXYGEN);
ENCHANTMENTS.put("respiration", Enchantment.OXYGEN);
ENCHANTMENTS.put("protection", Enchantment.PROTECTION_ENVIRONMENTAL);
ENCHANTMENTS.put("prot", Enchantment.PROTECTION_ENVIRONMENTAL);
ENCHANTMENTS.put("explosionsprotection", Enchantment.PROTECTION_EXPLOSIONS);
ENCHANTMENTS.put("expprot", Enchantment.PROTECTION_EXPLOSIONS);
ENCHANTMENTS.put("blastprotection", Enchantment.PROTECTION_EXPLOSIONS);
ENCHANTMENTS.put("fallprotection", Enchantment.PROTECTION_FALL);
ENCHANTMENTS.put("fallprot", Enchantment.PROTECTION_FALL);
ENCHANTMENTS.put("featherfalling", Enchantment.PROTECTION_FALL);
ENCHANTMENTS.put("fireprotection", Enchantment.PROTECTION_FIRE);
ENCHANTMENTS.put("fireprot", Enchantment.PROTECTION_FIRE);
ENCHANTMENTS.put("projectileprotection", Enchantment.PROTECTION_PROJECTILE);
ENCHANTMENTS.put("projprot", Enchantment.PROTECTION_PROJECTILE);
ENCHANTMENTS.put("silktouch", Enchantment.SILK_TOUCH);
ENCHANTMENTS.put("waterworker", Enchantment.WATER_WORKER);
ENCHANTMENTS.put("aquaaffinity", Enchantment.WATER_WORKER);
}
public static Enchantment getByName(String name) {
Enchantment enchantment;
if (NUMPATTERN.matcher(name).matches()) {
enchantment = Enchantment.getById(Integer.parseInt(name));
} else {
enchantment = Enchantment.getByName(name.toUpperCase(Locale.ENGLISH));
}
if (enchantment == null)
{
enchantment = ENCHANTMENTS.get(name.toLowerCase(Locale.ENGLISH));
}
return enchantment;
}
public static Set<Entry<String, Enchantment>> entrySet()
{
return ENCHANTMENTS.entrySet();
}
}

View File

@ -1,10 +1,10 @@
package com.earth2me.essentials.commands; package com.earth2me.essentials.commands;
import com.earth2me.essentials.Enchantments;
import static com.earth2me.essentials.I18n._; import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.User; import com.earth2me.essentials.User;
import com.earth2me.essentials.Util; import com.earth2me.essentials.Util;
import java.util.*; import java.util.*;
import java.util.regex.Pattern;
import org.bukkit.Server; import org.bukkit.Server;
import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@ -12,57 +12,11 @@ import org.bukkit.inventory.ItemStack;
public class Commandenchant extends EssentialsCommand public class Commandenchant extends EssentialsCommand
{ {
private static final Map<String, Enchantment> ENCHANTMENTS = new HashMap<String, Enchantment>();
private static final transient Pattern NUMPATTERN = Pattern.compile("\\d+");
static
{
ENCHANTMENTS.put("alldamage", Enchantment.DAMAGE_ALL);
ENCHANTMENTS.put("alldmg", Enchantment.DAMAGE_ALL);
ENCHANTMENTS.put("sharpness", Enchantment.DAMAGE_ALL);
ENCHANTMENTS.put("arthropodsdamage", Enchantment.DAMAGE_ARTHROPODS);
ENCHANTMENTS.put("ardmg", Enchantment.DAMAGE_ARTHROPODS);
ENCHANTMENTS.put("baneofarthropods", Enchantment.DAMAGE_ARTHROPODS);
ENCHANTMENTS.put("undeaddamage", Enchantment.DAMAGE_UNDEAD);
ENCHANTMENTS.put("smite", Enchantment.DAMAGE_UNDEAD);
ENCHANTMENTS.put("digspeed", Enchantment.DIG_SPEED);
ENCHANTMENTS.put("efficiency", Enchantment.DIG_SPEED);
ENCHANTMENTS.put("durability", Enchantment.DURABILITY);
ENCHANTMENTS.put("dura", Enchantment.DURABILITY);
ENCHANTMENTS.put("unbreaking", Enchantment.DURABILITY);
ENCHANTMENTS.put("fireaspect", Enchantment.FIRE_ASPECT);
ENCHANTMENTS.put("fire", Enchantment.FIRE_ASPECT);
ENCHANTMENTS.put("knockback", Enchantment.KNOCKBACK);
ENCHANTMENTS.put("blockslootbonus", Enchantment.LOOT_BONUS_BLOCKS);
ENCHANTMENTS.put("fortune", Enchantment.LOOT_BONUS_BLOCKS);
ENCHANTMENTS.put("mobslootbonus", Enchantment.LOOT_BONUS_MOBS);
ENCHANTMENTS.put("mobloot", Enchantment.LOOT_BONUS_MOBS);
ENCHANTMENTS.put("looting", Enchantment.LOOT_BONUS_MOBS);
ENCHANTMENTS.put("oxygen", Enchantment.OXYGEN);
ENCHANTMENTS.put("respiration", Enchantment.OXYGEN);
ENCHANTMENTS.put("protection", Enchantment.PROTECTION_ENVIRONMENTAL);
ENCHANTMENTS.put("prot", Enchantment.PROTECTION_ENVIRONMENTAL);
ENCHANTMENTS.put("explosionsprotection", Enchantment.PROTECTION_EXPLOSIONS);
ENCHANTMENTS.put("expprot", Enchantment.PROTECTION_EXPLOSIONS);
ENCHANTMENTS.put("blastprotection", Enchantment.PROTECTION_EXPLOSIONS);
ENCHANTMENTS.put("fallprotection", Enchantment.PROTECTION_FALL);
ENCHANTMENTS.put("fallprot", Enchantment.PROTECTION_FALL);
ENCHANTMENTS.put("featherfalling", Enchantment.PROTECTION_FALL);
ENCHANTMENTS.put("fireprotection", Enchantment.PROTECTION_FIRE);
ENCHANTMENTS.put("fireprot", Enchantment.PROTECTION_FIRE);
ENCHANTMENTS.put("projectileprotection", Enchantment.PROTECTION_PROJECTILE);
ENCHANTMENTS.put("projprot", Enchantment.PROTECTION_PROJECTILE);
ENCHANTMENTS.put("silktouch", Enchantment.SILK_TOUCH);
ENCHANTMENTS.put("waterworker", Enchantment.WATER_WORKER);
ENCHANTMENTS.put("aquaaffinity", Enchantment.WATER_WORKER);
}
public Commandenchant() public Commandenchant()
{ {
super("enchant"); super("enchant");
} }
//TODO: Implement charge costs: final Trade charge = new Trade("enchant-" + enchantmentName, ess); //TODO: Implement charge costs: final Trade charge = new Trade("enchant-" + enchantmentName, ess);
@Override @Override
protected void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception protected void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception
@ -75,7 +29,7 @@ public class Commandenchant extends EssentialsCommand
if (args.length == 0) if (args.length == 0)
{ {
final Set<String> enchantmentslist = new TreeSet<String>(); final Set<String> enchantmentslist = new TreeSet<String>();
for (Map.Entry<String, Enchantment> entry : ENCHANTMENTS.entrySet()) for (Map.Entry<String, Enchantment> entry : Enchantments.entrySet())
{ {
final String enchantmentName = entry.getValue().getName().toLowerCase(Locale.ENGLISH); final String enchantmentName = entry.getValue().getName().toLowerCase(Locale.ENGLISH);
if (enchantmentslist.contains(enchantmentName) || user.isAuthorized("essentials.enchant." + enchantmentName)) if (enchantmentslist.contains(enchantmentName) || user.isAuthorized("essentials.enchant." + enchantmentName))
@ -98,39 +52,36 @@ public class Commandenchant extends EssentialsCommand
level = -1; level = -1;
} }
} }
Enchantment enchantment = getEnchantment(args[0], user); final Enchantment enchantment = getEnchantment(args[0], user);
if (level < 0 || level > enchantment.getMaxLevel()) if (level < 0 || level > enchantment.getMaxLevel())
{ {
level = enchantment.getMaxLevel(); level = enchantment.getMaxLevel();
} }
if (level == 0) { if (level == 0)
{
stack.removeEnchantment(enchantment); stack.removeEnchantment(enchantment);
} else { }
else
{
stack.addEnchantment(enchantment, level); stack.addEnchantment(enchantment, level);
} }
user.getInventory().setItemInHand(stack); user.getInventory().setItemInHand(stack);
user.updateInventory(); user.updateInventory();
final String enchantmentName = enchantment.getName().toLowerCase(Locale.ENGLISH); final String enchantmentName = enchantment.getName().toLowerCase(Locale.ENGLISH);
if (level == 0) { if (level == 0)
{
user.sendMessage(_("enchantmentRemoved", enchantmentName.replace('_', ' '))); user.sendMessage(_("enchantmentRemoved", enchantmentName.replace('_', ' ')));
} else { }
else
{
user.sendMessage(_("enchantmentApplied", enchantmentName.replace('_', ' '))); user.sendMessage(_("enchantmentApplied", enchantmentName.replace('_', ' ')));
} }
} }
public static Enchantment getEnchantment(final String name, final User user) throws Exception public static Enchantment getEnchantment(final String name, final User user) throws Exception
{ {
Enchantment enchantment; final Enchantment enchantment = Enchantments.getByName(name);
if (NUMPATTERN.matcher(name).matches()) {
enchantment = Enchantment.getById(Integer.parseInt(name));
} else {
enchantment = Enchantment.getByName(name.toUpperCase(Locale.ENGLISH));
}
if (enchantment == null)
{
enchantment = ENCHANTMENTS.get(name.toLowerCase(Locale.ENGLISH));
}
if (enchantment == null) if (enchantment == null)
{ {
throw new Exception(_("enchantmentNotFound")); throw new Exception(_("enchantmentNotFound"));

View File

@ -1,12 +1,11 @@
package com.earth2me.essentials.signs; package com.earth2me.essentials.signs;
import com.earth2me.essentials.ChargeException; import com.earth2me.essentials.ChargeException;
import com.earth2me.essentials.Enchantments;
import com.earth2me.essentials.IEssentials; import com.earth2me.essentials.IEssentials;
import com.earth2me.essentials.User; import com.earth2me.essentials.User;
import static com.earth2me.essentials.I18n._; import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.Trade; import com.earth2me.essentials.Trade;
import java.util.HashMap;
import java.util.Map;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@ -23,12 +22,13 @@ public class SignEnchant extends EssentialsSign
protected boolean onSignCreate(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException protected boolean onSignCreate(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException
{ {
getItemStack(sign.getLine(1), 1, ess); getItemStack(sign.getLine(1), 1, ess);
String[] enchantLevel = sign.getLine(2).split(":"); final String[] enchantLevel = sign.getLine(2).split(":");
if (enchantLevel.length != 2) if (enchantLevel.length != 2)
{ {
throw new SignException(_("invalidSignLine", 2)); throw new SignException(_("invalidSignLine", 2));
} }
if (!ENCHANTMENTS.containsKey(enchantLevel[0])) final Enchantment enchantment = Enchantments.getByName(enchantLevel[0]);
if (enchantment == null)
{ {
throw new SignException(_("enchantmentNotFound")); throw new SignException(_("enchantmentNotFound"));
} }
@ -41,9 +41,9 @@ public class SignEnchant extends EssentialsSign
{ {
throw new SignException(ex.getMessage()); throw new SignException(ex.getMessage());
} }
if (level < 1 || level > ENCHANTMENTS.get(enchantLevel[0]).getMaxLevel()) if (level < 1 || level > enchantment.getMaxLevel())
{ {
sign.setLine(2, enchantLevel[0] + ":" + ENCHANTMENTS.get(enchantLevel[0]).getMaxLevel()); sign.setLine(2, enchantLevel[0] + ":" + enchantment.getMaxLevel());
} }
getTrade(sign, 3, ess); getTrade(sign, 3, ess);
return true; return true;
@ -52,9 +52,9 @@ public class SignEnchant extends EssentialsSign
@Override @Override
protected boolean onSignInteract(ISign sign, User player, String username, IEssentials ess) throws SignException, ChargeException protected boolean onSignInteract(ISign sign, User player, String username, IEssentials ess) throws SignException, ChargeException
{ {
Material search = getItemStack(sign.getLine(1), 1, ess).getType(); final Material search = getItemStack(sign.getLine(1), 1, ess).getType();
int slot; int slot;
Trade charge = getTrade(sign, 3, ess); final Trade charge = getTrade(sign, 3, ess);
charge.isAffordableFor(player); charge.isAffordableFor(player);
if (player.getInventory().contains(search)) if (player.getInventory().contains(search))
{ {
@ -65,74 +65,33 @@ public class SignEnchant extends EssentialsSign
player.sendMessage(_("missingItems", 1, search.toString())); player.sendMessage(_("missingItems", 1, search.toString()));
return true; return true;
} }
ItemStack toEnchant = player.getInventory().getItem(slot); final String[] enchantLevel = sign.getLine(2).split(":");
String[] enchantLevel = sign.getLine(2).split(":");
if (enchantLevel.length != 2) if (enchantLevel.length != 2)
{ {
player.sendMessage(_("invalidSignLine", 2)); player.sendMessage(_("invalidSignLine", 2));
return true; return true;
} }
if (ENCHANTMENTS.containsKey(enchantLevel[0])) final Enchantment enchantment = Enchantments.getByName(enchantLevel[0]);
{ if (enchantment == null)
try
{
toEnchant.addEnchantment(ENCHANTMENTS.get(enchantLevel[0]), Integer.parseInt(enchantLevel[1]));
}
catch (NumberFormatException ex)
{
toEnchant.addEnchantment(ENCHANTMENTS.get(enchantLevel[0]), 1);
}
}
else
{ {
player.sendMessage(_("enchantmentNotFound")); player.sendMessage(_("enchantmentNotFound"));
return true; return true;
} }
final ItemStack toEnchant = player.getInventory().getItem(slot);
try
{
toEnchant.addEnchantment(enchantment, Integer.parseInt(enchantLevel[1]));
}
catch (NumberFormatException ex)
{
toEnchant.addEnchantment(enchantment, enchantment.getMaxLevel());
}
charge.charge(player); charge.charge(player);
Trade.log("Sign", "Enchant", "Interact", username, charge, username, charge, sign.getBlock().getLocation(), ess); Trade.log("Sign", "Enchant", "Interact", username, charge, username, charge, sign.getBlock().getLocation(), ess);
player.getInventory().setItem(slot, toEnchant);
player.updateInventory();
return true; return true;
} }
private static final Map<String, Enchantment> ENCHANTMENTS = new HashMap<String, Enchantment>();
static
{
ENCHANTMENTS.put("alldamage", Enchantment.DAMAGE_ALL);
ENCHANTMENTS.put("alldmg", Enchantment.DAMAGE_ALL);
ENCHANTMENTS.put("sharpness", Enchantment.DAMAGE_ALL);
ENCHANTMENTS.put("arthropodsdamage", Enchantment.DAMAGE_ARTHROPODS);
ENCHANTMENTS.put("ardmg", Enchantment.DAMAGE_ARTHROPODS);
ENCHANTMENTS.put("baneofarthropods", Enchantment.DAMAGE_ARTHROPODS);
ENCHANTMENTS.put("undeaddamage", Enchantment.DAMAGE_UNDEAD);
ENCHANTMENTS.put("smite", Enchantment.DAMAGE_UNDEAD);
ENCHANTMENTS.put("digspeed", Enchantment.DIG_SPEED);
ENCHANTMENTS.put("efficiency", Enchantment.DIG_SPEED);
ENCHANTMENTS.put("durability", Enchantment.DURABILITY);
ENCHANTMENTS.put("dura", Enchantment.DURABILITY);
ENCHANTMENTS.put("unbreaking", Enchantment.DURABILITY);
ENCHANTMENTS.put("fireaspect", Enchantment.FIRE_ASPECT);
ENCHANTMENTS.put("fire", Enchantment.FIRE_ASPECT);
ENCHANTMENTS.put("knockback", Enchantment.KNOCKBACK);
ENCHANTMENTS.put("blockslootbonus", Enchantment.LOOT_BONUS_BLOCKS);
ENCHANTMENTS.put("fortune", Enchantment.LOOT_BONUS_BLOCKS);
ENCHANTMENTS.put("mobslootbonus", Enchantment.LOOT_BONUS_MOBS);
ENCHANTMENTS.put("mobloot", Enchantment.LOOT_BONUS_MOBS);
ENCHANTMENTS.put("looting", Enchantment.LOOT_BONUS_MOBS);
ENCHANTMENTS.put("oxygen", Enchantment.OXYGEN);
ENCHANTMENTS.put("respiration", Enchantment.OXYGEN);
ENCHANTMENTS.put("protection", Enchantment.PROTECTION_ENVIRONMENTAL);
ENCHANTMENTS.put("prot", Enchantment.PROTECTION_ENVIRONMENTAL);
ENCHANTMENTS.put("explosionsprotection", Enchantment.PROTECTION_EXPLOSIONS);
ENCHANTMENTS.put("expprot", Enchantment.PROTECTION_EXPLOSIONS);
ENCHANTMENTS.put("blastprotection", Enchantment.PROTECTION_EXPLOSIONS);
ENCHANTMENTS.put("fallprotection", Enchantment.PROTECTION_FALL);
ENCHANTMENTS.put("fallprot", Enchantment.PROTECTION_FALL);
ENCHANTMENTS.put("featherfalling", Enchantment.PROTECTION_FALL);
ENCHANTMENTS.put("fireprotection", Enchantment.PROTECTION_FIRE);
ENCHANTMENTS.put("fireprot", Enchantment.PROTECTION_FIRE);
ENCHANTMENTS.put("projectileprotection", Enchantment.PROTECTION_PROJECTILE);
ENCHANTMENTS.put("projprot", Enchantment.PROTECTION_PROJECTILE);
ENCHANTMENTS.put("silktouch", Enchantment.SILK_TOUCH);
ENCHANTMENTS.put("waterworker", Enchantment.WATER_WORKER);
ENCHANTMENTS.put("aquaaffinity", Enchantment.WATER_WORKER);
}
} }