Adds information about the appropriate crafting station in ActionStartEvent
All checks were successful
EpicKnarvik97/Blacksmith/pipeline/head This commit looks good

This commit is contained in:
Kristian Knarvik 2024-08-03 16:32:44 +02:00
parent 5efba6687c
commit 81c15b6600
9 changed files with 72 additions and 13 deletions

View File

@ -1,5 +1,7 @@
package net.knarcraft.blacksmith.event; package net.knarcraft.blacksmith.event;
import org.bukkit.Material;
/** /**
* An event triggered when a blacksmith or scrapper starts reforging or salvaging an item * 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(); long getActionDurationTicks();
/**
* Gets the appropriate crafting station for this action
*
* @return <p>The appropriate crafting station</p>
*/
Material craftingStation();
} }

View File

@ -1,6 +1,7 @@
package net.knarcraft.blacksmith.event; package net.knarcraft.blacksmith.event;
import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.api.npc.NPC;
import org.bukkit.Material;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -12,17 +13,21 @@ public class BlacksmithReforgeStartEvent extends AbstractBlacksmithPluginEvent i
private static final HandlerList handlers = new HandlerList(); private static final HandlerList handlers = new HandlerList();
private final long durationTicks; private final long durationTicks;
private final Material craftingStation;
/** /**
* 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 durationTicks <p>The duration of the reforge</p> * @param durationTicks <p>The duration of the reforge</p>
* @param craftingStation <p>The appropriate crafting station for this reforging</p>
*/ */
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); super(npc, player);
this.durationTicks = durationTicks; this.durationTicks = durationTicks;
this.craftingStation = craftingStation;
} }
@Override @Override
@ -30,6 +35,11 @@ public class BlacksmithReforgeStartEvent extends AbstractBlacksmithPluginEvent i
return durationTicks; return durationTicks;
} }
@Override
public Material craftingStation() {
return craftingStation;
}
/** /**
* Gets a handler-list containing all event handlers * Gets a handler-list containing all event handlers
* *

View File

@ -1,6 +1,7 @@
package net.knarcraft.blacksmith.event; package net.knarcraft.blacksmith.event;
import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.api.npc.NPC;
import org.bukkit.Material;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -13,17 +14,21 @@ public class ScrapperSalvageStartEvent extends AbstractBlacksmithPluginEvent imp
private static final HandlerList handlers = new HandlerList(); private static final HandlerList handlers = new HandlerList();
private final long durationTicks; private final long durationTicks;
private final Material craftingStation;
/** /**
* 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 durationTicks <p>The duration of the salvage</p> * @param durationTicks <p>The duration of the salvage</p>
* @param craftingStation <p>The appropriate crafting station for this salvaging</p>
*/ */
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); super(npc, player);
this.durationTicks = durationTicks; this.durationTicks = durationTicks;
this.craftingStation = craftingStation;
} }
@Override @Override
@ -31,6 +36,11 @@ public class ScrapperSalvageStartEvent extends AbstractBlacksmithPluginEvent imp
return this.durationTicks; return this.durationTicks;
} }
@Override
public Material craftingStation() {
return this.craftingStation;
}
/** /**
* Gets a handler-list containing all event handlers * Gets a handler-list containing all event handlers
* *

View File

@ -92,7 +92,7 @@ public class EconomyManager {
private static double getSalvageCost(@NotNull SalvageMethod salvageMethod) { private static double getSalvageCost(@NotNull SalvageMethod salvageMethod) {
GlobalScrapperSettings settings = BlacksmithPlugin.getInstance().getGlobalScrapperSettings(); GlobalScrapperSettings settings = BlacksmithPlugin.getInstance().getGlobalScrapperSettings();
return switch (salvageMethod) { return switch (salvageMethod) {
case SALVAGE -> settings.getSalvageCost(); case SALVAGE, EXTENDED_SALVAGE -> settings.getSalvageCost();
case NETHERITE -> settings.getNetheriteSalvageCost(); case NETHERITE -> settings.getNetheriteSalvageCost();
case ARMOR_TRIM -> settings.getArmorTrimSalvageCost(); case ARMOR_TRIM -> settings.getArmorTrimSalvageCost();
}; };

View File

@ -10,6 +10,11 @@ public enum SalvageMethod {
*/ */
SALVAGE, SALVAGE,
/**
* Salvaging unrepairable items normally by returning the item's crafting recipe, but with unrestricted
*/
EXTENDED_SALVAGE,
/** /**
* Removing the armor trim from an item * Removing the armor trim from an item
*/ */

