Skip to content

Commit

Permalink
Optimize admin shop detection with NamespacedKey
Browse files Browse the repository at this point in the history
Revised the ChestShopSign class to enhance the process of identifying an admin shop. This involved the new creation and utilization of the NamespacedKey, ADMINSHOP_NAMESPACED_KEY, linked with the sign's persistent data container. The modification improves the performance and accuracy of the admin shop detection.
  • Loading branch information
Feli499 committed Dec 20, 2023
1 parent 39989ef commit 0be745e
Show file tree
Hide file tree
Showing 16 changed files with 135 additions and 113 deletions.
Expand Up @@ -124,6 +124,14 @@ public String getSignLine(byte line) {
return signLines[line];
}

public String getOwnerName() {
return getSignLine((byte) 0);
}

public void setOwnerName(String name) {
this.setSignLine((byte) 0, name);
}

/**
* Returns the text on the sign
*
Expand Down
Expand Up @@ -2,20 +2,11 @@

import static com.Acrobot.ChestShop.Permission.ADMIN;
import static com.Acrobot.ChestShop.Permission.MOD;
import static com.Acrobot.ChestShop.Signs.ChestShopSign.NAME_LINE;

import com.Acrobot.Breeze.Utils.BlockUtil;
import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Permission;
import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Events.ShopDestroyedEvent;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import com.Acrobot.ChestShop.UUIDs.NameManager;
import com.Acrobot.ChestShop.Utils.uBlock;
import com.google.common.collect.Lists;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
Expand All @@ -34,6 +25,15 @@
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.metadata.FixedMetadataValue;

import com.Acrobot.Breeze.Utils.BlockUtil;
import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Permission;
import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Events.ShopDestroyedEvent;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import com.Acrobot.ChestShop.Utils.uBlock;
import com.google.common.collect.Lists;

/**
* @author Acrobot
*/
Expand Down Expand Up @@ -128,7 +128,7 @@ public static boolean canBlockBeBroken(Block block, Player breaker) {
continue;
}

