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,7 +92,6 @@ public class CrushingWheelBlock extends RotatedPillarKineticBlock implements ITE
if (AllBlocks.CRUSHING_WHEEL.has(otherState)) {
controllerShouldExist = true;
try {
CrushingWheelTileEntity te = getTileEntity(world, pos);
CrushingWheelTileEntity otherTe = getTileEntity(world, otherWheelPos);
@ -116,10 +115,6 @@ public class CrushingWheelBlock extends RotatedPillarKineticBlock implements ITE
}
if (otherState.get(AXIS) != state.get(AXIS))
controllerShouldExist = false;
} catch (TileEntityException e) {
controllerShouldExist = false;
}
}
if (!controllerShouldExist) {
@ -149,27 +144,25 @@ public class CrushingWheelBlock extends RotatedPillarKineticBlock implements ITE
@Override
public void onEntityCollision(BlockState state, World worldIn, BlockPos pos, Entity entityIn) {
try {
CrushingWheelTileEntity te = getTileEntity(worldIn, pos);
if (entityIn.getY() < pos.getY() + 1.25f || !entityIn.isOnGround())
return;
float speed = getTileEntityOptional(worldIn, pos).map(CrushingWheelTileEntity::getSpeed)
.orElse(0f);
double x = 0;
double z = 0;
if (state.get(AXIS) == Axis.X) {
z = te.getSpeed() / 20f;
z = speed / 20f;
x += (pos.getX() + .5f - entityIn.getX()) * .1f;
}
if (state.get(AXIS) == Axis.Z) {
x = te.getSpeed() / -20f;
x = speed / -20f;
z += (pos.getZ() + .5f - entityIn.getZ()) * .1f;
}
entityIn.setMotion(entityIn.getMotion()
.add(x, 0, z));
} catch (TileEntityException e) {
}
}
@Override

View File

@ -92,8 +92,9 @@ public class CrushingWheelControllerBlock extends DirectionalBlock
}
public void checkEntityForProcessing(World worldIn, BlockPos pos, Entity entityIn) {
try {
CrushingWheelControllerTileEntity te = getTileEntity(worldIn, pos);
if (te == null)
return;
if (te.crushingspeed == 0)
return;
if (entityIn instanceof ItemEntity)
@ -112,7 +113,6 @@ public class CrushingWheelControllerBlock extends DirectionalBlock
return;
te.startCrushing(entityIn);
} catch (TileEntityException e) {}
}
@Override
@ -167,26 +167,26 @@ public class CrushingWheelControllerBlock extends DirectionalBlock
@Override
public VoxelShape getCollisionShape(BlockState state, IBlockReader worldIn, BlockPos pos,
ISelectionContext context) {
VoxelShape standardShape = AllShapes.CRUSHING_WHEEL_CONTROLLER_COLLISION.get(state.get(FACING));
if (!state.get(VALID))
return AllShapes.CRUSHING_WHEEL_CONTROLLER_COLLISION.get(state.get(FACING));
return standardShape;
Entity entity = context.getEntity();
if (entity != null) {
if (entity == null)
return standardShape;
CompoundNBT data = entity.getPersistentData();
if (data.contains("BypassCrushingWheel")) {
if (data.contains("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.
return VoxelShapes.empty();
}
try {
CrushingWheelControllerTileEntity te = getTileEntity(worldIn, pos);
if (te.processingEntity == entity)
if (te != null && te.processingEntity == entity)
return VoxelShapes.empty();
} catch (TileEntityException e) {}
}
return AllShapes.CRUSHING_WHEEL_CONTROLLER_COLLISION.get(state.get(FACING));
return standardShape;
}
@Override

View File

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

View File

@ -36,8 +36,7 @@ public class ItemDrainBlock extends Block implements IWrenchable, ITE<ItemDrainT
BlockRayTraceResult hit) {
ItemStack heldItem = player.getHeldItem(handIn);
try {
ItemDrainTileEntity te = getTileEntity(worldIn, pos);
return onTileEntityUse(worldIn, pos, te -> {
if (!heldItem.isEmpty()) {
te.internalTank.allowInsertion();
ActionResultType tryExchange = tryExchange(worldIn, player, handIn, heldItem, te);
@ -53,10 +52,7 @@ public class ItemDrainBlock extends Block implements IWrenchable, ITE<ItemDrainT
te.notifyUpdate();
}
return ActionResultType.SUCCESS;
} catch (TileEntityException e) {
}
return ActionResultType.PASS;
});
}
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) {
ItemStack heldItem = player.getHeldItem(handIn);
try {
BasinTileEntity te = getTileEntity(worldIn, pos);
return onTileEntityUse(worldIn, pos, te -> {
if (!heldItem.isEmpty()) {
if (FluidHelper.tryEmptyItemIntoTE(worldIn, player, handIn, heldItem, te))
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,
1f + Create.RANDOM.nextFloat());
te.onEmptied();
} catch (TileEntityException e) {
}
return ActionResultType.SUCCESS;
});
}
@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) {
if (face.getAxis() != getRotationAxis(state))
return false;
try {
return getTileEntity(world, pos).hasPulley();
} catch (TileEntityException e) {
}
return false;
return getTileEntityOptional(world, pos).map(BeltTileEntity::hasPulley)
.orElse(false);
}
@Override
@ -367,22 +364,18 @@ public class BeltBlock extends HorizontalKineticBlock implements ITE<BeltTileEnt
return VoxelShapes.empty();
VoxelShape shape = getShape(state, worldIn, pos, context);
try {
return getTileEntityOptional(worldIn, pos).map(te -> {
if (context.getEntity() == null)
return shape;
BeltTileEntity belt = getTileEntity(worldIn, pos);
BeltTileEntity controller = belt.getControllerTE();
BeltTileEntity controller = te.getControllerTE();
if (controller == null)
return shape;
if (controller.passengers == null || !controller.passengers.containsKey(context.getEntity())) {
if (controller.passengers == null || !controller.passengers.containsKey(context.getEntity()))
return BeltShapes.getCollisionShape(state);
}
} catch (TileEntityException e) {
}
return shape;
}).orElse(shape);
}
@Override

View File

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

View File

@ -53,24 +53,19 @@ public class AnalogLeverBlock extends HorizontalFaceBlock implements ITE<AnalogL
return ActionResultType.SUCCESS;
}
try {
return onTileEntityUse(worldIn, pos, te -> {
boolean sneak = player.isSneaking();
AnalogLeverTileEntity te = getTileEntity(worldIn, pos);
te.changeState(sneak);
float f = .25f + ((te.state + 5) / 15f) * .5f;
worldIn.playSound(null, pos, SoundEvents.BLOCK_LEVER_CLICK, SoundCategory.BLOCKS, 0.2F, f);
} catch (TileEntityException e) {}
return ActionResultType.SUCCESS;
});
}
@Override
public int getWeakPower(BlockState blockState, IBlockReader blockAccess, BlockPos pos, Direction side) {
try {
return getTileEntity(blockAccess, pos).state;
} catch (TileEntityException e) {
return 0;
}
return getTileEntityOptional(blockAccess, pos).map(al -> al.state)
.orElse(0);
}
@Override
@ -86,27 +81,26 @@ public class AnalogLeverBlock extends HorizontalFaceBlock implements ITE<AnalogL
@Override
@OnlyIn(Dist.CLIENT)
public void animateTick(BlockState stateIn, World worldIn, BlockPos pos, Random rand) {
try {
AnalogLeverTileEntity tileEntity = getTileEntity(worldIn, pos);
if (tileEntity.state != 0 && rand.nextFloat() < 0.25F)
withTileEntityDo(worldIn, pos, te -> {
if (te.state != 0 && rand.nextFloat() < 0.25F)
addParticles(stateIn, worldIn, pos, 0.5F);
} catch (TileEntityException e) {}
});
}
@Override
public void onReplaced(BlockState state, World worldIn, BlockPos pos, BlockState newState, boolean isMoving) {
try {
AnalogLeverTileEntity tileEntity = getTileEntity(worldIn, pos);
if (!isMoving && state.getBlock() != newState.getBlock()) {
if (tileEntity.state != 0)
if (isMoving || state.getBlock() == newState.getBlock())
return;
withTileEntityDo(worldIn, pos, te -> {
if (te.state != 0)
updateNeighbors(state, worldIn, pos);
worldIn.removeTileEntity(pos);
}
} catch (TileEntityException e) {}
});
}
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();
double d0 = (double) pos.getX() + 0.5D + 0.1D * (double) direction.getXOffset()
+ 0.2D * (double) direction1.getXOffset();

View File

@ -42,11 +42,12 @@ public class NixieTubeBlock extends HorizontalBlock implements ITE<NixieTubeTile
@Override
public ActionResultType onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand,
BlockRayTraceResult ray) {
try {
ItemStack heldItem = player.getHeldItem(hand);
NixieTubeTileEntity nixie = getTileEntity(world, pos);
if (nixie == null)
return ActionResultType.PASS;
if (player.isSneaking())
return ActionResultType.PASS;
@ -87,9 +88,6 @@ public class NixieTubeBlock extends HorizontalBlock implements ITE<NixieTubeTile
}
}
} catch (TileEntityException e) {
}
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) {
if (!state.get(RECEIVER))
return 0;
try {
RedstoneLinkTileEntity tileEntity = getTileEntity(blockAccess, pos);
return tileEntity.getReceivedSignal();
} catch (TileEntityException e) {
}
return 0;
return getTileEntityOptional(blockAccess, pos).map(RedstoneLinkTileEntity::getReceivedSignal)
.orElse(0);
}
@Override
@ -149,17 +145,15 @@ public class RedstoneLinkBlock extends ProperDirectionalBlock implements ITE<Red
public ActionResultType toggleMode(BlockState state, World worldIn, BlockPos pos) {
if (worldIn.isRemote)
return ActionResultType.SUCCESS;
try {
RedstoneLinkTileEntity te = getTileEntity(worldIn, pos);
return onTileEntityUse(worldIn, pos, te -> {
Boolean wasReceiver = state.get(RECEIVER);
boolean blockPowered = worldIn.isBlockPowered(pos);
worldIn.setBlockState(pos, state.cycle(RECEIVER)
.with(POWERED, blockPowered), 3);
te.transmit(wasReceiver ? 0 : getPower(worldIn, pos));
return ActionResultType.SUCCESS;
} catch (TileEntityException e) {
}
return ActionResultType.PASS;
});
}
@Override

View File

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

View File

@ -2,102 +2,44 @@ package com.simibubi.create.foundation.block;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import com.simibubi.create.Create;
import com.simibubi.create.foundation.config.AllConfigs;
import com.simibubi.create.foundation.utility.WorldHelper;
import javax.annotation.Nullable;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.IWorld;
public interface ITE<T extends TileEntity> {
Class<T> getTileEntityClass();
default void withTileEntityDo(IBlockReader world, BlockPos pos, Consumer<T> action) {
try {
action.accept(getTileEntity(world, pos));
} catch (TileEntityException e) {
getTileEntityOptional(world, pos).ifPresent(action);
}
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) {
try {
return Optional.of(getTileEntity(world, pos));
} catch (TileEntityException e) {
}
return Optional.empty();
return Optional.ofNullable(getTileEntity(world, pos));
}
@Nullable
@SuppressWarnings("unchecked")
default T getTileEntity(IBlockReader worldIn, BlockPos pos) throws TileEntityException {
default T getTileEntity(IBlockReader worldIn, BlockPos pos) {
TileEntity tileEntity = worldIn.getTileEntity(pos);
Class<T> expectedClass = getTileEntityClass();
IWorld world = null;
if (worldIn instanceof IWorld)
world = (IWorld) worldIn;
if (tileEntity == null)
throw new MissingTileEntityException(world, pos, expectedClass);
return null;
if (!expectedClass.isInstance(tileEntity))
throw new InvalidTileEntityException(world, pos, expectedClass, tileEntity.getClass());
return null;
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 CWorldGen worldGen = nested(0, CWorldGen::new, Comments.worldGen);
public ConfigBool logTeErrors = b(false, "logTeErrors", Comments.logTeErrors);
@Override
public String getName() {
@ -12,7 +11,6 @@ public class CCommon extends ConfigBase {
private static class Comments {
static String worldGen = "Modify Create's impact on your terrain";
static String logTeErrors = "Forward caught TileEntityExceptions to the log at debug level.";
}
}