Skip to content

Commit

Permalink
Adds custom ui support for dynamically placed items
Browse files Browse the repository at this point in the history
  • Loading branch information
kyrptonaught committed Apr 12, 2024
1 parent 00aae82 commit 93f8f29
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 33 deletions.
77 changes: 44 additions & 33 deletions src/main/java/net/kyrptonaught/serverutils/customUI/CustomUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,40 +52,19 @@ public void onClose() {
}
};
gui.setTitle(getAsText(config.title));


int count = 0;
for (ScreenConfig.SlotDefinition slot : config.dynamicSlotList) {
ScreenConfig.SlotDefinition slotDefinition = getSlotDefinition(slot, player);
if (slotDefinition.hidden()) continue;
setClickHandlers(gui, count++, player, slotDefinition);
}

for (String slot : config.slots.keySet()) {
ScreenConfig.SlotDefinition slotDefinition = getSlotDefinition(config.slots.get(slot), player);

ItemStack itemStack = Registries.ITEM.get(new Identifier(slotDefinition.itemID)).getDefaultStack();

if (slotDefinition.itemNBT != null)
try {
NbtCompound compound = StringNbtReader.parse(slotDefinition.itemNBT);
itemStack.setNbt(compound);
} catch (CommandSyntaxException e) {
e.printStackTrace();
}

if (slotDefinition.customModelData != null)
try {
String value = ServerTranslator.translate(player, slotDefinition.customModelData);
int intValue = Integer.parseInt(value);
itemStack.getOrCreateNbt().putInt("CustomModelData", intValue);
} catch (NumberFormatException e) {
e.printStackTrace();
}

if (slotDefinition.displayName != null)
itemStack.setCustomName(getAsText(slotDefinition.displayName));
for (Integer slotNum : expandSlotString(slot)) {
gui.setSlot(slotNum, GuiElementBuilder.from(itemStack)
.setCallback((index, type, action) -> {
if (type.isLeft)
handleClick(player, slotDefinition.leftClickAction, slotDefinition.leftClickSound, slotDefinition);
if (type.isRight)
handleClick(player, slotDefinition.rightClickAction, slotDefinition.rightClickSound, slotDefinition);
})
);
}
if (slotDefinition.hidden()) continue;
for (Integer slotNum : expandSlotString(slot)) setClickHandlers(gui, slotNum, player, slotDefinition);
}

if (!screenHistory.containsKey(player.getUuid()))
Expand Down Expand Up @@ -121,6 +100,16 @@ private static void showLastScreen(ServerPlayerEntity player) {
showScreenFor(screenID, player);
}

private static void setClickHandlers(SimpleGui gui, int slotNum, ServerPlayerEntity player, ScreenConfig.SlotDefinition slotDefinition) {
gui.setSlot(slotNum, GuiElementBuilder.from(slotDefinition.generatedStack)
.setCallback((index, type, action) -> {
if (type.isLeft)
handleClick(player, slotDefinition.leftClickAction, slotDefinition.leftClickSound, slotDefinition);
if (type.isRight)
handleClick(player, slotDefinition.rightClickAction, slotDefinition.rightClickSound, slotDefinition);
})
);
}

private static ScreenConfig.SlotDefinition getSlotDefinition(ScreenConfig.SlotDefinition slotDefinition, ServerPlayerEntity player) {
copyFromPreset(slotDefinition);
Expand All @@ -139,10 +128,32 @@ private static ScreenConfig.SlotDefinition getSlotDefinition(ScreenConfig.SlotDe
value.dynamicItem = ScreenConfig.SlotDefinition.EMPTY_ITEM;
copyFromPreset(value);
value.copyFrom(slotDefinition);
return value;
slotDefinition = value;
}
}

slotDefinition.generatedStack = Registries.ITEM.get(new Identifier(slotDefinition.itemID)).getDefaultStack();

if (slotDefinition.itemNBT != null)
try {
NbtCompound compound = StringNbtReader.parse(slotDefinition.itemNBT);
slotDefinition.generatedStack.setNbt(compound);
} catch (CommandSyntaxException e) {
e.printStackTrace();
}

if (slotDefinition.customModelData != null)
try {
String value = ServerTranslator.translate(player, slotDefinition.customModelData);
int intValue = Integer.parseInt(value);
slotDefinition.generatedStack.getOrCreateNbt().putInt("CustomModelData", intValue);
} catch (NumberFormatException e) {
e.printStackTrace();
}

if (slotDefinition.displayName != null)
slotDefinition.generatedStack.setCustomName(getAsText(slotDefinition.displayName));

return slotDefinition;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package net.kyrptonaught.serverutils.customUI;

import net.kyrptonaught.serverutils.AbstractConfigFile;
import net.minecraft.item.ItemStack;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class ScreenConfig extends AbstractConfigFile {
public String title;
Expand All @@ -16,6 +19,8 @@ public class ScreenConfig extends AbstractConfigFile {

public HashMap<String, SlotDefinition> slots = new HashMap<>();

public List<SlotDefinition> dynamicSlotList = new ArrayList<>();

public static class SlotDefinition {
public String itemID;
public String itemNBT;
Expand All @@ -26,13 +31,17 @@ public static class SlotDefinition {
public String rightClickSound;
public String presetID;

public Boolean hidden;

public Boolean replaceOpenScreen;
public String customModelData;

public Boolean refreshOnInteract;

public DynamicItem dynamicItem;

public transient ItemStack generatedStack;

public boolean replaceOpenScreen() {
return replaceOpenScreen != null && replaceOpenScreen;
}
Expand All @@ -41,6 +50,10 @@ public boolean refreshOnInteract() {
return refreshOnInteract != null && refreshOnInteract;
}

public boolean hidden(){
return hidden != null && hidden;
}

public boolean isFieldBlank(String field) {
return field == null || field.isEmpty() || field.isBlank();
}
Expand Down Expand Up @@ -86,6 +99,9 @@ public SlotDefinition copyFrom(SlotDefinition other) {
if (refreshOnInteract == null)
refreshOnInteract = other.refreshOnInteract;

if (hidden == null)
hidden = other.hidden;

return this;
}

Expand Down

0 comments on commit 93f8f29

Please sign in to comment.