Skip to content

Commit

Permalink
Add ItemChecker in pre-shop creation process
Browse files Browse the repository at this point in the history
ItemChecker class has been added for the pre-shop creation process. It validates the ItemStack, and sets the name of the item type as the third line of signLines in PreShopCreationEvent. Unused import in PreShopCreationEvent has been removed.
  • Loading branch information
Feli499 committed Jan 1, 2024
1 parent 78e3a71 commit 5e125ed
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/Acrobot/ChestShop/ChestShop.java
Expand Up @@ -53,6 +53,7 @@
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 @@ -289,6 +290,7 @@ private void registerPreShopCreationEvents() {
registerEvent(new com.Acrobot.ChestShop.Listeners.PreShopCreation.ErrorMessageSender());
registerEvent(new PriceChecker());
registerEvent(new QuantityChecker());
registerEvent(new ItemChecker());
registerEvent(new TerrainChecker());
}

Expand Down
Expand Up @@ -6,8 +6,6 @@
import org.bukkit.event.HandlerList;
import org.bukkit.inventory.ItemStack;

import com.Acrobot.Breeze.Utils.MaterialUtil;

/**
* Represents a state before shop is created
*
Expand All @@ -30,14 +28,16 @@ public PreShopCreationEvent(Player creator, Sign sign, String[] signLines, ItemS
this.itemStack = itemStack;
if (itemStack == null)
outcome = CreationOutcome.INVALID_ITEM;
else
this.signLines[3] = MaterialUtil.getName(itemStack.getType());
}

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

public String setItemLine(String line) {
return signLines[3] = line;
}

public ItemStack getItemStack() {
return itemStack.clone();
}
Expand Down
@@ -0,0 +1,34 @@
package com.Acrobot.ChestShop.Listeners.PreShopCreation;

import static com.Acrobot.ChestShop.Events.PreShopCreationEvent.CreationOutcome.INVALID_ITEM;

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

import com.Acrobot.Breeze.Utils.MaterialUtil;
import com.Acrobot.ChestShop.Events.PreShopCreationEvent;

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

@EventHandler(priority = EventPriority.LOWEST)
public static void onPreShopCreation(PreShopCreationEvent event) {

ItemStack itemStack = event.getItemStack();
if (itemStack == null) {
event.setOutcome(INVALID_ITEM);
return;
}

String itemName = MaterialUtil.getName(itemStack.getType());
if (itemName == null) {
event.setOutcome(INVALID_ITEM);
return;
}
event.setItemLine(itemName);
}
}

0 comments on commit 5e125ed

Please sign in to comment.