Use reflection for multi-version support.

- Switched to using reflection for multi-version support of Minecraft to make supporting it a little easier. Shouldn't require an update for 1.14 anymore now (unless they change the format or something).
- Code style enforced.
This commit is contained in:
Pim van der Loos 2019-04-15 12:04:59 +02:00
parent 07e3b65f2b
commit da224bfc1a
No known key found for this signature in database
GPG Key ID: C16F020ADAE6D5A8
19 changed files with 669 additions and 1272 deletions

View File

@ -16,6 +16,7 @@
</repositories>
<dependencies>
<!--
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-1.9</artifactId>
@ -39,7 +40,7 @@
<artifactId>spigot-1.11</artifactId>
<version>1.11.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependency> -->
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-1.12</artifactId>

View File

@ -15,14 +15,7 @@ import nl.pim16aap2.armoredElytra.handlers.EventHandlers;
import nl.pim16aap2.armoredElytra.handlers.FlyDurabilityHandler;
import nl.pim16aap2.armoredElytra.handlers.LoginHandler;
import nl.pim16aap2.armoredElytra.handlers.Uninstaller;
import nl.pim16aap2.armoredElytra.nms.NBTEditor;
import nl.pim16aap2.armoredElytra.nms.NBTEditor_V1_10_R1;
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.nbtEditor.NBTEditor;
import nl.pim16aap2.armoredElytra.util.ArmorTier;
import nl.pim16aap2.armoredElytra.util.ConfigLoader;
import nl.pim16aap2.armoredElytra.util.Messages;
@ -31,7 +24,6 @@ import nl.pim16aap2.armoredElytra.util.Update;
public class ArmoredElytra extends JavaPlugin implements Listener
{
// TODO: Merge EventHandlers and EventHandlers_V1.9.
private NBTEditor nbtEditor;
private Messages messages;
private ConfigLoader config;
@ -48,79 +40,71 @@ public class ArmoredElytra extends JavaPlugin implements Listener
@Override
public void onEnable()
{
this.readConfigValues();
this.messages = new Messages(this);
this.readMessages();
readConfigValues();
messages = new Messages(this);
readMessages();
// Check if the user allows checking for updates.
if (config.getBool("checkForUpdates"))
{
// Check for updates in a new thread, so the server won't hang when it cannot contact the update servers.
Thread thread = new Thread(new Runnable()
final Thread thread = new Thread(() ->
{
@Override
public void run()
{
ArmoredElytra plugin = getPlugin();
Update update = new Update(278437, plugin);
String latestVersion = update.getLatestVersion();
final ArmoredElytra plugin = getPlugin();
final Update update = new Update(278437, plugin);
final String latestVersion = update.getLatestVersion();
if (latestVersion == null)
plugin.myLogger(Level.WARNING, "Encountered problem contacting update servers! Please check manually! The error above does not affect the plugin!");
if (latestVersion == null)
plugin.myLogger(Level.WARNING, "Encountered problem contacting update servers! Please check manually! The error above does not affect the plugin!");
else
{
final String thisVersion = plugin.getDescription().getVersion();
// Check if this is the latest version or not.
final int updateStatus = update.versionCompare(latestVersion, thisVersion);
if (updateStatus > 0)
{
// Load the loginHandler to show messages to the user when they join.
Bukkit.getPluginManager().registerEvents(new LoginHandler(plugin, "The Armored Elytra plugin is out of date!"), plugin);
plugin.myLogger(Level.INFO, "Plugin out of date! You are using version " + thisVersion + " but the latest version is version " + latestVersion + "!");
plugin.setUpToDate(false);
}
else
{
String thisVersion = plugin.getDescription().getVersion();
// Check if this is the latest version or not.
int updateStatus = update.versionCompare(latestVersion, thisVersion);
if (updateStatus > 0)
{
// Load the loginHandler to show messages to the user when they join.
Bukkit.getPluginManager().registerEvents(new LoginHandler(plugin, "The Armored Elytra plugin is out of date!"), plugin);
plugin.myLogger(Level.INFO, "Plugin out of date! You are using version " + thisVersion + " but the latest version is version " + latestVersion + "!");
plugin.setUpToDate(false);
}
else
{
plugin.setUpToDate(true);
plugin.myLogger(Level.INFO, "You seem to be using the latest version of this plugin!");
}
plugin.setUpToDate(true);
plugin.myLogger(Level.INFO, "You seem to be using the latest version of this plugin!");
}
}
});
thread.start();
}
else
myLogger(Level.INFO, "Plugin update checking not enabled! You will not receive any messages about new updates for this plugin. Please consider turning this on in the config.");
// Are stats allowed?
if (config.getBool("allowStats"))
{
myLogger(Level.INFO, "Enabling stats! Thanks, it really helps!");
@SuppressWarnings("unused")
final
Metrics metrics = new Metrics(this);
}
else
// Y u do dis? :(
myLogger(Level.INFO, "Stats disabled, not laoding stats :(... Please consider enabling it! I am a simple man, seeing higher user numbers helps me stay motivated!");
myLogger(Level.INFO, "Stats disabled, not loading stats :(... Please consider enabling it! I am a simple man, seeing higher user numbers helps me stay motivated!");
this.locale = config.getString("languageFile");
locale = config.getString("languageFile");
// Load the files for the correct version of Minecraft.
if (compatibleMCVer())
{
// 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);
Bukkit.getPluginManager().registerEvents(new EventHandlers(this, nbtEditor, is1_9), this);
getCommand("ArmoredElytra").setExecutor(new CommandHandler(this, nbtEditor));
}
else
{
myLogger(Level.WARNING, "Trying to load the plugin on an incompatible version of Minecraft! This plugin will NOT be enabled!");
Bukkit.getPluginManager().registerEvents(new LoginHandler(this, "The Armored Elytra plugin failed to start correctly! Please send the startup log to pim16aap2!"), this);
myLogger(Level.WARNING, "Plugin failed to load! Either your version isn't supported or something went horribly wrong! Please contact pim16aap2!");
return;
}
@ -138,7 +122,7 @@ public class ArmoredElytra extends JavaPlugin implements Listener
// Log all allowed enchantments.
myLogger(Level.INFO, ("Allowed enchantments:"));
for (String s : config.getStringList("allowedEnchantments"))
for (final String s : config.getStringList("allowedEnchantments"))
myLogger(Level.INFO, " - " + s);
}
else
@ -151,34 +135,34 @@ public class ArmoredElytra extends JavaPlugin implements Listener
public void readConfigValues()
{
// Load the settings from the config file.
this.config = new ConfigLoader(this);
config = new ConfigLoader(this);
// Check if the plugin should go into uninstall mode.
this.uninstallMode = config.getBool("uninstallMode");
uninstallMode = config.getBool("uninstallMode");
}
public Messages getMyMessages()
{
return this.messages;
return messages;
}
private void readMessages()
{
// Replace color codes by the corresponding colors.
this.usageDeniedMessage = this.getMyMessages().getString("MESSAGES.UsageDenied" ).replaceAll("&((?i)[0-9a-fk-or])", "\u00A7$1");
this.elytraReceivedMessage = this.getMyMessages().getString("MESSAGES.ElytraReceived").replaceAll("&((?i)[0-9a-fk-or])", "\u00A7$1");
this.elytraLore = this.getMyMessages().getString("MESSAGES.Lore" ).replaceAll("&((?i)[0-9a-fk-or])", "\u00A7$1");
usageDeniedMessage = getMyMessages().getString("MESSAGES.UsageDenied" ).replaceAll("&((?i)[0-9a-fk-or])", "\u00A7$1");
elytraReceivedMessage = getMyMessages().getString("MESSAGES.ElytraReceived").replaceAll("&((?i)[0-9a-fk-or])", "\u00A7$1");
elytraLore = getMyMessages().getString("MESSAGES.Lore" ).replaceAll("&((?i)[0-9a-fk-or])", "\u00A7$1");
this.leatherName = this.getMyMessages().getString("TIER.Leather" ).replaceAll("&((?i)[0-9a-fk-or])", "\u00A7$1");
this.goldName = this.getMyMessages().getString("TIER.Gold" ).replaceAll("&((?i)[0-9a-fk-or])", "\u00A7$1");
this.chainName = this.getMyMessages().getString("TIER.Chain" ).replaceAll("&((?i)[0-9a-fk-or])", "\u00A7$1");
this.ironName = this.getMyMessages().getString("TIER.Iron" ).replaceAll("&((?i)[0-9a-fk-or])", "\u00A7$1");
this.diamondName = this.getMyMessages().getString("TIER.Diamond" ).replaceAll("&((?i)[0-9a-fk-or])", "\u00A7$1");
leatherName = getMyMessages().getString("TIER.Leather" ).replaceAll("&((?i)[0-9a-fk-or])", "\u00A7$1");
goldName = getMyMessages().getString("TIER.Gold" ).replaceAll("&((?i)[0-9a-fk-or])", "\u00A7$1");
chainName = getMyMessages().getString("TIER.Chain" ).replaceAll("&((?i)[0-9a-fk-or])", "\u00A7$1");
ironName = getMyMessages().getString("TIER.Iron" ).replaceAll("&((?i)[0-9a-fk-or])", "\u00A7$1");
diamondName = getMyMessages().getString("TIER.Diamond" ).replaceAll("&((?i)[0-9a-fk-or])", "\u00A7$1");
// Change the string to null if it says "NONE".
this.usageDeniedMessage = (Objects.equals(usageDeniedMessage, new String("NONE")) ? null : usageDeniedMessage );
this.elytraReceivedMessage = (Objects.equals(elytraReceivedMessage, new String("NONE")) ? null : elytraReceivedMessage);
this.elytraLore = (Objects.equals(elytraLore, new String("NONE")) ? null : elytraLore );
usageDeniedMessage = (Objects.equals(usageDeniedMessage, new String("NONE")) ? null : usageDeniedMessage );
elytraReceivedMessage = (Objects.equals(elytraReceivedMessage, new String("NONE")) ? null : elytraReceivedMessage);
elytraLore = (Objects.equals(elytraLore, new String("NONE")) ? null : elytraLore );
}
// Returns true if this is the latest version of this plugin.
@ -216,7 +200,7 @@ public class ArmoredElytra extends JavaPlugin implements Listener
{
if (usageDeniedMessage != null)
{
String message = fillInArmorTierInStringNoColor(usageDeniedMessage, armorTier);
final String message = fillInArmorTierInStringNoColor(usageDeniedMessage, armorTier);
messagePlayer(player, ChatColor.RED, message);
}
}
@ -226,7 +210,7 @@ public class ArmoredElytra extends JavaPlugin implements Listener
{
if (elytraReceivedMessage != null)
{
String message = fillInArmorTierInStringNoColor(elytraReceivedMessage, armorTier);
final String message = fillInArmorTierInStringNoColor(elytraReceivedMessage, armorTier);
messagePlayer(player, ChatColor.GREEN, message);
}
}
@ -245,7 +229,7 @@ public class ArmoredElytra extends JavaPlugin implements Listener
// Print a string to the log.
public void myLogger(Level level, String str)
{
Bukkit.getLogger().log(level, "[" + this.getName() + "] " + str);
Bukkit.getLogger().log(level, "[" + getName() + "] " + str);
}
// Log message that only gets printed when debugging is enabled in the config file.
@ -265,44 +249,13 @@ public class ArmoredElytra extends JavaPlugin implements Listener
// Check + initialize for the correct version of Minecraft.
public boolean compatibleMCVer()
{
String version;
try
{
version = Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3];
}
catch (ArrayIndexOutOfBoundsException useAVersionMentionedInTheDescriptionPleaseException)
{
return false;
}
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);
else if (version.equals("v1_12_R1"))
nbtEditor = new NBTEditor_V1_12_R1(this);
else if (version.equals("v1_13_R1"))
nbtEditor = new NBTEditor_V1_13_R1(this);
else if (version.equals("v1_13_R2"))
nbtEditor = new NBTEditor_V1_13_R2(this);
// Return true if compatible.
return nbtEditor != null;
nbtEditor = new NBTEditor(this);
return nbtEditor.succes();
}
public String getElytraLore()
{
return this.elytraLore;
return elytraLore;
}
public String getArmoredElytrName(ArmorTier tier)
@ -311,19 +264,19 @@ public class ArmoredElytra extends JavaPlugin implements Listener
switch(tier)
{
case LEATHER:
ret = this.leatherName;
ret = leatherName;
break;
case GOLD:
ret = this.goldName;
ret = goldName;
break;
case CHAIN:
ret = this.chainName;
ret = chainName;
break;
case IRON:
ret = this.ironName;
ret = ironName;
break;
case DIAMOND:
ret = this.diamondName;
ret = diamondName;
break;
default:
ret = "NONE";

View File

@ -12,157 +12,148 @@ import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import nl.pim16aap2.armoredElytra.ArmoredElytra;
import nl.pim16aap2.armoredElytra.nms.NBTEditor;
import nl.pim16aap2.armoredElytra.nbtEditor.NBTEditor;
import nl.pim16aap2.armoredElytra.util.ArmorTier;
public class CommandHandler implements CommandExecutor
{
ArmoredElytra plugin;
NBTEditor nbtEditor;
private final ArmoredElytra plugin;
private final NBTEditor nbtEditor;
public CommandHandler(ArmoredElytra plugin, NBTEditor nbtEditor)
{
this.plugin = plugin;
this.nbtEditor = nbtEditor;
}
public CommandHandler(ArmoredElytra plugin, NBTEditor nbtEditor)
{
this.plugin = plugin;
this.nbtEditor = nbtEditor;
}
@Override
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
{
Player player;
Player player;
if (sender instanceof Player)
{
player = (Player) sender;
if (sender instanceof Player)
{
player = (Player) sender;
if (plugin.getUninstallMode())
{
plugin.messagePlayer(player, plugin.getMyMessages().getString("MESSAGES.UninstallMode"));
return true;
}
if (plugin.getUninstallMode())
{
plugin.messagePlayer(player, plugin.getMyMessages().getString("MESSAGES.UninstallMode"));
return true;
}
if (cmd.getName().equalsIgnoreCase("ArmoredElytra"))
{
if (args.length == 1 || args.length == 2)
{
ItemStack newElytra = null;
String tier = null;
Player receiver;
boolean allowed = false;
ArmorTier armorTier = ArmorTier.NONE;
if (args.length == 1)
{
receiver = player;
tier = args[0];
}
else
{
receiver = Bukkit.getPlayer(args[0]);
if (receiver == null)
{
plugin.messagePlayer(player, ChatColor.RED, "Player \"" + args[0] + "\" not found!");
return true;
}
tier = args[1];
}
if (cmd.getName().equalsIgnoreCase("ArmoredElytra"))
if (args.length == 1 || args.length == 2)
{
ItemStack newElytra = null;
String tier = null;
Player receiver;
boolean allowed = false;
ArmorTier armorTier = ArmorTier.NONE;
if (args.length == 1)
{
receiver = player;
tier = args[0];
}
else
{
receiver = Bukkit.getPlayer(args[0]);
if (receiver == null)
{
plugin.messagePlayer(player, ChatColor.RED, "Player \"" + args[0] + "\" not found!");
return true;
}
tier = args[1];
}
// Leather armor.
if (tier.equalsIgnoreCase("leather"))
{
armorTier = ArmorTier.LEATHER;
if (player.hasPermission("armoredelytra.give.leather"))
allowed = true;
if (tier.equalsIgnoreCase("leather"))
{
armorTier = ArmorTier.LEATHER;
if (player.hasPermission("armoredelytra.give.leather"))
allowed = true;
}
else if (tier.equalsIgnoreCase("gold"))
{
armorTier = ArmorTier.GOLD;
if (player.hasPermission("armoredelytra.give.gold"))
allowed = true;
}
else if (tier.equalsIgnoreCase("chain"))
{
armorTier = ArmorTier.CHAIN;
if (player.hasPermission("armoredelytra.give.chain"))
allowed = true;
}
else if (tier.equalsIgnoreCase("iron"))
{
armorTier = ArmorTier.IRON;
if (player.hasPermission("armoredelytra.give.iron"))
allowed = true;
}
else if (tier.equalsIgnoreCase("diamond"))
{
armorTier = ArmorTier.DIAMOND;
if (player.hasPermission("armoredelytra.give.diamond"))
allowed = true;
}
else
plugin.messagePlayer(player, plugin.getMyMessages().getString("MESSAGES.UnsupportedTier"));
// Gold armor.
}
else if (tier.equalsIgnoreCase("gold"))
{
armorTier = ArmorTier.GOLD;
if (player.hasPermission("armoredelytra.give.gold"))
allowed = true;
if (allowed)
{
plugin.elytraReceivedMessage(receiver, armorTier);
newElytra = nbtEditor.addArmorNBTTags(new ItemStack(Material.ELYTRA, 1), armorTier, plugin.getConfigLoader().getBool("unbreakable"));
plugin.giveArmoredElytraToPlayer(receiver, newElytra);
}
else
plugin.messagePlayer(player, plugin.fillInArmorTierInStringNoColor(plugin.getMyMessages().getString("MESSAGES.UnsupportedTier"), armorTier));
return true;
}
}
else
{
if (plugin.getUninstallMode())
{
plugin.myLogger(Level.INFO, "Plugin in uninstall mode! New Armored Elytras are not allowed!");
return true;
}
// Chain armor.
}
else if (tier.equalsIgnoreCase("chain"))
{
armorTier = ArmorTier.CHAIN;
if (player.hasPermission("armoredelytra.give.chain"))
allowed = true;
if (args.length == 2)
{
ItemStack newElytra = null;
String tier = args[1];
if (Bukkit.getPlayer(args[0]) != null)
{
player = Bukkit.getPlayer(args[0]);
ArmorTier armorTier = ArmorTier.NONE;
// Iron armor.
}
else if (tier.equalsIgnoreCase("iron"))
{
armorTier = ArmorTier.IRON;
if (player.hasPermission("armoredelytra.give.iron"))
allowed = true;
if (tier.equalsIgnoreCase("leather"))
armorTier = ArmorTier.LEATHER;
else if (tier.equalsIgnoreCase("gold"))
armorTier = ArmorTier.GOLD;
else if (tier.equalsIgnoreCase("chain"))
armorTier = ArmorTier.CHAIN;
else if (tier.equalsIgnoreCase("iron"))
armorTier = ArmorTier.IRON;
else if (tier.equalsIgnoreCase("diamond"))
armorTier = ArmorTier.DIAMOND;
else
// TODO: Return a more informative message.
return false;
// Diamond armor.
}
else if (tier.equalsIgnoreCase("diamond"))
{
armorTier = ArmorTier.DIAMOND;
if (player.hasPermission("armoredelytra.give.diamond"))
allowed = true;
}
else
plugin.messagePlayer(player, plugin.getMyMessages().getString("MESSAGES.UnsupportedTier"));
if (allowed)
{
plugin.elytraReceivedMessage(receiver, armorTier);
newElytra = nbtEditor.addArmorNBTTags(new ItemStack(Material.ELYTRA, 1), armorTier, plugin.getConfigLoader().getBool("unbreakable"));
plugin.giveArmoredElytraToPlayer(receiver, newElytra);
}
else
plugin.messagePlayer(player, plugin.fillInArmorTierInStringNoColor(plugin.getMyMessages().getString("MESSAGES.UnsupportedTier"), armorTier));
return true;
}
}
}
else
{
if (plugin.getUninstallMode())
{
plugin.myLogger(Level.INFO, "Plugin in uninstall mode! New Armored Elytras are not allowed!");
return true;
}
if (args.length == 2)
{
ItemStack newElytra = null;
String tier = args[1];
if (Bukkit.getPlayer(args[0]) != null)
{
player = Bukkit.getPlayer(args[0]);
ArmorTier armorTier = ArmorTier.NONE;
if (tier.equalsIgnoreCase("leather"))
armorTier = ArmorTier.LEATHER;
else if (tier.equalsIgnoreCase("gold"))
armorTier = ArmorTier.GOLD;
else if (tier.equalsIgnoreCase("chain"))
armorTier = ArmorTier.CHAIN;
else if (tier.equalsIgnoreCase("iron"))
armorTier = ArmorTier.IRON;
else if (tier.equalsIgnoreCase("diamond"))
armorTier = ArmorTier.DIAMOND;
// TODO: Catch user requesting non-existent tier.
plugin.elytraReceivedMessage(player, armorTier);
newElytra = nbtEditor.addArmorNBTTags(new ItemStack(Material.ELYTRA, 1), armorTier, plugin.getConfigLoader().getBool("unbreakable"));
plugin.giveArmoredElytraToPlayer(player, newElytra);
plugin.myLogger(Level.INFO, ("Giving an armored elytra of the " + ArmorTier.getArmor(armorTier) + " armor tier to player " + player.getName()));
return true;
}
else
{
plugin.myLogger(Level.INFO, ("Player " + args[1] + " not found!"));
return true;
}
}
}
return false;
plugin.elytraReceivedMessage(player, armorTier);
newElytra = nbtEditor.addArmorNBTTags(new ItemStack(Material.ELYTRA, 1), armorTier, plugin.getConfigLoader().getBool("unbreakable"));
plugin.giveArmoredElytraToPlayer(player, newElytra);
plugin.myLogger(Level.INFO, ("Giving an armored elytra of the " + ArmorTier.getArmor(armorTier) + " armor tier to player " + player.getName()));
return true;
}
else
{
plugin.myLogger(Level.INFO, ("Player " + args[1] + " not found!"));
return true;
}
}
}
return false;
}
}

View File

@ -24,7 +24,7 @@ import org.bukkit.inventory.meta.EnchantmentStorageMeta;
import org.bukkit.scheduler.BukkitRunnable;
import nl.pim16aap2.armoredElytra.ArmoredElytra;
import nl.pim16aap2.armoredElytra.nms.NBTEditor;
import nl.pim16aap2.armoredElytra.nbtEditor.NBTEditor;
import nl.pim16aap2.armoredElytra.util.Action;
import nl.pim16aap2.armoredElytra.util.AllowedToWearEnum;
import nl.pim16aap2.armoredElytra.util.ArmorTier;
@ -32,14 +32,14 @@ import nl.pim16aap2.armoredElytra.util.Util;
public class EventHandlers implements Listener
{
private int DIAMONDS_TO_FULL;
private int LEATHER_TO_FULL;
private int GOLD_TO_FULL;
private int IRON_TO_FULL;
private NBTEditor nbtEditor;
private final ArmoredElytra plugin;
private List<String> allowedEnchantments;
private boolean is1_9;
private final int DIAMONDS_TO_FULL;
private final int LEATHER_TO_FULL;
private final int GOLD_TO_FULL;
private final int IRON_TO_FULL;
private final NBTEditor nbtEditor;
private final ArmoredElytra plugin;
private final List<String> allowedEnchantments;
private final boolean is1_9;
public EventHandlers(ArmoredElytra plugin, NBTEditor nbtEditor, boolean is1_9)
{
@ -48,11 +48,11 @@ public class EventHandlers implements Listener
this.is1_9 = is1_9;
// Get the values of the config options.
this.allowedEnchantments = plugin.getConfigLoader().getStringList("allowedEnchantments");
this.LEATHER_TO_FULL = plugin.getConfigLoader().getInt("leatherRepair");
this.GOLD_TO_FULL = plugin.getConfigLoader().getInt("goldRepair");
this.IRON_TO_FULL = plugin.getConfigLoader().getInt("ironRepair");
this.DIAMONDS_TO_FULL = plugin.getConfigLoader().getInt("diamondsRepair");
allowedEnchantments = plugin.getConfigLoader().getStringList("allowedEnchantments");
LEATHER_TO_FULL = plugin.getConfigLoader().getInt("leatherRepair");
GOLD_TO_FULL = plugin.getConfigLoader().getInt("goldRepair");
IRON_TO_FULL = plugin.getConfigLoader().getInt("ironRepair");
DIAMONDS_TO_FULL = plugin.getConfigLoader().getInt("diamondsRepair");
}
// Remove item from player's chestplate slot and puts it in their normal inventory.
@ -100,7 +100,7 @@ public class EventHandlers implements Listener
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));
Map<Enchantment, Integer> combined = new HashMap<>(fixEnchantments(enchantments0));
// If the second set of enchantments is null, the combined enchantments are just the first enchantments.
if (enchantments1 == null)
@ -137,7 +137,6 @@ public class EventHandlers implements Listener
// 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)
{
case 1:
@ -156,7 +155,6 @@ public class EventHandlers implements Listener
combined.remove(Enchantment.PROTECTION_PROJECTILE);
break;
}
}
return combined;
}
@ -185,7 +183,7 @@ public class EventHandlers implements Listener
// Remove any disallowed enchantments in the map.
private Map<Enchantment, Integer> fixEnchantments(Map<Enchantment, Integer> enchantments)
{
Map<Enchantment, Integer> ret = new HashMap<Enchantment, Integer>(enchantments);
Map<Enchantment, Integer> ret = new HashMap<>(enchantments);
for (Map.Entry<Enchantment, Integer> entry : enchantments.entrySet())
if (!isAllowedEnchantment(entry.getKey()))
ret.remove(entry.getKey());
@ -271,7 +269,6 @@ public class EventHandlers implements Listener
ItemStack result = null;
if (itemA != null && itemB != null)
{
// If itemB is the elytra, while itemA isn't, switch itemA and itemB.
if (itemB.getType() == Material.ELYTRA && itemA.getType() != Material.ELYTRA)
{
@ -280,7 +277,6 @@ public class EventHandlers implements Listener
itemB = result;
result = null;
}
}
// Check if there are items in both input slots.
if (itemA != null && itemB != null)
@ -395,7 +391,7 @@ public class EventHandlers implements Listener
else
player.setItemOnCursor(result);
// Clean the anvil's inventory after transferring the items.
this.cleanAnvil(anvilInventory);
cleanAnvil(anvilInventory);
player.updateInventory();
return;
}
@ -431,7 +427,7 @@ public class EventHandlers implements Listener
ArmorTier armorTier = nbtEditor.getArmorTier(item);
if (nbtEditor.getArmorTier(item) == ArmorTier.NONE)
return;
AllowedToWearEnum allowed = this.isAllowedToWear(item, player, armorTier);
AllowedToWearEnum allowed = isAllowedToWear(item, player, armorTier);
if (allowed == AllowedToWearEnum.BROKEN)
{
plugin.messagePlayer(player, plugin.getMyMessages().getString("MESSAGES.RepairNeeded"));
@ -468,16 +464,16 @@ public class EventHandlers implements Listener
if (armorTier == ArmorTier.NONE)
return;
AllowedToWearEnum allowed = this.isAllowedToWear(chestplate, player, armorTier);
AllowedToWearEnum allowed = isAllowedToWear(chestplate, player, armorTier);
if (allowed == AllowedToWearEnum.BROKEN)
{
plugin.messagePlayer(player, plugin.getMyMessages().getString("MESSAGES.RepairNeeded"));
this.unenquipChestPlayer(player);
unenquipChestPlayer(player);
}
else if (allowed == AllowedToWearEnum.NOPERMISSION)
{
plugin.usageDeniedMessage(player, armorTier);
this.unenquipChestPlayer(player);
unenquipChestPlayer(player);
}
}
@ -507,7 +503,7 @@ public class EventHandlers implements Listener
{
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));
int newDurability = 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))
@ -519,7 +515,7 @@ public class EventHandlers implements Listener
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);
unenquipChestPlayer(p);
else
newDurability = durability + durabilityDelta;
}
@ -527,7 +523,7 @@ public class EventHandlers implements Listener
if (newDurability >= maxDurability)
{
newDurability = maxDurability;
this.unenquipChestPlayer(p);
unenquipChestPlayer(p);
}
elytra.setDurability((short) (newDurability));
}
@ -549,6 +545,6 @@ public class EventHandlers implements Listener
{
verifyArmorInChestSlot(player);
}
}.runTaskLater(this.plugin, 1);
}.runTaskLater(plugin, 1);
}
}

