diff --git a/paper-server/nms-patches/net/minecraft/world/level/gameevent/GameEventDispatcher.patch b/paper-server/nms-patches/net/minecraft/world/level/gameevent/GameEventDispatcher.patch index d2f58127f4..079afdc38c 100644 --- a/paper-server/nms-patches/net/minecraft/world/level/gameevent/GameEventDispatcher.patch +++ b/paper-server/nms-patches/net/minecraft/world/level/gameevent/GameEventDispatcher.patch @@ -1,26 +1,25 @@ --- a/net/minecraft/world/level/gameevent/GameEventDispatcher.java +++ b/net/minecraft/world/level/gameevent/GameEventDispatcher.java -@@ -11,6 +11,14 @@ +@@ -11,6 +11,13 @@ import net.minecraft.world.level.chunk.Chunk; import net.minecraft.world.phys.Vec3D; +// CraftBukkit start -+import net.minecraft.core.registries.BuiltInRegistries; +import org.bukkit.Bukkit; ++import org.bukkit.craftbukkit.CraftGameEvent; +import org.bukkit.craftbukkit.util.CraftLocation; -+import org.bukkit.craftbukkit.util.CraftNamespacedKey; +import org.bukkit.event.world.GenericGameEvent; +// CraftBukkit end + public class GameEventDispatcher { private final WorldServer level; -@@ -22,6 +30,14 @@ +@@ -22,6 +29,14 @@ public void post(GameEvent gameevent, Vec3D vec3d, GameEvent.a gameevent_a) { int i = gameevent.getNotificationRadius(); BlockPosition blockposition = BlockPosition.containing(vec3d); + // CraftBukkit start -+ GenericGameEvent event = new GenericGameEvent(org.bukkit.GameEvent.getByKey(CraftNamespacedKey.fromMinecraft(BuiltInRegistries.GAME_EVENT.getKey(gameevent))), CraftLocation.toBukkit(blockposition, level.getWorld()), (gameevent_a.sourceEntity() == null) ? null : gameevent_a.sourceEntity().getBukkitEntity(), i, !Bukkit.isPrimaryThread()); ++ GenericGameEvent event = new GenericGameEvent(CraftGameEvent.minecraftToBukkit(gameevent), CraftLocation.toBukkit(blockposition, level.getWorld()), (gameevent_a.sourceEntity() == null) ? null : gameevent_a.sourceEntity().getBukkitEntity(), i, !Bukkit.isPrimaryThread()); + level.getCraftServer().getPluginManager().callEvent(event); + if (event.isCancelled()) { + return; diff --git a/paper-server/nms-patches/net/minecraft/world/level/gameevent/vibrations/VibrationSystem.patch b/paper-server/nms-patches/net/minecraft/world/level/gameevent/vibrations/VibrationSystem.patch index 82732f57cb..ad86ab14ba 100644 --- a/paper-server/nms-patches/net/minecraft/world/level/gameevent/vibrations/VibrationSystem.patch +++ b/paper-server/nms-patches/net/minecraft/world/level/gameevent/vibrations/VibrationSystem.patch @@ -1,20 +1,19 @@ --- a/net/minecraft/world/level/gameevent/vibrations/VibrationSystem.java +++ b/net/minecraft/world/level/gameevent/vibrations/VibrationSystem.java -@@ -29,6 +29,13 @@ +@@ -29,6 +29,12 @@ import net.minecraft.world.phys.MovingObjectPosition; import net.minecraft.world.phys.Vec3D; +// CraftBukkit start -+import net.minecraft.core.registries.BuiltInRegistries; ++import org.bukkit.craftbukkit.CraftGameEvent; +import org.bukkit.craftbukkit.block.CraftBlock; -+import org.bukkit.craftbukkit.util.CraftNamespacedKey; +import org.bukkit.event.block.BlockReceiveGameEvent; +// CraftBukkit end + public interface VibrationSystem { GameEvent[] RESONANCE_EVENTS = new GameEvent[]{GameEvent.RESONATE_1, GameEvent.RESONATE_2, GameEvent.RESONATE_3, GameEvent.RESONATE_4, GameEvent.RESONATE_5, GameEvent.RESONATE_6, GameEvent.RESONATE_7, GameEvent.RESONATE_8, GameEvent.RESONATE_9, GameEvent.RESONATE_10, GameEvent.RESONATE_11, GameEvent.RESONATE_12, GameEvent.RESONATE_13, GameEvent.RESONATE_14, GameEvent.RESONATE_15}; -@@ -228,7 +235,8 @@ +@@ -228,7 +234,8 @@ if (vibrationsystem_d.requiresAdjacentChunksToBeTicking() && !areAdjacentChunksTicking(worldserver, blockposition1)) { return false; } else { @@ -24,7 +23,7 @@ vibrationsystem_a.setCurrentVibration((VibrationInfo) null); return true; } -@@ -285,8 +293,14 @@ +@@ -285,8 +292,14 @@ return false; } else { Vec3D vec3d1 = (Vec3D) optional.get(); @@ -33,7 +32,7 @@ + // CraftBukkit start + boolean defaultCancel = !vibrationsystem_d.canReceiveVibration(worldserver, BlockPosition.containing(vec3d), gameevent, gameevent_a); + Entity entity = gameevent_a.sourceEntity(); -+ BlockReceiveGameEvent event = new BlockReceiveGameEvent(org.bukkit.GameEvent.getByKey(CraftNamespacedKey.fromMinecraft(BuiltInRegistries.GAME_EVENT.getKey(gameevent))), CraftBlock.at(worldserver, BlockPosition.containing(vec3d1)), (entity == null) ? null : entity.getBukkitEntity()); ++ BlockReceiveGameEvent event = new BlockReceiveGameEvent(CraftGameEvent.minecraftToBukkit(gameevent), CraftBlock.at(worldserver, BlockPosition.containing(vec3d1)), (entity == null) ? null : entity.getBukkitEntity()); + event.setCancelled(defaultCancel); + worldserver.getCraftServer().getPluginManager().callEvent(event); + if (event.isCancelled()) { @@ -41,7 +40,7 @@ return false; } else if (isOccluded(worldserver, vec3d, vec3d1)) { return false; -@@ -339,7 +353,7 @@ +@@ -339,7 +352,7 @@ return instance.group(VibrationInfo.CODEC.optionalFieldOf("event").forGetter((vibrationsystem_a) -> { return Optional.ofNullable(vibrationsystem_a.currentVibration); }), VibrationSelector.CODEC.fieldOf("selector").forGetter(VibrationSystem.a::getSelectionStrategy), ExtraCodecs.NON_NEGATIVE_INT.fieldOf("event_delay").orElse(0).forGetter(VibrationSystem.a::getTravelTimeInTicks)).apply(instance, (optional, vibrationselector, integer) -> { diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftGameEvent.java b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftGameEvent.java new file mode 100644 index 0000000000..82f911fde5 --- /dev/null +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftGameEvent.java @@ -0,0 +1,71 @@ +package org.bukkit.craftbukkit; + +import com.google.common.base.Preconditions; +import net.minecraft.core.IRegistry; +import net.minecraft.core.registries.Registries; +import org.bukkit.GameEvent; +import org.bukkit.NamespacedKey; +import org.bukkit.Registry; +import org.bukkit.craftbukkit.util.CraftNamespacedKey; +import org.jetbrains.annotations.NotNull; + +public class CraftGameEvent extends GameEvent { + + public static GameEvent minecraftToBukkit(net.minecraft.world.level.gameevent.GameEvent minecraft) { + Preconditions.checkArgument(minecraft != null); + + IRegistry registry = CraftRegistry.getMinecraftRegistry(Registries.GAME_EVENT); + GameEvent bukkit = Registry.GAME_EVENT.get(CraftNamespacedKey.fromMinecraft(registry.getKey(minecraft))); + + Preconditions.checkArgument(bukkit != null); + + return bukkit; + } + + public static net.minecraft.world.level.gameevent.GameEvent bukkitToMinecraft(GameEvent bukkit) { + Preconditions.checkArgument(bukkit != null); + + return ((CraftGameEvent) bukkit).getHandle(); + } + + private final NamespacedKey key; + private final net.minecraft.world.level.gameevent.GameEvent handle; + + public CraftGameEvent(NamespacedKey key, net.minecraft.world.level.gameevent.GameEvent handle) { + this.key = key; + this.handle = handle; + } + + public net.minecraft.world.level.gameevent.GameEvent getHandle() { + return handle; + } + + @NotNull + @Override + public NamespacedKey getKey() { + return key; + } + + @Override + public boolean equals(Object other) { + if (this == other) { + return true; + } + + if (!(other instanceof CraftGameEvent)) { + return false; + } + + return getKey().equals(((GameEvent) other).getKey()); + } + + @Override + public int hashCode() { + return getKey().hashCode(); + } + + @Override + public String toString() { + return "CraftGameEvent{key=" + key + "}"; + } +} diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftMusicInstrument.java b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftMusicInstrument.java new file mode 100644 index 0000000000..89812da588 --- /dev/null +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftMusicInstrument.java @@ -0,0 +1,72 @@ +package org.bukkit.craftbukkit; + +import com.google.common.base.Preconditions; +import net.minecraft.core.IRegistry; +import net.minecraft.core.registries.Registries; +import net.minecraft.world.item.Instrument; +import org.bukkit.MusicInstrument; +import org.bukkit.NamespacedKey; +import org.bukkit.Registry; +import org.bukkit.craftbukkit.util.CraftNamespacedKey; +import org.jetbrains.annotations.NotNull; + +public class CraftMusicInstrument extends MusicInstrument { + + public static MusicInstrument minecraftToBukkit(Instrument minecraft) { + Preconditions.checkArgument(minecraft != null); + + IRegistry registry = CraftRegistry.getMinecraftRegistry(Registries.INSTRUMENT); + MusicInstrument bukkit = Registry.INSTRUMENT.get(CraftNamespacedKey.fromMinecraft(registry.getKey(minecraft))); + + Preconditions.checkArgument(bukkit != null); + + return bukkit; + } + + public static Instrument bukkitToMinecraft(MusicInstrument bukkit) { + Preconditions.checkArgument(bukkit != null); + + return ((CraftMusicInstrument) bukkit).getHandle(); + } + + private final NamespacedKey key; + private final Instrument handle; + + public CraftMusicInstrument(NamespacedKey key, Instrument handle) { + this.key = key; + this.handle = handle; + } + + public Instrument getHandle() { + return handle; + } + + @NotNull + @Override + public NamespacedKey getKey() { + return key; + } + + @Override + public boolean equals(Object other) { + if (this == other) { + return true; + } + + if (!(other instanceof CraftMusicInstrument)) { + return false; + } + + return getKey().equals(((MusicInstrument) other).getKey()); + } + + @Override + public int hashCode() { + return getKey().hashCode(); + } + + @Override + public String toString() { + return "CraftMusicInstrument{key=" + key + "}"; + } +} diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftRegistry.java b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftRegistry.java index d2e6a12549..df201d73a9 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftRegistry.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftRegistry.java @@ -1,5 +1,6 @@ package org.bukkit.craftbukkit; +import com.google.common.base.Preconditions; import java.util.HashMap; import java.util.Iterator; import java.util.Map; @@ -9,7 +10,10 @@ import net.minecraft.core.IRegistry; import net.minecraft.core.IRegistryCustom; import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.core.registries.Registries; +import net.minecraft.resources.ResourceKey; +import org.bukkit.GameEvent; import org.bukkit.Keyed; +import org.bukkit.MusicInstrument; import org.bukkit.NamespacedKey; import org.bukkit.Registry; import org.bukkit.craftbukkit.generator.structure.CraftStructure; @@ -24,7 +28,28 @@ import org.bukkit.inventory.meta.trim.TrimPattern; public class CraftRegistry implements Registry { + private static IRegistryCustom registry; + + public static void setMinecraftRegistry(IRegistryCustom registry) { + Preconditions.checkState(CraftRegistry.registry == null, "Registry already set"); + CraftRegistry.registry = registry; + } + + public static IRegistryCustom getMinecraftRegistry() { + return registry; + } + + public static IRegistry getMinecraftRegistry(ResourceKey> key) { + return getMinecraftRegistry().registryOrThrow(key); + } + public static Registry createRegistry(Class bukkitClass, IRegistryCustom registryHolder) { + if (bukkitClass == GameEvent.class) { + return new CraftRegistry<>(registryHolder.registryOrThrow(Registries.GAME_EVENT), CraftGameEvent::new); + } + if (bukkitClass == MusicInstrument.class) { + return new CraftRegistry<>(registryHolder.registryOrThrow(Registries.INSTRUMENT), CraftMusicInstrument::new); + } if (bukkitClass == Structure.class) { return new CraftRegistry<>(registryHolder.registryOrThrow(Registries.STRUCTURE), CraftStructure::new); } diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftServer.java index 1e04631b0f..7c4beedd93 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftServer.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftServer.java @@ -321,6 +321,8 @@ public final class CraftServer implements Server { Bukkit.setServer(this); + CraftRegistry.setMinecraftRegistry(console.registryAccess()); + // Register all the Enchantments and PotionTypes now so we can stop new registration immediately after Enchantments.SHARPNESS.getClass(); org.bukkit.enchantments.Enchantment.stopAcceptingRegistrations(); diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaMusicInstrument.java b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaMusicInstrument.java index 35a3610bb2..7e0c7c992f 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaMusicInstrument.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaMusicInstrument.java @@ -6,6 +6,7 @@ import net.minecraft.nbt.NBTTagCompound; import org.bukkit.Material; import org.bukkit.MusicInstrument; import org.bukkit.NamespacedKey; +import org.bukkit.Registry; import org.bukkit.configuration.serialization.DelegateDeserialization; import org.bukkit.inventory.meta.MusicInstrumentMeta; @@ -29,7 +30,7 @@ public class CraftMetaMusicInstrument extends CraftMetaItem implements MusicInst if (tag.contains(GOAT_HORN_INSTRUMENT.NBT)) { String string = tag.getString(GOAT_HORN_INSTRUMENT.NBT); - this.instrument = MusicInstrument.getByKey(NamespacedKey.fromString(string)); + this.instrument = Registry.INSTRUMENT.get(NamespacedKey.fromString(string)); } } @@ -38,7 +39,7 @@ public class CraftMetaMusicInstrument extends CraftMetaItem implements MusicInst String instrumentString = SerializableMeta.getString(map, GOAT_HORN_INSTRUMENT.BUKKIT, true); if (instrumentString != null) { - this.instrument = MusicInstrument.getByKey(NamespacedKey.fromString(instrumentString)); + this.instrument = Registry.INSTRUMENT.get(NamespacedKey.fromString(instrumentString)); } } diff --git a/paper-server/src/test/java/org/bukkit/support/AbstractTestingBase.java b/paper-server/src/test/java/org/bukkit/support/AbstractTestingBase.java index 212a348a27..7db12678a1 100644 --- a/paper-server/src/test/java/org/bukkit/support/AbstractTestingBase.java +++ b/paper-server/src/test/java/org/bukkit/support/AbstractTestingBase.java @@ -22,6 +22,7 @@ import net.minecraft.server.packs.resources.ResourceManager; import net.minecraft.world.flag.FeatureFlags; import net.minecraft.world.level.biome.BiomeBase; import org.bukkit.Material; +import org.bukkit.craftbukkit.CraftRegistry; import org.bukkit.craftbukkit.util.CraftMagicNumbers; import org.junit.Assert; @@ -63,6 +64,8 @@ public abstract class AbstractTestingBase { DummyServer.setup(); DummyEnchantments.setup(); + CraftRegistry.setMinecraftRegistry(REGISTRY_CUSTOM); + ImmutableList.Builder builder = ImmutableList.builder(); for (Material m : Material.values()) { if (m.isLegacy() || CraftMagicNumbers.getItem(m) == null) {