From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Jake Potrebic Date: Fri, 28 Jul 2023 15:02:44 -0700 Subject: [PATCH] Bandaid fix for Effect Effect or LevelEvent needs to be replaced but ideally after the enum PR has been merged upstream. Until then, this test and these fixes should address all the known issues with them diff --git a/src/main/java/org/bukkit/craftbukkit/CraftEffect.java b/src/main/java/org/bukkit/craftbukkit/CraftEffect.java index 5a5a8945c786e16ff0df62494ddd1ac85c42b53f..63f9735d356dafd579cee4423d3037eb4ed9b2c6 100644 --- a/src/main/java/org/bukkit/craftbukkit/CraftEffect.java +++ b/src/main/java/org/bukkit/craftbukkit/CraftEffect.java @@ -15,12 +15,15 @@ public class CraftEffect { public static int getDataValue(Effect effect, T data) { int datavalue; switch (effect) { + case PARTICLES_SCULK_CHARGE: // Paper - add missing effects case VILLAGER_PLANT_GROW: datavalue = (Integer) data; break; case POTION_BREAK: + if (data instanceof Potion) { // Paper - use Color for POTION_BREAK datavalue = ((Potion) data).toDamageValue() & 0x3F; break; + } // Paper - Color will fall through to cast below case INSTANT_POTION_BREAK: datavalue = ((Color) data).asRGB(); break; @@ -59,8 +62,15 @@ public class CraftEffect { } break; case STEP_SOUND: + if (data instanceof Material) { // Paper - support BlockData Preconditions.checkArgument(((Material) data).isBlock(), "Material %s is not a block!", data); datavalue = Block.getId(CraftMagicNumbers.getBlock((Material) data).defaultBlockState()); + // Paper start - support BlockData + break; + } + case PARTICLES_AND_SOUND_BRUSH_BLOCK_COMPLETE: + datavalue = Block.getId(((org.bukkit.craftbukkit.block.data.CraftBlockData) data).getState()); + // Paper end break; case COMPOSTER_FILL_ATTEMPT: datavalue = ((Boolean) data) ? 1 : 0; diff --git a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java index c7e6e73507ab198224de1b549f7c71887341c6a6..d6b789764abc50856dcc1c8da5e9e481f54d61ca 100644 --- a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java +++ b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java @@ -1379,7 +1379,7 @@ public class CraftWorld extends CraftRegionAccessor implements World { public void playEffect(Location loc, Effect effect, T data, int radius) { if (data != null) { Preconditions.checkArgument(effect.getData() != null, "Effect.%s does not have a valid Data", effect); - Preconditions.checkArgument(effect.getData().isAssignableFrom(data.getClass()), "%s data cannot be used for the %s effect", data.getClass().getName(), effect); + Preconditions.checkArgument(effect.isApplicable(data), "%s data cannot be used for the %s effect", data.getClass().getName(), effect); // Paper } else { // Special case: the axis is optional for ELECTRIC_SPARK Preconditions.checkArgument(effect.getData() == null || effect == Effect.ELECTRIC_SPARK, "Wrong kind of data for the %s effect", effect); diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java index a041fcf0702d2f8510365eac5d7d52dcaf1091d5..6c217dd817c6af0e49e33334b2aef79299372b66 100644 --- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java +++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java @@ -820,7 +820,7 @@ public class CraftPlayer extends CraftHumanEntity implements Player { Preconditions.checkArgument(effect != null, "Effect cannot be null"); if (data != null) { Preconditions.checkArgument(effect.getData() != null, "Effect.%s does not have a valid Data", effect); - Preconditions.checkArgument(effect.getData().isAssignableFrom(data.getClass()), "%s data cannot be used for the %s effect", data.getClass().getName(), effect); + Preconditions.checkArgument(effect.isApplicable(data), "%s data cannot be used for the %s effect", data.getClass().getName(), effect); // Paper } else { // Special case: the axis is optional for ELECTRIC_SPARK Preconditions.checkArgument(effect.getData() == null || effect == Effect.ELECTRIC_SPARK, "Wrong kind of data for the %s effect", effect); diff --git a/src/test/java/org/bukkit/EffectTest.java b/src/test/java/org/bukkit/EffectTest.java new file mode 100644 index 0000000000000000000000000000000000000000..35e46e3f3747193ddc2f5b8bb7bec1bc955228f3 --- /dev/null +++ b/src/test/java/org/bukkit/EffectTest.java @@ -0,0 +1,64 @@ +package org.bukkit; + +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import net.minecraft.world.level.block.LevelEvent; +import org.junit.Test; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +public class EffectTest { + + private static List collectNmsLevelEvents() throws ReflectiveOperationException { + final List events = new ArrayList<>(); + for (final Field field : LevelEvent.class.getFields()) { + if (Modifier.isStatic(field.getModifiers()) && Modifier.isFinal(field.getModifiers()) && field.getType() == int.class) { + events.add((int) field.get(null)); + } + } + return events; + } + + private static boolean isNotDeprecated(Effect effect) throws ReflectiveOperationException { + return !Effect.class.getDeclaredField(effect.name()).isAnnotationPresent(Deprecated.class); + } + + @SuppressWarnings("deprecation") + @Test + public void checkAllApiExists() throws ReflectiveOperationException { + Map toId = new HashMap<>(); + for (final Effect effect : Effect.values()) { + if (isNotDeprecated(effect)) { + final Effect put = toId.put(effect.getId(), effect); + assertNull("duplicate API effect: " + put, put); + } + } + + for (final Integer event : collectNmsLevelEvents()) { + assertNotNull("missing API Effect: " + event, toId.get(event)); + } + } + + @SuppressWarnings("deprecation") + @Test + public void checkNoExtraApi() throws ReflectiveOperationException { + Map toId = new HashMap<>(); + for (final Effect effect : Effect.values()) { + if (isNotDeprecated(effect)) { + final Effect put = toId.put(effect.getId(), effect); + assertNull("duplicate API effect: " + put, put); + } + } + + final List nmsEvents = collectNmsLevelEvents(); + for (final Map.Entry entry : toId.entrySet()) { + assertTrue("Extra API Effect: " + entry.getValue(), nmsEvents.contains(entry.getKey())); + } + } +}