1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-12-14 20:20:30 +00:00

Allow computers and inventories to be locked

Just like vanilla locking, this isn't accessible in survival.

> "im retired! im retired!!", i continue to insist as i slowly shrink
> and transform into a corn cob.
This commit is contained in:
Jonathan Coates 2022-05-27 19:16:45 +01:00
parent 8b89d88d04
commit 2efad38f53
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
8 changed files with 59 additions and 13 deletions

View File

@ -62,10 +62,9 @@ public abstract class TileGeneric extends TileEntity
return 8.0;
}
public boolean isUsable( PlayerEntity player, boolean ignoreRange )
public boolean isUsable( PlayerEntity player )
{
if( player == null || !player.isAlive() || getLevel().getBlockEntity( getBlockPos() ) != this ) return false;
if( ignoreRange ) return true;
double range = getInteractRange( player );
BlockPos pos = getBlockPos();

View File

@ -113,12 +113,12 @@ public class TileCommandComputer extends TileComputer
}
@Override
public boolean isUsable( PlayerEntity player, boolean ignoreRange )
public boolean isUsable( PlayerEntity player )
{
return isUsable( player ) && super.isUsable( player, ignoreRange );
return isCommandUsable( player ) && super.isUsable( player );
}
public static boolean isUsable( PlayerEntity player )
public static boolean isCommandUsable( PlayerEntity player )
{
MinecraftServer server = player.getServer();
if( server == null || !server.isCommandBlockEnabled() )

View File

@ -53,7 +53,7 @@ public class TileComputer extends TileComputerBase
protected boolean isUsableByPlayer( PlayerEntity player )
{
return isUsable( player, false );
return isUsable( player );
}
@Override

View File

@ -26,6 +26,7 @@ import net.minecraft.item.Items;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.network.play.server.SUpdateTileEntityPacket;
import net.minecraft.tileentity.ITickableTileEntity;
import net.minecraft.tileentity.LockableTileEntity;
import net.minecraft.tileentity.TileEntityType;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.Direction;
@ -36,6 +37,7 @@ import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.LockCode;
import net.minecraftforge.common.util.NonNullConsumer;
import javax.annotation.Nonnull;
@ -58,6 +60,8 @@ public abstract class TileComputerBase extends TileGeneric implements IComputerT
private int invalidSides = 0;
private final NonNullConsumer<Object>[] invalidate;
private LockCode lockCode = LockCode.NO_LOCK;
private final ComputerFamily family;
public TileComputerBase( TileEntityType<? extends TileGeneric> type, ComputerFamily family )
@ -112,6 +116,12 @@ public abstract class TileComputerBase extends TileGeneric implements IComputerT
return false;
}
@Override
public boolean isUsable( PlayerEntity player )
{
return super.isUsable( player ) && LockableTileEntity.canUnlock( player, lockCode, getDisplayName() );
}
@Nonnull
@Override
public ActionResultType onActivate( PlayerEntity player, Hand hand, BlockRayTraceResult hit )
@ -130,7 +140,7 @@ public abstract class TileComputerBase extends TileGeneric implements IComputerT
else if( !player.isCrouching() )
{
// Regular right click to activate computer
if( !getLevel().isClientSide && isUsable( player, false ) )
if( !getLevel().isClientSide && isUsable( player ) )
{
createServerComputer().turnOn();
new ComputerContainerData( createServerComputer() ).open( player, this );
@ -201,6 +211,8 @@ public abstract class TileComputerBase extends TileGeneric implements IComputerT
if( label != null ) nbt.putString( NBT_LABEL, label );
nbt.putBoolean( NBT_ON, on );
lockCode.addToTag( nbt );
return super.save( nbt );
}
@ -213,6 +225,8 @@ public abstract class TileComputerBase extends TileGeneric implements IComputerT
computerID = nbt.contains( NBT_ID ) ? nbt.getInt( NBT_ID ) : -1;
label = nbt.contains( NBT_LABEL ) ? nbt.getString( NBT_LABEL ) : null;
on = startOn = nbt.getBoolean( NBT_ON );
lockCode = LockCode.fromTag( nbt );
}
protected boolean isPeripheralBlockedOnSide( ComputerSide localSide )

View File

@ -43,7 +43,7 @@ public class ContainerViewComputer extends ComputerMenuWithoutInventory
}
// If we're a command computer then ensure we're in creative
if( computer.getFamily() == ComputerFamily.COMMAND && !TileCommandComputer.isUsable( player ) )
if( computer.getFamily() == ComputerFamily.COMMAND && !TileCommandComputer.isCommandUsable( player ) )
{
return false;
}

View File

