Implements #1
All checks were successful
KnarCraft/PlaceholderSigns/pipeline/head This commit looks good
All checks were successful
KnarCraft/PlaceholderSigns/pipeline/head This commit looks good
This commit is contained in:
@ -3,8 +3,11 @@ package net.knarcraft.placeholdersigns.runnable;
|
||||
import me.clip.placeholderapi.PlaceholderAPI;
|
||||
import net.knarcraft.knarlib.property.ColorConversion;
|
||||
import net.knarcraft.knarlib.util.ColorHelper;
|
||||
import net.knarcraft.placeholdersigns.PlaceholderSigns;
|
||||
import net.knarcraft.placeholdersigns.container.PlaceholderSign;
|
||||
import net.knarcraft.placeholdersigns.container.QueuedPlaceholderSign;
|
||||
import net.knarcraft.placeholdersigns.handler.PlaceholderSignHandler;
|
||||
import net.knarcraft.placeholdersigns.handler.PlaceholderSignUpdateQueueHandler;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.block.Sign;
|
||||
import org.bukkit.block.sign.Side;
|
||||
@ -31,40 +34,67 @@ public class SignUpdate implements Runnable {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
for (PlaceholderSign placeholderSign : signHandler.getSigns()) {
|
||||
// Ignore signs away from players
|
||||
Location location = placeholderSign.location();
|
||||
if (!location.getChunk().isLoaded()) {
|
||||
continue;
|
||||
PlaceholderSigns instance = PlaceholderSigns.getInstance();
|
||||
PlaceholderSignUpdateQueueHandler queueHandler = instance.getUpdateQueueHandler();
|
||||
|
||||
long startTime = System.nanoTime();
|
||||
long currentTime = System.currentTimeMillis();
|
||||
while (queueHandler.peekQueue() != null && System.nanoTime() - startTime < 25000000) {
|
||||
PlaceholderSign placeholderSign = instance.getUpdateQueueHandler().pollQueue();
|
||||
if (placeholderSign == null) {
|
||||
break;
|
||||
}
|
||||
|
||||
// If no longer a sign, remove
|
||||
if (!(location.getBlock().getState() instanceof Sign sign)) {
|
||||
signHandler.unregisterSign(placeholderSign);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Update placeholders
|
||||
SignSide front = sign.getSide(Side.FRONT);
|
||||
SignSide back = sign.getSide(Side.BACK);
|
||||
Map<Side, Map<Integer, String>> placeholders = placeholderSign.placeholders();
|
||||
String[] frontLines = front.getLines();
|
||||
String[] backLines = back.getLines();
|
||||
|
||||
// Only update the sign if the text has changed
|
||||
boolean updateNecessary = false;
|
||||
if (placeholders.get(Side.FRONT) != null) {
|
||||
updateNecessary |= updatePlaceholders(frontLines, placeholders.get(Side.FRONT), front);
|
||||
}
|
||||
if (placeholders.get(Side.BACK) != null) {
|
||||
updateNecessary |= updatePlaceholders(backLines, placeholders.get(Side.BACK), back);
|
||||
}
|
||||
if (updateNecessary) {
|
||||
sign.update();
|
||||
}
|
||||
updatePlaceholderSign(placeholderSign, currentTime);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the contents of a single placeholder sign
|
||||
*
|
||||
* @param placeholderSign <p>The placeholder sign to update</p>
|
||||
* @param currentTime <p>The current time, used for re-queuing the sign</p>
|
||||
*/
|
||||
private void updatePlaceholderSign(@NotNull PlaceholderSign placeholderSign, long currentTime) {
|
||||
PlaceholderSigns instance = PlaceholderSigns.getInstance();
|
||||
|
||||
// Ignore signs away from players
|
||||
Location location = placeholderSign.getLocation();
|
||||
|
||||
// If no longer a sign, remove
|
||||
if (!(location.getBlock().getState() instanceof Sign sign)) {
|
||||
this.signHandler.unregisterSign(placeholderSign);
|
||||
this.signHandler.save();
|
||||
return;
|
||||
}
|
||||
|
||||
// Update placeholders
|
||||
SignSide front = sign.getSide(Side.FRONT);
|
||||
SignSide back = sign.getSide(Side.BACK);
|
||||
Map<Side, Map<Integer, String>> placeholders = placeholderSign.getPlaceholders();
|
||||
String[] frontLines = front.getLines();
|
||||
String[] backLines = back.getLines();
|
||||
|
||||
// Only update the sign if the text has changed
|
||||
boolean updateNecessary = false;
|
||||
if (placeholders.get(Side.FRONT) != null) {
|
||||
updateNecessary |= updatePlaceholders(frontLines, placeholders.get(Side.FRONT), front);
|
||||
}
|
||||
if (placeholders.get(Side.BACK) != null) {
|
||||
updateNecessary |= updatePlaceholders(backLines, placeholders.get(Side.BACK), back);
|
||||
}
|
||||
if (updateNecessary) {
|
||||
sign.update();
|
||||
}
|
||||
|
||||
Integer updateDelay = placeholderSign.getUpdateDelay();
|
||||
if (updateDelay == null) {
|
||||
updateDelay = instance.getSignUpdateDelay();
|
||||
}
|
||||
|
||||
instance.getUpdateQueueHandler().queueSign(new QueuedPlaceholderSign(placeholderSign, currentTime + (updateDelay * 50)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the values of placeholders on a sign
|
||||
*
|
||||
|
Reference in New Issue
Block a user