- Fixed NPE when using command to make and AE.

- Fixed Enchantments, Lore, Name and armor status not being copied to new AE when enchanting.
- Fixed glitch that allowed players to put enchantments / curses on items that aren't allowed.
- Changed version number to 2.0
This commit is contained in:
Pim van der Loos 2018-01-18 12:26:35 +01:00
parent 80317b4b4e
commit cb18e97165
4 changed files with 44 additions and 45 deletions

View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>nl.pim16aap2</groupId> <groupId>nl.pim16aap2</groupId>
<artifactId>ArmoredElytra</artifactId> <artifactId>ArmoredElytra</artifactId>
<version>1.11.9-SNAPSHOT</version> <version>2.0-SNAPSHOT</version>
<repositories> <repositories>

View File

@ -56,11 +56,6 @@ public class ArmoredElytra extends JavaPlugin implements Listener
// Check if the plugin should go into uninstall mode. // Check if the plugin should go into uninstall mode.
uninstallMode = config.getBool("uninstallMode"); uninstallMode = config.getBool("uninstallMode");
getCommand("ArmoredElytra").setExecutor(new CommandHandler(this, nbtEditor, uninstallMode));
// Check if the user allows checking for updates. // Check if the user allows checking for updates.
if (config.getBool("checkForUpdates")) if (config.getBool("checkForUpdates"))
{ {
@ -111,11 +106,10 @@ public class ArmoredElytra extends JavaPlugin implements Listener
if (compatibleMCVer()) if (compatibleMCVer())
{ {
Bukkit.getPluginManager().registerEvents(new EventHandlers(this, nbtEditor), this); Bukkit.getPluginManager().registerEvents(new EventHandlers(this, nbtEditor), this);
getCommand("ArmoredElytra").setExecutor(new CommandHandler(this, nbtEditor, uninstallMode));
} }
else else
{
myLogger(Level.WARNING, "Trying to load the plugin on an incompatible version of Minecraft!"); myLogger(Level.WARNING, "Trying to load the plugin on an incompatible version of Minecraft!");
}
@ -123,8 +117,6 @@ public class ArmoredElytra extends JavaPlugin implements Listener
// Load the plugin normally if not in uninstall mode. // Load the plugin normally if not in uninstall mode.
if (!uninstallMode) if (!uninstallMode)
{ {
// Check if the user wants to disable durability penalty for flying with an armored elytra. // Check if the user wants to disable durability penalty for flying with an armored elytra.
if (config.getBool("noFlightDurability")) if (config.getBool("noFlightDurability"))
{ {
@ -137,9 +129,7 @@ public class ArmoredElytra extends JavaPlugin implements Listener
// Log all allowed enchantments. // Log all allowed enchantments.
myLogger(Level.INFO, ("Allowed enchantments:")); myLogger(Level.INFO, ("Allowed enchantments:"));
for (String s : config.getStringList("allowedEnchantments")) for (String s : config.getStringList("allowedEnchantments"))
{
myLogger(Level.INFO, " - " + s); myLogger(Level.INFO, " - " + s);
}
// Log whether or not curses are allowed. // Log whether or not curses are allowed.
myLogger(Level.INFO, "Curses on armored elytras are " + (config.getBool("allowCurses") ? "" : "not " + "allowed!")); myLogger(Level.INFO, "Curses on armored elytras are " + (config.getBool("allowCurses") ? "" : "not " + "allowed!"));
} }

View File

@ -1,6 +1,5 @@
package nl.pim16aap2.armoredElytra.handlers; package nl.pim16aap2.armoredElytra.handlers;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Random; import java.util.Random;
@ -71,7 +70,6 @@ public class EventHandlers implements Listener
if (Enchantment.getByName(s) != null) if (Enchantment.getByName(s) != null)
if (Enchantment.getByName(s).equals(enchant)) if (Enchantment.getByName(s).equals(enchant))
return true; return true;
return false; return false;
} }
@ -81,41 +79,32 @@ public class EventHandlers implements Listener
for (String s : cursedEnchantments) for (String s : cursedEnchantments)
if (Enchantment.getByName(s).equals(enchant)) if (Enchantment.getByName(s).equals(enchant))
return true; return true;
return false; return false;
} }
// Copy enchants of 2 items to one item. // Copy enchants of 2 items to one item.
public ItemStack addEnchants(ItemStack itemOne, ItemStack itemTwo, Player player) public ItemStack addEnchants(ItemStack itemOne, ItemStack itemTwo, Player player)
{ {
// Create the resulting item; ItemStack result = itemOne.clone();
ItemStack result = new ItemStack(Material.ELYTRA, 1);
// Get the enchantments of the first and second item in the anvil. Map<Enchantment, Integer> newEnchantments = itemTwo.getEnchantments();
Map<Enchantment, Integer> enchantmentsTemp = itemOne.getEnchantments();
Map<Enchantment, Integer> enchantments0 = new HashMap<Enchantment, Integer>();
Map<Enchantment, Integer> enchantments1 = itemTwo.getEnchantments();
for (Map.Entry<Enchantment, Integer> entry : enchantmentsTemp.entrySet()) for (Map.Entry<Enchantment, Integer> entry : result.getEnchantments().entrySet())
// Check if the enchantment is allowed or if it is a cursed enchantment while it's allowed. if (isAllowedEnchantment(entry.getKey()) == false && (cursesAllowed && isCursedEnchantment(entry.getKey())) == false)
if (isAllowedEnchantment(entry.getKey()) || (cursesAllowed && isCursedEnchantment(entry.getKey()))) result.removeEnchantment(entry.getKey());
enchantments0.put(entry.getKey(), entry.getValue());
// Add the enchantments copied from itemOne to the resulting item. // Enchants from enchanted books have to be accessed in a different way.
result.addUnsafeEnchantments(enchantments0);
// Enchants from enchanted books have to be access in a different way.
if (itemTwo.getType() == Material.ENCHANTED_BOOK && (nbtEditor.getArmorTier(itemOne) != ArmorTier.NONE)) if (itemTwo.getType() == Material.ENCHANTED_BOOK && (nbtEditor.getArmorTier(itemOne) != ArmorTier.NONE))
{ {
EnchantmentStorageMeta meta = (EnchantmentStorageMeta)itemTwo.getItemMeta(); EnchantmentStorageMeta meta = (EnchantmentStorageMeta)itemTwo.getItemMeta();
enchantments1 = meta.getStoredEnchants(); newEnchantments = meta.getStoredEnchants();
} }
// Copy enchantments from item1 to result. // Copy enchantments from item1 to result.
if (enchantments1!=null) if (newEnchantments!=null)
{ {
// Loop through the enchantments of item1. // Loop through the enchantments of item1.
for (Map.Entry<Enchantment, Integer > entry : enchantments1.entrySet()) for (Map.Entry<Enchantment, Integer > entry : newEnchantments.entrySet())
{ {
// If the enchantment is a curse and if the result does not already have it. // If the enchantment is a curse and if the result does not already have it.
if (isCursedEnchantment(entry.getKey()) && !result.containsEnchantment(entry.getKey())) if (isCursedEnchantment(entry.getKey()) && !result.containsEnchantment(entry.getKey()))
@ -129,10 +118,10 @@ public class EventHandlers implements Listener
int enchantLevel = entry.getValue(); int enchantLevel = entry.getValue();
// If item0 and item1 both have the same enchantment at the same level, result has level+1. // If item0 and item1 both have the same enchantment at the same level, result has level+1.
// If item0 and item1 both have the same enchantment at different levels, give the highest level to result. // If item0 and item1 both have the same enchantment at different levels, give the highest level to result.
if (enchantments0 != null) if (newEnchantments != null)
{ {
// Loop through the enchantments of item0 (which are already on the result). // Loop through the enchantments of item0 (which are already on the result).
for (Map.Entry<Enchantment, Integer > rentry : enchantments0.entrySet()) for (Map.Entry<Enchantment, Integer > rentry : newEnchantments.entrySet())
{ {
if (entry.getKey().getName() == rentry.getKey().getName()) if (entry.getKey().getName() == rentry.getKey().getName())
{ {
@ -178,6 +167,21 @@ public class EventHandlers implements Listener
return result; return result;
} }
public boolean verifyEnchants(Map<Enchantment, Integer> enchantments)
{
for (Map.Entry<Enchantment, Integer > entry : enchantments.entrySet())
{
// If it's a cursed enchantment, while it's not allowed, it's false.
if (isCursedEnchantment(entry.getKey()))
if (!cursesAllowed)
return false;
// If the enchantment is not allowed, it's false.
else if (!isAllowedEnchantment(entry.getKey()))
return false;
}
return true;
}
// Handle the anvil related parts. // Handle the anvil related parts.
@EventHandler @EventHandler
public void onInventoryClick(InventoryClickEvent e) public void onInventoryClick(InventoryClickEvent e)
@ -198,7 +202,7 @@ public class EventHandlers implements Listener
} }
catch (ClassCastException exception) catch (ClassCastException exception)
{ {
// Print warning to console and exit onInventoryClick event (no support for custom inventories as they are usually used for GUI's)). // Print warning to console and exit onInventoryClick event (no support for custom inventories as they are usually used for GUI's).
plugin.debugMsg(Level.WARNING, "Could not cast inventory to anvilInventory for player " + p.getName() + "! Armored Elytras cannot be crafted!"); plugin.debugMsg(Level.WARNING, "Could not cast inventory to anvilInventory for player " + p.getName() + "! Armored Elytras cannot be crafted!");
return; return;
} }
@ -209,7 +213,8 @@ public class EventHandlers implements Listener
{ {
// If there is an elytra in the final slot (it is unenchantable by default, so we can reasonably expect it to be an enchanted elytra) // If there is an elytra in the final slot (it is unenchantable by default, so we can reasonably expect it to be an enchanted elytra)
// and the player selects it, let the player transfer it to their inventory. // and the player selects it, let the player transfer it to their inventory.
if (anvilInventory.getItem(2).getType() == Material.ELYTRA && anvilInventory.getItem(0) != null && anvilInventory.getItem(1) != null) // Verify the end result first, to prevent glitches. If the end result is invalid, remove the item and update the player's inventory.
if (anvilInventory.getItem(2).getType() == Material.ELYTRA && anvilInventory.getItem(0) != null && anvilInventory.getItem(1) != null && verifyEnchants(anvilInventory.getItem(2).getEnchantments()))
{ {
if (e.isShiftClick()) if (e.isShiftClick())
p.getInventory().addItem(anvilInventory.getItem(2)); p.getInventory().addItem(anvilInventory.getItem(2));
@ -218,6 +223,11 @@ public class EventHandlers implements Listener
// Clean the anvil's inventory after transferring the items. // Clean the anvil's inventory after transferring the items.
cleanAnvil(anvilInventory); cleanAnvil(anvilInventory);
} }
else
{
anvilInventory.getItem(2).setAmount(0);
p.updateInventory();
}
} }
new BukkitRunnable() new BukkitRunnable()
@ -300,16 +310,15 @@ public class EventHandlers implements Listener
itemB.getType() == Material.CHAINMAIL_CHESTPLATE || itemB.getType() == Material.CHAINMAIL_CHESTPLATE ||
itemB.getType() == Material.IRON_CHESTPLATE || itemB.getType() == Material.IRON_CHESTPLATE ||
itemB.getType() == Material.DIAMOND_CHESTPLATE) itemB.getType() == Material.DIAMOND_CHESTPLATE)
{
// Add the NBT Tags for the elytra, to give it diamond_chestplate tier of armor protection. // Add the NBT Tags for the elytra, to give it diamond_chestplate tier of armor protection.
result = nbtEditor.addArmorNBTTags(result, armorTier, plugin.getConfigLoader().getBool("unbreakable")); result = nbtEditor.addArmorNBTTags(result, armorTier, plugin.getConfigLoader().getBool("unbreakable"));
}
else if ((nbtEditor.getArmorTier(itemA) != ArmorTier.NONE) && else if ((nbtEditor.getArmorTier(itemA) != ArmorTier.NONE) &&
(nbtEditor.getArmorTier(result) != ArmorTier.NONE)) (nbtEditor.getArmorTier(result) != ArmorTier.NONE))
{ {
armorTier = nbtEditor.getArmorTier(itemA); armorTier = nbtEditor.getArmorTier(itemA);
result = nbtEditor.addArmorNBTTags(result, armorTier, plugin.getConfigLoader().getBool("unbreakable")); result = nbtEditor.addArmorNBTTags(result, armorTier, plugin.getConfigLoader().getBool("unbreakable"));
} }
// result.setItemMeta(itemMeta)
anvilInventory.setItem(2, result); anvilInventory.setItem(2, result);
} }
} }

View File

@ -1,6 +1,6 @@
name: ArmoredElytra name: ArmoredElytra
main: nl.pim16aap2.armoredElytra.ArmoredElytra main: nl.pim16aap2.armoredElytra.ArmoredElytra
version: 1.11.9 version: 2.0
author: Pim author: Pim
commands: commands:
ArmoredElytra: ArmoredElytra: