diff --git a/src/main/java/dan200/computercraft/shared/common/TileGeneric.java b/src/main/java/dan200/computercraft/shared/common/TileGeneric.java index 86dd17504..9ade77ceb 100644 --- a/src/main/java/dan200/computercraft/shared/common/TileGeneric.java +++ b/src/main/java/dan200/computercraft/shared/common/TileGeneric.java @@ -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(); diff --git a/src/main/java/dan200/computercraft/shared/computer/blocks/TileCommandComputer.java b/src/main/java/dan200/computercraft/shared/computer/blocks/TileCommandComputer.java index e72d1348e..4fd1664a1 100644 --- a/src/main/java/dan200/computercraft/shared/computer/blocks/TileCommandComputer.java +++ b/src/main/java/dan200/computercraft/shared/computer/blocks/TileCommandComputer.java @@ -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() ) diff --git a/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputer.java b/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputer.java index 8859bf8ca..2ef8b8ea1 100644 --- a/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputer.java +++ b/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputer.java @@ -53,7 +53,7 @@ public class TileComputer extends TileComputerBase protected boolean isUsableByPlayer( PlayerEntity player ) { - return isUsable( player, false ); + return isUsable( player ); } @Override diff --git a/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputerBase.java b/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputerBase.java index 400fab60e..88bad785a 100644 --- a/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputerBase.java +++ b/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputerBase.java @@ -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[] invalidate; + private LockCode lockCode = LockCode.NO_LOCK; + private final ComputerFamily family; public TileComputerBase( TileEntityType 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 ) diff --git a/src/main/java/dan200/computercraft/shared/computer/inventory/ContainerViewComputer.java b/src/main/java/dan200/computercraft/shared/computer/inventory/ContainerViewComputer.java index a863b7305..5bdb9060d 100644 --- a/src/main/java/dan200/computercraft/shared/computer/inventory/ContainerViewComputer.java +++ b/src/main/java/dan200/computercraft/shared/computer/inventory/ContainerViewComputer.java @@ -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; } diff --git a/src/main/java/dan200/computercraft/shared/peripheral/diskdrive/TileDiskDrive.java b/src/main/java/dan200/computercraft/shared/peripheral/diskdrive/TileDiskDrive.java index fb20d0dff..2ade633b5 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/diskdrive/TileDiskDrive.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/diskdrive/TileDiskDrive.java @@ -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 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 diff --git a/src/main/java/dan200/computercraft/shared/peripheral/printer/TilePrinter.java b/src/main/java/dan200/computercraft/shared/peripheral/printer/TilePrinter.java index e4b599975..1c03fb766 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/printer/TilePrinter.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/printer/TilePrinter.java @@ -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 inventory = NonNullList.withSize( SLOTS, ItemStack.EMPTY ); private final SidedCaps 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 diff --git a/src/main/java/dan200/computercraft/shared/turtle/blocks/TileTurtle.java b/src/main/java/dan200/computercraft/shared/turtle/blocks/TileTurtle.java index 1051931ff..96e2eee20 100644 --- a/src/main/java/dan200/computercraft/shared/turtle/blocks/TileTurtle.java +++ b/src/main/java/dan200/computercraft/shared/turtle/blocks/TileTurtle.java @@ -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()