diff --git a/src/main/java/cc/sfclub/game/Oyster.java b/src/main/java/cc/sfclub/game/Oyster.java index 4f3ac13..9f157b3 100644 --- a/src/main/java/cc/sfclub/game/Oyster.java +++ b/src/main/java/cc/sfclub/game/Oyster.java @@ -23,12 +23,14 @@ import cc.sfclub.game.api.OysterAPI; import cc.sfclub.game.config.OysterConfig; +import cc.sfclub.game.listeners.JoinOrLeave; import cc.sfclub.game.module.i18n.LocaleLoader; import cc.sfclub.game.task.UpdateChecker; import cc.sfclub.game.util.Log; import cc.sfclub.game.util.SimpleConfig; import lombok.Getter; import lombok.SneakyThrows; +import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; import org.jetbrains.annotations.ApiStatus; @@ -82,6 +84,10 @@ public OysterConfig getOysterConfig() { return wrappedConfig.get(); } + public void registerListeners() { + Bukkit.getPluginManager().registerEvents(new JoinOrLeave(), this); + } + @SneakyThrows private void extractLangs() { saveResource("locale.zip", true); diff --git a/src/main/java/cc/sfclub/game/api/OysterAPI.java b/src/main/java/cc/sfclub/game/api/OysterAPI.java index 2f40e96..e12b9fa 100644 --- a/src/main/java/cc/sfclub/game/api/OysterAPI.java +++ b/src/main/java/cc/sfclub/game/api/OysterAPI.java @@ -24,9 +24,15 @@ import cc.sfclub.game.Oyster; import cc.sfclub.game.managers.FlagManager; import cc.sfclub.game.managers.GameManager; +import cc.sfclub.game.module.game.OysterGame; +import cc.sfclub.game.module.player.OysterPlayer; import lombok.Getter; import org.jetbrains.annotations.ApiStatus; +import java.util.Collection; +import java.util.Optional; +import java.util.UUID; + /** * Oyster APIs */ @@ -40,4 +46,14 @@ public class OysterAPI { public static OysterAPI getInstance() { return Oyster.getPlugin(Oyster.class).getApi(); } + + /** + * 根据 UUID 查找到玩家 + * + * @param playerUUID + * @return Optional\ + */ + public Optional findByPlayer(UUID playerUUID) { //todo to be optimized by caches. + return gameManager.getGames().map(OysterGame::getPlayers).flatMap(Collection::stream).filter(e -> e.getUniqueID().equals(playerUUID)).findFirst(); + } } diff --git a/src/main/java/cc/sfclub/game/listeners/JoinOrLeave.java b/src/main/java/cc/sfclub/game/listeners/JoinOrLeave.java new file mode 100644 index 0000000..189bb7a --- /dev/null +++ b/src/main/java/cc/sfclub/game/listeners/JoinOrLeave.java @@ -0,0 +1,40 @@ +/* + * + * Oyster - The universal minigame framework for spigot servers. + * Copyright (C) 2021 SaltedFish Club + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 + * USA + */ + +package cc.sfclub.game.listeners; + +import cc.sfclub.game.api.OysterAPI; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.player.PlayerQuitEvent; + +public class JoinOrLeave implements Listener { + @EventHandler + public void onLeave(PlayerQuitEvent event) { + OysterAPI.getInstance().findByPlayer(event.getPlayer().getUniqueId()).ifPresent(p -> p.setLastOnlineLocation(event.getPlayer().getLocation())); + } + + @EventHandler + public void onJoin(PlayerJoinEvent event) { + + } +} diff --git a/src/main/java/cc/sfclub/game/module/player/OysterPlayer.java b/src/main/java/cc/sfclub/game/module/player/OysterPlayer.java index 182652e..6d0ee64 100644 --- a/src/main/java/cc/sfclub/game/module/player/OysterPlayer.java +++ b/src/main/java/cc/sfclub/game/module/player/OysterPlayer.java @@ -29,7 +29,9 @@ import lombok.AccessLevel; import lombok.Getter; import lombok.NonNull; +import lombok.Setter; import org.bukkit.Bukkit; +import org.bukkit.Location; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.jetbrains.annotations.ApiStatus; @@ -61,7 +63,17 @@ public class OysterPlayer extends OysterEntity { private final OysterTeam team; @Getter(AccessLevel.PRIVATE) private final Set> sortedFlags; + /** + * 获取所属的游戏 + */ + @Getter private final OysterGame game; + /** + * 上次在线位置 + * // TODO: 23/08/2021 改良为OysterOfflinePlayerInfo & #30 Be abstractly 隐藏实现 + */ + @Setter + private Location lastOnlineLocation = null; public OysterPlayer(UUID bukkitPlayer, PlayerMechanic mechanic, OysterTeam team, OysterGame game, Collection> flags) { this.bukkitPlayer = bukkitPlayer; @@ -139,6 +151,15 @@ public Player getBukkitPlayer() { return Bukkit.getPlayer(bukkitPlayer); } + /** + * 获取玩家的 UUID + * + * @return + */ + public UUID getUniqueID() { + return bukkitPlayer; + } + @Override public Set> getFlags() { return sortedFlags; @@ -159,6 +180,11 @@ public void onData(GameEvent event) { mechanic.onData(event); } + @Nullable + public Location getLastOnlineLocation() { + return lastOnlineLocation; + } + @Nullable @Override public Entity getAsEntity() {