Convenience sided init/dispose in tile entity base class.

This commit is contained in:
Florian Nücke
2020-12-02 01:58:54 +01:00
parent c9931123c8
commit cf82fbe6f3
3 changed files with 38 additions and 14 deletions

View File

@@ -86,11 +86,7 @@ public final class TileEntityDeviceBusElement implements INBTSerializable<Compou
}
public void initialize() {
final World world = tileEntity.getWorld();
if (world == null || world.isRemote()) {
return;
}
final World world = Objects.requireNonNull(tileEntity.getWorld());
ServerScheduler.schedule(world, () -> {
if (tileEntity.isRemoved()) {
return;
@@ -149,11 +145,7 @@ public final class TileEntityDeviceBusElement implements INBTSerializable<Compou
}
private void scheduleBusScanInAdjacentBusElements() {
final World world = tileEntity.getWorld();
if (world == null || world.isRemote()) {
return;
}
final World world = Objects.requireNonNull(tileEntity.getWorld());
final BlockPos pos = tileEntity.getPos();
for (final Direction direction : Direction.values()) {
final BlockPos neighborPos = pos.offset(direction);

View File

@@ -3,6 +3,7 @@ package li.cil.oc2.common.tile;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityType;
import net.minecraft.util.Direction;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.util.LazyOptional;
import org.jetbrains.annotations.NotNull;
@@ -22,9 +23,40 @@ public abstract class AbstractTileEntity extends TileEntity {
}
protected void initialize() {
final World world = getWorld();
if (world == null) {
return;
}
if (world.isRemote()) {
initializeClient();
} else {
initializeServer();
}
}
protected void initializeClient() {
}
protected void initializeServer() {
}
protected void dispose() {
final World world = getWorld();
if (world == null) {
return;
}
if (world.isRemote()) {
disposeClient();
} else {
disposeServer();
}
}
protected void disposeClient() {
}
protected void disposeServer() {
}
@NotNull

View File

@@ -23,14 +23,14 @@ public class BusCableTileEntity extends AbstractTileEntity {
}
@Override
public void initialize() {
super.initialize();
protected void initializeServer() {
super.initializeServer();
busElement.initialize();
}
@Override
protected void dispose() {
super.dispose();
protected void disposeServer() {
super.disposeServer();
busElement.dispose();
}