001package net.tnemc.plugincore.sponge.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.sponge8.SpongePlayer;
022import net.tnemc.plugincore.core.compatibility.PlayerProvider;
023import net.tnemc.plugincore.core.io.message.MessageData;
024import org.spongepowered.api.data.Keys;
025import org.spongepowered.api.entity.living.player.User;
026import org.spongepowered.api.entity.living.player.server.ServerPlayer;
027import org.spongepowered.api.world.Location;
028import org.spongepowered.api.world.server.ServerWorld;
029import org.spongepowered.plugin.PluginContainer;
030
031import java.util.List;
032import java.util.Optional;
033import java.util.stream.Collectors;
034
035/**
036 * The Sponge implementation of the {@link PlayerProvider}.
037 *
038 * @author creatorfromhell
039 * @since 0.1.2.0
040 */
041public class SpongePlayerProvider extends SpongePlayer implements PlayerProvider {
042
043  public SpongePlayerProvider(final User user, final PluginContainer container) {
044
045    super(user, container);
046  }
047
048  /**
049   * Used to get the name of this player.
050   *
051   * @return The name of this player.
052   */
053  @Override
054  public String getName() {
055
056    return user.name();
057  }
058
059  /**
060   * Used to get the location of this player.
061   *
062   * @return The location of this player.
063   */
064  @Override
065  public Optional<net.tnemc.plugincore.core.compatibility.Location> getLocation() {
066
067    if(user.isOnline()) {
068
069      final Optional<ServerPlayer> player = user.player();
070      if(player.isPresent()) {
071        final Location<?, ?> locale = player.get().location();
072
073        return Optional.of(new net.tnemc.plugincore.core.compatibility.Location(((ServerWorld)locale.world()).key().value(),
074                                                                                locale.x(), locale.y(),
075                                                                                locale.z()));
076      }
077    }
078    return Optional.empty();
079  }
080
081  @Override
082  public String world() {
083
084    //TODO: Default world.
085    String world = "world";
086
087    if(user.isOnline()) {
088
089      final Optional<ServerPlayer> player = user.player();
090      if(player.isPresent()) {
091        world = ((ServerWorld)player.get().location().world()).key().value();
092      }
093    }
094    return world;
095  }
096
097  @Override
098  public String biome() {
099
100    String biome = "world";
101
102    if(user.isOnline()) {
103
104      final Optional<ServerPlayer> player = user.player();
105      if(player.isPresent()) {
106        biome = player.get().location().biome().toString();
107      }
108    }
109    return biome;
110  }
111
112  /**
113   * Used to get the amount of experience this player has.
114   *
115   * @return The amount of levels this player has.
116   */
117  @Override
118  public int getExp() {
119
120    if(user.player().isPresent()) {
121      return user.player().get().get(Keys.EXPERIENCE).orElse(0);
122    }
123    return 0;
124  }
125
126  /**
127   * Used to set the amount of experience this player has.
128   *
129   * @param exp The amount of experience to set for this player.
130   */
131  @Override
132  public void setExp(final int exp) {
133
134    if(user.player().isPresent()) {
135      user.player().get().offer(Keys.EXPERIENCE, exp);
136    }
137  }
138
139  /**
140   * Used to get the amount of experience levels this player has.
141   *
142   * @return The amount of experience levels this player has.
143   */
144  @Override
145  public int getExpLevel() {
146
147    if(user.player().isPresent()) {
148      return user.player().get().get(Keys.EXPERIENCE_LEVEL).orElse(0);
149    }
150    return 0;
151  }
152
153  /**
154   * Used to set the amount of experience levels this player has.
155   *
156   * @param level The amount of experience levels to set for this player.
157   */
158  @Override
159  public void setExpLevel(final int level) {
160
161    if(user.player().isPresent()) {
162      user.player().get().offer(Keys.EXPERIENCE_LEVEL, level);
163    }
164  }
165
166  @Override
167  public SpongeInventoryProvider inventory() {
168
169    return new SpongeInventoryProvider(identifier(), container);
170  }
171
172  /**
173   * Method for retrieving player permissions.
174   *
175   * @return A list of permission strings.
176   */
177  @Override
178  public List<String> getEffectivePermissions() {
179
180    return user.subjectData().allPermissions().values().stream()
181            .flatMap(map->map.keySet().stream())
182            .collect(Collectors.toList());
183  }
184
185  /**
186   * Used to send a message to this command source.
187   *
188   * @param messageData The message data to utilize for this translation.
189   */
190  @Override
191  public void message(final MessageData messageData) {
192
193  }
194
195  @Override
196  public void message(final String message) {
197
198    message(new MessageData(message));
199  }
200}