Skip to content

Commit

Permalink
Rework custom_model_data representation for 1.21.4 changes
Browse files Browse the repository at this point in the history
  • Loading branch information
avaruus1 committed Jan 29, 2025
1 parent 07e6c5d commit 29d09b6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
2 changes: 1 addition & 1 deletion SpongeAPI
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import net.minecraft.world.item.Rarity;
import net.minecraft.world.item.component.ChargedProjectiles;
import net.minecraft.world.item.component.Consumable;
import net.minecraft.world.item.component.CustomModelData;
import net.minecraft.world.item.component.DamageResistant;
import net.minecraft.world.item.component.ItemContainerContents;
import net.minecraft.world.item.component.ItemLore;
Expand All @@ -64,6 +65,7 @@
import org.spongepowered.api.item.ItemType;
import org.spongepowered.api.item.inventory.Inventory;
import org.spongepowered.api.item.inventory.Slot;
import org.spongepowered.api.util.Color;
import org.spongepowered.api.util.Ticks;
import org.spongepowered.common.SpongeCommon;
import org.spongepowered.common.adventure.SpongeAdventure;
Expand Down Expand Up @@ -116,10 +118,38 @@ public static void register(final DataProviderRegistrator registrator) {
.get(h -> (ItemType) h.getItem().getCraftingRemainder().getItem())
.create(Keys.DISPLAY_NAME)
.get(h -> SpongeAdventure.asAdventure(h.getDisplayName()))
// .create(Keys.CUSTOM_MODEL_DATA)
// .get(h -> h.getOrDefault(DataComponents.CUSTOM_MODEL_DATA, CustomModelData.EMPTY))
// .set((h, v) -> h.set(DataComponents.CUSTOM_MODEL_DATA, new CustomModelData(v)))
// .delete(h -> h.remove(DataComponents.CUSTOM_MODEL_DATA))
.create(Keys.CUSTOM_MODEL_DATA_FLOATS)
.get(h -> h.getOrDefault(DataComponents.CUSTOM_MODEL_DATA, CustomModelData.EMPTY).floats())
.set((h, v) -> {
final CustomModelData current = h.getOrDefault(DataComponents.CUSTOM_MODEL_DATA, CustomModelData.EMPTY);

h.set(DataComponents.CUSTOM_MODEL_DATA, new CustomModelData(List.copyOf(v), current.flags(), current.strings(), current.colors()));
})
.create(Keys.CUSTOM_MODEL_DATA_FLAGS)
.get(h -> h.getOrDefault(DataComponents.CUSTOM_MODEL_DATA, CustomModelData.EMPTY).flags())
.set((h, v) -> {
final CustomModelData current = h.getOrDefault(DataComponents.CUSTOM_MODEL_DATA, CustomModelData.EMPTY);

h.set(DataComponents.CUSTOM_MODEL_DATA, new CustomModelData(current.floats(), List.copyOf(v), current.strings(), current.colors()));
})
.create(Keys.CUSTOM_MODEL_DATA_STRINGS)
.get(h -> h.getOrDefault(DataComponents.CUSTOM_MODEL_DATA, CustomModelData.EMPTY).strings())
.set((h, v) -> {
final CustomModelData current = h.getOrDefault(DataComponents.CUSTOM_MODEL_DATA, CustomModelData.EMPTY);

h.set(DataComponents.CUSTOM_MODEL_DATA, new CustomModelData(current.floats(), current.flags(), List.copyOf(v), current.colors()));
})
.create(Keys.CUSTOM_MODEL_DATA_COLORS)
.get(h -> h.getOrDefault(DataComponents.CUSTOM_MODEL_DATA, CustomModelData.EMPTY).colors().stream()
.map(Color::ofRgb)
.toList())
.set((h, v) -> {
final CustomModelData current = h.getOrDefault(DataComponents.CUSTOM_MODEL_DATA, CustomModelData.EMPTY);

h.set(DataComponents.CUSTOM_MODEL_DATA, new CustomModelData(current.floats(), current.flags(), current.strings(), v.stream()
.map(Color::rgb)
.toList()));
})
.create(Keys.CUSTOM_NAME)
.get(h -> {
if (h.has(DataComponents.CUSTOM_NAME)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,10 @@ public void testData(final ServerPlayer player) {
this.checkOfferData(shulkerBullet, Keys.CUSTOM_NAME, Component.text("Angry Shulker Bullet"));

final ItemStack redFlard = ItemStack.of(ItemTypes.SLIME_BLOCK);
this.checkOfferData(redFlard, Keys.CUSTOM_MODEL_DATA, 123456);
this.checkOfferListData(redFlard, Keys.CUSTOM_MODEL_DATA_FLOATS, List.of(0.5f, 1.0f, 1.5f));
this.checkOfferListData(redFlard, Keys.CUSTOM_MODEL_DATA_FLAGS, List.of(true, false, true));
this.checkOfferListData(redFlard, Keys.CUSTOM_MODEL_DATA_STRINGS, List.of("abc", "bce"));
this.checkOfferListData(redFlard, Keys.CUSTOM_MODEL_DATA_COLORS, List.of(Color.LIME, Color.ofRgb(123, 231, 90)));
redFlard.offer(Keys.CUSTOM_NAME, Component.text("Red FLARD", NamedTextColor.DARK_RED));
redFlard.offer(Keys.LORE, Arrays.asList(Component.text("May ignite holder! Handle with care", NamedTextColor.GOLD)));
player.inventory().offer(redFlard);
Expand Down

0 comments on commit 29d09b6

Please sign in to comment.