From c1f0666d61596a5fc92758ad627a39a81a384ff7 Mon Sep 17 00:00:00 2001 From: "Luna Mira Lage (Desoroxxx)" Date: Fri, 25 Oct 2024 15:38:49 +0200 Subject: [PATCH] Type registry is now a singleton :3 --- .../paneedah/mwc/network/TypeRegistry.java | 77 ++++++++----------- .../mwc/network/messages/ExposureMessage.java | 4 +- .../mwc/network/messages/GrenadeMessage.java | 4 +- .../network/messages/MeleeAttackMessage.java | 4 +- .../mwc/network/messages/PermitMessage.java | 12 +-- .../messages/SpreadableExposureMessage.java | 4 +- .../weaponlib/PlayerItemInstance.java | 4 +- .../java/com/paneedah/weaponlib/Tags.java | 6 +- .../CompatibleExposureCapability.java | 4 +- .../com/paneedah/weaponlib/state/Permit.java | 4 +- 10 files changed, 55 insertions(+), 68 deletions(-) diff --git a/src/main/java/com/paneedah/mwc/network/TypeRegistry.java b/src/main/java/com/paneedah/mwc/network/TypeRegistry.java index dec94e243..70bbec0df 100644 --- a/src/main/java/com/paneedah/mwc/network/TypeRegistry.java +++ b/src/main/java/com/paneedah/mwc/network/TypeRegistry.java @@ -12,63 +12,54 @@ import com.paneedah.weaponlib.melee.PlayerMeleeInstance; import com.paneedah.weaponlib.state.Permit; import io.netty.buffer.ByteBuf; -import lombok.Getter; -import lombok.NoArgsConstructor; import java.nio.charset.StandardCharsets; -import java.security.NoSuchAlgorithmException; -import java.security.SecureRandom; import java.util.HashMap; -import java.util.UUID; -import static com.paneedah.mwc.ProjectConstants.LOGGER; import static com.paneedah.mwc.ProjectConstants.RED_LOGGER; -@NoArgsConstructor public final class TypeRegistry { - @Getter private static final TypeRegistry INSTANCE = new TypeRegistry(); - - private final HashMap> typeRegistry = new HashMap<>(); + private static final HashMap> typeRegistry = new HashMap<>(); static { - INSTANCE.register(PlayerItemInstance.class); - INSTANCE.register(PlayerWeaponInstance.class); - INSTANCE.register(PlayerMagazineInstance.class); - INSTANCE.register(PlayerMeleeInstance.class); - INSTANCE.register(PlayerGrenadeInstance.class); - INSTANCE.register(PlayerHandheldInstance.class); - INSTANCE.register(PlayerTabletInstance.class); - - INSTANCE.register(WeaponState.class); - INSTANCE.register(MagazineState.class); - INSTANCE.register(MeleeState.class); - INSTANCE.register(GrenadeState.class); - INSTANCE.register(HandheldState.class); - INSTANCE.register(TabletState.class); - - INSTANCE.register(Permit.class); - INSTANCE.register(WeaponAttachmentAspect.EnterAttachmentModePermit.class); - INSTANCE.register(WeaponAttachmentAspect.ExitAttachmentModePermit.class); - INSTANCE.register(WeaponAttachmentAspect.ChangeAttachmentPermit.class); - INSTANCE.register(MeleeAttachmentAspect.EnterAttachmentModePermit.class); - INSTANCE.register(MeleeAttachmentAspect.ExitAttachmentModePermit.class); - INSTANCE.register(MeleeAttachmentAspect.ChangeAttachmentPermit.class); - INSTANCE.register(WeaponReloadAspect.LoadPermit.class); - INSTANCE.register(WeaponReloadAspect.UnloadPermit.class); - INSTANCE.register(WeaponReloadAspect.CompoundPermit.class); - INSTANCE.register(MagazineReloadAspect.LoadPermit.class); - INSTANCE.register(MagazineReloadAspect.UnloadPermit.class); - - INSTANCE.register(LightExposure.class); - INSTANCE.register(SpreadableExposure.class); + register(PlayerItemInstance.class); + register(PlayerWeaponInstance.class); + register(PlayerMagazineInstance.class); + register(PlayerMeleeInstance.class); + register(PlayerGrenadeInstance.class); + register(PlayerHandheldInstance.class); + register(PlayerTabletInstance.class); + + register(WeaponState.class); + register(MagazineState.class); + register(MeleeState.class); + register(GrenadeState.class); + register(HandheldState.class); + register(TabletState.class); + + register(Permit.class); + register(WeaponAttachmentAspect.EnterAttachmentModePermit.class); + register(WeaponAttachmentAspect.ExitAttachmentModePermit.class); + register(WeaponAttachmentAspect.ChangeAttachmentPermit.class); + register(MeleeAttachmentAspect.EnterAttachmentModePermit.class); + register(MeleeAttachmentAspect.ExitAttachmentModePermit.class); + register(MeleeAttachmentAspect.ChangeAttachmentPermit.class); + register(WeaponReloadAspect.LoadPermit.class); + register(WeaponReloadAspect.UnloadPermit.class); + register(WeaponReloadAspect.CompoundPermit.class); + register(MagazineReloadAspect.LoadPermit.class); + register(MagazineReloadAspect.UnloadPermit.class); + + register(LightExposure.class); + register(SpreadableExposure.class); } - private void register(Class cls) { + private static void register(Class cls) { typeRegistry.put(cls.getName(), cls); } - public void toBytes(final T object, final ByteBuf byteBuf) { + public static void toBytes(final T object, final ByteBuf byteBuf) { final String className = object.getClass().getName(); if (!typeRegistry.containsKey(className)) { @@ -87,7 +78,7 @@ public void toBytes(final T object, final ByteBuf byte } } - public T fromBytes(final ByteBuf byteBuf) { + public static T fromBytes(final ByteBuf byteBuf) { Class targetClass; final byte[] classNameBytes = new byte[byteBuf.readByte()]; diff --git a/src/main/java/com/paneedah/mwc/network/messages/ExposureMessage.java b/src/main/java/com/paneedah/mwc/network/messages/ExposureMessage.java index 058ed4edd..b2e6e08d4 100644 --- a/src/main/java/com/paneedah/mwc/network/messages/ExposureMessage.java +++ b/src/main/java/com/paneedah/mwc/network/messages/ExposureMessage.java @@ -23,7 +23,7 @@ public void fromBytes(final ByteBuf byteBuf) { final int size = byteBuf.readInt(); for (int i = 0; i < size; i++) - exposures.add(TypeRegistry.getINSTANCE().fromBytes(byteBuf)); + exposures.add(TypeRegistry.fromBytes(byteBuf)); } @Override @@ -31,6 +31,6 @@ public void toBytes(final ByteBuf byteBuf) { byteBuf.writeInt(exposures.size()); for (Exposure exposure : exposures) - TypeRegistry.getINSTANCE().toBytes(exposure, byteBuf); + TypeRegistry.toBytes(exposure, byteBuf); } } diff --git a/src/main/java/com/paneedah/mwc/network/messages/GrenadeMessage.java b/src/main/java/com/paneedah/mwc/network/messages/GrenadeMessage.java index 0eeb70009..f1caff0e5 100644 --- a/src/main/java/com/paneedah/mwc/network/messages/GrenadeMessage.java +++ b/src/main/java/com/paneedah/mwc/network/messages/GrenadeMessage.java @@ -18,13 +18,13 @@ public final class GrenadeMessage implements IMessage { @Override public void fromBytes(final ByteBuf byteBuf) { - this.instance = TypeRegistry.getINSTANCE().fromBytes(byteBuf); + this.instance = TypeRegistry.fromBytes(byteBuf); this.activationTimestamp = byteBuf.readLong(); } @Override public void toBytes(final ByteBuf byteBuf) { - TypeRegistry.getINSTANCE().toBytes(instance, byteBuf); + TypeRegistry.toBytes(instance, byteBuf); byteBuf.writeLong(activationTimestamp); } } diff --git a/src/main/java/com/paneedah/mwc/network/messages/MeleeAttackMessage.java b/src/main/java/com/paneedah/mwc/network/messages/MeleeAttackMessage.java index ff308d966..45ee3e0a5 100644 --- a/src/main/java/com/paneedah/mwc/network/messages/MeleeAttackMessage.java +++ b/src/main/java/com/paneedah/mwc/network/messages/MeleeAttackMessage.java @@ -19,14 +19,14 @@ public final class MeleeAttackMessage implements IMessage { @Override public void fromBytes(final ByteBuf byteBuf) { - instance = TypeRegistry.getINSTANCE().fromBytes(byteBuf); + instance = TypeRegistry.fromBytes(byteBuf); entityId = byteBuf.readInt(); isHeavyAttack = byteBuf.readBoolean(); } @Override public void toBytes(final ByteBuf byteBuf) { - TypeRegistry.getINSTANCE().toBytes(instance, byteBuf); + TypeRegistry.toBytes(instance, byteBuf); byteBuf.writeInt(entityId); byteBuf.writeBoolean(isHeavyAttack); } diff --git a/src/main/java/com/paneedah/mwc/network/messages/PermitMessage.java b/src/main/java/com/paneedah/mwc/network/messages/PermitMessage.java index 3d5791e50..dfaf2529a 100644 --- a/src/main/java/com/paneedah/mwc/network/messages/PermitMessage.java +++ b/src/main/java/com/paneedah/mwc/network/messages/PermitMessage.java @@ -19,17 +19,13 @@ public final class PermitMessage implements IMessage { @Override public void fromBytes(final ByteBuf byteBuf) { - final TypeRegistry typeRegistry = TypeRegistry.getINSTANCE(); - - playerItemInstance = typeRegistry.fromBytes(byteBuf); - permit = typeRegistry.fromBytes(byteBuf); + playerItemInstance = TypeRegistry.fromBytes(byteBuf); + permit = TypeRegistry.fromBytes(byteBuf); } @Override public void toBytes(final ByteBuf byteBuf) { - final TypeRegistry typeRegistry = TypeRegistry.getINSTANCE(); - - typeRegistry.toBytes(playerItemInstance, byteBuf); - typeRegistry.toBytes(permit, byteBuf); + TypeRegistry.toBytes(playerItemInstance, byteBuf); + TypeRegistry.toBytes(permit, byteBuf); } } diff --git a/src/main/java/com/paneedah/mwc/network/messages/SpreadableExposureMessage.java b/src/main/java/com/paneedah/mwc/network/messages/SpreadableExposureMessage.java index ea683bb48..737214221 100644 --- a/src/main/java/com/paneedah/mwc/network/messages/SpreadableExposureMessage.java +++ b/src/main/java/com/paneedah/mwc/network/messages/SpreadableExposureMessage.java @@ -24,7 +24,7 @@ public void fromBytes(final ByteBuf byteBuf) { removed = byteBuf.readBoolean(); if (!removed) { - spreadableExposure = TypeRegistry.getINSTANCE().fromBytes(byteBuf); + spreadableExposure = TypeRegistry.fromBytes(byteBuf); } } @@ -33,7 +33,7 @@ public void toBytes(final ByteBuf byteBuf) { byteBuf.writeBoolean(removed); if (!removed) { - TypeRegistry.getINSTANCE().toBytes(spreadableExposure, byteBuf); + TypeRegistry.toBytes(spreadableExposure, byteBuf); } } } diff --git a/src/main/java/com/paneedah/weaponlib/PlayerItemInstance.java b/src/main/java/com/paneedah/weaponlib/PlayerItemInstance.java index e7e221f0c..98b472f13 100644 --- a/src/main/java/com/paneedah/weaponlib/PlayerItemInstance.java +++ b/src/main/java/com/paneedah/weaponlib/PlayerItemInstance.java @@ -69,7 +69,7 @@ public void read(ByteBuf byteBuf) { // state = WeaponState.DRAWING; - state = TypeRegistry.getINSTANCE().fromBytes(byteBuf); + state = TypeRegistry.fromBytes(byteBuf); } @Override @@ -81,7 +81,7 @@ public void write(ByteBuf byteBuf) { byteBuf.writeLong(updateId); - TypeRegistry.getINSTANCE().toBytes(state, byteBuf); + TypeRegistry.toBytes(state, byteBuf); } // ! This in the past was weirder, and I never really got how it worked, diff --git a/src/main/java/com/paneedah/weaponlib/Tags.java b/src/main/java/com/paneedah/weaponlib/Tags.java index 228cfd6b5..6cd58ef42 100644 --- a/src/main/java/com/paneedah/weaponlib/Tags.java +++ b/src/main/java/com/paneedah/weaponlib/Tags.java @@ -69,7 +69,7 @@ public static PlayerItemInstance getInstance(ItemStack itemStack) { byte[] bytes = itemStack.getTagCompound().getByteArray(INSTANCE_TAG); if (bytes != null && bytes.length > 0) { - return TypeRegistry.getINSTANCE().fromBytes(Unpooled.wrappedBuffer(bytes)); + return TypeRegistry.fromBytes(Unpooled.wrappedBuffer(bytes)); } return null; } @@ -82,7 +82,7 @@ public static > T getInstance(ItemStack itemStac byte[] bytes = itemStack.getTagCompound().getByteArray(INSTANCE_TAG); if (bytes != null && bytes.length > 0) { try { - return targetClass.cast(TypeRegistry.getINSTANCE().fromBytes(Unpooled.wrappedBuffer(bytes))); + return targetClass.cast(TypeRegistry.fromBytes(Unpooled.wrappedBuffer(bytes))); } catch (RuntimeException e) { return null; } @@ -97,7 +97,7 @@ public static void setInstance(ItemStack itemStack, PlayerItemInstance instan ByteBuf buf = Unpooled.buffer(); if (instance != null) { - TypeRegistry.getINSTANCE().toBytes(instance, buf); + TypeRegistry.toBytes(instance, buf); NBTTagCompound tagCompound = itemStack.getTagCompound(); tagCompound.setByteArray(INSTANCE_TAG, buf.array()); diff --git a/src/main/java/com/paneedah/weaponlib/compatibility/CompatibleExposureCapability.java b/src/main/java/com/paneedah/weaponlib/compatibility/CompatibleExposureCapability.java index ebb2b51b5..0f046a52a 100644 --- a/src/main/java/com/paneedah/weaponlib/compatibility/CompatibleExposureCapability.java +++ b/src/main/java/com/paneedah/weaponlib/compatibility/CompatibleExposureCapability.java @@ -83,7 +83,7 @@ public NBTBase writeNBT(Capability capability, ExposureContai NBTTagList exposureTags = new NBTTagList(); for (Exposure exposure : instance.getExposures().values()) { ByteBuf buf = Unpooled.buffer(); - TypeRegistry.getINSTANCE().toBytes(exposure, buf); + TypeRegistry.toBytes(exposure, buf); exposureTags.appendTag(new NBTTagByteArray(buf.array())); } tagCompound.setTag(TAG_EXPOSURES, exposureTags); @@ -100,7 +100,7 @@ public void readNBT(Capability capability, ExposureContainer for (int i = 0; i < exposureTags.tagCount(); i++) { NBTTagByteArray byteArray = (NBTTagByteArray) exposureTags.get(i); ByteBuf buf = Unpooled.wrappedBuffer(byteArray.getByteArray()); - Exposure exposure = TypeRegistry.getINSTANCE().fromBytes(buf); + Exposure exposure = TypeRegistry.fromBytes(buf); instance.getExposures().put(exposure.getClass(), exposure); } instance.setLastSyncTimestamp(tagCompound.getLong(TAG_LAST_SYNC)); diff --git a/src/main/java/com/paneedah/weaponlib/state/Permit.java b/src/main/java/com/paneedah/weaponlib/state/Permit.java index 01e8cfc66..83f65859e 100644 --- a/src/main/java/com/paneedah/weaponlib/state/Permit.java +++ b/src/main/java/com/paneedah/weaponlib/state/Permit.java @@ -43,7 +43,7 @@ public void read(ByteBuf byteBuf) { super.read(byteBuf); timestamp = byteBuf.readLong(); status = Status.values()[byteBuf.readInt()]; - state = TypeRegistry.getINSTANCE().fromBytes(byteBuf); + state = TypeRegistry.fromBytes(byteBuf); } @Override @@ -51,7 +51,7 @@ public void write(ByteBuf byteBuf) { super.write(byteBuf); byteBuf.writeLong(timestamp); byteBuf.writeInt(status.ordinal()); - TypeRegistry.getINSTANCE().toBytes(state, byteBuf); + TypeRegistry.toBytes(state, byteBuf); } }