- 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> <modelVersion>4.0.0</modelVersion>
<groupId>nl.pim16aap2</groupId> <groupId>nl.pim16aap2</groupId>
<artifactId>ArmoredElytra</artifactId> <artifactId>ArmoredElytra</artifactId>
<version>2.4.7-SNAPSHOT</version> <version>2.4.8-SNAPSHOT</version>
<repositories> <repositories>
<repository> <repository>
@ -16,6 +16,18 @@
</repositories> </repositories>
<dependencies> <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> <dependency>
<groupId>org.spigotmc</groupId> <groupId>org.spigotmc</groupId>
<artifactId>spigot-1.10</artifactId> <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_12_R1;
import nl.pim16aap2.armoredElytra.nms.NBTEditor_V1_13_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_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.ArmorTier;
import nl.pim16aap2.armoredElytra.util.ConfigLoader; import nl.pim16aap2.armoredElytra.util.ConfigLoader;
import nl.pim16aap2.armoredElytra.util.Messages; import nl.pim16aap2.armoredElytra.util.Messages;
@ -29,6 +31,7 @@ import nl.pim16aap2.armoredElytra.util.Update;
public class ArmoredElytra extends JavaPlugin implements Listener public class ArmoredElytra extends JavaPlugin implements Listener
{ {
// TODO: Merge EventHandlers and EventHandlers_V1.9.
private NBTEditor nbtEditor; private NBTEditor nbtEditor;
private Messages messages; private Messages messages;
private ConfigLoader config; private ConfigLoader config;
@ -40,6 +43,7 @@ public class ArmoredElytra extends JavaPlugin implements Listener
private String elytraLore; private String elytraLore;
private boolean upToDate; private boolean upToDate;
private String locale; private String locale;
private boolean is1_9;
@Override @Override
public void onEnable() public void onEnable()
@ -107,7 +111,11 @@ public class ArmoredElytra extends JavaPlugin implements Listener
// Load the files for the correct version of Minecraft. // Load the files for the correct version of Minecraft.
if (compatibleMCVer()) 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)); getCommand("ArmoredElytra").setExecutor(new CommandHandler(this, nbtEditor));
} }
else else
@ -268,7 +276,17 @@ public class ArmoredElytra extends JavaPlugin implements Listener
return false; return false;
} }
if (version.equals("v1_10_R1")) 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); nbtEditor = new NBTEditor_V1_10_R1(this);
else if (version.equals("v1_11_R1")) else if (version.equals("v1_11_R1"))
nbtEditor = new NBTEditor_V1_11_R1(this); nbtEditor = new NBTEditor_V1_11_R1(this);
@ -313,25 +331,6 @@ public class ArmoredElytra extends JavaPlugin implements Listener
return ret; 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) public void setUpToDate(boolean upToDate)
{ {
this.upToDate = upToDate; this.upToDate = upToDate;

View File

@ -26,6 +26,7 @@ import org.bukkit.scheduler.BukkitRunnable;
import nl.pim16aap2.armoredElytra.ArmoredElytra; import nl.pim16aap2.armoredElytra.ArmoredElytra;
import nl.pim16aap2.armoredElytra.nms.NBTEditor; import nl.pim16aap2.armoredElytra.nms.NBTEditor;
import nl.pim16aap2.armoredElytra.util.Action; import nl.pim16aap2.armoredElytra.util.Action;
import nl.pim16aap2.armoredElytra.util.AllowedToWearEnum;
import nl.pim16aap2.armoredElytra.util.ArmorTier; import nl.pim16aap2.armoredElytra.util.ArmorTier;
import nl.pim16aap2.armoredElytra.util.Util; import nl.pim16aap2.armoredElytra.util.Util;
@ -38,11 +39,13 @@ public class EventHandlers implements Listener
private NBTEditor nbtEditor; private NBTEditor nbtEditor;
private final ArmoredElytra plugin; private final ArmoredElytra plugin;
private List<String> allowedEnchantments; 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.plugin = plugin;
this.nbtEditor = nbtEditor; this.nbtEditor = nbtEditor;
this.is1_9 = is1_9;
// Get the values of the config options. // Get the values of the config options.
this.allowedEnchantments = plugin.getConfigLoader().getStringList("allowedEnchantments"); this.allowedEnchantments = plugin.getConfigLoader().getStringList("allowedEnchantments");
@ -52,16 +55,39 @@ public class EventHandlers implements Listener
this.DIAMONDS_TO_FULL = plugin.getConfigLoader().getInt("diamondsRepair"); 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)). // 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)
{
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(0).setAmount(0);
anvilInventory.getItem(1).setAmount(anvilInventory.getItem(1).getAmount() - 1); anvilInventory.getItem(1).setAmount(anvilInventory.getItem(1).getAmount() - 1);
anvilInventory.getItem(2).setAmount(0); anvilInventory.getItem(2).setAmount(0);
} }
}
// Check if the enchantment is allowed on elytras. // Check if the enchantment is allowed on elytras.
public boolean isAllowedEnchantment(Enchantment enchant) private boolean isAllowedEnchantment(Enchantment enchant)
{ {
for (String s : allowedEnchantments) for (String s : allowedEnchantments)
if (Enchantment.getByName(s) != null) if (Enchantment.getByName(s) != null)
@ -71,13 +97,15 @@ public class EventHandlers implements Listener
} }
// Combine 2 maps of enchantments (and remove any invalid ones). // 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); enchantments0 = fixEnchantments(enchantments0);
Map<Enchantment, Integer> combined = new HashMap<Enchantment, Integer>(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); enchantments1 = fixEnchantments(enchantments1);
// 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 : enchantments1.entrySet())
@ -102,8 +130,8 @@ public class EventHandlers implements Listener
} }
// Get the protection enchantment rating for both enchantment sets. // Get the protection enchantment rating for both enchantment sets.
int protVal0 = getProtectionEnchantmentsVal(enchantments0); int protVal0 = Util.getProtectionEnchantmentsVal(enchantments0);
int protVal1 = getProtectionEnchantmentsVal(enchantments1); int protVal1 = Util.getProtectionEnchantmentsVal(enchantments1);
// If they have different protection enchantments, keep enchantment1's enchantments // If they have different protection enchantments, keep enchantment1's enchantments
// And remove the protection enchantment from enchantments0. Yes, this system only works // And remove the protection enchantment from enchantments0. Yes, this system only works
@ -129,29 +157,11 @@ public class EventHandlers implements Listener
break; break;
} }
} }
}
return combined; 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 // 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. // Get the multiplier for the repair items.
double mult = 0.01; double mult = 0.01;
@ -173,7 +183,7 @@ public class EventHandlers implements Listener
} }
// Remove any disallowed enchantments in the map. // 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); Map<Enchantment, Integer> ret = new HashMap<Enchantment, Integer>(enchantments);
for (Map.Entry<Enchantment, Integer> entry : enchantments.entrySet()) 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. // 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()) for (Map.Entry<Enchantment, Integer> entry : enchantments.entrySet())
if (!isAllowedEnchantment(entry.getKey())) if (!isAllowedEnchantment(entry.getKey()))
return false; ++ret;
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;
}
return 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: * Valid inputs:
* - Elytra (armored or not) + chestplate -> Create Armored Elytra * - Elytra (armored or not) + chestplate -> Create Armored Elytra
@ -241,7 +214,7 @@ public class EventHandlers implements Listener
* - Elytra (not armored) + !chestplate -> None * - Elytra (not armored) + !chestplate -> None
* - * + * -> None * - * + * -> None
*/ */
public Action isValidInput(ItemStack itemOne, ItemStack itemTwo) private Action isValidInput(ItemStack itemOne, ItemStack itemTwo)
{ {
if (itemOne == null || itemTwo == null) if (itemOne == null || itemTwo == null)
return Action.NONE; return Action.NONE;
@ -260,7 +233,7 @@ public class EventHandlers implements Listener
Material matTwo = itemTwo.getType(); Material matTwo = itemTwo.getType();
// If the elytra is to be combined with chest armor... // If the elytra is to be combined with chest armor...
if (isChestPlate(matTwo)) if (Util.isChestPlate(matTwo))
return Action.CREATE; return Action.CREATE;
ArmorTier tier = nbtEditor.getArmorTier(itemOne); ArmorTier tier = nbtEditor.getArmorTier(itemOne);
@ -290,7 +263,7 @@ public class EventHandlers implements Listener
// Handle all anvil related stuff for this plugin. // Handle all anvil related stuff for this plugin.
@EventHandler @EventHandler
public void onAnvilInventoryOpen(PrepareAnvilEvent event) private void onAnvilInventoryOpen(PrepareAnvilEvent event)
{ {
Player player = (Player) event.getView().getPlayer(); Player player = (Player) event.getView().getPlayer();
ItemStack itemA = event.getInventory().getItem(0); ItemStack itemA = event.getInventory().getItem(0);
@ -332,7 +305,7 @@ public class EventHandlers implements Listener
enchantments = combineEnchantments(enchantments, itemB.getEnchantments()); enchantments = combineEnchantments(enchantments, itemB.getEnchantments());
break; break;
case CREATE: case CREATE:
newTier = armorToTier(itemB.getType()); newTier = Util.armorToTier(itemB.getType());
durability = 0; durability = 0;
enchantments = combineEnchantments(enchantments, itemB.getEnchantments()); enchantments = combineEnchantments(enchantments, itemB.getEnchantments());
break; break;
@ -342,7 +315,7 @@ public class EventHandlers implements Listener
durability = itemA.getDurability(); durability = itemA.getDurability();
// If there aren't any illegal enchantments on the book, continue as normal. // If there aren't any illegal enchantments on the book, continue as normal.
// Otherwise... Block. // Otherwise... Block.
if (verifyEnchantments(meta.getStoredEnchants())) if (verifyEnchantments(meta.getStoredEnchants()) != meta.getStoredEnchants().size())
{ {
enchantments = combineEnchantments(enchantments, meta.getStoredEnchants()); enchantments = combineEnchantments(enchantments, meta.getStoredEnchants());
break; break;
@ -354,7 +327,7 @@ public class EventHandlers implements Listener
return; return;
} }
if (plugin.playerHasCraftPerm(player, newTier)) if (Util.playerHasCraftPerm(player, newTier))
{ {
result = new ItemStack(Material.ELYTRA, 1); result = new ItemStack(Material.ELYTRA, 1);
if (enchantments != null) if (enchantments != null)
@ -377,12 +350,14 @@ public class EventHandlers implements Listener
@EventHandler @EventHandler
public void onInventoryClick(InventoryClickEvent e) 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. // Check if the event was a player who interacted with an anvil.
Player player = (Player) e.getWhoClicked(); Player player = (Player) e.getWhoClicked();
if (e.getView().getType() == InventoryType.ANVIL) if (e.getView().getType() != InventoryType.ANVIL)
{ return;
AnvilInventory anvilInventory; AnvilInventory anvilInventory;
// Try to cast inventory being used in the event to an anvil inventory. // Try to cast inventory being used in the event to an anvil inventory.
@ -404,7 +379,7 @@ public class EventHandlers implements Listener
{ {
ArmorTier armortier = nbtEditor.getArmorTier(anvilInventory.getItem(2)); ArmorTier armortier = nbtEditor.getArmorTier(anvilInventory.getItem(2));
// If there's an armored elytra in the final slot... // If there's an armored elytra in the final slot...
if (armortier != ArmorTier.NONE && plugin.playerHasCraftPerm(player, armortier)) if (armortier != ArmorTier.NONE && Util.playerHasCraftPerm(player, armortier))
{ {
// Create a new armored elytra and give that one to the player instead of the result. // 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. // 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.
@ -420,35 +395,54 @@ public class EventHandlers implements Listener
else else
player.setItemOnCursor(result); player.setItemOnCursor(result);
// Clean the anvil's inventory after transferring the items. // Clean the anvil's inventory after transferring the items.
cleanAnvil(anvilInventory); this.cleanAnvil(anvilInventory);
player.updateInventory(); player.updateInventory();
return; 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. // Check if the player tries to equip armor by richt clicking it.
@SuppressWarnings("deprecation")
@EventHandler @EventHandler
public void onRightClick(PlayerInteractEvent event) public void onRightClick(PlayerInteractEvent event)
{ {
Player player = event.getPlayer(); if (!event.getAction().equals(org.bukkit.event.block.Action.RIGHT_CLICK_AIR) &&
ItemStack item = player.getItemInHand(); !event.getAction().equals(org.bukkit.event.block.Action.RIGHT_CLICK_BLOCK))
return;
if (item != null) ItemStack item = event.getItem();
if (item.getType() == Material.ELYTRA && (nbtEditor.getArmorTier(item) != ArmorTier.NONE)) if (item == null)
return;
Player player = event.getPlayer();
if (item.getType() == Material.ELYTRA)
{ {
ArmorTier armorTier = nbtEditor.getArmorTier(item); ArmorTier armorTier = nbtEditor.getArmorTier(item);
if ((armorTier == ArmorTier.LEATHER && !player.hasPermission("armoredelytra.wear.leather")) || if (nbtEditor.getArmorTier(item) == ArmorTier.NONE)
(armorTier == ArmorTier.GOLD && !player.hasPermission("armoredelytra.wear.gold" )) || return;
(armorTier == ArmorTier.CHAIN && !player.hasPermission("armoredelytra.wear.chain" )) || AllowedToWearEnum allowed = this.isAllowedToWear(item, player, armorTier);
(armorTier == ArmorTier.IRON && !player.hasPermission("armoredelytra.wear.iron" )) || if (allowed == AllowedToWearEnum.BROKEN)
(armorTier == ArmorTier.DIAMOND && !player.hasPermission("armoredelytra.wear.diamond"))) {
plugin.messagePlayer(player, plugin.getMyMessages().getString("MESSAGES.RepairNeeded"));
event.setCancelled(true);
player.updateInventory();
}
else if (allowed == AllowedToWearEnum.NOPERMISSION)
{ {
plugin.usageDeniedMessage(player, armorTier); plugin.usageDeniedMessage(player, armorTier);
event.setCancelled(true); event.setCancelled(true);
player.updateInventory();
} }
} }
} }
@ -462,33 +456,28 @@ public class EventHandlers implements Listener
} }
// Check if the player is allowed to wear the armored elytra based on their permissions. // 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(); ItemStack chestplate = player.getInventory().getChestplate();
// If the player equips a new chestplate. // If the player equips a new chestplate.
if (player.getInventory().getChestplate() != null) if (player.getInventory().getChestplate() == null)
{ return;
// If that chestplate is an (armored) elytra.
if (nbtEditor.getArmorTier(chestplate) != ArmorTier.NONE)
{
ArmorTier armorTier = nbtEditor.getArmorTier(chestplate); ArmorTier armorTier = nbtEditor.getArmorTier(chestplate);
if ((chestplate.getDurability() >= chestplate.getType().getMaxDurability())) // If that chestplate is an (armored) elytra.
if (armorTier == ArmorTier.NONE)
return;
AllowedToWearEnum allowed = this.isAllowedToWear(chestplate, player, armorTier);
if (allowed == AllowedToWearEnum.BROKEN)
{ {
plugin.messagePlayer(player, plugin.getMyMessages().getString("MESSAGES.RepairNeeded")); plugin.messagePlayer(player, plugin.getMyMessages().getString("MESSAGES.RepairNeeded"));
Util.unenquipChestPlayer(player); this.unenquipChestPlayer(player);
player.updateInventory();
} }
else if ((armorTier == ArmorTier.LEATHER && !player.hasPermission("armoredelytra.wear.leather")) || else if (allowed == AllowedToWearEnum.NOPERMISSION)
(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); plugin.usageDeniedMessage(player, armorTier);
Util.unenquipChestPlayer(player); this.unenquipChestPlayer(player);
player.updateInventory();
}
}
} }
} }
@ -496,16 +485,19 @@ public class EventHandlers implements Listener
@EventHandler(ignoreCancelled = true) @EventHandler(ignoreCancelled = true)
public void onPlayerDamage(EntityDamageEvent e) public void onPlayerDamage(EntityDamageEvent e)
{ {
if(e.getEntity() instanceof Player) if(!(e.getEntity() instanceof Player))
{ return;
Player p = (Player) e.getEntity(); Player p = (Player) e.getEntity();
// If the player didn't die from the damage. // If the player didn't die from the damage.
if ((p.getHealth() - e.getFinalDamage()) > 0) if ((p.getHealth() - e.getFinalDamage()) > 0)
{ {
if (p.getInventory().getChestplate() != null) if (p.getInventory().getChestplate() == null)
{ return;
if (nbtEditor.getArmorTier(p.getInventory().getChestplate()) != ArmorTier.NONE)
{ if (nbtEditor.getArmorTier(p.getInventory().getChestplate()) == ArmorTier.NONE)
return;
ItemStack elytra = p.getInventory().getChestplate(); ItemStack elytra = p.getInventory().getChestplate();
DamageCause cause = e.getCause(); DamageCause cause = e.getCause();
@ -518,15 +510,16 @@ public class EventHandlers implements Listener
int newDurability = (int) (durability + ((int) (e.getDamage() / 4) > 1 ? (int) (e.getDamage() / 4) : 1)); 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 the elytra has the durability enchantment, we calculate the durability loss ourselves.
if (p.getInventory().getChestplate().containsEnchantment(Enchantment.DURABILITY)) { if (p.getInventory().getChestplate().containsEnchantment(Enchantment.DURABILITY))
{
// Get a random int between 0 and 100 // Get a random int between 0 and 100 to use in deciding if the durability enchantment will take effect.
Random r = new Random(); Random r = new Random();
int randomInt = r.nextInt(101); int randomInt = r.nextInt(101);
int enchantLevel = p.getInventory().getChestplate().getEnchantmentLevel(Enchantment.DURABILITY); int enchantLevel = p.getInventory().getChestplate().getEnchantmentLevel(Enchantment.DURABILITY);
int durabilityDelta = (100 / (enchantLevel + 1)) < randomInt ? 0 : 1; int durabilityDelta = (100 / (enchantLevel + 1)) < randomInt ? 0 : 1;
// If the durability equals/exceeds maxDurability, it's broken (0 = full item durability).
if (durability >= maxDurability) if (durability >= maxDurability)
Util.unenquipChestPlayer(p); this.unenquipChestPlayer(p);
else else
newDurability = durability + durabilityDelta; newDurability = durability + durabilityDelta;
} }
@ -534,22 +527,20 @@ public class EventHandlers implements Listener
if (newDurability >= maxDurability) if (newDurability >= maxDurability)
{ {
newDurability = maxDurability; newDurability = maxDurability;
Util.unenquipChestPlayer(p); this.unenquipChestPlayer(p);
} }
elytra.setDurability((short) (newDurability)); elytra.setDurability((short) (newDurability));
} }
} }
} }
}
}
}
// Check if the player is trying to equip a broken elytra (and prevent that). // Check if the player is trying to equip a broken elytra (and prevent that).
@EventHandler @EventHandler
public void playerEquipsArmor(InventoryClickEvent e) public void playerEquipsArmor(InventoryClickEvent e)
{ {
if (e.getWhoClicked() instanceof Player) if (!(e.getWhoClicked() instanceof Player))
{ return;
Player player = (Player) e.getWhoClicked(); Player player = (Player) e.getWhoClicked();
new BukkitRunnable() new BukkitRunnable()
{ {
@ -561,4 +552,3 @@ public class EventHandlers implements Listener
}.runTaskLater(this.plugin, 1); }.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); value = this.messageMap.get(key);
if (value == null) 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); plugin.myLogger(Level.WARNING, "Failed to get translation for key " + key);
} }
return value; return value;

View File

@ -1,13 +1,90 @@
package nl.pim16aap2.armoredElytra.util; 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.entity.Player;
import org.bukkit.inventory.ItemStack;
public class Util public class Util
{ {
// Remove item from player's chestplate slot and puts it in their normal inventory. // Check if an item is broken or not.
public static void unenquipChestPlayer(Player p) public static boolean isBroken(ItemStack item)
{ {
p.getInventory().addItem(p.getInventory().getChestplate()); return item.getDurability() >= item.getType().getMaxDurability();
p.getInventory().getChestplate().setAmount(0); }
// 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 name: ArmoredElytra
main: nl.pim16aap2.armoredElytra.ArmoredElytra main: nl.pim16aap2.armoredElytra.ArmoredElytra
version: 2.4.7 version: 2.4.8
author: Pim author: Pim
commands: commands:
ArmoredElytra: ArmoredElytra: