1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-11-17 15:24:52 +00:00

Add invisible slots to computer GUIs

This ensures inventory slots are synced while the container is open,
meaning the hotbar (which is visible underneath the GUI) correctly
updates.

Fixes #915
This commit is contained in:
Jonathan Coates 2021-09-19 11:18:24 +01:00
parent b17ab16e05
commit 2aa70b49c1
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
8 changed files with 94 additions and 13 deletions

View File

@ -18,6 +18,7 @@ import dan200.computercraft.shared.computer.blocks.BlockComputer;
import dan200.computercraft.shared.computer.blocks.TileCommandComputer;
import dan200.computercraft.shared.computer.blocks.TileComputer;
import dan200.computercraft.shared.computer.core.ComputerFamily;
import dan200.computercraft.shared.computer.inventory.ComputerMenuWithoutInventory;
import dan200.computercraft.shared.computer.inventory.ContainerComputerBase;
import dan200.computercraft.shared.computer.inventory.ContainerViewComputer;
import dan200.computercraft.shared.computer.items.ItemComputer;
@ -312,13 +313,13 @@ public final class Registry
static final DeferredRegister<ContainerType<?>> CONTAINERS = DeferredRegister.create( ForgeRegistries.CONTAINERS, ComputerCraft.MOD_ID );
public static final RegistryObject<ContainerType<ContainerComputerBase>> COMPUTER = CONTAINERS.register( "computer",
() -> ContainerData.toType( ComputerContainerData::new, ContainerComputerBase::new ) );
() -> ContainerData.toType( ComputerContainerData::new, ComputerMenuWithoutInventory::new ) );
public static final RegistryObject<ContainerType<ContainerComputerBase>> POCKET_COMPUTER = CONTAINERS.register( "pocket_computer",
() -> ContainerData.toType( ComputerContainerData::new, ContainerComputerBase::new ) );
() -> ContainerData.toType( ComputerContainerData::new, ComputerMenuWithoutInventory::new ) );
public static final RegistryObject<ContainerType<ContainerComputerBase>> POCKET_COMPUTER_NO_TERM = CONTAINERS.register( "pocket_computer_no_term",
() -> ContainerData.toType( ComputerContainerData::new, ContainerComputerBase::new ) );
() -> ContainerData.toType( ComputerContainerData::new, ComputerMenuWithoutInventory::new ) );
public static final RegistryObject<ContainerType<ContainerTurtle>> TURTLE = CONTAINERS.register( "turtle",
() -> ContainerData.toType( ComputerContainerData::new, ContainerTurtle::new ) );

View File

@ -237,7 +237,7 @@ public final class CommandComputerCraft
@Override
public Container createMenu( int id, @Nonnull PlayerInventory player, @Nonnull PlayerEntity entity )
{
return new ContainerViewComputer( id, computer );
return new ContainerViewComputer( id, player, computer );
}
} );
return 1;

View File

@ -12,7 +12,7 @@ import dan200.computercraft.shared.Registry;
import dan200.computercraft.shared.computer.core.ComputerFamily;
import dan200.computercraft.shared.computer.core.ComputerState;
import dan200.computercraft.shared.computer.core.ServerComputer;
import dan200.computercraft.shared.computer.inventory.ContainerComputerBase;
import dan200.computercraft.shared.computer.inventory.ComputerMenuWithoutInventory;
import dan200.computercraft.shared.util.CapabilityUtil;
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
@ -86,7 +86,7 @@ public class TileComputer extends TileComputerBase
@Override
public Container createMenu( int id, @Nonnull PlayerInventory inventory, @Nonnull PlayerEntity player )
{
return new ContainerComputerBase( Registry.ModContainers.COMPUTER.get(), id, this::isUsableByPlayer, createServerComputer(), getFamily() );
return new ComputerMenuWithoutInventory( Registry.ModContainers.COMPUTER.get(), id, inventory, this::isUsableByPlayer, createServerComputer(), getFamily() );
}
@Nonnull

View File

