ITE refactor

This commit is contained in:
simibubi 2021-06-09 23:48:34 +02:00
parent 2852756a68
commit 1f48c698e8
13 changed files with 179 additions and 288 deletions

View file

@ -92,34 +92,29 @@ public class CrushingWheelBlock extends RotatedPillarKineticBlock implements ITE
if (AllBlocks.CRUSHING_WHEEL.has(otherState)) { if (AllBlocks.CRUSHING_WHEEL.has(otherState)) {
controllerShouldExist = true; controllerShouldExist = true;
try { CrushingWheelTileEntity te = getTileEntity(world, pos);
CrushingWheelTileEntity te = getTileEntity(world, pos); CrushingWheelTileEntity otherTe = getTileEntity(world, otherWheelPos);
CrushingWheelTileEntity otherTe = getTileEntity(world, otherWheelPos);
if (te != null && otherTe != null && (te.getSpeed() > 0) != (otherTe.getSpeed() > 0) if (te != null && otherTe != null && (te.getSpeed() > 0) != (otherTe.getSpeed() > 0)
&& te.getSpeed() != 0) { && te.getSpeed() != 0) {
Axis wheelAxis = state.get(AXIS); Axis wheelAxis = state.get(AXIS);
Axis sideAxis = side.getAxis(); Axis sideAxis = side.getAxis();
int controllerADO = Math.round(Math.signum(te.getSpeed())) * side.getAxisDirection().getOffset(); int controllerADO = Math.round(Math.signum(te.getSpeed())) * side.getAxisDirection().getOffset();
Vector3d controllerDirVec = new Vector3d(wheelAxis == Axis.X ? 1 : 0 Vector3d controllerDirVec = new Vector3d(wheelAxis == Axis.X ? 1 : 0
, wheelAxis == Axis.Y ? 1 : 0 , wheelAxis == Axis.Y ? 1 : 0
, wheelAxis == Axis.Z ? 1 : 0) , wheelAxis == Axis.Z ? 1 : 0)
.crossProduct(new Vector3d(sideAxis == Axis.X ? 1 : 0 .crossProduct(new Vector3d(sideAxis == Axis.X ? 1 : 0
, sideAxis == Axis.Y ? 1 : 0 , sideAxis == Axis.Y ? 1 : 0
, sideAxis == Axis.Z ? 1 : 0)); , sideAxis == Axis.Z ? 1 : 0));
controllerNewDirection = Direction.getFacingFromVector(controllerDirVec.x * controllerADO controllerNewDirection = Direction.getFacingFromVector(controllerDirVec.x * controllerADO
, controllerDirVec.y * controllerADO , controllerDirVec.y * controllerADO
, controllerDirVec.z * controllerADO); , controllerDirVec.z * controllerADO);
controllerShouldBeValid = true; controllerShouldBeValid = true;
}
if (otherState.get(AXIS) != state.get(AXIS))
controllerShouldExist = false;
} catch (TileEntityException e) {
controllerShouldExist = false;
} }
if (otherState.get(AXIS) != state.get(AXIS))
controllerShouldExist = false;
} }
if (!controllerShouldExist) { if (!controllerShouldExist) {
@ -149,27 +144,25 @@ public class CrushingWheelBlock extends RotatedPillarKineticBlock implements ITE
@Override @Override
public void onEntityCollision(BlockState state, World worldIn, BlockPos pos, Entity entityIn) { public void onEntityCollision(BlockState state, World worldIn, BlockPos pos, Entity entityIn) {
try { if (entityIn.getY() < pos.getY() + 1.25f || !entityIn.isOnGround())
CrushingWheelTileEntity te = getTileEntity(worldIn, pos); return;
if (entityIn.getY() < pos.getY() + 1.25f || !entityIn.isOnGround())
return; float speed = getTileEntityOptional(worldIn, pos).map(CrushingWheelTileEntity::getSpeed)
.orElse(0f);
double x = 0; double x = 0;
double z = 0; double z = 0;
if (state.get(AXIS) == Axis.X) { if (state.get(AXIS) == Axis.X) {
z = te.getSpeed() / 20f; z = speed / 20f;
x += (pos.getX() + .5f - entityIn.getX()) * .1f; x += (pos.getX() + .5f - entityIn.getX()) * .1f;
}
if (state.get(AXIS) == Axis.Z) {
x = te.getSpeed() / -20f;
z += (pos.getZ() + .5f - entityIn.getZ()) * .1f;
}
entityIn.setMotion(entityIn.getMotion()
.add(x, 0, z));
} catch (TileEntityException e) {
} }
if (state.get(AXIS) == Axis.Z) {
x = speed / -20f;
z += (pos.getZ() + .5f - entityIn.getZ()) * .1f;
}
entityIn.setMotion(entityIn.getMotion()
.add(x, 0, z));
} }
@Override @Override

View file

@ -91,28 +91,28 @@ public class CrushingWheelControllerBlock extends DirectionalBlock
}); });
} }
public void checkEntityForProcessing(World worldIn, BlockPos pos, Entity entityIn){ public void checkEntityForProcessing(World worldIn, BlockPos pos, Entity entityIn) {
try { CrushingWheelControllerTileEntity te = getTileEntity(worldIn, pos);
CrushingWheelControllerTileEntity te = getTileEntity(worldIn, pos); if (te == null)
if (te.crushingspeed == 0) return;
return; if (te.crushingspeed == 0)
if (entityIn instanceof ItemEntity) return;
((ItemEntity) entityIn).setPickupDelay(10); if (entityIn instanceof ItemEntity)
CompoundNBT data = entityIn.getPersistentData(); ((ItemEntity) entityIn).setPickupDelay(10);
if (data.contains("BypassCrushingWheel")) { CompoundNBT data = entityIn.getPersistentData();
if (pos.equals(NBTUtil.readBlockPos(data.getCompound("BypassCrushingWheel")))) if (data.contains("BypassCrushingWheel")) {
return; if (pos.equals(NBTUtil.readBlockPos(data.getCompound("BypassCrushingWheel"))))
}
if (te.isOccupied())
return;
boolean isPlayer = entityIn instanceof PlayerEntity;
if (isPlayer && ((PlayerEntity) entityIn).isCreative())
return;
if (isPlayer && entityIn.world.getDifficulty() == Difficulty.PEACEFUL)
return; return;
}
if (te.isOccupied())
return;
boolean isPlayer = entityIn instanceof PlayerEntity;
if (isPlayer && ((PlayerEntity) entityIn).isCreative())
return;
if (isPlayer && entityIn.world.getDifficulty() == Difficulty.PEACEFUL)
return;
te.startCrushing(entityIn); te.startCrushing(entityIn);
} catch (TileEntityException e) {}
} }
@Override @Override
@ -166,27 +166,27 @@ public class CrushingWheelControllerBlock extends DirectionalBlock
@Override @Override
public VoxelShape getCollisionShape(BlockState state, IBlockReader worldIn, BlockPos pos, public VoxelShape getCollisionShape(BlockState state, IBlockReader worldIn, BlockPos pos,
ISelectionContext context) { ISelectionContext context) {
VoxelShape standardShape = AllShapes.CRUSHING_WHEEL_CONTROLLER_COLLISION.get(state.get(FACING));
if (!state.get(VALID)) if (!state.get(VALID))
return AllShapes.CRUSHING_WHEEL_CONTROLLER_COLLISION.get(state.get(FACING)); return standardShape;
Entity entity = context.getEntity(); Entity entity = context.getEntity();
if (entity != null) { if (entity == null)
return standardShape;
CompoundNBT data = entity.getPersistentData(); CompoundNBT data = entity.getPersistentData();
if (data.contains("BypassCrushingWheel")) { if (data.contains("BypassCrushingWheel"))
if (pos.equals(NBTUtil.readBlockPos(data.getCompound("BypassCrushingWheel")))) if (pos.equals(NBTUtil.readBlockPos(data.getCompound("BypassCrushingWheel"))))
if (state.get(FACING) != Direction.UP) //Allow output items to land on top of the block rather than falling back through. if (state.get(FACING) != Direction.UP) // Allow output items to land on top of the block rather than falling back through.
return VoxelShapes.empty();
}
try {
CrushingWheelControllerTileEntity te = getTileEntity(worldIn, pos);
if (te.processingEntity == entity)
return VoxelShapes.empty(); return VoxelShapes.empty();
} catch (TileEntityException e) {}
} CrushingWheelControllerTileEntity te = getTileEntity(worldIn, pos);
return AllShapes.CRUSHING_WHEEL_CONTROLLER_COLLISION.get(state.get(FACING)); if (te != null && te.processingEntity == entity)
return VoxelShapes.empty();
return standardShape;
} }
@Override @Override

