- Fixed double chests

- Now, the default config is in propper English :)
- Added Permissions to buying and selling
- Disallowed for diagonal chest linking
- Speeded up checking for amount of items in inventory
This commit is contained in:
Acrobot 2011-06-23 23:25:34 +02:00
parent 51296f6ac9
commit 36f25841af
14 changed files with 160 additions and 50 deletions

View File

@ -1,7 +1,7 @@
package com.Acrobot.ChestShop.Chests;
import com.Acrobot.ChestShop.Utils.InventoryUtil;
import com.Acrobot.ChestShop.Utils.SearchForBlock;
import com.Acrobot.ChestShop.Utils.BlockSearch;
import org.bukkit.block.Chest;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
@ -50,14 +50,14 @@ public class MinecraftChest implements ChestObject {
public void addItem(ItemStack item, int amount) {
int left = addItem(item, amount, main);
if (neighbor != null) {
if (neighbor != null && left > 0) {
addItem(item, left, neighbor);
}
}
public void removeItem(ItemStack item, short durability, int amount) {
int left = removeItem(item, durability, amount, main);
if (neighbor != null) {
if (neighbor != null && left > 0) {
removeItem(item, durability, left, neighbor);
}
}
@ -80,7 +80,7 @@ public class MinecraftChest implements ChestObject {
}
private Chest getNeighbor() {
return SearchForBlock.findNeighbor(main);
return BlockSearch.findNeighbor(main);
}
private static int amount(ItemStack item, short durability, Chest chest) {

View File

@ -13,7 +13,7 @@ public enum Language {
ACCESS_DENIED("You don't have permission to do that!"),
NOT_ENOUGH_MONEY("You have got not enough money!"),
NOT_ENOUGH_MONEY("You don't have enough money!"),
NOT_ENOUGH_MONEY_SHOP("Shop owner doesn't have enough money!"),
NO_BUYING_HERE("You can't buy here!"),
@ -21,9 +21,9 @@ public enum Language {
NOT_ENOUGH_SPACE_IN_INVENTORY("You haven't got enough space in inventory!"),
NOT_ENOUGH_SPACE_IN_CHEST("There isn't enough space in chest!"),
NOT_ENOUGH_ITEMS_TO_SELL("You have got not enough items to sell!"),
NOT_ENOUGH_ITEMS_TO_SELL("You don't have enough items to sell!"),
NOT_ENOUGH_STOCK("This shop has not enough stock."),
NOT_ENOUGH_STOCK("This shop is out of stock."),
NOT_ENOUGH_STOCK_IN_YOUR_SHOP("Your %material shop is out of stock!"),
YOU_BOUGHT_FROM_SHOP("You bought %amount %item from %owner for %price."),
@ -40,6 +40,8 @@ public enum Language {
PROTECTED_SHOP("Successfully protected the shop with LWC!"),
SHOP_CREATED("Shop successfully created!"),
NO_PERMISSION("You don't have permissions to do that!"),
NAME_TOO_LONG ("Unfortunately, your name is too long :( Please wait for newer shop version!"),
INCORRECT_ITEM_ID("You have specified invalid item id!");

View File

@ -1,7 +1,9 @@
package com.Acrobot.ChestShop.Listeners;
import com.Acrobot.ChestShop.Permission;
import com.Acrobot.ChestShop.Utils.SearchForBlock;
import com.Acrobot.ChestShop.Restrictions.RestrictedSign;
import com.Acrobot.ChestShop.Utils.BlockSearch;
import com.Acrobot.ChestShop.Utils.SignUtil;
import org.bukkit.block.Block;
import org.bukkit.block.Sign;
import org.bukkit.entity.Player;
@ -22,7 +24,15 @@ public class blockBreak extends BlockListener {
return;
}
Sign sign = SearchForBlock.findSign(block);
if(SignUtil.isSign(block)){
Sign currentSign = (Sign) block.getState();
if(RestrictedSign.isRestricted(currentSign)){
event.setCancelled(true);
}
currentSign.update(true);
}
Sign sign = BlockSearch.findSign(block);
if (sign != null) {
if (!player.getName().equals(sign.getLine(0))) {

View File

@ -5,8 +5,9 @@ import com.Acrobot.ChestShop.Config.Language;
import com.Acrobot.ChestShop.Config.Property;
import com.Acrobot.ChestShop.Permission;
import com.Acrobot.ChestShop.Protection.Default;
import com.Acrobot.ChestShop.Restrictions.RestrictedSign;
import com.Acrobot.ChestShop.Shop.ShopManagement;
import com.Acrobot.ChestShop.Utils.SearchForBlock;
import com.Acrobot.ChestShop.Utils.BlockSearch;
import com.Acrobot.ChestShop.Utils.SignUtil;
import net.minecraft.server.IInventory;
import net.minecraft.server.InventoryLargeChest;
@ -70,7 +71,7 @@ public class playerInteract extends PlayerListener {
}
if (player.getName().equals(sign.getLine(0))) {
Chest chest1 = SearchForBlock.findChest(sign);
Chest chest1 = BlockSearch.findChest(sign);
if (chest1 == null) {
player.sendMessage(Config.getLocal(Language.NO_CHEST_DETECTED));
return;
@ -79,7 +80,7 @@ public class playerInteract extends PlayerListener {
Inventory inv1 = chest1.getInventory();
IInventory iInv1 = ((CraftInventory) inv1).getInventory();
Chest chest2 = SearchForBlock.findNeighbor(chest1);
Chest chest2 = BlockSearch.findNeighbor(chest1);
if (chest2 != null) {
Inventory inv2 = chest2.getInventory();
@ -92,6 +93,12 @@ public class playerInteract extends PlayerListener {
return;
}
if(RestrictedSign.isRestricted(sign)){
if(!RestrictedSign.canAccess(sign, player)){
player.sendMessage(Config.getLocal(Language.ACCESS_DENIED));
return;
}
}
Action buy = (Config.getBoolean(Property.REVERSE_BUTTONS) ? Action.LEFT_CLICK_BLOCK : Action.RIGHT_CLICK_BLOCK);

View File

@ -7,12 +7,15 @@ import com.Acrobot.ChestShop.Items.Items;
import com.Acrobot.ChestShop.Permission;
import com.Acrobot.ChestShop.Protection.Default;
import com.Acrobot.ChestShop.Protection.Security;
import com.Acrobot.ChestShop.Restrictions.RestrictedSign;
import com.Acrobot.ChestShop.Utils.Numerical;
import com.Acrobot.ChestShop.Utils.SearchForBlock;
import com.Acrobot.ChestShop.Utils.BlockSearch;
import com.Acrobot.ChestShop.Utils.SignUtil;
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.block.BlockListener;
import org.bukkit.event.block.SignChangeEvent;
@ -40,6 +43,11 @@ public class signChange extends BlockListener {
if (isAlmostReady) {
if(player.getName().length() > 15){
player.sendMessage(Config.getLocal(Language.NAME_TOO_LONG));
dropSign(event);
return;
}
if (mat == null) {
player.sendMessage(Config.getLocal(Language.INCORRECT_ITEM_ID));
dropSign(event);
@ -54,6 +62,17 @@ public class signChange extends BlockListener {
return;
}
} else {
if(RestrictedSign.isRestricted(event.getLines())){
if(!playerIsAdmin){
player.sendMessage(Config.getLocal(Language.ACCESS_DENIED));
dropSign(event);
return;
}
Block secondSign = signBlock.getFace(BlockFace.DOWN);
if(!SignUtil.isSign(secondSign) || !SignUtil.isValid((Sign) secondSign.getState())){
dropSign(event);
}
}
return;
}
@ -92,7 +111,7 @@ public class signChange extends BlockListener {
}
}
Chest chest = SearchForBlock.findChest(signBlock);
Chest chest = BlockSearch.findChest(signBlock);
if (!isAdminShop) {
if (chest == null) {

View File

@ -9,6 +9,8 @@ import org.bukkit.entity.Player;
public enum Permission {
SHOP_CREATION("ChestShop.shop.create"),
EXCLUDE_ITEM("ChestShop.shop.exclude"),
BUY("ChestShop.shop.buy"),
SELL("ChestShop.shop.sell"),
ADMIN("ChestShop.admin");
private final String permission;

View File

@ -1,6 +1,6 @@
package com.Acrobot.ChestShop.Protection;
import com.Acrobot.ChestShop.Utils.SearchForBlock;
import com.Acrobot.ChestShop.Utils.BlockSearch;
import com.Acrobot.ChestShop.Utils.SignUtil;
import org.bukkit.block.Block;
import org.bukkit.block.Chest;
@ -12,16 +12,30 @@ import org.bukkit.entity.Player;
*/
public class Default implements Protection {
public boolean isProtected(Block block) {
Sign sign = SearchForBlock.findSign(block);
Chest nChest = SearchForBlock.findChest(block);
return ((SignUtil.isSign(block) && SignUtil.isValid((Sign) block.getState())) || sign != null) || (nChest != null && SearchForBlock.findSign(nChest.getBlock()) != null);
if((SignUtil.isSign(block) && SignUtil.isValid((Sign) block.getState())) || BlockSearch.findSign(block) != null){
return true;
} else {
if(!(block.getState() instanceof Chest)){
return false;
}
if(BlockSearch.findSign(block) != null){
return true;
}
Chest neighbor = BlockSearch.findNeighbor(block);
if(neighbor != null && BlockSearch.findSign(neighbor.getBlock()) != null){
return true;
}
}
return false;
}
public boolean canAccess(Player player, Block block) {
Sign sign = SearchForBlock.findSign(block);
Chest nChest = SearchForBlock.findNeighbor(block);
Sign nSign = (nChest != null ? SearchForBlock.findSign(nChest.getBlock()) : null);
return ((SignUtil.isSign(block) && SignUtil.isValid((Sign) block.getState()) && ((Sign) block.getState()).getLine(0).equals(player.getName())) || (sign != null && sign.getLine(0).equals(player.getName()))) || (nSign != null && nSign.getLine(0).equals(player.getName()));
Sign sign = BlockSearch.findSign(block);
Chest nChest = BlockSearch.findNeighbor(block);
Sign nSign = (nChest != null ? BlockSearch.findSign(nChest.getBlock()) : null);
return ((SignUtil.isSign(block) && SignUtil.isValid((Sign) block.getState()) && ((Sign) block.getState()).getLine(0).equals(player.getName())) || (sign != null && sign.getLine(0).equals(player.getName())))
|| (nSign != null && nSign.getLine(0).equals(player.getName()));
}
public boolean protect(String name, Block block) {

View File

@ -1,6 +1,6 @@
package com.Acrobot.ChestShop.Protection;
import com.Acrobot.ChestShop.Utils.SearchForBlock;
import com.Acrobot.ChestShop.Utils.BlockSearch;
import org.bukkit.block.Block;
import org.bukkit.block.Chest;
import org.bukkit.block.Sign;
@ -25,9 +25,9 @@ public class Security {
}
public static boolean canPlaceSign(Player p, Block block) {
Chest chest = SearchForBlock.findChest(block);
Sign sign1 = SearchForBlock.findSign(chest.getBlock());
Sign sign2 = SearchForBlock.findSign(block);
Chest chest = BlockSearch.findChest(block);
Sign sign1 = BlockSearch.findSign(chest.getBlock());
Sign sign2 = BlockSearch.findSign(block);
return (sign1 == null || sign1.getLine(0).startsWith(p.getName())) && (sign2 == null || sign2.getLine(0).startsWith(p.getName()));
}

View File

@ -0,0 +1,39 @@
package com.Acrobot.ChestShop.Restrictions;
import com.Acrobot.ChestShop.Permission;
import com.Acrobot.ChestShop.Utils.SignUtil;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.Sign;
import org.bukkit.entity.Player;
/**
* @author Acrobot
*/
public class RestrictedSign {
public static boolean isRestricted(Sign sign){
Block blockUp = sign.getBlock().getFace(BlockFace.UP);
return SignUtil.isSign(blockUp) && isRestricted(((Sign) blockUp.getState()).getLines());
}
public static boolean isRestricted(String[] lines){
return lines[0].equalsIgnoreCase("[restricted]");
}
public static boolean canAccess(Sign sign, Player player){
Block blockUp = sign.getBlock().getFace(BlockFace.UP);
if(Permission.permissions == null || !SignUtil.isSign(blockUp) || Permission.has(player, Permission.ADMIN)){
return true;
}
String world = blockUp.getWorld().getName();
String playerName = player.getName();
sign = (Sign) blockUp.getState();
boolean result = false;
for(int i = 1; i <= 3; i++){
result = result || Permission.permissions.inGroup(world, playerName, sign.getLine(i));
}
return result;
}
}

View File

@ -7,6 +7,7 @@ import com.Acrobot.ChestShop.Config.Language;
import com.Acrobot.ChestShop.Config.Property;
import com.Acrobot.ChestShop.Economy;
import com.Acrobot.ChestShop.Logging.Logging;
import com.Acrobot.ChestShop.Permission;
import com.Acrobot.ChestShop.Utils.InventoryUtil;
import com.Acrobot.ChestShop.Utils.SignUtil;
import org.bukkit.block.Sign;
@ -44,6 +45,10 @@ public class Shop {
player.sendMessage(Config.getLocal(Language.NO_BUYING_HERE));
return false;
}
if(!Permission.has(player, Permission.BUY)){
player.sendMessage(Config.getLocal(Language.NO_PERMISSION));
return false;
}
String playerName = player.getName();
if (!Economy.hasEnough(playerName, buyPrice)) {
player.sendMessage(Config.getLocal(Language.NOT_ENOUGH_MONEY));
@ -99,6 +104,10 @@ public class Shop {
player.sendMessage(Config.getLocal(Language.NO_SELLING_HERE));
return false;
}
if(!Permission.has(player, Permission.SELL)){
player.sendMessage(Config.getLocal(Language.NO_PERMISSION));
return false;
}
String account = getOwnerAccount();
boolean accountExists = !account.isEmpty() && Economy.hasAccount(account);

View File

@ -2,7 +2,7 @@ package com.Acrobot.ChestShop.Shop;
import com.Acrobot.ChestShop.Chests.MinecraftChest;
import com.Acrobot.ChestShop.Items.Items;
import com.Acrobot.ChestShop.Utils.SearchForBlock;
import com.Acrobot.ChestShop.Utils.BlockSearch;
import org.bukkit.ChatColor;
import org.bukkit.block.Chest;
import org.bukkit.block.Sign;
@ -14,7 +14,7 @@ import org.bukkit.inventory.ItemStack;
*/
public class ShopManagement {
public static boolean buy(Sign sign, Player player) {
Chest chestMc = SearchForBlock.findChest(sign);
Chest chestMc = BlockSearch.findChest(sign);
ItemStack item = Items.getItemStack(sign.getLine(3));
if(item == null){
player.sendMessage(ChatColor.RED + "[Shop] The item is not recognised!");
@ -26,7 +26,7 @@ public class ShopManagement {
}
public static boolean sell(Sign sign, Player player) {
Chest chestMc = SearchForBlock.findChest(sign);
Chest chestMc = BlockSearch.findChest(sign);
ItemStack item = Items.getItemStack(sign.getLine(3));
if(item == null){
player.sendMessage(ChatColor.RED + "[Shop] The item is not recognised!");

View File

@ -9,7 +9,10 @@ import org.bukkit.block.Sign;
/**
* @author Acrobot
*/
public class SearchForBlock {
public class BlockSearch {
static BlockFace[] chestFaces = {BlockFace.EAST, BlockFace.NORTH, BlockFace.WEST, BlockFace.SOUTH};
static BlockFace[] shopFaces = {BlockFace.DOWN, BlockFace.UP, BlockFace.EAST, BlockFace.NORTH, BlockFace.WEST, BlockFace.SOUTH, BlockFace.SELF};
public static Chest findChest(Sign sign) {
Block block = sign.getBlock();
@ -17,7 +20,7 @@ public class SearchForBlock {
}
public static Chest findChest(Block block) {
for (BlockFace bf : BlockFace.values()) {
for (BlockFace bf : shopFaces) {
Block faceBlock = block.getFace(bf);
if (faceBlock.getType() == Material.CHEST) {
return (Chest) faceBlock.getState();
@ -27,7 +30,7 @@ public class SearchForBlock {
}
public static Sign findSign(Block block) {
for (BlockFace bf : BlockFace.values()) {
for (BlockFace bf : shopFaces) {
Block faceBlock = block.getFace(bf);
if (SignUtil.isSign(faceBlock)) {
Sign sign = (Sign) faceBlock.getState();
@ -40,8 +43,7 @@ public class SearchForBlock {
}
public static Chest findNeighbor(Block block) {
BlockFace[] bf = {BlockFace.EAST, BlockFace.NORTH, BlockFace.WEST, BlockFace.SOUTH};
for (BlockFace blockFace : bf) {
for (BlockFace blockFace : chestFaces) {
Block neighborBlock = block.getFace(blockFace);
if (neighborBlock.getType() == Material.CHEST) {
return (Chest) neighborBlock.getState();

View File

@ -5,6 +5,7 @@ import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
import java.util.HashMap;
/**
* @author Acrobot
@ -12,11 +13,16 @@ import java.util.ArrayList;
public class InventoryUtil {
public static int remove(Inventory inv, ItemStack item, int amount, short durability) {
amount = (amount > 0 ? amount : 1);
Material itemMaterial = item.getType();
int amountLeft = amount;
for (int slot = 0; slot < inv.getSize(); slot++) {
if (amountLeft <= 0) {
int first = inv.first(itemMaterial);
if(first == -1){
return amount;
}
for (int slot = first; slot < inv.getSize(); slot++) {
if (amount <= 0) {
return 0;
}
@ -27,31 +33,33 @@ public class InventoryUtil {
if (currentItem.getType() == itemMaterial && (durability == -1 || currentItem.getDurability() == durability)) {
int currentAmount = currentItem.getAmount();
if (amountLeft == currentAmount) {
if (amount == currentAmount) {
currentItem = null;
amountLeft = 0;
} else if (amountLeft < currentAmount) {
currentItem.setAmount(currentAmount - amountLeft);
amountLeft = 0;
amount = 0;
} else if (amount < currentAmount) {
currentItem.setAmount(currentAmount - amount);
amount = 0;
} else {
currentItem = null;
amountLeft -= currentAmount;
amount -= currentAmount;
}
inv.setItem(slot, currentItem);
}
}
return amountLeft;
return amount;
}
public static int add(Inventory inv, ItemStack item, int amount) {
amount = (amount > 0 ? amount : 1);
Material itemMaterial = item.getType();
int maxStackSize = itemMaterial.getMaxStackSize();
if (amount <= maxStackSize) {
item.setAmount(amount);
inv.addItem(item);
return 0;
HashMap<Integer, ItemStack> left = inv.addItem(item);
return (left.isEmpty() ? 0 : left.get(0).getAmount());
}
ArrayList<ItemStack> items = new ArrayList<ItemStack>();
@ -65,11 +73,9 @@ public class InventoryUtil {
items.add(item);
}
}
Object[] iArray = items.toArray();
amount = 0;
for (Object o : iArray) {
ItemStack itemToAdd = (ItemStack) o;
for (ItemStack itemToAdd : items) {
amount += (!inv.addItem(itemToAdd).isEmpty() ? itemToAdd.getAmount() : 0);
}

View File

@ -3,7 +3,7 @@ name: ChestShop
main: com.Acrobot.ChestShop.ChestShop
database: true
version: 3.00 BETA 4
version: 3.00 BETA 5
author: Acrobot