Search in sources :

Example 6 with Lockable

use of pl.asie.charset.api.locks.Lockable in project Charset by CharsetMC.

the class EntityLock method applyPlayerInteraction.

@Override
public EnumActionResult applyPlayerInteraction(EntityPlayer player, Vec3d vec, EnumHand hand) {
    if (!world.isRemote && hand == EnumHand.MAIN_HAND && lockKey != null && getAttachedLock() != null) {
        boolean canUnlock = LockEventHandler.unlockOrRaiseError(player, getAttachedTile(), getAttachedLock());
        if (canUnlock) {
            Lockable lock = getAttachedLock();
            if (lock != null) {
                locked = false;
                BlockPos pos = this.hangingPosition.offset(this.facingDirection.getOpposite());
                IBlockState state = world.getBlockState(pos);
                state.getBlock().onBlockActivated(world, pos, state, player, hand, this.facingDirection, 0.5F + this.facingDirection.getFrontOffsetX() * 0.5F, 0.5F + this.facingDirection.getFrontOffsetY() * 0.5F, 0.5F + this.facingDirection.getFrontOffsetZ() * 0.5F);
                locked = true;
            }
            return EnumActionResult.SUCCESS;
        } else {
            return EnumActionResult.FAIL;
        }
    }
    return EnumActionResult.SUCCESS;
}
Also used : IBlockState(net.minecraft.block.state.IBlockState) BlockPos(net.minecraft.util.math.BlockPos) Lockable(pl.asie.charset.api.locks.Lockable)

Example 7 with Lockable

use of pl.asie.charset.api.locks.Lockable in project Charset by CharsetMC.

the class LockEventHandler method onAttachCapabilities.

@SubscribeEvent
public void onAttachCapabilities(AttachCapabilitiesEvent<TileEntity> event) {
    TileEntity tile = event.getObject();
    ResourceLocation location = TileEntity.getKey(tile.getClass());
    if (// f.e. IC2 energy net internals
    location == null)
        return;
    ThreeState state = CharsetIMC.INSTANCE.allows("lock", location);
    boolean hasCap = state == ThreeState.YES;
    if (state == ThreeState.MAYBE) {
        if (tile instanceof TileEntityLockable) {
            hasCap = true;
        }
    }
    if (hasCap) {
        if (PROVIDER == null) {
            PROVIDER = new CapabilityProviderFactory<>(Capabilities.LOCKABLE, Capabilities.LOCKABLE_STORAGE);
        }
        event.addCapability(LOCKABLE, PROVIDER.create(new Lockable(tile)));
    }
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) ThreeState(pl.asie.charset.lib.utils.ThreeState) TileEntityLockable(net.minecraft.tileentity.TileEntityLockable) ResourceLocation(net.minecraft.util.ResourceLocation) TileEntityLockable(net.minecraft.tileentity.TileEntityLockable) Lockable(pl.asie.charset.api.locks.Lockable) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent)

Example 8 with Lockable

use of pl.asie.charset.api.locks.Lockable in project Charset by CharsetMC.

the class LockEventHandler method onBlockBreak.

@SubscribeEvent
public void onBlockBreak(BlockEvent.BreakEvent event) {
    IBlockState state = event.getState();
    if (state.getBlock().hasTileEntity(state)) {
        TileEntity tile = event.getWorld().getTileEntity(event.getPos());
        Lockable lockable = getLock(tile);
        if (lockable != null && !unlockOrRaiseError(event.getPlayer(), tile, lockable)) {
            event.setCanceled(true);
        }
    }
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) IBlockState(net.minecraft.block.state.IBlockState) TileEntityLockable(net.minecraft.tileentity.TileEntityLockable) Lockable(pl.asie.charset.api.locks.Lockable) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent)

Example 9 with Lockable

use of pl.asie.charset.api.locks.Lockable in project Charset by CharsetMC.

the class LockEventHandler method getLock.

// ENSURE LocksCapabilityHandler has all of the capabilities used here whitelisted!
public static Lockable getLock(TileEntity tile) {
    if (tile != null) {
        if (tile.hasCapability(Capabilities.MULTIBLOCK_STRUCTURE, null)) {
            IMultiblockStructure structure = tile.getCapability(Capabilities.MULTIBLOCK_STRUCTURE, null);
            Iterator<BlockPos> iterator = structure.iterator();
            while (iterator.hasNext()) {
                TileEntity tile2 = tile.getWorld().getTileEntity(iterator.next());
                if (tile2 != null && tile2.hasCapability(Capabilities.LOCKABLE, null)) {
                    Lockable lock = tile2.getCapability(Capabilities.LOCKABLE, null);
                    if (lock.hasLock() && lock.getLock().isLockValid(tile2) && lock.getLock().isLocked()) {
                        return lock;
                    }
                }
            }
        } else if (tile.hasCapability(Capabilities.LOCKABLE, null)) {
            Lockable lock = tile.getCapability(Capabilities.LOCKABLE, null);
            if (lock.hasLock() && lock.getLock().isLockValid(tile) && lock.getLock().isLocked()) {
                return lock;
            }
        }
    }
    return null;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) BlockPos(net.minecraft.util.math.BlockPos) TileEntityLockable(net.minecraft.tileentity.TileEntityLockable) Lockable(pl.asie.charset.api.locks.Lockable) IMultiblockStructure(pl.asie.charset.api.lib.IMultiblockStructure)

Aggregations

Lockable (pl.asie.charset.api.locks.Lockable)9 TileEntity (net.minecraft.tileentity.TileEntity)6 TileEntityLockable (net.minecraft.tileentity.TileEntityLockable)6 SubscribeEvent (net.minecraftforge.fml.common.eventhandler.SubscribeEvent)4 BlockPos (net.minecraft.util.math.BlockPos)3 IBlockState (net.minecraft.block.state.IBlockState)2 IMultiblockStructure (pl.asie.charset.api.lib.IMultiblockStructure)2 EntityPlayer (net.minecraft.entity.player.EntityPlayer)1 ResourceLocation (net.minecraft.util.ResourceLocation)1 CustomCarryHandler (pl.asie.charset.api.carry.CustomCarryHandler)1 IMovable (pl.asie.charset.api.lib.IMovable)1 DefaultAudioReceiver (pl.asie.charset.lib.capability.audio.DefaultAudioReceiver)1 DefaultAudioSource (pl.asie.charset.lib.capability.audio.DefaultAudioSource)1 DefaultItemInsertionHandler (pl.asie.charset.lib.capability.inventory.DefaultItemInsertionHandler)1 DummyLaserReceiver (pl.asie.charset.lib.capability.laser.DummyLaserReceiver)1 DefaultPipeView (pl.asie.charset.lib.capability.pipe.DefaultPipeView)1 DummyBarrel (pl.asie.charset.lib.capability.storage.DummyBarrel)1 ThreeState (pl.asie.charset.lib.utils.ThreeState)1