View file

@ -99,13 +99,10 @@ public class MillstoneBlock extends KineticBlock implements ITE<MillstoneTileEnt
return; return;
MillstoneTileEntity millstone = null; MillstoneTileEntity millstone = null;
for (BlockPos pos : Iterate.hereAndBelow(entityIn.getBlockPos())) { for (BlockPos pos : Iterate.hereAndBelow(entityIn.getBlockPos()))
try { if (millstone == null)
millstone = getTileEntity(worldIn, pos); millstone = getTileEntity(worldIn, pos);
} catch (TileEntityException e) {
continue;
}
}
if (millstone == null) if (millstone == null)
return; return;

View file

@ -36,8 +36,7 @@ public class ItemDrainBlock extends Block implements IWrenchable, ITE<ItemDrainT
BlockRayTraceResult hit) { BlockRayTraceResult hit) {
ItemStack heldItem = player.getHeldItem(handIn); ItemStack heldItem = player.getHeldItem(handIn);
try { return onTileEntityUse(worldIn, pos, te -> {
ItemDrainTileEntity te = getTileEntity(worldIn, pos);
if (!heldItem.isEmpty()) { if (!heldItem.isEmpty()) {
te.internalTank.allowInsertion(); te.internalTank.allowInsertion();
ActionResultType tryExchange = tryExchange(worldIn, player, handIn, heldItem, te); ActionResultType tryExchange = tryExchange(worldIn, player, handIn, heldItem, te);
@ -53,10 +52,7 @@ public class ItemDrainBlock extends Block implements IWrenchable, ITE<ItemDrainT
te.notifyUpdate(); te.notifyUpdate();
} }
return ActionResultType.SUCCESS; return ActionResultType.SUCCESS;
} catch (TileEntityException e) { });
}
return ActionResultType.PASS;
} }
protected ActionResultType tryExchange(World worldIn, PlayerEntity player, Hand handIn, ItemStack heldItem, protected ActionResultType tryExchange(World worldIn, PlayerEntity player, Hand handIn, ItemStack heldItem,

View file

@ -92,8 +92,7 @@ public class BasinBlock extends Block implements ITE<BasinTileEntity>, IWrenchab
BlockRayTraceResult hit) { BlockRayTraceResult hit) {
ItemStack heldItem = player.getHeldItem(handIn); ItemStack heldItem = player.getHeldItem(handIn);
try { return onTileEntityUse(worldIn, pos, te -> {
BasinTileEntity te = getTileEntity(worldIn, pos);
if (!heldItem.isEmpty()) { if (!heldItem.isEmpty()) {
if (FluidHelper.tryEmptyItemIntoTE(worldIn, player, handIn, heldItem, te)) if (FluidHelper.tryEmptyItemIntoTE(worldIn, player, handIn, heldItem, te))
return ActionResultType.SUCCESS; return ActionResultType.SUCCESS;
@ -128,10 +127,8 @@ public class BasinBlock extends Block implements ITE<BasinTileEntity>, IWrenchab
worldIn.playSound(null, pos, SoundEvents.ENTITY_ITEM_PICKUP, SoundCategory.PLAYERS, .2f, worldIn.playSound(null, pos, SoundEvents.ENTITY_ITEM_PICKUP, SoundCategory.PLAYERS, .2f,
1f + Create.RANDOM.nextFloat()); 1f + Create.RANDOM.nextFloat());
te.onEmptied(); te.onEmptied();
} catch (TileEntityException e) { return ActionResultType.SUCCESS;
} });
return ActionResultType.SUCCESS;
} }
@Override @Override

View file

@ -102,11 +102,8 @@ public class BeltBlock extends HorizontalKineticBlock implements ITE<BeltTileEnt
public boolean hasShaftTowards(IWorldReader world, BlockPos pos, BlockState state, Direction face) { public boolean hasShaftTowards(IWorldReader world, BlockPos pos, BlockState state, Direction face) {
if (face.getAxis() != getRotationAxis(state)) if (face.getAxis() != getRotationAxis(state))
return false; return false;
try { return getTileEntityOptional(world, pos).map(BeltTileEntity::hasPulley)
return getTileEntity(world, pos).hasPulley(); .orElse(false);
} catch (TileEntityException e) {
}
return false;
} }
@Override @Override
@ -365,24 +362,20 @@ public class BeltBlock extends HorizontalKineticBlock implements ITE<BeltTileEnt
ISelectionContext context) { ISelectionContext context) {
if (state.getBlock() != this) if (state.getBlock() != this)
return VoxelShapes.empty(); return VoxelShapes.empty();
VoxelShape shape = getShape(state, worldIn, pos, context); VoxelShape shape = getShape(state, worldIn, pos, context);
try { return getTileEntityOptional(worldIn, pos).map(te -> {
if (context.getEntity() == null) if (context.getEntity() == null)
return shape; return shape;
BeltTileEntity belt = getTileEntity(worldIn, pos); BeltTileEntity controller = te.getControllerTE();
BeltTileEntity controller = belt.getControllerTE();
if (controller == null) if (controller == null)
return shape; return shape;
if (controller.passengers == null || !controller.passengers.containsKey(context.getEntity())) { if (controller.passengers == null || !controller.passengers.containsKey(context.getEntity()))
return BeltShapes.getCollisionShape(state); return BeltShapes.getCollisionShape(state);
} return shape;
} catch (TileEntityException e) { }).orElse(shape);
}
return shape;
} }
@Override @Override

View file

@ -204,20 +204,14 @@ public abstract class AbstractChuteBlock extends Block implements IWrenchable, I
return ActionResultType.PASS; return ActionResultType.PASS;
if (world.isRemote) if (world.isRemote)
return ActionResultType.SUCCESS; return ActionResultType.SUCCESS;
try {
ChuteTileEntity te = getTileEntity(world, pos); return onTileEntityUse(world, pos, te -> {
if (te == null)
return ActionResultType.PASS;
if (te.item.isEmpty()) if (te.item.isEmpty())
return ActionResultType.PASS; return ActionResultType.PASS;
player.inventory.placeItemBackInInventory(world, te.item); player.inventory.placeItemBackInInventory(world, te.item);
te.setItem(ItemStack.EMPTY); te.setItem(ItemStack.EMPTY);
return ActionResultType.SUCCESS; return ActionResultType.SUCCESS;
});
} catch (TileEntityException e) {
e.printStackTrace();
}
return ActionResultType.PASS;
} }
} }

View file

@ -47,30 +47,25 @@ public class AnalogLeverBlock extends HorizontalFaceBlock implements ITE<AnalogL
@Override @Override
public ActionResultType onUse(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand handIn, public ActionResultType onUse(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand handIn,
BlockRayTraceResult hit) { BlockRayTraceResult hit) {
if (worldIn.isRemote) { if (worldIn.isRemote) {
addParticles(state, worldIn, pos, 1.0F); addParticles(state, worldIn, pos, 1.0F);
return ActionResultType.SUCCESS; return ActionResultType.SUCCESS;
} }
try { return onTileEntityUse(worldIn, pos, te -> {
boolean sneak = player.isSneaking(); boolean sneak = player.isSneaking();
AnalogLeverTileEntity te = getTileEntity(worldIn, pos);
te.changeState(sneak); te.changeState(sneak);
float f = .25f + ((te.state + 5) / 15f) * .5f; float f = .25f + ((te.state + 5) / 15f) * .5f;
worldIn.playSound(null, pos, SoundEvents.BLOCK_LEVER_CLICK, SoundCategory.BLOCKS, 0.2F, f); worldIn.playSound(null, pos, SoundEvents.BLOCK_LEVER_CLICK, SoundCategory.BLOCKS, 0.2F, f);
} catch (TileEntityException e) {} return ActionResultType.SUCCESS;
});
return ActionResultType.SUCCESS;
} }
@Override @Override
public int getWeakPower(BlockState blockState, IBlockReader blockAccess, BlockPos pos, Direction side) { public int getWeakPower(BlockState blockState, IBlockReader blockAccess, BlockPos pos, Direction side) {
try { return getTileEntityOptional(blockAccess, pos).map(al -> al.state)
return getTileEntity(blockAccess, pos).state; .orElse(0);
} catch (TileEntityException e) {
return 0;
}
} }
@Override @Override
@ -86,34 +81,33 @@ public class AnalogLeverBlock extends HorizontalFaceBlock implements ITE<AnalogL
@Override @Override
@OnlyIn(Dist.CLIENT) @OnlyIn(Dist.CLIENT)
public void animateTick(BlockState stateIn, World worldIn, BlockPos pos, Random rand) { public void animateTick(BlockState stateIn, World worldIn, BlockPos pos, Random rand) {
try { withTileEntityDo(worldIn, pos, te -> {
AnalogLeverTileEntity tileEntity = getTileEntity(worldIn, pos); if (te.state != 0 && rand.nextFloat() < 0.25F)
if (tileEntity.state != 0 && rand.nextFloat() < 0.25F)
addParticles(stateIn, worldIn, pos, 0.5F); addParticles(stateIn, worldIn, pos, 0.5F);
} catch (TileEntityException e) {} });
} }
@Override @Override
public void onReplaced(BlockState state, World worldIn, BlockPos pos, BlockState newState, boolean isMoving) { public void onReplaced(BlockState state, World worldIn, BlockPos pos, BlockState newState, boolean isMoving) {
try { if (isMoving || state.getBlock() == newState.getBlock())
AnalogLeverTileEntity tileEntity = getTileEntity(worldIn, pos); return;
if (!isMoving && state.getBlock() != newState.getBlock()) { withTileEntityDo(worldIn, pos, te -> {
if (tileEntity.state != 0) if (te.state != 0)
updateNeighbors(state, worldIn, pos); updateNeighbors(state, worldIn, pos);
worldIn.removeTileEntity(pos); worldIn.removeTileEntity(pos);
} });
} catch (TileEntityException e) {}
} }
private static void addParticles(BlockState state, IWorld worldIn, BlockPos pos, float alpha) { private static void addParticles(BlockState state, IWorld worldIn, BlockPos pos, float alpha) {
Direction direction = state.get(HORIZONTAL_FACING).getOpposite(); Direction direction = state.get(HORIZONTAL_FACING)
.getOpposite();
Direction direction1 = getFacing(state).getOpposite(); Direction direction1 = getFacing(state).getOpposite();
double d0 = (double) pos.getX() + 0.5D + 0.1D * (double) direction.getXOffset() double d0 = (double) pos.getX() + 0.5D + 0.1D * (double) direction.getXOffset()
+ 0.2D * (double) direction1.getXOffset(); + 0.2D * (double) direction1.getXOffset();
double d1 = (double) pos.getY() + 0.5D + 0.1D * (double) direction.getYOffset() double d1 = (double) pos.getY() + 0.5D + 0.1D * (double) direction.getYOffset()
+ 0.2D * (double) direction1.getYOffset(); + 0.2D * (double) direction1.getYOffset();
double d2 = (double) pos.getZ() + 0.5D + 0.1D * (double) direction.getZOffset() double d2 = (double) pos.getZ() + 0.5D + 0.1D * (double) direction.getZOffset()
+ 0.2D * (double) direction1.getZOffset(); + 0.2D * (double) direction1.getZOffset();
worldIn.addParticle(new RedstoneParticleData(1.0F, 0.0F, 0.0F, alpha), d0, d1, d2, 0.0D, 0.0D, 0.0D); worldIn.addParticle(new RedstoneParticleData(1.0F, 0.0F, 0.0F, alpha), d0, d1, d2, 0.0D, 0.0D, 0.0D);
} }
@ -137,7 +131,7 @@ public class AnalogLeverBlock extends HorizontalFaceBlock implements ITE<AnalogL
public Class<AnalogLeverTileEntity> getTileEntityClass() { public Class<AnalogLeverTileEntity> getTileEntityClass() {
return AnalogLeverTileEntity.class; return AnalogLeverTileEntity.class;
} }
@Override @Override
public boolean allowsMovement(BlockState state, IBlockReader reader, BlockPos pos, PathType type) { public boolean allowsMovement(BlockState state, IBlockReader reader, BlockPos pos, PathType type) {
return false; return false;

View file

@ -42,54 +42,52 @@ public class NixieTubeBlock extends HorizontalBlock implements ITE<NixieTubeTile
@Override @Override
public ActionResultType onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, public ActionResultType onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand,
BlockRayTraceResult ray) { BlockRayTraceResult ray) {
try {
ItemStack heldItem = player.getHeldItem(hand); ItemStack heldItem = player.getHeldItem(hand);
NixieTubeTileEntity nixie = getTileEntity(world, pos); NixieTubeTileEntity nixie = getTileEntity(world, pos);
if (player.isSneaking()) if (nixie == null)
return ActionResultType.PASS;
if (player.isSneaking())
return ActionResultType.PASS;
if (heldItem.isEmpty()) {
if (nixie.reactsToRedstone())
return ActionResultType.PASS; return ActionResultType.PASS;
nixie.clearCustomText();
if (heldItem.isEmpty()) { updateDisplayedRedstoneValue(state, world, pos);
if (nixie.reactsToRedstone()) return ActionResultType.SUCCESS;
return ActionResultType.PASS;
nixie.clearCustomText();
updateDisplayedRedstoneValue(state, world, pos);
return ActionResultType.SUCCESS;
}
if (heldItem.getItem() == Items.NAME_TAG && heldItem.hasDisplayName()) {
Direction left = state.get(HORIZONTAL_FACING)
.rotateY();
Direction right = left.getOpposite();
if (world.isRemote)
return ActionResultType.SUCCESS;
BlockPos currentPos = pos;
while (true) {
BlockPos nextPos = currentPos.offset(left);
if (world.getBlockState(nextPos) != state)
break;
currentPos = nextPos;
}
int index = 0;
while (true) {
final int rowPosition = index;
withTileEntityDo(world, currentPos, te -> te.displayCustomNameOf(heldItem, rowPosition));
BlockPos nextPos = currentPos.offset(right);
if (world.getBlockState(nextPos) != state)
break;
currentPos = nextPos;
index++;
}
}
} catch (TileEntityException e) {
} }
if (heldItem.getItem() == Items.NAME_TAG && heldItem.hasDisplayName()) {
Direction left = state.get(HORIZONTAL_FACING)
.rotateY();
Direction right = left.getOpposite();
if (world.isRemote)
return ActionResultType.SUCCESS;
BlockPos currentPos = pos;
while (true) {
BlockPos nextPos = currentPos.offset(left);
if (world.getBlockState(nextPos) != state)
break;
currentPos = nextPos;
}
int index = 0;
while (true) {
final int rowPosition = index;
withTileEntityDo(world, currentPos, te -> te.displayCustomNameOf(heldItem, rowPosition));
BlockPos nextPos = currentPos.offset(right);
if (world.getBlockState(nextPos) != state)
break;
currentPos = nextPos;
index++;
}
}
return ActionResultType.PASS; return ActionResultType.PASS;
} }

