mirror of
https://github.com/DieReicheErethons/Brewery.git
synced 2024-11-29 12:45:14 +01:00
GetData Fixes, Added DataUpdater
This commit is contained in:
parent
ba87aaabcf
commit
f69d55ebb5
@ -9,6 +9,8 @@ import org.bukkit.block.BlockFace;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.Effect;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.material.Cauldron;
|
||||
import org.bukkit.material.MaterialData;
|
||||
|
||||
public class BCauldron {
|
||||
public static CopyOnWriteArrayList<BCauldron> bcauldrons = new CopyOnWriteArrayList<BCauldron>();
|
||||
@ -71,7 +73,7 @@ public class BCauldron {
|
||||
// get cauldron from block and add given ingredient
|
||||
public static boolean ingredientAdd(Block block, Material ingredient) {
|
||||
// if not empty
|
||||
if (block.getData() != 0) {
|
||||
if (getFillLevel(block) != 0) {
|
||||
BCauldron bcauldron = get(block);
|
||||
if (bcauldron != null) {
|
||||
bcauldron.add(ingredient);
|
||||
@ -116,6 +118,24 @@ public class BCauldron {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 0 = empty, 1 = something in, 2 = full
|
||||
public static byte getFillLevel(Block block) {
|
||||
if (block.getType() == Material.CAULDRON) {
|
||||
MaterialData data = block.getState().getData();
|
||||
if (data instanceof Cauldron) {
|
||||
Cauldron cauldron = (Cauldron) data;
|
||||
if (cauldron.isEmpty()) {
|
||||
return 0;
|
||||
} else if (cauldron.isFull()) {
|
||||
return 2;
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// prints the current cooking time to the player
|
||||
public static void printTime(Player player, Block block) {
|
||||
if (!player.hasPermission("brewery.cauldron.time")) {
|
||||
@ -134,7 +154,7 @@ public class BCauldron {
|
||||
|
||||
// reset to normal cauldron
|
||||
public static void remove(Block block) {
|
||||
if (block.getData() != 0) {
|
||||
if (getFillLevel(block) != 0) {
|
||||
BCauldron bcauldron = get(block);
|
||||
if (bcauldron != null) {
|
||||
bcauldrons.remove(bcauldron);
|
||||
@ -171,7 +191,7 @@ public class BCauldron {
|
||||
if (cauldron.state != 1) {
|
||||
config.set(prefix + ".state", cauldron.state);
|
||||
}
|
||||
config.set(prefix + ".ingredients", cauldron.ingredients);
|
||||
config.set(prefix + ".ingredients", cauldron.ingredients.serializeIngredients());
|
||||
id++;
|
||||
}
|
||||
}
|
||||
|
@ -110,6 +110,10 @@ public class BIngredients {
|
||||
return count;
|
||||
}
|
||||
|
||||
/*public Map<Material, Integer> getIngredients() {
|
||||
return ingredients;
|
||||
}*/
|
||||
|
||||
public int getCookedTime() {
|
||||
return cookedTime;
|
||||
}
|
||||
@ -297,17 +301,17 @@ public class BIngredients {
|
||||
if (cookedTime != 0) {
|
||||
config.set(path + ".cookedTime", cookedTime);
|
||||
}
|
||||
config.set(path + ".mats", ingredients);
|
||||
config.set(path + ".mats", serializeIngredients());
|
||||
return id;
|
||||
}
|
||||
|
||||
// convert the ingredient Material to id
|
||||
/*public Map<Integer, Integer> serializeIngredients() {
|
||||
Map<Integer, Integer> mats = new HashMap<Integer, Integer>();
|
||||
for (Material mat : ingredients.keySet()) {
|
||||
mats.put(mat.getId(), ingredients.get(mat));
|
||||
//convert the ingredient Material to String
|
||||
public Map<String, Integer> serializeIngredients() {
|
||||
Map<String, Integer> mats = new HashMap<String, Integer>();
|
||||
for (Map.Entry<Material, Integer> entry : ingredients.entrySet()) {
|
||||
mats.put(entry.getKey().name(), entry.getValue());
|
||||
}
|
||||
return mats;
|
||||
}*/
|
||||
}
|
||||
|
||||
}
|
@ -22,7 +22,7 @@ public class BPlayer {
|
||||
|
||||
// Settings
|
||||
public static Map<Material, Integer> drainItems = new HashMap<Material, Integer>();// DrainItem Material and Strength
|
||||
public static int pukeItemId;
|
||||
public static Material pukeItem;
|
||||
public static int hangoverTime;
|
||||
public static boolean overdrinkKick;
|
||||
public static boolean enableHome;
|
||||
@ -367,7 +367,7 @@ public class BPlayer {
|
||||
loc.setY(loc.getY() + 1.5);
|
||||
loc.setPitch(loc.getPitch() + 10);
|
||||
loc.add(direction);
|
||||
Item item = player.getWorld().dropItem(loc, new ItemStack(pukeItemId));
|
||||
Item item = player.getWorld().dropItem(loc, new ItemStack(pukeItem));
|
||||
item.setVelocity(direction);
|
||||
item.setPickupDelay(Integer.MAX_VALUE);
|
||||
}
|
||||
|
@ -5,10 +5,10 @@ import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.Map;
|
||||
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.TreeSpecies;
|
||||
import org.bukkit.entity.HumanEntity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.BlockFace;
|
||||
import org.bukkit.event.player.PlayerInteractEvent;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
@ -16,6 +16,7 @@ import org.bukkit.inventory.meta.PotionMeta;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.material.MaterialData;
|
||||
import org.bukkit.material.Stairs;
|
||||
import org.bukkit.material.Tree;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import com.dre.brewery.integration.GriefPreventionBarrel;
|
||||
@ -333,7 +334,7 @@ public class Barrel {
|
||||
switch (block.getType()) {
|
||||
case FENCE:
|
||||
case NETHER_FENCE:
|
||||
case SIGN:
|
||||
case SIGN_POST:
|
||||
case WALL_SIGN:
|
||||
Barrel barrel = getBySpigot(block);
|
||||
if (barrel != null) {
|
||||
@ -342,11 +343,11 @@ public class Barrel {
|
||||
return null;
|
||||
case WOOD:
|
||||
case WOOD_STAIRS:
|
||||
case ACACIA_STAIRS:
|
||||
case BIRCH_WOOD_STAIRS:
|
||||
case DARK_OAK_STAIRS:
|
||||
case JUNGLE_WOOD_STAIRS:
|
||||
case SPRUCE_WOOD_STAIRS:
|
||||
case ACACIA_STAIRS:
|
||||
case DARK_OAK_STAIRS:
|
||||
Barrel barrel2 = getByWood(block);
|
||||
if (barrel2 != null) {
|
||||
return barrel2;
|
||||
@ -605,7 +606,7 @@ public class Barrel {
|
||||
|
||||
// true for small barrels
|
||||
public static boolean isSign(Block spigot) {
|
||||
return spigot.getType() == Material.SIGN || spigot.getType() == Material.SIGN_POST;
|
||||
return spigot.getType() == Material.WALL_SIGN || spigot.getType() == Material.SIGN_POST;
|
||||
}
|
||||
|
||||
// woodtype of the block the spigot is attached to
|
||||
@ -624,15 +625,22 @@ public class Barrel {
|
||||
wood = this.spigot.getRelative(0, 0, -1);
|
||||
}
|
||||
if (wood.getType() == Material.WOOD) {
|
||||
byte data = wood.getData();
|
||||
if (data == 0x0) {
|
||||
return 2;
|
||||
} else if (data == 0x1) {
|
||||
return 4;
|
||||
} else if (data == 0x2) {
|
||||
return 1;
|
||||
} else {
|
||||
return 3;
|
||||
MaterialData data = wood.getState().getData();
|
||||
if (data instanceof Tree) {
|
||||
TreeSpecies woodType = ((Tree) data).getSpecies();
|
||||
if (woodType == TreeSpecies.GENERIC){
|
||||
return 2;
|
||||
} else if (woodType == TreeSpecies.REDWOOD) {
|
||||
return 4;
|
||||
} else if (woodType == TreeSpecies.BIRCH) {
|
||||
return 1;
|
||||
} else if (woodType == TreeSpecies.JUNGLE) {
|
||||
return 3;
|
||||
} else if (woodType == TreeSpecies.ACACIA) {
|
||||
return 5;
|
||||
} else if (woodType == TreeSpecies.DARK_OAK) {
|
||||
return 6;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (wood.getType() == Material.WOOD_STAIRS) {
|
||||
@ -647,6 +655,16 @@ public class Barrel {
|
||||
if (wood.getType() == Material.JUNGLE_WOOD_STAIRS) {
|
||||
return 3;
|
||||
}
|
||||
try {
|
||||
if (wood.getType() == Material.ACACIA_STAIRS) {
|
||||
return 5;
|
||||
}
|
||||
if (wood.getType() == Material.DARK_OAK_STAIRS) {
|
||||
return 6;
|
||||
}
|
||||
} catch (NoSuchFieldError e) {
|
||||
return 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -682,7 +700,17 @@ public class Barrel {
|
||||
}
|
||||
|
||||
public static boolean isStairs(Material material) {
|
||||
return material == Material.WOOD_STAIRS || material == Material.SPRUCE_WOOD_STAIRS || material == Material.BIRCH_WOOD_STAIRS || material == Material.JUNGLE_WOOD_STAIRS || material == Material.ACACIA_STAIRS || material == Material.DARK_OAK_STAIRS;
|
||||
switch (material) {
|
||||
case WOOD_STAIRS:
|
||||
case SPRUCE_WOOD_STAIRS:
|
||||
case BIRCH_WOOD_STAIRS:
|
||||
case JUNGLE_WOOD_STAIRS:
|
||||
case ACACIA_STAIRS:
|
||||
case DARK_OAK_STAIRS:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// returns null if Barrel is correctly placed; the block that is missing when not
|
||||
@ -748,9 +776,8 @@ public class Barrel {
|
||||
if (y == 0) {
|
||||
// stairs have to be upside down
|
||||
MaterialData data = block.getState().getData();
|
||||
if(data instanceof Stairs){
|
||||
Stairs sdata = (Stairs) data;
|
||||
if (sdata.getFacing() == BlockFace.DOWN) {
|
||||
if (data instanceof Stairs) {
|
||||
if (!((Stairs) data).isInverted()) {
|
||||
return block;
|
||||
}
|
||||
}
|
||||
|
104
src/com/dre/brewery/DataUpdater.java
Normal file
104
src/com/dre/brewery/DataUpdater.java
Normal file
@ -0,0 +1,104 @@
|
||||
package com.dre.brewery;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
|
||||
public class DataUpdater {
|
||||
|
||||
private FileConfiguration data;
|
||||
private File file;
|
||||
|
||||
public DataUpdater(FileConfiguration data, File file) {
|
||||
this.data = data;
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void update(String fromVersion) {
|
||||
if (fromVersion.equalsIgnoreCase("1.0")) {
|
||||
update10();
|
||||
//fromVersion = "1.1";
|
||||
}
|
||||
|
||||
try {
|
||||
data.save(file);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public void update10() {
|
||||
|
||||
data.set("Version", P.dataVersion);
|
||||
|
||||
ConfigurationSection section = data.getConfigurationSection("Ingredients");
|
||||
try {
|
||||
if (section != null) {
|
||||
for (String id : section.getKeys(false)) {
|
||||
ConfigurationSection matSection = section.getConfigurationSection(id + ".mats");
|
||||
if (matSection != null) {
|
||||
// matSection has all the materials + amount as Integers
|
||||
Map<String, Integer> ingredients = new HashMap<String, Integer>();
|
||||
for (String ingredient : matSection.getKeys(false)) {
|
||||
// convert to Material
|
||||
Material mat = Material.getMaterial(P.p.parseInt(ingredient));
|
||||
if (mat != null) {
|
||||
ingredients.put(mat.name(), matSection.getInt(ingredient));
|
||||
}
|
||||
}
|
||||
section.set(id + ".mats", ingredients);
|
||||
} else {
|
||||
P.p.errorLog("Ingredient id: '" + id + "' incomplete in data.yml");
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// Getting Material by id may not work in the future
|
||||
P.p.errorLog("Error Converting Ingredient Section of the Data File, newer versions of Bukkit may not support the old Save File anymore:");
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
section = data.getConfigurationSection("BCauldron");
|
||||
if (section != null) {
|
||||
try {
|
||||
for (String uuid : section.getKeys(false)) {
|
||||
ConfigurationSection cauldrons = section.getConfigurationSection(uuid);
|
||||
if (cauldrons != null) {
|
||||
for (String id : cauldrons.getKeys(false)) {
|
||||
ConfigurationSection ingredientSection = cauldrons.getConfigurationSection(id + ".ingredients");
|
||||
if (ingredientSection != null) {
|
||||
// has all the materials + amount as Integers
|
||||
Map<String, Integer> ingredients = new HashMap<String, Integer>();
|
||||
for (String ingredient : ingredientSection.getKeys(false)) {
|
||||
// convert to Material
|
||||
Material mat = Material.getMaterial(P.p.parseInt(ingredient));
|
||||
if (mat != null) {
|
||||
ingredients.put(mat.name(), ingredientSection.getInt(ingredient));
|
||||
}
|
||||
}
|
||||
cauldrons.set(id + ".ingredients", ingredients);
|
||||
} else {
|
||||
P.p.errorLog("BCauldron " + id + " is missing Ingredient Section");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// Getting Material by id may not work in the future
|
||||
P.p.errorLog("Error Converting Ingredient Section of Cauldrons, newer versions of Bukkit may not support the old Save File anymore:");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -35,6 +35,7 @@ public class P extends JavaPlugin {
|
||||
public static int lastBackup = 0;
|
||||
public static int lastSave = 1;
|
||||
public static int autosave = 3;
|
||||
final public static String dataVersion = "1.1";
|
||||
|
||||
// Third Party Enabled
|
||||
public boolean useWG; //WorldGuard
|
||||
@ -206,7 +207,7 @@ public class P extends JavaPlugin {
|
||||
// various Settings
|
||||
autosave = config.getInt("autosave", 3);
|
||||
debug = config.getBoolean("debug", false);
|
||||
BPlayer.pukeItemId = Material.matchMaterial(config.getString("pukeItem", "SOUL_SAND")).getId();
|
||||
BPlayer.pukeItem = Material.matchMaterial(config.getString("pukeItem", "SOUL_SAND"));
|
||||
BPlayer.hangoverTime = config.getInt("hangoverDays", 0) * 24 * 60;
|
||||
BPlayer.overdrinkKick = config.getBoolean("enableKickOnOverdrink", false);
|
||||
BPlayer.enableHome = config.getBoolean("enableHome", false);
|
||||
@ -276,6 +277,17 @@ public class P extends JavaPlugin {
|
||||
|
||||
FileConfiguration data = YamlConfiguration.loadConfiguration(file);
|
||||
|
||||
// Check if data is the newest version
|
||||
String version = data.getString("Version", null);
|
||||
if (version != null) {
|
||||
if (!version.equals(dataVersion)) {
|
||||
P.p.log("Data File is being updated...");
|
||||
new DataUpdater(data, file).update(version);
|
||||
data = YamlConfiguration.loadConfiguration(file);
|
||||
P.p.log("Data Updated to version: " + dataVersion);
|
||||
}
|
||||
}
|
||||
|
||||
// loading Ingredients into ingMap
|
||||
Map<String, BIngredients> ingMap = new HashMap<String, BIngredients>();
|
||||
ConfigurationSection section = data.getConfigurationSection("Ingredients");
|
||||
@ -285,9 +297,8 @@ public class P extends JavaPlugin {
|
||||
if (matSection != null) {
|
||||
// matSection has all the materials + amount as Integers
|
||||
Map<Material, Integer> ingredients = new HashMap<Material, Integer>();
|
||||
for (String ingredient : matSection.getKeys(false)) {
|
||||
// convert to Material
|
||||
ingredients.put(Material.getMaterial(parseInt(ingredient)), matSection.getInt(ingredient));
|
||||
for (String mat : matSection.getKeys(false)) {
|
||||
ingredients.put(Material.getMaterial(mat), matSection.getInt(mat));
|
||||
}
|
||||
ingMap.put(id, new BIngredients(ingredients, section.getInt(id + ".cookedTime", 0)));
|
||||
} else {
|
||||
@ -354,11 +365,9 @@ public class P extends JavaPlugin {
|
||||
// loads BIngredients from an ingredient section
|
||||
public BIngredients loadIngredients(ConfigurationSection section) {
|
||||
if (section != null) {
|
||||
// has all the materials + amount as Integers
|
||||
Map<Material, Integer> ingredients = new HashMap<Material, Integer>();
|
||||
for (String ingredient : section.getKeys(false)) {
|
||||
// convert to Material
|
||||
ingredients.put(Material.getMaterial(parseInt(ingredient)), section.getInt(ingredient));
|
||||
for (String mat : section.getKeys(false)) {
|
||||
ingredients.put(Material.getMaterial(mat), section.getInt(mat));
|
||||
}
|
||||
return new BIngredients(ingredients, 0);
|
||||
} else {
|
||||
@ -502,7 +511,7 @@ public class P extends JavaPlugin {
|
||||
|
||||
saveWorldNames(configFile, oldData.getConfigurationSection("Worlds"));
|
||||
|
||||
configFile.set("Version", "1.0");
|
||||
configFile.set("Version", dataVersion);
|
||||
|
||||
try {
|
||||
configFile.save(datafile);
|
||||
@ -607,7 +616,7 @@ public class P extends JavaPlugin {
|
||||
}
|
||||
}
|
||||
return true;
|
||||
case SIGN:
|
||||
case SIGN_POST:
|
||||
case WALL_SIGN:
|
||||
// remove small Barrels
|
||||
Barrel barrel2 = Barrel.getBySpigot(block);
|
||||
@ -626,11 +635,11 @@ public class P extends JavaPlugin {
|
||||
return true;
|
||||
case WOOD:
|
||||
case WOOD_STAIRS:
|
||||
case ACACIA_STAIRS:
|
||||
case BIRCH_WOOD_STAIRS:
|
||||
case DARK_OAK_STAIRS:
|
||||
case JUNGLE_WOOD_STAIRS:
|
||||
case SPRUCE_WOOD_STAIRS:
|
||||
case ACACIA_STAIRS:
|
||||
case DARK_OAK_STAIRS:
|
||||
Barrel barrel3 = Barrel.getByWood(block);
|
||||
if (barrel3 != null) {
|
||||
if (barrel3.hasPermsDestroy(player)) {
|
||||
|
@ -32,6 +32,9 @@ public class LogBlockBarrel {
|
||||
}
|
||||
|
||||
private void compareInv(final ItemStack[] after) {
|
||||
if (consumer == null) {
|
||||
return;
|
||||
}
|
||||
final ItemStack[] diff = compareInventories(items, after);
|
||||
for (final ItemStack item : diff) {
|
||||
consumer.queueChestAccess(player.getName(), loc, loc.getWorld().getBlockTypeIdAt(loc), (short) item.getTypeId(), (short) item.getAmount(), rawData(item));
|
||||
@ -62,6 +65,9 @@ public class LogBlockBarrel {
|
||||
}
|
||||
|
||||
public static void breakBarrel(String playerName, ItemStack[] contents, Location spigotLoc) {
|
||||
if (consumer == null) {
|
||||
return;
|
||||
}
|
||||
if (!isLogging(spigotLoc.getWorld(), Logging.CHESTACCESS)) return;
|
||||
final ItemStack[] items = compressInventory(contents);
|
||||
for (final ItemStack item : items) {
|
||||
|
@ -63,8 +63,8 @@ public class PlayerListener implements Listener {
|
||||
// reset cauldron when refilling to prevent
|
||||
// unlimited source of potions
|
||||
} else if (materialInHand == Material.WATER_BUCKET) {
|
||||
if (clickedBlock.getData() != 0) {
|
||||
if (clickedBlock.getData() < 3) {
|
||||
if (BCauldron.getFillLevel(clickedBlock) != 0) {
|
||||
if (BCauldron.getFillLevel(clickedBlock) < 2) {
|
||||
// will only remove when existing
|
||||
BCauldron.remove(clickedBlock);
|
||||
}
|
||||
@ -118,7 +118,7 @@ public class PlayerListener implements Listener {
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else if (type == Material.FENCE || type == Material.NETHER_FENCE || type == Material.SIGN || type == Material.WALL_SIGN) {
|
||||
} else if (type == Material.FENCE || type == Material.NETHER_FENCE || type == Material.SIGN_POST || type == Material.WALL_SIGN) {
|
||||
barrel = Barrel.getBySpigot(clickedBlock);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user