Skip to content

Commit

Permalink
Save waypoints
Browse files Browse the repository at this point in the history
  • Loading branch information
WalshyDev committed Dec 17, 2023
1 parent 5af81f1 commit fc7bc93
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 94 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
/.settings/
/.idea/
/.vscode/
/data-store/

dependency-reduced-pom.xml

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ public void addWaypoint(@Nonnull Player p, @Nonnull String name, @Nonnull Locati
}
}

profile.addWaypoint(new Waypoint(profile, id, event.getLocation(), event.getName()));
profile.addWaypoint(new Waypoint(p.getUniqueId(), id, event.getLocation(), event.getName()));

SoundEffect.GPS_NETWORK_ADD_WAYPOINT.playFor(p);
Slimefun.getLocalization().sendMessage(p, "gps.waypoint.added", true);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package io.github.thebusybiscuit.slimefun4.api.gps;

import java.util.Objects;
import java.util.UUID;

import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;

import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World.Environment;
import org.bukkit.entity.Player;
Expand All @@ -30,7 +32,7 @@
*/
public class Waypoint {

private final PlayerProfile profile;
private final UUID uuid;
private final String id;
private final String name;
private final Location location;
Expand All @@ -48,26 +50,40 @@ public class Waypoint {
* The name of this {@link Waypoint}
*/
@ParametersAreNonnullByDefault
public Waypoint(PlayerProfile profile, String id, Location loc, String name) {
Validate.notNull(profile, "Profile must never be null!");
public Waypoint(UUID uuid, String id, Location loc, String name) {
Validate.notNull(uuid, "UUID must never be null!");
Validate.notNull(id, "id must never be null!");
Validate.notNull(loc, "Location must never be null!");
Validate.notNull(name, "Name must never be null!");

this.profile = profile;
this.uuid = uuid;
this.id = id;
this.location = loc;
this.name = name;
}

/**
* This returns the owner of the {@link Waypoint}.
* This returns the owner {@link UUID} of the {@link Waypoint}.
*
* @return The corresponding owner {@link UUID}
*/
@Nonnull
public UUID getUuid() {
return this.uuid;
}

/**
* This returns the owner of the {@link Waypoint}.
*
* @return The corresponding {@link PlayerProfile}
*
* @deprecated Use {@link #getUuid()} instead
*/
@Nonnull
@Deprecated
public PlayerProfile getOwner() {
return profile;
// This is jank and should never actually return null
return PlayerProfile.find(Bukkit.getOfflinePlayer(uuid)).orElse(null);
}

/**
Expand Down Expand Up @@ -126,7 +142,7 @@ public ItemStack getIcon() {
*/
@Override
public int hashCode() {
return Objects.hash(profile.getUUID(), id, name, location);
return Objects.hash(this.uuid, this.id, this.name, this.location);
}

/**
Expand All @@ -139,7 +155,9 @@ public boolean equals(Object obj) {
}

Waypoint waypoint = (Waypoint) obj;
return profile.getUUID().equals(waypoint.getOwner().getUUID()) && id.equals(waypoint.getId()) && location.equals(waypoint.getLocation()) && name.equals(waypoint.getName());
return this.uuid.equals(waypoint.getUuid())
&& id.equals(waypoint.getId())
&& location.equals(waypoint.getLocation())
&& name.equals(waypoint.getName());
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.github.thebusybiscuit.slimefun4.api.player;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
Expand All @@ -11,8 +10,6 @@
import java.util.Set;
import java.util.UUID;
import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import javax.annotation.Nonnull;
Expand All @@ -22,14 +19,14 @@
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.NamespacedKey;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;

import io.github.bakedlibs.dough.common.ChatColors;
import io.github.bakedlibs.dough.common.CommonPatterns;
Expand Down Expand Up @@ -68,7 +65,6 @@ public class PlayerProfile {
private boolean dirty = false;
private boolean markedForDeletion = false;

private final List<Waypoint> waypoints = new ArrayList<>();
private final Map<Integer, PlayerBackpack> backpacks = new HashMap<>();
private final GuideHistory guideHistory = new GuideHistory(this);

Expand All @@ -83,22 +79,6 @@ protected PlayerProfile(@Nonnull OfflinePlayer p, PlayerData data) {

configFile = new Config("data-storage/Slimefun/Players/" + uuid.toString() + ".yml");
waypointsFile = new Config("data-storage/Slimefun/waypoints/" + uuid.toString() + ".yml");

loadProfileData();
}

private void loadProfileData() {
for (String key : waypointsFile.getKeys()) {
try {
if (waypointsFile.contains(key + ".world") && Bukkit.getWorld(waypointsFile.getString(key + ".world")) != null) {
String waypointName = waypointsFile.getString(key + ".name");
Location loc = waypointsFile.getLocation(key);
waypoints.add(new Waypoint(this, key, loc, waypointName));
}
} catch (Exception x) {
Slimefun.logger().log(Level.WARNING, x, () -> "Could not load Waypoint \"" + key + "\" for Player \"" + name + '"');
}
}
}

/**
Expand Down Expand Up @@ -176,9 +156,9 @@ public void setResearched(@Nonnull Research research, boolean unlock) {
dirty = true;

if (unlock) {
data.getResearches().add(research.getKey());
data.addResearch(research);
} else {
data.getResearches().remove(research.getKey());
data.removeResearch(research);
}
}

Expand All @@ -196,7 +176,7 @@ public boolean hasUnlocked(@Nullable Research research) {
return true;
}

return !research.isEnabled() || data.getResearches().contains(research.getKey());
return !research.isEnabled() || data.getResearches().contains(research);
}

/**
Expand All @@ -222,10 +202,7 @@ public boolean hasUnlockedEverything() {
* @return A {@code Hashset<Research>} of all Researches this {@link Player} has unlocked
*/
public @Nonnull Set<Research> getResearches() {
return Slimefun.getRegistry().getResearches()
.stream()
.filter((research) -> data.getResearches().contains(research.getKey()))
.collect(Collectors.toUnmodifiableSet());
return ImmutableSet.copyOf(this.data.getResearches());
}

/**
Expand All @@ -235,7 +212,7 @@ public boolean hasUnlockedEverything() {
* @return A {@link List} containing every {@link Waypoint}
*/
public @Nonnull List<Waypoint> getWaypoints() {
return ImmutableList.copyOf(waypoints);
return ImmutableList.copyOf(this.data.getWaypoints());
}

/**
Expand All @@ -246,21 +223,7 @@ public boolean hasUnlockedEverything() {
* The {@link Waypoint} to add
*/
public void addWaypoint(@Nonnull Waypoint waypoint) {
Validate.notNull(waypoint, "Cannot add a 'null' waypoint!");

for (Waypoint wp : waypoints) {
if (wp.getId().equals(waypoint.getId())) {
throw new IllegalArgumentException("A Waypoint with that id already exists for this Player");
}
}

if (waypoints.size() < 21) {
waypoints.add(waypoint);

waypointsFile.setValue(waypoint.getId(), waypoint.getLocation());
waypointsFile.setValue(waypoint.getId() + ".name", waypoint.getName());
markDirty();
}
this.data.addWaypoint(waypoint);
}

/**
Expand All @@ -271,12 +234,7 @@ public void addWaypoint(@Nonnull Waypoint waypoint) {
* The {@link Waypoint} to remove
*/
public void removeWaypoint(@Nonnull Waypoint waypoint) {
Validate.notNull(waypoint, "Cannot remove a 'null' waypoint!");

if (waypoints.remove(waypoint)) {
waypointsFile.setValue(waypoint.getId(), null);
markDirty();
}
this.data.removeWaypoint(waypoint);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package io.github.thebusybiscuit.slimefun4.storage.backend.legacy;

import io.github.bakedlibs.dough.config.Config;
import io.github.thebusybiscuit.slimefun4.api.gps.Waypoint;
import io.github.thebusybiscuit.slimefun4.api.researches.Research;
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;
import io.github.thebusybiscuit.slimefun4.storage.Storage;
import io.github.thebusybiscuit.slimefun4.storage.data.PlayerData;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.NamespacedKey;

import com.google.common.annotations.Beta;
Expand All @@ -14,6 +18,7 @@
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.logging.Level;

@Beta
public class LegacyStorage implements Storage {
Expand All @@ -22,36 +27,57 @@ public class LegacyStorage implements Storage {
public PlayerData loadPlayerData(@Nonnull UUID uuid) {
Config playerFile = new Config("data-storage/Slimefun/Players/" + uuid + ".yml");
// Not too sure why this is it's own file
Config waypointFile = new Config("data-storage/Slimefun/waypoints/" + uuid + ".yml");

Set<NamespacedKey> researches = new HashSet<>();
Config waypointsFile = new Config("data-storage/Slimefun/waypoints/" + uuid + ".yml");

// Load research
Set<Research> researches = new HashSet<>();
for (Research research : Slimefun.getRegistry().getResearches()) {
if (playerFile.contains("researches." + research.getID())) {
researches.add(research.getKey());
researches.add(research);
}
}

// Load waypoints
Set<Waypoint> waypoints = new HashSet<>();
for (String key : waypointsFile.getKeys()) {
try {
if (waypointsFile.contains(key + ".world") && Bukkit.getWorld(waypointsFile.getString(key + ".world")) != null) {
String waypointName = waypointsFile.getString(key + ".name");
Location loc = waypointsFile.getLocation(key);
waypoints.add(new Waypoint(uuid, key, loc, waypointName));
}
} catch (Exception x) {
Slimefun.logger().log(Level.WARNING, x, () -> "Could not load Waypoint \"" + key + "\" for Player \"" + uuid + '"');
}
}

// TODO:
// * Backpacks
// * Waypoints?

return new PlayerData(researches);
return new PlayerData(researches, waypoints);
}

@Override
public void savePlayerData(@Nonnull UUID uuid, @Nonnull PlayerData data) {
Config file = new Config("data-storage/Slimefun/Players/" + uuid + ".yml");
Config playerFile = new Config("data-storage/Slimefun/Players/" + uuid + ".yml");
// Not too sure why this is it's own file
Config waypointsFile = new Config("data-storage/Slimefun/waypoints/" + uuid + ".yml");

for (NamespacedKey key : data.getResearches()) {
// Legacy data uses IDs, we'll look these up
Optional<Research> research = Slimefun.getRegistry().getResearches().stream()
.filter((rs) -> key == rs.getKey())
.findFirst();
// Save research
for (Research research : data.getResearches()) {
// Legacy data uses IDs
playerFile.setValue("researches." + research.getID(), true);
}

research.ifPresent(value -> file.setValue("researches." + value.getID(), true));
// Save waypoints
for (Waypoint waypoint : data.getWaypoints()) {
// Legacy data uses IDs
waypointsFile.setValue(waypoint.getId(), waypoint.getLocation());
waypointsFile.setValue(waypoint.getId() + ".name", waypoint.getName());
}

file.save();
// Save files
playerFile.save();
waypointsFile.save();
}
}
Loading

0 comments on commit fc7bc93

Please sign in to comment.