diff --git a/src/main/java/de/fanta/challenges/challenges/RandomEffectChallenge.java b/src/main/java/de/fanta/challenges/challenges/RandomEffectChallenge.java index f7b5ba5..ce4ea41 100644 --- a/src/main/java/de/fanta/challenges/challenges/RandomEffectChallenge.java +++ b/src/main/java/de/fanta/challenges/challenges/RandomEffectChallenge.java @@ -17,6 +17,7 @@ public class RandomEffectChallenge implements Listener { private final Challenges plugin = Challenges.getPlugin(); + private final Random random = new Random(); private final HashMap chunkPotionEffectMap = new HashMap<>(); private final HashMap playerPotionEffectMap = new HashMap<>(); @@ -25,19 +26,25 @@ public void onPlayerMove(PlayerMoveEvent e) { Player p = e.getPlayer(); if (Config.getBoolean("randomeffect") && plugin.getTimer().isRunning() && !plugin.getVanish().isVanish(p)) { - long chunkkey = p.getLocation().getChunk().getChunkKey(); - Random random = new Random(); - PotionEffectType[] values = Registry.EFFECT.stream().filter(effect -> (!effect.equals(PotionEffectType.INSTANT_DAMAGE))).toArray(PotionEffectType[]::new); - PotionEffect randomEffect = new PotionEffect(values[random.nextInt((int) Arrays.stream(values).count())], 999999, random.nextInt(5)); - if (!chunkPotionEffectMap.containsKey(chunkkey)) { - chunkPotionEffectMap.put(chunkkey, randomEffect); + long chunkKey = p.getLocation().getChunk().getChunkKey(); + + if (!chunkPotionEffectMap.containsKey(chunkKey)) { + chunkPotionEffectMap.put(chunkKey, randomEffect()); } - if (chunkPotionEffectMap.get(chunkkey) != playerPotionEffectMap.get(p)) { + if (chunkPotionEffectMap.get(chunkKey) != playerPotionEffectMap.get(p)) { if (playerPotionEffectMap.get(p) != null) { p.removePotionEffect(playerPotionEffectMap.get(p).getType()); } - p.addPotionEffect(chunkPotionEffectMap.get(chunkkey)); + PotionEffect potionEffect = chunkPotionEffectMap.get(chunkKey); + p.addPotionEffect(potionEffect); + playerPotionEffectMap.put(p, potionEffect); } } } + + + private PotionEffect randomEffect() { + PotionEffectType[] values = Registry.EFFECT.stream().filter(effect -> (!effect.equals(PotionEffectType.INSTANT_DAMAGE))).toArray(PotionEffectType[]::new); + return new PotionEffect(values[random.nextInt((int) Arrays.stream(values).count())], PotionEffect.INFINITE_DURATION, random.nextInt(5)); + } }