Skip to content

Instantly share code, notes, and snippets.

@Daomephsta
Created December 21, 2018 06:54
Show Gist options
  • Save Daomephsta/070c4f86aa0068a5b9f6cf360b82e024 to your computer and use it in GitHub Desktop.
Save Daomephsta/070c4f86aa0068a5b9f6cf360b82e024 to your computer and use it in GitHub Desktop.
Fabric container code
package daomephsta.precisioncrafting.common
import java.util.function.Consumer
import daomephsta.precisioncrafting.common.precisiontable.BlockEntityPrecisionTable
import daomephsta.precisioncrafting.common.precisiontable.PrecisionTableContainer
import net.minecraft.container.Container
import net.minecraft.container.ContainerProvider
import net.minecraft.entity.player.PlayerEntity
import net.minecraft.entity.player.PlayerInventory
import net.minecraft.text.TextComponent
import net.minecraft.text.TranslatableTextComponent
import net.minecraft.util.Identifier
import net.minecraft.util.PacketByteBuf
import net.minecraft.util.math.BlockPos
import net.minecraft.world.World
public abstract class AbstractProxy
{
abstract void openGUI(int guiID, World world, PlayerEntity player, Object... data)
protected void openContainer(int guiID, World world, PlayerEntity player, Object[] data)
{
player.openContainer getContainerProvider(guiID, world, player, data)
}
private ContainerProvider getContainerProvider(int guiID, World world, PlayerEntity player, Object[] data)
{
switch (guiID)
{
case PrecisionCrafting.GUI_PRECISION_TABLE:
BlockEntityPrecisionTable table =
(BlockEntityPrecisionTable) world.getBlockEntity(BlockPos.fromLong((long) data[0]))
return new SimpleContainerProvider(PrecisionCrafting.BLOCK_PRECISION_TABLE.translationKey,
new PrecisionTableContainer(table, player.inventory), PrecisionCrafting.PRECISION_TABLE_ID)
default:
throw new IllegalArgumentException("Unknown GUI ID $guiID")
}
}
abstract void sendToServer(Identifier packetId, PacketByteBuf bytesWriter)
abstract void sendToClient(PlayerEntity player, Identifier packetId, PacketByteBuf bytes)
private static final class SimpleContainerProvider implements ContainerProvider
{
private final TextComponent name
private final Container container
private final String containerId
SimpleContainerProvider(String translationKey, Container container, String containerId)
{
this.name = new TranslatableTextComponent(translationKey)
this.container = container
this.containerId = containerId
}
@Override
TextComponent getName()
{
return name
}
@Override
Container createContainer(PlayerInventory playerInv, PlayerEntity player)
{
return container
}
@Override
String getContainerId()
{
return containerId
}
}
}
package daomephsta.precisioncrafting.client
import java.util.function.Consumer
import daomephsta.precisioncrafting.common.AbstractProxy
import daomephsta.precisioncrafting.common.PrecisionCrafting
import daomephsta.precisioncrafting.common.precisiontable.BlockEntityPrecisionTable
import net.minecraft.client.MinecraftClient
import net.minecraft.client.gui.Gui
import net.minecraft.entity.player.PlayerEntity
import net.minecraft.server.network.packet.CustomPayloadServerPacket
import net.minecraft.util.Identifier
import net.minecraft.util.PacketByteBuf
import net.minecraft.util.math.BlockPos
import net.minecraft.world.World
class ClientProxy extends AbstractProxy
{
@Override
void openGUI(int guiID, World world, PlayerEntity player, Object... data)
{
MinecraftClient.instance.openGui createGUI(guiID, world, player, data)
openContainer(guiID, world, player, data)
}
private Gui createGUI(int guiID, World world, PlayerEntity player, Object[] data)
{
switch (guiID)
{
case PrecisionCrafting.GUI_PRECISION_TABLE:
BlockEntityPrecisionTable table =
(BlockEntityPrecisionTable) world.getBlockEntity(BlockPos.fromLong((long) data[0]))
return new PrecisionTableGui(table, player.inventory)
default:
throw new IllegalArgumentException("Unknown GUI ID $guiID")
}
}
@Override
void sendToServer(Identifier packetId, PacketByteBuf bytes)
{
MinecraftClient.instance.networkHandler.clientConnection.sendPacket new CustomPayloadServerPacket(packetId, bytes)
}
@Override
void sendToClient(PlayerEntity player, Identifier packetId, PacketByteBuf bytes) {/*NO-OP*/}
}
package daomephsta.precisioncrafting.server
import daomephsta.precisioncrafting.common.AbstractProxy
import net.minecraft.client.network.packet.CustomPayloadClientPacket
import net.minecraft.entity.player.PlayerEntity
import net.minecraft.server.network.ServerPlayerEntity
import net.minecraft.util.Identifier
import net.minecraft.util.PacketByteBuf
import net.minecraft.world.World
class ServerProxy extends AbstractProxy
{
@Override
void openGUI(int guiID, World world, PlayerEntity player, Object... data)
{
openContainer guiID, world, player, data
}
@Override
public void sendToServer(Identifier packetId, PacketByteBuf bytes) {/*NO-OP*/}
@Override
public void sendToClient(PlayerEntity player, Identifier packetId, PacketByteBuf bytes)
{
((ServerPlayerEntity) player).networkHandler.sendPacket new CustomPayloadClientPacket(packetId, bytes)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment