From 3e2753affb597d9a9487a273ff4e97a04e35898e Mon Sep 17 00:00:00 2001 From: modmuss50 Date: Mon, 25 May 2020 12:55:08 +0100 Subject: [PATCH] Example of using the WIP Screen handler API --- Jenkinsfile | 23 ------ build.gradle | 3 + src/main/java/techreborn/client/GuiType.java | 80 ++++++++++++++------ 3 files changed, 61 insertions(+), 45 deletions(-) delete mode 100644 Jenkinsfile diff --git a/Jenkinsfile b/Jenkinsfile deleted file mode 100644 index 5d0a17882..000000000 --- a/Jenkinsfile +++ /dev/null @@ -1,23 +0,0 @@ -pipeline { - agent any - stages { - - stage('Init') { - steps { - sh "rm -rf build/libs/" - sh "chmod +x gradlew" - } - } - - stage ('Build') { - when { - branch '1.16' - } - steps { - sh "./gradlew clean build publish curseTools --refresh-dependencies --stacktrace" - - archiveArtifacts artifacts: '**/build/libs/*.jar', fingerprint: true - } - } - } -} diff --git a/build.gradle b/build.gradle index 461eefcb4..9b8fd1b94 100644 --- a/build.gradle +++ b/build.gradle @@ -44,6 +44,7 @@ repositories { //trinkets url = "https://maven.abusedmaster.xyz/" } + mavenLocal() maven { url = "https://jitpack.io" } @@ -76,6 +77,8 @@ dependencies { //Fabric api modImplementation "net.fabricmc.fabric-api:fabric-api:0.10.10+build.347-1.16" + modImplementation "net.fabricmc.fabric-api:fabric-screen-handler-api-v1:1.0.0+9ceae8129b" + optionalDependency ("me.shedaniel:RoughlyEnoughItems:4.3.7-unstable") disabledOptionalDependency ('io.github.cottonmc:LibCD:2.3.0+1.15.2') disabledOptionalDependency ('com.github.emilyploszaj:trinkets:2.4.2') diff --git a/src/main/java/techreborn/client/GuiType.java b/src/main/java/techreborn/client/GuiType.java index b1a5f1a6e..2a2989c82 100644 --- a/src/main/java/techreborn/client/GuiType.java +++ b/src/main/java/techreborn/client/GuiType.java @@ -24,22 +24,29 @@ package techreborn.client; +import io.netty.buffer.Unpooled; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; -import net.fabricmc.fabric.api.client.screen.ScreenProviderRegistry; -import net.fabricmc.fabric.api.container.ContainerFactory; -import net.fabricmc.fabric.api.container.ContainerProviderRegistry; +import net.fabricmc.fabric.api.client.screenhandler.v1.ScreenRegistry; +import net.fabricmc.fabric.api.screenhandler.v1.ExtendedScreenHandlerFactory; +import net.fabricmc.fabric.api.screenhandler.v1.ScreenHandlerRegistry; import net.minecraft.block.entity.BlockEntity; import net.minecraft.client.gui.screen.ingame.HandledScreen; import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.entity.player.PlayerInventory; import net.minecraft.network.PacketByteBuf; +import net.minecraft.screen.ScreenHandler; +import net.minecraft.screen.ScreenHandlerType; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.text.LiteralText; +import net.minecraft.text.Text; import net.minecraft.util.Identifier; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import reborncore.RebornCore; import reborncore.api.blockentity.IMachineGuiHandler; import reborncore.client.screen.BuiltScreenHandlerProvider; -import reborncore.client.screen.builder.ScreenHandlerBuilder; +import reborncore.client.screen.builder.BuiltScreenHandler; import techreborn.blockentity.data.DataDrivenBEProvider; import techreborn.blockentity.data.DataDrivenGui; import techreborn.blockentity.generator.PlasmaGeneratorBlockEntity; @@ -129,6 +136,7 @@ import techreborn.client.gui.GuiThermalGenerator; import techreborn.client.gui.GuiVacuumFreezer; import techreborn.client.gui.GuiWireMill; +import javax.annotation.Nullable; import java.util.HashMap; import java.util.Map; import java.util.function.Supplier; @@ -197,32 +205,59 @@ public final class GuiType implements IMachineGuiHandler } private final Identifier identifier; - private final Supplier>> factorySupplierMeme; + private final Supplier>> guiFactory; + private final ScreenHandlerRegistry.ExtendedClientHandlerFactory screenHandlerFactory; + private final ScreenHandlerType screenHandlerType; private GuiType(Identifier identifier, Supplier>> factorySupplierMeme) { this.identifier = identifier; - this.factorySupplierMeme = factorySupplierMeme; - register(); + this.guiFactory = factorySupplierMeme; + this.screenHandlerFactory = getScreenHandlerFactory(); + this.screenHandlerType = ScreenHandlerRegistry.registerExtended(identifier, screenHandlerFactory); + RebornCore.clientOnly(() -> () -> ScreenRegistry.register(screenHandlerType, getGuiFactory())); + } + + private ScreenHandlerRegistry.ExtendedClientHandlerFactory getScreenHandlerFactory() { + return (syncId, playerInventory, packetByteBuf) -> { + final BlockEntity blockEntity = playerInventory.player.world.getBlockEntity(packetByteBuf.readBlockPos()); + BuiltScreenHandler screenHandler = ((BuiltScreenHandlerProvider) blockEntity).createScreenHandler(syncId, playerInventory.player); + + //Set the screen handler type, not ideal but works lol + screenHandler.setType(screenHandlerType); + + return screenHandler; + }; } @Environment(EnvType.CLIENT) - private GuiFactory getFactory() { - return factorySupplierMeme.get().get(); + private GuiFactory getGuiFactory() { + return guiFactory.get().get(); } @Override public void open(PlayerEntity player, BlockPos pos, World world) { if(!world.isClient){ - ContainerProviderRegistry.INSTANCE.openContainer(identifier, player, packetByteBuf -> packetByteBuf.writeBlockPos(pos)); - } - } + //This is awful + player.openHandledScreen(new ExtendedScreenHandlerFactory() { + @Override + public void writeScreenOpeningData(ServerPlayerEntity serverPlayerEntity, PacketByteBuf packetByteBuf) { + packetByteBuf.writeBlockPos(pos); + } - private void register() { - ContainerProviderRegistry.INSTANCE.registerFactory(identifier, (syncID, identifier, playerEntity, packetByteBuf) -> { - final BlockEntity blockEntity = playerEntity.world.getBlockEntity(packetByteBuf.readBlockPos()); - return ((BuiltScreenHandlerProvider) blockEntity).createScreenHandler(syncID, playerEntity); - }); - RebornCore.clientOnly(() -> () -> ScreenProviderRegistry.INSTANCE.registerFactory(identifier, getFactory())); + @Override + public Text getDisplayName() { + return new LiteralText("What is this for?"); + } + + @Nullable + @Override + public ScreenHandler createMenu(int syncId, PlayerInventory inv, PlayerEntity player) { + PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer()); + buf.writeBlockPos(pos); + return screenHandlerFactory.create(syncId, inv, buf); + } + }); + } } public Identifier getIdentifier() { @@ -230,14 +265,15 @@ public final class GuiType implements IMachineGuiHandler } @Environment(EnvType.CLIENT) - public interface GuiFactory extends ContainerFactory { + public interface GuiFactory extends ScreenRegistry.Factory> { HandledScreen create(int syncId, PlayerEntity playerEntity, T blockEntity); @Override - default HandledScreen create(int syncId, Identifier identifier, PlayerEntity playerEntity, PacketByteBuf packetByteBuf) { + default HandledScreen create(BuiltScreenHandler builtScreenHandler, PlayerInventory playerInventory, Text text) { + PlayerEntity playerEntity = playerInventory.player; //noinspection unchecked - T blockEntity = (T) playerEntity.world.getBlockEntity(packetByteBuf.readBlockPos()); - return create(syncId, playerEntity, blockEntity); + T blockEntity = (T) builtScreenHandler.getBlockEntity(); + return create(builtScreenHandler.syncId, playerEntity, blockEntity); } }