diff options
author | Juuz <6596629+Juuxel@users.noreply.github.com> | 2021-12-22 18:25:31 +0200 |
---|---|---|
committer | Juuz <6596629+Juuxel@users.noreply.github.com> | 2021-12-22 18:25:31 +0200 |
commit | e43f9af80183b3198dfe7540f96b0f1c09cfb951 (patch) | |
tree | 6850cd9388f928ed9db3f0eb65ecc1862a23d3bd /GuiTest/src/main | |
parent | 5d9dddbf5f9784f7aba495bfadf103dfc413a443 (diff) | |
download | LibGui-e43f9af80183b3198dfe7540f96b0f1c09cfb951.tar.gz LibGui-e43f9af80183b3198dfe7540f96b0f1c09cfb951.tar.bz2 LibGui-e43f9af80183b3198dfe7540f96b0f1c09cfb951.zip |
Add test for blocks without any inventory of their own
Diffstat (limited to 'GuiTest/src/main')
4 files changed, 77 insertions, 5 deletions
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 17bfdaa..f938749 100644 --- a/GuiTest/src/main/java/io/github/cottonmc/test/LibGuiTest.java +++ b/GuiTest/src/main/java/io/github/cottonmc/test/LibGuiTest.java @@ -1,15 +1,13 @@ package io.github.cottonmc.test; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.Optional; - import net.fabricmc.api.ModInitializer; import net.fabricmc.fabric.api.object.builder.v1.block.entity.FabricBlockEntityTypeBuilder; import net.fabricmc.fabric.api.screenhandler.v1.ScreenHandlerRegistry; import net.fabricmc.loader.api.FabricLoader; import net.fabricmc.loader.api.ModContainer; +import net.minecraft.block.AbstractBlock; +import net.minecraft.block.Block; +import net.minecraft.block.Blocks; import net.minecraft.block.entity.BlockEntityType; import net.minecraft.entity.player.PlayerInventory; import net.minecraft.item.BlockItem; @@ -20,13 +18,20 @@ import net.minecraft.screen.ScreenHandlerType; import net.minecraft.util.Identifier; import net.minecraft.util.registry.Registry; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Optional; + public class LibGuiTest implements ModInitializer { public static final String MODID = "libgui-test"; public static GuiBlock GUI_BLOCK; + public static Block NO_BLOCK_INVENTORY_BLOCK; public static BlockItem GUI_BLOCK_ITEM; public static BlockEntityType<GuiBlockEntity> GUI_BLOCKENTITY_TYPE; public static ScreenHandlerType<TestDescription> GUI_SCREEN_HANDLER_TYPE; + public static ScreenHandlerType<ReallySimpleDescription> REALLY_SIMPLE_SCREEN_HANDLER_TYPE; @Override public void onInitialize() { @@ -36,6 +41,9 @@ public class LibGuiTest implements ModInitializer { Registry.register(Registry.BLOCK, new Identifier(MODID, "gui"), GUI_BLOCK); GUI_BLOCK_ITEM = new BlockItem(GUI_BLOCK, new Item.Settings().group(ItemGroup.MISC)); Registry.register(Registry.ITEM, new Identifier(MODID, "gui"), GUI_BLOCK_ITEM); + NO_BLOCK_INVENTORY_BLOCK = new NoBlockInventoryBlock(AbstractBlock.Settings.copy(Blocks.STONE)); + Registry.register(Registry.BLOCK, new Identifier(MODID, "no_block_inventory"), NO_BLOCK_INVENTORY_BLOCK); + Registry.register(Registry.ITEM, new Identifier(MODID, "no_block_inventory"), new BlockItem(NO_BLOCK_INVENTORY_BLOCK, new Item.Settings().group(ItemGroup.MISC))); GUI_BLOCKENTITY_TYPE = FabricBlockEntityTypeBuilder.create(GuiBlockEntity::new, GUI_BLOCK).build(null); Registry.register(Registry.BLOCK_ENTITY_TYPE, new Identifier(MODID, "gui"), GUI_BLOCKENTITY_TYPE); @@ -43,6 +51,8 @@ public class LibGuiTest implements ModInitializer { return new TestDescription(GUI_SCREEN_HANDLER_TYPE, syncId, inventory, ScreenHandlerContext.EMPTY); }); + REALLY_SIMPLE_SCREEN_HANDLER_TYPE = ScreenHandlerRegistry.registerSimple(new Identifier(MODID, "really_simple"), ReallySimpleDescription::new); + 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/NoBlockInventoryBlock.java b/GuiTest/src/main/java/io/github/cottonmc/test/NoBlockInventoryBlock.java new file mode 100644 index 0000000..2b784a6 --- /dev/null +++ b/GuiTest/src/main/java/io/github/cottonmc/test/NoBlockInventoryBlock.java @@ -0,0 +1,39 @@ +package io.github.cottonmc.test; + +import net.minecraft.block.Block; +import net.minecraft.block.BlockState; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.entity.player.PlayerInventory; +import net.minecraft.screen.NamedScreenHandlerFactory; +import net.minecraft.screen.ScreenHandler; +import net.minecraft.text.Text; +import net.minecraft.util.ActionResult; +import net.minecraft.util.Hand; +import net.minecraft.util.hit.BlockHitResult; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; + +import org.jetbrains.annotations.Nullable; + +public class NoBlockInventoryBlock extends Block implements NamedScreenHandlerFactory { + public NoBlockInventoryBlock(Settings settings) { + super(settings); + } + + @Override + public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { + player.openHandledScreen(this); + return world.isClient ? ActionResult.SUCCESS : ActionResult.CONSUME; + } + + @Override + public Text getDisplayName() { + return getName(); + } + + @Nullable + @Override + public ScreenHandler createMenu(int syncId, PlayerInventory inv, PlayerEntity player) { + return new ReallySimpleDescription(syncId, inv); + } +} diff --git a/GuiTest/src/main/java/io/github/cottonmc/test/ReallySimpleDescription.java b/GuiTest/src/main/java/io/github/cottonmc/test/ReallySimpleDescription.java new file mode 100644 index 0000000..bcb9213 --- /dev/null +++ b/GuiTest/src/main/java/io/github/cottonmc/test/ReallySimpleDescription.java @@ -0,0 +1,16 @@ +package io.github.cottonmc.test; + +import net.minecraft.entity.player.PlayerInventory; + +import io.github.cottonmc.cotton.gui.SyncedGuiDescription; +import io.github.cottonmc.cotton.gui.widget.WGridPanel; + +// A really simple GUI description that only contains a player inventory panel. +public class ReallySimpleDescription extends SyncedGuiDescription { + public ReallySimpleDescription(int syncId, PlayerInventory playerInventory) { + super(LibGuiTest.REALLY_SIMPLE_SCREEN_HANDLER_TYPE, syncId, playerInventory); + setTitleVisible(false); + ((WGridPanel) getRootPanel()).add(createPlayerInventoryPanel(), 0, 0); + getRootPanel().validate(this); + } +} 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 8d6cb18..45b1cbf 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 @@ -1,5 +1,7 @@ package io.github.cottonmc.test.client; +import io.github.cottonmc.test.ReallySimpleDescription; + import net.fabricmc.api.ClientModInitializer; import net.fabricmc.fabric.api.client.command.v1.ClientCommandManager; import net.fabricmc.fabric.api.client.screenhandler.v1.ScreenRegistry; @@ -24,6 +26,11 @@ public class LibGuiTestClient implements ClientModInitializer { (desc, inventory, title) -> new CottonInventoryScreen<>(desc, inventory.player, title) ); + ScreenRegistry.<ReallySimpleDescription, CottonInventoryScreen<ReallySimpleDescription>>register( + LibGuiTest.REALLY_SIMPLE_SCREEN_HANDLER_TYPE, + (desc, inventory, title) -> new CottonInventoryScreen<>(desc, inventory.player, title) + ); + CottonHud.add(new WHudTest(), 10, -20, 10, 10); CottonHud.add(new WLabel(new LiteralText("Test label")), 10, -30, 10, 10); |