Skip to content

Commit

Permalink
chore: added some codecs/stream codecs to some utility objects
Browse files Browse the repository at this point in the history
  • Loading branch information
desht committed May 13, 2024
1 parent a395b2a commit 8d58c99
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 94 deletions.
59 changes: 18 additions & 41 deletions common/src/main/java/dev/ftb/mods/ftblibrary/math/ChunkDimPos.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
package dev.ftb.mods.ftblibrary.math;

import io.netty.buffer.ByteBuf;
import net.minecraft.core.BlockPos;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.Level;

import java.util.Objects;
public record ChunkDimPos(ResourceKey<Level> dimension, ChunkPos chunkPos) implements Comparable<ChunkDimPos> {
private static final StreamCodec<ByteBuf,ChunkPos> CHUNK_POS_STREAM_CODEC = StreamCodec.composite(
ByteBufCodecs.INT, cp -> cp.x,
ByteBufCodecs.INT, cp -> cp.z,
ChunkPos::new
);


public class ChunkDimPos implements Comparable<ChunkDimPos> {
private final ResourceKey<Level> dimension;
private final ChunkPos chunkPos;
private int hash;
public static StreamCodec<FriendlyByteBuf, ChunkDimPos> STREAM_CODEC = StreamCodec.composite(
ResourceKey.streamCodec(Registries.DIMENSION), ChunkDimPos::dimension,
CHUNK_POS_STREAM_CODEC, ChunkDimPos::chunkPos,
ChunkDimPos::new
);

public ChunkDimPos(ResourceKey<Level> dim, int x, int z) {
dimension = dim;
chunkPos = new ChunkPos(x, z);

int h = Objects.hash(dimension.location(), chunkPos);
hash = h == 0 ? 1 : h;
}

public ChunkDimPos(ResourceKey<Level> dim, ChunkPos pos) {
this(dim, pos.x, pos.z);
this(dim, new ChunkPos(x, z));
}

public ChunkDimPos(Level world, BlockPos pos) {
Expand All @@ -34,10 +36,6 @@ public ChunkDimPos(Entity entity) {
this(entity.level(), entity.blockPosition());
}

public ChunkPos getChunkPos() {
return chunkPos;
}

public int x() {
return chunkPos.x;
}
Expand All @@ -50,31 +48,10 @@ public ResourceKey<Level> dimension() {
return dimension;
}

@Override
public String toString() {
return "[" + dimension.location() + ":" + x() + ":" + z() + "]";
}

@Override
public int hashCode() {
return hash;
}

@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
} else if (obj instanceof ChunkDimPos p) {
return dimension == p.dimension && chunkPos.equals(p.chunkPos);
}

return false;
}

@Override
public int compareTo(ChunkDimPos o) {
var i = dimension.location().compareTo(o.dimension.location());
return i == 0 ? Long.compare(getChunkPos().toLong(), o.getChunkPos().toLong()) : i;
return i == 0 ? Long.compare(chunkPos.toLong(), o.chunkPos.toLong()) : i;
}

public ChunkDimPos offset(int ox, int oz) {
Expand Down
44 changes: 16 additions & 28 deletions common/src/main/java/dev/ftb/mods/ftblibrary/math/XZ.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
package dev.ftb.mods.ftblibrary.math;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.core.Vec3i;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.Level;


public record XZ(int x, int z) {
public static final Codec<XZ> CODEC = RecordCodecBuilder.create(builder -> builder.group(
Codec.INT.fieldOf("x").forGetter(XZ::x),
Codec.INT.fieldOf("z").forGetter(XZ::z)
).apply(builder, XZ::new));

public static StreamCodec<FriendlyByteBuf, XZ> STREAM_CODEC = StreamCodec.composite(
ByteBufCodecs.INT, XZ::x,
ByteBufCodecs.INT, XZ::z,
XZ::new
);

public static XZ of(int x, int z) {
return new XZ(x, z);
}
Expand Down Expand Up @@ -43,34 +59,6 @@ public static XZ regionFromBlock(Vec3i pos) {
return regionFromBlock(pos.getX(), pos.getZ());
}

// public final int x;
// public final int z;
//
// private XZ(int _x, int _z) {
// x = _x;
// z = _z;
// }

public int hashCode() {
var x1 = 1664525 * x + 1013904223;
var z1 = 1664525 * (z ^ -559038737) + 1013904223;
return x1 ^ z1;
}

public boolean equals(Object o) {
if (o == this) {
return true;
} else if (!(o instanceof XZ p)) {
return false;
} else {
return x == p.x && z == p.z;
}
}

public String toString() {
return "[" + x + ", " + z + "]";
}

public ChunkDimPos dim(ResourceKey<Level> type) {
return new ChunkDimPos(type, x, z);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
package dev.ftb.mods.ftblibrary.snbt;

import net.minecraft.nbt.*;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.codec.StreamCodec;
import org.jetbrains.annotations.Nullable;

import java.util.*;


public class SNBTCompoundTag extends CompoundTag {
public static final StreamCodec<FriendlyByteBuf,SNBTCompoundTag> STREAM_CODEC = new StreamCodec<>() {
@Override
public SNBTCompoundTag decode(FriendlyByteBuf object) {
return SNBTNet.readCompound(object);
}

@Override
public void encode(FriendlyByteBuf object, SNBTCompoundTag object2) {
SNBTNet.write(object, object2);
}
};

public static SNBTCompoundTag of(@Nullable Tag tag) {
if (tag instanceof SNBTCompoundTag) {
return (SNBTCompoundTag) tag;
Expand Down
41 changes: 16 additions & 25 deletions common/src/main/java/dev/ftb/mods/ftblibrary/snbt/SNBTNet.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,22 @@ public class SNBTNet {
public static final LongArrayTag EMPTY_LONG_ARRAY = new LongArrayTag(new long[0]);

public static void write(FriendlyByteBuf buf, @Nullable Tag tag) {
if (tag instanceof ByteTag) {
buf.writeByte(((ByteTag) tag).getAsByte());
} else if (tag instanceof ShortTag) {
buf.writeShort(((ShortTag) tag).getAsShort());
} else if (tag instanceof IntTag) {
buf.writeInt(((IntTag) tag).getAsInt());
} else if (tag instanceof LongTag) {
buf.writeLong(((LongTag) tag).getAsLong());
} else if (tag instanceof FloatTag) {
buf.writeFloat(((FloatTag) tag).getAsFloat());
} else if (tag instanceof DoubleTag) {
buf.writeDouble(((DoubleTag) tag).getAsDouble());
} else if (tag instanceof ByteArrayTag) {
writeByteArray(buf, (ByteArrayTag) tag);
} else if (tag instanceof StringTag) {
buf.writeUtf(tag.getAsString(), Short.MAX_VALUE);
} else if (tag instanceof ListTag) {
writeList(buf, (ListTag) tag);
} else if (tag instanceof CompoundTag) {
writeCompound(buf, SNBTCompoundTag.of(tag));
} else if (tag instanceof IntArrayTag) {
writeIntArray(buf, (IntArrayTag) tag);
} else if (tag instanceof LongArrayTag) {
writeLongArray(buf, (LongArrayTag) tag);
}
switch (tag) {
case ByteTag byteTag -> buf.writeByte(byteTag.getAsByte());
case ShortTag shortTag -> buf.writeShort(shortTag.getAsShort());
case IntTag intTag -> buf.writeInt(intTag.getAsInt());
case LongTag longTag -> buf.writeLong(longTag.getAsLong());
case FloatTag floatTag -> buf.writeFloat(floatTag.getAsFloat());
case DoubleTag doubleTag -> buf.writeDouble(doubleTag.getAsDouble());
case ByteArrayTag byteTags -> writeByteArray(buf, byteTags);
case StringTag stringTag -> buf.writeUtf(stringTag.getAsString(), Short.MAX_VALUE);
case ListTag tags -> writeList(buf, tags);
case CompoundTag compoundTag -> writeCompound(buf, SNBTCompoundTag.of(compoundTag));
case IntArrayTag intTags -> writeIntArray(buf, intTags);
case LongArrayTag longTags -> writeLongArray(buf, longTags);
case null, default -> {
}
}
}

public static void writeCompound(FriendlyByteBuf buf, @Nullable SNBTCompoundTag tag) {
Expand Down

0 comments on commit 8d58c99

Please sign in to comment.