Adds increased detection and optional refunding of signs broken by non-players

This commit is contained in:
Kristian Knarvik 2022-03-02 14:05:20 +01:00
parent 3d83458b9c
commit c2ffe5e903
6 changed files with 133 additions and 39 deletions

View File

@ -88,6 +88,8 @@ Removes a registered paid sign
* ignoreColor - Whether to ignore any color or formatting applied to the text when trying to match a paid sign's text. * ignoreColor - Whether to ignore any color or formatting applied to the text when trying to match a paid sign's text.
The option can be set on a per-sign basis, but this value is used if not specified. The correct value depends on The option can be set on a per-sign basis, but this value is used if not specified. The correct value depends on
whether the plugin signs it should match allow coloring or not. whether the plugin signs it should match allow coloring or not.
* enableRefunds - Whether to enable refunds to the sign creator when a sign detected as a paid sign is broken (payment * refundsEnabled - Whether to enable refunds to the sign creator when a sign detected as a paid sign is broken (payment
will always go to the original creator) will always go to the original creator)
* refundPercentage - The percentage of the paid sign cost to refund (0-100) * refundPercentage - The percentage of the paid sign cost to refund (0-100)
* refundAlways - Whether to refund when signs that players have paid for are broken by anything. This includes tnt,
creepers, pistons and similar

View File

@ -11,7 +11,7 @@ import net.knarcraft.paidsigns.command.RemoveConditionCommand;
import net.knarcraft.paidsigns.command.RemoveConditionTabCompleter; import net.knarcraft.paidsigns.command.RemoveConditionTabCompleter;
import net.knarcraft.paidsigns.command.RemoveTabCommand; import net.knarcraft.paidsigns.command.RemoveTabCommand;
import net.knarcraft.paidsigns.formatting.Translator; import net.knarcraft.paidsigns.formatting.Translator;
import net.knarcraft.paidsigns.listener.BlockBreakListener; import net.knarcraft.paidsigns.listener.SignBreakListener;
import net.knarcraft.paidsigns.listener.SignListener; import net.knarcraft.paidsigns.listener.SignListener;
import net.knarcraft.paidsigns.manager.EconomyManager; import net.knarcraft.paidsigns.manager.EconomyManager;
import net.knarcraft.paidsigns.manager.PaidSignManager; import net.knarcraft.paidsigns.manager.PaidSignManager;
@ -37,8 +37,9 @@ public final class PaidSigns extends JavaPlugin {
private String language; private String language;
private boolean ignoreCase; private boolean ignoreCase;
private boolean ignoreColor; private boolean ignoreColor;
private boolean enableRefunds; private boolean refundsEnabled;
private short refundPercentage; private short refundPercentage;
private boolean refundAlways;
/** /**
* Instantiates a new paid signs object * Instantiates a new paid signs object
@ -67,7 +68,7 @@ public final class PaidSigns extends JavaPlugin {
PluginManager pluginManager = getServer().getPluginManager(); PluginManager pluginManager = getServer().getPluginManager();
pluginManager.registerEvents(new SignListener(), this); pluginManager.registerEvents(new SignListener(), this);
pluginManager.registerEvents(new BlockBreakListener(), this); pluginManager.registerEvents(new SignBreakListener(), this);
registerCommands(); registerCommands();
} }
@ -120,7 +121,7 @@ public final class PaidSigns extends JavaPlugin {
* @return <p>Whether refunds are currently enabled</p> * @return <p>Whether refunds are currently enabled</p>
*/ */
public boolean areRefundsEnabled() { public boolean areRefundsEnabled() {
return this.enableRefunds; return this.refundsEnabled;
} }
/** /**
@ -137,6 +138,15 @@ public final class PaidSigns extends JavaPlugin {
return this.refundPercentage; return this.refundPercentage;
} }
/**
* Gets whether refunds should always happen, even if signs are not broken by players
*
* @return <p>True if refunds should always happen</p>
*/
public boolean refundAlways() {
return this.refundAlways;
}
/** /**
* Registers the commands used by this plugin * Registers the commands used by this plugin
*/ */
@ -175,11 +185,12 @@ public final class PaidSigns extends JavaPlugin {
config.options().copyDefaults(true); config.options().copyDefaults(true);
this.saveDefaultConfig(); this.saveDefaultConfig();
this.saveConfig(); this.saveConfig();
language = config.getString("language", "en");
ignoreCase = config.getBoolean("ignoreCase", true); ignoreCase = config.getBoolean("ignoreCase", true);
ignoreColor = config.getBoolean("ignoreColor", false); ignoreColor = config.getBoolean("ignoreColor", false);
enableRefunds = config.getBoolean("enableRefunds", true); refundsEnabled = config.getBoolean("refundsEnabled", true);
refundPercentage = (short) config.getInt("refundPercentage", 100); refundPercentage = (short) config.getInt("refundPercentage", 100);
language = config.getString("language", "en"); refundAlways = config.getBoolean("refundAlways", false);
} }
/** /**

View File

@ -1,27 +0,0 @@
package net.knarcraft.paidsigns.listener;
import net.knarcraft.paidsigns.manager.TrackedSignManager;
import org.bukkit.block.Sign;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import java.io.IOException;
/**
* A listener that listens for any tracked signs being broken
*/
public class BlockBreakListener implements Listener {
@EventHandler(priority = EventPriority.MONITOR)
public void onBlockBreak(BlockBreakEvent event) {
if (event.getBlock().getState() instanceof Sign) {
try {
TrackedSignManager.removeTrackedSign(event.getBlock().getLocation());
} catch (IOException ignored) {
}
}
}
}

