Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Desoroxxx committed Oct 25, 2024
1 parent c1f0666 commit 3fc1538
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 31 deletions.
22 changes: 12 additions & 10 deletions src/main/java/com/paneedah/mwc/network/TypeRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@
import com.paneedah.weaponlib.melee.PlayerMeleeInstance;
import com.paneedah.weaponlib.state.Permit;
import io.netty.buffer.ByteBuf;
import lombok.NoArgsConstructor;

import java.lang.reflect.InvocationTargetException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;

import static com.paneedah.mwc.ProjectConstants.RED_LOGGER;
import static lombok.AccessLevel.PRIVATE;

@NoArgsConstructor(access = PRIVATE)
public final class TypeRegistry {

private static final HashMap<String, Class<? extends ISerializable>> typeRegistry = new HashMap<>();
Expand Down Expand Up @@ -59,7 +63,7 @@ private static <T extends ISerializable> void register(Class<T> cls) {
typeRegistry.put(cls.getName(), cls);
}

public static <T extends ISerializable> void toBytes(final T object, final ByteBuf byteBuf) {
public static <T extends ISerializable> void write(final ByteBuf byteBuf, final T object) {
final String className = object.getClass().getName();

if (!typeRegistry.containsKey(className)) {
Expand All @@ -78,22 +82,20 @@ public static <T extends ISerializable> void toBytes(final T object, final ByteB
}
}

public static <T extends ISerializable> T fromBytes(final ByteBuf byteBuf) {
Class<T> targetClass;

public static <T extends ISerializable> T read(final ByteBuf byteBuf) {
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");
RED_LOGGER.printFramedError("Networking", "Failed to deserialize object because its class is not registered", "Weapon will probably reset to it's default state");
return null;
}

targetClass = (Class<T>) typeRegistry.get(className);
final Class<T> targetClass = (Class<T>) typeRegistry.get(className);

if (targetClass == null) {
RED_LOGGER.printFramedError("Networking", "Failed to deserialize object", "Weapon will reset to it's default state");
RED_LOGGER.printFramedError("Networking", "Failed to deserialize object", "Weapon will probably reset to it's default state");
return null;
}

Expand All @@ -103,9 +105,9 @@ public static <T extends ISerializable> T fromBytes(final ByteBuf byteBuf) {
instance = constants[byteBuf.readInt()];
} else {
try {
instance = targetClass.newInstance();
} catch (InstantiationException | IllegalAccessException exception) {
RED_LOGGER.printFramedError("Networking", "Failed to create instance", "Weapon will reset to it's default state", exception.getMessage(), exception.getStackTrace()[3].toString());
instance = targetClass.getDeclaredConstructor().newInstance();
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException exception) {
RED_LOGGER.printFramedError("Networking", "Failed to create instance", "Weapon will probably reset to it's default state", exception.getMessage(), exception.getStackTrace()[3].toString());
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ public void fromBytes(final ByteBuf byteBuf) {
final int size = byteBuf.readInt();

for (int i = 0; i < size; i++)
exposures.add(TypeRegistry.fromBytes(byteBuf));
exposures.add(TypeRegistry.read(byteBuf));
}

@Override
public void toBytes(final ByteBuf byteBuf) {
byteBuf.writeInt(exposures.size());

for (Exposure exposure : exposures)
TypeRegistry.toBytes(exposure, byteBuf);
TypeRegistry.write(exposure, byteBuf);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ public final class GrenadeMessage implements IMessage {

@Override
public void fromBytes(final ByteBuf byteBuf) {
this.instance = TypeRegistry.fromBytes(byteBuf);
this.instance = TypeRegistry.read(byteBuf);
this.activationTimestamp = byteBuf.readLong();
}

@Override
public void toBytes(final ByteBuf byteBuf) {
TypeRegistry.toBytes(instance, byteBuf);
TypeRegistry.write(instance, byteBuf);
byteBuf.writeLong(activationTimestamp);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ public final class MeleeAttackMessage implements IMessage {

@Override
public void fromBytes(final ByteBuf byteBuf) {
instance = TypeRegistry.fromBytes(byteBuf);
instance = TypeRegistry.read(byteBuf);
entityId = byteBuf.readInt();
isHeavyAttack = byteBuf.readBoolean();
}

@Override
public void toBytes(final ByteBuf byteBuf) {
TypeRegistry.toBytes(instance, byteBuf);
TypeRegistry.write(instance, byteBuf);
byteBuf.writeInt(entityId);
byteBuf.writeBoolean(isHeavyAttack);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ public final class PermitMessage implements IMessage {

@Override
public void fromBytes(final ByteBuf byteBuf) {
playerItemInstance = TypeRegistry.fromBytes(byteBuf);
permit = TypeRegistry.fromBytes(byteBuf);
playerItemInstance = TypeRegistry.read(byteBuf);
permit = TypeRegistry.read(byteBuf);
}

@Override
public void toBytes(final ByteBuf byteBuf) {
TypeRegistry.toBytes(playerItemInstance, byteBuf);
TypeRegistry.toBytes(permit, byteBuf);
TypeRegistry.write(playerItemInstance, byteBuf);
TypeRegistry.write(permit, byteBuf);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void fromBytes(final ByteBuf byteBuf) {
removed = byteBuf.readBoolean();

if (!removed) {
spreadableExposure = TypeRegistry.fromBytes(byteBuf);
spreadableExposure = TypeRegistry.read(byteBuf);
}
}

Expand All @@ -33,7 +33,7 @@ public void toBytes(final ByteBuf byteBuf) {
byteBuf.writeBoolean(removed);

if (!removed) {
TypeRegistry.toBytes(spreadableExposure, byteBuf);
TypeRegistry.write(spreadableExposure, byteBuf);
}
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/paneedah/weaponlib/PlayerItemInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void read(ByteBuf byteBuf) {

// state = WeaponState.DRAWING;

state = TypeRegistry.fromBytes(byteBuf);
state = TypeRegistry.read(byteBuf);
}

@Override
Expand All @@ -81,7 +81,7 @@ public void write(ByteBuf byteBuf) {

byteBuf.writeLong(updateId);

TypeRegistry.toBytes(state, byteBuf);
TypeRegistry.write(state, byteBuf);
}

// ! This in the past was weirder, and I never really got how it worked,
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/paneedah/weaponlib/Tags.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.fromBytes(Unpooled.wrappedBuffer(bytes));
return TypeRegistry.read(Unpooled.wrappedBuffer(bytes));
}
return null;
}
Expand All @@ -82,7 +82,7 @@ public static <T extends PlayerItemInstance<?>> T getInstance(ItemStack itemStac
byte[] bytes = itemStack.getTagCompound().getByteArray(INSTANCE_TAG);
if (bytes != null && bytes.length > 0) {
try {
return targetClass.cast(TypeRegistry.fromBytes(Unpooled.wrappedBuffer(bytes)));
return targetClass.cast(TypeRegistry.read(Unpooled.wrappedBuffer(bytes)));
} catch (RuntimeException e) {
return null;
}
Expand All @@ -97,7 +97,7 @@ public static void setInstance(ItemStack itemStack, PlayerItemInstance<?> instan

ByteBuf buf = Unpooled.buffer();
if (instance != null) {
TypeRegistry.toBytes(instance, buf);
TypeRegistry.write(instance, buf);
NBTTagCompound tagCompound = itemStack.getTagCompound();
tagCompound.setByteArray(INSTANCE_TAG, buf.array());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public NBTBase writeNBT(Capability<ExposureContainer> capability, ExposureContai
NBTTagList exposureTags = new NBTTagList();
for (Exposure exposure : instance.getExposures().values()) {
ByteBuf buf = Unpooled.buffer();
TypeRegistry.toBytes(exposure, buf);
TypeRegistry.write(exposure, buf);
exposureTags.appendTag(new NBTTagByteArray(buf.array()));
}
tagCompound.setTag(TAG_EXPOSURES, exposureTags);
Expand All @@ -100,7 +100,7 @@ public void readNBT(Capability<ExposureContainer> 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.fromBytes(buf);
Exposure exposure = TypeRegistry.read(buf);
instance.getExposures().put(exposure.getClass(), exposure);
}
instance.setLastSyncTimestamp(tagCompound.getLong(TAG_LAST_SYNC));
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/paneedah/weaponlib/state/Permit.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ public void read(ByteBuf byteBuf) {
super.read(byteBuf);
timestamp = byteBuf.readLong();
status = Status.values()[byteBuf.readInt()];
state = TypeRegistry.fromBytes(byteBuf);
state = TypeRegistry.read(byteBuf);
}

@Override
public void write(ByteBuf byteBuf) {
super.write(byteBuf);
byteBuf.writeLong(timestamp);
byteBuf.writeInt(status.ordinal());
TypeRegistry.toBytes(state, byteBuf);
TypeRegistry.write(state, byteBuf);
}

}

0 comments on commit 3fc1538

Please sign in to comment.