2018-10-21 12:51:13 +02:00
|
|
|
package de.ellpeck.naturesaura.packet;
|
|
|
|
|
2021-09-26 16:33:05 +02:00
|
|
|
import de.ellpeck.naturesaura.NaturesAura;
|
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;
|
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;
|
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() {
|
|
|
|
}
|
|
|
|
|
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();
|
2018-10-21 12:51:13 +02:00
|
|
|
|
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())
|
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
|
|
|
}
|
|
|
|
|
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);
|
2018-10-21 12:51:13 +02:00
|
|
|
|
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());
|
2018-10-21 12:51:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2021-09-26 16:33:05 +02:00
|
|
|
try {
|
2021-12-15 16:30:22 +01:00
|
|
|
var chunk = level.getChunk(this.chunkX, this.chunkZ);
|
2021-09-26 16:33:05 +02:00
|
|
|
if (chunk.isEmpty())
|
|
|
|
return false;
|
2021-12-23 13:27:52 +01:00
|
|
|
var auraChunk = (AuraChunk) chunk.getCapability(NaturesAuraAPI.CAP_AURA_CHUNK).orElse(null);
|
2021-09-26 16:33:05 +02:00
|
|
|
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
|
|
|
}
|