mirror of
https://e.coding.net/circlecloud/RealBackpacks.git
synced 2024-12-04 03:49:07 +00:00
use new serialization and remove json...
Signed-off-by: j502647092 <jtb1@163.com>
This commit is contained in:
parent
89ce4c1e95
commit
60093be9bc
48
pom.xml
48
pom.xml
@ -3,17 +3,15 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>cn.CityCraft</groupId>
|
||||
<artifactId>RealBackpacks</artifactId>
|
||||
<version>0.1.5-SNAPSHOT</version>
|
||||
<version>0.1.5</version>
|
||||
<name>RealBackpacks</name>
|
||||
<build>
|
||||
<finalName>${project.name}</finalName>
|
||||
<sourceDirectory>src</sourceDirectory>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src</directory>
|
||||
<excludes>
|
||||
<exclude>**/*.java</exclude>
|
||||
</excludes>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
<plugins>
|
||||
@ -25,6 +23,34 @@
|
||||
<target>1.7</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>2.3</version>
|
||||
<configuration>
|
||||
<createDependencyReducedPom>false</createDependencyReducedPom>
|
||||
<minimizeJar>true</minimizeJar>
|
||||
<artifactSet>
|
||||
<includes>
|
||||
<include>cn.citycraft:PluginHelper</include>
|
||||
</includes>
|
||||
</artifactSet>
|
||||
<relocations>
|
||||
<relocation>
|
||||
<pattern>cn.citycraft.PluginHelper</pattern>
|
||||
<shadedPattern>${project.groupId}.${project.artifactId}</shadedPattern>
|
||||
</relocation>
|
||||
</relocations>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<repositories>
|
||||
@ -32,6 +58,10 @@
|
||||
<id>spigot-repo</id>
|
||||
<url>https://hub.spigotmc.org/nexus/content/groups/public/</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>sumcraft-repo</id>
|
||||
<url>http://ci.sumcraft.net:8080/plugin/repository/everything/</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
@ -40,13 +70,19 @@
|
||||
<type>jar</type>
|
||||
<version>1.8.3-R0.1-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<dependency>
|
||||
<groupId>net.milkbowl.vault</groupId>
|
||||
<artifactId>VaultAPI</artifactId>
|
||||
<version>1.5</version>
|
||||
<scope>system</scope>
|
||||
<systemPath>${project.basedir}/lib/Vault.jar</systemPath>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cn.citycraft</groupId>
|
||||
<artifactId>PluginHelper</artifactId>
|
||||
<type>jar</type>
|
||||
<version>1.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
|
@ -1,46 +0,0 @@
|
||||
package cn.citycraft.RealBackpacks.config;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
|
||||
|
||||
public class Config extends ConfigLoader {
|
||||
private static String CONFIG_NAME = "config.yml";
|
||||
private static FileConfig instance;
|
||||
private static File file;
|
||||
|
||||
public Config(Plugin p) {
|
||||
super(p, CONFIG_NAME);
|
||||
file = new File(p.getDataFolder(), CONFIG_NAME);
|
||||
instance = super.getInstance();
|
||||
}
|
||||
|
||||
public static void load(Plugin p) {
|
||||
new Config(p);
|
||||
}
|
||||
|
||||
public static FileConfig getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public static String getMessage(String path) {
|
||||
String message = instance.getString(path).replaceAll("&", "§");
|
||||
return message;
|
||||
}
|
||||
|
||||
public static String[] getStringArray(String path) {
|
||||
return instance.getStringList(path).toArray(new String[0]);
|
||||
}
|
||||
|
||||
public static void save(){
|
||||
try {
|
||||
instance.save(file);
|
||||
} catch (IOException e) {
|
||||
saveError(file);
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,102 +0,0 @@
|
||||
package cn.citycraft.RealBackpacks.config;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
public class ConfigLoader extends FileConfig {
|
||||
protected static FileConfig config;
|
||||
protected static boolean tip = true;
|
||||
protected static Plugin plugin;
|
||||
|
||||
public ConfigLoader(Plugin p, File file) {
|
||||
ConfigLoader.plugin = p;
|
||||
config = loadConfig(p, file, null, true);
|
||||
}
|
||||
|
||||
public ConfigLoader(Plugin p, File file, boolean res) {
|
||||
ConfigLoader.plugin = p;
|
||||
config = loadConfig(p, file, null, res);
|
||||
}
|
||||
|
||||
public ConfigLoader(Plugin p, File file, String ver) {
|
||||
ConfigLoader.plugin = p;
|
||||
config = loadConfig(p, file, ver, true);
|
||||
}
|
||||
|
||||
public ConfigLoader(Plugin p, File file, String ver, boolean res) {
|
||||
ConfigLoader.plugin = p;
|
||||
config = loadConfig(p, file, ver, res);
|
||||
}
|
||||
|
||||
public ConfigLoader(Plugin p, String filename) {
|
||||
ConfigLoader.plugin = p;
|
||||
config = loadConfig(p, new File(p.getDataFolder(), filename), null,
|
||||
true);
|
||||
}
|
||||
|
||||
public ConfigLoader(Plugin p, String filename, boolean res) {
|
||||
ConfigLoader.plugin = p;
|
||||
config = loadConfig(p, new File(p.getDataFolder(), filename), null, res);
|
||||
}
|
||||
|
||||
public ConfigLoader(Plugin p, String filename, String ver) {
|
||||
ConfigLoader.plugin = p;
|
||||
config = loadConfig(p, new File(p.getDataFolder(), filename), ver, true);
|
||||
}
|
||||
|
||||
public ConfigLoader(Plugin p, String filename, String ver, boolean res) {
|
||||
ConfigLoader.plugin = p;
|
||||
config = loadConfig(p, new File(p.getDataFolder(), filename), ver, true);
|
||||
}
|
||||
|
||||
public static FileConfig getInstance() {
|
||||
return config;
|
||||
}
|
||||
|
||||
public FileConfig loadConfig(Plugin p, File file, String ver, boolean res) {
|
||||
tip = res ;
|
||||
if (!file.getParentFile().exists()) {
|
||||
file.getParentFile().mkdirs();
|
||||
p.getLogger().info("创建新的文件夹" + file.getParentFile().getAbsolutePath() + "...");
|
||||
}
|
||||
if (!file.exists()) {
|
||||
fileCreate(p, file, res);
|
||||
} else {
|
||||
if (ver != null) {
|
||||
FileConfig configcheck = init(file);
|
||||
String version = configcheck.getString("version");
|
||||
if (version == null || !version.equals(ver)) {
|
||||
p.saveResource(file.getName(), true);
|
||||
p.getLogger().warning(
|
||||
"配置文件: " + file.getName() + " 版本过低 正在升级...");
|
||||
}
|
||||
}
|
||||
}
|
||||
if (tip)
|
||||
p.getLogger().info(
|
||||
"载入配置文件: " + file.getName()
|
||||
+ (ver != null ? " 版本: " + ver : ""));
|
||||
return init(file);
|
||||
}
|
||||
|
||||
private void fileCreate(Plugin p, File file, boolean res) {
|
||||
if (res) {
|
||||
p.saveResource(file.getName(), false);
|
||||
} else {
|
||||
try {
|
||||
p.getLogger().info("创建新的配置文件" + file.getAbsolutePath() + "...");
|
||||
file.createNewFile();
|
||||
} catch (IOException e) {
|
||||
p.getLogger().info("配置文件" + file.getName() + "创建失败...");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void saveError(File file) {
|
||||
plugin.getLogger().info("配置文件" + file.getName() + "保存错误...");
|
||||
}
|
||||
|
||||
}
|
@ -1,108 +0,0 @@
|
||||
package cn.citycraft.RealBackpacks.config;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.Reader;
|
||||
import java.io.Writer;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import org.apache.commons.lang.Validate;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.configuration.Configuration;
|
||||
import org.bukkit.configuration.InvalidConfigurationException;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConstructor;
|
||||
import org.bukkit.configuration.file.YamlRepresenter;
|
||||
import org.yaml.snakeyaml.DumperOptions;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
import org.yaml.snakeyaml.representer.Representer;
|
||||
|
||||
import com.google.common.base.Charsets;
|
||||
import com.google.common.io.Files;
|
||||
|
||||
/**
|
||||
* An implementation of {@link Configuration} which saves all files in Yaml. Note that this
|
||||
* implementation is not synchronized.
|
||||
*/
|
||||
public class FileConfig extends YamlConfiguration {
|
||||
|
||||
public static FileConfig init(File file) {
|
||||
return FileConfig.loadConfiguration(file);
|
||||
}
|
||||
|
||||
public static FileConfig loadConfiguration(File file) {
|
||||
Validate.notNull(file, "File cannot be null");
|
||||
FileConfig config = new FileConfig();
|
||||
try {
|
||||
config.load(file);
|
||||
} catch (FileNotFoundException ex) {
|
||||
} catch (IOException ex) {
|
||||
Bukkit.getLogger().log(Level.SEVERE, "Cannot load " + file, ex);
|
||||
} catch (InvalidConfigurationException ex) {
|
||||
Bukkit.getLogger().log(Level.SEVERE, "Cannot load " + file, ex);
|
||||
}
|
||||
return config;
|
||||
}
|
||||
|
||||
protected final DumperOptions yamlOptions = new DumperOptions();
|
||||
|
||||
protected final Representer yamlRepresenter = new YamlRepresenter();
|
||||
|
||||
protected final Yaml yaml = new Yaml(new YamlConstructor(), yamlRepresenter, yamlOptions);
|
||||
|
||||
@Override
|
||||
public void load(File file) throws FileNotFoundException, IOException, InvalidConfigurationException {
|
||||
Validate.notNull(file, "File cannot be null");
|
||||
final FileInputStream stream = new FileInputStream(file);
|
||||
load(new InputStreamReader(stream, Charsets.UTF_8));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load(Reader reader) throws IOException, InvalidConfigurationException {
|
||||
BufferedReader input = (reader instanceof BufferedReader) ? (BufferedReader) reader
|
||||
: new BufferedReader(reader);
|
||||
StringBuilder builder = new StringBuilder();
|
||||
try {
|
||||
String line;
|
||||
while ((line = input.readLine()) != null) {
|
||||
builder.append(line);
|
||||
builder.append('\n');
|
||||
}
|
||||
} finally {
|
||||
input.close();
|
||||
}
|
||||
loadFromString(builder.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(File file) throws IOException {
|
||||
Validate.notNull(file, "File cannot be null");
|
||||
Files.createParentDirs(file);
|
||||
String data = saveToString();
|
||||
Writer writer = new OutputStreamWriter(new FileOutputStream(file), Charsets.UTF_8);
|
||||
try {
|
||||
writer.write(data);
|
||||
} finally {
|
||||
writer.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String saveToString() {
|
||||
yamlOptions.setIndent(options().indent());
|
||||
yamlOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
|
||||
yamlRepresenter.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
|
||||
String header = buildHeader();
|
||||
String dump = yaml.dump(getValues(false));
|
||||
if (dump.equals(BLANK_CONFIG)) {
|
||||
dump = "";
|
||||
}
|
||||
return header + dump;
|
||||
}
|
||||
}
|
@ -1,48 +0,0 @@
|
||||
package cn.citycraft.RealBackpacks.config;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
public class PlayerConfig extends ConfigLoader {
|
||||
private static String CONFIG_FOLDER = "userdate";
|
||||
private static FileConfig instance;
|
||||
private static File file;
|
||||
|
||||
public PlayerConfig(Plugin p, String player) {
|
||||
super(p, CONFIG_FOLDER + File.separator + player + ".yml", false);
|
||||
file = new File(p.getDataFolder(), CONFIG_FOLDER + File.separator
|
||||
+ player + ".yml");
|
||||
instance = super.getInstance();
|
||||
}
|
||||
|
||||
public static FileConfig getInstance(Plugin p, Player player) {
|
||||
new PlayerConfig(p, player.getName());
|
||||
return instance;
|
||||
}
|
||||
|
||||
public static FileConfig getInstance(Plugin p, String player) {
|
||||
new PlayerConfig(p, player);
|
||||
return instance;
|
||||
}
|
||||
|
||||
public static String getMessage(String path) {
|
||||
String message = instance.getString(path).replaceAll("&", "§");
|
||||
return message;
|
||||
}
|
||||
|
||||
public static String[] getStringArray(String path) {
|
||||
return instance.getStringList(path).toArray(new String[0]);
|
||||
}
|
||||
|
||||
public static void save() {
|
||||
try {
|
||||
instance.save(file);
|
||||
} catch (IOException e) {
|
||||
saveError(file);
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,419 +0,0 @@
|
||||
package cn.citycraft.RealBackpacks.json;
|
||||
|
||||
/*
|
||||
* Copyright (c) 2002 JSON.org
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* The Software shall be used for Good, not Evil.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
public class JSONArray {
|
||||
|
||||
private final ArrayList<Object> myArrayList;
|
||||
|
||||
public JSONArray() {
|
||||
this.myArrayList = new ArrayList<Object>();
|
||||
}
|
||||
|
||||
public JSONArray(Collection<?> collection) {
|
||||
this.myArrayList = new ArrayList<Object>();
|
||||
if (collection != null) {
|
||||
Iterator<?> iter = collection.iterator();
|
||||
while (iter.hasNext()) {
|
||||
this.myArrayList.add(JSONObject.wrap(iter.next()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public JSONArray(JSONTokener x) throws JSONException {
|
||||
this();
|
||||
if (x.nextClean() != '[')
|
||||
throw x.syntaxError("A JSONArray text must start with '['");
|
||||
if (x.nextClean() != ']') {
|
||||
x.back();
|
||||
for (;;) {
|
||||
if (x.nextClean() == ',') {
|
||||
x.back();
|
||||
this.myArrayList.add(JSONObject.NULL);
|
||||
} else {
|
||||
x.back();
|
||||
this.myArrayList.add(x.nextValue());
|
||||
}
|
||||
switch (x.nextClean()) {
|
||||
case ';':
|
||||
case ',':
|
||||
if (x.nextClean() == ']')
|
||||
return;
|
||||
x.back();
|
||||
break;
|
||||
case ']':
|
||||
return;
|
||||
default:
|
||||
throw x.syntaxError("Expected a ',' or ']'");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public JSONArray(Object array) throws JSONException {
|
||||
this();
|
||||
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)));
|
||||
}
|
||||
} else
|
||||
throw new JSONException(
|
||||
"JSONArray initial value should be a string or collection or array.");
|
||||
}
|
||||
|
||||
public JSONArray(String source) throws JSONException {
|
||||
this(new JSONTokener(source));
|
||||
}
|
||||
|
||||
public Object get(int index) throws JSONException {
|
||||
Object object = this.opt(index);
|
||||
if (object == null)
|
||||
throw new JSONException("JSONArray[" + index + "] not found.");
|
||||
return object;
|
||||
}
|
||||
|
||||
public boolean getBoolean(int index) throws JSONException {
|
||||
Object object = this.get(index);
|
||||
if (object.equals(Boolean.FALSE)
|
||||
|| (object instanceof String && ((String) object)
|
||||
.equalsIgnoreCase("false")))
|
||||
return false;
|
||||
else if (object.equals(Boolean.TRUE)
|
||||
|| (object instanceof String && ((String) object)
|
||||
.equalsIgnoreCase("true")))
|
||||
return true;
|
||||
throw new JSONException("JSONArray[" + index + "] is not a boolean.");
|
||||
}
|
||||
|
||||
public double getDouble(int index) throws JSONException {
|
||||
Object object = this.get(index);
|
||||
try {
|
||||
return object instanceof Number ? ((Number) object).doubleValue()
|
||||
: Double.parseDouble((String) object);
|
||||
} catch (Exception e) {
|
||||
throw new JSONException("JSONArray[" + index + "] is not a number.");
|
||||
}
|
||||
}
|
||||
|
||||
public int getInt(int index) throws JSONException {
|
||||
Object object = this.get(index);
|
||||
try {
|
||||
return object instanceof Number ? ((Number) object).intValue()
|
||||
: Integer.parseInt((String) object);
|
||||
} catch (Exception e) {
|
||||
throw new JSONException("JSONArray[" + index + "] is not a number.");
|
||||
}
|
||||
}
|
||||
|
||||
public JSONArray getJSONArray(int index) throws JSONException {
|
||||
Object object = this.get(index);
|
||||
if (object instanceof JSONArray)
|
||||
return (JSONArray) object;
|
||||
throw new JSONException("JSONArray[" + index + "] is not a JSONArray.");
|
||||
}
|
||||
|
||||
public JSONObject getJSONObject(int index) throws JSONException {
|
||||
Object object = this.get(index);
|
||||
if (object instanceof JSONObject)
|
||||
return (JSONObject) object;
|
||||
throw new JSONException("JSONArray[" + index + "] is not a JSONObject.");
|
||||
}
|
||||
|
||||
public long getLong(int index) throws JSONException {
|
||||
Object object = this.get(index);
|
||||
try {
|
||||
return object instanceof Number ? ((Number) object).longValue()
|
||||
: Long.parseLong((String) object);
|
||||
} catch (Exception e) {
|
||||
throw new JSONException("JSONArray[" + index + "] is not a number.");
|
||||
}
|
||||
}
|
||||
|
||||
public String getString(int index) throws JSONException {
|
||||
Object object = this.get(index);
|
||||
if (object instanceof String)
|
||||
return (String) object;
|
||||
throw new JSONException("JSONArray[" + index + "] not a string.");
|
||||
}
|
||||
|
||||
public boolean isNull(int index) {
|
||||
return JSONObject.NULL.equals(this.opt(index));
|
||||
}
|
||||
|
||||
public String join(String separator) throws JSONException {
|
||||
int len = this.length();
|
||||
StringBuffer sb = new StringBuffer();
|
||||
|
||||
for (int i = 0; i < len; i += 1) {
|
||||
if (i > 0) {
|
||||
sb.append(separator);
|
||||
}
|
||||
sb.append(JSONObject.valueToString(this.myArrayList.get(i)));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public int length() {
|
||||
return this.myArrayList.size();
|
||||
}
|
||||
|
||||
public Object opt(int index) {
|
||||
return (index < 0 || index >= this.length()) ? null : this.myArrayList
|
||||
.get(index);
|
||||
}
|
||||
|
||||
public boolean optBoolean(int index) {
|
||||
return this.optBoolean(index, false);
|
||||
}
|
||||
|
||||
public boolean optBoolean(int index, boolean defaultValue) {
|
||||
try {
|
||||
return this.getBoolean(index);
|
||||
} catch (Exception e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
public double optDouble(int index) {
|
||||
return this.optDouble(index, Double.NaN);
|
||||
}
|
||||
|
||||
public double optDouble(int index, double defaultValue) {
|
||||
try {
|
||||
return this.getDouble(index);
|
||||
} catch (Exception e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
public int optInt(int index) {
|
||||
return this.optInt(index, 0);
|
||||
}
|
||||
|
||||
public int optInt(int index, int defaultValue) {
|
||||
try {
|
||||
return this.getInt(index);
|
||||
} catch (Exception e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
public JSONArray optJSONArray(int index) {
|
||||
Object o = this.opt(index);
|
||||
return o instanceof JSONArray ? (JSONArray) o : null;
|
||||
}
|
||||
|
||||
public JSONObject optJSONObject(int index) {
|
||||
Object o = this.opt(index);
|
||||
return o instanceof JSONObject ? (JSONObject) o : null;
|
||||
}
|
||||
|
||||
public long optLong(int index) {
|
||||
return this.optLong(index, 0);
|
||||
}
|
||||
|
||||
public long optLong(int index, long defaultValue) {
|
||||
try {
|
||||
return this.getLong(index);
|
||||
} catch (Exception e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
public String optString(int index) {
|
||||
return this.optString(index, "");
|
||||
}
|
||||
|
||||
public String optString(int index, String defaultValue) {
|
||||
Object object = this.opt(index);
|
||||
return JSONObject.NULL.equals(object) ? defaultValue : object
|
||||
.toString();
|
||||
}
|
||||
|
||||
public JSONArray put(boolean value) {
|
||||
this.put(value ? Boolean.TRUE : Boolean.FALSE);
|
||||
return this;
|
||||
}
|
||||
|
||||
public JSONArray put(Collection<?> value) {
|
||||
this.put(new JSONArray(value));
|
||||
return this;
|
||||
}
|
||||
|
||||
public JSONArray put(double value) throws JSONException {
|
||||
Double d = new Double(value);
|
||||
JSONObject.testValidity(d);
|
||||
this.put(d);
|
||||
return this;
|
||||
}
|
||||
|
||||
public JSONArray put(int value) {
|
||||
this.put(new Integer(value));
|
||||
return this;
|
||||
}
|
||||
|
||||
public JSONArray put(int index, boolean value) throws JSONException {
|
||||
this.put(index, value ? Boolean.TRUE : Boolean.FALSE);
|
||||
return this;
|
||||
}
|
||||
|
||||
public JSONArray put(int index, Collection<?> value) throws JSONException {
|
||||
this.put(index, new JSONArray(value));
|
||||
return this;
|
||||
}
|
||||
|
||||
public JSONArray put(int index, double value) throws JSONException {
|
||||
this.put(index, new Double(value));
|
||||
return this;
|
||||
}
|
||||
|
||||
public JSONArray put(int index, int value) throws JSONException {
|
||||
this.put(index, new Integer(value));
|
||||
return this;
|
||||
}
|
||||
|
||||
public JSONArray put(int index, long value) throws JSONException {
|
||||
this.put(index, new Long(value));
|
||||
return this;
|
||||
}
|
||||
|
||||
public JSONArray put(int index, Map<?, ?> value) throws JSONException {
|
||||
this.put(index, new JSONObject(value));
|
||||
return this;
|
||||
}
|
||||
|
||||
public JSONArray put(int index, Object value) throws JSONException {
|
||||
JSONObject.testValidity(value);
|
||||
if (index < 0)
|
||||
throw new JSONException("JSONArray[" + index + "] not found.");
|
||||
if (index < this.length()) {
|
||||
this.myArrayList.set(index, value);
|
||||
} else {
|
||||
while (index != this.length()) {
|
||||
this.put(JSONObject.NULL);
|
||||
}
|
||||
this.put(value);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public JSONArray put(long value) {
|
||||
this.put(new Long(value));
|
||||
return this;
|
||||
}
|
||||
|
||||
public JSONArray put(Map<?, ?> value) {
|
||||
this.put(new JSONObject());
|
||||
return this;
|
||||
}
|
||||
|
||||
public JSONArray put(Object value) {
|
||||
this.myArrayList.add(value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Object remove(int index) {
|
||||
Object o = this.opt(index);
|
||||
this.myArrayList.remove(index);
|
||||
return o;
|
||||
}
|
||||
|
||||
public JSONObject toJSONObject(JSONArray names) throws JSONException {
|
||||
if (names == null || names.length() == 0 || this.length() == 0)
|
||||
return null;
|
||||
JSONObject jo = new JSONObject();
|
||||
for (int i = 0; i < names.length(); i += 1) {
|
||||
jo.put(names.getString(i), this.opt(i));
|
||||
}
|
||||
return jo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
try {
|
||||
return this.toString(0);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public String toString(int indentFactor) throws JSONException {
|
||||
StringWriter sw = new StringWriter();
|
||||
synchronized (sw.getBuffer()) {
|
||||
return this.write(sw, indentFactor, 0).toString();
|
||||
}
|
||||
}
|
||||
|
||||
public Writer write(Writer writer) throws JSONException {
|
||||
return this.write(writer, 0, 0);
|
||||
}
|
||||
|
||||
Writer write(Writer writer, int indentFactor, int indent)
|
||||
throws JSONException {
|
||||
try {
|
||||
boolean commanate = false;
|
||||
int length = this.length();
|
||||
writer.write('[');
|
||||
|
||||
if (length == 1) {
|
||||
JSONObject.writeValue(writer, this.myArrayList.get(0),
|
||||
indentFactor, indent);
|
||||
} else if (length != 0) {
|
||||
final int newindent = indent + indentFactor;
|
||||
|
||||
for (int i = 0; i < length; i += 1) {
|
||||
if (commanate) {
|
||||
writer.write(',');
|
||||
}
|
||||
if (indentFactor > 0) {
|
||||
writer.write('\n');
|
||||
}
|
||||
JSONObject.indent(writer, newindent);
|
||||
JSONObject.writeValue(writer, this.myArrayList.get(i),
|
||||
indentFactor, newindent);
|
||||
commanate = true;
|
||||
}
|
||||
if (indentFactor > 0) {
|
||||
writer.write('\n');
|
||||
}
|
||||
JSONObject.indent(writer, indent);
|
||||
}
|
||||
writer.write(']');
|
||||
return writer;
|
||||
} catch (IOException e) {
|
||||
throw new JSONException(e);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
package cn.citycraft.RealBackpacks.json;
|
||||
|
||||
public class JSONException extends Exception {
|
||||
|
||||
private static final long serialVersionUID = 0;
|
||||
private Throwable cause;
|
||||
|
||||
public JSONException(final String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public JSONException(final Throwable cause) {
|
||||
super(cause.getMessage());
|
||||
this.cause = cause;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Throwable getCause() {
|
||||
return this.cause;
|
||||
}
|
||||
}
|
@ -1,926 +0,0 @@
|
||||
package cn.citycraft.RealBackpacks.json;
|
||||
|
||||
/*
|
||||
* Copyright (c) 2002 JSON.org
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* The Software shall be used for Good, not Evil.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.Collection;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.ResourceBundle;
|
||||
import java.util.Set;
|
||||
|
||||
public class JSONObject {
|
||||
|
||||
private static final int keyPoolSize = 100;
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
private static HashMap keyPool = new HashMap(keyPoolSize);
|
||||
|
||||
private static final class Null {
|
||||
|
||||
@Override
|
||||
protected final Object clone() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object object) {
|
||||
return object == null || object == this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "null";
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
private final Map map;
|
||||
|
||||
public static final Object NULL = new Null();
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public JSONObject() {
|
||||
this.map = new HashMap();
|
||||
}
|
||||
|
||||
public JSONObject(final JSONObject jo, final String[] names) {
|
||||
this();
|
||||
for (int i = 0; i < names.length; i += 1) {
|
||||
try {
|
||||
this.putOnce(names[i], jo.opt(names[i]));
|
||||
} catch (final Exception ignore) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public JSONObject(final JSONTokener x) throws JSONException {
|
||||
this();
|
||||
char c;
|
||||
String key;
|
||||
|
||||
if (x.nextClean() != '{') {
|
||||
throw x.syntaxError("A JSONObject text must begin with '{'");
|
||||
}
|
||||
for (;;) {
|
||||
c = x.nextClean();
|
||||
switch (c) {
|
||||
case 0:
|
||||
throw x.syntaxError("A JSONObject text must end with '}'");
|
||||
case '}':
|
||||
return;
|
||||
default:
|
||||
x.back();
|
||||
key = x.nextValue().toString();
|
||||
}
|
||||
|
||||
// The key is followed by ':'. We will also tolerate '=' or '=>'.
|
||||
|
||||
c = x.nextClean();
|
||||
if (c == '=') {
|
||||
if (x.next() != '>') {
|
||||
x.back();
|
||||
}
|
||||
} else if (c != ':') {
|
||||
throw x.syntaxError("Expected a ':' after a key");
|
||||
}
|
||||
this.putOnce(key, x.nextValue());
|
||||
|
||||
// Pairs are separated by ','. We will also tolerate ';'.
|
||||
|
||||
switch (x.nextClean()) {
|
||||
case ';':
|
||||
case ',':
|
||||
if (x.nextClean() == '}') {
|
||||
return;
|
||||
}
|
||||
x.back();
|
||||
break;
|
||||
case '}':
|
||||
return;
|
||||
default:
|
||||
throw x.syntaxError("Expected a ',' or '}'");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
public JSONObject(final Map map) {
|
||||
this.map = new HashMap();
|
||||
if (map != null) {
|
||||
final Iterator i = map.entrySet().iterator();
|
||||
while (i.hasNext()) {
|
||||
final Map.Entry e = (Map.Entry) i.next();
|
||||
final Object value = e.getValue();
|
||||
if (value != null) {
|
||||
this.map.put(e.getKey(), wrap(value));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public JSONObject(final Object bean) {
|
||||
this();
|
||||
this.populateMap(bean);
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public JSONObject(final Object object, final String names[]) {
|
||||
this();
|
||||
final Class c = object.getClass();
|
||||
for (int i = 0; i < names.length; i += 1) {
|
||||
final String name = names[i];
|
||||
try {
|
||||
this.putOpt(name, c.getField(name).get(object));
|
||||
} catch (final Exception ignore) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public JSONObject(final String source) throws JSONException {
|
||||
this(new JSONTokener(source));
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public JSONObject(final String baseName, final Locale locale) throws JSONException {
|
||||
this();
|
||||
final ResourceBundle bundle = ResourceBundle.getBundle(baseName, locale, Thread.currentThread().getContextClassLoader());
|
||||
|
||||
// Iterate through the keys in the bundle.
|
||||
|
||||
final Enumeration keys = bundle.getKeys();
|
||||
while (keys.hasMoreElements()) {
|
||||
final Object key = keys.nextElement();
|
||||
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
|
||||
// the deepest nested JSONObject.
|
||||
|
||||
final String[] path = ((String) key).split("\\.");
|
||||
final int last = path.length - 1;
|
||||
JSONObject target = this;
|
||||
for (int i = 0; i < last; i += 1) {
|
||||
final String segment = path[i];
|
||||
JSONObject nextTarget = target.optJSONObject(segment);
|
||||
if (nextTarget == null) {
|
||||
nextTarget = new JSONObject();
|
||||
target.put(segment, nextTarget);
|
||||
}
|
||||
target = nextTarget;
|
||||
}
|
||||
target.put(path[last], bundle.getString((String) key));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public JSONObject accumulate(final String key, final Object value) throws JSONException {
|
||||
testValidity(value);
|
||||
final Object object = this.opt(key);
|
||||
if (object == null) {
|
||||
this.put(key, value instanceof JSONArray ? new JSONArray().put(value) : value);
|
||||
} else if (object instanceof JSONArray) {
|
||||
((JSONArray) object).put(value);
|
||||
} else {
|
||||
this.put(key, new JSONArray().put(object).put(value));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public JSONObject append(final String key, final Object value) throws JSONException {
|
||||
testValidity(value);
|
||||
final Object object = this.opt(key);
|
||||
if (object == null) {
|
||||
this.put(key, new JSONArray().put(value));
|
||||
} else if (object instanceof JSONArray) {
|
||||
this.put(key, ((JSONArray) object).put(value));
|
||||
} else {
|
||||
throw new JSONException("JSONObject[" + key + "] is not a JSONArray.");
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public static String doubleToString(final double 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 && string.indexOf('E') < 0) {
|
||||
while (string.endsWith("0")) {
|
||||
string = string.substring(0, string.length() - 1);
|
||||
}
|
||||
if (string.endsWith(".")) {
|
||||
string = string.substring(0, string.length() - 1);
|
||||
}
|
||||
}
|
||||
return string;
|
||||
}
|
||||
|
||||
public Object get(final String key) throws JSONException {
|
||||
if (key == null) {
|
||||
throw new JSONException("Null key.");
|
||||
}
|
||||
final Object object = this.opt(key);
|
||||
if (object == null) {
|
||||
throw new JSONException("JSONObject[" + quote(key) + "] not found.");
|
||||
}
|
||||
return object;
|
||||
}
|
||||
|
||||
public boolean getBoolean(final String key) throws JSONException {
|
||||
final Object object = this.get(key);
|
||||
if (object.equals(Boolean.FALSE) || (object instanceof String && ((String) object).equalsIgnoreCase("false"))) {
|
||||
return false;
|
||||
} else if (object.equals(Boolean.TRUE) || (object instanceof String && ((String) object).equalsIgnoreCase("true"))) {
|
||||
return true;
|
||||
}
|
||||
throw new JSONException("JSONObject[" + quote(key) + "] is not a Boolean.");
|
||||
}
|
||||
|
||||
public double getDouble(final String key) throws JSONException {
|
||||
final Object object = this.get(key);
|
||||
try {
|
||||
return object instanceof Number ? ((Number) object).doubleValue() : Double.parseDouble((String) object);
|
||||
} catch (final Exception e) {
|
||||
throw new JSONException("JSONObject[" + quote(key) + "] is not a number.");
|
||||
}
|
||||
}
|
||||
|
||||
public int getInt(final String key) throws JSONException {
|
||||
final Object object = this.get(key);
|
||||
try {
|
||||
return object instanceof Number ? ((Number) object).intValue() : Integer.parseInt((String) object);
|
||||
} catch (final Exception e) {
|
||||
throw new JSONException("JSONObject[" + quote(key) + "] is not an int.");
|
||||
}
|
||||
}
|
||||
|
||||
public JSONArray getJSONArray(final String key) throws JSONException {
|
||||
final Object object = this.get(key);
|
||||
if (object instanceof JSONArray) {
|
||||
return (JSONArray) object;
|
||||
}
|
||||
throw new JSONException("JSONObject[" + quote(key) + "] is not a JSONArray.");
|
||||
}
|
||||
|
||||
public JSONObject getJSONObject(final String key) throws JSONException {
|
||||
final Object object = this.get(key);
|
||||
if (object instanceof JSONObject) {
|
||||
return (JSONObject) object;
|
||||
}
|
||||
throw new JSONException("JSONObject[" + quote(key) + "] is not a JSONObject.");
|
||||
}
|
||||
|
||||
public long getLong(final String key) throws JSONException {
|
||||
final Object object = this.get(key);
|
||||
try {
|
||||
return object instanceof Number ? ((Number) object).longValue() : Long.parseLong((String) object);
|
||||
} catch (final Exception e) {
|
||||
throw new JSONException("JSONObject[" + quote(key) + "] is not a long.");
|
||||
}
|
||||
}
|
||||
|
||||
public static String[] getNames(final JSONObject jo) {
|
||||
final int length = jo.length();
|
||||
if (length == 0) {
|
||||
return null;
|
||||
}
|
||||
@SuppressWarnings("rawtypes")
|
||||
final Iterator iterator = jo.keys();
|
||||
final String[] names = new String[length];
|
||||
int i = 0;
|
||||
while (iterator.hasNext()) {
|
||||
names[i] = (String) iterator.next();
|
||||
i += 1;
|
||||
}
|
||||
return names;
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public static String[] getNames(final Object object) {
|
||||
if (object == null) {
|
||||
return null;
|
||||
}
|
||||
final Class klass = object.getClass();
|
||||
final Field[] fields = klass.getFields();
|
||||
final int length = fields.length;
|
||||
if (length == 0) {
|
||||
return null;
|
||||
}
|
||||
final String[] names = new String[length];
|
||||
for (int i = 0; i < length; i += 1) {
|
||||
names[i] = fields[i].getName();
|
||||
}
|
||||
return names;
|
||||
}
|
||||
|
||||
public String getString(final String key) throws JSONException {
|
||||
final Object object = this.get(key);
|
||||
if (object instanceof String) {
|
||||
return (String) object;
|
||||
}
|
||||
throw new JSONException("JSONObject[" + quote(key) + "] not a string.");
|
||||
}
|
||||
|
||||
public boolean has(final String key) {
|
||||
return this.map.containsKey(key);
|
||||
}
|
||||
|
||||
public JSONObject increment(final String key) throws JSONException {
|
||||
final Object value = this.opt(key);
|
||||
if (value == null) {
|
||||
this.put(key, 1);
|
||||
} else if (value instanceof Integer) {
|
||||
this.put(key, ((Integer) value).intValue() + 1);
|
||||
} else if (value instanceof Long) {
|
||||
this.put(key, ((Long) value).longValue() + 1);
|
||||
} else if (value instanceof Double) {
|
||||
this.put(key, ((Double) value).doubleValue() + 1);
|
||||
} else if (value instanceof Float) {
|
||||
this.put(key, ((Float) value).floatValue() + 1);
|
||||
} else {
|
||||
throw new JSONException("Unable to increment [" + quote(key) + "].");
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean isNull(final String key) {
|
||||
return JSONObject.NULL.equals(this.opt(key));
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public Iterator keys() {
|
||||
return this.keySet().iterator();
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public Set keySet() {
|
||||
return this.map.keySet();
|
||||
}
|
||||
|
||||
public int length() {
|
||||
return this.map.size();
|
||||
}
|
||||
|
||||
public JSONArray names() {
|
||||
final JSONArray ja = new JSONArray();
|
||||
@SuppressWarnings("rawtypes")
|
||||
final Iterator keys = this.keys();
|
||||
while (keys.hasNext()) {
|
||||
ja.put(keys.next());
|
||||
}
|
||||
return ja.length() == 0 ? null : ja;
|
||||
}
|
||||
|
||||
public static String numberToString(final Number number) throws JSONException {
|
||||
if (number == null) {
|
||||
throw new JSONException("Null pointer");
|
||||
}
|
||||
testValidity(number);
|
||||
|
||||
// Shave off trailing zeros and decimal point, if possible.
|
||||
|
||||
String string = number.toString();
|
||||
if (string.indexOf('.') > 0 && string.indexOf('e') < 0 && string.indexOf('E') < 0) {
|
||||
while (string.endsWith("0")) {
|
||||
string = string.substring(0, string.length() - 1);
|
||||
}
|
||||
if (string.endsWith(".")) {
|
||||
string = string.substring(0, string.length() - 1);
|
||||
}
|
||||
}
|
||||
return string;
|
||||
}
|
||||
|
||||
public Object opt(final String key) {
|
||||
return key == null ? null : this.map.get(key);
|
||||
}
|
||||
|
||||
public boolean optBoolean(final String key) {
|
||||
return this.optBoolean(key, false);
|
||||
}
|
||||
|
||||
public boolean optBoolean(final String key, final boolean defaultValue) {
|
||||
try {
|
||||
return this.getBoolean(key);
|
||||
} catch (final Exception e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
public double optDouble(final String key) {
|
||||
return this.optDouble(key, Double.NaN);
|
||||
}
|
||||
|
||||
public double optDouble(final String key, final double defaultValue) {
|
||||
try {
|
||||
return this.getDouble(key);
|
||||
} catch (final Exception e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
public int optInt(final String key) {
|
||||
return this.optInt(key, 0);
|
||||
}
|
||||
|
||||
public int optInt(final String key, final int defaultValue) {
|
||||
try {
|
||||
return this.getInt(key);
|
||||
} catch (final Exception e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
public JSONArray optJSONArray(final String key) {
|
||||
final Object o = this.opt(key);
|
||||
return o instanceof JSONArray ? (JSONArray) o : null;
|
||||
}
|
||||
|
||||
public JSONObject optJSONObject(final String key) {
|
||||
final Object object = this.opt(key);
|
||||
return object instanceof JSONObject ? (JSONObject) object : null;
|
||||
}
|
||||
|
||||
public long optLong(final String key) {
|
||||
return this.optLong(key, 0);
|
||||
}
|
||||
|
||||
public long optLong(final String key, final long defaultValue) {
|
||||
try {
|
||||
return this.getLong(key);
|
||||
} catch (final Exception e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
public String optString(final String key) {
|
||||
return this.optString(key, "");
|
||||
}
|
||||
|
||||
public String optString(final String key, final String defaultValue) {
|
||||
final Object object = this.opt(key);
|
||||
return NULL.equals(object) ? defaultValue : object.toString();
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
private void populateMap(final Object bean) {
|
||||
final Class klass = bean.getClass();
|
||||
|
||||
// If klass is a System class then set includeSuperClass to false.
|
||||
|
||||
final boolean includeSuperClass = klass.getClassLoader() != null;
|
||||
|
||||
final Method[] methods = includeSuperClass ? klass.getMethods() : klass.getDeclaredMethods();
|
||||
for (int i = 0; i < methods.length; i += 1) {
|
||||
try {
|
||||
final Method method = methods[i];
|
||||
if (Modifier.isPublic(method.getModifiers())) {
|
||||
final String name = method.getName();
|
||||
String key = "";
|
||||
if (name.startsWith("get")) {
|
||||
if ("getClass".equals(name) || "getDeclaringClass".equals(name)) {
|
||||
key = "";
|
||||
} else {
|
||||
key = name.substring(3);
|
||||
}
|
||||
} else if (name.startsWith("is")) {
|
||||
key = name.substring(2);
|
||||
}
|
||||
if (key.length() > 0 && Character.isUpperCase(key.charAt(0)) && method.getParameterTypes().length == 0) {
|
||||
if (key.length() == 1) {
|
||||
key = key.toLowerCase();
|
||||
} else if (!Character.isUpperCase(key.charAt(1))) {
|
||||
key = key.substring(0, 1).toLowerCase() + key.substring(1);
|
||||
}
|
||||
|
||||
final Object result = method.invoke(bean, (Object[]) null);
|
||||
if (result != null) {
|
||||
this.map.put(key, wrap(result));
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (final Exception ignore) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public JSONObject put(final String key, final boolean value) throws JSONException {
|
||||
this.put(key, value ? Boolean.TRUE : Boolean.FALSE);
|
||||
return this;
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public JSONObject put(final String key, final Collection value) throws JSONException {
|
||||
this.put(key, new JSONArray(value));
|
||||
return this;
|
||||
}
|
||||
|
||||
public JSONObject put(final String key, final double value) throws JSONException {
|
||||
this.put(key, new Double(value));
|
||||
return this;
|
||||
}
|
||||
|
||||
public JSONObject put(final String key, final int value) throws JSONException {
|
||||
this.put(key, new Integer(value));
|
||||
return this;
|
||||
}
|
||||
|
||||
public JSONObject put(final String key, final long value) throws JSONException {
|
||||
this.put(key, new Long(value));
|
||||
return this;
|
||||
}
|
||||
|
||||
public JSONObject put(final String key, @SuppressWarnings("rawtypes") final Map value) throws JSONException {
|
||||
this.put(key, new JSONObject(value));
|
||||
return this;
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
public JSONObject put(String key, final Object value) throws JSONException {
|
||||
String pooled;
|
||||
if (key == null) {
|
||||
throw new JSONException("Null key.");
|
||||
}
|
||||
if (value != null) {
|
||||
testValidity(value);
|
||||
pooled = (String) keyPool.get(key);
|
||||
if (pooled == null) {
|
||||
if (keyPool.size() >= keyPoolSize) {
|
||||
keyPool = new HashMap(keyPoolSize);
|
||||
}
|
||||
keyPool.put(key, key);
|
||||
} else {
|
||||
key = pooled;
|
||||
}
|
||||
this.map.put(key, value);
|
||||
} else {
|
||||
this.remove(key);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public JSONObject putOnce(final String key, final Object value) throws JSONException {
|
||||
if (key != null && value != null) {
|
||||
if (this.opt(key) != null) {
|
||||
throw new JSONException("Duplicate key \"" + key + "\"");
|
||||
}
|
||||
this.put(key, value);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public JSONObject putOpt(final String key, final Object value) throws JSONException {
|
||||
if (key != null && value != null) {
|
||||
this.put(key, value);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public static String quote(final String string) {
|
||||
final StringWriter sw = new StringWriter();
|
||||
synchronized (sw.getBuffer()) {
|
||||
try {
|
||||
return quote(string, sw).toString();
|
||||
} catch (final IOException ignored) {
|
||||
// will never happen - we are writing to a string writer
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static Writer quote(final String string, final Writer w) throws IOException {
|
||||
if (string == null || string.length() == 0) {
|
||||
w.write("\"\"");
|
||||
return w;
|
||||
}
|
||||
|
||||
char b;
|
||||
char c = 0;
|
||||
String hhhh;
|
||||
int i;
|
||||
final int len = string.length();
|
||||
|
||||
w.write('"');
|
||||
for (i = 0; i < len; i += 1) {
|
||||
b = c;
|
||||
c = string.charAt(i);
|
||||
switch (c) {
|
||||
case '\\':
|
||||
case '"':
|
||||
w.write('\\');
|
||||
w.write(c);
|
||||
break;
|
||||
case '/':
|
||||
if (b == '<') {
|
||||
w.write('\\');
|
||||
}
|
||||
w.write(c);
|
||||
break;
|
||||
case '\b':
|
||||
w.write("\\b");
|
||||
break;
|
||||
case '\t':
|
||||
w.write("\\t");
|
||||
break;
|
||||
case '\n':
|
||||
w.write("\\n");
|
||||
break;
|
||||
case '\f':
|
||||
w.write("\\f");
|
||||
break;
|
||||
case '\r':
|
||||
w.write("\\r");
|
||||
break;
|
||||
default:
|
||||
if (c < ' ' || (c >= '\u0080' && c < '\u00a0') || (c >= '\u2000' && c < '\u2100')) {
|
||||
w.write("\\u");
|
||||
hhhh = Integer.toHexString(c);
|
||||
w.write("0000", 0, 4 - hhhh.length());
|
||||
w.write(hhhh);
|
||||
} else {
|
||||
w.write(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
w.write('"');
|
||||
return w;
|
||||
}
|
||||
|
||||
public Object remove(final String key) {
|
||||
return this.map.remove(key);
|
||||
}
|
||||
|
||||
public static Object stringToValue(final String string) {
|
||||
Double d;
|
||||
if (string.equals("")) {
|
||||
return string;
|
||||
}
|
||||
if (string.equalsIgnoreCase("true")) {
|
||||
return Boolean.TRUE;
|
||||
}
|
||||
if (string.equalsIgnoreCase("false")) {
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
if (string.equalsIgnoreCase("null")) {
|
||||
return JSONObject.NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* If it might be a number, try converting it.
|
||||
* If a number cannot be produced, then the value will just
|
||||
* be a string. Note that the plus and implied string
|
||||
* conventions are non-standard. A JSON parser may accept
|
||||
* non-JSON forms as long as it accepts all correct JSON forms.
|
||||
*/
|
||||
|
||||
final char b = string.charAt(0);
|
||||
if ((b >= '0' && b <= '9') || b == '.' || b == '-' || b == '+') {
|
||||
try {
|
||||
if (string.indexOf('.') > -1 || string.indexOf('e') > -1 || string.indexOf('E') > -1) {
|
||||
d = Double.valueOf(string);
|
||||
if (!d.isInfinite() && !d.isNaN()) {
|
||||
return d;
|
||||
}
|
||||
} else {
|
||||
final Long myLong = new Long(string);
|
||||
if (myLong.longValue() == myLong.intValue()) {
|
||||
return new Integer(myLong.intValue());
|
||||
} else {
|
||||
return myLong;
|
||||
}
|
||||
}
|
||||
} catch (final Exception ignore) {
|
||||
}
|
||||
}
|
||||
return string;
|
||||
}
|
||||
|
||||
public static void testValidity(final Object o) throws JSONException {
|
||||
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()) {
|
||||
throw new JSONException("JSON does not allow non-finite numbers.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public JSONArray toJSONArray(final JSONArray names) throws JSONException {
|
||||
if (names == null || names.length() == 0) {
|
||||
return null;
|
||||
}
|
||||
final JSONArray ja = new JSONArray();
|
||||
for (int i = 0; i < names.length(); i += 1) {
|
||||
ja.put(this.opt(names.getString(i)));
|
||||
}
|
||||
return ja;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
try {
|
||||
return this.toString(0);
|
||||
} catch (final Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public String toString(final int indentFactor) throws JSONException {
|
||||
final StringWriter w = new StringWriter();
|
||||
synchronized (w.getBuffer()) {
|
||||
return this.write(w, indentFactor, 0).toString();
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public static String valueToString(final Object value) throws JSONException {
|
||||
if (value == null || value.equals(null)) {
|
||||
return "null";
|
||||
}
|
||||
if (value instanceof JSONString) {
|
||||
Object object;
|
||||
try {
|
||||
object = ((JSONString) value).toJSONString();
|
||||
} catch (final Exception e) {
|
||||
throw new JSONException(e);
|
||||
}
|
||||
if (object instanceof String) {
|
||||
return (String) object;
|
||||
}
|
||||
throw new JSONException("Bad value from toJSONString: " + object);
|
||||
}
|
||||
if (value instanceof Number) {
|
||||
return numberToString((Number) value);
|
||||
}
|
||||
if (value instanceof Boolean || value instanceof JSONObject || value instanceof JSONArray) {
|
||||
return value.toString();
|
||||
}
|
||||
if (value instanceof Map) {
|
||||
return new JSONObject((Map) value).toString();
|
||||
}
|
||||
if (value instanceof Collection) {
|
||||
return new JSONArray((Collection) value).toString();
|
||||
}
|
||||
if (value.getClass().isArray()) {
|
||||
return new JSONArray(value).toString();
|
||||
}
|
||||
return quote(value.toString());
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public static Object wrap(final Object object) {
|
||||
try {
|
||||
if (object == null) {
|
||||
return NULL;
|
||||
}
|
||||
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 || object instanceof Long || object instanceof Boolean || object instanceof Float || object instanceof Double || object instanceof String) {
|
||||
return object;
|
||||
}
|
||||
|
||||
if (object instanceof Collection) {
|
||||
return new JSONArray((Collection) object);
|
||||
}
|
||||
if (object.getClass().isArray()) {
|
||||
return new JSONArray(object);
|
||||
}
|
||||
if (object instanceof Map) {
|
||||
return new JSONObject((Map) object);
|
||||
}
|
||||
final Package objectPackage = object.getClass().getPackage();
|
||||
final String objectPackageName = objectPackage != null ? objectPackage.getName() : "";
|
||||
if (objectPackageName.startsWith("java.") || objectPackageName.startsWith("javax.") || object.getClass().getClassLoader() == null) {
|
||||
return object.toString();
|
||||
}
|
||||
return new JSONObject(object);
|
||||
} catch (final Exception exception) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public Writer write(final Writer writer) throws JSONException {
|
||||
return this.write(writer, 0, 0);
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
static final Writer writeValue(final Writer writer, final Object value, final int indentFactor, final int indent) throws JSONException, IOException {
|
||||
if (value == null || value.equals(null)) {
|
||||
writer.write("null");
|
||||
} else if (value instanceof JSONObject) {
|
||||
((JSONObject) value).write(writer, indentFactor, indent);
|
||||
} else if (value instanceof JSONArray) {
|
||||
((JSONArray) value).write(writer, indentFactor, indent);
|
||||
} else if (value instanceof Map) {
|
||||
new JSONObject((Map) value).write(writer, indentFactor, indent);
|
||||
} else if (value instanceof Collection) {
|
||||
new JSONArray((Collection) value).write(writer, indentFactor, indent);
|
||||
} else if (value.getClass().isArray()) {
|
||||
new JSONArray(value).write(writer, indentFactor, indent);
|
||||
} else if (value instanceof Number) {
|
||||
writer.write(numberToString((Number) value));
|
||||
} else if (value instanceof Boolean) {
|
||||
writer.write(value.toString());
|
||||
} else if (value instanceof JSONString) {
|
||||
Object o;
|
||||
try {
|
||||
o = ((JSONString) value).toJSONString();
|
||||
} catch (final Exception e) {
|
||||
throw new JSONException(e);
|
||||
}
|
||||
writer.write(o != null ? o.toString() : quote(value.toString()));
|
||||
} else {
|
||||
quote(value.toString(), writer);
|
||||
}
|
||||
return writer;
|
||||
}
|
||||
|
||||
static final void indent(final Writer writer, final int indent) throws IOException {
|
||||
for (int i = 0; i < indent; i += 1) {
|
||||
writer.write(' ');
|
||||
}
|
||||
}
|
||||
|
||||
Writer write(final Writer writer, final int indentFactor, final int indent) throws JSONException {
|
||||
try {
|
||||
boolean commanate = false;
|
||||
final int length = this.length();
|
||||
@SuppressWarnings("rawtypes")
|
||||
final Iterator keys = this.keys();
|
||||
writer.write('{');
|
||||
|
||||
if (length == 1) {
|
||||
final Object key = keys.next();
|
||||
writer.write(quote(key.toString()));
|
||||
writer.write(':');
|
||||
if (indentFactor > 0) {
|
||||
writer.write(' ');
|
||||
}
|
||||
writeValue(writer, this.map.get(key), indentFactor, indent);
|
||||
} else if (length != 0) {
|
||||
final int newindent = indent + indentFactor;
|
||||
while (keys.hasNext()) {
|
||||
final Object key = keys.next();
|
||||
if (commanate) {
|
||||
writer.write(',');
|
||||
}
|
||||
if (indentFactor > 0) {
|
||||
writer.write('\n');
|
||||
}
|
||||
indent(writer, newindent);
|
||||
writer.write(quote(key.toString()));
|
||||
writer.write(':');
|
||||
if (indentFactor > 0) {
|
||||
writer.write(' ');
|
||||
}
|
||||
writeValue(writer, this.map.get(key), indentFactor, newindent);
|
||||
commanate = true;
|
||||
}
|
||||
if (indentFactor > 0) {
|
||||
writer.write('\n');
|
||||
}
|
||||
indent(writer, indent);
|
||||
}
|
||||
writer.write('}');
|
||||
return writer;
|
||||
} catch (final IOException exception) {
|
||||
throw new JSONException(exception);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,6 +0,0 @@
|
||||
package cn.citycraft.RealBackpacks.json;
|
||||
|
||||
public interface JSONString {
|
||||
|
||||
public String toJSONString();
|
||||
}
|
@ -1,315 +0,0 @@
|
||||
package cn.citycraft.RealBackpacks.json;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
|
||||
/*
|
||||
* Copyright (c) 2002 JSON.org
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* The Software shall be used for Good, not Evil.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
public class JSONTokener {
|
||||
|
||||
private long character;
|
||||
private boolean eof;
|
||||
private long index;
|
||||
private long line;
|
||||
private char previous;
|
||||
private final Reader reader;
|
||||
private boolean usePrevious;
|
||||
|
||||
public JSONTokener(final Reader reader) {
|
||||
this.reader = reader.markSupported() ? reader : new BufferedReader(reader);
|
||||
this.eof = false;
|
||||
this.usePrevious = false;
|
||||
this.previous = 0;
|
||||
this.index = 0;
|
||||
this.character = 1;
|
||||
this.line = 1;
|
||||
}
|
||||
|
||||
public JSONTokener(final InputStream inputStream) throws JSONException {
|
||||
this(new InputStreamReader(inputStream));
|
||||
}
|
||||
|
||||
public JSONTokener(final String s) {
|
||||
this(new StringReader(s));
|
||||
}
|
||||
|
||||
public void back() throws JSONException {
|
||||
if (this.usePrevious || this.index <= 0) {
|
||||
throw new JSONException("Stepping back two steps is not supported");
|
||||
}
|
||||
this.index -= 1;
|
||||
this.character -= 1;
|
||||
this.usePrevious = true;
|
||||
this.eof = false;
|
||||
}
|
||||
|
||||
public static int dehexchar(final char c) {
|
||||
if (c >= '0' && c <= '9') {
|
||||
return c - '0';
|
||||
}
|
||||
if (c >= 'A' && c <= 'F') {
|
||||
return c - ('A' - 10);
|
||||
}
|
||||
if (c >= 'a' && c <= 'f') {
|
||||
return c - ('a' - 10);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public boolean end() {
|
||||
return this.eof && !this.usePrevious;
|
||||
}
|
||||
|
||||
public boolean more() throws JSONException {
|
||||
this.next();
|
||||
if (this.end()) {
|
||||
return false;
|
||||
}
|
||||
this.back();
|
||||
return true;
|
||||
}
|
||||
|
||||
public char next() throws JSONException {
|
||||
int c;
|
||||
if (this.usePrevious) {
|
||||
this.usePrevious = false;
|
||||
c = this.previous;
|
||||
} else {
|
||||
try {
|
||||
c = this.reader.read();
|
||||
} catch (final IOException exception) {
|
||||
throw new JSONException(exception);
|
||||
}
|
||||
|
||||
if (c <= 0) { // End of stream
|
||||
this.eof = true;
|
||||
c = 0;
|
||||
}
|
||||
}
|
||||
this.index += 1;
|
||||
if (this.previous == '\r') {
|
||||
this.line += 1;
|
||||
this.character = c == '\n' ? 0 : 1;
|
||||
} else if (c == '\n') {
|
||||
this.line += 1;
|
||||
this.character = 0;
|
||||
} else {
|
||||
this.character += 1;
|
||||
}
|
||||
this.previous = (char) c;
|
||||
return this.previous;
|
||||
}
|
||||
|
||||
public char next(final char c) throws JSONException {
|
||||
final char n = this.next();
|
||||
if (n != c) {
|
||||
throw this.syntaxError("Expected '" + c + "' and instead saw '" + n + "'");
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
public String next(final int n) throws JSONException {
|
||||
if (n == 0) {
|
||||
return "";
|
||||
}
|
||||
|
||||
final char[] chars = new char[n];
|
||||
int pos = 0;
|
||||
|
||||
while (pos < n) {
|
||||
chars[pos] = this.next();
|
||||
if (this.end()) {
|
||||
throw this.syntaxError("Substring bounds error");
|
||||
}
|
||||
pos += 1;
|
||||
}
|
||||
return new String(chars);
|
||||
}
|
||||
|
||||
public char nextClean() throws JSONException {
|
||||
for (;;) {
|
||||
final char c = this.next();
|
||||
if (c == 0 || c > ' ') {
|
||||
return c;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String nextString(final char quote) throws JSONException {
|
||||
char c;
|
||||
final StringBuffer sb = new StringBuffer();
|
||||
for (;;) {
|
||||
c = this.next();
|
||||
switch (c) {
|
||||
case 0:
|
||||
case '\n':
|
||||
case '\r':
|
||||
throw this.syntaxError("Unterminated string");
|
||||
case '\\':
|
||||
c = this.next();
|
||||
switch (c) {
|
||||
case 'b':
|
||||
sb.append('\b');
|
||||
break;
|
||||
case 't':
|
||||
sb.append('\t');
|
||||
break;
|
||||
case 'n':
|
||||
sb.append('\n');
|
||||
break;
|
||||
case 'f':
|
||||
sb.append('\f');
|
||||
break;
|
||||
case 'r':
|
||||
sb.append('\r');
|
||||
break;
|
||||
case 'u':
|
||||
sb.append((char) Integer.parseInt(this.next(4), 16));
|
||||
break;
|
||||
case '"':
|
||||
case '\'':
|
||||
case '\\':
|
||||
case '/':
|
||||
sb.append(c);
|
||||
break;
|
||||
default:
|
||||
throw this.syntaxError("Illegal escape.");
|
||||
}
|
||||
break;
|
||||
default:
|
||||
if (c == quote) {
|
||||
return sb.toString();
|
||||
}
|
||||
sb.append(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String nextTo(final char delimiter) throws JSONException {
|
||||
final StringBuffer sb = new StringBuffer();
|
||||
for (;;) {
|
||||
final char c = this.next();
|
||||
if (c == delimiter || c == 0 || c == '\n' || c == '\r') {
|
||||
if (c != 0) {
|
||||
this.back();
|
||||
}
|
||||
return sb.toString().trim();
|
||||
}
|
||||
sb.append(c);
|
||||
}
|
||||
}
|
||||
|
||||
public String nextTo(final String delimiters) throws JSONException {
|
||||
char c;
|
||||
final StringBuffer sb = new StringBuffer();
|
||||
for (;;) {
|
||||
c = this.next();
|
||||
if (delimiters.indexOf(c) >= 0 || c == 0 || c == '\n' || c == '\r') {
|
||||
if (c != 0) {
|
||||
this.back();
|
||||
}
|
||||
return sb.toString().trim();
|
||||
}
|
||||
sb.append(c);
|
||||
}
|
||||
}
|
||||
|
||||
public Object nextValue() throws JSONException {
|
||||
char c = this.nextClean();
|
||||
String string;
|
||||
|
||||
switch (c) {
|
||||
case '"':
|
||||
case '\'':
|
||||
return this.nextString(c);
|
||||
case '{':
|
||||
this.back();
|
||||
return new JSONObject(this);
|
||||
case '[':
|
||||
this.back();
|
||||
return new JSONArray(this);
|
||||
}
|
||||
|
||||
/*
|
||||
* Handle unquoted text. This could be the values true, false, or
|
||||
* null, or it can be a number. An implementation (such as this one)
|
||||
* is allowed to also accept non-standard forms.
|
||||
*
|
||||
* Accumulate characters until we reach the end of the text or a
|
||||
* formatting character.
|
||||
*/
|
||||
|
||||
final StringBuffer sb = new StringBuffer();
|
||||
while (c >= ' ' && ",:]}/\\\"[{;=#".indexOf(c) < 0) {
|
||||
sb.append(c);
|
||||
c = this.next();
|
||||
}
|
||||
this.back();
|
||||
|
||||
string = sb.toString().trim();
|
||||
if ("".equals(string)) {
|
||||
throw this.syntaxError("Missing value");
|
||||
}
|
||||
return JSONObject.stringToValue(string);
|
||||
}
|
||||
|
||||
public char skipTo(final char to) throws JSONException {
|
||||
char c;
|
||||
try {
|
||||
final long startIndex = this.index;
|
||||
final long startCharacter = this.character;
|
||||
final long startLine = this.line;
|
||||
this.reader.mark(1000000);
|
||||
do {
|
||||
c = this.next();
|
||||
if (c == 0) {
|
||||
this.reader.reset();
|
||||
this.index = startIndex;
|
||||
this.character = startCharacter;
|
||||
this.line = startLine;
|
||||
return c;
|
||||
}
|
||||
} while (c != to);
|
||||
} catch (final IOException exc) {
|
||||
throw new JSONException(exc);
|
||||
}
|
||||
|
||||
this.back();
|
||||
return c;
|
||||
}
|
||||
|
||||
public JSONException syntaxError(final String message) {
|
||||
return new JSONException(message + this.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return " at " + this.index + " [character " + this.character + " line " + this.line + "]";
|
||||
}
|
||||
}
|
@ -1,163 +0,0 @@
|
||||
package cn.citycraft.RealBackpacks.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.configuration.serialization.ConfigurationSerializable;
|
||||
import org.bukkit.configuration.serialization.ConfigurationSerialization;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import cn.citycraft.RealBackpacks.json.JSONArray;
|
||||
import cn.citycraft.RealBackpacks.json.JSONException;
|
||||
import cn.citycraft.RealBackpacks.json.JSONObject;
|
||||
|
||||
/**
|
||||
* Fancy JSON serialization mostly by evilmidget38.
|
||||
*
|
||||
* @author evilmidget38, gomeow
|
||||
*
|
||||
*/
|
||||
public class Serialization {
|
||||
public static String NullString = "|--空--|";
|
||||
public static String SplitString = "<-分割->";
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static Map<String, Object> toMap(final JSONObject object)
|
||||
throws JSONException {
|
||||
final Map<String, Object> map = new HashMap<String, Object>();
|
||||
final Iterator<String> keys = object.keys();
|
||||
while (keys.hasNext()) {
|
||||
final String key = keys.next();
|
||||
map.put(key, fromJson(object.get(key)));
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
private static Object fromJson(final Object json) throws JSONException {
|
||||
if (json == JSONObject.NULL) {
|
||||
return null;
|
||||
} else if (json instanceof JSONObject) {
|
||||
return toMap((JSONObject) json);
|
||||
} else if (json instanceof JSONArray) {
|
||||
return toList((JSONArray) json);
|
||||
} else {
|
||||
return json;
|
||||
}
|
||||
}
|
||||
|
||||
public static List<Object> toList(final JSONArray array)
|
||||
throws JSONException {
|
||||
final List<Object> list = new ArrayList<Object>();
|
||||
for (int i = 0; i < array.length(); i++) {
|
||||
list.add(fromJson(array.get(i)));
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public static List<String> stringToList(final String listString) {
|
||||
return Arrays.asList(listString.split(SplitString));
|
||||
}
|
||||
|
||||
public static String listToString(final List<String> list) {
|
||||
String newString = null;
|
||||
for (final String s : list) {
|
||||
if (newString == null) {
|
||||
newString = s;
|
||||
} else {
|
||||
newString += SplitString + s;
|
||||
}
|
||||
}
|
||||
return newString;
|
||||
}
|
||||
|
||||
public static List<String> toString(final Inventory inv) {
|
||||
final List<String> result = new ArrayList<String>();
|
||||
final List<ConfigurationSerializable> items = new ArrayList<ConfigurationSerializable>();
|
||||
for (final ItemStack is : inv.getContents()) {
|
||||
items.add(is);
|
||||
}
|
||||
for (final ConfigurationSerializable cs : items) {
|
||||
if (cs == null) {
|
||||
result.add(NullString);
|
||||
} else {
|
||||
result.add(new JSONObject(serialize(cs)).toString());
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static Inventory toInventory(final List<String> stringItems,
|
||||
final String name, final int size) {
|
||||
final Inventory inv = Bukkit.createInventory(null, size,
|
||||
ChatColor.translateAlternateColorCodes('&', name));
|
||||
final List<ItemStack> contents = new ArrayList<ItemStack>();
|
||||
for (final String piece : stringItems) {
|
||||
if (piece.equalsIgnoreCase(NullString)) {
|
||||
contents.add(null);
|
||||
} else {
|
||||
try {
|
||||
final ItemStack item = (ItemStack) deserialize(toMap(new JSONObject(
|
||||
piece)));
|
||||
contents.add(item);
|
||||
} catch (final JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
final ItemStack[] items = new ItemStack[contents.size()];
|
||||
for (int x = 0; x < contents.size(); x++) {
|
||||
items[x] = contents.get(x);
|
||||
}
|
||||
inv.setContents(items);
|
||||
return inv;
|
||||
}
|
||||
|
||||
public static Map<String, Object> serialize(
|
||||
final ConfigurationSerializable cs) {
|
||||
final Map<String, Object> serialized = recreateMap(cs.serialize());
|
||||
for (final Entry<String, Object> entry : serialized.entrySet()) {
|
||||
if (entry.getValue() instanceof ConfigurationSerializable) {
|
||||
entry.setValue(serialize((ConfigurationSerializable) entry
|
||||
.getValue()));
|
||||
}
|
||||
}
|
||||
serialized.put(ConfigurationSerialization.SERIALIZED_TYPE_KEY,
|
||||
ConfigurationSerialization.getAlias(cs.getClass()));
|
||||
return serialized;
|
||||
}
|
||||
|
||||
public static Map<String, Object> recreateMap(
|
||||
final Map<String, Object> original) {
|
||||
final Map<String, Object> map = new HashMap<String, Object>();
|
||||
for (final Entry<String, Object> entry : original.entrySet()) {
|
||||
map.put(entry.getKey(), entry.getValue());
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
public static ConfigurationSerializable deserialize(
|
||||
final Map<String, Object> map) {
|
||||
for (final Entry<String, Object> entry : map.entrySet()) {
|
||||
if (entry.getValue() instanceof Map
|
||||
&& ((Map) entry.getValue())
|
||||
.containsKey(ConfigurationSerialization.SERIALIZED_TYPE_KEY)) {
|
||||
entry.setValue(deserialize((Map) entry.getValue()));
|
||||
}
|
||||
if (entry.getValue() instanceof Double) {
|
||||
Double db = (Double) entry.getValue();
|
||||
int value = db.intValue();
|
||||
entry.setValue(value);
|
||||
}
|
||||
}
|
||||
return ConfigurationSerialization.deserializeObject(map);
|
||||
}
|
||||
}
|
@ -1,62 +0,0 @@
|
||||
package cn.citycraft.RealBackpacks.util;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URL;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import com.google.common.base.Charsets;
|
||||
|
||||
public class VersionChecker {
|
||||
Plugin plugin;
|
||||
|
||||
public VersionChecker(Plugin plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
public void VersionCheck(final Player player) {
|
||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
String website = plugin.getDescription().getWebsite();
|
||||
String readURL = website
|
||||
+ (website.substring(website.length() - 1).equals("/") ? "" : "/")
|
||||
+ "/lastSuccessfulBuild/artifact/src/plugin.yml";
|
||||
FileConfiguration config;
|
||||
String currentVersion = plugin.getDescription().getVersion();
|
||||
try {
|
||||
URL url = new URL(readURL);
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream(),
|
||||
Charsets.UTF_8));
|
||||
config = YamlConfiguration.loadConfiguration(br);
|
||||
String newVersion = config.getString("version");
|
||||
br.close();
|
||||
if (!newVersion.equals(currentVersion)) {
|
||||
String[] msg = new String[] {
|
||||
ChatColor.GREEN + plugin.getName() + "插件最新版本 v" + newVersion,
|
||||
ChatColor.RED + "服务器运行版本: v" + currentVersion,
|
||||
ChatColor.GOLD + "插件更新网站: " + ChatColor.BLUE
|
||||
+ plugin.getDescription().getWebsite() };
|
||||
if (player != null) {
|
||||
player.sendMessage(msg);
|
||||
} else {
|
||||
for (String s : msg) {
|
||||
plugin.getLogger().info(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
plugin.getLogger().warning("版本更新检查失败!");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
@ -1,10 +1,5 @@
|
||||
package cn.citycraft.RealBackpacks;
|
||||
|
||||
import java.io.File;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
@ -16,24 +11,17 @@ import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import cn.citycraft.RealBackpacks.config.FileConfig;
|
||||
import cn.citycraft.RealBackpacks.config.PlayerConfig;
|
||||
import cn.citycraft.RealBackpacks.util.MysqlFunctions;
|
||||
import cn.citycraft.RealBackpacks.util.RBUtil;
|
||||
import cn.citycraft.RealBackpacks.util.Serialization;
|
||||
|
||||
public class MainCommand implements CommandExecutor {
|
||||
|
||||
private final RealBackpacks plugin;
|
||||
|
||||
private boolean exist = false;
|
||||
// private final boolean exist = false;
|
||||
|
||||
private final String[] helps = new String[] { "§6====== 真实背包插件 By:喵♂呜 ======",
|
||||
"§4* §a查看可购买列表 §7/rb list ",
|
||||
"§4* §a购买背包 §7/rb buy <背包名称> ",
|
||||
"§4* §a给玩家指定背包 §7/rb give <玩家名称> <背包名称>",
|
||||
"§4* §a查看玩家指定背包 §7/rb view <玩家名称> <背包名称>",
|
||||
"§4* §a数据转移至MySQL §7/rb filetomysql" };
|
||||
private final String[] helps = new String[] { "§6====== 真实背包插件 By:喵♂呜 ======", "§4* §a查看可购买列表 §7/rb list ", "§4* §a购买背包 §7/rb buy <背包名称> ",
|
||||
"§4* §a给玩家指定背包 §7/rb give <玩家名称> <背包名称>", "§4* §a查看玩家指定背包 §7/rb view <玩家名称> <背包名称>", "§4* §a数据转移至MySQL §7/rb filetomysql" };
|
||||
|
||||
public MainCommand(final RealBackpacks plugin) {
|
||||
this.plugin = plugin;
|
||||
@ -191,76 +179,75 @@ public class MainCommand implements CommandExecutor {
|
||||
sender.sendMessage(ChatColor.RED + other.getName() + "的背包已经满了");
|
||||
return false;
|
||||
}
|
||||
} else if (command.equalsIgnoreCase("filetomysql")) {
|
||||
if (plugin.isUsingPerms() && !sender.hasPermission("rb.filetomysql")) {
|
||||
return false;
|
||||
}
|
||||
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (!MysqlFunctions.checkIfTableExists("rb_data")) {
|
||||
MysqlFunctions.createTables();
|
||||
exist = false;
|
||||
} else {
|
||||
exist = true;
|
||||
}
|
||||
try {
|
||||
final Connection conn = DriverManager.getConnection(plugin.getUrl(), plugin.getUser(), plugin.getPass());
|
||||
final File dir = new File(plugin.getDataFolder() + File.separator + "userdata");
|
||||
int i = 0, times = 0;
|
||||
final int files = dir.listFiles().length;
|
||||
for (final File child : dir.listFiles()) {
|
||||
final String player = child.getName().replace(".yml", "");
|
||||
final FileConfig config = PlayerConfig.getInstance(plugin, player);
|
||||
|
||||
i++;
|
||||
PreparedStatement statement = null;
|
||||
PreparedStatement state = null;
|
||||
for (final String backpack : config.getConfigurationSection("").getKeys(false)) {
|
||||
if (exist) {
|
||||
statement = conn.prepareStatement("SELECT EXISTS(SELECT 1 FROM rb_data WHERE player = ? AND backpack = ? LIMIT 1);");
|
||||
statement.setString(1, player);
|
||||
statement.setString(2, backpack);
|
||||
final ResultSet res = statement.executeQuery();
|
||||
if (res.next()) {
|
||||
if (res.getInt(1) == 1) {
|
||||
state = conn.prepareStatement("UPDATE rb_data SET player=?, backpack=?, inventory=? WHERE player=? AND backpack=?;");
|
||||
state.setString(1, player);
|
||||
state.setString(2, backpack);
|
||||
state.setString(3, Serialization.listToString(config.getStringList(backpack + ".Inventory")));
|
||||
state.setString(4, player);
|
||||
state.setString(5, backpack);
|
||||
} else {
|
||||
state = conn.prepareStatement("INSERT INTO rb_data (player, backpack, inventory) VALUES(?, ?, ?);");
|
||||
state.setString(1, player);
|
||||
state.setString(2, backpack);
|
||||
state.setString(3, Serialization.listToString(config.getStringList(backpack + ".Inventory")));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
state = conn.prepareStatement("INSERT INTO rb_data (player, backpack, inventory) VALUES(?, ?, ?);");
|
||||
state.setString(1, player);
|
||||
state.setString(2, backpack);
|
||||
state.setString(3, Serialization.listToString(config.getStringList(backpack + ".Inventory")));
|
||||
}
|
||||
if (state != null) {
|
||||
state.executeUpdate();
|
||||
state.close();
|
||||
}
|
||||
}
|
||||
if (i == 50) {
|
||||
i = 0;
|
||||
times++;
|
||||
sender.sendMessage(ChatColor.LIGHT_PURPLE + "" + times * 50 + "/" + files + " files have been transferred.");
|
||||
}
|
||||
}
|
||||
conn.close();
|
||||
sender.sendMessage(ChatColor.LIGHT_PURPLE + "数据转换完成.");
|
||||
} catch (final SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
// } else if (command.equalsIgnoreCase("filetomysql")) {
|
||||
// if (plugin.isUsingPerms() && !sender.hasPermission("rb.filetomysql")) {
|
||||
// return false;
|
||||
// }
|
||||
// plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() {
|
||||
// @Override
|
||||
// public void run() {
|
||||
// if (!MysqlFunctions.checkIfTableExists("rb_data")) {
|
||||
// MysqlFunctions.createTables();
|
||||
// exist = false;
|
||||
// } else {
|
||||
// exist = true;
|
||||
// }
|
||||
// try {
|
||||
// final Connection conn = DriverManager.getConnection(plugin.getUrl(), plugin.getUser(), plugin.getPass());
|
||||
// final File dir = new File(plugin.getDataFolder() + File.separator + "userdata");
|
||||
// int i = 0, times = 0;
|
||||
// final int files = dir.listFiles().length;
|
||||
// for (final File child : dir.listFiles()) {
|
||||
// final String player = child.getName().replace(".yml", "");
|
||||
// final FileConfig config = PlayerConfig.getInstance(plugin, player);
|
||||
// i++;
|
||||
// PreparedStatement statement = null;
|
||||
// PreparedStatement state = null;
|
||||
// for (final String backpack : config.getConfigurationSection("").getKeys(false)) {
|
||||
// if (exist) {
|
||||
// statement = conn.prepareStatement("SELECT EXISTS(SELECT 1 FROM rb_data WHERE player = ? AND backpack = ? LIMIT 1);");
|
||||
// statement.setString(1, player);
|
||||
// statement.setString(2, backpack);
|
||||
// final ResultSet res = statement.executeQuery();
|
||||
// if (res.next()) {
|
||||
// if (res.getInt(1) == 1) {
|
||||
// state = conn.prepareStatement("UPDATE rb_data SET player=?, backpack=?, inventory=? WHERE player=? AND backpack=?;");
|
||||
// state.setString(1, player);
|
||||
// state.setString(2, backpack);
|
||||
// state.setString(3, Serialization.listToString(config.getStringList(backpack + ".Inventory")));
|
||||
// state.setString(4, player);
|
||||
// state.setString(5, backpack);
|
||||
// } else {
|
||||
// state = conn.prepareStatement("INSERT INTO rb_data (player, backpack, inventory) VALUES(?, ?, ?);");
|
||||
// state.setString(1, player);
|
||||
// state.setString(2, backpack);
|
||||
// state.setString(3, Serialization.listToString(config.getStringList(backpack + ".Inventory")));
|
||||
// }
|
||||
// }
|
||||
// } else {
|
||||
// state = conn.prepareStatement("INSERT INTO rb_data (player, backpack, inventory) VALUES(?, ?, ?);");
|
||||
// state.setString(1, player);
|
||||
// state.setString(2, backpack);
|
||||
// state.setString(3, Serialization.listToString(config.getStringList(backpack + ".Inventory")));
|
||||
// }
|
||||
// if (state != null) {
|
||||
// state.executeUpdate();
|
||||
// state.close();
|
||||
// }
|
||||
// }
|
||||
// if (i == 50) {
|
||||
// i = 0;
|
||||
// times++;
|
||||
// sender.sendMessage(ChatColor.LIGHT_PURPLE + "" + times * 50 + "/" + files + " files have been transferred.");
|
||||
// }
|
||||
// }
|
||||
// conn.close();
|
||||
// sender.sendMessage(ChatColor.LIGHT_PURPLE + "数据转换完成.");
|
||||
// } catch (final SQLException e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// }
|
||||
// });
|
||||
} else if (command.equalsIgnoreCase("view")) {
|
||||
if (!(args.length == 3)) {
|
||||
sender.sendMessage(ChatColor.RED + "命令错误. 正确命令:" + ChatColor.GRAY + " /rb view <player> <backpack>");
|
||||
@ -284,43 +271,43 @@ public class MainCommand implements CommandExecutor {
|
||||
return false;
|
||||
}
|
||||
Inventory inv = null;
|
||||
String name = args[1];
|
||||
final String name = args[1];
|
||||
final Player p = (Player) sender;
|
||||
final List<String> key = plugin.backpackData.get(backpack);
|
||||
if (!plugin.isUsingMysql()) {
|
||||
boolean fileExists = false;
|
||||
String fullName = null;
|
||||
final File dir = new File(plugin.getDataFolder() + File.separator + "userdata");
|
||||
for (final File f : dir.listFiles()) {
|
||||
final String fileName = f.getName();
|
||||
fullName = fileName.replace(".yml", "");
|
||||
if (fullName.equalsIgnoreCase(name)) {
|
||||
name = fullName;
|
||||
fileExists = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!fileExists) {
|
||||
sender.sendMessage(ChatColor.RED + "这货从来没打开过背包,所以是空的,2333.");
|
||||
return false;
|
||||
}
|
||||
final FileConfig config = PlayerConfig.getInstance(plugin, fullName);
|
||||
if (config.getStringList(backpack + ".Inventory") == null) {
|
||||
inv = plugin.getServer().createInventory(p, Integer.parseInt(key.get(0)), ChatColor.translateAlternateColorCodes('&', fullName + "'s " + backpack + " data"));
|
||||
} else {
|
||||
inv = Serialization.toInventory(config.getStringList(backpack + ".Inventory"), fullName + "'s " + backpack + " data", Integer.parseInt(key.get(0)));
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
inv = MysqlFunctions.getBackpackInv(name, backpack);
|
||||
} catch (final SQLException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
if (inv == null) {
|
||||
sender.sendMessage(ChatColor.RED + "这货从来没打开过背包,所以是空的,2333.");
|
||||
return false;
|
||||
}
|
||||
// final List<String> key = plugin.backpackData.get(backpack);
|
||||
// if (!plugin.isUsingMysql()) {
|
||||
// boolean fileExists = false;
|
||||
// String fullName = null;
|
||||
// final File dir = new File(plugin.getDataFolder() + File.separator + "userdata");
|
||||
// for (final File f : dir.listFiles()) {
|
||||
// final String fileName = f.getName();
|
||||
// fullName = fileName.replace(".yml", "");
|
||||
// if (fullName.equalsIgnoreCase(name)) {
|
||||
// name = fullName;
|
||||
// fileExists = true;
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// if (!fileExists) {
|
||||
// sender.sendMessage(ChatColor.RED + "这货从来没打开过背包,所以是空的,2333.");
|
||||
// return false;
|
||||
// }
|
||||
// final FileConfig config = PlayerConfig.getInstance(plugin, fullName);
|
||||
// if (config.getStringList(backpack + ".Inventory") == null) {
|
||||
// inv = plugin.getServer().createInventory(p, Integer.parseInt(key.get(0)), ChatColor.translateAlternateColorCodes('&', fullName + "'s " + backpack + " data"));
|
||||
// } else {
|
||||
// inv = Serialization.toInventory(config.getStringList(backpack + ".Inventory"), fullName + "'s " + backpack + " data", Integer.parseInt(key.get(0)));
|
||||
// }
|
||||
// } else {
|
||||
try {
|
||||
inv = MysqlFunctions.getBackpackInv(name, backpack);
|
||||
} catch (final SQLException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
if (inv == null) {
|
||||
sender.sendMessage(ChatColor.RED + "这货从来没打开过背包,所以是空的,2333.");
|
||||
return false;
|
||||
}
|
||||
// }
|
||||
if (plugin.playerData.containsKey(name)) {
|
||||
sender.sendMessage(ChatColor.RED + "玩家打开了背包,请等待玩家关闭.");
|
||||
return false;
|
@ -5,8 +5,6 @@ import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import net.milkbowl.vault.economy.Economy;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
@ -17,14 +15,14 @@ import org.bukkit.material.MaterialData;
|
||||
import org.bukkit.plugin.RegisteredServiceProvider;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import cn.citycraft.RealBackpacks.config.Config;
|
||||
import cn.citycraft.PluginHelper.config.FileConfig;
|
||||
import cn.citycraft.RealBackpacks.listeners.CraftListener;
|
||||
import cn.citycraft.RealBackpacks.listeners.EntityListener;
|
||||
import cn.citycraft.RealBackpacks.listeners.InventoryListener;
|
||||
import cn.citycraft.RealBackpacks.listeners.PlayerListener;
|
||||
import cn.citycraft.RealBackpacks.util.MysqlFunctions;
|
||||
import cn.citycraft.RealBackpacks.util.RBUtil;
|
||||
import cn.citycraft.RealBackpacks.util.VersionChecker;
|
||||
import net.milkbowl.vault.economy.Economy;
|
||||
|
||||
public class RealBackpacks extends JavaPlugin {
|
||||
|
||||
@ -35,7 +33,6 @@ public class RealBackpacks extends JavaPlugin {
|
||||
|
||||
private static boolean average = false;
|
||||
private static boolean add = false;
|
||||
public static VersionChecker versionChecker;
|
||||
private boolean usingMysql = false;
|
||||
private boolean vault = true;
|
||||
private boolean usingPermissions = true;
|
||||
@ -58,6 +55,8 @@ public class RealBackpacks extends JavaPlugin {
|
||||
|
||||
public List<String> slowedPlayers = new ArrayList<String>();
|
||||
|
||||
public FileConfig config;
|
||||
|
||||
// List key
|
||||
// 0 = Size
|
||||
// 1 = UseRecipe
|
||||
@ -81,26 +80,10 @@ public class RealBackpacks extends JavaPlugin {
|
||||
|
||||
@Override
|
||||
public FileConfiguration getConfig() {
|
||||
if (Config.getInstance() == null) {
|
||||
Config.load(this);
|
||||
if (config == null) {
|
||||
config = new FileConfig(this);
|
||||
}
|
||||
return Config.getInstance();
|
||||
}
|
||||
|
||||
private ItemStack getConfigLore(final ItemStack item, final String backpack) {
|
||||
final List<String> key = backpackData.get(backpack);
|
||||
final ItemMeta meta = item.getItemMeta();
|
||||
final ArrayList<String> lore = new ArrayList<String>();
|
||||
lore.clear();
|
||||
if (backpackLore.get(backpack) != null) {
|
||||
for (final String s : backpackLore.get(backpack)) {
|
||||
lore.add(ChatColor.translateAlternateColorCodes('&', s));
|
||||
}
|
||||
meta.setLore(lore);
|
||||
}
|
||||
meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', key.get(3)));
|
||||
item.setItemMeta(meta);
|
||||
return item;
|
||||
return config;
|
||||
}
|
||||
|
||||
public String getPass() {
|
||||
@ -139,7 +122,7 @@ public class RealBackpacks extends JavaPlugin {
|
||||
public void onEnable() {
|
||||
final String p = getServer().getClass().getPackage().getName();
|
||||
final String version = p.substring(p.lastIndexOf('.') + 1);
|
||||
String classname = getClass().getPackage().getName() + ".versions." + version;
|
||||
final String classname = getClass().getPackage().getName() + ".versions." + version;
|
||||
try {
|
||||
final Class<?> clazz = Class.forName(classname);
|
||||
final Constructor<?> cons = clazz.getDeclaredConstructor(getClass());
|
||||
@ -174,30 +157,23 @@ public class RealBackpacks extends JavaPlugin {
|
||||
getLogger().info("真实背包已加载 By: 喵♂呜.");
|
||||
}
|
||||
}
|
||||
versionChecker = new VersionChecker(this);
|
||||
versionChecker.VersionCheck(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reloadConfig() {
|
||||
Config.load(this);
|
||||
config.reload();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveConfig() {
|
||||
if (Config.getInstance() == null) {
|
||||
Config.load(this);
|
||||
}
|
||||
Config.save();
|
||||
config.save();
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public void setup() {
|
||||
user = getConfig().getString("Data.MySQL.username");
|
||||
password = getConfig().getString("Data.MySQL.password");
|
||||
url = "jdbc:mysql://" + getConfig().getString("Data.MySQL.ip") + ":"
|
||||
+ getConfig().getInt("Data.MySQL.port") + "/"
|
||||
+ getConfig().getString("Data.MySQL.database");
|
||||
url = "jdbc:mysql://" + getConfig().getString("Data.MySQL.ip") + ":" + getConfig().getInt("Data.MySQL.port") + "/" + getConfig().getString("Data.MySQL.database");
|
||||
|
||||
if (!getConfig().isSet("Config.MultipleBackpacksInInventory.average")) {
|
||||
average = false;
|
||||
@ -213,8 +189,7 @@ public class RealBackpacks extends JavaPlugin {
|
||||
|
||||
if (!getConfig().isSet("Data.FileSystem")) {
|
||||
usingMysql = false;
|
||||
} else if (getConfig().getString("Data.FileSystem").equalsIgnoreCase("mysql")
|
||||
|| getConfig().getString("Data.FileSystem").equalsIgnoreCase("sql")) {
|
||||
} else if (getConfig().getString("Data.FileSystem").equalsIgnoreCase("mysql") || getConfig().getString("Data.FileSystem").equalsIgnoreCase("sql")) {
|
||||
usingMysql = true;
|
||||
if (!MysqlFunctions.checkIfTableExists("rb_data")) {
|
||||
MysqlFunctions.createTables();
|
||||
@ -240,8 +215,7 @@ public class RealBackpacks extends JavaPlugin {
|
||||
|
||||
final List<String> key = backpackData.get(backpack);
|
||||
final String backpackItem = key.get(2);
|
||||
backpackItems.put(backpack,
|
||||
getConfigLore(RBUtil.getItemstackFromString(backpackItem), backpack));
|
||||
backpackItems.put(backpack, getConfigLore(RBUtil.getItemstackFromString(backpackItem), backpack));
|
||||
|
||||
ShapedRecipe recipe = null;
|
||||
if (key.get(1).equalsIgnoreCase("true")) {
|
||||
@ -287,14 +261,11 @@ public class RealBackpacks extends JavaPlugin {
|
||||
continue;
|
||||
}
|
||||
if (itemsplit.length > 1) {
|
||||
final Material baseblock = Material.getMaterial(Integer
|
||||
.parseInt(itemsplit[0]));
|
||||
final MaterialData ingredient = new MaterialData(baseblock,
|
||||
(byte) Integer.parseInt(itemsplit[1]));
|
||||
final Material baseblock = Material.getMaterial(Integer.parseInt(itemsplit[0]));
|
||||
final MaterialData ingredient = new MaterialData(baseblock, (byte) Integer.parseInt(itemsplit[1]));
|
||||
recipe.setIngredient(shapechar, ingredient);
|
||||
} else {
|
||||
final Material baseblock = Material.getMaterial(Integer
|
||||
.parseInt(itemsplit[0]));
|
||||
final Material baseblock = Material.getMaterial(Integer.parseInt(itemsplit[0]));
|
||||
recipe.setIngredient(shapechar, baseblock);
|
||||
}
|
||||
}
|
||||
@ -304,39 +275,25 @@ public class RealBackpacks extends JavaPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
private boolean setupEconomy() {
|
||||
if (getServer().getPluginManager().getPlugin("Vault") == null)
|
||||
return false;
|
||||
final RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager()
|
||||
.getRegistration(Economy.class);
|
||||
if (rsp == null)
|
||||
return false;
|
||||
econ = rsp.getProvider();
|
||||
return econ != null;
|
||||
}
|
||||
|
||||
public void setupLists() {
|
||||
backpacks.clear();
|
||||
backpackRecipe.clear();
|
||||
backpackData.clear();
|
||||
backpackLore.clear();
|
||||
for (final String backpack : getConfig().getConfigurationSection("Backpacks")
|
||||
.getKeys(false)) {
|
||||
for (final String backpack : getConfig().getConfigurationSection("Backpacks").getKeys(false)) {
|
||||
final List<String> list = new ArrayList<String>();
|
||||
backpacks.add(backpack);
|
||||
list.add(0, getConfig().getString("Backpacks." + backpack + ".Size"));
|
||||
list.add(1, getConfig().getString("Backpacks." + backpack + ".UseRecipe"));
|
||||
if (getConfig().getStringList("Backpacks." + backpack + ".Recipe") != null) {
|
||||
backpackRecipe.put(backpack,
|
||||
getConfig().getStringList("Backpacks." + backpack + ".Recipe"));
|
||||
backpackRecipe.put(backpack, getConfig().getStringList("Backpacks." + backpack + ".Recipe"));
|
||||
} else {
|
||||
backpackRecipe.put(backpack, null);
|
||||
}
|
||||
list.add(2, getConfig().getString("Backpacks." + backpack + ".BackpackItem.id"));
|
||||
list.add(3, getConfig().getString("Backpacks." + backpack + ".BackpackItem.name"));
|
||||
if (getConfig().getStringList("Backpacks." + backpack + ".BackpackItem.lore") != null) {
|
||||
backpackLore.put(backpack,
|
||||
getConfig().getStringList("Backpacks." + backpack + ".BackpackItem.lore"));
|
||||
backpackLore.put(backpack, getConfig().getStringList("Backpacks." + backpack + ".BackpackItem.lore"));
|
||||
} else {
|
||||
backpackLore.put(backpack, null);
|
||||
}
|
||||
@ -344,26 +301,11 @@ public class RealBackpacks extends JavaPlugin {
|
||||
list.add(5, getConfig().getString("Backpacks." + backpack + ".onDeath.dropContents"));
|
||||
list.add(6, getConfig().getString("Backpacks." + backpack + ".onDeath.dropBackpack"));
|
||||
list.add(7, getConfig().getString("Backpacks." + backpack + ".onDeath.keepBackpack"));
|
||||
list.add(8, getConfig()
|
||||
.getString("Backpacks." + backpack + ".WalkSpeedFeature.enabled"));
|
||||
list.add(
|
||||
9,
|
||||
getConfig().getString(
|
||||
"Backpacks." + backpack + ".WalkSpeedFeature.walkingSpeed"));
|
||||
list.add(
|
||||
10,
|
||||
getConfig().getString(
|
||||
"Backpacks." + backpack + ".IncreasedHungerFeature.enabled"));
|
||||
list.add(
|
||||
11,
|
||||
getConfig().getString(
|
||||
"Backpacks." + backpack
|
||||
+ ".IncreasedHungerFeature.extraHungerBarsToDeplete"));
|
||||
list.add(
|
||||
12,
|
||||
getConfig().getString(
|
||||
"Backpacks." + backpack
|
||||
+ ".IncreasedHungerFeature.hungerBarsToSubtractWhenEating"));
|
||||
list.add(8, getConfig().getString("Backpacks." + backpack + ".WalkSpeedFeature.enabled"));
|
||||
list.add(9, getConfig().getString("Backpacks." + backpack + ".WalkSpeedFeature.walkingSpeed"));
|
||||
list.add(10, getConfig().getString("Backpacks." + backpack + ".IncreasedHungerFeature.enabled"));
|
||||
list.add(11, getConfig().getString("Backpacks." + backpack + ".IncreasedHungerFeature.extraHungerBarsToDeplete"));
|
||||
list.add(12, getConfig().getString("Backpacks." + backpack + ".IncreasedHungerFeature.hungerBarsToSubtractWhenEating"));
|
||||
list.add(13, getConfig().getString("Backpacks." + backpack + ".Purchasable"));
|
||||
list.add(14, getConfig().getString("Backpacks." + backpack + ".Price"));
|
||||
list.add(15, getConfig().getString("Backpacks." + backpack + ".OpenWith"));
|
||||
@ -371,11 +313,37 @@ public class RealBackpacks extends JavaPlugin {
|
||||
list.add(17, getConfig().getString("Backpacks." + backpack + ".addGlow"));
|
||||
list.add(18, getConfig().getString("Backpacks." + backpack + ".Unstackable"));
|
||||
backpackData.put(backpack, list);
|
||||
backpackBlacklist.put(backpack,
|
||||
getConfig().getStringList("Backpacks." + backpack + ".ItemBlacklist"));
|
||||
backpackWhitelist.put(backpack,
|
||||
getConfig().getStringList("Backpacks." + backpack + ".ItemWhitelist"));
|
||||
backpackBlacklist.put(backpack, getConfig().getStringList("Backpacks." + backpack + ".ItemBlacklist"));
|
||||
backpackWhitelist.put(backpack, getConfig().getStringList("Backpacks." + backpack + ".ItemWhitelist"));
|
||||
}
|
||||
}
|
||||
|
||||
private ItemStack getConfigLore(final ItemStack item, final String backpack) {
|
||||
final List<String> key = backpackData.get(backpack);
|
||||
final ItemMeta meta = item.getItemMeta();
|
||||
final ArrayList<String> lore = new ArrayList<String>();
|
||||
lore.clear();
|
||||
if (backpackLore.get(backpack) != null) {
|
||||
for (final String s : backpackLore.get(backpack)) {
|
||||
lore.add(ChatColor.translateAlternateColorCodes('&', s));
|
||||
}
|
||||
meta.setLore(lore);
|
||||
}
|
||||
meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', key.get(3)));
|
||||
item.setItemMeta(meta);
|
||||
return item;
|
||||
}
|
||||
|
||||
private boolean setupEconomy() {
|
||||
if (getServer().getPluginManager().getPlugin("Vault") == null) {
|
||||
return false;
|
||||
}
|
||||
final RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
|
||||
if (rsp == null) {
|
||||
return false;
|
||||
}
|
||||
econ = rsp.getProvider();
|
||||
return econ != null;
|
||||
}
|
||||
|
||||
}
|
@ -15,7 +15,6 @@ import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
|
||||
import cn.citycraft.RealBackpacks.RealBackpacks;
|
||||
import cn.citycraft.RealBackpacks.config.PlayerConfig;
|
||||
import cn.citycraft.RealBackpacks.util.MysqlFunctions;
|
||||
import cn.citycraft.RealBackpacks.util.RBUtil;
|
||||
import cn.citycraft.RealBackpacks.util.Serialization;
|
||||
@ -162,7 +161,7 @@ public class InventoryListener implements Listener {
|
||||
public void onInventoryClose(final InventoryCloseEvent e) {
|
||||
final String name = e.getPlayer().getName();
|
||||
final Inventory inv = e.getView().getTopInventory();
|
||||
final List<String> invString = Serialization.toString(inv);
|
||||
final String invString = Serialization.toString(inv);
|
||||
final String backpack = plugin.playerData.get(name);
|
||||
plugin.playerData.remove(name);
|
||||
final String adminBackpack = plugin.adminFullView.get(name);
|
||||
@ -171,16 +170,16 @@ public class InventoryListener implements Listener {
|
||||
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (plugin.isUsingMysql()) {
|
||||
try {
|
||||
MysqlFunctions.addBackpackData(name, backpack, invString);
|
||||
} catch (final SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
PlayerConfig.getInstance(plugin, name).set(backpack + ".Inventory", invString);
|
||||
PlayerConfig.save();
|
||||
// if (plugin.isUsingMysql()) {
|
||||
try {
|
||||
MysqlFunctions.addBackpackData(name, backpack, invString);
|
||||
} catch (final SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
// } else {
|
||||
// PlayerConfig.getInstance(plugin, name).set(backpack + ".Inventory", invString);
|
||||
// PlayerConfig.save();
|
||||
// }
|
||||
}
|
||||
});
|
||||
} else if (adminBackpack != null) {
|
||||
@ -188,16 +187,16 @@ public class InventoryListener implements Listener {
|
||||
@Override
|
||||
public void run() {
|
||||
final String[] split = adminBackpack.split(":");
|
||||
if (plugin.isUsingMysql()) {
|
||||
try {
|
||||
MysqlFunctions.addBackpackData(split[0], split[1], invString);
|
||||
} catch (final SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
PlayerConfig.getInstance(plugin, split[1]).set(split[0] + ".Inventory", invString);
|
||||
PlayerConfig.save();
|
||||
// if (plugin.isUsingMysql()) {
|
||||
try {
|
||||
MysqlFunctions.addBackpackData(split[0], split[1], invString);
|
||||
} catch (final SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
// } else {
|
||||
// PlayerConfig.getInstance(plugin, split[1]).set(split[0] + ".Inventory", invString);
|
||||
// PlayerConfig.save();
|
||||
// }
|
||||
}
|
||||
});
|
||||
} else if (plugin.adminRestrictedView.contains(name)) {
|
@ -14,18 +14,14 @@ import org.bukkit.event.block.Action;
|
||||
import org.bukkit.event.entity.PlayerDeathEvent;
|
||||
import org.bukkit.event.player.PlayerDropItemEvent;
|
||||
import org.bukkit.event.player.PlayerInteractEvent;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.event.player.PlayerPickupItemEvent;
|
||||
import org.bukkit.event.player.PlayerRespawnEvent;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import cn.citycraft.RealBackpacks.RealBackpacks;
|
||||
import cn.citycraft.RealBackpacks.config.FileConfig;
|
||||
import cn.citycraft.RealBackpacks.config.PlayerConfig;
|
||||
import cn.citycraft.RealBackpacks.util.MysqlFunctions;
|
||||
import cn.citycraft.RealBackpacks.util.RBUtil;
|
||||
import cn.citycraft.RealBackpacks.util.Serialization;
|
||||
|
||||
public class PlayerListener implements Listener {
|
||||
|
||||
@ -50,20 +46,19 @@ public class PlayerListener implements Listener {
|
||||
if (key.get(5) != null && key.get(5).equalsIgnoreCase("true")) {
|
||||
// Drop contents
|
||||
Inventory binv = null;
|
||||
if (plugin.isUsingMysql()) {
|
||||
try {
|
||||
binv = MysqlFunctions.getBackpackInv(name, backpack);
|
||||
} catch (final SQLException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
FileConfig config = PlayerConfig.getInstance(plugin, name);
|
||||
if (config.getStringList(backpack + ".Inventory") == null) {
|
||||
continue;
|
||||
}
|
||||
binv = Serialization.toInventory(config.getStringList(backpack + ".Inventory"),
|
||||
key.get(3), Integer.parseInt(key.get(0)));
|
||||
// if (plugin.isUsingMysql()) {
|
||||
try {
|
||||
binv = MysqlFunctions.getBackpackInv(name, backpack);
|
||||
} catch (final SQLException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
// } else {
|
||||
// final FileConfig config = PlayerConfig.getInstance(plugin, name);
|
||||
// if (config.getStringList(backpack + ".Inventory") == null) {
|
||||
// continue;
|
||||
// }
|
||||
// binv = Serialization.toInventory(config.getStringList(backpack + ".Inventory"), key.get(3), Integer.parseInt(key.get(0)));
|
||||
// }
|
||||
if (plugin.playerData.containsKey(name)) {
|
||||
if (p.getItemOnCursor() != null) {
|
||||
p.setItemOnCursor(null);
|
||||
@ -111,8 +106,9 @@ public class PlayerListener implements Listener {
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGH)
|
||||
public void onInteract(final PlayerInteractEvent e) {
|
||||
if (e.getAction().equals(Action.PHYSICAL))
|
||||
if (e.getAction().equals(Action.PHYSICAL)) {
|
||||
return;
|
||||
}
|
||||
|
||||
final Action act = e.getAction();
|
||||
final Player p = e.getPlayer();
|
||||
@ -121,9 +117,7 @@ public class PlayerListener implements Listener {
|
||||
if (item.hasItemMeta()) {
|
||||
for (final String backpack : plugin.backpacks) {
|
||||
final List<String> key = plugin.backpackData.get(backpack);
|
||||
if (item.getItemMeta().hasDisplayName()
|
||||
&& item.getItemMeta().getDisplayName()
|
||||
.equals(ChatColor.translateAlternateColorCodes('&', key.get(3)))) {
|
||||
if (item.getItemMeta().hasDisplayName() && item.getItemMeta().getDisplayName().equals(ChatColor.translateAlternateColorCodes('&', key.get(3)))) {
|
||||
if (plugin.isUsingPerms() && !p.hasPermission("rb." + backpack + ".use")) {
|
||||
p.sendMessage(ChatColor.RED + "你没有打开此背包的权限...");
|
||||
continue;
|
||||
@ -158,29 +152,27 @@ public class PlayerListener implements Listener {
|
||||
p.updateInventory();
|
||||
}
|
||||
Inventory inv = null;
|
||||
if (plugin.isUsingMysql()) {
|
||||
try {
|
||||
inv = MysqlFunctions.getBackpackInv(name, backpack);
|
||||
} catch (final SQLException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
if (inv == null) {
|
||||
inv = plugin.getServer().createInventory(p,
|
||||
Integer.parseInt(key.get(0)),
|
||||
ChatColor.translateAlternateColorCodes('&', key.get(3)));
|
||||
}
|
||||
} else {
|
||||
FileConfig config = PlayerConfig.getInstance(plugin, name);
|
||||
if (!config.isSet(backpack + ".Inventory")) {
|
||||
inv = plugin.getServer().createInventory(p,
|
||||
Integer.parseInt(key.get(0)),
|
||||
ChatColor.translateAlternateColorCodes('&', key.get(3)));
|
||||
} else {
|
||||
inv = Serialization.toInventory(
|
||||
config.getStringList(backpack + ".Inventory"), key.get(3),
|
||||
Integer.parseInt(key.get(0)));
|
||||
}
|
||||
// if (plugin.isUsingMysql()) {
|
||||
try {
|
||||
inv = MysqlFunctions.getBackpackInv(name, backpack);
|
||||
} catch (final SQLException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
if (inv == null) {
|
||||
inv = plugin.getServer().createInventory(p, Integer.parseInt(key.get(0)), ChatColor.translateAlternateColorCodes('&', key.get(3)));
|
||||
}
|
||||
// } else {
|
||||
// FileConfig config = PlayerConfig.getInstance(plugin, name);
|
||||
// if (!config.isSet(backpack + ".Inventory")) {
|
||||
// inv = plugin.getServer().createInventory(p,
|
||||
// Integer.parseInt(key.get(0)),
|
||||
// ChatColor.translateAlternateColorCodes('&', key.get(3)));
|
||||
// } else {
|
||||
// inv = Serialization.toInventory(
|
||||
// config.getStringList(backpack + ".Inventory"), key.get(3),
|
||||
// Integer.parseInt(key.get(0)));
|
||||
// }
|
||||
// }
|
||||
if (p.getOpenInventory().getTopInventory() != null) {
|
||||
p.closeInventory();
|
||||
}
|
||||
@ -192,14 +184,6 @@ public class PlayerListener implements Listener {
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGH)
|
||||
public void onJoin(PlayerJoinEvent e) {
|
||||
Player player = e.getPlayer();
|
||||
if (player.hasPermission("rb.reload")) {
|
||||
RealBackpacks.versionChecker.VersionCheck(player);
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
|
||||
public void onPickup(final PlayerPickupItemEvent e) {
|
||||
final ItemStack item = e.getItem().getItemStack();
|
||||
@ -218,7 +202,7 @@ public class PlayerListener implements Listener {
|
||||
final Inventory inv = p.getInventory();
|
||||
final Location loc = e.getItem().getLocation();
|
||||
final ItemStack backpackItem = plugin.backpackItems.get(backpack);
|
||||
int emptySlots = 0,itemAmount = item.getAmount();
|
||||
int emptySlots = 0, itemAmount = item.getAmount();
|
||||
for (final ItemStack invItem : inv.getContents()) {
|
||||
if (invItem == null) {
|
||||
emptySlots++;
|
||||
@ -258,8 +242,7 @@ public class PlayerListener implements Listener {
|
||||
final String name = p.getName();
|
||||
for (final String backpack : plugin.backpacks) {
|
||||
final List<String> key = plugin.backpackData.get(backpack);
|
||||
if (key.get(7) != null && key.get(7).equalsIgnoreCase("true")
|
||||
&& deadPlayers.get(name) != null && deadPlayers.get(name).equals(backpack)) {
|
||||
if (key.get(7) != null && key.get(7).equalsIgnoreCase("true") && deadPlayers.get(name) != null && deadPlayers.get(name).equals(backpack)) {
|
||||
// Keep backpack
|
||||
p.getInventory().addItem(plugin.backpackItems.get(backpack));
|
||||
p.updateInventory();
|
@ -7,7 +7,6 @@ import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
@ -18,7 +17,7 @@ public class MysqlFunctions {
|
||||
|
||||
private static cn.citycraft.RealBackpacks.RealBackpacks plugin;
|
||||
|
||||
public static void addBackpackData(final String playerName, final String backpack, final List<String> invString) throws SQLException {
|
||||
public static void addBackpackData(final String playerName, final String backpack, final String invString) throws SQLException {
|
||||
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
@ -35,14 +34,14 @@ public class MysqlFunctions {
|
||||
state = conn.prepareStatement("UPDATE rb_data SET player=?, backpack=?, inventory=? WHERE player=? AND backpack=?;");
|
||||
state.setString(1, playerName);
|
||||
state.setString(2, backpack);
|
||||
state.setString(3, Serialization.listToString(invString));
|
||||
state.setString(3, invString);
|
||||
state.setString(4, playerName);
|
||||
state.setString(5, backpack);
|
||||
} else {
|
||||
state = conn.prepareStatement("INSERT INTO rb_data (player, backpack, inventory) VALUES(?, ?, ?);");
|
||||
state.setString(1, playerName);
|
||||
state.setString(2, backpack);
|
||||
state.setString(3, Serialization.listToString(invString));
|
||||
state.setString(3, invString);
|
||||
}
|
||||
}
|
||||
if (state != null) {
|
||||
@ -122,13 +121,13 @@ public class MysqlFunctions {
|
||||
if (res.next()) {
|
||||
final String invString = res.getString(1);
|
||||
if (invString != null) {
|
||||
returnInv = Serialization.toInventory(Serialization.stringToList(invString), ChatColor.translateAlternateColorCodes('&', plugin.backpackData.get(backpack).get(3)),
|
||||
returnInv = Serialization.toInventory(invString, ChatColor.translateAlternateColorCodes('&', plugin.backpackData.get(backpack).get(3)),
|
||||
Integer.parseInt(plugin.backpackData.get(backpack).get(0)));
|
||||
}
|
||||
}
|
||||
state.close();
|
||||
conn.close();
|
||||
} catch (final SQLException e) {
|
||||
} catch (final Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return returnInv;
|
@ -8,7 +8,6 @@ import org.bukkit.Material;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import cn.citycraft.RealBackpacks.RealBackpacks;
|
||||
import cn.citycraft.RealBackpacks.config.PlayerConfig;
|
||||
|
||||
public class RBUtil {
|
||||
|
||||
@ -18,12 +17,13 @@ public class RBUtil {
|
||||
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (plugin.isUsingMysql()) {
|
||||
MysqlFunctions.delete(name, backpack);
|
||||
} else {
|
||||
PlayerConfig.getInstance(plugin, name).set(backpack + ".Inventory", null);
|
||||
PlayerConfig.save();
|
||||
}
|
||||
// if (plugin.isUsingMysql()) {
|
||||
MysqlFunctions.delete(name, backpack);
|
||||
// } else {
|
||||
// FileConfig = new cn.citycraft.PluginHelper.config.PlayerConfig(plugin, name);
|
||||
// PlayerConfig.getInstance(plugin, name).set(backpack + ".Inventory", null);
|
||||
// PlayerConfig.save();
|
||||
// }
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -59,12 +59,10 @@ public class RBUtil {
|
||||
if (split.length == 1) {
|
||||
item = new ItemStack(Material.getMaterial(Integer.parseInt(split[0])), 1);
|
||||
} else {
|
||||
if (split[1].equalsIgnoreCase("enchant") || split[1].equalsIgnoreCase("lore")
|
||||
|| split[1].equalsIgnoreCase("all")) {
|
||||
if (split[1].equalsIgnoreCase("enchant") || split[1].equalsIgnoreCase("lore") || split[1].equalsIgnoreCase("all")) {
|
||||
item = new ItemStack(Material.getMaterial(Integer.parseInt(split[0])));
|
||||
} else {
|
||||
item = new ItemStack(Material.getMaterial(Integer.parseInt(split[0])), 1,
|
||||
(byte) Integer.parseInt(split[1]));
|
||||
item = new ItemStack(Material.getMaterial(Integer.parseInt(split[0])), 1, (byte) Integer.parseInt(split[1]));
|
||||
}
|
||||
}
|
||||
return item;
|
||||
@ -72,8 +70,9 @@ public class RBUtil {
|
||||
|
||||
public static boolean hasLore(final ItemStack item) {
|
||||
if (item.getItemMeta() != null) {
|
||||
if (item.getItemMeta().hasDisplayName() || item.getItemMeta().hasLore())
|
||||
if (item.getItemMeta().hasDisplayName() || item.getItemMeta().hasLore()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@ -83,8 +82,9 @@ public class RBUtil {
|
||||
int i = 0;
|
||||
if (split.length != 1) {
|
||||
for (i = 1; i < split.length; i++) {
|
||||
if (split[i].equalsIgnoreCase("enchant") || split[i].equalsIgnoreCase("all"))
|
||||
if (split[i].equalsIgnoreCase("enchant") || split[i].equalsIgnoreCase("all")) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
@ -95,8 +95,9 @@ public class RBUtil {
|
||||
int i = 0;
|
||||
if (split.length != 1) {
|
||||
for (i = 1; i < split.length; i++) {
|
||||
if (split[i].equalsIgnoreCase("lore") || split[i].equalsIgnoreCase("all"))
|
||||
if (split[i].equalsIgnoreCase("lore") || split[i].equalsIgnoreCase("all")) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
@ -107,15 +108,16 @@ public class RBUtil {
|
||||
final boolean enchant = item.getEnchantments().size() >= 1;
|
||||
final boolean isLored = isLored(s);
|
||||
final boolean isEnchanted = isEnchanted(s);
|
||||
if (!isLored && !isEnchanted && item.isSimilar(getItemstackFromString(s)))
|
||||
if (!isLored && !isEnchanted && item.isSimilar(getItemstackFromString(s))) {
|
||||
return true;
|
||||
else if (item.getType() == getItemstackFromString(s).getType()) {
|
||||
if (enchant && !lore)
|
||||
} else if (item.getType() == getItemstackFromString(s).getType()) {
|
||||
if (enchant && !lore) {
|
||||
return isEnchanted && !isLored;
|
||||
else if (enchant && lore)
|
||||
} else if (enchant && lore) {
|
||||
return isEnchanted && isLored;
|
||||
else if (!enchant && lore)
|
||||
} else if (!enchant && lore) {
|
||||
return isLored && !isEnchanted;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@ -124,10 +126,11 @@ public class RBUtil {
|
||||
RBUtil.plugin = plugin;
|
||||
}
|
||||
|
||||
public static String stringToBackpack(String s) {
|
||||
public static String stringToBackpack(final String s) {
|
||||
for (final String b : plugin.backpacks) {
|
||||
if (b.equalsIgnoreCase(s))
|
||||
if (b.equalsIgnoreCase(s)) {
|
||||
return b;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
@ -0,0 +1,65 @@
|
||||
package cn.citycraft.RealBackpacks.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.configuration.InvalidConfigurationException;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import cn.citycraft.PluginHelper.config.FileConfig;
|
||||
|
||||
// import cn.citycraft.RealBackpacks.json.JSONArray;
|
||||
// import cn.citycraft.RealBackpacks.json.JSONException;
|
||||
// import cn.citycraft.RealBackpacks.json.JSONObject;
|
||||
|
||||
/**
|
||||
* Fancy JSON serialization mostly by evilmidget38.
|
||||
*
|
||||
* @author evilmidget38, gomeow
|
||||
*
|
||||
*/
|
||||
public class Serialization {
|
||||
public static String INVKEY = "Inventory";
|
||||
public static String ITEMKEY = "Item";
|
||||
|
||||
public static String listToString(final List<String> list) {
|
||||
final FileConfig yml = new FileConfig();
|
||||
yml.set(INVKEY, list);
|
||||
return yml.saveToString();
|
||||
}
|
||||
|
||||
public static List<String> stringToList(final String invString) {
|
||||
final FileConfig yml = new FileConfig();
|
||||
return yml.getStringList(INVKEY);
|
||||
}
|
||||
|
||||
public static Inventory toInventory(final String invString, final String name, final int size) throws InvalidConfigurationException {
|
||||
final List<String> itemlist = stringToList(invString);
|
||||
final ItemStack[] items = new ItemStack[itemlist.size()];
|
||||
for (int i = 0; i < items.length; i++) {
|
||||
final FileConfig yml = new FileConfig();
|
||||
yml.loadFromString(itemlist.get(i));
|
||||
items[i] = yml.getItemStack(ITEMKEY + i);
|
||||
}
|
||||
final Inventory inv = Bukkit.createInventory(null, size, ChatColor.translateAlternateColorCodes('&', name));
|
||||
inv.setContents(items);
|
||||
return inv;
|
||||
}
|
||||
|
||||
public static List<String> toList(final Inventory inv) {
|
||||
final List<String> invlist = new ArrayList<String>();
|
||||
for (int i = 0; i < inv.getContents().length; i++) {
|
||||
final FileConfig yml = new FileConfig();
|
||||
yml.set(ITEMKEY + i, inv.getContents()[i]);
|
||||
invlist.add(yml.saveToString());
|
||||
}
|
||||
return invlist;
|
||||
}
|
||||
|
||||
public static String toString(final Inventory inv) {
|
||||
return listToString(toList(inv));
|
||||
}
|
||||
}
|
@ -1,8 +1,9 @@
|
||||
name: RealBackpacks
|
||||
main: cn.citycraft.RealBackpacks.RealBackpacks
|
||||
version: 0.1.5
|
||||
website: http://ci.citycraft.cn:8800/jenkins/job/RealBackpacks/
|
||||
author: Slayr288,喵♂呜
|
||||
name: ${project.artifactId}
|
||||
description: ${project.description}
|
||||
main: ${project.groupId}.${project.artifactId}.${project.artifactId}
|
||||
version: ${project.version}
|
||||
author: 喵♂呜
|
||||
website: http://ci.SumCraft.cn:8080/job/${project.artifactId}/
|
||||
softdepend: [Vault]
|
||||
commands:
|
||||
rb:
|
Loading…
Reference in New Issue
Block a user