import java.util.Map; import java.util.HashMap; import java.util.concurrent.ConcurrentHashMap; import java.io.*; public class Economy extends Plugin { @Override public void enable() {} @Override public void disable() {} @Override public void initialize() { PluginListener plugin = new EconomyPluginListener(); PluginLoader loader = etc.getLoader(); loader.addListener( PluginLoader.Hook.COMMAND, plugin, this, PluginListener.Priority.MEDIUM ); loader.addListener( PluginLoader.Hook.SERVERCOMMAND, plugin, this, PluginListener.Priority.MEDIUM ); } class ExchangeRate{ public ExchangeRate() {} public ExchangeRate(int id, String name, int sell, int buy) { this.id = id; this.name = name; this.sell = sell; this.buy = buy; } int id; //blockid String name; int sell; //the amount the user receives when selling item int buy; } public class EconomyPluginListener extends PluginListener { final static String balanceFile = "economy-balance.txt"; final static String ratesFile = "economy-rates.txt"; private Map itemMap = new HashMap(); private Map balances = new ConcurrentHashMap(); private Map rates = new HashMap(); public EconomyPluginListener() { loadExchangeRates(); loadUserBalances(); } private void loadExchangeRates() { try { File f = new File(ratesFile); if (f.exists() ) { BufferedReader in = new BufferedReader(new InputStreamReader( new FileInputStream(f) ) ); rates.clear(); itemMap.clear(); String line; while ( (line = in.readLine()) != null) { line = line.trim(); if (line.length() == 0 || line.startsWith("#")) { continue; } String parts[] = line.split(":"); ExchangeRate r = new ExchangeRate(); r.id = Integer.parseInt(parts[0]); r.name = parts[1]; r.sell = Integer.parseInt(parts[2]); r.buy = Integer.parseInt(parts[3]); rates.put(r.id, r); itemMap.put(r.name.toLowerCase(), r.id); } } else { System.out.println("Could not find rates-file - creating new"); BufferedWriter out = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(f)) ); out.write("#itemID:name:sellrate:buyrate\n"); out.close(); } } catch (Exception e) { System.out.println("Error loading rates: " + e.getMessage() ); } } private void loadUserBalances() { try { File f = new File(balanceFile); if (f.exists() ) { BufferedReader in = new BufferedReader(new InputStreamReader( new FileInputStream(f) ) ); String line; while ( (line = in.readLine()) != null) { line = line.trim(); if (line.length() == 0 || line.startsWith("#")) { continue; } String parts[] = line.split(":"); balances.put(parts[0], Integer.parseInt(parts[1]) ); } in.close(); } } catch (Exception e) { System.out.println("Error loading balances: " + e.getMessage() ); } } private synchronized void saveUserBalance(String username) { try { FileOutputStream fos = new FileOutputStream(balanceFile); BufferedWriter out = new BufferedWriter( new OutputStreamWriter(fos) ); for (String key : balances.keySet() ) { out.write( key + ":" + balances.get(key) ); out.newLine(); } out.close(); fos.close(); } catch (Exception e) { System.out.println("Error saving user balance: " + e.getMessage() ); } } private void adminCoins(Player player, String[] split) { if(split.length != 4 || !(split[1].equals("give") || split[1].equals("take") )) { player.sendMessage( Colors.Green + "Usage: /admincoins (give|take) "); return; } Player p2 = etc.getServer().getPlayer( split[2]); if (p2 == null) { player.sendMessage( Colors.Green + split[0] + ": could not find player " + split[2]); return; } int amount; try { amount = Integer.parseInt(split[3]); } catch (Exception e) { player.sendMessage( Colors.Green + split[0] + ": amount must be a number"); return; } Integer balance = balances.get(p2.getName() ); if (balance == null) { balance = 0; } if (split[1].equals("give")) { balance += amount; player.sendMessage(Colors.Green + split[0] + ": coins given to " + p2.getName() ); p2.sendMessage(Colors.Green + "You have been given " + amount + " coins"); } else { //take if (amount > balance) { player.sendMessage(Colors.Green + split[0] + ": " + p2.getName() + " doesnt have that many coins"); return; } balance -= amount; player.sendMessage(Colors.Green + split[0] + ": coins taken from " + p2.getName() ); p2.sendMessage(Colors.Green + amount + " coins have been taken from your account"); } balances.put(p2.getName(), balance); saveUserBalance(p2.getName() ); } private void giveCoins(Player player, String[] split) { if (split.length != 3) { player.sendMessage( Colors.Green + "Usage: /givecoins - gives coins to a user from your account"); return; } Player p2 = etc.getServer().getPlayer( split[1]); if (p2 == null) { player.sendMessage( Colors.Green + "Givecoins: could not find player " + split[1]); return; } int amount; try { amount = Integer.parseInt(split[2]); } catch (Exception e) { player.sendMessage( Colors.Green + "Givecoins: amount must be a number"); return; } Integer source = balances.get(player.getName() ); if (source == null || source < amount) { player.sendMessage(Colors.Green + "Givecoins: you don't have that much to give"); return; } Integer destination = balances.get(p2.getName()); if (destination == null) { destination = 0; } balances.put(player.getName(), source - amount); balances.put(p2.getName(), destination+amount); saveUserBalance(player.getName() ); saveUserBalance(p2.getName() ); player.sendMessage(Colors.Green + "Givecoins, you have given " + amount + " coins to " + p2.getName() ); p2.sendMessage(Colors.Green + "Givecoins, you have received " + amount + " coins from " + player.getName() ); } private void balance(Player player) { Integer balObj = balances.get( player.getName() ); int bal = (balObj != null ? balObj : 0); player.sendMessage( Colors.Green + "Your balance is " + bal + " coins"); } private void rates(Player player) { player.sendMessage( Colors.Green + "Current exchange rates"); for(ExchangeRate r : rates.values() ) { player.sendMessage(Colors.Green + r.name + "(" + r.id + ") sell= " + r.sell + " buy=" + r.buy ); } } int getItemId(String str) { int item = -1; try { item = Integer.parseInt( str ); } catch (Exception e) { Integer itemId = itemMap.get( str.toLowerCase() ); if (itemId != null) { item = itemId; } } return item; } private boolean validateBuySell(Player player, String[] split) { if (split.length != 3) { player.sendMessage( Colors.Green + "Usage: " + split[0] + " "); return false; } int item = getItemId(split[1]); if (item == -1) { player.sendMessage( Colors.Green + split[0] + ": must be an integer or name of a traded item"); return false; } int amount; try { amount = Integer.parseInt(split[2]); } catch (Exception e) { player.sendMessage( Colors.Green + split[0] + ": amount must be an integer"); return false; } if (amount < 0 || amount > 1024) { player.sendMessage( Colors.Green + split[0] + ": amount must be between 0 and 1024"); return false; } ExchangeRate r = rates.get(item); if (r == null) { player.sendMessage( Colors.Green + split[0] + ": item with id " + item + " can not be traded"); return false; } return true; } private void buy(Player player, String[] split) { int item = getItemId(split[1]); int amount = Integer.parseInt(split[2]); ExchangeRate rate = rates.get(item); int value = rate.buy * amount; Integer balance = balances.get( player.getName() ); if (balance == null || balance < value) { player.sendMessage( Colors.Green + split[0] + ": you don't have enough coins"); return; } player.giveItem(item,amount); int newBalance = balance-value; balances.put(player.getName(), newBalance); player.sendMessage( Colors.Green + "You have bought " + amount + " x " + rate.name ); player.sendMessage( Colors.Green + "Your new balance is " + newBalance ); saveUserBalance(player.getName() ); } private boolean hasItem(Inventory inv, int itemID, int amount) { int count = 0; for (int i=0; i<36; i++) { Item item = inv.getItemFromSlot(i); if (item != null) { if (item.getItemId() == itemID) { count += item.getAmount(); } } } return (count >= amount); } private void sell(Player player, String[] split) { int itemId = getItemId( split[1] ); int amount = Integer.parseInt(split[2]); ExchangeRate rate = rates.get(itemId); Inventory inv = player.getInventory(); /* if (! inv.hasItem(itemId, amount, 1024) ) { player.sendMessage(Colors.Green + split[0] + ": you don't have that many pieces of " + rate.name ); return; } */ if (! hasItem(inv, itemId, amount) ) { player.sendMessage(Colors.Green + split[0] + ": you don't have that many pieces of " + rate.name ); return; } Integer balance = balances.get( player.getName() ); if (balance == null) { balance = 0; } Item item = new Item(itemId, amount); inv.removeItem(item); inv.updateInventory(); balance += (amount * rate.sell); balances.put( player.getName(), balance); player.sendMessage( Colors.Green + "You have sold " + amount + " x " + rate.name ); player.sendMessage( Colors.Green + "Your new balance is " + balance ); saveUserBalance(player.getName() ); } @Override public boolean onCommand(Player player, java.lang.String[] split) { if (split[0].equals("/balance") ) { balance(player); return true; } if (split[0].equals("/admincoins") && player.canUseCommand("/levelarea") ) { adminCoins(player,split); return true; } if (split[0].equals("/givecoins") ) { giveCoins(player,split); return true; } if (split[0].equals("/rates") ) { rates(player); return true; } if (split[0].equals("/sell") ) { if (validateBuySell(player,split)) { sell(player, split); } return true; } if (split[0].equals("/buy") ) { if (validateBuySell(player,split)) { buy(player, split); } return true; } if (split[0].equals("/reload") && player.canUseCommand("/levelarea") ) { loadExchangeRates(); } return false; } public boolean onConsoleCommand(String[] split) { if (split[0].equals("reload") ) { loadExchangeRates(); } return false; } } }