Makes a method to check if a block is a sign

This will hopefully prevent any more mistakes related to checking if something is a sign or not.
This commit is contained in:
Kristian Knarvik 2022-11-12 12:48:21 +01:00
parent cf66113ac1
commit e7c40fb4b0
4 changed files with 31 additions and 6 deletions

View File

@ -2,8 +2,8 @@ package net.knarcraft.paidsigns.listener;
import net.knarcraft.paidsigns.PaidSigns;
import net.knarcraft.paidsigns.manager.TrackedSignManager;
import net.knarcraft.paidsigns.utility.SignHelper;
import org.bukkit.block.Block;
import org.bukkit.block.Sign;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
@ -92,7 +92,7 @@ public class SignBreakListener implements Listener {
* @param refund <p>Whether to perform a refund after un-tracking the sign</p>
*/
private void removeTrackedSign(Block block, boolean refund) {
if (block.getState() instanceof Sign) {
if (SignHelper.isSign(block)) {
try {
TrackedSignManager.removeTrackedSign(block.getLocation(), refund, false);
} catch (IOException ignored) {

View File

@ -7,10 +7,10 @@ import net.knarcraft.paidsigns.container.PaidSignConditionMatch;
import net.knarcraft.paidsigns.formatting.PaidSignsTranslatableMessage;
import net.knarcraft.paidsigns.manager.EconomyManager;
import net.knarcraft.paidsigns.manager.TrackedSignManager;
import net.knarcraft.paidsigns.utility.SignHelper;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.block.Sign;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
@ -127,7 +127,7 @@ public class SignListener implements Listener {
//Immediately refund if a plugin destroyed the sign within 5 ticks of the creation
Bukkit.getScheduler().scheduleSyncDelayedTask(PaidSigns.getInstance(), () -> {
Block block = signLocation.getBlock();
if (!(block.getState() instanceof Sign)) {
if (!SignHelper.isSign(block)) {
try {
TrackedSignManager.removeTrackedSign(block.getLocation(), true, true);
} catch (IOException e) {

View File

@ -3,10 +3,10 @@ package net.knarcraft.paidsigns.manager;
import net.knarcraft.paidsigns.PaidSigns;
import net.knarcraft.paidsigns.container.TrackedSign;
import net.knarcraft.paidsigns.formatting.PaidSignsTranslatableMessage;
import net.knarcraft.paidsigns.utility.SignHelper;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.block.Sign;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
@ -110,7 +110,7 @@ public final class TrackedSignManager {
TrackedSign trackedSign = new TrackedSign(playerId, cost);
//Prevent destroyed signs from being tracked indefinitely
if (!(signLocation.getBlock().getState() instanceof Sign)) {
if (!SignHelper.isSign(signLocation.getBlock())) {
Bukkit.getScheduler().scheduleSyncDelayedTask(PaidSigns.getInstance(), () -> {
PaidSigns.getInstance().getLogger().log(Level.WARNING, "The sign at " + signLocation +
" no longer exists. Removing from sign tracker. Refunding the player.");

View File

@ -0,0 +1,25 @@
package net.knarcraft.paidsigns.utility;
import org.bukkit.block.Block;
import org.bukkit.block.Sign;
/**
* A helper class for dealing with signs
*/
public final class SignHelper {
private SignHelper() {
}
/**
* Checks whether the given block is a sign
*
* @param block <p>The block to check</p>
* @return <p>True if the block is a sign</p>
*/
public static boolean isSign(Block block) {
return block.getState() instanceof Sign;
}
}