From 81c15b660071db170879803dbca0694613c49ab6 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Sat, 3 Aug 2024 16:32:44 +0200 Subject: [PATCH] Adds information about the appropriate crafting station in ActionStartEvent --- .../blacksmith/event/ActionStartEvent.java | 9 +++++++++ .../event/BlacksmithReforgeStartEvent.java | 18 ++++++++++++++---- .../event/ScrapperSalvageStartEvent.java | 18 ++++++++++++++---- .../blacksmith/manager/EconomyManager.java | 2 +- .../blacksmith/property/SalvageMethod.java | 5 +++++ .../blacksmith/trait/ReforgeSession.java | 5 +++++ .../blacksmith/trait/SalvageSession.java | 10 ++++++++++ .../blacksmith/trait/ScrapperTrait.java | 5 +++-- .../knarcraft/blacksmith/trait/Session.java | 13 +++++++++++-- 9 files changed, 72 insertions(+), 13 deletions(-) diff --git a/src/main/java/net/knarcraft/blacksmith/event/ActionStartEvent.java b/src/main/java/net/knarcraft/blacksmith/event/ActionStartEvent.java index 1cb2b2f..024cac7 100644 --- a/src/main/java/net/knarcraft/blacksmith/event/ActionStartEvent.java +++ b/src/main/java/net/knarcraft/blacksmith/event/ActionStartEvent.java @@ -1,5 +1,7 @@ package net.knarcraft.blacksmith.event; +import org.bukkit.Material; + /** * An event triggered when a blacksmith or scrapper starts reforging or salvaging an item */ @@ -13,4 +15,11 @@ public interface ActionStartEvent extends BlacksmithPluginEvent { */ long getActionDurationTicks(); + /** + * Gets the appropriate crafting station for this action + * + * @return

The appropriate crafting station

+ */ + Material craftingStation(); + } diff --git a/src/main/java/net/knarcraft/blacksmith/event/BlacksmithReforgeStartEvent.java b/src/main/java/net/knarcraft/blacksmith/event/BlacksmithReforgeStartEvent.java index 82f4b0b..7476f22 100644 --- a/src/main/java/net/knarcraft/blacksmith/event/BlacksmithReforgeStartEvent.java +++ b/src/main/java/net/knarcraft/blacksmith/event/BlacksmithReforgeStartEvent.java @@ -1,6 +1,7 @@ package net.knarcraft.blacksmith.event; import net.citizensnpcs.api.npc.NPC; +import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.event.HandlerList; import org.jetbrains.annotations.NotNull; @@ -12,17 +13,21 @@ public class BlacksmithReforgeStartEvent extends AbstractBlacksmithPluginEvent i private static final HandlerList handlers = new HandlerList(); private final long durationTicks; + private final Material craftingStation; /** * Instantiates a new blacksmith reforge start event * - * @param npc

The NPC involved in the event

- * @param player

The player involved in the event

- * @param durationTicks

The duration of the reforge

+ * @param npc

The NPC involved in the event

+ * @param player

The player involved in the event

+ * @param durationTicks

The duration of the reforge

+ * @param craftingStation

The appropriate crafting station for this reforging

*/ - public BlacksmithReforgeStartEvent(@NotNull NPC npc, @NotNull Player player, long durationTicks) { + public BlacksmithReforgeStartEvent(@NotNull NPC npc, @NotNull Player player, long durationTicks, + @NotNull Material craftingStation) { super(npc, player); this.durationTicks = durationTicks; + this.craftingStation = craftingStation; } @Override @@ -30,6 +35,11 @@ public class BlacksmithReforgeStartEvent extends AbstractBlacksmithPluginEvent i return durationTicks; } + @Override + public Material craftingStation() { + return craftingStation; + } + /** * Gets a handler-list containing all event handlers * diff --git a/src/main/java/net/knarcraft/blacksmith/event/ScrapperSalvageStartEvent.java b/src/main/java/net/knarcraft/blacksmith/event/ScrapperSalvageStartEvent.java index df2f4b0..7c1213f 100644 --- a/src/main/java/net/knarcraft/blacksmith/event/ScrapperSalvageStartEvent.java +++ b/src/main/java/net/knarcraft/blacksmith/event/ScrapperSalvageStartEvent.java @@ -1,6 +1,7 @@ package net.knarcraft.blacksmith.event; import net.citizensnpcs.api.npc.NPC; +import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.event.HandlerList; import org.jetbrains.annotations.NotNull; @@ -13,17 +14,21 @@ public class ScrapperSalvageStartEvent extends AbstractBlacksmithPluginEvent imp private static final HandlerList handlers = new HandlerList(); private final long durationTicks; + private final Material craftingStation; /** * Instantiates a new scrapper salvage start event * - * @param npc

The NPC involved in the event

- * @param player

The player involved in the event

- * @param durationTicks

The duration of the salvage

+ * @param npc

The NPC involved in the event

+ * @param player

The player involved in the event

+ * @param durationTicks

The duration of the salvage

+ * @param craftingStation

The appropriate crafting station for this salvaging

*/ - public ScrapperSalvageStartEvent(@NotNull NPC npc, @NotNull Player player, long durationTicks) { + public ScrapperSalvageStartEvent(@NotNull NPC npc, @NotNull Player player, long durationTicks, + @NotNull Material craftingStation) { super(npc, player); this.durationTicks = durationTicks; + this.craftingStation = craftingStation; } @Override @@ -31,6 +36,11 @@ public class ScrapperSalvageStartEvent extends AbstractBlacksmithPluginEvent imp return this.durationTicks; } + @Override + public Material craftingStation() { + return this.craftingStation; + } + /** * Gets a handler-list containing all event handlers * diff --git a/src/main/java/net/knarcraft/blacksmith/manager/EconomyManager.java b/src/main/java/net/knarcraft/blacksmith/manager/EconomyManager.java index 112959c..717e58c 100644 --- a/src/main/java/net/knarcraft/blacksmith/manager/EconomyManager.java +++ b/src/main/java/net/knarcraft/blacksmith/manager/EconomyManager.java @@ -92,7 +92,7 @@ public class EconomyManager { private static double getSalvageCost(@NotNull SalvageMethod salvageMethod) { GlobalScrapperSettings settings = BlacksmithPlugin.getInstance().getGlobalScrapperSettings(); return switch (salvageMethod) { - case SALVAGE -> settings.getSalvageCost(); + case SALVAGE, EXTENDED_SALVAGE -> settings.getSalvageCost(); case NETHERITE -> settings.getNetheriteSalvageCost(); case ARMOR_TRIM -> settings.getArmorTrimSalvageCost(); }; diff --git a/src/main/java/net/knarcraft/blacksmith/property/SalvageMethod.java b/src/main/java/net/knarcraft/blacksmith/property/SalvageMethod.java index da88161..581f6b9 100644 --- a/src/main/java/net/knarcraft/blacksmith/property/SalvageMethod.java +++ b/src/main/java/net/knarcraft/blacksmith/property/SalvageMethod.java @@ -10,6 +10,11 @@ public enum SalvageMethod { */ SALVAGE, + /** + * Salvaging unrepairable items normally by returning the item's crafting recipe, but with unrestricted + */ + EXTENDED_SALVAGE, + /** * Removing the armor trim from an item */ diff --git a/src/main/java/net/knarcraft/blacksmith/trait/ReforgeSession.java b/src/main/java/net/knarcraft/blacksmith/trait/ReforgeSession.java index da23862..8fea898 100644 --- a/src/main/java/net/knarcraft/blacksmith/trait/ReforgeSession.java +++ b/src/main/java/net/knarcraft/blacksmith/trait/ReforgeSession.java @@ -92,6 +92,11 @@ public class ReforgeSession extends Session implements Runnable { } } + @Override + protected @NotNull Material getCraftingStation() { + return Material.ANVIL; + } + /** * Runs the actual reforge which fixes the item and gives it back to the player */ diff --git a/src/main/java/net/knarcraft/blacksmith/trait/SalvageSession.java b/src/main/java/net/knarcraft/blacksmith/trait/SalvageSession.java index 849d1d3..4785d24 100644 --- a/src/main/java/net/knarcraft/blacksmith/trait/SalvageSession.java +++ b/src/main/java/net/knarcraft/blacksmith/trait/SalvageSession.java @@ -9,6 +9,7 @@ import net.knarcraft.blacksmith.manager.EconomyManager; import net.knarcraft.blacksmith.property.SalvageMethod; import net.knarcraft.blacksmith.util.ItemHelper; import net.knarcraft.blacksmith.util.SalvageHelper; +import org.bukkit.Material; import org.bukkit.Sound; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; @@ -87,6 +88,15 @@ public class SalvageSession extends Session implements Runnable { } } + @Override + protected @NotNull Material getCraftingStation() { + return switch (this.salvageMethod) { + case EXTENDED_SALVAGE -> Material.CRAFTING_TABLE; + case SALVAGE -> Material.ANVIL; + case NETHERITE, ARMOR_TRIM -> Material.SMITHING_TABLE; + }; + } + /** * Runs the actual salvage which fixes the item and gives it back to the player */ diff --git a/src/main/java/net/knarcraft/blacksmith/trait/ScrapperTrait.java b/src/main/java/net/knarcraft/blacksmith/trait/ScrapperTrait.java index d4e649c..da76a41 100644 --- a/src/main/java/net/knarcraft/blacksmith/trait/ScrapperTrait.java +++ b/src/main/java/net/knarcraft/blacksmith/trait/ScrapperTrait.java @@ -177,12 +177,13 @@ public class ScrapperTrait extends CustomTrait { // Check if any salvage will be produced RecipeResult recipeResult = getSalvage(itemInHand, extended); + SalvageMethod method = ItemHelper.isRepairable(itemInHand) ? SalvageMethod.SALVAGE : SalvageMethod.EXTENDED_SALVAGE; boolean noUsefulSalvage = recipeResult == null || recipeResult.salvage().isEmpty(); if (noUsefulSalvage) { sendNPCMessage(this.npc, player, getSettings().getTooDamagedMessage()); - return new SalvageResult(SalvageMethod.SALVAGE, new ArrayList<>(), SalvageState.NO_SALVAGE, 0); + return new SalvageResult(method, new ArrayList<>(), SalvageState.NO_SALVAGE, 0); } else { - return new SalvageResult(SalvageMethod.SALVAGE, recipeResult.salvage(), SalvageState.FOUND_SALVAGE, + return new SalvageResult(method, recipeResult.salvage(), SalvageState.FOUND_SALVAGE, recipeResult.recipe().getResult().getAmount()); } } diff --git a/src/main/java/net/knarcraft/blacksmith/trait/Session.java b/src/main/java/net/knarcraft/blacksmith/trait/Session.java index ff35b92..c909ae7 100644 --- a/src/main/java/net/knarcraft/blacksmith/trait/Session.java +++ b/src/main/java/net/knarcraft/blacksmith/trait/Session.java @@ -6,6 +6,7 @@ import net.knarcraft.blacksmith.event.BlacksmithReforgeStartEvent; import net.knarcraft.blacksmith.event.NPCSoundEvent; import net.knarcraft.blacksmith.event.ScrapperSalvageStartEvent; import net.knarcraft.blacksmith.util.ItemHelper; +import org.bukkit.Material; import org.bukkit.Sound; import org.bukkit.SoundCategory; import org.bukkit.World; @@ -100,10 +101,11 @@ public abstract class Session implements Runnable { this.finishTime = System.currentTimeMillis() + (actionDelay * 1000L); long actionDelayTicks = actionDelay * 20L; + BlacksmithPlugin instance = BlacksmithPlugin.getInstance(); if (this instanceof ReforgeSession) { - BlacksmithPlugin.getInstance().callEvent(new BlacksmithReforgeStartEvent(npc, player, actionDelayTicks)); + instance.callEvent(new BlacksmithReforgeStartEvent(npc, player, actionDelayTicks, getCraftingStation())); } else if (this instanceof SalvageSession) { - BlacksmithPlugin.getInstance().callEvent(new ScrapperSalvageStartEvent(npc, player, actionDelayTicks)); + instance.callEvent(new ScrapperSalvageStartEvent(npc, player, actionDelayTicks, getCraftingStation())); } taskId = scheduler.scheduleSyncDelayedTask(BlacksmithPlugin.getInstance(), this, actionDelayTicks); @@ -157,6 +159,13 @@ public abstract class Session implements Runnable { */ protected abstract int getActionDelay(); + /** + * Gets the appropriate crafting station for this session + * + * @return

The appropriate crafting station

+ */ + protected abstract @NotNull Material getCraftingStation(); + /** * Plays an NPC sound *