Added multiworld support

This commit is contained in:
Sn0wStorm 2013-05-09 21:47:58 +02:00
parent 873b54f532
commit 43824e80a6
7 changed files with 206 additions and 108 deletions

View File

@ -105,18 +105,34 @@ public class BCauldron {
}
}
public static void save(ConfigurationSection config) {
//unloads cauldrons that are in a unloading world
public static void onUnload(String name) {
for (BCauldron bcauldron : bcauldrons) {
if (bcauldron.block.getWorld().getName().equals(name)) {
bcauldrons.remove(bcauldron);
}
}
}
public static void save(ConfigurationSection config, ConfigurationSection oldConfig) {
int id = 0;
for (BCauldron cauldron : bcauldrons) {
// cauldrons are randomly listed
ConfigurationSection section = config.createSection("" + id);
section.set("block", cauldron.block.getWorld().getName() + "/" + cauldron.block.getX() + "/" + cauldron.block.getY() + "/" + cauldron.block.getZ());
// cauldrons are sorted in worldUUID.randomId
String prefix = cauldron.block.getWorld().getUID().toString() + "." + id;
config.set(prefix + ".block", cauldron.block.getX() + "/" + cauldron.block.getY() + "/" + cauldron.block.getZ());
if (cauldron.state != 1) {
section.set("state", cauldron.state);
config.set(prefix + ".state", cauldron.state);
}
cauldron.ingredients.save(section.createSection("ingredients"));
cauldron.ingredients.save(config.createSection(prefix + ".ingredients"));
id++;
}
// copy cauldrons that are not loaded
for (String uuid : oldConfig.getKeys(false)) {
if (!config.contains(uuid)) {
config.set(uuid, oldConfig.get(uuid));
}
}
}
// bukkit bug not updating the inventory while executing event, have to

View File

@ -12,10 +12,7 @@ import org.bukkit.configuration.ConfigurationSection;
import com.dre.brewery.Brew;
public class BPlayer {
public static Map<String, BPlayer> players = new HashMap<String, BPlayer>();// Players
// name
// and
// BPlayer
public static Map<String, BPlayer> players = new HashMap<String, BPlayer>();// Players name and BPlayer
private int quality = 0;// = quality of drunkeness * drunkeness
private int drunkeness = 0;// = amount of drunkeness

View File

@ -131,9 +131,9 @@ public class Barrel {
}
// "broken" is the block that destroyed, throw them there!
if (broken != null) {
broken.getLocation().getWorld().dropItem(broken.getLocation(), item);
broken.getWorld().dropItem(broken.getLocation(), item);
} else {
spigot.getLocation().getWorld().dropItem(spigot.getLocation(), item);
spigot.getWorld().dropItem(spigot.getLocation(), item);
}
}
}
@ -141,18 +141,23 @@ public class Barrel {
barrels.remove(this);
}
//unloads barrels that are in a unloading world
public static void onUnload(String name) {
for (Barrel barrel : barrels) {
if (barrel.spigot.getWorld().getName().equals(name)) {
barrels.remove(barrel);
}
}
}
// Saves all data
public static void save(ConfigurationSection config) {
public static void save(ConfigurationSection config, ConfigurationSection oldConfig) {
int id = 0;
for (Barrel barrel : barrels) {
// barrels are listed randomly
ConfigurationSection idConfig = config.createSection("" + id);
// block: worldname/x/y/z
idConfig.set("spigot", barrel.spigot.getWorld().getName() + "/" + barrel.spigot.getX() + "/" + barrel.spigot.getY() + "/" + barrel.spigot.getZ());
if (barrel.time != 0) {
idConfig.set("time", barrel.time);
}
// barrels are sorted in worldUUID.randomId
String prefix = barrel.spigot.getWorld().getUID().toString() + "." + id;
// block: x/y/z
config.set(prefix + ".spigot", barrel.spigot.getX() + "/" + barrel.spigot.getY() + "/" + barrel.spigot.getZ());
// not saving the inventory if there is none, or it is empty
if (barrel.inventory != null) {
@ -163,8 +168,12 @@ public class Barrel {
item = barrel.inventory.getItem(slot);
if (item != null) {
if (invConfig == null) {
if (barrel.time != 0) {
//time is only needed when there are items in inventory
config.set(prefix + ".time", barrel.time);
}
// create section only when items in inventory
invConfig = idConfig.createSection("inv");
invConfig = config.createSection(prefix + ".inv");
}
// ItemStacks are configurationSerializeable, makes them
// really easy to save
@ -177,6 +186,12 @@ public class Barrel {
id++;
}
// also save barrels that are not loaded
for (String uuid : oldConfig.getKeys(false)) {
if (!config.contains(uuid)) {
config.set(uuid, oldConfig.get(uuid));
}
}
}
// direction of the barrel from the spigot

View File

@ -2,6 +2,7 @@ package com.dre.brewery;
import java.util.Map;
import java.util.HashMap;
import java.util.UUID;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.configuration.file.FileConfiguration;
@ -13,6 +14,8 @@ import org.apache.commons.lang.math.NumberUtils;
import com.dre.brewery.listeners.BlockListener;
import com.dre.brewery.listeners.PlayerListener;
import com.dre.brewery.listeners.EntityListener;
import com.dre.brewery.listeners.InventoryListener;
import com.dre.brewery.listeners.WorldListener;
import org.bukkit.event.HandlerList;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
@ -28,6 +31,8 @@ public class P extends JavaPlugin {
public BlockListener blockListener;
public PlayerListener playerListener;
public EntityListener entityListener;
public InventoryListener inventoryListener;
public WorldListener worldListener;
@Override
public void onEnable() {
@ -40,10 +45,15 @@ public class P extends JavaPlugin {
blockListener = new BlockListener();
playerListener = new PlayerListener();
entityListener = new EntityListener();
inventoryListener = new InventoryListener();
worldListener = new WorldListener();
p.getServer().getPluginManager().registerEvents(blockListener, p);
p.getServer().getPluginManager().registerEvents(playerListener, p);
p.getServer().getPluginManager().registerEvents(entityListener, p);
p.getServer().getPluginManager().registerEvents(inventoryListener, p);
p.getServer().getPluginManager().registerEvents(worldListener, p);
p.getServer().getScheduler().runTaskTimer(p, new BreweryRunnable(), 1200, 1200);
this.log(this.getDescription().getName() + " enabled!");
@ -121,58 +131,6 @@ public class P extends JavaPlugin {
}
}
// loading BCauldron
section = data.getConfigurationSection("BCauldron");
if (section != null) {
for (String cauldron : section.getKeys(false)) {
// block is splitted into worldname/x/y/z
String block = section.getString(cauldron + ".block");
if (block != null) {
String[] splitted = block.split("/");
if (splitted.length == 4) {
new BCauldron(getServer().getWorld(splitted[0]).getBlockAt(parseInt(splitted[1]), parseInt(splitted[2]), parseInt(splitted[3])),
loadIngredients(section.getConfigurationSection(cauldron + ".ingredients")), section.getInt(cauldron + ".state", 1));
} else {
errorLog("Incomplete Block-Data in data.yml: " + section.getCurrentPath() + "." + cauldron);
}
} else {
errorLog("Missing Block-Data in data.yml: " + section.getCurrentPath() + "." + cauldron);
}
}
}
// loading Barrel
section = data.getConfigurationSection("Barrel");
if (section != null) {
for (String barrel : section.getKeys(false)) {
// block spigot is splitted into worldname/x/y/z
String spigot = section.getString(barrel + ".spigot");
if (spigot != null) {
String[] splitted = spigot.split("/");
if (splitted.length == 4) {
// load itemStacks from invSection
ConfigurationSection invSection = section.getConfigurationSection(barrel + ".inv");
if (invSection != null) {
// Map<String,ItemStack> inventory =
// section.getValues(barrel+"inv");
new Barrel(getServer().getWorld(splitted[0]).getBlockAt(parseInt(splitted[1]), parseInt(splitted[2]), parseInt(splitted[3])), invSection.getValues(true),
(float) section.getDouble(barrel + ".time", 0.0));
} else {
// errorLog("Inventory of "+section.getCurrentPath()+"."+barrel+" in data.yml is missing");
// Barrel has no inventory
new Barrel(getServer().getWorld(splitted[0]).getBlockAt(parseInt(splitted[1]), parseInt(splitted[2]), parseInt(splitted[3])), (float) section.getDouble(barrel
+ ".time", 0.0));
}
} else {
errorLog("Incomplete Block-Data in data.yml: " + section.getCurrentPath() + "." + barrel);
}
} else {
errorLog("Missing Block-Data in data.yml: " + section.getCurrentPath() + "." + barrel);
}
}
}
// loading BPlayer
section = data.getConfigurationSection("Player");
if (section != null) {
@ -182,6 +140,10 @@ public class P extends JavaPlugin {
}
}
for (org.bukkit.World world : p.getServer().getWorlds()) {
loadWorldData(world.getUID().toString());
}
} else {
errorLog("No data.yml found, will create new one!");
}
@ -205,12 +167,77 @@ public class P extends JavaPlugin {
return new BIngredients();
}
// load Block locations of given world
public void loadWorldData(String uuid) {
File file = new File(p.getDataFolder(), "data.yml");
if (file.exists()) {
FileConfiguration data = YamlConfiguration.loadConfiguration(file);
// loading BCauldron
if (data.contains("BCauldron." + uuid)) {
ConfigurationSection section = data.getConfigurationSection("BCauldron." + uuid);
for (String cauldron : section.getKeys(false)) {
// block is splitted into x/y/z
String block = section.getString(cauldron + ".block");
if (block != null) {
String[] splitted = block.split("/");
if (splitted.length == 3) {
new BCauldron(getServer().getWorld(UUID.fromString(uuid)).getBlockAt(parseInt(splitted[0]), parseInt(splitted[1]), parseInt(splitted[2])),
loadIngredients(section.getConfigurationSection(cauldron + ".ingredients")), section.getInt(cauldron + ".state", 1));
} else {
errorLog("Incomplete Block-Data in data.yml: " + section.getCurrentPath() + "." + cauldron);
}
} else {
errorLog("Missing Block-Data in data.yml: " + section.getCurrentPath() + "." + cauldron);
}
}
}
// loading Barrel
if (data.contains("Barrel." + uuid)) {
ConfigurationSection section = data.getConfigurationSection("Barrel." + uuid);
for (String barrel : section.getKeys(false)) {
// block spigot is splitted into x/y/z
String spigot = section.getString(barrel + ".spigot");
if (spigot != null) {
String[] splitted = spigot.split("/");
if (splitted.length == 3) {
// load itemStacks from invSection
ConfigurationSection invSection = section.getConfigurationSection(barrel + ".inv");
if (invSection != null) {
new Barrel(getServer().getWorld(UUID.fromString(uuid)).getBlockAt(parseInt(splitted[0]), parseInt(splitted[1]), parseInt(splitted[2])),
invSection.getValues(true), (float) section.getDouble(barrel + ".time", 0.0));
} else {
// Barrel has no inventory
new Barrel(getServer().getWorld(UUID.fromString(uuid)).getBlockAt(parseInt(splitted[0]), parseInt(splitted[1]), parseInt(splitted[2])),
(float) section.getDouble(barrel + ".time", 0.0));
}
} else {
errorLog("Incomplete Block-Data in data.yml: " + section.getCurrentPath() + "." + barrel);
}
} else {
errorLog("Missing Block-Data in data.yml: " + section.getCurrentPath() + "." + barrel);
}
}
}
}
}
// save all Data
public void saveData() {
File datafile = new File(p.getDataFolder(), "data.yml");
FileConfiguration oldConfig = YamlConfiguration.loadConfiguration(datafile);
if (datafile.exists()) {
if (lastBackup > 10) {
datafile.renameTo(new File(p.getDataFolder(), "dataBackup.yml"));
lastBackup = 0;
} else {
lastBackup++;
}
@ -222,11 +249,11 @@ public class P extends JavaPlugin {
Brew.save(configFile.createSection("Brew"));
}
if (!BCauldron.bcauldrons.isEmpty()) {
BCauldron.save(configFile.createSection("BCauldron"));
BCauldron.save(configFile.createSection("BCauldron"), oldConfig.getConfigurationSection("BCauldron"));
}
if (!Barrel.barrels.isEmpty()) {
Barrel.save(configFile.createSection("Barrel"));
Barrel.save(configFile.createSection("Barrel"), oldConfig.getConfigurationSection("Barrel"));
}
if (!BPlayer.players.isEmpty()) {

View File

@ -0,0 +1,45 @@
package com.dre.brewery.listeners;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.BrewEvent;
import org.bukkit.inventory.BrewerInventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.Material;
import com.dre.brewery.Brew;
public class InventoryListener implements Listener {
@EventHandler(priority = EventPriority.HIGH)
public void onBrew(BrewEvent event) {
int slot = 0;
BrewerInventory inv = event.getContents();
ItemStack item;
boolean custom = false;
Integer[] contents = new Integer[3];
while (slot < 3) {
item = inv.getItem(slot);
contents[slot] = 0;
if (item != null) {
if (item.getType() == Material.POTION) {
if (item.hasItemMeta()) {
if (Brew.potions.containsKey(Brew.getUID(item))) {
// has custom potion in "slot"
contents[slot] = 1;
custom = true;
}
}
}
}
slot++;
}
if (custom) {
event.setCancelled(true);
Brew.distillAll(inv, contents);
}
}
}

View File

@ -5,14 +5,12 @@ import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerItemConsumeEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.inventory.BrewEvent;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.BrewerInventory;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
@ -88,36 +86,6 @@ public class PlayerListener implements Listener {
}
@EventHandler(priority = EventPriority.HIGH)
public void onBrew(BrewEvent event) {
int slot = 0;
BrewerInventory inv = event.getContents();
ItemStack item;
boolean custom = false;
Integer[] contents = new Integer[3];
while (slot < 3) {
item = inv.getItem(slot);
contents[slot] = 0;
if (item != null) {
if (item.getType() == Material.POTION) {
if (item.hasItemMeta()) {
if (Brew.potions.containsKey(Brew.getUID(item))) {
// has custom potion in "slot"
contents[slot] = 1;
custom = true;
}
}
}
}
slot++;
}
if (custom) {
event.setCancelled(true);
Brew.distillAll(inv, contents);
}
}
// player drinks a custom potion
@EventHandler(priority = EventPriority.NORMAL)
public void onPlayerItemConsume(PlayerItemConsumeEvent event) {

View File

@ -0,0 +1,30 @@
package com.dre.brewery.listeners;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.world.WorldLoadEvent;
import org.bukkit.event.world.WorldUnloadEvent;
import com.dre.brewery.P;
import com.dre.brewery.BCauldron;
import com.dre.brewery.Barrel;
public class WorldListener implements Listener {
@EventHandler
public void onWorldLoad(WorldLoadEvent event) {
P.p.log("loading world with uuid " + event.getWorld().getUID().toString());
P.p.loadWorldData(event.getWorld().getUID().toString());
}
@EventHandler
public void onWorldUnload(WorldUnloadEvent event) {
P.p.log("Unloading world with uuid " + event.getWorld().getUID().toString());
if (!event.isCancelled()) {
P.p.saveData();
Barrel.onUnload(event.getWorld().getName());
BCauldron.onUnload(event.getWorld().getName());
}
}
}