mirror of
https://github.com/Xiao-MoMi/Custom-Fishing.git
synced 2026-01-06 15:51:50 +00:00
1.0
This commit is contained in:
@@ -21,8 +21,12 @@ import net.momirealms.customfishing.ConfigReader;
|
||||
import net.momirealms.customfishing.CustomFishing;
|
||||
import net.momirealms.customfishing.competition.bossbar.BossBarConfig;
|
||||
import net.momirealms.customfishing.competition.bossbar.BossBarManager;
|
||||
import net.momirealms.customfishing.item.Loot;
|
||||
import net.momirealms.customfishing.competition.ranking.Ranking;
|
||||
import net.momirealms.customfishing.competition.ranking.RankingImpl;
|
||||
import net.momirealms.customfishing.competition.ranking.RedisRankingImpl;
|
||||
import net.momirealms.customfishing.competition.reward.Reward;
|
||||
import net.momirealms.customfishing.utils.AdventureManager;
|
||||
import net.momirealms.customfishing.utils.JedisUtil;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
@@ -43,6 +47,7 @@ public class Competition {
|
||||
private final BossBarConfig bossBarConfig;
|
||||
private final List<String> startMessage;
|
||||
private final List<String> endMessage;
|
||||
private final HashMap<String, List<Reward>> rewardsMap;
|
||||
|
||||
public static long remainingTime;
|
||||
public static float progress;
|
||||
@@ -55,21 +60,24 @@ public class Competition {
|
||||
this.bossBarConfig = competitionConfig.getBossBarConfig();
|
||||
this.startMessage = competitionConfig.getStartMessage();
|
||||
this.endMessage = competitionConfig.getEndMessage();
|
||||
this.rewardsMap = competitionConfig.getRewards();
|
||||
}
|
||||
|
||||
public void begin(boolean forceStart) {
|
||||
|
||||
if (goal == Goal.RANDOM) {
|
||||
goal = getRandomGoal();
|
||||
}
|
||||
|
||||
remainingTime = this.duration;
|
||||
this.startTime = Instant.now().getEpochSecond();
|
||||
|
||||
Collection<? extends Player> playerCollections = Bukkit.getOnlinePlayers();
|
||||
if (playerCollections.size() >= minPlayers || forceStart) {
|
||||
status = true;
|
||||
ranking = new Ranking();
|
||||
if (JedisUtil.useRedis){
|
||||
ranking = new RedisRankingImpl();
|
||||
}else {
|
||||
ranking = new RankingImpl();
|
||||
}
|
||||
startTimer();
|
||||
if (startMessage != null){
|
||||
playerCollections.forEach(player -> {
|
||||
@@ -118,16 +126,18 @@ public class Competition {
|
||||
BossBarManager.stopAllTimer();
|
||||
this.timerTask.cancel();
|
||||
status = false;
|
||||
givePrize();
|
||||
if (endMessage != null){
|
||||
List<String> newMessage = new ArrayList<>();
|
||||
endMessage.forEach(message -> {
|
||||
float first = ranking.getScoreAt(1);
|
||||
float second = ranking.getScoreAt(2);
|
||||
float third = ranking.getScoreAt(3);
|
||||
CompetitionPlayer[] competitionPlayers = ranking.getTop3Player();
|
||||
float first = Optional.ofNullable(competitionPlayers[0]).orElse(CompetitionPlayer.emptyPlayer).getScore();
|
||||
float second = Optional.ofNullable(competitionPlayers[1]).orElse(CompetitionPlayer.emptyPlayer).getScore();
|
||||
float third = Optional.ofNullable(competitionPlayers[2]).orElse(CompetitionPlayer.emptyPlayer).getScore();
|
||||
newMessage.add(message
|
||||
.replace("{1st}", Optional.ofNullable(ranking.getPlayerAt(1)).orElse(ConfigReader.Message.noPlayer))
|
||||
.replace("{2nd}", Optional.ofNullable(ranking.getPlayerAt(2)).orElse(ConfigReader.Message.noPlayer))
|
||||
.replace("{3rd}", Optional.ofNullable(ranking.getPlayerAt(3)).orElse(ConfigReader.Message.noPlayer))
|
||||
.replace("{1st}", Optional.ofNullable(Optional.ofNullable(competitionPlayers[0]).orElse(CompetitionPlayer.emptyPlayer).getPlayer()).orElse(ConfigReader.Message.noPlayer))
|
||||
.replace("{2nd}", Optional.ofNullable(Optional.ofNullable(competitionPlayers[1]).orElse(CompetitionPlayer.emptyPlayer).getPlayer()).orElse(ConfigReader.Message.noPlayer))
|
||||
.replace("{3rd}", Optional.ofNullable(Optional.ofNullable(competitionPlayers[2]).orElse(CompetitionPlayer.emptyPlayer).getPlayer()).orElse(ConfigReader.Message.noPlayer))
|
||||
.replace("{1st_points}", first < 0 ? ConfigReader.Message.noScore : String.format("%.1f",(first)))
|
||||
.replace("{2nd_points}", second < 0 ? ConfigReader.Message.noScore : String.format("%.1f",(second)))
|
||||
.replace("{3rd_points}", third < 0 ? ConfigReader.Message.noScore : String.format("%.1f",(third))));
|
||||
@@ -138,7 +148,44 @@ public class Competition {
|
||||
});
|
||||
});
|
||||
}
|
||||
ranking.clear();
|
||||
Bukkit.getScheduler().runTaskLaterAsynchronously(CustomFishing.instance, ()-> {
|
||||
ranking.clear();
|
||||
}, 300);
|
||||
}
|
||||
|
||||
public void givePrize(){
|
||||
if (ranking.getSize() != 0 && rewardsMap != null) {
|
||||
Iterator<String> iterator = ranking.getIterator();
|
||||
int i = 1;
|
||||
while (iterator.hasNext()) {
|
||||
if (i < rewardsMap.size()) {
|
||||
String playerName = iterator.next();
|
||||
Player player = Bukkit.getPlayer(playerName);
|
||||
if (player != null){
|
||||
for (Reward reward : rewardsMap.get(String.valueOf(i))) {
|
||||
reward.giveReward(player);
|
||||
}
|
||||
}
|
||||
i++;
|
||||
}
|
||||
else {
|
||||
List<Reward> rewards = rewardsMap.get("participation");
|
||||
if (rewards != null) {
|
||||
iterator.forEachRemaining(playerName -> {
|
||||
Player player = Bukkit.getPlayer(playerName);
|
||||
if (player != null){
|
||||
for (Reward reward : rewards) {
|
||||
reward.giveReward(player);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void cancel() {
|
||||
@@ -148,18 +195,10 @@ public class Competition {
|
||||
status = false;
|
||||
}
|
||||
|
||||
public void refreshRanking(String player, Loot loot) {
|
||||
CompetitionPlayer competitionPlayer = ranking.getCompetitionPlayer(player);
|
||||
float score;
|
||||
if (this.goal == Goal.TOTAL_POINTS) score = loot.getPoint();
|
||||
else score = 1.0f;
|
||||
if (competitionPlayer != null) {
|
||||
ranking.removePlayer(competitionPlayer);
|
||||
competitionPlayer.addScore(score);
|
||||
ranking.addPlayer(competitionPlayer);
|
||||
} else {
|
||||
ranking.addPlayer(player, score);
|
||||
}
|
||||
public void refreshRanking(String player, float score) {
|
||||
if (this.goal != Goal.TOTAL_SCORE) score = 1.0f;
|
||||
if (score == 0) return;
|
||||
ranking.refreshData(player, score);
|
||||
}
|
||||
|
||||
private Goal getRandomGoal() {
|
||||
|
||||
Reference in New Issue
Block a user