From ce2b07a37caa04ab92ae0591e2d3cfe5d3ab17c9 Mon Sep 17 00:00:00 2001 From: Phoenix616 Date: Wed, 18 Nov 2020 15:10:58 +0100 Subject: [PATCH] Ignore signs and transactions made by players with invalid names --- .../java/com/Acrobot/ChestShop/ChestShop.java | 1 + .../PreTransaction/InvalidNameIgnorer.java | 26 +++++++++++++++++++ .../ChestShop/Signs/ChestShopSign.java | 4 +-- 3 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/Acrobot/ChestShop/Listeners/PreTransaction/InvalidNameIgnorer.java diff --git a/src/main/java/com/Acrobot/ChestShop/ChestShop.java b/src/main/java/com/Acrobot/ChestShop/ChestShop.java index c3a1163..3c044fd 100644 --- a/src/main/java/com/Acrobot/ChestShop/ChestShop.java +++ b/src/main/java/com/Acrobot/ChestShop/ChestShop.java @@ -319,6 +319,7 @@ public class ChestShop extends JavaPlugin { registerEvent(new AmountAndPriceChecker()); } + registerEvent(new InvalidNameIgnorer()); registerEvent(new CreativeModeIgnorer()); registerEvent(new ErrorMessageSender()); registerEvent(new PermissionChecker()); diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/PreTransaction/InvalidNameIgnorer.java b/src/main/java/com/Acrobot/ChestShop/Listeners/PreTransaction/InvalidNameIgnorer.java new file mode 100644 index 0000000..409e056 --- /dev/null +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/PreTransaction/InvalidNameIgnorer.java @@ -0,0 +1,26 @@ +package com.Acrobot.ChestShop.Listeners.PreTransaction; + +import com.Acrobot.ChestShop.Events.PreTransactionEvent; +import com.Acrobot.ChestShop.Signs.ChestShopSign; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; + +import java.util.regex.Pattern; + +public class InvalidNameIgnorer implements Listener { + + private final static Pattern USERNAME_PATTERN = Pattern.compile("^\\w+$"); + + @EventHandler(priority = EventPriority.LOWEST) + public static void onPreTransaction(PreTransactionEvent event) { + if (event.isCancelled()) { + return; + } + + String name = event.getClient().getName(); + if (ChestShopSign.isAdminShop(name) || !USERNAME_PATTERN.matcher(name).matches()) { + event.setCancelled(PreTransactionEvent.TransactionOutcome.CLIENT_DOES_NOT_HAVE_PERMISSION); + } + } +} diff --git a/src/main/java/com/Acrobot/ChestShop/Signs/ChestShopSign.java b/src/main/java/com/Acrobot/ChestShop/Signs/ChestShopSign.java index 8bbad8f..dd42035 100644 --- a/src/main/java/com/Acrobot/ChestShop/Signs/ChestShopSign.java +++ b/src/main/java/com/Acrobot/ChestShop/Signs/ChestShopSign.java @@ -26,10 +26,10 @@ public class ChestShopSign { public static final byte ITEM_LINE = 3; public static final Pattern[] SHOP_SIGN_PATTERN = { - Pattern.compile("^?[\\w -.:]*$"), + Pattern.compile("^?[\\w \\-.:]*$"), Pattern.compile("^[1-9][0-9]{0,5}$"), Pattern.compile("(?i)^[\\d.bs(free) :]+$"), - Pattern.compile("^[\\w? #:-]+$") + Pattern.compile("^[\\w? #:\\-]+$") }; public static final String AUTOFILL_CODE = "?";