Search in sources :

Example 1 with IControllerTile

use of mods.railcraft.api.signals.IControllerTile in project Railcraft by Railcraft.

the class ItemSignalLabel method onItemUse.

@Override
public EnumActionResult onItemUse(ItemStack stack, EntityPlayer playerIn, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
    if (Game.isHost(worldIn) && playerIn.isSneaking() && stack.hasDisplayName()) {
        TileEntity tile = worldIn.getTileEntity(pos);
        Set<AbstractPair> pairs = new HashSet<AbstractPair>();
        if (tile instanceof IReceiverTile) {
            pairs.add(((IReceiverTile) tile).getReceiver());
        }
        if (tile instanceof IControllerTile) {
            pairs.add(((IControllerTile) tile).getController());
        }
        if (tile instanceof ISignalBlockTile) {
            pairs.add(((ISignalBlockTile) tile).getSignalBlock());
        }
        if (!pairs.isEmpty()) {
            String newName = stack.getDisplayName();
            boolean done = false;
            for (AbstractPair pair : pairs) {
                if (!newName.equals(pair.getName())) {
                    pair.setName(newName);
                    done = true;
                }
            }
            if (done) {
                --stack.stackSize;
                PlayerPlugin.swingArm(playerIn, hand);
                IBlockState state = WorldPlugin.getBlockState(worldIn, pos);
                worldIn.notifyBlockUpdate(pos, state, state, 3);
                return EnumActionResult.SUCCESS;
            }
        }
    }
    return super.onItemUse(stack, playerIn, worldIn, pos, hand, facing, hitX, hitY, hitZ);
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) IReceiverTile(mods.railcraft.api.signals.IReceiverTile) IBlockState(net.minecraft.block.state.IBlockState) IControllerTile(mods.railcraft.api.signals.IControllerTile) ISignalBlockTile(mods.railcraft.api.signals.ISignalBlockTile) AbstractPair(mods.railcraft.api.signals.AbstractPair) HashSet(java.util.HashSet)

Example 2 with IControllerTile

use of mods.railcraft.api.signals.IControllerTile in project Railcraft by Railcraft.

the class ItemSignalTuner method onItemUse.

//TODO: Add chat name highlighting formatting styles
@Override
public EnumActionResult onItemUse(ItemStack stack, EntityPlayer playerIn, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
    if (actionCleanPairing(stack, playerIn, worldIn, IControllerTile.class, IControllerTile::getController)) {
        return EnumActionResult.SUCCESS;
    }
    TileEntity tile = worldIn.getTileEntity(pos);
    if (tile != null) {
        WorldCoordinate previousTarget = getPairData(stack);
        if (tile instanceof IReceiverTile && previousTarget != null) {
            if (Game.isHost(worldIn)) {
                SignalReceiver receiver = ((IReceiverTile) tile).getReceiver();
                if (!Objects.equals(pos, previousTarget.getPos())) {
                    tile = worldIn.getTileEntity(previousTarget.getPos());
                    if (tile instanceof IControllerTile) {
                        SignalController controller = ((IControllerTile) tile).getController();
                        if (receiver.getTile() != controller.getTile()) {
                            controller.registerReceiver(receiver);
                            controller.endPairing();
                            ChatPlugin.sendLocalizedChatFromServer(playerIn, LOC_PREFIX + "success", controller.getLocalizationTag(), receiver.getLocalizationTag());
                            clearPairData(stack);
                            return EnumActionResult.SUCCESS;
                        }
                    } else if (WorldPlugin.isBlockLoaded(worldIn, previousTarget.getPos())) {
                        ChatPlugin.sendLocalizedChatFromServer(playerIn, LOC_PREFIX + "abandon.gone");
                        clearPairData(stack);
                    } else {
                        ChatPlugin.sendLocalizedChatFromServer(playerIn, LOC_PREFIX + "abandon.chunk");
                        clearPairData(stack);
                    }
                }
            }
        } else if (tile instanceof IControllerTile) {
            if (Game.isHost(worldIn)) {
                SignalController controller = ((IControllerTile) tile).getController();
                if (previousTarget == null || !Objects.equals(pos, previousTarget.getPos())) {
                    ChatPlugin.sendLocalizedChatFromServer(playerIn, LOC_PREFIX + "start", controller.getLocalizationTag());
                    setPairData(stack, tile);
                    controller.startPairing();
                } else {
                    ChatPlugin.sendLocalizedChatFromServer(playerIn, LOC_PREFIX + "stop", controller.getLocalizationTag());
                    controller.endPairing();
                    clearPairData(stack);
                }
            }
        } else
            return EnumActionResult.PASS;
        return EnumActionResult.SUCCESS;
    }
    return EnumActionResult.PASS;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) IReceiverTile(mods.railcraft.api.signals.IReceiverTile) WorldCoordinate(mods.railcraft.api.core.WorldCoordinate) IControllerTile(mods.railcraft.api.signals.IControllerTile) SignalController(mods.railcraft.api.signals.SignalController) SignalReceiver(mods.railcraft.api.signals.SignalReceiver)

Aggregations

IControllerTile (mods.railcraft.api.signals.IControllerTile)2 IReceiverTile (mods.railcraft.api.signals.IReceiverTile)2 TileEntity (net.minecraft.tileentity.TileEntity)2 HashSet (java.util.HashSet)1 WorldCoordinate (mods.railcraft.api.core.WorldCoordinate)1 AbstractPair (mods.railcraft.api.signals.AbstractPair)1 ISignalBlockTile (mods.railcraft.api.signals.ISignalBlockTile)1 SignalController (mods.railcraft.api.signals.SignalController)1 SignalReceiver (mods.railcraft.api.signals.SignalReceiver)1 IBlockState (net.minecraft.block.state.IBlockState)1