Update to 1.17

This commit is contained in:
Sn0wStorm 2021-06-14 16:39:29 +02:00
parent da9f84acb4
commit b43c647c5f
8 changed files with 102 additions and 52 deletions

View File

@ -148,7 +148,7 @@
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.16.5-R0.1-SNAPSHOT</version>
<version>1.17-R0.1-SNAPSHOT</version>
<scope>provided</scope>
<exclusions>
<exclusion>

View File

@ -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

View File

@ -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<String, BPlayer> players = new HashMap<>();// Players uuid and BPlayer
private static Map<Player, MutableInt> 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));
}
}

View File

@ -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) {

View File

@ -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;

View File

@ -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);

View File

@ -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;

View File

@ -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;
}