#1248: Load GameEvent and MusicInstrument from registry

By: DerFrZocker <derrieple@gmail.com>
This commit is contained in:
CraftBukkit/Spigot
2023-09-02 12:55:23 +10:00
parent a16df1b04c
commit c8a9b8d25c
8 changed files with 186 additions and 14 deletions

View File

@@ -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<net.minecraft.world.level.gameevent.GameEvent> 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 + "}";
}
}

View File

@@ -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<Instrument> 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 + "}";
}
}

View File

@@ -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<B extends Keyed, M> implements Registry<B> {
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 <E> IRegistry<E> getMinecraftRegistry(ResourceKey<IRegistry<E>> key) {
return getMinecraftRegistry().registryOrThrow(key);
}
public static <B extends Keyed> Registry<?> createRegistry(Class<B> 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);
}

View File

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

View File

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