-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathHomeManager.java
More file actions
86 lines (75 loc) · 3.03 KB
/
Copy pathHomeManager.java
File metadata and controls
86 lines (75 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package dev.hytalemodding.hyssentials.manager;
import com.hypixel.hytale.math.vector.Rotation3f;
import org.joml.Vector3d;
import com.hypixel.hytale.server.core.universe.PlayerRef;
import com.hypixel.hytale.server.core.universe.world.World;
import dev.hytalemodding.hyssentials.data.LocationData;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
public class HomeManager {
private final DataManager dataManager;
private final RankManager rankManager;
private final Map<UUID, Map<String, LocationData>> playerHomes;
public HomeManager(@Nonnull DataManager dataManager, @Nonnull RankManager rankManager) {
this.dataManager = dataManager;
this.rankManager = rankManager;
this.playerHomes = dataManager.loadPlayerHomes();
}
public boolean setHome(@Nonnull UUID playerUuid, @Nonnull String name, @Nonnull World world,
@Nonnull Vector3d position, @Nonnull Rotation3f rotation, int effectiveMaxHomes) {
Map<String, LocationData> homes = playerHomes.computeIfAbsent(playerUuid, k -> new ConcurrentHashMap<>());
String lowerName = name.toLowerCase();
if (!homes.containsKey(lowerName) && homes.size() >= effectiveMaxHomes) {
return false;
}
homes.put(lowerName, LocationData.from(world.getName(), position, rotation));
dataManager.savePlayerHomes(playerHomes);
return true;
}
@Nullable
public LocationData getHome(@Nonnull UUID playerUuid, @Nonnull String name) {
Map<String, LocationData> homes = playerHomes.get(playerUuid);
if (homes == null) {
return null;
}
return homes.get(name.toLowerCase());
}
public boolean deleteHome(@Nonnull UUID playerUuid, @Nonnull String name) {
Map<String, LocationData> homes = playerHomes.get(playerUuid);
if (homes == null) {
return false;
}
LocationData removed = homes.remove(name.toLowerCase());
if (removed != null) {
dataManager.savePlayerHomes(playerHomes);
return true;
}
return false;
}
@Nonnull
public Set<String> getHomeNames(@Nonnull UUID playerUuid) {
Map<String, LocationData> homes = playerHomes.get(playerUuid);
if (homes == null) {
return Collections.emptySet();
}
return Collections.unmodifiableSet(homes.keySet());
}
public int getHomeCount(@Nonnull UUID playerUuid) {
Map<String, LocationData> homes = playerHomes.get(playerUuid);
return homes == null ? 0 : homes.size();
}
public int getMaxHomesForPlayer(@Nonnull PlayerRef player) {
return rankManager.getEffectiveMaxHomes(player);
}
public void save() {
dataManager.savePlayerHomes(playerHomes);
}
public int getTotalHomeCount() {
return playerHomes.values().stream().mapToInt(Map::size).sum();
}
}