Skip to content

Commit 231b2a1

Browse files
committed
feat: add new biome achievement type
1 parent 416fb30 commit 231b2a1

2 files changed

Lines changed: 142 additions & 0 deletions

File tree

Readme.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ RCAchievements ist das Achievement Plugin des [Raid-Craft](https://raid-craft.de
1212
* [Achievement Configuration](#achievement-configuration)
1313
* [Achievement Types](#achievement-types)
1414
* [none](#none)
15+
* [ART-Framework](#art-framework)
1516
* [block](#block)
1617
* [combined](#combined)
1718
* [location](#location)
1819
* [kill-entity](#kill-entity)
1920
* [craft](#craft)
2021
* [login](#login)
2122
* [statistic](#statistic)
23+
* [biomes](#biomes)
2224

2325
## Configuration
2426

@@ -196,3 +198,14 @@ A `entity` must be specified if the `statistic` is one of the following: `KILL_E
196198
| `entity` | | Only required if `statistic` is one of the following: `KILL_ENTITY`, `ENTITY_KILLED_BY` |
197199
| `prefix` | `Fortschritt:` | The prefix that is written before the statistic count. |
198200
| `suffix` | | A suffix that should be written after the statistic count in the overview, e.g. `<prefix> 10/100 <suffix>`. |
201+
202+
### biomes
203+
204+
Keeps track of biomes the player visited. Defaults to all biomes if none are listed.
205+
206+
| Config | Default | Description |
207+
| ------ | ------- | ----------- |
208+
| `count` | `size of biome list` | How many biomes from the list below must the player visit. |
209+
| `biomes` | `[]` | A list of biomes the player must visit. Defaults to all biomes in the game. |
210+
| `prefix` | `Fortschritt:` | The prefix that is written before the statistic count. |
211+
| `suffix` | `Biome besucht` | A suffix that should be written after the statistic count in the overview, e.g. `<prefix> 10/100 <suffix>`. |
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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

Comments
 (0)