Skip to content

Commit

Permalink
Move the block's associated TileEntity on block move properly
Browse files Browse the repository at this point in the history
  • Loading branch information
ExE-Boss committed May 28, 2017
1 parent 037ccaf commit a7ad118
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import nova.core.util.shape.Cuboid;
import nova.core.world.World;
import nova.core.wrapper.mc.forge.v1_11_2.util.WrapperEvent;
import nova.core.wrapper.mc.forge.v1_11_2.wrapper.VectorConverter;
import nova.core.wrapper.mc.forge.v1_11_2.wrapper.block.world.WorldConverter;
import nova.core.wrapper.mc.forge.v1_11_2.wrapper.cuboid.CuboidConverter;
import nova.core.wrapper.mc.forge.v1_11_2.wrapper.data.DataConverter;
Expand Down Expand Up @@ -124,7 +125,7 @@ public BWBlock(IBlockState blockState, World world, Vector3D pos) {
true
);
return aabbs.stream()
.map(aabb -> (Cuboid) Game.natives().toNova(aabb))
.map(CuboidConverter.instance()::toNova)
.map(cuboid -> cuboid.subtract(pos))
.collect(Collectors.toSet());
}).setSelectionBoxes(entity -> {
Expand Down Expand Up @@ -205,7 +206,7 @@ public int meta() {
}

public BlockPos blockPos() {
return new BlockPos(x(), y(), z());
return VectorConverter.instance().toNative(position());
}

public IBlockAccess blockAccess() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,14 @@ public BWBlockFactory(Block block) {
public Block getBlock() {
return block;
}

@Override
public String getLocalizedName() {
return getBlock().getLocalizedName();
}

@Override
public String getUnlocalizedName() {
return getBlock().getUnlocalizedName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package nova.core.wrapper.mc.forge.v1_11_2.wrapper.block.backward;

import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
Expand Down Expand Up @@ -70,14 +71,20 @@ public void setWorld(World world) {
net.minecraft.world.World oldWorld = Game.natives().toNative(this.world);
net.minecraft.world.World newWorld = Game.natives().toNative(world);
Optional<TileEntity> tileEntity = Optional.ofNullable(oldWorld.getTileEntity(pos));
newWorld.setBlockState(pos, block.blockState());
Optional<NBTTagCompound> nbt = Optional.empty();
if (tileEntity.isPresent()) {
newWorld.setTileEntity(pos, tileEntity.get());
tileEntity.get().setWorld(newWorld);
} else {
newWorld.setTileEntity(pos, null);
NBTTagCompound compound = new NBTTagCompound();
tileEntity.get().writeToNBT(compound);
nbt = Optional.of(compound);
}
newWorld.setBlockState(pos, block.blockState());
oldWorld.removeTileEntity(pos);
oldWorld.setBlockToAir(pos);
Optional<TileEntity> newTileEntity = Optional.ofNullable(newWorld.getTileEntity(pos));
if (newTileEntity.isPresent() && nbt.isPresent()) {
newTileEntity.get().readFromNBT(nbt.get());
}
this.world = world;
}

@Override
Expand All @@ -86,13 +93,22 @@ public void setPosition(Vector3D position) {
BlockPos newPos = VectorConverter.instance().toNative(position);
net.minecraft.world.World world = Game.natives().toNative(this.world);
Optional<TileEntity> tileEntity = Optional.ofNullable(blockAccess().getTileEntity(oldPos));
world.setBlockState(newPos, block.blockState());
Optional<NBTTagCompound> nbt = Optional.empty();
if (tileEntity.isPresent()) {
world.setTileEntity(newPos, tileEntity.get());
tileEntity.get().setPos(newPos);
} else {
world.setTileEntity(newPos, null);
NBTTagCompound compound = new NBTTagCompound();
tileEntity.get().writeToNBT(compound);
compound.setInteger("x", newPos.getX());
compound.setInteger("y", newPos.getY());
compound.setInteger("z", newPos.getZ());
nbt = Optional.of(compound);
}
world.setBlockState(newPos, block.blockState());
world.removeTileEntity(oldPos);
world.setBlockToAir(oldPos);
Optional<TileEntity> newTileEntity = Optional.ofNullable(blockAccess().getTileEntity(newPos));
if (newTileEntity.isPresent() && nbt.isPresent()) {
newTileEntity.get().readFromNBT(nbt.get());
}
this.position = position;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@

package nova.core.wrapper.mc.forge.v1_11_2.wrapper.block.forward;

import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
import nova.core.block.Block;
import nova.core.component.transform.BlockTransform;
import nova.core.retention.Data;
import nova.core.retention.Storable;
import nova.core.world.World;
import nova.core.wrapper.mc.forge.v1_11_2.wrapper.VectorConverter;
import nova.core.wrapper.mc.forge.v1_11_2.wrapper.block.world.WorldConverter;
import nova.internal.core.Game;
import org.apache.commons.math3.geometry.euclidean.threed.Vector3D;

import java.util.Optional;
Expand All @@ -38,9 +38,9 @@
*/
public class FWBlockTransform extends BlockTransform {

public final Block block;
public final World world;
public final Vector3D position;
private Block block;
private World world;
private Vector3D position;

public FWBlockTransform(Block block, World world, Vector3D position) {
this.block = block;
Expand Down Expand Up @@ -68,28 +68,39 @@ public IBlockAccess blockAccess() {

@Override
public void setWorld(World world) {
BlockPos pos = blockPos();
Optional<TileEntity> tileEntity = Optional.ofNullable(blockAccess().getTileEntity(pos));
world.setBlock(position, block.getFactory());
tileEntity.ifPresent(te -> {
net.minecraft.world.World newWorld = Game.natives().toNative(world);
newWorld.setTileEntity(pos, te);
te.setWorld(newWorld);
});
Optional<Data> data = Optional.empty();
if (block instanceof Storable) {
data = Optional.of(new Data());
((Storable) block).save(data.get());
}
this.world.removeBlock(position);
Optional<Block> newBlock = world.getBlock(position);
if (newBlock.isPresent()) {
block = newBlock.get();
if (newBlock.get() instanceof Storable && data.isPresent()) {
((Storable) newBlock.get()).load(data.get());
}
}
this.world = world;
}

@Override
public void setPosition(Vector3D position) {
BlockPos oldPos = blockPos();
BlockPos newPos = VectorConverter.instance().toNative(position);
Optional<TileEntity> tileEntity = Optional.ofNullable(blockAccess().getTileEntity(oldPos));
world.setBlock(position, block.getFactory());
tileEntity.ifPresent(te -> {
net.minecraft.world.World world = Game.natives().toNative(this.world);
world.setTileEntity(newPos, te);
te.setPos(newPos);
});
Optional<Data> data = Optional.empty();
if (block instanceof Storable) {
data = Optional.of(new Data());
((Storable) block).save(data.get());
}
world.removeBlock(this.position);
Optional<Block> newBlock = world.getBlock(position);
if (newBlock.isPresent()) {
block = newBlock.get();
if (newBlock.get() instanceof Storable && data.isPresent()) {
((Storable) newBlock.get()).load(data.get());
}
}
this.position = position;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ public Block getBlock() {
}

public void setBlock(Block block) {
if (block.components.has(TEBlockTransform.class))
block.components.remove(TEBlockTransform.class);
block.components.getOrAdd(new TEBlockTransform(this));
this.block = block;
}

Expand All @@ -84,6 +87,8 @@ public SPacketUpdateTileEntity getUpdatePacket() {
@Override
public void validate() {
super.validate();
if (block.components.has(TEBlockTransform.class))
block.components.remove(TEBlockTransform.class);
block.components.getOrAdd(new TEBlockTransform(this));

if (cacheData != null && block instanceof Storable) {
Expand Down Expand Up @@ -179,6 +184,7 @@ private FWPacketUpdateTileEntity(Packet<T> packet, BlockPos blockPosIn, int meta
}

@Override
@SuppressWarnings("unchecked")
public void processPacket(INetHandlerPlayClient handler) {
super.processPacket(handler);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,14 @@
package nova.core.wrapper.mc.forge.v1_11_2.wrapper.block.forward;

import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import nova.core.block.Block;
import nova.core.block.BlockFactory;
import nova.core.component.Updater;
import nova.core.component.fluid.SidedTankProvider;
import nova.core.util.Direction;
import nova.core.wrapper.mc.forge.v1_11_2.asm.lib.ComponentInjector;
import nova.core.wrapper.mc.forge.v1_11_2.util.WrapperEvent;
import nova.internal.core.Game;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;

/**
* @author Vic Nightfall
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraft.tileentity.TileEntity;
import nova.core.block.Block;
import nova.core.component.renderer.DynamicRenderer;
import nova.core.wrapper.mc.forge.v1_11_2.render.RenderUtility;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@
package nova.core.wrapper.mc.forge.v1_11_2.wrapper.block.forward;

import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
import nova.core.block.Block;
import nova.core.component.transform.BlockTransform;
import nova.core.world.World;
import nova.core.wrapper.mc.forge.v1_11_2.wrapper.VectorConverter;
import nova.core.wrapper.mc.forge.v1_11_2.wrapper.block.world.WorldConverter;
import nova.internal.core.Game;
import org.apache.commons.math3.geometry.euclidean.threed.Vector3D;

import java.util.Objects;

/**
* @author ExE Boss
*/
Expand All @@ -38,6 +41,10 @@ public TEBlockTransform(FWTile tileEntity) {
this.tileEntity = tileEntity;
}

public Block block() {
return tileEntity.block;
}

@Override
public Vector3D position() {
return VectorConverter.instance().toNova(tileEntity.getPos());
Expand All @@ -48,28 +55,25 @@ public World world() {
return WorldConverter.instance().toNova(tileEntity.getWorld());
}

public net.minecraft.world.World mcWorld() {
public BlockPos blockPos() {
return tileEntity.getPos();
}

public IBlockAccess blockAccess() {
return tileEntity.getWorld();
}

@Override
public void setWorld(nova.core.world.World world) {
nova.core.world.World originalWorld = world();
Vector3D originalPosition = position();
world.setBlock(position(), tileEntity.block.getFactory());
net.minecraft.world.World mcWorld = Game.natives().toNative(world);
mcWorld.setTileEntity(tileEntity.getPos(), tileEntity);
tileEntity.setWorld(mcWorld);
originalWorld.removeBlock(originalPosition);
public void setWorld(World world) {
world().setBlock(position(), tileEntity.block.getFactory());
world().removeBlock(position());
Objects.requireNonNull((FWTile) WorldConverter.instance().toNative(world).getTileEntity(blockPos())).setBlock(tileEntity.block);
}

@Override
public void setPosition(Vector3D position) {
Vector3D originalPosition = position();
world().setBlock(position, tileEntity.block.getFactory());
BlockPos newPos = VectorConverter.instance().toNative(position);
mcWorld().setTileEntity(newPos, tileEntity);
tileEntity.setPos(newPos);
world().removeBlock(originalPosition);
world().removeBlock(position());
Objects.requireNonNull((FWTile) blockAccess().getTileEntity(VectorConverter.instance().toNative(position))).setBlock(tileEntity.block);
}
}

0 comments on commit a7ad118

Please sign in to comment.