Skip to content

Commit

Permalink
Make sync map initialization lazy (fix #84)
Browse files Browse the repository at this point in the history
  • Loading branch information
boq committed Dec 24, 2019
1 parent 9d7ebad commit 239bb8a
Showing 1 changed file with 9 additions and 20 deletions.
29 changes: 9 additions & 20 deletions src/main/java/openmods/tileentity/SyncedTileEntity.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package openmods.tileentity;

import com.google.common.base.Preconditions;
import com.google.common.collect.Sets;
import io.netty.buffer.Unpooled;
import java.io.IOException;
Expand All @@ -11,8 +10,8 @@
import net.minecraft.network.PacketBuffer;
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.util.Constants;
import net.minecraftforge.fml.common.FMLCommonHandler;
import openmods.network.rpc.IRpcTarget;
import openmods.network.rpc.RpcCallDispatcher;
import openmods.network.rpc.targets.SyncRpcTarget;
Expand Down Expand Up @@ -40,30 +39,18 @@ public SyncedTileEntity() {
createSyncedFields();
}

private void createSyncMap(World world) {
final SyncMap syncMap = world.isRemote? new SyncMapClient() : new SyncMapTile(this, UpdateStrategy.WITH_INITIAL_PACKET);

private SyncMap createSyncMap() {
final SyncMap syncMap = FMLCommonHandler.instance().getEffectiveSide().isClient()
? new SyncMapClient()
: new SyncMapTile(this, UpdateStrategy.WITH_INITIAL_PACKET);
SyncObjectScanner.INSTANCE.registerAllFields(syncMap, this);

syncMap.addSyncListener(changes -> markUpdated());

this.syncMap = syncMap;
onSyncMapCreate(syncMap);
return syncMap;
}

protected void onSyncMapCreate(SyncMap syncMap) {}

@Override
public void validate() {
super.validate();
createSyncMap(world);
}

@Override
protected void setWorldCreate(World worldIn) {
createSyncMap(worldIn);
}

protected DropTagSerializer getDropSerializer() {
if (tagSerializer == null) tagSerializer = new DropTagSerializer();
return tagSerializer;
Expand Down Expand Up @@ -101,7 +88,9 @@ public boolean trySync() {

@Override
public SyncMap getSyncMap() {
Preconditions.checkState(syncMap != null, "Tile entity not initialized properly");
if (syncMap == null) {
syncMap = createSyncMap();
}
return syncMap;
}

Expand Down

0 comments on commit 239bb8a

Please sign in to comment.