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 050 super(player, BukkitPluginCore.instance().getPlugin()); 051 this.player = player; 052 } 053 054 public static BukkitPlayerProvider find(final String identifier) { 055 056 try { 057 return new BukkitPlayerProvider(Bukkit.getOfflinePlayer(UUID.fromString(identifier))); 058 } catch(final Exception ignore) { 059 return new BukkitPlayerProvider(Bukkit.getOfflinePlayer(identifier)); 060 } 061 } 062 063 /** 064 * Used to get the {@link UUID} of this player. 065 * 066 * @return The {@link UUID} of this player. 067 */ 068 @Override 069 public UUID identifier() { 070 071 return player.getUniqueId(); 072 } 073 074 public OfflinePlayer getPlayer() { 075 076 return player; 077 } 078 079 /** 080 * Used to get the name of this player. 081 * 082 * @return The name of this player. 083 */ 084 @Override 085 public String getName() { 086 087 return player.getName(); 088 } 089 090 /** 091 * Used to get the location of this player. 092 * 093 * @return The location of this player. 094 */ 095 @Override 096 public Optional<Location> getLocation() { 097 098 if(player.getPlayer() == null) { 099 return Optional.empty(); 100 } 101 102 final org.bukkit.Location locale = player.getPlayer().getLocation(); 103 104 return Optional.of(new Location(locale.getWorld().getName(), 105 locale.getX(), locale.getY(), 106 locale.getZ() 107 )); 108 } 109 110 /** 111 * Used to get the name of the world this player is currently in. 112 * 113 * @return The name of the world. 114 */ 115 @Override 116 public String world() { 117 118 String world = BukkitPluginCore.instance().getPlugin().getServer().getWorlds().get(0).getName(); 119 120 if(player.getPlayer() != null) { 121 world = player.getPlayer().getWorld().getName(); 122 } 123 return world; 124 } 125 126 /** 127 * Used to get the name of the biome this player is currently in. 128 * 129 * @return The name of the biome. 130 */ 131 @Override 132 public String biome() { 133 134 String biome = BukkitPluginCore.instance().getPlugin().getServer().getWorlds().get(0).getName(); 135 136 if(player.getPlayer() != null) { 137 biome = player.getPlayer().getLocation().getBlock().getBiome().getKey().getKey(); 138 } 139 return biome; 140 } 141 142 /** 143 * Used to get the amount of experience this player has. 144 * 145 * @return The amount of levels this player has. 146 */ 147 @Override 148 public int getExp() { 149 150 if(player.getPlayer() == null) { 151 return 0; 152 } 153 return (int)player.getPlayer().getExp(); 154 } 155 156 /** 157 * Used to set the amount of experience this player has. 158 * 159 * @param exp The amount of experience to set for this player. 160 */ 161 @Override 162 public void setExp(final int exp) { 163 164 if(player.getPlayer() != null) { 165 player.getPlayer().setTotalExperience(exp); 166 } 167 } 168 169 /** 170 * Used to get the amount of experience levels this player has. 171 * 172 * @return The amount of experience levels this player has. 173 */ 174 @Override 175 public int getExpLevel() { 176 177 if(player.getPlayer() == null) { 178 return 0; 179 } 180 return player.getPlayer().getLevel(); 181 } 182 183 /** 184 * Used to set the amount of experience levels this player has. 185 * 186 * @param level The amount of experience levels to set for this player. 187 */ 188 @Override 189 public void setExpLevel(final int level) { 190 191 if(player.getPlayer() != null) { 192 player.getPlayer().setLevel(level); 193 } 194 } 195 196 @Override 197 public BukkitInventoryProvider inventory() { 198 199 return new BukkitInventoryProvider(identifier(), BukkitPluginCore.instance().getPlugin()); 200 } 201 202 /** 203 * Method for retrieving player permissions. 204 * 205 * @return A list of permission strings. 206 */ 207 @Override 208 public List<String> getEffectivePermissions() { 209 210 if(player.getPlayer() == null) { 211 212 return new ArrayList<>(); 213 } 214 215 return player.getPlayer().getEffectivePermissions().stream() 216 .map(PermissionAttachmentInfo::getPermission) 217 .collect(Collectors.toList()); 218 } 219 220 /** 221 * Used to determine if this player has the specified permission node. 222 * 223 * @param permission The node to check for. 224 * 225 * @return True if the player has the permission, otherwise false. 226 */ 227 @Override 228 public boolean hasPermission(final String permission) { 229 230 if(player.getPlayer() == null) { 231 return false; 232 } 233 return player.getPlayer().hasPermission(permission); 234 } 235 236 @Override 237 public void message(final String message) { 238 239 if(player.getPlayer() != null) { 240 message(new MessageData(message)); 241 } 242 } 243 244 /** 245 * Used to send a message to this command source. 246 * 247 * @param messageData The message data to utilize for this translation. 248 */ 249 @Override 250 public void message(final MessageData messageData) { 251 252 if(player.getPlayer() == null) { 253 return; 254 } 255 256 try(final BukkitAudiences provider = BukkitAudiences.create(BukkitPluginCore.instance().getPlugin())) { 257 MessageHandler.translate(messageData, player.getUniqueId(), provider.sender(player.getPlayer())); 258 } 259 } 260}