Skip to content

Commit

Permalink
Update polymer port to my pr 1.21.4 version of trinkets
Browse files Browse the repository at this point in the history
  • Loading branch information
Patbox committed Jan 26, 2025
1 parent 814d05a commit d956644
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 74 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
run: ./gradlew build
- name: capture build artifacts
if: ${{ runner.os == 'Linux' && matrix.java == '21' }} # Only upload artifacts built from LTS java on one OS
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
with:
name: Artifacts
path: build/libs/
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
MODRINTH: ${{ secrets.MODRINTH }}
CHANGELOG: ${{ github.event.release.body }}
- name: Upload GitHub release
uses: AButler/upload-release-assets@v2.0
uses: AButler/upload-release-assets@v3.0
with:
files: 'build/libs/*.jar;!build/libs/*-sources.jar;!build/libs/*-dev.jar'
repo-token: ${{ secrets.GITHUB_TOKEN }}
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ dependencies {
modImplementation include("eu.pb4:sgui:${project.sgui_version}")
modImplementation "eu.pb4:polymer-core:${project.polymer_version}"
modImplementation "eu.pb4:polymer-resource-pack:${project.polymer_version}"
modImplementation "eu.pb4:polymer-resource-pack-extras:${project.polymer_version}"
modImplementation include("eu.pb4:player-data-api:${project.pda_version}")
modImplementation include("xyz.nucleoid:server-translations-api:${project.translation_version}")

Expand Down
1 change: 0 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ cloth_config_version=17.0.144

port_version=0


polymer_version=0.11.5+1.21.4
sgui_version=1.8.2+1.21.4
translation_version=2.4.0+1.21.2-rc1
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/dev/emi/trinkets/api/SlotType.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public SlotType(String group, String name, int order, int amount, Identifier ico
this.dropRule = dropRule;

this.itemIcon = itemIcon.copy();
this.itemIcon.set(DataComponentTypes.CUSTOM_MODEL_DATA, GuiModels.getOrCreate(icon, this.itemIcon.getItem()).asComponent());
GuiModels.createModel(this.icon);
}

public String getGroup() {
Expand Down Expand Up @@ -124,7 +124,7 @@ public void write(NbtCompound data) {
tag.put("TooltipPredicates", tooltipPredicateList);
tag.putString("DropRule", dropRule.toString());

tag.put("PolyPort$icon", this.itemIcon.encodeAllowEmpty(DynamicRegistryManager.of(Registries.REGISTRIES)));
tag.put("PolyPort$icon", this.itemIcon.toNbtAllowEmpty(DynamicRegistryManager.of(Registries.REGISTRIES)));

data.put("SlotData", tag);
}
Expand Down
18 changes: 0 additions & 18 deletions src/main/java/dev/emi/trinkets/mixin/RecipeBookScreenMixin.java

This file was deleted.

31 changes: 23 additions & 8 deletions src/main/java/dev/emi/trinkets/poly/Elements.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,35 @@
package dev.emi.trinkets.poly;

import dev.emi.trinkets.TrinketsMain;
import eu.pb4.polymer.resourcepack.api.PolymerModelData;
import eu.pb4.sgui.api.elements.GuiElementBuilder;
import net.minecraft.item.Item;
import net.minecraft.item.Items;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;

public class Elements {
public static final GuiElementBuilder FILLER = new GuiElementBuilder(Items.WHITE_STAINED_GLASS_PANE).hideTooltip()
.setCustomModelData(GuiModels.getOrCreate(Identifier.of(TrinketsMain.MOD_ID, "gui/polybuttons/filler"), Items.WHITE_STAINED_GLASS_PANE).value());
public static final GuiElementBuilder FILLER_NAVBAR = new GuiElementBuilder(Items.BLACK_STAINED_GLASS_PANE).hideTooltip();
public static final Getter FILLER = new Getter(Items.WHITE_STAINED_GLASS_PANE, Identifier.of(TrinketsMain.MOD_ID, "gui/polybuttons/filler"));
public static final Getter FILLER_NAVBAR = new Getter(Items.BLACK_STAINED_GLASS_PANE, Identifier.ofVanilla("air"));


public static final PolymerModelData PREVIOUS = GuiModels.getOrCreate(Identifier.of(TrinketsMain.MOD_ID, "gui/polybuttons/previous"), Items.GREEN_STAINED_GLASS_PANE);
public static final PolymerModelData NEXT = GuiModels.getOrCreate(Identifier.of(TrinketsMain.MOD_ID, "gui/polybuttons/next"), Items.GREEN_STAINED_GLASS_PANE);
public static final PolymerModelData SUBPAGE = GuiModels.getOrCreate(Identifier.of(TrinketsMain.MOD_ID, "gui/polybuttons/subpage"), Items.LIGHT_BLUE_STAINED_GLASS_PANE);
public static final Getter PREVIOUS = new Getter(Items.GREEN_STAINED_GLASS_PANE, Identifier.of(TrinketsMain.MOD_ID, "gui/polybuttons/previous"));
public static final Getter NEXT = new Getter(Items.GREEN_STAINED_GLASS_PANE, Identifier.of(TrinketsMain.MOD_ID, "gui/polybuttons/next"));
public static final Getter SUBPAGE = new Getter(Items.LIGHT_BLUE_STAINED_GLASS_PANE, Identifier.of(TrinketsMain.MOD_ID, "gui/polybuttons/subpage"));


public record Getter(Item item, Identifier modelId) {

public Getter {
if (modelId != null) {
GuiModels.createModel(modelId);
}
}

public GuiElementBuilder get(boolean hasPack) {
var b = new GuiElementBuilder(this.item);
if (hasPack && modelId != null) {
b.model(modelId);
}
return b;
}
}
}
20 changes: 12 additions & 8 deletions src/main/java/dev/emi/trinkets/poly/GuiModels.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
package dev.emi.trinkets.poly;

import dev.emi.trinkets.TrinketsMain;
import eu.pb4.polymer.resourcepack.api.PolymerModelData;
import eu.pb4.polymer.resourcepack.api.PolymerResourcePackUtils;
import eu.pb4.polymer.resourcepack.api.ResourcePackBuilder;
import net.minecraft.item.Item;
import eu.pb4.polymer.resourcepack.extras.api.format.item.ItemAsset;
import eu.pb4.polymer.resourcepack.extras.api.format.item.model.BasicItemModel;
import eu.pb4.polymer.resourcepack.extras.api.format.item.model.ItemModel;
import net.minecraft.util.Identifier;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class GuiModels {
private static final Set<Identifier> MODELS = new HashSet<>();
private static final Map<Item, Map<Identifier, PolymerModelData>> MODEL_MAP = new HashMap<>();
private static boolean init = true;

public static PolymerModelData getOrCreate(Identifier icon, Item item) {
public static Identifier createModel(Identifier icon) {
if (icon.getNamespace().equals(Identifier.DEFAULT_NAMESPACE)) {
return icon;
}

if (init) {
init = false;
PolymerResourcePackUtils.addModAssets(TrinketsMain.MOD_ID);

PolymerResourcePackUtils.RESOURCE_PACK_CREATION_EVENT.register(GuiModels::createFiles);
}

MODELS.add(icon);
return MODEL_MAP.computeIfAbsent(item, i -> new HashMap<>()).computeIfAbsent(icon, i -> PolymerResourcePackUtils.requestModel(item, icon));
return icon;
}

private static void createFiles(ResourcePackBuilder polymerRPBuilder) {
Expand All @@ -43,6 +44,9 @@ private static void createFiles(ResourcePackBuilder polymerRPBuilder) {


polymerRPBuilder.addData("assets/" + id.getNamespace() + "/models/" + id.getPath() + ".json", json.getBytes(StandardCharsets.UTF_8));

polymerRPBuilder.addData("assets/" + id.getNamespace() + "/items/" + id.getPath() + ".json",
new ItemAsset(new BasicItemModel(id), ItemAsset.Properties.DEFAULT));
}
{
var json = """
Expand Down
67 changes: 34 additions & 33 deletions src/main/java/dev/emi/trinkets/poly/TrinketsFlatUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,12 @@ public static int open(ServerPlayerEntity player) {
public void drawLines() {
if (this.compact && !PolymerResourcePackUtils.hasMainPack(this.player)) {
for (int x = 0; x < 5; x++) {
this.setSlot(9 * x + 4, Elements.FILLER);
this.setSlot(9 * x + 4, Elements.FILLER.get(false).hideTooltip());
}
}

var hasPack = PolymerResourcePackUtils.hasMainPack(this.player);

for (int i = 0; i < this.displayPerPage; i++) {
var y = page * this.displayPerPage + i;
if (y < this.inventories.size()) {
Expand All @@ -88,11 +90,11 @@ public void drawLines() {
if (this.compact) {
int base = i / 2 * 9 + ((i % 2) * 5);
for (int x = 0; x < 4; x++) {
this.setSlot(base + x, Elements.FILLER);
this.setSlot(base + x, Elements.FILLER.get(hasPack).hideTooltip());
}
} else {
for (int x = 0; x < 9; x++) {
this.setSlot(i * 9 + x, Elements.FILLER);
this.setSlot(i * 9 + x, Elements.FILLER.get(hasPack).hideTooltip());
}
}
this.cachedSize[i] = 0;
Expand All @@ -116,45 +118,49 @@ private void drawLine(int index, TrinketInventory trinketInventory, int subPage)
var type = trinketInventory.getSlotType();
this.cachedSize[index] = trinketInventory.size();
this.currentlyDisplayed[index] = trinketInventory;
boolean hasPack = PolymerResourcePackUtils.hasMainPack(player);

var base = this.compact ? index / 2 * 9 + ((index % 2) * 5) : index * 9;
var invSize = this.compact ? 2 : 6;

int slot = 0;
this.setSlot(base + slot++, GuiElementBuilder.from(type.getIconItem()).setName(type.getTranslation().formatted(Formatting.WHITE)).hideDefaultTooltip());
var icon = GuiElementBuilder.from(type.getIconItem())
.setName(type.getTranslation().formatted(Formatting.WHITE)).hideDefaultTooltip();
if (hasPack) {
icon.model(type.getIcon());
}
this.setSlot(base + slot++, icon);

if (!this.compact) {
this.setSlot(base + slot++, Elements.FILLER);
this.setSlot(base + slot++, Elements.FILLER.get(hasPack).hideTooltip());
}

boolean hasPack = PolymerResourcePackUtils.hasMainPack(player);

if (trinketInventory.size() <= invSize) {
for (int i = 0; i < invSize; i++) {
if (i < trinketInventory.size()) {
this.setSlotRedirect(base + slot++, new SurvivalTrinketSlot(trinketInventory, i, 0, 0, this.component.getGroups().get(type.getGroup()), type, 0, true));
} else {
this.setSlot(base + slot++, Elements.FILLER);
this.setSlot(base + slot++, Elements.FILLER.get(hasPack).hideTooltip());
}
}

if (hasPack) {
this.setSlot(base + slot++, ItemStack.EMPTY);
} else {
this.setSlot(base + slot++, Elements.FILLER);
this.setSlot(base + slot++, Elements.FILLER.get(hasPack).hideTooltip());
}
} else {
for (int i = 0; i < invSize; i++) {
if (subPage * invSize + i < trinketInventory.size()) {
this.setSlotRedirect(base + slot++, new SurvivalTrinketSlot(trinketInventory, subPage * invSize + i, 0, 0, this.component.getGroups().get(type.getGroup()), type, 0, true));
} else {
this.setSlot(base + slot++, Elements.FILLER);
this.setSlot(base + slot++, Elements.FILLER.get(hasPack).hideTooltip());
}
}

this.setSlot(base + slot++,
new GuiElementBuilder(Elements.SUBPAGE.item())
.setCustomModelData(Elements.SUBPAGE.value())
Elements.SUBPAGE.get(hasPack)
.setName(Text.empty()
.append(Text.literal("« ").formatted(Formatting.GRAY))
.append((this.subPage[index] + 1) + "/" + ((trinketInventory.size() - 1) / invSize + 1))
Expand Down Expand Up @@ -186,18 +192,16 @@ private void drawLine(int index, TrinketInventory trinketInventory, int subPage)
}

private void drawNavbar() {
boolean addNavbarFiller = !PolymerResourcePackUtils.hasMainPack(this.player);
boolean hasPack = PolymerResourcePackUtils.hasMainPack(this.player);
var navabarFiller = Elements.FILLER_NAVBAR.get(hasPack).hideTooltip();

if (this.inventories.size() > this.displayPerPage) {

if (addNavbarFiller) {
this.setSlot(5 * 9 + 0, Elements.FILLER_NAVBAR);
this.setSlot(5 * 9 + 1, Elements.FILLER_NAVBAR);
}
this.setSlot(5 * 9 + 0, navabarFiller);
this.setSlot(5 * 9 + 1, navabarFiller);

this.setSlot(5 * 9 + 2, new GuiElementBuilder(Elements.PREVIOUS.item())
.setName(Text.translatable("createWorld.customize.custom.prev"))
.setCustomModelData(Elements.PREVIOUS.value())
this.setSlot(5 * 9 + 2, Elements.PREVIOUS.get(hasPack)
.setName(Text.translatable("spectatorMenu.previous_page"))
.hideDefaultTooltip()
.setCallback((x, y, z) -> {
this.page -= 1;
Expand All @@ -211,16 +215,15 @@ private void drawNavbar() {
playClickSound(this.player);
})
);
if (addNavbarFiller) {
this.setSlot(5 * 9 + 3, Elements.FILLER_NAVBAR);
this.setSlot(5 * 9 + 4, Elements.FILLER_NAVBAR);
this.setSlot(5 * 9 + 5, Elements.FILLER_NAVBAR);
}

this.setSlot(5 * 9 + 6, new GuiElementBuilder(Elements.NEXT.item())
.setName(Text.translatable("createWorld.customize.custom.next"))
this.setSlot(5 * 9 + 3, navabarFiller);
this.setSlot(5 * 9 + 4, navabarFiller);
this.setSlot(5 * 9 + 5, navabarFiller);


this.setSlot(5 * 9 + 6, Elements.NEXT.get(hasPack)
.setName(Text.translatable("spectatorMenu.next_page"))
.hideDefaultTooltip()
.setCustomModelData(Elements.NEXT.value())
.setCallback((x, y, z) -> {
this.page += 1;

Expand All @@ -235,13 +238,11 @@ private void drawNavbar() {
);


if (addNavbarFiller) {
this.setSlot(5 * 9 + 7, Elements.FILLER_NAVBAR);
this.setSlot(5 * 9 + 8, Elements.FILLER_NAVBAR);
}
} else if (addNavbarFiller) {
this.setSlot(5 * 9 + 7, navabarFiller);
this.setSlot(5 * 9 + 8, navabarFiller);
} else {
for (int i = 0; i < 9; i++) {
this.setSlot(5 * 9 + i, Elements.FILLER_NAVBAR);
this.setSlot(5 * 9 + i, navabarFiller);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/testmod/java/dev/emi/trinkets/TestTrinket.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.Nullable;
import xyz.nucleoid.packettweaker.PacketContext;

import java.util.UUID;

Expand Down Expand Up @@ -47,7 +48,7 @@ public Multimap<RegistryEntry<EntityAttribute>, EntityAttributeModifier> getModi
}

@Override
public Item getPolymerItem(ItemStack itemStack, @Nullable ServerPlayerEntity player) {
public Item getPolymerItem(ItemStack itemStack, PacketContext context) {
return Items.STICK;
}
}
3 changes: 2 additions & 1 deletion src/testmod/java/dev/emi/trinkets/TestTrinket2.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.Nullable;
import xyz.nucleoid.packettweaker.PacketContext;

import java.util.ArrayList;

Expand All @@ -39,7 +40,7 @@ public Multimap<RegistryEntry<EntityAttribute>, EntityAttributeModifier> getModi
}

@Override
public Item getPolymerItem(ItemStack itemStack, @Nullable ServerPlayerEntity player) {
public Item getPolymerItem(ItemStack itemStack, PacketContext context) {
return Items.FEATHER;
}
}

0 comments on commit d956644

Please sign in to comment.