Skip to content

Commit

Permalink
Refactor sign validation and creation in ChestShop
Browse files Browse the repository at this point in the history
Updated the sign creation process to handle item autofill validation directly. Included changes to the ChestShopSign class to centralize sign validity checks, therefore enhancing code readability and consistency. Various adjustments throughout the codebase were made to support these changes.
  • Loading branch information
Feli499 committed Dec 31, 2023
1 parent ec4563b commit c4fb1c2
Show file tree
Hide file tree
Showing 16 changed files with 116 additions and 97 deletions.
8 changes: 0 additions & 8 deletions src/main/java/com/Acrobot/ChestShop/ChestShop.java
Expand Up @@ -21,9 +21,6 @@
import com.Acrobot.ChestShop.Commands.Give;
import com.Acrobot.ChestShop.Commands.ItemInfo;
import com.Acrobot.ChestShop.Commands.RemoveAccessor;
import com.Acrobot.ChestShop.Commands.SetAmount;
import com.Acrobot.ChestShop.Commands.SetItem;
import com.Acrobot.ChestShop.Commands.SetPrice;
import com.Acrobot.ChestShop.Commands.Toggle;
import com.Acrobot.ChestShop.Commands.Version;
import com.Acrobot.ChestShop.Configuration.Messages;
Expand Down Expand Up @@ -56,7 +53,6 @@
import com.Acrobot.ChestShop.Listeners.PostTransaction.TransactionLogger;
import com.Acrobot.ChestShop.Listeners.PostTransaction.TransactionMessageSender;
import com.Acrobot.ChestShop.Listeners.PreShopCreation.ChestChecker;
import com.Acrobot.ChestShop.Listeners.PreShopCreation.ItemChecker;
import com.Acrobot.ChestShop.Listeners.PreShopCreation.MoneyChecker;
import com.Acrobot.ChestShop.Listeners.PreShopCreation.NameChecker;
import com.Acrobot.ChestShop.Listeners.PreShopCreation.PriceChecker;
Expand Down Expand Up @@ -143,9 +139,6 @@ public void onEnable() {
getCommand("csVersion").setExecutor(new Version());
getCommand("csGive").setExecutor(new Give());
getCommand("cstoggle").setExecutor(new Toggle());
getCommand("csSetItem").setExecutor(new SetItem());
getCommand("csSetPrice").setExecutor(new SetPrice());
getCommand("csSetAmount").setExecutor(new SetAmount());
getCommand("csAddAccessor").setExecutor(new AddAccessor());
getCommand("csRemoveAccessor").setExecutor(new RemoveAccessor());
}
Expand Down Expand Up @@ -290,7 +283,6 @@ private void registerPreShopCreationEvents() {
}

registerEvent(new ChestChecker());
registerEvent(new ItemChecker());
registerEvent(new MoneyChecker());
registerEvent(new NameChecker());
registerEvent(new com.Acrobot.ChestShop.Listeners.PreShopCreation.PermissionChecker());
Expand Down
15 changes: 8 additions & 7 deletions src/main/java/com/Acrobot/ChestShop/Economy/Economy.java
@@ -1,17 +1,18 @@
package com.Acrobot.ChestShop.Economy;

import java.math.BigDecimal;
import java.util.UUID;

import org.bukkit.World;
import org.bukkit.inventory.Inventory;

import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Containers.AdminInventory;
import com.Acrobot.ChestShop.Events.Economy.CurrencyAddEvent;
import com.Acrobot.ChestShop.Events.Economy.CurrencyCheckEvent;
import com.Acrobot.ChestShop.Events.Economy.CurrencyFormatEvent;
import com.Acrobot.ChestShop.Events.Economy.CurrencySubtractEvent;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import org.bukkit.World;
import org.bukkit.inventory.Inventory;

import java.math.BigDecimal;
import java.util.UUID;

