001package net.tnemc.plugincore.bukkit.impl;
002
003/*
004 * The New Plugin Core
005 * Copyright (C) 2022 - 2024 Daniel "creatorfromhell" Vidmar
006 *
007 * This program is free software: you can redistribute it and/or modify
008 * it under the terms of the GNU Affero General Public License as published by
009 * the Free Software Foundation, either version 3 of the License, or
010 * (at your option) any later version.
011 *
012 * This program is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015 * GNU Affero General Public License for more details.
016 *
017 * You should have received a copy of the GNU Affero General Public License
018 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
019 */
020
021import net.kyori.adventure.platform.bukkit.BukkitAudiences;
022import net.tnemc.menu.bukkit.BukkitPlayer;
023import net.tnemc.plugincore.bukkit.BukkitPluginCore;
024import net.tnemc.plugincore.core.compatibility.Location;
025import net.tnemc.plugincore.core.compatibility.PlayerProvider;
026import net.tnemc.plugincore.core.io.message.MessageData;
027import net.tnemc.plugincore.core.io.message.MessageHandler;
028import org.bukkit.Bukkit;
029import org.bukkit.OfflinePlayer;
030import org.bukkit.permissions.PermissionAttachmentInfo;
031
032import java.util.ArrayList;
033import java.util.List;
034import java.util.Optional;
035import java.util.UUID;
036import java.util.stream.Collectors;
037
038/**
039 * BukkitPlayerProvider
040 *
041 * @author creatorfromhell
042 * @since 0.1.2.0
043 */
044public class BukkitPlayerProvider extends BukkitPlayer implements PlayerProvider {
045
046  private final OfflinePlayer player;
047
048  public BukkitPlayerProvider(final OfflinePlayer player) {
049    super(player, BukkitPluginCore.instance().getPlugin());
050    this.player = player;
051  }
052
053  /**
054   * Used to get the {@link UUID} of this player.
055   *
056   * @return The {@link UUID} of this player.
057   */
058  @Override
059  public UUID identifier() {
060    return player.getUniqueId();
061  }
062
063  public OfflinePlayer getPlayer() {
064    return player;
065  }
066
067  /**
068   * Used to get the name of this player.
069   *
070   * @return The name of this player.
071   */
072  @Override
073  public String getName() {
074    return player.getName();
075  }
076
077  /**
078   * Used to get the location of this player.
079   *
080   * @return The location of this player.
081   */
082  @Override
083  public Optional<Location> getLocation() {
084    if(player.getPlayer() == null) {
085      return Optional.empty();
086    }
087
088    final org.bukkit.Location locale = player.getPlayer().getLocation();
089
090    return Optional.of(new Location(locale.getWorld().getName(),
091                                    locale.getX(), locale.getY(),
092                                    locale.getZ()
093    ));
094  }
095
096  /**
097   * Used to get the name of the world this player is currently in.
098   *
099   * @return The name of the world.
100   */
101  @Override
102  public String world() {
103    String world = BukkitPluginCore.instance().getPlugin().getServer().getWorlds().get(0).getName();
104
105    if(player.getPlayer() != null) {
106      world = player.getPlayer().getWorld().getName();
107    }
108    return world;
109  }
110
111  /**
112   * Used to get the name of the biome this player is currently in.
113   *
114   * @return The name of the biome.
115   */
116  @Override
117  public String biome() {
118    String biome = BukkitPluginCore.instance().getPlugin().getServer().getWorlds().get(0).getName();
119
120    if(player.getPlayer() != null) {
121      biome = player.getPlayer().getLocation().getBlock().getBiome().getKey().getKey();
122    }
123    return biome;
124  }
125
126  /**
127   * Used to get the amount of experience this player has.
128   *
129   * @return The amount of levels this player has.
130   */
131  @Override
132  public int getExp() {
133    if(player.getPlayer() == null) {
134      return 0;
135    }
136    return (int)player.getPlayer().getExp();
137  }
138
139  /**
140   * Used to set the amount of experience this player has.
141   *
142   * @param exp The amount of experience to set for this player.
143   */
144  @Override
145  public void setExp(final int exp) {
146    if(player.getPlayer() != null) {
147      player.getPlayer().setTotalExperience(exp);
148    }
149  }
150
151  /**
152   * Used to get the amount of experience levels this player has.
153   *
154   * @return The amount of experience levels this player has.
155   */
156  @Override
157  public int getExpLevel() {
158    if(player.getPlayer() == null) {
159      return 0;
160    }
161    return player.getPlayer().getLevel();
162  }
163
164  /**
165   * Used to set the amount of experience levels this player has.
166   *
167   * @param level The amount of experience levels to set for this player.
168   */
169  @Override
170  public void setExpLevel(final int level) {
171    if(player.getPlayer() != null) {
172      player.getPlayer().setLevel(level);
173    }
174  }
175
176  @Override
177  public BukkitInventoryProvider inventory() {
178    return new BukkitInventoryProvider(identifier(), BukkitPluginCore.instance().getPlugin());
179  }
180
181  /**
182   * Method for retrieving player permissions.
183   *
184   * @return A list of permission strings.
185   */
186  @Override
187  public List<String> getEffectivePermissions() {
188    if(player.getPlayer() == null) {
189
190      return new ArrayList<>();
191    }
192
193    return player.getPlayer().getEffectivePermissions().stream()
194            .map(PermissionAttachmentInfo::getPermission)
195            .collect(Collectors.toList());
196  }
197
198  /**
199   * Used to determine if this player has the specified permission node.
200   *
201   * @param permission The node to check for.
202   *
203   * @return True if the player has the permission, otherwise false.
204   */
205  @Override
206  public boolean hasPermission(final String permission) {
207    if(player.getPlayer() == null) {
208      return false;
209    }
210    return player.getPlayer().hasPermission(permission);
211  }
212
213  @Override
214  public void message(final String message) {
215    if(player.getPlayer() != null) {
216      message(new MessageData(message));
217    }
218  }
219
220  /**
221   * Used to send a message to this command source.
222   *
223   * @param messageData The message data to utilize for this translation.
224   */
225  @Override
226  public void message(final MessageData messageData) {
227    if(player.getPlayer() == null) {
228      return;
229    }
230
231    try(final BukkitAudiences provider = BukkitAudiences.create(BukkitPluginCore.instance().getPlugin())) {
232      MessageHandler.translate(messageData, player.getUniqueId(), provider.sender(player.getPlayer()));
233    }
234  }
235
236  public static BukkitPlayerProvider find(final String identifier) {
237    try {
238      return new BukkitPlayerProvider(Bukkit.getOfflinePlayer(UUID.fromString(identifier)));
239    } catch (final Exception ignore) {
240      return new BukkitPlayerProvider(Bukkit.getOfflinePlayer(identifier));
241    }
242  }
243}