Skip to content

Commit

Permalink
Merge pull request #3159 from Multiverse/dtm/mv5/sonarqube-fixes
Browse files Browse the repository at this point in the history
Checkstyle/Sonarqube fixes.
  • Loading branch information
dumptruckman authored Jan 24, 2025
2 parents d7c1954 + 949c470 commit d3274cc
Show file tree
Hide file tree
Showing 35 changed files with 515 additions and 394 deletions.
2 changes: 1 addition & 1 deletion config/mv_checks.xml
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@
<property name="throwsIndent" value="8"/>
<property name="arrayInitIndent" value="4"/>
<property name="lineWrappingIndentation" value="8"/>
<property name="forceStrictCondition" value="true"/>
<property name="forceStrictCondition" value="false"/>
</module>
<module name="NoCodeInFile"/>
<module name="OuterTypeFilename"/>
Expand Down
125 changes: 75 additions & 50 deletions src/main/java/org/mvplugins/multiverse/core/anchor/AnchorManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,21 @@
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import com.dumptruckman.minecraft.util.Logging;
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;
Expand All @@ -34,6 +37,10 @@
*/
@Service
public final class AnchorManager {

private static final String ANCHORS_FILE = "anchors.yml";
private static final String ANCHORS_CONFIG_SECTION = "anchors";

private Map<String, Location> anchors;
private FileConfiguration anchorConfig;

Expand All @@ -42,53 +49,54 @@ public final class AnchorManager {
private final MVCoreConfig config;

@Inject
public AnchorManager(
AnchorManager(
MultiverseCore plugin,
LocationManipulation locationManipulation,
MVCoreConfig config
) {
MVCoreConfig config) {
this.plugin = plugin;
this.locationManipulation = locationManipulation;
this.config = config;

this.anchors = new HashMap<String, Location>();
anchors = new HashMap<>();
}

/**
* Loads all anchors.
*/
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");
anchors = new HashMap<>();
anchorConfig = YamlConfiguration.loadConfiguration(new File(plugin.getDataFolder(), ANCHORS_FILE));
var anchorsSection = getAnchorsConfigSection();
Set<String> anchorKeys = anchorsSection.getKeys(false);
for (String key : anchorKeys) {
//world:x,y,z:pitch:yaw
Location anchorLocation = this.locationManipulation.stringToLocation(anchorsSection.getString(key, ""));
Location anchorLocation = locationManipulation.stringToLocation(anchorsSection.getString(key, ""));
if (anchorLocation != null) {
Logging.config("Loading anchor: '%s'...", key);
this.anchors.put(key, anchorLocation);
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");
private ConfigurationSection getAnchorsConfigSection() {
var anchorsConfigSection = anchorConfig.getConfigurationSection(ANCHORS_CONFIG_SECTION);
if (anchorsConfigSection == null) {
anchorsConfigSection = anchorConfig.createSection(ANCHORS_CONFIG_SECTION);
}
return anchorsConfigSection;
}

/**
* 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"));
anchorConfig.save(new File(plugin.getDataFolder(), ANCHORS_FILE));
return true;
} catch (IOException e) {
Logging.severe("Failed to save anchors.yml. Please check your file permissions.");
Expand All @@ -98,29 +106,32 @@ public boolean saveAnchors() {

/**
* Gets the {@link Location} associated with an anchor.
*
* @param anchor The name of the anchor.
* @return The {@link Location}.
*/
public Location getAnchorLocation(String anchor) {
if (this.anchors.containsKey(anchor)) {
return this.anchors.get(anchor);
if (anchors.containsKey(anchor)) {
return anchors.get(anchor);
}
return 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.
*/
public boolean saveAnchorLocation(String anchor, String location) {
Location parsed = this.locationManipulation.stringToLocation(location);
return parsed != null && this.saveAnchorLocation(anchor, parsed);
Location parsed = locationManipulation.stringToLocation(location);
return saveAnchorLocation(anchor, parsed);
}

/**
* 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.
Expand All @@ -129,60 +140,74 @@ 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();
getAnchorsConfigSection().set(anchor, locationManipulation.locationToString(l));
anchors.put(anchor, l);
return saveAnchors();
}

/**
* Gets all anchors.
*
* @return An unmodifiable {@link Set} containing all anchors.
*/
public Set<String> getAllAnchors() {
return Collections.unmodifiableSet(this.anchors.keySet());
return Collections.unmodifiableSet(anchors.keySet());
}

/**
* 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(Player player) {
if (player == null) {
return anchors.keySet();
} else {
return getAnchorsForPlayer(player);
}
Set<String> myAnchors = new HashSet<String>();
for (String anchor : this.anchors.keySet()) {
Location ancLoc = this.anchors.get(anchor);
if (ancLoc == null) {
continue;
}
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));
}
}

private Set<String> getAnchorsForPlayer(@NotNull Player player) {
return anchors.entrySet().stream()
.filter(entry -> shouldIncludeAnchorForPlayer(entry.getKey(), entry.getValue(), player))
.map(Map.Entry::getKey)
.collect(Collectors.toSet());
}

private boolean shouldIncludeAnchorForPlayer(String anchor, Location location, Player player) {
var world = getLocationWorld(location);
return world != null && playerCanAccess(player, world, anchor);
}

private @Nullable World getLocationWorld(@Nullable Location location) {
if (location == null) {
return null;
}
return location.getWorld();
}

private boolean playerCanAccess(Player player, World world, String anchor) {
String worldPerm = "multiverse.access." + world.getName();
if (config.getEnforceAccess() && !player.hasPermission(worldPerm)) {
Logging.finer(String.format("Not adding anchor %s to the list, user %s doesn't have the %s permission "
+ "and 'enforceaccess' is enabled!", anchor, player.getName(), worldPerm));
return false;
}
return Collections.unmodifiableSet(myAnchors);
return true;
}

/**
* Deletes the specified anchor.
*
* @param s 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);
return this.saveAnchors();
if (anchors.containsKey(s)) {
anchors.remove(s);
getAnchorsConfigSection().set(s, null);
return saveAnchors();
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.mvplugins.multiverse.core.anchor.AnchorManager;
import org.mvplugins.multiverse.core.commandtools.MVCommandIssuer;
import org.mvplugins.multiverse.core.commandtools.MVCommandManager;
import org.mvplugins.multiverse.core.commandtools.MultiverseCommand;

@Service
@CommandAlias("mv")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ final class AnchorListCommand extends CoreCommand {
private final AnchorManager anchorManager;
private final LocationManipulation locationManipulation;

private final CommandValueFlag<Integer> PAGE_FLAG = flag(CommandValueFlag
private final CommandValueFlag<Integer> pageFlag = flag(CommandValueFlag
.builder("--page", Integer.class)
.addAlias("-p")
.context(value -> {
Expand All @@ -48,7 +48,7 @@ final class AnchorListCommand extends CoreCommand {
})
.build());

private final CommandValueFlag<ContentFilter> FILTER_FLAG = flag(CommandValueFlag
private final CommandValueFlag<ContentFilter> filterFlag = flag(CommandValueFlag
.builder("--filter", ContentFilter.class)
.addAlias("-f")
.context(value -> {
Expand Down Expand Up @@ -88,8 +88,8 @@ void onAnchorListCommand(
.withSendHandler(PagedSendHandler.create()
.withHeader("&3==== [ Multiverse Anchors ] ====")
.doPagination(true)
.withTargetPage(parsedFlags.flagValue(PAGE_FLAG, 1))
.withFilter(parsedFlags.flagValue(FILTER_FLAG, DefaultContentFilter.get())))
.withTargetPage(parsedFlags.flagValue(pageFlag, 1))
.withFilter(parsedFlags.flagValue(filterFlag, DefaultContentFilter.get())))
.send(issuer);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,9 @@ void onAnchorSetCommand(
String anchorName) {
Location anchorLocation = player.getLocation();
if (anchorManager.saveAnchorLocation(anchorName, anchorLocation)) {
issuer.sendMessage("&aAnchor &f" + anchorName + "&a set to &f"
+ locationManipulation.locationToString(anchorLocation));
sendAnchorSetSuccessMessage(issuer, anchorName, locationManipulation.locationToString(anchorLocation));
} else {
issuer.sendMessage("&cFailed to set anchor &f" + anchorName + ".");
sendAnchorSetFailedMessage(issuer, anchorName);
}
}

Expand All @@ -78,9 +77,17 @@ void onAnchorSetCommand(
@Description("")
String locationString) {
if (anchorManager.saveAnchorLocation(anchorName, locationString)) {
issuer.sendMessage("&aAnchor &f" + anchorName + "&a set to &f" + locationString);
sendAnchorSetSuccessMessage(issuer, anchorName, locationString);
} else {
issuer.sendMessage("&cFailed to set anchor &f" + anchorName + ".");
sendAnchorSetFailedMessage(issuer, anchorName);
}
}

private void sendAnchorSetSuccessMessage(MVCommandIssuer issuer, String anchorName, String locationString) {
issuer.sendMessage("&aAnchor &f" + anchorName + "&a set to &f" + locationString);
}

private void sendAnchorSetFailedMessage(MVCommandIssuer issuer, String anchorName) {
issuer.sendMessage("&cFailed to set anchor &f" + anchorName + ".");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
import org.mvplugins.multiverse.core.commandtools.MVCommandIssuer;
import org.mvplugins.multiverse.core.commandtools.MVCommandManager;
import org.mvplugins.multiverse.core.destination.DestinationInstance;
import org.mvplugins.multiverse.core.permissions.CorePermissionsChecker;
import org.mvplugins.multiverse.core.locale.MVCorei18n;
import org.mvplugins.multiverse.core.locale.message.Message;
import org.mvplugins.multiverse.core.locale.message.MessageReplacement.Replace;
import org.mvplugins.multiverse.core.permissions.CorePermissionsChecker;
import org.mvplugins.multiverse.core.teleportation.LocationManipulation;

import static org.mvplugins.multiverse.core.locale.message.MessageReplacement.replace;
Expand Down Expand Up @@ -56,8 +57,8 @@ void onCheckCommand(
issuer.sendInfo(this.corePermissionsChecker.checkTeleportPermissions(player, player, destination)
? MVCorei18n.CHECK_HASPERMISSION
: MVCorei18n.CHECK_NOPERMISSION,
replace("{player}").with(player.getName()),
replace("{destination}").with(destination));
Replace.PLAYER.with(player.getName()),
Replace.DESTINATION.with(destination));
issuer.sendInfo(MVCorei18n.CHECK_LOCATION,
replace("{location}").with(destination.getLocation(player)
.map(locationManipulation::locationToString)
Expand Down
Loading

0 comments on commit d3274cc

Please sign in to comment.