Fix achievements causing a CME

This commit is contained in:
Sam 2018-09-14 15:55:51 +01:00 committed by Alexander Meech
parent 1dd451ed42
commit bd3b9ffc07
12 changed files with 79 additions and 63 deletions

View File

@ -1471,7 +1471,7 @@ public enum Achievement
new String[]{"Nano Games.Wins"}, new String[]{"Nano Games.Wins"},
new String[]{"Win Nano Games"}, new String[]{"Win Nano Games"},
new int[][] {new int[] {100,0,1000}, new int[] {0,0,5000}, new int[]{0,0,10000}, new int[]{0,0,25000}, new int[]{0,0,50000}}, new int[][] {new int[] {100,0,1000}, new int[] {0,0,5000}, new int[]{0,0,10000}, new int[]{0,0,25000}, new int[]{0,0,50000}},
new int[]{1,10,50,100,500}, new int[]{2,10,50,100,500},
"I", "I",
new String[] {"II","III","IV","V","X"}, new String[] {"II","III","IV","V","X"},
AchievementCategory.NANO_GAMES), AchievementCategory.NANO_GAMES),
@ -1494,12 +1494,6 @@ public enum Achievement
new int[]{1}, new int[]{1},
AchievementCategory.NANO_GAMES), AchievementCategory.NANO_GAMES),
NANO_CASH_OUT("Cha-Ching", 500,
new String[]{"Nano Games.CashOut"},
new String[]{"Cash out for the first time"},
new int[]{1},
AchievementCategory.NANO_GAMES),
NANO_PLAY("Nani Games?", 500, NANO_PLAY("Nani Games?", 500,
new String[]{"Nano Games.Wins", "Nano Games.Losses"}, new String[]{"Nano Games.Wins", "Nano Games.Losses"},
new String[]{"Play a Nano Game"}, new String[]{"Play a Nano Game"},

View File

@ -1,5 +1,7 @@
package mineplex.core.achievement; package mineplex.core.achievement;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID; import java.util.UUID;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -14,7 +16,6 @@ import mineplex.core.account.permissions.Permission;
import mineplex.core.account.permissions.PermissionGroup; import mineplex.core.account.permissions.PermissionGroup;
import mineplex.core.achievement.command.StatsCommand; import mineplex.core.achievement.command.StatsCommand;
import mineplex.core.achievement.ui.AchievementShop; import mineplex.core.achievement.ui.AchievementShop;
import mineplex.core.common.util.NautHashMap;
import mineplex.core.donation.DonationManager; import mineplex.core.donation.DonationManager;
import mineplex.core.elo.EloManager; import mineplex.core.elo.EloManager;
import mineplex.core.gadget.GadgetManager; import mineplex.core.gadget.GadgetManager;
@ -46,7 +47,7 @@ public class AchievementManager extends MiniPlugin
private final AchievementShop _shop; private final AchievementShop _shop;
private final NautHashMap<String, NautHashMap<Achievement, AchievementLog>> _log = new NautHashMap<>(); private final Map<String, Map<Achievement, AchievementLog>> _log = new HashMap<>();
public AchievementManager(StatsManager statsManager, CoreClientManager clientManager, DonationManager donationManager, IncognitoManager incognitoManager, EloManager eloManager) public AchievementManager(StatsManager statsManager, CoreClientManager clientManager, DonationManager donationManager, IncognitoManager incognitoManager, EloManager eloManager)
{ {
@ -116,8 +117,7 @@ public class AchievementManager extends MiniPlugin
{ {
if (stat.equalsIgnoreCase(event.getStatName())) if (stat.equalsIgnoreCase(event.getStatName()))
{ {
if (!_log.containsKey(player.getName())) _log.computeIfAbsent(player.getName(), k -> new HashMap<>());
_log.put(player.getName(), new NautHashMap<>());
//Record that achievement has leveled up //Record that achievement has leveled up
if (type.getLevelData(event.getValueAfter()).getLevel() > type.getLevelData(event.getValueBefore()).getLevel()) if (type.getLevelData(event.getValueAfter()).getLevel() > type.getLevelData(event.getValueBefore()).getLevel())
@ -194,7 +194,7 @@ public class AchievementManager extends MiniPlugin
_log.remove(player.getName()); _log.remove(player.getName());
} }
public NautHashMap<Achievement, AchievementLog> getLog(Player player) public Map<Achievement, AchievementLog> getLog(Player player)
{ {
return _log.get(player.getName()); return _log.get(player.getName());
} }

View File

@ -49,9 +49,11 @@ public class LoopedNotePlayer implements Runnable, Component, Lifetimed
this(lifetime, song, listeners, null); this(lifetime, song, listeners, null);
} }
public LoopedNotePlayer(Lifetime lifetime, NoteSong song, Player listener, int sleepMillis) private LoopedNotePlayer(Lifetime lifetime, NoteSong song, Player listener, int sleepMillis)
{ {
this(lifetime, song, sleepMillis, Collections.singleton(listener), null); this(lifetime, song, sleepMillis, Collections.singleton(listener), null);
activate();
} }
public LoopedNotePlayer(Lifetime lifetime, NoteSong song, Collection<Player> listeners, Predicate<Player> shouldPlay) public LoopedNotePlayer(Lifetime lifetime, NoteSong song, Collection<Player> listeners, Predicate<Player> shouldPlay)
@ -164,9 +166,7 @@ public class LoopedNotePlayer implements Runnable, Component, Lifetimed
public LoopedNotePlayer cloneForPlayer(Player player, float tempoFactor) public LoopedNotePlayer cloneForPlayer(Player player, float tempoFactor)
{ {
LoopedNotePlayer notePlayer = new LoopedNotePlayer(getLifetime(), _song, player, (int) (_sleepMillis * tempoFactor)); return new LoopedNotePlayer(getLifetime(), _song, player, (int) (_sleepMillis * tempoFactor));
notePlayer.setTick(_tick);
return notePlayer;
} }
@Override @Override

View File

@ -8,7 +8,6 @@ import org.bukkit.Location;
import org.bukkit.Sound; import org.bukkit.Sound;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import mineplex.core.achievement.Achievement;
import mineplex.core.antihack.AntiHack; import mineplex.core.antihack.AntiHack;
import mineplex.core.common.util.UtilServer; import mineplex.core.common.util.UtilServer;
import mineplex.core.lifetimes.Lifetimed; import mineplex.core.lifetimes.Lifetimed;
@ -333,12 +332,6 @@ public abstract class Game extends ListenerComponent implements Lifetimed, TeamC
_statsComponent.addStat(player, stat, amount, limitTo1, global); _statsComponent.addStat(player, stat, amount, limitTo1, global);
} }
@Override
public void rewardAchievement(Player player, Achievement achievement)
{
_statsComponent.rewardAchievement(player, achievement);
}
/* /*
Component Getters Component Getters
*/ */

View File

@ -201,8 +201,6 @@ public class GameCurrencyManager extends GameManager implements CurrencyComponen
{ {
statsManager.incrementStat(player, gameName + ".PlayInARow", 1); statsManager.incrementStat(player, gameName + ".PlayInARow", 1);
} }
statsManager.incrementStat(player, gameName + ".CashOut", 1);
} }
// Remove from session // Remove from session

