diff options
author | Falkreon <falkreon@gmail.com> | 2019-08-25 21:38:11 -0500 |
---|---|---|
committer | Falkreon <falkreon@gmail.com> | 2019-08-25 21:38:11 -0500 |
commit | c365433e34bdd95e83740a5af6a3338444a3540a (patch) | |
tree | 8ecabd4ee8dacc31ae5237094dd85f82108b30ab /GuiTest | |
parent | 2ae4c11c84ad90031b0fd32e011c49849352bcc5 (diff) | |
download | LibGui-c365433e34bdd95e83740a5af6a3338444a3540a.tar.gz LibGui-c365433e34bdd95e83740a5af6a3338444a3540a.tar.bz2 LibGui-c365433e34bdd95e83740a5af6a3338444a3540a.zip |
More gui test, fix slot offsets, tooltips, and button hovers
Diffstat (limited to 'GuiTest')
9 files changed, 356 insertions, 14 deletions
diff --git a/GuiTest/build.gradle b/GuiTest/build.gradle index c45af5e..ddd6891 100644 --- a/GuiTest/build.gradle +++ b/GuiTest/build.gradle @@ -22,6 +22,10 @@ group = project.maven_group minecraft { } +//configurations.modApi { +// resolutionStrategy.cacheChangingModulesFor 0, 'seconds' +//} + repositories { mavenLocal(); maven { url "http://server.bbkr.space:8081/artifactory/libs-release" } @@ -39,7 +43,9 @@ dependencies { modApi "io.github.prospector:modmenu:1.7.9+build.118" - modApi "io.github.cottonmc:LibGui:1.3.1-SNAPSHOT" + //modApi ("io.github.cottonmc:LibGui:1.3.1-SNAPSHOT") { changing = true } + compile project(":LibGUI"); + runtime project(":LibGUI"); } processResources { diff --git a/GuiTest/settings.gradle b/GuiTest/settings.gradle index 5b60df3..55a4710 100644 --- a/GuiTest/settings.gradle +++ b/GuiTest/settings.gradle @@ -8,3 +8,6 @@ pluginManagement { gradlePluginPortal() } } + +include ":LibGUI" +project(":LibGUI").projectDir = file("../")
\ No newline at end of file diff --git a/GuiTest/src/main/java/io/github/cottonmc/test/GuiBlock.java b/GuiTest/src/main/java/io/github/cottonmc/test/GuiBlock.java new file mode 100644 index 0000000..ec37c55 --- /dev/null +++ b/GuiTest/src/main/java/io/github/cottonmc/test/GuiBlock.java @@ -0,0 +1,38 @@ +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.minecraft.block.entity.BlockEntity; +import net.minecraft.entity.player.PlayerEntity; +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 GuiBlock() { + super(FabricBlockSettings.copy(Blocks.IRON_BLOCK).build()); + } + + + @Override + public boolean activate(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)); + } + return true; + } + + + @Override + public BlockEntity createBlockEntity(BlockView var1) { + return new GuiBlockEntity(); + } +} diff --git a/GuiTest/src/main/java/io/github/cottonmc/test/GuiBlockEntity.java b/GuiTest/src/main/java/io/github/cottonmc/test/GuiBlockEntity.java new file mode 100644 index 0000000..f07ccb7 --- /dev/null +++ b/GuiTest/src/main/java/io/github/cottonmc/test/GuiBlockEntity.java @@ -0,0 +1,26 @@ +package io.github.cottonmc.test; + +import net.minecraft.block.entity.BlockEntity; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.item.ItemStack; +import net.minecraft.util.DefaultedList; + +public class GuiBlockEntity extends BlockEntity implements ImplementedInventory { + + DefaultedList<ItemStack> items = DefaultedList.ofSize(8, ItemStack.EMPTY); + + public GuiBlockEntity() { + super(LibGuiTest.GUI_BLOCKENTITY_TYPE); + } + + @Override + public DefaultedList<ItemStack> getItems() { + return items; + } + + @Override + public boolean canPlayerUseInv(PlayerEntity player) { + return pos.isWithinDistance(player.getBlockPos(), 4.5); + } + +} diff --git a/GuiTest/src/main/java/io/github/cottonmc/test/ImplementedInventory.java b/GuiTest/src/main/java/io/github/cottonmc/test/ImplementedInventory.java new file mode 100644 index 0000000..177affd --- /dev/null +++ b/GuiTest/src/main/java/io/github/cottonmc/test/ImplementedInventory.java @@ -0,0 +1,213 @@ +package io.github.cottonmc.test; + +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.inventory.Inventories; +import net.minecraft.inventory.Inventory; +import net.minecraft.inventory.SidedInventory; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.util.DefaultedList; +import net.minecraft.util.math.Direction; + +import java.util.List; + +/** + * A simple {@code SidedInventory} implementation with only default methods + an item list getter. + * + * <h2>Reading and writing to tags</h2> + * Use {@link Inventories#fromTag(CompoundTag, DefaultedList)} and {@link Inventories#toTag(CompoundTag, DefaultedList)} + * on {@linkplain #getItems() the item list}. + * + * License: <a href="https://creativecommons.org/publicdomain/zero/1.0/">CC0</a> + * @author Juuz + */ +@FunctionalInterface +public interface ImplementedInventory extends SidedInventory { + /** + * Gets the item list of this inventory. + * Must return the same instance every time it's called. + * + * @return the item list + */ + DefaultedList<ItemStack> getItems(); + + // Creation + + /** + * Creates an inventory from the item list. + * + * @param items the item list + * @return a new inventory + */ + static ImplementedInventory of(DefaultedList<ItemStack> items) { + return () -> items; + } + + /** + * Creates a new inventory with the size. + * + * @param size the inventory size + * @return a new inventory + */ + static ImplementedInventory ofSize(int size) { + return of(DefaultedList.ofSize(size, ItemStack.EMPTY)); + } + + // SidedInventory + + /** + * Gets the available slots to automation on the side. + * + * <p>The default implementation returns an array of all slots. + * + * @param side the side + * @return the available slots + */ + @Override + default int[] getInvAvailableSlots(Direction side) { + int[] result = new int[getItems().size()]; + for (int i = 0; i < result.length; i++) { + result[i] = i; + } + + return result; + } + + /** + * Returns true if the stack can be inserted in the slot at the side. + * + * <p>The default implementation returns true. + * + * @param slot the slot + * @param stack the stack + * @param side the side + * @return true if the stack can be inserted + */ + @Override + default boolean canInsertInvStack(int slot, ItemStack stack, Direction side) { + return true; + } + + /** + * Returns true if the stack can be extracted from the slot at the side. + * + * <p>The default implementation returns true. + * + * @param slot the slot + * @param stack the stack + * @param side the side + * @return true if the stack can be extracted + */ + @Override + default boolean canExtractInvStack(int slot, ItemStack stack, Direction side) { + return true; + } + + // Inventory + + /** + * Returns the inventory size. + * + * <p>The default implementation returns the size of {@link #getItems()}. + * + * @return the inventory size + */ + @Override + default int getInvSize() { + return getItems().size(); + } + + /** + * @return true if this inventory has only empty stacks, false otherwise + */ + @Override + default boolean isInvEmpty() { + for (int i = 0; i < getInvSize(); i++) { + ItemStack stack = getInvStack(i); + if (!stack.isEmpty()) { + return false; + } + } + + return true; + } + + /** + * Gets the item in the slot. + * + * @param slot the slot + * @return the item in the slot + */ + @Override + default ItemStack getInvStack(int slot) { + return getItems().get(slot); + } + + /** + * Takes a stack of the size from the slot. + * + * <p>(default implementation) If there are less items in the slot than what are requested, + * takes all items in that slot. + * + * @param slot the slot + * @param count the item count + * @return a stack + */ + @Override + default ItemStack takeInvStack(int slot, int count) { + ItemStack result = Inventories.splitStack(getItems(), slot, count); + if (!result.isEmpty()) { + markDirty(); + } + + return result; + } + + /** + * Removes the current stack in the {@code slot} and returns it. + * + * <p>The default implementation uses {@link Inventories#removeStack(List, int)} + * + * @param slot the slot + * @return the removed stack + */ + @Override + default ItemStack removeInvStack(int slot) { + return Inventories.removeStack(getItems(), slot); + } + + /** + * Replaces the current stack in the {@code slot} with the provided stack. + * + * <p>If the stack is too big for this inventory ({@link Inventory#getInvMaxStackAmount()}), + * it gets resized to this inventory's maximum amount. + * + * @param slot the slot + * @param stack the stack + */ + @Override + default void setInvStack(int slot, ItemStack stack) { + getItems().set(slot, stack); + if (stack.getCount() > getInvMaxStackAmount()) { + stack.setCount(getInvMaxStackAmount()); + } + } + + /** + * Clears {@linkplain #getItems() the item list}}. + */ + @Override + default void clear() { + getItems().clear(); + } + + @Override + default void markDirty() { + // Override if you want behavior. + } + + @Override + default boolean canPlayerUseInv(PlayerEntity player) { + return true; + } +}
\ No newline at end of file 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 e2431c8..2e35a55 100644 --- a/GuiTest/src/main/java/io/github/cottonmc/test/LibGuiTest.java +++ b/GuiTest/src/main/java/io/github/cottonmc/test/LibGuiTest.java @@ -1,17 +1,38 @@ package io.github.cottonmc.test; import net.fabricmc.api.ModInitializer; +import net.fabricmc.fabric.api.container.ContainerProviderRegistry; +import net.minecraft.block.entity.BlockEntityType; +import net.minecraft.container.BlockContext; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.item.BlockItem; +import net.minecraft.item.Item; +import net.minecraft.item.ItemGroup; import net.minecraft.util.Identifier; +import net.minecraft.util.PacketByteBuf; import net.minecraft.util.registry.Registry; public class LibGuiTest implements ModInitializer { public static final String MODID = "libgui-test"; + public static GuiBlock GUI_BLOCK; + public static BlockItem GUI_BLOCK_ITEM; + public static BlockEntityType<GuiBlockEntity> GUI_BLOCKENTITY_TYPE; @Override public void onInitialize() { - //TODO: Register an item that spawns a clientside gui, and a block that spawns a serverside one - Registry.register(Registry.ITEM, new Identifier(MODID, "client_gui"), new GuiItem()); + + GUI_BLOCK = new GuiBlock(); + 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); + GUI_BLOCKENTITY_TYPE = BlockEntityType.Builder.create(GuiBlockEntity::new, GUI_BLOCK).build(null); + Registry.register(Registry.BLOCK_ENTITY, new Identifier(MODID, "gui"), GUI_BLOCKENTITY_TYPE); + + + ContainerProviderRegistry.INSTANCE.registerFactory(new Identifier(MODID, "gui"), (int syncId, Identifier identifier, PlayerEntity player, PacketByteBuf buf)->{ + return new TestContainer(syncId, player.inventory, BlockContext.create(player.getEntityWorld(), buf.readBlockPos())); + }); } } diff --git a/GuiTest/src/main/java/io/github/cottonmc/test/TestContainer.java b/GuiTest/src/main/java/io/github/cottonmc/test/TestContainer.java new file mode 100644 index 0000000..975cd6f --- /dev/null +++ b/GuiTest/src/main/java/io/github/cottonmc/test/TestContainer.java @@ -0,0 +1,30 @@ +package io.github.cottonmc.test; + +import io.github.cottonmc.cotton.gui.CottonScreenController; +import io.github.cottonmc.cotton.gui.widget.WButton; +import io.github.cottonmc.cotton.gui.widget.WGridPanel; +import io.github.cottonmc.cotton.gui.widget.WItemSlot; +import io.github.cottonmc.cotton.gui.widget.WPlayerInvPanel; +import net.minecraft.container.BlockContext; +import net.minecraft.entity.player.PlayerInventory; +import net.minecraft.text.LiteralText; + +public class TestContainer extends CottonScreenController { + + public TestContainer(int syncId, PlayerInventory playerInventory, BlockContext context) { + super(null, syncId, playerInventory, getBlockInventory(context), null); + + WGridPanel root = (WGridPanel)this.getRootPanel(); + + root.add(WItemSlot.of(blockInventory, 0, 4, 1), 0, 1); + + WButton button = new WButton(new LiteralText("Test Button")); + root.add(button, 0, 3, 5, 1); + + + root.add(new WPlayerInvPanel(playerInventory), 0, 5); + + + this.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 42b4b42..360fd73 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,12 +1,18 @@ package io.github.cottonmc.test.client; +import io.github.cottonmc.cotton.gui.client.CottonScreen; +import io.github.cottonmc.test.LibGuiTest; +import io.github.cottonmc.test.TestContainer; import net.fabricmc.api.ClientModInitializer; +import net.fabricmc.fabric.api.client.screen.ScreenProviderRegistry; +import net.minecraft.container.BlockContext; +import net.minecraft.util.Identifier; public class LibGuiTestClient implements ClientModInitializer { @Override public void onInitializeClient() { - + ScreenProviderRegistry.INSTANCE.registerFactory(new Identifier(LibGuiTest.MODID, "gui"), (syncId, identifier, player, buf)->new CottonScreen<TestContainer>(new TestContainer(syncId, player.inventory, BlockContext.create(player.getEntityWorld(), buf.readBlockPos())), player)); } } diff --git a/GuiTest/src/main/java/io/github/cottonmc/test/client/TestClientGui.java b/GuiTest/src/main/java/io/github/cottonmc/test/client/TestClientGui.java index 73f23b4..4b914f8 100644 --- a/GuiTest/src/main/java/io/github/cottonmc/test/client/TestClientGui.java +++ b/GuiTest/src/main/java/io/github/cottonmc/test/client/TestClientGui.java @@ -13,23 +13,22 @@ import net.minecraft.text.LiteralText; public class TestClientGui extends LightweightGuiDescription { public TestClientGui() { - WGridPanel root = new WGridPanel(); + WGridPanel root = new WGridPanel(24); this.setRootPanel(root); + WLabel title = new WLabel(new LiteralText("Client Test Gui"), WLabel.DEFAULT_TEXT_COLOR); + root.add(title, 0, 0); + WTextField text = new WTextField(); text.setSuggestion("Test Suggestion"); root.add(text, 0, 1, 8, 1); + text.setSize(8*18, 20); ArrayList<String> data = new ArrayList<>(); - data.add("This"); - data.add("is"); - data.add("a"); - data.add("test"); - data.add("of the"); - data.add("new"); - data.add("recycler-style"); - data.add("list"); - data.add("widget"); + for(int i=0; i<100; i++) { + data.add(""+i); + } + BiConsumer<String, WLabel> configurator = (String s, WLabel label) -> { label.setText(new LiteralText(s)); }; |