mirror of
https://github.com/Creators-of-Create/Create.git
synced 2024-11-10 12:02:48 +01:00
ITE refactor
This commit is contained in:
parent
2852756a68
commit
1f48c698e8
13 changed files with 179 additions and 288 deletions
|
@ -92,34 +92,29 @@ 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);
|
||||
CrushingWheelTileEntity te = getTileEntity(world, pos);
|
||||
CrushingWheelTileEntity otherTe = getTileEntity(world, otherWheelPos);
|
||||
|
||||
if (te != null && otherTe != null && (te.getSpeed() > 0) != (otherTe.getSpeed() > 0)
|
||||
&& te.getSpeed() != 0) {
|
||||
Axis wheelAxis = state.get(AXIS);
|
||||
Axis sideAxis = side.getAxis();
|
||||
int controllerADO = Math.round(Math.signum(te.getSpeed())) * side.getAxisDirection().getOffset();
|
||||
Vector3d controllerDirVec = new Vector3d(wheelAxis == Axis.X ? 1 : 0
|
||||
, wheelAxis == Axis.Y ? 1 : 0
|
||||
, wheelAxis == Axis.Z ? 1 : 0)
|
||||
.crossProduct(new Vector3d(sideAxis == Axis.X ? 1 : 0
|
||||
, sideAxis == Axis.Y ? 1 : 0
|
||||
, sideAxis == Axis.Z ? 1 : 0));
|
||||
if (te != null && otherTe != null && (te.getSpeed() > 0) != (otherTe.getSpeed() > 0)
|
||||
&& te.getSpeed() != 0) {
|
||||
Axis wheelAxis = state.get(AXIS);
|
||||
Axis sideAxis = side.getAxis();
|
||||
int controllerADO = Math.round(Math.signum(te.getSpeed())) * side.getAxisDirection().getOffset();
|
||||
Vector3d controllerDirVec = new Vector3d(wheelAxis == Axis.X ? 1 : 0
|
||||
, wheelAxis == Axis.Y ? 1 : 0
|
||||
, wheelAxis == Axis.Z ? 1 : 0)
|
||||
.crossProduct(new Vector3d(sideAxis == Axis.X ? 1 : 0
|
||||
, sideAxis == Axis.Y ? 1 : 0
|
||||
, sideAxis == Axis.Z ? 1 : 0));
|
||||
|
||||
controllerNewDirection = Direction.getFacingFromVector(controllerDirVec.x * controllerADO
|
||||
, controllerDirVec.y * controllerADO
|
||||
, controllerDirVec.z * controllerADO);
|
||||
controllerNewDirection = Direction.getFacingFromVector(controllerDirVec.x * controllerADO
|
||||
, controllerDirVec.y * controllerADO
|
||||
, controllerDirVec.z * controllerADO);
|
||||
|
||||
controllerShouldBeValid = true;
|
||||
}
|
||||
if (otherState.get(AXIS) != state.get(AXIS))
|
||||
controllerShouldExist = false;
|
||||
|
||||
} catch (TileEntityException e) {
|
||||
controllerShouldExist = false;
|
||||
controllerShouldBeValid = true;
|
||||
}
|
||||
if (otherState.get(AXIS) != state.get(AXIS))
|
||||
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;
|
||||
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;
|
||||
double x = 0;
|
||||
double z = 0;
|
||||
|
||||
if (state.get(AXIS) == Axis.X) {
|
||||
z = te.getSpeed() / 20f;
|
||||
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.X) {
|
||||
z = speed / 20f;
|
||||
x += (pos.getX() + .5f - entityIn.getX()) * .1f;
|
||||
}
|
||||
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
|
||||
|
|
|
@ -91,28 +91,28 @@ public class CrushingWheelControllerBlock extends DirectionalBlock
|
|||
});
|
||||
}
|
||||
|
||||
public void checkEntityForProcessing(World worldIn, BlockPos pos, Entity entityIn){
|
||||
try {
|
||||
CrushingWheelControllerTileEntity te = getTileEntity(worldIn, pos);
|
||||
if (te.crushingspeed == 0)
|
||||
return;
|
||||
if (entityIn instanceof ItemEntity)
|
||||
((ItemEntity) entityIn).setPickupDelay(10);
|
||||
CompoundNBT data = entityIn.getPersistentData();
|
||||
if (data.contains("BypassCrushingWheel")) {
|
||||
if (pos.equals(NBTUtil.readBlockPos(data.getCompound("BypassCrushingWheel"))))
|
||||
return;
|
||||
}
|
||||
if (te.isOccupied())
|
||||
return;
|
||||
boolean isPlayer = entityIn instanceof PlayerEntity;
|
||||
if (isPlayer && ((PlayerEntity) entityIn).isCreative())
|
||||
return;
|
||||
if (isPlayer && entityIn.world.getDifficulty() == Difficulty.PEACEFUL)
|
||||
public void checkEntityForProcessing(World worldIn, BlockPos pos, Entity entityIn) {
|
||||
CrushingWheelControllerTileEntity te = getTileEntity(worldIn, pos);
|
||||
if (te == null)
|
||||
return;
|
||||
if (te.crushingspeed == 0)
|
||||
return;
|
||||
if (entityIn instanceof ItemEntity)
|
||||
((ItemEntity) entityIn).setPickupDelay(10);
|
||||
CompoundNBT data = entityIn.getPersistentData();
|
||||
if (data.contains("BypassCrushingWheel")) {
|
||||
if (pos.equals(NBTUtil.readBlockPos(data.getCompound("BypassCrushingWheel"))))
|
||||
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);
|
||||
} catch (TileEntityException e) {}
|
||||
te.startCrushing(entityIn);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -166,27 +166,27 @@ public class CrushingWheelControllerBlock extends DirectionalBlock
|
|||
|
||||
@Override
|
||||
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))
|
||||
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 (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)
|
||||
CompoundNBT data = entity.getPersistentData();
|
||||
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();
|
||||
} catch (TileEntityException e) {}
|
||||
}
|
||||
return AllShapes.CRUSHING_WHEEL_CONTROLLER_COLLISION.get(state.get(FACING));
|
||||
|
||||
CrushingWheelControllerTileEntity te = getTileEntity(worldIn, pos);
|
||||
if (te != null && te.processingEntity == entity)
|
||||
return VoxelShapes.empty();
|
||||
|
||||
return standardShape;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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;
|
||||
return ActionResultType.SUCCESS;
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -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
|
||||
|
@ -365,24 +362,20 @@ public class BeltBlock extends HorizontalKineticBlock implements ITE<BeltTileEnt
|
|||
ISelectionContext context) {
|
||||
if (state.getBlock() != this)
|
||||
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;
|
||||
return shape;
|
||||
|
||||
}).orElse(shape);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -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;
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -47,30 +47,25 @@ public class AnalogLeverBlock extends HorizontalFaceBlock implements ITE<AnalogL
|
|||
|
||||
@Override
|
||||
public ActionResultType onUse(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand handIn,
|
||||
BlockRayTraceResult hit) {
|
||||
BlockRayTraceResult hit) {
|
||||
if (worldIn.isRemote) {
|
||||
addParticles(state, worldIn, pos, 1.0F);
|
||||
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;
|
||||
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,34 +81,33 @@ 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)
|
||||
updateNeighbors(state, worldIn, pos);
|
||||
worldIn.removeTileEntity(pos);
|
||||
}
|
||||
} catch (TileEntityException e) {}
|
||||
if (isMoving || state.getBlock() == newState.getBlock())
|
||||
return;
|
||||
withTileEntityDo(worldIn, pos, te -> {
|
||||
if (te.state != 0)
|
||||
updateNeighbors(state, worldIn, pos);
|
||||
worldIn.removeTileEntity(pos);
|
||||
});
|
||||
}
|
||||
|
||||
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();
|
||||
+ 0.2D * (double) direction1.getXOffset();
|
||||
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()
|
||||
+ 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);
|
||||
}
|
||||
|
||||
|
@ -137,7 +131,7 @@ public class AnalogLeverBlock extends HorizontalFaceBlock implements ITE<AnalogL
|
|||
public Class<AnalogLeverTileEntity> getTileEntityClass() {
|
||||
return AnalogLeverTileEntity.class;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean allowsMovement(BlockState state, IBlockReader reader, BlockPos pos, PathType type) {
|
||||
return false;
|
||||
|
|
|
@ -42,54 +42,52 @@ 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);
|
||||
ItemStack heldItem = player.getHeldItem(hand);
|
||||
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;
|
||||
|
||||
if (heldItem.isEmpty()) {
|
||||
if (nixie.reactsToRedstone())
|
||||
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) {
|
||||
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++;
|
||||
}
|
||||
}
|
||||
|
||||
return ActionResultType.PASS;
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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.";
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue