Make Admin Shops use containers if available (Resolves #402)

Admin Shops can now be stocked the same way as player shops if a valid
 shop container is nearby. If no container is nearby it will just work
 as an unlimited shop.

This behaviour can be disabled with the FORCE_UNLIMITED_ADMIN_SHOP
 config option so that an admin shop is unlimited even though a valid
 container is next to it.
This commit is contained in:
Phoenix616 2021-01-24 23:33:36 +01:00
parent 9843e92525
commit ba47b82ba1
No known key found for this signature in database
GPG Key ID: 40E2321E71738EB0
8 changed files with 30 additions and 39 deletions

View File

@ -170,6 +170,9 @@ public class Properties {
@ConfigurationComment("First line of your Admin Shop's sign should look like this:") @ConfigurationComment("First line of your Admin Shop's sign should look like this:")
public static String ADMIN_SHOP_NAME = "Admin Shop"; public static String ADMIN_SHOP_NAME = "Admin Shop";
@ConfigurationComment("Make all admin shops be unlimited even if they have a shop container at the sign")
public static boolean FORCE_UNLIMITED_ADMIN_SHOP = false;
@ConfigurationComment("The name of the economy account which Admin Shops should use and to which all taxes will go") @ConfigurationComment("The name of the economy account which Admin Shops should use and to which all taxes will go")
public static String SERVER_ECONOMY_ACCOUNT = ""; public static String SERVER_ECONOMY_ACCOUNT = "";

View File

@ -6,6 +6,8 @@ import org.bukkit.event.Cancellable;
import org.bukkit.event.Event; import org.bukkit.event.Event;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
import javax.annotation.Nullable;
/** /**
* @author Acrobot * @author Acrobot
*/ */
@ -17,7 +19,7 @@ public class BuildPermissionEvent extends Event implements Cancellable {
private boolean allowed = true; private boolean allowed = true;
public BuildPermissionEvent(Player player, Location chest, Location sign) { public BuildPermissionEvent(Player player, @Nullable Location chest, Location sign) {
this.player = player; this.player = player;
this.chest = chest; this.chest = chest;
this.sign = sign; this.sign = sign;
@ -27,7 +29,7 @@ public class BuildPermissionEvent extends Event implements Cancellable {
return player; return player;
} }
public Location getChest() { public @Nullable Location getChest() {
return chest; return chest;
} }

View File

@ -165,11 +165,7 @@ public class SignBreak implements Listener {
} }
public static void sendShopDestroyedEvent(Sign sign, Player player) { public static void sendShopDestroyedEvent(Sign sign, Player player) {
Container connectedContainer = null; Container connectedContainer = uBlock.findConnectedContainer(sign.getBlock());
if (!ChestShopSign.isAdminShop(sign)) {
connectedContainer = uBlock.findConnectedContainer(sign.getBlock());
}
Event event = new ShopDestroyedEvent(player, sign, connectedContainer); Event event = new ShopDestroyedEvent(player, sign, connectedContainer);
ChestShop.callEvent(event); ChestShop.callEvent(event);

View File

@ -46,7 +46,8 @@ public class StockCounterModule implements Listener {
event.setSignLine(QUANTITY_LINE, Integer.toString(quantity)); event.setSignLine(QUANTITY_LINE, Integer.toString(quantity));
} }
if (!Properties.USE_STOCK_COUNTER || ChestShopSign.isAdminShop(event.getSignLine(NAME_LINE))) { if (!Properties.USE_STOCK_COUNTER
|| (Properties.FORCE_UNLIMITED_ADMIN_SHOP && ChestShopSign.isAdminShop(event.getSignLine(NAME_LINE)))) {
return; return;
} }
@ -71,11 +72,8 @@ public class StockCounterModule implements Listener {
} }
for (Sign shopSign : uBlock.findConnectedShopSigns(event.getInventory().getHolder())) { for (Sign shopSign : uBlock.findConnectedShopSigns(event.getInventory().getHolder())) {
if (ChestShopSign.isAdminShop(shopSign)) { if (!Properties.USE_STOCK_COUNTER
return; || (Properties.FORCE_UNLIMITED_ADMIN_SHOP && ChestShopSign.isAdminShop(shopSign))) {
}
if (!Properties.USE_STOCK_COUNTER) {
if (QuantityUtil.quantityLineContainsCounter(shopSign.getLine(QUANTITY_LINE))) { if (QuantityUtil.quantityLineContainsCounter(shopSign.getLine(QUANTITY_LINE))) {
removeCounterFromQuantityLine(shopSign); removeCounterFromQuantityLine(shopSign);
} }
@ -111,7 +109,7 @@ public class StockCounterModule implements Listener {
return; return;
} }
if (ChestShopSign.isAdminShop(event.getSign())) { if (Properties.FORCE_UNLIMITED_ADMIN_SHOP && ChestShopSign.isAdminShop(event.getSign())) {
return; return;
} }

View File

@ -19,9 +19,7 @@ import com.Acrobot.ChestShop.Utils.uBlock;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.GameMode; import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.Container; import org.bukkit.block.Container;
import org.bukkit.block.Sign; import org.bukkit.block.Sign;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -232,7 +230,11 @@ public class PlayerInteract implements Listener {
ItemStack[] items = InventoryUtil.getItemsStacked(item); ItemStack[] items = InventoryUtil.getItemsStacked(item);
if (adminShop) { // Create virtual admin inventory if
// - it's an admin shop
// - there is no container for the shop sign
// - the config doesn't force unlimited admin shop stock
if (adminShop && (ownerInventory == null || Properties.FORCE_UNLIMITED_ADMIN_SHOP)) {
ownerInventory = new AdminInventory(action == buy ? Arrays.stream(items).map(ItemStack::clone).toArray(ItemStack[]::new) : new ItemStack[0]); ownerInventory = new AdminInventory(action == buy ? Arrays.stream(items).map(ItemStack::clone).toArray(ItemStack[]::new) : new ItemStack[0]);
} }

View File

@ -27,9 +27,13 @@ public class EmptyShopDeleter implements Listener {
return; return;
} }
Inventory ownerInventory = event.getOwnerInventory();
Sign sign = event.getSign(); Sign sign = event.getSign();
Container connectedContainer = uBlock.findConnectedContainer(sign);
if (ChestShopSign.isAdminShop(sign)) {
return;
}
Inventory ownerInventory = event.getOwnerInventory();
if (!shopShouldBeRemoved(ownerInventory, event.getStock())) { if (!shopShouldBeRemoved(ownerInventory, event.getStock())) {
return; return;
@ -39,6 +43,8 @@ public class EmptyShopDeleter implements Listener {
return; return;
} }
Container connectedContainer = uBlock.findConnectedContainer(sign);
ShopDestroyedEvent destroyedEvent = new ShopDestroyedEvent(null, event.getSign(), connectedContainer); ShopDestroyedEvent destroyedEvent = new ShopDestroyedEvent(null, event.getSign(), connectedContainer);
ChestShop.callEvent(destroyedEvent); ChestShop.callEvent(destroyedEvent);
@ -62,7 +68,7 @@ public class EmptyShopDeleter implements Listener {
} }
private static boolean shopShouldBeRemoved(Inventory inventory, ItemStack[] stock) { private static boolean shopShouldBeRemoved(Inventory inventory, ItemStack[] stock) {
if (Properties.REMOVE_EMPTY_SHOPS && !ChestShopSign.isAdminShop(inventory)) { if (Properties.REMOVE_EMPTY_SHOPS) {
if (Properties.ALLOW_PARTIAL_TRANSACTIONS) { if (Properties.ALLOW_PARTIAL_TRANSACTIONS) {
for (ItemStack itemStack : stock) { for (ItemStack itemStack : stock) {
if (inventory.containsAtLeast(itemStack, 1)) { if (inventory.containsAtLeast(itemStack, 1)) {

View File

@ -25,14 +25,12 @@ public class ChestChecker implements Listener {
public static void onPreShopCreation(PreShopCreationEvent event) { public static void onPreShopCreation(PreShopCreationEvent event) {
String nameLine = event.getSignLine(NAME_LINE); String nameLine = event.getSignLine(NAME_LINE);
if (ChestShopSign.isAdminShop(nameLine)) {
return;
}
Container connectedContainer = uBlock.findConnectedContainer(event.getSign().getBlock()); Container connectedContainer = uBlock.findConnectedContainer(event.getSign().getBlock());
if (connectedContainer == null) { if (connectedContainer == null) {
event.setOutcome(NO_CHEST); if (!ChestShopSign.isAdminShop(nameLine)) {
event.setOutcome(NO_CHEST);
}
return; return;
} }

View File

@ -3,9 +3,7 @@ package com.Acrobot.ChestShop.Listeners.PreShopCreation;
import com.Acrobot.ChestShop.ChestShop; import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Events.PreShopCreationEvent; import com.Acrobot.ChestShop.Events.PreShopCreationEvent;
import com.Acrobot.ChestShop.Events.Protection.BuildPermissionEvent; import com.Acrobot.ChestShop.Events.Protection.BuildPermissionEvent;
import com.Acrobot.ChestShop.Permission;
import com.Acrobot.ChestShop.Security; import com.Acrobot.ChestShop.Security;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import com.Acrobot.ChestShop.Utils.uBlock; import com.Acrobot.ChestShop.Utils.uBlock;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.block.Container; import org.bukkit.block.Container;
@ -14,8 +12,6 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import static com.Acrobot.ChestShop.Events.PreShopCreationEvent.CreationOutcome.NO_PERMISSION_FOR_TERRAIN; 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;
/** /**
* @author Acrobot * @author Acrobot
@ -24,18 +20,8 @@ public class TerrainChecker implements Listener {
@EventHandler @EventHandler
public static void onPreShopCreation(PreShopCreationEvent event) { public static void onPreShopCreation(PreShopCreationEvent event) {
String nameLine = event.getSignLine(NAME_LINE);
if (ChestShopSign.isAdminShop(nameLine)) {
return;
}
Player player = event.getPlayer(); Player player = event.getPlayer();
if (Permission.has(player, ADMIN)) {
return;
}
if (!Security.canPlaceSign(player, event.getSign())) { if (!Security.canPlaceSign(player, event.getSign())) {
event.setOutcome(NO_PERMISSION_FOR_TERRAIN); event.setOutcome(NO_PERMISSION_FOR_TERRAIN);
return; return;