001package net.tnemc.plugincore.bukkit.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 net.tnemc.plugincore.core.compatibility.ChunkProvider;
021import org.bukkit.Chunk;
022import org.bukkit.NamespacedKey;
023import org.bukkit.Registry;
024import org.bukkit.block.Biome;
025import org.bukkit.generator.structure.Structure;
026import org.jetbrains.annotations.NotNull;
027
028import java.util.Locale;
029
030/**
031 * BukkitChunkProvider
032 *
033 * @author creatorfromhell
034 * @since 1.1.0.1
035 */
036public class BukkitChunkProvider implements ChunkProvider {
037
038  private final Chunk chunk;
039  private final int y;
040
041  public BukkitChunkProvider(final Chunk chunk, final int y) {
042
043    this.chunk = chunk;
044    this.y = y;
045  }
046
047  /**
048   * This method is used to get the chunk's x coordinate.
049   *
050   * @return The chunk's x coordinate.
051   */
052  @Override
053  public int x() {
054
055    return chunk.getX();
056  }
057
058  /**
059   * This method is used to get the chunk's y coordinate.
060   *
061   * @return The chunk's y coordinate.
062   */
063  @Override
064  public int y() {
065
066    return y;
067  }
068
069  /**
070   * This method is used to get the chunk's z coordinate.
071   *
072   * @return The chunk's z coordinate.
073   */
074  @Override
075  public int z() {
076
077    return chunk.getZ();
078  }
079
080  /**
081   * Checks if the ChunkProvider represents a slime chunk.
082   *
083   * @return True if the ChunkProvider is a slime chunk, false otherwise.
084   */
085  @Override
086  public boolean isSlime() {
087
088    return chunk.isSlimeChunk();
089  }
090
091  /**
092   * Checks if the specified biome is contained within the chunk provider.
093   *
094   * @param biome The biome to check for.
095   *
096   * @return True if the specified biome is contained, false otherwise.
097   */
098  @Override
099  public boolean containsBiome(final @NotNull String biome) {
100
101    try {
102
103      final NamespacedKey biomeKey = NamespacedKey.fromString(biome.toLowerCase(Locale.ROOT));
104      if(biomeKey == null) {
105        return false;
106      }
107
108      final Biome biomeObj = Registry.BIOME.get(biomeKey);
109      if(biomeObj == null) {
110        return false;
111      }
112
113      return chunk.contains(biomeObj);
114
115    } catch(final NoSuchMethodError ignore) {
116
117      return chunk.contains(Biome.valueOf(biome));
118    }
119  }
120
121  /**
122   * Checks if the provided structure is contained. Only on 1.21.1.
123   *
124   * @param structure The structure to check if contained.
125   *
126   * @return True if the structure is contained, false otherwise.
127   */
128  @Override
129  public boolean containsStructure(final @NotNull String structure) {
130
131    final NamespacedKey structureKey = NamespacedKey.fromString(structure.toLowerCase(Locale.ROOT));
132    if(structureKey == null) {
133      return false;
134    }
135
136    final Structure structureObj = Registry.STRUCTURE.get(structureKey);
137    if(structureObj == null) {
138      return false;
139    }
140
141    return !chunk.getStructures(structureObj).isEmpty();
142  }
143}