Skip to content

Commit

Permalink
refactor(versions): use DataComponent API for creating default items
Browse files Browse the repository at this point in the history
  • Loading branch information
Siroshun09 committed Nov 19, 2024
1 parent 4cc8109 commit 848d227
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 123 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package net.okocraft.box.version.common.item;

import io.papermc.paper.datacomponent.DataComponentTypes;
import io.papermc.paper.datacomponent.item.Fireworks;
import io.papermc.paper.datacomponent.item.ItemEnchantments;
import io.papermc.paper.datacomponent.item.OminousBottleAmplifier;
import io.papermc.paper.datacomponent.item.PotionContents;
import io.papermc.paper.registry.RegistryAccess;
import io.papermc.paper.registry.RegistryKey;
import net.okocraft.box.api.util.ItemNameGenerator;
import net.okocraft.box.storage.api.model.item.provider.DefaultItem;
import org.bukkit.Bukkit;
import org.bukkit.Keyed;
import org.bukkit.Material;
import org.bukkit.MusicInstrument;
import org.bukkit.Registry;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.ItemType;
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
import org.bukkit.inventory.meta.FireworkMeta;
import org.bukkit.inventory.meta.MusicInstrumentMeta;
import org.bukkit.inventory.meta.OminousBottleMeta;
import org.bukkit.potion.PotionType;
import org.jetbrains.annotations.NotNull;

Expand All @@ -24,27 +26,35 @@
public final class ItemSources {

@SuppressWarnings("deprecation")
public static @NotNull Stream<DefaultItem> itemTypes(@NotNull Registry<ItemType> registry) {
public static @NotNull Stream<DefaultItem> itemTypes() {
var world = Bukkit.getWorlds().getFirst();
var excludedTypes = Set.of(ItemType.AIR, ItemType.GOAT_HORN, ItemType.FIREWORK_ROCKET, ItemType.OMINOUS_BOTTLE);
return registry.stream()
return registry(RegistryKey.ITEM).stream()
.filter(Predicate.not(excludedTypes::contains))
.filter(world::isEnabled)
.map(type -> new DefaultItem(ItemNameGenerator.key(type), ItemStack.of(type.asMaterial(), 1))); // FIXME: remove asMaterial in the future
}

public static @NotNull Stream<DefaultItem> potions(@NotNull Registry<PotionType> registry) {
return DefaultPotionItems.stream(registry);
public static @NotNull Stream<DefaultItem> potions() {
return registry(RegistryKey.POTION).stream().flatMap(ItemSources::createItemsForPotionType);
}

public static @NotNull Stream<DefaultItem> enchantedBooks(Registry<Enchantment> registry) {
return registry.stream()
private static @NotNull Stream<DefaultItem> createItemsForPotionType(@NotNull PotionType potionType) {
return Stream.of(Material.POTION, Material.SPLASH_POTION, Material.LINGERING_POTION, Material.TIPPED_ARROW)
.map(itemType -> {
var name = ItemNameGenerator.keys(itemType, potionType);
var item = ItemStack.of(itemType, 1);
item.setData(DataComponentTypes.POTION_CONTENTS, PotionContents.potionContents().potion(potionType));
return new DefaultItem(name, item);
});
}

public static @NotNull Stream<DefaultItem> enchantedBooks() {
return registry(RegistryKey.ENCHANTMENT).stream()
.map(enchantment -> {
var name = ItemNameGenerator.keys(Material.ENCHANTED_BOOK, enchantment);

var book = new ItemStack(Material.ENCHANTED_BOOK);
book.editMeta(EnchantmentStorageMeta.class, meta -> meta.addStoredEnchant(enchantment, enchantment.getMaxLevel(), false));

book.setData(DataComponentTypes.STORED_ENCHANTMENTS, ItemEnchantments.itemEnchantments().add(enchantment, enchantment.getMaxLevel()));
return new DefaultItem(name, book);
});
}
Expand All @@ -53,18 +63,16 @@ public final class ItemSources {
return IntStream.of(1, 2, 3).mapToObj(power -> {
var name = ItemNameGenerator.key(Material.FIREWORK_ROCKET) + "_" + power;
var firework = new ItemStack(Material.FIREWORK_ROCKET);
firework.editMeta(FireworkMeta.class, meta -> meta.setPower(power));

firework.setData(DataComponentTypes.FIREWORKS, Fireworks.fireworks().flightDuration(power));
return new DefaultItem(name, firework);
});
}

public static @NotNull Stream<DefaultItem> goatHorns(@NotNull Registry<MusicInstrument> registry) {
return registry.stream()
.map(instrument -> {
public static @NotNull Stream<DefaultItem> goatHorns() {
return registry(RegistryKey.INSTRUMENT).stream().map(instrument -> {
var name = ItemNameGenerator.key(instrument);
var goatHorn = new ItemStack(Material.GOAT_HORN);
goatHorn.editMeta(MusicInstrumentMeta.class, meta -> meta.setInstrument(instrument));
goatHorn.setData(DataComponentTypes.INSTRUMENT, instrument);
return new DefaultItem(name, goatHorn);
});
}
Expand All @@ -73,12 +81,15 @@ public final class ItemSources {
return IntStream.rangeClosed(1, 5).mapToObj(level -> {
var name = ItemNameGenerator.key(Material.OMINOUS_BOTTLE) + "_" + level;
var bottle = new ItemStack(Material.OMINOUS_BOTTLE);
bottle.editMeta(OminousBottleMeta.class, meta -> meta.setAmplifier(level - 1));

bottle.setData(DataComponentTypes.OMINOUS_BOTTLE_AMPLIFIER, OminousBottleAmplifier.amplifier(level - 1));
return new DefaultItem(name, bottle);
});
}

private static <T extends Keyed> @NotNull Registry<T> registry(@NotNull RegistryKey<T> key) {
return RegistryAccess.registryAccess().getRegistry(key);
}

public static class Merger {
private Stream<DefaultItem> current;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package net.okocraft.box.version.paper_1_21_2;

import io.papermc.paper.registry.RegistryAccess;
import io.papermc.paper.registry.RegistryKey;
import net.okocraft.box.ap.annotation.version.VersionSpecific;
import net.okocraft.box.api.util.MCDataVersion;
import net.okocraft.box.storage.api.model.item.provider.DefaultItem;
import net.okocraft.box.version.common.item.ItemSources;
import org.bukkit.Keyed;
import org.bukkit.Registry;
import org.jetbrains.annotations.NotNull;

import java.util.stream.Stream;
Expand All @@ -21,19 +17,15 @@ public final class Paper_1_21_2 {
@VersionSpecific.DefaultItemSource
public static @NotNull Stream<DefaultItem> defaultItems() {
return new ItemSources.Merger()
.append(ItemSources.itemTypes(registry(RegistryKey.ITEM)))
.append(ItemSources.potions(registry(RegistryKey.POTION)))
.append(ItemSources.enchantedBooks(registry(RegistryKey.ENCHANTMENT)))
.append(ItemSources.itemTypes())
.append(ItemSources.potions())
.append(ItemSources.enchantedBooks())
.append(ItemSources.fireworks())
.append(ItemSources.goatHorns(registry(RegistryKey.INSTRUMENT)))
.append(ItemSources.goatHorns())
.append(ItemSources.ominousBottles())
.result();
}

private static <T extends Keyed> @NotNull Registry<T> registry(@NotNull RegistryKey<T> key) {
return RegistryAccess.registryAccess().getRegistry(key);
}

private Paper_1_21_2() {
throw new UnsupportedOperationException();
}
Expand Down

0 comments on commit 848d227

Please sign in to comment.