2013-04-19 16:35:08 +02:00
|
|
|
package com.dre.brewery;
|
|
|
|
|
|
|
|
import java.util.concurrent.CopyOnWriteArrayList;
|
|
|
|
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.Material;
|
|
|
|
import org.bukkit.block.Block;
|
2013-04-28 23:57:41 +02:00
|
|
|
import org.bukkit.block.BlockFace;
|
2013-04-19 16:35:08 +02:00
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
import org.bukkit.Effect;
|
2013-04-30 21:41:16 +02:00
|
|
|
import org.bukkit.configuration.ConfigurationSection;
|
2013-04-19 16:35:08 +02:00
|
|
|
|
|
|
|
import com.dre.brewery.BIngredients;
|
|
|
|
|
|
|
|
public class BCauldron {
|
2013-05-03 16:08:05 +02:00
|
|
|
public static CopyOnWriteArrayList<BCauldron> bcauldrons = new CopyOnWriteArrayList<BCauldron>();
|
2013-04-19 16:35:08 +02:00
|
|
|
|
|
|
|
private BIngredients ingredients;
|
|
|
|
private Block block;
|
|
|
|
private int state;
|
|
|
|
|
2013-05-03 16:08:05 +02:00
|
|
|
public BCauldron(Block block, Material ingredient) {
|
2013-04-19 16:35:08 +02:00
|
|
|
this.block = block;
|
|
|
|
this.state = 1;
|
|
|
|
this.ingredients = new BIngredients();
|
|
|
|
add(ingredient);
|
|
|
|
bcauldrons.add(this);
|
|
|
|
}
|
|
|
|
|
2013-05-03 16:08:05 +02:00
|
|
|
// loading from file
|
|
|
|
public BCauldron(Block block, BIngredients ingredients, int state) {
|
2013-04-30 21:41:16 +02:00
|
|
|
this.block = block;
|
|
|
|
this.state = state;
|
|
|
|
this.ingredients = ingredients;
|
|
|
|
bcauldrons.add(this);
|
|
|
|
}
|
|
|
|
|
2013-05-03 16:08:05 +02:00
|
|
|
public void onUpdate() {
|
|
|
|
// Check if fire still alive
|
|
|
|
if (block.getRelative(BlockFace.DOWN).getType() == Material.FIRE || block.getRelative(BlockFace.DOWN).getType() == Material.STATIONARY_LAVA
|
|
|
|
|| block.getRelative(BlockFace.DOWN).getType() == Material.LAVA) {
|
|
|
|
// add a minute to cooking time
|
2013-04-28 23:57:41 +02:00
|
|
|
state++;
|
|
|
|
}
|
2013-04-19 16:35:08 +02:00
|
|
|
}
|
|
|
|
|
2013-05-03 16:08:05 +02:00
|
|
|
// add an ingredient to the cauldron
|
|
|
|
public void add(Material ingredient) {
|
2013-04-19 16:35:08 +02:00
|
|
|
ingredients.add(ingredient);
|
2013-05-03 16:08:05 +02:00
|
|
|
block.getWorld().playEffect(block.getLocation(), Effect.EXTINGUISH, 0);
|
|
|
|
if (state > 1) {
|
2013-04-19 16:35:08 +02:00
|
|
|
state--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-27 17:31:42 +02:00
|
|
|
// get cauldron by Block
|
|
|
|
public static BCauldron get(Block block) {
|
|
|
|
for (BCauldron bcauldron : bcauldrons) {
|
|
|
|
if (bcauldron.block.equals(block)) {
|
|
|
|
return bcauldron;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-05-03 16:08:05 +02:00
|
|
|
// get cauldron from block and add given ingredient
|
|
|
|
public static boolean ingredientAdd(Block block, Material ingredient) {
|
|
|
|
// if not empty
|
|
|
|
if (block.getData() != 0) {
|
2013-07-27 17:31:42 +02:00
|
|
|
BCauldron bcauldron = get(block);
|
|
|
|
if (bcauldron != null) {
|
|
|
|
bcauldron.add(ingredient);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
new BCauldron(block, ingredient);
|
|
|
|
return true;
|
2013-04-19 16:35:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-05-03 16:08:05 +02:00
|
|
|
// fills players bottle with cooked brew
|
|
|
|
public static boolean fill(Player player, Block block) {
|
2013-07-27 17:31:42 +02:00
|
|
|
BCauldron bcauldron = get(block);
|
|
|
|
if (bcauldron != null) {
|
|
|
|
ItemStack potion = bcauldron.ingredients.cook(bcauldron.state);
|
|
|
|
if (potion != null) {
|
|
|
|
// Bukkit Bug, inventory not updating while in event so this
|
|
|
|
// will delay the give
|
|
|
|
// but could also just use deprecated updateInventory()
|
|
|
|
giveItem(player, potion);
|
|
|
|
// player.getInventory().addItem(potion);
|
|
|
|
// player.getInventory().updateInventory();
|
|
|
|
if (block.getData() > 3) {
|
|
|
|
block.setData((byte) 3);
|
2013-04-19 16:35:08 +02:00
|
|
|
}
|
2013-07-27 17:31:42 +02:00
|
|
|
block.setData((byte) (block.getData() - 1));
|
|
|
|
|
|
|
|
if (block.getData() == 0) {
|
|
|
|
bcauldrons.remove(bcauldron);
|
|
|
|
}
|
|
|
|
return true;
|
2013-04-19 16:35:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-07-27 17:31:42 +02:00
|
|
|
// prints the current cooking time to the player
|
|
|
|
public static void printTime(Player player, Block block) {
|
|
|
|
BCauldron bcauldron = get(block);
|
|
|
|
if (bcauldron != null) {
|
|
|
|
if (bcauldron.state > 1) {
|
|
|
|
P.p.msg(player, "Dieser Kessel siedet nun seit " + bcauldron.state + " Minuten");
|
|
|
|
} else {
|
|
|
|
P.p.msg(player, "Dieser Kessel siedet seit weniger als einer Minute");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-03 16:08:05 +02:00
|
|
|
// reset to normal cauldron
|
|
|
|
public static void remove(Block block) {
|
2013-07-27 17:31:42 +02:00
|
|
|
BCauldron bcauldron = get(block);
|
|
|
|
if (bcauldron != null) {
|
|
|
|
bcauldrons.remove(bcauldron);
|
2013-04-19 16:35:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-09 21:47:58 +02:00
|
|
|
//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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-10 00:01:28 +02:00
|
|
|
public static void save(ConfigurationSection config, ConfigurationSection oldData) {
|
2013-07-28 23:53:35 +02:00
|
|
|
P.p.createWorldSections(config);
|
|
|
|
|
2013-05-28 16:25:06 +02:00
|
|
|
if (!bcauldrons.isEmpty()) {
|
|
|
|
int id = 0;
|
|
|
|
for (BCauldron cauldron : bcauldrons) {
|
2013-06-05 19:44:30 +02:00
|
|
|
String worldName = cauldron.block.getWorld().getName();
|
|
|
|
String prefix = null;
|
|
|
|
|
|
|
|
if (worldName.startsWith("DXL_")) {
|
|
|
|
prefix = P.p.getDxlName(worldName) + "." + id;
|
|
|
|
} else {
|
|
|
|
prefix = cauldron.block.getWorld().getUID().toString() + "." + id;
|
|
|
|
}
|
2013-05-28 16:25:06 +02:00
|
|
|
|
|
|
|
config.set(prefix + ".block", cauldron.block.getX() + "/" + cauldron.block.getY() + "/" + cauldron.block.getZ());
|
|
|
|
if (cauldron.state != 1) {
|
|
|
|
config.set(prefix + ".state", cauldron.state);
|
|
|
|
}
|
|
|
|
cauldron.ingredients.save(config.createSection(prefix + ".ingredients"));
|
|
|
|
id++;
|
2013-04-30 21:41:16 +02:00
|
|
|
}
|
|
|
|
}
|
2013-05-09 21:47:58 +02:00
|
|
|
// copy cauldrons that are not loaded
|
2013-05-10 00:01:28 +02:00
|
|
|
if (oldData != null){
|
|
|
|
for (String uuid : oldData.getKeys(false)) {
|
|
|
|
if (!config.contains(uuid)) {
|
|
|
|
config.set(uuid, oldData.get(uuid));
|
|
|
|
}
|
2013-05-09 21:47:58 +02:00
|
|
|
}
|
|
|
|
}
|
2013-04-30 21:41:16 +02:00
|
|
|
}
|
|
|
|
|
2013-05-03 16:08:05 +02:00
|
|
|
// bukkit bug not updating the inventory while executing event, have to
|
|
|
|
// schedule the give
|
|
|
|
public static void giveItem(final Player player, final ItemStack item) {
|
2013-04-19 16:35:08 +02:00
|
|
|
P.p.getServer().getScheduler().runTaskLater(P.p, new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
player.getInventory().addItem(item);
|
|
|
|
}
|
2013-05-03 16:08:05 +02:00
|
|
|
}, 1L);
|
2013-04-19 16:35:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|