From e7d79baaba437f8d2722ef7058c35b8cb232f8b1 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Tue, 27 Jun 2023 09:44:19 +0200 Subject: [PATCH] turn drain spots into a class --- .../ellpeck/naturesaura/chunk/AuraChunk.java | 65 +++++++++++-------- .../naturesaura/packet/PacketAuraChunk.java | 27 +++----- 2 files changed, 49 insertions(+), 43 deletions(-) diff --git a/src/main/java/de/ellpeck/naturesaura/chunk/AuraChunk.java b/src/main/java/de/ellpeck/naturesaura/chunk/AuraChunk.java index 1730c58d..a0105787 100644 --- a/src/main/java/de/ellpeck/naturesaura/chunk/AuraChunk.java +++ b/src/main/java/de/ellpeck/naturesaura/chunk/AuraChunk.java @@ -24,6 +24,7 @@ import org.apache.commons.lang3.mutable.MutableObject; import org.apache.commons.lang3.tuple.Pair; import java.util.ArrayList; +import java.util.Collection; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -33,7 +34,7 @@ public class AuraChunk implements IAuraChunk { private final LevelChunk chunk; private final IAuraType type; - private final Map drainSpots = new ConcurrentHashMap<>(); + private final Map drainSpots = new ConcurrentHashMap<>(); private final Table> auraAndSpotAmountCache = HashBasedTable.create(); private final Table[]> limitSpotCache = HashBasedTable.create(); private final List effects = new ArrayList<>(); @@ -103,11 +104,11 @@ public class AuraChunk implements IAuraChunk { return this.storeAura(pos, amount, true, false); } - private MutableInt getActualDrainSpot(BlockPos pos, boolean make) { + private DrainSpot getActualDrainSpot(BlockPos pos, boolean make) { var spot = this.drainSpots.get(pos); if (spot == null && make) { - spot = new MutableInt(); - this.addDrainSpot(pos, spot); + spot = new DrainSpot(pos, 0); + this.addDrainSpot(spot); } return spot; } @@ -118,20 +119,20 @@ public class AuraChunk implements IAuraChunk { return spot == null ? 0 : spot.intValue(); } - private void addDrainSpot(BlockPos pos, MutableInt spot) { - var expX = pos.getX() >> 4; - var expZ = pos.getZ() >> 4; + private void addDrainSpot(DrainSpot spot) { + var expX = spot.pos.getX() >> 4; + var expZ = spot.pos.getZ() >> 4; var myPos = this.chunk.getPos(); if (expX != myPos.x || expZ != myPos.z) - throw new IllegalArgumentException("Tried to add drain spot " + pos + " to chunk at " + myPos.x + ", " + myPos.z + " when it should've been added to chunk at " + expX + ", " + expZ); + throw new IllegalArgumentException("Tried to add drain spot " + spot.pos + " to chunk at " + myPos.x + ", " + myPos.z + " when it should've been added to chunk at " + expX + ", " + expZ); - this.drainSpots.put(pos, spot); + this.drainSpots.put(spot.pos, spot); } - public void setSpots(Map spots) { + public void setSpots(Collection spots) { this.drainSpots.clear(); - for (var entry : spots.entrySet()) - this.addDrainSpot(entry.getKey(), entry.getValue()); + for (var spot : spots) + this.addDrainSpot(spot); this.addOrRemoveAsActive(); } @@ -170,7 +171,7 @@ public class AuraChunk implements IAuraChunk { public PacketAuraChunk makePacket() { var pos = this.chunk.getPos(); - return new PacketAuraChunk(pos.x, pos.z, this.drainSpots); + return new PacketAuraChunk(pos.x, pos.z, this.drainSpots.values()); } public void getSpots(BlockPos pos, int radius, BiConsumer consumer) { @@ -245,13 +246,8 @@ public class AuraChunk implements IAuraChunk { @Override public CompoundTag serializeNBT() { var list = new ListTag(); - for (var entry : this.drainSpots.entrySet()) { - var tag = new CompoundTag(); - tag.putLong("pos", entry.getKey().asLong()); - tag.putInt("amount", entry.getValue().intValue()); - list.add(tag); - } - + for (var spot : this.drainSpots.values()) + list.add(spot.serializeNBT()); var compound = new CompoundTag(); compound.put("drain_spots", list); return compound; @@ -261,12 +257,8 @@ public class AuraChunk implements IAuraChunk { public void deserializeNBT(CompoundTag compound) { this.drainSpots.clear(); var list = compound.getList("drain_spots", 10); - for (var base : list) { - var tag = (CompoundTag) base; - this.addDrainSpot( - BlockPos.of(tag.getLong("pos")), - new MutableInt(tag.getInt("amount"))); - } + for (var base : list) + this.addDrainSpot(new DrainSpot((CompoundTag) base)); this.addOrRemoveAsActive(); } @@ -279,4 +271,25 @@ public class AuraChunk implements IAuraChunk { data.auraChunksWithSpots.remove(chunkPos); } } + + public static class DrainSpot extends MutableInt { + + public final BlockPos pos; + + public DrainSpot(BlockPos pos, int value) { + super(value); + this.pos = pos; + } + + public DrainSpot(CompoundTag tag) { + this(BlockPos.of(tag.getLong("pos")), tag.getInt("amount")); + } + + public CompoundTag serializeNBT() { + var ret = new CompoundTag(); + ret.putLong("pos", this.pos.asLong()); + ret.putInt("amount", this.intValue()); + return ret; + } + } } diff --git a/src/main/java/de/ellpeck/naturesaura/packet/PacketAuraChunk.java b/src/main/java/de/ellpeck/naturesaura/packet/PacketAuraChunk.java index 1e2a3911..8abc3f81 100644 --- a/src/main/java/de/ellpeck/naturesaura/packet/PacketAuraChunk.java +++ b/src/main/java/de/ellpeck/naturesaura/packet/PacketAuraChunk.java @@ -4,23 +4,22 @@ import de.ellpeck.naturesaura.NaturesAura; import de.ellpeck.naturesaura.api.NaturesAuraAPI; import de.ellpeck.naturesaura.chunk.AuraChunk; import de.ellpeck.naturesaura.events.ClientEvents; -import net.minecraft.core.BlockPos; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.world.level.Level; import net.minecraftforge.network.NetworkEvent; -import org.apache.commons.lang3.mutable.MutableInt; -import java.util.HashMap; -import java.util.Map; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; import java.util.function.Supplier; public class PacketAuraChunk { private int chunkX; private int chunkZ; - private Map drainSpots; + private Collection drainSpots; - public PacketAuraChunk(int chunkX, int chunkZ, Map drainSpots) { + public PacketAuraChunk(int chunkX, int chunkZ, Collection drainSpots) { this.chunkX = chunkX; this.chunkZ = chunkZ; this.drainSpots = drainSpots; @@ -34,14 +33,10 @@ public class PacketAuraChunk { packet.chunkX = buf.readInt(); packet.chunkZ = buf.readInt(); - packet.drainSpots = new HashMap<>(); + packet.drainSpots = new ArrayList<>(); var amount = buf.readInt(); - for (var i = 0; i < amount; i++) { - packet.drainSpots.put( - BlockPos.of(buf.readLong()), - new MutableInt(buf.readInt()) - ); - } + for (var i = 0; i < amount; i++) + packet.drainSpots.add(new AuraChunk.DrainSpot(buf.readNbt())); return packet; } @@ -51,10 +46,8 @@ public class PacketAuraChunk { buf.writeInt(packet.chunkZ); buf.writeInt(packet.drainSpots.size()); - for (var entry : packet.drainSpots.entrySet()) { - buf.writeLong(entry.getKey().asLong()); - buf.writeInt(entry.getValue().intValue()); - } + for (var entry : packet.drainSpots) + buf.writeNbt(entry.serializeNBT()); } public static void onMessage(PacketAuraChunk message, Supplier ctx) {