View File

@ -5,20 +5,20 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerItemDamageEvent;
import nl.pim16aap2.armoredElytra.nms.NBTEditor;
import nl.pim16aap2.armoredElytra.nbtEditor.NBTEditor;
import nl.pim16aap2.armoredElytra.util.ArmorTier;
public class FlyDurabilityHandler implements Listener
{
private NBTEditor nbtEditor;
private final NBTEditor nbtEditor;
public FlyDurabilityHandler(NBTEditor nbtEditor)
{
this.nbtEditor = nbtEditor;
}
// Do not decrease elytra durability while flying.
// This also cancels durability decrease when it should while flying, but that shouldn't really matter.
// Do not decrease elytra durability while flying. This also cancels durability decrease when
// it should (i.e. getting hit) while flying, but I don't really care.
@EventHandler
public void onItemDamage(PlayerItemDamageEvent e)
{

View File

@ -11,9 +11,8 @@ import nl.pim16aap2.armoredElytra.ArmoredElytra;
public class LoginHandler implements Listener
{
ArmoredElytra plugin;
String message;
private final ArmoredElytra plugin;
private final String message;
public LoginHandler(ArmoredElytra plugin, String message)
{
@ -26,7 +25,6 @@ public class LoginHandler implements Listener
{
Player player = event.getPlayer();
if (player.hasPermission("armoredElytra.admin"))
{
// Slight delay so the player actually receives the message;
new BukkitRunnable()
{
@ -35,7 +33,6 @@ public class LoginHandler implements Listener
{
plugin.messagePlayer(player, ChatColor.AQUA, message);
}
}.runTaskLater(this.plugin, 10);
}
}.runTaskLater(plugin, 10);
}
}

View File

@ -13,13 +13,13 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable;
import nl.pim16aap2.armoredElytra.ArmoredElytra;
import nl.pim16aap2.armoredElytra.nms.NBTEditor;
import nl.pim16aap2.armoredElytra.nbtEditor.NBTEditor;
import nl.pim16aap2.armoredElytra.util.ArmorTier;
public class Uninstaller implements Listener
{
ArmoredElytra plugin;
NBTEditor nbtEditor;
private final ArmoredElytra plugin;
private final NBTEditor nbtEditor;
public Uninstaller(ArmoredElytra plugin, NBTEditor nbtEditor)
{
@ -45,7 +45,6 @@ public class Uninstaller implements Listener
public void onChestOpen(InventoryOpenEvent event)
{
if (event.getInventory().getType().equals(InventoryType.CHEST))
{
// Slight delay so the inventory has time to get loaded.
new BukkitRunnable()
{
@ -57,8 +56,7 @@ public class Uninstaller implements Listener
if (removed != 0)
plugin.messagePlayer((Player) (event.getPlayer()), ChatColor.RED, "Removed " + removed + " armored elytras from your chest!");
}
}.runTaskLater(this.plugin, 20);
}
}.runTaskLater(plugin, 20);
}
@EventHandler
@ -73,8 +71,8 @@ public class Uninstaller implements Listener
Inventory inv = event.getPlayer().getInventory();
int removed = removeArmoredElytras(inv);
if (removed != 0)
plugin.messagePlayer((Player) (event.getPlayer()), ChatColor.RED, "Removed " + removed + " armored elytras from your inventory!");
plugin.messagePlayer(event.getPlayer(), ChatColor.RED, "Removed " + removed + " armored elytras from your inventory!");
}
}.runTaskLater(this.plugin, 20);
}.runTaskLater(plugin, 20);
}
}