if (Properties.TURN_OFF_SIGN_PROTECTION || canDestroyShop(breaker, sign.getLine(NAME_LINE))) {
if (Properties.TURN_OFF_SIGN_PROTECTION || canDestroyShop(breaker, sign)) {
brokenBlocks.add(sign);
} else {
canBeBroken = false;
Expand All @@ -146,8 +146,8 @@ public static boolean canBlockBeBroken(Block block, Player breaker) {
return true;
}

private static boolean canDestroyShop(Player player, String name) {
return player != null && (hasShopBreakingPermission(player) || NameManager.canUseName(player, name));
private static boolean canDestroyShop(Player player, Sign sign) {
return player != null && (hasShopBreakingPermission(player) || ChestShopSign.isOwner(player, sign));
}

private static boolean hasShopBreakingPermission(Player player) {
Expand Down
Expand Up @@ -5,31 +5,13 @@
import static com.Acrobot.ChestShop.Events.TransactionEvent.TransactionType.BUY;
import static com.Acrobot.ChestShop.Events.TransactionEvent.TransactionType.SELL;
import static com.Acrobot.ChestShop.Signs.ChestShopSign.ITEM_LINE;
import static com.Acrobot.ChestShop.Signs.ChestShopSign.NAME_LINE;
import static com.Acrobot.ChestShop.Signs.ChestShopSign.PRICE_LINE;
import static com.Acrobot.ChestShop.Signs.ChestShopSign.QUANTITY_LINE;
import static org.bukkit.event.block.Action.LEFT_CLICK_BLOCK;
import static org.bukkit.event.block.Action.RIGHT_CLICK_BLOCK;

import com.Acrobot.Breeze.Utils.BlockUtil;
import com.Acrobot.Breeze.Utils.InventoryUtil;
import com.Acrobot.Breeze.Utils.MaterialUtil;
import com.Acrobot.Breeze.Utils.NumberUtil;
import com.Acrobot.Breeze.Utils.PriceUtil;
import com.Acrobot.ChestShop.Permission;
import com.Acrobot.ChestShop.Security;
import com.Acrobot.ChestShop.Commands.ItemInfo;
import com.Acrobot.ChestShop.Configuration.Messages;
import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Containers.AdminInventory;
import com.Acrobot.ChestShop.Events.PreTransactionEvent;
import com.Acrobot.ChestShop.Events.TransactionEvent;
import com.Acrobot.ChestShop.Events.TransactionEvent.TransactionType;
import com.Acrobot.ChestShop.Plugins.ChestShop;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import com.Acrobot.ChestShop.UUIDs.NameManager;
import com.Acrobot.ChestShop.Utils.uBlock;
import java.util.UUID;

import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.OfflinePlayer;
Expand All @@ -47,6 +29,24 @@
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

import com.Acrobot.Breeze.Utils.BlockUtil;
import com.Acrobot.Breeze.Utils.InventoryUtil;
import com.Acrobot.Breeze.Utils.MaterialUtil;
import com.Acrobot.Breeze.Utils.NumberUtil;
import com.Acrobot.Breeze.Utils.PriceUtil;
import com.Acrobot.ChestShop.Permission;
import com.Acrobot.ChestShop.Security;
import com.Acrobot.ChestShop.Commands.ItemInfo;
import com.Acrobot.ChestShop.Configuration.Messages;
import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Containers.AdminInventory;
import com.Acrobot.ChestShop.Events.PreTransactionEvent;
import com.Acrobot.ChestShop.Events.TransactionEvent;
import com.Acrobot.ChestShop.Events.TransactionEvent.TransactionType;
import com.Acrobot.ChestShop.Plugins.ChestShop;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import com.Acrobot.ChestShop.Utils.uBlock;

/**
* @author Acrobot
*/
Expand Down Expand Up @@ -169,12 +169,11 @@ private static void showShopInfo(Player player, Sign sign) {
}

private static PreTransactionEvent preparePreTransactionEvent(Sign sign, Player player, Action action) {
String name = sign.getLine(NAME_LINE);

String quantity = sign.getLine(QUANTITY_LINE);
String prices = sign.getLine(PRICE_LINE);
String material = sign.getLine(ITEM_LINE);

UUID uuid = NameManager.getUUIDFor(name);
UUID uuid = ChestShopSign.getOwnerUUID(sign);

if (uuid == null) {
return null;
Expand Down
@@ -1,7 +1,6 @@
package com.Acrobot.ChestShop.Listeners.PostShopCreation;

import static com.Acrobot.ChestShop.Permission.NOFEE;
import static com.Acrobot.ChestShop.Signs.ChestShopSign.NAME_LINE;

import java.math.BigDecimal;

Expand Down Expand Up @@ -33,7 +32,7 @@ public static void onShopCreation(ShopCreatedEvent event) {
return;
}

if (ChestShopSign.isAdminShop(event.getSignLine(NAME_LINE))) {
if (ChestShopSign.isAdminShop(event.getSign())) {
return;
}

Expand Down
@@ -1,14 +1,17 @@
package com.Acrobot.ChestShop.Listeners.PostShopCreation;

import com.Acrobot.Breeze.Utils.LocationUtil;
import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Events.ShopCreatedEvent;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import static com.Acrobot.ChestShop.Signs.ChestShopSign.ITEM_LINE;
import static com.Acrobot.ChestShop.Signs.ChestShopSign.PRICE_LINE;
import static com.Acrobot.ChestShop.Signs.ChestShopSign.QUANTITY_LINE;

import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;

import static com.Acrobot.ChestShop.Signs.ChestShopSign.*;
import com.Acrobot.Breeze.Utils.LocationUtil;
import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Events.ShopCreatedEvent;
import com.Acrobot.ChestShop.Signs.ChestShopSign;

/**
* @author Acrobot
Expand All @@ -19,8 +22,7 @@ public class ShopCreationLogger implements Listener {
@EventHandler(priority = EventPriority.MONITOR)
public static void onShopCreation(final ShopCreatedEvent event) {
String creator = event.getPlayer().getName();
String shopOwner = event.getSignLine(NAME_LINE);
String typeOfShop = ChestShopSign.isAdminShop(shopOwner) ? "an Admin Shop" : "a shop";
String typeOfShop = ChestShopSign.isAdminShop(event.getSign()) ? "an Admin Shop" : "a shop";

String item = event.getSignLine(QUANTITY_LINE) + ' ' + event.getSignLine(ITEM_LINE);
String prices = event.getSignLine(PRICE_LINE);
Expand Down
@@ -1,12 +1,5 @@
package com.Acrobot.ChestShop.Listeners.PostShopCreation;

import static com.Acrobot.ChestShop.Signs.ChestShopSign.NAME_LINE;

import com.Acrobot.Breeze.Utils.BlockUtil;
import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Events.ShopCreatedEvent;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import com.Acrobot.ChestShop.Utils.uBlock;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
Expand All @@ -16,6 +9,12 @@
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;

import com.Acrobot.Breeze.Utils.BlockUtil;
import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Events.ShopCreatedEvent;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import com.Acrobot.ChestShop.Utils.uBlock;

/**
* @author Acrobot
*/
Expand All @@ -27,7 +26,7 @@ public static void onShopCreation(ShopCreatedEvent event) {
return;
}

if (ChestShopSign.isAdminShop(event.getSign().getLine(NAME_LINE))) {
if (ChestShopSign.isAdminShop(event.getSign())) {
return;
}

Expand Down
Expand Up @@ -3,29 +3,28 @@
import static com.Acrobot.ChestShop.Events.PreShopCreationEvent.CreationOutcome.NO_CHEST;
import static com.Acrobot.ChestShop.Events.PreShopCreationEvent.CreationOutcome.NO_PERMISSION_FOR_CHEST;
import static com.Acrobot.ChestShop.Permission.ADMIN;
import static com.Acrobot.ChestShop.Signs.ChestShopSign.NAME_LINE;

import com.Acrobot.ChestShop.Permission;
import com.Acrobot.ChestShop.Security;
import com.Acrobot.ChestShop.Events.PreShopCreationEvent;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import com.Acrobot.ChestShop.Utils.uBlock;
import org.bukkit.block.Container;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;

import com.Acrobot.ChestShop.Permission;
import com.Acrobot.ChestShop.Security;
import com.Acrobot.ChestShop.Events.PreShopCreationEvent;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import com.Acrobot.ChestShop.Utils.uBlock;

/**
* @author Acrobot
*/
public class ChestChecker implements Listener {

@EventHandler(priority = EventPriority.LOW)
public static void onPreShopCreation(PreShopCreationEvent event) {
String nameLine = event.getSignLine(NAME_LINE);

if (ChestShopSign.isAdminShop(nameLine)) {
if (ChestShopSign.isAdminShop(event.getSign())) {
return;
}

Expand Down
@@ -1,20 +1,20 @@
package com.Acrobot.ChestShop.Listeners.PreShopCreation;

import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Events.Economy.CurrencyCheckEvent;
import com.Acrobot.ChestShop.Events.PreShopCreationEvent;
import com.Acrobot.ChestShop.Permission;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import static com.Acrobot.ChestShop.Events.PreShopCreationEvent.CreationOutcome.NOT_ENOUGH_MONEY;
import static com.Acrobot.ChestShop.Permission.NOFEE;

import java.math.BigDecimal;

import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;

import java.math.BigDecimal;

import static com.Acrobot.ChestShop.Events.PreShopCreationEvent.CreationOutcome.NOT_ENOUGH_MONEY;
import static com.Acrobot.ChestShop.Permission.NOFEE;
import static com.Acrobot.ChestShop.Signs.ChestShopSign.NAME_LINE;
import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Permission;
import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Events.PreShopCreationEvent;
import com.Acrobot.ChestShop.Events.Economy.CurrencyCheckEvent;
import com.Acrobot.ChestShop.Signs.ChestShopSign;

/**
* @author Acrobot
Expand All @@ -29,7 +29,7 @@ public static void onPreShopCreation(PreShopCreationEvent event) {
return;
}

if (ChestShopSign.isAdminShop(event.getSignLine(NAME_LINE))) {
if (ChestShopSign.isAdminShop(event.getSign())) {
return;
}

Expand Down
@@ -1,7 +1,5 @@
package com.Acrobot.ChestShop.Listeners.PreShopCreation;

import static com.Acrobot.ChestShop.Signs.ChestShopSign.NAME_LINE;

import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
Expand All @@ -19,15 +17,15 @@ public class NameChecker implements Listener {

@EventHandler(priority = EventPriority.LOWEST)
public static void onPreShopCreation(PreShopCreationEvent event) {
String name = event.getSignLine(NAME_LINE);

String name = event.getOwnerName();
Player player = event.getPlayer();
if (Properties.ADMIN_SHOP_NAME.equalsIgnoreCase(name)) {
name = Properties.ADMIN_SHOP_NAME;
event.setSignLine(NAME_LINE, name);
}
if (name.isEmpty() || (!NameManager.canUseName(player, name) && !Permission.has(player, Permission.ADMIN))) {
String shortName = NameManager.getNameFor(player);
event.setSignLine(NAME_LINE, shortName);
name = NameManager.getNameFor(player);
}
event.setOwnerName(name);
}
}
Expand Up @@ -2,7 +2,12 @@

import static com.Acrobot.ChestShop.Events.PreShopCreationEvent.CreationOutcome.NO_PERMISSION_FOR_TERRAIN;
import static com.Acrobot.ChestShop.Permission.ADMIN;
import static com.Acrobot.ChestShop.Signs.ChestShopSign.NAME_LINE;

import org.bukkit.Location;
import org.bukkit.block.Container;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;

import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Permission;
Expand All @@ -11,11 +16,6 @@
import com.Acrobot.ChestShop.Events.Protection.BuildPermissionEvent;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import com.Acrobot.ChestShop.Utils.uBlock;
import org.bukkit.Location;
import org.bukkit.block.Container;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;

/**
* @author Acrobot
Expand All @@ -24,9 +24,8 @@ public class TerrainChecker implements Listener {

@EventHandler
public static void onPreShopCreation(PreShopCreationEvent event) {
String nameLine = event.getSignLine(NAME_LINE);

if (ChestShopSign.isAdminShop(nameLine)) {
if (ChestShopSign.isAdminShop(event.getSign())) {
return;
}

Expand Down
@@ -1,7 +1,6 @@
package com.Acrobot.ChestShop.Listeners.ShopRemoval;

import static com.Acrobot.ChestShop.Permission.NOFEE;
import static com.Acrobot.ChestShop.Signs.ChestShopSign.NAME_LINE;

import java.math.BigDecimal;
import java.util.UUID;
Expand All @@ -18,6 +17,7 @@
import com.Acrobot.ChestShop.Events.ShopDestroyedEvent;
import com.Acrobot.ChestShop.Events.Economy.CurrencyAddEvent;
import com.Acrobot.ChestShop.Events.Economy.CurrencySubtractEvent;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import com.Acrobot.ChestShop.UUIDs.NameManager;

/**
Expand All @@ -32,7 +32,7 @@ public static void onShopDestroy(ShopDestroyedEvent event) {
return;
}

UUID owner = NameManager.getUUIDFor(event.getSign().getLine(NAME_LINE));
UUID owner = ChestShopSign.getOwnerUUID(event.getSign());

CurrencyAddEvent currencyEvent = new CurrencyAddEvent(BigDecimal.valueOf(refundPrice), owner, event.getSign().getWorld());
ChestShop.callEvent(currencyEvent);
Expand Down

0 comments on commit 0be745e

Please sign in to comment.