Search in sources :

Example 1 with ISpikeMaulTarget

use of mods.railcraft.api.core.items.ISpikeMaulTarget in project Railcraft by Railcraft.

the class ItemSpikeMaul method onItemUse.

@Override
public EnumActionResult onItemUse(ItemStack stack, EntityPlayer playerIn, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
    playerIn.swingArm(hand);
    if (ISpikeMaulTarget.spikeMaulTargets.isEmpty())
        return EnumActionResult.PASS;
    IBlockState oldState = WorldPlugin.getBlockState(worldIn, pos);
    TileEntity oldTile = null;
    if (oldState.getBlock().hasTileEntity(oldState)) {
        oldTile = WorldPlugin.getBlockTile(worldIn, pos);
    }
    if (!TrackTools.isRailBlock(oldState))
        return EnumActionResult.PASS;
    TrackType trackType = TrackTools.getTrackTypeAt(worldIn, pos, oldState);
    BlockRailBase.EnumRailDirection shape = TrackTools.getTrackDirectionRaw(oldState);
    if (!TrackShapeHelper.isAscending(shape)) {
        Deque<ISpikeMaulTarget> targets = new LinkedList<>(ISpikeMaulTarget.spikeMaulTargets);
        Set<ISpikeMaulTarget> tried = new HashSet<>();
        boolean foundMatch = false;
        ISpikeMaulTarget target;
        while ((target = targets.poll()) != null && !tried.contains(target)) {
            if (target.matches(worldIn, pos, oldState)) {
                foundMatch = true;
                break;
            } else {
                tried.add(target);
                targets.addLast(target);
            }
        }
        if (foundMatch) {
            if (Game.isClient(worldIn))
                return EnumActionResult.SUCCESS;
            WorldPlugin.setBlockToAir(worldIn, pos);
            ChargeManager.getNetwork(worldIn).deregisterChargeNode(pos);
            while ((target = targets.poll()) != null) {
                if (target.setToTarget(worldIn, pos, oldState, playerIn, shape, trackType)) {
                    SoundHelper.playPlaceSoundForBlock(worldIn, pos);
                    stack.damageItem(1, playerIn);
                    return EnumActionResult.SUCCESS;
                }
            }
            WorldPlugin.setBlockState(worldIn, pos, oldState);
            if (oldTile != null) {
                oldTile.validate();
                worldIn.setTileEntity(pos, oldTile);
            }
            return EnumActionResult.FAIL;
        }
    }
    return EnumActionResult.PASS;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) IBlockState(net.minecraft.block.state.IBlockState) ISpikeMaulTarget(mods.railcraft.api.core.items.ISpikeMaulTarget) TrackType(mods.railcraft.api.tracks.TrackType) BlockRailBase(net.minecraft.block.BlockRailBase)

Aggregations

ISpikeMaulTarget (mods.railcraft.api.core.items.ISpikeMaulTarget)1 TrackType (mods.railcraft.api.tracks.TrackType)1 BlockRailBase (net.minecraft.block.BlockRailBase)1 IBlockState (net.minecraft.block.state.IBlockState)1 TileEntity (net.minecraft.tileentity.TileEntity)1