@Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { // TODO Auto-generated method stub // buyclaimblocks Player player = (sender instanceof Player) ? (Player) sender : null; if (player == null) return false; GriefPrevention inst = GriefPrevention.instance; if (command.getName().equalsIgnoreCase("buyclaimblocks") && player != null) { // if economy is disabled, don't do anything if (GriefPrevention.economy == null) { GriefPrevention.sendMessage(player, TextMode.Err, Messages.BuySellNotConfigured); return true; } if (!player.hasPermission("griefprevention.buysellclaimblocks")) { GriefPrevention.sendMessage(player, TextMode.Err, Messages.NoPermissionForCommand); return true; } // if purchase disabled, send error message if (GriefPrevention.instance.config_economy_claimBlocksPurchaseCost == 0) { GriefPrevention.sendMessage(player, TextMode.Err, Messages.OnlySellBlocks); return true; } // if no parameter, just tell player cost per block and balance if (args.length != 1) { GriefPrevention.sendMessage( player, TextMode.Info, Messages.BlockPurchaseCost, String.valueOf(GriefPrevention.instance.config_economy_claimBlocksPurchaseCost), String.valueOf(GriefPrevention.economy.getBalance(player.getName()))); return false; } else { // determine max purchasable blocks PlayerData playerData = inst.dataStore.getPlayerData(player.getName()); int maxPurchasable = GriefPrevention.instance.config_claims_maxAccruedBlocks - playerData.accruedClaimBlocks; // if the player is at his max, tell him so if (maxPurchasable <= 0) { GriefPrevention.sendMessage(player, TextMode.Err, Messages.ClaimBlockLimit); return true; } // try to parse number of blocks int blockCount; try { blockCount = Integer.parseInt(args[0]); } catch (NumberFormatException numberFormatException) { return false; // causes usage to be displayed } if (blockCount <= 0) { return false; } // correct block count to max allowed if (blockCount > maxPurchasable) { blockCount = maxPurchasable; } // if the player can't afford his purchase, send error // message double balance = inst.economy.getBalance(player.getName()); double totalCost = blockCount * GriefPrevention.instance.config_economy_claimBlocksPurchaseCost; if (totalCost > balance) { GriefPrevention.sendMessage( player, TextMode.Err, Messages.InsufficientFunds, String.valueOf(totalCost), String.valueOf(balance)); } // otherwise carry out transaction else { // withdraw cost inst.economy.withdrawPlayer(player.getName(), totalCost); // add blocks playerData.accruedClaimBlocks += blockCount; inst.dataStore.savePlayerData(player.getName(), playerData); // inform player GriefPrevention.sendMessage( player, TextMode.Success, Messages.PurchaseConfirmation, String.valueOf(totalCost), String.valueOf(playerData.getRemainingClaimBlocks())); } return true; } } // sellclaimblocks <amount> else if (command.getName().equalsIgnoreCase("sellclaimblocks") && player != null) { // if economy is disabled, don't do anything if (GriefPrevention.economy == null) { GriefPrevention.sendMessage(player, TextMode.Err, Messages.BuySellNotConfigured); return true; } if (!player.hasPermission("griefprevention.buysellclaimblocks")) { GriefPrevention.sendMessage(player, TextMode.Err, Messages.NoPermissionForCommand); return true; } // if disabled, error message if (GriefPrevention.instance.config_economy_claimBlocksSellValue == 0) { GriefPrevention.sendMessage(player, TextMode.Err, Messages.OnlyPurchaseBlocks); return true; } // load player data PlayerData playerData = inst.dataStore.getPlayerData(player.getName()); int availableBlocks = playerData.getRemainingClaimBlocks(); // if no amount provided, just tell player value per block sold, // and how many he can sell if (args.length != 1) { GriefPrevention.sendMessage( player, TextMode.Info, Messages.BlockSaleValue, String.valueOf(GriefPrevention.instance.config_economy_claimBlocksSellValue), String.valueOf(availableBlocks)); return false; } // parse number of blocks int blockCount; try { blockCount = Integer.parseInt(args[0]); } catch (NumberFormatException numberFormatException) { return false; // causes usage to be displayed } if (blockCount <= 0) { return false; } // if he doesn't have enough blocks, tell him so if (blockCount > availableBlocks) { GriefPrevention.sendMessage(player, TextMode.Err, Messages.NotEnoughBlocksForSale); } // otherwise carry out the transaction else { // compute value and deposit it double totalValue = blockCount * GriefPrevention.instance.config_economy_claimBlocksSellValue; inst.economy.depositPlayer(player.getName(), totalValue); // subtract blocks playerData.accruedClaimBlocks -= blockCount; inst.dataStore.savePlayerData(player.getName(), playerData); // inform player GriefPrevention.sendMessage( player, TextMode.Success, Messages.BlockSaleConfirmation, String.valueOf(totalValue), String.valueOf(playerData.getRemainingClaimBlocks())); } return true; } return false; }
@Override synchronized PlayerData getPlayerDataFromStorage(String playerName) { // if the file exists when we check for the specific casing, use that file. // On Windows machines the FS will not be case sensitive, however, for *nix based machines // the file systems and the file I/O API are case sensitive. We save data lowercase now // however previous installations may have upper-cased filenames. Thus we will // look for the filename for the file that it would be named if we create the path // with a case-insensitive player name. File CaseInsensitive = new File(getPlayerDataFile(playerName)); File playerFile; playerFile = CaseInsensitive; PlayerData playerData = new PlayerData(); playerData.playerName = playerName; // if it doesn't exist as a file if (!playerFile.exists()) { // create a file with defaults, but only if the player has been // online before. Player playerobj = Bukkit.getPlayer(playerName); if (playerobj == null) { this.savePlayerData(playerName, playerData); } else if (playerobj.hasPlayedBefore() || playerobj.isOnline()) { this.savePlayerData(playerName, playerData); } } // otherwise, read the file else { BufferedReader inStream = null; try { inStream = new BufferedReader(new FileReader(playerFile.getAbsolutePath())); // first line is last login timestamp String lastLoginTimestampString = inStream.readLine(); // convert that to a date and store it DateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss"); try { playerData.lastLogin = dateFormat.parse(lastLoginTimestampString); } catch (ParseException parseException) { GriefPrevention.AddLogEntry( "Unable to load last login for \"" + playerFile.getName() + "\"."); playerData.lastLogin = null; } // second line is accrued claim blocks String accruedBlocksString = inStream.readLine(); // convert that to a number and store it playerData.accruedClaimBlocks = Integer.parseInt(accruedBlocksString); // third line is any bonus claim blocks granted by // administrators String bonusBlocksString = inStream.readLine(); // convert that to a number and store it playerData.bonusClaimBlocks = Integer.parseInt(bonusBlocksString); // fourth line is a double-semicolon-delimited list of claims, // which is currently ignored try { inStream.readLine(); String playerinventoryclear = inStream.readLine(); playerData.ClearInventoryOnJoin = Boolean.parseBoolean(playerinventoryclear); } catch (Exception exx) { } // do nothing, seems like there was no value. Oh well. inStream.close(); } // if there's any problem with the file's content, log an error // message catch (Exception e) { GriefPrevention.AddLogEntry("Unable to load data for player \"" + playerName + "\": "); } try { if (inStream != null) inStream.close(); } catch (IOException exception) { } } return playerData; }