Add smithing table AE creation

This commit is contained in:
Pim van der Loos 2020-11-24 12:04:50 +01:00
parent b8fdec0ed5
commit 0e4be02056
No known key found for this signature in database
GPG Key ID: C16F020ADAE6D5A8
6 changed files with 138 additions and 52 deletions

View File

@ -5,6 +5,7 @@ import nl.pim16aap2.armoredElytra.handlers.CommandHandler;
import nl.pim16aap2.armoredElytra.handlers.EventHandlers;
import nl.pim16aap2.armoredElytra.handlers.FlyDurabilityHandler;
import nl.pim16aap2.armoredElytra.handlers.LoginHandler;
import nl.pim16aap2.armoredElytra.handlers.SmithingTableHandler;
import nl.pim16aap2.armoredElytra.handlers.Uninstaller;
import nl.pim16aap2.armoredElytra.nbtEditor.INBTEditor;
import nl.pim16aap2.armoredElytra.nbtEditor.NBTEditor;
@ -22,7 +23,6 @@ import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
import java.util.EnumMap;
import java.util.Map;
@ -84,12 +84,12 @@ public class ArmoredElytra extends JavaPlugin implements Listener
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!");
if (config.craftingInSmithingTable())
throw new NotImplementedException();
else
{
Bukkit.getPluginManager().registerEvents(new AnvilHandler(this, !config.uninstallMode()), this);
}
final Listener creationListener = config.craftingInSmithingTable() ?
new SmithingTableHandler(this, !config.uninstallMode()) :
new AnvilHandler(this, !config.uninstallMode());
Bukkit.getPluginManager().registerEvents(creationListener, this);
Bukkit.getPluginManager().registerEvents(new EventHandlers(this), this);
getCommand("ArmoredElytra").setExecutor(new CommandHandler(this));
@ -135,6 +135,11 @@ public class ArmoredElytra extends JavaPlugin implements Listener
return parts.length > 0 && parts[0].equals("758abbe");
}
public MinecraftVersion getMinecraftVersion()
{
return minecraftVersion;
}
public Messages getMyMessages()
{
return messages;

View File

@ -18,51 +18,11 @@ import org.bukkit.inventory.ItemStack;
import java.util.logging.Level;
public class AnvilHandler implements Listener
public class AnvilHandler extends ArmoredElytraHandler implements Listener
{
private final ArmoredElytra plugin;
private final boolean creationEnabled;
public AnvilHandler(final ArmoredElytra plugin, final boolean creationEnabled)
{
this.plugin = plugin;
this.creationEnabled = creationEnabled;
}
private void cleanAnvilInventory(AnvilInventory anvilInventory)
{
if (anvilInventory.getItem(0) != null)
anvilInventory.getItem(0).setAmount(0);
if (anvilInventory.getItem(1) != null)
anvilInventory.getItem(1).setAmount(anvilInventory.getItem(1).getAmount() - 1);
if (anvilInventory.getItem(2) != null)
anvilInventory.getItem(2).setAmount(0);
}
// Repair an Armored Elytra
private short repairItem(short curDur, ItemStack repairItem)
{
// Get the multiplier for the repair items.
double mult = 0.01;
if (repairItem.getType().equals(Material.LEATHER))
mult *= (100.0f / plugin.getConfigLoader().LEATHER_TO_FULL());
else if (repairItem.getType().equals(Material.GOLD_INGOT))
mult *= (100.0f / plugin.getConfigLoader().GOLD_TO_FULL());
else if (repairItem.getType().equals(Material.IRON_INGOT))
mult *= (100.0f / plugin.getConfigLoader().IRON_TO_FULL());
else if (repairItem.getType().equals(Material.DIAMOND))
mult *= (100.0f / plugin.getConfigLoader().DIAMONDS_TO_FULL());
else if (repairItem.getType().equals(XMaterial.NETHERITE_INGOT.parseMaterial()))
mult *= (100.0f / plugin.getConfigLoader().NETHERITE_TO_FULL());
int maxDurability = Material.ELYTRA.getMaxDurability();
int newDurability = (int) (curDur - (maxDurability * mult));
return (short) (newDurability <= 0 ? 0 : newDurability);
super(plugin, creationEnabled);
}
// Valid inputs:

View File

@ -0,0 +1,60 @@
package nl.pim16aap2.armoredElytra.handlers;
import nl.pim16aap2.armoredElytra.ArmoredElytra;
import nl.pim16aap2.armoredElytra.util.XMaterial;
import org.bukkit.Material;
import org.bukkit.inventory.AnvilInventory;
import org.bukkit.inventory.ItemStack;
/**
* Base class for the anvil / smithing table handlers.
*
* @author Pim
*/
abstract class ArmoredElytraHandler
{
protected final ArmoredElytra plugin;
protected final boolean creationEnabled;
public ArmoredElytraHandler(final ArmoredElytra plugin, final boolean creationEnabled)
{
this.plugin = plugin;
this.creationEnabled = creationEnabled;
}
protected void cleanAnvilInventory(AnvilInventory anvilInventory)
{
if (anvilInventory.getItem(0) != null)
anvilInventory.getItem(0).setAmount(0);
if (anvilInventory.getItem(1) != null)
anvilInventory.getItem(1).setAmount(anvilInventory.getItem(1).getAmount() - 1);
if (anvilInventory.getItem(2) != null)
anvilInventory.getItem(2).setAmount(0);
}
// Repair an Armored Elytra
protected short repairItem(short curDur, ItemStack repairItem)
{
// Get the multiplier for the repair items.
double mult = 0.01;
if (repairItem.getType().equals(Material.LEATHER))
mult *= (100.0f / plugin.getConfigLoader().LEATHER_TO_FULL());
else if (repairItem.getType().equals(Material.GOLD_INGOT))
mult *= (100.0f / plugin.getConfigLoader().GOLD_TO_FULL());
else if (repairItem.getType().equals(Material.IRON_INGOT))
mult *= (100.0f / plugin.getConfigLoader().IRON_TO_FULL());
else if (repairItem.getType().equals(Material.DIAMOND))
mult *= (100.0f / plugin.getConfigLoader().DIAMONDS_TO_FULL());
else if (repairItem.getType().equals(XMaterial.NETHERITE_INGOT.parseMaterial()))
mult *= (100.0f / plugin.getConfigLoader().NETHERITE_TO_FULL());
int maxDurability = Material.ELYTRA.getMaxDurability();
int newDurability = (int) (curDur - (maxDurability * mult));
return (short) (newDurability <= 0 ? 0 : newDurability);
}
}

View File

@ -0,0 +1,49 @@
package nl.pim16aap2.armoredElytra.handlers;
import nl.pim16aap2.armoredElytra.ArmoredElytra;
import nl.pim16aap2.armoredElytra.enchantment.EnchantmentManager;
import nl.pim16aap2.armoredElytra.util.ArmorTier;
import nl.pim16aap2.armoredElytra.util.Util;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.PrepareSmithingEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.SmithingInventory;
public class SmithingTableHandler extends ArmoredElytraHandler implements Listener
{
public SmithingTableHandler(final ArmoredElytra plugin, final boolean creationEnabled)
{
super(plugin, creationEnabled);
}
@EventHandler(ignoreCancelled = true)
public void onSmithingTableUsage(final PrepareSmithingEvent event)
{
final SmithingInventory inventory = event.getInventory();
final ItemStack[] contents = inventory.getContents();
final ItemStack itemStackA = contents[0];
final ItemStack itemStackB = contents[1];
if (itemStackA == null || itemStackB == null ||
itemStackA.getType() != Material.ELYTRA || !Util.isChestPlate(itemStackB))
return;
final ArmorTier newTier = Util.armorToTier(itemStackB.getType());
final EnchantmentManager enchantments = new EnchantmentManager(itemStackA);
final Player player = (Player) event.getView().getPlayer();
final ItemStack result;
if (plugin.playerHasCraftPerm(player, newTier))
{
result = ArmoredElytra.getInstance().getNbtEditor()
.addArmorNBTTags(new ItemStack(Material.ELYTRA, 1), newTier,
plugin.getConfigLoader().unbreakable());
enchantments.apply(result);
event.setResult(result);
}
}
}

View File

@ -133,8 +133,15 @@ public class ConfigLoader
DIAMONDS_TO_FULL = addNewConfigOption(config, "diamondsRepair", 3, null);
NETHERITE_TO_FULL = addNewConfigOption(config, "netheriteIngotsRepair", 3, null);
// craftingInSmithingTable = addNewConfigOption(config, "craftingInSmithingTable", true, craftingInSmithingTableComment);
craftingInSmithingTable = false;
final boolean smithingTableAllowed = plugin.getMinecraftVersion().isNewerThan(MinecraftVersion.v1_15);
craftingInSmithingTable = addNewConfigOption(config, "craftingInSmithingTable", smithingTableAllowed,
craftingInSmithingTableComment);
if (craftingInSmithingTable && !smithingTableAllowed)
{
Bukkit.getLogger().log(Level.WARNING, "You tried to enable crafting in smithing tables, " +
"but this is only supported on 1.16+! Reverting to disabled.");
craftingInSmithingTable = false;
}
defaultAllowedEnchantments = addNewConfigOption(config, "allowedEnchantments", defaultAllowedEnchantments,
enchantmentsComment);
@ -202,7 +209,7 @@ public class ConfigLoader
e.printStackTrace();
}
}
public boolean allowStats()
{
return allowStats;

View File

@ -62,6 +62,11 @@ public class Util
return ret;
}
public static boolean isChestPlate(ItemStack itemStack)
{
return isChestPlate(itemStack.getType());
}
// Check if mat is a chest plate.
public static boolean isChestPlate(Material mat)
{