Adds most of the code necessary for #11

This commit is contained in:
2022-10-14 21:45:57 +02:00
parent 0c6a28d7df
commit 0357de1cf7
10 changed files with 404 additions and 28 deletions

View File

@ -12,6 +12,7 @@ import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.scheduler.BukkitScheduler;
import java.util.ArrayList;
import java.util.Calendar;
@ -32,6 +33,7 @@ public class ReforgeSession implements Runnable {
private final NPC npc;
private final ItemStack itemToReforge;
private int taskId;
private long finishTime = 0;
private final NPCSettings config;
private static final String[] enchantments = new String[Enchantment.values().length];
private static final Random random = new Random();
@ -48,15 +50,27 @@ public class ReforgeSession implements Runnable {
this.blacksmithTrait = blacksmithTrait;
this.player = player;
this.npc = npc;
itemToReforge = player.getInventory().getItemInMainHand();
this.itemToReforge = player.getInventory().getItemInMainHand();
this.config = config;
int i = 0;
for (Enchantment enchantment : Enchantment.values()) {
enchantments[i++] = enchantment.getKey().toString();
//Populate enchantments the first time this is run
if (enchantments[0] == null) {
int i = 0;
for (Enchantment enchantment : Enchantment.values()) {
enchantments[i++] = enchantment.getKey().toString();
}
}
}
/**
* Gets the time in milliseconds when this reforging session will finish
*
* @return <p>The time the reforging will finish</p>
*/
public long getFinishTime() {
return this.finishTime;
}
/**
* Runs the actual reforge which fixes the item and gives it back to the player
*/
@ -237,16 +251,17 @@ public class ReforgeSession implements Runnable {
* Begins the actual item reforging
*/
public void beginReforge() {
BukkitScheduler scheduler = BlacksmithPlugin.getInstance().getServer().getScheduler();
int reforgeDelay;
if (!config.getDisableCoolDown()) {
//Finish the reforging after a random delay between the max and min
taskId = BlacksmithPlugin.getInstance().getServer().getScheduler().scheduleSyncDelayedTask(
BlacksmithPlugin.getInstance(), this, (new Random().nextInt(config.getMaxReforgeDelay()) +
config.getMinReforgeDelay()) * 20L);
reforgeDelay = new Random().nextInt(config.getMaxReforgeDelay()) + config.getMinReforgeDelay();
} else {
//Finish the reforging as soon as possible
taskId = BlacksmithPlugin.getInstance().getServer().getScheduler().scheduleSyncDelayedTask(
BlacksmithPlugin.getInstance(), this, 0);
reforgeDelay = 0;
}
this.finishTime = System.currentTimeMillis() + (reforgeDelay * 1000L);
taskId = scheduler.scheduleSyncDelayedTask(BlacksmithPlugin.getInstance(), this, reforgeDelay * 20L);
}
/**