@ -26,12 +26,14 @@ import net.minecraft.inventory.container.INamedContainerProvider;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.tileentity.ITickableTileEntity;
import net.minecraft.tileentity.LockableTileEntity;
import net.minecraft.tileentity.TileEntityType;
import net.minecraft.util.*;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.LockCode;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.fml.network.NetworkHooks;
@ -58,6 +60,7 @@ public final class TileDiskDrive extends TileGeneric implements DefaultInventory
}
ITextComponent customName;
private LockCode lockCode;
private final Map<IComputerAccess, MountInfo> computers = new HashMap<>();
@ -92,6 +95,12 @@ public final class TileDiskDrive extends TileGeneric implements DefaultInventory
peripheralCap = CapabilityUtil.invalidate( peripheralCap );
}
@Override
public boolean isUsable( PlayerEntity player )
{
return super.isUsable( player ) && LockableTileEntity.canUnlock( player, lockCode, getDisplayName() );
}
@Nonnull
@Override
public ActionResultType onActivate( PlayerEntity player, Hand hand, BlockRayTraceResult hit )
@ -111,7 +120,10 @@ public final class TileDiskDrive extends TileGeneric implements DefaultInventory
else
{
// Open the GUI
if( !getLevel().isClientSide ) NetworkHooks.openGui( (ServerPlayerEntity) player, this );
if( !getLevel().isClientSide && isUsable( player ) )
{
NetworkHooks.openGui( (ServerPlayerEntity) player, this );
}
return ActionResultType.SUCCESS;
}
}
@ -132,6 +144,8 @@ public final class TileDiskDrive extends TileGeneric implements DefaultInventory
diskStack = ItemStack.of( item );
diskMount = null;
}
lockCode = LockCode.fromTag( nbt );
}
@Nonnull
@ -146,6 +160,9 @@ public final class TileDiskDrive extends TileGeneric implements DefaultInventory
diskStack.save( item );
nbt.put( NBT_ITEM, item );
}
lockCode.addToTag( nbt );
return super.save( nbt );
}
@ -297,7 +314,7 @@ public final class TileDiskDrive extends TileGeneric implements DefaultInventory
@Override
public boolean stillValid( @Nonnull PlayerEntity player )
{
return isUsable( player, false );
return isUsable( player );
}
@Override

View File

@ -22,12 +22,14 @@ import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.tileentity.LockableTileEntity;
import net.minecraft.tileentity.TileEntityType;
import net.minecraft.util.*;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.util.math.vector.Vector3d;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.LockCode;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.fml.network.NetworkHooks;
@ -54,6 +56,7 @@ public final class TilePrinter extends TileGeneric implements DefaultSidedInvent
private static final int[] SIDE_SLOTS = new int[] { 0 };
ITextComponent customName;
private LockCode lockCode;
private final NonNullList<ItemStack> inventory = NonNullList.withSize( SLOTS, ItemStack.EMPTY );
private final SidedCaps<IItemHandler> itemHandlerCaps =
@ -83,13 +86,22 @@ public final class TilePrinter extends TileGeneric implements DefaultSidedInvent
peripheralCap = CapabilityUtil.invalidate( peripheralCap );
}
@Override
public boolean isUsable( PlayerEntity player )
{
return super.isUsable( player ) && LockableTileEntity.canUnlock( player, lockCode, getDisplayName() );
}
@Nonnull
@Override
public ActionResultType onActivate( PlayerEntity player, Hand hand, BlockRayTraceResult hit )
{
if( player.isCrouching() ) return ActionResultType.PASS;
if( !getLevel().isClientSide ) NetworkHooks.openGui( (ServerPlayerEntity) player, this );
if( !getLevel().isClientSide && isUsable( player ) )
{
NetworkHooks.openGui( (ServerPlayerEntity) player, this );
}
return ActionResultType.SUCCESS;
}
@ -110,6 +122,8 @@ public final class TilePrinter extends TileGeneric implements DefaultSidedInvent
// Read inventory
ItemStackHelper.loadAllItems( nbt, inventory );
lockCode = LockCode.fromTag( nbt );
}
@Nonnull
@ -129,6 +143,8 @@ public final class TilePrinter extends TileGeneric implements DefaultSidedInvent
// Write inventory
ItemStackHelper.saveAllItems( nbt, inventory );
lockCode.addToTag( nbt );
return super.save( nbt );
}
@ -231,7 +247,7 @@ public final class TilePrinter extends TileGeneric implements DefaultSidedInvent
@Override
public boolean stillValid( @Nonnull PlayerEntity playerEntity )
{
return isUsable( playerEntity, false );
return isUsable( playerEntity );
}
// ISidedInventory implementation

View File

@ -479,7 +479,7 @@ public class TileTurtle extends TileComputerBase implements ITurtleTile, Default
@Override
public boolean stillValid( @Nonnull PlayerEntity player )
{
return isUsable( player, false );
return isUsable( player );
}
private void onInventoryDefinitelyChanged()