Refactor anchor manager with varv and fix checkstyles

This commit is contained in:
Ben Woo 2023-09-21 17:32:47 +08:00
parent 65288f4037
commit 913ceb3006
No known key found for this signature in database
GPG Key ID: FB2A3645536E12C8
2 changed files with 112 additions and 97 deletions

View File

@ -368,7 +368,7 @@ public class MultiverseCore extends JavaPlugin implements MVCore {
// TODO: Make this all Try<Void>
return configProvider.get().save().isSuccess()
&& worldManagerProvider.get().saveWorldsConfig()
&& anchorManagerProvider.get().saveAnchors();
&& anchorManagerProvider.get().saveAnchors().isSuccess();
}
/**

View File

@ -16,126 +16,126 @@ import java.util.Map;
import java.util.Set;
import com.dumptruckman.minecraft.util.Logging;
import io.vavr.control.Try;
import jakarta.inject.Inject;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jvnet.hk2.annotations.Service;
import org.mvplugins.multiverse.core.MultiverseCore;
import org.mvplugins.multiverse.core.api.LocationManipulation;
import org.mvplugins.multiverse.core.config.MVCoreConfig;
import org.mvplugins.multiverse.core.world.WorldManager;
import org.mvplugins.multiverse.core.world.entrycheck.WorldEntryCheckerProvider;
/**
* Manages anchors.
*/
@Service
public class AnchorManager {
private Map<String, Location> anchors;
private FileConfiguration anchorConfig;
private static final String ANCHORS_SECTION_NAME = "anchors";
private static final String ANCHORS_FILE_NAME = "anchors.yml";
private final Plugin plugin;
private final Map<String, Location> anchors;
private final File anchorsFile;
private final FileConfiguration anchorConfig;
private final LocationManipulation locationManipulation;
private final MVCoreConfig config;
private final WorldManager worldManager;
private final WorldEntryCheckerProvider worldEntryCheckerProvider;
@Inject
public AnchorManager(
MultiverseCore plugin,
LocationManipulation locationManipulation,
MVCoreConfig config
) {
this.plugin = plugin;
AnchorManager(
@NotNull MultiverseCore plugin,
@NotNull LocationManipulation locationManipulation,
@NotNull WorldManager worldManager,
@NotNull WorldEntryCheckerProvider worldEntryCheckerProvider) {
this.anchors = new HashMap<>();
this.anchorsFile = new File(plugin.getDataFolder(), ANCHORS_FILE_NAME);
this.anchorConfig = new YamlConfiguration();
this.locationManipulation = locationManipulation;
this.config = config;
this.anchors = new HashMap<String, Location>();
this.worldManager = worldManager;
this.worldEntryCheckerProvider = worldEntryCheckerProvider;
}
/**
* Loads all anchors.
*
* @return Empty {@link Try} if all anchors were successfully loaded, or the exception if an error occurred.
*/
public void loadAnchors() {
this.anchors = new HashMap<String, Location>();
this.anchorConfig = YamlConfiguration.loadConfiguration(new File(this.plugin.getDataFolder(), "anchors.yml"));
this.ensureConfigIsPrepared();
ConfigurationSection anchorsSection = this.anchorConfig.getConfigurationSection("anchors");
Set<String> anchorKeys = anchorsSection.getKeys(false);
for (String key : anchorKeys) {
//world:x,y,z:pitch:yaw
Location anchorLocation = this.locationManipulation.stringToLocation(anchorsSection.getString(key, ""));
if (anchorLocation != null) {
Logging.config("Loading anchor: '%s'...", key);
public Try<Void> loadAnchors() {
return Try.run(() -> {
anchors.clear();
anchorConfig.load(anchorsFile);
ConfigurationSection anchorsSection = getAnchorConfigSection();
Set<String> anchorKeys = anchorsSection.getKeys(false);
anchorKeys.forEach(key -> {
//world:x,y,z:pitch:yaw
String locationString = anchorsSection.getString(key, "");
Location anchorLocation = this.locationManipulation.stringToLocation(locationString);
if (anchorLocation == null) {
Logging.warning("The location for anchor '%s' is INVALID.", key);
return;
}
this.anchors.put(key, anchorLocation);
} else {
Logging.warning("The location for anchor '%s' is INVALID.", key);
}
}
}
private void ensureConfigIsPrepared() {
if (this.anchorConfig.getConfigurationSection("anchors") == null) {
this.anchorConfig.createSection("anchors");
}
});
});
}
/**
* Saves all anchors.
*
* @return True if all anchors were successfully saved.
*/
public boolean saveAnchors() {
try {
this.anchorConfig.save(new File(this.plugin.getDataFolder(), "anchors.yml"));
return true;
} catch (IOException e) {
Logging.severe("Failed to save anchors.yml. Please check your file permissions.");
return false;
}
public Try<Void> saveAnchors() {
return Try.run(() -> anchorConfig.save(anchorsFile));
}
/**
* Gets the {@link Location} associated with an anchor.
* @param anchor The name of the anchor.
*
* @param anchorName The name of the anchor.
* @return The {@link Location}.
*/
public Location getAnchorLocation(String anchor) {
if (this.anchors.containsKey(anchor)) {
return this.anchors.get(anchor);
}
return null;
public Location getAnchorLocation(@Nullable String anchorName) {
return this.anchors.getOrDefault(anchorName, null);
}
/**
* Saves an anchor.
* @param anchor The name of the anchor.
* @param location The location of the anchor as string.
* @return True if the anchor was successfully saved.
*
* @param anchorName The name of the anchor.
* @param locationString The location of the anchor as string.
* @return Empty {@link Try} if all anchors were successfully loaded, or the exception if an error occurred.
*/
public boolean saveAnchorLocation(String anchor, String location) {
Location parsed = this.locationManipulation.stringToLocation(location);
return parsed != null && this.saveAnchorLocation(anchor, parsed);
public Try<Location> saveAnchorLocation(String anchorName, String locationString) {
Location parsed = this.locationManipulation.stringToLocation(locationString);
if (parsed == null) {
return Try.failure(new IOException("Invalid location string: " + locationString));
}
return this.saveAnchorLocation(anchorName, parsed).map(ignore -> getAnchorLocation(anchorName));
}
/**
* Saves an anchor.
* @param anchor The name of the anchor.
* @param l The {@link Location} of the anchor.
* @return True if the anchor was successfully saved.
*
* @param anchorName The name of the anchor.
* @param location The {@link Location} of the anchor.
* @return Empty {@link Try} if all anchors were successfully loaded, or the exception if an error occurred.
*/
public boolean saveAnchorLocation(String anchor, Location l) {
if (l == null) {
return false;
}
this.anchorConfig.set("anchors." + anchor, this.locationManipulation.locationToString(l));
this.anchors.put(anchor, l);
return this.saveAnchors();
public Try<Void> saveAnchorLocation(@NotNull String anchorName, @NotNull Location location) {
getAnchorConfigSection().set(anchorName, this.locationManipulation.locationToString(location));
this.anchors.put(anchorName, location);
return saveAnchors();
}
/**
* Gets all anchors.
*
* @return An unmodifiable {@link Set} containing all anchors.
*/
public Set<String> getAllAnchors() {
@ -144,46 +144,61 @@ public class AnchorManager {
/**
* Gets all anchors that the specified {@link Player} can access.
* @param p The {@link Player}.
*
* @param player The {@link Player}.
* @return An unmodifiable {@link Set} containing all anchors the specified {@link Player} can access.
*/
public Set<String> getAnchors(Player p) {
if (p == null) {
return this.anchors.keySet();
public Set<String> getAnchors(@Nullable Player player) {
if (player == null) {
return getAllAnchors();
}
Set<String> myAnchors = new HashSet<String>();
for (String anchor : this.anchors.keySet()) {
Location ancLoc = this.anchors.get(anchor);
if (ancLoc == null) {
continue;
Set<String> myAnchors = new HashSet<>();
this.anchors.forEach((anchorName, anchorLocation) -> {
World anchorWorld = anchorLocation.getWorld();
if (anchorWorld == null) {
return;
}
String worldPerm = "multiverse.access." + ancLoc.getWorld().getName();
// Add to the list if we're not enforcing access
// OR
// We are enforcing access and the user has the permission.
if (!config.getEnforceAccess() ||
(config.getEnforceAccess() && p.hasPermission(worldPerm))) {
myAnchors.add(anchor);
} else {
Logging.finer(String.format("Not adding anchor %s to the list, user %s doesn't have the %s " +
"permission and 'enforceaccess' is enabled!",
anchor, p.getName(), worldPerm));
}
}
return Collections.unmodifiableSet(myAnchors);
worldManager.getWorld(anchorWorld)
.map(world -> worldEntryCheckerProvider.forSender(player).canAccessWorld(world).isSuccess())
.peek(success -> {
if (success) {
myAnchors.add(anchorName);
} else {
Logging.finer("Player '%s' cannot access anchor '%s'.",
player.getName(), anchorName);
}
})
.onEmpty(() -> {
Logging.finer("Anchor '%s' located in world '%s is not in a Multiverse world.",
anchorName, anchorLocation.getWorld().getName());
});
});
return myAnchors;
}
/**
* Deletes the specified anchor.
* @param s The name of the anchor.
*
* @param anchorName The name of the anchor.
* @return True if the anchor was successfully deleted.
*/
public boolean deleteAnchor(String s) {
if (this.anchors.containsKey(s)) {
this.anchors.remove(s);
this.anchorConfig.set("anchors." + s, null);
public Try<Void> deleteAnchor(String anchorName) {
if (this.anchors.containsKey(anchorName)) {
this.anchors.remove(anchorName);
getAnchorConfigSection().set(anchorName, null);
return this.saveAnchors();
}
return false;
return Try.failure(new Exception("Anchor " + anchorName + " not found."));
}
private ConfigurationSection getAnchorConfigSection() {
ConfigurationSection anchorsSection = anchorConfig.getConfigurationSection(ANCHORS_SECTION_NAME);
return anchorsSection == null
? anchorConfig.createSection(ANCHORS_SECTION_NAME)
: anchorsSection;
}
}