2018-10-21 12:51:13 +02:00
|
|
|
package de.ellpeck.naturesaura.packet;
|
|
|
|
|
2018-11-12 22:04:40 +01:00
|
|
|
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
|
2018-11-13 11:39:28 +01:00
|
|
|
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;
|
2018-10-21 12:51:13 +02:00
|
|
|
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;
|
2018-10-21 12:51:13 +02:00
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
2020-01-22 01:35:47 +01:00
|
|
|
import java.util.function.Supplier;
|
2018-10-21 12:51:13 +02:00
|
|
|
|
2020-01-22 23:21:52 +01:00
|
|
|
public class PacketAuraChunk {
|
2018-10-21 12:51:13 +02:00
|
|
|
|
|
|
|
private int chunkX;
|
|
|
|
private int chunkZ;
|
2018-10-24 13:06:24 +02:00
|
|
|
private Map<BlockPos, MutableInt> drainSpots;
|
2018-10-21 12:51:13 +02:00
|
|
|
|
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();
|
2018-10-21 12:51:13 +02:00
|
|
|
|
2020-01-22 01:35:47 +01:00
|
|
|
packet.drainSpots = new HashMap<>();
|
2018-10-21 12:51:13 +02:00
|
|
|
int amount = buf.readInt();
|
|
|
|
for (int i = 0; i < amount; i++) {
|
2020-01-22 01:35:47 +01:00
|
|
|
packet.drainSpots.put(
|
2018-10-21 12:51:13 +02:00
|
|
|
BlockPos.fromLong(buf.readLong()),
|
2018-10-24 13:06:24 +02:00
|
|
|
new MutableInt(buf.readInt())
|
2018-10-21 12:51:13 +02:00
|
|
|
);
|
|
|
|
}
|
2020-01-22 01:35:47 +01:00
|
|
|
|
|
|
|
return packet;
|
2018-10-21 12:51:13 +02:00
|
|
|
}
|
|
|
|
|
2020-01-22 01:35:47 +01:00
|
|
|
public static void toBytes(PacketAuraChunk packet, PacketBuffer buf) {
|
|
|
|
buf.writeInt(packet.chunkX);
|
|
|
|
buf.writeInt(packet.chunkZ);
|
2018-10-21 12:51:13 +02:00
|
|
|
|
2020-01-22 01:35:47 +01:00
|
|
|
buf.writeInt(packet.drainSpots.size());
|
|
|
|
for (Map.Entry<BlockPos, MutableInt> entry : packet.drainSpots.entrySet()) {
|
2018-10-21 12:51:13 +02:00
|
|
|
buf.writeLong(entry.getKey().toLong());
|
2018-10-24 13:06:24 +02:00
|
|
|
buf.writeInt(entry.getValue().intValue());
|
2018-10-21 12:51:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
2018-10-21 12:51:13 +02:00
|
|
|
}
|
2020-01-22 01:35:47 +01:00
|
|
|
}
|