/[projects]/miscJava/minecraft-plugins/economyplugin/src/Economy.java
ViewVC logotype

Contents of /miscJava/minecraft-plugins/economyplugin/src/Economy.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1172 - (show annotations) (download)
Tue Oct 19 07:29:23 2010 UTC (13 years, 7 months ago) by torben
File size: 11139 byte(s)
very stupid bug - should of course check users access to the right command
1 import java.util.Map;
2 import java.util.HashMap;
3 import java.util.concurrent.ConcurrentHashMap;
4 import java.io.*;
5
6 public class Economy extends Plugin {
7 @Override
8 public void enable() {}
9
10 @Override
11 public void disable() {}
12
13 @Override
14 public void initialize() {
15 PluginListener plugin = new EconomyPluginListener();
16 PluginLoader loader = etc.getLoader();
17 loader.addListener( PluginLoader.Hook.COMMAND, plugin, this, PluginListener.Priority.MEDIUM );
18 loader.addListener( PluginLoader.Hook.SERVERCOMMAND, plugin, this, PluginListener.Priority.MEDIUM );
19 }
20
21 class ExchangeRate{
22 public ExchangeRate() {}
23 public ExchangeRate(int id, String name, int sell, int buy) {
24 this.id = id;
25 this.name = name;
26 this.sell = sell;
27 this.buy = buy;
28 }
29
30 int id; //blockid
31 String name;
32 int sell; //the amount the user receives when selling item
33 int buy;
34 }
35
36 public class EconomyPluginListener extends PluginListener {
37 final static String balanceFile = "economy-balance.txt";
38 final static String ratesFile = "economy-rates.txt";
39
40 private Map<String,Integer> itemMap = new HashMap<String,Integer>();
41 private Map<String, Integer> balances = new ConcurrentHashMap<String,Integer>();
42 private Map<Integer, ExchangeRate> rates = new HashMap<Integer,ExchangeRate>();
43
44 public EconomyPluginListener() {
45 loadExchangeRates();
46 loadUserBalances();
47 }
48
49 private void loadExchangeRates() {
50 try {
51 File f = new File(ratesFile);
52 if (f.exists() ) {
53 BufferedReader in = new BufferedReader(new InputStreamReader( new FileInputStream(f) ) );
54
55 rates.clear();
56 itemMap.clear();
57
58 String line;
59 while ( (line = in.readLine()) != null) {
60 line = line.trim();
61 if (line.length() == 0 || line.startsWith("#")) {
62 continue;
63 }
64
65 String parts[] = line.split(":");
66 ExchangeRate r = new ExchangeRate();
67 r.id = Integer.parseInt(parts[0]);
68 r.name = parts[1];
69 r.sell = Integer.parseInt(parts[2]);
70 r.buy = Integer.parseInt(parts[3]);
71
72 rates.put(r.id, r);
73
74 itemMap.put(r.name.toLowerCase(), r.id);
75
76 }
77 } else {
78 System.out.println("Could not find rates-file - creating new");
79 BufferedWriter out = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(f)) );
80
81 out.write("#itemID:name:sellrate:buyrate\n");
82 out.close();
83 }
84 } catch (Exception e) {
85 System.out.println("Error loading rates: " + e.getMessage() );
86 }
87 }
88
89 private void loadUserBalances() {
90 try {
91 File f = new File(balanceFile);
92 if (f.exists() ) {
93 BufferedReader in = new BufferedReader(new InputStreamReader( new FileInputStream(f) ) );
94
95 String line;
96 while ( (line = in.readLine()) != null) {
97 line = line.trim();
98 if (line.length() == 0 || line.startsWith("#")) {
99 continue;
100 }
101
102 String parts[] = line.split(":");
103 balances.put(parts[0], Integer.parseInt(parts[1]) );
104 }
105
106 in.close();
107 }
108 } catch (Exception e) {
109 System.out.println("Error loading balances: " + e.getMessage() );
110 }
111 }
112
113 private synchronized void saveUserBalance(String username) {
114 try {
115 FileOutputStream fos = new FileOutputStream(balanceFile);
116 BufferedWriter out = new BufferedWriter( new OutputStreamWriter(fos) );
117
118 for (String key : balances.keySet() ) {
119 out.write( key + ":" + balances.get(key) );
120 out.newLine();
121 }
122
123
124 out.close();
125 fos.close();
126 } catch (Exception e) {
127 System.out.println("Error saving user balance: " + e.getMessage() );
128 }
129
130 }
131
132
133 private void adminCoins(Player player, String[] split) {
134 if(split.length != 4 || !(split[1].equals("give") || split[1].equals("take") )) {
135 player.sendMessage( Colors.Green + "Usage: /admincoins (give|take) <player> <amount>");
136 return;
137 }
138
139 Player p2 = etc.getServer().getPlayer( split[2]);
140 if (p2 == null) {
141 player.sendMessage( Colors.Green + split[0] + ": could not find player " + split[2]);
142 return;
143 }
144
145 int amount;
146 try {
147 amount = Integer.parseInt(split[3]);
148 } catch (Exception e) {
149 player.sendMessage( Colors.Green + split[0] + ": amount must be a number");
150 return;
151 }
152
153 Integer balance = balances.get(p2.getName() );
154 if (balance == null) {
155 balance = 0;
156 }
157
158 if (split[1].equals("give")) {
159 balance += amount;
160
161 player.sendMessage(Colors.Green + split[0] + ": coins given to " + p2.getName() );
162 p2.sendMessage(Colors.Green + "You have been given " + amount + " coins");
163
164 } else { //take
165 if (amount > balance) {
166 player.sendMessage(Colors.Green + split[0] + ": " + p2.getName() + " doesnt have that many coins");
167 return;
168 }
169 balance -= amount;
170
171 player.sendMessage(Colors.Green + split[0] + ": coins taken from " + p2.getName() );
172 p2.sendMessage(Colors.Green + amount + " coins have been taken from your account");
173
174 }
175
176 balances.put(p2.getName(), balance);
177 saveUserBalance(p2.getName() );
178
179 }
180
181 private void giveCoins(Player player, String[] split) {
182 if (split.length != 3) {
183 player.sendMessage( Colors.Green + "Usage: /givecoins <player> <amount> - gives coins to a user from your account");
184 return;
185 }
186 Player p2 = etc.getServer().getPlayer( split[1]);
187 if (p2 == null) {
188 player.sendMessage( Colors.Green + "Givecoins: could not find player " + split[1]);
189 return;
190 }
191 int amount;
192 try {
193 amount = Integer.parseInt(split[2]);
194 } catch (Exception e) {
195 player.sendMessage( Colors.Green + "Givecoins: amount must be a number");
196 return;
197 }
198
199 Integer source = balances.get(player.getName() );
200 if (source == null || source < amount) {
201 player.sendMessage(Colors.Green + "Givecoins: you don't have that much to give");
202 return;
203 }
204
205 Integer destination = balances.get(p2.getName());
206 if (destination == null) {
207 destination = 0;
208 }
209
210 balances.put(player.getName(), source - amount);
211 balances.put(p2.getName(), destination+amount);
212
213 saveUserBalance(player.getName() );
214 saveUserBalance(p2.getName() );
215
216 player.sendMessage(Colors.Green + "Givecoins, you have given " + amount + " coins to " + p2.getName() );
217 p2.sendMessage(Colors.Green + "Givecoins, you have received " + amount + " coins from " + player.getName() );
218
219
220
221 }
222
223 private void balance(Player player) {
224 Integer balObj = balances.get( player.getName() );
225
226 int bal = (balObj != null ? balObj : 0);
227
228 player.sendMessage( Colors.Green + "Your balance is " + bal + " coins");
229 }
230
231 private void rates(Player player) {
232
233 player.sendMessage( Colors.Green + "Current exchange rates");
234
235 for(ExchangeRate r : rates.values() ) {
236 player.sendMessage(Colors.Green + r.name + "(" + r.id + ") sell= " + r.sell + " buy=" + r.buy );
237 }
238 }
239
240 int getItemId(String str) {
241 int item = -1;
242 try {
243 item = Integer.parseInt( str );
244 } catch (Exception e) {
245 Integer itemId = itemMap.get( str.toLowerCase() );
246 if (itemId != null) {
247 item = itemId;
248 }
249 }
250 return item;
251 }
252
253 private boolean validateBuySell(Player player, String[] split) {
254 if (split.length != 3) {
255 player.sendMessage( Colors.Green + "Usage: " + split[0] + " <item> <amount>");
256 return false;
257 }
258
259 int item = getItemId(split[1]);
260 if (item == -1) {
261 player.sendMessage( Colors.Green + split[0] + ": <item> must be an integer or name of a traded item");
262 return false;
263 }
264
265 int amount;
266 try {
267 amount = Integer.parseInt(split[2]);
268 } catch (Exception e) {
269 player.sendMessage( Colors.Green + split[0] + ": amount must be an integer");
270 return false;
271 }
272
273 if (amount < 0 || amount > 1024) {
274 player.sendMessage( Colors.Green + split[0] + ": amount must be between 0 and 1024");
275 return false;
276 }
277
278
279 ExchangeRate r = rates.get(item);
280 if (r == null) {
281 player.sendMessage( Colors.Green + split[0] + ": item with id " + item + " can not be traded");
282 return false;
283 }
284
285 return true;
286 }
287
288
289
290 private void buy(Player player, String[] split) {
291 int item = getItemId(split[1]);
292 int amount = Integer.parseInt(split[2]);
293
294 ExchangeRate rate = rates.get(item);
295
296 int value = rate.buy * amount;
297
298 Integer balance = balances.get( player.getName() );
299
300 if (balance == null || balance < value) {
301 player.sendMessage( Colors.Green + split[0] + ": you don't have enough coins");
302 return;
303 }
304
305 player.giveItem(item,amount);
306 int newBalance = balance-value;
307 balances.put(player.getName(), newBalance);
308
309
310 player.sendMessage( Colors.Green + "You have bought " + amount + " x " + rate.name );
311 player.sendMessage( Colors.Green + "Your new balance is " + newBalance );
312
313
314 saveUserBalance(player.getName() );
315 }
316
317 private boolean hasItem(Inventory inv, int itemID, int amount) {
318 int count = 0;
319 for (int i=0; i<36; i++) {
320 Item item = inv.getItemFromSlot(i);
321 if (item != null) {
322 if (item.getItemId() == itemID) {
323 count += item.getAmount();
324 }
325 }
326 }
327 return (count >= amount);
328
329 }
330
331 private void sell(Player player, String[] split) {
332 int itemId = getItemId( split[1] );
333 int amount = Integer.parseInt(split[2]);
334
335
336 ExchangeRate rate = rates.get(itemId);
337
338 Inventory inv = player.getInventory();
339
340 /* if (! inv.hasItem(itemId, amount, 1024) ) {
341 player.sendMessage(Colors.Green + split[0] + ": you don't have that many pieces of " + rate.name );
342 return;
343 }
344 */
345 if (! hasItem(inv, itemId, amount) ) {
346 player.sendMessage(Colors.Green + split[0] + ": you don't have that many pieces of " + rate.name );
347 return;
348 }
349
350
351 Integer balance = balances.get( player.getName() );
352 if (balance == null) {
353 balance = 0;
354 }
355
356 Item item = new Item(itemId, amount);
357 inv.removeItem(item);
358 inv.updateInventory();
359
360 balance += (amount * rate.sell);
361 balances.put( player.getName(), balance);
362
363 player.sendMessage( Colors.Green + "You have sold " + amount + " x " + rate.name );
364 player.sendMessage( Colors.Green + "Your new balance is " + balance );
365
366 saveUserBalance(player.getName() );
367 }
368
369
370
371 @Override
372 public boolean onCommand(Player player, java.lang.String[] split) {
373 if (split[0].equals("/balance") ) {
374 balance(player);
375 return true;
376 }
377
378
379 if (split[0].equals("/admincoins") && player.canUseCommand("/admincoins") ) {
380 adminCoins(player,split);
381 return true;
382 }
383
384 if (split[0].equals("/givecoins") ) {
385 giveCoins(player,split);
386 return true;
387 }
388
389 if (split[0].equals("/rates") ) {
390 rates(player);
391 return true;
392 }
393
394 if (split[0].equals("/sell") ) {
395 if (validateBuySell(player,split)) {
396 sell(player, split);
397 }
398 return true;
399 }
400
401 if (split[0].equals("/buy") ) {
402 if (validateBuySell(player,split)) {
403 buy(player, split);
404 }
405 return true;
406 }
407
408 if (split[0].equals("/reload") && player.canUseCommand("/reload") ) {
409 loadExchangeRates();
410 }
411
412
413 return false;
414
415 }
416
417 public boolean onConsoleCommand(String[] split) {
418 if (split[0].equals("reload") ) {
419 loadExchangeRates();
420 }
421 return false;
422 }
423 }
424 }

  ViewVC Help
Powered by ViewVC 1.1.20