turn drain spots into a class

This commit is contained in:
Ell 2023-06-27 09:44:19 +02:00
parent 5d53126a53
commit e7d79baaba
2 changed files with 49 additions and 43 deletions

View file

@ -24,6 +24,7 @@ import org.apache.commons.lang3.mutable.MutableObject;
import org.apache.commons.lang3.tuple.Pair; import org.apache.commons.lang3.tuple.Pair;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
@ -33,7 +34,7 @@ public class AuraChunk implements IAuraChunk {
private final LevelChunk chunk; private final LevelChunk chunk;
private final IAuraType type; private final IAuraType type;
private final Map<BlockPos, MutableInt> drainSpots = new ConcurrentHashMap<>(); private final Map<BlockPos, DrainSpot> drainSpots = new ConcurrentHashMap<>();
private final Table<BlockPos, Integer, Pair<Integer, Integer>> auraAndSpotAmountCache = HashBasedTable.create(); private final Table<BlockPos, Integer, Pair<Integer, Integer>> auraAndSpotAmountCache = HashBasedTable.create();
private final Table<BlockPos, Integer, Pair<BlockPos, Integer>[]> limitSpotCache = HashBasedTable.create(); private final Table<BlockPos, Integer, Pair<BlockPos, Integer>[]> limitSpotCache = HashBasedTable.create();
private final List<IDrainSpotEffect> effects = new ArrayList<>(); private final List<IDrainSpotEffect> effects = new ArrayList<>();
@ -103,11 +104,11 @@ public class AuraChunk implements IAuraChunk {
return this.storeAura(pos, amount, true, false); 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); var spot = this.drainSpots.get(pos);
if (spot == null && make) { if (spot == null && make) {
spot = new MutableInt(); spot = new DrainSpot(pos, 0);
this.addDrainSpot(pos, spot); this.addDrainSpot(spot);
} }
return spot; return spot;
} }
@ -118,20 +119,20 @@ public class AuraChunk implements IAuraChunk {
return spot == null ? 0 : spot.intValue(); return spot == null ? 0 : spot.intValue();
} }
private void addDrainSpot(BlockPos pos, MutableInt spot) { private void addDrainSpot(DrainSpot spot) {
var expX = pos.getX() >> 4; var expX = spot.pos.getX() >> 4;
var expZ = pos.getZ() >> 4; var expZ = spot.pos.getZ() >> 4;
var myPos = this.chunk.getPos(); var myPos = this.chunk.getPos();
if (expX != myPos.x || expZ != myPos.z) 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<BlockPos, MutableInt> spots) { public void setSpots(Collection<DrainSpot> spots) {
this.drainSpots.clear(); this.drainSpots.clear();
for (var entry : spots.entrySet()) for (var spot : spots)
this.addDrainSpot(entry.getKey(), entry.getValue()); this.addDrainSpot(spot);
this.addOrRemoveAsActive(); this.addOrRemoveAsActive();
} }
@ -170,7 +171,7 @@ public class AuraChunk implements IAuraChunk {
public PacketAuraChunk makePacket() { public PacketAuraChunk makePacket() {
var pos = this.chunk.getPos(); 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<BlockPos, Integer> consumer) { public void getSpots(BlockPos pos, int radius, BiConsumer<BlockPos, Integer> consumer) {
@ -245,13 +246,8 @@ public class AuraChunk implements IAuraChunk {
@Override @Override
public CompoundTag serializeNBT() { public CompoundTag serializeNBT() {
var list = new ListTag(); var list = new ListTag();
for (var entry : this.drainSpots.entrySet()) { for (var spot : this.drainSpots.values())
var tag = new CompoundTag(); list.add(spot.serializeNBT());
tag.putLong("pos", entry.getKey().asLong());
tag.putInt("amount", entry.getValue().intValue());
list.add(tag);
}
var compound = new CompoundTag(); var compound = new CompoundTag();
compound.put("drain_spots", list); compound.put("drain_spots", list);
return compound; return compound;
@ -261,12 +257,8 @@ public class AuraChunk implements IAuraChunk {
public void deserializeNBT(CompoundTag compound) { public void deserializeNBT(CompoundTag compound) {
this.drainSpots.clear(); this.drainSpots.clear();
var list = compound.getList("drain_spots", 10); var list = compound.getList("drain_spots", 10);
for (var base : list) { for (var base : list)
var tag = (CompoundTag) base; this.addDrainSpot(new DrainSpot((CompoundTag) base));
this.addDrainSpot(
BlockPos.of(tag.getLong("pos")),
new MutableInt(tag.getInt("amount")));
}
this.addOrRemoveAsActive(); this.addOrRemoveAsActive();
} }
@ -279,4 +271,25 @@ public class AuraChunk implements IAuraChunk {
data.auraChunksWithSpots.remove(chunkPos); 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;
}
}
} }

View file

@ -4,23 +4,22 @@ import de.ellpeck.naturesaura.NaturesAura;
import de.ellpeck.naturesaura.api.NaturesAuraAPI; import de.ellpeck.naturesaura.api.NaturesAuraAPI;
import de.ellpeck.naturesaura.chunk.AuraChunk; import de.ellpeck.naturesaura.chunk.AuraChunk;
import de.ellpeck.naturesaura.events.ClientEvents; import de.ellpeck.naturesaura.events.ClientEvents;
import net.minecraft.core.BlockPos;
import net.minecraft.network.FriendlyByteBuf; import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.level.Level; import net.minecraft.world.level.Level;
import net.minecraftforge.network.NetworkEvent; import net.minecraftforge.network.NetworkEvent;
import org.apache.commons.lang3.mutable.MutableInt;
import java.util.HashMap; import java.util.ArrayList;
import java.util.Map; import java.util.Collection;
import java.util.List;
import java.util.function.Supplier; import java.util.function.Supplier;
public class PacketAuraChunk { public class PacketAuraChunk {
private int chunkX; private int chunkX;
private int chunkZ; private int chunkZ;
private Map<BlockPos, MutableInt> drainSpots; private Collection<AuraChunk.DrainSpot> drainSpots;
public PacketAuraChunk(int chunkX, int chunkZ, Map<BlockPos, MutableInt> drainSpots) { public PacketAuraChunk(int chunkX, int chunkZ, Collection<AuraChunk.DrainSpot> drainSpots) {
this.chunkX = chunkX; this.chunkX = chunkX;
this.chunkZ = chunkZ; this.chunkZ = chunkZ;
this.drainSpots = drainSpots; this.drainSpots = drainSpots;
@ -34,14 +33,10 @@ public class PacketAuraChunk {
packet.chunkX = buf.readInt(); packet.chunkX = buf.readInt();
packet.chunkZ = buf.readInt(); packet.chunkZ = buf.readInt();
packet.drainSpots = new HashMap<>(); packet.drainSpots = new ArrayList<>();
var amount = buf.readInt(); var amount = buf.readInt();
for (var i = 0; i < amount; i++) { for (var i = 0; i < amount; i++)
packet.drainSpots.put( packet.drainSpots.add(new AuraChunk.DrainSpot(buf.readNbt()));
BlockPos.of(buf.readLong()),
new MutableInt(buf.readInt())
);
}
return packet; return packet;
} }
@ -51,10 +46,8 @@ public class PacketAuraChunk {
buf.writeInt(packet.chunkZ); buf.writeInt(packet.chunkZ);
buf.writeInt(packet.drainSpots.size()); buf.writeInt(packet.drainSpots.size());
for (var entry : packet.drainSpots.entrySet()) { for (var entry : packet.drainSpots)
buf.writeLong(entry.getKey().asLong()); buf.writeNbt(entry.serializeNBT());
buf.writeInt(entry.getValue().intValue());
}
} }
public static void onMessage(PacketAuraChunk message, Supplier<NetworkEvent.Context> ctx) { public static void onMessage(PacketAuraChunk message, Supplier<NetworkEvent.Context> ctx) {