-
-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
GH-835 Add permission-based access to warps #856
base: master
Are you sure you want to change the base?
Changes from 4 commits
fca57f8
65e7564
374fc89
f1a6ada
7eb3bfc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,10 +2,16 @@ | |
|
||
import org.bukkit.Location; | ||
|
||
import java.util.List; | ||
|
||
public interface Warp { | ||
|
||
Location getLocation(); | ||
|
||
String getName(); | ||
|
||
List<String> getPermissions(); | ||
|
||
void setPermissions(List<String> permissions); | ||
|
||
Comment on lines
+15
to
+16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Codebase verification Thread-Safety Issue: Concurrent Access to Warp Objects The Consider implementing synchronization or using thread-safe data structures to manage permissions safely and prevent race conditions. 🔗 Analysis chainLGTM: New method setPermissions() is well-defined, but consider thread-safety. The Consider adding Javadoc to describe the method's purpose and parameters, e.g.: /**
* Sets the list of permissions required to access this warp.
*
* @param permissions A list of permission strings to be associated with this warp.
*/
void setPermissions(List<String> permissions); Additionally, consider the thread-safety implications of this setter method. If this interface is implemented by mutable objects that might be accessed concurrently, you may want to ensure thread-safe operations or document any synchronization requirements. To verify the usage and potential concurrency issues, you can run the following script: This script will help identify potential concurrency issues and how Warp objects are being used throughout the codebase. 🏁 Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Description: Check for potential concurrency issues with Warp implementations
# Search for classes implementing the Warp interface
echo "Classes implementing Warp interface:"
rg --type java "class\s+\w+\s+implements\s+.*Warp"
# Search for synchronized methods or blocks in Warp implementations
echo "\nSynchronized methods or blocks in Warp implementations:"
rg --type java "class\s+\w+\s+implements\s+.*Warp" -A 20 | rg "synchronized"
# Search for concurrent access to Warp objects
echo "\nPotential concurrent access to Warp objects:"
rg --type java "getWarp|setWarp|createWarp|deleteWarp"
Length of output: 4222 |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,8 +11,14 @@ public interface WarpService { | |
|
||
void removeWarp(String warp); | ||
|
||
void addPermissions(String warp, String... permissions); | ||
|
||
void removePermission(String warp, String permission); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing return of all permissions |
||
boolean warpExists(String name); | ||
|
||
boolean doesWarpPermissionExist(String warp, String permission); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is permission needed here? We can just check it using isEmpty on permissions list in object |
||
|
||
Optional<Warp> findWarp(String name); | ||
|
||
Collection<String> getNamesOfWarps(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
package com.eternalcode.core.configuration.implementation; | ||
|
||
import com.eternalcode.commons.bukkit.position.Position; | ||
import com.eternalcode.core.configuration.ReloadableConfig; | ||
import com.eternalcode.core.injector.annotations.component.ConfigurationFile; | ||
import com.eternalcode.commons.bukkit.position.Position; | ||
import net.dzikoysk.cdn.entity.Description; | ||
import net.dzikoysk.cdn.entity.Exclude; | ||
import net.dzikoysk.cdn.source.Resource; | ||
|
@@ -21,7 +21,8 @@ public class LocationsConfiguration implements ReloadableConfig { | |
@Description("# This is spawn location, for your own safety, please don't touch it.") | ||
public Position spawn = EMPTY_POSITION; | ||
|
||
@Description("# These are warp locations, for your own safety, please don't touch it.") | ||
@Description("# Warps now are stored in warps.yml. This is deprecated.") | ||
@Deprecated | ||
Comment on lines
+24
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Codebase verification Issues Found: Deprecated
🔗 Analysis chainApproved: Warps field deprecated and description updated. The changes to the
These modifications align with the PR objectives of implementing permission-based access to warps. To ensure a smooth transition, please verify the following:
Run the following script to identify potential usages of the deprecated Please review the results and update the affected code as necessary. 🏁 Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Description: Find usages of the deprecated warps field
# Search for direct accesses to the warps field
echo "Searching for direct accesses to the warps field:"
rg --type java "\.warps\b" --glob '!LocationsConfiguration.java'
# Search for method calls that might be using the warps field
echo "Searching for potential method calls using the warps field:"
rg --type java "\b(get|set|add|remove|clear)Warp(s)?\b" --glob '!LocationsConfiguration.java'
Length of output: 4910 |
||
public Map<String, Position> warps = new HashMap<>(); | ||
Comment on lines
+24
to
26
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Codebase verification Action Required: Deprecated The deprecated
Please update these usages to utilize the new 🔗 Analysis chainApproved: Warps field deprecated and description updated The changes to the
These modifications align with the PR objectives of implementing permission-based access to warps. However, please ensure the following:
To verify the current usage of the deprecated Please review the results and update the affected code as necessary to use the new 🏁 Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Description: Find usages of the deprecated warps field
# Search for direct accesses to the warps field
echo "Searching for direct accesses to the warps field:"
rg --type java "\.warps\b" --glob '!LocationsConfiguration.java'
# Search for method calls that might be using the warps field
echo "Searching for potential method calls using the warps field:"
rg --type java "\b(get|set|add|remove|clear)Warp(s)?\b" --glob '!LocationsConfiguration.java'
Length of output: 4910 |
||
|
||
@Description("# This is jail location, for your own safety, please don't touch it.") | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.eternalcode.core.configuration.implementation; | ||
|
||
import com.eternalcode.core.configuration.ReloadableConfig; | ||
import com.eternalcode.core.feature.warp.WarpConfigEntry; | ||
import com.eternalcode.core.injector.annotations.component.ConfigurationFile; | ||
import net.dzikoysk.cdn.entity.Description; | ||
import net.dzikoysk.cdn.source.Resource; | ||
import net.dzikoysk.cdn.source.Source; | ||
|
||
import java.io.File; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
@ConfigurationFile | ||
public class WarpsConfiguration implements ReloadableConfig { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. More like WarpsLocation, configuration is in config |
||
|
||
@Description({"# Warps configuration", "# These are warp locations, for your own safety, please don't touch it."}) | ||
public Map<String, WarpConfigEntry> warps = new HashMap<>(); | ||
Comment on lines
+17
to
+18
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Improve encapsulation and naming of the While the field is appropriately typed and described, there are several improvements that can be made:
Here's a suggested refactor: @Description({"# Warps configuration", "# These are warp locations, for your own safety, please don't modify directly."})
private final Map<String, WarpConfigEntry> warpsLocation = new HashMap<>();
public Map<String, WarpConfigEntry> getWarpsLocation() {
return Collections.unmodifiableMap(warpsLocation);
}
public void setWarp(String key, WarpConfigEntry value) {
warpsLocation.put(key, value);
}
public void removeWarp(String key) {
warpsLocation.remove(key);
} This approach maintains functionality while providing better control over how the warps are accessed and modified. |
||
|
||
@Override | ||
public Resource resource(File folder) { | ||
return Source.of(folder, "warps.yml"); | ||
} | ||
|
||
} | ||
Comment on lines
+1
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Consider adding utility methods and validation logic. While the current implementation is functional, consider the following enhancements to improve the robustness and usability of the
Here are some suggested method signatures: public boolean isValidWarp(WarpConfigEntry entry) {
// Implement validation logic
}
public void mergeConfiguration(Map<String, WarpConfigEntry> newWarps) {
// Implement merge logic
}
public boolean hasWarp(String warpName) {
return warps.containsKey(warpName);
} These additions would enhance the functionality and make the class more robust and easier to use in other parts of the system. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.eternalcode.core.feature.warp; | ||
|
||
import com.eternalcode.commons.bukkit.position.Position; | ||
import net.dzikoysk.cdn.entity.Contextual; | ||
|
||
import java.util.List; | ||
|
||
@Contextual | ||
public class WarpConfigEntry { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the difference between |
||
public Position position; | ||
public List<String> permissions; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Consider using a Set for permissions and add validation The current implementation uses a
Consider the following improvements:
Here's an example of how you could implement these changes: import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
public class WarpConfigEntry {
private final Position position;
private final Set<String> permissions;
public WarpConfigEntry(Position position, List<String> permissions) {
this.position = position;
this.permissions = normalizePermissions(permissions);
}
private Set<String> normalizePermissions(List<String> permissions) {
if (permissions == null) {
throw new IllegalArgumentException("Permissions list cannot be null");
}
return permissions.stream()
.filter(p -> p != null && !p.trim().isEmpty())
.map(String::toLowerCase)
.collect(Collectors.toCollection(HashSet::new));
}
// ... getters and other methods
} This implementation ensures that the permissions are stored efficiently, normalized, and validated.
Comment on lines
+10
to
+11
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Enhance encapsulation and optimize data structures. The current implementation with public fields violates encapsulation principles and might lead to inconsistent object state. Consider the following improvements:
Example refactoring: private final Position position;
private final Set<String> permissions;
public Position getPosition() {
return position;
}
public Set<String> getPermissions() {
return Collections.unmodifiableSet(permissions);
} This refactoring improves encapsulation, prevents duplicate permissions, and ensures immutability of the object state. |
||
|
||
public WarpConfigEntry() { | ||
} | ||
|
||
public WarpConfigEntry(Position position, List<String> permissions) { | ||
this.position = position; | ||
this.permissions = permissions; | ||
} | ||
Comment on lines
+13
to
+19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Improve constructor implementation and consider removing the default constructor. The current constructor implementation has potential issues:
Consider the following improvements:
Example refactoring: public WarpConfigEntry(Position position, Set<String> permissions) {
this.position = Objects.requireNonNull(position, "Position cannot be null");
this.permissions = Collections.unmodifiableSet(new HashSet<>(
Objects.requireNonNull(permissions, "Permissions cannot be null")
));
} This refactoring ensures that the object is always in a valid state and prevents external modification of the permissions set. |
||
} | ||
Comment on lines
+8
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Consider enhancing encapsulation and adding documentation The
Here's an example of how you could refactor the class: /**
* Represents a configuration entry for a warp.
*/
@Contextual
public class WarpConfigEntry {
private final Position position;
private final List<String> permissions;
/**
* Constructs a new WarpConfigEntry.
*
* @param position The position of the warp.
* @param permissions The list of permissions required to access the warp.
*/
public WarpConfigEntry(Position position, List<String> permissions) {
this.position = position;
this.permissions = List.copyOf(permissions); // Create an immutable copy
}
public Position getPosition() {
return position;
}
public List<String> getPermissions() {
return List.copyOf(permissions); // Return an immutable copy
}
} This refactored version provides better encapsulation, immutability, and documentation.
Comment on lines
+1
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Codebase verification Clarify the responsibilities and naming conventions of Warp-related classes. The current structure shows potential naming inconsistencies and unclear relationships between
This will help in streamlining the warp system's architecture and making the design decisions more transparent. 🔗 Analysis chainClarify the purpose and relationship of WarpConfigEntry in the warp system. The purpose and usage of
This information will help in understanding the design decisions and potentially identifying opportunities for system-wide improvements. To help clarify the relationship between 🏁 Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Description: Find usages and related classes of WarpConfigEntry
echo "Searching for WarpConfigEntry usages:"
rg --type java -n "WarpConfigEntry"
echo "\nSearching for other Warp-related classes:"
rg --type java -l "class.*Warp"
echo "\nSearching for WarpImpl class definition:"
rg --type java -n "class WarpImpl"
Length of output: 4339 Script: #!/bin/bash
# Description: Find interactions between WarpImpl and WarpConfigEntry
echo "Searching for WarpImpl usages of WarpConfigEntry:"
rg --type java "WarpConfigEntry" eternalcore-core/src/main/java/com/eternalcode/core/feature/warp/WarpImpl.java
echo "\nSearching for methods in WarpConfigRepository that use WarpConfigEntry:"
rg --type java "WarpConfigEntry" eternalcore-core/src/main/java/com/eternalcode/core/feature/warp/WarpConfigRepository.java
Length of output: 1071 Script: #!/bin/bash
# Description: Retrieve the definition of the Warp interface
echo "Fetching Warp interface definition:"
rg --type java "interface Warp" eternalcore-core/src/main/java/com/eternalcode/core/feature/warp/
echo "\nFetching all implementations of the Warp interface:"
rg --type java "implements Warp" eternalcore-core/src/main/java/com/eternalcode/core/feature/warp/
echo "\nFinding usages of WarpImpl in the codebase:"
rg --type java "WarpImpl"
Length of output: 1883 |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,61 +1,126 @@ | ||||||||||||||||||||||||||
package com.eternalcode.core.feature.warp; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
import com.eternalcode.commons.bukkit.position.Position; | ||||||||||||||||||||||||||
import com.eternalcode.commons.bukkit.position.PositionAdapter; | ||||||||||||||||||||||||||
import com.eternalcode.core.configuration.ConfigurationManager; | ||||||||||||||||||||||||||
import com.eternalcode.core.configuration.implementation.LocationsConfiguration; | ||||||||||||||||||||||||||
import com.eternalcode.core.configuration.implementation.WarpsConfiguration; | ||||||||||||||||||||||||||
import com.eternalcode.core.injector.annotations.Inject; | ||||||||||||||||||||||||||
import com.eternalcode.core.injector.annotations.component.Service; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
import java.util.ArrayList; | ||||||||||||||||||||||||||
import java.util.HashMap; | ||||||||||||||||||||||||||
import java.util.List; | ||||||||||||||||||||||||||
import java.util.Map; | ||||||||||||||||||||||||||
import java.util.Optional; | ||||||||||||||||||||||||||
import java.util.concurrent.CompletableFuture; | ||||||||||||||||||||||||||
import java.util.function.Consumer; | ||||||||||||||||||||||||||
import java.util.stream.Collectors; | ||||||||||||||||||||||||||
import panda.std.Option; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
@Service | ||||||||||||||||||||||||||
P1otrulla marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||
class WarpConfigRepository implements WarpRepository { | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
private final LocationsConfiguration locationsConfiguration; | ||||||||||||||||||||||||||
private final WarpsConfiguration warpsConfiguration; | ||||||||||||||||||||||||||
private final ConfigurationManager configurationManager; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
@Inject | ||||||||||||||||||||||||||
WarpConfigRepository(ConfigurationManager configurationManager, LocationsConfiguration locationsConfiguration) { | ||||||||||||||||||||||||||
WarpConfigRepository( | ||||||||||||||||||||||||||
ConfigurationManager configurationManager, | ||||||||||||||||||||||||||
LocationsConfiguration locationsConfiguration, | ||||||||||||||||||||||||||
WarpsConfiguration warpsConfiguration | ||||||||||||||||||||||||||
) { | ||||||||||||||||||||||||||
this.locationsConfiguration = locationsConfiguration; | ||||||||||||||||||||||||||
this.configurationManager = configurationManager; | ||||||||||||||||||||||||||
this.warpsConfiguration = warpsConfiguration; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
this.migrateWarps(); | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
@Override | ||||||||||||||||||||||||||
public void addWarp(Warp warp) { | ||||||||||||||||||||||||||
this.edit(warps -> warps.put(warp.getName(), PositionAdapter.convert(warp.getLocation()))); | ||||||||||||||||||||||||||
WarpConfigEntry warpConfigEntry = new WarpConfigEntry(PositionAdapter.convert(warp.getLocation()), warp.getPermissions()); | ||||||||||||||||||||||||||
this.edit(warps -> warps.put( | ||||||||||||||||||||||||||
warp.getName(), | ||||||||||||||||||||||||||
warpConfigEntry | ||||||||||||||||||||||||||
)); | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
@Override | ||||||||||||||||||||||||||
public void removeWarp(String warp) { | ||||||||||||||||||||||||||
this.edit(warps -> warps.remove(warp)); | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
private void edit(Consumer<Map<String, Position>> editor) { | ||||||||||||||||||||||||||
HashMap<String, Position> warps = new HashMap<>(this.locationsConfiguration.warps); | ||||||||||||||||||||||||||
@Override | ||||||||||||||||||||||||||
public void addPermissions(String warp, String... permissions) { | ||||||||||||||||||||||||||
this.edit(warps -> { | ||||||||||||||||||||||||||
WarpConfigEntry warpConfigEntry = warps.get(warp); | ||||||||||||||||||||||||||
if (warpConfigEntry == null) { | ||||||||||||||||||||||||||
return; | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
List<String> newPermissions = new ArrayList<>(warpConfigEntry.permissions); | ||||||||||||||||||||||||||
newPermissions.addAll(List.of(permissions)); | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
warpConfigEntry.permissions = newPermissions; | ||||||||||||||||||||||||||
Comment on lines
+61
to
+64
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prevent possible In the Consider initializing the permissions list when it's if (warpConfigEntry == null) {
return;
}
+if (warpConfigEntry.permissions == null) {
+ warpConfigEntry.permissions = new ArrayList<>();
+}
List<String> newPermissions = new ArrayList<>(warpConfigEntry.permissions);
newPermissions.addAll(List.of(permissions));
warpConfigEntry.permissions = newPermissions; 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
Comment on lines
+53
to
+66
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Refactor Suggestion: Reduce Code Duplication in Permission Methods The You could implement a private method that handles permission updates, parameterized by the action to perform (add or remove): private void updatePermissions(String warp, Consumer<List<String>> permissionUpdater) {
this.edit(warps -> {
WarpConfigEntry warpConfigEntry = warps.get(warp);
if (warpConfigEntry == null) {
return;
}
List<String> newPermissions = new ArrayList<>(warpConfigEntry.permissions);
permissionUpdater.accept(newPermissions);
warpConfigEntry.permissions = newPermissions;
});
} Then, refactor @Override
public void addPermissions(String warp, String... permissions) {
this.updatePermissions(warp, perms -> perms.addAll(List.of(permissions)));
}
@Override
public void removePermission(String warp, String permission) {
this.updatePermissions(warp, perms -> perms.remove(permission));
} Also applies to: 68-82 |
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
@Override | ||||||||||||||||||||||||||
public void removePermission(String warp, String permission) { | ||||||||||||||||||||||||||
this.edit(warps -> { | ||||||||||||||||||||||||||
WarpConfigEntry warpConfigEntry = warps.get(warp); | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
if (warpConfigEntry == null) { | ||||||||||||||||||||||||||
return; | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
List<String> newPermissions = new ArrayList<>(warpConfigEntry.permissions); | ||||||||||||||||||||||||||
newPermissions.remove(permission); | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
warpConfigEntry.permissions = newPermissions; | ||||||||||||||||||||||||||
Comment on lines
+77
to
+80
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handle In the Initialize the permissions list if it's if (warpConfigEntry == null) {
return;
}
+if (warpConfigEntry.permissions == null) {
+ warpConfigEntry.permissions = new ArrayList<>();
+}
List<String> newPermissions = new ArrayList<>(warpConfigEntry.permissions);
newPermissions.remove(permission);
warpConfigEntry.permissions = newPermissions; 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
private void edit(Consumer<Map<String, WarpConfigEntry>> editor) { | ||||||||||||||||||||||||||
Map<String, WarpConfigEntry> warps = new HashMap<>(this.warpsConfiguration.warps); | ||||||||||||||||||||||||||
editor.accept(warps); | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
this.locationsConfiguration.warps = warps; | ||||||||||||||||||||||||||
this.configurationManager.save(this.locationsConfiguration); | ||||||||||||||||||||||||||
warps.forEach((key, value) -> System.out.println(key + ": " + value)); | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is it needed here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Replace Using Apply this change to use a logger: +import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+private static final Logger logger = LoggerFactory.getLogger(WarpConfigRepository.class);
...
-warps.forEach((key, value) -> System.out.println(key + ": " + value));
+warps.forEach((key, value) -> logger.debug("{}: {}", key, value));
|
||||||||||||||||||||||||||
this.warpsConfiguration.warps.putAll(warps); | ||||||||||||||||||||||||||
this.configurationManager.save(this.warpsConfiguration); | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
@Override | ||||||||||||||||||||||||||
public CompletableFuture<Optional<Warp>> getWarp(String name) { | ||||||||||||||||||||||||||
return CompletableFuture.completedFuture(Optional.of(this.locationsConfiguration.warps.get(name)) | ||||||||||||||||||||||||||
.map(location -> new WarpImpl(name, location))); | ||||||||||||||||||||||||||
return CompletableFuture.completedFuture(Optional.of(this.warpsConfiguration.warps.get(name)) | ||||||||||||||||||||||||||
.map(warpConfigEntry -> new WarpImpl(name, warpConfigEntry.position, warpConfigEntry.permissions))); | ||||||||||||||||||||||||||
Comment on lines
+95
to
+96
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prevent In the Here's the suggested fix: -return CompletableFuture.completedFuture(Optional.of(this.warpsConfiguration.warps.get(name))
+return CompletableFuture.completedFuture(Optional.ofNullable(this.warpsConfiguration.warps.get(name))
.map(warpConfigEntry -> new WarpImpl(name, warpConfigEntry.position, warpConfigEntry.permissions))); 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
@Override | ||||||||||||||||||||||||||
public CompletableFuture<List<Warp>> getWarps() { | ||||||||||||||||||||||||||
return CompletableFuture.completedFuture(this.locationsConfiguration.warps.entrySet().stream() | ||||||||||||||||||||||||||
.map(entry -> new WarpImpl(entry.getKey(), entry.getValue())) | ||||||||||||||||||||||||||
return CompletableFuture.completedFuture(this.warpsConfiguration.warps.entrySet().stream() | ||||||||||||||||||||||||||
.map(warpConfigEntry -> { | ||||||||||||||||||||||||||
WarpConfigEntry warpContextual = warpConfigEntry.getValue(); | ||||||||||||||||||||||||||
return new WarpImpl(warpConfigEntry.getKey(), warpContextual.position, warpContextual.permissions); | ||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||
.collect(Collectors.toList())); | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
private void migrateWarps() { | ||||||||||||||||||||||||||
if (this.locationsConfiguration.warps.isEmpty()) { | ||||||||||||||||||||||||||
return; | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
Comment on lines
+110
to
+112
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handle potential In the Add a null check before proceeding: if (this.locationsConfiguration.warps == null || this.locationsConfiguration.warps.isEmpty()) {
return;
} 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
this.edit(warps -> warps.putAll(this.locationsConfiguration.warps | ||||||||||||||||||||||||||
.entrySet() | ||||||||||||||||||||||||||
.stream() | ||||||||||||||||||||||||||
.collect(Collectors.toMap(Map.Entry::getKey, entry -> | ||||||||||||||||||||||||||
new WarpConfigEntry(entry.getValue(), new ArrayList<>())) | ||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||
)); | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
this.locationsConfiguration.warps.clear(); | ||||||||||||||||||||||||||
this.configurationManager.save(this.locationsConfiguration); | ||||||||||||||||||||||||||
this.configurationManager.save(this.warpsConfiguration); | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -4,14 +4,19 @@ | |||||||||||||||||||
import com.eternalcode.commons.bukkit.position.PositionAdapter; | ||||||||||||||||||||
import org.bukkit.Location; | ||||||||||||||||||||
|
||||||||||||||||||||
import java.util.Collections; | ||||||||||||||||||||
import java.util.List; | ||||||||||||||||||||
|
||||||||||||||||||||
class WarpImpl implements Warp { | ||||||||||||||||||||
|
||||||||||||||||||||
private final String name; | ||||||||||||||||||||
private final Position position; | ||||||||||||||||||||
private List<String> permissions; | ||||||||||||||||||||
|
||||||||||||||||||||
WarpImpl(String name, Position position) { | ||||||||||||||||||||
WarpImpl(String name, Position position, List<String> permissions) { | ||||||||||||||||||||
this.name = name; | ||||||||||||||||||||
this.position = position; | ||||||||||||||||||||
this.permissions = permissions; | ||||||||||||||||||||
Comment on lines
+16
to
+19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Consider using defensive copying for the permissions list. While the constructor correctly initializes the new Consider applying this change: WarpImpl(String name, Position position, List<String> permissions) {
this.name = name;
this.position = position;
- this.permissions = permissions;
+ this.permissions = new ArrayList<>(permissions);
} Don't forget to add the import for 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
@Override | ||||||||||||||||||||
|
@@ -23,4 +28,13 @@ public String getName() { | |||||||||||||||||||
public Location getLocation() { | ||||||||||||||||||||
return PositionAdapter.convert(this.position); | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
@Override | ||||||||||||||||||||
public List<String> getPermissions() { | ||||||||||||||||||||
return Collections.unmodifiableList(this.permissions); | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
public void setPermissions(List<String> permissions) { | ||||||||||||||||||||
this.permissions = permissions; | ||||||||||||||||||||
} | ||||||||||||||||||||
Comment on lines
+37
to
+39
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure the permissions list is non-null and make a defensive copy in the setter To prevent Apply this diff to enhance the method: public void setPermissions(List<String> permissions) {
+ if (permissions == null) {
+ throw new IllegalArgumentException("Permissions list cannot be null");
+ }
- this.permissions = permissions;
+ this.permissions = new ArrayList<>(permissions);
} Don't forget to import 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this method from public API