Skip to content

Commit

Permalink
remove bad supports() in key impl
Browse files Browse the repository at this point in the history
  • Loading branch information
MrHell228 committed Mar 9, 2025
1 parent da6e70e commit 873584c
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 53 deletions.
2 changes: 1 addition & 1 deletion SpongeAPI
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import net.minecraft.network.chat.Component;
import net.minecraft.server.network.Filterable;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.component.WritableBookContent;
import net.minecraft.world.item.component.WrittenBookContent;
import org.spongepowered.api.data.Keys;
Expand Down Expand Up @@ -61,7 +60,6 @@ public static void register(final DataProviderRegistrator registrator) {
h.set(DataComponents.WRITTEN_BOOK_CONTENT,
new WrittenBookContent(content.title(), author, content.generation(), content.pages(), content.resolved()));
})
.supports(h -> h.getItem() == Items.WRITTEN_BOOK)
.create(Keys.GENERATION)
.get(h -> {
final WrittenBookContent content = h.get(DataComponents.WRITTEN_BOOK_CONTENT);
Expand All @@ -79,7 +77,6 @@ public static void register(final DataProviderRegistrator registrator) {
new WrittenBookContent(content.title(), content.author(), v, content.pages(), content.resolved()));
return true;
})
.supports(h -> h.getItem() == Items.WRITTEN_BOOK)
.create(Keys.PAGES)
.get(h -> {
final WrittenBookContent content = h.get(DataComponents.WRITTEN_BOOK_CONTENT);
Expand All @@ -99,7 +96,6 @@ public static void register(final DataProviderRegistrator registrator) {
h.set(DataComponents.WRITTEN_BOOK_CONTENT,
new WrittenBookContent(content.title(), content.author(), content.generation(), Collections.emptyList(), content.resolved()));
})
.supports(h -> h.getItem() == Items.WRITTEN_BOOK)
.create(Keys.PLAIN_PAGES)
.get(h -> {
final WritableBookContent content = h.get(DataComponents.WRITABLE_BOOK_CONTENT);
Expand All @@ -109,8 +105,7 @@ public static void register(final DataProviderRegistrator registrator) {
return content.pages().stream().map(Filterable::raw).toList();
})
.set((h, v) -> h.set(DataComponents.WRITABLE_BOOK_CONTENT, new WritableBookContent(v.stream().map(Filterable::passThrough).toList())))
.delete(h -> h.set(DataComponents.WRITABLE_BOOK_CONTENT, new WritableBookContent(Collections.emptyList())))
.supports(h -> h.getItem() == Items.WRITABLE_BOOK);
.delete(h -> h.set(DataComponents.WRITABLE_BOOK_CONTENT, new WritableBookContent(Collections.emptyList())));
}
// @formatter:on

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
package org.spongepowered.common.data.provider.item.stack;

import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import org.spongepowered.api.data.Keys;
import org.spongepowered.common.data.provider.DataProviderRegistrator;
import org.spongepowered.common.util.FireworkUtil;
Expand All @@ -46,7 +45,6 @@ public static void register(final DataProviderRegistrator registrator) {
.get(h -> FireworkUtil.getFireworkEffects(h).orElse(null))
.setAnd(FireworkUtil::setFireworkEffects)
.deleteAnd(FireworkUtil::removeFireworkEffects)
.supports(h -> h.getItem() == Items.FIREWORK_ROCKET || h.getItem() == Items.FIREWORK_STAR)
.create(Keys.FIREWORK_FLIGHT_MODIFIER)
.get(h -> {
final OptionalInt modifier = FireworkUtil.getFlightModifier(h);
Expand All @@ -61,8 +59,7 @@ public static void register(final DataProviderRegistrator registrator) {
return false;
}
return FireworkUtil.setFlightModifier(h, ticks);
})
.supports(h -> h.getItem() == Items.FIREWORK_ROCKET);
});
}
// @formatter:on
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.food.FoodProperties;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.Rarity;
import net.minecraft.world.item.component.ChargedProjectiles;
import net.minecraft.world.item.component.CustomModelData;
Expand Down Expand Up @@ -143,15 +142,13 @@ public static void register(final DataProviderRegistrator registrator) {
if (h.has(DataComponents.CUSTOM_NAME)) {
return SpongeAdventure.asAdventure(h.getHoverName());
}
if (h.getItem() == Items.WRITTEN_BOOK) {
// When no custom name is set on a written book fallback to its title
// The custom name has a higher priority than the title so no setter is needed.
var bookContent = h.get(DataComponents.WRITTEN_BOOK_CONTENT);
if (bookContent != null) {
final String rawTitle = bookContent.title().raw();
if (!StringUtil.isBlank(rawTitle)) {
return LegacyComponentSerializer.legacySection().deserialize(rawTitle);
}
// When no custom name is set on a written book fallback to its title
// The custom name has a higher priority than the title so no setter is needed.
var bookContent = h.get(DataComponents.WRITTEN_BOOK_CONTENT);
if (bookContent != null) {
final String rawTitle = bookContent.title().raw();
if (!StringUtil.isBlank(rawTitle)) {
return LegacyComponentSerializer.legacySection().deserialize(rawTitle);
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.alchemy.Potion;
import net.minecraft.world.item.alchemy.PotionContents;
import org.spongepowered.api.data.Keys;
Expand Down Expand Up @@ -58,7 +57,6 @@ public static void register(final DataProviderRegistrator registrator) {
.get(h -> Color.ofRgb(h.getOrDefault(DataComponents.POTION_CONTENTS, PotionContents.EMPTY).getColor()))
.set((h, v) -> h.update(DataComponents.POTION_CONTENTS, PotionContents.EMPTY, contents -> new PotionContents(contents.potion(), Optional.of(v.rgb()), contents.customEffects())))
.delete(h -> h.update(DataComponents.POTION_CONTENTS, PotionContents.EMPTY, contents -> new PotionContents(contents.potion(), Optional.empty(), contents.customEffects())))
.supports(h -> h.getItem() == Items.POTION || h.getItem() == Items.SPLASH_POTION || h.getItem() == Items.LINGERING_POTION || h.getItem() == Items.TIPPED_ARROW)
.create(Keys.CUSTOM_POTION_EFFECTS)
.get(h -> {
final List<MobEffectInstance> effects = h.getOrDefault(DataComponents.POTION_CONTENTS, PotionContents.EMPTY).customEffects();
Expand All @@ -69,22 +67,18 @@ public static void register(final DataProviderRegistrator registrator) {
h.update(DataComponents.POTION_CONTENTS, PotionContents.EMPTY, contents -> new PotionContents(contents.potion(), contents.customColor(), mcList));
})
.delete(h -> h.update(DataComponents.POTION_CONTENTS, PotionContents.EMPTY, contents -> new PotionContents(contents.potion(), contents.customColor(), Collections.emptyList())))
.supports(h -> h.getItem() == Items.POTION || h.getItem() == Items.SPLASH_POTION || h.getItem() == Items.LINGERING_POTION || h.getItem() == Items.TIPPED_ARROW)
.create(Keys.POTION_TYPE)
.get(h -> (PotionType) h.getOrDefault(DataComponents.POTION_CONTENTS, PotionContents.EMPTY).potion().map(Holder::value).orElse(null)) // TODO empty POTION gone?
.set((h, v) -> {
final var potion = Optional.ofNullable(v).map(Potion.class::cast).map(BuiltInRegistries.POTION::wrapAsHolder); // TODO set empty POTION? same as delete?
h.update(DataComponents.POTION_CONTENTS, PotionContents.EMPTY, contents -> new PotionContents(potion, contents.customColor(), contents.customEffects()));
})
.delete(h -> h.update(DataComponents.POTION_CONTENTS, PotionContents.EMPTY, contents -> new PotionContents(Optional.empty(), contents.customColor(), contents.customEffects())))
.supports(h -> h.getItem() == Items.POTION || h.getItem() == Items.SPLASH_POTION ||
h.getItem() == Items.LINGERING_POTION || h.getItem() == Items.TIPPED_ARROW)
.create(Keys.POTION_EFFECTS)
.get(h -> {
final Iterable<MobEffectInstance> effects = h.getOrDefault(DataComponents.POTION_CONTENTS, PotionContents.EMPTY).getAllEffects();
return ImmutableList.copyOf((List<PotionEffect>) (Object) effects);
})
.supports(h -> h.getItem() == Items.POTION || h.getItem() == Items.SPLASH_POTION || h.getItem() == Items.LINGERING_POTION || h.getItem() == Items.TIPPED_ARROW)
;
}
// @formatter:on
Expand Down
42 changes: 16 additions & 26 deletions src/main/java/org/spongepowered/common/util/FireworkUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,11 @@ public static boolean setFireworkEffects(final ItemStack item, final List<? exte

if (item.getItem() == Items.FIREWORK_STAR) {
item.set(DataComponents.FIREWORK_EXPLOSION, (FireworkExplosion) (Object) effects.getFirst());
return true;
} else if (item.getItem() == Items.FIREWORK_ROCKET) {
} else {
final List<FireworkExplosion> mcEffects = effects.stream().map(FireworkExplosion.class::cast).toList();
item.update(DataComponents.FIREWORKS, new Fireworks(1, Collections.emptyList()), p -> new Fireworks(p.flightDuration(), mcEffects));
return true;
}

return false;
return true;
}

public static Optional<List<FireworkEffect>> getFireworkEffects(final FireworkRocketEntity firework) {
Expand All @@ -77,20 +74,18 @@ public static Optional<List<FireworkEffect>> getFireworkEffects(final ItemStack
return Optional.empty();
}

if (item.getItem() == Items.FIREWORK_ROCKET) {
final Fireworks fireworks = item.get(DataComponents.FIREWORKS);
if (fireworks == null || fireworks.explosions().isEmpty()) {
return Optional.empty();
if (item.getItem() == Items.FIREWORK_STAR) {
final FireworkExplosion fireworkExplosion = item.get(DataComponents.FIREWORK_EXPLOSION);
if (fireworkExplosion != null) {
return Optional.of(List.of((FireworkEffect) (Object) fireworkExplosion));
}
return Optional.of(fireworks.explosions().stream().map(FireworkEffect.class::cast).toList());
}

Preconditions.checkArgument(item.getItem() == Items.FIREWORK_STAR, "Item is not a firework star!");
final FireworkExplosion fireworkExplosion = item.get(DataComponents.FIREWORK_EXPLOSION);
if (fireworkExplosion == null) {
final Fireworks fireworks = item.get(DataComponents.FIREWORKS);
if (fireworks == null || fireworks.explosions().isEmpty()) {
return Optional.empty();
}
return Optional.of(List.of((FireworkEffect) (Object) fireworkExplosion));
return Optional.of(fireworks.explosions().stream().map(FireworkEffect.class::cast).toList());
}

public static boolean removeFireworkEffects(final FireworkRocketEntity firework) {
Expand All @@ -106,14 +101,12 @@ public static boolean removeFireworkEffects(final ItemStack item) {
item.remove(DataComponents.FIREWORK_EXPLOSION);
return true;
}
if (item.getItem() == Items.FIREWORK_ROCKET) {
if (item.has(DataComponents.FIREWORKS)) {
// keep flight duration
item.update(DataComponents.FIREWORKS, null, p -> new Fireworks(p.flightDuration(), Collections.emptyList()));
}
return true;

if (item.has(DataComponents.FIREWORKS)) {
// keep flight duration
item.update(DataComponents.FIREWORKS, null, p -> new Fireworks(p.flightDuration(), Collections.emptyList()));
}
return false;
return true;
}

public static boolean setFlightModifier(final FireworkRocketEntity firework, final int modifier) {
Expand All @@ -127,11 +120,8 @@ public static boolean setFlightModifier(final ItemStack item, final int modifier
return false;
}

if (item.getItem() == Items.FIREWORK_ROCKET) {
item.update(DataComponents.FIREWORKS, new Fireworks(1, Collections.emptyList()), p -> new Fireworks(modifier, p.explosions()));
return true;
}
return false;
item.update(DataComponents.FIREWORKS, new Fireworks(1, Collections.emptyList()), p -> new Fireworks(modifier, p.explosions()));
return true;
}

public static OptionalInt getFlightModifier(final FireworkRocketEntity firework) {
Expand Down

0 comments on commit 873584c

Please sign in to comment.