some refactors + better id setting

This commit is contained in:
JISAUAY 2025-02-14 15:07:23 -06:00
parent 1f5a062416
commit d897688ab4
9 changed files with 72 additions and 29 deletions

View File

@ -1,4 +1,4 @@
package net.hexa.Plugin; package net.hexa.MythicItems;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
@ -6,7 +6,7 @@ import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import net.hexa.Plugin.Mythics.RockCracker; import net.hexa.MythicItems.Mythics.RockCracker;
public class CommandMythicEssence implements CommandExecutor { public class CommandMythicEssence implements CommandExecutor {

View File

@ -1,12 +1,13 @@
package net.hexa.Plugin; package net.hexa.MythicItems;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
public class Plugin extends JavaPlugin { public class MythicItems extends JavaPlugin {
@Override @Override
public void onEnable() { public void onEnable() {
getLogger().info("onEnable is called!"); getLogger().info("onEnable is called!");
this.getCommand("essence").setExecutor(new CommandMythicEssence()); this.getCommand("essence").setExecutor(new CommandMythicEssence());
getServer().getPluginManager().registerEvents(new MythicListener(), this);
} }
@Override @Override

View File

@ -1,13 +1,12 @@
package net.hexa.Plugin; package net.hexa.MythicItems;
import net.hexa.MythicItems.Mythics.MythicItem;
import net.hexa.Plugin.Mythics.Constants;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
public class MythicListener implements Listener { public class MythicListener implements Listener {
@ -16,12 +15,11 @@ public class MythicListener implements Listener {
Player player = event.getPlayer(); Player player = event.getPlayer();
ItemStack item = player.getInventory().getItemInMainHand(); ItemStack item = player.getInventory().getItemInMainHand();
PersistentDataContainer itemData = item.getItemMeta().getPersistentDataContainer();
boolean isMythic = itemData.has(Constants.mythic_key); boolean isMythic = MythicItem.isMythic(item);
if (isMythic) { if (isMythic) {
int mythic_id = itemData.get(Constants.mythic_key, PersistentDataType.INTEGER); int mythic_id = MythicItem.getMythicId(item);
} }
} }

View File

@ -1,8 +1,8 @@
package net.hexa.Plugin.Mythics; package net.hexa.MythicItems.Mythics;
import net.hexa.Plugin.Plugin; import net.hexa.MythicItems.MythicItems;
import org.bukkit.NamespacedKey; import org.bukkit.NamespacedKey;
public class Constants { public class Constants {
public static NamespacedKey mythic_key = new NamespacedKey(new Plugin(), "mythic-id"); public static NamespacedKey mythic_key = new NamespacedKey(new MythicItems(), "mythic-id");
} }

View File

@ -0,0 +1,28 @@
package net.hexa.MythicItems.Mythics;
import org.bukkit.inventory.ItemStack;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
public abstract class MythicItem {
public ItemStack item;
public MythicItem() {}
public static int getMythicId(ItemStack item) {
PersistentDataContainer itemData = item.getItemMeta().getPersistentDataContainer();
return itemData.get(Constants.mythic_key, PersistentDataType.INTEGER);
}
public static boolean isMythic(ItemStack item) {
PersistentDataContainer itemData = item.getItemMeta().getPersistentDataContainer();
return itemData.has(Constants.mythic_key);
}
protected void setMythicId(int id) {
PersistentDataContainer itemData = item.getItemMeta().getPersistentDataContainer();
itemData.set(Constants.mythic_key, PersistentDataType.INTEGER, id);
}
}

View File

@ -1,8 +1,6 @@
package net.hexa.Plugin.Mythics; package net.hexa.MythicItems.Mythics;
import net.hexa.Plugin.Plugin;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.persistence.PersistentDataType; import org.bukkit.persistence.PersistentDataType;
@ -21,6 +19,6 @@ public class RockCracker extends MythicItem {
item.getItemMeta().setLore(Arrays.asList("and the land thrashes in furious pain...")); item.getItemMeta().setLore(Arrays.asList("and the land thrashes in furious pain..."));
item.getItemMeta().setEnchantable(0); item.getItemMeta().setEnchantable(0);
item.getItemMeta().getPersistentDataContainer().set(Constants.mythic_key, PersistentDataType.INTEGER, 1); setMythicId(1);
} }
} }

View File

@ -0,0 +1,27 @@
package net.hexa.MythicItems.Mythics;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.Arrays;
public class Terra extends MythicItem {
public Terra() {
item = new ItemStack(Material.NETHERITE_SHOVEL);
item.addUnsafeEnchantment(Enchantment.EFFICIENCY, 8);
item.addUnsafeEnchantment(Enchantment.UNBREAKING, 10);
ItemMeta meta = item.getItemMeta();
meta.setUnbreakable(true);
meta.setDisplayName("Terra");
meta.setLore(Arrays.asList("All will eventually return to Terra."));
meta.setEnchantable(0);
item.setItemMeta(meta);
setMythicId(2);
}
}

View File

@ -1,9 +0,0 @@
package net.hexa.Plugin.Mythics;
import org.bukkit.inventory.ItemStack;
public abstract class MythicItem {
public ItemStack item;
public MythicItem() {}
}

View File

@ -1,6 +1,6 @@
name: EconPlugin name: MythicItems
version: 1.0 version: 1.0
main: net.hexa.Plugin.Plugin main: net.hexa.MythicItems.MythicItems
api-version: '1.21.4' api-version: '1.21.4'
commands: commands: