From 334aba22d2a5b6201aa3b3069d3ee686295b6c5b Mon Sep 17 00:00:00 2001 From: TheTrouper <93684527+thetrouper@users.noreply.github.com> Date: Thu, 8 Feb 2024 15:53:06 -0600 Subject: [PATCH] Working on upgrade item --- .../github/thetrouper/sssbliss/SSSBliss.java | 5 +- .../sssbliss/events/UpgradeListener.java | 20 ++++++++ .../sssbliss/server/crafting/UpgradeItem.java | 47 +++++++++++++++++++ .../sssbliss/server/util/PlayerUtils.java | 43 +++++++++++++++++ 4 files changed, 113 insertions(+), 2 deletions(-) create mode 100644 src/main/java/io/github/thetrouper/sssbliss/events/UpgradeListener.java create mode 100644 src/main/java/io/github/thetrouper/sssbliss/server/crafting/UpgradeItem.java create mode 100644 src/main/java/io/github/thetrouper/sssbliss/server/util/PlayerUtils.java diff --git a/src/main/java/io/github/thetrouper/sssbliss/SSSBliss.java b/src/main/java/io/github/thetrouper/sssbliss/SSSBliss.java index 73357fb..50e017c 100644 --- a/src/main/java/io/github/thetrouper/sssbliss/SSSBliss.java +++ b/src/main/java/io/github/thetrouper/sssbliss/SSSBliss.java @@ -4,6 +4,7 @@ import io.github.itzispyder.pdk.PDK; import io.github.itzispyder.pdk.utils.SchedulerUtils; import io.github.itzispyder.pdk.utils.misc.JsonSerializable; import io.github.thetrouper.sssbliss.cmds.*; +import io.github.thetrouper.sssbliss.server.crafting.UpgradeItem; import io.github.thetrouper.sssbliss.data.config.*; import io.github.thetrouper.sssbliss.events.*; import io.github.thetrouper.sssbliss.server.functions.GemPassiveEffects; @@ -51,7 +52,8 @@ public final class SSSBliss extends JavaPlugin { // Plugin startup logic log.info("Starting Up! (" + getDescription().getVersion() + ")..."); - // Enable Functions + // Recipes + UpgradeItem.addUpgradeRecipe(); // Commands new SSSBlissCommand().register(); @@ -63,7 +65,6 @@ public final class SSSBliss extends JavaPlugin { new DeathListener().register(); //new GemMoveListener().register(); - // Scheduled timers SchedulerUtils.repeat(100, GemPassiveEffects::applyPassiveEffects); diff --git a/src/main/java/io/github/thetrouper/sssbliss/events/UpgradeListener.java b/src/main/java/io/github/thetrouper/sssbliss/events/UpgradeListener.java new file mode 100644 index 0000000..00a9bc0 --- /dev/null +++ b/src/main/java/io/github/thetrouper/sssbliss/events/UpgradeListener.java @@ -0,0 +1,20 @@ +package io.github.thetrouper.sssbliss.events; + +import io.github.itzispyder.pdk.events.CustomListener; +import io.github.thetrouper.sssbliss.server.crafting.UpgradeItem; +import io.github.thetrouper.sssbliss.server.util.GemUtils; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.player.PlayerInteractEvent; + +public class UpgradeListener implements CustomListener { + + @EventHandler + private void onUpgrade(PlayerInteractEvent e) { + if (!e.getAction().isRightClick() || !e.getItem().isSimilar(UpgradeItem.UPGRADE_ITEM)) return; + if (GemUtils.hasGem(e.getPlayer()).upgrade() == null) return; + Player p = e.getPlayer(); + p.getInventory().removeItem(UpgradeItem.UPGRADE_ITEM); + + } +} diff --git a/src/main/java/io/github/thetrouper/sssbliss/server/crafting/UpgradeItem.java b/src/main/java/io/github/thetrouper/sssbliss/server/crafting/UpgradeItem.java new file mode 100644 index 0000000..e20b9e6 --- /dev/null +++ b/src/main/java/io/github/thetrouper/sssbliss/server/crafting/UpgradeItem.java @@ -0,0 +1,47 @@ +package io.github.thetrouper.sssbliss.server.crafting; + +import io.github.itzispyder.pdk.Global; +import io.github.itzispyder.pdk.plugin.builders.ItemBuilder; +import io.github.thetrouper.sssbliss.SSSBliss; +import org.bukkit.Material; +import org.bukkit.NamespacedKey; +import org.bukkit.enchantments.Enchantment; +import org.bukkit.inventory.ItemFlag; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.ShapedRecipe; + +public class UpgradeItem { + + public static final ItemStack UPGRADE_ITEM = ItemBuilder.create() + .material(Material.HEART_OF_THE_SEA) + .name(Global.instance.color("&bGem Upgrade")) + .lore(Global.instance.color("&7Right click to upgrade your gem!")) + .enchant(Enchantment.LURE,1) + .flag(ItemFlag.HIDE_ENCHANTS) + .build(); + public static void addUpgradeRecipe() { + // Create a NamespacedKey for your recipe (replace "your_plugin_name" with your actual plugin name) + NamespacedKey key = new NamespacedKey(SSSBliss.getInstance(), "custom_crafting_recipe"); + + // Create the recipe + ShapedRecipe recipe = new ShapedRecipe(key, UPGRADE_ITEM); + recipe.shape("ABC", "DEF", "GHI"); // Define the crafting grid + + // Define the ingredients + recipe.setIngredient('A', Material.NETHERITE_INGOT); + recipe.setIngredient('B', Material.DIAMOND_BLOCK); + recipe.setIngredient('C', Material.NETHERITE_INGOT); + + recipe.setIngredient('D', Material.DIAMOND_BLOCK); + recipe.setIngredient('E', Material.TRIDENT); + recipe.setIngredient('F', Material.DIAMOND_BLOCK); + + recipe.setIngredient('G', Material.NETHERITE_INGOT); + recipe.setIngredient('H', Material.DIAMOND_BLOCK); + recipe.setIngredient('I', Material.NETHERITE_INGOT); + // Repeat for other letters in the grid + + // Add the recipe to the server + SSSBliss.getInstance().getServer().addRecipe(recipe); + } +} diff --git a/src/main/java/io/github/thetrouper/sssbliss/server/util/PlayerUtils.java b/src/main/java/io/github/thetrouper/sssbliss/server/util/PlayerUtils.java new file mode 100644 index 0000000..9ab017f --- /dev/null +++ b/src/main/java/io/github/thetrouper/sssbliss/server/util/PlayerUtils.java @@ -0,0 +1,43 @@ +package io.github.thetrouper.sssbliss.server.util; + +import org.bukkit.entity.Player; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; + +public class PlayerUtils { + /** + * Removes a specified amount of a specific ItemStack from a player's inventory. + * + * @param player The player from whose inventory items will be removed. + * @param item The ItemStack to be removed. + * @param amount The amount of items to remove. + * @return {@code true} if the specified amount of items was successfully removed, + * {@code false} if the player does not have enough items. + */ + public static boolean removeItems(Player player, ItemStack item, int amount) { + Inventory inventory = player.getInventory(); + + int totalAmountToRemove = amount; + + for (ItemStack stack : inventory.getContents()) { + if (stack != null && stack.isSimilar(item)) { + int stackAmount = stack.getAmount(); + + if (stackAmount <= totalAmountToRemove) { + totalAmountToRemove -= stackAmount; + inventory.removeItem(stack); + } else { + stack.setAmount(stackAmount - totalAmountToRemove); + totalAmountToRemove = 0; + break; + } + + if (totalAmountToRemove == 0) { + return true; // Successfully removed the items + } + } + } + + return false; // Player didn't have enough items + } +}