Merge branch 'non_player_kinetic_placement_fix' into mc1.15/dev

This commit is contained in:
LordGrimmauld 2020-07-04 14:29:38 +02:00
commit 8ddf3fabba
24 changed files with 294 additions and 180 deletions

View File

@ -27,14 +27,17 @@ public abstract class DirectionalAxisKineticBlock extends DirectionalKineticBloc
}
protected Direction getFacingForPlacement(BlockItemUseContext context) {
Direction facing = context.getNearestLookingDirection().getOpposite();
if (context.getPlayer().isSneaking())
Direction facing = context.getNearestLookingDirection()
.getOpposite();
if (context.getPlayer() != null && context.getPlayer()
.isSneaking())
facing = facing.getOpposite();
return facing;
}
protected boolean getAxisAlignmentForPlacement(BlockItemUseContext context) {
return context.getPlacementHorizontalFacing().getAxis() == Axis.X;
return context.getPlacementHorizontalFacing()
.getAxis() == Axis.X;
}
@Override
@ -44,30 +47,37 @@ public abstract class DirectionalAxisKineticBlock extends DirectionalKineticBloc
World world = context.getWorld();
boolean alongFirst = false;
if (facing.getAxis().isHorizontal()) {
if (facing.getAxis()
.isHorizontal()) {
alongFirst = facing.getAxis() == Axis.Z;
Block blockAbove = world.getBlockState(pos.offset(Direction.UP)).getBlock();
Block blockAbove = world.getBlockState(pos.offset(Direction.UP))
.getBlock();
boolean shaftAbove = blockAbove instanceof IRotate && ((IRotate) blockAbove).hasShaftTowards(world,
pos.up(), world.getBlockState(pos.up()), Direction.DOWN);
Block blockBelow = world.getBlockState(pos.offset(Direction.DOWN)).getBlock();
pos.up(), world.getBlockState(pos.up()), Direction.DOWN);
Block blockBelow = world.getBlockState(pos.offset(Direction.DOWN))
.getBlock();
boolean shaftBelow = blockBelow instanceof IRotate && ((IRotate) blockBelow).hasShaftTowards(world,
pos.down(), world.getBlockState(pos.down()), Direction.UP);
pos.down(), world.getBlockState(pos.down()), Direction.UP);
if (shaftAbove || shaftBelow)
alongFirst = facing.getAxis() == Axis.X;
}
if (facing.getAxis().isVertical()) {
if (facing.getAxis()
.isVertical()) {
alongFirst = getAxisAlignmentForPlacement(context);
Direction prefferedSide = null;
for (Direction side : Direction.values()) {
if (side.getAxis().isVertical())
if (side.getAxis()
.isVertical())
continue;
BlockState blockState = context.getWorld().getBlockState(context.getPos().offset(side));
BlockState blockState = context.getWorld()
.getBlockState(context.getPos()
.offset(side));
if (blockState.getBlock() instanceof IRotate) {
if (((IRotate) blockState.getBlock()).hasShaftTowards(context.getWorld(),
context.getPos().offset(side), blockState, side.getOpposite()))
if (((IRotate) blockState.getBlock()).hasShaftTowards(context.getWorld(), context.getPos()
.offset(side), blockState, side.getOpposite()))
if (prefferedSide != null && prefferedSide.getAxis() != side.getAxis()) {
prefferedSide = null;
break;
@ -81,12 +91,15 @@ public abstract class DirectionalAxisKineticBlock extends DirectionalKineticBloc
}
}
return this.getDefaultState().with(FACING, facing).with(AXIS_ALONG_FIRST_COORDINATE, alongFirst);
return this.getDefaultState()
.with(FACING, facing)
.with(AXIS_ALONG_FIRST_COORDINATE, alongFirst);
}
@Override
public Axis getRotationAxis(BlockState state) {
Axis pistonAxis = state.get(FACING).getAxis();
Axis pistonAxis = state.get(FACING)
.getAxis();
boolean alongFirst = state.get(AXIS_ALONG_FIRST_COORDINATE);
if (pistonAxis == Axis.X)

View File

@ -27,10 +27,12 @@ public abstract class DirectionalKineticBlock extends KineticBlock {
public Direction getPreferredFacing(BlockItemUseContext context) {
Direction prefferedSide = null;
for (Direction side : Direction.values()) {
BlockState blockState = context.getWorld().getBlockState(context.getPos().offset(side));
BlockState blockState = context.getWorld()
.getBlockState(context.getPos()
.offset(side));
if (blockState.getBlock() instanceof IRotate) {
if (((IRotate) blockState.getBlock()).hasShaftTowards(context.getWorld(), context.getPos().offset(side),
blockState, side.getOpposite()))
if (((IRotate) blockState.getBlock()).hasShaftTowards(context.getWorld(), context.getPos()
.offset(side), blockState, side.getOpposite()))
if (prefferedSide != null && prefferedSide.getAxis() != side.getAxis()) {
prefferedSide = null;
break;
@ -45,10 +47,11 @@ public abstract class DirectionalKineticBlock extends KineticBlock {
@Override
public BlockState getStateForPlacement(BlockItemUseContext context) {
Direction preferred = getPreferredFacing(context);
if (preferred == null || context.getPlayer().isSneaking()) {
if (preferred == null || (context.getPlayer() != null && context.getPlayer()
.isSneaking())) {
Direction nearestLookingDirection = context.getNearestLookingDirection();
return getDefaultState().with(FACING,
context.getPlayer().isSneaking() ? nearestLookingDirection : nearestLookingDirection.getOpposite());
return getDefaultState().with(FACING, context.getPlayer() != null && context.getPlayer()
.isSneaking() ? nearestLookingDirection : nearestLookingDirection.getOpposite());
}
return getDefaultState().with(FACING, preferred.getOpposite());
}

View File

@ -16,7 +16,8 @@ public abstract class RotatedPillarKineticBlock extends KineticBlock {
public RotatedPillarKineticBlock(Properties properties) {
super(properties);
this.setDefaultState(this.getDefaultState().with(AXIS, Direction.Axis.Y));
this.setDefaultState(this.getDefaultState()
.with(AXIS, Direction.Axis.Y));
}
@Override
@ -40,10 +41,12 @@ public abstract class RotatedPillarKineticBlock extends KineticBlock {
public static Axis getPreferredAxis(BlockItemUseContext context) {
Axis prefferedAxis = null;
for (Direction side : Direction.values()) {
BlockState blockState = context.getWorld().getBlockState(context.getPos().offset(side));
BlockState blockState = context.getWorld()
.getBlockState(context.getPos()
.offset(side));
if (blockState.getBlock() instanceof IRotate) {
if (((IRotate) blockState.getBlock()).hasShaftTowards(context.getWorld(), context.getPos().offset(side),
blockState, side.getOpposite()))
if (((IRotate) blockState.getBlock()).hasShaftTowards(context.getWorld(), context.getPos()
.offset(side), blockState, side.getOpposite()))
if (prefferedAxis != null && prefferedAxis != side.getAxis()) {
prefferedAxis = null;
break;
@ -63,10 +66,15 @@ public abstract class RotatedPillarKineticBlock extends KineticBlock {
@Override
public BlockState getStateForPlacement(BlockItemUseContext context) {
Axis preferredAxis = getPreferredAxis(context);
if (preferredAxis != null && !context.getPlayer().isSneaking())
return this.getDefaultState().with(AXIS, preferredAxis);
return this.getDefaultState().with(AXIS, context.getPlayer().isSneaking() ? context.getFace().getAxis()
: context.getNearestLookingDirection().getAxis());
if (preferredAxis != null && (context.getPlayer() == null || !context.getPlayer()
.isSneaking()))
return this.getDefaultState()
.with(AXIS, preferredAxis);
return this.getDefaultState()
.with(AXIS, preferredAxis != null && context.getPlayer()
.isSneaking() ? context.getFace()
.getAxis()
: context.getNearestLookingDirection()
.getAxis());
}
}

View File

@ -76,8 +76,8 @@ public class MechanicalCrafterBlock extends HorizontalKineticBlock implements IT
BlockState blockState = context.getWorld()
.getBlockState(placedOnPos);
if ((blockState.getBlock() != this) || context.getPlayer()
.isSneaking()) {
if ((blockState.getBlock() != this) || (context.getPlayer() != null && context.getPlayer()
.isSneaking())) {
BlockState stateForPlacement = super.getStateForPlacement(context);
Direction direction = stateForPlacement.get(HORIZONTAL_FACING);
if (direction != face)

View File

@ -33,7 +33,7 @@ public class HandCrankBlock extends DirectionalKineticBlock implements ITE<HandC
public VoxelShape getShape(BlockState state, IBlockReader worldIn, BlockPos pos, ISelectionContext context) {
return AllShapes.CRANK.get(state.get(FACING));
}
@Override
public BlockRenderType getRenderType(BlockState state) {
return BlockRenderType.ENTITYBLOCK_ANIMATED;
@ -41,8 +41,9 @@ public class HandCrankBlock extends DirectionalKineticBlock implements ITE<HandC
@Override
public ActionResultType onUse(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand handIn,
BlockRayTraceResult hit) {
boolean handEmpty = player.getHeldItem(handIn).isEmpty();
BlockRayTraceResult hit) {
boolean handEmpty = player.getHeldItem(handIn)
.isEmpty();
if (!handEmpty && player.isSneaking())
return ActionResultType.PASS;
@ -54,22 +55,25 @@ public class HandCrankBlock extends DirectionalKineticBlock implements ITE<HandC
@Override
public BlockState getStateForPlacement(BlockItemUseContext context) {
Direction preferred = getPreferredFacing(context);
if (preferred == null || context.getPlayer().isSneaking())
if (preferred == null || (context.getPlayer() != null && context.getPlayer()
.isSneaking()))
return getDefaultState().with(FACING, context.getFace());
return getDefaultState().with(FACING, preferred.getOpposite());
}
@Override
public boolean isValidPosition(BlockState state, IWorldReader worldIn, BlockPos pos) {
Direction facing = state.get(FACING).getOpposite();
Direction facing = state.get(FACING)
.getOpposite();
BlockPos neighbourPos = pos.offset(facing);
BlockState neighbour = worldIn.getBlockState(neighbourPos);
return !neighbour.getCollisionShape(worldIn, neighbourPos).isEmpty();
return !neighbour.getCollisionShape(worldIn, neighbourPos)
.isEmpty();
}
@Override
public void neighborChanged(BlockState state, World worldIn, BlockPos pos, Block blockIn, BlockPos fromPos,
boolean isMoving) {
boolean isMoving) {
if (worldIn.isRemote)
return;
@ -89,12 +93,14 @@ public class HandCrankBlock extends DirectionalKineticBlock implements ITE<HandC
@Override
public boolean hasShaftTowards(IWorldReader world, BlockPos pos, BlockState state, Direction face) {
return face == state.get(FACING).getOpposite();
return face == state.get(FACING)
.getOpposite();
}
@Override
public Axis getRotationAxis(BlockState state) {
return state.get(FACING).getAxis();
return state.get(FACING)
.getAxis();
}
@Override

View File

@ -65,7 +65,7 @@ public class EncasedFanBlock extends DirectionalKineticBlock implements ITE<Enca
Direction preferredFacing = getPreferredFacing(context);
if (preferredFacing == null)
preferredFacing = context.getNearestLookingDirection();
return getDefaultState().with(FACING, context.getPlayer()
return getDefaultState().with(FACING, context.getPlayer() != null && context.getPlayer()
.isSneaking() ? preferredFacing : preferredFacing.getOpposite());
}

View File

@ -34,7 +34,8 @@ public class CreativeMotorBlock extends DirectionalKineticBlock {
@Override
public BlockState getStateForPlacement(BlockItemUseContext context) {
Direction preferred = getPreferredFacing(context);
if (context.getPlayer().isSneaking() || preferred == null)
if ((context.getPlayer() != null && context.getPlayer()
.isSneaking()) || preferred == null)
return super.getStateForPlacement(context);
return getDefaultState().with(FACING, preferred);
}
@ -48,7 +49,8 @@ public class CreativeMotorBlock extends DirectionalKineticBlock {
@Override
public Axis getRotationAxis(BlockState state) {
return state.get(FACING).getAxis();
return state.get(FACING)
.getAxis();
}
@Override

View File

@ -40,13 +40,14 @@ public class LinearChassisBlock extends AbstractChassisBlock {
.getOpposite());
BlockState blockState = context.getWorld()
.getBlockState(placedOnPos);
if (isChassis(blockState) && !context.getPlayer()
.isSneaking())
return getDefaultState().with(AXIS, blockState.get(AXIS));
if (!context.getPlayer()
.isSneaking())
if (context.getPlayer() == null || !context.getPlayer()
.isSneaking()) {
if (isChassis(blockState))
return getDefaultState().with(AXIS, blockState.get(AXIS));
return getDefaultState().with(AXIS, context.getNearestLookingDirection()
.getAxis());
}
return super.getStateForPlacement(context);
}

View File

@ -49,7 +49,7 @@ public class SuperGlueItem extends Item {
PlayerEntity playerentity = context.getPlayer();
ItemStack itemstack = context.getItem();
if (playerentity != null && !this.canPlace(playerentity, direction, itemstack, blockpos1))
if (playerentity == null || !this.canPlace(playerentity, direction, itemstack, blockpos1))
return ActionResultType.FAIL;
World world = context.getWorld();
@ -82,7 +82,8 @@ public class SuperGlueItem extends Item {
public static void spawnParticles(World world, BlockPos pos, Direction direction, boolean fullBlock) {
Vec3d vec = new Vec3d(direction.getDirectionVec());
Vec3d plane = VecHelper.planeByNormal(vec);
Vec3d facePos = VecHelper.getCenterOf(pos).add(vec.scale(.5f));
Vec3d facePos = VecHelper.getCenterOf(pos)
.add(vec.scale(.5f));
float distance = fullBlock ? 1f : .25f + .25f * (world.rand.nextFloat() - .5f);
plane = plane.scale(distance);
@ -90,13 +91,14 @@ public class SuperGlueItem extends Item {
for (int i = fullBlock ? 40 : 15; i > 0; i--) {
Vec3d offset = VecHelper.rotate(plane, 360 * world.rand.nextFloat(), direction.getAxis());
Vec3d motion = offset.normalize().scale(1 / 16f);
Vec3d motion = offset.normalize()
.scale(1 / 16f);
if (fullBlock)
offset = new Vec3d(MathHelper.clamp(offset.x, -.5, .5), MathHelper.clamp(offset.y, -.5, .5),
MathHelper.clamp(offset.z, -.5, .5));
MathHelper.clamp(offset.z, -.5, .5));
Vec3d particlePos = facePos.add(offset);
world.addParticle(new ItemParticleData(ParticleTypes.ITEM, stack), particlePos.x, particlePos.y,
particlePos.z, motion.x, motion.y, motion.z);
particlePos.z, motion.x, motion.y, motion.z);
}
}

View File

@ -34,7 +34,7 @@ public class WaterWheelBlock extends HorizontalKineticBlock implements ITE<Water
public TileEntity createTileEntity(BlockState state, IBlockReader world) {
return AllTileEntities.WATER_WHEEL.create();
}
@Override
public BlockRenderType getRenderType(BlockState state) {
return BlockRenderType.ENTITYBLOCK_ANIMATED;
@ -47,8 +47,11 @@ public class WaterWheelBlock extends HorizontalKineticBlock implements ITE<Water
BlockState neighbourState = worldIn.getBlockState(neighbourPos);
if (!AllBlocks.WATER_WHEEL.has(neighbourState))
continue;
if (neighbourState.get(HORIZONTAL_FACING).getAxis() != state.get(HORIZONTAL_FACING).getAxis()
|| state.get(HORIZONTAL_FACING).getAxis() != direction.getAxis())
if (neighbourState.get(HORIZONTAL_FACING)
.getAxis() != state.get(HORIZONTAL_FACING)
.getAxis()
|| state.get(HORIZONTAL_FACING)
.getAxis() != direction.getAxis())
return false;
}
@ -57,7 +60,7 @@ public class WaterWheelBlock extends HorizontalKineticBlock implements ITE<Water
@Override
public BlockState updatePostPlacement(BlockState stateIn, Direction facing, BlockState facingState, IWorld worldIn,
BlockPos currentPos, BlockPos facingPos) {
BlockPos currentPos, BlockPos facingPos) {
World world = worldIn.getWorld();
if (world == null || worldIn instanceof WrappedWorld)
return stateIn;
@ -78,7 +81,8 @@ public class WaterWheelBlock extends HorizontalKineticBlock implements ITE<Water
}
private void updateFlowAt(BlockState state, World world, BlockPos pos, Direction f) {
if (f.getAxis() == state.get(HORIZONTAL_FACING).getAxis())
if (f.getAxis() == state.get(HORIZONTAL_FACING)
.getAxis())
return;
IFluidState fluid = world.getFluidState(pos.offset(f));
@ -87,7 +91,8 @@ public class WaterWheelBlock extends HorizontalKineticBlock implements ITE<Water
int clockwiseMultiplier = 2;
Vec3d vec = fluid.getFlow(world, pos.offset(f));
vec = vec.scale(f.getAxisDirection().getOffset());
vec = vec.scale(f.getAxisDirection()
.getOffset());
vec = new Vec3d(Math.signum(vec.x), Math.signum(vec.y), Math.signum(vec.z));
Vec3d flow = vec;
@ -125,23 +130,28 @@ public class WaterWheelBlock extends HorizontalKineticBlock implements ITE<Water
@Override
public BlockState getStateForPlacement(BlockItemUseContext context) {
Direction facing = context.getFace();
BlockState placedOn = context.getWorld().getBlockState(context.getPos().offset(facing.getOpposite()));
BlockState placedOn = context.getWorld()
.getBlockState(context.getPos()
.offset(facing.getOpposite()));
if (AllBlocks.WATER_WHEEL.has(placedOn))
return getDefaultState().with(HORIZONTAL_FACING, placedOn.get(HORIZONTAL_FACING));
if (facing.getAxis().isHorizontal())
return getDefaultState().with(HORIZONTAL_FACING,
context.getPlayer().isSneaking() ? facing.getOpposite() : facing);
if (facing.getAxis()
.isHorizontal())
return getDefaultState().with(HORIZONTAL_FACING, context.getPlayer() != null && context.getPlayer()
.isSneaking() ? facing.getOpposite() : facing);
return super.getStateForPlacement(context);
}
@Override
public boolean hasShaftTowards(IWorldReader world, BlockPos pos, BlockState state, Direction face) {
return state.get(HORIZONTAL_FACING).getAxis() == face.getAxis();
return state.get(HORIZONTAL_FACING)
.getAxis() == face.getAxis();
}
@Override
public Axis getRotationAxis(BlockState state) {
return state.get(HORIZONTAL_FACING).getAxis();
return state.get(HORIZONTAL_FACING)
.getAxis();
}
@Override

View File

@ -102,8 +102,8 @@ public class SequencedGearshiftBlock extends HorizontalAxisKineticBlock implemen
@Override
public BlockState getStateForPlacement(BlockItemUseContext context) {
Axis preferredAxis = RotatedPillarKineticBlock.getPreferredAxis(context);
if (preferredAxis != null && !context.getPlayer()
.isSneaking())
if (preferredAxis != null && (context.getPlayer() == null || !context.getPlayer()
.isSneaking()))
return withAxis(preferredAxis, context);
return withAxis(context.getNearestLookingDirection()
.getAxis(), context);

View File

@ -291,7 +291,7 @@ public class BeltBlock extends HorizontalKineticBlock implements ITE<BeltTileEnt
if (world.isRemote)
return ActionResultType.SUCCESS;
world.setBlockState(context.getPos(), state.with(CASING, false), 3);
if (!player.isCreative())
if (player != null && !player.isCreative())
player.inventory.placeItemBackInInventory(world, AllBlocks.BRASS_CASING.asStack());
return ActionResultType.SUCCESS;
}
@ -305,7 +305,7 @@ public class BeltBlock extends HorizontalKineticBlock implements ITE<BeltTileEnt
belt.detachKinetics();
belt.attachKinetics();
}
if (!player.isCreative())
if (player != null && !player.isCreative())
player.inventory.placeItemBackInInventory(world, AllBlocks.SHAFT.asStack());
return ActionResultType.SUCCESS;
}

View File

@ -14,6 +14,7 @@ import com.simibubi.create.foundation.advancement.AllTriggers;
import com.simibubi.create.foundation.config.AllConfigs;
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.BlockItem;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemStack;
@ -30,6 +31,8 @@ import net.minecraft.util.NonNullList;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import javax.annotation.Nonnull;
public class BeltConnectorItem extends BlockItem {
public BeltConnectorItem(Properties properties) {
@ -48,12 +51,13 @@ public class BeltConnectorItem extends BlockItem {
super.fillItemGroup(p_150895_1_, p_150895_2_);
}
@Nonnull
@Override
public ActionResultType onItemUse(ItemUseContext context) {
if (context.getPlayer()
.isSneaking()) {
PlayerEntity playerEntity = context.getPlayer();
if (playerEntity != null && playerEntity.isSneaking()) {
context.getItem()
.setTag(null);
.setTag(null);
return ActionResultType.SUCCESS;
}
@ -65,7 +69,7 @@ public class BeltConnectorItem extends BlockItem {
return validAxis ? ActionResultType.SUCCESS : ActionResultType.FAIL;
CompoundNBT tag = context.getItem()
.getOrCreateTag();
.getOrCreateTag();
BlockPos firstPulley = null;
// Remove first if no longer existant or valid
@ -74,11 +78,11 @@ public class BeltConnectorItem extends BlockItem {
if (!validateAxis(world, firstPulley)) {
tag.remove("FirstPulley");
context.getItem()
.setTag(tag);
.setTag(tag);
}
}
if (!validAxis)
if (!validAxis || playerEntity == null)
return ActionResultType.FAIL;
if (tag.contains("FirstPulley")) {
@ -86,32 +90,29 @@ public class BeltConnectorItem extends BlockItem {
if (!canConnect(world, firstPulley, pos))
return ActionResultType.FAIL;
if (firstPulley != null && !firstPulley.equals(pos) && !world.isRemote) {
if (firstPulley != null && !firstPulley.equals(pos)) {
createBelts(world, firstPulley, pos);
AllTriggers.triggerFor(AllTriggers.CONNECT_BELT, context.getPlayer());
if (!context.getPlayer()
.isCreative())
AllTriggers.triggerFor(AllTriggers.CONNECT_BELT, playerEntity);
if (!playerEntity.isCreative())
context.getItem()
.shrink(1);
.shrink(1);
}
if (!context.getItem()
.isEmpty()) {
.isEmpty()) {
context.getItem()
.setTag(null);
context.getPlayer()
.getCooldownTracker()
.setCooldown(this, 5);
.setTag(null);
playerEntity.getCooldownTracker()
.setCooldown(this, 5);
}
return ActionResultType.SUCCESS;
}
tag.put("FirstPulley", NBTUtil.writeBlockPos(pos));
context.getItem()
.setTag(tag);
context.getPlayer()
.getCooldownTracker()
.setCooldown(this, 5);
.setTag(tag);
playerEntity.getCooldownTracker()
.setCooldown(this, 5);
return ActionResultType.SUCCESS;
}
@ -123,7 +124,7 @@ public class BeltConnectorItem extends BlockItem {
BlockPos diff = end.subtract(start);
if (diff.getX() == diff.getZ())
facing = Direction.getFacingFromAxis(facing.getAxisDirection(), world.getBlockState(start)
.get(BlockStateProperties.AXIS) == Axis.X ? Axis.Z : Axis.X);
.get(BlockStateProperties.AXIS) == Axis.X ? Axis.Z : Axis.X);
List<BlockPos> beltsToCreate = getBeltChainBetween(start, end, slope, facing);
BlockState beltBlock = AllBlocks.BELT.getDefaultState();
@ -134,8 +135,8 @@ public class BeltConnectorItem extends BlockItem {
if (part == Part.MIDDLE && pulley)
part = Part.PULLEY;
world.setBlockState(pos, beltBlock.with(BeltBlock.SLOPE, slope)
.with(BeltBlock.PART, part)
.with(BeltBlock.HORIZONTAL_FACING, facing), 3);
.with(BeltBlock.PART, part)
.with(BeltBlock.HORIZONTAL_FACING, facing), 3);
}
}
@ -148,7 +149,7 @@ public class BeltConnectorItem extends BlockItem {
axisDirection = diff.getY() > 0 ? AxisDirection.POSITIVE : AxisDirection.NEGATIVE;
else
axisDirection = beltAxis.getCoordinate(diff.getX(), 0, diff.getZ()) > 0 ? AxisDirection.POSITIVE
: AxisDirection.NEGATIVE;
: AxisDirection.NEGATIVE;
return Direction.getFacingFromAxis(axisDirection, beltAxis);
}
@ -197,20 +198,20 @@ public class BeltConnectorItem extends BlockItem {
BlockPos diff = second.subtract(first);
Axis axis = world.getBlockState(first)
.get(BlockStateProperties.AXIS);
.get(BlockStateProperties.AXIS);
int x = diff.getX();
int y = diff.getY();
int z = diff.getZ();
int sames = ((Math.abs(x) == Math.abs(y)) ? 1 : 0) + ((Math.abs(y) == Math.abs(z)) ? 1 : 0)
+ ((Math.abs(z) == Math.abs(x)) ? 1 : 0);
+ ((Math.abs(z) == Math.abs(x)) ? 1 : 0);
if (axis.getCoordinate(x, y, z) != 0)
return false;
if (sames != 1)
return false;
if (axis != world.getBlockState(second)
.get(BlockStateProperties.AXIS))
.get(BlockStateProperties.AXIS))
return false;
TileEntity tileEntity = world.getTileEntity(first);
@ -234,7 +235,7 @@ public class BeltConnectorItem extends BlockItem {
if (ShaftBlock.isShaft(blockState) && blockState.get(ShaftBlock.AXIS) == axis)
continue;
if (!blockState.getMaterial()
.isReplaceable())
.isReplaceable())
return false;
}
@ -248,7 +249,7 @@ public class BeltConnectorItem extends BlockItem {
if (!ShaftBlock.isShaft(world.getBlockState(pos)))
return false;
if (world.getBlockState(pos)
.get(BlockStateProperties.AXIS) == Axis.Y)
.get(BlockStateProperties.AXIS) == Axis.Y)
return false;
return true;
}

View File

@ -29,7 +29,8 @@ import net.minecraft.world.IWorldReader;
public class EncasedBeltBlock extends RotatedPillarKineticBlock {
public static final IProperty<Part> PART = EnumProperty.create("part", Part.class);
public static final BooleanProperty CONNECTED_ALONG_FIRST_COORDINATE = DirectionalAxisKineticBlock.AXIS_ALONG_FIRST_COORDINATE;
public static final BooleanProperty CONNECTED_ALONG_FIRST_COORDINATE =
DirectionalAxisKineticBlock.AXIS_ALONG_FIRST_COORDINATE;
public EncasedBeltBlock(Properties properties) {
super(properties);
@ -40,12 +41,12 @@ public class EncasedBeltBlock extends RotatedPillarKineticBlock {
public boolean shouldCheckWeakPower(BlockState state, IWorldReader world, BlockPos pos, Direction side) {
return false;
}
@Override
public PushReaction getPushReaction(BlockState state) {
return PushReaction.NORMAL;
}
@Override
protected void fillStateContainer(Builder<Block, BlockState> builder) {
super.fillStateContainer(builder.add(PART, CONNECTED_ALONG_FIRST_COORDINATE));
@ -53,8 +54,10 @@ public class EncasedBeltBlock extends RotatedPillarKineticBlock {
@Override
public BlockState getStateForPlacement(BlockItemUseContext context) {
Axis placedAxis = context.getNearestLookingDirection().getAxis();
Axis axis = context.getPlayer().isSneaking() ? placedAxis : getPreferredAxis(context);
Axis placedAxis = context.getNearestLookingDirection()
.getAxis();
Axis axis = context.getPlayer() != null && context.getPlayer()
.isSneaking() ? placedAxis : getPreferredAxis(context);
if (axis == null)
axis = placedAxis;
@ -64,20 +67,20 @@ public class EncasedBeltBlock extends RotatedPillarKineticBlock {
continue;
BlockPos pos = context.getPos();
BlockPos offset = pos.offset(facing);
state = updatePostPlacement(state, facing, context.getWorld().getBlockState(offset), context.getWorld(),
pos, offset);
state = updatePostPlacement(state, facing, context.getWorld()
.getBlockState(offset), context.getWorld(), pos, offset);
}
return state;
}
@Override
public BlockState updatePostPlacement(BlockState stateIn, Direction face, BlockState neighbour, IWorld worldIn,
BlockPos currentPos, BlockPos facingPos) {
BlockPos currentPos, BlockPos facingPos) {
Part part = stateIn.get(PART);
Axis axis = stateIn.get(AXIS);
boolean connectionAlongFirst = stateIn.get(CONNECTED_ALONG_FIRST_COORDINATE);
Axis connectionAxis = connectionAlongFirst ? (axis == Axis.X ? Axis.Y : Axis.X)
: (axis == Axis.Z ? Axis.Y : Axis.Z);
Axis connectionAxis =
connectionAlongFirst ? (axis == Axis.X ? Axis.Y : Axis.X) : (axis == Axis.Z ? Axis.Y : Axis.Z);
Axis faceAxis = face.getAxis();
boolean facingAlongFirst = axis == Axis.X ? faceAxis.isVertical() : faceAxis == Axis.X;
@ -99,8 +102,8 @@ public class EncasedBeltBlock extends RotatedPillarKineticBlock {
Part otherPart = neighbour.get(PART);
Axis otherAxis = neighbour.get(AXIS);
boolean otherConnection = neighbour.get(CONNECTED_ALONG_FIRST_COORDINATE);
Axis otherConnectionAxis = otherConnection ? (otherAxis == Axis.X ? Axis.Y : Axis.X)
: (otherAxis == Axis.Z ? Axis.Y : Axis.Z);
Axis otherConnectionAxis =
otherConnection ? (otherAxis == Axis.X ? Axis.Y : Axis.X) : (otherAxis == Axis.Z ? Axis.Y : Axis.Z);
if (neighbour.get(AXIS) == faceAxis)
return stateIn;
@ -117,12 +120,14 @@ public class EncasedBeltBlock extends RotatedPillarKineticBlock {
if ((part == Part.START) != positive)
part = Part.MIDDLE;
return stateIn.with(PART, part).with(CONNECTED_ALONG_FIRST_COORDINATE, connectionAlongFirst);
return stateIn.with(PART, part)
.with(CONNECTED_ALONG_FIRST_COORDINATE, connectionAlongFirst);
}
@Override
public BlockState updateAfterWrenched(BlockState newState, ItemUseContext context) {
Blocks.AIR.getDefaultState().updateNeighbors(context.getWorld(), context.getPos(), 1);
Blocks.AIR.getDefaultState()
.updateNeighbors(context.getWorld(), context.getPos(), 1);
Axis axis = newState.get(AXIS);
newState = getDefaultState().with(AXIS, axis);
for (Direction facing : Direction.values()) {
@ -130,8 +135,9 @@ public class EncasedBeltBlock extends RotatedPillarKineticBlock {
continue;
BlockPos pos = context.getPos();
BlockPos offset = pos.offset(facing);
newState = updatePostPlacement(newState, facing, context.getWorld().getBlockState(offset), context.getWorld(),
pos, offset); }
newState = updatePostPlacement(newState, facing, context.getWorld()
.getBlockState(offset), context.getWorld(), pos, offset);
}
newState.updateNeighbors(context.getWorld(), context.getPos(), 1 | 2);
return newState;
}
@ -150,13 +156,13 @@ public class EncasedBeltBlock extends RotatedPillarKineticBlock {
Part part = state.get(PART);
Axis axis = state.get(AXIS);
boolean connectionAlongFirst = state.get(CONNECTED_ALONG_FIRST_COORDINATE);
Axis connectionAxis = connectionAlongFirst ? (axis == Axis.X ? Axis.Y : Axis.X)
: (axis == Axis.Z ? Axis.Y : Axis.Z);
Axis connectionAxis =
connectionAlongFirst ? (axis == Axis.X ? Axis.Y : Axis.X) : (axis == Axis.Z ? Axis.Y : Axis.Z);
Axis otherAxis = other.get(AXIS);
boolean otherConnection = other.get(CONNECTED_ALONG_FIRST_COORDINATE);
Axis otherConnectionAxis = otherConnection ? (otherAxis == Axis.X ? Axis.Y : Axis.X)
: (otherAxis == Axis.Z ? Axis.Y : Axis.Z);
Axis otherConnectionAxis =
otherConnection ? (otherAxis == Axis.X ? Axis.Y : Axis.X) : (otherAxis == Axis.Z ? Axis.Y : Axis.Z);
if (otherConnectionAxis != connectionAxis)
return false;

View File

@ -36,11 +36,13 @@ public class EncasedShaftBlock extends RotatedPillarKineticBlock {
@Override
public BlockState getStateForPlacement(BlockItemUseContext context) {
if (context.getPlayer().isSneaking())
if (context.getPlayer() != null && context.getPlayer()
.isSneaking())
return super.getStateForPlacement(context);
Axis preferredAxis = getPreferredAxis(context);
return this.getDefaultState().with(AXIS,
preferredAxis == null ? context.getNearestLookingDirection().getAxis() : preferredAxis);
return this.getDefaultState()
.with(AXIS, preferredAxis == null ? context.getNearestLookingDirection()
.getAxis() : preferredAxis);
}
@Override

View File

@ -52,7 +52,7 @@ public interface IWrenchable {
BlockPos pos = context.getPos();
PlayerEntity player = context.getPlayer();
if (world instanceof ServerWorld) {
if (!player.isCreative())
if (player != null && !player.isCreative())
Block.getDrops(state, (ServerWorld) world, pos, world.getTileEntity(pos), player, context.getItem())
.forEach(itemStack -> {
player.inventory.placeItemBackInInventory(world, itemStack);

View File

@ -6,24 +6,28 @@ import net.minecraft.item.Item;
import net.minecraft.item.ItemUseContext;
import net.minecraft.util.ActionResultType;
import javax.annotation.Nonnull;
public class WrenchItem extends Item {
public WrenchItem(Properties properties) {
super(properties);
}
@Nonnull
@Override
public ActionResultType onItemUse(ItemUseContext context) {
PlayerEntity player = context.getPlayer();
if (!player.isAllowEdit())
if (player == null || !player.isAllowEdit())
return super.onItemUse(context);
BlockState state = context.getWorld().getBlockState(context.getPos());
BlockState state = context.getWorld()
.getBlockState(context.getPos());
if (!(state.getBlock() instanceof IWrenchable))
return super.onItemUse(context);
IWrenchable actor = (IWrenchable) state.getBlock();
if (player.isSneaking())
if (player.isSneaking())
return actor.onSneakWrenched(state, context);
return actor.onWrenched(state, context);
}

View File

@ -21,7 +21,8 @@ public class TreeFertilizerItem extends Item {
@Override
public ActionResultType onItemUse(ItemUseContext context) {
BlockState state = context.getWorld().getBlockState(context.getPos());
BlockState state = context.getWorld()
.getBlockState(context.getPos());
Block block = state.getBlock();
if (block instanceof SaplingBlock) {
@ -34,31 +35,46 @@ public class TreeFertilizerItem extends Item {
BlockPos saplingPos = context.getPos();
for (BlockPos pos : BlockPos.getAllInBoxMutable(-1, 0, -1, 1, 0, 1)) {
if (context.getWorld().getBlockState(saplingPos.add(pos)).getBlock() == block)
if (context.getWorld()
.getBlockState(saplingPos.add(pos))
.getBlock() == block)
world.setBlockState(pos.up(10), state.with(SaplingBlock.STAGE, 1));
}
((SaplingBlock) block).grow(world, world.getRandom(), BlockPos.ZERO.up(10), state.with(SaplingBlock.STAGE, 1));
((SaplingBlock) block).grow(world, world.getRandom(), BlockPos.ZERO.up(10),
state.with(SaplingBlock.STAGE, 1));
for (BlockPos pos : world.blocksAdded.keySet()) {
BlockPos actualPos = pos.add(saplingPos).down(10);
BlockPos actualPos = pos.add(saplingPos)
.down(10);
// Don't replace Bedrock
if (context.getWorld().getBlockState(actualPos).getBlockHardness(context.getWorld(), actualPos) == -1)
if (context.getWorld()
.getBlockState(actualPos)
.getBlockHardness(context.getWorld(), actualPos) == -1)
continue;
// Don't replace solid blocks with leaves
if (!world.getBlockState(pos).isNormalCube(world, pos) && !context.getWorld().getBlockState(actualPos)
.getCollisionShape(context.getWorld(), actualPos).isEmpty())
if (!world.getBlockState(pos)
.isNormalCube(world, pos)
&& !context.getWorld()
.getBlockState(actualPos)
.getCollisionShape(context.getWorld(), actualPos)
.isEmpty())
continue;
if (world.getBlockState(pos).getBlock() == Blocks.GRASS_BLOCK
|| world.getBlockState(pos).getBlock() == Blocks.PODZOL)
if (world.getBlockState(pos)
.getBlock() == Blocks.GRASS_BLOCK
|| world.getBlockState(pos)
.getBlock() == Blocks.PODZOL)
continue;
context.getWorld().setBlockState(actualPos, world.getBlockState(pos));
context.getWorld()
.setBlockState(actualPos, world.getBlockState(pos));
}
if (!context.getPlayer().isCreative())
context.getItem().shrink(1);
if (context.getPlayer() != null && !context.getPlayer()
.isCreative())
context.getItem()
.shrink(1);
return ActionResultType.SUCCESS;
}

View File

@ -38,6 +38,8 @@ import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.fml.DistExecutor;
import net.minecraftforge.fml.network.PacketDistributor;
import javax.annotation.Nonnull;
public class SymmetryWandItem extends Item {
public static final String SYMMETRY = "symmetry";
@ -48,10 +50,13 @@ public class SymmetryWandItem extends Item {
.rarity(Rarity.UNCOMMON));
}
@Nonnull
@Override
public ActionResultType onItemUse(ItemUseContext context) {
PlayerEntity player = context.getPlayer();
BlockPos pos = context.getPos();
if (player == null)
return ActionResultType.PASS;
player.getCooldownTracker()
.setCooldown(this, 5);
ItemStack wand = player.getHeldItem(context.getHand());

View File

@ -41,6 +41,8 @@ import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.fml.DistExecutor;
import net.minecraftforge.fml.network.PacketDistributor;
import javax.annotation.Nonnull;
public abstract class ZapperItem extends Item {
public ZapperItem(Properties properties) {
@ -80,10 +82,11 @@ public abstract class ZapperItem extends Item {
return newStack.getItem() instanceof ZapperItem;
}
@Nonnull
@Override
public ActionResultType onItemUse(ItemUseContext context) {
// Shift -> open GUI
if (context.getPlayer()
if (context.getPlayer() != null && context.getPlayer()
.isSneaking()) {
if (context.getWorld().isRemote) {
DistExecutor.runWhenOn(Dist.CLIENT, () -> () -> {
@ -218,7 +221,7 @@ public abstract class ZapperItem extends Item {
public boolean onEntitySwing(ItemStack stack, LivingEntity entity) {
return true;
}
@Override
public boolean canPlayerBreakBlockWhileHolding(BlockState state, World worldIn, BlockPos pos, PlayerEntity player) {
return false;

View File

@ -23,7 +23,8 @@ public class CrateBlock extends ProperDirectionalBlock implements IWrenchable {
public CrateBlock(Properties p_i48415_1_) {
super(p_i48415_1_);
setDefaultState(getDefaultState().with(FACING, Direction.UP).with(DOUBLE, false));
setDefaultState(getDefaultState().with(FACING, Direction.UP)
.with(DOUBLE, false));
}
@Override
@ -33,17 +34,18 @@ public class CrateBlock extends ProperDirectionalBlock implements IWrenchable {
@Override
public BlockState updatePostPlacement(BlockState stateIn, Direction facing, BlockState facingState, IWorld worldIn,
BlockPos currentPos, BlockPos facingPos) {
BlockPos currentPos, BlockPos facingPos) {
boolean isDouble = stateIn.get(DOUBLE);
Direction blockFacing = stateIn.get(FACING);
boolean isFacingOther = facingState.getBlock() == this && facingState.get(DOUBLE)
&& facingState.get(FACING) == facing.getOpposite();
&& facingState.get(FACING) == facing.getOpposite();
if (!isDouble) {
if (!isFacingOther)
return stateIn;
return stateIn.with(DOUBLE, true).with(FACING, facing);
return stateIn.with(DOUBLE, true)
.with(FACING, facing);
}
if (facing != blockFacing)
@ -59,18 +61,22 @@ public class CrateBlock extends ProperDirectionalBlock implements IWrenchable {
BlockPos pos = context.getPos();
World world = context.getWorld();
if (!context.getPlayer().isSneaking()) {
if (context.getPlayer() == null || !context.getPlayer()
.isSneaking()) {
for (Direction d : Direction.values()) {
BlockState state = world.getBlockState(pos.offset(d));
if (state.getBlock() == this && !state.get(DOUBLE))
return getDefaultState().with(FACING, d).with(DOUBLE, true);
return getDefaultState().with(FACING, d)
.with(DOUBLE, true);
}
}
Direction placedOnFace = context.getFace().getOpposite();
Direction placedOnFace = context.getFace()
.getOpposite();
BlockState state = world.getBlockState(pos.offset(placedOnFace));
if (state.getBlock() == this && !state.get(DOUBLE))
return getDefaultState().with(FACING, placedOnFace).with(DOUBLE, true);
return getDefaultState().with(FACING, placedOnFace)
.with(DOUBLE, true);
return getDefaultState();
}

View File

@ -28,7 +28,8 @@ public class RedstoneContactBlock extends ProperDirectionalBlock implements IPor
public RedstoneContactBlock(Properties properties) {
super(properties);
setDefaultState(getDefaultState().with(POWERED, false).with(FACING, Direction.UP));
setDefaultState(getDefaultState().with(POWERED, false)
.with(FACING, Direction.UP));
}
@Override
@ -39,10 +40,13 @@ public class RedstoneContactBlock extends ProperDirectionalBlock implements IPor
@Override
public BlockState getStateForPlacement(BlockItemUseContext context) {
BlockState state = getDefaultState().with(FACING, context.getNearestLookingDirection().getOpposite());
Direction placeDirection = context.getFace().getOpposite();
BlockState state = getDefaultState().with(FACING, context.getNearestLookingDirection()
.getOpposite());
Direction placeDirection = context.getFace()
.getOpposite();
if (context.getPlayer().isSneaking() || hasValidContact(context.getWorld(), context.getPos(), placeDirection))
if ((context.getPlayer() != null && context.getPlayer()
.isSneaking()) || hasValidContact(context.getWorld(), context.getPos(), placeDirection))
state = state.with(FACING, placeDirection);
if (hasValidContact(context.getWorld(), context.getPos(), state.get(FACING)))
state = state.with(POWERED, true);
@ -52,7 +56,7 @@ public class RedstoneContactBlock extends ProperDirectionalBlock implements IPor
@Override
public BlockState updatePostPlacement(BlockState stateIn, Direction facing, BlockState facingState, IWorld worldIn,
BlockPos currentPos, BlockPos facingPos) {
BlockPos currentPos, BlockPos facingPos) {
if (facing != stateIn.get(FACING))
return stateIn;
boolean hasValidContact = hasValidContact(worldIn, currentPos, facing);

View File

@ -36,6 +36,8 @@ import net.minecraftforge.fml.network.NetworkHooks;
import net.minecraftforge.items.ItemHandlerHelper;
import net.minecraftforge.items.ItemStackHandler;
import javax.annotation.Nonnull;
public class FilterItem extends Item implements INamedContainerProvider {
private FilterType type;
@ -43,22 +45,25 @@ public class FilterItem extends Item implements INamedContainerProvider {
private enum FilterType {
REGULAR, ATTRIBUTE;
}
public static FilterItem regular(Properties properties) {
return new FilterItem(FilterType.REGULAR, properties);
}
public static FilterItem attribute(Properties properties) {
return new FilterItem(FilterType.ATTRIBUTE, properties);
}
private FilterItem(FilterType type, Properties properties) {
super(properties);
this.type = type;
}
@Nonnull
@Override
public ActionResultType onItemUse(ItemUseContext context) {
if (context.getPlayer() == null)
return ActionResultType.PASS;
return onItemRightClick(context.getWorld(), context.getPlayer(), context.getHand()).getType();
}
@ -73,16 +78,17 @@ public class FilterItem extends Item implements INamedContainerProvider {
ItemDescription.add(tooltip, makeSummary);
}
}
private List<String> makeSummary(ItemStack filter) {
List<String> list = new ArrayList<>();
if (type == FilterType.REGULAR) {
ItemStackHandler filterItems = getFilterItems(filter);
boolean blacklist = filter.getOrCreateTag().getBoolean("Blacklist");
boolean blacklist = filter.getOrCreateTag()
.getBoolean("Blacklist");
list.add(TextFormatting.GOLD
+ (blacklist ? Lang.translate("gui.filter.blacklist") : Lang.translate("gui.filter.whitelist")));
+ (blacklist ? Lang.translate("gui.filter.blacklist") : Lang.translate("gui.filter.whitelist")));
int count = 0;
for (int i = 0; i < filterItems.getSlots(); i++) {
if (count > 3) {
@ -93,7 +99,8 @@ public class FilterItem extends Item implements INamedContainerProvider {
ItemStack filterStack = filterItems.getStackInSlot(i);
if (filterStack.isEmpty())
continue;
list.add(TextFormatting.GRAY + "- " + filterStack.getDisplayName().getFormattedText());
list.add(TextFormatting.GRAY + "- " + filterStack.getDisplayName()
.getFormattedText());
count++;
}
@ -102,15 +109,17 @@ public class FilterItem extends Item implements INamedContainerProvider {
}
if (type == FilterType.ATTRIBUTE) {
WhitelistMode whitelistMode = WhitelistMode.values()[filter.getOrCreateTag().getInt("WhitelistMode")];
WhitelistMode whitelistMode = WhitelistMode.values()[filter.getOrCreateTag()
.getInt("WhitelistMode")];
list.add(TextFormatting.GOLD + (whitelistMode == WhitelistMode.WHITELIST_CONJ
? Lang.translate("gui.attribute_filter.whitelist_conjunctive")
: whitelistMode == WhitelistMode.WHITELIST_DISJ
? Lang.translate("gui.attribute_filter.whitelist_disjunctive")
: Lang.translate("gui.attribute_filter.blacklist")));
? Lang.translate("gui.attribute_filter.whitelist_conjunctive")
: whitelistMode == WhitelistMode.WHITELIST_DISJ
? Lang.translate("gui.attribute_filter.whitelist_disjunctive")
: Lang.translate("gui.attribute_filter.blacklist")));
int count = 0;
ListNBT attributes = filter.getOrCreateTag().getList("MatchedAttributes", NBT.TAG_COMPOUND);
ListNBT attributes = filter.getOrCreateTag()
.getList("MatchedAttributes", NBT.TAG_COMPOUND);
for (INBT inbt : attributes) {
ItemAttribute attribute = ItemAttribute.fromNBT((CompoundNBT) inbt);
if (count > 3) {
@ -174,15 +183,17 @@ public class FilterItem extends Item implements INamedContainerProvider {
private static boolean test(World world, ItemStack stack, ItemStack filter, boolean matchNBT) {
if (filter.isEmpty())
return true;
if (!(filter.getItem() instanceof FilterItem))
return (matchNBT ? ItemHandlerHelper.canItemStacksStack(filter, stack)
: ItemStack.areItemsEqual(filter, stack));
: ItemStack.areItemsEqual(filter, stack));
if (AllItems.FILTER.get() == filter.getItem()) {
ItemStackHandler filterItems = getFilterItems(filter);
boolean respectNBT = filter.getOrCreateTag().getBoolean("RespectNBT");
boolean blacklist = filter.getOrCreateTag().getBoolean("Blacklist");
boolean respectNBT = filter.getOrCreateTag()
.getBoolean("RespectNBT");
boolean blacklist = filter.getOrCreateTag()
.getBoolean("Blacklist");
for (int slot = 0; slot < filterItems.getSlots(); slot++) {
ItemStack stackInSlot = filterItems.getStackInSlot(slot);
if (stackInSlot.isEmpty())
@ -195,8 +206,10 @@ public class FilterItem extends Item implements INamedContainerProvider {
}
if (AllItems.ATTRIBUTE_FILTER.get() == filter.getItem()) {
WhitelistMode whitelistMode = WhitelistMode.values()[filter.getOrCreateTag().getInt("WhitelistMode")];
ListNBT attributes = filter.getOrCreateTag().getList("MatchedAttributes", NBT.TAG_COMPOUND);
WhitelistMode whitelistMode = WhitelistMode.values()[filter.getOrCreateTag()
.getInt("WhitelistMode")];
ListNBT attributes = filter.getOrCreateTag()
.getList("MatchedAttributes", NBT.TAG_COMPOUND);
for (INBT inbt : attributes) {
ItemAttribute attribute = ItemAttribute.fromNBT((CompoundNBT) inbt);
boolean matches = attribute.appliesTo(stack, world);

View File

@ -41,6 +41,8 @@ import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.fml.DistExecutor;
import net.minecraftforge.fml.common.thread.SidedThreadGroups;
import javax.annotation.Nonnull;
public class SchematicItem extends Item {
public SchematicItem(Properties properties) {
@ -70,8 +72,10 @@ public class SchematicItem extends Item {
@OnlyIn(value = Dist.CLIENT)
public void addInformation(ItemStack stack, World worldIn, List<ITextComponent> tooltip, ITooltipFlag flagIn) {
if (stack.hasTag()) {
if (stack.getTag().contains("File"))
tooltip.add(new StringTextComponent(TextFormatting.GOLD + stack.getTag().getString("File")));
if (stack.getTag()
.contains("File"))
tooltip.add(new StringTextComponent(TextFormatting.GOLD + stack.getTag()
.getString("File")));
} else {
tooltip.add(new StringTextComponent(TextFormatting.RED + Lang.translate("schematic.invalid")));
}
@ -95,12 +99,15 @@ public class SchematicItem extends Item {
public static Template loadSchematic(ItemStack blueprint) {
Template t = new Template();
String owner = blueprint.getTag().getString("Owner");
String schematic = blueprint.getTag().getString("File");
String owner = blueprint.getTag()
.getString("Owner");
String schematic = blueprint.getTag()
.getString("File");
String filepath = "";
if (Thread.currentThread().getThreadGroup() == SidedThreadGroups.SERVER)
if (Thread.currentThread()
.getThreadGroup() == SidedThreadGroups.SERVER)
filepath = "schematics/uploaded/" + owner + "/" + schematic;
else
filepath = "schematics/" + schematic;
@ -121,9 +128,10 @@ public class SchematicItem extends Item {
return t;
}
@Nonnull
@Override
public ActionResultType onItemUse(ItemUseContext context) {
if (!onItemUse(context.getPlayer(), context.getHand()))
if (context.getPlayer() != null && !onItemUse(context.getPlayer(), context.getHand()))
return super.onItemUse(context);
return ActionResultType.SUCCESS;
}
@ -138,7 +146,8 @@ public class SchematicItem extends Item {
private boolean onItemUse(PlayerEntity player, Hand hand) {
if (!player.isSneaking() || hand != Hand.MAIN_HAND)
return false;
if (!player.getHeldItem(hand).hasTag())
if (!player.getHeldItem(hand)
.hasTag())
return false;
DistExecutor.runWhenOn(Dist.CLIENT, () -> this::displayBlueprintScreen);
return true;