001package net.tnemc.plugincore.paper.impl;
002/*
003 * The New Plugin Core
004 * Copyright (C) 2022 - 2025 Daniel "creatorfromhell" Vidmar
005 *
006 * This program is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU Affero General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (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
014 * GNU Affero General Public License for more details.
015 *
016 * You should have received a copy of the GNU Affero General Public License
017 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
018 */
019
020import io.papermc.paper.registry.RegistryAccess;
021import io.papermc.paper.registry.RegistryKey;
022import net.tnemc.item.providers.VersionUtil;
023import net.tnemc.plugincore.core.compatibility.ChunkProvider;
024import org.bukkit.Chunk;
025import org.bukkit.NamespacedKey;
026import org.bukkit.Registry;
027import org.bukkit.StructureType;
028import org.bukkit.block.Biome;
029import org.bukkit.generator.structure.Structure;
030import org.jetbrains.annotations.NotNull;
031
032import java.util.Locale;
033
034/**
035 * PaperChunkProvider
036 *
037 * @author creatorfromhell
038 * @since 1.1.0.1
039 */
040public class PaperChunkProvider implements ChunkProvider {
041
042  private final Chunk chunk;
043  private final int y;
044
045  public PaperChunkProvider(final Chunk chunk, final int y) {
046
047    this.chunk = chunk;
048    this.y = y;
049  }
050
051  /**
052   * This method is used to get the chunk's x coordinate.
053   *
054   * @return The chunk's x coordinate.
055   */
056  @Override
057  public int x() {
058
059    return chunk.getX();
060  }
061
062  /**
063   * This method is used to get the chunk's y coordinate.
064   *
065   * @return The chunk's y coordinate.
066   */
067  @Override
068  public int y() {
069
070    return y;
071  }
072
073  /**
074   * This method is used to get the chunk's z coordinate.
075   *
076   * @return The chunk's z coordinate.
077   */
078  @Override
079  public int z() {
080
081    return chunk.getZ();
082  }
083
084  /**
085   * Checks if the ChunkProvider represents a slime chunk.
086   *
087   * @return True if the ChunkProvider is a slime chunk, false otherwise.
088   */
089  @Override
090  public boolean isSlime() {
091
092    return chunk.isSlimeChunk();
093  }
094
095  /**
096   * Checks if the specified biome is contained within the chunk provider.
097   *
098   * @param biome The biome to check for.
099   *
100   * @return True if the specified biome is contained, false otherwise.
101   */
102  @SuppressWarnings({ "removal", "UnstableApiUsage" })
103  @Override
104  public boolean containsBiome(final @NotNull String biome) {
105
106    try {
107
108      final Registry<@NotNull Biome> registry = RegistryAccess.registryAccess().getRegistry(RegistryKey.BIOME);
109
110      final NamespacedKey biomeKey = NamespacedKey.fromString(biome.toLowerCase(Locale.ROOT));
111      if(biomeKey == null) {
112        return false;
113      }
114
115      final Biome biomeObj = registry.get(biomeKey);
116      if(biomeObj == null) {
117        return false;
118      }
119
120      return chunk.contains(biomeObj);
121
122    } catch(final NoSuchMethodError ignore) {
123
124      return chunk.contains(Biome.valueOf(biome));
125    }
126  }
127
128  /**
129   * Checks if the provided structure is contained. Only on 1.21.1.
130   *
131   * @param structure The structure to check if contained.
132   *
133   * @return True if the structure is contained, false otherwise.
134   */
135  @Override
136  public boolean containsStructure(final @NotNull String structure) {
137
138    final Registry<@NotNull Structure> registry = RegistryAccess.registryAccess().getRegistry(RegistryKey.STRUCTURE);
139
140    final NamespacedKey structureKey = NamespacedKey.fromString(structure.toLowerCase(Locale.ROOT));
141    if(structureKey == null) {
142      return false;
143    }
144
145    final Structure structureObj = registry.get(structureKey);
146    if(structureObj == null) {
147      return false;
148    }
149
150    return !chunk.getStructures(structureObj).isEmpty();
151  }
152}