From afb608b609e44d161dafbaa5414380f922567ee5 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Mon, 29 Jul 2024 01:40:08 +0200 Subject: [PATCH] Makes ActionStartEvent give duration in ticks instead of end time --- .../blacksmith/event/ActionStartEvent.java | 8 ++---- .../event/BlacksmithReforgeStartEvent.java | 16 +++++------ .../event/ScrapperSalvageStartEvent.java | 16 +++++------ .../knarcraft/blacksmith/trait/Session.java | 22 +++------------ .../blacksmith/util/SalvageHelper.java | 28 +++++++++++++------ 5 files changed, 43 insertions(+), 47 deletions(-) diff --git a/src/main/java/net/knarcraft/blacksmith/event/ActionStartEvent.java b/src/main/java/net/knarcraft/blacksmith/event/ActionStartEvent.java index b0c6b42..1cb2b2f 100644 --- a/src/main/java/net/knarcraft/blacksmith/event/ActionStartEvent.java +++ b/src/main/java/net/knarcraft/blacksmith/event/ActionStartEvent.java @@ -7,12 +7,10 @@ package net.knarcraft.blacksmith.event; public interface ActionStartEvent extends BlacksmithPluginEvent { /** - * Gets the end time for this action + * Gets the amount of ticks this action will take * - *

The end time is a millisecond time-stamp, basically the same format used by currentTimeMillis();.

- * - * @return

The end time

+ * @return

The duration in ticks

*/ - long getActionEndTime(); + long getActionDurationTicks(); } diff --git a/src/main/java/net/knarcraft/blacksmith/event/BlacksmithReforgeStartEvent.java b/src/main/java/net/knarcraft/blacksmith/event/BlacksmithReforgeStartEvent.java index 410278c..82f4b0b 100644 --- a/src/main/java/net/knarcraft/blacksmith/event/BlacksmithReforgeStartEvent.java +++ b/src/main/java/net/knarcraft/blacksmith/event/BlacksmithReforgeStartEvent.java @@ -11,23 +11,23 @@ import org.jetbrains.annotations.NotNull; public class BlacksmithReforgeStartEvent extends AbstractBlacksmithPluginEvent implements ActionStartEvent { private static final HandlerList handlers = new HandlerList(); - private final Long reforgeEndTime; + private final long durationTicks; /** * Instantiates a new blacksmith reforge start event * - * @param npc

The NPC involved in the event

- * @param player

The player involved in the event

- * @param reforgeEndTime

The time at which this reforging ends

+ * @param npc

The NPC involved in the event

+ * @param player

The player involved in the event

+ * @param durationTicks

The duration of the reforge

*/ - public BlacksmithReforgeStartEvent(@NotNull NPC npc, @NotNull Player player, long reforgeEndTime) { + public BlacksmithReforgeStartEvent(@NotNull NPC npc, @NotNull Player player, long durationTicks) { super(npc, player); - this.reforgeEndTime = reforgeEndTime; + this.durationTicks = durationTicks; } @Override - public long getActionEndTime() { - return reforgeEndTime; + public long getActionDurationTicks() { + return durationTicks; } /** diff --git a/src/main/java/net/knarcraft/blacksmith/event/ScrapperSalvageStartEvent.java b/src/main/java/net/knarcraft/blacksmith/event/ScrapperSalvageStartEvent.java index 07df374..df2f4b0 100644 --- a/src/main/java/net/knarcraft/blacksmith/event/ScrapperSalvageStartEvent.java +++ b/src/main/java/net/knarcraft/blacksmith/event/ScrapperSalvageStartEvent.java @@ -12,23 +12,23 @@ import org.jetbrains.annotations.NotNull; public class ScrapperSalvageStartEvent extends AbstractBlacksmithPluginEvent implements ActionStartEvent { private static final HandlerList handlers = new HandlerList(); - private final long salvageEndTime; + private final long durationTicks; /** * Instantiates a new scrapper salvage start event * - * @param npc

The NPC involved in the event

- * @param player

The player involved in the event

- * @param salvageEndTime

The time at which this salvaging ends

+ * @param npc

The NPC involved in the event

+ * @param player

The player involved in the event

+ * @param durationTicks

The duration of the salvage

*/ - public ScrapperSalvageStartEvent(@NotNull NPC npc, @NotNull Player player, long salvageEndTime) { + public ScrapperSalvageStartEvent(@NotNull NPC npc, @NotNull Player player, long durationTicks) { super(npc, player); - this.salvageEndTime = salvageEndTime; + this.durationTicks = durationTicks; } @Override - public long getActionEndTime() { - return this.salvageEndTime; + public long getActionDurationTicks() { + return this.durationTicks; } /** diff --git a/src/main/java/net/knarcraft/blacksmith/trait/Session.java b/src/main/java/net/knarcraft/blacksmith/trait/Session.java index 2b053fa..a2f0142 100644 --- a/src/main/java/net/knarcraft/blacksmith/trait/Session.java +++ b/src/main/java/net/knarcraft/blacksmith/trait/Session.java @@ -99,22 +99,15 @@ public abstract class Session implements Runnable { BukkitScheduler scheduler = BlacksmithPlugin.getInstance().getServer().getScheduler(); int actionDelay = getActionDelay(); this.finishTime = System.currentTimeMillis() + (actionDelay * 1000L); + long actionDelayTicks = actionDelay * 20L; if (this instanceof ReforgeSession) { - BlacksmithPlugin.getInstance().callEvent(new BlacksmithReforgeStartEvent(npc, player, this.finishTime)); + BlacksmithPlugin.getInstance().callEvent(new BlacksmithReforgeStartEvent(npc, player, actionDelayTicks)); } else if (this instanceof SalvageSession) { - BlacksmithPlugin.getInstance().callEvent(new ScrapperSalvageStartEvent(npc, player, this.finishTime)); + BlacksmithPlugin.getInstance().callEvent(new ScrapperSalvageStartEvent(npc, player, actionDelayTicks)); } - taskId = scheduler.scheduleSyncDelayedTask(BlacksmithPlugin.getInstance(), this, actionDelay * 20L); - int playWorkSound = scheduler.scheduleSyncRepeatingTask(BlacksmithPlugin.getInstance(), - () -> { - if (random.nextInt(100) < 20) { - this.playWorkSound(); - } - }, 20, 5); - scheduler.scheduleSyncDelayedTask(BlacksmithPlugin.getInstance(), () -> scheduler.cancelTask(playWorkSound), - (actionDelay * 20L) - 20); + taskId = scheduler.scheduleSyncDelayedTask(BlacksmithPlugin.getInstance(), this, actionDelayTicks); } /** @@ -174,13 +167,6 @@ public abstract class Session implements Runnable { playSound(this.npc.getEntity(), sound); } - /** - * Plays the working NPC sound - */ - protected void playWorkSound() { - playSound(this.npc.getEntity(), Sound.ITEM_ARMOR_EQUIP_NETHERITE); - } - /** * Plays a npc sound using a cancellable event * diff --git a/src/main/java/net/knarcraft/blacksmith/util/SalvageHelper.java b/src/main/java/net/knarcraft/blacksmith/util/SalvageHelper.java index 31a48c1..f5197a2 100644 --- a/src/main/java/net/knarcraft/blacksmith/util/SalvageHelper.java +++ b/src/main/java/net/knarcraft/blacksmith/util/SalvageHelper.java @@ -184,15 +184,12 @@ public final class SalvageHelper { } /** - * Gets the salvage resulting from the given recipe and the given item + * Gets the raw salvage of a recipe, assuming full salvage would be possible * - * @param recipe

The recipe to get salvage for

- * @param salvagedItem

The item to be salvaged

- * @param trashSalvage

Any material treated as trash salvage

- * @return

A list of items, or null if not a valid type of recipe

+ * @param recipe

The recipe to get salvage for

+ * @return

The salvage resulting from the recipe

*/ - private static @Nullable List getRecipeSalvage(@NotNull Recipe recipe, @NotNull ItemStack salvagedItem, - @NotNull Collection trashSalvage) { + private static List getRawRecipeSalvage(@NotNull Recipe recipe) { List ingredients; if (recipe instanceof ShapedRecipe shapedRecipe) { ingredients = getIngredients(shapedRecipe); @@ -203,8 +200,23 @@ public final class SalvageHelper { return null; } //Make things easier by eliminating identical stacks - ingredients = combineStacks(ingredients); + return combineStacks(ingredients); + } + /** + * Gets the salvage resulting from the given recipe and the given item + * + * @param recipe

The recipe to get salvage for

+ * @param salvagedItem

The item to be salvaged

+ * @param trashSalvage

Any material treated as trash salvage

+ * @return

A list of items, or null if not a valid type of recipe

+ */ + private static @Nullable List getRecipeSalvage(@NotNull Recipe recipe, @NotNull ItemStack salvagedItem, + @NotNull Collection trashSalvage) { + List ingredients = getRawRecipeSalvage(recipe); + if (ingredients == null) { + return null; + } return combineStacks(getSalvage(copyItems(ingredients), salvagedItem, trashSalvage)); }