made aura properly sync when joining the world

This commit is contained in:
Ellpeck 2018-10-21 13:12:03 +02:00
parent cb12a890ab
commit c22a1da6b4
3 changed files with 22 additions and 1 deletions

View file

@ -16,6 +16,7 @@ import net.minecraft.world.chunk.Chunk;
import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilityProvider; import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.common.util.INBTSerializable; import net.minecraftforge.common.util.INBTSerializable;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import org.apache.commons.lang3.mutable.MutableDouble; import org.apache.commons.lang3.mutable.MutableDouble;
import org.apache.commons.lang3.mutable.MutableInt; import org.apache.commons.lang3.mutable.MutableInt;
import org.apache.commons.lang3.mutable.MutableObject; import org.apache.commons.lang3.mutable.MutableObject;
@ -132,7 +133,7 @@ public class AuraChunk implements ICapabilityProvider, INBTSerializable<NBTTagCo
if (this.needsSync) { if (this.needsSync) {
PacketHandler.sendToAllLoaded(world, PacketHandler.sendToAllLoaded(world,
new BlockPos(this.chunk.x * 16, 0, this.chunk.z * 16), new BlockPos(this.chunk.x * 16, 0, this.chunk.z * 16),
new PacketAuraChunk(this.chunk.x, this.chunk.z, this.drainSpots)); this.makePacket());
this.needsSync = false; this.needsSync = false;
} }
@ -166,6 +167,10 @@ public class AuraChunk implements ICapabilityProvider, INBTSerializable<NBTTagCo
} }
} }
public IMessage makePacket() {
return new PacketAuraChunk(this.chunk.x, this.chunk.z, this.drainSpots);
}
@Override @Override
public boolean hasCapability(@Nonnull Capability<?> capability, @Nullable EnumFacing facing) { public boolean hasCapability(@Nonnull Capability<?> capability, @Nullable EnumFacing facing) {
return capability == Capabilities.auraChunk; return capability == Capabilities.auraChunk;

View file

@ -3,10 +3,12 @@ package de.ellpeck.naturesaura.events;
import de.ellpeck.naturesaura.NaturesAura; import de.ellpeck.naturesaura.NaturesAura;
import de.ellpeck.naturesaura.aura.Capabilities; import de.ellpeck.naturesaura.aura.Capabilities;
import de.ellpeck.naturesaura.aura.chunk.AuraChunk; import de.ellpeck.naturesaura.aura.chunk.AuraChunk;
import de.ellpeck.naturesaura.packet.PacketHandler;
import net.minecraft.util.ResourceLocation; import net.minecraft.util.ResourceLocation;
import net.minecraft.world.WorldServer; import net.minecraft.world.WorldServer;
import net.minecraft.world.chunk.Chunk; import net.minecraft.world.chunk.Chunk;
import net.minecraftforge.event.AttachCapabilitiesEvent; import net.minecraftforge.event.AttachCapabilitiesEvent;
import net.minecraftforge.event.world.ChunkWatchEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent; import net.minecraftforge.fml.common.gameevent.TickEvent;
@ -34,4 +36,13 @@ public class CommonEvents {
} }
} }
} }
@SubscribeEvent
public void onChunkWatch(ChunkWatchEvent.Watch event) {
Chunk chunk = event.getChunkInstance();
if (!chunk.getWorld().isRemote && chunk.hasCapability(Capabilities.auraChunk, null)) {
AuraChunk auraChunk = chunk.getCapability(Capabilities.auraChunk, null);
PacketHandler.sendTo(event.getPlayer(), auraChunk.makePacket());
}
}
} }

View file

@ -1,6 +1,8 @@
package de.ellpeck.naturesaura.packet; package de.ellpeck.naturesaura.packet;
import de.ellpeck.naturesaura.NaturesAura; import de.ellpeck.naturesaura.NaturesAura;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraftforge.fml.common.network.NetworkRegistry; import net.minecraftforge.fml.common.network.NetworkRegistry;
@ -27,4 +29,7 @@ public final class PacketHandler {
network.sendToAllAround(message, new NetworkRegistry.TargetPoint(world.provider.getDimension(), pos.getX(), pos.getY(), pos.getZ(), range)); network.sendToAllAround(message, new NetworkRegistry.TargetPoint(world.provider.getDimension(), pos.getX(), pos.getY(), pos.getZ(), range));
} }
public static void sendTo(EntityPlayer player, IMessage message) {
network.sendTo(message, (EntityPlayerMP) player);
}
} }