mirror of
https://github.com/ChestShop-authors/ChestShop-3.git
synced 2024-12-24 01:07:32 +01:00
Many, many changes
- Added ShopDestroyedEvent - Fixed signs stacked on each other not working - Fixed Admin Shops selling materials with metadata - Fixed shops accepting '0' quantity - Started working on database stuff - Split BlockBreak listener to many files - Added methods to InventoryUtil - Fixed discounts
This commit is contained in:
parent
1e85e427ca
commit
1bf4651efa
8
com/Acrobot/Breeze/Database/DatabaseDriver.java
Normal file
8
com/Acrobot/Breeze/Database/DatabaseDriver.java
Normal file
@ -0,0 +1,8 @@
|
||||
package com.Acrobot.Breeze.Database;
|
||||
|
||||
/**
|
||||
* @author Acrobot
|
||||
*/
|
||||
public interface DatabaseDriver {
|
||||
|
||||
}
|
@ -22,6 +22,10 @@ public class InventoryUtil {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (inventory.getType() == null) {
|
||||
return Integer.MAX_VALUE;
|
||||
}
|
||||
|
||||
HashMap<Integer, ? extends ItemStack> items = inventory.all(item.getType());
|
||||
int itemAmount = 0;
|
||||
|
||||
@ -36,6 +40,39 @@ public class InventoryUtil {
|
||||
return itemAmount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tells if the inventory is empty
|
||||
*
|
||||
* @param inventory inventory
|
||||
* @return Is the inventory empty?
|
||||
*/
|
||||
public static boolean isEmpty(Inventory inventory) {
|
||||
for (ItemStack stack : inventory.getContents()) {
|
||||
if (!MaterialUtil.isEmpty(stack)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the inventory has stock of this type
|
||||
*
|
||||
* @param items items
|
||||
* @param inventory inventory
|
||||
* @return Does the inventory contain stock of this type?
|
||||
*/
|
||||
public static boolean hasItems(ItemStack[] items, Inventory inventory) {
|
||||
for (ItemStack item : items) {
|
||||
if (InventoryUtil.getAmount(item, inventory) < item.getAmount()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the item fits the inventory
|
||||
*
|
||||
@ -50,7 +87,7 @@ public class InventoryUtil {
|
||||
if (left <= 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
if (MaterialUtil.isEmpty(iStack)) {
|
||||
left -= inventory.getMaxStackSize();
|
||||
continue;
|
||||
|
@ -7,22 +7,18 @@ import com.Acrobot.ChestShop.Config.Property;
|
||||
import com.Acrobot.ChestShop.DB.Generator;
|
||||
import com.Acrobot.ChestShop.DB.Queue;
|
||||
import com.Acrobot.ChestShop.DB.Transaction;
|
||||
import com.Acrobot.ChestShop.Listeners.Block.BlockBreak;
|
||||
import com.Acrobot.ChestShop.Listeners.Block.BlockPlace;
|
||||
import com.Acrobot.ChestShop.Listeners.Block.Break.ChestBreak;
|
||||
import com.Acrobot.ChestShop.Listeners.Block.EntityExplode;
|
||||
import com.Acrobot.ChestShop.Listeners.Block.Break.SignBreak;
|
||||
import com.Acrobot.ChestShop.Listeners.Block.SignChange;
|
||||
import com.Acrobot.ChestShop.Listeners.ItemInfoListener;
|
||||
import com.Acrobot.ChestShop.Listeners.Player.PlayerConnect;
|
||||
import com.Acrobot.ChestShop.Listeners.Player.PlayerInteract;
|
||||
import com.Acrobot.ChestShop.Listeners.Player.PlayerInventory;
|
||||
import com.Acrobot.ChestShop.Listeners.Player.ShortNameSaver;
|
||||
import com.Acrobot.ChestShop.Listeners.PostTransaction.EconomicModule;
|
||||
import com.Acrobot.ChestShop.Listeners.PostTransaction.ItemManager;
|
||||
import com.Acrobot.ChestShop.Listeners.PostTransaction.*;
|
||||
import com.Acrobot.ChestShop.Listeners.PreTransaction.*;
|
||||
import com.Acrobot.ChestShop.Listeners.PostTransaction.EmptyShopDeleter;
|
||||
import com.Acrobot.ChestShop.Listeners.PostTransaction.TransactionLogger;
|
||||
import com.Acrobot.ChestShop.Listeners.PostTransaction.TransactionMessageSender;
|
||||
import com.Acrobot.ChestShop.Listeners.ShopRefundListener;
|
||||
import com.Acrobot.ChestShop.Logging.FileFormatter;
|
||||
import com.Acrobot.ChestShop.Signs.RestrictedSign;
|
||||
import com.avaje.ebean.EbeanServer;
|
||||
@ -141,11 +137,11 @@ public class ChestShop extends JavaPlugin {
|
||||
private void registerEvents() {
|
||||
registerEvent(new com.Acrobot.ChestShop.Plugins.ChestShop()); //Chest protection
|
||||
|
||||
registerEvent(new BlockBreak());
|
||||
registerEvent(new SignBreak());
|
||||
registerEvent(new ChestBreak());
|
||||
|
||||
registerEvent(new BlockPlace());
|
||||
registerEvent(new SignChange());
|
||||
registerEvent(new EntityExplode());
|
||||
registerEvent(new PlayerConnect());
|
||||
registerEvent(new PlayerInteract());
|
||||
registerEvent(new PlayerInventory());
|
||||
@ -169,6 +165,7 @@ public class ChestShop extends JavaPlugin {
|
||||
|
||||
registerEvent(new RestrictedSign());
|
||||
registerEvent(new DiscountModule());
|
||||
registerEvent(new ShopRefundListener());
|
||||
|
||||
if (Config.getBoolean(ALLOW_PARTIAL_TRANSACTIONS)) {
|
||||
registerEvent(new PartialTransactionModule());
|
||||
|
@ -88,6 +88,16 @@ public class AdminInventory implements Inventory {
|
||||
}
|
||||
|
||||
public HashMap<Integer, ? extends ItemStack> all(Material material) {
|
||||
if (material.getMaxDurability() != 0) {
|
||||
HashMap<Integer, ItemStack> items = new HashMap<Integer, ItemStack>();
|
||||
|
||||
for (short currentDurability = 0; currentDurability < material.getMaxDurability(); currentDurability++) {
|
||||
items.put((int) currentDurability, new ItemStack(material, Integer.MAX_VALUE, currentDurability));
|
||||
}
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
return all(material.getId());
|
||||
}
|
||||
|
||||
|
@ -6,6 +6,8 @@ import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.event.HandlerList;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* @author Acrobot
|
||||
*/
|
||||
@ -13,11 +15,12 @@ public class ShopCreatedEvent extends Event {
|
||||
private static final HandlerList handlers = new HandlerList();
|
||||
|
||||
private Player player;
|
||||
|
||||
private Sign sign;
|
||||
private Chest chest;
|
||||
private String[] signLines;
|
||||
|
||||
public ShopCreatedEvent(Player player, Sign sign, Chest chest, String[] signLines) {
|
||||
public ShopCreatedEvent(Player player, Sign sign, @Nullable Chest chest, String[] signLines) {
|
||||
this.player = player;
|
||||
this.sign = sign;
|
||||
this.chest = chest;
|
||||
@ -36,7 +39,7 @@ public class ShopCreatedEvent extends Event {
|
||||
return sign;
|
||||
}
|
||||
|
||||
public Chest getChest() {
|
||||
@Nullable public Chest getChest() {
|
||||
return chest;
|
||||
}
|
||||
|
||||
|
48
com/Acrobot/ChestShop/Events/ShopDestroyedEvent.java
Normal file
48
com/Acrobot/ChestShop/Events/ShopDestroyedEvent.java
Normal file
@ -0,0 +1,48 @@
|
||||
package com.Acrobot.ChestShop.Events;
|
||||
|
||||
import org.bukkit.block.Chest;
|
||||
import org.bukkit.block.Sign;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.event.HandlerList;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* @author Acrobot
|
||||
*/
|
||||
public class ShopDestroyedEvent extends Event {
|
||||
private static final HandlerList handlers = new HandlerList();
|
||||
|
||||
private final Player destroyer;
|
||||
|
||||
private final Sign sign;
|
||||
private final Chest chest;
|
||||
|
||||
public ShopDestroyedEvent(@Nullable Player destroyer, Sign sign, @Nullable Chest chest) {
|
||||
this.destroyer = destroyer;
|
||||
this.sign = sign;
|
||||
this.chest = chest;
|
||||
}
|
||||
|
||||
@Nullable public Player getDestroyer() {
|
||||
return destroyer;
|
||||
}
|
||||
|
||||
@Nullable public Chest getChest() {
|
||||
return chest;
|
||||
}
|
||||
|
||||
public Sign getSign() {
|
||||
return sign;
|
||||
}
|
||||
|
||||
public static HandlerList getHandlerList() {
|
||||
return handlers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HandlerList getHandlers() {
|
||||
return handlers;
|
||||
}
|
||||
}
|
@ -1,129 +0,0 @@
|
||||
package com.Acrobot.ChestShop.Listeners.Block;
|
||||
|
||||
import com.Acrobot.Breeze.Utils.BlockUtil;
|
||||
import com.Acrobot.ChestShop.Config.Config;
|
||||
import com.Acrobot.ChestShop.Config.Language;
|
||||
import com.Acrobot.ChestShop.Economy.Economy;
|
||||
import com.Acrobot.ChestShop.Permission;
|
||||
import com.Acrobot.ChestShop.Utils.uBlock;
|
||||
import com.Acrobot.ChestShop.Utils.uName;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.BlockFace;
|
||||
import org.bukkit.block.Sign;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.block.BlockBreakEvent;
|
||||
import org.bukkit.event.block.BlockPistonExtendEvent;
|
||||
import org.bukkit.event.block.BlockPistonRetractEvent;
|
||||
import org.bukkit.material.Directional;
|
||||
import org.bukkit.material.PistonBaseMaterial;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static com.Acrobot.ChestShop.Config.Property.SHOP_REFUND_PRICE;
|
||||
import static com.Acrobot.ChestShop.Config.Property.TURN_OFF_SIGN_PROTECTION;
|
||||
|
||||
/**
|
||||
* @author Acrobot
|
||||
*/
|
||||
public class BlockBreak implements Listener {
|
||||
public static boolean cancellingBlockBreak(Block block, Player player) {
|
||||
if (block == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (BlockUtil.isSign(block)) block.getState().update(); //Show the text immediately
|
||||
|
||||
Sign sign = uBlock.findValidShopSign(block, (player != null ? uName.stripName(player.getName()) : null));
|
||||
if (!isCorrectSign(sign, block)) return false; //It's not a correct shop sign, so don't cancel it
|
||||
if (playerIsNotOwner(player, sign)) return !isAdmin(player) && (!BlockUtil.isSign(block) || !Config.getBoolean(TURN_OFF_SIGN_PROTECTION)); //If the player isn't the owner or an admin - cancel!
|
||||
|
||||
if (weShouldReturnMoney() && !Permission.has(player, Permission.NOFEE)) {
|
||||
float refundPrice = Config.getFloat(SHOP_REFUND_PRICE);
|
||||
Economy.add(uName.getName(sign.getLine(0)), refundPrice); //Add some money
|
||||
player.sendMessage(Config.getLocal(Language.SHOP_REFUNDED).replace("%amount", Economy.formatBalance(refundPrice)));
|
||||
}
|
||||
|
||||
return false; //Player is the owner, so we don't want to cancel this :)
|
||||
}
|
||||
|
||||
private static boolean isAdmin(Player p) {
|
||||
return p != null && (Permission.has(p, Permission.ADMIN) || Permission.has(p, Permission.MOD));
|
||||
}
|
||||
|
||||
private static boolean weShouldReturnMoney() {
|
||||
return Config.getFloat(SHOP_REFUND_PRICE) != 0;
|
||||
}
|
||||
|
||||
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
|
||||
public static void onBlockBreak(BlockBreakEvent event) {
|
||||
if (cancellingBlockBreak(event.getBlock(), event.getPlayer())) event.setCancelled(true);
|
||||
}
|
||||
|
||||
private static boolean isCorrectSign(Sign sign, Block block) {
|
||||
return sign != null && (sign.getBlock().equals(block) || uBlock.getAttachedFace(sign).equals(block));
|
||||
}
|
||||
|
||||
private static boolean playerIsNotOwner(Player player, Sign sign) {
|
||||
return player == null || (!uName.stripName(player.getName()).equals(sign.getLine(0))
|
||||
&& !Permission.otherName(player, sign.getLine(0)));
|
||||
}
|
||||
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public static void onBlockPistonExtend(BlockPistonExtendEvent event) {
|
||||
for (Block b : getExtendBlocks(event)) {
|
||||
if (cancellingBlockBreak(b, null)) {
|
||||
event.setCancelled(true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public static void onBlockPistonRetract(BlockPistonRetractEvent event) {
|
||||
if (cancellingBlockBreak(getRetractBlock(event), null)) event.setCancelled(true);
|
||||
}
|
||||
|
||||
private static Block getRetractBlock(BlockPistonRetractEvent event) {
|
||||
Block block = getRetractLocationBlock(event);
|
||||
return (block != null && !BlockUtil.isSign(block) ? block : null);
|
||||
}
|
||||
|
||||
//Those are fixes for CraftBukkit's piston bug, where piston appears not to be a piston.
|
||||
private static BlockFace getPistonDirection(Block block) {
|
||||
return block.getState().getData() instanceof PistonBaseMaterial ? ((Directional) block.getState().getData()).getFacing() : null;
|
||||
}
|
||||
|
||||
private static Block getRetractLocationBlock(BlockPistonRetractEvent event) {
|
||||
BlockFace pistonDirection = getPistonDirection(event.getBlock());
|
||||
return pistonDirection != null ? event.getBlock().getRelative((pistonDirection), 2).getLocation().getBlock() : null;
|
||||
}
|
||||
|
||||
private static List<Block> getExtendBlocks(BlockPistonExtendEvent event) {
|
||||
BlockFace pistonDirection = getPistonDirection(event.getBlock());
|
||||
|
||||
if (pistonDirection == null) {
|
||||
return new ArrayList<Block>();
|
||||
}
|
||||
|
||||
Block piston = event.getBlock();
|
||||
List<Block> pushedBlocks = new ArrayList<Block>();
|
||||
|
||||
for (int currentBlock = 1; currentBlock < event.getLength() + 1; currentBlock++) {
|
||||
Block block = piston.getRelative(pistonDirection, currentBlock);
|
||||
Material blockType = block.getType();
|
||||
|
||||
if (blockType == Material.AIR) {
|
||||
break;
|
||||
}
|
||||
|
||||
pushedBlocks.add(block);
|
||||
}
|
||||
|
||||
return pushedBlocks;
|
||||
}
|
||||
}
|
@ -1,13 +1,16 @@
|
||||
package com.Acrobot.ChestShop.Listeners.Block.Break;
|
||||
|
||||
import com.Acrobot.ChestShop.Config.Config;
|
||||
import com.Acrobot.ChestShop.Config.Property;
|
||||
import com.Acrobot.ChestShop.Listeners.Player.PlayerInteract;
|
||||
import com.Acrobot.ChestShop.Plugins.ChestShop;
|
||||
import com.Acrobot.ChestShop.Signs.ChestShopSign;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.block.BlockBreakEvent;
|
||||
import org.bukkit.event.entity.EntityExplodeEvent;
|
||||
|
||||
import static com.Acrobot.ChestShop.Config.Property.USE_BUILT_IN_PROTECTION;
|
||||
import static org.bukkit.Material.CHEST;
|
||||
@ -18,15 +21,30 @@ import static org.bukkit.Material.CHEST;
|
||||
public class ChestBreak implements Listener {
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public static void onChestBreak(BlockBreakEvent event) {
|
||||
if (event.getBlock().getType() != CHEST || !Config.getBoolean(USE_BUILT_IN_PROTECTION)) {
|
||||
return;
|
||||
}
|
||||
|
||||
Player player = event.getPlayer();
|
||||
Block block = event.getBlock();
|
||||
|
||||
if (!PlayerInteract.canOpenOtherShops(player) && !ChestShop.canAccess(player, block)) {
|
||||
if (!canChestBeBroken(event.getBlock(), event.getPlayer())) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public static void onExplosion(EntityExplodeEvent event) {
|
||||
if (event.blockList() == null || !Config.getBoolean(Property.USE_BUILT_IN_PROTECTION)) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (Block block : event.blockList()) {
|
||||
if (!canChestBeBroken(block, null)) {
|
||||
event.setCancelled(true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean canChestBeBroken(Block chest, Player breaker) {
|
||||
if (chest.getType() != CHEST || !Config.getBoolean(USE_BUILT_IN_PROTECTION) || ChestShopSign.isShopChest(chest)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return breaker != null && (PlayerInteract.canOpenOtherShops(breaker) || ChestShop.canAccess(breaker, chest));
|
||||
}
|
||||
}
|
||||
|
200
com/Acrobot/ChestShop/Listeners/Block/Break/SignBreak.java
Normal file
200
com/Acrobot/ChestShop/Listeners/Block/Break/SignBreak.java
Normal file
@ -0,0 +1,200 @@
|
||||
package com.Acrobot.ChestShop.Listeners.Block.Break;
|
||||
|
||||
import com.Acrobot.Breeze.Utils.BlockUtil;
|
||||
import com.Acrobot.ChestShop.Config.Config;
|
||||
import com.Acrobot.ChestShop.Config.Property;
|
||||
import com.Acrobot.ChestShop.Events.ShopDestroyedEvent;
|
||||
import com.Acrobot.ChestShop.Permission;
|
||||
import com.Acrobot.ChestShop.Signs.ChestShopSign;
|
||||
import com.Acrobot.ChestShop.Utils.uBlock;
|
||||
import com.Acrobot.ChestShop.Utils.uName;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.BlockFace;
|
||||
import org.bukkit.block.Chest;
|
||||
import org.bukkit.block.Sign;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.block.BlockBreakEvent;
|
||||
import org.bukkit.event.block.BlockPistonExtendEvent;
|
||||
import org.bukkit.event.block.BlockPistonRetractEvent;
|
||||
import org.bukkit.event.entity.EntityExplodeEvent;
|
||||
import org.bukkit.material.Directional;
|
||||
import org.bukkit.material.PistonBaseMaterial;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import static com.Acrobot.Breeze.Utils.BlockUtil.getAttachedFace;
|
||||
import static com.Acrobot.Breeze.Utils.BlockUtil.isSign;
|
||||
import static com.Acrobot.ChestShop.Config.Property.TURN_OFF_SIGN_PROTECTION;
|
||||
import static com.Acrobot.ChestShop.Permission.ADMIN;
|
||||
import static com.Acrobot.ChestShop.Permission.MOD;
|
||||
import static com.Acrobot.ChestShop.Signs.ChestShopSign.NAME_LINE;
|
||||
|
||||
/**
|
||||
* @author Acrobot
|
||||
*/
|
||||
public class SignBreak implements Listener {
|
||||
private static final BlockFace[] SIGN_CONNECTION_FACES = {BlockFace.SOUTH, BlockFace.NORTH, BlockFace.EAST, BlockFace.WEST};
|
||||
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public static void onSignBreak(BlockBreakEvent event) {
|
||||
if (!canBlockBeBroken(event.getBlock(), event.getPlayer())) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public static void onBlockPistonExtend(BlockPistonExtendEvent event) {
|
||||
for (Block block : getExtendBlocks(event)) {
|
||||
if (!canBlockBeBroken(block, null)) {
|
||||
event.setCancelled(true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public static void onBlockPistonRetract(BlockPistonRetractEvent event) {
|
||||
if (!canBlockBeBroken(getRetractBlock(event), null)) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public static void onExplosion(EntityExplodeEvent event) {
|
||||
if (event.blockList() == null || !Config.getBoolean(Property.USE_BUILT_IN_PROTECTION)) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (Block block : event.blockList()) {
|
||||
if (!canBlockBeBroken(block, null)) {
|
||||
event.setCancelled(true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean canBlockBeBroken(Block block, Player breaker) {
|
||||
List<Sign> attachedSigns = getAttachedSigns(block);
|
||||
List<Sign> brokenBlocks = new LinkedList<Sign>();
|
||||
|
||||
boolean canBeBroken = true;
|
||||
|
||||
for (Sign sign : attachedSigns) {
|
||||
sign.update();
|
||||
|
||||
if (!canBeBroken || !ChestShopSign.isValid(sign)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (Config.getBoolean(TURN_OFF_SIGN_PROTECTION) || canDestroyShop(breaker, sign.getLine(NAME_LINE))) {
|
||||
brokenBlocks.add(sign);
|
||||
} else {
|
||||
canBeBroken = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (!canBeBroken) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (Sign sign : brokenBlocks) {
|
||||
sendShopDestroyedEvent(sign, breaker);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static boolean canDestroyShop(Player player, String name) {
|
||||
return player == null || hasShopBreakingPermission(player) || canUseName(player, name);
|
||||
}
|
||||
|
||||
private static boolean canUseName(Player player, String name) {
|
||||
return uName.canUseName(player, name);
|
||||
}
|
||||
|
||||
private static boolean hasShopBreakingPermission(Player player) {
|
||||
return Permission.has(player, ADMIN) || Permission.has(player, MOD);
|
||||
}
|
||||
|
||||
private static void sendShopDestroyedEvent(Sign sign, Player player) {
|
||||
Chest connectedChest = null;
|
||||
|
||||
if (!ChestShopSign.isAdminShop(sign)) {
|
||||
connectedChest = uBlock.findConnectedChest(sign.getBlock());
|
||||
}
|
||||
|
||||
Event event = new ShopDestroyedEvent(player, sign, connectedChest);
|
||||
Bukkit.getPluginManager().callEvent(event);
|
||||
}
|
||||
|
||||
private static List<Sign> getAttachedSigns(Block block) {
|
||||
if (isSign(block)) {
|
||||
return Arrays.asList((Sign) block.getState());
|
||||
} else {
|
||||
List<Sign> attachedSigns = new LinkedList<Sign>();
|
||||
|
||||
for (BlockFace face : SIGN_CONNECTION_FACES) {
|
||||
Block relative = block.getRelative(face);
|
||||
|
||||
if (!isSign(relative)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Sign sign = (Sign) relative.getState();
|
||||
|
||||
if (getAttachedFace(sign).equals(block)) {
|
||||
attachedSigns.add(sign);
|
||||
}
|
||||
}
|
||||
|
||||
return attachedSigns;
|
||||
}
|
||||
}
|
||||
|
||||
private static Block getRetractBlock(BlockPistonRetractEvent event) {
|
||||
Block block = getRetractLocationBlock(event);
|
||||
return (block != null && !BlockUtil.isSign(block) ? block : null);
|
||||
}
|
||||
|
||||
//Those are fixes for CraftBukkit's piston bug, where piston appears not to be a piston.
|
||||
private static BlockFace getPistonDirection(Block block) {
|
||||
return block.getState().getData() instanceof PistonBaseMaterial ? ((Directional) block.getState().getData()).getFacing() : null;
|
||||
}
|
||||
|
||||
private static Block getRetractLocationBlock(BlockPistonRetractEvent event) {
|
||||
BlockFace pistonDirection = getPistonDirection(event.getBlock());
|
||||
return pistonDirection != null ? event.getBlock().getRelative((pistonDirection), 2).getLocation().getBlock() : null;
|
||||
}
|
||||
|
||||
private static List<Block> getExtendBlocks(BlockPistonExtendEvent event) {
|
||||
BlockFace pistonDirection = getPistonDirection(event.getBlock());
|
||||
|
||||
if (pistonDirection == null) {
|
||||
return new ArrayList<Block>();
|
||||
}
|
||||
|
||||
Block piston = event.getBlock();
|
||||
List<Block> pushedBlocks = new ArrayList<Block>();
|
||||
|
||||
for (int currentBlock = 1; currentBlock < event.getLength() + 1; currentBlock++) {
|
||||
Block block = piston.getRelative(pistonDirection, currentBlock);
|
||||
Material blockType = block.getType();
|
||||
|
||||
if (blockType == Material.AIR) {
|
||||
break;
|
||||
}
|
||||
|
||||
pushedBlocks.add(block);
|
||||
}
|
||||
|
||||
return pushedBlocks;
|
||||
}
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
package com.Acrobot.ChestShop.Listeners.Block;
|
||||
|
||||
import com.Acrobot.ChestShop.Config.Config;
|
||||
import com.Acrobot.ChestShop.Config.Property;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.entity.EntityExplodeEvent;
|
||||
|
||||
/**
|
||||
* @author Acrobot
|
||||
*/
|
||||
public class EntityExplode implements Listener {
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public static void onEntityExplode(EntityExplodeEvent event) {
|
||||
if (event.blockList() == null || !Config.getBoolean(Property.USE_BUILT_IN_PROTECTION)) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (Block block : event.blockList()) {
|
||||
if (BlockBreak.cancellingBlockBreak(block, null)) {
|
||||
event.setCancelled(true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -202,6 +202,8 @@ public class SignChange implements Listener {
|
||||
line = line.replace(" ", "");
|
||||
}
|
||||
|
||||
line = line.replace('b', 'B').replace('s', 'S');
|
||||
|
||||
return (line.length() > 15 ? null : line);
|
||||
}
|
||||
|
||||
|
@ -115,6 +115,10 @@ public class PlayerInteract implements Listener {
|
||||
|
||||
int amount = Integer.parseInt(sign.getLine(QUANTITY_LINE));
|
||||
|
||||
if (amount < 1) {
|
||||
amount = 1;
|
||||
}
|
||||
|
||||
if (Config.getBoolean(SHIFT_SELLS_EVERYTHING) && player.isSneaking() && price != PriceUtil.NO_PRICE) {
|
||||
int newAmount = getItemAmount(item, ownerInventory, player, action);
|
||||
if (newAmount > 0) {
|
||||
@ -132,20 +136,13 @@ public class PlayerInteract implements Listener {
|
||||
}
|
||||
|
||||
private static int getItemAmount(ItemStack item, Inventory inventory, Player player, Action action) {
|
||||
int amount;
|
||||
Action buy = Config.getBoolean(REVERSE_BUTTONS) ? LEFT_CLICK_BLOCK : RIGHT_CLICK_BLOCK;
|
||||
|
||||
if (action == buy) {
|
||||
if (inventory instanceof AdminInventory) {
|
||||
amount = Integer.MAX_VALUE;
|
||||
} else {
|
||||
amount = InventoryUtil.getAmount(item, inventory);
|
||||
}
|
||||
return InventoryUtil.getAmount(item, inventory);
|
||||
} else {
|
||||
amount = InventoryUtil.getAmount(item, player.getInventory());
|
||||
return InventoryUtil.getAmount(item, player.getInventory());
|
||||
}
|
||||
|
||||
return amount;
|
||||
}
|
||||
|
||||
public static boolean canOpenOtherShops(Player player) {
|
||||
|
@ -36,7 +36,7 @@ public class EmptyShopDeleter implements Listener {
|
||||
|
||||
sign.getBlock().setType(Material.AIR);
|
||||
|
||||
if (Config.getBoolean(REMOVE_EMPTY_CHESTS) && !ChestShopSign.isAdminShop(ownerInventory) && chestIsEmpty(ownerInventory)) {
|
||||
if (Config.getBoolean(REMOVE_EMPTY_CHESTS) && !ChestShopSign.isAdminShop(ownerInventory) && InventoryUtil.isEmpty(ownerInventory)) {
|
||||
Chest connectedChest = uBlock.findConnectedChest(sign);
|
||||
connectedChest.getBlock().setType(Material.AIR);
|
||||
} else {
|
||||
@ -44,27 +44,7 @@ public class EmptyShopDeleter implements Listener {
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean chestIsEmpty(Inventory inventory) {
|
||||
for (ItemStack item : inventory.getContents()) {
|
||||
if (item != null) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static boolean shopShouldBeRemoved(Inventory inventory, ItemStack[] stock) {
|
||||
return Config.getBoolean(REMOVE_EMPTY_SHOPS) && !ChestShopSign.isAdminShop(inventory) && !hasMoreStock(inventory, stock);
|
||||
}
|
||||
|
||||
private static boolean hasMoreStock(Inventory inventory, ItemStack[] stock) {
|
||||
for (ItemStack stack : stock) {
|
||||
if (InventoryUtil.getAmount(stack, inventory) > 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
return Config.getBoolean(REMOVE_EMPTY_SHOPS) && !ChestShopSign.isAdminShop(inventory) && !InventoryUtil.hasItems(stock, inventory);
|
||||
}
|
||||
}
|
||||
|
@ -30,7 +30,7 @@ public class AmountAndPriceChecker implements Listener {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!hasItems(ownerInventory, stock)) {
|
||||
if (!InventoryUtil.hasItems(stock, ownerInventory)) {
|
||||
event.setCancelled(NOT_ENOUGH_STOCK_IN_CHEST);
|
||||
}
|
||||
}
|
||||
@ -49,18 +49,8 @@ public class AmountAndPriceChecker implements Listener {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!hasItems(clientInventory, stock)) {
|
||||
if (!InventoryUtil.hasItems(stock, clientInventory)) {
|
||||
event.setCancelled(NOT_ENOUGH_STOCK_IN_INVENTORY);
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean hasItems(Inventory inventory, ItemStack[] items) {
|
||||
for (ItemStack item : items) {
|
||||
if (InventoryUtil.getAmount(item, inventory) < item.getAmount()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -51,7 +51,7 @@ public class DiscountModule implements Listener {
|
||||
return;
|
||||
}
|
||||
|
||||
if (PriceUtil.getSellPrice(event.getSign().getLine(PRICE_LINE)) != NO_PRICE) {
|
||||
if (PriceUtil.getBuyPrice(event.getSign().getLine(PRICE_LINE)) == NO_PRICE) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -50,7 +50,7 @@ public class PartialTransactionModule implements Listener {
|
||||
|
||||
stock = event.getStock();
|
||||
|
||||
if (!hasItems(event.getOwnerInventory(), stock)) {
|
||||
if (!InventoryUtil.hasItems(stock, event.getOwnerInventory())) {
|
||||
ItemStack[] itemsHad = getItems(stock, event.getOwnerInventory());
|
||||
int posessedItemCount = getItemCount(itemsHad);
|
||||
|
||||
@ -91,7 +91,7 @@ public class PartialTransactionModule implements Listener {
|
||||
|
||||
stock = event.getStock();
|
||||
|
||||
if (!hasItems(event.getClientInventory(), stock)) {
|
||||
if (!InventoryUtil.hasItems(stock, event.getClientInventory())) {
|
||||
ItemStack[] itemsHad = getItems(stock, event.getClientInventory());
|
||||
int posessedItemCount = getItemCount(itemsHad);
|
||||
|
||||
@ -105,16 +105,6 @@ public class PartialTransactionModule implements Listener {
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean hasItems(Inventory inventory, ItemStack[] items) {
|
||||
for (ItemStack item : items) {
|
||||
if (!inventory.contains(item, item.getAmount())) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static int getAmountOfAffordableItems(double walletMoney, double pricePerItem) {
|
||||
return (int) Math.floor(walletMoney / pricePerItem);
|
||||
}
|
||||
|
33
com/Acrobot/ChestShop/Listeners/ShopRefundListener.java
Normal file
33
com/Acrobot/ChestShop/Listeners/ShopRefundListener.java
Normal file
@ -0,0 +1,33 @@
|
||||
package com.Acrobot.ChestShop.Listeners;
|
||||
|
||||
import com.Acrobot.ChestShop.Config.Config;
|
||||
import com.Acrobot.ChestShop.Config.Language;
|
||||
import com.Acrobot.ChestShop.Economy.Economy;
|
||||
import com.Acrobot.ChestShop.Events.ShopDestroyedEvent;
|
||||
import com.Acrobot.ChestShop.Permission;
|
||||
import com.Acrobot.ChestShop.Signs.ChestShopSign;
|
||||
import com.Acrobot.ChestShop.Utils.uName;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
import static com.Acrobot.ChestShop.Config.Property.SHOP_REFUND_PRICE;
|
||||
import static com.Acrobot.ChestShop.Permission.NOFEE;
|
||||
|
||||
/**
|
||||
* @author Acrobot
|
||||
*/
|
||||
public class ShopRefundListener implements Listener {
|
||||
@EventHandler
|
||||
public static void onShopDestroy(ShopDestroyedEvent event) {
|
||||
float refundPrice = Config.getFloat(SHOP_REFUND_PRICE);
|
||||
|
||||
if (event.getDestroyer() == null || Permission.has(event.getDestroyer(), NOFEE) || refundPrice == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
String ownerName = uName.getName(event.getSign().getLine(ChestShopSign.NAME_LINE));
|
||||
Economy.add(ownerName, refundPrice);
|
||||
|
||||
event.getDestroyer().sendMessage(Config.getLocal(Language.SHOP_REFUNDED).replace("%amount", Economy.formatBalance(refundPrice)));
|
||||
}
|
||||
}
|
@ -3,8 +3,11 @@ package com.Acrobot.ChestShop.Signs;
|
||||
import com.Acrobot.Breeze.Utils.BlockUtil;
|
||||
import com.Acrobot.ChestShop.Config.Config;
|
||||
import com.Acrobot.ChestShop.Containers.AdminInventory;
|
||||
import com.Acrobot.ChestShop.Utils.uBlock;
|
||||
import com.Acrobot.ChestShop.Utils.uName;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.Chest;
|
||||
import org.bukkit.block.Sign;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
@ -24,7 +27,7 @@ public class ChestShopSign {
|
||||
|
||||
public static final Pattern[] SHOP_SIGN_PATTERN = {
|
||||
Pattern.compile("^[\\w -]*$"),
|
||||
Pattern.compile("^[0-9]+$"),
|
||||
Pattern.compile("^[1-9][0-9]*$"),
|
||||
Pattern.compile("(?i)^[\\d.bs(free) :]+$"),
|
||||
Pattern.compile("^[\\w : -]+$")
|
||||
};
|
||||
@ -53,6 +56,14 @@ public class ChestShopSign {
|
||||
return BlockUtil.isSign(sign) && isValid((Sign) sign.getState());
|
||||
}
|
||||
|
||||
public static boolean isShopChest(Block chest) {
|
||||
if (chest.getType() != Material.CHEST) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return uBlock.getConnectedSign((Chest) chest.getState()) != null;
|
||||
}
|
||||
|
||||
public static boolean canAccess(Player player, Sign sign) {
|
||||
if (player == null) return false;
|
||||
if (sign == null) return true;
|
||||
|
@ -18,6 +18,7 @@ import org.bukkit.event.block.SignChangeEvent;
|
||||
import static com.Acrobot.ChestShop.Config.Language.ACCESS_DENIED;
|
||||
import static com.Acrobot.ChestShop.Config.Language.RESTRICTED_SIGN_CREATED;
|
||||
import static com.Acrobot.ChestShop.Events.PreTransactionEvent.TransactionOutcome.SHOP_IS_RESTRICTED;
|
||||
import static com.Acrobot.ChestShop.Permission.ADMIN;
|
||||
|
||||
/**
|
||||
* @author Acrobot
|
||||
@ -52,14 +53,14 @@ public class RestrictedSign implements Listener {
|
||||
}
|
||||
Block connectedSign = event.getBlock().getRelative(BlockFace.DOWN);
|
||||
|
||||
if (!Permission.has(player, Permission.ADMIN) || !BlockUtil.isSign(connectedSign) || !ChestShopSign.isValid(connectedSign)) {
|
||||
if (!Permission.has(player, ADMIN) || !ChestShopSign.isValid(connectedSign)) {
|
||||
dropSignAndCancelEvent(event);
|
||||
return;
|
||||
}
|
||||
|
||||
Sign sign = (Sign) connectedSign.getState();
|
||||
|
||||
if (!ChestShopSign.isValid(sign) || !ChestShopSign.canAccess(player, sign)) {
|
||||
if (!ChestShopSign.canAccess(player, sign) && !Permission.has(player, ADMIN)) {
|
||||
dropSignAndCancelEvent(event);
|
||||
}
|
||||
|
||||
@ -84,7 +85,13 @@ public class RestrictedSign implements Listener {
|
||||
Block currentBlock = location.getBlock();
|
||||
|
||||
if (BlockUtil.isSign(currentBlock)) {
|
||||
return (Sign) currentBlock.getState();
|
||||
Sign sign = (Sign) currentBlock.getState();
|
||||
|
||||
if (isRestricted(sign)) {
|
||||
return sign;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
for (BlockFace face : SIGN_CONNECTION_FACES) {
|
||||
@ -138,7 +145,7 @@ public class RestrictedSign implements Listener {
|
||||
}
|
||||
|
||||
public static boolean hasPermission(Player p, String[] lines) {
|
||||
if (Permission.has(p, Permission.ADMIN)) {
|
||||
if (Permission.has(p, ADMIN)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -2,7 +2,7 @@ name: ChestShop
|
||||
|
||||
main: com.Acrobot.ChestShop.ChestShop
|
||||
|
||||
version: 3.50t0013
|
||||
version: 3.50t0022
|
||||
|
||||
#for CButD
|
||||
dev-url: http://dev.bukkit.org/server-mods/chestshop/
|
||||
|
Loading…
Reference in New Issue
Block a user