Skip to content

Commit

Permalink
Mostly refactoring, IMovingWorldTileEntity - -> IMovingTile
Browse files Browse the repository at this point in the history
  • Loading branch information
darkevilmac committed Oct 15, 2016
1 parent 6adee01 commit 2323f08
Show file tree
Hide file tree
Showing 11 changed files with 137 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package io.github.elytra.movingworld.api;

import io.github.elytra.movingworld.common.chunk.mobilechunk.MobileChunk;
import io.github.elytra.movingworld.common.entity.EntityMovingWorld;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.common.DimensionManager;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityManager;

/**
* This is pretty much a stub, I might actually use it but now isn't the time.
*/
public class CapabilityMovingTileHandler {

//@CapabilityInject(IMovingTile.class)
//public static Capability<IMovingTile> MOVING_TILE_CAPABILITY = null;

private static void register() {
CapabilityManager.INSTANCE.register(IMovingTile.class, new Capability.IStorage<IMovingTile>() {
@Override
public NBTBase writeNBT(Capability<IMovingTile> capability, IMovingTile instance, EnumFacing side) {
NBTTagCompound compound = new NBTTagCompound();

if (instance.getChunkPos() == null || instance.getParentMovingWorld() == null) {
compound.setBoolean("Empty", true);
} else {
compound.setBoolean("Empty", false);
compound.setLong("ChunkPos", instance.getChunkPos().toLong());
compound.setUniqueId("MovingWorldID", instance.getParentMovingWorld().getPersistentID());
compound.setInteger("Dimension", instance.getParentMovingWorld().getEntityWorld().provider.getDimension());
}

return compound;
}

@Override
public void readNBT(Capability<IMovingTile> capability, IMovingTile instance, EnumFacing side, NBTBase nbt) {
if (nbt != null && nbt instanceof NBTTagCompound) {
NBTTagCompound compound = (NBTTagCompound) nbt;

if (!compound.getBoolean("Empty")) {
instance.setParentMovingWorld((EntityMovingWorld)
DimensionManager.getWorld(compound.getInteger("Dimension"))
.getEntityFromUuid(compound.getUniqueId("MovingWorldID")));
instance.setChunkPos(BlockPos.fromLong(compound.getLong("ChunkPos")));
}
}
}
}, () -> new IMovingTile() {

EntityMovingWorld movingWorld = null;
BlockPos chunkPos;

@Override
public void setParentMovingWorld(EntityMovingWorld movingWorld, BlockPos chunkPos) {
this.movingWorld = movingWorld;
this.chunkPos = chunkPos;
}

@Override
public EntityMovingWorld getParentMovingWorld() {
return movingWorld;
}

@Override
public void setParentMovingWorld(EntityMovingWorld entityMovingWorld) {
this.movingWorld = entityMovingWorld;

}

@Override
public BlockPos getChunkPos() {
return chunkPos;
}

@Override
public void setChunkPos(BlockPos chunkPos) {
this.chunkPos = chunkPos;
}

@Override
public void tick(MobileChunk mobileChunk) {
// No implementation by default.
}
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@
import io.github.elytra.movingworld.common.entity.EntityMovingWorld;
import net.minecraft.util.math.BlockPos;

public interface IMovingWorldTileEntity {
public interface IMovingTile {

void setParentMovingWorld(BlockPos chunkPos, EntityMovingWorld entityMovingWorld);
void setParentMovingWorld(EntityMovingWorld movingWorld, BlockPos chunkPos);

EntityMovingWorld getParentMovingWorld();

void setParentMovingWorld(EntityMovingWorld entityMovingWorld);

BlockPos getChunkPos();

void setChunkPos(BlockPos chunkPos);

/**
* Called each tick from the mobilechunk, I advise strongly against any major modifications to
* the chunk.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.github.elytra.movingworld.common.block;

import io.github.elytra.movingworld.common.tile.TileMovingWorldMarkingBlock;
import io.github.elytra.movingworld.common.tile.TileMovingMarkingBlock;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.PropertyDirection;
Expand All @@ -22,8 +22,8 @@ protected BlockMovingWorldMarker(Material material) {

public static void onPlacedBy(World world, BlockPos pos, IBlockState state, EntityLivingBase entity, ItemStack itemStack) {
if (world != null && !world.isRemote && entity != null && entity instanceof EntityPlayer) {
if (world.getTileEntity(pos) != null && world.getTileEntity(pos) instanceof TileMovingWorldMarkingBlock) {
TileMovingWorldMarkingBlock tile = (TileMovingWorldMarkingBlock) world.getTileEntity(pos);
if (world.getTileEntity(pos) != null && world.getTileEntity(pos) instanceof TileMovingMarkingBlock) {
TileMovingMarkingBlock tile = (TileMovingMarkingBlock) world.getTileEntity(pos);
tile.getInfo().setOwner(((EntityPlayer) entity).getGameProfile().getId());
}
}
Expand All @@ -34,8 +34,8 @@ public void onBlockPlacedBy(World world, BlockPos pos, IBlockState state, Entity
super.onBlockPlacedBy(world, pos, state, entity, itemStack);

if (world != null && !world.isRemote && entity != null && entity instanceof EntityPlayer) {
if (world.getTileEntity(pos) != null && world.getTileEntity(pos) instanceof TileMovingWorldMarkingBlock) {
TileMovingWorldMarkingBlock tile = (TileMovingWorldMarkingBlock) world.getTileEntity(pos);
if (world.getTileEntity(pos) != null && world.getTileEntity(pos) instanceof TileMovingMarkingBlock) {
TileMovingMarkingBlock tile = (TileMovingMarkingBlock) world.getTileEntity(pos);
tile.getInfo().setOwner(((EntityPlayer) entity).getGameProfile().getId());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import io.github.elytra.movingworld.common.block.BlockMovingWorldMarker;
import io.github.elytra.movingworld.common.chunk.assembly.AssembleResult;
import io.github.elytra.movingworld.common.chunk.assembly.CanAssemble;
import io.github.elytra.movingworld.common.tile.TileMovingWorldMarkingBlock;
import io.github.elytra.movingworld.common.tile.TileMovingMarkingBlock;
import io.netty.buffer.ByteBuf;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
Expand Down Expand Up @@ -96,7 +96,7 @@ public boolean isBlockMovingWorldMarker(Block block) {
}

public boolean isTileMovingWorldMarker(TileEntity tile) {
return tile != null && tile instanceof TileMovingWorldMarkingBlock;
return tile != null && tile instanceof TileMovingMarkingBlock;
}

public EnumFacing getFrontDirection(LocatedBlock marker) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import io.github.elytra.movingworld.common.chunk.LocatedBlock;
import io.github.elytra.movingworld.common.chunk.MovingWorldAssemblyInteractor;
import io.github.elytra.movingworld.common.entity.EntityMovingWorld;
import io.github.elytra.movingworld.common.tile.TileMovingWorldMarkingBlock;
import io.github.elytra.movingworld.common.tile.TileMovingMarkingBlock;
import io.github.elytra.movingworld.common.util.LocatedBlockList;
import io.github.elytra.movingworld.common.util.MaterialDensity;
import io.netty.buffer.ByteBuf;
Expand Down Expand Up @@ -133,9 +133,9 @@ public void setWorldBlocksToAir(World world, EntityMovingWorld entityMovingWorld

LocatedBlockList setAirState2 = new LocatedBlockList();

if (movingWorldMarkingBlock != null && movingWorldMarkingBlock.tileEntity != null && movingWorldMarkingBlock.tileEntity instanceof TileMovingWorldMarkingBlock
&& ((TileMovingWorldMarkingBlock) movingWorldMarkingBlock.tileEntity).removedFluidBlocks != null &&
!((TileMovingWorldMarkingBlock) movingWorldMarkingBlock.tileEntity).removedFluidBlocks.isEmpty()) {
if (movingWorldMarkingBlock != null && movingWorldMarkingBlock.tileEntity != null && movingWorldMarkingBlock.tileEntity instanceof TileMovingMarkingBlock
&& ((TileMovingMarkingBlock) movingWorldMarkingBlock.tileEntity).removedFluidBlocks != null &&
!((TileMovingMarkingBlock) movingWorldMarkingBlock.tileEntity).removedFluidBlocks.isEmpty()) {

setFluids = true;
}
Expand Down Expand Up @@ -170,13 +170,13 @@ public void setWorldBlocksToAir(World world, EntityMovingWorld entityMovingWorld
}

if (setFluids) {
for (LocatedBlock fluid : ((TileMovingWorldMarkingBlock) movingWorldMarkingBlock.tileEntity).removedFluidBlocks) {
for (LocatedBlock fluid : ((TileMovingMarkingBlock) movingWorldMarkingBlock.tileEntity).removedFluidBlocks) {
if (fluid != null && world.isAirBlock(fluid.blockPos)) {
world.setBlockState(fluid.blockPos, fluid.blockState, 2);
}
}

for (LocatedBlock fluid : ((TileMovingWorldMarkingBlock) movingWorldMarkingBlock.tileEntity).removedFluidBlocks) {
for (LocatedBlock fluid : ((TileMovingMarkingBlock) movingWorldMarkingBlock.tileEntity).removedFluidBlocks) {
if (fluid != null && world.isAirBlock(fluid.blockPos)) {
world.setBlockState(fluid.blockPos, fluid.blockState, 3);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package io.github.elytra.movingworld.common.chunk.assembly;

import io.github.elytra.movingworld.MovingWorldMod;
import io.github.elytra.movingworld.api.IMovingWorldTileEntity;
import io.github.elytra.movingworld.api.IMovingTile;
import io.github.elytra.movingworld.common.chunk.LocatedBlock;
import io.github.elytra.movingworld.common.chunk.MovingWorldAssemblyInteractor;
import io.github.elytra.movingworld.common.chunk.mobilechunk.MobileChunk;
import io.github.elytra.movingworld.common.entity.EntityMovingWorld;
import io.github.elytra.movingworld.common.event.DisassembleBlockEvent;
import io.github.elytra.movingworld.common.tile.TileMovingWorldMarkingBlock;
import io.github.elytra.movingworld.common.tile.TileMovingMarkingBlock;
import io.github.elytra.movingworld.common.util.*;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
Expand All @@ -26,7 +26,7 @@ public class ChunkDisassembler {

private AssembleResult result;
private LocatedBlockList removedFluidBlocks;
private TileMovingWorldMarkingBlock tileMarker;
private TileMovingMarkingBlock tileMarker;

public ChunkDisassembler(EntityMovingWorld EntityMovingWorld) {
movingWorld = EntityMovingWorld;
Expand Down Expand Up @@ -77,8 +77,8 @@ public boolean canDisassemble(MovingWorldAssemblyInteractor assemblyInteractor)

public AssembleResult doDisassemble(MovingWorldAssemblyInteractor assemblyInteractor) {
tileMarker = null;
if (movingWorld.getMobileChunk().marker != null && movingWorld.getMobileChunk().marker.tileEntity != null && movingWorld.getMobileChunk().marker.tileEntity instanceof TileMovingWorldMarkingBlock)
tileMarker = (TileMovingWorldMarkingBlock) movingWorld.getMobileChunk().marker.tileEntity;
if (movingWorld.getMobileChunk().marker != null && movingWorld.getMobileChunk().marker.tileEntity != null && movingWorld.getMobileChunk().marker.tileEntity instanceof TileMovingMarkingBlock)
tileMarker = (TileMovingMarkingBlock) movingWorld.getMobileChunk().marker.tileEntity;

removedFluidBlocks = new LocatedBlockList();
World world = movingWorld.getEntityWorld();
Expand Down Expand Up @@ -213,8 +213,8 @@ LocatedBlockList processLocatedBlockList(World world, LocatedBlockList locatedBl
}
if (tileentity != null) {
tileentity.setPos(pos);
if (tileentity instanceof IMovingWorldTileEntity) {
((IMovingWorldTileEntity) tileentity).setParentMovingWorld(new BlockPos(i, j, k), null);
if (tileentity instanceof IMovingTile) {
((IMovingTile) tileentity).setParentMovingWorld(null, new BlockPos(i, j, k));
}
NBTTagCompound tileTag = new NBTTagCompound();
tileentity.writeToNBT(tileTag);
Expand All @@ -225,7 +225,7 @@ LocatedBlockList processLocatedBlockList(World world, LocatedBlockList locatedBl
tileentity = world.getTileEntity(pos);

if (tileMarker != null && tileMarker.getPos().equals(tileentity.getPos())) {
tileMarker = (TileMovingWorldMarkingBlock) tileentity;
tileMarker = (TileMovingMarkingBlock) tileentity;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.google.common.collect.HashBiMap;
import io.github.elytra.movingworld.MovingWorldMod;
import io.github.elytra.movingworld.api.IMovingWorldTileEntity;
import io.github.elytra.movingworld.api.IMovingTile;
import io.github.elytra.movingworld.common.chunk.LocatedBlock;
import io.github.elytra.movingworld.common.chunk.mobilechunk.world.FakeWorld;
import io.github.elytra.movingworld.common.entity.EntityMovingWorld;
Expand Down Expand Up @@ -43,7 +43,7 @@ public abstract class MobileChunk implements IBlockAccess {
public boolean isChunkLoaded;
public boolean isModified;
public LocatedBlock marker;
public ArrayList<IMovingWorldTileEntity> movingWorldTileEntities;
public ArrayList<IMovingTile> movingWorldTileEntities;
private Map<BlockPos, ExtendedBlockStorage> blockStorageMap;
private boolean boundsInit;
private BlockPos minBounds;
Expand All @@ -62,7 +62,7 @@ public MobileChunk(World world, EntityMovingWorld entitymovingWorld) {
updatableTiles = new ArrayList<>();
boundingBoxes = HashBiMap.create();
chunkBoundingBoxes = HashBiMap.create();
movingWorldTileEntities = new ArrayList<IMovingWorldTileEntity>();
movingWorldTileEntities = new ArrayList<IMovingTile>();
marker = null;

isChunkLoaded = false;
Expand Down Expand Up @@ -502,10 +502,10 @@ private void setChunkBlockTileEntity(BlockPos pos, TileEntity tileentity) {
tileentity.invalidate();
chunkTileEntityMap.put(chunkPosition, tileentity);

if (tileentity instanceof IMovingWorldTileEntity) {
if (tileentity instanceof IMovingTile) {
if (!movingWorldTileEntities.contains(tileentity))
movingWorldTileEntities.add((IMovingWorldTileEntity) tileentity);
((IMovingWorldTileEntity) tileentity).setParentMovingWorld(chunkPosition, entityMovingWorld);
movingWorldTileEntities.add((IMovingTile) tileentity);
((IMovingTile) tileentity).setParentMovingWorld(entityMovingWorld, chunkPosition);
} else if (tileentity instanceof ITickable && MovingWorldMod.instance.getNetworkConfig().isTileUpdatable(tileentity.getClass())) {
updatableTiles.add(tileentity);
}
Expand All @@ -527,10 +527,10 @@ public void removeChunkBlockTileEntity(BlockPos pos) {
if (isChunkLoaded) {
TileEntity tileentity = chunkTileEntityMap.remove(chunkPosition);
if (tileentity != null) {
if (tileentity instanceof IMovingWorldTileEntity) {
if (tileentity instanceof IMovingTile) {
if (!movingWorldTileEntities.contains(tileentity))
movingWorldTileEntities.add((IMovingWorldTileEntity) tileentity);
((IMovingWorldTileEntity) tileentity).setParentMovingWorld(pos, null);
movingWorldTileEntities.add((IMovingTile) tileentity);
((IMovingTile) tileentity).setParentMovingWorld(null, pos);
}
if (tileentity instanceof ITickable && MovingWorldMod.instance.getNetworkConfig().isTileUpdatable(tileentity.getClass())) {
updatableTiles.remove(tileentity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.google.common.collect.Lists;
import io.github.elytra.movingworld.MovingWorldMod;
import io.github.elytra.movingworld.api.IMovingWorldTileEntity;
import io.github.elytra.movingworld.api.IMovingTile;
import io.github.elytra.movingworld.common.chunk.ChunkIO;
import io.github.elytra.movingworld.common.chunk.LocatedBlock;
import io.github.elytra.movingworld.common.chunk.MovingWorldAssemblyInteractor;
Expand Down Expand Up @@ -53,9 +53,8 @@
*/
public abstract class EntityMovingWorld extends EntityBoat implements IEntityAdditionalSpawnData {

private static final DataParameter<Integer> THIRTY = EntityDataManager.<Integer>createKey(EntityMovingWorld.class, DataSerializers.VARINT);
public static final DataParameter<Boolean> IS_FLYING = EntityDataManager.createKey(EntityMovingWorld.class, DataSerializers.BOOLEAN);

private static final DataParameter<Integer> THIRTY = EntityDataManager.<Integer>createKey(EntityMovingWorld.class, DataSerializers.VARINT);
public EntityPlayer controllingPassenger;

public float motionYaw;
Expand Down Expand Up @@ -387,7 +386,7 @@ protected void handleClientUpdate() {
protected void handleServerUpdate(double horvel) {
if (getMobileChunk() != null) {
if (!getMobileChunk().movingWorldTileEntities.isEmpty())
for (IMovingWorldTileEntity movingWorldTileEntity : getMobileChunk().movingWorldTileEntities) {
for (IMovingTile movingWorldTileEntity : getMobileChunk().movingWorldTileEntities) {
movingWorldTileEntity.tick(getMobileChunk());
}
if (!getMobileChunk().updatableTiles.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import io.github.elytra.movingworld.common.chunk.ChunkIO;
import io.github.elytra.movingworld.common.chunk.mobilechunk.MobileChunkServer;
import io.github.elytra.movingworld.common.network.MovingWorldNetworking;
import io.github.elytra.movingworld.common.tile.TileMovingWorldMarkingBlock;
import io.github.elytra.movingworld.common.tile.TileMovingMarkingBlock;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
Expand Down Expand Up @@ -63,8 +63,8 @@ public void onChunkUpdate() {
continue;

TileEntity te = getMobileChunkServer().getTileEntity(tilePosition);
if (te instanceof TileMovingWorldMarkingBlock) {
((TileMovingWorldMarkingBlock) te).writeNBTForSending(nbt);
if (te instanceof TileMovingMarkingBlock) {
((TileMovingMarkingBlock) te).writeNBTForSending(nbt);
} else {
te.writeToNBT(nbt);
}
Expand Down
Loading

0 comments on commit 2323f08

Please sign in to comment.