1
0
mirror of https://e.coding.net/circlecloud/Soulbound.git synced 2025-11-25 21:46:20 +00:00

using PluginHelper and update...

Signed-off-by: j502647092 <jtb1@163.com>
This commit is contained in:
j502647092
2015-09-28 21:06:34 +08:00
parent 5ab2ac354c
commit 37963894f3
42 changed files with 150 additions and 513 deletions

View File

@@ -0,0 +1,119 @@
package com.me.tft_02.soulbound.config;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
public abstract class AutoUpdateConfigLoader extends ConfigLoader {
public AutoUpdateConfigLoader(String relativePath, String fileName) {
super(relativePath, fileName);
}
public AutoUpdateConfigLoader(String fileName) {
super(fileName);
}
@SuppressWarnings("deprecation")
@Override
protected void loadFile() {
super.loadFile();
FileConfiguration internalConfig = YamlConfiguration.loadConfiguration(plugin.getResource(fileName));
Set<String> configKeys = config.getKeys(true);
Set<String> internalConfigKeys = internalConfig.getKeys(true);
boolean needSave = false;
Set<String> oldKeys = new HashSet<String>(configKeys);
oldKeys.removeAll(internalConfigKeys);
Set<String> newKeys = new HashSet<String>(internalConfigKeys);
newKeys.removeAll(configKeys);
// Don't need a re-save if we have old keys sticking around?
// Would be less saving, but less... correct?
if (!newKeys.isEmpty() || !oldKeys.isEmpty()) {
needSave = true;
}
for (String key : oldKeys) {
plugin.debug("Removing unused key: " + key);
config.set(key, null);
}
for (String key : newKeys) {
plugin.debug("Adding new key: " + key + " = " + internalConfig.get(key));
config.set(key, internalConfig.get(key));
}
if (needSave) {
// Get Bukkit's version of an acceptable config with new keys, and no old keys
String output = config.saveToString();
// Convert to the superior 4 space indentation
output = output.replace(" ", " ");
// Rip out Bukkit's attempt to save comments at the top of the file
while (output.indexOf('#') != -1) {
output = output.substring(output.indexOf('\n', output.indexOf('#')) + 1);
}
// Read the internal config to get comments, then put them in the new one
try {
// Read internal
BufferedReader reader = new BufferedReader(new InputStreamReader(plugin.getResource(fileName)));
HashMap<String, String> comments = new HashMap<String, String>();
String temp = "";
String line;
while ((line = reader.readLine()) != null) {
if (line.contains("#")) {
temp += line + "\n";
}
else if (line.contains(":")) {
line = line.substring(0, line.indexOf(":") + 1);
if (!temp.isEmpty()) {
comments.put(line, temp);
temp = "";
}
}
}
// Dump to the new one
for (String key : comments.keySet()) {
if (output.indexOf(key) != -1) {
output = output.substring(0, output.indexOf(key)) + comments.get(key) + output.substring(output.indexOf(key));
}
}
}
catch (Exception e) {
e.printStackTrace();
}
// Save it
try {
String saveName = fileName;
// At this stage we cannot guarantee that Config has been loaded, so we do the check directly here
if (!plugin.getConfig().getBoolean("General.Config_Update_Overwrite", true)) {
saveName += ".new";
}
BufferedWriter writer = new BufferedWriter(new FileWriter(new File(plugin.getDataFolder(), saveName)));
writer.write(output);
writer.flush();
writer.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}

View File

@@ -0,0 +1,89 @@
package com.me.tft_02.soulbound.config;
import java.util.ArrayList;
import java.util.List;
public class Config extends AutoUpdateConfigLoader {
private static Config instance;
private Config() {
super("config.yml");
}
public static Config getInstance() {
if (instance == null) {
instance = new Config();
}
return instance;
}
@Override
protected void loadKeys() {}
/* @formatter:off */
/* GENERAL SETTINGS */
// public String getLocale() { return config.getString("General.Locale", "en_us"); }
// public int getSaveInterval() { return config.getInt("General.Save_Interval", 10); }
public boolean getStatsTrackingEnabled() { return config.getBoolean("General.Stats_Tracking", true); }
public boolean getUpdateCheckEnabled() { return config.getBoolean("General.Update_Check", true); }
public boolean getPreferBeta() { return config.getBoolean("General.Prefer_Beta", false); }
public boolean getVerboseLoggingEnabled() { return config.getBoolean("General.Verbose_Logging", false); }
public boolean getConfigOverwriteEnabled() { return config.getBoolean("General.Config_Update_Overwrite", true); }
/* @formatter:on */
/* SOULBOUND SETTINGS */
public boolean getShowNameInLore() { return config.getBoolean("Soulbound.Show_Name_In_Lore", true); }
public boolean getFeedbackEnabled() { return config.getBoolean("Soulbound.Feedback_Messages_Enabled", true); }
public boolean getPreventItemDrop() { return config.getBoolean("Soulbound.Prevent_Item_Drop", false); }
public boolean getDeleteOnDrop() { return config.getBoolean("Soulbound.Delete_On_Drop", false); }
public boolean getAllowItemStoring() { return config.getBoolean("Soulbound.Allow_Item_Storing", true); }
public boolean getInfiniteDurability() { return config.getBoolean("Soulbound.Infinite_Durability", false); }
public List<String> getBlockedCommands() { return config.getStringList("Soulbound.Blocked_Commands"); }
public List<String> getBindCommands() { return config.getStringList("Soulbound.Commands_Bind_When_Used"); }
// EpicBossRecoded config settings
public boolean getEBRBindOnPickup() { return config.getBoolean("Dependency_Plugins.EpicBossRecoded.BindOnPickup"); }
public boolean getEBRBindOnEquip() { return config.getBoolean("Dependency_Plugins.EpicBossRecoded.BindOnEquip"); }
public boolean getEBRBindOnUse() { return config.getBoolean("Dependency_Plugins.EpicBossRecoded.BindOnUse");}
// DiabloDrops config settings
public List<String> getDiabloDropsBindOnPickupTiers() { return getDiabloDropsItemTiers("BindOnPickup");}
public List<String> getDiabloDropsBindOnUseTiers() { return getDiabloDropsItemTiers("BindOnUse");}
public List<String> getDiabloDropsBindOnEquipTiers() { return getDiabloDropsItemTiers("BindOnEquip"); }
public List<String> getDiabloDropsItemTiers(String bindType) {
String[] tiersString = config.getString("Dependency_Plugins.DiabloDrops." + bindType).replaceAll(" ", "").split("[,]");
List<String> tiers = new ArrayList<String>();
for (String tier : tiersString) {
tiers.add(tier);
}
return tiers;
}
// LoreLocks config settings
public boolean getLoreLocksBindKeys() { return config.getBoolean("Dependency_Plugins.LoreLocks.Bind_Keys"); }
// MythicDrops config settings
public List<String> getMythicDropsBindOnPickupTiers() { return getMythicDropsItemTiers("BindOnPickup"); }
public List<String> getMythicDropsBindOnUseTiers() { return getMythicDropsItemTiers("BindOnUse"); }
public List<String> getMythicDropsBindOnEquipTiers() { return getMythicDropsItemTiers("BindOnEquip"); }
public List<String> getMythicDropsItemTiers(String bindType) {
String[] tiersString = config.getString("Dependency_Plugins.MythicDrops." + bindType).replaceAll(" ", "").split("[,]");
List<String> tiers = new ArrayList<String>();
for (String tier : tiersString) {
tiers.add(tier);
}
return tiers;
}
}

View File

@@ -0,0 +1,93 @@
package com.me.tft_02.soulbound.config;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import com.me.tft_02.soulbound.Soulbound;
public abstract class ConfigLoader {
protected static final Soulbound plugin = Soulbound.p;
protected String fileName;
protected File configFile;
protected FileConfiguration config;
public ConfigLoader(String relativePath, String fileName) {
this.fileName = fileName;
configFile = new File(plugin.getDataFolder(), relativePath + File.separator + fileName);
loadFile();
}
public ConfigLoader(String fileName) {
this.fileName = fileName;
configFile = new File(plugin.getDataFolder(), fileName);
loadFile();
}
protected void loadFile() {
if (!configFile.exists()) {
plugin.debug("Creating Soulbound " + fileName + " File...");
createFile();
}
else {
plugin.debug("Loading Soulbound " + fileName + " File...");
}
config = YamlConfiguration.loadConfiguration(configFile);
}
protected abstract void loadKeys();
protected void createFile() {
configFile.getParentFile().mkdirs();
InputStream inputStream = plugin.getResource(fileName);
if (inputStream == null) {
plugin.getLogger().severe("Missing resource file: '" + fileName + "' please notify the plugin authors");
return;
}
OutputStream outputStream = null;
try {
outputStream = new FileOutputStream(configFile);
int read;
byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
finally {
if (outputStream != null) {
try {
outputStream.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
try {
inputStream.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}

View File

@@ -0,0 +1,151 @@
package com.me.tft_02.soulbound.config;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.material.MaterialData;
import com.me.tft_02.soulbound.datatypes.ActionType;
import com.me.tft_02.soulbound.datatypes.SoulbindItem;
public class ItemsConfig extends ConfigLoader {
private static ItemsConfig instance;
private List<SoulbindItem> soulbindOnCraft = new ArrayList<SoulbindItem>();
private List<SoulbindItem> soulbindOnOpenChest = new ArrayList<SoulbindItem>();
private List<SoulbindItem> soulbindOnPickupItem = new ArrayList<SoulbindItem>();
private List<SoulbindItem> soulbindOnDrop = new ArrayList<SoulbindItem>();
private List<SoulbindItem> soulbindOnRespawn = new ArrayList<SoulbindItem>();
private List<SoulbindItem> soulbindOnKit = new ArrayList<SoulbindItem>();
public ItemsConfig() {
super("items.yml");
loadKeys();
}
public static ItemsConfig getInstance() {
if (instance == null) {
instance = new ItemsConfig();
}
return instance;
}
@SuppressWarnings("deprecation")
@Override
protected void loadKeys() {
ConfigurationSection configurationSection = config.getConfigurationSection("Items");
if (configurationSection == null) {
return;
}
Set<String> itemConfigSet = configurationSection.getKeys(false);
for (String itemName : itemConfigSet) {
String[] itemInfo = itemName.split("[|]");
Material itemMaterial = Material.matchMaterial(itemInfo[0]);
if (itemMaterial == null) {
plugin.getLogger().warning(
"Invalid material name. This item will be skipped. - " + itemInfo[0]);
continue;
}
byte itemData = (itemInfo.length == 2) ? Byte.valueOf(itemInfo[1]) : 0;
MaterialData itemMaterialData = new MaterialData(itemMaterial, itemData);
List<String> lore = new ArrayList<String>();
if (config.contains("Items." + itemName + ".Lore")) {
for (String loreEntry : config.getStringList("Items." + itemName + ".Lore")) {
lore.add(ChatColor.translateAlternateColorCodes('&', loreEntry));
}
}
String name = null;
if (config.contains("Items." + itemName + ".Name")) {
name = ChatColor.translateAlternateColorCodes('&',
config.getString("Items." + itemName + ".Name"));
}
SoulbindItem soulbindItem = new SoulbindItem(itemMaterialData, name, lore);
List<String> actions = config.getStringList("Items." + itemName + ".Actions");
for (ActionType actionType : ActionType.values()) {
if (actions.contains(actionType.toString())) {
addSoulbindItem(actionType, soulbindItem);
}
}
}
}
private void addSoulbindItem(ActionType actionType, SoulbindItem soulbindItem) {
switch (actionType) {
case CRAFT:
soulbindOnCraft.add(soulbindItem);
return;
case OPEN_CHEST:
soulbindOnOpenChest.add(soulbindItem);
return;
case PICKUP_ITEM:
soulbindOnPickupItem.add(soulbindItem);
return;
case DROP_ITEM:
soulbindOnDrop.add(soulbindItem);
return;
case RESPAWN:
soulbindOnRespawn.add(soulbindItem);
return;
case KIT:
soulbindOnKit.add(soulbindItem);
return;
}
}
public List<SoulbindItem> getSoulbindItems(ActionType actionType) {
switch (actionType) {
case CRAFT:
return soulbindOnCraft;
case OPEN_CHEST:
return soulbindOnOpenChest;
case PICKUP_ITEM:
return soulbindOnPickupItem;
case DROP_ITEM:
return soulbindOnDrop;
case RESPAWN:
return soulbindOnRespawn;
case KIT:
return soulbindOnKit;
default:
return null;
}
}
public boolean isActionItem(ItemStack itemStack, ActionType actionType) {
for (SoulbindItem soulbindItem : getSoulbindItems(actionType)) {
if (itemStack.getData().equals(soulbindItem.getMaterialData())) {
if (itemStack.hasItemMeta()) {
ItemMeta itemMeta = itemStack.getItemMeta();
if (soulbindItem.getName() != null)
if (itemMeta.getDisplayName().contains(soulbindItem.getName()))
if (soulbindItem.getLore() != null && !soulbindItem.getLore().isEmpty())
if (itemMeta.hasLore()
&& itemMeta.getLore().containsAll(soulbindItem.getLore()))
return true;
}
}
}
return false;
}
}