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;
}
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)));
}
}
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);
}
}
}
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;
}
Aggregations