Add an API Helper to ChestGUI to allow inplace updating of the stack that was clicked.
This commit is contained in:
parent
95405f9e5a
commit
c6954add09
7 changed files with 68 additions and 19 deletions
|
@ -23,6 +23,8 @@ import net.minecraftforge.network.NetworkHooks;
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class ChestGUI
|
||||
{
|
||||
|
@ -36,9 +38,9 @@ public class ChestGUI
|
|||
public boolean hasReset = false;
|
||||
public boolean hasRemove = false;
|
||||
|
||||
private Runnable onAdd;
|
||||
private Runnable onReset;
|
||||
private Runnable onRemove;
|
||||
private IChestGUIButtonCallback onAdd;
|
||||
private IChestGUIButtonCallback onReset;
|
||||
private IChestGUIButtonCallback onRemove;
|
||||
|
||||
public ChestGUIButton addBtn = null;
|
||||
public ChestGUIButton resetBtn = null;
|
||||
|
@ -46,21 +48,21 @@ public class ChestGUI
|
|||
|
||||
|
||||
|
||||
public ChestGUI withAdd(Runnable onAdd)
|
||||
public ChestGUI withAdd(IChestGUIButtonCallback onAdd)
|
||||
{
|
||||
hasAdd=true;
|
||||
this.onAdd=onAdd;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ChestGUI withReset(Runnable onReset)
|
||||
public ChestGUI withReset(IChestGUIButtonCallback onReset)
|
||||
{
|
||||
hasReset = true;
|
||||
this.onReset = onReset;
|
||||
return this;
|
||||
}
|
||||
|
||||
private ChestGUI withRemove(Runnable onRemove)
|
||||
private ChestGUI withRemove(IChestGUIButtonCallback onRemove)
|
||||
{
|
||||
hasRemove = true;
|
||||
this.onRemove=onRemove;
|
||||
|
@ -68,6 +70,10 @@ public class ChestGUI
|
|||
}
|
||||
public ChestGUI withButton(ChestGUIButton button)
|
||||
{
|
||||
if(buttons.size()>=2*9)
|
||||
{
|
||||
return this;
|
||||
}
|
||||
buttons.add(button);
|
||||
container.setStackInSlot(button.getSlotNum(), button.buildIcon());
|
||||
|
||||
|
@ -98,8 +104,8 @@ public class ChestGUI
|
|||
{
|
||||
ItemStack remStack = new ItemStack(ModItems.CHESTGUI_REM.get(), 1);
|
||||
|
||||
ChestGUIButton rem = new ChestGUIButton(remStack, ()-> {
|
||||
onRemove.run();
|
||||
ChestGUIButton rem = new ChestGUIButton(remStack, (stack)-> {
|
||||
onRemove.run(stack);
|
||||
}, new Vector2i(2, 3));
|
||||
|
||||
removeBtn = rem;
|
||||
|
@ -111,8 +117,8 @@ public class ChestGUI
|
|||
{
|
||||
ItemStack resStack = new ItemStack(ModItems.CHESTGUI_RESET.get(), 1);
|
||||
|
||||
ChestGUIButton rem = new ChestGUIButton(resStack, ()-> {
|
||||
onReset.run();
|
||||
ChestGUIButton rem = new ChestGUIButton(resStack, (stack)-> {
|
||||
onReset.run(stack);
|
||||
}, new Vector2i(2, 4));
|
||||
|
||||
resetBtn = rem;
|
||||
|
@ -126,8 +132,8 @@ public class ChestGUI
|
|||
|
||||
ItemStack remStack = new ItemStack(ModItems.CHESTGUI_ADD.get(), 1);
|
||||
|
||||
ChestGUIButton rem = new ChestGUIButton(remStack, ()-> {
|
||||
onAdd.run();
|
||||
ChestGUIButton rem = new ChestGUIButton(remStack, (stack)-> {
|
||||
onAdd.run(stack);
|
||||
}, new Vector2i(2, 5));
|
||||
|
||||
addBtn = rem;
|
||||
|
|
|
@ -20,13 +20,14 @@ public class ChestGUIButton
|
|||
private String name;
|
||||
private List<LoreEntry> tooltipInfo = new ArrayList<>();
|
||||
|
||||
private Runnable callback;
|
||||
private IChestGUIButtonCallback callback;
|
||||
private CompoundTag NBT = new CompoundTag();
|
||||
|
||||
/**
|
||||
* Position is Row (X), Column (Y)
|
||||
*/
|
||||
private Vector2i position;
|
||||
private ItemStack built;
|
||||
|
||||
public ChestGUIButton withName(String name)
|
||||
{
|
||||
|
@ -34,7 +35,7 @@ public class ChestGUIButton
|
|||
return this;
|
||||
}
|
||||
|
||||
public ChestGUIButton(Item icon, String name, Runnable callback, Vector2i position)
|
||||
public ChestGUIButton(Item icon, String name, IChestGUIButtonCallback callback, Vector2i position)
|
||||
{
|
||||
this.icon = icon;
|
||||
this.name = name;
|
||||
|
@ -43,7 +44,7 @@ public class ChestGUIButton
|
|||
tooltipInfo = new ArrayList<>();
|
||||
}
|
||||
|
||||
public ChestGUIButton(ItemStack existing, Runnable callback, Vector2i position)
|
||||
public ChestGUIButton(ItemStack existing, IChestGUIButtonCallback callback, Vector2i position)
|
||||
{
|
||||
this.callback = callback;
|
||||
this.position = position;
|
||||
|
@ -80,6 +81,7 @@ public class ChestGUIButton
|
|||
|
||||
|
||||
ret = ret.setHoverName(ChatHelpers.macro(name));
|
||||
built=ret;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
@ -90,6 +92,7 @@ public class ChestGUIButton
|
|||
ItemStackHandler st = new ItemStackHandler(1);
|
||||
st.setStackInSlot(0, stack);
|
||||
|
||||
built=stack;
|
||||
return st;
|
||||
}
|
||||
|
||||
|
@ -138,6 +141,6 @@ public class ChestGUIButton
|
|||
|
||||
public void clicked()
|
||||
{
|
||||
callback.run();
|
||||
callback.run(built);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
package dev.zontreck.libzontreck.chestgui;
|
||||
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface IChestGUIButtonCallback
|
||||
{
|
||||
void run(ItemStack stack);
|
||||
}
|
Reference in a new issue