Fixes some savage fail problems
All checks were successful
EpicKnarvik97/Blacksmith/pipeline/head This commit looks good

Fixes item not being returned when salvage fails
Removes some redundancy when giving back items
Makes extended salvage return 50% of the salvage (no items if only one item would be returned)
Adds a better message when failing to salvage extended salvage items
Fixes a bug in the caching of smith presets
This commit is contained in:
2024-05-07 01:45:48 +02:00
parent 1d7e8a0732
commit 33ef557771
11 changed files with 170 additions and 132 deletions

View File

@@ -1,15 +1,22 @@
package net.knarcraft.blacksmith.trait;
import net.citizensnpcs.api.npc.NPC;
import net.knarcraft.blacksmith.BlacksmithPlugin;
import net.knarcraft.blacksmith.util.ItemHelper;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitScheduler;
import org.jetbrains.annotations.NotNull;
import java.util.Random;
import java.util.logging.Level;
/**
* A runnable session for performing a reforging/salvage task
*/
public abstract class Session implements Runnable {
protected static final Random random = new Random();
protected final Player player;
protected long finishTime;
protected int taskId;
@@ -85,6 +92,47 @@ public abstract class Session implements Runnable {
taskId = scheduler.scheduleSyncDelayedTask(BlacksmithPlugin.getInstance(), this, actionDelay * 20L);
}
/**
* Gives the resulting item to the player
*
* @param hasDelay <p>Whether the session was delayed, or if the item was processed instantly</p>
* @param dropItem <p>Whether the item should be dropped on the ground</p>
* @param npc <p>The NPC holding the item</p>
* @param itemToReturn <p>The item to return to the player</p>
*/
protected void giveResultingItem(boolean hasDelay, boolean dropItem, @NotNull NPC npc, @NotNull ItemStack itemToReturn) {
if (hasDelay) {
//If the player isn't online, or the player cannot fit the item, drop the item to prevent it from disappearing
if (dropItem || !this.player.isOnline() || !ItemHelper.canFitItem(this.player.getInventory(), itemToReturn)) {
this.player.getWorld().dropItemNaturally(npc.getEntity().getLocation(), itemToReturn);
} else {
this.player.getInventory().addItem(itemToReturn);
}
} else {
//It can be assumed as this happens instantly, that the player still has the item's previous slot selected
this.player.getInventory().setItemInMainHand(itemToReturn);
}
}
/**
* Randomly damages the given item
*
* @param item <p>The item to damage</p>
*/
protected void damageItemRandomly(@NotNull ItemStack item) {
short currentItemDurability = ItemHelper.getDurability(item);
short newDurability = (short) (currentItemDurability + (currentItemDurability * random.nextInt(8)));
short maxDurability = item.getType().getMaxDurability();
if (newDurability <= 0) {
newDurability = (short) (maxDurability / 3);
} else if (currentItemDurability + newDurability > maxDurability) {
newDurability = (short) (maxDurability - random.nextInt(maxDurability - 25));
}
if (ItemHelper.updateDamage(item, maxDurability - newDurability)) {
BlacksmithPlugin.getInstance().getLogger().log(Level.WARNING, "Unable to update damage for " + item);
}
}
/**
* Gets the delay for this session's action to finish
*