View File

@ -0,0 +1,103 @@
package net.knarcraft.paidsigns.listener;
import net.knarcraft.paidsigns.PaidSigns;
import net.knarcraft.paidsigns.manager.TrackedSignManager;
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;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockExplodeEvent;
import org.bukkit.event.block.BlockPistonExtendEvent;
import org.bukkit.event.block.BlockPistonRetractEvent;
import org.bukkit.event.entity.EntityChangeBlockEvent;
import org.bukkit.event.entity.EntityExplodeEvent;
import java.io.IOException;
import java.util.List;
/**
* A listener that listens for any tracked signs being broken
*/
public class SignBreakListener implements Listener {
@EventHandler(priority = EventPriority.MONITOR)
public void onBlockBreak(BlockBreakEvent event) {
if (event.isCancelled()) {
return;
}
removeTrackedSign(event.getBlock(), true);
}
@EventHandler(priority = EventPriority.MONITOR)
public void onExplosion(BlockExplodeEvent event) {
if (event.isCancelled()) {
return;
}
removeTrackedSigns(event.blockList());
}
@EventHandler(priority = EventPriority.MONITOR)
public void onEntityExplosion(EntityExplodeEvent event) {
if (event.isCancelled()) {
return;
}
removeTrackedSigns(event.blockList());
}
@EventHandler(priority = EventPriority.MONITOR)
public void onPistonPush(BlockPistonExtendEvent event) {
if (event.isCancelled()) {
return;
}
removeTrackedSigns(event.getBlocks());
}
@EventHandler(priority = EventPriority.MONITOR)
public void onPistonPull(BlockPistonRetractEvent event) {
if (event.isCancelled()) {
return;
}
removeTrackedSigns(event.getBlocks());
}
@EventHandler(priority = EventPriority.MONITOR)
public void onEntityBlockChange(EntityChangeBlockEvent event) {
if (event.isCancelled()) {
return;
}
if (event.getEntity() instanceof Player) {
return;
}
removeTrackedSign(event.getBlock(), PaidSigns.getInstance().refundAlways());
}
/**
* Removes all tracked signs from the given blocks
*
* @param blocks <p>The blocks to search for tracked signs</p>
*/
private void removeTrackedSigns(List<Block> blocks) {
for (Block block : blocks) {
removeTrackedSign(block, PaidSigns.getInstance().refundAlways());
}
}
/**
* Tries to remove any tracked sign at the given block
*
* @param block <p>The block that might be a sign</p>
* @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) {
try {
TrackedSignManager.removeTrackedSign(block.getLocation(), refund);
} catch (IOException ignored) {
}
}
}
}

View File

@ -49,16 +49,17 @@ public final class TrackedSignManager {
* Removes a tracked sign from the manager * Removes a tracked sign from the manager
* *
* @param signLocation <p>The location the sign was removed from</p> * @param signLocation <p>The location the sign was removed from</p>
* @param refund <p>Whether to perform a refund after un-tracking the sign</p>
* @throws IOException <p>If unable to save the tracked signs</p> * @throws IOException <p>If unable to save the tracked signs</p>
*/ */
public static void removeTrackedSign(Location signLocation) throws IOException { public static void removeTrackedSign(Location signLocation, boolean refund) throws IOException {
if (!trackedSigns.containsKey(signLocation)) { if (!trackedSigns.containsKey(signLocation)) {
return; return;
} }
TrackedSign trackedSign = trackedSigns.get(signLocation); TrackedSign trackedSign = trackedSigns.get(signLocation);
trackedSigns.remove(signLocation); trackedSigns.remove(signLocation);
saveTrackedSigns(); saveTrackedSigns();
if (!PaidSigns.getInstance().areRefundsEnabled()) { if (!PaidSigns.getInstance().areRefundsEnabled() || !refund) {
return; return;
} }
OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(trackedSign.getPlayerId()); OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(trackedSign.getPlayerId());

View File

@ -13,7 +13,11 @@ ignoreColor: false
# Whether to enable refunds to the sign creator when a sign detected as a paid sign is broken (payment will always go # Whether to enable refunds to the sign creator when a sign detected as a paid sign is broken (payment will always go
# to the original creator) # to the original creator)
enableRefunds: true refundsEnabled: true
# The percentage of the paid sign cost to refund (0-100) # The percentage of the paid sign cost to refund (0-100)
refundPercentage: 100 refundPercentage: 100
# Whether to refund when signs that players have paid for are broken by anything. This includes tnt, creepers, pistons
# and similar
refundAlways: false