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

78 lines
2.5 KiB
Java
Raw Normal View History

package de.ellpeck.naturesaura.packet;
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;
2020-01-22 01:35:47 +01:00
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.chunk.Chunk;
2019-10-20 22:30:49 +02:00
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
2020-01-22 01:35:47 +01:00
import net.minecraftforge.fml.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() {
}
2020-01-22 01:35:47 +01:00
public static PacketAuraChunk fromBytes(PacketBuffer buf) {
PacketAuraChunk packet = new PacketAuraChunk();
packet.chunkX = buf.readInt();
packet.chunkZ = buf.readInt();
2020-01-22 01:35:47 +01:00
packet.drainSpots = new HashMap<>();
int amount = buf.readInt();
for (int i = 0; i < amount; i++) {
2020-01-22 01:35:47 +01:00
packet.drainSpots.put(
BlockPos.fromLong(buf.readLong()),
2018-10-24 13:06:24 +02:00
new MutableInt(buf.readInt())
);
}
2020-01-22 01:35:47 +01:00
return packet;
}
2020-01-22 01:35:47 +01:00
public static void toBytes(PacketAuraChunk packet, PacketBuffer buf) {
buf.writeInt(packet.chunkX);
buf.writeInt(packet.chunkZ);
2020-01-22 01:35:47 +01:00
buf.writeInt(packet.drainSpots.size());
for (Map.Entry<BlockPos, MutableInt> entry : packet.drainSpots.entrySet()) {
buf.writeLong(entry.getKey().toLong());
2018-10-24 13:06:24 +02:00
buf.writeInt(entry.getValue().intValue());
}
}
2020-01-22 23:21:52 +01:00
public boolean tryHandle(World world) {
Chunk chunk = world.getChunk(this.chunkX, this.chunkZ);
if (chunk.isEmpty())
return false;
AuraChunk auraChunk = (AuraChunk) chunk.getCapability(NaturesAuraAPI.capAuraChunk).orElse(null);
if (auraChunk == null)
return false;
auraChunk.setSpots(this.drainSpots);
return true;
}
2020-01-22 01:35:47 +01:00
2020-01-22 23:21:52 +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);
}
2020-01-22 01:35:47 +01:00
}