Writes a lot of code necessary for the scrapper

Adds the classes necessary for the new scrapper
Partly implements some scrapper functionality
Restructures some classes to reduce code duplication
Moves some classes to make some classes easier to find
Adds a bunch of TODOs where things have an unfinished implementation
This commit is contained in:
2023-11-14 16:04:48 +01:00
parent 1938a690c6
commit f3f3f66c38
40 changed files with 1788 additions and 594 deletions

View File

@@ -0,0 +1,95 @@
package net.knarcraft.blacksmith.trait;
import net.knarcraft.blacksmith.BlacksmithPlugin;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitScheduler;
import org.jetbrains.annotations.NotNull;
/**
* A runnable session for performing a reforging/salvage task
*/
public abstract class Session implements Runnable {
protected final Player player;
protected long finishTime;
protected int taskId;
/**
* Instantiates a new session
*
* @param player <p>The player the session belongs to</p>
*/
public Session(@NotNull Player player) {
this.player = player;
}
/**
* Gets whether the given player is currently in a reforging session
*
* @param other <p>The player to check if is in session</p>
* @return <p>False if the given player is in a reforge session</p>
*/
public boolean isNotInSession(@NotNull Player other) {
return !player.getUniqueId().equals(other.getUniqueId());
}
/**
* Gets the player currently in this reforge session
*
* @return <p>The player currently in this reforge session</p>
*/
public @NotNull Player getPlayer() {
return player;
}
/**
* Gets whether the current session is invalid, and should be ended
*
* <p>If the player has switched their item, the player cannot pay, or some other condition fails,
* this returns true.</p>
*
* @return <p>True if the current session should end</p>
*/
public abstract boolean isSessionInvalid();
/**
* Gets whether the current session is still running
*
* @return <p>True if the current session is still running</p>
*/
public boolean isRunning() {
return BlacksmithPlugin.getInstance().getServer().getScheduler().isQueued(taskId);
}
/**
* Gets the time in milliseconds when this session's action will finish
*
* <p>This returns 0 if the action hasn't been run yet.</p>
*
* @return <p>The timestamp for when the action will finish</p>
*/
public long getFinishTime() {
return this.finishTime;
}
/**
* Schedules this session's main task for after the action delay has passed
*/
public void scheduleAction() {
if (isRunning()) {
throw new IllegalStateException("Session action tried to run twice!");
}
BukkitScheduler scheduler = BlacksmithPlugin.getInstance().getServer().getScheduler();
int actionDelay = getActionDelay();
this.finishTime = System.currentTimeMillis() + (actionDelay * 1000L);
taskId = scheduler.scheduleSyncDelayedTask(BlacksmithPlugin.getInstance(), this, actionDelay * 20L);
}
/**
* Gets the delay for this session's action to finish
*
* @return <p>The delay to wait for this session's action to finish</p>
*/
protected abstract int getActionDelay();
}