- Code improvements.

- You can now customize name and lore for armored elytras (or not have any lore at all) in the config file. %ARMOR_TIER% works here as well.
- Fixed some minor bugs with incorrect strings being printed sometimes.
This commit is contained in:
Pim van der Loos 2017-09-29 22:08:41 +02:00
parent 6d707c34a8
commit 23889c0845
8 changed files with 169 additions and 110 deletions

View File

@ -1,7 +1,7 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>nl.pim16aap2</groupId> <groupId>nl.pim16aap2</groupId>
<artifactId>ArmoredElytra</artifactId> <artifactId>ArmoredElytra</artifactId>
<version>1.6.0-SNAPSHOT</version> <version>1.7.0-SNAPSHOT</version>
<repositories> <repositories>

View File

@ -33,6 +33,8 @@ public class ArmoredElytra extends JavaPlugin implements Listener
private String elytraReceivedMessage; private String elytraReceivedMessage;
private boolean checkForUpdates; private boolean checkForUpdates;
private boolean upToDate; private boolean upToDate;
private String elytraName;
private String elytraLore;
@Override @Override
public void onEnable() public void onEnable()
@ -47,6 +49,10 @@ public class ArmoredElytra extends JavaPlugin implements Listener
"PROTECTION_PROJECTILE","PROTECTION_ENVIRONMENTAL","THORNS"}); "PROTECTION_PROJECTILE","PROTECTION_ENVIRONMENTAL","THORNS"});
config.addDefault("usageDeniedMessage", "You do not have the required permissions to wear %ARMOR_TIER% armored elytras!"); config.addDefault("usageDeniedMessage", "You do not have the required permissions to wear %ARMOR_TIER% armored elytras!");
config.addDefault("elytraReceivedMessage", "A(n) %ARMOR_TIER% armored elytra has been bestowed upon you!"); config.addDefault("elytraReceivedMessage", "A(n) %ARMOR_TIER% armored elytra has been bestowed upon you!");
config.addDefault("elytraName", "%ARMOR_TIER% Armored Elytra");
config.addDefault("elytraLore", "Elytra with %ARMOR_TIER% level protection.");
config.addDefault("checkForUpdates", true); config.addDefault("checkForUpdates", true);
saveDefaultConfig(); saveDefaultConfig();
@ -59,11 +65,13 @@ public class ArmoredElytra extends JavaPlugin implements Listener
allowedEnchants = list.toArray(new String[0]); allowedEnchants = list.toArray(new String[0]);
usageDeniedMessage = config.getString("usageDeniedMessage"); usageDeniedMessage = config.getString("usageDeniedMessage");
elytraReceivedMessage = config.getString("elytraReceivedMessage"); elytraReceivedMessage = config.getString("elytraReceivedMessage");
elytraName = config.getString("elytraName");
elytraLore = config.getString("elytraLore");
checkForUpdates = config.getBoolean("checkForUpdates"); checkForUpdates = config.getBoolean("checkForUpdates");
usageDeniedMessage = (Objects.equals(usageDeniedMessage, new String("NONE")) ? null : usageDeniedMessage); usageDeniedMessage = (Objects.equals(usageDeniedMessage, new String("NONE")) ? null : usageDeniedMessage);
elytraReceivedMessage = (Objects.equals(elytraReceivedMessage, new String("NONE")) ? null : elytraReceivedMessage); elytraReceivedMessage = (Objects.equals(elytraReceivedMessage, new String("NONE")) ? null : elytraReceivedMessage);
elytraLore = (Objects.equals(elytraLore, new String("NONE")) ? null : elytraLore);
// Check if the user allows checking for updates. // Check if the user allows checking for updates.
if (checkForUpdates) if (checkForUpdates)
@ -122,25 +130,31 @@ public class ArmoredElytra extends JavaPlugin implements Listener
messagePlayer(player, ChatColor.WHITE, s); messagePlayer(player, ChatColor.WHITE, s);
} }
// Convert int of armorTier to its string.
public String armorTierToString(int armorTier) public String armorTierToString(int armorTier)
{ {
String armorTierName = null; String armorTierName = null;
switch(armorTier) switch(armorTier)
{ {
case 0: case 0:
armorTierName = "unarmored"; armorTierName = "Unarmored";
break;
case 1: case 1:
armorTierName = "leather"; armorTierName = "Leather";
break;
case 2: case 2:
armorTierName = "gold"; armorTierName = "Gold";
break;
case 3: case 3:
armorTierName = "chain"; armorTierName = "Chain";
break;
case 4: case 4:
armorTierName = "iron"; armorTierName = "Iron";
break;
case 5: case 5:
armorTierName = "diamond"; armorTierName = "Diamond";
break;
} }
return armorTierName; return armorTierName;
} }
@ -149,8 +163,7 @@ public class ArmoredElytra extends JavaPlugin implements Listener
{ {
if (usageDeniedMessage != null) if (usageDeniedMessage != null)
{ {
String armorTierName = armorTierToString(armorTier); String message = fillInArmorTierInString(usageDeniedMessage, armorTier);
String message = usageDeniedMessage.replace("%ARMOR_TIER%", armorTierName);
messagePlayer(player, ChatColor.RED, message); messagePlayer(player, ChatColor.RED, message);
} }
} }
@ -160,12 +173,19 @@ public class ArmoredElytra extends JavaPlugin implements Listener
{ {
if (elytraReceivedMessage != null) if (elytraReceivedMessage != null)
{ {
String armorTierName = armorTierToString(armorTier); String message = fillInArmorTierInString(elytraReceivedMessage, armorTier);
String message = elytraReceivedMessage.replace("%ARMOR_TIER%", armorTierName);
messagePlayer(player, ChatColor.GREEN, message); messagePlayer(player, ChatColor.GREEN, message);
} }
} }
// Replace %ARMOR_TIER% by the name of that armor tier in a string.
public String fillInArmorTierInString(String string, int armorTier)
{
String armorTierName = armorTierToString(armorTier);
String replaced = string.replace("%ARMOR_TIER%", armorTierName);
return replaced;
}
// Print a string to the log. // Print a string to the log.
public void myLogger(Level level, String s) public void myLogger(Level level, String s)
{ {
@ -206,47 +226,37 @@ public class ArmoredElytra extends JavaPlugin implements Listener
// Leather armor. // Leather armor.
if (tier.equalsIgnoreCase("leather")) if (tier.equalsIgnoreCase("leather"))
{ {
if (player.hasPermission("armoredelytra.give.leather")) armorTier = 1;
{ if (player.hasPermission("armoredelytra.give.leather"))
armorTier = 1;
allowed = true; allowed = true;
}
// Gold armor. // Gold armor.
} else if (tier.equalsIgnoreCase("gold")) } else if (tier.equalsIgnoreCase("gold"))
{ {
if (player.hasPermission("armoredelytra.give.gold")) armorTier = 2;
{ if (player.hasPermission("armoredelytra.give.gold"))
armorTier = 2;
allowed = true; allowed = true;
}
// Chain armor. // Chain armor.
} else if (tier.equalsIgnoreCase("chain")) } else if (tier.equalsIgnoreCase("chain"))
{ {
armorTier = 3;
if (player.hasPermission("armoredelytra.give.chain")) if (player.hasPermission("armoredelytra.give.chain"))
{
armorTier = 3;
allowed = true; allowed = true;
}
// Iron armor. // Iron armor.
} else if (tier.equalsIgnoreCase("iron")) } else if (tier.equalsIgnoreCase("iron"))
{ {
if (player.hasPermission("armoredelytra.give.iron")) armorTier = 4;
{ if (player.hasPermission("armoredelytra.give.iron"))
armorTier = 4;
allowed = true; allowed = true;
}
// Diamond armor. // Diamond armor.
} else if (tier.equalsIgnoreCase("diamond")) } else if (tier.equalsIgnoreCase("diamond"))
{ {
if (player.hasPermission("armoredelytra.give.diamond")) armorTier = 5;
{ if (player.hasPermission("armoredelytra.give.diamond"))
armorTier = 5;
allowed = true; allowed = true;
}
} else } else
{ {
messagePlayer(player, "Not a supported armor tier! Try one of these: leather, gold, chain, iron, diamond."); messagePlayer(player, "Not a supported armor tier! Try one of these: leather, gold, chain, iron, diamond.");
@ -337,11 +347,11 @@ public class ArmoredElytra extends JavaPlugin implements Listener
if (version.equals("v1_11_R1")) if (version.equals("v1_11_R1"))
{ {
nbtEditor = new NBTEditor_V1_11_R1(); nbtEditor = new NBTEditor_V1_11_R1(elytraName, elytraLore, this);
} else if (version.equals("v1_12_R1")) } else if (version.equals("v1_12_R1"))
{ {
nbtEditor = new NBTEditor_V1_12_R1(); nbtEditor = new NBTEditor_V1_12_R1(elytraName, elytraLore, this);
} }
// Return true if compatible. // Return true if compatible.
return nbtEditor != null; return nbtEditor != null;

View File

@ -89,20 +89,12 @@ public class EventHandlers implements Listener
return false; return false;
} }
// Check if the elytra being checked is an armored one. // Check if the elytra being checked is an armored one.
public boolean isArmoredElytra(ItemStack elytra) public boolean isArmoredElytra(ItemStack elytra)
{ {
if (elytra.hasItemMeta() && elytra.getType() == Material.ELYTRA) return (nbtEditor.getArmorTier(elytra) == 0 ? false : true);
if (elytra.getItemMeta().hasLore())
if (elytra.getItemMeta().getLore().toString().equals("[This is an armored Elytra.]"))
{
return true;
}
return false;
} }
// Copy enchants of 2 items to one item. // Copy enchants of 2 items to one item.
public ItemStack addEnchants(ItemStack itemOne, ItemStack itemTwo, Player player) public ItemStack addEnchants(ItemStack itemOne, ItemStack itemTwo, Player player)
{ {
@ -419,7 +411,6 @@ public class EventHandlers implements Listener
} }
} }
// Remove item from player's chestplate slot and puts it in their normal inventory. // Remove item from player's chestplate slot and puts it in their normal inventory.
public void unenquipChestPlayer(Player p) public void unenquipChestPlayer(Player p)
{ {

View File

@ -5,6 +5,6 @@ import org.bukkit.inventory.ItemStack;
public interface NBTEditor public interface NBTEditor
{ {
public ItemStack addArmorNBTTags(ItemStack item, int armorTier); public ItemStack addArmorNBTTags(ItemStack item, int armorTier);
public int getArmorTier(ItemStack item); public int getArmorTier(ItemStack item);
} }

View File

@ -11,10 +11,23 @@ import net.minecraft.server.v1_11_R1.NBTTagCompound;
import net.minecraft.server.v1_11_R1.NBTTagInt; import net.minecraft.server.v1_11_R1.NBTTagInt;
import net.minecraft.server.v1_11_R1.NBTTagList; import net.minecraft.server.v1_11_R1.NBTTagList;
import net.minecraft.server.v1_11_R1.NBTTagString; import net.minecraft.server.v1_11_R1.NBTTagString;
import nl.pim16aap2.armoredElytra.ArmoredElytra;
public class NBTEditor_V1_11_R1 implements NBTEditor public class NBTEditor_V1_11_R1 implements NBTEditor
{ {
String elytraName;
String elytraLore;
ArmoredElytra plugin;
// Get the names and lores for every tier of armor.
public NBTEditor_V1_11_R1(String elytraName, String elytraLore, ArmoredElytra plugin)
{
this.elytraName = elytraName;
this.elytraLore = elytraLore;
this.plugin = plugin;
}
// Add armor to the supplied item, based on the armorTier.
@Override @Override
public ItemStack addArmorNBTTags(ItemStack item, int armorTier) public ItemStack addArmorNBTTags(ItemStack item, int armorTier)
{ {
@ -56,9 +69,12 @@ public class NBTEditor_V1_11_R1 implements NBTEditor
default: default:
color = ChatColor.WHITE; color = ChatColor.WHITE;
} }
itemmeta.setDisplayName(color+"Armored Elytra");
itemmeta.setLore(Arrays.asList("This is an armored Elytra.")); itemmeta.setDisplayName(color+plugin.fillInArmorTierInString(elytraName, armorTier));
if (elytraLore != null)
itemmeta.setLore(Arrays.asList(plugin.fillInArmorTierInString(elytraLore, armorTier)));
item.setItemMeta(itemmeta); item.setItemMeta(itemmeta);
net.minecraft.server.v1_11_R1.ItemStack nmsStack = CraftItemStack.asNMSCopy(item); net.minecraft.server.v1_11_R1.ItemStack nmsStack = CraftItemStack.asNMSCopy(item);
NBTTagCompound compound = (nmsStack.hasTag()) ? nmsStack.getTag() : new NBTTagCompound(); NBTTagCompound compound = (nmsStack.hasTag()) ? nmsStack.getTag() : new NBTTagCompound();
NBTTagList modifiers = new NBTTagList(); NBTTagList modifiers = new NBTTagList();
@ -70,7 +86,8 @@ public class NBTEditor_V1_11_R1 implements NBTEditor
armor.set("UUIDLeast", new NBTTagInt(894654)); armor.set("UUIDLeast", new NBTTagInt(894654));
armor.set("UUIDMost", new NBTTagInt(2872)); armor.set("UUIDMost", new NBTTagInt(2872));
armor.set("Slot", new NBTTagString("chest")); armor.set("Slot", new NBTTagString("chest"));
modifiers.add(armor); modifiers.add(armor);
NBTTagCompound armorTough = new NBTTagCompound(); NBTTagCompound armorTough = new NBTTagCompound();
armorTough.set("AttributeName", new NBTTagString("generic.armorToughness")); armorTough.set("AttributeName", new NBTTagString("generic.armorToughness"));
armorTough.set("Name", new NBTTagString("generic.armorToughness")); armorTough.set("Name", new NBTTagString("generic.armorToughness"));
@ -80,45 +97,52 @@ public class NBTEditor_V1_11_R1 implements NBTEditor
armorTough.set("UUIDMost", new NBTTagInt(2872)); armorTough.set("UUIDMost", new NBTTagInt(2872));
armorTough.set("Slot", new NBTTagString("chest")); armorTough.set("Slot", new NBTTagString("chest"));
modifiers.add(armorTough); modifiers.add(armorTough);
compound.set("AttributeModifiers", modifiers); compound.set("AttributeModifiers", modifiers);
item = CraftItemStack.asBukkitCopy(nmsStack); item = CraftItemStack.asBukkitCopy(nmsStack);
return item; return item;
} }
// Get the armor tier of the item. // Get the armor tier of the supplied item.
@Override
public int getArmorTier(ItemStack item) public int getArmorTier(ItemStack item)
{ {
ItemStack itemTest = item.clone(); int armorTier = 0;
itemTest = addArmorNBTTags(itemTest, 1); int armorValue = 0;
if (itemTest.equals(item))
{
return 1;
}
itemTest = addArmorNBTTags(itemTest, 2); // Get the NBT tags from the item.
if (itemTest.equals(item)) NBTTagCompound compound = CraftItemStack.asNMSCopy(item).getTag();
{ if (compound == null)
return 2; return 0;
} String nbtTags = compound.toString();
itemTest = addArmorNBTTags(itemTest, 3); // Check if the item has the generic.armor attribute.
if (itemTest.equals(item)) int pos = nbtTags.indexOf(",Slot:\"chest\",AttributeName:\"generic.armor\"");
if (pos > 0)
{ {
return 3; // 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 0;
itemTest = addArmorNBTTags(itemTest, 4); switch (armorValue)
if (itemTest.equals(item))
{ {
return 4; case 3:
armorTier = 1;
break;
case 5:
armorTier = 2;
break;
case 6:
armorTier = 4;
break;
case 8:
armorTier = 5;
break;
} }
return armorTier;
itemTest = addArmorNBTTags(itemTest, 5);
if (itemTest.equals(item))
{
return 5;
}
return 0;
} }
} }

View File

@ -11,9 +11,23 @@ import net.minecraft.server.v1_12_R1.NBTTagCompound;
import net.minecraft.server.v1_12_R1.NBTTagInt; import net.minecraft.server.v1_12_R1.NBTTagInt;
import net.minecraft.server.v1_12_R1.NBTTagList; import net.minecraft.server.v1_12_R1.NBTTagList;
import net.minecraft.server.v1_12_R1.NBTTagString; import net.minecraft.server.v1_12_R1.NBTTagString;
import nl.pim16aap2.armoredElytra.ArmoredElytra;
public class NBTEditor_V1_12_R1 implements NBTEditor public class NBTEditor_V1_12_R1 implements NBTEditor
{ {
String elytraName;
String elytraLore;
ArmoredElytra plugin;
// Get the names and lores for every tier of armor.
public NBTEditor_V1_12_R1(String elytraName, String elytraLore, ArmoredElytra plugin)
{
this.elytraName = elytraName;
this.elytraLore = elytraLore;
this.plugin = plugin;
}
// Add armor to the supplied item, based on the armorTier.
@Override @Override
public ItemStack addArmorNBTTags(ItemStack item, int armorTier) public ItemStack addArmorNBTTags(ItemStack item, int armorTier)
{ {
@ -55,9 +69,12 @@ public class NBTEditor_V1_12_R1 implements NBTEditor
default: default:
color = ChatColor.WHITE; color = ChatColor.WHITE;
} }
itemmeta.setDisplayName(color+"Armored Elytra");
itemmeta.setLore(Arrays.asList("This is an armored Elytra.")); itemmeta.setDisplayName(color+plugin.fillInArmorTierInString(elytraName, armorTier));
if (elytraLore != null)
itemmeta.setLore(Arrays.asList(plugin.fillInArmorTierInString(elytraLore, armorTier)));
item.setItemMeta(itemmeta); item.setItemMeta(itemmeta);
net.minecraft.server.v1_12_R1.ItemStack nmsStack = CraftItemStack.asNMSCopy(item); net.minecraft.server.v1_12_R1.ItemStack nmsStack = CraftItemStack.asNMSCopy(item);
NBTTagCompound compound = (nmsStack.hasTag()) ? nmsStack.getTag() : new NBTTagCompound(); NBTTagCompound compound = (nmsStack.hasTag()) ? nmsStack.getTag() : new NBTTagCompound();
NBTTagList modifiers = new NBTTagList(); NBTTagList modifiers = new NBTTagList();
@ -69,7 +86,8 @@ public class NBTEditor_V1_12_R1 implements NBTEditor
armor.set("UUIDLeast", new NBTTagInt(894654)); armor.set("UUIDLeast", new NBTTagInt(894654));
armor.set("UUIDMost", new NBTTagInt(2872)); armor.set("UUIDMost", new NBTTagInt(2872));
armor.set("Slot", new NBTTagString("chest")); armor.set("Slot", new NBTTagString("chest"));
modifiers.add(armor); modifiers.add(armor);
NBTTagCompound armorTough = new NBTTagCompound(); NBTTagCompound armorTough = new NBTTagCompound();
armorTough.set("AttributeName", new NBTTagString("generic.armorToughness")); armorTough.set("AttributeName", new NBTTagString("generic.armorToughness"));
armorTough.set("Name", new NBTTagString("generic.armorToughness")); armorTough.set("Name", new NBTTagString("generic.armorToughness"));
@ -79,44 +97,52 @@ public class NBTEditor_V1_12_R1 implements NBTEditor
armorTough.set("UUIDMost", new NBTTagInt(2872)); armorTough.set("UUIDMost", new NBTTagInt(2872));
armorTough.set("Slot", new NBTTagString("chest")); armorTough.set("Slot", new NBTTagString("chest"));
modifiers.add(armorTough); modifiers.add(armorTough);
compound.set("AttributeModifiers", modifiers); compound.set("AttributeModifiers", modifiers);
item = CraftItemStack.asBukkitCopy(nmsStack); item = CraftItemStack.asBukkitCopy(nmsStack);
return item; return item;
} }
// Get the armor tier of the item. // Get the armor tier of the supplied item.
@Override
public int getArmorTier(ItemStack item) public int getArmorTier(ItemStack item)
{ {
ItemStack itemTest = item.clone(); int armorTier = 0;
itemTest = addArmorNBTTags(itemTest, 1); int armorValue = 0;
if (itemTest.equals(item))
{
return 1;
}
itemTest = addArmorNBTTags(itemTest, 2); // Get the NBT tags from the item.
if (itemTest.equals(item)) NBTTagCompound compound = CraftItemStack.asNMSCopy(item).getTag();
{ if (compound == null)
return 2; return 0;
} String nbtTags = compound.toString();
itemTest = addArmorNBTTags(itemTest, 3); // Check if the item has the generic.armor attribute.
if (itemTest.equals(item)) int pos = nbtTags.indexOf(",Slot:\"chest\",AttributeName:\"generic.armor\"");
if (pos > 0)
{ {
return 3; // 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 0;
itemTest = addArmorNBTTags(itemTest, 4); switch (armorValue)
if (itemTest.equals(item))
{ {
return 4; case 3:
armorTier = 1;
break;
case 5:
armorTier = 2;
break;
case 6:
armorTier = 4;
break;
case 8:
armorTier = 5;
break;
} }
return armorTier;
itemTest = addArmorNBTTags(itemTest, 5);
if (itemTest.equals(item))
{
return 5;
}
return 0;
} }
} }

View File

@ -19,11 +19,19 @@ allowedEnchantments:
- PROTECTION_ENVIRONMENTAL - PROTECTION_ENVIRONMENTAL
- THORNS - THORNS
# Message players receive when they lack the required permissions to wear a certain armor tier. "NONE" = no message # Message players receive when they lack the required permissions to wear a certain armor tier. "NONE" = no message.
# %ARMOR_TIER% is replaced by the name of the armor tier.
usageDeniedMessage: "You do not have the required permissions to wear %ARMOR_TIER% armored elytras!" usageDeniedMessage: "You do not have the required permissions to wear %ARMOR_TIER% armored elytras!"
# Message players receive when they are given an armored elytra using commands. "NONE" = no message # Message players receive when they are given an armored elytra using commands. "NONE" = no message.
# %ARMOR_TIER% is replaced by the name of the armor tier.
elytraReceivedMessage: "A(n) %ARMOR_TIER% armored elytra has been bestowed upon you!" elytraReceivedMessage: "A(n) %ARMOR_TIER% armored elytra has been bestowed upon you!"
# The name of armored elytras. %ARMOR_TIER% is replaced by the name of the armor tier.
elytraName: '%ARMOR_TIER% Armored Elytra'
# The lore of armored elytras. "NONE" = no lore. %ARMOR_TIER% is replaced by the name of the armor tier.
elytraLore: Elytra with %ARMOR_TIER% level protection.
# Allow this plugin to check for updates on startup. It will not download new versions! # Allow this plugin to check for updates on startup. It will not download new versions!
checkForUpdates: true checkForUpdates: true

View File

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