Skip to content
This repository has been archived by the owner on Apr 27, 2024. It is now read-only.

Commit

Permalink
Solve #30
Browse files Browse the repository at this point in the history
WIP
  • Loading branch information
iceBear67 committed Aug 23, 2021
1 parent 657e746 commit 096c6c7
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/main/java/cc/sfclub/game/Oyster.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/cc/sfclub/game/api/OysterAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -40,4 +46,14 @@ public class OysterAPI {
public static OysterAPI getInstance() {
return Oyster.getPlugin(Oyster.class).getApi();
}

/**
* 根据 UUID 查找到玩家
*
* @param playerUUID
* @return Optional\<Player\>
*/
public Optional<OysterPlayer> 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();
}
}
40 changes: 40 additions & 0 deletions src/main/java/cc/sfclub/game/listeners/JoinOrLeave.java
Original file line number Diff line number Diff line change
@@ -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) {

}
}
26 changes: 26 additions & 0 deletions src/main/java/cc/sfclub/game/module/player/OysterPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -61,7 +63,17 @@ public class OysterPlayer extends OysterEntity<OysterPlayer> {
private final OysterTeam team;
@Getter(AccessLevel.PRIVATE)
private final Set<Flag<OysterPlayer>> 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<Flag<OysterPlayer>> flags) {
this.bukkitPlayer = bukkitPlayer;
Expand Down Expand Up @@ -139,6 +151,15 @@ public Player getBukkitPlayer() {
return Bukkit.getPlayer(bukkitPlayer);
}

/**
* 获取玩家的 UUID
*
* @return
*/
public UUID getUniqueID() {
return bukkitPlayer;
}

@Override
public Set<Flag<OysterPlayer>> getFlags() {
return sortedFlags;
Expand All @@ -159,6 +180,11 @@ public void onData(GameEvent event) {
mechanic.onData(event);
}

@Nullable
public Location getLastOnlineLocation() {
return lastOnlineLocation;
}

@Nullable
@Override
public Entity getAsEntity() {
Expand Down

0 comments on commit 096c6c7

Please sign in to comment.