Skip to content

Commit

Permalink
cleanup 4
Browse files Browse the repository at this point in the history
  • Loading branch information
strubium committed May 28, 2024
1 parent 11a65be commit 3023114
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 18 deletions.
8 changes: 0 additions & 8 deletions src/main/java/mcjty/theoneprobe/Utilities.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package mcjty.theoneprobe;


import mcjty.theoneprobe.Tools;
import mcjty.theoneprobe.api.ElementAlignment;
import mcjty.theoneprobe.api.IProbeInfo;
import mcjty.theoneprobe.api.ProbeMode;
Expand All @@ -19,12 +17,6 @@
import java.util.Set;

public class Utilities {

public static final String MODID_DYNAMIC_TREES = "dynamictrees";
public static final String MODID_APOTHEOSIS = "apotheosis";
public static final String MODID_PROJECTE = "projecte";
public static final String MODID_THAUMCRAFT = "thaumcraft";

public static final DecimalFormat FORMAT = new DecimalFormat("#,###.#");

@Nonnull
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/mcjty/theoneprobe/keys/KeyBindings.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,11 @@ public class KeyBindings {

public static KeyBinding toggleLiquids;
public static KeyBinding toggleVisible;
// public static KeyBinding generateLag;

public static void init() {
toggleLiquids = new KeyBinding("key.toggleLiquids", KeyConflictContext.IN_GAME, Keyboard.KEY_L, "key.categories.theoneprobe");
toggleVisible = new KeyBinding("key.toggleVisible", KeyConflictContext.IN_GAME, Keyboard.KEY_NONE, "key.categories.theoneprobe");
// generateLag = new KeyBinding("key.generateLag", KeyConflictContext.IN_GAME, Keyboard.KEY_U, "key.categories.theoneprobe");
ClientRegistry.registerKeyBinding(toggleLiquids);
ClientRegistry.registerKeyBinding(toggleVisible);
// ClientRegistry.registerKeyBinding(generateLag);
}
}
2 changes: 0 additions & 2 deletions src/main/java/mcjty/theoneprobe/keys/KeyInputHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ public void onKeyInput(InputEvent.KeyInputEvent event) {
if (!ConfigSetup.holdKeyToMakeVisible) {
ConfigSetup.setVisible(!ConfigSetup.isVisible);
}
// } else if (KeyBindings.generateLag.isPressed()) {
// PacketHandler.INSTANCE.sendToServer(new PacketGenerateLag());
}
}
}
112 changes: 107 additions & 5 deletions src/main/java/mcjty/theoneprobe/network/NetworkTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@

