chore: minor tweaks

This commit is contained in:
mworzala 2024-05-23 17:01:55 -04:00 committed by Matt Worzala
parent 4674a42964
commit f2665be443
12 changed files with 54 additions and 21 deletions

View File

@ -1,7 +1,6 @@
package net.minestom.server.entity.damage;
import net.minestom.server.registry.DynamicRegistry;
import net.minestom.server.registry.DynamicRegistryImpl;
import net.minestom.server.registry.ProtocolObject;
import net.minestom.server.registry.Registry;
import net.minestom.server.utils.NamespaceID;
@ -35,7 +34,7 @@ public sealed interface DamageType extends ProtocolObject, DamageTypes permits D
*/
@ApiStatus.Internal
static @NotNull DynamicRegistry<DamageType> createDefaultRegistry() {
return new DynamicRegistryImpl<>(
return DynamicRegistry.create(
"minecraft:damage_type", DamageTypeImpl.REGISTRY_NBT_TYPE, Registry.Resource.DAMAGE_TYPES,
(namespace, props) -> new DamageTypeImpl(Registry.damageType(namespace, props))
);

View File

@ -6,7 +6,10 @@ import net.kyori.adventure.nbt.StringBinaryTag;
import net.minestom.server.entity.Entity;
import net.minestom.server.entity.Metadata;
import net.minestom.server.network.NetworkBuffer;
import net.minestom.server.registry.*;
import net.minestom.server.registry.DynamicRegistry;
import net.minestom.server.registry.ProtocolObject;
import net.minestom.server.registry.Registries;
import net.minestom.server.registry.Registry;
import net.minestom.server.utils.NamespaceID;
import net.minestom.server.utils.nbt.BinaryTagSerializer;
import net.minestom.server.utils.validate.Check;
@ -87,7 +90,7 @@ public class WolfMeta extends TameableAnimalMeta {
*/
@ApiStatus.Internal
static @NotNull DynamicRegistry<Variant> createDefaultRegistry() {
return new DynamicRegistryImpl<>(
return DynamicRegistry.create(
"minecraft:wolf_variant", VariantImpl.REGISTRY_NBT_TYPE, Registry.Resource.WOLF_VARIANTS,
(namespace, props) -> new WolfMeta.VariantImpl(Registry.wolfVariant(namespace, props))
);

View File

@ -1,7 +1,10 @@
package net.minestom.server.instance.block.banner;
import net.minestom.server.network.NetworkBuffer;
import net.minestom.server.registry.*;
import net.minestom.server.registry.DynamicRegistry;
import net.minestom.server.registry.ProtocolObject;
import net.minestom.server.registry.Registries;
import net.minestom.server.registry.Registry;
import net.minestom.server.utils.NamespaceID;
import net.minestom.server.utils.nbt.BinaryTagSerializer;
import org.jetbrains.annotations.ApiStatus;
@ -36,7 +39,7 @@ public sealed interface BannerPattern extends ProtocolObject, BannerPatterns per
*/
@ApiStatus.Internal
static @NotNull DynamicRegistry<BannerPattern> createDefaultRegistry() {
return new DynamicRegistryImpl<>(
return DynamicRegistry.create(
"minecraft:banner_pattern", BannerPatternImpl.REGISTRY_NBT_TYPE, Registry.Resource.BANNER_PATTERNS,
(namespace, props) -> new BannerPatternImpl(Registry.bannerPattern(namespace, props))
);

View File

@ -3,7 +3,10 @@ package net.minestom.server.item.armor;
import net.kyori.adventure.text.Component;
import net.minestom.server.item.Material;
import net.minestom.server.network.NetworkBuffer;
import net.minestom.server.registry.*;
import net.minestom.server.registry.DynamicRegistry;
import net.minestom.server.registry.ProtocolObject;
import net.minestom.server.registry.Registries;
import net.minestom.server.registry.Registry;
import net.minestom.server.utils.NamespaceID;
import net.minestom.server.utils.nbt.BinaryTagSerializer;
import org.jetbrains.annotations.ApiStatus;
@ -47,7 +50,7 @@ public sealed interface TrimMaterial extends ProtocolObject permits TrimMaterial
*/
@ApiStatus.Internal
static @NotNull DynamicRegistry<TrimMaterial> createDefaultRegistry() {
return new DynamicRegistryImpl<>(
return DynamicRegistry.create(
"minecraft:trim_material", TrimMaterialImpl.REGISTRY_NBT_TYPE, Registry.Resource.TRIM_MATERIALS,
(namespace, props) -> new TrimMaterialImpl(Registry.trimMaterial(namespace, props))
);

View File

@ -3,7 +3,10 @@ package net.minestom.server.item.armor;
import net.kyori.adventure.text.Component;
import net.minestom.server.item.Material;
import net.minestom.server.network.NetworkBuffer;
import net.minestom.server.registry.*;
import net.minestom.server.registry.DynamicRegistry;
import net.minestom.server.registry.ProtocolObject;
import net.minestom.server.registry.Registries;
import net.minestom.server.registry.Registry;
import net.minestom.server.utils.NamespaceID;
import net.minestom.server.utils.nbt.BinaryTagSerializer;
import org.jetbrains.annotations.ApiStatus;
@ -40,7 +43,7 @@ public sealed interface TrimPattern extends ProtocolObject permits TrimPatternIm
*/
@ApiStatus.Internal
static @NotNull DynamicRegistry<TrimPattern> createDefaultRegistry() {
return new DynamicRegistryImpl<>(
return DynamicRegistry.create(
"minecraft:trim_pattern", TrimPatternImpl.REGISTRY_NBT_TYPE, Registry.Resource.TRIM_PATTERNS,
(namespace, props) -> new TrimPatternImpl(Registry.trimPattern(namespace, props))
);

View File

@ -1,7 +1,6 @@
package net.minestom.server.message;
import net.minestom.server.registry.DynamicRegistry;
import net.minestom.server.registry.DynamicRegistryImpl;
import net.minestom.server.registry.ProtocolObject;
import net.minestom.server.registry.Registry;
import net.minestom.server.utils.NamespaceID;
@ -34,7 +33,7 @@ public sealed interface ChatType extends ProtocolObject, ChatTypes permits ChatT
*/
@ApiStatus.Internal
static @NotNull DynamicRegistry<ChatType> createDefaultRegistry() {
return new DynamicRegistryImpl<>(
return DynamicRegistry.create(
"minecraft:chat_type", ChatTypeImpl.REGISTRY_NBT_TYPE, Registry.Resource.CHAT_TYPES,
(namespace, props) -> new ChatTypeImpl(Registry.chatType(namespace, props))
);

View File

@ -15,6 +15,7 @@ public record ClientSelectKnownPacksPacket(
public ClientSelectKnownPacksPacket {
Check.argCondition(entries.size() > MAX_ENTRIES, "Too many known packs: {0} > {1}", entries.size(), MAX_ENTRIES);
entries = List.copyOf(entries);
}
public ClientSelectKnownPacksPacket(@NotNull NetworkBuffer reader) {

View File

@ -18,6 +18,7 @@ public record SelectKnownPacksPacket(
public SelectKnownPacksPacket {
Check.argCondition(entries.size() > MAX_ENTRIES, "Too many known packs: {0} > {1}", entries.size(), MAX_ENTRIES);
entries = List.copyOf(entries);
}
public SelectKnownPacksPacket(@NotNull NetworkBuffer reader) {

View File

@ -4,11 +4,13 @@ import net.kyori.adventure.key.Keyed;
import net.minestom.server.entity.Player;
import net.minestom.server.network.packet.server.SendablePacket;
import net.minestom.server.utils.NamespaceID;
import net.minestom.server.utils.nbt.BinaryTagSerializer;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Comparator;
import java.util.List;
public sealed interface DynamicRegistry<T extends ProtocolObject> permits DynamicRegistryImpl {
@ -43,6 +45,27 @@ public sealed interface DynamicRegistry<T extends ProtocolObject> permits Dynami
}
}
@ApiStatus.Internal
static <T extends ProtocolObject> @NotNull DynamicRegistry<T> create(
@NotNull String id, @NotNull BinaryTagSerializer<T> nbtType) {
return new DynamicRegistryImpl<>(id, nbtType);
}
@ApiStatus.Internal
static <T extends ProtocolObject> @NotNull DynamicRegistry<T> create(
@NotNull String id, @NotNull BinaryTagSerializer<T> nbtType,
@NotNull Registry.Resource resource, @NotNull Registry.Container.Loader<T> loader) {
return new DynamicRegistryImpl<>(id, nbtType, resource, loader);
}
@ApiStatus.Internal
static <T extends ProtocolObject> @NotNull DynamicRegistry<T> create(
@NotNull String id, @NotNull BinaryTagSerializer<T> nbtType,
@NotNull Registry.Resource resource, @NotNull Registry.Container.Loader<T> loader,
@Nullable Comparator<String> idComparator) {
return new DynamicRegistryImpl<>(id, nbtType, resource, loader, idComparator);
}
@Nullable T get(int id);
@Nullable T get(@NotNull NamespaceID namespace);
default @Nullable T get(@NotNull Key<T> key) {

View File

@ -17,7 +17,7 @@ import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.locks.ReentrantLock;
@ApiStatus.Internal
public final class DynamicRegistryImpl<T extends ProtocolObject> implements DynamicRegistry<T> {
final class DynamicRegistryImpl<T extends ProtocolObject> implements DynamicRegistry<T> {
private static final UnsupportedOperationException UNSAFE_REMOVE_EXCEPTION = new UnsupportedOperationException("Unsafe remove is disabled. Enable by setting the system property 'minestom.registry.unsafe-remove' to 'true'");
record KeyImpl<T extends ProtocolObject>(NamespaceID namespace) implements Key<T> {
@ -44,23 +44,23 @@ public final class DynamicRegistryImpl<T extends ProtocolObject> implements Dyna
private final CachedPacket vanillaRegistryDataPacket = new CachedPacket(() -> createRegistryDataPacket(true));
private final ReentrantLock lock = new ReentrantLock(); // Protects writes
private final List<T> entryById = new CopyOnWriteArrayList<>(); // We use a CopyOnWriteArrayList even with the lock above because it handles concurrent iteration
private final List<T> entryById = new CopyOnWriteArrayList<>();
private final Map<NamespaceID, T> entryByName = new ConcurrentHashMap<>();
private final List<NamespaceID> idByName = new CopyOnWriteArrayList<>();
private final String id;
private final BinaryTagSerializer<T> nbtType;
public DynamicRegistryImpl(@NotNull String id, BinaryTagSerializer<T> nbtType) {
DynamicRegistryImpl(@NotNull String id, BinaryTagSerializer<T> nbtType) {
this.id = id;
this.nbtType = nbtType;
}
public DynamicRegistryImpl(@NotNull String id, BinaryTagSerializer<T> nbtType, @NotNull Registry.Resource resource, @NotNull Registry.Container.Loader<T> loader) {
DynamicRegistryImpl(@NotNull String id, BinaryTagSerializer<T> nbtType, @NotNull Registry.Resource resource, @NotNull Registry.Container.Loader<T> loader) {
this(id, nbtType, resource, loader, null);
}
public DynamicRegistryImpl(@NotNull String id, BinaryTagSerializer<T> nbtType, @NotNull Registry.Resource resource, @NotNull Registry.Container.Loader<T> loader, @Nullable Comparator<String> idComparator) {
DynamicRegistryImpl(@NotNull String id, BinaryTagSerializer<T> nbtType, @NotNull Registry.Resource resource, @NotNull Registry.Container.Loader<T> loader, @Nullable Comparator<String> idComparator) {
this(id, nbtType);
loadStaticRegistry(resource, loader, idComparator);
}

View File

@ -1,7 +1,6 @@
package net.minestom.server.world;
import net.minestom.server.registry.DynamicRegistry;
import net.minestom.server.registry.DynamicRegistryImpl;
import net.minestom.server.registry.ProtocolObject;
import net.minestom.server.registry.Registry;
import net.minestom.server.utils.NamespaceID;
@ -33,7 +32,7 @@ public sealed interface DimensionType extends ProtocolObject, DimensionTypes per
*/
@ApiStatus.Internal
static @NotNull DynamicRegistry<DimensionType> createDefaultRegistry() {
return new DynamicRegistryImpl<>(
return DynamicRegistry.create(
"minecraft:dimension_type", DimensionTypeImpl.REGISTRY_NBT_TYPE, Registry.Resource.DIMENSION_TYPES,
(namespace, props) -> new DimensionTypeImpl(Registry.dimensionType(namespace, props))
);

View File

@ -2,7 +2,6 @@ package net.minestom.server.world.biome;
import net.minestom.server.coordinate.Point;
import net.minestom.server.registry.DynamicRegistry;
import net.minestom.server.registry.DynamicRegistryImpl;
import net.minestom.server.registry.ProtocolObject;
import net.minestom.server.registry.Registry;
import net.minestom.server.utils.NamespaceID;
@ -28,7 +27,7 @@ public sealed interface Biome extends Biomes, ProtocolObject permits BiomeImpl {
*/
@ApiStatus.Internal
static @NotNull DynamicRegistry<Biome> createDefaultRegistry() {
return new DynamicRegistryImpl<>(
return DynamicRegistry.create(
"minecraft:worldgen/biome", BiomeImpl.REGISTRY_NBT_TYPE, Registry.Resource.BIOMES,
(namespace, props) -> new BiomeImpl(Registry.biome(namespace, props)),
// We force plains to be first because it allows convenient palette initialization.