mirror of
https://github.com/GTNewHorizons/ForestryMC.git
synced 2026-11-22 12:59:44 +00:00
Prevent exception from accessing tile entities from ChunkCache
This commit is contained in:
1 parent
0b491f26fc
commit
81dd20b41c
5 files changed
+32
-16
No files matched your search
@@ -51,11 +51,6 @@ public class BlockSapling extends BlockTreeContainer implements IGrowable, IStat
|
||||
/* PROPERTYS */
|
||||
public static final PropertyTree TREE = new PropertyTree("tree");
|
||||
|
||||
@Nullable
|
||||
public static TileSapling getSaplingTile(IBlockAccess world, BlockPos pos) {
|
||||
return TileUtil.getTile(world, pos, TileSapling.class);
|
||||
}
|
||||
|
||||
public BlockSapling() {
|
||||
super(Material.PLANTS);
|
||||
setSoundType(SoundType.PLANT);
|
||||
@@ -104,7 +99,7 @@ public class BlockSapling extends BlockTreeContainer implements IGrowable, IStat
|
||||
|
||||
@Override
|
||||
public IBlockState getActualState(IBlockState state, IBlockAccess world, BlockPos pos) {
|
||||
TileSapling sapling = getSaplingTile(world, pos);
|
||||
TileSapling sapling = TileUtil.getTile(world, pos, TileSapling.class);
|
||||
if (sapling != null && sapling.getTree() != null) {
|
||||
state = state.withProperty(TREE, sapling.getTree().getGenome().getPrimary());
|
||||
} else {
|
||||
@@ -134,7 +129,7 @@ public class BlockSapling extends BlockTreeContainer implements IGrowable, IStat
|
||||
|
||||
/* PLANTING */
|
||||
public static boolean canBlockStay(IBlockAccess world, BlockPos pos) {
|
||||
TileSapling tile = getSaplingTile(world, pos);
|
||||
TileSapling tile = TileUtil.getTile(world, pos, TileSapling.class);
|
||||
if (tile == null) {
|
||||
return false;
|
||||
}
|
||||
@@ -160,7 +155,7 @@ public class BlockSapling extends BlockTreeContainer implements IGrowable, IStat
|
||||
|
||||
@Override
|
||||
public ItemStack getPickBlock(IBlockState state, RayTraceResult target, World world, BlockPos pos, EntityPlayer player) {
|
||||
TileSapling sapling = getSaplingTile(world, pos);
|
||||
TileSapling sapling = TileUtil.getTile(world, pos, TileSapling.class);
|
||||
if (sapling == null || sapling.getTree() == null) {
|
||||
return ItemStack.EMPTY;
|
||||
}
|
||||
@@ -183,7 +178,7 @@ public class BlockSapling extends BlockTreeContainer implements IGrowable, IStat
|
||||
return;
|
||||
}
|
||||
|
||||
TileSapling sapling = getSaplingTile(world, pos);
|
||||
TileSapling sapling = TileUtil.getTile(world, pos, TileSapling.class);
|
||||
if (sapling != null && sapling.getTree() != null) {
|
||||
ItemStack saplingStack = TreeManager.treeRoot.getMemberStack(sapling.getTree(), EnumGermlingType.SAPLING);
|
||||
ItemStackUtil.dropItemStackAsEntity(saplingStack, world, pos);
|
||||
@@ -197,7 +192,7 @@ public class BlockSapling extends BlockTreeContainer implements IGrowable, IStat
|
||||
if (world.rand.nextFloat() >= 0.45F) {
|
||||
return false;
|
||||
}
|
||||
TileSapling saplingTile = getSaplingTile(world, pos);
|
||||
TileSapling saplingTile = TileUtil.getTile(world, pos, TileSapling.class);
|
||||
return saplingTile == null || saplingTile.canAcceptBoneMeal(rand);
|
||||
}
|
||||
|
||||
@@ -208,7 +203,7 @@ public class BlockSapling extends BlockTreeContainer implements IGrowable, IStat
|
||||
|
||||
@Override
|
||||
public void grow(World world, Random rand, BlockPos pos, IBlockState state) {
|
||||
TileSapling saplingTile = getSaplingTile(world, pos);
|
||||
TileSapling saplingTile = TileUtil.getTile(world, pos, TileSapling.class);
|
||||
if (saplingTile != null) {
|
||||
saplingTile.tryGrow(rand, true);
|
||||
}
|
||||
|
||||
@@ -12,14 +12,18 @@ package forestry.core.tiles;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import net.minecraft.block.BlockFlowerPot;
|
||||
import net.minecraft.block.state.IBlockState;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.inventory.IInventory;
|
||||
import net.minecraft.inventory.ISidedInventory;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.ChunkCache;
|
||||
import net.minecraft.world.IBlockAccess;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraft.world.chunk.Chunk;
|
||||
import net.minecraftforge.items.CapabilityItemHandler;
|
||||
import net.minecraftforge.items.IItemHandler;
|
||||
import net.minecraftforge.items.wrapper.InvWrapper;
|
||||
@@ -36,9 +40,22 @@ public abstract class TileUtil {
|
||||
player.getDistanceSq(pos.getX() + 0.5D, pos.getY() + 0.5D, pos.getZ() + 0.5D) <= 64.0D;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the tile of the specified class, returns null if it is the wrong type or does not exist.
|
||||
* Avoids creating new tile entities when using a ChunkCache (off the main thread).
|
||||
* see {@link BlockFlowerPot#getActualState(IBlockState, IBlockAccess, BlockPos)}
|
||||
*/
|
||||
@Nullable
|
||||
public static <T extends TileEntity> T getTile(IBlockAccess world, BlockPos pos, Class<T> tileClass) {
|
||||
TileEntity tileEntity = world.getTileEntity(pos);
|
||||
TileEntity tileEntity;
|
||||
|
||||
if (world instanceof ChunkCache) {
|
||||
ChunkCache chunkCache = (ChunkCache) world;
|
||||
tileEntity = chunkCache.getTileEntity(pos, Chunk.EnumCreateEntityType.CHECK);
|
||||
} else {
|
||||
tileEntity = world.getTileEntity(pos);
|
||||
}
|
||||
|
||||
if (tileClass.isInstance(tileEntity)) {
|
||||
return tileClass.cast(tileEntity);
|
||||
} else {
|
||||
|
||||
@@ -18,6 +18,7 @@ import forestry.api.core.ICamouflagedTile;
|
||||
import forestry.api.multiblock.IMultiblockComponent;
|
||||
import forestry.api.multiblock.IMultiblockController;
|
||||
import forestry.core.network.PacketBufferForestry;
|
||||
import forestry.core.tiles.TileUtil;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
@@ -60,7 +61,7 @@ public class CamouflageUtil {
|
||||
|
||||
@Nullable
|
||||
public static ICamouflageHandler getCamouflageHandler(IBlockAccess world, BlockPos pos) {
|
||||
TileEntity tile = world.getTileEntity(pos);
|
||||
TileEntity tile = TileUtil.getTile(world, pos, TileEntity.class);
|
||||
if (tile instanceof ICamouflagedTile) {
|
||||
ICamouflagedTile block = (ICamouflagedTile) tile;
|
||||
String type = block.getCamouflageType();
|
||||
@@ -81,7 +82,7 @@ public class CamouflageUtil {
|
||||
}
|
||||
|
||||
public static ItemStack getCamouflageBlock(IBlockAccess world, BlockPos pos) {
|
||||
TileEntity tile = world.getTileEntity(pos);
|
||||
TileEntity tile = TileUtil.getTile(world, pos, TileEntity.class);
|
||||
if (tile instanceof ICamouflagedTile) {
|
||||
ICamouflagedTile block = (ICamouflagedTile) tile;
|
||||
String type = block.getCamouflageType();
|
||||
|
||||
@@ -31,6 +31,7 @@ import forestry.core.blocks.properties.UnlistedBlockAccess;
|
||||
import forestry.core.blocks.properties.UnlistedBlockPos;
|
||||
import forestry.core.multiblock.MultiblockTileEntityForestry;
|
||||
import forestry.core.tiles.IActivatable;
|
||||
import forestry.core.tiles.TileUtil;
|
||||
import forestry.core.utils.CamouflageUtil;
|
||||
import forestry.core.utils.ItemStackUtil;
|
||||
import forestry.core.utils.Log;
|
||||
@@ -242,7 +243,7 @@ public abstract class BlockGreenhouse extends BlockStructure implements ISpriteR
|
||||
|
||||
@Override
|
||||
public IBlockState getActualState(IBlockState state, IBlockAccess worldIn, BlockPos pos) {
|
||||
TileEntity tile = worldIn.getTileEntity(pos);
|
||||
TileEntity tile = TileUtil.getTile(worldIn, pos, TileEntity.class);
|
||||
if (tile instanceof IActivatable) {
|
||||
state = state.withProperty(STATE, ((IActivatable) tile).isActive() ? State.ON : State.OFF);
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ import forestry.api.core.IModelBaker;
|
||||
import forestry.core.blocks.properties.UnlistedBlockAccess;
|
||||
import forestry.core.blocks.properties.UnlistedBlockPos;
|
||||
import forestry.core.models.ModelBlockDefault;
|
||||
import forestry.core.tiles.TileUtil;
|
||||
import forestry.core.utils.CamouflageUtil;
|
||||
import forestry.greenhouse.blocks.BlockGreenhouse;
|
||||
import forestry.greenhouse.blocks.BlockGreenhouseType;
|
||||
@@ -27,6 +28,7 @@ import net.minecraft.block.state.IBlockState;
|
||||
import net.minecraft.client.renderer.block.model.IBakedModel;
|
||||
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.IBlockAccess;
|
||||
@@ -75,7 +77,7 @@ public class ModelGreenhouse extends ModelBlockDefault<BlockGreenhouse, ModelGre
|
||||
|
||||
if (!camouflageStack.isEmpty()) {
|
||||
ICamouflageHandler camouflageHandler = CamouflageUtil.getCamouflageHandler(world, pos);
|
||||
ICamouflagedTile camouflageTile = (ICamouflagedTile) world.getTileEntity(pos);
|
||||
ICamouflagedTile camouflageTile = (ICamouflagedTile) TileUtil.getTile(world, pos, TileEntity.class);
|
||||
ICamouflageItemHandler itemHandler = CamouflageManager.camouflageAccess.getHandlerFromItem(camouflageStack);
|
||||
if (itemHandler != null && camouflageHandler != null && camouflageTile != null) {
|
||||
Pair<IBlockState, IBakedModel> model = itemHandler.getModel(camouflageStack, camouflageHandler, camouflageTile);
|
||||
|
||||
Reference in new issue
Block a user