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

83 lines
2.7 KiB
Java
Raw Normal View History

package de.ellpeck.naturesaura.packet;
import de.ellpeck.naturesaura.NaturesAura;
import de.ellpeck.naturesaura.aura.Capabilities;
import de.ellpeck.naturesaura.aura.chunk.AuraChunk;
import de.ellpeck.naturesaura.aura.chunk.DrainSpot;
import io.netty.buffer.ByteBuf;
import net.minecraft.client.Minecraft;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.chunk.Chunk;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import java.util.HashMap;
import java.util.Map;
public class PacketAuraChunk implements IMessage {
private int chunkX;
private int chunkZ;
private Map<BlockPos, DrainSpot> drainSpots;
public PacketAuraChunk(int chunkX, int chunkZ, Map<BlockPos, DrainSpot> drainSpots) {
this.chunkX = chunkX;
this.chunkZ = chunkZ;
this.drainSpots = drainSpots;
}
public PacketAuraChunk() {
}
@Override
public void fromBytes(ByteBuf buf) {
this.chunkX = buf.readInt();
this.chunkZ = buf.readInt();
this.drainSpots = new HashMap<>();
int amount = buf.readInt();
for (int i = 0; i < amount; i++) {
this.drainSpots.put(
BlockPos.fromLong(buf.readLong()),
new DrainSpot(buf.readInt())
);
}
}
@Override
public void toBytes(ByteBuf buf) {
buf.writeInt(this.chunkX);
buf.writeInt(this.chunkZ);
buf.writeInt(this.drainSpots.size());
for (Map.Entry<BlockPos, DrainSpot> entry : this.drainSpots.entrySet()) {
buf.writeLong(entry.getKey().toLong());
buf.writeInt(entry.getValue().getAmount());
}
}
public static class Handler implements IMessageHandler<PacketAuraChunk, IMessage> {
@Override
@SideOnly(Side.CLIENT)
public IMessage onMessage(PacketAuraChunk message, MessageContext ctx) {
NaturesAura.proxy.scheduleTask(() -> {
World world = Minecraft.getMinecraft().world;
if (world != null) {
Chunk chunk = world.getChunk(message.chunkX, message.chunkZ);
if (chunk.hasCapability(Capabilities.auraChunk, null)) {
AuraChunk auraChunk = chunk.getCapability(Capabilities.auraChunk, null);
auraChunk.setSpots(message.drainSpots);
}
}
});
return null;
}
}
}