2013-04-19 16:35:08 +02:00
|
|
|
package com.dre.brewery;
|
|
|
|
|
2016-07-09 00:01:31 +02:00
|
|
|
import com.dre.brewery.api.events.IngedientAddEvent;
|
2019-10-12 13:52:55 +02:00
|
|
|
import org.bukkit.Effect;
|
|
|
|
import org.bukkit.Material;
|
2013-04-19 16:35:08 +02:00
|
|
|
import org.bukkit.block.Block;
|
2013-04-28 23:57:41 +02:00
|
|
|
import org.bukkit.block.BlockFace;
|
2019-10-12 13:52:55 +02:00
|
|
|
import org.bukkit.block.data.BlockData;
|
|
|
|
import org.bukkit.block.data.Levelled;
|
2013-04-30 21:41:16 +02:00
|
|
|
import org.bukkit.configuration.ConfigurationSection;
|
2019-10-12 13:52:55 +02:00
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
|
|
|
|
import java.util.concurrent.CopyOnWriteArrayList;
|
2013-04-19 16:35:08 +02:00
|
|
|
|
|
|
|
public class BCauldron {
|
2019-10-12 13:52:55 +02:00
|
|
|
public static final byte EMPTY = 0, SOME = 1, FULL = 2;
|
2016-07-09 00:01:31 +02:00
|
|
|
public static CopyOnWriteArrayList<BCauldron> bcauldrons = new CopyOnWriteArrayList<>(); // TODO find best Collection
|
2013-04-19 16:35:08 +02:00
|
|
|
|
2013-09-04 23:32:09 +02:00
|
|
|
private BIngredients ingredients = new BIngredients();
|
2016-07-09 00:01:31 +02:00
|
|
|
private final Block block;
|
2013-09-04 23:32:09 +02:00
|
|
|
private int state = 1;
|
|
|
|
private boolean someRemoved = false;
|
2013-04-19 16:35:08 +02:00
|
|
|
|
2016-07-09 00:01:31 +02:00
|
|
|
public BCauldron(Block block) {
|
2013-04-19 16:35:08 +02:00
|
|
|
this.block = block;
|
|
|
|
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
|
2019-10-12 13:52:55 +02:00
|
|
|
if (!BUtil.isChunkLoaded(block) || LegacyUtil.isFireForCauldron(block.getRelative(BlockFace.DOWN))) {
|
2013-05-03 16:08:05 +02:00
|
|
|
// add a minute to cooking time
|
2013-04-28 23:57:41 +02:00
|
|
|
state++;
|
2013-09-04 23:32:09 +02:00
|
|
|
if (someRemoved) {
|
|
|
|
ingredients = ingredients.clone();
|
|
|
|
someRemoved = false;
|
|
|
|
}
|
2013-04-28 23:57:41 +02:00
|
|
|
}
|
2013-04-19 16:35:08 +02:00
|
|
|
}
|
|
|
|
|
2013-05-03 16:08:05 +02:00
|
|
|
// add an ingredient to the cauldron
|
2014-08-20 17:40:10 +02:00
|
|
|
public void add(ItemStack ingredient) {
|
2016-07-09 00:01:31 +02:00
|
|
|
if (ingredient == null || ingredient.getType() == Material.AIR) return;
|
|
|
|
if (someRemoved) { // TODO no need to clone
|
2013-09-04 23:32:09 +02:00
|
|
|
ingredients = ingredients.clone();
|
|
|
|
someRemoved = false;
|
|
|
|
}
|
2014-08-20 17:40:10 +02:00
|
|
|
ingredient = new ItemStack(ingredient.getType(), 1, ingredient.getDurability());
|
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
|
2016-07-09 00:01:31 +02:00
|
|
|
// Calls the IngredientAddEvent and may be cancelled or changed
|
|
|
|
public static boolean ingredientAdd(Block block, ItemStack ingredient, Player player) {
|
2013-05-03 16:08:05 +02:00
|
|
|
// if not empty
|
2019-10-12 13:52:55 +02:00
|
|
|
if (LegacyUtil.getFillLevel(block) != EMPTY) {
|
2013-07-27 17:31:42 +02:00
|
|
|
BCauldron bcauldron = get(block);
|
2016-07-09 00:01:31 +02:00
|
|
|
if (bcauldron == null) {
|
|
|
|
bcauldron = new BCauldron(block);
|
|
|
|
}
|
|
|
|
|
|
|
|
IngedientAddEvent event = new IngedientAddEvent(player, block, bcauldron, ingredient);
|
|
|
|
P.p.getServer().getPluginManager().callEvent(event);
|
|
|
|
if (!event.isCancelled()) {
|
|
|
|
bcauldron.add(event.getIngredient());
|
2019-10-12 13:52:55 +02:00
|
|
|
return event.willTakeItem();
|
2013-07-27 17:31:42 +02:00
|
|
|
} else {
|
2016-07-09 00:01:31 +02:00
|
|
|
return false;
|
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) {
|
2014-05-07 02:37:28 +02:00
|
|
|
if (!player.hasPermission("brewery.cauldron.fill")) {
|
|
|
|
P.p.msg(player, P.p.languageReader.get("Perms_NoCauldronFill"));
|
|
|
|
return true;
|
|
|
|
}
|
2013-07-27 17:31:42 +02:00
|
|
|
ItemStack potion = bcauldron.ingredients.cook(bcauldron.state);
|
|
|
|
if (potion != null) {
|
|
|
|
|
2018-10-27 12:09:53 +02:00
|
|
|
if (P.use1_13) {
|
2018-10-31 20:22:48 +01:00
|
|
|
BlockData data = block.getBlockData();
|
|
|
|
Levelled cauldron = ((Levelled) data);
|
2018-10-27 12:09:53 +02:00
|
|
|
if (cauldron.getLevel() <= 0) {
|
|
|
|
bcauldrons.remove(bcauldron);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
cauldron.setLevel(cauldron.getLevel() - 1);
|
2018-10-31 20:22:48 +01:00
|
|
|
// Update the new Level to the Block
|
|
|
|
// We have to use the BlockData variable "data" here instead of the casted "cauldron"
|
|
|
|
// otherwise < 1.13 crashes on plugin load for not finding the BlockData Class
|
|
|
|
block.setBlockData(data);
|
2018-10-27 12:09:53 +02:00
|
|
|
|
|
|
|
if (cauldron.getLevel() <= 0) {
|
|
|
|
bcauldrons.remove(bcauldron);
|
|
|
|
} else {
|
|
|
|
bcauldron.someRemoved = true;
|
|
|
|
}
|
|
|
|
|
2013-09-04 23:32:09 +02:00
|
|
|
} else {
|
2018-10-27 12:09:53 +02:00
|
|
|
byte data = block.getData();
|
|
|
|
if (data > 3) {
|
|
|
|
data = 3;
|
|
|
|
} else if (data <= 0) {
|
|
|
|
bcauldrons.remove(bcauldron);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
data -= 1;
|
|
|
|
LegacyUtil.setData(block, data);
|
|
|
|
|
|
|
|
if (data == 0) {
|
|
|
|
bcauldrons.remove(bcauldron);
|
|
|
|
} else {
|
|
|
|
bcauldron.someRemoved = true;
|
|
|
|
}
|
2013-07-27 17:31:42 +02:00
|
|
|
}
|
2015-03-14 21:57:56 +01:00
|
|
|
// 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();
|
2013-07-27 17:31:42 +02:00
|
|
|
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) {
|
2014-05-07 02:37:28 +02:00
|
|
|
if (!player.hasPermission("brewery.cauldron.time")) {
|
|
|
|
P.p.msg(player, P.p.languageReader.get("Error_NoPermissions"));
|
|
|
|
return;
|
|
|
|
}
|
2013-07-27 17:31:42 +02:00
|
|
|
BCauldron bcauldron = get(block);
|
|
|
|
if (bcauldron != null) {
|
|
|
|
if (bcauldron.state > 1) {
|
2013-10-15 18:14:11 +02:00
|
|
|
P.p.msg(player, P.p.languageReader.get("Player_CauldronInfo1", "" + bcauldron.state));
|
2013-07-27 17:31:42 +02:00
|
|
|
} else {
|
2013-10-15 18:14:11 +02:00
|
|
|
P.p.msg(player, P.p.languageReader.get("Player_CauldronInfo2"));
|
2013-07-27 17:31:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-03 16:08:05 +02:00
|
|
|
// reset to normal cauldron
|
2016-07-09 00:01:31 +02:00
|
|
|
public static boolean remove(Block block) {
|
2019-10-12 13:52:55 +02:00
|
|
|
if (LegacyUtil.getFillLevel(block) != EMPTY) {
|
2014-04-08 16:11:20 +02:00
|
|
|
BCauldron bcauldron = get(block);
|
|
|
|
if (bcauldron != null) {
|
|
|
|
bcauldrons.remove(bcauldron);
|
2016-07-09 00:01:31 +02:00
|
|
|
return true;
|
2014-04-08 16:11:20 +02:00
|
|
|
}
|
2013-04-19 16:35:08 +02:00
|
|
|
}
|
2016-07-09 00:01:31 +02:00
|
|
|
return false;
|
2013-04-19 16:35:08 +02:00
|
|
|
}
|
|
|
|
|
2013-08-14 20:08:55 +02:00
|
|
|
// unloads cauldrons that are in a unloading world
|
|
|
|
// as they were written to file just before, this is safe to do
|
2013-05-09 21:47:58 +02:00
|
|
|
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) {
|
2019-10-12 13:52:55 +02:00
|
|
|
BUtil.createWorldSections(config);
|
2013-07-28 23:53:35 +02:00
|
|
|
|
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();
|
2014-04-08 16:11:20 +02:00
|
|
|
String prefix;
|
2013-06-05 19:44:30 +02:00
|
|
|
|
|
|
|
if (worldName.startsWith("DXL_")) {
|
2019-10-12 13:52:55 +02:00
|
|
|
prefix = BUtil.getDxlName(worldName) + "." + id;
|
2013-06-05 19:44:30 +02:00
|
|
|
} 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);
|
|
|
|
}
|
2014-06-03 21:03:32 +02:00
|
|
|
config.set(prefix + ".ingredients", cauldron.ingredients.serializeIngredients());
|
2013-05-28 16:25:06 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-10-27 12:09:53 +02:00
|
|
|
}
|