View File

@ -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 * Runs the actual reforge which fixes the item and gives it back to the player
*/ */

View File

@ -9,6 +9,7 @@ import net.knarcraft.blacksmith.manager.EconomyManager;
import net.knarcraft.blacksmith.property.SalvageMethod; import net.knarcraft.blacksmith.property.SalvageMethod;
import net.knarcraft.blacksmith.util.ItemHelper; import net.knarcraft.blacksmith.util.ItemHelper;
import net.knarcraft.blacksmith.util.SalvageHelper; import net.knarcraft.blacksmith.util.SalvageHelper;
import org.bukkit.Material;
import org.bukkit.Sound; import org.bukkit.Sound;
import org.bukkit.entity.LivingEntity; import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player; 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 * Runs the actual salvage which fixes the item and gives it back to the player
*/ */

View File

@ -177,12 +177,13 @@ public class ScrapperTrait extends CustomTrait<ScrapperSetting> {
// Check if any salvage will be produced // Check if any salvage will be produced
RecipeResult recipeResult = getSalvage(itemInHand, extended); RecipeResult recipeResult = getSalvage(itemInHand, extended);
SalvageMethod method = ItemHelper.isRepairable(itemInHand) ? SalvageMethod.SALVAGE : SalvageMethod.EXTENDED_SALVAGE;
boolean noUsefulSalvage = recipeResult == null || recipeResult.salvage().isEmpty(); boolean noUsefulSalvage = recipeResult == null || recipeResult.salvage().isEmpty();
if (noUsefulSalvage) { if (noUsefulSalvage) {
sendNPCMessage(this.npc, player, getSettings().getTooDamagedMessage()); 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 { } else {
return new SalvageResult(SalvageMethod.SALVAGE, recipeResult.salvage(), SalvageState.FOUND_SALVAGE, return new SalvageResult(method, recipeResult.salvage(), SalvageState.FOUND_SALVAGE,
recipeResult.recipe().getResult().getAmount()); recipeResult.recipe().getResult().getAmount());
} }
} }

View File

@ -6,6 +6,7 @@ import net.knarcraft.blacksmith.event.BlacksmithReforgeStartEvent;
import net.knarcraft.blacksmith.event.NPCSoundEvent; import net.knarcraft.blacksmith.event.NPCSoundEvent;
import net.knarcraft.blacksmith.event.ScrapperSalvageStartEvent; import net.knarcraft.blacksmith.event.ScrapperSalvageStartEvent;
import net.knarcraft.blacksmith.util.ItemHelper; import net.knarcraft.blacksmith.util.ItemHelper;
import org.bukkit.Material;
import org.bukkit.Sound; import org.bukkit.Sound;
import org.bukkit.SoundCategory; import org.bukkit.SoundCategory;
import org.bukkit.World; import org.bukkit.World;
@ -100,10 +101,11 @@ public abstract class Session implements Runnable {
this.finishTime = System.currentTimeMillis() + (actionDelay * 1000L); this.finishTime = System.currentTimeMillis() + (actionDelay * 1000L);
long actionDelayTicks = actionDelay * 20L; long actionDelayTicks = actionDelay * 20L;
BlacksmithPlugin instance = BlacksmithPlugin.getInstance();
if (this instanceof ReforgeSession) { 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) { } 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); taskId = scheduler.scheduleSyncDelayedTask(BlacksmithPlugin.getInstance(), this, actionDelayTicks);
@ -157,6 +159,13 @@ public abstract class Session implements Runnable {
*/ */
protected abstract int getActionDelay(); protected abstract int getActionDelay();
/**
* Gets the appropriate crafting station for this session
*
* @return <p>The appropriate crafting station</p>
*/
protected abstract @NotNull Material getCraftingStation();
/** /**
* Plays an NPC sound * Plays an NPC sound
* *