diff --git a/src/main/java/com/paneedah/mwc/network/TypeRegistry.java b/src/main/java/com/paneedah/mwc/network/TypeRegistry.java index 86469f9d4..dec94e243 100644 --- a/src/main/java/com/paneedah/mwc/network/TypeRegistry.java +++ b/src/main/java/com/paneedah/mwc/network/TypeRegistry.java @@ -1,14 +1,27 @@ package com.paneedah.mwc.network; +import com.paneedah.weaponlib.*; +import com.paneedah.weaponlib.electronics.HandheldState; +import com.paneedah.weaponlib.electronics.PlayerHandheldInstance; +import com.paneedah.weaponlib.electronics.PlayerTabletInstance; +import com.paneedah.weaponlib.electronics.TabletState; +import com.paneedah.weaponlib.grenade.GrenadeState; +import com.paneedah.weaponlib.grenade.PlayerGrenadeInstance; +import com.paneedah.weaponlib.melee.MeleeAttachmentAspect; +import com.paneedah.weaponlib.melee.MeleeState; +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 @@ -16,34 +29,57 @@ public final class TypeRegistry { @Getter private static final TypeRegistry INSTANCE = new TypeRegistry(); - private final HashMap> typeRegistry = new HashMap<>(); - - public void register(Class cls) { - typeRegistry.put(getUuid(cls), cls); + private 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); } - public UUID getUuid(final Class aClass) { - try { - final SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG"); - secureRandom.setSeed(aClass.getName().getBytes()); - return new UUID(secureRandom.nextLong(), secureRandom.nextLong()); - } catch (NoSuchAlgorithmException noSuchAlgorithmException) { - RED_LOGGER.printFramedError("Networking", "Failed to create a secure random", "", noSuchAlgorithmException.getMessage()); - throw new RuntimeException(noSuchAlgorithmException); - } - + private void register(Class cls) { + typeRegistry.put(cls.getName(), cls); } public void toBytes(final T object, final ByteBuf byteBuf) { - final UUID uuid = getUuid(object.getClass()); + final String className = object.getClass().getName(); - if (!typeRegistry.containsKey(uuid)) { + if (!typeRegistry.containsKey(className)) { RED_LOGGER.printFramedError("Networking", "Failed to write object because its class is not registered", "", "Object: " + object, "Class: " + object.getClass()); - throw new RuntimeException(); + throw new IllegalStateException("Failed to write object because its class is not registered"); } - byteBuf.writeLong(uuid.getMostSignificantBits()); - byteBuf.writeLong(uuid.getLeastSignificantBits()); + byte[] classNameBytes = className.getBytes(StandardCharsets.UTF_8); + byteBuf.writeByte((byte) classNameBytes.length); + byteBuf.writeBytes(classNameBytes); + if (object.getClass().isEnum()) { byteBuf.writeInt(((Enum) object).ordinal()); } else { @@ -52,16 +88,22 @@ public void toBytes(final T object, final ByteBuf byte } public T fromBytes(final ByteBuf byteBuf) { - long mostSigBits = byteBuf.readLong(); - long leastSigBits = byteBuf.readLong(); - UUID typeUuid = new UUID(mostSigBits, leastSigBits); + Class targetClass; + + final byte[] classNameBytes = new byte[byteBuf.readByte()]; + byteBuf.readBytes(classNameBytes); + final String className = new String(classNameBytes, StandardCharsets.UTF_8); + if (!typeRegistry.containsKey(className)) { + RED_LOGGER.printFramedError("Networking", "Failed to deserialize object because its class is not registered", "Weapon will reset to it's default state"); + return null; + } - Class targetClass = (Class) typeRegistry.get(typeUuid); + targetClass = (Class) typeRegistry.get(className); if (targetClass == null) { - RED_LOGGER.printFramedError("Networking", "Failed to deserialize object.\nDid you forget to register type?", ""); - throw new RuntimeException(); + RED_LOGGER.printFramedError("Networking", "Failed to deserialize object", "Weapon will reset to it's default state"); + return null; } T instance; @@ -72,8 +114,8 @@ public T fromBytes(final ByteBuf byteBuf) { try { instance = targetClass.newInstance(); } catch (InstantiationException | IllegalAccessException exception) { - RED_LOGGER.printFramedError("Networking", "Failed to create instance", "", exception.getMessage(), exception.getStackTrace()[3].toString()); - throw new RuntimeException(); + RED_LOGGER.printFramedError("Networking", "Failed to create instance", "Weapon will reset to it's default state", exception.getMessage(), exception.getStackTrace()[3].toString()); + return null; } instance.read(byteBuf); diff --git a/src/main/java/com/paneedah/weaponlib/CommonModContext.java b/src/main/java/com/paneedah/weaponlib/CommonModContext.java index 6ca0911fc..5c9ae85f1 100644 --- a/src/main/java/com/paneedah/weaponlib/CommonModContext.java +++ b/src/main/java/com/paneedah/weaponlib/CommonModContext.java @@ -49,33 +49,6 @@ public class CommonModContext implements ModContext { - static { - TypeRegistry.getINSTANCE().register(LoadPermit.class); - TypeRegistry.getINSTANCE().register(MagazineState.class); - TypeRegistry.getINSTANCE().register(PlayerItemInstance.class); - TypeRegistry.getINSTANCE().register(PlayerWeaponInstance.class); - TypeRegistry.getINSTANCE().register(PlayerMagazineInstance.class); - TypeRegistry.getINSTANCE().register(PlayerWeaponInstance.class); - TypeRegistry.getINSTANCE().register(Permit.class); - TypeRegistry.getINSTANCE().register(EnterAttachmentModePermit.class); - TypeRegistry.getINSTANCE().register(ExitAttachmentModePermit.class); - TypeRegistry.getINSTANCE().register(ChangeAttachmentPermit.class); - TypeRegistry.getINSTANCE().register(CompoundPermit.class); - TypeRegistry.getINSTANCE().register(UnloadPermit.class); - TypeRegistry.getINSTANCE().register(LoadPermit.class); - TypeRegistry.getINSTANCE().register(PlayerWeaponInstance.class); - TypeRegistry.getINSTANCE().register(WeaponState.class); - TypeRegistry.getINSTANCE().register(PlayerMeleeInstance.class); - TypeRegistry.getINSTANCE().register(PlayerGrenadeInstance.class); - TypeRegistry.getINSTANCE().register(PlayerTabletInstance.class); - TypeRegistry.getINSTANCE().register(PlayerHandheldInstance.class); - TypeRegistry.getINSTANCE().register(MeleeState.class); - TypeRegistry.getINSTANCE().register(TabletState.class); - TypeRegistry.getINSTANCE().register(HandheldState.class); - TypeRegistry.getINSTANCE().register(SpreadableExposure.class); - TypeRegistry.getINSTANCE().register(LightExposure.class); - } - static class BulletImpactSoundKey { private final Material material; diff --git a/src/main/java/com/paneedah/weaponlib/MagazineReloadAspect.java b/src/main/java/com/paneedah/weaponlib/MagazineReloadAspect.java index acbcdcb58..dc088a2f8 100644 --- a/src/main/java/com/paneedah/weaponlib/MagazineReloadAspect.java +++ b/src/main/java/com/paneedah/weaponlib/MagazineReloadAspect.java @@ -20,11 +20,6 @@ public class MagazineReloadAspect implements Aspect { - static { - TypeRegistry.getINSTANCE().register(LoadPermit.class); - TypeRegistry.getINSTANCE().register(UnloadPermit.class); - } - private static final Set allowedUpdateFromStates = new HashSet<>( Arrays.asList( MagazineState.LOAD_REQUESTED, diff --git a/src/main/java/com/paneedah/weaponlib/MagazineState.java b/src/main/java/com/paneedah/weaponlib/MagazineState.java index 31b22a5d0..67fde72a2 100644 --- a/src/main/java/com/paneedah/weaponlib/MagazineState.java +++ b/src/main/java/com/paneedah/weaponlib/MagazineState.java @@ -88,10 +88,4 @@ public void read(ByteBuf byteBuf) { public void write(ByteBuf byteBuf) { // not need to write anything, parent type registry should take care of it } - - static { - TypeRegistry.getINSTANCE().register(MagazineState.class); - } - - } diff --git a/src/main/java/com/paneedah/weaponlib/PlayerItemInstance.java b/src/main/java/com/paneedah/weaponlib/PlayerItemInstance.java index 41da43c5a..e7e221f0c 100644 --- a/src/main/java/com/paneedah/weaponlib/PlayerItemInstance.java +++ b/src/main/java/com/paneedah/weaponlib/PlayerItemInstance.java @@ -19,10 +19,6 @@ @NoArgsConstructor public class PlayerItemInstance> extends UniversalObject implements ExtendedState { - static { - TypeRegistry.getINSTANCE().register(PlayerItemInstance.class); - } - protected boolean compoundMagSwapCompleted = false; @Getter @Setter protected int itemInventoryIndex; diff --git a/src/main/java/com/paneedah/weaponlib/PlayerItemInstanceRegistry.java b/src/main/java/com/paneedah/weaponlib/PlayerItemInstanceRegistry.java index 71b75912c..a97dee7de 100644 --- a/src/main/java/com/paneedah/weaponlib/PlayerItemInstanceRegistry.java +++ b/src/main/java/com/paneedah/weaponlib/PlayerItemInstanceRegistry.java @@ -151,6 +151,8 @@ private PlayerItemInstance createItemInstance(final EntityPlayer player, fina result.setItemInventoryIndex(slot); result.setPlayer(player); + + Tags.setInstance(itemStack, result); } } diff --git a/src/main/java/com/paneedah/weaponlib/PlayerMagazineInstance.java b/src/main/java/com/paneedah/weaponlib/PlayerMagazineInstance.java index ceeface0c..6bb835fab 100644 --- a/src/main/java/com/paneedah/weaponlib/PlayerMagazineInstance.java +++ b/src/main/java/com/paneedah/weaponlib/PlayerMagazineInstance.java @@ -9,10 +9,6 @@ @NoArgsConstructor public class PlayerMagazineInstance extends PlayerItemInstance { - static { - TypeRegistry.getINSTANCE().register(PlayerMagazineInstance.class); - } - // private int ammo; @Override diff --git a/src/main/java/com/paneedah/weaponlib/PlayerWeaponInstance.java b/src/main/java/com/paneedah/weaponlib/PlayerWeaponInstance.java index ad27b5dd9..ff7212bfd 100644 --- a/src/main/java/com/paneedah/weaponlib/PlayerWeaponInstance.java +++ b/src/main/java/com/paneedah/weaponlib/PlayerWeaponInstance.java @@ -40,10 +40,6 @@ public class PlayerWeaponInstance extends PlayerItemInstance implem private static final int SERIAL_VERSION = 9; - static { - TypeRegistry.getINSTANCE().register(PlayerWeaponInstance.class); - } - private static final UUID NIGHT_VISION_SOURCE_UUID = UUID.randomUUID(); private static final UUID VIGNETTE_SOURCE_UUID = UUID.randomUUID(); private static final UUID BLUR_SOURCE_UUID = UUID.randomUUID(); diff --git a/src/main/java/com/paneedah/weaponlib/WeaponAttachmentAspect.java b/src/main/java/com/paneedah/weaponlib/WeaponAttachmentAspect.java index 1f3cfa39e..4ea4302e4 100644 --- a/src/main/java/com/paneedah/weaponlib/WeaponAttachmentAspect.java +++ b/src/main/java/com/paneedah/weaponlib/WeaponAttachmentAspect.java @@ -23,12 +23,6 @@ public final class WeaponAttachmentAspect implements Aspect { - static { - TypeRegistry.getINSTANCE().register(EnterAttachmentModePermit.class); - TypeRegistry.getINSTANCE().register(ExitAttachmentModePermit.class); - TypeRegistry.getINSTANCE().register(ChangeAttachmentPermit.class); - } - private static class AttachmentLookupResult { CompatibleAttachment compatibleAttachment; int index = -1; diff --git a/src/main/java/com/paneedah/weaponlib/WeaponReloadAspect.java b/src/main/java/com/paneedah/weaponlib/WeaponReloadAspect.java index 0d47d91e3..cf74dc6ae 100644 --- a/src/main/java/com/paneedah/weaponlib/WeaponReloadAspect.java +++ b/src/main/java/com/paneedah/weaponlib/WeaponReloadAspect.java @@ -28,13 +28,6 @@ public class WeaponReloadAspect implements Aspect ALLOWED_UPDATE_FROM_STATES = new HashSet<>(Arrays.asList( WeaponState.AWAIT_FURTHER_LOAD_INSTRUCTIONS, WeaponState.COMPOUND_REQUESTED, diff --git a/src/main/java/com/paneedah/weaponlib/WeaponState.java b/src/main/java/com/paneedah/weaponlib/WeaponState.java index c71b9e0e3..1fbf4e674 100644 --- a/src/main/java/com/paneedah/weaponlib/WeaponState.java +++ b/src/main/java/com/paneedah/weaponlib/WeaponState.java @@ -118,9 +118,5 @@ public void read(ByteBuf byteBuf) { public void write(ByteBuf byteBuf) { // No need to write anything, parent type registry should take care of it } - - static { - TypeRegistry.getINSTANCE().register(WeaponState.class); - } } diff --git a/src/main/java/com/paneedah/weaponlib/electronics/HandheldState.java b/src/main/java/com/paneedah/weaponlib/electronics/HandheldState.java index 5fdec976a..418689843 100644 --- a/src/main/java/com/paneedah/weaponlib/electronics/HandheldState.java +++ b/src/main/java/com/paneedah/weaponlib/electronics/HandheldState.java @@ -94,10 +94,4 @@ public void read(ByteBuf byteBuf) { public void write(ByteBuf byteBuf) { // not need to write anything, parent type registry should take care of it } - - static { - TypeRegistry.getINSTANCE().register(HandheldState.class); - } - - } diff --git a/src/main/java/com/paneedah/weaponlib/electronics/PlayerHandheldInstance.java b/src/main/java/com/paneedah/weaponlib/electronics/PlayerHandheldInstance.java index fb5b5d3d5..704c81468 100644 --- a/src/main/java/com/paneedah/weaponlib/electronics/PlayerHandheldInstance.java +++ b/src/main/java/com/paneedah/weaponlib/electronics/PlayerHandheldInstance.java @@ -13,10 +13,6 @@ public class PlayerHandheldInstance extends PlayerItemInstance { private static final int SERIAL_VERSION = 1; - static { - TypeRegistry.getINSTANCE().register(PlayerHandheldInstance.class); - } - public PlayerHandheldInstance(int itemInventoryIndex, EntityLivingBase player, ItemStack itemStack) { super(itemInventoryIndex, player, itemStack); } diff --git a/src/main/java/com/paneedah/weaponlib/electronics/PlayerTabletInstance.java b/src/main/java/com/paneedah/weaponlib/electronics/PlayerTabletInstance.java index 677dd2e20..a33a8178e 100644 --- a/src/main/java/com/paneedah/weaponlib/electronics/PlayerTabletInstance.java +++ b/src/main/java/com/paneedah/weaponlib/electronics/PlayerTabletInstance.java @@ -17,10 +17,6 @@ public class PlayerTabletInstance extends PlayerItemInstance { private static final int SERIAL_VERSION = 1; - static { - TypeRegistry.getINSTANCE().register(PlayerTabletInstance.class); - } - private int activeWatchIndex; public PlayerTabletInstance(int itemInventoryIndex, EntityLivingBase player, ItemStack itemStack) { diff --git a/src/main/java/com/paneedah/weaponlib/electronics/TabletState.java b/src/main/java/com/paneedah/weaponlib/electronics/TabletState.java index d58dee3de..82864a873 100644 --- a/src/main/java/com/paneedah/weaponlib/electronics/TabletState.java +++ b/src/main/java/com/paneedah/weaponlib/electronics/TabletState.java @@ -94,10 +94,4 @@ public void read(ByteBuf byteBuf) { public void write(ByteBuf byteBuf) { // not need to write anything, parent type registry should take care of it } - - static { - TypeRegistry.getINSTANCE().register(TabletState.class); - } - - } diff --git a/src/main/java/com/paneedah/weaponlib/grenade/GrenadeState.java b/src/main/java/com/paneedah/weaponlib/grenade/GrenadeState.java index 3dccd9e35..90e59b36d 100644 --- a/src/main/java/com/paneedah/weaponlib/grenade/GrenadeState.java +++ b/src/main/java/com/paneedah/weaponlib/grenade/GrenadeState.java @@ -88,10 +88,4 @@ public void read(ByteBuf byteBuf) { public void write(ByteBuf byteBuf) { // not need to write anything, parent type registry should take care of it } - - static { - TypeRegistry.getINSTANCE().register(GrenadeState.class); - } - - } diff --git a/src/main/java/com/paneedah/weaponlib/grenade/PlayerGrenadeInstance.java b/src/main/java/com/paneedah/weaponlib/grenade/PlayerGrenadeInstance.java index f3edca8fc..46a46c5a9 100644 --- a/src/main/java/com/paneedah/weaponlib/grenade/PlayerGrenadeInstance.java +++ b/src/main/java/com/paneedah/weaponlib/grenade/PlayerGrenadeInstance.java @@ -19,10 +19,6 @@ public class PlayerGrenadeInstance extends PlayerItemInstance { private static final int SERIAL_VERSION = 11; - static { - TypeRegistry.getINSTANCE().register(PlayerGrenadeInstance.class); - } - private int ammo; private long activationTimestamp; diff --git a/src/main/java/com/paneedah/weaponlib/melee/MeleeAttachmentAspect.java b/src/main/java/com/paneedah/weaponlib/melee/MeleeAttachmentAspect.java index cb3f9cde2..2cda9c42d 100644 --- a/src/main/java/com/paneedah/weaponlib/melee/MeleeAttachmentAspect.java +++ b/src/main/java/com/paneedah/weaponlib/melee/MeleeAttachmentAspect.java @@ -22,12 +22,6 @@ public final class MeleeAttachmentAspect implements Aspect { - static { - TypeRegistry.getINSTANCE().register(EnterAttachmentModePermit.class); - TypeRegistry.getINSTANCE().register(ExitAttachmentModePermit.class); - TypeRegistry.getINSTANCE().register(ChangeAttachmentPermit.class); - } - private static class AttachmentLookupResult { CompatibleAttachment compatibleAttachment; int index = -1; diff --git a/src/main/java/com/paneedah/weaponlib/melee/MeleeState.java b/src/main/java/com/paneedah/weaponlib/melee/MeleeState.java index dfa81e870..2cd9a55d0 100644 --- a/src/main/java/com/paneedah/weaponlib/melee/MeleeState.java +++ b/src/main/java/com/paneedah/weaponlib/melee/MeleeState.java @@ -102,10 +102,4 @@ public void read(ByteBuf byteBuf) { public void write(ByteBuf byteBuf) { // not need to write anything, parent type registry should take care of it } - - static { - TypeRegistry.getINSTANCE().register(MeleeState.class); - } - - } diff --git a/src/main/java/com/paneedah/weaponlib/melee/PlayerMeleeInstance.java b/src/main/java/com/paneedah/weaponlib/melee/PlayerMeleeInstance.java index 54dccd148..508278d75 100644 --- a/src/main/java/com/paneedah/weaponlib/melee/PlayerMeleeInstance.java +++ b/src/main/java/com/paneedah/weaponlib/melee/PlayerMeleeInstance.java @@ -20,10 +20,6 @@ public class PlayerMeleeInstance extends PlayerItemInstance { private static final int SERIAL_VERSION = 7; - static { - TypeRegistry.getINSTANCE().register(PlayerMeleeInstance.class); - } - private int ammo; private long lastFireTimestamp; private byte activeTextureIndex; diff --git a/src/main/java/com/paneedah/weaponlib/state/Permit.java b/src/main/java/com/paneedah/weaponlib/state/Permit.java index 4a94f1e5b..01e8cfc66 100644 --- a/src/main/java/com/paneedah/weaponlib/state/Permit.java +++ b/src/main/java/com/paneedah/weaponlib/state/Permit.java @@ -6,10 +6,6 @@ public class Permit> extends UniversalObject { - static { - TypeRegistry.getINSTANCE().register(Permit.class); - } - public enum Status {REQUESTED, GRANTED, DENIED, UNKNOWN} protected S state;