#895: Load GameEvent and MusicInstrument from registry

By: DerFrZocker <derrieple@gmail.com>
This commit is contained in:
Bukkit/Spigot 2023-09-02 12:55:16 +10:00
parent 641a55cbc5
commit 32643feee7
3 changed files with 40 additions and 64 deletions

View File

@ -1,19 +1,17 @@
package org.bukkit; package org.bukkit;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
/** /**
* Represents a generic Mojang game event. * Represents a generic Mojang game event.
*/ */
public final class GameEvent implements Keyed { public abstract class GameEvent implements Keyed {
private static final Map<NamespacedKey, GameEvent> GAME_EVENTS = new HashMap<>();
//
public static final GameEvent BLOCK_ACTIVATE = getEvent("block_activate"); public static final GameEvent BLOCK_ACTIVATE = getEvent("block_activate");
public static final GameEvent BLOCK_ATTACH = getEvent("block_attach"); public static final GameEvent BLOCK_ATTACH = getEvent("block_attach");
public static final GameEvent BLOCK_CHANGE = getEvent("block_change"); public static final GameEvent BLOCK_CHANGE = getEvent("block_change");
@ -110,43 +108,39 @@ public final class GameEvent implements Keyed {
public static final GameEvent RESONATE_13 = getEvent("resonate_13"); public static final GameEvent RESONATE_13 = getEvent("resonate_13");
public static final GameEvent RESONATE_14 = getEvent("resonate_14"); public static final GameEvent RESONATE_14 = getEvent("resonate_14");
public static final GameEvent RESONATE_15 = getEvent("resonate_15"); public static final GameEvent RESONATE_15 = getEvent("resonate_15");
//
private final NamespacedKey key;
private GameEvent(NamespacedKey key) {
this.key = key;
GAME_EVENTS.put(key, this);
}
@NotNull
@Override
public NamespacedKey getKey() {
return key;
}
/** /**
* Returns a {@link GameEvent} by a {@link NamespacedKey}. * Returns a {@link GameEvent} by a {@link NamespacedKey}.
* *
* @param namespacedKey the key * @param namespacedKey the key
* @return the event or null * @return the event or null
* @deprecated Use {@link Registry#get(NamespacedKey)} instead.
*/ */
@Nullable @Nullable
@Deprecated
public static GameEvent getByKey(@NotNull NamespacedKey namespacedKey) { public static GameEvent getByKey(@NotNull NamespacedKey namespacedKey) {
return GAME_EVENTS.get(namespacedKey); return Registry.GAME_EVENT.get(namespacedKey);
} }
/** /**
* Returns the set of all GameEvents. * Returns the set of all GameEvents.
* *
* @return the memoryKeys * @return the memoryKeys
* @deprecated use {@link Registry#iterator()}.
*/ */
@NotNull @NotNull
@Deprecated
public static Collection<GameEvent> values() { public static Collection<GameEvent> values() {
return Collections.unmodifiableCollection(GAME_EVENTS.values()); return Collections.unmodifiableCollection(Lists.newArrayList(Registry.GAME_EVENT));
} }
private static GameEvent getEvent(String vanilla) { @NotNull
return new GameEvent(NamespacedKey.minecraft(vanilla)); private static GameEvent getEvent(@NotNull String key) {
NamespacedKey namespacedKey = NamespacedKey.minecraft(key);
GameEvent gameEvent = Registry.GAME_EVENT.get(namespacedKey);
Preconditions.checkNotNull(gameEvent, "No GameEvent found for %s. This is a bug.", namespacedKey);
return gameEvent;
} }
} }

View File

@ -1,17 +1,14 @@
package org.bukkit; package org.bukkit;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
public final class MusicInstrument implements Keyed { public abstract class MusicInstrument implements Keyed {
private static final Map<NamespacedKey, MusicInstrument> INSTRUMENTS = new HashMap<>();
//
public static final MusicInstrument PONDER = getInstrument("ponder_goat_horn"); public static final MusicInstrument PONDER = getInstrument("ponder_goat_horn");
public static final MusicInstrument SING = getInstrument("sing_goat_horn"); public static final MusicInstrument SING = getInstrument("sing_goat_horn");
public static final MusicInstrument SEEK = getInstrument("seek_goat_horn"); public static final MusicInstrument SEEK = getInstrument("seek_goat_horn");
@ -20,47 +17,39 @@ public final class MusicInstrument implements Keyed {
public static final MusicInstrument CALL = getInstrument("call_goat_horn"); public static final MusicInstrument CALL = getInstrument("call_goat_horn");
public static final MusicInstrument YEARN = getInstrument("yearn_goat_horn"); public static final MusicInstrument YEARN = getInstrument("yearn_goat_horn");
public static final MusicInstrument DREAM = getInstrument("dream_goat_horn"); public static final MusicInstrument DREAM = getInstrument("dream_goat_horn");
//
private final NamespacedKey key;
private MusicInstrument(NamespacedKey key) {
this.key = key;
INSTRUMENTS.put(key, this);
}
@NotNull
@Override
public NamespacedKey getKey() {
return key;
}
/** /**
* Returns a {@link MusicInstrument} by a {@link NamespacedKey}. * Returns a {@link MusicInstrument} by a {@link NamespacedKey}.
* *
* @param namespacedKey the key * @param namespacedKey the key
* @return the event or null * @return the event or null
* @deprecated Use {@link Registry#get(NamespacedKey)} instead.
*/ */
@Nullable @Nullable
@Deprecated
public static MusicInstrument getByKey(@NotNull NamespacedKey namespacedKey) { public static MusicInstrument getByKey(@NotNull NamespacedKey namespacedKey) {
Preconditions.checkArgument(namespacedKey != null, "NamespacedKey cannot be null"); return Registry.INSTRUMENT.get(namespacedKey);
return INSTRUMENTS.get(namespacedKey);
} }
/** /**
* Returns all known MusicInstruments. * Returns all known MusicInstruments.
* *
* @return the memoryKeys * @return the memoryKeys
* @deprecated use {@link Registry#iterator()}.
*/ */
@NotNull @NotNull
@Deprecated
public static Collection<MusicInstrument> values() { public static Collection<MusicInstrument> values() {
return Collections.unmodifiableCollection(INSTRUMENTS.values()); return Collections.unmodifiableCollection(Lists.newArrayList(Registry.INSTRUMENT));
} }
private static MusicInstrument getInstrument(@NotNull String name) { @NotNull
Preconditions.checkArgument(name != null, "Instrument name cannot be null"); private static MusicInstrument getInstrument(@NotNull String key) {
NamespacedKey namespacedKey = NamespacedKey.minecraft(key);
MusicInstrument instrument = Registry.INSTRUMENT.get(namespacedKey);
return new MusicInstrument(NamespacedKey.minecraft(name)); Preconditions.checkNotNull(instrument, "No MusicInstrument found for %s. This is a bug.", namespacedKey);
return instrument;
} }
} }

View File

@ -6,6 +6,7 @@ import com.google.common.collect.ImmutableMap;
import java.util.Arrays; import java.util.Arrays;
import java.util.Iterator; import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.function.Predicate; import java.util.function.Predicate;
import org.bukkit.advancement.Advancement; import org.bukkit.advancement.Advancement;
import org.bukkit.attribute.Attribute; import org.bukkit.attribute.Attribute;
@ -116,6 +117,12 @@ public interface Registry<T extends Keyed> extends Iterable<T> {
* @see EntityType * @see EntityType
*/ */
Registry<EntityType> ENTITY_TYPE = new SimpleRegistry<>(EntityType.class, (entity) -> entity != EntityType.UNKNOWN); Registry<EntityType> ENTITY_TYPE = new SimpleRegistry<>(EntityType.class, (entity) -> entity != EntityType.UNKNOWN);
/**
* Server instruments.
*
* @see MusicInstrument
*/
Registry<MusicInstrument> INSTRUMENT = Objects.requireNonNull(Bukkit.getRegistry(MusicInstrument.class), "No registry present for MusicInstrument. This is a bug.");
/** /**
* Default server loot tables. * Default server loot tables.
* *
@ -214,21 +221,7 @@ public interface Registry<T extends Keyed> extends Iterable<T> {
* *
* @see GameEvent * @see GameEvent
*/ */
Registry<GameEvent> GAME_EVENT = new Registry<GameEvent>() { Registry<GameEvent> GAME_EVENT = Objects.requireNonNull(Bukkit.getRegistry(GameEvent.class), "No registry present for GameEvent. This is a bug.");
@NotNull
@Override
public Iterator iterator() {
return GameEvent.values().iterator();
}
@Nullable
@Override
public GameEvent get(@NotNull NamespacedKey key) {
return GameEvent.getByKey(key);
}
};
/** /**
* Get the object by its key. * Get the object by its key.
* *