View File

@ -9,6 +9,7 @@ import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent; import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent; import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Sound;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
@ -81,12 +82,6 @@ public class GameStatsComponent extends GameComponent<Game> implements StatsComp
_statsManager.incrementStat(player, stat, amount); _statsManager.incrementStat(player, stat, amount);
} }
@Override
public void rewardAchievement(Player player, Achievement achievement)
{
getGame().addStat(player, achievement.getStats()[0], 1, true, achievement.getCategory() == AchievementCategory.GLOBAL);
}
/* /*
Copied from AchievementSummaryComponent Copied from AchievementSummaryComponent
// TODO Replace and reinvent when reimplementing achievements // TODO Replace and reinvent when reimplementing achievements
@ -104,15 +99,17 @@ public class GameStatsComponent extends GameComponent<Game> implements StatsComp
for (Player player : UtilServer.getPlayersCollection()) for (Player player : UtilServer.getPlayersCollection())
{ {
if (manager.getLog(player) == null) Map<Achievement, AchievementLog> logs = manager.getLog(player);
if (logs == null)
{ {
continue; continue;
} }
manager.getLog(player).entrySet().forEach(entry -> logs.forEach((achievement, log) ->
{ {
Achievement achievement = entry.getKey(); player.sendMessage(achievement.getName() + " - " + log.LevelUp + " " + log.Amount);
AchievementLog log = entry.getValue();
AchievementData data = manager.get(player, achievement); AchievementData data = manager.get(player, achievement);
if (log.LevelUp) if (log.LevelUp)
@ -133,14 +130,17 @@ public class GameStatsComponent extends GameComponent<Game> implements StatsComp
{ {
title = C.cPurpleB + "Achievement Get! "; title = C.cPurpleB + "Achievement Get! ";
_taskManager.completedTask(success -> if (!_taskManager.hasCompletedTask(player, achievement.getName()))
{ {
if (success) _taskManager.completedTask(success ->
{ {
_donationManager.rewardCurrencyUntilSuccess(GlobalCurrency.GEM, player, achievement.getName(), achievement.getGemReward()); if (success)
rewardPlayer(player, achievement, rewards); {
} _donationManager.rewardCurrencyUntilSuccess(GlobalCurrency.GEM, player, achievement.getName(), achievement.getGemReward());
}, player, achievement.getName()); rewardPlayer(player, achievement, rewards);
}
}, player, achievement.getName());
}
} }
else else
{ {
@ -151,12 +151,17 @@ public class GameStatsComponent extends GameComponent<Game> implements StatsComp
TextComponent message = new TextComponent(title + C.cGray + nameLevel); TextComponent message = new TextComponent(title + C.cGray + nameLevel);
message.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(C.cAquaB + nameLevel + "\n" + C.cDAqua + description.toString().substring(1) + reward) message.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(C.cAquaB + nameLevel + "\n" + C.cDAqua + description.toString().substring(1) + " " + reward)
.create())); .create()));
player.sendMessage("");
player.spigot().sendMessage(message); player.spigot().sendMessage(message);
player.sendMessage("");
player.playSound(player.getLocation(), Sound.LEVEL_UP, 1, 1);
} }
}); });
manager.clearLog(player);
} }
} }
@ -212,7 +217,7 @@ public class GameStatsComponent extends GameComponent<Game> implements StatsComp
{ {
reward += C.cGoldB + "+" + crowns + " Crowns "; reward += C.cGoldB + "+" + crowns + " Crowns ";
} }
if (xp > 0 && !_taskManager.hasCompletedTask(player, Achievement.GLOBAL_MINEPLEX_LEVEL.getName())) if (xp > 0)
{ {
reward += C.cYellowB + "+" + xp + " EXP "; reward += C.cYellowB + "+" + xp + " EXP ";
} }
@ -236,7 +241,8 @@ public class GameStatsComponent extends GameComponent<Game> implements StatsComp
} }
if (xp > 0) if (xp > 0)
{ {
_statsManager.incrementStat(player, Achievement.GLOBAL_MINEPLEX_LEVEL.getStats()[0], xp); // Need to delay a tick so we don't CME the achievement log loop
_statsManager.runSyncLater(() -> _statsManager.incrementStat(player, Achievement.GLOBAL_MINEPLEX_LEVEL.getStats()[0], xp), 0);
} }
} }
} }

View File

@ -6,7 +6,6 @@ import java.util.concurrent.TimeUnit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import mineplex.core.achievement.Achievement;
import mineplex.core.common.util.UtilPlayer; import mineplex.core.common.util.UtilPlayer;
import mineplex.game.nano.game.Game; import mineplex.game.nano.game.Game;
import mineplex.game.nano.game.Game.GameState; import mineplex.game.nano.game.Game.GameState;
@ -83,12 +82,12 @@ public class GeneralStatsTracker extends StatTracker<Game>
if (second != null) if (second != null)
{ {
second.forEach(player -> rewardAchievement(player, Achievement.NANO_SECOND_PLACE)); second.forEach(player -> addStat(player, "SecondPlace", 1, true, false));
} }
if (third != null) if (third != null)
{ {
third.forEach(player -> rewardAchievement(player, Achievement.NANO_THIRD_PLACE)); third.forEach(player -> addStat(player, "ThirdPlace", 1, true, false));
} }
} }
} }

View File

@ -4,7 +4,6 @@ import java.util.Arrays;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import mineplex.core.achievement.Achievement;
import mineplex.core.lifetimes.ListenerComponent; import mineplex.core.lifetimes.ListenerComponent;
import mineplex.game.nano.game.Game; import mineplex.game.nano.game.Game;
import mineplex.game.nano.game.Game.GameState; import mineplex.game.nano.game.Game.GameState;
@ -26,10 +25,4 @@ public class StatTracker<T extends Game> extends ListenerComponent implements St
{ {
_game.addStat(player, stat, amount, limitTo1, global); _game.addStat(player, stat, amount, limitTo1, global);
} }
@Override
public void rewardAchievement(Player player, Achievement achievement)
{
_game.rewardAchievement(player, achievement);
}
} }

View File

@ -2,13 +2,9 @@ package mineplex.game.nano.game.components.stats;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import mineplex.core.achievement.Achievement;
public interface StatsComponent public interface StatsComponent
{ {
void addStat(Player player, String stat, int amount, boolean limitTo1, boolean global); void addStat(Player player, String stat, int amount, boolean limitTo1, boolean global);
void rewardAchievement(Player player, Achievement achievement);
} }

View File

@ -1,6 +1,9 @@
package mineplex.game.nano.game.games.minekart; package mineplex.game.nano.game.games.minekart;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -40,6 +43,9 @@ import mineplex.core.common.util.UtilShapes;
import mineplex.core.common.util.UtilTextMiddle; import mineplex.core.common.util.UtilTextMiddle;
import mineplex.core.common.util.UtilTime; import mineplex.core.common.util.UtilTime;
import mineplex.core.itemstack.ItemBuilder; import mineplex.core.itemstack.ItemBuilder;
import mineplex.core.noteblock.LoopedNotePlayer;
import mineplex.core.noteblock.NBSReader;
import mineplex.core.noteblock.NoteSong;
import mineplex.core.packethandler.IPacketHandler; import mineplex.core.packethandler.IPacketHandler;
import mineplex.core.packethandler.PacketInfo; import mineplex.core.packethandler.PacketInfo;
import mineplex.core.recharge.Recharge; import mineplex.core.recharge.Recharge;
@ -103,6 +109,8 @@ public class MineKart extends SoloGame implements IPacketHandler
.addLore("Click to reset your kart", "back on the track.") .addLore("Click to reset your kart", "back on the track.")
.build(); .build();
private final LoopedNotePlayer _notePlayer;
public MineKart(NanoManager manager) public MineKart(NanoManager manager)
{ {
super(manager, GameType.MINEKART, new String[] super(manager, GameType.MINEKART, new String[]
@ -115,6 +123,33 @@ public class MineKart extends SoloGame implements IPacketHandler
_karts = new HashMap<>(); _karts = new HashMap<>();
_positions = new ArrayList<>(); _positions = new ArrayList<>();
NoteSong song;
try
{
song = NBSReader.loadSong(".." + File.separator + ".." + File.separator + "update" + File.separator + "songs" + File.separator + "minekart.nbs");
}
catch (FileNotFoundException e)
{
song = null;
e.printStackTrace();
}
if (song != null)
{
_notePlayer = new LoopedNotePlayer(getLifetime(), song, player ->
{
Kart kart = _karts.get(player);
return kart != null && kart.getLap() < LAPS;
});
getLifetime().register(_notePlayer, Collections.singleton(GameState.Live));
}
else
{
_notePlayer = null;
}
_teamComponent.setAdjustSpawnYaw(false); _teamComponent.setAdjustSpawnYaw(false);
_damageComponent.setDamage(false); _damageComponent.setDamage(false);
@ -369,6 +404,11 @@ public class MineKart extends SoloGame implements IPacketHandler
boolean finalLap = lap == LAPS; boolean finalLap = lap == LAPS;
if (finalLap && _notePlayer != null)
{
_notePlayer.cloneForPlayer(player, 0.75F);
}
UtilTextMiddle.display(C.cYellowB + "Lap " + lap, finalLap ? C.cAquaB + "FINAL LAP" : "", 10, 40, 10, player); UtilTextMiddle.display(C.cYellowB + "Lap " + lap, finalLap ? C.cAquaB + "FINAL LAP" : "", 10, 40, 10, player);
player.sendMessage(F.main(getManager().getName(), "Lap " + F.count(lap) + (finalLap ? F.color(" FINAL LAP", C.cAquaB) : "") + "!")); player.sendMessage(F.main(getManager().getName(), "Lap " + F.count(lap) + (finalLap ? F.color(" FINAL LAP", C.cAquaB) : "") + "!"));
} }

View File

@ -90,7 +90,7 @@ public class ChallengePlayMusic extends Challenge
int tick = _songTick.getOrDefault(player, 0); int tick = _songTick.getOrDefault(player, 0);
_songTick.put(player, tick + 1); _songTick.put(player, tick + 1);
_song.getLayers().forEach(layer -> _song.getLayerMap().values().forEach(layer ->
{ {
Note note = layer.getNote(tick); Note note = layer.getNote(tick);

View File

@ -6,7 +6,6 @@ import org.bukkit.entity.Player;
import mineplex.core.achievement.Achievement; import mineplex.core.achievement.Achievement;
import mineplex.core.achievement.AchievementData; import mineplex.core.achievement.AchievementData;
import mineplex.core.achievement.AchievementLog;
import mineplex.core.achievement.AchievementManager; import mineplex.core.achievement.AchievementManager;
import mineplex.core.common.currency.GlobalCurrency; import mineplex.core.common.currency.GlobalCurrency;
import mineplex.core.common.jsonchat.HoverEvent; import mineplex.core.common.jsonchat.HoverEvent;
@ -43,10 +42,8 @@ public class AchievementSummaryComponent extends ComplexSummaryComponent
AtomicInteger progressFor = new AtomicInteger(); AtomicInteger progressFor = new AtomicInteger();
StringBuilder hoverTextBuilder = new StringBuilder(100); StringBuilder hoverTextBuilder = new StringBuilder(100);
_achievementManager.getLog(player).entrySet().forEach(entry -> _achievementManager.getLog(player).forEach((achievement, log) ->
{ {
Achievement achievement = entry.getKey();
AchievementLog log = entry.getValue();
AchievementData data = _achievementManager.get(player, achievement); AchievementData data = _achievementManager.get(player, achievement);
if (log.LevelUp) if (log.LevelUp)