2023-08-13 00:19:34 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Jake Potrebic <jake.m.potrebic@gmail.com>
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
2024-04-26 23:29:16 +02:00
index 71733f918ed84b9879ac1b142ef6205c5e768a9c..c856384019eff2f2d0bb831ebe1ccb0fb9210782 100644
2023-08-13 00:19:34 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/CraftEffect.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftEffect.java
2024-04-26 23:29:16 +02:00
@@ -15,6 +15,14 @@ public class CraftEffect {
2023-08-13 00:19:34 +02:00
public static <T> int getDataValue(Effect effect, T data) {
int datavalue;
switch (effect) {
2024-04-26 23:29:16 +02:00
+ // Paper start - add missing effects
+ case PARTICLES_SCULK_CHARGE:
+ case TRIAL_SPAWNER_DETECT_PLAYER:
+ case BEE_GROWTH:
+ case TURTLE_EGG_PLACEMENT:
+ case SMASH_ATTACK:
+ case TRIAL_SPAWNER_DETECT_PLAYER_OMINOUS:
+ // Paper end - add missing effects
2023-08-13 00:19:34 +02:00
case VILLAGER_PLANT_GROW:
datavalue = (Integer) data;
break;
2024-04-26 23:29:16 +02:00
@@ -26,6 +34,13 @@ public class CraftEffect {
2023-12-08 21:07:56 +01:00
Preconditions.checkArgument(data == Material.AIR || ((Material) data).isRecord(), "Invalid record type for Material %s!", data);
2024-01-14 10:46:04 +01:00
datavalue = Item.getId(CraftItemType.bukkitToMinecraft((Material) data));
2023-12-08 21:07:56 +01:00
break;
+ // Paper start - handle shoot white smoke event
+ case SHOOT_WHITE_SMOKE:
+ final BlockFace face = (BlockFace) data;
+ Preconditions.checkArgument(face.isCartesian(), face + " isn't cartesian");
+ datavalue = org.bukkit.craftbukkit.block.CraftBlock.blockFaceToNotch(face).get3DDataValue();
+ break;
+ // Paper end - handle shoot white smoke event
case SMOKE:
switch ((BlockFace) data) {
case DOWN:
2024-04-26 23:29:16 +02:00
@@ -57,10 +72,25 @@ public class CraftEffect {
2023-08-13 00:19:34 +02:00
}
break;
case STEP_SOUND:
+ if (data instanceof Material) { // Paper - support BlockData
Preconditions.checkArgument(((Material) data).isBlock(), "Material %s is not a block!", data);
2024-01-14 10:46:04 +01:00
datavalue = Block.getId(CraftBlockType.bukkitToMinecraft((Material) data).defaultBlockState());
2023-08-13 00:19:34 +02:00
+ // 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:
2024-04-26 23:29:16 +02:00
+ // Paper start - add missing effects
+ case TRIAL_SPAWNER_SPAWN:
+ case TRIAL_SPAWNER_SPAWN_MOB_AT:
+ case VAULT_ACTIVATE:
+ case VAULT_DEACTIVATE:
+ case TRIAL_SPAWNER_BECOME_OMINOUS:
+ case TRIAL_SPAWNER_SPAWN_ITEM:
+ // Paper end - add missing effects
2023-08-13 00:19:34 +02:00
datavalue = ((Boolean) data) ? 1 : 0;
2024-04-26 23:29:16 +02:00
break;
case BONE_MEAL_USE:
2023-08-13 00:19:34 +02:00
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
2024-04-25 23:21:18 +02:00
index 0c5c67480e16333641f4ebc89d892f7a0e2387fd..18c1cceb9e8b2873b24134a9e012633616634aae 100644
2023-08-13 00:19:34 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
2024-04-25 07:29:18 +02:00
@@ -1402,7 +1402,7 @@ public class CraftWorld extends CraftRegionAccessor implements World {
2023-08-13 00:19:34 +02:00
public <T> 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
2024-05-11 23:48:37 +02:00
index 7dfad8abaf5db2ca5ea942a0ce92c3313a132205..36ffa642655bea0281251996b8207dd6335043ba 100644
2023-08-13 00:19:34 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
2024-04-25 23:21:18 +02:00
@@ -915,7 +915,7 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
2023-08-13 00:19:34 +02:00
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
2024-04-26 20:39:20 +02:00
index 0000000000000000000000000000000000000000..eaccc9a154ff2f498d765a2932b6f3edb449f4f7
2023-08-13 00:19:34 +02:00
--- /dev/null
+++ b/src/test/java/org/bukkit/EffectTest.java
2024-04-26 20:39:20 +02:00
@@ -0,0 +1,79 @@
2023-08-13 00:19:34 +02:00
+package org.bukkit;
+
2024-04-26 20:39:20 +02:00
+import com.google.common.base.Joiner;
2023-08-13 00:19:34 +02:00
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
+import java.util.ArrayList;
+import java.util.HashMap;
2024-04-26 20:39:20 +02:00
+import java.util.HashSet;
2023-08-13 00:19:34 +02:00
+import java.util.List;
+import java.util.Map;
2024-04-26 20:39:20 +02:00
+import java.util.Set;
2023-08-13 00:19:34 +02:00
+import net.minecraft.world.level.block.LevelEvent;
2023-09-24 09:16:58 +02:00
+import org.junit.jupiter.api.Test;
2023-08-13 00:19:34 +02:00
+
2023-09-24 09:16:58 +02:00
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
2024-04-26 20:39:20 +02:00
+import static org.junit.jupiter.api.Assertions.fail;
2023-08-13 00:19:34 +02:00
+
+public class EffectTest {
+
+ private static List<Integer> collectNmsLevelEvents() throws ReflectiveOperationException {
+ final List<Integer> 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<Integer, Effect> toId = new HashMap<>();
+ for (final Effect effect : Effect.values()) {
+ if (isNotDeprecated(effect)) {
+ final Effect put = toId.put(effect.getId(), effect);
2023-09-24 09:16:58 +02:00
+ assertNull(put, "duplicate API effect: " + put);
2023-08-13 00:19:34 +02:00
+ }
+ }
+
2024-04-26 20:39:20 +02:00
+ final Set<Integer> missingEvents = new HashSet<>();
2023-08-13 00:19:34 +02:00
+ for (final Integer event : collectNmsLevelEvents()) {
2024-04-26 20:39:20 +02:00
+ if (toId.get(event) == null) {
+ missingEvents.add(event);
+ }
+ }
+ if (!missingEvents.isEmpty()) {
+ fail("Missing API Effects:\n" + Joiner.on("\n").join(missingEvents));
2023-08-13 00:19:34 +02:00
+ }
+ }
+
+ @Test
+ public void checkNoExtraApi() throws ReflectiveOperationException {
+ Map<Integer, Effect> toId = new HashMap<>();
+ for (final Effect effect : Effect.values()) {
+ if (isNotDeprecated(effect)) {
+ final Effect put = toId.put(effect.getId(), effect);
2023-09-24 09:16:58 +02:00
+ assertNull(put, "duplicate API effect: " + put);
2023-08-13 00:19:34 +02:00
+ }
+ }
+
+ final List<Integer> nmsEvents = collectNmsLevelEvents();
2024-04-26 20:39:20 +02:00
+ final Set<Effect> extraApiEffects = new HashSet<>();
2023-08-13 00:19:34 +02:00
+ for (final Map.Entry<Integer, Effect> entry : toId.entrySet()) {
2024-04-26 20:39:20 +02:00
+ if (!nmsEvents.contains(entry.getKey())) {
+ extraApiEffects.add(entry.getValue());
+ }
+ }
+ if (!extraApiEffects.isEmpty()) {
+ fail("Extra API Effects:\n" + Joiner.on("\n").join(extraApiEffects));
2023-08-13 00:19:34 +02:00
+ }
+ }
+}