diff options
Diffstat (limited to 'GuiTest/src/main')
5 files changed, 45 insertions, 27 deletions
diff --git a/GuiTest/src/main/java/io/github/cottonmc/test/GuiBlock.java b/GuiTest/src/main/java/io/github/cottonmc/test/GuiBlock.java index ded6fcb..69a0640 100644 --- a/GuiTest/src/main/java/io/github/cottonmc/test/GuiBlock.java +++ b/GuiTest/src/main/java/io/github/cottonmc/test/GuiBlock.java @@ -1,38 +1,35 @@ package io.github.cottonmc.test; -import net.fabricmc.fabric.api.block.FabricBlockSettings; -import net.fabricmc.fabric.api.container.ContainerProviderRegistry; -import net.minecraft.block.Block; -import net.minecraft.block.BlockEntityProvider; -import net.minecraft.block.BlockState; -import net.minecraft.block.Blocks; +import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; +import net.minecraft.block.*; import net.minecraft.block.entity.BlockEntity; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.util.ActionResult; import net.minecraft.util.Hand; -import net.minecraft.util.Identifier; import net.minecraft.util.hit.BlockHitResult; import net.minecraft.util.math.BlockPos; import net.minecraft.world.BlockView; import net.minecraft.world.World; -public class GuiBlock extends Block implements BlockEntityProvider { +public class GuiBlock extends BlockWithEntity { public GuiBlock() { - super(FabricBlockSettings.copy(Blocks.IRON_BLOCK).build()); + super(FabricBlockSettings.copy(Blocks.IRON_BLOCK)); } @Override public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hitResult) { - if (!world.isClient) { - ContainerProviderRegistry.INSTANCE.openContainer(new Identifier(LibGuiTest.MODID, "gui"), player, (buf)->buf.writeBlockPos(pos)); - } + player.openHandledScreen(state.createScreenHandlerFactory(world, pos)); return ActionResult.SUCCESS; } - @Override public BlockEntity createBlockEntity(BlockView var1) { return new GuiBlockEntity(); } + + @Override + public BlockRenderType getRenderType(BlockState state) { + return BlockRenderType.MODEL; + } } diff --git a/GuiTest/src/main/java/io/github/cottonmc/test/GuiBlockEntity.java b/GuiTest/src/main/java/io/github/cottonmc/test/GuiBlockEntity.java index 3e76fdd..c699b47 100644 --- a/GuiTest/src/main/java/io/github/cottonmc/test/GuiBlockEntity.java +++ b/GuiTest/src/main/java/io/github/cottonmc/test/GuiBlockEntity.java @@ -2,10 +2,18 @@ package io.github.cottonmc.test; import net.minecraft.block.entity.BlockEntity; import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.entity.player.PlayerInventory; import net.minecraft.item.ItemStack; +import net.minecraft.screen.NamedScreenHandlerFactory; +import net.minecraft.screen.ScreenHandler; +import net.minecraft.screen.ScreenHandlerContext; +import net.minecraft.text.LiteralText; +import net.minecraft.text.Text; import net.minecraft.util.collection.DefaultedList; -public class GuiBlockEntity extends BlockEntity implements ImplementedInventory { +import javax.annotation.Nullable; + +public class GuiBlockEntity extends BlockEntity implements ImplementedInventory, NamedScreenHandlerFactory { DefaultedList<ItemStack> items = DefaultedList.ofSize(8, ItemStack.EMPTY); @@ -23,4 +31,14 @@ public class GuiBlockEntity extends BlockEntity implements ImplementedInventory return pos.isWithinDistance(player.getBlockPos(), 4.5); } + @Override + public Text getDisplayName() { + return new LiteralText(""); // no title + } + + @Nullable + @Override + public ScreenHandler createMenu(int syncId, PlayerInventory inv, PlayerEntity player) { + return new TestDescription(LibGuiTest.GUI_SCREEN_HANDLER_TYPE, syncId, inv, ScreenHandlerContext.create(world, pos)); + } } diff --git a/GuiTest/src/main/java/io/github/cottonmc/test/LibGuiTest.java b/GuiTest/src/main/java/io/github/cottonmc/test/LibGuiTest.java index 90f3c92..8b567b7 100644 --- a/GuiTest/src/main/java/io/github/cottonmc/test/LibGuiTest.java +++ b/GuiTest/src/main/java/io/github/cottonmc/test/LibGuiTest.java @@ -6,16 +6,16 @@ import java.nio.file.Path; import java.util.Optional; import net.fabricmc.api.ModInitializer; -import net.fabricmc.fabric.api.container.ContainerProviderRegistry; +import net.fabricmc.fabric.api.screenhandler.v1.ScreenHandlerRegistry; import net.fabricmc.loader.api.FabricLoader; import net.fabricmc.loader.api.ModContainer; import net.minecraft.block.entity.BlockEntityType; -import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.entity.player.PlayerInventory; import net.minecraft.item.BlockItem; import net.minecraft.item.Item; import net.minecraft.item.ItemGroup; -import net.minecraft.network.PacketByteBuf; import net.minecraft.screen.ScreenHandlerContext; +import net.minecraft.screen.ScreenHandlerType; import net.minecraft.util.Identifier; import net.minecraft.util.registry.Registry; @@ -25,6 +25,8 @@ public class LibGuiTest implements ModInitializer { public static GuiBlock GUI_BLOCK; public static BlockItem GUI_BLOCK_ITEM; public static BlockEntityType<GuiBlockEntity> GUI_BLOCKENTITY_TYPE; + public static ScreenHandlerType<TestDescription> GUI_SCREEN_HANDLER_TYPE; + @Override public void onInitialize() { Registry.register(Registry.ITEM, new Identifier(MODID, "client_gui"), new GuiItem()); @@ -36,11 +38,10 @@ public class LibGuiTest implements ModInitializer { GUI_BLOCKENTITY_TYPE = BlockEntityType.Builder.create(GuiBlockEntity::new, GUI_BLOCK).build(null); Registry.register(Registry.BLOCK_ENTITY_TYPE, new Identifier(MODID, "gui"), GUI_BLOCKENTITY_TYPE); - - ContainerProviderRegistry.INSTANCE.registerFactory(new Identifier(MODID, "gui"), (int syncId, Identifier identifier, PlayerEntity player, PacketByteBuf buf)->{ - return new TestDescription(syncId, player.inventory, ScreenHandlerContext.create(player.getEntityWorld(), buf.readBlockPos())); + GUI_SCREEN_HANDLER_TYPE = ScreenHandlerRegistry.registerSimple(new Identifier(MODID, "gui"), (int syncId, PlayerInventory inventory) -> { + return new TestDescription(GUI_SCREEN_HANDLER_TYPE, syncId, inventory, ScreenHandlerContext.EMPTY); }); - + Optional<ModContainer> containerOpt = FabricLoader.getInstance().getModContainer("jankson"); if (containerOpt.isPresent()) { ModContainer jankson = containerOpt.get(); diff --git a/GuiTest/src/main/java/io/github/cottonmc/test/TestDescription.java b/GuiTest/src/main/java/io/github/cottonmc/test/TestDescription.java index 3d5e48c..732494e 100644 --- a/GuiTest/src/main/java/io/github/cottonmc/test/TestDescription.java +++ b/GuiTest/src/main/java/io/github/cottonmc/test/TestDescription.java @@ -4,12 +4,13 @@ import io.github.cottonmc.cotton.gui.SyncedGuiDescription; import io.github.cottonmc.cotton.gui.widget.*; import net.minecraft.entity.player.PlayerInventory; import net.minecraft.screen.ScreenHandlerContext; +import net.minecraft.screen.ScreenHandlerType; import net.minecraft.text.LiteralText; public class TestDescription extends SyncedGuiDescription { - public TestDescription(int syncId, PlayerInventory playerInventory, ScreenHandlerContext context) { - super(syncId, playerInventory, getBlockInventory(context), null); + public TestDescription(ScreenHandlerType<?> type, int syncId, PlayerInventory playerInventory, ScreenHandlerContext context) { + super(type, syncId, playerInventory, getBlockInventory(context), null); WGridPanel root = (WGridPanel)this.getRootPanel(); diff --git a/GuiTest/src/main/java/io/github/cottonmc/test/client/LibGuiTestClient.java b/GuiTest/src/main/java/io/github/cottonmc/test/client/LibGuiTestClient.java index f0d3f27..5ef61c2 100644 --- a/GuiTest/src/main/java/io/github/cottonmc/test/client/LibGuiTestClient.java +++ b/GuiTest/src/main/java/io/github/cottonmc/test/client/LibGuiTestClient.java @@ -4,15 +4,16 @@ import io.github.cottonmc.cotton.gui.client.CottonInventoryScreen; import io.github.cottonmc.test.LibGuiTest; import io.github.cottonmc.test.TestDescription; import net.fabricmc.api.ClientModInitializer; -import net.fabricmc.fabric.api.client.screen.ScreenProviderRegistry; -import net.minecraft.screen.ScreenHandlerContext; -import net.minecraft.util.Identifier; +import net.fabricmc.fabric.api.client.screenhandler.v1.ScreenRegistry; public class LibGuiTestClient implements ClientModInitializer { @Override public void onInitializeClient() { - ScreenProviderRegistry.INSTANCE.registerFactory(new Identifier(LibGuiTest.MODID, "gui"), (syncId, identifier, player, buf)->new CottonInventoryScreen<TestDescription>(new TestDescription(syncId, player.inventory, ScreenHandlerContext.create(player.getEntityWorld(), buf.readBlockPos())), player)); + ScreenRegistry.<TestDescription, CottonInventoryScreen<TestDescription>>register( + LibGuiTest.GUI_SCREEN_HANDLER_TYPE, + (desc, inventory, title) -> new CottonInventoryScreen<>(desc, inventory.player, title) + ); } } |