- Added 1.9 support.

- Fixed books with 1 or more invalid enchants not working.
This commit is contained in:
Pim van der Loos 2018-11-06 13:23:56 +01:00
parent 9355a9343b
commit 07e3b65f2b
No known key found for this signature in database
GPG Key ID: C16F020ADAE6D5A8
9 changed files with 667 additions and 349 deletions

14
pom.xml
View File

@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>nl.pim16aap2</groupId>
<artifactId>ArmoredElytra</artifactId>
<version>2.4.7-SNAPSHOT</version>
<version>2.4.8-SNAPSHOT</version>
<repositories>
<repository>
@ -16,6 +16,18 @@
</repositories>
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-1.9</artifactId>
<version>1.9-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-1.9.4</artifactId>
<version>1.9.4-R0.2-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-1.10</artifactId>

View File

@ -21,6 +21,8 @@ import nl.pim16aap2.armoredElytra.nms.NBTEditor_V1_11_R1;
import nl.pim16aap2.armoredElytra.nms.NBTEditor_V1_12_R1;
import nl.pim16aap2.armoredElytra.nms.NBTEditor_V1_13_R1;
import nl.pim16aap2.armoredElytra.nms.NBTEditor_V1_13_R2;
import nl.pim16aap2.armoredElytra.nms.NBTEditor_V1_9_R1;
import nl.pim16aap2.armoredElytra.nms.NBTEditor_V1_9_R2;
import nl.pim16aap2.armoredElytra.util.ArmorTier;
import nl.pim16aap2.armoredElytra.util.ConfigLoader;
import nl.pim16aap2.armoredElytra.util.Messages;
@ -28,18 +30,20 @@ import nl.pim16aap2.armoredElytra.util.Metrics;
import nl.pim16aap2.armoredElytra.util.Update;
public class ArmoredElytra extends JavaPlugin implements Listener
{
private NBTEditor nbtEditor;
private Messages messages;
private ConfigLoader config;
{
// TODO: Merge EventHandlers and EventHandlers_V1.9.
private NBTEditor nbtEditor;
private Messages messages;
private ConfigLoader config;
private String leatherName, ironName, goldName, chainName, diamondName;
private String elytraReceivedMessage;
private String usageDeniedMessage;
private boolean uninstallMode;
private String elytraLore;
private boolean upToDate;
private String locale;
private String leatherName, ironName, goldName, chainName, diamondName;
private String elytraReceivedMessage;
private String usageDeniedMessage;
private boolean uninstallMode;
private String elytraLore;
private boolean upToDate;
private String locale;
private boolean is1_9;
@Override
public void onEnable()
@ -107,7 +111,11 @@ public class ArmoredElytra extends JavaPlugin implements Listener
// Load the files for the correct version of Minecraft.
if (compatibleMCVer())
{
Bukkit.getPluginManager().registerEvents(new EventHandlers(this, nbtEditor), this);
// if (this.is1_9)
// Bukkit.getPluginManager().registerEvents(new EventHandlers_V1_9(this, nbtEditor), this);
// else
// Bukkit.getPluginManager().registerEvents(new EventHandlers(this, nbtEditor), this);
Bukkit.getPluginManager().registerEvents(new EventHandlers(this, nbtEditor, this.is1_9), this);
getCommand("ArmoredElytra").setExecutor(new CommandHandler(this, nbtEditor));
}
else
@ -268,16 +276,26 @@ public class ArmoredElytra extends JavaPlugin implements Listener
return false;
}
if (version.equals("v1_10_R1"))
nbtEditor = new NBTEditor_V1_10_R1(this);
if ( version.equals("v1_9_R1"))
{
nbtEditor = new NBTEditor_V1_9_R1(this);
this.is1_9 = true;
}
else if (version.equals("v1_9_R2"))
{
nbtEditor = new NBTEditor_V1_9_R2(this);
this.is1_9 = true;
}
else if (version.equals("v1_10_R1"))
nbtEditor = new NBTEditor_V1_10_R1(this);
else if (version.equals("v1_11_R1"))
nbtEditor = new NBTEditor_V1_11_R1(this);
nbtEditor = new NBTEditor_V1_11_R1(this);
else if (version.equals("v1_12_R1"))
nbtEditor = new NBTEditor_V1_12_R1(this);
nbtEditor = new NBTEditor_V1_12_R1(this);
else if (version.equals("v1_13_R1"))
nbtEditor = new NBTEditor_V1_13_R1(this);
nbtEditor = new NBTEditor_V1_13_R1(this);
else if (version.equals("v1_13_R2"))
nbtEditor = new NBTEditor_V1_13_R2(this);
nbtEditor = new NBTEditor_V1_13_R2(this);
// Return true if compatible.
return nbtEditor != null;
}
@ -312,25 +330,6 @@ public class ArmoredElytra extends JavaPlugin implements Listener
}
return ret;
}
public boolean playerHasCraftPerm(Player player, ArmorTier tier)
{
switch (tier)
{
case LEATHER:
return player.hasPermission("armoredelytra.craft.leather");
case GOLD:
return player.hasPermission("armoredelytra.craft.gold");
case CHAIN:
return player.hasPermission("armoredelytra.craft.chain");
case IRON:
return player.hasPermission("armoredelytra.craft.iron");
case DIAMOND:
return player.hasPermission("armoredelytra.craft.diamond");
default:
return false;
}
}
public void setUpToDate(boolean upToDate)
{

View File

@ -26,6 +26,7 @@ import org.bukkit.scheduler.BukkitRunnable;
import nl.pim16aap2.armoredElytra.ArmoredElytra;
import nl.pim16aap2.armoredElytra.nms.NBTEditor;
import nl.pim16aap2.armoredElytra.util.Action;
import nl.pim16aap2.armoredElytra.util.AllowedToWearEnum;
import nl.pim16aap2.armoredElytra.util.ArmorTier;
import nl.pim16aap2.armoredElytra.util.Util;
@ -38,11 +39,13 @@ public class EventHandlers implements Listener
private NBTEditor nbtEditor;
private final ArmoredElytra plugin;
private List<String> allowedEnchantments;
private boolean is1_9;
public EventHandlers(ArmoredElytra plugin, NBTEditor nbtEditor)
public EventHandlers(ArmoredElytra plugin, NBTEditor nbtEditor, boolean is1_9)
{
this.plugin = plugin;
this.nbtEditor = nbtEditor;
this.is1_9 = is1_9;
// Get the values of the config options.
this.allowedEnchantments = plugin.getConfigLoader().getStringList("allowedEnchantments");
@ -51,17 +54,40 @@ public class EventHandlers implements Listener
this.IRON_TO_FULL = plugin.getConfigLoader().getInt("ironRepair");
this.DIAMONDS_TO_FULL = plugin.getConfigLoader().getInt("diamondsRepair");
}
// Remove item from player's chestplate slot and puts it in their normal inventory.
private void unenquipChestPlayer(Player p)
{
if (is1_9)
p.getInventory().getChestplate().setType(Material.AIR);
else
{
p.getInventory().addItem(p.getInventory().getChestplate());
p.getInventory().getChestplate().setAmount(0);
p.updateInventory();
}
}
// Clear the anvil's inventory (destroy all the items in all 3 slots (second slot is not emptied, when repairing you can safely give multiple items)).
public void cleanAnvil(AnvilInventory anvilInventory)
private void cleanAnvil(AnvilInventory anvilInventory)
{
anvilInventory.getItem(0).setAmount(0);
anvilInventory.getItem(1).setAmount(anvilInventory.getItem(1).getAmount() - 1);
anvilInventory.getItem(2).setAmount(0);
if (is1_9)
{
ItemStack air = new ItemStack(Material.AIR, 1);
anvilInventory.setItem(0, air);
anvilInventory.setItem(1, air);
anvilInventory.setItem(2, air);
}
else
{
anvilInventory.getItem(0).setAmount(0);
anvilInventory.getItem(1).setAmount(anvilInventory.getItem(1).getAmount() - 1);
anvilInventory.getItem(2).setAmount(0);
}
}
// Check if the enchantment is allowed on elytras.
public boolean isAllowedEnchantment(Enchantment enchant)
private boolean isAllowedEnchantment(Enchantment enchant)
{
for (String s : allowedEnchantments)
if (Enchantment.getByName(s) != null)
@ -71,101 +97,85 @@ public class EventHandlers implements Listener
}
// Combine 2 maps of enchantments (and remove any invalid ones).
public Map<Enchantment, Integer> combineEnchantments(Map<Enchantment, Integer> enchantments0, Map<Enchantment, Integer> enchantments1)
private Map<Enchantment, Integer> combineEnchantments(Map<Enchantment, Integer> enchantments0, Map<Enchantment, Integer> enchantments1)
{
enchantments0 = fixEnchantments(enchantments0);
Map<Enchantment, Integer> combined = new HashMap<Enchantment, Integer>(fixEnchantments(enchantments0));
if (enchantments1 != null)
// If the second set of enchantments is null, the combined enchantments are just the first enchantments.
if (enchantments1 == null)
return combined;
enchantments1 = fixEnchantments(enchantments1);
// Loop through the enchantments of item1.
for (Map.Entry<Enchantment, Integer > entry : enchantments1.entrySet())
{
enchantments1 = fixEnchantments(enchantments1);
// Loop through the enchantments of item1.
for (Map.Entry<Enchantment, Integer > entry : enchantments1.entrySet())
Integer enchantLevel = enchantments0.get(entry.getKey());
if (enchantLevel != null)
{
Integer enchantLevel = enchantments0.get(entry.getKey());
if (enchantLevel != null)
if (entry.getValue() == enchantLevel && entry.getValue() < entry.getKey().getMaxLevel())
enchantLevel = entry.getValue() + 1;
else if (entry.getValue() > enchantLevel)
enchantLevel = entry.getValue();
// If the enchantment level has changed,
if (enchantLevel != enchantments0.get(entry.getKey()))
{
if (entry.getValue() == enchantLevel && entry.getValue() < entry.getKey().getMaxLevel())
enchantLevel = entry.getValue() + 1;
else if (entry.getValue() > enchantLevel)
enchantLevel = entry.getValue();
// If the enchantment level has changed,
if (enchantLevel != enchantments0.get(entry.getKey()))
{
combined.remove(entry.getKey());
combined.put(entry.getKey(), enchantLevel);
}
combined.remove(entry.getKey());
combined.put(entry.getKey(), enchantLevel);
}
else if (enchantLevel == null)
combined.put(entry.getKey(), entry.getValue());
}
// Get the protection enchantment rating for both enchantment sets.
int protVal0 = getProtectionEnchantmentsVal(enchantments0);
int protVal1 = getProtectionEnchantmentsVal(enchantments1);
// If they have different protection enchantments, keep enchantment1's enchantments
// And remove the protection enchantment from enchantments0. Yes, this system only works
// If there is 1 protection enchantment on
if (protVal0 != 0 && protVal1 != 0 && protVal0 != protVal1)
else if (enchantLevel == null)
combined.put(entry.getKey(), entry.getValue());
}
// Get the protection enchantment rating for both enchantment sets.
int protVal0 = Util.getProtectionEnchantmentsVal(enchantments0);
int protVal1 = Util.getProtectionEnchantmentsVal(enchantments1);
// If they have different protection enchantments, keep enchantment1's enchantments
// And remove the protection enchantment from enchantments0. Yes, this system only works
// If there is 1 protection enchantment on
if (protVal0 != 0 && protVal1 != 0 && protVal0 != protVal1)
{
switch(protVal0)
{
switch(protVal0)
{
case 1:
combined.remove(Enchantment.PROTECTION_ENVIRONMENTAL);
break;
case 2:
combined.remove(Enchantment.PROTECTION_EXPLOSIONS);
break;
case 4:
combined.remove(Enchantment.PROTECTION_FALL);
break;
case 8:
combined.remove(Enchantment.PROTECTION_FIRE);
break;
case 16:
combined.remove(Enchantment.PROTECTION_PROJECTILE);
break;
}
case 1:
combined.remove(Enchantment.PROTECTION_ENVIRONMENTAL);
break;
case 2:
combined.remove(Enchantment.PROTECTION_EXPLOSIONS);
break;
case 4:
combined.remove(Enchantment.PROTECTION_FALL);
break;
case 8:
combined.remove(Enchantment.PROTECTION_FIRE);
break;
case 16:
combined.remove(Enchantment.PROTECTION_PROJECTILE);
break;
}
}
return combined;
}
// Function that returns which/how many protection enchantments there are.
public int getProtectionEnchantmentsVal(Map<Enchantment, Integer> enchantments)
{
int ret = 0;
if (enchantments.containsKey(Enchantment.PROTECTION_ENVIRONMENTAL))
ret += 1;
if (enchantments.containsKey(Enchantment.PROTECTION_EXPLOSIONS))
ret += 2;
if (enchantments.containsKey(Enchantment.PROTECTION_FALL))
ret += 4;
if (enchantments.containsKey(Enchantment.PROTECTION_FIRE))
ret += 8;
if (enchantments.containsKey(Enchantment.PROTECTION_PROJECTILE))
ret += 16;
return ret;
}
// Repair an Armored Elytra
public short repairItem(short curDur, ItemStack repairItem)
private short repairItem(short curDur, ItemStack repairItem)
{
// Get the multiplier for the repair items.
double mult = 0.01;
if ( repairItem.getType() == Material.LEATHER)
mult *= (100/LEATHER_TO_FULL);
mult *= (100 / LEATHER_TO_FULL);
else if (repairItem.getType() == Material.GOLD_INGOT)
mult *= (100/GOLD_TO_FULL);
mult *= (100 / GOLD_TO_FULL);
else if (repairItem.getType() == Material.IRON_INGOT)
mult *= (100/IRON_TO_FULL);
mult *= (100 / IRON_TO_FULL);
else if (repairItem.getType() == Material.DIAMOND)
mult *= (100/DIAMONDS_TO_FULL);
mult *= (100 / DIAMONDS_TO_FULL);
int maxDurability = Material.ELYTRA.getMaxDurability();
int newDurability = (int) (curDur - (maxDurability * mult));
@ -173,7 +183,7 @@ public class EventHandlers implements Listener
}
// Remove any disallowed enchantments in the map.
public Map<Enchantment, Integer> fixEnchantments(Map<Enchantment, Integer> enchantments)
private Map<Enchantment, Integer> fixEnchantments(Map<Enchantment, Integer> enchantments)
{
Map<Enchantment, Integer> ret = new HashMap<Enchantment, Integer>(enchantments);
for (Map.Entry<Enchantment, Integer> entry : enchantments.entrySet())
@ -183,52 +193,15 @@ public class EventHandlers implements Listener
}
// Verify there aren't any disallowed enchantments in the map.
public boolean verifyEnchantments(Map<Enchantment, Integer> enchantments)
private int verifyEnchantments(Map<Enchantment, Integer> enchantments)
{
int ret = 0;
for (Map.Entry<Enchantment, Integer> entry : enchantments.entrySet())
if (!isAllowedEnchantment(entry.getKey()))
return false;
return true;
}
// Get the armor tier from a chest plate.
public ArmorTier armorToTier(Material item)
{
ArmorTier ret = ArmorTier.NONE;
switch (item)
{
case LEATHER_CHESTPLATE:
ret = ArmorTier.LEATHER;
break;
case GOLD_CHESTPLATE:
ret = ArmorTier.GOLD;
break;
case CHAINMAIL_CHESTPLATE:
ret = ArmorTier.CHAIN;
break;
case IRON_CHESTPLATE:
ret = ArmorTier.IRON;
break;
case DIAMOND_CHESTPLATE:
ret = ArmorTier.DIAMOND;
break;
default:
break;
}
++ret;
return ret;
}
// Check if mat is a chest plate.
public boolean isChestPlate(Material mat)
{
if (mat == Material.LEATHER_CHESTPLATE || mat == Material.GOLD_CHESTPLATE ||
mat == Material.CHAINMAIL_CHESTPLATE || mat == Material.IRON_CHESTPLATE ||
mat == Material.DIAMOND_CHESTPLATE)
return true;
return false;
}
/*
* Valid inputs:
* - Elytra (armored or not) + chestplate -> Create Armored Elytra
@ -241,7 +214,7 @@ public class EventHandlers implements Listener
* - Elytra (not armored) + !chestplate -> None
* - * + * -> None
*/
public Action isValidInput(ItemStack itemOne, ItemStack itemTwo)
private Action isValidInput(ItemStack itemOne, ItemStack itemTwo)
{
if (itemOne == null || itemTwo == null)
return Action.NONE;
@ -260,7 +233,7 @@ public class EventHandlers implements Listener
Material matTwo = itemTwo.getType();
// If the elytra is to be combined with chest armor...
if (isChestPlate(matTwo))
if (Util.isChestPlate(matTwo))
return Action.CREATE;
ArmorTier tier = nbtEditor.getArmorTier(itemOne);
@ -290,7 +263,7 @@ public class EventHandlers implements Listener
// Handle all anvil related stuff for this plugin.
@EventHandler
public void onAnvilInventoryOpen(PrepareAnvilEvent event)
private void onAnvilInventoryOpen(PrepareAnvilEvent event)
{
Player player = (Player) event.getView().getPlayer();
ItemStack itemA = event.getInventory().getItem(0);
@ -309,67 +282,67 @@ public class EventHandlers implements Listener
}
}
// Check if there are items in both input slots.
// Check if there are items in both input slots.
if (itemA != null && itemB != null)
{
Action action = isValidInput(itemA, itemB);
ArmorTier newTier = ArmorTier.NONE;
ArmorTier curTier = nbtEditor.getArmorTier(itemA);
short durability = 0;
Map<Enchantment, Integer> enchantments = itemA.getEnchantments();
enchantments = fixEnchantments(enchantments);
switch (action)
Action action = isValidInput(itemA, itemB);
ArmorTier newTier = ArmorTier.NONE;
ArmorTier curTier = nbtEditor.getArmorTier(itemA);
short durability = 0;
Map<Enchantment, Integer> enchantments = itemA.getEnchantments();
enchantments = fixEnchantments(enchantments);
switch (action)
{
case REPAIR:
newTier = curTier;
durability = repairItem(itemA.getDurability(), itemB);
break;
case COMBINE:
newTier = curTier;
durability = (short) (- itemA.getType().getMaxDurability() - itemA.getDurability() - itemB.getDurability());
durability = durability < 0 ? 0 : durability;
enchantments = combineEnchantments(enchantments, itemB.getEnchantments());
break;
case CREATE:
newTier = Util.armorToTier(itemB.getType());
durability = 0;
enchantments = combineEnchantments(enchantments, itemB.getEnchantments());
break;
case ENCHANT:
EnchantmentStorageMeta meta = (EnchantmentStorageMeta) itemB.getItemMeta();
newTier = curTier;
durability = itemA.getDurability();
// If there aren't any illegal enchantments on the book, continue as normal.
// Otherwise... Block.
if (verifyEnchantments(meta.getStoredEnchants()) != meta.getStoredEnchants().size())
{
case REPAIR:
newTier = curTier;
durability = repairItem(itemA.getDurability(), itemB);
enchantments = combineEnchantments(enchantments, meta.getStoredEnchants());
break;
case COMBINE:
newTier = curTier;
durability = (short) (- itemA.getType().getMaxDurability() - itemA.getDurability() - itemB.getDurability());
durability = durability < 0 ? 0 : durability;
enchantments = combineEnchantments(enchantments, itemB.getEnchantments());
break;
case CREATE:
newTier = armorToTier(itemB.getType());
durability = 0;
enchantments = combineEnchantments(enchantments, itemB.getEnchantments());
break;
case ENCHANT:
EnchantmentStorageMeta meta = (EnchantmentStorageMeta) itemB.getItemMeta();
newTier = curTier;
durability = itemA.getDurability();
// If there aren't any illegal enchantments on the book, continue as normal.
// Otherwise... Block.
if (verifyEnchantments(meta.getStoredEnchants()))
{
enchantments = combineEnchantments(enchantments, meta.getStoredEnchants());
break;
}
case BLOCK:
event.setResult(null);
player.updateInventory();
case NONE:
return;
}
case BLOCK:
event.setResult(null);
player.updateInventory();
case NONE:
return;
}
if (Util.playerHasCraftPerm(player, newTier))
{
result = new ItemStack(Material.ELYTRA, 1);
if (enchantments != null)
result.addUnsafeEnchantments(enchantments);
result.setDurability(durability);
if (plugin.playerHasCraftPerm(player, newTier))
{
result = new ItemStack(Material.ELYTRA, 1);
if (enchantments != null)
result.addUnsafeEnchantments(enchantments);
result.setDurability(durability);
result = nbtEditor.addArmorNBTTags(result, newTier, plugin.getConfigLoader().getBool("unbreakable"));
event.setResult(result);
}
result = nbtEditor.addArmorNBTTags(result, newTier, plugin.getConfigLoader().getBool("unbreakable"));
event.setResult(result);
}
}
// Check if either itemA or itemB is unoccupied.
if ((itemA == null || itemB == null) && nbtEditor.getArmorTier(event.getInventory().getItem(2)) != ArmorTier.NONE)
// If Item2 is occupied despite itemA or itemB not being occupied. (only for armored elytra)/
event.setResult(null);
// If Item2 is occupied despite itemA or itemB not being occupied. (only for armored elytra)/
event.setResult(null);
player.updateInventory();
}
@ -377,80 +350,101 @@ public class EventHandlers implements Listener
@EventHandler
public void onInventoryClick(InventoryClickEvent e)
{
if (e.getWhoClicked() instanceof Player)
if (!(e.getWhoClicked() instanceof Player))
return;
// Check if the event was a player who interacted with an anvil.
Player player = (Player) e.getWhoClicked();
if (e.getView().getType() != InventoryType.ANVIL)
return;
AnvilInventory anvilInventory;
// Try to cast inventory being used in the event to an anvil inventory.
// This will throw a ClassCastException when a CraftInventoryCustom is used.
try
{
// Check if the event was a player who interacted with an anvil.
Player player = (Player) e.getWhoClicked();
if (e.getView().getType() == InventoryType.ANVIL)
anvilInventory = (AnvilInventory) e.getInventory();
}
catch (ClassCastException exception)
{
// 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 " + player.getName() + "! Armored Elytras cannot be crafted!");
return;
}
int slot = e.getRawSlot();
if (slot == 2 && anvilInventory.getItem(0) != null && anvilInventory.getItem(1) != null && anvilInventory.getItem(2) != null)
{
ArmorTier armortier = nbtEditor.getArmorTier(anvilInventory.getItem(2));
// If there's an armored elytra in the final slot...
if (armortier != ArmorTier.NONE && Util.playerHasCraftPerm(player, armortier))
{
AnvilInventory anvilInventory;
// Try to cast inventory being used in the event to an anvil inventory.
// This will throw a ClassCastException when a CraftInventoryCustom is used.
try
// Create a new armored elytra and give that one to the player instead of the result.
// This is done because after putting item0 in AFTER item1, the first letter of the color code shows up, this gets rid of that problem.
ItemStack result = nbtEditor.addArmorNBTTags(anvilInventory.getItem(2), armortier, plugin.getConfigLoader().getBool("unbreakable"));
// Give the result to the player and clear the anvil's inventory.
if (e.isShiftClick())
{
anvilInventory = (AnvilInventory) e.getInventory();
}
catch (ClassCastException exception)
{
// 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 " + player.getName() + "! Armored Elytras cannot be crafted!");
return;
}
int slot = e.getRawSlot();
if (slot == 2 && anvilInventory.getItem(0) != null && anvilInventory.getItem(1) != null && anvilInventory.getItem(2) != null)
{
ArmorTier armortier = nbtEditor.getArmorTier(anvilInventory.getItem(2));
// If there's an armored elytra in the final slot...
if (armortier != ArmorTier.NONE && plugin.playerHasCraftPerm(player, armortier))
{
// Create a new armored elytra and give that one to the player instead of the result.
// This is done because after putting item0 in AFTER item1, the first letter of the color code shows up, this gets rid of that problem.
ItemStack result = nbtEditor.addArmorNBTTags(anvilInventory.getItem(2), armortier, plugin.getConfigLoader().getBool("unbreakable"));
// Give the result to the player and clear the anvil's inventory.
if (e.isShiftClick())
{
// If the player's inventory is full, don't do anything.
if (player.getInventory().firstEmpty() == -1)
return;
player.getInventory().addItem(result);
}
else
player.setItemOnCursor(result);
// Clean the anvil's inventory after transferring the items.
cleanAnvil(anvilInventory);
player.updateInventory();
// If the player's inventory is full, don't do anything.
if (player.getInventory().firstEmpty() == -1)
return;
}
player.getInventory().addItem(result);
}
else
player.setItemOnCursor(result);
// Clean the anvil's inventory after transferring the items.
this.cleanAnvil(anvilInventory);
player.updateInventory();
return;
}
}
}
// Make sure the player has the correct permission and that the item is not broken.
private AllowedToWearEnum isAllowedToWear(ItemStack elytra, Player player, ArmorTier armorTier)
{
if (Util.isBroken(elytra))
return AllowedToWearEnum.BROKEN;
if (!Util.playerHasWearPerm(player, armorTier))
return AllowedToWearEnum.NOPERMISSION;
return AllowedToWearEnum.ALLOWED;
}
// Check if the player tries to equip armor by richt clicking it.
@SuppressWarnings("deprecation")
@EventHandler
public void onRightClick(PlayerInteractEvent event)
{
Player player = event.getPlayer();
ItemStack item = player.getItemInHand();
if (!event.getAction().equals(org.bukkit.event.block.Action.RIGHT_CLICK_AIR) &&
!event.getAction().equals(org.bukkit.event.block.Action.RIGHT_CLICK_BLOCK))
return;
if (item != null)
if (item.getType() == Material.ELYTRA && (nbtEditor.getArmorTier(item) != ArmorTier.NONE))
{
ArmorTier armorTier = nbtEditor.getArmorTier(item);
if ((armorTier == ArmorTier.LEATHER && !player.hasPermission("armoredelytra.wear.leather")) ||
(armorTier == ArmorTier.GOLD && !player.hasPermission("armoredelytra.wear.gold" )) ||
(armorTier == ArmorTier.CHAIN && !player.hasPermission("armoredelytra.wear.chain" )) ||
(armorTier == ArmorTier.IRON && !player.hasPermission("armoredelytra.wear.iron" )) ||
(armorTier == ArmorTier.DIAMOND && !player.hasPermission("armoredelytra.wear.diamond")))
{
plugin.usageDeniedMessage(player, armorTier);
event.setCancelled(true);
}
ItemStack item = event.getItem();
if (item == null)
return;
Player player = event.getPlayer();
if (item.getType() == Material.ELYTRA)
{
ArmorTier armorTier = nbtEditor.getArmorTier(item);
if (nbtEditor.getArmorTier(item) == ArmorTier.NONE)
return;
AllowedToWearEnum allowed = this.isAllowedToWear(item, player, armorTier);
if (allowed == AllowedToWearEnum.BROKEN)
{
plugin.messagePlayer(player, plugin.getMyMessages().getString("MESSAGES.RepairNeeded"));
event.setCancelled(true);
player.updateInventory();
}
else if (allowed == AllowedToWearEnum.NOPERMISSION)
{
plugin.usageDeniedMessage(player, armorTier);
event.setCancelled(true);
player.updateInventory();
}
}
}
// Player closes their inventory. Also checks for whether they are allowed to wear the armored elytra they are wearing.
@ -462,33 +456,28 @@ public class EventHandlers implements Listener
}
// Check if the player is allowed to wear the armored elytra based on their permissions.
public void verifyArmorInChestSlot(Player player)
private void verifyArmorInChestSlot(Player player)
{
ItemStack chestplate = player.getInventory().getChestplate();
// If the player equips a new chestplate.
if (player.getInventory().getChestplate() != null)
if (player.getInventory().getChestplate() == null)
return;
ArmorTier armorTier = nbtEditor.getArmorTier(chestplate);
// If that chestplate is an (armored) elytra.
if (armorTier == ArmorTier.NONE)
return;
AllowedToWearEnum allowed = this.isAllowedToWear(chestplate, player, armorTier);
if (allowed == AllowedToWearEnum.BROKEN)
{
// If that chestplate is an (armored) elytra.
if (nbtEditor.getArmorTier(chestplate) != ArmorTier.NONE)
{
ArmorTier armorTier = nbtEditor.getArmorTier(chestplate);
if ((chestplate.getDurability() >= chestplate.getType().getMaxDurability()))
{
plugin.messagePlayer(player, plugin.getMyMessages().getString("MESSAGES.RepairNeeded"));
Util.unenquipChestPlayer(player);
player.updateInventory();
}
else if ((armorTier == ArmorTier.LEATHER && !player.hasPermission("armoredelytra.wear.leather")) ||
(armorTier == ArmorTier.GOLD && !player.hasPermission("armoredelytra.wear.gold" )) ||
(armorTier == ArmorTier.CHAIN && !player.hasPermission("armoredelytra.wear.chain" )) ||
(armorTier == ArmorTier.IRON && !player.hasPermission("armoredelytra.wear.iron" )) ||
(armorTier == ArmorTier.DIAMOND && !player.hasPermission("armoredelytra.wear.diamond")))
{
plugin.usageDeniedMessage(player, armorTier);
Util.unenquipChestPlayer(player);
player.updateInventory();
}
}
plugin.messagePlayer(player, plugin.getMyMessages().getString("MESSAGES.RepairNeeded"));
this.unenquipChestPlayer(player);
}
else if (allowed == AllowedToWearEnum.NOPERMISSION)
{
plugin.usageDeniedMessage(player, armorTier);
this.unenquipChestPlayer(player);
}
}
@ -496,50 +485,51 @@ public class EventHandlers implements Listener
@EventHandler(ignoreCancelled = true)
public void onPlayerDamage(EntityDamageEvent e)
{
if(e.getEntity() instanceof Player)
if(!(e.getEntity() instanceof Player))
return;
Player p = (Player) e.getEntity();
// If the player didn't die from the damage.
if ((p.getHealth() - e.getFinalDamage()) > 0)
{
Player p = (Player) e.getEntity();
// If the player didn't die from the damage.
if ((p.getHealth() - e.getFinalDamage()) > 0)
if (p.getInventory().getChestplate() == null)
return;
if (nbtEditor.getArmorTier(p.getInventory().getChestplate()) == ArmorTier.NONE)
return;
ItemStack elytra = p.getInventory().getChestplate();
DamageCause cause = e.getCause();
// The elytra doesn't receive any damage for these causes:
if (cause != DamageCause.DROWNING && cause != DamageCause.STARVATION && cause != DamageCause.SUFFOCATION &&
cause != DamageCause.SUICIDE && cause != DamageCause.FLY_INTO_WALL && cause != DamageCause.POISON)
{
if (p.getInventory().getChestplate() != null)
int durability = p.getInventory().getChestplate().getDurability();
int maxDurability = p.getInventory().getChestplate().getType().getMaxDurability();
int newDurability = (int) (durability + ((int) (e.getDamage() / 4) > 1 ? (int) (e.getDamage() / 4) : 1));
// If the elytra has the durability enchantment, we calculate the durability loss ourselves.
if (p.getInventory().getChestplate().containsEnchantment(Enchantment.DURABILITY))
{
if (nbtEditor.getArmorTier(p.getInventory().getChestplate()) != ArmorTier.NONE)
{
ItemStack elytra = p.getInventory().getChestplate();
DamageCause cause = e.getCause();
// The elytra doesn't receive any damage for these causes:
if (cause != DamageCause.DROWNING && cause != DamageCause.STARVATION && cause != DamageCause.SUFFOCATION &&
cause != DamageCause.SUICIDE && cause != DamageCause.FLY_INTO_WALL && cause != DamageCause.POISON)
{
int durability = p.getInventory().getChestplate().getDurability();
int maxDurability = p.getInventory().getChestplate().getType().getMaxDurability();
int newDurability = (int) (durability + ((int) (e.getDamage() / 4) > 1 ? (int) (e.getDamage() / 4) : 1));
// If the elytra has the durability enchantment, we calculate the durability loss ourselves.
if (p.getInventory().getChestplate().containsEnchantment(Enchantment.DURABILITY)) {
// Get a random int between 0 and 100
Random r = new Random();
int randomInt = r.nextInt(101);
int enchantLevel = p.getInventory().getChestplate().getEnchantmentLevel(Enchantment.DURABILITY);
int durabilityDelta = (100 / (enchantLevel + 1)) < randomInt ? 0 : 1;
if (durability >= maxDurability)
Util.unenquipChestPlayer(p);
else
newDurability = durability + durabilityDelta;
}
// If the item should be broken, make sure it really is broken and unequip it.
if (newDurability >= maxDurability)
{
newDurability = maxDurability;
Util.unenquipChestPlayer(p);
}
elytra.setDurability((short) (newDurability));
}
}
// Get a random int between 0 and 100 to use in deciding if the durability enchantment will take effect.
Random r = new Random();
int randomInt = r.nextInt(101);
int enchantLevel = p.getInventory().getChestplate().getEnchantmentLevel(Enchantment.DURABILITY);
int durabilityDelta = (100 / (enchantLevel + 1)) < randomInt ? 0 : 1;
// If the durability equals/exceeds maxDurability, it's broken (0 = full item durability).
if (durability >= maxDurability)
this.unenquipChestPlayer(p);
else
newDurability = durability + durabilityDelta;
}
// If the item should be broken, make sure it really is broken and unequip it.
if (newDurability >= maxDurability)
{
newDurability = maxDurability;
this.unenquipChestPlayer(p);
}
elytra.setDurability((short) (newDurability));
}
}
}
@ -548,17 +538,17 @@ public class EventHandlers implements Listener
@EventHandler
public void playerEquipsArmor(InventoryClickEvent e)
{
if (e.getWhoClicked() instanceof Player)
if (!(e.getWhoClicked() instanceof Player))
return;
Player player = (Player) e.getWhoClicked();
new BukkitRunnable()
{
Player player = (Player) e.getWhoClicked();
new BukkitRunnable()
@Override
public void run()
{
@Override
public void run()
{
verifyArmorInChestSlot(player);
}
}.runTaskLater(this.plugin, 1);
}
verifyArmorInChestSlot(player);
}
}.runTaskLater(this.plugin, 1);
}
}

View File

@ -0,0 +1,117 @@
package nl.pim16aap2.armoredElytra.nms;
import java.util.Arrays;
import org.bukkit.Material;
import org.bukkit.craftbukkit.v1_9_R1.inventory.CraftItemStack;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import net.minecraft.server.v1_9_R1.NBTTagByte;
import net.minecraft.server.v1_9_R1.NBTTagCompound;
import net.minecraft.server.v1_9_R1.NBTTagInt;
import net.minecraft.server.v1_9_R1.NBTTagList;
import net.minecraft.server.v1_9_R1.NBTTagString;
import nl.pim16aap2.armoredElytra.ArmoredElytra;
import nl.pim16aap2.armoredElytra.util.ArmorTier;
public class NBTEditor_V1_9_R1 implements NBTEditor
{
private ArmoredElytra plugin;
// Get the names and lores for every tier of armor.
public NBTEditor_V1_9_R1(ArmoredElytra plugin)
{
this.plugin = plugin;
}
// Add armor to the supplied item, based on the armorTier.
@Override
public ItemStack addArmorNBTTags(ItemStack item, ArmorTier armorTier, boolean unbreakable)
{
ItemMeta itemmeta = item.getItemMeta();
int armorProtection = ArmorTier.getArmor(armorTier);
int armorToughness = ArmorTier.getToughness(armorTier);
itemmeta.setDisplayName(plugin.getArmoredElytrName(armorTier));
if (plugin.getElytraLore() != null)
itemmeta.setLore(Arrays.asList(plugin.fillInArmorTierInStringNoColor(plugin.getElytraLore(), armorTier)));
item.setItemMeta(itemmeta);
net.minecraft.server.v1_9_R1.ItemStack nmsStack = CraftItemStack.asNMSCopy(item);
NBTTagCompound compound = (nmsStack.hasTag()) ? nmsStack.getTag() : new NBTTagCompound();
NBTTagList modifiers = new NBTTagList();
NBTTagCompound armor = new NBTTagCompound();
armor.set("AttributeName", new NBTTagString("generic.armor"));
armor.set("Name", new NBTTagString("generic.armor"));
armor.set("Amount", new NBTTagInt(armorProtection));
armor.set("Operation", new NBTTagInt(0));
armor.set("UUIDLeast", new NBTTagInt(894654));
armor.set("UUIDMost", new NBTTagInt(2872));
armor.set("Slot", new NBTTagString("chest"));
modifiers.add(armor);
NBTTagCompound armorTough = new NBTTagCompound();
armorTough.set("AttributeName", new NBTTagString("generic.armorToughness"));
armorTough.set("Name", new NBTTagString("generic.armorToughness"));
armorTough.set("Amount", new NBTTagInt(armorToughness));
armorTough.set("Operation", new NBTTagInt(0));
armorTough.set("UUIDLeast", new NBTTagInt(894654));
armorTough.set("UUIDMost", new NBTTagInt(2872));
armorTough.set("Slot", new NBTTagString("chest"));
modifiers.add(armorTough);
if (unbreakable)
compound.set("Unbreakable", new NBTTagByte((byte) 1));
compound.set("AttributeModifiers", modifiers);
item = CraftItemStack.asBukkitCopy(nmsStack);
return item;
}
// Get the armor tier of the supplied item.
@Override
public ArmorTier getArmorTier(ItemStack item)
{
if (item == null)
return ArmorTier.NONE;
if (item.getType() != Material.ELYTRA)
return ArmorTier.NONE;
// Get the NBT tags from the item.
NBTTagCompound compound = CraftItemStack.asNMSCopy(item).getTag();
if (compound == null)
return ArmorTier.NONE;
String nbtTags = compound.toString();
// Check if the item has the generic.armor attribute.
// Format = <level>,Slot:"chest",AttributeName:"generic.armor so get pos of char before
// The start of the string, as that's the value of the generic.armor attribute.
int pos = nbtTags.indexOf(",Slot:\"chest\",AttributeName:\"generic.armor\"");
int armorValue = 0;
if (pos > 0)
{
// If so, get the value of the generic.armor attribute.
pos--;
String stringAtPos = nbtTags.substring(pos, pos + 1);
armorValue = Integer.parseInt(stringAtPos);
}
else
// Otherwise, the item has no armor, so return 0;
return ArmorTier.NONE;
switch (armorValue)
{
case 3:
return ArmorTier.LEATHER;
case 5:
return ArmorTier.GOLD;
case 6:
return ArmorTier.IRON;
case 8:
return ArmorTier.DIAMOND;
default:
return ArmorTier.NONE;
}
}
}

View File

@ -0,0 +1,117 @@
package nl.pim16aap2.armoredElytra.nms;
import java.util.Arrays;
import org.bukkit.Material;
import org.bukkit.craftbukkit.v1_9_R2.inventory.CraftItemStack;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import net.minecraft.server.v1_9_R2.NBTTagByte;
import net.minecraft.server.v1_9_R2.NBTTagCompound;
import net.minecraft.server.v1_9_R2.NBTTagInt;
import net.minecraft.server.v1_9_R2.NBTTagList;
import net.minecraft.server.v1_9_R2.NBTTagString;
import nl.pim16aap2.armoredElytra.ArmoredElytra;
import nl.pim16aap2.armoredElytra.util.ArmorTier;
public class NBTEditor_V1_9_R2 implements NBTEditor
{
private ArmoredElytra plugin;
// Get the names and lores for every tier of armor.
public NBTEditor_V1_9_R2(ArmoredElytra plugin)
{
this.plugin = plugin;
}
// Add armor to the supplied item, based on the armorTier.
@Override
public ItemStack addArmorNBTTags(ItemStack item, ArmorTier armorTier, boolean unbreakable)
{
ItemMeta itemmeta = item.getItemMeta();
int armorProtection = ArmorTier.getArmor(armorTier);
int armorToughness = ArmorTier.getToughness(armorTier);
itemmeta.setDisplayName(plugin.getArmoredElytrName(armorTier));
if (plugin.getElytraLore() != null)
itemmeta.setLore(Arrays.asList(plugin.fillInArmorTierInStringNoColor(plugin.getElytraLore(), armorTier)));
item.setItemMeta(itemmeta);
net.minecraft.server.v1_9_R2.ItemStack nmsStack = CraftItemStack.asNMSCopy(item);
NBTTagCompound compound = (nmsStack.hasTag()) ? nmsStack.getTag() : new NBTTagCompound();
NBTTagList modifiers = new NBTTagList();
NBTTagCompound armor = new NBTTagCompound();
armor.set("AttributeName", new NBTTagString("generic.armor"));
armor.set("Name", new NBTTagString("generic.armor"));
armor.set("Amount", new NBTTagInt(armorProtection));
armor.set("Operation", new NBTTagInt(0));
armor.set("UUIDLeast", new NBTTagInt(894654));
armor.set("UUIDMost", new NBTTagInt(2872));
armor.set("Slot", new NBTTagString("chest"));
modifiers.add(armor);
NBTTagCompound armorTough = new NBTTagCompound();
armorTough.set("AttributeName", new NBTTagString("generic.armorToughness"));
armorTough.set("Name", new NBTTagString("generic.armorToughness"));
armorTough.set("Amount", new NBTTagInt(armorToughness));
armorTough.set("Operation", new NBTTagInt(0));
armorTough.set("UUIDLeast", new NBTTagInt(894654));
armorTough.set("UUIDMost", new NBTTagInt(2872));
armorTough.set("Slot", new NBTTagString("chest"));
modifiers.add(armorTough);
if (unbreakable)
compound.set("Unbreakable", new NBTTagByte((byte) 1));
compound.set("AttributeModifiers", modifiers);
item = CraftItemStack.asBukkitCopy(nmsStack);
return item;
}
// Get the armor tier of the supplied item.
@Override
public ArmorTier getArmorTier(ItemStack item)
{
if (item == null)
return ArmorTier.NONE;
if (item.getType() != Material.ELYTRA)
return ArmorTier.NONE;
// Get the NBT tags from the item.
NBTTagCompound compound = CraftItemStack.asNMSCopy(item).getTag();
if (compound == null)
return ArmorTier.NONE;
String nbtTags = compound.toString();
// Check if the item has the generic.armor attribute.
// Format = <level>,Slot:"chest",AttributeName:"generic.armor so get pos of char before
// The start of the string, as that's the value of the generic.armor attribute.
int pos = nbtTags.indexOf(",Slot:\"chest\",AttributeName:\"generic.armor\"");
int armorValue = 0;
if (pos > 0)
{
// If so, get the value of the generic.armor attribute.
pos--;
String stringAtPos = nbtTags.substring(pos, pos + 1);
armorValue = Integer.parseInt(stringAtPos);
}
else
// Otherwise, the item has no armor, so return 0;
return ArmorTier.NONE;
switch (armorValue)
{
case 3:
return ArmorTier.LEATHER;
case 5:
return ArmorTier.GOLD;
case 6:
return ArmorTier.IRON;
case 8:
return ArmorTier.DIAMOND;
default:
return ArmorTier.NONE;
}
}
}