@ -0,0 +1,41 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2021. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/
package dan200.computercraft.shared.computer.inventory;
import dan200.computercraft.shared.computer.core.ComputerFamily;
import dan200.computercraft.shared.computer.core.IComputer;
import dan200.computercraft.shared.network.container.ComputerContainerData;
import dan200.computercraft.shared.util.InvisibleSlot;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.container.ContainerType;
import java.util.function.Predicate;
/**
* A computer menu which does not have any visible inventory.
*
* This adds invisible versions of the player's hotbars slots, to ensure they're synced to the client when changed.
*/
public class ComputerMenuWithoutInventory extends ContainerComputerBase
{
public ComputerMenuWithoutInventory( ContainerType<? extends ContainerComputerBase> type, int id, PlayerInventory player, Predicate<PlayerEntity> canUse, IComputer computer, ComputerFamily family )
{
super( type, id, canUse, computer, family );
addSlots( player );
}
public ComputerMenuWithoutInventory( ContainerType<? extends ContainerComputerBase> type, int id, PlayerInventory player, ComputerContainerData data )
{
super( type, id, player, data );
addSlots( player );
}
private void addSlots( PlayerInventory player )
{
for( int i = 0; i < 9; i++ ) addSlot( new InvisibleSlot( player, i ) );
}
}

View File

@ -34,7 +34,7 @@ import java.util.UUID;
import java.util.function.Function;
import java.util.function.Predicate;
public class ContainerComputerBase extends Container implements IContainerComputer
public abstract class ContainerComputerBase extends Container implements IContainerComputer
{
private static final String LIST_PREFIX = "\n \u2022 ";

View File

@ -16,14 +16,14 @@ import net.minecraft.entity.player.PlayerInventory;
import javax.annotation.Nonnull;
public class ContainerViewComputer extends ContainerComputerBase
public class ContainerViewComputer extends ComputerMenuWithoutInventory
{
private final int width;
private final int height;
public ContainerViewComputer( int id, ServerComputer computer )
public ContainerViewComputer( int id, PlayerInventory player, ServerComputer computer )
{
super( Registry.ModContainers.VIEW_COMPUTER.get(), id, player -> canInteractWith( computer, player ), computer, computer.getFamily() );
super( Registry.ModContainers.VIEW_COMPUTER.get(), id, player, p -> canInteractWith( computer, p ), computer, computer.getFamily() );
width = height = 0;
}

View File

@ -7,7 +7,7 @@ package dan200.computercraft.shared.pocket.inventory;
import dan200.computercraft.shared.Registry;
import dan200.computercraft.shared.computer.core.ServerComputer;
import dan200.computercraft.shared.computer.inventory.ContainerComputerBase;
import dan200.computercraft.shared.computer.inventory.ComputerMenuWithoutInventory;
import dan200.computercraft.shared.pocket.items.ItemPocketComputer;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
@ -49,8 +49,8 @@ public class PocketComputerMenuProvider implements INamedContainerProvider
@Override
public Container createMenu( int id, @Nonnull PlayerInventory inventory, @Nonnull PlayerEntity entity )
{
return new ContainerComputerBase(
isTypingOnly ? Registry.ModContainers.POCKET_COMPUTER_NO_TERM.get() : Registry.ModContainers.POCKET_COMPUTER.get(), id,
return new ComputerMenuWithoutInventory(
isTypingOnly ? Registry.ModContainers.POCKET_COMPUTER_NO_TERM.get() : Registry.ModContainers.POCKET_COMPUTER.get(), id, inventory,
p -> {
ItemStack stack = p.getItemInHand( hand );
return stack.getItem() == item && ItemPocketComputer.getServerComputer( stack ) == computer;

View File

@ -0,0 +1,39 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2021. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/
package dan200.computercraft.shared.util;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.container.Slot;
import net.minecraft.item.ItemStack;
import javax.annotation.Nonnull;
public class InvisibleSlot extends Slot
{
public InvisibleSlot( IInventory container, int slot )
{
super( container, slot, 0, 0 );
}
@Override
public boolean mayPlace( @Nonnull ItemStack stack )
{
return false;
}
@Override
public boolean mayPickup( @Nonnull PlayerEntity player )
{
return false;
}
@Override
public boolean isActive()
{
return false;
}
}