Skip to content

Commit

Permalink
Make Admin Shops use containers if available (Resolves #402)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Phoenix616 committed Jan 24, 2021
1 parent 9843e92 commit ba47b82
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ public <T> Object parseToJava(Class<T> type, Object object) {
@ConfigurationComment("First line of your Admin Shop's sign should look like this:")
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")
public static String SERVER_ECONOMY_ACCOUNT = "";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;

import javax.annotation.Nullable;

/**
* @author Acrobot
*/
Expand All @@ -17,7 +19,7 @@ public class BuildPermissionEvent extends Event implements Cancellable {

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.chest = chest;
this.sign = sign;
Expand All @@ -27,7 +29,7 @@ public Player getPlayer() {
return player;
}

public Location getChest() {
public @Nullable Location getChest() {
return chest;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,7 @@ private static boolean canDestroyShop(Player player, String name) {
}

public static void sendShopDestroyedEvent(Sign sign, Player player) {
Container connectedContainer = null;

if (!ChestShopSign.isAdminShop(sign)) {
connectedContainer = uBlock.findConnectedContainer(sign.getBlock());
}
Container connectedContainer = uBlock.findConnectedContainer(sign.getBlock());

Event event = new ShopDestroyedEvent(player, sign, connectedContainer);
ChestShop.callEvent(event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public static void onPreShopCreation(PreShopCreationEvent event) {
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;
}

Expand All @@ -71,11 +72,8 @@ public static void onInventoryClose(InventoryCloseEvent event) {
}

for (Sign shopSign : uBlock.findConnectedShopSigns(event.getInventory().getHolder())) {
if (ChestShopSign.isAdminShop(shopSign)) {
return;
}

if (!Properties.USE_STOCK_COUNTER) {
if (!Properties.USE_STOCK_COUNTER
|| (Properties.FORCE_UNLIMITED_ADMIN_SHOP && ChestShopSign.isAdminShop(shopSign))) {
if (QuantityUtil.quantityLineContainsCounter(shopSign.getLine(QUANTITY_LINE))) {
removeCounterFromQuantityLine(shopSign);
}
Expand Down Expand Up @@ -111,7 +109,7 @@ public static void onTransaction(final TransactionEvent event) {
return;
}

if (ChestShopSign.isAdminShop(event.getSign())) {
if (Properties.FORCE_UNLIMITED_ADMIN_SHOP && ChestShopSign.isAdminShop(event.getSign())) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.Container;
import org.bukkit.block.Sign;
import org.bukkit.entity.Player;
Expand Down Expand Up @@ -232,7 +230,11 @@ private static PreTransactionEvent preparePreTransactionEvent(Sign sign, Player

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]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@ public static void onTransaction(TransactionEvent event) {
return;
}

Inventory ownerInventory = event.getOwnerInventory();
Sign sign = event.getSign();
Container connectedContainer = uBlock.findConnectedContainer(sign);

if (ChestShopSign.isAdminShop(sign)) {
return;
}

Inventory ownerInventory = event.getOwnerInventory();

if (!shopShouldBeRemoved(ownerInventory, event.getStock())) {
return;
Expand All @@ -39,6 +43,8 @@ public static void onTransaction(TransactionEvent event) {
return;
}

Container connectedContainer = uBlock.findConnectedContainer(sign);

ShopDestroyedEvent destroyedEvent = new ShopDestroyedEvent(null, event.getSign(), connectedContainer);
ChestShop.callEvent(destroyedEvent);

Expand All @@ -62,7 +68,7 @@ public static void onTransaction(TransactionEvent event) {
}

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) {
for (ItemStack itemStack : stock) {
if (inventory.containsAtLeast(itemStack, 1)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,12 @@ public class ChestChecker implements Listener {
public static void onPreShopCreation(PreShopCreationEvent event) {
String nameLine = event.getSignLine(NAME_LINE);

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

Container connectedContainer = uBlock.findConnectedContainer(event.getSign().getBlock());

if (connectedContainer == null) {
event.setOutcome(NO_CHEST);
if (!ChestShopSign.isAdminShop(nameLine)) {
event.setOutcome(NO_CHEST);
}
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Events.PreShopCreationEvent;
import com.Acrobot.ChestShop.Events.Protection.BuildPermissionEvent;
import com.Acrobot.ChestShop.Permission;
import com.Acrobot.ChestShop.Security;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import com.Acrobot.ChestShop.Utils.uBlock;
import org.bukkit.Location;
import org.bukkit.block.Container;
Expand All @@ -14,8 +12,6 @@
import org.bukkit.event.Listener;

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
Expand All @@ -24,18 +20,8 @@ public class TerrainChecker implements Listener {

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

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

Player player = event.getPlayer();

if (Permission.has(player, ADMIN)) {
return;
}

if (!Security.canPlaceSign(player, event.getSign())) {
event.setOutcome(NO_PERMISSION_FOR_TERRAIN);
return;
Expand Down

0 comments on commit ba47b82

Please sign in to comment.