NaturesAura/src/main/java/de/ellpeck/naturesaura/packet/PacketAuraChunk.java

81 lines
2.6 KiB
Java
Raw Normal View History

package de.ellpeck.naturesaura.packet;
import de.ellpeck.naturesaura.NaturesAura;
2018-11-12 22:04:40 +01:00
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
import de.ellpeck.naturesaura.chunk.AuraChunk;
2020-01-22 23:21:52 +01:00
import de.ellpeck.naturesaura.events.ClientEvents;
2021-12-04 15:40:09 +01:00
import net.minecraft.core.BlockPos;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.level.Level;
import net.minecraftforge.network.NetworkEvent;
2018-10-24 13:06:24 +02:00
import org.apache.commons.lang3.mutable.MutableInt;
import java.util.HashMap;
import java.util.Map;
2020-01-22 01:35:47 +01:00
import java.util.function.Supplier;
2020-01-22 23:21:52 +01:00
public class PacketAuraChunk {
private int chunkX;
private int chunkZ;
2018-10-24 13:06:24 +02:00
private Map<BlockPos, MutableInt> drainSpots;
2020-01-22 23:21:52 +01:00
public PacketAuraChunk(int chunkX, int chunkZ, Map<BlockPos, MutableInt> drainSpots) {
this.chunkX = chunkX;
this.chunkZ = chunkZ;
this.drainSpots = drainSpots;
}
private PacketAuraChunk() {
}
2021-12-04 15:40:09 +01:00
public static PacketAuraChunk fromBytes(FriendlyByteBuf buf) {
2021-12-15 16:30:22 +01:00
var packet = new PacketAuraChunk();
2020-01-22 01:35:47 +01:00
packet.chunkX = buf.readInt();
packet.chunkZ = buf.readInt();
2020-01-22 01:35:47 +01:00
packet.drainSpots = new HashMap<>();
2021-12-15 16:30:22 +01:00
var amount = buf.readInt();
for (var i = 0; i < amount; i++) {
2020-01-22 01:35:47 +01:00
packet.drainSpots.put(
2021-12-15 16:24:53 +01:00
BlockPos.of(buf.readLong()),
2018-10-24 13:06:24 +02:00
new MutableInt(buf.readInt())
);
}
2020-01-22 01:35:47 +01:00
return packet;
}
2021-12-04 15:40:09 +01:00
public static void toBytes(PacketAuraChunk packet, FriendlyByteBuf buf) {
2020-01-22 01:35:47 +01:00
buf.writeInt(packet.chunkX);
buf.writeInt(packet.chunkZ);
2020-01-22 01:35:47 +01:00
buf.writeInt(packet.drainSpots.size());
2021-12-15 16:30:22 +01:00
for (var entry : packet.drainSpots.entrySet()) {
2021-12-15 16:24:53 +01:00
buf.writeLong(entry.getKey().asLong());
2018-10-24 13:06:24 +02:00
buf.writeInt(entry.getValue().intValue());
}
}
2020-02-07 15:22:30 +01:00
public static void onMessage(PacketAuraChunk message, Supplier<NetworkEvent.Context> ctx) {
ctx.get().enqueueWork(() -> ClientEvents.PENDING_AURA_CHUNKS.add(message));
ctx.get().setPacketHandled(true);
}
2021-12-04 15:40:09 +01:00
public boolean tryHandle(Level level) {
try {
2021-12-15 16:30:22 +01:00
var chunk = level.getChunk(this.chunkX, this.chunkZ);
if (chunk.isEmpty())
return false;
2021-12-23 13:27:52 +01:00
var auraChunk = (AuraChunk) chunk.getCapability(NaturesAuraAPI.CAP_AURA_CHUNK).orElse(null);
if (auraChunk == null)
return false;
auraChunk.setSpots(this.drainSpots);
return true;
} catch (Exception e) {
NaturesAura.LOGGER.error("There was an error handling an aura chunk packet", e);
return true;
}
2020-01-22 23:21:52 +01:00
}
2020-01-22 01:35:47 +01:00
}