From b43c647c5fa795900fe5fabfa9c10e0600196936 Mon Sep 17 00:00:00 2001 From: Sn0wStorm Date: Mon, 14 Jun 2021 16:39:29 +0200 Subject: [PATCH] Update to 1.17 --- pom.xml | 2 +- src/com/dre/brewery/BCauldron.java | 33 ++++++++---- src/com/dre/brewery/BPlayer.java | 54 ++++++------------- .../integration/IntegrationListener.java | 2 +- .../brewery/listeners/CauldronListener.java | 46 ++++++++++++++++ .../dre/brewery/listeners/PlayerListener.java | 2 +- src/com/dre/brewery/utility/BUtil.java | 5 +- src/com/dre/brewery/utility/LegacyUtil.java | 10 +++- 8 files changed, 102 insertions(+), 52 deletions(-) diff --git a/pom.xml b/pom.xml index ffd9721..50b77de 100644 --- a/pom.xml +++ b/pom.xml @@ -148,7 +148,7 @@ org.spigotmc spigot-api - 1.16.5-R0.1-SNAPSHOT + 1.17-R0.1-SNAPSHOT provided diff --git a/src/com/dre/brewery/BCauldron.java b/src/com/dre/brewery/BCauldron.java index 035228c..6992e8f 100644 --- a/src/com/dre/brewery/BCauldron.java +++ b/src/com/dre/brewery/BCauldron.java @@ -60,7 +60,7 @@ public class BCauldron { if (!BUtil.isChunkLoaded(block)) { increaseState(); } else { - if (block.getType() != Material.CAULDRON) { + if (!LegacyUtil.isWaterCauldron(block.getType())) { // Catch any WorldEdit etc. removal return false; } @@ -172,21 +172,35 @@ public class BCauldron { if (P.use1_13) { BlockData data = block.getBlockData(); + if (!(data instanceof Levelled)) { + bcauldrons.remove(block); + return false; + } Levelled cauldron = ((Levelled) data); if (cauldron.getLevel() <= 0) { bcauldrons.remove(block); return false; } - cauldron.setLevel(cauldron.getLevel() - 1); - // 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); - if (cauldron.getLevel() <= 0) { + if (LegacyUtil.WATER_CAULDRON != null && cauldron.getLevel() == 1) { + // Empty Cauldron + P.p.log("Empty Cauldron"); + block.setType(Material.CAULDRON); bcauldrons.remove(block); } else { - changed = true; + P.p.log("Setting level to : " + (cauldron.getLevel() - 1)); + cauldron.setLevel(cauldron.getLevel() - 1); + + // 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); + + if (cauldron.getLevel() <= 0) { + bcauldrons.remove(block); + } else { + changed = true; + } } } else { @@ -399,9 +413,10 @@ public class BCauldron { } return; - // reset cauldron when refilling to prevent unlimited source of potions + // Ignore Water Buckets } else if (materialInHand == Material.WATER_BUCKET) { if (!P.use1_9) { + // reset < 1.9 cauldron when refilling to prevent unlimited source of potions // We catch >=1.9 cases in the Cauldron Listener if (LegacyUtil.getFillLevel(clickedBlock) == 1) { // will only remove when existing diff --git a/src/com/dre/brewery/BPlayer.java b/src/com/dre/brewery/BPlayer.java index 7566b63..92292c1 100644 --- a/src/com/dre/brewery/BPlayer.java +++ b/src/com/dre/brewery/BPlayer.java @@ -27,19 +27,13 @@ import org.bukkit.potion.PotionEffectType; import org.bukkit.util.Vector; import org.jetbrains.annotations.Nullable; -import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; import java.util.*; public class BPlayer { private static Map players = new HashMap<>();// Players uuid and BPlayer private static Map pTasks = new HashMap<>();// Player and count private static int taskId; - private static boolean modAge = true; private static Random pukeRand; - private static Method itemHandle; - private static Field age; private final String uuid; private int quality = 0;// = quality of drunkeness * drunkeness @@ -620,39 +614,23 @@ public class BPlayer { Item item = player.getWorld().dropItem(loc, new ItemStack(BConfig.pukeItem)); item.setVelocity(direction); item.setPickupDelay(32767); // Item can never be picked up when pickup delay is 32767 - //item.setTicksLived(6000 - pukeDespawntime); // Well this does not work... - if (modAge) { - int pukeDespawntime = BConfig.pukeDespawntime; - if (pukeDespawntime >= 5800) { - return; - } - try { - if (itemHandle == null) { - itemHandle = Class.forName(P.p.getServer().getClass().getPackage().getName() + ".entity.CraftItem").getMethod("getHandle", (Class[]) null); - } - Object entityItem = itemHandle.invoke(item, (Object[]) null); - if (age == null) { - age = entityItem.getClass().getDeclaredField("age"); - age.setAccessible(true); - } + if (P.use1_14) item.setPersistent(false); // No need to save Puke items - // Setting the age determines when an item is despawned. At age 6000 it is removed. - if (pukeDespawntime <= 0) { - // Just show the item for a tick - age.setInt(entityItem, 5999); - } else if (pukeDespawntime <= 120) { - // it should despawn in less than 6 sec. Add up to half of that randomly - age.setInt(entityItem, 6000 - pukeDespawntime + pukeRand.nextInt((int) (pukeDespawntime / 2F))); - } else { - // Add up to 5 sec randomly - age.setInt(entityItem, 6000 - pukeDespawntime + pukeRand.nextInt(100)); - } - return; - } catch (InvocationTargetException | ClassNotFoundException | NoSuchFieldException | IllegalAccessException | NoSuchMethodException e) { - e.printStackTrace(); - } - modAge = false; - P.p.errorLog("Failed to set Despawn Time on item " + BConfig.pukeItem.name()); + int pukeDespawntime = BConfig.pukeDespawntime; + if (pukeDespawntime >= 5800) { + return; + } + + // Setting the age determines when an item is despawned. At age 6000 it is removed. + if (pukeDespawntime <= 0) { + // Just show the item for a few ticks + item.setTicksLived(5996); + } else if (pukeDespawntime <= 120) { + // it should despawn in less than 6 sec. Add up to half of that randomly + item.setTicksLived(6000 - pukeDespawntime + pukeRand.nextInt((int) (pukeDespawntime / 2F))); + } else { + // Add up to 5 sec randomly + item.setTicksLived(6000 - pukeDespawntime + pukeRand.nextInt(100)); } } diff --git a/src/com/dre/brewery/integration/IntegrationListener.java b/src/com/dre/brewery/integration/IntegrationListener.java index fe1666f..4408b45 100644 --- a/src/com/dre/brewery/integration/IntegrationListener.java +++ b/src/com/dre/brewery/integration/IntegrationListener.java @@ -324,7 +324,7 @@ public class IntegrationListener implements Listener { if (!BConfig.hasMMOItems) return; try { if (event.getAction() == Action.RIGHT_CLICK_BLOCK && event.hasItem() && event.getHand() == EquipmentSlot.HAND) { - if (event.getClickedBlock() != null && event.getClickedBlock().getType() == Material.CAULDRON) { + if (event.getClickedBlock() != null && LegacyUtil.isWaterCauldron(event.getClickedBlock().getType())) { NBTItem item = NBTItem.get(event.getItem()); if (item.hasType()) { for (RecipeItem rItem : BCauldronRecipe.acceptedCustom) { diff --git a/src/com/dre/brewery/listeners/CauldronListener.java b/src/com/dre/brewery/listeners/CauldronListener.java index c119c58..661ab2b 100644 --- a/src/com/dre/brewery/listeners/CauldronListener.java +++ b/src/com/dre/brewery/listeners/CauldronListener.java @@ -1,6 +1,11 @@ package com.dre.brewery.listeners; import com.dre.brewery.BCauldron; +import com.dre.brewery.P; +import com.dre.brewery.utility.LegacyUtil; +import org.bukkit.Material; +import org.bukkit.block.BlockState; +import org.bukkit.block.data.Levelled; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; @@ -8,8 +13,49 @@ import org.bukkit.event.block.CauldronLevelChangeEvent; public class CauldronListener implements Listener { + /** + * Water in Cauldron gets filled up: remove BCauldron to disallow unlimited Brews + * Water in Cauldron gets removed: remove BCauldron to remove Brew data and stop particles + */ @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) public void onCauldronChange(CauldronLevelChangeEvent event) { + if (LegacyUtil.WATER_CAULDRON == null) { + // < 1.17 + oldCauldronChange(event); + return; + } + + Material currentType = event.getBlock().getType(); + BlockState newState = event.getNewState(); + Material newType = newState.getType(); + + P.p.log("OldType: " + currentType + " NewType: " + newType); + + + if (currentType == Material.WATER_CAULDRON) { + if (newType != Material.WATER_CAULDRON) { + // Change from water to anything else + if (event.getReason() != CauldronLevelChangeEvent.ChangeReason.BOTTLE_FILL) { + BCauldron.remove(event.getBlock()); + } + } else { // newType == Material.WATER_CAULDRON + // Water level change + Levelled oldCauldron = ((Levelled) event.getBlock().getBlockData()); + Levelled newCauldron = ((Levelled) newState.getBlockData()); + + P.p.log("OldLevel: " + oldCauldron.getLevel() + " Newlevel: " + newCauldron.getLevel()); + + // Water Level increased somehow, might be Bucket, Bottle, Rain, etc. + if (newCauldron.getLevel() > oldCauldron.getLevel()) { + BCauldron.remove(event.getBlock()); + } + } + } + } + + + @SuppressWarnings("deprecation") + private void oldCauldronChange(CauldronLevelChangeEvent event) { if (event.getNewLevel() == 0 && event.getOldLevel() != 0) { if (event.getReason() == CauldronLevelChangeEvent.ChangeReason.BOTTLE_FILL) { return; diff --git a/src/com/dre/brewery/listeners/PlayerListener.java b/src/com/dre/brewery/listeners/PlayerListener.java index b216132..9e3a948 100644 --- a/src/com/dre/brewery/listeners/PlayerListener.java +++ b/src/com/dre/brewery/listeners/PlayerListener.java @@ -70,7 +70,7 @@ public class PlayerListener implements Listener { if (player.isSneaking()) return; // -- Interacting with a Cauldron -- - if (type == Material.CAULDRON) { + if (LegacyUtil.isWaterCauldron(type)) { // Handle the Cauldron Interact // The Event might get cancelled in here BCauldron.clickCauldron(event); diff --git a/src/com/dre/brewery/utility/BUtil.java b/src/com/dre/brewery/utility/BUtil.java index 1cdc7da..7c57371 100644 --- a/src/com/dre/brewery/utility/BUtil.java +++ b/src/com/dre/brewery/utility/BUtil.java @@ -257,8 +257,11 @@ public class BUtil { * @return True if the Block can be destroyed */ public static boolean blockDestroy(Block block, Player player, BarrelDestroyEvent.Reason reason) { + if (block == null || block.getType() == null) { + return true; + } Material type = block.getType(); - if (type == Material.CAULDRON) { + if (type == Material.CAULDRON || type == LegacyUtil.WATER_CAULDRON) { // will only remove when existing BCauldron.remove(block); return true; diff --git a/src/com/dre/brewery/utility/LegacyUtil.java b/src/com/dre/brewery/utility/LegacyUtil.java index d050601..58db8b6 100644 --- a/src/com/dre/brewery/utility/LegacyUtil.java +++ b/src/com/dre/brewery/utility/LegacyUtil.java @@ -78,6 +78,7 @@ public class LegacyUtil { FENCES = fences; } + public static final Material WATER_CAULDRON = get("WATER_CAULDRON"); public static final Material MAGMA_BLOCK = get("MAGMA_BLOCK", "MAGMA"); public static final Material CAMPFIRE = get("CAMPFIRE"); public static final Material SOUL_CAMPFIRE = get("SOUL_CAMPFIRE"); @@ -220,13 +221,20 @@ public class LegacyUtil { } } + /** + * Test if this Material Type is a Cauldron filled with water, or any cauldron in 1.16 and lower + */ + public static boolean isWaterCauldron(Material type) { + return WATER_CAULDRON != null ? type == WATER_CAULDRON : type == Material.CAULDRON; + } + /** * Get The Fill Level of a Cauldron Block, 0 = empty, 1 = something in, 2 = full * * @return 0 = empty, 1 = something in, 2 = full */ public static byte getFillLevel(Block block) { - if (block.getType() != Material.CAULDRON) { + if (!isWaterCauldron(block.getType())) { return EMPTY; }