Allow renaming armored elytras in an anvil

This commit is contained in:
Pim van der Loos 2021-01-08 12:32:22 +01:00
parent abc8d7475e
commit b49f24dae4
No known key found for this signature in database
GPG Key ID: C16F020ADAE6D5A8
4 changed files with 62 additions and 17 deletions

View File

@ -6,6 +6,7 @@ import nl.pim16aap2.armoredElytra.util.ArmorTier;
import nl.pim16aap2.armoredElytra.util.EnchantmentContainer; import nl.pim16aap2.armoredElytra.util.EnchantmentContainer;
import nl.pim16aap2.armoredElytra.util.Util; import nl.pim16aap2.armoredElytra.util.Util;
import nl.pim16aap2.armoredElytra.util.XMaterial; import nl.pim16aap2.armoredElytra.util.XMaterial;
import org.bukkit.ChatColor;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
@ -16,6 +17,7 @@ import org.bukkit.event.inventory.InventoryType;
import org.bukkit.event.inventory.PrepareAnvilEvent; import org.bukkit.event.inventory.PrepareAnvilEvent;
import org.bukkit.inventory.AnvilInventory; import org.bukkit.inventory.AnvilInventory;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.logging.Level; import java.util.logging.Level;
@ -100,13 +102,12 @@ public class AnvilHandler extends ArmoredElytraHandler implements Listener
ItemStack result = null; ItemStack result = null;
if (itemA != null && itemB != null) if (itemA != null && itemB != null)
// If itemB is the elytra, while itemA isn't, switch itemA and itemB. // If itemB is the (armored) elytra, while itemA isn't, switch itemA and itemB.
if (itemB.getType() == Material.ELYTRA && itemA.getType() != Material.ELYTRA) if (itemB.getType() == Material.ELYTRA && itemA.getType() != Material.ELYTRA)
{ {
result = itemA; ItemStack tmp = itemA;
itemA = itemB; itemA = itemB;
itemB = result; itemB = tmp;
result = null;
} }
// Check if there are items in both input slots. // Check if there are items in both input slots.
@ -163,8 +164,10 @@ public class AnvilHandler extends ArmoredElytraHandler implements Listener
enchantments.applyEnchantments(result); enchantments.applyEnchantments(result);
result.setDurability(durability); result.setDurability(durability);
final String name = getElytraResultName(itemA, action, newTier, event.getInventory().getRenameText());
result = ArmoredElytra.getInstance().getNbtEditor() result = ArmoredElytra.getInstance().getNbtEditor()
.addArmorNBTTags(result, newTier, plugin.getConfigLoader().unbreakable()); .addArmorNBTTags(result, newTier, plugin.getConfigLoader().unbreakable(), name);
event.setResult(result); event.setResult(result);
return; return;
@ -178,6 +181,26 @@ public class AnvilHandler extends ArmoredElytraHandler implements Listener
event.setResult(null); event.setResult(null);
} }
private String getElytraResultName(final ItemStack baseItem, final Action action,
final ArmorTier armorTier, final String renameText)
{
final String tierName = ArmoredElytra.getInstance().getArmoredElytraName(armorTier);
if (renameText == null || !plugin.getConfigLoader().allowRenaming())
return tierName;
final ItemMeta meta = baseItem.getItemMeta();
final String currentName = meta == null ? null : meta.getDisplayName();
// When the rename text is empty, give it the default tier-name when creating a new armored elytra
// (so it's named properly) or when the current name is already the tier name (just returning the current
// name would strip the tier's color in this case).
if ((action == Action.CREATE && renameText.equals("")) ||
ChatColor.stripColor(tierName).equals(ChatColor.stripColor(renameText)))
return tierName;
return renameText.equals("") ? currentName : renameText;
}
// Let the player take items out of the anvil. // Let the player take items out of the anvil.
@EventHandler @EventHandler
public void onInventoryClick(InventoryClickEvent e) public void onInventoryClick(InventoryClickEvent e)
@ -215,14 +238,7 @@ public class AnvilHandler extends ArmoredElytraHandler implements Listener
// 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 && plugin.playerHasCraftPerm(player, armortier))
{ {
// Create a new armored elytra and give that one to the player instead of the final ItemStack result = anvilInventory.getItem(2);
// result.
// This is done because after putting item0 in AFTER item1, the first letter of
// the color code shows up, this gets rid of that problem.
ItemStack result = ArmoredElytra.getInstance().getNbtEditor()
.addArmorNBTTags(anvilInventory.getItem(2), armortier,
plugin.getConfigLoader().unbreakable());
// Give the result to the player and clear the anvil's inventory. // Give the result to the player and clear the anvil's inventory.
if (!giveItemToPlayer(player, result, e.isShiftClick())) if (!giveItemToPlayer(player, result, e.isShiftClick()))
return; return;

View File

@ -1,5 +1,6 @@
package nl.pim16aap2.armoredElytra.nbtEditor; package nl.pim16aap2.armoredElytra.nbtEditor;
import nl.pim16aap2.armoredElytra.ArmoredElytra;
import nl.pim16aap2.armoredElytra.util.ArmorTier; import nl.pim16aap2.armoredElytra.util.ArmorTier;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@ -7,7 +8,8 @@ public interface INBTEditor
{ {
/** /**
* Adds a given {@link ArmorTier} to an item. The item will be cloned. Note that setting the armor tier to {@link * Adds a given {@link ArmorTier} to an item. The item will be cloned. Note that setting the armor tier to {@link
* ArmorTier#NONE} has no effect (besides making a copy of the item). * ArmorTier#NONE} has no effect (besides making a copy of the item). The default name for the given tier is
* applied. See {@link ArmoredElytra#getArmoredElytraName(ArmorTier)}.
* *
* @param item The item. * @param item The item.
* @param armorTier The {@link ArmorTier} that will be added to it. * @param armorTier The {@link ArmorTier} that will be added to it.
@ -16,6 +18,18 @@ public interface INBTEditor
*/ */
ItemStack addArmorNBTTags(ItemStack item, ArmorTier armorTier, boolean unbreakable); ItemStack addArmorNBTTags(ItemStack item, ArmorTier armorTier, boolean unbreakable);
/**
* Adds a given {@link ArmorTier} to an item. The item will be cloned. Note that setting the armor tier to {@link
* ArmorTier#NONE} has no effect (besides making a copy of the item).
*
* @param item The item.
* @param armorTier The {@link ArmorTier} that will be added to it.
* @param unbreakable Whether the resulting item should be unbreakable.
* @param name The name fo the item.
* @return The NEW item.
*/
ItemStack addArmorNBTTags(ItemStack item, ArmorTier armorTier, boolean unbreakable, final String name);
/** /**
* Checks which {@link ArmorTier} is on an item. * Checks which {@link ArmorTier} is on an item.
* *

View File

@ -19,12 +19,15 @@ public class NBTEditor implements INBTEditor
private static final NamespacedKey armorTierKey = new NamespacedKey(ArmoredElytra.getInstance(), private static final NamespacedKey armorTierKey = new NamespacedKey(ArmoredElytra.getInstance(),
"ARMOR_TIER_LEVEL"); "ARMOR_TIER_LEVEL");
public NBTEditor() @Override
public ItemStack addArmorNBTTags(ItemStack item, ArmorTier armorTier, boolean unbreakable)
{ {
return addArmorNBTTags(item, armorTier, unbreakable,
ArmoredElytra.getInstance().getArmoredElytraName(armorTier));
} }
@Override @Override
public ItemStack addArmorNBTTags(ItemStack item, ArmorTier armorTier, boolean unbreakable) public ItemStack addArmorNBTTags(ItemStack item, ArmorTier armorTier, boolean unbreakable, final String name)
{ {
if (armorTier == null || armorTier == ArmorTier.NONE) if (armorTier == null || armorTier == ArmorTier.NONE)
return new ItemStack(item); return new ItemStack(item);
@ -45,7 +48,7 @@ public class NBTEditor implements INBTEditor
"generic.knockback_resistance"); "generic.knockback_resistance");
meta.setUnbreakable(unbreakable); meta.setUnbreakable(unbreakable);
meta.setDisplayName(ArmoredElytra.getInstance().getArmoredElytraName(armorTier)); meta.setDisplayName(name);
ret.setItemMeta(meta); ret.setItemMeta(meta);
return ret; return ret;

View File

@ -36,6 +36,7 @@ public class ConfigLoader
private boolean craftingInSmithingTable; private boolean craftingInSmithingTable;
private boolean bypassWearPerm; private boolean bypassWearPerm;
private boolean bypassCraftPerm; private boolean bypassCraftPerm;
private boolean allowRenaming;
private final ArrayList<nl.pim16aap2.armoredElytra.util.ConfigOption<?>> configOptionsList; private final ArrayList<nl.pim16aap2.armoredElytra.util.ConfigOption<?>> configOptionsList;
private final ArmoredElytra plugin; private final ArmoredElytra plugin;
@ -118,6 +119,10 @@ public class ConfigLoader
"This option only works on 1.16+! When enabled, armored elytra creation in anvils is disabled. ", "This option only works on 1.16+! When enabled, armored elytra creation in anvils is disabled. ",
"Instead, you will have to craft them in a smithy. Enchanting/repairing them still works via the anvil." "Instead, you will have to craft them in a smithy. Enchanting/repairing them still works via the anvil."
}; };
String[] allowRenamingComment =
{
"Whether or not to allow renaming of armored elytras in anvils."
};
// Set default list of allowed enchantments. // Set default list of allowed enchantments.
@ -172,6 +177,8 @@ public class ConfigLoader
allowMultipleProtectionEnchantments = addNewConfigOption(config, "allowMultipleProtectionEnchantments", false, allowMultipleProtectionEnchantments = addNewConfigOption(config, "allowMultipleProtectionEnchantments", false,
allowMultipleProtectionEnchantmentsComment); allowMultipleProtectionEnchantmentsComment);
allowRenaming = addNewConfigOption(config, "allowRenaming", true, allowRenamingComment);
checkForUpdates = addNewConfigOption(config, "checkForUpdates", true, updateComment); checkForUpdates = addNewConfigOption(config, "checkForUpdates", true, updateComment);
allowStats = addNewConfigOption(config, "allowStats", true, bStatsComment); allowStats = addNewConfigOption(config, "allowStats", true, bStatsComment);
enableDebug = addNewConfigOption(config, "enableDebug", false, debugComment); enableDebug = addNewConfigOption(config, "enableDebug", false, debugComment);
@ -287,6 +294,11 @@ public class ConfigLoader
return allowMultipleProtectionEnchantments; return allowMultipleProtectionEnchantments;
} }
public boolean allowRenaming()
{
return allowRenaming;
}
public boolean uninstallMode() public boolean uninstallMode()
{ {
return uninstallMode; return uninstallMode;