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