|
| 1 | +package de.raidcraft.achievements.types; |
| 2 | + |
| 3 | +import de.raidcraft.achievements.AchievementContext; |
| 4 | +import de.raidcraft.achievements.TypeFactory; |
| 5 | +import de.raidcraft.achievements.util.LocationUtil; |
| 6 | +import lombok.extern.java.Log; |
| 7 | +import org.bukkit.Location; |
| 8 | +import org.bukkit.block.Biome; |
| 9 | +import org.bukkit.configuration.ConfigurationSection; |
| 10 | +import org.bukkit.entity.Player; |
| 11 | +import org.bukkit.event.EventHandler; |
| 12 | +import org.bukkit.event.Listener; |
| 13 | +import org.bukkit.event.player.PlayerMoveEvent; |
| 14 | + |
| 15 | +import java.util.*; |
| 16 | +import java.util.stream.Collectors; |
| 17 | + |
| 18 | +import static java.util.stream.Collectors.toMap; |
| 19 | +import static java.util.stream.Collectors.toSet; |
| 20 | + |
| 21 | +@Log(topic = "RCAchievements:biome") |
| 22 | +public class BiomeAchievement extends CountAchievement implements Listener { |
| 23 | + |
| 24 | + public static final String VISITED_BIOMES = "visited_biomes"; |
| 25 | + |
| 26 | + public static class Factory implements TypeFactory<BiomeAchievement> { |
| 27 | + |
| 28 | + @Override |
| 29 | + public String identifier() { |
| 30 | + |
| 31 | + return "biome"; |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + public Class<BiomeAchievement> typeClass() { |
| 36 | + |
| 37 | + return BiomeAchievement.class; |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public BiomeAchievement create(AchievementContext context) { |
| 42 | + |
| 43 | + return new BiomeAchievement(context); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + private final Set<Biome> biomeTypes = new HashSet<>(); |
| 48 | + private final Map<UUID, Location> lastLocations = new HashMap<>(); |
| 49 | + private final Map<UUID, Set<Biome>> playerVisitedBiomesMap = new HashMap<>(); |
| 50 | + |
| 51 | + protected BiomeAchievement(AchievementContext context) { |
| 52 | + |
| 53 | + super(context); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public boolean load(ConfigurationSection config) { |
| 58 | + |
| 59 | + biomeTypes.clear(); |
| 60 | + |
| 61 | + for (String biome : config.getStringList("biomes")) { |
| 62 | + try { |
| 63 | + biomeTypes.add(Biome.valueOf(biome.toUpperCase())); |
| 64 | + } catch (IllegalArgumentException e) { |
| 65 | + log.warning("invalid biome name " + biome + " in config of: " + achievement()); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + if (biomeTypes.isEmpty()) { |
| 70 | + biomeTypes.addAll(Arrays.asList(Biome.values())); |
| 71 | + } |
| 72 | + |
| 73 | + config.set("count", config.getInt("count", biomeTypes.size())); |
| 74 | + suffix(config.getString("suffix", "Biome besucht")); |
| 75 | + |
| 76 | + return super.load(config); |
| 77 | + } |
| 78 | + |
| 79 | + @SuppressWarnings("unchecked") |
| 80 | + @Override |
| 81 | + public void enable() { |
| 82 | + |
| 83 | + Map<UUID, Set<String>> entry = store().get(VISITED_BIOMES, Map.class, new HashMap<UUID, Set<String>>()); |
| 84 | + for (Map.Entry<UUID, Set<String>> playerEntry : entry.entrySet()) { |
| 85 | + playerVisitedBiomesMap.put(playerEntry.getKey(), playerEntry.getValue().stream() |
| 86 | + .map(Biome::valueOf) |
| 87 | + .collect(Collectors.toSet()) |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + super.enable(); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public void disable() { |
| 96 | + |
| 97 | + store().set(VISITED_BIOMES, playerVisitedBiomesMap.entrySet().stream() |
| 98 | + .collect(toMap(Map.Entry::getKey, entry -> entry.getValue().stream() |
| 99 | + .map(Enum::name) |
| 100 | + .collect(toSet()) |
| 101 | + ))).save(); |
| 102 | + |
| 103 | + super.disable(); |
| 104 | + } |
| 105 | + |
| 106 | + @EventHandler(ignoreCancelled = true) |
| 107 | + public void onPlayerMove(PlayerMoveEvent event) { |
| 108 | + |
| 109 | + if (notApplicable(event.getPlayer())) return; |
| 110 | + if (!moved(event.getPlayer())) return; |
| 111 | + Biome biome = event.getPlayer().getLocation().getBlock().getBiome(); |
| 112 | + if (!biomeTypes.contains(biome)) return; |
| 113 | + |
| 114 | + Set<Biome> visitedBiomes = playerVisitedBiomesMap |
| 115 | + .computeIfAbsent(event.getPlayer().getUniqueId(), uuid -> new HashSet<>()); |
| 116 | + if (visitedBiomes.contains(biome)) return; |
| 117 | + |
| 118 | + visitedBiomes.add(biome); |
| 119 | + playerVisitedBiomesMap.put(event.getPlayer().getUniqueId(), visitedBiomes); |
| 120 | + increaseAndCheck(player(event.getPlayer())); |
| 121 | + } |
| 122 | + |
| 123 | + private boolean moved(Player player) { |
| 124 | + |
| 125 | + Location lastLocation = lastLocations.getOrDefault(player.getUniqueId(), player.getLocation()); |
| 126 | + lastLocations.put(player.getUniqueId(), lastLocation); |
| 127 | + return !LocationUtil.isBlockEquals(lastLocation, player.getLocation()); |
| 128 | + } |
| 129 | +} |
0 commit comments