public class NetworkTools {

/**
* Reads an NBTTagCompound from the given ByteBuf.
*
* @param dataIn The ByteBuf to read from.
* @return The NBTTagCompound read from the buffer, or null if an error occurred.
*/
public static NBTTagCompound readNBT(ByteBuf dataIn) {
PacketBuffer buf = new PacketBuffer(dataIn);
try {
Expand All @@ -21,6 +27,12 @@ public static NBTTagCompound readNBT(ByteBuf dataIn) {
return null;
}

/**
* Writes an NBTTagCompound to the given ByteBuf.
*
* @param dataOut The ByteBuf to write to.
* @param nbt The NBTTagCompound to write.
*/
public static void writeNBT(ByteBuf dataOut, NBTTagCompound nbt) {
PacketBuffer buf = new PacketBuffer(dataOut);
try {
Expand All @@ -30,8 +42,13 @@ public static void writeNBT(ByteBuf dataOut, NBTTagCompound nbt) {
}
}


/// This function supports itemstacks with more then 64 items.
/**
* Reads an ItemStack from the given ByteBuf.
* Supports ItemStacks with more than 64 items.
*
* @param dataIn The ByteBuf to read from.
* @return The ItemStack read from the buffer, or ItemStack.EMPTY if an error occurred.
*/
public static ItemStack readItemStack(ByteBuf dataIn) {
PacketBuffer buf = new PacketBuffer(dataIn);
try {
Expand All @@ -45,7 +62,13 @@ public static ItemStack readItemStack(ByteBuf dataIn) {
return ItemStack.EMPTY;
}

/// This function supports itemstacks with more then 64 items.
/**
* Writes an ItemStack to the given ByteBuf.
* Supports ItemStacks with more than 64 items.
*
* @param dataOut The ByteBuf to write to.
* @param itemStack The ItemStack to write.
*/
public static void writeItemStack(ByteBuf dataOut, ItemStack itemStack) {
PacketBuffer buf = new PacketBuffer(dataOut);
NBTTagCompound nbt = new NBTTagCompound();
Expand All @@ -58,6 +81,12 @@ public static void writeItemStack(ByteBuf dataOut, ItemStack itemStack) {
}
}

/**
* Reads a String from the given ByteBuf.
*
* @param dataIn The ByteBuf to read from.
* @return The String read from the buffer, or null if the length was -1, or an empty string if the length was 0.
*/
public static String readString(ByteBuf dataIn) {
int s = dataIn.readInt();
if (s == -1) {
Expand All @@ -71,6 +100,12 @@ public static String readString(ByteBuf dataIn) {
return new String(dst);
}

/**
* Writes a String to the given ByteBuf.
*
* @param dataOut The ByteBuf to write to.
* @param str The String to write.
*/
public static void writeString(ByteBuf dataOut, String str) {
if (str == null) {
dataOut.writeInt(-1);
Expand All @@ -83,6 +118,12 @@ public static void writeString(ByteBuf dataOut, String str) {
}
}

/**
* Reads a UTF-8 encoded String from the given ByteBuf.
*
* @param dataIn The ByteBuf to read from.
* @return The String read from the buffer, or null if the length was -1, or an empty string if the length was 0.
*/
public static String readStringUTF8(ByteBuf dataIn) {
int s = dataIn.readInt();
if (s == -1) {
Expand All @@ -96,6 +137,12 @@ public static String readStringUTF8(ByteBuf dataIn) {
return new String(dst, java.nio.charset.StandardCharsets.UTF_8);
}

/**
* Writes a UTF-8 encoded String to the given ByteBuf.
*
* @param dataOut The ByteBuf to write to.
* @param str The String to write.
*/
public static void writeStringUTF8(ByteBuf dataOut, String str) {
if (str == null) {
dataOut.writeInt(-1);
Expand All @@ -108,16 +155,36 @@ public static void writeStringUTF8(ByteBuf dataOut, String str) {
}
}

/**
* Reads a BlockPos from the given ByteBuf.
*
* @param dataIn The ByteBuf to read from.
* @return The BlockPos read from the buffer.
*/
public static BlockPos readPos(ByteBuf dataIn) {
return new BlockPos(dataIn.readInt(), dataIn.readInt(), dataIn.readInt());
}

/**
* Writes a BlockPos to the given ByteBuf.
*
* @param dataOut The ByteBuf to write to.
* @param pos The BlockPos to write.
*/
public static void writePos(ByteBuf dataOut, BlockPos pos) {
dataOut.writeInt(pos.getX());
dataOut.writeInt(pos.getY());
dataOut.writeInt(pos.getZ());
}

/**
* Writes an enum value to the given ByteBuf.
*
* @param buf The ByteBuf to write to.
* @param value The enum value to write.
* @param nullValue The value to write if the actual value is null.
* @param <T> The enum type.
*/
public static <T extends Enum<T>> void writeEnum(ByteBuf buf, T value, T nullValue) {
if (value == null) {
buf.writeInt(nullValue.ordinal());
Expand All @@ -126,25 +193,54 @@ public static <T extends Enum<T>> void writeEnum(ByteBuf buf, T value, T nullVal
}
}

/**
* Reads an enum value from the given ByteBuf.
*
* @param buf The ByteBuf to read from.
* @param values The array of possible enum values.
* @param <T> The enum type.
* @return The enum value read from the buffer.
*/
public static <T extends Enum<T>> T readEnum(ByteBuf buf, T[] values) {
return values[buf.readInt()];
}

/**
* Writes a collection of enum values to the given ByteBuf.
*
* @param buf The ByteBuf to write to.
* @param collection The collection of enum values to write.
* @param <T> The enum type.
*/
public static <T extends Enum<T>> void writeEnumCollection(ByteBuf buf, Collection<T> collection) {
buf.writeInt(collection.size());
for (T type : collection) {
buf.writeInt(type.ordinal());
}
}

/**
* Reads a collection of enum values from the given ByteBuf.
*
* @param buf The ByteBuf to read from.
* @param collection The collection to populate with the enum values.
* @param values The array of possible enum values.
* @param <T> The enum type.
*/
public static <T extends Enum<T>> void readEnumCollection(ByteBuf buf, Collection<T> collection, T[] values) {
collection.clear();
int size = buf.readInt();
for (int i = 0 ; i < size ; i++) {
for (int i = 0; i < size; i++) {
collection.add(values[buf.readInt()]);
}
}

/**
* Writes a Float value to the given ByteBuf.
*
* @param buf The ByteBuf to write to.
* @param f The Float value to write, or null to indicate no value.
*/
public static void writeFloat(ByteBuf buf, Float f) {
if (f != null) {
buf.writeBoolean(true);
Expand All @@ -154,11 +250,17 @@ public static void writeFloat(ByteBuf buf, Float f) {
}
}

/**
* Reads a Float value from the given ByteBuf.
*
* @param buf The ByteBuf to read from.
* @return The Float value read from the buffer, or null if no value was written.
*/
public static Float readFloat(ByteBuf buf) {
if (buf.readBoolean()) {
return buf.readFloat();
} else {
return null;
}
}
}
}

0 comments on commit 3023114

Please sign in to comment.