001package net.tnemc.item.paper.platform.registry;
002/*
003 * The New Item Library
004 * Copyright (C) 2022 - 2025 Daniel "creatorfromhell" Vidmar
005 *
006 * This program is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019 */
020
021import io.papermc.paper.registry.RegistryAccess;
022import io.papermc.paper.registry.RegistryKey;
023import net.tnemc.item.paper.platform.PaperItemPlatform;
024import net.tnemc.item.platform.registry.BaseHelper;
025import net.tnemc.item.platform.registry.SupplierRegistryHandler;
026import net.tnemc.item.providers.VersionUtil;
027import org.bukkit.Registry;
028import org.bukkit.inventory.ItemFlag;
029
030import java.util.LinkedList;
031
032/**
033 * PaperHelper
034 *
035 * @author creatorfromhell
036 * @since 0.2.0.0
037 */
038public class PaperHelper extends BaseHelper {
039
040  public PaperHelper() {
041    registerHandler("materials", new SupplierRegistryHandler(() -> {
042
043      final LinkedList<String> keys = new LinkedList<>();
044
045      Registry.MATERIAL.forEach(material -> {
046        if(material.isItem()) {
047          keys.add(material.getKey().getKey());
048        }
049      });
050      return keys;
051    }));
052
053    registerHandler("enchantments", new SupplierRegistryHandler(() -> {
054
055      final LinkedList<String> keys = new LinkedList<>();
056      if(VersionUtil.isVersion(PaperItemPlatform.instance().version(), "1.21")) {
057
058        RegistryAccess.registryAccess().getRegistry(RegistryKey.ENCHANTMENT).forEach((enchantment)->{
059          if(enchantment != null) {
060
061            keys.add(enchantment.getKey().toString());
062          }
063        });
064
065
066      } else {
067
068        Registry.ENCHANTMENT.forEach((enchantment) -> {
069          if(enchantment != null) {
070
071            keys.add(enchantment.getKey().toString());
072          }
073        });
074      }
075      return keys;
076    }));
077
078    registerHandler("flags", new SupplierRegistryHandler(() -> {
079      final LinkedList<String> keys = new LinkedList<>();
080
081      for(final ItemFlag itemFlag : ItemFlag.values()) {
082        keys.add(itemFlag.name());
083      }
084      return keys;
085    }));
086
087    initializeHandlers();
088  }
089}