1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-08-05 21:33:54 +00:00

Add validation to printer slots (#1099)

This commit is contained in:
Drew Edwards 2022-05-23 17:02:12 +01:00 committed by GitHub
parent 24ed0ca723
commit 34dc915d57
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 6 deletions

View File

@ -7,6 +7,7 @@ package dan200.computercraft.shared.peripheral.printer;
import dan200.computercraft.shared.Registry; import dan200.computercraft.shared.Registry;
import dan200.computercraft.shared.util.SingleIntArray; import dan200.computercraft.shared.util.SingleIntArray;
import dan200.computercraft.shared.util.ValidatingSlot;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory; import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.IInventory; import net.minecraft.inventory.IInventory;
@ -33,13 +34,13 @@ public class ContainerPrinter extends Container
addDataSlots( properties ); addDataSlots( properties );
// Ink slot // Ink slot
addSlot( new Slot( inventory, 0, 13, 35 ) ); addSlot( new ValidatingSlot( inventory, 0, 13, 35, TilePrinter::isInk ) );
// In-tray // In-tray
for( int x = 0; x < 6; x++ ) addSlot( new Slot( inventory, x + 1, 61 + x * 18, 22 ) ); for( int x = 0; x < 6; x++ ) addSlot( new ValidatingSlot( inventory, x + 1, 61 + x * 18, 22, TilePrinter::isPaper ) );
// Out-tray // Out-tray
for( int x = 0; x < 6; x++ ) addSlot( new Slot( inventory, x + 7, 61 + x * 18, 49 ) ); for( int x = 0; x < 6; x++ ) addSlot( new ValidatingSlot( inventory, x + 7, 61 + x * 18, 49, o -> false ) );
// Player inv // Player inv
for( int y = 0; y < 3; y++ ) for( int y = 0; y < 3; y++ )

View File

@ -308,7 +308,7 @@ public final class TilePrinter extends TileGeneric implements DefaultSidedInvent
return ColourUtils.getStackColour( stack ) != null; return ColourUtils.getStackColour( stack ) != null;
} }
private static boolean isPaper( @Nonnull ItemStack stack ) static boolean isPaper( @Nonnull ItemStack stack )
{ {
Item item = stack.getItem(); Item item = stack.getItem();
return item == Items.PAPER return item == Items.PAPER

View File

@ -10,17 +10,21 @@ import net.minecraft.inventory.container.Slot;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.util.function.Predicate;
public class ValidatingSlot extends Slot public class ValidatingSlot extends Slot
{ {
public ValidatingSlot( IInventory inventoryIn, int index, int xPosition, int yPosition ) private final Predicate<ItemStack> predicate;
public ValidatingSlot( IInventory inventoryIn, int index, int xPosition, int yPosition, Predicate<ItemStack> predicate )
{ {
super( inventoryIn, index, xPosition, yPosition ); super( inventoryIn, index, xPosition, yPosition );
this.predicate = predicate;
} }
@Override @Override
public boolean mayPlace( @Nonnull ItemStack stack ) public boolean mayPlace( @Nonnull ItemStack stack )
{ {
return true; // inventory.isItemValidForSlot( slotNumber, stack ); return predicate.test( stack );
} }
} }