View File

@ -0,0 +1,6 @@
package nl.pim16aap2.armoredElytra.nbtEditor;
public interface GetArmorValue
{
public int armorValueFromNBTString(String nbtString);
}

View File

@ -0,0 +1,34 @@
package nl.pim16aap2.armoredElytra.nbtEditor;
import java.util.logging.Level;
import nl.pim16aap2.armoredElytra.ArmoredElytra;
public class GetArmorValueNew implements GetArmorValue
{
private final ArmoredElytra plugin;
public GetArmorValueNew(ArmoredElytra plugin)
{
this.plugin = plugin;
}
@Override
public int armorValueFromNBTString(String nbtString)
{
int pos = nbtString.indexOf(",Slot:\"chest\",AttributeName:\"generic.armor\"");
if (pos > 0)
try
{
String stringAtPos = nbtString.substring(pos - 4, pos - 1);
return (int) Double.parseDouble(stringAtPos);
}
catch (Exception e)
{
plugin.myLogger(Level.INFO, "Failed to obtain armor value from NBT!");
return 0;
}
else
return 0;
}
}

View File

@ -0,0 +1,28 @@
package nl.pim16aap2.armoredElytra.nbtEditor;
import nl.pim16aap2.armoredElytra.ArmoredElytra;
public class GetArmorValueOld implements GetArmorValue
{
private final ArmoredElytra plugin;
public GetArmorValueOld(ArmoredElytra plugin)
{
this.plugin = plugin;
}
@Override
public int armorValueFromNBTString(String nbtString)
{
int pos = nbtString.indexOf(",Slot:\"chest\",AttributeName:\"generic.armor\"");
if (pos > 0)
{
// If so, get the value of the generic.armor attribute.
pos--;
String stringAtPos = nbtString.substring(pos, pos + 1);
return Integer.parseInt(stringAtPos);
}
else
return 0;
}
}

View File

@ -0,0 +1,232 @@
package nl.pim16aap2.armoredElytra.nbtEditor;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import nl.pim16aap2.armoredElytra.ArmoredElytra;
import nl.pim16aap2.armoredElytra.util.ArmorTier;
public class NBTEditor
{
private final ArmoredElytra plugin;
private final String NMSbase;
private final String CraftBase;
private Method asNMSCopy;
private Method asBukkitCopy;
private Class<?> NMSItemStack;
private Class<?> CraftItemStack;
private Class<?> NBTTagCompound;
private Class<?> NBTTagList;
private Class<?> NBTBase;
private Class<?> NBTTagString;
private Class<?> NBTTagByte;
private Class<?> NBTTagInt;
private Method hasTag;
private Method getTag;
private Method addCompound;
private Method setTag;
private Method setCompoundByte;
private Method setCompoundTagList;
private Constructor<?> NBTTagStringCtor;
private Constructor<?> NBTTagByteCtor;
private Constructor<?> NBTTagIntCtor;
private boolean success = false;
private GetArmorValue getArmorValue;
public NBTEditor(ArmoredElytra plugin)
{
this.plugin = plugin;
final String versionString = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
NMSbase = "net.minecraft.server." + versionString + ".";
CraftBase = "org.bukkit.craftbukkit." + versionString + ".";
constructNMSClasses();
getTagReadingMethod();
}
private void getTagReadingMethod()
{
if (!success)
return;
String version;
try
{
version = Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3];
}
catch (final ArrayIndexOutOfBoundsException useAVersionMentionedInTheDescriptionPleaseException)
{
success = false;
return;
}
// Old versions use the old format. It is assumed here that all versions from 1.13.2 on will use the
// new format. Might want to put this in a nice interface instead of an ugly boolean.
// Spigot's 1.13.1 uses the old format, but 1.13.2 uses the new format. They share the same version number though.
if (version.equals("v1_9_R1" ) || version.equals("v1_9_R2" ) || version.equals("v1_10_R1") ||
version.equals("v1_11_R1") || version.equals("v1_12_R1") || version.equals("v1_13_R1") ||
version.equals("v1_13_R2") && Bukkit.getVersion().split(" ")[2].equals("1.13.1)"))
getArmorValue = new GetArmorValueOld(plugin);
else
getArmorValue = new GetArmorValueNew(plugin);
}
public boolean succes()
{
return success;
}
private void constructNMSClasses()
{
try
{
NMSItemStack = getNMSClass("ItemStack");
hasTag = NMSItemStack.getMethod("hasTag");
getTag = NMSItemStack.getMethod("getTag");
CraftItemStack = getCraftClass("inventory.CraftItemStack");
asNMSCopy = CraftItemStack.getMethod("asNMSCopy", ItemStack.class);
asBukkitCopy = CraftItemStack.getMethod("asBukkitCopy", NMSItemStack);
NBTBase = getNMSClass("NBTBase");
NBTTagString = getNMSClass("NBTTagString");
NBTTagStringCtor = NBTTagString.getConstructor(String.class);
NBTTagByte = getNMSClass("NBTTagByte");
NBTTagByteCtor = NBTTagByte.getConstructor(byte.class);
NBTTagInt = getNMSClass("NBTTagInt");
NBTTagIntCtor = NBTTagInt.getConstructor(int.class);
NBTTagCompound = getNMSClass("NBTTagCompound");
setTag = NBTTagCompound.getMethod("set", String.class, NBTBase);
NBTTagList = getNMSClass("NBTTagList");
addCompound = NBTTagList.getMethod("add", NBTBase);
setCompoundTagList = NBTTagCompound.getMethod("set", String.class, NBTBase);
setCompoundByte = NBTTagCompound.getMethod("set", String.class, NBTBase);
success = true;
}
catch (NoSuchMethodException | SecurityException | ClassNotFoundException e)
{
e.printStackTrace();
success = false;
}
}
// Add armor to the supplied item, based on the armorTier.
public ItemStack addArmorNBTTags(ItemStack item, ArmorTier armorTier, boolean unbreakable)
{
try
{
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);
Object nmsStack = asNMSCopy.invoke(null, item);
Object compound = ((boolean) hasTag.invoke(nmsStack) ? getTag.invoke(nmsStack) : NBTTagCompound.newInstance());
Object modifiers = NBTTagList.newInstance();
Object armor = NBTTagCompound.newInstance();
setTag.invoke (armor, "AttributeName", NBTTagStringCtor.newInstance("generic.armor"));
setTag.invoke (armor, "Name", NBTTagStringCtor.newInstance("generic.armor"));
setTag.invoke (armor, "Amount", NBTTagIntCtor.newInstance(armorProtection));
setTag.invoke (armor, "Operation", NBTTagIntCtor.newInstance(0));
setTag.invoke (armor, "UUIDLeast", NBTTagIntCtor.newInstance(894654));
setTag.invoke (armor, "UUIDMost", NBTTagIntCtor.newInstance(2872));
setTag.invoke (armor, "Slot", NBTTagStringCtor.newInstance("chest"));
addCompound.invoke(modifiers, armor);
Object armorTough = NBTTagCompound.newInstance();
setTag.invoke (armorTough, "AttributeName", NBTTagStringCtor.newInstance("generic.armorToughness"));
setTag.invoke (armorTough, "Name", NBTTagStringCtor.newInstance("generic.armorToughness"));
setTag.invoke (armorTough, "Amount", NBTTagIntCtor.newInstance(armorToughness));
setTag.invoke (armorTough, "Operation", NBTTagIntCtor.newInstance(0));
setTag.invoke (armorTough, "UUIDLeast", NBTTagIntCtor.newInstance(894654));
setTag.invoke (armorTough, "UUIDMost", NBTTagIntCtor.newInstance(2872));
setTag.invoke (armorTough, "Slot", NBTTagStringCtor.newInstance("chest"));
addCompound.invoke(modifiers, armorTough);
if (unbreakable)
setCompoundByte.invoke(compound, "Unbreakable", NBTTagByteCtor.newInstance((byte) 1));
setCompoundTagList.invoke(compound, "AttributeModifiers", modifiers);
item = (ItemStack) asBukkitCopy.invoke(null, nmsStack);
}
catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | InstantiationException e)
{
// TODO: Log this or something. Pretty serious issue for a plugin based entirely on this code.
e.printStackTrace();
}
return item;
}
// Get the armor tier of the supplied item.
public ArmorTier getArmorTier(ItemStack item)
{
try
{
if (item == null)
return ArmorTier.NONE;
if (item.getType() != Material.ELYTRA)
return ArmorTier.NONE;
// Get the NBT tags from the item.
Object compound = getTag.invoke(asNMSCopy.invoke(null, item));
if (compound == null)
return ArmorTier.NONE;
switch (getArmorValue.armorValueFromNBTString(compound.toString()))
{
case 3:
return ArmorTier.LEATHER;
case 5:
return ArmorTier.GOLD;
case 6:
return ArmorTier.IRON;
case 8:
return ArmorTier.DIAMOND;
default:
return ArmorTier.NONE;
}
}
catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e)
{
e.printStackTrace();
return null;
}
}
private Class<?> getNMSClass(String name) throws ClassNotFoundException
{
return Class.forName(NMSbase + name);
}
private Class<?> getCraftClass(String name) throws ClassNotFoundException
{
return Class.forName(CraftBase + name);
}
}

