use of mekanism.common.tile.interfaces.ITileDirectional in project Mekanism by mekanism.
the class ItemTierInstaller method useOn.
@Nonnull
@Override
public ActionResultType useOn(ItemUseContext context) {
PlayerEntity player = context.getPlayer();
World world = context.getLevel();
if (world.isClientSide || player == null) {
return ActionResultType.PASS;
}
BlockPos pos = context.getClickedPos();
BlockState state = world.getBlockState(pos);
Block block = state.getBlock();
if (Attribute.has(block, AttributeUpgradeable.class)) {
AttributeUpgradeable upgradeableBlock = Attribute.get(block, AttributeUpgradeable.class);
BaseTier baseTier = Attribute.getBaseTier(block);
if (baseTier == fromTier && baseTier != toTier) {
BlockState upgradeState = upgradeableBlock.upgradeResult(state, toTier);
if (state == upgradeState) {
return ActionResultType.PASS;
}
TileEntity tile = WorldUtils.getTileEntity(world, pos);
if (tile instanceof ITierUpgradable) {
if (tile instanceof TileEntityMekanism && !((TileEntityMekanism) tile).playersUsing.isEmpty()) {
return ActionResultType.FAIL;
}
IUpgradeData upgradeData = ((ITierUpgradable) tile).getUpgradeData();
if (upgradeData == null) {
if (((ITierUpgradable) tile).canBeUpgraded()) {
Mekanism.logger.warn("Got no upgrade data for block {} at position: {} in {} but it said it would be able to provide some.", block, pos, world);
return ActionResultType.FAIL;
}
} else {
world.setBlockAndUpdate(pos, upgradeState);
// TODO: Make it so it doesn't have to be a TileEntityMekanism?
TileEntityMekanism upgradedTile = WorldUtils.getTileEntity(TileEntityMekanism.class, world, pos);
if (upgradedTile == null) {
Mekanism.logger.warn("Error upgrading block at position: {} in {}.", pos, world);
return ActionResultType.FAIL;
} else {
if (tile instanceof ITileDirectional && ((ITileDirectional) tile).isDirectional()) {
upgradedTile.setFacing(((ITileDirectional) tile).getDirection());
}
upgradedTile.parseUpgradeData(upgradeData);
upgradedTile.sendUpdatePacket();
upgradedTile.setChanged();
if (!player.isCreative()) {
context.getItemInHand().shrink(1);
}
return ActionResultType.SUCCESS;
}
}
}
}
}
return ActionResultType.PASS;
}
Aggregations