View File

@ -0,0 +1,6 @@
package nl.pim16aap2.armoredElytra.util;
public enum AllowedToWearEnum
{
BROKEN, NOPERMISSION, ALLOWED
}

View File

@ -71,7 +71,7 @@ public class Messages
value = this.messageMap.get(key);
if (value == null)
{
value = "BigDoors: Translation not found! Contact server admin!";
value = "ArmoredElytra: Translation not found! Contact server admin!";
plugin.myLogger(Level.WARNING, "Failed to get translation for key " + key);
}
return value;

View File

@ -1,13 +1,90 @@
package nl.pim16aap2.armoredElytra.util;
import java.util.Map;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
public class Util
{
// Remove item from player's chestplate slot and puts it in their normal inventory.
public static void unenquipChestPlayer(Player p)
{
p.getInventory().addItem(p.getInventory().getChestplate());
p.getInventory().getChestplate().setAmount(0);
}
{
// Check if an item is broken or not.
public static boolean isBroken(ItemStack item)
{
return item.getDurability() >= item.getType().getMaxDurability();
}
// Get the armor tier from a chest plate.
public static ArmorTier armorToTier(Material item)
{
ArmorTier ret = ArmorTier.NONE;
switch (item)
{
case LEATHER_CHESTPLATE:
ret = ArmorTier.LEATHER;
break;
case GOLD_CHESTPLATE:
ret = ArmorTier.GOLD;
break;
case CHAINMAIL_CHESTPLATE:
ret = ArmorTier.CHAIN;
break;
case IRON_CHESTPLATE:
ret = ArmorTier.IRON;
break;
case DIAMOND_CHESTPLATE:
ret = ArmorTier.DIAMOND;
break;
default:
break;
}
return ret;
}
// Check if mat is a chest plate.
public static boolean isChestPlate(Material mat)
{
if (mat == Material.LEATHER_CHESTPLATE || mat == Material.GOLD_CHESTPLATE ||
mat == Material.CHAINMAIL_CHESTPLATE || mat == Material.IRON_CHESTPLATE ||
mat == Material.DIAMOND_CHESTPLATE)
return true;
return false;
}
// Function that returns which/how many protection enchantments there are.
public static int getProtectionEnchantmentsVal(Map<Enchantment, Integer> enchantments)
{
int ret = 0;
if (enchantments.containsKey(Enchantment.PROTECTION_ENVIRONMENTAL))
ret += 1;
if (enchantments.containsKey(Enchantment.PROTECTION_EXPLOSIONS))
ret += 2;
if (enchantments.containsKey(Enchantment.PROTECTION_FALL))
ret += 4;
if (enchantments.containsKey(Enchantment.PROTECTION_FIRE))
ret += 8;
if (enchantments.containsKey(Enchantment.PROTECTION_PROJECTILE))
ret += 16;
return ret;
}
public static boolean playerHasCraftPerm(Player player, ArmorTier armorTier)
{
return ((armorTier == ArmorTier.LEATHER && player.hasPermission("armoredelytra.craft.leather")) ||
(armorTier == ArmorTier.GOLD && player.hasPermission("armoredelytra.craft.gold" )) ||
(armorTier == ArmorTier.CHAIN && player.hasPermission("armoredelytra.craft.chain" )) ||
(armorTier == ArmorTier.IRON && player.hasPermission("armoredelytra.craft.iron" )) ||
(armorTier == ArmorTier.DIAMOND && player.hasPermission("armoredelytra.craft.diamond")));
}
public static boolean playerHasWearPerm(Player player, ArmorTier armorTier)
{
return ((armorTier == ArmorTier.LEATHER && player.hasPermission("armoredelytra.wear.leather" )) ||
(armorTier == ArmorTier.GOLD && player.hasPermission("armoredelytra.wear.gold" )) ||
(armorTier == ArmorTier.CHAIN && player.hasPermission("armoredelytra.wear.chain" )) ||
(armorTier == ArmorTier.IRON && player.hasPermission("armoredelytra.wear.iron" )) ||
(armorTier == ArmorTier.DIAMOND && player.hasPermission("armoredelytra.wear.diamond" )));
}
}

View File

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