View file

@ -114,12 +114,8 @@ public class RedstoneLinkBlock extends ProperDirectionalBlock implements ITE<Red
public int getWeakPower(BlockState state, IBlockReader blockAccess, BlockPos pos, Direction side) { public int getWeakPower(BlockState state, IBlockReader blockAccess, BlockPos pos, Direction side) {
if (!state.get(RECEIVER)) if (!state.get(RECEIVER))
return 0; return 0;
try { return getTileEntityOptional(blockAccess, pos).map(RedstoneLinkTileEntity::getReceivedSignal)
RedstoneLinkTileEntity tileEntity = getTileEntity(blockAccess, pos); .orElse(0);
return tileEntity.getReceivedSignal();
} catch (TileEntityException e) {
}
return 0;
} }
@Override @Override
@ -149,17 +145,15 @@ public class RedstoneLinkBlock extends ProperDirectionalBlock implements ITE<Red
public ActionResultType toggleMode(BlockState state, World worldIn, BlockPos pos) { public ActionResultType toggleMode(BlockState state, World worldIn, BlockPos pos) {
if (worldIn.isRemote) if (worldIn.isRemote)
return ActionResultType.SUCCESS; return ActionResultType.SUCCESS;
try {
RedstoneLinkTileEntity te = getTileEntity(worldIn, pos); return onTileEntityUse(worldIn, pos, te -> {
Boolean wasReceiver = state.get(RECEIVER); Boolean wasReceiver = state.get(RECEIVER);
boolean blockPowered = worldIn.isBlockPowered(pos); boolean blockPowered = worldIn.isBlockPowered(pos);
worldIn.setBlockState(pos, state.cycle(RECEIVER) worldIn.setBlockState(pos, state.cycle(RECEIVER)
.with(POWERED, blockPowered), 3); .with(POWERED, blockPowered), 3);
te.transmit(wasReceiver ? 0 : getPower(worldIn, pos)); te.transmit(wasReceiver ? 0 : getPower(worldIn, pos));
return ActionResultType.SUCCESS; return ActionResultType.SUCCESS;
} catch (TileEntityException e) { });
}
return ActionResultType.PASS;
} }
@Override @Override

