ADD: Multipage chestgui support, javadoc

This commit is contained in:
zontreck 2024-01-09 03:14:14 -07:00
parent e1c522e7a1
commit c8136451e0
17 changed files with 336 additions and 2 deletions

View file

@ -53,7 +53,7 @@ mod_name=Zontreck Library Mod
# The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default.
mod_license=GPLv3
# The mod version. See https://semver.org/
mod_version=1.10.010824.1717
mod_version=1.10.010924.0311
# The group ID for the mod. It is only important when publishing as an artifact to a Maven repository.
# This should match the base package used for the mod sources.
# See https://maven.apache.org/guides/mini/guide-naming-conventions.html

View file

@ -13,10 +13,13 @@ import dev.zontreck.eventsbus.Bus;
import dev.zontreck.libzontreck.chestgui.ChestGUIRegistry;
import dev.zontreck.libzontreck.currency.Bank;
import dev.zontreck.libzontreck.currency.CurrencyHelper;
import dev.zontreck.libzontreck.items.CreativeModeTabs;
import dev.zontreck.libzontreck.items.ModItems;
import dev.zontreck.libzontreck.menus.ChestGUIScreen;
import dev.zontreck.libzontreck.types.ModMenuTypes;
import dev.zontreck.libzontreck.networking.NetworkEvents;
import net.minecraft.client.gui.screens.MenuScreens;
import net.minecraftforge.registries.RegisterEvent;
import org.slf4j.Logger;
import com.mojang.logging.LogUtils;
@ -89,6 +92,8 @@ public class LibZontreck {
Bus.Reset();
ModMenuTypes.REGISTRY.register(bus);
//CreativeModeTabs.register(bus);
ModItems.register(bus);
Bus.Register(CurrencyHelper.class, null);
Bus.Register(Bank.class, null);

View file

@ -3,6 +3,7 @@ package dev.zontreck.libzontreck.chestgui;
import dev.zontreck.libzontreck.LibZontreck;
import dev.zontreck.libzontreck.events.CloseGUIEvent;
import dev.zontreck.libzontreck.events.OpenGUIEvent;
import dev.zontreck.libzontreck.items.ModItems;
import dev.zontreck.libzontreck.menus.ChestGUIMenu;
import dev.zontreck.libzontreck.networking.ModMessages;
import dev.zontreck.libzontreck.networking.packets.S2CCloseChestGUI;
@ -13,6 +14,7 @@ import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.SimpleMenuProvider;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.LogicalSide;
import net.minecraftforge.items.ItemStackHandler;
@ -29,14 +31,215 @@ public class ChestGUI
private UUID player;
public List<ChestGUIButton> buttons = new ArrayList<>();
private ResourceLocation id;
private int page =0;
private boolean hasAdd = false;
private boolean hasReset = false;
private boolean hasRemove = false;
private Runnable onAdd;
private Runnable onReset;
private Runnable onRemove;
public ChestGUI withAdd(Runnable onAdd)
{
hasAdd=true;
this.onAdd=onAdd;
return this;
}
public ChestGUI withReset(Runnable onReset)
{
hasReset = true;
this.onReset = onReset;
return this;
}
private ChestGUI withRemove(Runnable onRemove)
{
hasRemove = true;
this.onRemove=onRemove;
return this;
}
public ChestGUI withButton(ChestGUIButton button)
{
buttons.add(button);
container.setStackInSlot(button.getSlotNum(), button.buildIcon());
//container.setStackInSlot(button.getSlotNum(), button.buildIcon());
return this;
}
/**
* Increment to the next page
*/
public void nextPage()
{
page++;
checkPageButtons();
}
/**
* Go back a previous page, if possible
*/
public void prevPage()
{
page--;
checkPageButtons();
}
/*
X X X X X X X X X
X X X X X X X X X
< 0 0 - @ + 0 0 >
*/
// LEGEND:
// X = ChestGUIButton
// < = Previous Page Button
// 0 = Empty Slot
// - = Remove / Subtract
// @ = Reset / Refresh
// + = Add
// > = Next Page
/**
* Sanity checks the page update
*/
public void checkPageButtons() {
int maxPerPage = 2 * 9;
int maxForPage = maxPerPage * page;
int totalButtons = buttons.size();
int totalPages = (totalButtons - 1) / maxPerPage; // Calculate total pages
// Ensure the current page is within bounds
if (page < 0) {
page = 0;
} else if (page > totalPages) {
page = totalPages;
}
// Perform additional logic if needed for displaying buttons on the GUI
// ...
updateContainerForPage(); // Update the container for the current page
}
/**
* Update the container with the page's buttons
*/
public void updateContainerForPage() {
int maxPerPage = 2 * 9;
int startIndex = maxPerPage * page;
int endIndex = Math.min(startIndex + maxPerPage, buttons.size());
// Logic to update the container based on buttons for the current page
ItemStackHandler pageContainer = new ItemStackHandler((9 * 3)); // Create a new container for the page
for (int i = startIndex; i < endIndex; i++) {
ChestGUIButton button = buttons.get(i);
// Calculate position relative to the page
int relativeIndex = i - startIndex;
int row = relativeIndex / 9;
int col = relativeIndex % 9;
Vector2i position = new Vector2i(row, col); // Create position for the button
button.withPosition(position); // Set the button's position
int slot = row * 9 + col; // Calculate the slot based on (row, column)
pageContainer.setStackInSlot(slot, button.buildIcon()); // Add button to the container
}
if(hasMultiPage())
{
if(!isFirstPage())
{
ItemStack backStack = new ItemStack(ModItems.CHESTGUI_BACK.get(), 1);
ChestGUIButton prev = new ChestGUIButton(backStack, ()->{
close();
prevPage();
open();
}, new Vector2i(3, 0));
pageContainer.setStackInSlot(prev.getSlotNum(), prev.buildIcon());
}
if(!isLastPage())
{
ItemStack forwardStack = new ItemStack(ModItems.CHESTGUI_FORWARD.get(), 1);
ChestGUIButton nxt = new ChestGUIButton(forwardStack, ()->{
close();
prevPage();
open();
}, new Vector2i(3, 8));
pageContainer.setStackInSlot(nxt.getSlotNum(), nxt.buildIcon());
}
}
if(hasRemove)
{
ItemStack remStack = new ItemStack(ModItems.CHESTGUI_REM.get(), 1);
ChestGUIButton rem = new ChestGUIButton(remStack, ()-> {
onRemove.run();
}, new Vector2i(3, 3));
pageContainer.setStackInSlot(rem.getSlotNum(), rem.buildIcon());
}
if(hasReset)
{
ItemStack resStack = new ItemStack(ModItems.CHESTGUI_RESET.get(), 1);
ChestGUIButton rem = new ChestGUIButton(resStack, ()-> {
onReset.run();
}, new Vector2i(3, 4));
pageContainer.setStackInSlot(rem.getSlotNum(), rem.buildIcon());
}
if(hasAdd)
{
ItemStack remStack = new ItemStack(ModItems.CHESTGUI_ADD.get(), 1);
ChestGUIButton rem = new ChestGUIButton(remStack, ()-> {
onAdd.run();
}, new Vector2i(3, 5));
pageContainer.setStackInSlot(rem.getSlotNum(), rem.buildIcon());
}
this.container = pageContainer; // Update the container with the new page content
}
public boolean isFirstPage() {
return page == 0;
}
public boolean isLastPage() {
int maxPerPage = 2 * 9;
int totalButtons = buttons.size();
int totalPages = (totalButtons - 1) / maxPerPage;
return page >= totalPages;
}
/**
* Checks if the number of buttons warrants adding the next/previous buttons to the utility row
* @return True if the number of buttons exceeds (2*9)
*/
public boolean hasMultiPage()
{
return (buttons.size() > (2*9));
}
public ChestGUI withTitle(String title)
{
MenuTitle = title;

View file

@ -103,6 +103,18 @@ public class ChestGUIButton
return this;
}
/**
* Sets the position and returns the builder
* @param pos New button position
* @return This button instance
*/
public ChestGUIButton withPosition(Vector2i pos)
{
this.position=pos;
return this;
}
/**
* Check if the slot's row and column match (X,Y)
* @param slot

View file

@ -0,0 +1,46 @@
package dev.zontreck.libzontreck.items;
import dev.zontreck.libzontreck.LibZontreck;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.Component;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.ItemLike;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;
//@Mod.EventBusSubscriber(modid = LibZontreck.MOD_ID, bus = Mod.EventBusSubscriber.Bus.MOD)
public class CreativeModeTabs
{
public static final DeferredRegister<CreativeModeTab> REGISTRY = DeferredRegister.create(Registries.CREATIVE_MODE_TAB, LibZontreck.MOD_ID);
public static final List<Supplier<? extends ItemLike>> LZ_MOD_ITEMS = new ArrayList<>();
public static final RegistryObject<CreativeModeTab> LIBZONTRECK_TAB = REGISTRY.register("libzontreck", ()->CreativeModeTab.builder()
.title(Component.translatable("itemGroup.tabs.libzontreck"))
.icon(Items.BARRIER::getDefaultInstance)
.displayItems((display,output)->LZ_MOD_ITEMS.forEach(it->output.accept(it.get())))
.build()
);
public static <T extends Item> RegistryObject<T> addToLZTab(RegistryObject<T> item)
{
LZ_MOD_ITEMS.add(item);
return item;
}
public static void register(IEventBus bus)
{
REGISTRY.register(bus);
}
}

View file

@ -0,0 +1,29 @@
package dev.zontreck.libzontreck.items;
import dev.zontreck.libzontreck.LibZontreck;
import net.minecraft.world.item.Item;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;
public class ModItems
{
public static final DeferredRegister<Item> REGISTRY = DeferredRegister.create(ForgeRegistries.ITEMS, LibZontreck.MOD_ID);
public static final RegistryObject<Item> CHESTGUI_ADD = REGISTRY.register("chestgui_add", ()->new Item(new Item.Properties()));
public static final RegistryObject<Item> CHESTGUI_REM = REGISTRY.register("chestgui_remove", ()->new Item(new Item.Properties()));
public static final RegistryObject<Item> CHESTGUI_BACK = REGISTRY.register("chestgui_back", ()->new Item(new Item.Properties()));
public static final RegistryObject<Item> CHESTGUI_RESET = REGISTRY.register("chestgui_reset", ()->new Item(new Item.Properties()));
public static final RegistryObject<Item> CHESTGUI_FORWARD = REGISTRY.register("chestgui_forward", ()->new Item(new Item.Properties()));
public static void register(IEventBus bus)
{
REGISTRY.register(bus);
}
}

View file

@ -0,0 +1,9 @@
{
"itemGroup.tabs.libzontreck": "Aria's Library",
"item.libzontreck.chestgui_add": "Add",
"item.libzontreck.chestgui_remove": "Remove",
"item.libzontreck.chestgui_back": "Previous Page",
"item.libzontreck.chestgui_forward": "Next Page",
"item.libzontreck.chestgui_reset": "Reset"
}

View file

@ -0,0 +1,6 @@
{
"parent": "item/generated",
"textures": {
"layer0": "libzontreck:item/chestgui_add"
}
}

View file

@ -0,0 +1,6 @@
{
"parent": "item/generated",
"textures": {
"layer0": "libzontreck:item/chestgui_back"
}
}

View file

@ -0,0 +1,6 @@
{
"parent": "item/generated",
"textures": {
"layer0": "libzontreck:item/chestgui_forward"
}
}

View file

@ -0,0 +1,6 @@
{
"parent": "item/generated",
"textures": {
"layer0": "libzontreck:item/chestgui_remove"
}
}

View file

@ -0,0 +1,6 @@
{
"parent": "item/generated",
"textures": {
"layer0": "libzontreck:item/chestgui_reset"
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 125 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 156 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 162 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 104 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 206 B