Makes ActionStartEvent give duration in ticks instead of end time
All checks were successful
EpicKnarvik97/Blacksmith/pipeline/head This commit looks good

This commit is contained in:
Kristian Knarvik 2024-07-29 01:40:08 +02:00
parent 523e4cb47a
commit afb608b609
5 changed files with 43 additions and 47 deletions

View File

@ -7,12 +7,10 @@ package net.knarcraft.blacksmith.event;
public interface ActionStartEvent extends BlacksmithPluginEvent { public interface ActionStartEvent extends BlacksmithPluginEvent {
/** /**
* Gets the end time for this action * Gets the amount of ticks this action will take
* *
* <p>The end time is a millisecond time-stamp, basically the same format used by currentTimeMillis();.</p> * @return <p>The duration in ticks</p>
*
* @return <p>The end time</p>
*/ */
long getActionEndTime(); long getActionDurationTicks();
} }

View File

@ -11,23 +11,23 @@ import org.jetbrains.annotations.NotNull;
public class BlacksmithReforgeStartEvent extends AbstractBlacksmithPluginEvent implements ActionStartEvent { public class BlacksmithReforgeStartEvent extends AbstractBlacksmithPluginEvent implements ActionStartEvent {
private static final HandlerList handlers = new HandlerList(); private static final HandlerList handlers = new HandlerList();
private final Long reforgeEndTime; private final long durationTicks;
/** /**
* Instantiates a new blacksmith reforge start event * Instantiates a new blacksmith reforge start event
* *
* @param npc <p>The NPC involved in the event</p> * @param npc <p>The NPC involved in the event</p>
* @param player <p>The player involved in the event</p> * @param player <p>The player involved in the event</p>
* @param reforgeEndTime <p>The time at which this reforging ends</p> * @param durationTicks <p>The duration of the reforge</p>
*/ */
public BlacksmithReforgeStartEvent(@NotNull NPC npc, @NotNull Player player, long reforgeEndTime) { public BlacksmithReforgeStartEvent(@NotNull NPC npc, @NotNull Player player, long durationTicks) {
super(npc, player); super(npc, player);
this.reforgeEndTime = reforgeEndTime; this.durationTicks = durationTicks;
} }
@Override @Override
public long getActionEndTime() { public long getActionDurationTicks() {
return reforgeEndTime; return durationTicks;
} }
/** /**

View File

@ -12,23 +12,23 @@ import org.jetbrains.annotations.NotNull;
public class ScrapperSalvageStartEvent extends AbstractBlacksmithPluginEvent implements ActionStartEvent { public class ScrapperSalvageStartEvent extends AbstractBlacksmithPluginEvent implements ActionStartEvent {
private static final HandlerList handlers = new HandlerList(); private static final HandlerList handlers = new HandlerList();
private final long salvageEndTime; private final long durationTicks;
/** /**
* Instantiates a new scrapper salvage start event * Instantiates a new scrapper salvage start event
* *
* @param npc <p>The NPC involved in the event</p> * @param npc <p>The NPC involved in the event</p>
* @param player <p>The player involved in the event</p> * @param player <p>The player involved in the event</p>
* @param salvageEndTime <p>The time at which this salvaging ends</p> * @param durationTicks <p>The duration of the salvage</p>
*/ */
public ScrapperSalvageStartEvent(@NotNull NPC npc, @NotNull Player player, long salvageEndTime) { public ScrapperSalvageStartEvent(@NotNull NPC npc, @NotNull Player player, long durationTicks) {
super(npc, player); super(npc, player);
this.salvageEndTime = salvageEndTime; this.durationTicks = durationTicks;
} }
@Override @Override
public long getActionEndTime() { public long getActionDurationTicks() {
return this.salvageEndTime; return this.durationTicks;
} }
/** /**

View File

@ -99,22 +99,15 @@ public abstract class Session implements Runnable {
BukkitScheduler scheduler = BlacksmithPlugin.getInstance().getServer().getScheduler(); BukkitScheduler scheduler = BlacksmithPlugin.getInstance().getServer().getScheduler();
int actionDelay = getActionDelay(); int actionDelay = getActionDelay();
this.finishTime = System.currentTimeMillis() + (actionDelay * 1000L); this.finishTime = System.currentTimeMillis() + (actionDelay * 1000L);
long actionDelayTicks = actionDelay * 20L;
if (this instanceof ReforgeSession) { 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) { } 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); taskId = scheduler.scheduleSyncDelayedTask(BlacksmithPlugin.getInstance(), this, actionDelayTicks);
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);
} }
/** /**
@ -174,13 +167,6 @@ public abstract class Session implements Runnable {
playSound(this.npc.getEntity(), sound); 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 * Plays a npc sound using a cancellable event
* *

View File

@ -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 <p>The recipe to get salvage for</p> * @param recipe <p>The recipe to get salvage for</p>
* @param salvagedItem <p>The item to be salvaged</p> * @return <p>The salvage resulting from the recipe</p>
* @param trashSalvage <p>Any material treated as trash salvage</p>
* @return <p>A list of items, or null if not a valid type of recipe</p>
*/ */
private static @Nullable List<ItemStack> getRecipeSalvage(@NotNull Recipe recipe, @NotNull ItemStack salvagedItem, private static List<ItemStack> getRawRecipeSalvage(@NotNull Recipe recipe) {
@NotNull Collection<Material> trashSalvage) {
List<ItemStack> ingredients; List<ItemStack> ingredients;
if (recipe instanceof ShapedRecipe shapedRecipe) { if (recipe instanceof ShapedRecipe shapedRecipe) {
ingredients = getIngredients(shapedRecipe); ingredients = getIngredients(shapedRecipe);
@ -203,8 +200,23 @@ public final class SalvageHelper {
return null; return null;
} }
//Make things easier by eliminating identical stacks //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 <p>The recipe to get salvage for</p>
* @param salvagedItem <p>The item to be salvaged</p>
* @param trashSalvage <p>Any material treated as trash salvage</p>
* @return <p>A list of items, or null if not a valid type of recipe</p>
*/
private static @Nullable List<ItemStack> getRecipeSalvage(@NotNull Recipe recipe, @NotNull ItemStack salvagedItem,
@NotNull Collection<Material> trashSalvage) {
List<ItemStack> ingredients = getRawRecipeSalvage(recipe);
if (ingredients == null) {
return null;
}
return combineStacks(getSalvage(copyItems(ingredients), salvagedItem, trashSalvage)); return combineStacks(getSalvage(copyItems(ingredients), salvagedItem, trashSalvage));
} }