mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-11-26 17:55:14 +00:00
Initial work on GUIs
Needs a bit of a cleanup, but it's a start!
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* This file is part of ComputerCraft - http://www.computercraft.info
|
||||
* Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
|
||||
* Send enquiries to dratcliffe@gmail.com
|
||||
*/
|
||||
|
||||
package dan200.computercraft.shared.network.container;
|
||||
|
||||
import dan200.computercraft.shared.computer.core.ComputerFamily;
|
||||
import dan200.computercraft.shared.computer.core.ServerComputer;
|
||||
import net.minecraft.network.PacketBuffer;
|
||||
|
||||
public class ComputerContainerData implements ContainerData
|
||||
{
|
||||
private final int id;
|
||||
private final ComputerFamily family;
|
||||
|
||||
public ComputerContainerData( ServerComputer computer )
|
||||
{
|
||||
this.id = computer.getInstanceID();
|
||||
this.family = computer.getFamily();
|
||||
}
|
||||
|
||||
public ComputerContainerData( PacketBuffer buf )
|
||||
{
|
||||
this.id = buf.readInt();
|
||||
this.family = buf.readEnumValue( ComputerFamily.class );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBytes( PacketBuffer buf )
|
||||
{
|
||||
buf.writeInt( id );
|
||||
buf.writeEnumValue( family );
|
||||
}
|
||||
|
||||
public int getInstanceId()
|
||||
{
|
||||
return id;
|
||||
}
|
||||
|
||||
public ComputerFamily getFamily()
|
||||
{
|
||||
return family;
|
||||
}
|
||||
}
|
||||
@@ -10,32 +10,36 @@ import net.minecraft.entity.player.PlayerEntity;
|
||||
import net.minecraft.entity.player.PlayerInventory;
|
||||
import net.minecraft.entity.player.ServerPlayerEntity;
|
||||
import net.minecraft.inventory.container.Container;
|
||||
import net.minecraft.inventory.container.ContainerType;
|
||||
import net.minecraft.inventory.container.INamedContainerProvider;
|
||||
import net.minecraft.network.PacketBuffer;
|
||||
import net.minecraftforge.common.extensions.IForgeContainerType;
|
||||
import net.minecraftforge.fml.network.NetworkHooks;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* A horrible hack to allow opening GUIs until Forge adds a built-in system.
|
||||
* An extension over the basic {@link IForgeContainerType}/{@link NetworkHooks#openGui(ServerPlayerEntity, INamedContainerProvider, Consumer)}
|
||||
* hooks, with a more convenient way of reading and writing data.
|
||||
*/
|
||||
public interface ContainerData<T extends Container> extends INamedContainerProvider
|
||||
public interface ContainerData
|
||||
{
|
||||
void toBytes( PacketBuffer buf );
|
||||
|
||||
default void open( PlayerEntity player )
|
||||
default void open( PlayerEntity player, INamedContainerProvider owner )
|
||||
{
|
||||
NetworkHooks.openGui( (ServerPlayerEntity) player, this, this::toBytes );
|
||||
NetworkHooks.openGui( (ServerPlayerEntity) player, owner, this::toBytes );
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
T createMenu( int id, @Nonnull PlayerInventory inventory, @Nonnull PlayerEntity player );
|
||||
|
||||
static <C extends Container, T extends ContainerData<C>> net.minecraft.inventory.container.ContainerType<C> create( Function<PacketBuffer, T> reader )
|
||||
static <C extends Container, T extends ContainerData> ContainerType<C> toType( Function<PacketBuffer, T> reader, Factory<C, T> factory )
|
||||
{
|
||||
return new net.minecraft.inventory.container.ContainerType<>(
|
||||
( id, player ) -> reader.apply( null ).createMenu( id, player, player.player )
|
||||
);
|
||||
return IForgeContainerType.create( ( id, player, data ) -> factory.create( id, player, reader.apply( data ) ) );
|
||||
}
|
||||
|
||||
interface Factory<C extends Container, T extends ContainerData>
|
||||
{
|
||||
C create( int id, @Nonnull PlayerInventory inventory, T data );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,8 +11,6 @@ import net.minecraft.entity.player.PlayerEntity;
|
||||
import net.minecraft.entity.player.PlayerInventory;
|
||||
import net.minecraft.network.PacketBuffer;
|
||||
import net.minecraft.util.Hand;
|
||||
import net.minecraft.util.text.ITextComponent;
|
||||
import net.minecraft.util.text.TranslationTextComponent;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
@@ -21,7 +19,7 @@ import javax.annotation.Nonnull;
|
||||
*
|
||||
* @see dan200.computercraft.shared.pocket.items.ItemPocketComputer
|
||||
*/
|
||||
public class PocketComputerContainerData implements ContainerData<ContainerPocketComputer>
|
||||
public class PocketComputerContainerData implements ContainerData
|
||||
{
|
||||
private final Hand hand;
|
||||
|
||||
@@ -42,14 +40,6 @@ public class PocketComputerContainerData implements ContainerData<ContainerPocke
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public ITextComponent getDisplayName()
|
||||
{
|
||||
return new TranslationTextComponent( "gui.computercraft.pocket" );
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public ContainerPocketComputer createMenu( int id, @Nonnull PlayerInventory inventory, @Nonnull PlayerEntity player )
|
||||
{
|
||||
return new ContainerPocketComputer( id, player, hand );
|
||||
|
||||
@@ -11,8 +11,6 @@ import net.minecraft.entity.player.PlayerEntity;
|
||||
import net.minecraft.entity.player.PlayerInventory;
|
||||
import net.minecraft.network.PacketBuffer;
|
||||
import net.minecraft.util.Hand;
|
||||
import net.minecraft.util.text.ITextComponent;
|
||||
import net.minecraft.util.text.TranslationTextComponent;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
@@ -21,7 +19,7 @@ import javax.annotation.Nonnull;
|
||||
*
|
||||
* @see dan200.computercraft.shared.media.items.ItemPrintout
|
||||
*/
|
||||
public class PrintoutContainerData implements ContainerData<ContainerHeldItem>
|
||||
public class PrintoutContainerData implements ContainerData
|
||||
{
|
||||
private final Hand hand;
|
||||
|
||||
@@ -42,14 +40,6 @@ public class PrintoutContainerData implements ContainerData<ContainerHeldItem>
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public ITextComponent getDisplayName()
|
||||
{
|
||||
return new TranslationTextComponent( "gui.computercraft.printout" );
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public ContainerHeldItem createMenu( int id, @Nonnull PlayerInventory inventory, @Nonnull PlayerEntity player )
|
||||
{
|
||||
return new ContainerHeldItem( ContainerHeldItem.PRINTOUT_TYPE, id, player, hand );
|
||||
|
||||
@@ -6,18 +6,9 @@
|
||||
|
||||
package dan200.computercraft.shared.network.container;
|
||||
|
||||
import dan200.computercraft.ComputerCraft;
|
||||
import dan200.computercraft.core.terminal.Terminal;
|
||||
import dan200.computercraft.shared.computer.core.ComputerFamily;
|
||||
import dan200.computercraft.shared.computer.core.IComputer;
|
||||
import dan200.computercraft.shared.computer.core.ServerComputer;
|
||||
import dan200.computercraft.shared.computer.inventory.ContainerViewComputer;
|
||||
import net.minecraft.entity.player.PlayerEntity;
|
||||
import net.minecraft.entity.player.PlayerInventory;
|
||||
import net.minecraft.network.PacketBuffer;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraft.util.text.ITextComponent;
|
||||
import net.minecraft.util.text.TranslationTextComponent;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
@@ -26,18 +17,14 @@ import javax.annotation.Nonnull;
|
||||
*
|
||||
* @see dan200.computercraft.shared.command.CommandComputerCraft
|
||||
*/
|
||||
public class ViewComputerContainerData implements ContainerData<ContainerViewComputer>
|
||||
public class ViewComputerContainerData extends ComputerContainerData
|
||||
{
|
||||
public static final ResourceLocation ID = new ResourceLocation( ComputerCraft.MOD_ID, "view_computer_gui" );
|
||||
|
||||
private final int instanceId;
|
||||
private final int width;
|
||||
private final int height;
|
||||
private final ComputerFamily family;
|
||||
|
||||
public ViewComputerContainerData( ServerComputer computer )
|
||||
{
|
||||
instanceId = computer.getInstanceID();
|
||||
super( computer );
|
||||
Terminal terminal = computer.getTerminal();
|
||||
if( terminal != null )
|
||||
{
|
||||
@@ -48,38 +35,30 @@ public class ViewComputerContainerData implements ContainerData<ContainerViewCom
|
||||
{
|
||||
width = height = 0;
|
||||
}
|
||||
family = computer.getFamily();
|
||||
}
|
||||
|
||||
public ViewComputerContainerData( PacketBuffer buffer )
|
||||
{
|
||||
instanceId = buffer.readVarInt();
|
||||
super( buffer );
|
||||
width = buffer.readVarInt();
|
||||
height = buffer.readVarInt();
|
||||
family = buffer.readEnumValue( ComputerFamily.class );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBytes( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
buf.writeVarInt( instanceId );
|
||||
super.toBytes( buf );
|
||||
buf.writeVarInt( width );
|
||||
buf.writeVarInt( height );
|
||||
buf.writeEnumValue( family );
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public ITextComponent getDisplayName()
|
||||
public int getWidth()
|
||||
{
|
||||
return new TranslationTextComponent( "gui.computercraft.view_computer" );
|
||||
return width;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public ContainerViewComputer createMenu( int id, @Nonnull PlayerInventory inventory, @Nonnull PlayerEntity player )
|
||||
public int getHeight()
|
||||
{
|
||||
IComputer computer = (player.world.isRemote ? ComputerCraft.clientComputerRegistry : ComputerCraft.serverComputerRegistry).get( id );
|
||||
return new ContainerViewComputer( id, computer );
|
||||
return height;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user