Oh, so very much formatting and stuffs
This commit is contained in:
@@ -2,6 +2,7 @@ package com.drtshock.playervaults;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
@@ -23,14 +24,13 @@ import org.bukkit.event.player.PlayerInteractEvent;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
import org.bukkit.event.player.PlayerTeleportEvent;
|
||||
import org.bukkit.event.server.PluginDisableEvent;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
|
||||
import com.drtshock.playervaults.commands.VaultViewInfo;
|
||||
import com.drtshock.playervaults.util.DropOnDeath;
|
||||
import com.drtshock.playervaults.util.Lang;
|
||||
import com.drtshock.playervaults.util.VaultManager;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.server.PluginDisableEvent;
|
||||
|
||||
public class Listeners implements Listener {
|
||||
|
||||
@@ -39,6 +39,7 @@ public class Listeners implements Listener {
|
||||
public Listeners(PlayerVaults playerVaults) {
|
||||
this.plugin = playerVaults;
|
||||
}
|
||||
|
||||
VaultManager vm = new VaultManager(plugin);
|
||||
|
||||
/**
|
||||
@@ -53,7 +54,7 @@ public class Listeners implements Listener {
|
||||
VaultViewInfo info = PlayerVaults.IN_VAULT.get(p.getName());
|
||||
try {
|
||||
vm.saveVault(inv, info.getHolder(), info.getNumber());
|
||||
} catch (IOException e) {
|
||||
} catch(IOException e) {
|
||||
}
|
||||
PlayerVaults.OPENINVENTORIES.remove(info.toString());
|
||||
}
|
||||
@@ -69,8 +70,8 @@ public class Listeners implements Listener {
|
||||
*/
|
||||
@EventHandler
|
||||
public void onDisableEvent(PluginDisableEvent event) {
|
||||
for (Player p : Bukkit.getOnlinePlayers()) {
|
||||
if(PlayerVaults.IN_VAULT.containsKey(p.getName())) {
|
||||
for(Player p:Bukkit.getOnlinePlayers()) {
|
||||
if (PlayerVaults.IN_VAULT.containsKey(p.getName())) {
|
||||
p.closeInventory();
|
||||
}
|
||||
}
|
||||
@@ -90,7 +91,7 @@ public class Listeners implements Listener {
|
||||
@EventHandler
|
||||
public void onJoin(PlayerJoinEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
vm.playerVaultFile(player.getName());
|
||||
vm.getPlayerVaultFile(player.getName());
|
||||
if (player.isOp() && PlayerVaults.UPDATE) {
|
||||
player.sendMessage(ChatColor.GREEN + "Version " + PlayerVaults.NEWVERSION + " of PlayerVaults is up for download!");
|
||||
player.sendMessage(ChatColor.GREEN + PlayerVaults.LINK + " to view the changelog and download!");
|
||||
@@ -127,10 +128,7 @@ public class Listeners implements Listener {
|
||||
if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
|
||||
if (PlayerVaults.IN_VAULT.containsKey(player.getName())) {
|
||||
Block block = event.getClickedBlock();
|
||||
|
||||
/**
|
||||
* Different inventories that we don't want the player to open.
|
||||
*/
|
||||
// Different inventories that we don't want the player to open.
|
||||
if (block.getType() == Material.CHEST
|
||||
|| block.getType() == Material.ENDER_CHEST
|
||||
|| block.getType() == Material.FURNACE
|
||||
@@ -190,7 +188,8 @@ public class Listeners implements Listener {
|
||||
owner = PlayerVaults.SIGNS.getString(world + ";;" + x + ";;" + y + ";;" + z + ".owner");
|
||||
}
|
||||
int num = PlayerVaults.SIGNS.getInt(world + ";;" + x + ";;" + y + ";;" + z + ".chest");
|
||||
PlayerVaults.VM.loadVault(player, (self) ? player.getName() : owner, num);
|
||||
Inventory inv = PlayerVaults.VM.loadVault((self) ? player.getName() : owner, num);
|
||||
player.openInventory(inv);
|
||||
PlayerVaults.IN_VAULT.put(player.getName(), new VaultViewInfo((self) ? player.getName() : owner, num));
|
||||
event.setCancelled(true);
|
||||
player.sendMessage(Lang.TITLE.toString() + Lang.OPEN_WITH_SIGN.toString().replace("%v", String.valueOf(num)).replace("%p", (self) ? player.getName() : owner));
|
||||
@@ -217,11 +216,15 @@ public class Listeners implements Listener {
|
||||
blockChangeCheck(event.getBlock().getLocation());
|
||||
}
|
||||
|
||||
public void blockChangeCheck(Location l) {
|
||||
String world = l.getWorld().getName();
|
||||
int x = l.getBlockX();
|
||||
int y = l.getBlockY();
|
||||
int z = l.getBlockZ();
|
||||
/**
|
||||
* Check if the location given is a sign, and if so, remove it from the signs.yml file
|
||||
* @param location The location to check
|
||||
*/
|
||||
public void blockChangeCheck(Location location) {
|
||||
String world = location.getWorld().getName();
|
||||
int x = location.getBlockX();
|
||||
int y = location.getBlockY();
|
||||
int z = location.getBlockZ();
|
||||
if (plugin.getSigns().getKeys(false).contains(world + ";;" + x + ";;" + y + ";;" + z)) {
|
||||
plugin.getSigns().set(world + ";;" + x + ";;" + y + ";;" + z, null);
|
||||
plugin.saveSigns();
|
||||
@@ -229,8 +232,7 @@ public class Listeners implements Listener {
|
||||
}
|
||||
|
||||
/**
|
||||
* Don't let a player open a trading inventory OR a minecart while he has
|
||||
* his vault open.
|
||||
* Don't let a player open a trading inventory OR a minecart while he has his vault open.
|
||||
*/
|
||||
@EventHandler
|
||||
public void onInteractEntity(PlayerInteractEntityEvent event) {
|
||||
|
||||
@@ -53,9 +53,9 @@ public class PlayerVaults extends JavaPlugin {
|
||||
loadSigns();
|
||||
startMetrics();
|
||||
Updater u = new Updater();
|
||||
if(getConfig().getBoolean("check-update")) {
|
||||
if (getConfig().getBoolean("check-update")) {
|
||||
try {
|
||||
if(u.getUpdate(getDescription().getVersion())) {
|
||||
if (u.getUpdate(getDescription().getVersion())) {
|
||||
UPDATE = true;
|
||||
}
|
||||
} catch(IOException e) {
|
||||
@@ -72,7 +72,7 @@ public class PlayerVaults extends JavaPlugin {
|
||||
getCommand("workbench").setExecutor(commands);
|
||||
setupEconomy();
|
||||
|
||||
if(getConfig().getBoolean("drop-on-death.enabled")) {
|
||||
if (getConfig().getBoolean("drop-on-death.enabled")) {
|
||||
DROP_ON_DEATH = true;
|
||||
INVENTORIES_TO_DROP = getConfig().getInt("drop-on-death.inventories");
|
||||
}
|
||||
@@ -81,6 +81,9 @@ public class PlayerVaults extends JavaPlugin {
|
||||
VM = new VaultManager(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Start metrics
|
||||
*/
|
||||
public void startMetrics() {
|
||||
try {
|
||||
Metrics metrics = new Metrics(this);
|
||||
@@ -90,12 +93,17 @@ public class PlayerVaults extends JavaPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup economy
|
||||
*
|
||||
* @return Whether or not economy exists.
|
||||
*/
|
||||
private boolean setupEconomy() {
|
||||
if(getServer().getPluginManager().getPlugin("Vault") == null) {
|
||||
if (getServer().getPluginManager().getPlugin("Vault") == null) {
|
||||
return false;
|
||||
}
|
||||
RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
|
||||
if(rsp == null) {
|
||||
if (rsp == null) {
|
||||
return false;
|
||||
}
|
||||
ECON = rsp.getProvider();
|
||||
@@ -103,18 +111,24 @@ public class PlayerVaults extends JavaPlugin {
|
||||
return ECON != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the config.yml file.
|
||||
*/
|
||||
public void loadConfig() {
|
||||
File config = new File(getDataFolder() + File.separator + "config.yml");
|
||||
if(!config.exists()) {
|
||||
if (!config.exists()) {
|
||||
saveDefaultConfig();
|
||||
} else {
|
||||
updateConfig();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the signs.yml file.
|
||||
*/
|
||||
public void loadSigns() {
|
||||
File signs = new File(getDataFolder(), "signs.yml");
|
||||
if(!signs.exists()) {
|
||||
if (!signs.exists()) {
|
||||
try {
|
||||
signs.createNewFile();
|
||||
} catch(IOException e) {
|
||||
@@ -127,10 +141,18 @@ public class PlayerVaults extends JavaPlugin {
|
||||
PlayerVaults.SIGNS = YamlConfiguration.loadConfiguration(signs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the signs.yml config.
|
||||
*
|
||||
* @return The signs.yml config.
|
||||
*/
|
||||
public YamlConfiguration getSigns() {
|
||||
return PlayerVaults.SIGNS;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the signs.yml file.
|
||||
*/
|
||||
public void saveSigns() {
|
||||
try {
|
||||
PlayerVaults.SIGNS.save(PlayerVaults.SIGNS_FILE);
|
||||
@@ -141,6 +163,9 @@ public class PlayerVaults extends JavaPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the config.yml file.
|
||||
*/
|
||||
public void updateConfig() {
|
||||
boolean checkUpdate = getConfig().getBoolean("check-update", true);
|
||||
boolean ecoEnabled = getConfig().getBoolean("economy.enabled", false);
|
||||
@@ -166,18 +191,28 @@ public class PlayerVaults extends JavaPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set an object in the config.yml
|
||||
* @param path The path in the config.
|
||||
* @param object What to be saved.
|
||||
* @param conf Where to save the object.
|
||||
*/
|
||||
public <T> void setInConfig(String path, T object, YamlConfiguration conf) {
|
||||
conf.set(path, object);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the lang.yml file.
|
||||
* @return The lang.yml config.
|
||||
*/
|
||||
public YamlConfiguration loadLang() {
|
||||
File lang = new File(getDataFolder(), "lang.yml");
|
||||
if(!lang.exists()) {
|
||||
if (!lang.exists()) {
|
||||
try {
|
||||
getDataFolder().mkdir();
|
||||
lang.createNewFile();
|
||||
InputStream defConfigStream = this.getResource("lang.yml");
|
||||
if(defConfigStream != null) {
|
||||
if (defConfigStream != null) {
|
||||
YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
|
||||
defConfig.save(lang);
|
||||
Lang.setFile(defConfig);
|
||||
@@ -197,10 +232,18 @@ public class PlayerVaults extends JavaPlugin {
|
||||
return conf;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the lang.yml config.
|
||||
* @return The lang.yml config.
|
||||
*/
|
||||
public YamlConfiguration getLang() {
|
||||
return LANG;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the lang.yml file.
|
||||
* @return The lang.yml file.
|
||||
*/
|
||||
public File getLangFile() {
|
||||
return LANG_FILE;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.drtshock.playervaults.commands;
|
||||
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.Command;
|
||||
@@ -17,27 +16,28 @@ public class Commands implements CommandExecutor {
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
|
||||
if(cmd.getName().equalsIgnoreCase("pv")) {
|
||||
if(sender instanceof Player) {
|
||||
if (cmd.getName().equalsIgnoreCase("pv")) {
|
||||
if (sender instanceof Player) {
|
||||
Player p = (Player) sender;
|
||||
switch (args.length) {
|
||||
case 1:
|
||||
if(VaultOperations.openOwnVault(p, args[0]))
|
||||
if (VaultOperations.openOwnVault(p, args[0]))
|
||||
PlayerVaults.IN_VAULT.put(sender.getName(), new VaultViewInfo(sender.getName(), Integer.parseInt(args[0])));
|
||||
break;
|
||||
case 2:
|
||||
if(VaultOperations.openOtherVault(p, args[0], args[1]))
|
||||
if (VaultOperations.openOtherVault(p, args[0], args[1]))
|
||||
PlayerVaults.IN_VAULT.put(sender.getName(), new VaultViewInfo(args[0], Integer.parseInt(args[1])));
|
||||
break;
|
||||
default:
|
||||
Feedback.showHelp(sender, Feedback.Type.OPEN);
|
||||
sender.sendMessage(Lang.TITLE + "/pv <number>");
|
||||
sender.sendMessage(Lang.TITLE + "/pv <player> <number>");
|
||||
}
|
||||
}
|
||||
else sender.sendMessage(Lang.TITLE.toString() + ChatColor.RED + Lang.PLAYER_ONLY);
|
||||
} else if(cmd.getName().equalsIgnoreCase("pvdel")) {
|
||||
} else if (cmd.getName().equalsIgnoreCase("pvdel")) {
|
||||
switch (args.length) {
|
||||
case 1:
|
||||
if(sender instanceof Player) {
|
||||
if (sender instanceof Player) {
|
||||
Player p = (Player) sender;
|
||||
VaultOperations.deleteOwnVault(p, args[0]);
|
||||
}
|
||||
@@ -49,11 +49,12 @@ public class Commands implements CommandExecutor {
|
||||
VaultOperations.deleteOtherVault(sender, args[0], args[1]);
|
||||
break;
|
||||
default:
|
||||
Feedback.showHelp(sender, Feedback.Type.DELETE);
|
||||
sender.sendMessage(Lang.TITLE + "/pvdel <number>");
|
||||
sender.sendMessage(Lang.TITLE + "/pvdel <player> <number>");
|
||||
}
|
||||
} else if(cmd.getName().equalsIgnoreCase("workbench")) {
|
||||
if(sender.hasPermission("playervaults.workbench")) {
|
||||
if(sender instanceof Player) {
|
||||
} else if (cmd.getName().equalsIgnoreCase("workbench")) {
|
||||
if (sender.hasPermission("playervaults.workbench")) {
|
||||
if (sender instanceof Player) {
|
||||
Inventory workbench = Bukkit.createInventory(null, InventoryType.WORKBENCH);
|
||||
((Player) sender).openInventory(workbench);
|
||||
sender.sendMessage(Lang.TITLE.toString() + Lang.OPEN_WORKBENCH);
|
||||
@@ -63,10 +64,10 @@ public class Commands implements CommandExecutor {
|
||||
} else {
|
||||
sender.sendMessage(Lang.TITLE.toString() + Lang.NO_PERMS);
|
||||
}
|
||||
} else if(cmd.getName().equalsIgnoreCase("pvsign")) {
|
||||
if(sender.hasPermission("playervaults.signs.set")) {
|
||||
if(sender instanceof Player) {
|
||||
if(args.length == 1) {
|
||||
} else if (cmd.getName().equalsIgnoreCase("pvsign")) {
|
||||
if (sender.hasPermission("playervaults.signs.set")) {
|
||||
if (sender instanceof Player) {
|
||||
if (args.length == 1) {
|
||||
int i = 0;
|
||||
try {
|
||||
i = Integer.parseInt(args[0]);
|
||||
@@ -78,7 +79,7 @@ public class Commands implements CommandExecutor {
|
||||
PlayerVaults.SET_SIGN.put(sender.getName(), new SignSetInfo(i));
|
||||
sender.sendMessage(Lang.TITLE.toString() + Lang.CLICK_A_SIGN);
|
||||
}
|
||||
else if(args.length >= 2) {
|
||||
else if (args.length >= 2) {
|
||||
int i = 0;
|
||||
try {
|
||||
i = Integer.parseInt(args[1]);
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
package com.drtshock.playervaults.commands;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import com.drtshock.playervaults.util.Lang;
|
||||
|
||||
public class Feedback {
|
||||
public enum Type {
|
||||
OPEN,
|
||||
DELETE,
|
||||
WORKBENCH, // For later versions
|
||||
FURNACE;
|
||||
}
|
||||
|
||||
public static void noPerms(CommandSender sender) {
|
||||
sender.sendMessage(Lang.TITLE + "" + Lang.NO_PERMS);
|
||||
return;
|
||||
}
|
||||
|
||||
public static void showHelp(CommandSender sender, Type t) {
|
||||
if(t == Type.OPEN) {
|
||||
sender.sendMessage(Lang.TITLE + "/pv <number>");
|
||||
sender.sendMessage(Lang.TITLE + "/pv <player> <number>");
|
||||
}
|
||||
else if(t == Type.DELETE) {
|
||||
sender.sendMessage(Lang.TITLE + "/pvdel <number>");
|
||||
sender.sendMessage(Lang.TITLE + "/pvdel <player> <number>");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,31 +1,56 @@
|
||||
package com.drtshock.playervaults.commands;
|
||||
|
||||
/**
|
||||
* A class for setting signs. Stores information about the sign owner, number,
|
||||
* and whether or not is opens a self vault or another person's vault.
|
||||
*/
|
||||
public class SignSetInfo {
|
||||
|
||||
private String o;
|
||||
private int i;
|
||||
private String owner;
|
||||
private int number;
|
||||
private boolean self = false;
|
||||
|
||||
public SignSetInfo(String o, int i) {
|
||||
this.o = o;
|
||||
this.i = i;
|
||||
/**
|
||||
* Construct a SignSetInfo object for another person.
|
||||
* @param s The vault owner.
|
||||
* @param i The vault number.
|
||||
*/
|
||||
public SignSetInfo(String s, int i) {
|
||||
this.owner = s;
|
||||
this.number = i;
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a SignSetInfo object for opening to self.
|
||||
* @param i The vault number.
|
||||
*/
|
||||
public SignSetInfo(int i) {
|
||||
this.i = i;
|
||||
this.number = i;
|
||||
this.self = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get whether or not the sign will open their own vault or another person's.
|
||||
* @return Whether or not it is a 'self' sign.
|
||||
*/
|
||||
public boolean isSelf() {
|
||||
return this.self;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the owner of the vault.
|
||||
* @return The owner of the vault.
|
||||
*/
|
||||
public String getOwner() {
|
||||
return this.o;
|
||||
return this.owner;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the vault number.
|
||||
* @return The vault number.
|
||||
*/
|
||||
public int getChest() {
|
||||
return this.i;
|
||||
return this.number;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import java.io.IOException;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
|
||||
import com.drtshock.playervaults.PlayerVaults;
|
||||
import com.drtshock.playervaults.util.EconomyOperations;
|
||||
@@ -12,101 +13,107 @@ import com.drtshock.playervaults.util.Lang;
|
||||
|
||||
public class VaultOperations {
|
||||
|
||||
public static boolean checkPerms(CommandSender cs, int number) {
|
||||
if(cs.hasPermission("playervaults.amount." + String.valueOf(number))) return true;
|
||||
/**
|
||||
* Check whether or not the player has permission to open the requested vault.
|
||||
* @param sender The person to check.
|
||||
* @param number The vault number.
|
||||
* @return Whether or not they have permission.
|
||||
*/
|
||||
public static boolean checkPerms(CommandSender sender, int number) {
|
||||
if (sender.hasPermission("playervaults.amount." + String.valueOf(number))) return true;
|
||||
for(int x = number; x <= 99; x++) {
|
||||
if(cs.hasPermission("playervaults.amount." + String.valueOf(x))) return true;
|
||||
if (sender.hasPermission("playervaults.amount." + String.valueOf(x))) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Open a player's own vault.
|
||||
* Return true if allowed. Otherwise false.
|
||||
* @param Sender sender
|
||||
* @param String arg
|
||||
* @return success
|
||||
* @param player The player to open to.
|
||||
* @param arg The vault number to open.
|
||||
* @return Whether or not the player was allowed to open it.
|
||||
*/
|
||||
public static boolean openOwnVault(Player sender, String arg) {
|
||||
if(arg.matches("^[0-9]{1,2}$")) {
|
||||
public static boolean openOwnVault(Player player, String arg) {
|
||||
if (arg.matches("^[0-9]{1,2}$")) {
|
||||
int number = 0;
|
||||
try {
|
||||
number = Integer.parseInt(arg);
|
||||
if(number == 0)
|
||||
if (number == 0)
|
||||
return false;
|
||||
} catch(NumberFormatException nfe) {
|
||||
sender.sendMessage(Lang.TITLE.toString() + ChatColor.RED + Lang.MUST_BE_NUMBER);
|
||||
player.sendMessage(Lang.TITLE.toString() + ChatColor.RED + Lang.MUST_BE_NUMBER);
|
||||
return false;
|
||||
}
|
||||
if(checkPerms(sender, number)) {
|
||||
if(EconomyOperations.payToOpen(sender)) {
|
||||
PlayerVaults.VM.loadVault(sender, sender.getName(), number);
|
||||
sender.sendMessage(Lang.TITLE.toString() + Lang.OPEN_VAULT.toString().replace("%v", arg));
|
||||
if (checkPerms(player, number)) {
|
||||
if (EconomyOperations.payToOpen(player)) {
|
||||
Inventory inv = PlayerVaults.VM.loadVault(player.getName(), number);
|
||||
player.openInventory(inv);
|
||||
player.sendMessage(Lang.TITLE.toString() + Lang.OPEN_VAULT.toString().replace("%v", arg));
|
||||
return true;
|
||||
} else {
|
||||
sender.sendMessage(Lang.TITLE.toString() + Lang.INSUFFICIENT_FUNDS);
|
||||
player.sendMessage(Lang.TITLE.toString() + Lang.INSUFFICIENT_FUNDS);
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
Feedback.noPerms(sender);
|
||||
player.sendMessage(Lang.TITLE.toString() + Lang.NO_PERMS);
|
||||
}
|
||||
} else {
|
||||
sender.sendMessage(Lang.TITLE.toString() + Lang.MUST_BE_NUMBER);
|
||||
player.sendMessage(Lang.TITLE.toString() + Lang.MUST_BE_NUMBER);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Open another player's vault.
|
||||
* Return true if allowed. Otherwise false.
|
||||
* @param Sender sender
|
||||
* @param String user
|
||||
* @param String arg
|
||||
* @return success
|
||||
* @param player The player to open to.
|
||||
* @param user The user to whom the requested vault belongs.
|
||||
* @param arg The vault number to open.
|
||||
* @return Whether or not the player was allowed to open it.
|
||||
*/
|
||||
public static boolean openOtherVault(Player sender, String user, String arg) {
|
||||
if(sender.hasPermission("playervaults.admin")) {
|
||||
if(arg.matches("^[0-9]{1,2}$")) {
|
||||
public static boolean openOtherVault(Player player, String user, String arg) {
|
||||
if (player.hasPermission("playervaults.admin")) {
|
||||
if (arg.matches("^[0-9]{1,2}$")) {
|
||||
int number = 0;
|
||||
try {
|
||||
number = Integer.parseInt(arg);
|
||||
if(number == 0)
|
||||
if (number == 0)
|
||||
return false;
|
||||
} catch(NumberFormatException nfe) {
|
||||
sender.sendMessage(Lang.TITLE.toString() + ChatColor.RED + Lang.MUST_BE_NUMBER);
|
||||
player.sendMessage(Lang.TITLE.toString() + ChatColor.RED + Lang.MUST_BE_NUMBER);
|
||||
}
|
||||
PlayerVaults.VM.loadVault(sender, user, number);
|
||||
sender.sendMessage(Lang.TITLE.toString() + Lang.OPEN_OTHER_VAULT.toString().replace("%v", arg).replace("%p", user));
|
||||
Inventory inv = PlayerVaults.VM.loadVault(user, number);
|
||||
player.openInventory(inv);
|
||||
player.sendMessage(Lang.TITLE.toString() + Lang.OPEN_OTHER_VAULT.toString().replace("%v", arg).replace("%p", user));
|
||||
return true;
|
||||
} else {
|
||||
sender.sendMessage(Lang.TITLE.toString() + Lang.MUST_BE_NUMBER);
|
||||
player.sendMessage(Lang.TITLE.toString() + Lang.MUST_BE_NUMBER);
|
||||
}
|
||||
}
|
||||
else {
|
||||
Feedback.noPerms(sender);
|
||||
player.sendMessage(Lang.TITLE.toString() + Lang.NO_PERMS);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a player's own vault.
|
||||
* Return true if allowed. Otherwise false.
|
||||
* @param Sender sender
|
||||
* @param String arg
|
||||
* @param player The player to delete.
|
||||
* @param user The user to whom the deleted vault belongs.
|
||||
* @param arg The vault number to delete.
|
||||
*/
|
||||
public static void deleteOwnVault(Player sender, String arg) {
|
||||
if(arg.matches("^[0-9]{1,2}$")) {
|
||||
if (arg.matches("^[0-9]{1,2}$")) {
|
||||
int number = 0;
|
||||
try {
|
||||
number = Integer.parseInt(arg);
|
||||
if(number == 0)
|
||||
if (number == 0)
|
||||
sender.sendMessage(Lang.TITLE.toString() + ChatColor.RED + Lang.MUST_BE_NUMBER);
|
||||
return;
|
||||
} catch(NumberFormatException nfe) {
|
||||
sender.sendMessage(Lang.TITLE.toString() + ChatColor.RED + Lang.MUST_BE_NUMBER);
|
||||
}
|
||||
try {
|
||||
if(EconomyOperations.refundOnDelete(sender, number)) {
|
||||
if (EconomyOperations.refundOnDelete(sender, number)) {
|
||||
PlayerVaults.VM.deleteVault(sender, sender.getName(), number);
|
||||
return;
|
||||
}
|
||||
@@ -119,19 +126,18 @@ public class VaultOperations {
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete another player's vault.
|
||||
* Return true if allowed. Otherwise false.
|
||||
* @param Sender sender
|
||||
* @param String user
|
||||
* @param String arg
|
||||
* Delete a player's own vault.
|
||||
* @param player The player to delete.
|
||||
* @param user The user to whom the deleted vault belongs.
|
||||
* @param arg The vault number to delete.
|
||||
*/
|
||||
public static void deleteOtherVault(CommandSender sender, String user, String arg) {
|
||||
if(sender.hasPermission("playervaults.delete")) {
|
||||
if(arg.matches("^[0-9]{1,2}$")) {
|
||||
if (sender.hasPermission("playervaults.delete")) {
|
||||
if (arg.matches("^[0-9]{1,2}$")) {
|
||||
int number = 0;
|
||||
try {
|
||||
number = Integer.parseInt(arg);
|
||||
if(number == 0) {
|
||||
if (number == 0) {
|
||||
sender.sendMessage(Lang.TITLE.toString() + ChatColor.RED + Lang.MUST_BE_NUMBER);
|
||||
return;
|
||||
}
|
||||
@@ -147,7 +153,9 @@ public class VaultOperations {
|
||||
sender.sendMessage(Lang.TITLE.toString() + Lang.MUST_BE_NUMBER);
|
||||
}
|
||||
}
|
||||
else Feedback.noPerms(sender);
|
||||
else {
|
||||
sender.sendMessage(Lang.TITLE.toString() + Lang.NO_PERMS);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,26 +1,43 @@
|
||||
package com.drtshock.playervaults.commands;
|
||||
|
||||
/**
|
||||
* A class that stores information about a vault viewing including the holder of the vault,
|
||||
* and the vault number.
|
||||
*/
|
||||
public class VaultViewInfo {
|
||||
|
||||
String s;
|
||||
int i;
|
||||
String holder;
|
||||
int number;
|
||||
|
||||
/**
|
||||
* Make a VaultViewObject
|
||||
* @param s The holder of the vault.
|
||||
* @param i The vault number.
|
||||
*/
|
||||
public VaultViewInfo(String s, int i) {
|
||||
this.s = s;
|
||||
this.i = i;
|
||||
this.holder = s;
|
||||
this.number = i;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the holder of the vault.
|
||||
* @return The holder of the vault.
|
||||
*/
|
||||
public String getHolder() {
|
||||
return this.s;
|
||||
return this.holder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the vault number.
|
||||
* @return The vault number.
|
||||
*/
|
||||
public int getNumber() {
|
||||
return this.i;
|
||||
return this.number;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.s + " " + this.i;
|
||||
return this.holder + " " + this.number;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -7,25 +7,19 @@ import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import com.drtshock.playervaults.PlayerVaults;
|
||||
|
||||
/**
|
||||
* A class that contains a method to drop the contents of a player's vault when they die.
|
||||
*/
|
||||
public class DropOnDeath {
|
||||
|
||||
public static PlayerVaults PLUGIN;
|
||||
|
||||
public DropOnDeath(PlayerVaults instance) {
|
||||
DropOnDeath.PLUGIN = instance;
|
||||
}
|
||||
|
||||
static VaultManager VAULT_MANAGER = new VaultManager(PLUGIN);
|
||||
|
||||
/**
|
||||
* Drops all items when a player dies.
|
||||
* @param player
|
||||
* @param player The player to drop the inventory of.
|
||||
*/
|
||||
public static void drop(Player player) {
|
||||
Location loc = player.getLocation();
|
||||
|
||||
for(int count = 1; count <= PlayerVaults.INVENTORIES_TO_DROP; count++) {
|
||||
Inventory inv = VAULT_MANAGER.getVault(player, count);
|
||||
Inventory inv = PlayerVaults.VM.getVault(player.getName(), count);
|
||||
ItemStack[] stack = inv.getContents();
|
||||
for(ItemStack is:stack) {
|
||||
loc.getWorld().dropItemNaturally(loc, is);
|
||||
|
||||
@@ -13,6 +13,9 @@ import org.bukkit.entity.Player;
|
||||
|
||||
import com.drtshock.playervaults.PlayerVaults;
|
||||
|
||||
/**
|
||||
* A class that handles all economy operations.
|
||||
*/
|
||||
public class EconomyOperations {
|
||||
|
||||
private static File CONFIG_FILE;
|
||||
@@ -28,17 +31,16 @@ public class EconomyOperations {
|
||||
|
||||
/**
|
||||
* Have a player pay to open a vault.
|
||||
* Returns true if successful. Otherwise false.
|
||||
* @param player
|
||||
* @return transaction success
|
||||
* @param player The player to pay.
|
||||
* @return The transaction success.
|
||||
*/
|
||||
public static boolean payToOpen(Player player) {
|
||||
if(!BUKKIT_CONFIG.getBoolean("economy.enabled") || player.hasPermission("playervaults.free") || !PlayerVaults.USE_VAULT)
|
||||
if (!BUKKIT_CONFIG.getBoolean("economy.enabled") || player.hasPermission("playervaults.free") || !PlayerVaults.USE_VAULT)
|
||||
return true;
|
||||
|
||||
double cost = BUKKIT_CONFIG.getDouble("economy.cost-to-open", 10);
|
||||
EconomyResponse resp = PlayerVaults.ECON.withdrawPlayer(player.getName(), cost);
|
||||
if(resp.transactionSuccess()) {
|
||||
if (resp.transactionSuccess()) {
|
||||
player.sendMessage(Lang.TITLE.toString() + Lang.COST_TO_OPEN.toString().replaceAll("%price", "" + cost));
|
||||
return true;
|
||||
}
|
||||
@@ -48,17 +50,16 @@ public class EconomyOperations {
|
||||
|
||||
/**
|
||||
* Have a player pay to create a vault.
|
||||
* Returns true if successful. Otherwise false.
|
||||
* @param player
|
||||
* @return transaction success
|
||||
* @param player The player to pay.
|
||||
* @return The transaction success
|
||||
*/
|
||||
public static boolean payToCreate(Player player) {
|
||||
if(!BUKKIT_CONFIG.getBoolean("economy.enabled") || player.hasPermission("playervaults.free") || !PlayerVaults.USE_VAULT)
|
||||
if (!BUKKIT_CONFIG.getBoolean("economy.enabled") || player.hasPermission("playervaults.free") || !PlayerVaults.USE_VAULT)
|
||||
return true;
|
||||
|
||||
double cost = BUKKIT_CONFIG.getDouble("economy.cost-to-create", 100);
|
||||
EconomyResponse resp = PlayerVaults.ECON.withdrawPlayer(player.getName(), cost);
|
||||
if(resp.transactionSuccess()) {
|
||||
if (resp.transactionSuccess()) {
|
||||
player.sendMessage(Lang.TITLE.toString() + Lang.COST_TO_CREATE.toString().replaceAll("%price", "" + cost));
|
||||
return true;
|
||||
}
|
||||
@@ -68,20 +69,19 @@ public class EconomyOperations {
|
||||
|
||||
/**
|
||||
* Have a player get his money back when vault is deleted.
|
||||
* Returns true if successful. Otherwise false.
|
||||
* @param player
|
||||
* @return transaction success.
|
||||
* @param player The player to receive the money.
|
||||
* @return The transaction success.
|
||||
*/
|
||||
public static boolean refundOnDelete(Player player, int number) {
|
||||
String directory = "plugins" + File.separator + "PlayerVaults" + File.separator + "vaults";
|
||||
|
||||
if(!BUKKIT_CONFIG.getBoolean("economy.enabled") || player.hasPermission("playervaults.free") || !PlayerVaults.USE_VAULT)
|
||||
if (!BUKKIT_CONFIG.getBoolean("economy.enabled") || player.hasPermission("playervaults.free") || !PlayerVaults.USE_VAULT) {
|
||||
return true;
|
||||
}
|
||||
String name = player.getName().toLowerCase();
|
||||
File file = new File(directory + File.separator + name.toLowerCase() + ".yml");
|
||||
YamlConfiguration playerFile = YamlConfiguration.loadConfiguration(file);
|
||||
if(file.exists()) {
|
||||
if(playerFile.getString("vault" + number) == null) {
|
||||
if (file.exists()) {
|
||||
if (playerFile.getString("vault" + number) == null) {
|
||||
player.sendMessage(Lang.TITLE.toString() + ChatColor.RED + Lang.VAULT_DOES_NOT_EXIST);
|
||||
return false;
|
||||
}
|
||||
@@ -92,7 +92,7 @@ public class EconomyOperations {
|
||||
}
|
||||
double cost = BUKKIT_CONFIG.getDouble("economy.refund-on-delete");
|
||||
EconomyResponse resp = PlayerVaults.ECON.depositPlayer(player.getName(), cost);
|
||||
if(resp.transactionSuccess()) {
|
||||
if (resp.transactionSuccess()) {
|
||||
player.sendMessage(Lang.TITLE.toString() + Lang.REFUND_AMOUNT.toString().replaceAll("%price", String.valueOf(cost)));
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -3,6 +3,9 @@ package com.drtshock.playervaults.util;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
/**
|
||||
* An enum for requesting strings from the language file.
|
||||
*/
|
||||
public enum Lang {
|
||||
TITLE("title-name", "&4[&fPlayerVaults&4]:"),
|
||||
OPEN_VAULT("open-vault", "&fOpening vault &a%v"),
|
||||
@@ -21,34 +24,51 @@ public enum Lang {
|
||||
COST_TO_OPEN("cost-to-open", "&fYou were charged &c%price &ffor opening that vault."),
|
||||
VAULT_DOES_NOT_EXIST("vault-does-not-exist", "&cThat vault does not exist!"),
|
||||
CLICK_A_SIGN("click-a-sign", "&fNow click a sign!"),
|
||||
NOT_A_SIGN("not-a-sign","&cYou must click a sign!"),
|
||||
NOT_A_SIGN("not-a-sign", "&cYou must click a sign!"),
|
||||
SET_SIGN("set-sign-success", "&fYou have successfully set a PlayerVault access sign!"),
|
||||
OPEN_WITH_SIGN("open-with-sign", "&fOpening vault &a%v &fof &a%p");
|
||||
|
||||
private String path;
|
||||
private String def; // Default string
|
||||
private String def;
|
||||
private static YamlConfiguration LANG;
|
||||
|
||||
/**
|
||||
* Lang enum constructor.
|
||||
* @param path The string path.
|
||||
* @param start The default string.
|
||||
*/
|
||||
Lang(String path, String start) {
|
||||
this.path = path;
|
||||
this.def = start;
|
||||
}
|
||||
|
||||
public static void setFile(YamlConfiguration yc) {
|
||||
LANG = yc;
|
||||
/**
|
||||
* Set the {@code YamlConfiguration} to use.
|
||||
* @param config The config to set.
|
||||
*/
|
||||
public static void setFile(YamlConfiguration config) {
|
||||
LANG = config;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
if(this == TITLE)
|
||||
if (this == TITLE)
|
||||
return ChatColor.translateAlternateColorCodes('&', LANG.getString(this.path, def)) + " ";
|
||||
return ChatColor.translateAlternateColorCodes('&', LANG.getString(this.path, def));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the default value of the path.
|
||||
* @return The default value of the path.
|
||||
*/
|
||||
public String getDefault() {
|
||||
return this.def;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the path to the string.
|
||||
* @return The path to the string.
|
||||
*/
|
||||
public String getPath() {
|
||||
return this.path;
|
||||
}
|
||||
|
||||
@@ -134,7 +134,7 @@ public class Metrics {
|
||||
private volatile int taskId = -1;
|
||||
|
||||
public Metrics(final Plugin plugin) throws IOException {
|
||||
if(plugin == null) {
|
||||
if (plugin == null) {
|
||||
throw new IllegalArgumentException("Plugin cannot be null");
|
||||
}
|
||||
|
||||
@@ -149,7 +149,7 @@ public class Metrics {
|
||||
configuration.addDefault("guid", UUID.randomUUID().toString());
|
||||
|
||||
// Do we need to create the file?
|
||||
if(configuration.get("guid", null) == null) {
|
||||
if (configuration.get("guid", null) == null) {
|
||||
configuration.options().header("http://mcstats.org").copyDefaults(true);
|
||||
configuration.save(configurationFile);
|
||||
}
|
||||
@@ -166,7 +166,7 @@ public class Metrics {
|
||||
* @return Graph object created. Will never return NULL under normal circumstances unless bad parameters are given
|
||||
*/
|
||||
public Graph createGraph(final String name) {
|
||||
if(name == null) {
|
||||
if (name == null) {
|
||||
throw new IllegalArgumentException("Graph name cannot be null");
|
||||
}
|
||||
|
||||
@@ -186,7 +186,7 @@ public class Metrics {
|
||||
* @param graph The name of the graph
|
||||
*/
|
||||
public void addGraph(final Graph graph) {
|
||||
if(graph == null) {
|
||||
if (graph == null) {
|
||||
throw new IllegalArgumentException("Graph cannot be null");
|
||||
}
|
||||
|
||||
@@ -199,7 +199,7 @@ public class Metrics {
|
||||
* @param plotter The plotter to use to plot custom data
|
||||
*/
|
||||
public void addCustomData(final Plotter plotter) {
|
||||
if(plotter == null) {
|
||||
if (plotter == null) {
|
||||
throw new IllegalArgumentException("Plotter cannot be null");
|
||||
}
|
||||
|
||||
@@ -219,14 +219,14 @@ public class Metrics {
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
public boolean start() {
|
||||
synchronized (optOutLock) {
|
||||
synchronized(optOutLock) {
|
||||
// Did we opt out?
|
||||
if(isOptOut()) {
|
||||
if (isOptOut()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Is metrics already running?
|
||||
if(taskId >= 0) {
|
||||
if (taskId >= 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -238,10 +238,10 @@ public class Metrics {
|
||||
public void run() {
|
||||
try {
|
||||
// This has to be synchronized or it can collide with the disable method.
|
||||
synchronized (optOutLock) {
|
||||
synchronized(optOutLock) {
|
||||
// Disable Task, if it is running and the server owner decided to
|
||||
// opt-out
|
||||
if(isOptOut() && taskId > 0) {
|
||||
if (isOptOut() && taskId > 0) {
|
||||
plugin.getServer().getScheduler().cancelTask(taskId);
|
||||
taskId = -1;
|
||||
// Tell all plotters to stop gathering information.
|
||||
@@ -276,7 +276,7 @@ public class Metrics {
|
||||
* @return true if metrics should be opted out of it
|
||||
*/
|
||||
public boolean isOptOut() {
|
||||
synchronized (optOutLock) {
|
||||
synchronized(optOutLock) {
|
||||
try {
|
||||
// Reload the metrics file
|
||||
configuration.load(getConfigFile());
|
||||
@@ -298,15 +298,15 @@ public class Metrics {
|
||||
*/
|
||||
public void enable() throws IOException {
|
||||
// This has to be synchronized or it can collide with the check in the task.
|
||||
synchronized (optOutLock) {
|
||||
synchronized(optOutLock) {
|
||||
// Check if the server owner has already set opt-out, if not, set it.
|
||||
if(isOptOut()) {
|
||||
if (isOptOut()) {
|
||||
configuration.set("opt-out", false);
|
||||
configuration.save(configurationFile);
|
||||
}
|
||||
|
||||
// Enable Task, if it is not running
|
||||
if(taskId < 0) {
|
||||
if (taskId < 0) {
|
||||
start();
|
||||
}
|
||||
}
|
||||
@@ -319,15 +319,15 @@ public class Metrics {
|
||||
*/
|
||||
public void disable() throws IOException {
|
||||
// This has to be synchronized or it can collide with the check in the task.
|
||||
synchronized (optOutLock) {
|
||||
synchronized(optOutLock) {
|
||||
// Check if the server owner has already set opt-out, if not, set it.
|
||||
if(!isOptOut()) {
|
||||
if (!isOptOut()) {
|
||||
configuration.set("opt-out", true);
|
||||
configuration.save(configurationFile);
|
||||
}
|
||||
|
||||
// Disable Task, if it is running
|
||||
if(taskId > 0) {
|
||||
if (taskId > 0) {
|
||||
this.plugin.getServer().getScheduler().cancelTask(taskId);
|
||||
taskId = -1;
|
||||
}
|
||||
@@ -369,16 +369,16 @@ public class Metrics {
|
||||
encodeDataPair(data, "revision", String.valueOf(REVISION));
|
||||
|
||||
// If we're pinging, append it
|
||||
if(isPing) {
|
||||
if (isPing) {
|
||||
encodeDataPair(data, "ping", "true");
|
||||
}
|
||||
|
||||
// Acquire a lock on the graphs, which lets us make the assumption we also lock everything
|
||||
// inside of the graph (e.g plotters)
|
||||
synchronized (graphs) {
|
||||
synchronized(graphs) {
|
||||
final Iterator<Graph> iter = graphs.iterator();
|
||||
|
||||
while (iter.hasNext()) {
|
||||
while(iter.hasNext()) {
|
||||
final Graph graph = iter.next();
|
||||
|
||||
for(Plotter plotter:graph.getPlotters()) {
|
||||
@@ -405,7 +405,7 @@ public class Metrics {
|
||||
|
||||
// Mineshafter creates a socks proxy, so we can safely bypass it
|
||||
// It does not reroute POST requests so we need to go around it
|
||||
if(isMineshafterPresent()) {
|
||||
if (isMineshafterPresent()) {
|
||||
connection = url.openConnection(Proxy.NO_PROXY);
|
||||
} else {
|
||||
connection = url.openConnection();
|
||||
@@ -426,15 +426,15 @@ public class Metrics {
|
||||
writer.close();
|
||||
reader.close();
|
||||
|
||||
if(response == null || response.startsWith("ERR")) {
|
||||
if (response == null || response.startsWith("ERR")) {
|
||||
throw new IOException(response); // Throw the exception
|
||||
} else {
|
||||
// Is this the first update this hour?
|
||||
if(response.contains("OK This is your first update this hour")) {
|
||||
synchronized (graphs) {
|
||||
if (response.contains("OK This is your first update this hour")) {
|
||||
synchronized(graphs) {
|
||||
final Iterator<Graph> iter = graphs.iterator();
|
||||
|
||||
while (iter.hasNext()) {
|
||||
while(iter.hasNext()) {
|
||||
final Graph graph = iter.next();
|
||||
|
||||
for(Plotter plotter:graph.getPlotters()) {
|
||||
@@ -550,7 +550,7 @@ public class Metrics {
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object object) {
|
||||
if(!(object instanceof Graph)) {
|
||||
if (!(object instanceof Graph)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -624,7 +624,7 @@ public class Metrics {
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object object) {
|
||||
if(!(object instanceof Plotter)) {
|
||||
if (!(object instanceof Plotter)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -17,17 +17,18 @@ import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
/**
|
||||
* Fancy JSON serialization mostly by evilmidget38.
|
||||
* @author evilmidget38, gomeow
|
||||
*
|
||||
*/
|
||||
public class Serialization {
|
||||
|
||||
/*
|
||||
* All normal functions
|
||||
*/
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static Map<String, Object> toMap(JSONObject object) throws JSONException {
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
Iterator<String> keys = object.keys();
|
||||
while (keys.hasNext()) {
|
||||
while(keys.hasNext()) {
|
||||
String key = (String) keys.next();
|
||||
map.put(key, fromJson(object.get(key)));
|
||||
}
|
||||
@@ -35,11 +36,11 @@ public class Serialization {
|
||||
}
|
||||
|
||||
private static Object fromJson(Object json) throws JSONException {
|
||||
if(json == JSONObject.NULL) {
|
||||
if (json == JSONObject.NULL) {
|
||||
return null;
|
||||
} else if(json instanceof JSONObject) {
|
||||
} else if (json instanceof JSONObject) {
|
||||
return toMap((JSONObject) json);
|
||||
} else if(json instanceof JSONArray) {
|
||||
} else if (json instanceof JSONArray) {
|
||||
return toList((JSONArray) json);
|
||||
} else {
|
||||
return json;
|
||||
@@ -61,7 +62,7 @@ public class Serialization {
|
||||
items.add(is);
|
||||
}
|
||||
for(ConfigurationSerializable cs:items) {
|
||||
if(cs == null) {
|
||||
if (cs == null) {
|
||||
result.add("null");
|
||||
}
|
||||
else {
|
||||
@@ -75,7 +76,7 @@ public class Serialization {
|
||||
Inventory inv = Bukkit.createInventory(null, 54, ChatColor.RED + "Vault #" + number);
|
||||
List<ItemStack> contents = new ArrayList<ItemStack>();
|
||||
for(String piece:stringItems) {
|
||||
if(piece.equalsIgnoreCase("null")) {
|
||||
if (piece.equalsIgnoreCase("null")) {
|
||||
contents.add(null);
|
||||
}
|
||||
else {
|
||||
@@ -97,7 +98,7 @@ public class Serialization {
|
||||
public static Map<String, Object> serialize(ConfigurationSerializable cs) {
|
||||
Map<String, Object> serialized = recreateMap(cs.serialize());
|
||||
for(Entry<String, Object> entry:serialized.entrySet()) {
|
||||
if(entry.getValue() instanceof ConfigurationSerializable) {
|
||||
if (entry.getValue() instanceof ConfigurationSerializable) {
|
||||
entry.setValue(serialize((ConfigurationSerializable) entry.getValue()));
|
||||
}
|
||||
}
|
||||
@@ -113,20 +114,14 @@ public class Serialization {
|
||||
return map;
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
public static ConfigurationSerializable deserialize(Map<String, Object> map) {
|
||||
for(Entry<String, Object> entry:map.entrySet()) {
|
||||
// Check if any of its sub-maps are ConfigurationSerializable. They need to be done
|
||||
// first.
|
||||
if(entry.getValue() instanceof Map && ((Map) entry.getValue()).containsKey(ConfigurationSerialization.SERIALIZED_TYPE_KEY)) {
|
||||
if (entry.getValue() instanceof Map && ((Map) entry.getValue()).containsKey(ConfigurationSerialization.SERIALIZED_TYPE_KEY)) {
|
||||
entry.setValue(deserialize((Map) entry.getValue()));
|
||||
}
|
||||
}
|
||||
return ConfigurationSerialization.deserializeObject(map);
|
||||
}
|
||||
|
||||
/*
|
||||
* All old methods for transferring
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
@@ -15,6 +15,9 @@ import org.json.JSONObject;
|
||||
|
||||
import com.drtshock.playervaults.PlayerVaults;
|
||||
|
||||
/**
|
||||
* A class for updating the lang.yml and checking for updates at DBO.
|
||||
*/
|
||||
public class Updater extends PlayerVaults {
|
||||
|
||||
SortedMap<String, String> lang = new TreeMap<String, String>();
|
||||
@@ -22,7 +25,7 @@ public class Updater extends PlayerVaults {
|
||||
public Updater() {
|
||||
YamlConfiguration langConf = super.getLang();
|
||||
for(Lang item:Lang.values()) {
|
||||
if(langConf.getString(item.getPath()) == null) {
|
||||
if (langConf.getString(item.getPath()) == null) {
|
||||
langConf.set(item.getPath(), item.getDefault());
|
||||
}
|
||||
}
|
||||
@@ -35,7 +38,13 @@ public class Updater extends PlayerVaults {
|
||||
}
|
||||
}
|
||||
|
||||
public boolean getUpdate(String v) throws IOException {
|
||||
/**
|
||||
* Check whether or not there is a new update.
|
||||
* @param currentVersion The current running version.
|
||||
* @return Whether or not an update is available.
|
||||
* @throws IOException Oh no!
|
||||
*/
|
||||
public boolean getUpdate(String currentVersion) throws IOException {
|
||||
JSONObject json;
|
||||
try {
|
||||
json = getInfo();
|
||||
@@ -44,7 +53,7 @@ public class Updater extends PlayerVaults {
|
||||
PlayerVaults.NEWVERSION = version;
|
||||
String goodLink = new BufferedReader(new InputStreamReader(new URL("http://is.gd/create.php?format=simple&url=" + link).openStream())).readLine();
|
||||
PlayerVaults.LINK = goodLink;
|
||||
if(!version.equalsIgnoreCase(v)) {
|
||||
if (!version.equalsIgnoreCase(currentVersion)) {
|
||||
return true;
|
||||
}
|
||||
} catch(JSONException e) {
|
||||
@@ -53,6 +62,11 @@ public class Updater extends PlayerVaults {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the information about versions from DBO.
|
||||
* @return The information in JSON.
|
||||
* @throws IOException Oh no!
|
||||
*/
|
||||
public JSONObject getInfo() throws IOException {
|
||||
URL url = new URL("http://api.bukget.org/3/plugins/bukkit/playervaults/latest");
|
||||
BufferedReader in = null;
|
||||
@@ -67,7 +81,7 @@ public class Updater extends PlayerVaults {
|
||||
in.close();
|
||||
return json;
|
||||
} catch(JSONException e) {
|
||||
throw new IOException("Oh no!");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -10,12 +10,14 @@ import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
|
||||
import com.drtshock.playervaults.PlayerVaults;
|
||||
import com.drtshock.playervaults.commands.VaultViewInfo;
|
||||
|
||||
/**
|
||||
* A class for managing actual IO to the files, loading inventories, and saving them.
|
||||
*/
|
||||
public class VaultManager {
|
||||
|
||||
public PlayerVaults plugin;
|
||||
@@ -23,21 +25,22 @@ public class VaultManager {
|
||||
public VaultManager(PlayerVaults instance) {
|
||||
this.plugin = instance;
|
||||
}
|
||||
|
||||
private final String directory = "plugins" + File.separator + "PlayerVaults" + File.separator + "vaults";
|
||||
|
||||
/**
|
||||
* Method to save player's vault. Serialize his inventory. Save the
|
||||
* vaults.yml
|
||||
*
|
||||
* @param player
|
||||
* @throws IOException
|
||||
* Saves the inventory to the specified player and vault number.
|
||||
* @param inventory The inventory to be saved.
|
||||
* @param player The player of whose file to save to.
|
||||
* @param number The vault number.
|
||||
* @throws IOException Uh oh!
|
||||
*/
|
||||
public void saveVault(Inventory inv, String player, int number) throws IOException {
|
||||
YamlConfiguration yaml = playerVaultFile(player);
|
||||
public void saveVault(Inventory inventory, String player, int number) throws IOException {
|
||||
YamlConfiguration yaml = getPlayerVaultFile(player);
|
||||
yaml.set("vault" + number, null);
|
||||
List<String> list = Serialization.toString(inv);
|
||||
List<String> list = Serialization.toString(inventory);
|
||||
String[] ser = list.toArray(new String[list.size()]);
|
||||
for (int x = 0; x < ser.length; x++) {
|
||||
for(int x = 0; x < ser.length; x++) {
|
||||
if (!ser[x].equalsIgnoreCase("null")) {
|
||||
yaml.set("vault" + number + "." + x, ser[x]);
|
||||
}
|
||||
@@ -46,22 +49,22 @@ public class VaultManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to load player's vault. Deserialize his inventory
|
||||
*
|
||||
* TODO: Check to see if the path exists before we get it!
|
||||
* Load the player's vault and return it.
|
||||
* @param holder The holder of the vault.
|
||||
* @param number The vault number.
|
||||
*/
|
||||
public void loadVault(Player player, String holder, int number) {
|
||||
public Inventory loadVault(String holder, int number) {
|
||||
VaultViewInfo info = new VaultViewInfo(holder, number);
|
||||
Inventory inv = null;
|
||||
if (PlayerVaults.OPENINVENTORIES.containsKey(info.toString())) {
|
||||
inv = PlayerVaults.OPENINVENTORIES.get(info.toString());
|
||||
} else {
|
||||
YamlConfiguration playerFile = playerVaultFile(holder);
|
||||
YamlConfiguration playerFile = getPlayerVaultFile(holder);
|
||||
if (playerFile.getConfigurationSection("vault" + number) == null) {
|
||||
inv = Bukkit.createInventory(player, 54, ChatColor.DARK_RED + "Vault #" + String.valueOf(number));
|
||||
inv = Bukkit.createInventory(null, 54, ChatColor.DARK_RED + "Vault #" + String.valueOf(number));
|
||||
} else {
|
||||
List<String> data = new ArrayList<String>();
|
||||
for (int x = 0; x < 54; x++) {
|
||||
for(int x = 0; x < 54; x++) {
|
||||
String line = playerFile.getString("vault" + number + "." + x);
|
||||
if (line != null) {
|
||||
data.add(line);
|
||||
@@ -73,22 +76,20 @@ public class VaultManager {
|
||||
}
|
||||
PlayerVaults.OPENINVENTORIES.put(info.toString(), inv);
|
||||
}
|
||||
player.openInventory(inv);
|
||||
return inv;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an inventory without opening it. Used for dropping a players
|
||||
* inventories on death.
|
||||
*
|
||||
* @param player
|
||||
* @param number
|
||||
* @return the inventory
|
||||
* Gets an inventory without storing references to it. Used for dropping a players inventories on death.
|
||||
* @param holder The holder of the vault.
|
||||
* @param number The vault number.
|
||||
* @return The inventory of the specified holder and vault number.
|
||||
*/
|
||||
public Inventory getVault(Player player, int number) {
|
||||
YamlConfiguration playerFile = playerVaultFile(player.getName());
|
||||
public Inventory getVault(String holder, int number) {
|
||||
YamlConfiguration playerFile = getPlayerVaultFile(holder);
|
||||
List<String> data = playerFile.getStringList("vault" + number);
|
||||
if (data == null) {
|
||||
Inventory inv = Bukkit.createInventory(player, 54, ChatColor.GREEN + "Vault #" + String.valueOf(number));
|
||||
Inventory inv = Bukkit.createInventory(null, 54, ChatColor.GREEN + "Vault #" + String.valueOf(number));
|
||||
return inv;
|
||||
} else {
|
||||
Inventory inv = Serialization.toInventory(data, number);
|
||||
@@ -98,43 +99,42 @@ public class VaultManager {
|
||||
|
||||
/**
|
||||
* Deletes a players vault.
|
||||
*
|
||||
* @param sender
|
||||
* @param target
|
||||
* @param number
|
||||
* @throws IOException
|
||||
* @param sender The sender of whom to send messages to.
|
||||
* @param holder The vault holder.
|
||||
* @param number The vault number.
|
||||
* @throws IOException Uh oh!
|
||||
*/
|
||||
public void deleteVault(CommandSender sender, String target, int number) throws IOException {
|
||||
String name = target.toLowerCase();
|
||||
public void deleteVault(CommandSender sender, String holder, int number) throws IOException {
|
||||
String name = holder.toLowerCase();
|
||||
File file = new File(directory + File.separator + name.toLowerCase() + ".yml");
|
||||
FileConfiguration playerFile = YamlConfiguration.loadConfiguration(file);
|
||||
if (file.exists()) {
|
||||
playerFile.set("vault" + number, null);
|
||||
playerFile.save(file);
|
||||
}
|
||||
if (sender.getName().equalsIgnoreCase(target)) {
|
||||
if (sender.getName().equalsIgnoreCase(holder)) {
|
||||
sender.sendMessage(Lang.TITLE.toString() + Lang.DELETE_VAULT.toString().replace("%v", String.valueOf(number)));
|
||||
} else {
|
||||
sender.sendMessage(Lang.TITLE.toString() + Lang.DELETE_OTHER_VAULT.toString().replace("%v", String.valueOf(number)).replace("%p", target));
|
||||
sender.sendMessage(Lang.TITLE.toString() + Lang.DELETE_OTHER_VAULT.toString().replace("%v", String.valueOf(number)).replace("%p", holder));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the player's vault file. Create if doesn't exist.
|
||||
*
|
||||
* @param player
|
||||
* @return playerVaultFile file.
|
||||
* Get the holder's vault file. Create if doesn't exist.
|
||||
* @param holder The vault holder.
|
||||
* @return The holder's vault config file.
|
||||
*/
|
||||
public YamlConfiguration playerVaultFile(String player) {
|
||||
public YamlConfiguration getPlayerVaultFile(String holder) {
|
||||
File folder = new File(directory);
|
||||
if (!folder.exists()) {
|
||||
folder.mkdir();
|
||||
}
|
||||
File file = new File(directory + File.separator + player.toLowerCase() + ".yml");
|
||||
File file = new File(directory + File.separator + holder.toLowerCase() + ".yml");
|
||||
if (!file.exists()) {
|
||||
try {
|
||||
file.createNewFile();
|
||||
} catch (IOException e) {
|
||||
} catch(IOException e) {
|
||||
// Who cares?
|
||||
}
|
||||
}
|
||||
YamlConfiguration playerFile = YamlConfiguration.loadConfiguration(file);
|
||||
@@ -143,15 +143,14 @@ public class VaultManager {
|
||||
|
||||
/**
|
||||
* Save the players vault file.
|
||||
*
|
||||
* @param name
|
||||
* @param yaml
|
||||
* @throws IOException
|
||||
* @param holder The vault holder of whose file to save.
|
||||
* @param yaml The config to save.
|
||||
* @throws IOException Uh oh!
|
||||
*/
|
||||
public void saveFile(String name, YamlConfiguration yaml) throws IOException {
|
||||
File file = new File(directory + File.separator + name.toLowerCase() + ".yml");
|
||||
public void saveFile(String holder, YamlConfiguration yaml) throws IOException {
|
||||
File file = new File(directory + File.separator + holder.toLowerCase() + ".yml");
|
||||
if (file.exists()) {
|
||||
file.renameTo(new File(directory + File.separator + "backups" + File.separator + name.toLowerCase() + ".yml"));
|
||||
file.renameTo(new File(directory + File.separator + "backups" + File.separator + holder.toLowerCase() + ".yml"));
|
||||
}
|
||||
yaml.save(file);
|
||||
}
|
||||
|
||||
@@ -100,13 +100,13 @@ public class JSONArray {
|
||||
*/
|
||||
public JSONArray(JSONTokener x) throws JSONException {
|
||||
this();
|
||||
if(x.nextClean() != '[') {
|
||||
if (x.nextClean() != '[') {
|
||||
throw x.syntaxError("A JSONArray text must start with '['");
|
||||
}
|
||||
if(x.nextClean() != ']') {
|
||||
if (x.nextClean() != ']') {
|
||||
x.back();
|
||||
for(;;) {
|
||||
if(x.nextClean() == ',') {
|
||||
if (x.nextClean() == ',') {
|
||||
x.back();
|
||||
this.myArrayList.add(JSONObject.NULL);
|
||||
} else {
|
||||
@@ -116,7 +116,7 @@ public class JSONArray {
|
||||
switch (x.nextClean()) {
|
||||
case ';':
|
||||
case ',':
|
||||
if(x.nextClean() == ']') {
|
||||
if (x.nextClean() == ']') {
|
||||
return;
|
||||
}
|
||||
x.back();
|
||||
@@ -147,9 +147,9 @@ public class JSONArray {
|
||||
*/
|
||||
public JSONArray(Collection<?> collection) {
|
||||
this.myArrayList = new ArrayList<Object>();
|
||||
if(collection != null) {
|
||||
if (collection != null) {
|
||||
Iterator<?> iter = collection.iterator();
|
||||
while (iter.hasNext()) {
|
||||
while(iter.hasNext()) {
|
||||
this.myArrayList.add(JSONObject.wrap(iter.next()));
|
||||
}
|
||||
}
|
||||
@@ -161,7 +161,7 @@ public class JSONArray {
|
||||
*/
|
||||
public JSONArray(Object array) throws JSONException {
|
||||
this();
|
||||
if(array.getClass().isArray()) {
|
||||
if (array.getClass().isArray()) {
|
||||
int length = Array.getLength(array);
|
||||
for(int i = 0; i < length; i += 1) {
|
||||
this.put(JSONObject.wrap(Array.get(array, i)));
|
||||
@@ -181,7 +181,7 @@ public class JSONArray {
|
||||
*/
|
||||
public Object get(int index) throws JSONException {
|
||||
Object object = this.opt(index);
|
||||
if(object == null) {
|
||||
if (object == null) {
|
||||
throw new JSONException("JSONArray[" + index + "] not found.");
|
||||
}
|
||||
return object;
|
||||
@@ -198,11 +198,11 @@ public class JSONArray {
|
||||
*/
|
||||
public boolean getBoolean(int index) throws JSONException {
|
||||
Object object = this.get(index);
|
||||
if(object.equals(Boolean.FALSE) ||
|
||||
if (object.equals(Boolean.FALSE) ||
|
||||
(object instanceof String &&
|
||||
((String) object).equalsIgnoreCase("false"))) {
|
||||
return false;
|
||||
} else if(object.equals(Boolean.TRUE) ||
|
||||
} else if (object.equals(Boolean.TRUE) ||
|
||||
(object instanceof String &&
|
||||
((String) object).equalsIgnoreCase("true"))) {
|
||||
return true;
|
||||
@@ -258,7 +258,7 @@ public class JSONArray {
|
||||
*/
|
||||
public JSONArray getJSONArray(int index) throws JSONException {
|
||||
Object object = this.get(index);
|
||||
if(object instanceof JSONArray) {
|
||||
if (object instanceof JSONArray) {
|
||||
return (JSONArray) object;
|
||||
}
|
||||
throw new JSONException("JSONArray[" + index +
|
||||
@@ -274,7 +274,7 @@ public class JSONArray {
|
||||
*/
|
||||
public JSONObject getJSONObject(int index) throws JSONException {
|
||||
Object object = this.get(index);
|
||||
if(object instanceof JSONObject) {
|
||||
if (object instanceof JSONObject) {
|
||||
return (JSONObject) object;
|
||||
}
|
||||
throw new JSONException("JSONArray[" + index +
|
||||
@@ -309,7 +309,7 @@ public class JSONArray {
|
||||
*/
|
||||
public String getString(int index) throws JSONException {
|
||||
Object object = this.get(index);
|
||||
if(object instanceof String) {
|
||||
if (object instanceof String) {
|
||||
return (String) object;
|
||||
}
|
||||
throw new JSONException("JSONArray[" + index + "] not a string.");
|
||||
@@ -337,7 +337,7 @@ public class JSONArray {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
|
||||
for(int i = 0; i < len; i += 1) {
|
||||
if(i > 0) {
|
||||
if (i > 0) {
|
||||
sb.append(separator);
|
||||
}
|
||||
sb.append(JSONObject.valueToString(this.myArrayList.get(i)));
|
||||
@@ -711,13 +711,13 @@ public class JSONArray {
|
||||
*/
|
||||
public JSONArray put(int index, Object value) throws JSONException {
|
||||
JSONObject.testValidity(value);
|
||||
if(index < 0) {
|
||||
if (index < 0) {
|
||||
throw new JSONException("JSONArray[" + index + "] not found.");
|
||||
}
|
||||
if(index < this.length()) {
|
||||
if (index < this.length()) {
|
||||
this.myArrayList.set(index, value);
|
||||
} else {
|
||||
while (index != this.length()) {
|
||||
while(index != this.length()) {
|
||||
this.put(JSONObject.NULL);
|
||||
}
|
||||
this.put(value);
|
||||
@@ -747,7 +747,7 @@ public class JSONArray {
|
||||
* @throws JSONException If any of the names are null.
|
||||
*/
|
||||
public JSONObject toJSONObject(JSONArray names) throws JSONException {
|
||||
if(names == null || names.length() == 0 || this.length() == 0) {
|
||||
if (names == null || names.length() == 0 || this.length() == 0) {
|
||||
return null;
|
||||
}
|
||||
JSONObject jo = new JSONObject();
|
||||
@@ -789,7 +789,7 @@ public class JSONArray {
|
||||
*/
|
||||
public String toString(int indentFactor) throws JSONException {
|
||||
StringWriter sw = new StringWriter();
|
||||
synchronized (sw.getBuffer()) {
|
||||
synchronized(sw.getBuffer()) {
|
||||
return this.write(sw, indentFactor, 0).toString();
|
||||
}
|
||||
}
|
||||
@@ -827,17 +827,17 @@ public class JSONArray {
|
||||
int length = this.length();
|
||||
writer.write('[');
|
||||
|
||||
if(length == 1) {
|
||||
if (length == 1) {
|
||||
JSONObject.writeValue(writer, this.myArrayList.get(0),
|
||||
indentFactor, indent);
|
||||
} else if(length != 0) {
|
||||
} else if (length != 0) {
|
||||
final int newindent = indent + indentFactor;
|
||||
|
||||
for(int i = 0; i < length; i += 1) {
|
||||
if(commanate) {
|
||||
if (commanate) {
|
||||
writer.write(',');
|
||||
}
|
||||
if(indentFactor > 0) {
|
||||
if (indentFactor > 0) {
|
||||
writer.write('\n');
|
||||
}
|
||||
JSONObject.indent(writer, newindent);
|
||||
@@ -845,7 +845,7 @@ public class JSONArray {
|
||||
indentFactor, newindent);
|
||||
commanate = true;
|
||||
}
|
||||
if(indentFactor > 0) {
|
||||
if (indentFactor > 0) {
|
||||
writer.write('\n');
|
||||
}
|
||||
JSONObject.indent(writer, indent);
|
||||
|
||||
@@ -6,6 +6,7 @@ package org.json;
|
||||
* @version 2010-12-24
|
||||
*/
|
||||
public class JSONException extends Exception {
|
||||
|
||||
private static final long serialVersionUID = 0;
|
||||
private Throwable cause;
|
||||
|
||||
|
||||
@@ -94,6 +94,7 @@ import java.util.Set;
|
||||
* @version 2012-12-01
|
||||
*/
|
||||
public class JSONObject {
|
||||
|
||||
/**
|
||||
* The maximum number of keys in the key pool.
|
||||
*/
|
||||
@@ -192,7 +193,7 @@ public class JSONObject {
|
||||
char c;
|
||||
String key;
|
||||
|
||||
if(x.nextClean() != '{') {
|
||||
if (x.nextClean() != '{') {
|
||||
throw x.syntaxError("A JSONObject text must begin with '{'");
|
||||
}
|
||||
for(;;) {
|
||||
@@ -210,11 +211,11 @@ public class JSONObject {
|
||||
// The key is followed by ':'. We will also tolerate '=' or '=>'.
|
||||
|
||||
c = x.nextClean();
|
||||
if(c == '=') {
|
||||
if(x.next() != '>') {
|
||||
if (c == '=') {
|
||||
if (x.next() != '>') {
|
||||
x.back();
|
||||
}
|
||||
} else if(c != ':') {
|
||||
} else if (c != ':') {
|
||||
throw x.syntaxError("Expected a ':' after a key");
|
||||
}
|
||||
this.putOnce(key, x.nextValue());
|
||||
@@ -224,7 +225,7 @@ public class JSONObject {
|
||||
switch (x.nextClean()) {
|
||||
case ';':
|
||||
case ',':
|
||||
if(x.nextClean() == '}') {
|
||||
if (x.nextClean() == '}') {
|
||||
return;
|
||||
}
|
||||
x.back();
|
||||
@@ -246,12 +247,12 @@ public class JSONObject {
|
||||
*/
|
||||
public JSONObject(Map map) {
|
||||
this.map = new HashMap();
|
||||
if(map != null) {
|
||||
if (map != null) {
|
||||
Iterator i = map.entrySet().iterator();
|
||||
while (i.hasNext()) {
|
||||
while(i.hasNext()) {
|
||||
Map.Entry e = (Map.Entry) i.next();
|
||||
Object value = e.getValue();
|
||||
if(value != null) {
|
||||
if (value != null) {
|
||||
this.map.put(e.getKey(), wrap(value));
|
||||
}
|
||||
}
|
||||
@@ -332,9 +333,9 @@ public class JSONObject {
|
||||
// Iterate through the keys in the bundle.
|
||||
|
||||
Enumeration keys = bundle.getKeys();
|
||||
while (keys.hasMoreElements()) {
|
||||
while(keys.hasMoreElements()) {
|
||||
Object key = keys.nextElement();
|
||||
if(key instanceof String) {
|
||||
if (key instanceof String) {
|
||||
|
||||
// Go through the path, ensuring that there is a nested JSONObject for each
|
||||
// segment except the last. Add the value using the last segment's name into
|
||||
@@ -346,7 +347,7 @@ public class JSONObject {
|
||||
for(int i = 0; i < last; i += 1) {
|
||||
String segment = path[i];
|
||||
JSONObject nextTarget = target.optJSONObject(segment);
|
||||
if(nextTarget == null) {
|
||||
if (nextTarget == null) {
|
||||
nextTarget = new JSONObject();
|
||||
target.put(segment, nextTarget);
|
||||
}
|
||||
@@ -379,11 +380,11 @@ public class JSONObject {
|
||||
) throws JSONException {
|
||||
testValidity(value);
|
||||
Object object = this.opt(key);
|
||||
if(object == null) {
|
||||
if (object == null) {
|
||||
this.put(key, value instanceof JSONArray
|
||||
? new JSONArray().put(value)
|
||||
: value);
|
||||
} else if(object instanceof JSONArray) {
|
||||
} else if (object instanceof JSONArray) {
|
||||
((JSONArray) object).put(value);
|
||||
} else {
|
||||
this.put(key, new JSONArray().put(object).put(value));
|
||||
@@ -405,9 +406,9 @@ public class JSONObject {
|
||||
public JSONObject append(String key, Object value) throws JSONException {
|
||||
testValidity(value);
|
||||
Object object = this.opt(key);
|
||||
if(object == null) {
|
||||
if (object == null) {
|
||||
this.put(key, new JSONArray().put(value));
|
||||
} else if(object instanceof JSONArray) {
|
||||
} else if (object instanceof JSONArray) {
|
||||
this.put(key, ((JSONArray) object).put(value));
|
||||
} else {
|
||||
throw new JSONException("JSONObject[" + key +
|
||||
@@ -423,19 +424,19 @@ public class JSONObject {
|
||||
* @return A String.
|
||||
*/
|
||||
public static String doubleToString(double d) {
|
||||
if(Double.isInfinite(d) || Double.isNaN(d)) {
|
||||
if (Double.isInfinite(d) || Double.isNaN(d)) {
|
||||
return "null";
|
||||
}
|
||||
|
||||
// Shave off trailing zeros and decimal point, if possible.
|
||||
|
||||
String string = Double.toString(d);
|
||||
if(string.indexOf('.') > 0 && string.indexOf('e') < 0 &&
|
||||
if (string.indexOf('.') > 0 && string.indexOf('e') < 0 &&
|
||||
string.indexOf('E') < 0) {
|
||||
while (string.endsWith("0")) {
|
||||
while(string.endsWith("0")) {
|
||||
string = string.substring(0, string.length() - 1);
|
||||
}
|
||||
if(string.endsWith(".")) {
|
||||
if (string.endsWith(".")) {
|
||||
string = string.substring(0, string.length() - 1);
|
||||
}
|
||||
}
|
||||
@@ -450,11 +451,11 @@ public class JSONObject {
|
||||
* @throws JSONException if the key is not found.
|
||||
*/
|
||||
public Object get(String key) throws JSONException {
|
||||
if(key == null) {
|
||||
if (key == null) {
|
||||
throw new JSONException("Null key.");
|
||||
}
|
||||
Object object = this.opt(key);
|
||||
if(object == null) {
|
||||
if (object == null) {
|
||||
throw new JSONException("JSONObject[" + quote(key) +
|
||||
"] not found.");
|
||||
}
|
||||
@@ -471,11 +472,11 @@ public class JSONObject {
|
||||
*/
|
||||
public boolean getBoolean(String key) throws JSONException {
|
||||
Object object = this.get(key);
|
||||
if(object.equals(Boolean.FALSE) ||
|
||||
if (object.equals(Boolean.FALSE) ||
|
||||
(object instanceof String &&
|
||||
((String) object).equalsIgnoreCase("false"))) {
|
||||
return false;
|
||||
} else if(object.equals(Boolean.TRUE) ||
|
||||
} else if (object.equals(Boolean.TRUE) ||
|
||||
(object instanceof String &&
|
||||
((String) object).equalsIgnoreCase("true"))) {
|
||||
return true;
|
||||
@@ -533,7 +534,7 @@ public class JSONObject {
|
||||
*/
|
||||
public JSONArray getJSONArray(String key) throws JSONException {
|
||||
Object object = this.get(key);
|
||||
if(object instanceof JSONArray) {
|
||||
if (object instanceof JSONArray) {
|
||||
return (JSONArray) object;
|
||||
}
|
||||
throw new JSONException("JSONObject[" + quote(key) +
|
||||
@@ -550,7 +551,7 @@ public class JSONObject {
|
||||
*/
|
||||
public JSONObject getJSONObject(String key) throws JSONException {
|
||||
Object object = this.get(key);
|
||||
if(object instanceof JSONObject) {
|
||||
if (object instanceof JSONObject) {
|
||||
return (JSONObject) object;
|
||||
}
|
||||
throw new JSONException("JSONObject[" + quote(key) +
|
||||
@@ -584,13 +585,13 @@ public class JSONObject {
|
||||
*/
|
||||
public static String[] getNames(JSONObject jo) {
|
||||
int length = jo.length();
|
||||
if(length == 0) {
|
||||
if (length == 0) {
|
||||
return null;
|
||||
}
|
||||
Iterator iterator = jo.keys();
|
||||
String[] names = new String[length];
|
||||
int i = 0;
|
||||
while (iterator.hasNext()) {
|
||||
while(iterator.hasNext()) {
|
||||
names[i] = (String) iterator.next();
|
||||
i += 1;
|
||||
}
|
||||
@@ -603,13 +604,13 @@ public class JSONObject {
|
||||
* @return An array of field names, or null if there are no names.
|
||||
*/
|
||||
public static String[] getNames(Object object) {
|
||||
if(object == null) {
|
||||
if (object == null) {
|
||||
return null;
|
||||
}
|
||||
Class klass = object.getClass();
|
||||
Field[] fields = klass.getFields();
|
||||
int length = fields.length;
|
||||
if(length == 0) {
|
||||
if (length == 0) {
|
||||
return null;
|
||||
}
|
||||
String[] names = new String[length];
|
||||
@@ -628,7 +629,7 @@ public class JSONObject {
|
||||
*/
|
||||
public String getString(String key) throws JSONException {
|
||||
Object object = this.get(key);
|
||||
if(object instanceof String) {
|
||||
if (object instanceof String) {
|
||||
return (String) object;
|
||||
}
|
||||
throw new JSONException("JSONObject[" + quote(key) +
|
||||
@@ -655,15 +656,15 @@ public class JSONObject {
|
||||
*/
|
||||
public JSONObject increment(String key) throws JSONException {
|
||||
Object value = this.opt(key);
|
||||
if(value == null) {
|
||||
if (value == null) {
|
||||
this.put(key, 1);
|
||||
} else if(value instanceof Integer) {
|
||||
} else if (value instanceof Integer) {
|
||||
this.put(key, ((Integer) value).intValue() + 1);
|
||||
} else if(value instanceof Long) {
|
||||
} else if (value instanceof Long) {
|
||||
this.put(key, ((Long) value).longValue() + 1);
|
||||
} else if(value instanceof Double) {
|
||||
} else if (value instanceof Double) {
|
||||
this.put(key, ((Double) value).doubleValue() + 1);
|
||||
} else if(value instanceof Float) {
|
||||
} else if (value instanceof Float) {
|
||||
this.put(key, ((Float) value).floatValue() + 1);
|
||||
} else {
|
||||
throw new JSONException("Unable to increment [" + quote(key) + "].");
|
||||
@@ -718,7 +719,7 @@ public class JSONObject {
|
||||
public JSONArray names() {
|
||||
JSONArray ja = new JSONArray();
|
||||
Iterator keys = this.keys();
|
||||
while (keys.hasNext()) {
|
||||
while(keys.hasNext()) {
|
||||
ja.put(keys.next());
|
||||
}
|
||||
return ja.length() == 0 ? null : ja;
|
||||
@@ -732,7 +733,7 @@ public class JSONObject {
|
||||
*/
|
||||
public static String numberToString(Number number)
|
||||
throws JSONException {
|
||||
if(number == null) {
|
||||
if (number == null) {
|
||||
throw new JSONException("Null pointer");
|
||||
}
|
||||
testValidity(number);
|
||||
@@ -740,12 +741,12 @@ public class JSONObject {
|
||||
// Shave off trailing zeros and decimal point, if possible.
|
||||
|
||||
String string = number.toString();
|
||||
if(string.indexOf('.') > 0 && string.indexOf('e') < 0 &&
|
||||
if (string.indexOf('.') > 0 && string.indexOf('e') < 0 &&
|
||||
string.indexOf('E') < 0) {
|
||||
while (string.endsWith("0")) {
|
||||
while(string.endsWith("0")) {
|
||||
string = string.substring(0, string.length() - 1);
|
||||
}
|
||||
if(string.endsWith(".")) {
|
||||
if (string.endsWith(".")) {
|
||||
string = string.substring(0, string.length() - 1);
|
||||
}
|
||||
}
|
||||
@@ -947,31 +948,31 @@ public class JSONObject {
|
||||
for(int i = 0; i < methods.length; i += 1) {
|
||||
try {
|
||||
Method method = methods[i];
|
||||
if(Modifier.isPublic(method.getModifiers())) {
|
||||
if (Modifier.isPublic(method.getModifiers())) {
|
||||
String name = method.getName();
|
||||
String key = "";
|
||||
if(name.startsWith("get")) {
|
||||
if("getClass".equals(name) ||
|
||||
if (name.startsWith("get")) {
|
||||
if ("getClass".equals(name) ||
|
||||
"getDeclaringClass".equals(name)) {
|
||||
key = "";
|
||||
} else {
|
||||
key = name.substring(3);
|
||||
}
|
||||
} else if(name.startsWith("is")) {
|
||||
} else if (name.startsWith("is")) {
|
||||
key = name.substring(2);
|
||||
}
|
||||
if(key.length() > 0 &&
|
||||
if (key.length() > 0 &&
|
||||
Character.isUpperCase(key.charAt(0)) &&
|
||||
method.getParameterTypes().length == 0) {
|
||||
if(key.length() == 1) {
|
||||
if (key.length() == 1) {
|
||||
key = key.toLowerCase();
|
||||
} else if(!Character.isUpperCase(key.charAt(1))) {
|
||||
} else if (!Character.isUpperCase(key.charAt(1))) {
|
||||
key = key.substring(0, 1).toLowerCase() +
|
||||
key.substring(1);
|
||||
}
|
||||
|
||||
Object result = method.invoke(bean, (Object[]) null);
|
||||
if(result != null) {
|
||||
if (result != null) {
|
||||
this.map.put(key, wrap(result));
|
||||
}
|
||||
}
|
||||
@@ -1072,14 +1073,14 @@ public class JSONObject {
|
||||
*/
|
||||
public JSONObject put(String key, Object value) throws JSONException {
|
||||
String pooled;
|
||||
if(key == null) {
|
||||
if (key == null) {
|
||||
throw new JSONException("Null key.");
|
||||
}
|
||||
if(value != null) {
|
||||
if (value != null) {
|
||||
testValidity(value);
|
||||
pooled = (String) keyPool.get(key);
|
||||
if(pooled == null) {
|
||||
if(keyPool.size() >= keyPoolSize) {
|
||||
if (pooled == null) {
|
||||
if (keyPool.size() >= keyPoolSize) {
|
||||
keyPool = new HashMap(keyPoolSize);
|
||||
}
|
||||
keyPool.put(key, key);
|
||||
@@ -1103,8 +1104,8 @@ public class JSONObject {
|
||||
* @throws JSONException if the key is a duplicate
|
||||
*/
|
||||
public JSONObject putOnce(String key, Object value) throws JSONException {
|
||||
if(key != null && value != null) {
|
||||
if(this.opt(key) != null) {
|
||||
if (key != null && value != null) {
|
||||
if (this.opt(key) != null) {
|
||||
throw new JSONException("Duplicate key \"" + key + "\"");
|
||||
}
|
||||
this.put(key, value);
|
||||
@@ -1123,7 +1124,7 @@ public class JSONObject {
|
||||
* @throws JSONException If the value is a non-finite number.
|
||||
*/
|
||||
public JSONObject putOpt(String key, Object value) throws JSONException {
|
||||
if(key != null && value != null) {
|
||||
if (key != null && value != null) {
|
||||
this.put(key, value);
|
||||
}
|
||||
return this;
|
||||
@@ -1139,7 +1140,7 @@ public class JSONObject {
|
||||
*/
|
||||
public static String quote(String string) {
|
||||
StringWriter sw = new StringWriter();
|
||||
synchronized (sw.getBuffer()) {
|
||||
synchronized(sw.getBuffer()) {
|
||||
try {
|
||||
return quote(string, sw).toString();
|
||||
} catch(IOException ignored) {
|
||||
@@ -1150,7 +1151,7 @@ public class JSONObject {
|
||||
}
|
||||
|
||||
public static Writer quote(String string, Writer w) throws IOException {
|
||||
if(string == null || string.length() == 0) {
|
||||
if (string == null || string.length() == 0) {
|
||||
w.write("\"\"");
|
||||
return w;
|
||||
}
|
||||
@@ -1172,7 +1173,7 @@ public class JSONObject {
|
||||
w.write(c);
|
||||
break;
|
||||
case '/':
|
||||
if(b == '<') {
|
||||
if (b == '<') {
|
||||
w.write('\\');
|
||||
}
|
||||
w.write(c);
|
||||
@@ -1193,7 +1194,7 @@ public class JSONObject {
|
||||
w.write("\\r");
|
||||
break;
|
||||
default:
|
||||
if(c < ' ' || (c >= '\u0080' && c < '\u00a0')
|
||||
if (c < ' ' || (c >= '\u0080' && c < '\u00a0')
|
||||
|| (c >= '\u2000' && c < '\u2100')) {
|
||||
w.write("\\u");
|
||||
hhhh = Integer.toHexString(c);
|
||||
@@ -1226,16 +1227,16 @@ public class JSONObject {
|
||||
*/
|
||||
public static Object stringToValue(String string) {
|
||||
Double d;
|
||||
if(string.equals("")) {
|
||||
if (string.equals("")) {
|
||||
return string;
|
||||
}
|
||||
if(string.equalsIgnoreCase("true")) {
|
||||
if (string.equalsIgnoreCase("true")) {
|
||||
return Boolean.TRUE;
|
||||
}
|
||||
if(string.equalsIgnoreCase("false")) {
|
||||
if (string.equalsIgnoreCase("false")) {
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
if(string.equalsIgnoreCase("null")) {
|
||||
if (string.equalsIgnoreCase("null")) {
|
||||
return JSONObject.NULL;
|
||||
}
|
||||
|
||||
@@ -1248,17 +1249,17 @@ public class JSONObject {
|
||||
*/
|
||||
|
||||
char b = string.charAt(0);
|
||||
if((b >= '0' && b <= '9') || b == '.' || b == '-' || b == '+') {
|
||||
if ((b >= '0' && b <= '9') || b == '.' || b == '-' || b == '+') {
|
||||
try {
|
||||
if(string.indexOf('.') > -1 ||
|
||||
if (string.indexOf('.') > -1 ||
|
||||
string.indexOf('e') > -1 || string.indexOf('E') > -1) {
|
||||
d = Double.valueOf(string);
|
||||
if(!d.isInfinite() && !d.isNaN()) {
|
||||
if (!d.isInfinite() && !d.isNaN()) {
|
||||
return d;
|
||||
}
|
||||
} else {
|
||||
Long myLong = new Long(string);
|
||||
if(myLong.longValue() == myLong.intValue()) {
|
||||
if (myLong.longValue() == myLong.intValue()) {
|
||||
return new Integer(myLong.intValue());
|
||||
} else {
|
||||
return myLong;
|
||||
@@ -1276,14 +1277,14 @@ public class JSONObject {
|
||||
* @throws JSONException If o is a non-finite number.
|
||||
*/
|
||||
public static void testValidity(Object o) throws JSONException {
|
||||
if(o != null) {
|
||||
if(o instanceof Double) {
|
||||
if(((Double) o).isInfinite() || ((Double) o).isNaN()) {
|
||||
if (o != null) {
|
||||
if (o instanceof Double) {
|
||||
if (((Double) o).isInfinite() || ((Double) o).isNaN()) {
|
||||
throw new JSONException(
|
||||
"JSON does not allow non-finite numbers.");
|
||||
}
|
||||
} else if(o instanceof Float) {
|
||||
if(((Float) o).isInfinite() || ((Float) o).isNaN()) {
|
||||
} else if (o instanceof Float) {
|
||||
if (((Float) o).isInfinite() || ((Float) o).isNaN()) {
|
||||
throw new JSONException(
|
||||
"JSON does not allow non-finite numbers.");
|
||||
}
|
||||
@@ -1300,7 +1301,7 @@ public class JSONObject {
|
||||
* @throws JSONException If any of the values are non-finite numbers.
|
||||
*/
|
||||
public JSONArray toJSONArray(JSONArray names) throws JSONException {
|
||||
if(names == null || names.length() == 0) {
|
||||
if (names == null || names.length() == 0) {
|
||||
return null;
|
||||
}
|
||||
JSONArray ja = new JSONArray();
|
||||
@@ -1344,7 +1345,7 @@ public class JSONObject {
|
||||
*/
|
||||
public String toString(int indentFactor) throws JSONException {
|
||||
StringWriter w = new StringWriter();
|
||||
synchronized (w.getBuffer()) {
|
||||
synchronized(w.getBuffer()) {
|
||||
return this.write(w, indentFactor, 0).toString();
|
||||
}
|
||||
}
|
||||
@@ -1371,35 +1372,35 @@ public class JSONObject {
|
||||
* @throws JSONException If the value is or contains an invalid number.
|
||||
*/
|
||||
public static String valueToString(Object value) throws JSONException {
|
||||
if(value == null || value.equals(null)) {
|
||||
if (value == null || value.equals(null)) {
|
||||
return "null";
|
||||
}
|
||||
if(value instanceof JSONString) {
|
||||
if (value instanceof JSONString) {
|
||||
Object object;
|
||||
try {
|
||||
object = ((JSONString) value).toJSONString();
|
||||
} catch(Exception e) {
|
||||
throw new JSONException(e);
|
||||
}
|
||||
if(object instanceof String) {
|
||||
if (object instanceof String) {
|
||||
return (String) object;
|
||||
}
|
||||
throw new JSONException("Bad value from toJSONString: " + object);
|
||||
}
|
||||
if(value instanceof Number) {
|
||||
if (value instanceof Number) {
|
||||
return numberToString((Number) value);
|
||||
}
|
||||
if(value instanceof Boolean || value instanceof JSONObject ||
|
||||
if (value instanceof Boolean || value instanceof JSONObject ||
|
||||
value instanceof JSONArray) {
|
||||
return value.toString();
|
||||
}
|
||||
if(value instanceof Map) {
|
||||
if (value instanceof Map) {
|
||||
return new JSONObject((Map) value).toString();
|
||||
}
|
||||
if(value instanceof Collection) {
|
||||
if (value instanceof Collection) {
|
||||
return new JSONArray((Collection) value).toString();
|
||||
}
|
||||
if(value.getClass().isArray()) {
|
||||
if (value.getClass().isArray()) {
|
||||
return new JSONArray(value).toString();
|
||||
}
|
||||
return quote(value.toString());
|
||||
@@ -1419,10 +1420,10 @@ public class JSONObject {
|
||||
*/
|
||||
public static Object wrap(Object object) {
|
||||
try {
|
||||
if(object == null) {
|
||||
if (object == null) {
|
||||
return NULL;
|
||||
}
|
||||
if(object instanceof JSONObject || object instanceof JSONArray ||
|
||||
if (object instanceof JSONObject || object instanceof JSONArray ||
|
||||
NULL.equals(object) || object instanceof JSONString ||
|
||||
object instanceof Byte || object instanceof Character ||
|
||||
object instanceof Short || object instanceof Integer ||
|
||||
@@ -1432,20 +1433,20 @@ public class JSONObject {
|
||||
return object;
|
||||
}
|
||||
|
||||
if(object instanceof Collection) {
|
||||
if (object instanceof Collection) {
|
||||
return new JSONArray((Collection) object);
|
||||
}
|
||||
if(object.getClass().isArray()) {
|
||||
if (object.getClass().isArray()) {
|
||||
return new JSONArray(object);
|
||||
}
|
||||
if(object instanceof Map) {
|
||||
if (object instanceof Map) {
|
||||
return new JSONObject((Map) object);
|
||||
}
|
||||
Package objectPackage = object.getClass().getPackage();
|
||||
String objectPackageName = objectPackage != null
|
||||
? objectPackage.getName()
|
||||
: "";
|
||||
if(objectPackageName.startsWith("java.") ||
|
||||
if (objectPackageName.startsWith("java.") ||
|
||||
objectPackageName.startsWith("javax.") ||
|
||||
object.getClass().getClassLoader() == null) {
|
||||
return object.toString();
|
||||
@@ -1471,24 +1472,24 @@ public class JSONObject {
|
||||
|
||||
static final Writer writeValue(Writer writer, Object value,
|
||||
int indentFactor, int indent) throws JSONException, IOException {
|
||||
if(value == null || value.equals(null)) {
|
||||
if (value == null || value.equals(null)) {
|
||||
writer.write("null");
|
||||
} else if(value instanceof JSONObject) {
|
||||
} else if (value instanceof JSONObject) {
|
||||
((JSONObject) value).write(writer, indentFactor, indent);
|
||||
} else if(value instanceof JSONArray) {
|
||||
} else if (value instanceof JSONArray) {
|
||||
((JSONArray) value).write(writer, indentFactor, indent);
|
||||
} else if(value instanceof Map) {
|
||||
} else if (value instanceof Map) {
|
||||
new JSONObject((Map) value).write(writer, indentFactor, indent);
|
||||
} else if(value instanceof Collection) {
|
||||
} else if (value instanceof Collection) {
|
||||
new JSONArray((Collection) value).write(writer, indentFactor,
|
||||
indent);
|
||||
} else if(value.getClass().isArray()) {
|
||||
} else if (value.getClass().isArray()) {
|
||||
new JSONArray(value).write(writer, indentFactor, indent);
|
||||
} else if(value instanceof Number) {
|
||||
} else if (value instanceof Number) {
|
||||
writer.write(numberToString((Number) value));
|
||||
} else if(value instanceof Boolean) {
|
||||
} else if (value instanceof Boolean) {
|
||||
writer.write(value.toString());
|
||||
} else if(value instanceof JSONString) {
|
||||
} else if (value instanceof JSONString) {
|
||||
Object o;
|
||||
try {
|
||||
o = ((JSONString) value).toJSONString();
|
||||
@@ -1525,35 +1526,35 @@ public class JSONObject {
|
||||
Iterator keys = this.keys();
|
||||
writer.write('{');
|
||||
|
||||
if(length == 1) {
|
||||
if (length == 1) {
|
||||
Object key = keys.next();
|
||||
writer.write(quote(key.toString()));
|
||||
writer.write(':');
|
||||
if(indentFactor > 0) {
|
||||
if (indentFactor > 0) {
|
||||
writer.write(' ');
|
||||
}
|
||||
writeValue(writer, this.map.get(key), indentFactor, indent);
|
||||
} else if(length != 0) {
|
||||
} else if (length != 0) {
|
||||
final int newindent = indent + indentFactor;
|
||||
while (keys.hasNext()) {
|
||||
while(keys.hasNext()) {
|
||||
Object key = keys.next();
|
||||
if(commanate) {
|
||||
if (commanate) {
|
||||
writer.write(',');
|
||||
}
|
||||
if(indentFactor > 0) {
|
||||
if (indentFactor > 0) {
|
||||
writer.write('\n');
|
||||
}
|
||||
indent(writer, newindent);
|
||||
writer.write(quote(key.toString()));
|
||||
writer.write(':');
|
||||
if(indentFactor > 0) {
|
||||
if (indentFactor > 0) {
|
||||
writer.write(' ');
|
||||
}
|
||||
writeValue(writer, this.map.get(key), indentFactor,
|
||||
newindent);
|
||||
commanate = true;
|
||||
}
|
||||
if(indentFactor > 0) {
|
||||
if (indentFactor > 0) {
|
||||
writer.write('\n');
|
||||
}
|
||||
indent(writer, indent);
|
||||
|
||||
@@ -9,6 +9,7 @@ package org.json;
|
||||
* of using the Object's <code>toString()</code> method and quoting the result.
|
||||
*/
|
||||
public interface JSONString {
|
||||
|
||||
/**
|
||||
* The <code>toJSONString</code> method allows a class to produce its own JSON
|
||||
* serialization.
|
||||
|
||||
@@ -87,7 +87,7 @@ public class JSONTokener {
|
||||
* the next number or identifier.
|
||||
*/
|
||||
public void back() throws JSONException {
|
||||
if(this.usePrevious || this.index <= 0) {
|
||||
if (this.usePrevious || this.index <= 0) {
|
||||
throw new JSONException("Stepping back two steps is not supported");
|
||||
}
|
||||
this.index -= 1;
|
||||
@@ -103,13 +103,13 @@ public class JSONTokener {
|
||||
* @return An int between 0 and 15, or -1 if c was not a hex digit.
|
||||
*/
|
||||
public static int dehexchar(char c) {
|
||||
if(c >= '0' && c <= '9') {
|
||||
if (c >= '0' && c <= '9') {
|
||||
return c - '0';
|
||||
}
|
||||
if(c >= 'A' && c <= 'F') {
|
||||
if (c >= 'A' && c <= 'F') {
|
||||
return c - ('A' - 10);
|
||||
}
|
||||
if(c >= 'a' && c <= 'f') {
|
||||
if (c >= 'a' && c <= 'f') {
|
||||
return c - ('a' - 10);
|
||||
}
|
||||
return -1;
|
||||
@@ -126,7 +126,7 @@ public class JSONTokener {
|
||||
*/
|
||||
public boolean more() throws JSONException {
|
||||
this.next();
|
||||
if(this.end()) {
|
||||
if (this.end()) {
|
||||
return false;
|
||||
}
|
||||
this.back();
|
||||
@@ -140,7 +140,7 @@ public class JSONTokener {
|
||||
*/
|
||||
public char next() throws JSONException {
|
||||
int c;
|
||||
if(this.usePrevious) {
|
||||
if (this.usePrevious) {
|
||||
this.usePrevious = false;
|
||||
c = this.previous;
|
||||
} else {
|
||||
@@ -150,16 +150,16 @@ public class JSONTokener {
|
||||
throw new JSONException(exception);
|
||||
}
|
||||
|
||||
if(c <= 0) { // End of stream
|
||||
if (c <= 0) { // End of stream
|
||||
this.eof = true;
|
||||
c = 0;
|
||||
}
|
||||
}
|
||||
this.index += 1;
|
||||
if(this.previous == '\r') {
|
||||
if (this.previous == '\r') {
|
||||
this.line += 1;
|
||||
this.character = c == '\n' ? 0 : 1;
|
||||
} else if(c == '\n') {
|
||||
} else if (c == '\n') {
|
||||
this.line += 1;
|
||||
this.character = 0;
|
||||
} else {
|
||||
@@ -178,7 +178,7 @@ public class JSONTokener {
|
||||
*/
|
||||
public char next(char c) throws JSONException {
|
||||
char n = this.next();
|
||||
if(n != c) {
|
||||
if (n != c) {
|
||||
throw this.syntaxError("Expected '" + c + "' and instead saw '" +
|
||||
n + "'");
|
||||
}
|
||||
@@ -195,16 +195,16 @@ public class JSONTokener {
|
||||
* n characters remaining in the source string.
|
||||
*/
|
||||
public String next(int n) throws JSONException {
|
||||
if(n == 0) {
|
||||
if (n == 0) {
|
||||
return "";
|
||||
}
|
||||
|
||||
char[] chars = new char[n];
|
||||
int pos = 0;
|
||||
|
||||
while (pos < n) {
|
||||
while(pos < n) {
|
||||
chars[pos] = this.next();
|
||||
if(this.end()) {
|
||||
if (this.end()) {
|
||||
throw this.syntaxError("Substring bounds error");
|
||||
}
|
||||
pos += 1;
|
||||
@@ -220,7 +220,7 @@ public class JSONTokener {
|
||||
public char nextClean() throws JSONException {
|
||||
for(;;) {
|
||||
char c = this.next();
|
||||
if(c == 0 || c > ' ') {
|
||||
if (c == 0 || c > ' ') {
|
||||
return c;
|
||||
}
|
||||
}
|
||||
@@ -279,7 +279,7 @@ public class JSONTokener {
|
||||
}
|
||||
break;
|
||||
default:
|
||||
if(c == quote) {
|
||||
if (c == quote) {
|
||||
return sb.toString();
|
||||
}
|
||||
sb.append(c);
|
||||
@@ -297,8 +297,8 @@ public class JSONTokener {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for(;;) {
|
||||
char c = this.next();
|
||||
if(c == delimiter || c == 0 || c == '\n' || c == '\r') {
|
||||
if(c != 0) {
|
||||
if (c == delimiter || c == 0 || c == '\n' || c == '\r') {
|
||||
if (c != 0) {
|
||||
this.back();
|
||||
}
|
||||
return sb.toString().trim();
|
||||
@@ -318,9 +318,9 @@ public class JSONTokener {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for(;;) {
|
||||
c = this.next();
|
||||
if(delimiters.indexOf(c) >= 0 || c == 0 ||
|
||||
if (delimiters.indexOf(c) >= 0 || c == 0 ||
|
||||
c == '\n' || c == '\r') {
|
||||
if(c != 0) {
|
||||
if (c != 0) {
|
||||
this.back();
|
||||
}
|
||||
return sb.toString().trim();
|
||||
@@ -362,14 +362,14 @@ public class JSONTokener {
|
||||
*/
|
||||
|
||||
StringBuffer sb = new StringBuffer();
|
||||
while (c >= ' ' && ",:]}/\\\"[{;=#".indexOf(c) < 0) {
|
||||
while(c >= ' ' && ",:]}/\\\"[{;=#".indexOf(c) < 0) {
|
||||
sb.append(c);
|
||||
c = this.next();
|
||||
}
|
||||
this.back();
|
||||
|
||||
string = sb.toString().trim();
|
||||
if("".equals(string)) {
|
||||
if ("".equals(string)) {
|
||||
throw this.syntaxError("Missing value");
|
||||
}
|
||||
return JSONObject.stringToValue(string);
|
||||
@@ -391,14 +391,14 @@ public class JSONTokener {
|
||||
this.reader.mark(1000000);
|
||||
do {
|
||||
c = this.next();
|
||||
if(c == 0) {
|
||||
if (c == 0) {
|
||||
this.reader.reset();
|
||||
this.index = startIndex;
|
||||
this.character = startCharacter;
|
||||
this.line = startLine;
|
||||
return c;
|
||||
}
|
||||
} while (c != to);
|
||||
} while(c != to);
|
||||
} catch(IOException exc) {
|
||||
throw new JSONException(exc);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user