2018-10-26 21:17:09 +02:00
|
|
|
package com.dre.brewery;
|
|
|
|
|
2016-07-09 00:01:31 +02:00
|
|
|
import com.dre.brewery.api.events.barrel.BarrelAccessEvent;
|
|
|
|
import com.dre.brewery.api.events.barrel.BarrelCreateEvent;
|
|
|
|
import com.dre.brewery.api.events.barrel.BarrelDestroyEvent;
|
|
|
|
import com.dre.brewery.api.events.barrel.BarrelRemoveEvent;
|
2019-10-16 17:21:04 +02:00
|
|
|
import com.dre.brewery.filedata.BConfig;
|
2019-11-06 19:44:52 +01:00
|
|
|
import com.dre.brewery.integration.barrel.LogBlockBarrel;
|
2016-07-01 22:01:35 +02:00
|
|
|
import com.dre.brewery.lore.BrewLore;
|
2019-10-16 20:07:09 +02:00
|
|
|
import com.dre.brewery.utility.BUtil;
|
2019-10-21 18:47:19 +02:00
|
|
|
import com.dre.brewery.utility.BoundingBox;
|
2019-10-16 20:07:09 +02:00
|
|
|
import com.dre.brewery.utility.LegacyUtil;
|
2019-10-16 19:35:29 +02:00
|
|
|
import org.bukkit.Location;
|
2018-10-26 21:17:09 +02:00
|
|
|
import org.bukkit.Material;
|
2019-10-16 17:21:04 +02:00
|
|
|
import org.bukkit.Sound;
|
|
|
|
import org.bukkit.SoundCategory;
|
2018-10-26 21:17:09 +02:00
|
|
|
import org.bukkit.block.Block;
|
2019-08-19 14:37:45 +02:00
|
|
|
import org.bukkit.configuration.ConfigurationSection;
|
2019-09-12 11:01:13 +02:00
|
|
|
import org.bukkit.entity.HumanEntity;
|
2019-08-19 14:37:45 +02:00
|
|
|
import org.bukkit.entity.Player;
|
2018-10-26 21:17:09 +02:00
|
|
|
import org.bukkit.event.player.PlayerInteractEvent;
|
|
|
|
import org.bukkit.inventory.Inventory;
|
|
|
|
import org.bukkit.inventory.InventoryHolder;
|
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
import org.bukkit.inventory.meta.PotionMeta;
|
|
|
|
import org.bukkit.scheduler.BukkitRunnable;
|
2019-10-18 16:30:03 +02:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
2019-10-21 18:47:19 +02:00
|
|
|
import org.jetbrains.annotations.Nullable;
|
2018-10-26 21:17:09 +02:00
|
|
|
|
2019-08-19 14:37:45 +02:00
|
|
|
import java.util.ArrayList;
|
2019-09-12 11:01:13 +02:00
|
|
|
import java.util.List;
|
2019-08-19 14:37:45 +02:00
|
|
|
import java.util.Map;
|
2018-10-26 21:17:09 +02:00
|
|
|
|
2019-11-25 22:16:16 +01:00
|
|
|
/**
|
|
|
|
* A Multi Block Barrel with Inventory
|
|
|
|
*/
|
2018-10-26 21:17:09 +02:00
|
|
|
public class Barrel implements InventoryHolder {
|
|
|
|
|
2019-11-08 16:22:58 +01:00
|
|
|
public static List<Barrel> barrels = new ArrayList<>();
|
2019-12-02 15:56:30 +01:00
|
|
|
private static int check = 0; // Which Barrel was last checked
|
2018-10-26 21:17:09 +02:00
|
|
|
|
2019-10-16 19:35:29 +02:00
|
|
|
private final Block spigot;
|
|
|
|
private final BarrelBody body; // The Blocks that make up a Barrel in the World
|
|
|
|
private boolean checked; // Checked by the random BarrelCheck routine
|
2018-10-26 21:17:09 +02:00
|
|
|
private Inventory inventory;
|
|
|
|
private float time;
|
|
|
|
|
|
|
|
public Barrel(Block spigot, byte signoffset) {
|
|
|
|
this.spigot = spigot;
|
2019-10-18 16:30:03 +02:00
|
|
|
if (isLarge()) {
|
|
|
|
inventory = P.p.getServer().createInventory(this, 27, P.p.languageReader.get("Etc_Barrel"));
|
|
|
|
} else {
|
|
|
|
inventory = P.p.getServer().createInventory(this, 9, P.p.languageReader.get("Etc_Barrel"));
|
|
|
|
}
|
2019-10-16 19:35:29 +02:00
|
|
|
body = new BarrelBody(this, signoffset);
|
2018-10-26 21:17:09 +02:00
|
|
|
}
|
|
|
|
|
2019-11-25 22:16:16 +01:00
|
|
|
/**
|
|
|
|
* load from file
|
|
|
|
*/
|
2019-10-21 18:47:19 +02:00
|
|
|
public Barrel(Block spigot, byte sign, BoundingBox bounds, Map<String, Object> items, float time) {
|
2020-04-07 12:45:55 +02:00
|
|
|
this(spigot, sign, bounds, items, time, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load from File
|
|
|
|
* <p>If async: true, The Barrel Bounds will not be recreated when missing/corrupt, getBody().getBounds() will be null if it needs recreating
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public Barrel(Block spigot, byte sign, BoundingBox bounds, Map<String, Object> items, float time, boolean async) {
|
2018-10-26 21:17:09 +02:00
|
|
|
this.spigot = spigot;
|
|
|
|
if (isLarge()) {
|
2016-07-09 00:01:31 +02:00
|
|
|
this.inventory = P.p.getServer().createInventory(this, 27, P.p.languageReader.get("Etc_Barrel"));
|
2018-10-26 21:17:09 +02:00
|
|
|
} else {
|
2016-07-09 00:01:31 +02:00
|
|
|
this.inventory = P.p.getServer().createInventory(this, 9, P.p.languageReader.get("Etc_Barrel"));
|
2018-10-26 21:17:09 +02:00
|
|
|
}
|
|
|
|
if (items != null) {
|
|
|
|
for (String slot : items.keySet()) {
|
|
|
|
if (items.get(slot) instanceof ItemStack) {
|
|
|
|
this.inventory.setItem(P.p.parseInt(slot), (ItemStack) items.get(slot));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.time = time;
|
|
|
|
|
2020-04-07 12:45:55 +02:00
|
|
|
body = new BarrelBody(this, sign, bounds, async);
|
2018-10-26 21:17:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void onUpdate() {
|
|
|
|
for (Barrel barrel : barrels) {
|
|
|
|
// Minecraft day is 20 min, so add 1/20 to the time every minute
|
|
|
|
barrel.time += (1.0 / 20.0);
|
|
|
|
}
|
2019-11-26 21:29:41 +01:00
|
|
|
int numBarrels = barrels.size();
|
|
|
|
if (check == 0 && numBarrels > 0) {
|
|
|
|
Barrel random = barrels.get((int) Math.floor(Math.random() * numBarrels));
|
2018-10-26 21:17:09 +02:00
|
|
|
if (random != null) {
|
|
|
|
// You have been selected for a random search
|
|
|
|
// We want to check at least one barrel every time
|
|
|
|
random.checked = false;
|
|
|
|
}
|
2019-11-26 21:29:41 +01:00
|
|
|
if (numBarrels > 50) {
|
|
|
|
Barrel randomInTheBack = barrels.get(numBarrels - 1 - (int) (Math.random() * (numBarrels >>> 2)));
|
|
|
|
if (randomInTheBack != null) {
|
|
|
|
// Prioritize checking one of the less recently used barrels as well
|
|
|
|
randomInTheBack.checked = false;
|
|
|
|
}
|
|
|
|
}
|
2018-10-26 21:17:09 +02:00
|
|
|
new BarrelCheck().runTaskTimer(P.p, 1, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasPermsOpen(Player player, PlayerInteractEvent event) {
|
|
|
|
if (isLarge()) {
|
|
|
|
if (!player.hasPermission("brewery.openbarrel.big")) {
|
|
|
|
P.p.msg(player, P.p.languageReader.get("Error_NoBarrelAccess"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!player.hasPermission("brewery.openbarrel.small")) {
|
|
|
|
P.p.msg(player, P.p.languageReader.get("Error_NoBarrelAccess"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-09 00:01:31 +02:00
|
|
|
// Call event
|
|
|
|
BarrelAccessEvent accessEvent = new BarrelAccessEvent(this, player, event.getClickedBlock());
|
2019-10-12 14:47:01 +02:00
|
|
|
// Listened to by WGBarrel7, WGBarrelNew, WGBarrelOld, GriefPreventionBarrel (IntegrationListener)
|
2016-07-09 00:01:31 +02:00
|
|
|
P.p.getServer().getPluginManager().callEvent(accessEvent);
|
|
|
|
if (accessEvent.isCancelled()) {
|
|
|
|
return false;
|
2018-10-26 21:17:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-11-25 22:16:16 +01:00
|
|
|
/**
|
|
|
|
* Ask for permission to destroy barrel
|
|
|
|
*/
|
2016-07-09 00:01:31 +02:00
|
|
|
public boolean hasPermsDestroy(Player player, Block block, BarrelDestroyEvent.Reason reason) {
|
|
|
|
// Listened to by LWCBarrel (IntegrationListener)
|
|
|
|
BarrelDestroyEvent destroyEvent = new BarrelDestroyEvent(this, block, reason, player);
|
|
|
|
P.p.getServer().getPluginManager().callEvent(destroyEvent);
|
|
|
|
return !destroyEvent.isCancelled();
|
2018-10-26 21:17:09 +02:00
|
|
|
}
|
|
|
|
|
2019-11-25 22:16:16 +01:00
|
|
|
/**
|
|
|
|
* player opens the barrel
|
|
|
|
*/
|
2018-10-26 21:17:09 +02:00
|
|
|
public void open(Player player) {
|
|
|
|
if (inventory == null) {
|
|
|
|
if (isLarge()) {
|
2016-07-09 00:01:31 +02:00
|
|
|
inventory = P.p.getServer().createInventory(this, 27, P.p.languageReader.get("Etc_Barrel"));
|
2018-10-26 21:17:09 +02:00
|
|
|
} else {
|
2016-07-09 00:01:31 +02:00
|
|
|
inventory = P.p.getServer().createInventory(this, 9, P.p.languageReader.get("Etc_Barrel"));
|
2018-10-26 21:17:09 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (time > 0) {
|
|
|
|
// if nobody has the inventory opened
|
|
|
|
if (inventory.getViewers().isEmpty()) {
|
|
|
|
// if inventory contains potions
|
|
|
|
if (inventory.contains(Material.POTION)) {
|
2019-10-16 19:35:29 +02:00
|
|
|
byte wood = body.getWood();
|
2018-10-26 21:17:09 +02:00
|
|
|
long loadTime = System.nanoTime();
|
|
|
|
for (ItemStack item : inventory.getContents()) {
|
|
|
|
if (item != null) {
|
|
|
|
Brew brew = Brew.get(item);
|
|
|
|
if (brew != null) {
|
|
|
|
brew.age(item, time, wood);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
loadTime = System.nanoTime() - loadTime;
|
|
|
|
float ftime = (float) (loadTime / 1000000.0);
|
|
|
|
P.p.debugLog("opening Barrel with potions (" + ftime + "ms)");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// reset barreltime, potions have new age
|
|
|
|
time = 0;
|
|
|
|
|
2019-10-16 17:21:04 +02:00
|
|
|
if (BConfig.useLB) {
|
2018-10-26 21:17:09 +02:00
|
|
|
try {
|
|
|
|
LogBlockBarrel.openBarrel(player, inventory, spigot.getLocation());
|
|
|
|
} catch (Throwable e) {
|
|
|
|
P.p.errorLog("Failed to Log Barrel to LogBlock!");
|
|
|
|
P.p.errorLog("Brewery was tested with version 1.94 of LogBlock!");
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
player.openInventory(inventory);
|
|
|
|
}
|
|
|
|
|
2019-10-16 17:21:04 +02:00
|
|
|
public void playOpeningSound() {
|
|
|
|
float randPitch = (float) (Math.random() * 0.1);
|
2019-10-16 19:35:29 +02:00
|
|
|
Location location = getSpigot().getLocation();
|
2019-10-18 16:30:03 +02:00
|
|
|
if (location.getWorld() == null) return;
|
2019-10-16 17:21:04 +02:00
|
|
|
if (isLarge()) {
|
2019-10-16 19:35:29 +02:00
|
|
|
location.getWorld().playSound(location, Sound.BLOCK_CHEST_OPEN, SoundCategory.BLOCKS, 0.4f, 0.55f + randPitch);
|
2019-10-16 17:21:04 +02:00
|
|
|
//getSpigot().getWorld().playSound(getSpigot().getLocation(), Sound.ITEM_BUCKET_EMPTY, SoundCategory.BLOCKS, 0.5f, 0.6f + randPitch);
|
2019-10-16 19:35:29 +02:00
|
|
|
location.getWorld().playSound(location, Sound.BLOCK_BREWING_STAND_BREW, SoundCategory.BLOCKS, 0.4f, 0.45f + randPitch);
|
2019-10-16 17:21:04 +02:00
|
|
|
} else {
|
2019-10-16 19:35:29 +02:00
|
|
|
location.getWorld().playSound(location, Sound.BLOCK_BARREL_OPEN, SoundCategory.BLOCKS, 0.5f, 0.8f + randPitch);
|
2019-10-16 17:21:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void playClosingSound() {
|
|
|
|
float randPitch = (float) (Math.random() * 0.1);
|
2019-10-16 19:35:29 +02:00
|
|
|
Location location = getSpigot().getLocation();
|
2019-10-18 16:30:03 +02:00
|
|
|
if (location.getWorld() == null) return;
|
2019-10-16 17:21:04 +02:00
|
|
|
if (isLarge()) {
|
2019-10-16 19:35:29 +02:00
|
|
|
location.getWorld().playSound(location, Sound.BLOCK_BARREL_CLOSE, SoundCategory.BLOCKS, 0.5f, 0.5f + randPitch);
|
|
|
|
location.getWorld().playSound(location, Sound.ITEM_BUCKET_EMPTY, SoundCategory.BLOCKS, 0.2f, 0.6f + randPitch);
|
2019-10-16 17:21:04 +02:00
|
|
|
} else {
|
2019-10-16 19:35:29 +02:00
|
|
|
location.getWorld().playSound(location, Sound.BLOCK_BARREL_CLOSE, SoundCategory.BLOCKS, 0.5f, 0.8f + randPitch);
|
2019-10-16 17:21:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-26 21:17:09 +02:00
|
|
|
@Override
|
2019-10-18 16:30:03 +02:00
|
|
|
@NotNull
|
2018-10-26 21:17:09 +02:00
|
|
|
public Inventory getInventory() {
|
|
|
|
return inventory;
|
|
|
|
}
|
|
|
|
|
2019-10-18 16:30:03 +02:00
|
|
|
@NotNull
|
2019-08-17 21:00:15 +02:00
|
|
|
public Block getSpigot() {
|
|
|
|
return spigot;
|
|
|
|
}
|
|
|
|
|
2019-10-18 16:30:03 +02:00
|
|
|
@NotNull
|
2019-10-16 19:35:29 +02:00
|
|
|
public BarrelBody getBody() {
|
|
|
|
return body;
|
|
|
|
}
|
|
|
|
|
2016-07-09 00:01:31 +02:00
|
|
|
public float getTime() {
|
|
|
|
return time;
|
|
|
|
}
|
|
|
|
|
2019-11-25 22:16:16 +01:00
|
|
|
/**
|
|
|
|
* Returns true if this Block is part of this Barrel
|
|
|
|
*/
|
2018-10-26 21:17:09 +02:00
|
|
|
public boolean hasBlock(Block block) {
|
2019-10-16 19:35:29 +02:00
|
|
|
return body.hasBlock(block);
|
2018-10-26 21:17:09 +02:00
|
|
|
}
|
|
|
|
|
2019-10-21 18:47:19 +02:00
|
|
|
/**
|
2019-11-25 22:16:16 +01:00
|
|
|
* @deprecated just use hasBlock
|
2019-10-21 18:47:19 +02:00
|
|
|
*/
|
|
|
|
@Deprecated
|
2018-10-26 21:17:09 +02:00
|
|
|
public boolean hasWoodBlock(Block block) {
|
2019-10-21 18:47:19 +02:00
|
|
|
return body.hasBlock(block);
|
2018-10-26 21:17:09 +02:00
|
|
|
}
|
|
|
|
|
2019-10-21 18:47:19 +02:00
|
|
|
/**
|
2019-11-25 22:16:16 +01:00
|
|
|
* @deprecated just use hasBlock
|
2019-10-21 18:47:19 +02:00
|
|
|
*/
|
|
|
|
@Deprecated
|
2018-10-26 21:17:09 +02:00
|
|
|
public boolean hasStairsBlock(Block block) {
|
2019-10-21 18:47:19 +02:00
|
|
|
return body.hasBlock(block);
|
2018-10-26 21:17:09 +02:00
|
|
|
}
|
|
|
|
|
2019-11-25 22:16:16 +01:00
|
|
|
/**
|
|
|
|
* Get the Barrel by Block, null if that block is not part of a barrel
|
|
|
|
*/
|
2019-10-21 18:47:19 +02:00
|
|
|
@Nullable
|
2018-10-26 21:17:09 +02:00
|
|
|
public static Barrel get(Block block) {
|
|
|
|
if (block == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
Material type = block.getType();
|
|
|
|
if (LegacyUtil.isFence(type) || LegacyUtil.isSign(type) ) {
|
|
|
|
return getBySpigot(block);
|
2019-10-21 18:47:19 +02:00
|
|
|
} else {
|
2018-10-26 21:17:09 +02:00
|
|
|
return getByWood(block);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-25 22:16:16 +01:00
|
|
|
/**
|
|
|
|
* Get the Barrel by Sign or Spigot (Fastest)
|
|
|
|
*/
|
2019-10-21 18:47:19 +02:00
|
|
|
@Nullable
|
2018-10-26 21:17:09 +02:00
|
|
|
public static Barrel getBySpigot(Block sign) {
|
|
|
|
// convert spigot if neccessary
|
2019-10-16 19:35:29 +02:00
|
|
|
Block spigot = BarrelBody.getSpigotOfSign(sign);
|
2018-10-26 21:17:09 +02:00
|
|
|
|
|
|
|
byte signoffset = 0;
|
|
|
|
if (!spigot.equals(sign)) {
|
|
|
|
signoffset = (byte) (sign.getY() - spigot.getY());
|
|
|
|
}
|
|
|
|
|
2019-11-26 21:29:41 +01:00
|
|
|
int i = 0;
|
2018-10-26 21:17:09 +02:00
|
|
|
for (Barrel barrel : barrels) {
|
2019-10-16 19:35:29 +02:00
|
|
|
if (barrel.body.isSignOfBarrel(signoffset)) {
|
2019-10-18 16:30:03 +02:00
|
|
|
if (barrel.spigot.equals(spigot)) {
|
2019-10-16 19:35:29 +02:00
|
|
|
if (barrel.body.getSignoffset() == 0 && signoffset != 0) {
|
2018-10-26 21:17:09 +02:00
|
|
|
// Barrel has no signOffset even though we clicked a sign, may be old
|
2019-10-16 19:35:29 +02:00
|
|
|
barrel.body.setSignoffset(signoffset);
|
2018-10-26 21:17:09 +02:00
|
|
|
}
|
2019-11-26 21:29:41 +01:00
|
|
|
moveMRU(i);
|
2018-10-26 21:17:09 +02:00
|
|
|
return barrel;
|
|
|
|
}
|
|
|
|
}
|
2019-11-26 21:29:41 +01:00
|
|
|
i++;
|
2018-10-26 21:17:09 +02:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-11-25 22:16:16 +01:00
|
|
|
/**
|
|
|
|
* Get the barrel by its corpus (Wood Planks, Stairs)
|
|
|
|
*/
|
2019-10-21 18:47:19 +02:00
|
|
|
@Nullable
|
2018-10-26 21:17:09 +02:00
|
|
|
public static Barrel getByWood(Block wood) {
|
2019-10-21 18:47:19 +02:00
|
|
|
if (LegacyUtil.isWoodPlanks(wood.getType()) || LegacyUtil.isWoodStairs(wood.getType())) {
|
2019-11-26 21:29:41 +01:00
|
|
|
int i = 0;
|
2018-10-26 21:17:09 +02:00
|
|
|
for (Barrel barrel : barrels) {
|
2019-10-21 18:47:19 +02:00
|
|
|
if (barrel.getSpigot().getWorld().equals(wood.getWorld()) && barrel.body.getBounds().contains(wood)) {
|
2019-11-26 21:29:41 +01:00
|
|
|
moveMRU(i);
|
2018-10-26 21:17:09 +02:00
|
|
|
return barrel;
|
|
|
|
}
|
2019-11-26 21:29:41 +01:00
|
|
|
i++;
|
2018-10-26 21:17:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-11-26 21:29:41 +01:00
|
|
|
// Move Barrel that was recently used more towards the front of the List
|
|
|
|
// Optimizes retrieve by Block over time
|
|
|
|
private static void moveMRU(int index) {
|
|
|
|
if (index > 0) {
|
|
|
|
// Swap entry at the index with the one next to it
|
|
|
|
barrels.set(index - 1, barrels.set(index, barrels.get(index - 1)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-25 22:16:16 +01:00
|
|
|
/**
|
|
|
|
* creates a new Barrel out of a sign
|
|
|
|
*/
|
2018-10-26 21:17:09 +02:00
|
|
|
public static boolean create(Block sign, Player player) {
|
2019-10-16 19:35:29 +02:00
|
|
|
Block spigot = BarrelBody.getSpigotOfSign(sign);
|
2018-10-26 21:17:09 +02:00
|
|
|
|
|
|
|
byte signoffset = 0;
|
|
|
|
if (!spigot.equals(sign)) {
|
|
|
|
signoffset = (byte) (sign.getY() - spigot.getY());
|
|
|
|
}
|
|
|
|
|
|
|
|
Barrel barrel = getBySpigot(spigot);
|
|
|
|
if (barrel == null) {
|
|
|
|
barrel = new Barrel(spigot, signoffset);
|
2019-10-16 19:35:29 +02:00
|
|
|
if (barrel.body.getBrokenBlock(true) == null) {
|
2018-10-26 21:17:09 +02:00
|
|
|
if (LegacyUtil.isSign(spigot.getType())) {
|
|
|
|
if (!player.hasPermission("brewery.createbarrel.small")) {
|
|
|
|
P.p.msg(player, P.p.languageReader.get("Perms_NoSmallBarrelCreate"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!player.hasPermission("brewery.createbarrel.big")) {
|
|
|
|
P.p.msg(player, P.p.languageReader.get("Perms_NoBigBarrelCreate"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-07-09 00:01:31 +02:00
|
|
|
BarrelCreateEvent createEvent = new BarrelCreateEvent(barrel, player);
|
|
|
|
P.p.getServer().getPluginManager().callEvent(createEvent);
|
|
|
|
if (!createEvent.isCancelled()) {
|
2019-11-26 21:29:41 +01:00
|
|
|
barrels.add(0, barrel);
|
2016-07-09 00:01:31 +02:00
|
|
|
return true;
|
|
|
|
}
|
2018-10-26 21:17:09 +02:00
|
|
|
}
|
|
|
|
} else {
|
2019-10-16 19:35:29 +02:00
|
|
|
if (barrel.body.getSignoffset() == 0 && signoffset != 0) {
|
|
|
|
barrel.body.setSignoffset(signoffset);
|
2018-10-26 21:17:09 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-11-13 22:07:22 +01:00
|
|
|
/**
|
|
|
|
* Removes a barrel, throwing included potions to the ground
|
|
|
|
*
|
|
|
|
* @param broken The Block that was broken
|
|
|
|
* @param breaker The Player that broke it, or null if not known
|
|
|
|
* @param dropItems If the items in the barrels inventory should drop to the ground
|
|
|
|
*/
|
|
|
|
public void remove(@Nullable Block broken, @Nullable Player breaker, boolean dropItems) {
|
|
|
|
BarrelRemoveEvent event = new BarrelRemoveEvent(this, dropItems);
|
2016-07-09 00:01:31 +02:00
|
|
|
// Listened to by LWCBarrel (IntegrationListener)
|
|
|
|
P.p.getServer().getPluginManager().callEvent(event);
|
|
|
|
|
2018-10-26 21:17:09 +02:00
|
|
|
if (inventory != null) {
|
2019-10-18 16:30:03 +02:00
|
|
|
List<HumanEntity> viewers = new ArrayList<>(inventory.getViewers());
|
2019-09-12 11:01:13 +02:00
|
|
|
// Copy List to fix ConcModExc
|
|
|
|
for (HumanEntity viewer : viewers) {
|
|
|
|
viewer.closeInventory();
|
2018-10-26 21:17:09 +02:00
|
|
|
}
|
|
|
|
ItemStack[] items = inventory.getContents();
|
2019-08-19 14:37:45 +02:00
|
|
|
inventory.clear();
|
2019-10-16 17:21:04 +02:00
|
|
|
if (BConfig.useLB && breaker != null) {
|
2018-10-26 21:17:09 +02:00
|
|
|
try {
|
2019-10-12 13:52:55 +02:00
|
|
|
LogBlockBarrel.breakBarrel(breaker, items, spigot.getLocation());
|
2018-10-26 21:17:09 +02:00
|
|
|
} catch (Throwable e) {
|
|
|
|
P.p.errorLog("Failed to Log Barrel-break to LogBlock!");
|
|
|
|
P.p.errorLog("Brewery was tested with version 1.94 of LogBlock!");
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
2019-11-13 22:07:22 +01:00
|
|
|
if (event.willDropItems()) {
|
2016-07-09 00:01:31 +02:00
|
|
|
for (ItemStack item : items) {
|
|
|
|
if (item != null) {
|
|
|
|
Brew brew = Brew.get(item);
|
|
|
|
if (brew != null) {
|
|
|
|
// Brew before throwing
|
2019-10-16 19:35:29 +02:00
|
|
|
brew.age(item, time, body.getWood());
|
2016-07-09 00:01:31 +02:00
|
|
|
PotionMeta meta = (PotionMeta) item.getItemMeta();
|
|
|
|
if (BrewLore.hasColorLore(meta)) {
|
|
|
|
BrewLore lore = new BrewLore(brew, meta);
|
|
|
|
lore.convertLore(false);
|
|
|
|
lore.write();
|
|
|
|
item.setItemMeta(meta);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// "broken" is the block that destroyed, throw them there!
|
|
|
|
if (broken != null) {
|
|
|
|
broken.getWorld().dropItem(broken.getLocation(), item);
|
|
|
|
} else {
|
|
|
|
spigot.getWorld().dropItem(spigot.getLocation(), item);
|
2018-10-26 21:17:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
barrels.remove(this);
|
|
|
|
}
|
|
|
|
|
2019-11-25 22:16:16 +01:00
|
|
|
/**
|
|
|
|
* is this a Large barrel?
|
|
|
|
*/
|
2018-10-26 21:17:09 +02:00
|
|
|
public boolean isLarge() {
|
2016-07-09 00:01:31 +02:00
|
|
|
return !isSmall();
|
|
|
|
}
|
|
|
|
|
2019-11-25 22:16:16 +01:00
|
|
|
/**
|
|
|
|
* is this a Small barrel?
|
|
|
|
*/
|
2016-07-09 00:01:31 +02:00
|
|
|
public boolean isSmall() {
|
2019-10-18 16:30:03 +02:00
|
|
|
return LegacyUtil.isSign(spigot.getType());
|
2018-10-26 21:17:09 +02:00
|
|
|
}
|
|
|
|
|
2019-11-25 22:16:16 +01:00
|
|
|
/**
|
|
|
|
* returns the Sign of a large barrel, the spigot if there is none
|
|
|
|
*/
|
2018-10-26 21:17:09 +02:00
|
|
|
public Block getSignOfSpigot() {
|
2019-10-16 19:35:29 +02:00
|
|
|
return body.getSignOfSpigot();
|
2018-10-26 21:17:09 +02:00
|
|
|
}
|
|
|
|
|
2019-11-25 22:16:16 +01:00
|
|
|
/**
|
|
|
|
* returns the fence above/below a block, itself if there is none
|
|
|
|
*/
|
2018-10-26 21:17:09 +02:00
|
|
|
public static Block getSpigotOfSign(Block block) {
|
2019-10-16 19:35:29 +02:00
|
|
|
return BarrelBody.getSpigotOfSign(block);
|
2018-10-26 21:17:09 +02:00
|
|
|
}
|
|
|
|
|
2019-11-25 22:16:16 +01:00
|
|
|
/**
|
|
|
|
* returns null if Barrel is correctly placed; the block that is missing when not.
|
|
|
|
* <p>The barrel needs to be formed correctly
|
|
|
|
*
|
|
|
|
* @param force to also check even if chunk is not loaded
|
|
|
|
*/
|
2018-10-26 21:17:09 +02:00
|
|
|
public Block getBrokenBlock(boolean force) {
|
2019-10-16 19:35:29 +02:00
|
|
|
return body.getBrokenBlock(force);
|
2018-10-26 21:17:09 +02:00
|
|
|
}
|
|
|
|
|
2019-11-25 22:16:16 +01:00
|
|
|
/**
|
|
|
|
* unloads barrels that are in a unloading world
|
|
|
|
*/
|
2019-10-16 17:21:04 +02:00
|
|
|
public static void onUnload(String name) {
|
2019-11-08 16:22:58 +01:00
|
|
|
barrels.removeIf(barrel -> barrel.spigot.getWorld().getName().equals(name));
|
2019-10-16 17:21:04 +02:00
|
|
|
}
|
|
|
|
|
2019-11-25 22:16:16 +01:00
|
|
|
/**
|
|
|
|
* Saves all data
|
|
|
|
*/
|
2019-10-16 17:21:04 +02:00
|
|
|
public static void save(ConfigurationSection config, ConfigurationSection oldData) {
|
|
|
|
BUtil.createWorldSections(config);
|
|
|
|
|
2019-10-16 19:35:29 +02:00
|
|
|
if (!barrels.isEmpty()) {
|
2019-10-16 17:21:04 +02:00
|
|
|
int id = 0;
|
|
|
|
for (Barrel barrel : barrels) {
|
|
|
|
|
|
|
|
String worldName = barrel.spigot.getWorld().getName();
|
|
|
|
String prefix;
|
|
|
|
|
|
|
|
if (worldName.startsWith("DXL_")) {
|
|
|
|
prefix = BUtil.getDxlName(worldName) + "." + id;
|
|
|
|
} else {
|
|
|
|
prefix = barrel.spigot.getWorld().getUID().toString() + "." + id;
|
|
|
|
}
|
|
|
|
|
|
|
|
// block: x/y/z
|
|
|
|
config.set(prefix + ".spigot", barrel.spigot.getX() + "/" + barrel.spigot.getY() + "/" + barrel.spigot.getZ());
|
|
|
|
|
2019-10-16 19:35:29 +02:00
|
|
|
// save the body data into the section as well
|
|
|
|
barrel.body.save(config, prefix);
|
2019-10-16 17:21:04 +02:00
|
|
|
|
|
|
|
if (barrel.inventory != null) {
|
|
|
|
int slot = 0;
|
|
|
|
ItemStack item;
|
|
|
|
ConfigurationSection invConfig = null;
|
|
|
|
while (slot < barrel.inventory.getSize()) {
|
|
|
|
item = barrel.inventory.getItem(slot);
|
|
|
|
if (item != null) {
|
|
|
|
if (invConfig == null) {
|
|
|
|
if (barrel.time != 0) {
|
|
|
|
config.set(prefix + ".time", barrel.time);
|
|
|
|
}
|
|
|
|
invConfig = config.createSection(prefix + ".inv");
|
|
|
|
}
|
|
|
|
// ItemStacks are configurationSerializeable, makes them
|
|
|
|
// really easy to save
|
|
|
|
invConfig.set(slot + "", item);
|
|
|
|
}
|
|
|
|
|
|
|
|
slot++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
id++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// also save barrels that are not loaded
|
|
|
|
if (oldData != null){
|
|
|
|
for (String uuid : oldData.getKeys(false)) {
|
|
|
|
if (!config.contains(uuid)) {
|
|
|
|
config.set(uuid, oldData.get(uuid));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-26 21:17:09 +02:00
|
|
|
public static class BarrelCheck extends BukkitRunnable {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
boolean repeat = true;
|
|
|
|
while (repeat) {
|
|
|
|
if (check < barrels.size()) {
|
|
|
|
Barrel barrel = barrels.get(check);
|
|
|
|
if (!barrel.checked) {
|
2019-10-16 19:35:29 +02:00
|
|
|
Block broken = barrel.body.getBrokenBlock(false);
|
2018-10-26 21:17:09 +02:00
|
|
|
if (broken != null) {
|
2019-11-26 21:29:41 +01:00
|
|
|
P.p.debugLog("Barrel at "
|
|
|
|
+ broken.getWorld().getName() + "/" + broken.getX() + "/" + broken.getY() + "/" + broken.getZ()
|
|
|
|
+ " has been destroyed unexpectedly, contents will drop");
|
2018-10-26 21:17:09 +02:00
|
|
|
// remove the barrel if it was destroyed
|
2019-11-13 22:07:22 +01:00
|
|
|
barrel.remove(broken, null, true);
|
2018-10-26 21:17:09 +02:00
|
|
|
} else {
|
2019-11-26 21:29:41 +01:00
|
|
|
// Dont check this barrel again, its enough to check it once after every restart (and when randomly chosen)
|
|
|
|
// as now this is only the backup if we dont register the barrel breaking,
|
|
|
|
// for example when removing it with some world editor
|
2018-10-26 21:17:09 +02:00
|
|
|
barrel.checked = true;
|
|
|
|
}
|
|
|
|
repeat = false;
|
|
|
|
}
|
|
|
|
check++;
|
|
|
|
} else {
|
|
|
|
check = 0;
|
|
|
|
repeat = false;
|
|
|
|
cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|