From 6ee4bbfe3daa4ef47cc4e72d8c9b8a15184ded29 Mon Sep 17 00:00:00 2001 From: Lukas Alt Date: Sun, 2 Jul 2023 23:43:51 +0200 Subject: [PATCH 1/3] Fix metrics scheduler for Folia (#2449) --- src/main/java/com/comphenix/protocol/metrics/Metrics.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/comphenix/protocol/metrics/Metrics.java b/src/main/java/com/comphenix/protocol/metrics/Metrics.java index 03e8d9bb..3f41b5a1 100644 --- a/src/main/java/com/comphenix/protocol/metrics/Metrics.java +++ b/src/main/java/com/comphenix/protocol/metrics/Metrics.java @@ -1,9 +1,9 @@ package com.comphenix.protocol.metrics; +import com.comphenix.protocol.ProtocolLib; import org.bukkit.Bukkit; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; -import org.bukkit.plugin.Plugin; import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.ServicePriority; import org.json.simple.JSONArray; @@ -56,7 +56,7 @@ public class Metrics { private static String serverUUID; // The plugin - private final Plugin plugin; + private final ProtocolLib plugin; // A list with all custom charts private final List charts = new ArrayList<>(); @@ -66,7 +66,7 @@ public class Metrics { * * @param plugin The plugin which stats should be submitted. */ - public Metrics(Plugin plugin) { + public Metrics(ProtocolLib plugin) { if (plugin == null) { throw new IllegalArgumentException("Plugin cannot be null!"); } @@ -151,7 +151,7 @@ public class Metrics { } // Nevertheless we want our code to run in the Bukkit main thread, so we have to use the Bukkit scheduler // Don't be afraid! The connection to the bStats server is still async, only the stats collection is sync ;) - Bukkit.getScheduler().runTask(plugin, Metrics.this::submitData); + plugin.getScheduler().runTask(Metrics.this::submitData); } }, 1000L * 60 * 5, 1000L * 60 * 30); // Submit the data every 30 minutes, first time after 5 minutes to give other plugins enough time to start From 2c48b1c019c1f7752a3fb8bff5c1fc3310945420 Mon Sep 17 00:00:00 2001 From: RobotHanzo <36107150+RobotHanzo@users.noreply.github.com> Date: Mon, 3 Jul 2023 05:49:29 +0800 Subject: [PATCH 2/3] Added support for dust color transition particles (#2455) --- .../protocol/wrappers/WrappedParticle.java | 92 +++++++++++++++++-- .../wrappers/WrappedParticleTest.java | 18 ++++ 2 files changed, 102 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/comphenix/protocol/wrappers/WrappedParticle.java b/src/main/java/com/comphenix/protocol/wrappers/WrappedParticle.java index daec8100..2550b143 100644 --- a/src/main/java/com/comphenix/protocol/wrappers/WrappedParticle.java +++ b/src/main/java/com/comphenix/protocol/wrappers/WrappedParticle.java @@ -75,9 +75,31 @@ public class WrappedParticle { /** * Gets this Particle's Bukkit/ProtocolLib data. The type of this data depends on the - * {@link #getParticle() Particle type}. For Block particles it will be {@link WrappedBlockData}, - * for Item crack particles, it will be an {@link ItemStack}, and for redstone particles it will - * be {@link Particle.DustOptions} + * {@link #getParticle() Particle type}. Refer to the table below for the corresponding data types. + *

+ * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *
Particle Data Types
Particle TypeParticle Data Type
Block particles (BLOCK_CRACK, BLOCK_DUST, FALLING_DUST){@link WrappedBlockData}
Item crack particles{@link ItemStack}
Redstone particles{@link Particle.DustOptions}
Dust color transition particles{@link Particle.DustTransition}
* * @return The particle data */ @@ -110,6 +132,9 @@ public class WrappedParticle { case REDSTONE: data = getRedstone(handle); break; + case DUST_COLOR_TRANSITION: + data = getDustTransition(handle); + break; default: break; } @@ -133,7 +158,7 @@ public class WrappedParticle { private static Object getRedstone(Object handle) { int r, g, b; - float alpha; + float size; if (MinecraftVersion.FEATURE_PREVIEW_UPDATE.atOrAbove()) { StructureModifier modifier = new StructureModifier<>(handle.getClass()).withTarget(handle); @@ -142,7 +167,7 @@ public class WrappedParticle { r = (int) (rgb.x() * 255); g = (int) (rgb.y() * 255); b = (int) (rgb.z() * 255); - alpha = (float) modifier.withType(float.class).read(0); + size = (float) modifier.withType(float.class).read(0); } else if (MinecraftVersion.CAVES_CLIFFS_1.atOrAbove()) { if (VECTOR_3FA == null) { VECTOR_3FA = MinecraftReflection.getLibraryClass("com.mojang.math.Vector3fa"); @@ -156,16 +181,67 @@ public class WrappedParticle { r = (int) (rgbModifier.withType(float.class).read(0) * 255); g = (int) (rgbModifier.withType(float.class).read(1) * 255); b = (int) (rgbModifier.withType(float.class).read(2) * 255); - alpha = (float) modifier.withType(float.class).read(0); + size = (float) modifier.withType(float.class).read(0); } else { StructureModifier modifier = new StructureModifier<>(handle.getClass()).withTarget(handle).withType(float.class); r = (int) (modifier.read(0) * 255); g = (int) (modifier.read(1) * 255); b = (int) (modifier.read(2) * 255); - alpha = modifier.read(3); + size = modifier.read(3); } - return new Particle.DustOptions(Color.fromRGB(r, g, b), alpha); + return new Particle.DustOptions(Color.fromRGB(r, g, b), size); + } + + private static Object getDustTransition(Object handle) { + int fromR, fromG, fromB, toR, toG, toB; + float size; + + if (MinecraftVersion.FEATURE_PREVIEW_UPDATE.atOrAbove()) { + StructureModifier modifier = new StructureModifier<>(handle.getClass()).withTarget(handle); + org.joml.Vector3f toRGB = (org.joml.Vector3f) modifier.withType(org.joml.Vector3f.class).read(0); + org.joml.Vector3f fromRGB = (org.joml.Vector3f) modifier.withType(org.joml.Vector3f.class).read(1); + size = (float) modifier.withType(float.class).read(0); + + fromR = (int) (fromRGB.x() * 255); + fromG = (int) (fromRGB.y() * 255); + fromB = (int) (fromRGB.z() * 255); + + toR = (int) (toRGB.x() * 255); + toG = (int) (toRGB.y() * 255); + toB = (int) (toRGB.z() * 255); + } else if (MinecraftVersion.CAVES_CLIFFS_1.atOrAbove()) { + if (VECTOR_3FA == null) { + VECTOR_3FA = MinecraftReflection.getLibraryClass("com.mojang.math.Vector3fa"); + } + + StructureModifier modifier = new StructureModifier<>(handle.getClass()).withTarget(handle); + + Object toRGB = modifier.withType(VECTOR_3FA).read(0); + Object fromRGB = modifier.withType(VECTOR_3FA).read(1); + size = (float) modifier.withType(float.class).read(0); + StructureModifier rgbModifier = new StructureModifier<>(VECTOR_3FA).withTarget(fromRGB); + StructureModifier rgbModifier2 = new StructureModifier<>(VECTOR_3FA).withTarget(toRGB); + + fromR = (int) (rgbModifier.withType(float.class).read(0) * 255); + fromG = (int) (rgbModifier.withType(float.class).read(1) * 255); + fromB = (int) (rgbModifier.withType(float.class).read(2) * 255); + + toR = (int) (rgbModifier2.withType(float.class).read(0) * 255); + toG = (int) (rgbModifier2.withType(float.class).read(1) * 255); + toB = (int) (rgbModifier2.withType(float.class).read(2) * 255); + } else { + StructureModifier modifier = new StructureModifier<>(handle.getClass()).withTarget(handle).withType(float.class); + toR = (int) (modifier.read(0) * 255); + toG = (int) (modifier.read(1) * 255); + toB = (int) (modifier.read(2) * 255); + size = modifier.read(3); + fromR = (int) (modifier.read(4) * 255); + fromG = (int) (modifier.read(5) * 255); + fromB = (int) (modifier.read(6) * 255); + } + + return new Particle.DustTransition(Color.fromRGB(fromR, fromG, fromB), Color.fromRGB(toR, toG, toB), size); } public static WrappedParticle create(Particle particle, T data) { diff --git a/src/test/java/com/comphenix/protocol/wrappers/WrappedParticleTest.java b/src/test/java/com/comphenix/protocol/wrappers/WrappedParticleTest.java index 23104993..93ae9b34 100644 --- a/src/test/java/com/comphenix/protocol/wrappers/WrappedParticleTest.java +++ b/src/test/java/com/comphenix/protocol/wrappers/WrappedParticleTest.java @@ -10,6 +10,7 @@ import org.bukkit.Color; import org.bukkit.Material; import org.bukkit.Particle; import org.bukkit.Particle.DustOptions; +import org.bukkit.Particle.DustTransition; import org.bukkit.inventory.ItemStack; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -59,4 +60,21 @@ public class WrappedParticleTest { assertEquals(beforeDust.getColor(), afterDust.getColor()); assertEquals(beforeDust.getSize(), afterDust.getSize(), 0); } + + @Test + public void testDustColorTransition() { + PacketContainer packet = new PacketContainer(PacketType.Play.Server.WORLD_PARTICLES); + WrappedParticle before = WrappedParticle.create(Particle.DUST_COLOR_TRANSITION, new DustTransition(Color.BLUE, Color.RED, 1)); + packet.getNewParticles().write(0, before); + + WrappedParticle after = packet.getNewParticles().read(0); + assertEquals(before.getParticle(), after.getParticle()); + + Particle.DustTransition beforeDust = (Particle.DustTransition) before.getData(); + Particle.DustTransition afterDust = (Particle.DustTransition) after.getData(); + + assertEquals(beforeDust.getColor(), afterDust.getColor()); + assertEquals(beforeDust.getToColor(), afterDust.getToColor()); + assertEquals(beforeDust.getSize(), afterDust.getSize(), 0); + } } From 12d814182d8aa004442c70f91eab66c4d86d9aa9 Mon Sep 17 00:00:00 2001 From: LOOHP Date: Mon, 3 Jul 2023 22:23:31 +0800 Subject: [PATCH 3/3] Implement getNewEntityTracker for Folia (#2464) --- .../protocol/injector/EntityUtilities.java | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/comphenix/protocol/injector/EntityUtilities.java b/src/main/java/com/comphenix/protocol/injector/EntityUtilities.java index 262eda43..442c25cc 100644 --- a/src/main/java/com/comphenix/protocol/injector/EntityUtilities.java +++ b/src/main/java/com/comphenix/protocol/injector/EntityUtilities.java @@ -27,6 +27,7 @@ import com.comphenix.protocol.reflect.fuzzy.FuzzyMethodContract; import com.comphenix.protocol.utility.MinecraftFields; import com.comphenix.protocol.utility.MinecraftReflection; import com.comphenix.protocol.utility.MinecraftVersion; +import com.comphenix.protocol.utility.Util; import com.comphenix.protocol.wrappers.WrappedIntHashMap; import java.lang.reflect.Field; @@ -34,7 +35,6 @@ import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.Collection; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -61,6 +61,7 @@ class EntityUtilities { private FieldAccessor entityTrackerField; private FieldAccessor trackedPlayersField; private FieldAccessor trackedEntitiesField; + private FieldAccessor foliaTrackerField; private MethodAccessor getEntity; private MethodAccessor getChunkProvider; @@ -203,6 +204,25 @@ class EntityUtilities { @SuppressWarnings("unchecked") private Object getNewEntityTracker(Object worldServer, int entityId) { + if (Util.isUsingFolia()) { + if (this.getEntity == null) { + Method entityGetter = FuzzyReflection.fromObject(worldServer).getMethodByReturnTypeAndParameters( + "getEntity", + MinecraftReflection.getEntityClass(), + int.class); + this.getEntity = Accessors.getMethodAccessor(entityGetter); + } + + Object entity = this.getEntity.invoke(worldServer, entityId); + + if (this.foliaTrackerField == null) { + this.foliaTrackerField = Accessors.getFieldAccessor(FuzzyReflection.fromClass(entity.getClass(), false) + .getField(FuzzyFieldContract.newBuilder().typeExact(MinecraftReflection.getEntityTrackerClass()).build())); + } + + return this.foliaTrackerField.get(entity); + } + if (this.getChunkProvider == null) { Class chunkProviderClass = MinecraftReflection.getChunkProviderServer(); this.getChunkProvider = Accessors.getMethodAccessor(FuzzyReflection.fromClass(worldServer.getClass(), false)