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;
public static String getVersion() {
return desc.getVersion();
}
//iCSversion
if(commandName.equals("icsversion")){
sender.sendMessage("ChestShop's version is: " + desc.getVersion());
return true;
}
public static String getPluginName() {
return desc.getName();
}
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;
}
}
//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;
@ -11,55 +9,55 @@ import org.bukkit.inventory.ItemStack;
/**
* @author Acrobot
*/
public class MinecraftChest implements ChestObject{
public class MinecraftChest implements ChestObject {
Chest main;
Chest neighbor;
public MinecraftChest(Chest chest){
public MinecraftChest(Chest chest) {
this.main = chest;
this.neighbor = getNeighbor();
}
public ItemStack[] getContents() {
ItemStack[] contents = new ItemStack[(neighbor != null ? 54 : 27)];
ItemStack[] chest1 = main.getInventory().getContents();
System.arraycopy(chest1, 0, contents, 0, chest1.length);
if(neighbor != null){
if (neighbor != null) {
ItemStack[] chest2 = neighbor.getInventory().getContents();
System.arraycopy(chest2, 0, contents, chest1.length, chest2.length);
}
return contents;
}
public void setSlot(int slot, ItemStack item) {
if(slot < main.getInventory().getSize()){
if (slot < main.getInventory().getSize()) {
main.getInventory().setItem(slot, item);
} else{
} else {
neighbor.getInventory().setItem(slot - main.getInventory().getSize(), item);
}
}
public void clearSlot(int slot) {
if(slot < main.getInventory().getSize()){
if (slot < main.getInventory().getSize()) {
main.getInventory().setItem(slot, null);
} else{
} else {
neighbor.getInventory().setItem(slot - main.getInventory().getSize(), null);
}
}
public void addItem(ItemStack item, short durability, int amount) {
int left = addItem(item, durability, amount, main);
if(neighbor != null){
addItem(item, durability, left, neighbor);
int left = addItem(item, amount, main);
if (neighbor != null) {
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) {
removeItem(item, durability, left, neighbor);
}
}
@ -81,33 +79,25 @@ public class MinecraftChest implements ChestObject{
return main.getInventory().getSize() + (neighbor != null ? neighbor.getInventory().getSize() : 0);
}
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 :/
private Chest getNeighbor() {
return SearchForBlock.findNeighbor(main);
}
private static int amount(ItemStack item, short durability, Chest chest){
private static int amount(ItemStack item, short durability, Chest chest) {
return InventoryUtil.amount(chest.getInventory(), item, durability);
}
private static int fits(ItemStack item, int amount, short durability, Chest chest){
private static int fits(ItemStack item, int amount, short durability, Chest chest) {
Inventory inv = chest.getInventory();
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);
}
private static int removeItem(ItemStack item, short durability, int amount, Chest chest){
private static int removeItem(ItemStack item, short durability, int amount, Chest chest) {
Inventory inv = chest.getInventory();
return InventoryUtil.remove(inv, item, amount, durability);
}

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

@ -4,31 +4,32 @@ import com.nijikokun.register.payment.Method;
/**
* @author Acrobot
* Economy management
* Economy management
*/
public class Economy {
public static Method economy;
public static boolean hasAccount(String p){
public static boolean hasAccount(String p) {
return economy.hasAccount(p);
}
public static void add(String name, float amount){
public static void add(String name, float amount) {
economy.getAccount(name).add(amount);
}
public static void substract(String name, float amount){
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);
}
public static double balance(String name){
public static double balance(String name) {
return economy.getAccount(name).balance();
}
public static String formatBalance(double amount){
public static String formatBalance(double amount) {
return economy.format(amount);
}
}

View File

@ -6,25 +6,25 @@ import org.bukkit.inventory.ItemStack;
/**
* @author Acrobot
* Manages ItemStack names and ID's
* Manages ItemStack names and ID's
*/
public class Items {
public static String getItemName(ItemStack itemStack){
public static String getItemName(ItemStack itemStack) {
return getItemName(itemStack.getType().name());
}
public static String getItemName(String itemName){
return getMaterial(itemName).name();
public static String getItemName(String itemName) {
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(" ", "");
for(Material m: Material.values()){
String matName = m.name().toLowerCase().replace("_","").replace(" ", "");
if(matName.startsWith(itemName) && (matName.length() < length)){
itemName = itemName.toLowerCase().replace("_", "").replace(" ", "");
for (Material m : Material.values()) {
String matName = m.name().toLowerCase().replace("_", "").replace(" ", "");
if (matName.startsWith(itemName) && (matName.length() < length)) {
length = matName.length();
finalMat = m;
}
@ -32,14 +32,14 @@ public class Items {
return finalMat;
}
public static int getItemID(String itemName){
return getMaterial(itemName).getId();
public static int getItemID(String itemName) {
return getMat(itemName).getId();
}
public static ItemStack getItemStack(String itemName){
if(Odd.isInitialized()){
public static ItemStack getItemStack(String itemName) {
if (Odd.isInitialized()) {
ItemStack odd = Odd.returnItemStack(itemName.replace(":", ";"));
if(odd != null){
if (odd != null) {
return odd;
}
}
@ -47,12 +47,18 @@ public class Items {
itemName = split[0];
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);
if (Numerical.isInteger(itemName)) {
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

@ -9,13 +9,15 @@ import org.bukkit.inventory.ItemStack;
public class Odd {
public static OddItem oddItem;
public static boolean isInitialized(){
public static boolean isInitialized() {
return oddItem != null;
}
public static ItemStack returnItemStack(String name){
try{
public static ItemStack returnItemStack(String name) {
try {
return oddItem.getItemStack(name);
} catch (Exception ignored){return null;}
} catch (Exception ignored) {
return null;
}
}
}

View File

@ -6,5 +6,5 @@ import org.bukkit.event.block.BlockListener;
* @author Acrobot
*/
public class blockBreak extends BlockListener {
}

View File

@ -10,9 +10,9 @@ import org.bukkit.event.block.BlockPlaceEvent;
* @author Acrobot
*/
public class blockPlace extends BlockListener {
public void onBlockPlace(BlockPlaceEvent event){
public void onBlockPlace(BlockPlaceEvent event) {
Block block = event.getBlockAgainst();
if(SignUtil.isSign(block) && SignUtil.isValid((Sign) block.getState())){
if (SignUtil.isSign(block) && SignUtil.isValid((Sign) block.getState())) {
event.setCancelled(true);
}
}

View File

@ -4,53 +4,94 @@ 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{
public class playerInteract extends PlayerListener {
public void onPlayerInteract(PlayerInteractEvent event){
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();
if(action != Action.LEFT_CLICK_BLOCK && action != Action.RIGHT_CLICK_BLOCK){
if (action != Action.LEFT_CLICK_BLOCK && action != Action.RIGHT_CLICK_BLOCK) {
return;
}
Block block = event.getClickedBlock();
Block block = event.getClickedBlock();
if(block.getType() == Material.CHEST){
if(Security.isProtected(block) && !Security.canAccess(player, block)){
if (block.getType() == Material.CHEST) {
if (Security.isProtected(block) && !Security.canAccess(player, block)) {
Message.sendMsg(player, "ACCESS_DENIED");
event.setCancelled(true);
return;
}
}
if(!SignUtil.isSign(block)){
if (!SignUtil.isSign(block)) {
return;
}
Sign sign = (Sign) block.getState();
if(!SignUtil.isValid(sign)){
if (!SignUtil.isValid(sign)) {
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){
if (action == buy) {
ShopManagement.buy(sign, player);
} else{
} else {
ShopManagement.sell(sign, player);
}
}

View File

@ -20,16 +20,16 @@ import org.yi.acru.bukkit.Lockette.Lockette;
/**
* @author Acrobot
*/
public class pluginEnable extends ServerListener{
public class pluginEnable extends ServerListener {
private Methods methods = new Methods();
public void onPluginEnable(PluginEnableEvent event){
public void onPluginEnable(PluginEnableEvent event) {
//Economy plugins
if(!this.methods.hasMethod()){
if(methods.setMethod(event.getPlugin())){
if (!this.methods.hasMethod()) {
if (methods.setMethod(event.getPlugin())) {
Economy.economy = methods.getMethod();
System.out.println("[ChestShop] " + Economy.economy.getName() + ' ' + Economy.economy.getVersion() + " loaded.");
}

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 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 {
public static void log(String string){
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,7 +6,7 @@ import org.bukkit.entity.Player;
* @author Acrobot
*/
public class Message {
public static void sendMsg(Player player, String msg){
public static void sendMsg(Player player, String msg) {
player.sendMessage(msg);
}
}

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

@ -8,7 +8,7 @@ import org.bukkit.entity.Player;
/**
* @author Acrobot
*/
public class Default implements Protection{
public class Default implements Protection {
public boolean isProtected(Block block) {
return (block != null) && SignUtil.isSign(block) && SignUtil.isValid((Sign) block.getState());
}

View File

@ -8,7 +8,7 @@ import org.bukkit.entity.Player;
/**
* @author Acrobot
*/
public class LWCplugin implements Protection{
public class LWCplugin implements Protection {
public static LWC lwc;

View File

@ -7,7 +7,7 @@ import org.yi.acru.bukkit.Lockette.Lockette;
/**
* @author Acrobot
*/
public class LockettePlugin implements Protection{
public class LockettePlugin implements Protection {
public static Lockette lockette;
public boolean isProtected(Block block) {

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;
/**
@ -9,15 +12,23 @@ import org.bukkit.entity.Player;
public class Security {
public static Protection protection = new Default();
public static boolean protect(String name, Block block){
public static boolean protect(String name, Block block) {
return protection.protect(name, block);
}
public static boolean canAccess(Player player, Block block){
public static boolean canAccess(Player player, Block block) {
return protection.canAccess(player, block);
}
public static boolean isProtected(Block block){
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,14 +17,14 @@ 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){
public Shop(ChestObject chest, Sign sign, ItemStack... itemStacks) {
this.stock = itemStacks[0];
this.chest = chest;
this.buyPrice = SignUtil.buyPrice(sign.getLine(2));
@ -30,39 +33,39 @@ public class Shop {
this.stockAmount = SignUtil.itemAmount(sign.getLine(1));
}
public boolean buy(Player player){
if(buyPrice == -1){
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;
}
if(!fits(stock, player)){
if (!fits(stock, player)) {
player.sendMessage(Config.getLocal("NOT_ENOUGH_SPACE_IN_INVENTORY"));
return false;
}
String materialName = stock.getType().name();
if(!isAdminShop() && !hasEnoughStock()){
if (!isAdminShop() && !hasEnoughStock()) {
player.sendMessage(Config.getLocal("NOT_ENOUGH_STOCK"));
sendMessageToOwner(Config.getLocal("NOT_ENOUGH_STOCK_IN_YOUR_SHOP").replace("%material", materialName));
return false;
}
if(!getOwner().isEmpty() && Economy.hasAccount(getOwner())){
if (!getOwner().isEmpty() && Economy.hasAccount(getOwner())) {
Economy.add(getOwner(), buyPrice);
}
Economy.substract(player.getName(), buyPrice);
if(!isAdminShop()){
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)
@ -74,38 +77,43 @@ public class Shop {
.replace("%item", materialName)
.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(sellPrice == -1){
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;
}
if(!isAdminShop() && !fits(stock, chest)){
if (!isAdminShop() && !fits(stock, chest)) {
player.sendMessage(Config.getLocal("NOT_ENOUGH_SPACE_IN_CHEST"));
return false;
}
if(InventoryUtil.amount(player.getInventory(), stock, stock.getDurability()) < stockAmount){
if (InventoryUtil.amount(player.getInventory(), stock, stock.getDurability()) < stockAmount) {
player.sendMessage(Config.getLocal("NOT_ENOUGH_ITEMS_TO_SELL"));
return false;
}
if(!getOwner().isEmpty() && Economy.hasAccount(getOwner())){
if (!getOwner().isEmpty() && Economy.hasAccount(getOwner())) {
Economy.substract(getOwner(), sellPrice);
}
if(!isAdminShop()){
if (!isAdminShop()) {
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,37 +131,47 @@ 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;
}
private String getOwner(){
if(SignUtil.isAdminShop(owner)){
private String getOwner() {
if (SignUtil.isAdminShop(owner)) {
return Config.getString("shopEconomyAccount");
} else{
} else {
return owner;
}
}
private boolean isAdminShop(){
private boolean isAdminShop() {
return SignUtil.isAdminShop(owner);
}
private boolean hasEnoughStock(){
private boolean hasEnoughStock() {
return chest.hasEnough(stock, stockAmount, stock.getDurability());
}
private static boolean fits(ItemStack item, Player player){
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;
}
private static boolean fits(ItemStack item, ChestObject chest){
private static boolean fits(ItemStack item, ChestObject chest) {
return chest.fits(item, item.getAmount(), item.getDurability());
}
private void sendMessageToOwner(String msg){
if(!isAdminShop()){
private void sendMessageToOwner(String msg) {
if (!isAdminShop()) {
Player player = ChestShop.getBukkitServer().getPlayer(owner);
if(player != null){
if (player != null) {
player.sendMessage(msg);
}
}

View File

@ -11,19 +11,17 @@ import org.bukkit.entity.Player;
* @author Acrobot
*/
public class ShopManagement {
public static boolean buy(Sign sign, Player player){
public static boolean buy(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.buy(player);
return true;
return shop.buy(player);
}
public static boolean sell(Sign sign, Player 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

@ -13,7 +13,7 @@ import java.util.HashMap;
public class Config {
private static File configFile = new File("plugins/ChestShop/config.yml");
private static File langFile = new File("plugins/ChestShop/local.yml");
private static Configuration config = new Configuration(configFile);
private static Configuration language = new Configuration(langFile);
@ -21,9 +21,8 @@ public class Config {
private static String langChar = Character.toString((char) 167);
public static void setUp(){
if(!configFile.exists()){
public static void setUp() {
if (!configFile.exists()) {
try {
configFile.createNewFile();
Logging.log("Successfully created blank configuration file");
@ -33,54 +32,56 @@ public class Config {
}
config.load();
language.load();
Defaults.set();
}
public static boolean getBoolean(String node){
return config.getBoolean(node, (Boolean) getDefaultValue(node));
public static boolean getBoolean(String node) {
return (Boolean) getValue(node);
}
public static String getString(String node){
return getColored(config.getString(node, (String) getDefaultValue(node)));
public static String getString(String node) {
return getColored((String) getValue(node));
}
public static int getInteger(String node){
return config.getInt(node, Integer.parseInt(getDefaultValue(node).toString()));
public static int getInteger(String node) {
return Integer.parseInt(getValue(node).toString());
}
public static double getDouble(String node){
public static double getDouble(String node) {
return config.getDouble(node, -1);
}
public static Object getDefaultValue(String node, Configuration configuration, File file){
if(configuration.getProperty(node) == null){
try{
private static Object getValue(String node, Configuration configuration, File file) {
if (configuration.getProperty(node) == null) {
try {
Object defaultValue = defaultValues.get(node);
if(defaultValue != null){
if (defaultValue != null) {
FileWriter fw = new FileWriter(file, true);
fw.write('\n' + node+": " + defaultValue);
fw.write('\n' + node + ": " + defaultValue);
fw.close();
}
} catch (Exception e){
configuration.load();
} catch (Exception e) {
Logging.log("Failed to update config file!");
}
}
configuration.load();
return configuration.getProperty(node);
}
public static String getColored(String msg){
public static String getColored(String msg) {
return msg.replaceAll("&", langChar);
}
public static String getLocal(String node){
return getColored(language.getString("prefix",(String) getDefaultLocal("prefix")) + language.getString(node, (String) getDefaultLocal(node)));
public static String getLocal(String 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

@ -5,15 +5,25 @@ package com.Acrobot.ChestShop.Utils;
*/
public class Defaults {
public static void set(){
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

@ -9,29 +9,29 @@ import org.bukkit.inventory.ItemStack;
*/
public class InventoryUtil {
public static int remove(Inventory inv, ItemStack item, int amount, short durability){
public static int remove(Inventory inv, ItemStack item, int amount, short durability) {
Material itemMaterial = item.getType();
int amountLeft = amount;
for(int slot = 0; slot < inv.getSize(); slot++){
if(amountLeft <= 0){
for (int slot = 0; slot < inv.getSize(); slot++) {
if (amountLeft <= 0) {
return 0;
}
ItemStack currentItem = inv.getItem(slot);
if(currentItem == null || currentItem.getType() == Material.AIR){
if (currentItem == null || currentItem.getType() == Material.AIR) {
continue;
}
if(currentItem.getType() == itemMaterial && (durability == -1 || currentItem.getDurability() == durability)){
if (currentItem.getType() == itemMaterial && (durability == -1 || currentItem.getDurability() == durability)) {
int currentAmount = currentItem.getAmount();
if(amountLeft == currentAmount){
if (amountLeft == currentAmount) {
currentItem = null;
amountLeft = 0;
} else if(amountLeft < currentAmount){
} else if (amountLeft < currentAmount) {
currentItem.setAmount(currentAmount - amountLeft);
amountLeft = 0;
} else{
} else {
currentItem = null;
amountLeft -= currentAmount;
}
@ -42,7 +42,7 @@ public class InventoryUtil {
return amountLeft;
}
public static int add(Inventory inv, ItemStack item, int amount){
public static int add(Inventory inv, ItemStack item, int amount) {
ItemStack[] contents = inv.getContents();
Material itemMaterial = item.getType();
short durability = item.getDurability();
@ -51,31 +51,31 @@ 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){
if (amountLeft <= 0) {
return 0;
}
if(itemStack != null && itemStack.getType() != Material.AIR){ //Our slot is not free
if (itemStack != null && itemStack.getType() != Material.AIR) { //Our slot is not free
int currentAmount = itemStack.getAmount();
Material currentMaterial = itemStack.getType();
short currentDurability = itemStack.getDurability();
if(currentMaterial == itemMaterial && (currentDurability == durability)){
if((currentAmount + amountLeft) <= maxStackSize){
if (currentMaterial == itemMaterial && (currentDurability == durability)) {
if ((currentAmount + amountLeft) <= maxStackSize) {
baseItem.setAmount(currentAmount + amountLeft);
amountLeft = 0;
} else{
} else {
baseItem.setAmount(maxStackSize);
amountLeft -= (maxStackSize - currentAmount);
}
inv.setItem(slot, baseItem);
}
}else{ //Free slot
if(amountLeft <= maxStackSize){ //There is less to add than whole stack
} else { //Free slot
if (amountLeft <= maxStackSize) { //There is less to add than whole stack
baseItem.setAmount(amountLeft);
inv.setItem(slot, baseItem);
amountLeft = 0;
} else{ //We add whole stack
} else { //We add whole stack
baseItem.setAmount(maxStackSize);
inv.setItem(slot, baseItem);
amountLeft -= maxStackSize;
@ -85,12 +85,12 @@ public class InventoryUtil {
return amountLeft;
}
public static int amount(Inventory inv, ItemStack item, short durability){
public static int amount(Inventory inv, ItemStack item, short durability) {
int amount = 0;
ItemStack[] contents = inv.getContents();
for(ItemStack i : contents){
if(i != null){
if(i.getType() == item.getType() && (durability == -1 || i.getDurability() == durability || (durability == 0 && i.getDurability() == -1))){
for (ItemStack i : contents) {
if (i != null) {
if (i.getType() == item.getType() && (durability == -1 || i.getDurability() == durability || (durability == 0 && i.getDurability() == -1))) {
amount += i.getAmount();
}
}
@ -98,34 +98,33 @@ public class InventoryUtil {
return amount;
}
public static int fits(Inventory inv, ItemStack item, int amount, short durability){
public static int fits(Inventory inv, ItemStack item, int amount, short durability) {
Material itemMaterial = item.getType();
int maxStackSize = itemMaterial.getMaxStackSize();
int amountLeft = amount;
for(ItemStack currentItem : inv.getContents()){
if(amountLeft <= 0){
for (ItemStack currentItem : inv.getContents()) {
if (amountLeft <= 0) {
return 0;
}
if(currentItem == null || currentItem.getType() == Material.AIR){
if (currentItem == null || currentItem.getType() == Material.AIR) {
amountLeft -= maxStackSize;
continue;
}
int currentAmount = currentItem.getAmount();
if(currentAmount == itemMaterial.getMaxStackSize()){
if (currentAmount == itemMaterial.getMaxStackSize()) {
continue;
}
if(currentItem.getType() == itemMaterial && (durability == -1 || currentItem.getDurability() == durability)){
if (currentItem.getType() == itemMaterial && (durability == -1 || currentItem.getDurability() == durability)) {
currentAmount = currentAmount < 1 ? 1 : currentAmount;
amountLeft = (amountLeft <= currentAmount ? 0 : currentAmount);
}
}
return amountLeft;
}
}

View File

@ -2,32 +2,33 @@ package com.Acrobot.ChestShop.Utils;
/**
* Checks if string is a numerical value
*
* @author Acrobot
*/
public class Numerical {
public static boolean isInteger(String string){
try{
public static boolean isInteger(String string) {
try {
Integer.parseInt(string);
return true;
} catch (Exception e){
} catch (Exception e) {
return false;
}
}
public static boolean isFloat(String string){
try{
public static boolean isFloat(String string) {
try {
Float.parseFloat(string);
return true;
} catch (Exception e){
} catch (Exception e) {
return false;
}
}
public static boolean isDouble(String string){
try{
public static boolean isDouble(String string) {
try {
Double.parseDouble(string);
return true;
} catch (Exception e){
} catch (Exception e) {
return false;
}
}

View File

@ -11,31 +11,43 @@ import org.bukkit.block.Sign;
*/
public class SearchForBlock {
public static Chest findChest(Sign sign){
public static Chest findChest(Sign sign) {
Block block = sign.getBlock();
return findChest(block);
}
public static Chest findChest(Block block){
for(BlockFace bf : BlockFace.values()){
public static Chest findChest(Block block) {
for (BlockFace bf : BlockFace.values()) {
Block faceBlock = block.getFace(bf);
if(faceBlock.getType() == Material.CHEST){
if (faceBlock.getType() == Material.CHEST) {
return (Chest) faceBlock.getState();
}
}
return null;
}
public static Sign findSign(Block block){
for(BlockFace bf : BlockFace.values()){
public static Sign findSign(Block block) {
for (BlockFace bf : BlockFace.values()) {
Block faceBlock = block.getFace(bf);
if(SignUtil.isSign(faceBlock)){
Sign sign = (Sign) faceBlock;
if(SignUtil.isValid(sign)){
if (SignUtil.isSign(faceBlock)) {
Sign sign = (Sign) faceBlock.getState();
if (SignUtil.isValid(sign)) {
return sign;
}
}
}
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

@ -9,72 +9,77 @@ import org.bukkit.block.Sign;
*/
public class SignUtil {
public static boolean isSign(Block block){
public static boolean isSign(Block block) {
return (block.getType() == Material.SIGN_POST || block.getType() == Material.WALL_SIGN);
}
public static boolean isAdminShop(String owner){
public static boolean isAdminShop(String owner) {
return owner.toLowerCase().replace(" ", "").equals("adminshop");
}
public static boolean isValid(Sign sign){
public static boolean isValid(Sign sign) {
return isValid(sign.getLines());
}
public static boolean isValid(String[] lines){
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"));
} catch (Exception e){
public static boolean isValid(String[] line) {
try {
return isValidPreparedSign(line) && (line[2].contains("B") || line[2].contains("S"));
} catch (Exception e) {
return false;
}
}
public static float buyPrice(String text){
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;
}
}
public static float buyPrice(String text) {
text = text.replace(" ", "").toLowerCase();
int buyPart = (text.contains("b") ? 0 : -1);
if(buyPart == -1){
if (buyPart == -1) {
return -1;
}
text = text.replace("b", "").replace("s", "");
String[] split = text.split(":");
if(Numerical.isFloat(split[0])){
if (Numerical.isFloat(split[0])) {
float buyPrice = Float.parseFloat(split[0]);
return (buyPrice != 0 ? buyPrice : -1);
}else if(split[0].equals("free")){
} else if (split[0].equals("free")) {
return 0;
}
return -1;
}
public static float sellPrice(String text){
public static float sellPrice(String text) {
text = text.replace(" ", "").toLowerCase();
int sellPart = (text.contains("b") && text.contains("s") ? 1 : (text.contains("s") ? 0 : -1));
text = text.replace("b", "").replace("s", "");
String[] split = text.split(":");
if(sellPart == -1 || (sellPart == 1 && split.length < 2)){
if (sellPart == -1 || (sellPart == 1 && split.length < 2)) {
return -1;
}
if(Numerical.isFloat(split[sellPart])){
if (Numerical.isFloat(split[sellPart])) {
Float sellPrice = Float.parseFloat(split[sellPart]);
return (sellPrice != 0 ? sellPrice : -1);
}else if(split[sellPart].equals("free")){
} else if (split[sellPart].equals("free")) {
return 0;
}
return -1;
}
public static int itemAmount(String text){
if(Numerical.isInteger(text)){
public static int itemAmount(String text) {
if (Numerical.isInteger(text)) {
return Integer.parseInt(text);
} else{
} else {
return 0;
}
}

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>
/<command>
chestOptions:
aliases: [coptions,cop,cpref]
description: Sets ChestShop's preferences - Not implemented yet
usage: |
/<command> (option) (value)
() - optional