Search in sources :

Example 1 with ITierUpgradable

use of mekanism.common.tile.interfaces.ITierUpgradable 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;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) TileEntityMekanism(mekanism.common.tile.base.TileEntityMekanism) ITileDirectional(mekanism.common.tile.interfaces.ITileDirectional) BlockState(net.minecraft.block.BlockState) ITierUpgradable(mekanism.common.tile.interfaces.ITierUpgradable) IUpgradeData(mekanism.common.upgrade.IUpgradeData) AttributeUpgradeable(mekanism.common.block.attribute.AttributeUpgradeable) Block(net.minecraft.block.Block) BlockPos(net.minecraft.util.math.BlockPos) World(net.minecraft.world.World) BaseTier(mekanism.api.tier.BaseTier) PlayerEntity(net.minecraft.entity.player.PlayerEntity) Nonnull(javax.annotation.Nonnull)

Aggregations

Nonnull (javax.annotation.Nonnull)1 BaseTier (mekanism.api.tier.BaseTier)1 AttributeUpgradeable (mekanism.common.block.attribute.AttributeUpgradeable)1 TileEntityMekanism (mekanism.common.tile.base.TileEntityMekanism)1 ITierUpgradable (mekanism.common.tile.interfaces.ITierUpgradable)1 ITileDirectional (mekanism.common.tile.interfaces.ITileDirectional)1 IUpgradeData (mekanism.common.upgrade.IUpgradeData)1 Block (net.minecraft.block.Block)1 BlockState (net.minecraft.block.BlockState)1 PlayerEntity (net.minecraft.entity.player.PlayerEntity)1 TileEntity (net.minecraft.tileentity.TileEntity)1 BlockPos (net.minecraft.util.math.BlockPos)1 World (net.minecraft.world.World)1