/**
* @author Acrobot
Expand All @@ -23,7 +24,7 @@ public static String getServerAccountName() {
}

public static boolean isOwnerEconomicallyActive(Inventory inventory) {
return !ChestShopSign.isAdminShop(inventory) || !getServerAccountName().isEmpty();
return !(inventory instanceof AdminInventory) || !getServerAccountName().isEmpty();
}

public static boolean add(UUID name, World world, double amount) {
Expand Down
Expand Up @@ -4,6 +4,7 @@
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.bukkit.inventory.ItemStack;

/**
* Represents a state before shop is created
Expand All @@ -18,11 +19,23 @@ public class PreShopCreationEvent extends Event {
private CreationOutcome outcome = CreationOutcome.SHOP_CREATED_SUCCESSFULLY;
private Sign sign;
private String[] signLines;
private ItemStack itemStack;

public PreShopCreationEvent(Player creator, Sign sign, String[] signLines) {
public PreShopCreationEvent(Player creator, Sign sign, String[] signLines, ItemStack itemStack) {
this.creator = creator;
this.sign = sign;
this.signLines = signLines.clone();
this.itemStack = itemStack;
if (itemStack == null)
outcome = CreationOutcome.INVALID_ITEM;
}

public String getQuantityLine() {
return signLines[2];
}

public ItemStack getItemStack() {
return itemStack.clone();
}

/**
Expand Down
16 changes: 15 additions & 1 deletion src/main/java/com/Acrobot/ChestShop/Events/ShopCreatedEvent.java
@@ -1,11 +1,15 @@
package com.Acrobot.ChestShop.Events;

import javax.annotation.Nullable;

import org.bukkit.block.Container;
import org.bukkit.block.Sign;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.bukkit.inventory.ItemStack;

import com.Acrobot.ChestShop.Signs.ChestShopMetaData;

/**
* Represents a state after shop creation
Expand All @@ -19,14 +23,24 @@ public class ShopCreatedEvent extends Event {

private final Sign sign;
private final String[] signLines;
private final ChestShopMetaData chestShopMetaData;
@Nullable
private final Container chest;

public ShopCreatedEvent(Player creator, Sign sign, @Nullable Container chest, String[] signLines) {
public ShopCreatedEvent(Player creator, Sign sign, @Nullable Container chest, String[] signLines, ChestShopMetaData chestShopMetaData) {
this.creator = creator;
this.sign = sign;
this.chest = chest;
this.signLines = signLines.clone();
this.chestShopMetaData = chestShopMetaData;
}

public ItemStack getItemStack() {
return chestShopMetaData.getItemStack();
}

public int getQuantity() {
return chestShopMetaData.getQuantity();
}

/**
Expand Down
Expand Up @@ -124,7 +124,7 @@ public static boolean canBlockBeBroken(Block block, Player breaker) {

for (Sign sign : attachedSigns) {

if (!canBeBroken || !ChestShopSign.isLegacyValid(sign)) {
if (!canBeBroken || !ChestShopSign.isChestShop(sign)) {
continue;
}

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

import java.util.UUID;

import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
Expand All @@ -8,6 +10,7 @@
import org.bukkit.block.Sign;
import org.bukkit.block.sign.Side;
import org.bukkit.block.sign.SignSide;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.SignChangeEvent;
Expand All @@ -17,12 +20,15 @@

import com.Acrobot.Breeze.Utils.BlockUtil;
import com.Acrobot.Breeze.Utils.MaterialUtil;
import com.Acrobot.Breeze.Utils.PriceUtil;
import com.Acrobot.Breeze.Utils.StringUtil;
import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Events.PreShopCreationEvent;
import com.Acrobot.ChestShop.Events.ShopCreatedEvent;
import com.Acrobot.ChestShop.Signs.ChestShopMetaData;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import com.Acrobot.ChestShop.UUIDs.NameManager;
import com.Acrobot.ChestShop.Utils.uBlock;

/**
Expand Down Expand Up @@ -51,13 +57,8 @@ public static void onSignChange(SignChangeEvent event) {
return;
}

PreShopCreationEvent preEvent = new PreShopCreationEvent(event.getPlayer(), (Sign) signBlock.getState(), line);

ItemStack itemStack = getItemStack(event.getLine(4), (Sign) event.getBlock().getState());
if (itemStack == null) {
preEvent.setOutcome(PreShopCreationEvent.CreationOutcome.INVALID_ITEM);
}

PreShopCreationEvent preEvent = new PreShopCreationEvent(event.getPlayer(), (Sign) signBlock.getState(), line, itemStack);
ChestShop.callEvent(preEvent);

if (preEvent.isCancelled()) {
Expand All @@ -68,7 +69,10 @@ public static void onSignChange(SignChangeEvent event) {
event.setLine(i, preEvent.getSignLine(i));
}

ShopCreatedEvent postEvent = new ShopCreatedEvent(preEvent.getPlayer(), preEvent.getSign(), uBlock.findConnectedChest(preEvent.getSign()), preEvent.getSignLines());
ChestShopMetaData chestShopMetaData = createShopMetaData(preEvent.getSign(), event.getPlayer(), event.getLines(), itemStack);

ShopCreatedEvent postEvent = new ShopCreatedEvent(preEvent.getPlayer(), preEvent.getSign(),
uBlock.findConnectedChest(preEvent.getSign()), preEvent.getSignLines(), chestShopMetaData);
ChestShop.callEvent(postEvent);

// clear back side
Expand All @@ -78,7 +82,37 @@ public static void onSignChange(SignChangeEvent event) {
signSide.setLine(i, "");
}

ChestShopSign.createShop(sign, event.getPlayer(), event.getLines(), itemStack);
ChestShopSign.saveChestShopMetaData(sign, chestShopMetaData);
}

public static ChestShopMetaData createShopMetaData(Sign sign, Player creator, String[] signLines, ItemStack itemStack) {

int quantity = Integer.parseInt(signLines[1].replaceAll("[^0-9]", ""));

String priceLine = signLines[3];
double sellPrice = PriceUtil.getSellPrice(priceLine);
double buyPrice = PriceUtil.getBuyPrice(priceLine);

String ownerLine = signLines[0];
boolean isAdminShop = ChestShopSign.isAdminshopLine(ownerLine);

if (isAdminShop) {
return createAdminChestShop(sign, quantity, sellPrice, buyPrice, itemStack);
} else {
return createChestShop(sign, creator.getUniqueId(), quantity, sellPrice, buyPrice, itemStack);
}

}

private static ChestShopMetaData createChestShop(Sign sign, UUID owner, int quantity, double sellPrice, double buyPrice,
ItemStack itemStack) {

return new ChestShopMetaData(owner, quantity, sellPrice, buyPrice, itemStack);
}

private static ChestShopMetaData createAdminChestShop(Sign sign, int quantity, double sellPrice, double buyPrice, ItemStack itemStack) {

return new ChestShopMetaData(NameManager.getAdminShopUUID(), quantity, sellPrice, buyPrice, itemStack);
}

private static ItemStack getItemStack(String itemLine, Sign sign) {
Expand Down
@@ -1,7 +1,6 @@
package com.Acrobot.ChestShop.Listeners.Modules;

import static com.Acrobot.ChestShop.Events.PreShopCreationEvent.CreationOutcome.INVALID_PRICE;
import static com.Acrobot.ChestShop.Signs.ChestShopSign.ITEM_LINE;
import static com.Acrobot.ChestShop.Signs.ChestShopSign.PRICE_LINE;

import java.io.File;
Expand All @@ -12,7 +11,6 @@
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;

import com.Acrobot.Breeze.Utils.MaterialUtil;
import com.Acrobot.Breeze.Utils.PriceUtil;
import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Events.PreShopCreationEvent;
Expand Down Expand Up @@ -50,7 +48,7 @@ public PriceRestrictionModule() {

@EventHandler
public void onPreShopCreation(PreShopCreationEvent event) {
ItemStack material = MaterialUtil.getItem(event.getSignLine(ITEM_LINE));
ItemStack material = event.getItemStack();

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

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;
Expand All @@ -24,7 +22,7 @@ public static void onShopCreation(final ShopCreatedEvent event) {
String creator = event.getPlayer().getName();
String typeOfShop = ChestShopSign.isAdminShop(event.getSign()) ? "an Admin Shop" : "a shop";

String item = event.getSignLine(QUANTITY_LINE) + ' ' + event.getSignLine(ITEM_LINE);
String item = event.getQuantity() + ' ' + event.getItemStack().getType().toString();
String prices = event.getSignLine(PRICE_LINE);
String location = LocationUtil.locationToString(event.getSign().getLocation());

Expand Down
@@ -1,12 +1,5 @@
package com.Acrobot.ChestShop.Listeners.PostTransaction;

import com.Acrobot.Breeze.Utils.InventoryUtil;
import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Events.ShopDestroyedEvent;
import com.Acrobot.ChestShop.Events.TransactionEvent;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import com.Acrobot.ChestShop.Utils.uBlock;
import org.bukkit.Material;
import org.bukkit.block.Container;
import org.bukkit.block.Sign;
Expand All @@ -16,6 +9,14 @@
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

import com.Acrobot.Breeze.Utils.InventoryUtil;
import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Events.ShopDestroyedEvent;
import com.Acrobot.ChestShop.Events.TransactionEvent;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import com.Acrobot.ChestShop.Utils.uBlock;

/**
* @author Acrobot
*/
Expand All @@ -31,7 +32,7 @@ public static void onTransaction(TransactionEvent event) {
Sign sign = event.getSign();
Container connectedChest = uBlock.findConnectedChest(sign);

if (!shopShouldBeRemoved(ownerInventory, event.getStock())) {
if (!shopShouldBeRemoved(sign, ownerInventory, event.getStock())) {
return;
}

Expand Down Expand Up @@ -60,14 +61,14 @@ public static void onTransaction(TransactionEvent event) {
}
sign.getBlock().setType(Material.AIR);

if (Properties.REMOVE_EMPTY_CHESTS && !ChestShopSign.isAdminShop(ownerInventory) && InventoryUtil.isEmpty(ownerInventory)) {
if (Properties.REMOVE_EMPTY_CHESTS && !ChestShopSign.isAdminShop(sign) && InventoryUtil.isEmpty(ownerInventory)) {
connectedChest.getBlock().setType(Material.AIR);
} else {
ownerInventory.addItem(new ItemStack(signMaterial, 1));
}
}

private static boolean shopShouldBeRemoved(Inventory inventory, ItemStack stock) {
return Properties.REMOVE_EMPTY_SHOPS && !ChestShopSign.isAdminShop(inventory) && !InventoryUtil.hasItems(stock, inventory);
private static boolean shopShouldBeRemoved(Sign sign, Inventory inventory, ItemStack stock) {
return Properties.REMOVE_EMPTY_SHOPS && !ChestShopSign.isAdminShop(sign) && !InventoryUtil.hasItems(stock, inventory);
}
}
Expand Up @@ -5,7 +5,6 @@
import static com.Acrobot.ChestShop.Permission.SHOP_CREATION_BUY;
import static com.Acrobot.ChestShop.Permission.SHOP_CREATION_ID;
import static com.Acrobot.ChestShop.Permission.SHOP_CREATION_SELL;
import static com.Acrobot.ChestShop.Signs.ChestShopSign.ITEM_LINE;
import static com.Acrobot.ChestShop.Signs.ChestShopSign.PRICE_LINE;
import static org.bukkit.event.EventPriority.HIGH;

Expand All @@ -14,7 +13,6 @@
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;

import com.Acrobot.Breeze.Utils.MaterialUtil;
import com.Acrobot.Breeze.Utils.PriceUtil;
import com.Acrobot.ChestShop.Permission;
import com.Acrobot.ChestShop.Events.PreShopCreationEvent;
Expand All @@ -33,9 +31,8 @@ public static void onPreShopCreation(PreShopCreationEvent event) {
}

String priceLine = event.getSignLine(PRICE_LINE);
String itemLine = event.getSignLine(ITEM_LINE);

ItemStack item = MaterialUtil.getItem(itemLine);
ItemStack item = event.getItemStack();

if (item == null || Permission.has(player, SHOP_CREATION_ID + item.getType().name().toLowerCase())) {
return;
Expand Down

0 comments on commit c4fb1c2

Please sign in to comment.