Changed "a bit"

This commit is contained in:
Acrobot 2011-05-29 13:25:25 +02:00
parent fc4909bb90
commit 444b09fe1a
39 changed files with 1044 additions and 316 deletions

View File

@ -1,20 +1,29 @@
package com.Acrobot.ChestShop;
import com.Acrobot.ChestShop.Items.Items;
import com.Acrobot.ChestShop.Commands.ItemInfo;
import com.Acrobot.ChestShop.Commands.Options;
import com.Acrobot.ChestShop.Commands.Version;
import com.Acrobot.ChestShop.DB.Queue;
import com.Acrobot.ChestShop.DB.Transaction;
import com.Acrobot.ChestShop.Listeners.*;
import com.Acrobot.ChestShop.Logging.FileWriterQueue;
import com.Acrobot.ChestShop.Logging.Logging;
import com.Acrobot.ChestShop.Utils.Config;
import com.Acrobot.ChestShop.Utils.Defaults;
import com.avaje.ebean.EbeanServer;
import org.bukkit.Server;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import javax.persistence.PersistenceException;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
* Main file of the plugin
*
* @author Acrobot
*/
public class ChestShop extends JavaPlugin {
@ -25,24 +34,42 @@ public class ChestShop extends JavaPlugin {
private final signChange signChange = new signChange();
private final playerInteract playerInteract = new playerInteract();
private PluginDescriptionFile desc;
private static PluginDescriptionFile desc;
private static Server server;
public void onEnable() {
PluginManager pm = getServer().getPluginManager();
//Register our events
pm.registerEvent(Event.Type.BLOCK_BREAK, blockBreak, Event.Priority.Normal, this);
pm.registerEvent(Event.Type.BLOCK_PLACE, blockPlace, Event.Priority.Normal, this);
pm.registerEvent(Event.Type.SIGN_CHANGE, signChange, Event.Priority.Normal, this);
pm.registerEvent(Event.Type.PLAYER_INTERACT, playerInteract, Event.Priority.Highest, this);
pm.registerEvent(Event.Type.PLUGIN_ENABLE, pluginEnable, Event.Priority.Monitor, this);
pm.registerEvent(Event.Type.PLAYER_INTERACT_ENTITY, playerInteract, Event.Priority.Monitor, this);
desc = this.getDescription();
server = getServer();
//Set up our config file!
Config.setUp();
Defaults.set();
//Now set up our database for storing transactions!
setupDBfile();
if(Config.getBoolean("useDB")){
setupDB();
getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Queue(), 200L, 200L);
}
//Now set up our logging to file!
if(Config.getBoolean("logToFile")){
getServer().getScheduler().scheduleAsyncRepeatingTask(this, new FileWriterQueue(), 201L, 201L);
}
//Register our commands!
getCommand("iteminfo").setExecutor(new ItemInfo());
getCommand("chestOptions").setExecutor(new Options());
getCommand("csVersion").setExecutor(new Version());
System.out.println('[' + desc.getName() + "] version " + desc.getVersion() + " initialized!");
}
@ -51,48 +78,49 @@ public class ChestShop extends JavaPlugin {
System.out.println('[' + desc.getName() + "] version " + desc.getVersion() + " shutting down!");
}
///////////////////// DATABASE STUFF ////////////////////////////////
private void setupDB() {
try {
getDatabase().find(Transaction.class).findRowCount();
} catch (PersistenceException pe) {
Logging.log("Installing database for " + getPluginName());
installDDL();
}
}
private static void setupDBfile() {
File file = new File("ebean.properties");
if(!file.exists()){
try{
file.createNewFile();
} catch (Exception e){
Logging.log("Failed to create ebean.properties file!");
}
}
}
@Override
public List<Class<?>> getDatabaseClasses() {
List<Class<?>> list = new ArrayList<Class<?>>();
list.add(Transaction.class);
return list;
}
///////////////////////////////////////////////////////////////////////////////
public static Server getBukkitServer() {
return server;
}
public boolean onCommand (CommandSender sender, Command cmd, String label, String[] args){
String commandName = cmd.getName().toLowerCase();
int argCount = args.length;
//iCSversion
if(commandName.equals("icsversion")){
sender.sendMessage("ChestShop's version is: " + desc.getVersion());
return true;
public static String getVersion() {
return desc.getVersion();
}
if(!(sender instanceof Player)){
return false;
}
Player p = (Player) sender;
//ItemInfo
if(commandName.equals("iteminfo")){
if(argCount == 0){
p.sendMessage(Items.getItemID(p.getItemInHand().getType().name()) + " " + Items.getItemName(p.getItemInHand()));
return true;
}
if(argCount == 1){
String itemName = Items.getItemID(Items.getItemName(args[0])) + " " + Items.getItemName(args[0]);
p.sendMessage(itemName);
return true;
}
public static String getPluginName() {
return desc.getName();
}
//Silly :)
if(commandName.equals("buy")){
p.sendMessage("Hey, there is no buy command! Just right click the sign!");
return true;
}
if(commandName.equals("sell")){
p.sendMessage("Hey, there is no sell command! Just left click the sign!");
return true;
}
return false;
public static EbeanServer getDB(){
return new ChestShop().getDatabase();
}
}

View File

@ -9,13 +9,17 @@ public interface ChestObject {
public ItemStack[] getContents();
public void setSlot(int slot, ItemStack item);
public void clearSlot(int slot);
public void addItem(ItemStack item, short durability, int amount);
public void removeItem(ItemStack item, short durability, int amount);
public int amount(ItemStack item, short durability);
public boolean hasEnough(ItemStack item, int amount, short durability);
public boolean fits(ItemStack item, int amount, short durability);
public int getSize();

View File

@ -1,9 +1,7 @@
package com.Acrobot.ChestShop.Chests;
import com.Acrobot.ChestShop.Utils.InventoryUtil;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import com.Acrobot.ChestShop.Utils.SearchForBlock;
import org.bukkit.block.Chest;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
@ -51,9 +49,9 @@ public class MinecraftChest implements ChestObject{
}
public void addItem(ItemStack item, short durability, int amount) {
int left = addItem(item, durability, amount, main);
int left = addItem(item, amount, main);
if (neighbor != null) {
addItem(item, durability, left, neighbor);
addItem(item, left, neighbor);
}
}
@ -82,15 +80,7 @@ public class MinecraftChest implements ChestObject{
}
private Chest getNeighbor() {
BlockFace[] bf = {BlockFace.EAST, BlockFace.NORTH, BlockFace.WEST, BlockFace.SOUTH};
Block chestBlock = main.getBlock();
for(BlockFace blockFace : bf){
Block neighborBlock = chestBlock.getFace(blockFace);
if(neighborBlock.getType() == Material.CHEST){
return (Chest) neighborBlock.getState();
}
}
return null; //Shame, we didn't find double chest :/
return SearchForBlock.findNeighbor(main);
}
private static int amount(ItemStack item, short durability, Chest chest) {
@ -102,7 +92,7 @@ public class MinecraftChest implements ChestObject{
return InventoryUtil.fits(inv, item, amount, durability);
}
private static int addItem(ItemStack item, short durability, int amount, Chest chest){
private static int addItem(ItemStack item, int amount, Chest chest) {
Inventory inv = chest.getInventory();
return InventoryUtil.add(inv, item, amount);
}

View File

@ -0,0 +1,48 @@
package com.Acrobot.ChestShop.Commands;
import com.Acrobot.ChestShop.Items.Items;
import com.Acrobot.ChestShop.Utils.Config;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
/**
* @author Acrobot
*/
public class ItemInfo implements CommandExecutor {
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
boolean isPlayer = (sender instanceof Player);
if (args.length == 0) {
if (!isPlayer) {
return false;
}
Player player = (Player) sender;
ItemStack itemInHand = player.getItemInHand();
if (itemInHand.getType() == Material.AIR) {
return false;
}
player.sendMessage(Config.getLocal("iteminfo"));
player.sendMessage(itemInHand.getTypeId() + ":" + itemInHand.getDurability() + " - " + itemInHand.getType().name());
return true;
} else {
ItemStack item = Items.getItemStack(args[0]);
if (item == null) {
return false;
}
sender.sendMessage(Config.getLocal("iteminfo"));
sender.sendMessage(item.getTypeId() + ":" + item.getDurability() + " - " + item.getType().name());
return true;
}
}
}

View File

@ -0,0 +1,128 @@
package com.Acrobot.ChestShop.Commands;
import com.Acrobot.ChestShop.Utils.Config;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.util.HashMap;
/**
* @author Acrobot
*/
public class Options implements CommandExecutor {
public boolean balance;
public boolean outOfStock;
public boolean someoneBought;
public Options() {
this.balance = true;
this.outOfStock = true;
this.someoneBought = true;
}
public static boolean exists(String name) {
name = name.toLowerCase();
return name.equals("balance") || name.equals("outofstock") || name.equals("someonebought");
}
public boolean getOption(String name) {
name = name.toLowerCase();
if (name.equals("balance")) {
return balance;
}
if (name.equals("outofstock")) {
return outOfStock;
}
if (name.equals("someonebought")) {
return someoneBought;
}
return false;
}
public boolean setOption(String name, boolean value) {
if (name.equals("balance")) {
balance = value;
return true;
}
if (name.equals("outofstock")) {
outOfStock = value;
return true;
}
if (name.equals("someonebought")) {
someoneBought = value;
return true;
}
return false;
}
public static HashMap<Player, Options> playerPreferences = new HashMap<Player, Options>();
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (!(sender instanceof Player)) {
return false;
}
Player player = (Player) sender;
if (!playerPreferences.containsKey(player)) {
playerPreferences.put(player, new Options());
}
if (args.length == 0) {
String[] options = optionList();
player.sendMessage(Config.getLocal("options"));
for (String s : options) {
player.sendMessage(s);
}
return true;
}
if (args.length == 1) {
Options options = playerPreferences.get(player);
Boolean exists = exists(args[0]);
if (!exists) {
return false;
}
player.sendMessage(Config.getColored("&a" + args[0] + " is set to: " + options.getOption(args[0])));
return true;
}
if (args.length == 2) {
try {
Boolean option = Boolean.parseBoolean(args[1]);
Options options = playerPreferences.get(player);
Boolean exists = exists(args[0]);
if (!exists) {
return false;
}
Boolean success = options.setOption(args[0], option);
if (!success) {
return false;
}
player.sendMessage(Config.getColored("&aSuccessfully set " + args[0] + " to " + args[1]));
return true;
} catch (Exception e) {
return false;
}
}
return false;
}
private static String[] optionList() {
return new String[]{
"balance - show current balance after transaction",
"outOfStock - show that your shop is out of stock",
"someoneBought - show that someone bought from your shop"
};
}
}

View File

@ -0,0 +1,16 @@
package com.Acrobot.ChestShop.Commands;
import com.Acrobot.ChestShop.ChestShop;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
/**
* @author Acrobot
*/
public class Version implements CommandExecutor {
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
sender.sendMessage(ChestShop.getPluginName() + "'s version is: " + ChestShop.getVersion());
return true;
}
}

View File

@ -0,0 +1,30 @@
package com.Acrobot.ChestShop.DB;
import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Utils.Config;
import java.util.LinkedList;
import java.util.List;
/**
* @author Acrobot
*/
public class Queue implements Runnable {
private static List<Transaction> queue = new LinkedList<Transaction>();
public static void addToQueue(Transaction t){
queue.add(t);
}
public static void saveQueue() {
ChestShop.getBukkitServer().getScheduler().scheduleAsyncDelayedTask(new ChestShop(), new Queue());
ChestShop.getBukkitServer().broadcastMessage("Successfully saved queue!");
}
public void run() {
List<Transaction> toDelete = ChestShop.getDB().find(Transaction.class).where().lt("sec", System.currentTimeMillis()/1000 - Config.getInteger("DBtimeToLive")).findList();
ChestShop.getDB().delete(toDelete);
ChestShop.getDB().save(queue);
queue.clear();
}
}

View File

@ -0,0 +1,101 @@
package com.Acrobot.ChestShop.DB;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
/**
* @author Acrobot
*/
@Entity()
@Table(name = "cs_transactions")
public class Transaction {
@Id
private int id;
private boolean buy;
private String shopOwner;
private String shopUser;
private int itemID;
private int itemDurability;
private int amount;
private float price;
private long sec;
public float getAveragePricePerItem() {
return price / amount;
}
public int getId() {
return id;
}
public boolean isBuy() {
return buy;
}
public String getShopOwner() {
return shopOwner;
}
public String getShopUser() {
return shopUser;
}
public int getItemID() {
return itemID;
}
public int getItemDurability() {
return itemDurability;
}
public int getAmount() {
return amount;
}
public float getPrice() {
return price;
}
public long getSec() {
return sec;
}
public void setId(int id) {
this.id = id;
}
public void setBuy(boolean buy) {
this.buy = buy;
}
public void setShopOwner(String shopOwner) {
this.shopOwner = shopOwner;
}
public void setShopUser(String shopUser) {
this.shopUser = shopUser;
}
public void setItemID(int itemID) {
this.itemID = itemID;
}
public void setItemDurability(int itemDurability) {
this.itemDurability = itemDurability;
}
public void setAmount(int amount) {
this.amount = amount;
}
public void setPrice(float price) {
this.price = price;
}
public void setSec(long sec) {
this.sec = sec;
}
}

View File

@ -20,6 +20,7 @@ public class Economy {
public static void substract(String name, float amount) {
economy.getAccount(name).subtract(amount);
}
public static boolean hasEnough(String name, float amount) {
return economy.getAccount(name).hasEnough(amount);
}

View File

@ -15,10 +15,10 @@ public class Items {
}
public static String getItemName(String itemName) {
return getMaterial(itemName).name();
return getMat(itemName).name();
}
public static Material getMaterial(String itemName){
public static Material getMat(String itemName) {
int length = 256;
Material finalMat = null;
itemName = itemName.toLowerCase().replace("_", "").replace(" ", "");
@ -33,7 +33,7 @@ public class Items {
}
public static int getItemID(String itemName) {
return getMaterial(itemName).getId();
return getMat(itemName).getId();
}
public static ItemStack getItemStack(String itemName) {
@ -48,11 +48,17 @@ public class Items {
short dataValue = (short) (split.length > 1 && Numerical.isInteger(split[1]) ? Integer.parseInt(split[1]) : 0);
if (Numerical.isInteger(itemName)) {
return new ItemStack(Material.getMaterial(Integer.parseInt(itemName)), 1, dataValue);
Material mat = Material.getMaterial(Integer.parseInt(itemName));
return mat == null ? null : new ItemStack(mat, 1, dataValue);
}
Material mat = getMaterial(itemName);
Material mat = getMat(itemName);
return (mat != null ? new ItemStack(mat, 1, dataValue) : null);
}
public static Material getMaterial(String name) {
ItemStack is = getItemStack(name);
return is != null ? is.getType() : null;
}
}

View File

@ -16,6 +16,8 @@ public class Odd {
public static ItemStack returnItemStack(String name) {
try {
return oddItem.getItemStack(name);
} catch (Exception ignored){return null;}
} catch (Exception ignored) {
return null;
}
}
}

View File

@ -4,20 +4,32 @@ import com.Acrobot.ChestShop.Messaging.Message;
import com.Acrobot.ChestShop.Protection.Security;
import com.Acrobot.ChestShop.Shop.ShopManagement;
import com.Acrobot.ChestShop.Utils.Config;
import com.Acrobot.ChestShop.Utils.SearchForBlock;
import com.Acrobot.ChestShop.Utils.SignUtil;
import net.minecraft.server.IInventory;
import net.minecraft.server.InventoryLargeChest;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.Chest;
import org.bukkit.block.Sign;
import org.bukkit.craftbukkit.entity.CraftPlayer;
import org.bukkit.craftbukkit.inventory.CraftInventory;
import org.bukkit.entity.Player;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerListener;
import org.bukkit.inventory.Inventory;
import java.util.HashMap;
/**
* @author Acrobot
*/
public class playerInteract extends PlayerListener {
private HashMap<Player, Long> time = new HashMap<Player, Long>();
public static int interval = 100;
public void onPlayerInteract(PlayerInteractEvent event) {
Action action = event.getAction();
Player player = event.getPlayer();
@ -44,10 +56,39 @@ public class playerInteract extends PlayerListener{
return;
}
if (time.containsKey(player) && (System.currentTimeMillis() - time.get(player)) < interval) {
return;
}
time.put(player, System.currentTimeMillis());
if(player.isSneaking()){
return;
}
if(player.getName().startsWith(sign.getLine(0))){
Chest chest1 = SearchForBlock.findChest(sign);
Inventory inv1 = chest1.getInventory();
IInventory iInv1 = ((CraftInventory) inv1).getInventory();
Chest chest2 = SearchForBlock.findNeighbor(chest1);
if(chest2 != null){
Inventory inv2 = chest2.getInventory();
IInventory iInv2 = ((CraftInventory) inv2).getInventory();
IInventory largeChest = new InventoryLargeChest("Shop", iInv1, iInv2);
((CraftPlayer) player).getHandle().a(largeChest);
return;
} else if(chest1 != null){
((CraftPlayer) player).getHandle().a(iInv1);
return;
} else {
player.sendMessage(Config.getLocal("NO_CHEST_DETECTED"));
return;
}
}
Action buy = (Config.getBoolean("reverse_buttons") ? Action.LEFT_CLICK_BLOCK : Action.RIGHT_CLICK_BLOCK);
if (action == buy) {
ShopManagement.buy(sign, player);
} else {

View File

@ -1,9 +1,125 @@
package com.Acrobot.ChestShop.Listeners;
import com.Acrobot.ChestShop.Items.Items;
import com.Acrobot.ChestShop.Permission;
import com.Acrobot.ChestShop.Protection.Security;
import com.Acrobot.ChestShop.Utils.Config;
import com.Acrobot.ChestShop.Utils.Numerical;
import com.Acrobot.ChestShop.Utils.SearchForBlock;
import com.Acrobot.ChestShop.Utils.SignUtil;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.Chest;
import org.bukkit.entity.Player;
import org.bukkit.event.block.BlockListener;
import org.bukkit.event.block.SignChangeEvent;
import org.bukkit.inventory.ItemStack;
/**
* @author Acrobot
*/
public class signChange extends BlockListener {
public void onSignChange(SignChangeEvent event) {
Block signBlock = event.getBlock();
String[] line = event.getLines();
Boolean isAlmostReady = SignUtil.isValidPreparedSign(event.getLines());
Player player = event.getPlayer();
ItemStack stock = Items.getItemStack(line[3]);
Material mat = stock == null ? null : stock.getType();
boolean playerIsAdmin = Permission.has(player, Permission.ADMIN);
Permission shopCreation = Permission.SHOP_CREATION;
if (mat == null) {
player.sendMessage(Config.getLocal("INCORRECT_ITEM_ID"));
dropSign(event);
return;
}
if (isAlmostReady) {
if (!playerIsAdmin && !(Permission.has(player, shopCreation)
|| ((Permission.has(player, shopCreation.getPermission() + '.' + mat.getId())
|| !Permission.has(player, Permission.EXCLUDE_ITEM.getPermission() + '.' + mat.getId()))))) {
player.sendMessage(Config.getLocal("YOU_CAN'T_CREATE_SHOP"));
dropSign(event);
return;
}
} else {
return;
}
Boolean isReady = SignUtil.isValid(line);
if (line[0].isEmpty() || (!line[0].startsWith(player.getName()) && !Permission.has(player, Permission.ADMIN))) {
event.setLine(0, player.getName());
}
line = event.getLines();
boolean isAdminShop = SignUtil.isAdminShop(line[0]);
if (!isReady) {
int prices = line[2].split(":").length;
String oldLine = line[2];
if (prices == 1) {
event.setLine(2, "B " + oldLine);
} else {
event.setLine(2, "B " + oldLine + " S");
}
}
String[] split = line[3].split(":");
if (Numerical.isInteger(split[0])) {
String matName = mat.name();
if (split.length == 2) {
int length = matName.length();
int maxLength = (15 - split[1].length() - 1);
if (length > maxLength) {
matName = matName.substring(0, maxLength);
}
event.setLine(3, matName + ':' + split[1]);
} else {
event.setLine(3, matName);
}
}
Chest chest = SearchForBlock.findChest(signBlock);
if (!isAdminShop) {
if (chest == null) {
player.sendMessage(Config.getLocal("NO_CHEST_DETECTED"));
dropSign(event);
return;
} else if (!playerIsAdmin) {
boolean canPlaceSign = Security.canPlaceSign(player, signBlock);
if (!canPlaceSign) {
player.sendMessage(Config.getLocal("ANOTHER_SHOP_DETECTED"));
dropSign(event);
return;
}
}
}
if (Security.protect(player.getName(), chest.getBlock())) {
player.sendMessage(Config.getLocal("PROTECTED_SHOP"));
}
player.sendMessage(Config.getLocal("SHOP_CREATED"));
}
public static void dropSign(SignChangeEvent event) {
event.setCancelled(true);
Block block = event.getBlock();
block.setType(Material.AIR);
block.getWorld().dropItemNaturally(block.getLocation(), new ItemStack(Material.SIGN, 1));
}
}

View File

@ -0,0 +1,33 @@
package com.Acrobot.ChestShop.Logging;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.LinkedList;
import java.util.List;
/**
* @author Acrobot
*/
public class FileWriterQueue implements Runnable{
private static List<String> queue = new LinkedList<String>();
public static String filePath = "plugins/ChestShop/ChestShop.log";
public static void addToQueue(String message){
queue.add(message);
}
public void run() {
try{
BufferedWriter bw = new BufferedWriter(new FileWriter(filePath, true));
for(String msg : queue){
bw.write(msg);
bw.newLine();
}
bw.close();
} catch (Exception e){
Logging.log("Couldn't write to log file!");
}
}
}

View File

@ -1,10 +1,51 @@
package com.Acrobot.ChestShop.Logging;
import com.Acrobot.ChestShop.DB.Queue;
import com.Acrobot.ChestShop.DB.Transaction;
import com.Acrobot.ChestShop.Shop.Shop;
import com.Acrobot.ChestShop.Utils.Config;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author Acrobot
*/
public class Logging {
private static DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
public static String getDateAndTime() {
Date date = new Date();
return dateFormat.format(date);
}
public static void log(String string) {
System.out.println("[ChestShop] " + string);
FileWriterQueue.addToQueue(getDateAndTime() + ' ' + string);
}
public static void logTransaction(boolean isBuying, Shop shop, Player player){
log(player.getName() + (isBuying ? " bought " : " sold ") + shop.stockAmount + ' ' + shop.stock.getType() + " for " + (isBuying ? shop.buyPrice + " from " : shop.sellPrice + " to ") + shop.owner);
if(!Config.getBoolean("useDB")){
return;
}
Transaction transaction = new Transaction();
transaction.setAmount(shop.stockAmount);
transaction.setBuy(isBuying);
ItemStack stock = shop.stock;
transaction.setItemDurability(stock.getDurability());
transaction.setItemID(stock.getTypeId());
transaction.setPrice((isBuying ? shop.buyPrice : shop.sellPrice));
transaction.setSec(System.currentTimeMillis()/1000);
transaction.setShopOwner(shop.owner);
transaction.setShopUser(player.getName());
Queue.addToQueue(transaction);
}
}

View File

@ -6,14 +6,33 @@ import org.bukkit.entity.Player;
/**
* @author Acrobot
*/
public class Permission {
public enum Permission {
SHOP_CREATION("iConomyChestShop.shop.create"),
EXCLUDE_ITEM("iConomyChestShop.shop.exclude"),
ADMIN("iConomyChestShop.admin");
private final String permission;
Permission(String permission) {
this.permission = permission;
}
public String getPermission() {
return permission;
}
public static PermissionHandler permissions;
public static boolean has(Player player, String permission) {
public static boolean has(Player player, Permission permission) {
String node = permission.permission;
return has(player, node);
}
public static boolean has(Player player, String node) {
if (permissions != null) {
return permissions.has(player, permission);
return permissions.has(player, node);
} else {
return !permission.contains("exclude") && (!permission.contains("admin") || player.isOp());
return !node.contains("exclude") && (!node.contains("admin") || player.isOp());
}
}
}

View File

@ -9,6 +9,8 @@ import org.bukkit.entity.Player;
*/
public interface Protection {
public boolean isProtected(Block block);
public boolean canAccess(Player player, Block block);
public boolean protect(String name, Block block);
}

View File

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

View File

@ -3,10 +3,13 @@ package com.Acrobot.ChestShop.Shop;
import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Chests.ChestObject;
import com.Acrobot.ChestShop.Economy;
import com.Acrobot.ChestShop.Logging.Logging;
import com.Acrobot.ChestShop.Utils.Config;
import com.Acrobot.ChestShop.Utils.InventoryUtil;
import com.Acrobot.ChestShop.Utils.SignUtil;
import net.minecraft.server.EntityPlayer;
import org.bukkit.block.Sign;
import org.bukkit.craftbukkit.entity.CraftPlayer;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
@ -14,12 +17,12 @@ import org.bukkit.inventory.ItemStack;
* @author Acrobot
*/
public class Shop {
private ItemStack stock;
private int stockAmount;
private ChestObject chest;
private float buyPrice;
private float sellPrice;
private String owner;
public ItemStack stock;
public int stockAmount;
public ChestObject chest;
public float buyPrice;
public float sellPrice;
public String owner;
public Shop(ChestObject chest, Sign sign, ItemStack... itemStacks) {
this.stock = itemStacks[0];
@ -31,6 +34,10 @@ public class Shop {
}
public boolean buy(Player player) {
if(chest == null && !isAdminShop()){
player.sendMessage(Config.getLocal("NO_CHEST_DETECTED"));
return false;
}
if (buyPrice == -1) {
player.sendMessage(Config.getLocal("NO_BUYING_HERE"));
return false;
@ -57,12 +64,8 @@ public class Shop {
if (!isAdminShop()) {
chest.removeItem(stock, stock.getDurability(), stockAmount);
}
InventoryUtil.add(player.getInventory(), stock, stockAmount);
player.updateInventory();
String formatedPrice = Economy.formatBalance(buyPrice);
player.sendMessage(Config.getLocal("YOU_BOUGHT_FROM_SHOP")
.replace("%amount", String.valueOf(stockAmount))
.replace("%item", materialName)
@ -75,10 +78,19 @@ public class Shop {
.replace("%buyer", player.getName())
.replace("%price", formatedPrice));
InventoryUtil.add(player.getInventory(), stock, stockAmount);
Logging.logTransaction(true, this, player);
updateInventory(player);
return true;
}
public boolean sell(Player player) {
if(chest == null && !isAdminShop()){
player.sendMessage(Config.getLocal("NO_CHEST_DETECTED"));
return false;
}
if (sellPrice == -1) {
player.sendMessage(Config.getLocal("NO_SELLING_HERE"));
return false;
@ -102,10 +114,6 @@ public class Shop {
chest.addItem(stock, stock.getDurability(), stockAmount);
}
InventoryUtil.remove(player.getInventory(), stock, stockAmount, stock.getDurability());
player.updateInventory();
Economy.add(player.getName(), sellPrice);
String materialName = stock.getType().name();
@ -123,6 +131,11 @@ public class Shop {
.replace("%seller", player.getName())
.replace("%price", formatedBalance));
InventoryUtil.remove(player.getInventory(), stock, stockAmount, stock.getDurability());
Logging.logTransaction(false, this, player);
updateInventory(player);
return true;
}
@ -142,6 +155,11 @@ public class Shop {
return chest.hasEnough(stock, stockAmount, stock.getDurability());
}
private void updateInventory(Player player){
EntityPlayer p = ((CraftPlayer) player).getHandle();
p.a(p.activeContainer);
}
private static boolean fits(ItemStack item, Player player) {
return InventoryUtil.fits(player.getInventory(), item, item.getAmount(), item.getDurability()) <= 0;
}

View File

@ -15,15 +15,13 @@ public class ShopManagement {
Chest chestMc = SearchForBlock.findChest(sign);
Shop shop = new Shop(chestMc != null ? new MinecraftChest(chestMc) : null, sign, Items.getItemStack(sign.getLine(3)));
shop.buy(player);
return true;
return shop.buy(player);
}
public static boolean sell(Sign sign, Player player) {
Chest chestMc = SearchForBlock.findChest(sign);
Shop shop = new Shop(chestMc != null ? new MinecraftChest(chestMc) : null, sign, Items.getItemStack(sign.getLine(3)));
shop.sell(player);
return true;
return shop.sell(player);
}
}

View File

@ -21,7 +21,6 @@ public class Config {
private static String langChar = Character.toString((char) 167);
public static void setUp() {
if (!configFile.exists()) {
try {
@ -33,25 +32,27 @@ public class Config {
}
config.load();
language.load();
Defaults.set();
}
public static boolean getBoolean(String node) {
return config.getBoolean(node, (Boolean) getDefaultValue(node));
return (Boolean) getValue(node);
}
public static String getString(String node) {
return getColored(config.getString(node, (String) getDefaultValue(node)));
return getColored((String) getValue(node));
}
public static int getInteger(String node) {
return config.getInt(node, Integer.parseInt(getDefaultValue(node).toString()));
return Integer.parseInt(getValue(node).toString());
}
public static double getDouble(String node) {
return config.getDouble(node, -1);
}
public static Object getDefaultValue(String node, Configuration configuration, File file){
private static Object getValue(String node, Configuration configuration, File file) {
if (configuration.getProperty(node) == null) {
try {
Object defaultValue = defaultValues.get(node);
@ -60,11 +61,11 @@ public class Config {
fw.write('\n' + node + ": " + defaultValue);
fw.close();
}
configuration.load();
} catch (Exception e) {
Logging.log("Failed to update config file!");
}
}
configuration.load();
return configuration.getProperty(node);
}
@ -73,14 +74,14 @@ public class Config {
}
public static String getLocal(String node) {
return getColored(language.getString("prefix",(String) getDefaultLocal("prefix")) + language.getString(node, (String) getDefaultLocal(node)));
return getColored(getDefaultLocal("prefix") + (String) getDefaultLocal(node));
}
public static Object getDefaultValue(String node){
return getDefaultValue(node, config, configFile);
private static Object getValue(String node) {
return getValue(node, config, configFile);
}
public static Object getDefaultLocal(String node){
return getDefaultValue(node, language, langFile);
private static Object getDefaultLocal(String node) {
return getValue(node, language, langFile);
}
}

View File

@ -7,13 +7,23 @@ public class Defaults {
public static void set() {
Config.defaultValues.put("reverse_buttons", "false #If true, people buy with left click and sell with right click");
Config.defaultValues.put("shopEconomyAccount", "\"\" #Place economy account you want Admin Shops to be assigned to");
Config.defaultValues.put("shopEconomyAccount", "\"\" #Economy account's name you want Admin Shops to be assigned to");
Config.defaultValues.put("logToFile", "false #If true, plugin will log transactions in its own file");
Config.defaultValues.put("useDB", "false #If true, plugin will log transactions in EBean database");
//LANGUAGE:
Config.defaultValues.put("prefix", "\"&2[Shop]&f\"");
Config.defaultValues.put("prefix", "\"&a[Shop] &f\"");
Config.defaultValues.put("iteminfo", "\"&aItem Information:&f\"");
Config.defaultValues.put("options", "\"&aCustomizable options: \"");
Config.defaultValues.put("NO_BUYING_HERE", "\"You can't buy here!\"");
Config.defaultValues.put("NO_SELLING_HERE", "\"You can't sell here!\"");
Config.defaultValues.put("NOT_ENOUGH_SPACE_IN_INVENTORY", "\"You haven't got enough space in inventory!\"");
Config.defaultValues.put("NOT_ENOUGH_SPACE_IN_CHEST", "\"There isn't enough space in chest!\"");
Config.defaultValues.put("NOT_ENOUGH_ITEMS_TO_SELL", "\"You have got not enough items to sell!\"");
Config.defaultValues.put("NOT_ENOUGH_STOCK", "\"This shop has not enough stock.\"");
Config.defaultValues.put("NOT_ENOUGH_STOCK_IN_YOUR_SHOP", "\"Your %material shop is out of stock!\"");
@ -22,5 +32,14 @@ public class Defaults {
Config.defaultValues.put("YOU_SOLD_TO_SHOP", "\"You sold %amount %item to %buyer for %price.\"");
Config.defaultValues.put("SOMEBODY_SOLD_TO_YOUR_SHOP", "\"%seller sold %amount %item for %price to you.\"");
Config.defaultValues.put("YOU_CAN'T_CREATE_SHOP", "\"You can't create this type of shop!\"");
Config.defaultValues.put("NO_CHEST_DETECTED", "\"Couldn't find a chest!\"");
Config.defaultValues.put("ANOTHER_SHOP_DETECTED", "\"Another player's shop detected!\"");
Config.defaultValues.put("PROTECTED_SHOP", "\"Successfully protected the shop!\"");
Config.defaultValues.put("SHOP_CREATED", "\"Shop successfully created!\"");
Config.defaultValues.put("INCORRECT_ITEM_ID", "\"You have specified invalid item id!\"");
}
}

View File

@ -51,7 +51,7 @@ public class InventoryUtil {
int maxStackSize = itemMaterial.getMaxStackSize();
ItemStack baseItem = item.clone();
for(int slot = 0; slot++ <= inv.getSize();){
for (int slot = 0; slot < contents.length; slot++) {
ItemStack itemStack = contents[slot];
if (amountLeft <= 0) {
return 0;
@ -126,6 +126,5 @@ public class InventoryUtil {
}
return amountLeft;
}
}

View File

@ -2,6 +2,7 @@ package com.Acrobot.ChestShop.Utils;
/**
* Checks if string is a numerical value
*
* @author Acrobot
*/
public class Numerical {

View File

@ -30,7 +30,7 @@ public class SearchForBlock {
for (BlockFace bf : BlockFace.values()) {
Block faceBlock = block.getFace(bf);
if (SignUtil.isSign(faceBlock)) {
Sign sign = (Sign) faceBlock;
Sign sign = (Sign) faceBlock.getState();
if (SignUtil.isValid(sign)) {
return sign;
}
@ -38,4 +38,16 @@ public class SearchForBlock {
}
return null;
}
public static Chest findNeighbor(Chest chest){
BlockFace[] bf = {BlockFace.EAST, BlockFace.NORTH, BlockFace.WEST, BlockFace.SOUTH};
Block chestBlock = chest.getBlock();
for (BlockFace blockFace : bf) {
Block neighborBlock = chestBlock.getFace(blockFace);
if (neighborBlock.getType() == Material.CHEST) {
return (Chest) neighborBlock.getState();
}
}
return null; //Shame, we didn't find double chest :/
}
}

View File

@ -20,13 +20,18 @@ public class SignUtil {
public static boolean isValid(Sign sign) {
return isValid(sign.getLines());
}
public static boolean isValid(String[] lines){
public static boolean isValid(String[] line) {
try {
String line1 = lines[0];
String line2 = lines[1];
String line3 = lines[2];
String line4 = lines[3];
return !line1.contains("[") && !line1.contains("]") && !line4.isEmpty() && Numerical.isInteger(line2) && (line3.contains("B") || line3.contains("S"));
return isValidPreparedSign(line) && (line[2].contains("B") || line[2].contains("S"));
} catch (Exception e) {
return false;
}
}
public static boolean isValidPreparedSign(String[] line) {
try {
return !line[0].contains("[") && !line[0].contains("]") && !line[3].isEmpty() && !line[3].split(":")[0].isEmpty() && Numerical.isInteger(line[1]) && line[2].split(":").length <= 2;
} catch (Exception e) {
return false;
}

View File

@ -15,12 +15,31 @@ import java.util.Set;
* @license: GNUv3 Affero License <http://www.gnu.org/licenses/agpl-3.0.html>
*/
public class Methods {
private boolean self = false;
private Method Method = null;
private String preferred = "";
private Set<Method> Methods = new HashSet<Method>();
private Set<String> Dependencies = new HashSet<String>();
private Set<Method> Attachables = new HashSet<Method>();
public Methods() {
this._init();
}
/**
* Allows you to set which economy plugin is most preferred.
*
* @param preferred
*/
public Methods(String preferred) {
this._init();
if(this.Dependencies.contains(preferred)) {
this.preferred = preferred;
}
}
private void _init() {
this.addMethod("iConomy", new com.nijikokun.register.payment.methods.iCo4());
this.addMethod("iConomy", new com.nijikokun.register.payment.methods.iCo5());
this.addMethod("BOSEconomy", new com.nijikokun.register.payment.methods.BOSE());
@ -48,23 +67,64 @@ public class Methods {
}
public boolean hasMethod() {
return (this.Method != null);
return (Method != null);
}
public boolean setMethod(Plugin method) {
if(hasMethod()) return true;
if(self) { self = false; return false; }
PluginManager manager = method.getServer().getPluginManager();
int count = 0;
boolean match = false;
Plugin plugin = null;
PluginManager manager = method.getServer().getPluginManager();
for(String name: this.Dependencies) {
if(hasMethod()) break;
if(method.getDescription().getName().equals(name)) plugin = method; else plugin = manager.getPlugin(name);
if(plugin == null) continue;
if(!plugin.isEnabled()) continue;
if(!plugin.isEnabled()) {
this.self = true;
manager.enablePlugin(plugin);
}
Method current = this.createMethod(plugin);
if (current != null) this.Method = current;
if(current == null) continue;
if(this.preferred.isEmpty())
this.Method = current;
else {
this.Attachables.add(current);
}
}
if(!this.preferred.isEmpty()) {
do {
if(hasMethod()) {
match = true;
} else {
for(Method attached: this.Attachables) {
if(attached == null) continue;
if(hasMethod()) {
match = true;
break;
}
if(this.preferred.isEmpty()) this.Method = attached;
if(count == 0) {
if(this.preferred.equalsIgnoreCase(attached.getName()))
this.Method = attached;
} else {
this.Method = attached;
}
}
count++;
}
} while(!match);
}
return hasMethod();

View File

@ -58,7 +58,7 @@ public class BOSE implements Method {
BOSEconomy = (BOSEconomy)plugin;
}
public class BOSEAccount implements MethodAccount {
public static class BOSEAccount implements MethodAccount {
private String name;
private BOSEconomy BOSEconomy;
@ -121,7 +121,7 @@ public class BOSE implements Method {
}
}
public class BOSEBankAccount implements MethodBankAccount {
public static class BOSEBankAccount implements MethodBankAccount {
private String bank;
private String name;
private BOSEconomy BOSEconomy;

View File

@ -17,7 +17,7 @@ public class EE17 implements Method {
}
public String getName() {
return "EssentialsEco";
return "Essentials";
}
public String getVersion() {
@ -56,6 +56,7 @@ public class EE17 implements Method {
public boolean isCompatible(Plugin plugin) {
try { Class.forName("com.earth2me.essentials.api.Economy"); }
catch(Exception e) { return false; }
return plugin.getDescription().getName().equalsIgnoreCase("essentials") && plugin instanceof Essentials;
}
@ -63,7 +64,7 @@ public class EE17 implements Method {
Essentials = (Essentials)plugin;
}
public class EEcoAccount implements MethodAccount {
public static class EEcoAccount implements MethodAccount {
private String name;
public EEcoAccount(String name) {

View File

@ -58,7 +58,7 @@ public class iCo4 implements Method {
iConomy = (iConomy)plugin;
}
public class iCoAccount implements MethodAccount {
public static class iCoAccount implements MethodAccount {
private Account account;
public iCoAccount(Account account) {

View File

@ -42,7 +42,7 @@ public class iCo5 implements Method {
}
public boolean hasBankAccount(String bank, String name) {
return (!hasBank(bank)) && iConomy.getBank(name).hasAccount(name);
return (hasBank(bank)) && iConomy.getBank(bank).hasAccount(name);
}
public MethodAccount getAccount(String name) {
@ -61,7 +61,7 @@ public class iCo5 implements Method {
iConomy = (iConomy)plugin;
}
public class iCoAccount implements MethodAccount {
public static class iCoAccount implements MethodAccount {
private Account account;
private Holdings holdings;
@ -131,7 +131,7 @@ public class iCo5 implements Method {
}
}
public class iCoBankAccount implements MethodBankAccount {
public static class iCoBankAccount implements MethodBankAccount {
private BankAccount account;
private Holdings holdings;

View File

@ -10,23 +10,20 @@ author: Acrobot
description: >
A chest shop for economy mods.
commands:
buy:
aliases: chBuy
description: Toggles mode to buying
usage: |
/<command>
sell:
aliases: chSell
description: Toggles mode to selling
usage: |
/<command>
iteminfo:
aliases:
aliases: [ii,iinfo]
description: Lists item id and names
usage: |
/<command> <partial item name or id>
/<command> (item name/item ID/alias)
() - optional
csVersion:
aliases:
aliases: cv
description: Shows the ChestShop's version
usage: |
/<command>
chestOptions:
aliases: [coptions,cop,cpref]
description: Sets ChestShop's preferences - Not implemented yet
usage: |
/<command> (option) (value)
() - optional