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