View File

@ -1,12 +0,0 @@
package nl.pim16aap2.armoredElytra.nms;
import org.bukkit.inventory.ItemStack;
import nl.pim16aap2.armoredElytra.util.ArmorTier;
public interface NBTEditor
{
public ItemStack addArmorNBTTags(ItemStack item, ArmorTier armorTier, boolean unbreakable);
public ArmorTier getArmorTier(ItemStack item);
}

View File

@ -1,117 +0,0 @@
package nl.pim16aap2.armoredElytra.nms;
import java.util.Arrays;
import org.bukkit.Material;
import org.bukkit.craftbukkit.v1_10_R1.inventory.CraftItemStack;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import net.minecraft.server.v1_10_R1.NBTTagByte;
import net.minecraft.server.v1_10_R1.NBTTagCompound;
import net.minecraft.server.v1_10_R1.NBTTagInt;
import net.minecraft.server.v1_10_R1.NBTTagList;
import net.minecraft.server.v1_10_R1.NBTTagString;
import nl.pim16aap2.armoredElytra.ArmoredElytra;
import nl.pim16aap2.armoredElytra.util.ArmorTier;
public class NBTEditor_V1_10_R1 implements NBTEditor
{
private ArmoredElytra plugin;
// Get the names and lores for every tier of armor.
public NBTEditor_V1_10_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_10_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

@ -1,117 +0,0 @@
package nl.pim16aap2.armoredElytra.nms;
import java.util.Arrays;
import org.bukkit.Material;
import org.bukkit.craftbukkit.v1_11_R1.inventory.CraftItemStack;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import net.minecraft.server.v1_11_R1.NBTTagByte;
import net.minecraft.server.v1_11_R1.NBTTagCompound;
import net.minecraft.server.v1_11_R1.NBTTagInt;
import net.minecraft.server.v1_11_R1.NBTTagList;
import net.minecraft.server.v1_11_R1.NBTTagString;
import nl.pim16aap2.armoredElytra.ArmoredElytra;
import nl.pim16aap2.armoredElytra.util.ArmorTier;
public class NBTEditor_V1_11_R1 implements NBTEditor
{
private ArmoredElytra plugin;
// Get the names and lores for every tier of armor.
public NBTEditor_V1_11_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_11_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

@ -1,117 +0,0 @@
package nl.pim16aap2.armoredElytra.nms;
import java.util.Arrays;
import org.bukkit.Material;
import org.bukkit.craftbukkit.v1_12_R1.inventory.CraftItemStack;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import net.minecraft.server.v1_12_R1.NBTTagByte;
import net.minecraft.server.v1_12_R1.NBTTagCompound;
import net.minecraft.server.v1_12_R1.NBTTagInt;
import net.minecraft.server.v1_12_R1.NBTTagList;
import net.minecraft.server.v1_12_R1.NBTTagString;
import nl.pim16aap2.armoredElytra.ArmoredElytra;
import nl.pim16aap2.armoredElytra.util.ArmorTier;
public class NBTEditor_V1_12_R1 implements NBTEditor
{
private ArmoredElytra plugin;
// Get the names and lores for every tier of armor.
public NBTEditor_V1_12_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_12_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

@ -1,121 +0,0 @@
package nl.pim16aap2.armoredElytra.nms;
import java.util.Arrays;
import org.bukkit.Material;
import org.bukkit.craftbukkit.v1_13_R1.inventory.CraftItemStack;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import net.minecraft.server.v1_13_R1.NBTTagByte;
import net.minecraft.server.v1_13_R1.NBTTagCompound;
import net.minecraft.server.v1_13_R1.NBTTagInt;
import net.minecraft.server.v1_13_R1.NBTTagList;
import net.minecraft.server.v1_13_R1.NBTTagString;
import nl.pim16aap2.armoredElytra.ArmoredElytra;
import nl.pim16aap2.armoredElytra.util.ArmorTier;
public class NBTEditor_V1_13_R1 implements NBTEditor
{
private ArmoredElytra plugin;
// Get the names and lores for every tier of armor.
public NBTEditor_V1_13_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_13_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.
// The level is now formatted as x.xd, e.g. 6.0d.
int pos = nbtTags.indexOf(",Slot:\"chest\",AttributeName:\"generic.armor\"");
int armorValue = 0;
if (pos <= 0)
return ArmorTier.NONE;
try
{
String stringAtPos = nbtTags.substring(pos - 4, pos - 1);
armorValue = (int) Double.parseDouble(stringAtPos);
}
catch (Exception e)
{
armorValue = 0;
}
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

@ -1,121 +0,0 @@
package nl.pim16aap2.armoredElytra.nms;
import java.util.Arrays;
import org.bukkit.Material;
import org.bukkit.craftbukkit.v1_13_R2.inventory.CraftItemStack;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import net.minecraft.server.v1_13_R2.NBTTagByte;
import net.minecraft.server.v1_13_R2.NBTTagCompound;
import net.minecraft.server.v1_13_R2.NBTTagInt;
import net.minecraft.server.v1_13_R2.NBTTagList;
import net.minecraft.server.v1_13_R2.NBTTagString;
import nl.pim16aap2.armoredElytra.ArmoredElytra;
import nl.pim16aap2.armoredElytra.util.ArmorTier;
public class NBTEditor_V1_13_R2 implements NBTEditor
{
private ArmoredElytra plugin;
// Get the names and lores for every tier of armor.
public NBTEditor_V1_13_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_13_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.
// The level is now formatted as x.xd, e.g. 6.0d.
int pos = nbtTags.indexOf(",Slot:\"chest\",AttributeName:\"generic.armor\"");
int armorValue = 0;
if (pos <= 0)
return ArmorTier.NONE;
try
{
String stringAtPos = nbtTags.substring(pos - 4, pos - 1);
armorValue = (int) Double.parseDouble(stringAtPos);
}
catch (Exception e)
{
armorValue = 0;
}
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

@ -1,117 +0,0 @@
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

@ -1,117 +0,0 @@
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;
}
}
}