mirror of https://github.com/taoneill/war.git
Unsafe/illegal enchantments are now supported
Closes gh-461. Closes gh-429. Items enchanted with Time the Enchanter to high levels now get managed and persisted by War properly.
This commit is contained in:
parent
bdc8b4057c
commit
41ecb512ed
|
@ -338,11 +338,29 @@ public class War extends JavaPlugin {
|
|||
public ItemStack copyStack(ItemStack originalStack) {
|
||||
ItemStack copiedStack = new ItemStack(originalStack.getType(), originalStack.getAmount(), originalStack.getDurability(), new Byte(originalStack.getData().getData()));
|
||||
copiedStack.setDurability(originalStack.getDurability());
|
||||
for (Enchantment enchantment : originalStack.getEnchantments().keySet()) {
|
||||
copiedStack.addEnchantment(Enchantment.getById(enchantment.getId()), originalStack.getEnchantments().get(enchantment));
|
||||
}
|
||||
copyEnchantments(originalStack, copiedStack);
|
||||
|
||||
return copiedStack;
|
||||
}
|
||||
|
||||
public void copyEnchantments(ItemStack originalStack, ItemStack copiedStack) {
|
||||
for (Enchantment enchantment : originalStack.getEnchantments().keySet()) {
|
||||
int level = originalStack.getEnchantments().get(enchantment);
|
||||
safelyEnchant(copiedStack, enchantment, level);
|
||||
}
|
||||
}
|
||||
|
||||
public void safelyEnchant(ItemStack target, Enchantment enchantment, int level) {
|
||||
try {
|
||||
if (level > enchantment.getMaxLevel()) {
|
||||
target.addUnsafeEnchantment(Enchantment.getById(enchantment.getId()), level);
|
||||
} else {
|
||||
target.addEnchantment(Enchantment.getById(enchantment.getId()), level);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
War.war.log("Failed to apply enchantment id:" + enchantment.getId() + " level:" + level, Level.WARNING);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the player-inventory to a loadout hashmap
|
||||
|
|
|
@ -2,12 +2,14 @@ package com.tommytony.war.event;
|
|||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Effect;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.block.BlockFace;
|
||||
import org.bukkit.enchantments.Enchantment;
|
||||
import org.bukkit.entity.Item;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.Event.Result;
|
||||
|
@ -228,9 +230,8 @@ public class WarPlayerListener implements Listener {
|
|||
ItemStack inHand = event.getItem();
|
||||
|
||||
if (inHand != null) {
|
||||
ItemStack newItemInHand = new ItemStack(inHand.getType(), inHand.getAmount(), inHand.getDurability(), inHand.getData().getData());
|
||||
newItemInHand.setDurability(inHand.getDurability());
|
||||
newItemInHand.addEnchantments(inHand.getEnchantments());
|
||||
ItemStack newItemInHand = War.war.copyStack(inHand);
|
||||
|
||||
event.getPlayer().setItemInHand(newItemInHand);
|
||||
event.setCancelled(true);
|
||||
|
||||
|
|
|
@ -5,6 +5,8 @@ import java.util.HashMap;
|
|||
import org.bukkit.enchantments.Enchantment;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import com.tommytony.war.War;
|
||||
|
||||
public class LoadoutTxtMapper {
|
||||
|
||||
public static String fromLoadoutToString(HashMap<Integer, ItemStack> loadout) {
|
||||
|
@ -51,7 +53,7 @@ public class LoadoutTxtMapper {
|
|||
String[] enchantmentSplit = enchantmentStr.split(":");
|
||||
int enchantId = Integer.parseInt(enchantmentSplit[0]);
|
||||
int level = Integer.parseInt(enchantmentSplit[1]);
|
||||
item.addEnchantment(Enchantment.getById(enchantId), level);
|
||||
War.war.safelyEnchant(item, Enchantment.getById(enchantId), level);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,6 +9,8 @@ import org.bukkit.configuration.ConfigurationSection;
|
|||
import org.bukkit.enchantments.Enchantment;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import com.tommytony.war.War;
|
||||
|
||||
public class LoadoutYmlMapper {
|
||||
|
||||
public static void fromConfigToLoadouts(ConfigurationSection config, HashMap<String, HashMap<Integer, ItemStack>> loadouts) {
|
||||
|
@ -41,7 +43,7 @@ public class LoadoutYmlMapper {
|
|||
if (enchantmentStringSplit.length == 2) {
|
||||
int enchantId = Integer.parseInt(enchantmentStringSplit[0]);
|
||||
int level = Integer.parseInt(enchantmentStringSplit[1]);
|
||||
stack.addEnchantment(Enchantment.getById(enchantId), level);
|
||||
War.war.safelyEnchant(stack, Enchantment.getById(enchantId), level);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -252,7 +252,7 @@ public class VolumeMapper {
|
|||
String[] enchantmentSplit = enchantmentStr.split(":");
|
||||
int enchantId = Integer.parseInt(enchantmentSplit[0]);
|
||||
int level = Integer.parseInt(enchantmentSplit[1]);
|
||||
stack.addEnchantment(Enchantment.getById(enchantId), level);
|
||||
War.war.safelyEnchant(stack, Enchantment.getById(enchantId), level);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -201,7 +201,7 @@ commands:
|
|||
/zonecfg playerloadoutasdefault:true -> the player can bring a copy of his outside-world inventory to fight, it replaces the default loadout. \\
|
||||
/zonecfg reward:default -> sets the winner's reward to your current items.
|
||||
\\\\ Print config \\
|
||||
/zonecfg [zone-name] -print (or /zonecfg -p) -> prints out the current warzone settings. \\
|
||||
/zonecfg [zone-name] print (or /zonecfg -p) -> prints out the warzone settings. \\
|
||||
aliases:
|
||||
- setzoneconfig
|
||||
teamcfg:
|
||||
|
@ -217,7 +217,7 @@ commands:
|
|||
/teamcfg <team-name> playerloadoutasdefault:true -> the player can bring a copy of his outside-world inventory to fight, it replaces the default loadout. \\
|
||||
/teamcfg <team-name> reward:default -> sets the winner's reward to your current items.
|
||||
\\\\ Print config \\
|
||||
/teamcfg [zone-name] <team-name> -print (or /teamcfg -p) -> prints out the current team settings. \\
|
||||
/teamcfg [zone-name] <team-name> print (or /teamcfg -p) -> prints out the team settings. \\
|
||||
aliases:
|
||||
- setteamconfig
|
||||
zonemaker:
|
||||
|
@ -280,7 +280,7 @@ commands:
|
|||
/warcfg playerloadoutasdefault:true -> the player can bring a copy of his outside-world inventory to fight, it replaces the default loadout. \\
|
||||
/warcfg reward:default -> sets the winner's reward to your current items.
|
||||
\\\\ Print config \\
|
||||
/warcfg -print (or /warcfg -p) -> prints out the current War settings. \\
|
||||
/warcfg print (or /warcfg -p) -> prints out the current War settings. \\
|
||||
aliases:
|
||||
- setwarconfig
|
||||
# Fallback
|
||||
|
|
Loading…
Reference in New Issue