View file

@ -1,5 +1,7 @@
package com.simibubi.create.content.logistics.block.redstone; package com.simibubi.create.content.logistics.block.redstone;
import java.util.Random;
import com.simibubi.create.AllItems; import com.simibubi.create.AllItems;
import com.simibubi.create.AllShapes; import com.simibubi.create.AllShapes;
import com.simibubi.create.AllTileEntities; import com.simibubi.create.AllTileEntities;
@ -33,8 +35,6 @@ import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.fml.DistExecutor; import net.minecraftforge.fml.DistExecutor;
import net.minecraftforge.items.CapabilityItemHandler; import net.minecraftforge.items.CapabilityItemHandler;
import java.util.Random;
public class StockpileSwitchBlock extends HorizontalBlock implements ITE<StockpileSwitchTileEntity>, IWrenchable { public class StockpileSwitchBlock extends HorizontalBlock implements ITE<StockpileSwitchTileEntity>, IWrenchable {
public static final IntegerProperty INDICATOR = IntegerProperty.create("indicator", 0, 6); public static final IntegerProperty INDICATOR = IntegerProperty.create("indicator", 0, 6);
@ -85,19 +85,14 @@ public class StockpileSwitchBlock extends HorizontalBlock implements ITE<Stockpi
public int getWeakPower(BlockState blockState, IBlockReader blockAccess, BlockPos pos, Direction side) { public int getWeakPower(BlockState blockState, IBlockReader blockAccess, BlockPos pos, Direction side) {
if (side == blockState.get(HORIZONTAL_FACING).getOpposite()) if (side == blockState.get(HORIZONTAL_FACING).getOpposite())
return 0; return 0;
try { return getTileEntityOptional(blockAccess, pos).filter(StockpileSwitchTileEntity::isPowered)
return getTileEntity(blockAccess, pos).isPowered() ? 15 : 0; .map($ -> 15)
} catch (TileEntityException e) { .orElse(0);
}
return 0;
} }
@Override @Override
public void scheduledTick(BlockState blockState, ServerWorld world, BlockPos pos, Random random) { public void scheduledTick(BlockState blockState, ServerWorld world, BlockPos pos, Random random) {
try { getTileEntityOptional(world, pos).ifPresent(StockpileSwitchTileEntity::updatePowerAfterDelay);
getTileEntity(world, pos).updatePowerAfterDelay();
} catch (TileEntityException e) {
}
} }
@Override @Override

View file

@ -2,102 +2,44 @@ package com.simibubi.create.foundation.block;
import java.util.Optional; import java.util.Optional;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Function;
import com.simibubi.create.Create; import javax.annotation.Nullable;
import com.simibubi.create.foundation.config.AllConfigs;
import com.simibubi.create.foundation.utility.WorldHelper;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ResourceLocation; import net.minecraft.util.ActionResultType;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockReader; import net.minecraft.world.IBlockReader;
import net.minecraft.world.IWorld;
public interface ITE<T extends TileEntity> { public interface ITE<T extends TileEntity> {
Class<T> getTileEntityClass(); Class<T> getTileEntityClass();
default void withTileEntityDo(IBlockReader world, BlockPos pos, Consumer<T> action) { default void withTileEntityDo(IBlockReader world, BlockPos pos, Consumer<T> action) {
try { getTileEntityOptional(world, pos).ifPresent(action);
action.accept(getTileEntity(world, pos));
} catch (TileEntityException e) {
}
} }
default ActionResultType onTileEntityUse(IBlockReader world, BlockPos pos, Function<T, ActionResultType> action) {
return getTileEntityOptional(world, pos).map(action)
.orElse(ActionResultType.PASS);
}
default Optional<T> getTileEntityOptional(IBlockReader world, BlockPos pos) { default Optional<T> getTileEntityOptional(IBlockReader world, BlockPos pos) {
try { return Optional.ofNullable(getTileEntity(world, pos));
return Optional.of(getTileEntity(world, pos));
} catch (TileEntityException e) {
}
return Optional.empty();
} }
@Nullable
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
default T getTileEntity(IBlockReader worldIn, BlockPos pos) throws TileEntityException { default T getTileEntity(IBlockReader worldIn, BlockPos pos) {
TileEntity tileEntity = worldIn.getTileEntity(pos); TileEntity tileEntity = worldIn.getTileEntity(pos);
Class<T> expectedClass = getTileEntityClass(); Class<T> expectedClass = getTileEntityClass();
IWorld world = null;
if (worldIn instanceof IWorld)
world = (IWorld) worldIn;
if (tileEntity == null) if (tileEntity == null)
throw new MissingTileEntityException(world, pos, expectedClass); return null;
if (!expectedClass.isInstance(tileEntity)) if (!expectedClass.isInstance(tileEntity))
throw new InvalidTileEntityException(world, pos, expectedClass, tileEntity.getClass()); return null;
return (T) tileEntity; return (T) tileEntity;
} }
static class TileEntityException extends Throwable {
private static final long serialVersionUID = 1L;
public TileEntityException(IWorld world, BlockPos pos, Class<?> teClass) {
super(makeBaseMessage(world, pos, teClass));
}
public TileEntityException(String message) {
super(message);
report(this);
}
static String makeBaseMessage(IWorld world, BlockPos pos, Class<?> expectedTeClass) {
return String.format("[%s] @(%d, %d, %d), expecting a %s", getDimensionName(world), pos.getX(), pos.getY(),
pos.getZ(), expectedTeClass.getSimpleName());
}
static String getDimensionName(IWorld world) {
String notAvailable = "Dim N/A";
if (world == null)
return notAvailable;
ResourceLocation registryName = WorldHelper.getDimensionID(world);
if (registryName == null)
return notAvailable;
return registryName.toString();
}
}
static class MissingTileEntityException extends TileEntityException {
private static final long serialVersionUID = 1L;
public MissingTileEntityException(IWorld world, BlockPos pos, Class<?> teClass) {
super("Missing TileEntity: " + makeBaseMessage(world, pos, teClass));
}
}
static class InvalidTileEntityException extends TileEntityException {
private static final long serialVersionUID = 1L;
public InvalidTileEntityException(IWorld world, BlockPos pos, Class<?> expectedTeClass, Class<?> foundTeClass) {
super("Wrong TileEntity: " + makeBaseMessage(world, pos, expectedTeClass) + ", found "
+ foundTeClass.getSimpleName());
}
}
static void report(TileEntityException e) {
if (AllConfigs.COMMON.logTeErrors.get())
Create.LOGGER.debug("TileEntityException thrown!", e);
}
} }

View file

@ -3,7 +3,6 @@ package com.simibubi.create.foundation.config;
public class CCommon extends ConfigBase { public class CCommon extends ConfigBase {
public CWorldGen worldGen = nested(0, CWorldGen::new, Comments.worldGen); public CWorldGen worldGen = nested(0, CWorldGen::new, Comments.worldGen);
public ConfigBool logTeErrors = b(false, "logTeErrors", Comments.logTeErrors);
@Override @Override
public String getName() { public String getName() {
@ -12,7 +11,6 @@ public class CCommon extends ConfigBase {
private static class Comments { private static class Comments {
static String worldGen = "Modify Create's impact on your terrain"; static String worldGen = "Modify Create's impact on your terrain";
static String logTeErrors = "Forward caught TileEntityExceptions to the log at debug level.";
} }
} }