Skip to content

Commit

Permalink
Update adapters.
Browse files Browse the repository at this point in the history
Don't update unchanged blocks, do change NBT, no need to light.

Also clean up the forge side a bit.
  • Loading branch information
wizjany committed Mar 16, 2019
1 parent c885f70 commit 678a78a
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 82 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Expand Up @@ -166,21 +166,19 @@ public <B extends BlockStateHolder<B>> boolean setBlock(BlockVector3 position, B
boolean successful = successState != null;

// Create the TileEntity
if (successful) {
if (block instanceof BaseBlock && ((BaseBlock) block).hasNbtData()) {
// Kill the old TileEntity
world.removeTileEntity(pos);
NBTTagCompound nativeTag = NBTConverter.toNative(((BaseBlock) block).getNbtData());
nativeTag.putString("id", ((BaseBlock) block).getNbtId());
TileEntityUtils.setTileEntity(world, position, nativeTag);
if (successful || old == newState) {
if (block instanceof BaseBlock) {
CompoundTag tag = ((BaseBlock) block).getNbtData();
if (tag != null) {
NBTTagCompound nativeTag = NBTConverter.toNative(tag);
nativeTag.putString("id", ((BaseBlock) block).getNbtId());
TileEntityUtils.setTileEntity(world, position, nativeTag);
}
}
}

if (notifyAndLight) {
if (!successful) {
newState = old;
}
world.checkLight(pos);
if (successful && notifyAndLight) {
//world.checkLight(pos);
world.markAndNotifyBlock(pos, chunk, old, newState, UPDATE | NOTIFY);
}

Expand Down
Expand Up @@ -29,8 +29,6 @@
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

import java.lang.reflect.Constructor;

import javax.annotation.Nullable;

/**
Expand All @@ -46,45 +44,14 @@ private TileEntityUtils() {
*
* @param tag the tag
* @param position the position
* @return a tag compound
*/
private static NBTTagCompound updateForSet(NBTTagCompound tag, BlockVector3 position) {
private static void updateForSet(NBTTagCompound tag, BlockVector3 position) {
checkNotNull(tag);
checkNotNull(position);

tag.put("x", new NBTTagInt(position.getBlockX()));
tag.put("y", new NBTTagInt(position.getBlockY()));
tag.put("z", new NBTTagInt(position.getBlockZ()));

return tag;
}

/**
* Set a tile entity at the given location.
*
* @param world the world
* @param position the position
* @param clazz the tile entity class
* @param tag the tag for the tile entity (may be null to not set NBT data)
*/
static void setTileEntity(World world, BlockVector3 position, Class<? extends TileEntity> clazz, @Nullable NBTTagCompound tag) {
checkNotNull(world);
checkNotNull(position);
checkNotNull(clazz);

TileEntity tileEntity = constructTileEntity(world, position, clazz);

if (tileEntity == null) {
return;
}

if (tag != null) {
// Set X, Y, Z
updateForSet(tag, position);
tileEntity.read(tag);
}

world.setTileEntity(new BlockPos(position.getBlockX(), position.getBlockY(), position.getBlockZ()), tileEntity);
}

/**
Expand All @@ -105,42 +72,6 @@ static void setTileEntity(World world, BlockVector3 position, @Nullable NBTTagCo
}
}

/**
* Construct a tile entity from the given class.
*
* @param world the world
* @param position the position
* @param clazz the class
* @return a tile entity (may be null if it failed)
*/
@Nullable
static TileEntity constructTileEntity(World world, BlockVector3 position, Class<? extends TileEntity> clazz) {
Constructor<? extends TileEntity> baseConstructor;
try {
baseConstructor = clazz.getConstructor(); // creates "blank" TE
} catch (Throwable e) {
return null; // every TE *should* have this constructor, so this isn't necessary
}

TileEntity genericTE;
try {
genericTE = baseConstructor.newInstance();
} catch (Throwable e) {
return null;
}

/*
genericTE.blockType = Block.blocksList[block.getId()];
genericTE.blockMetadata = block.getData();
genericTE.xCoord = pt.getBlockX();
genericTE.yCoord = pt.getBlockY();
genericTE.zCoord = pt.getBlockZ();
genericTE.worldObj = world;
*/ // handled by internal code

return genericTE;
}

public static NBTTagCompound copyNbtData(TileEntity tile) {
NBTTagCompound tag = new NBTTagCompound();
tile.write(tag);
Expand Down

0 comments on commit 678a78a

Please sign in to comment.