Heavily refactor PlotSquaredProtection.class

Moved code into private methods to make the Location#at call more readable
and to reduce duplicate code.

`PlotSquared.get()` is marked as `@NotNull` and is never null
if `PlotSquaredProtection#isEnabled` returns true.
That's why I removed the `null` check on the *API* with a check if PlotSquared is enabled.

I replaced the usage of Java StreamAPI with a simple for loop for better potential performance.
The loop is so simple and we don't know the plugins that might be using this class,
thus greater performance for a small loss on readability is probably worth it

+ Removed some unused imports
This commit is contained in:
Christian Koop 2022-12-30 16:11:15 +01:00
parent 3a95b13419
commit 7975f9088d
No known key found for this signature in database
GPG Key ID: 89A8181384E010A3

View File

@ -1,19 +1,16 @@
package com.songoda.core.hooks.protection;
import com.plotsquared.core.PlotAPI;
import com.plotsquared.core.PlotSquared;
import com.plotsquared.core.plot.Plot;
import com.plotsquared.core.plot.PlotArea;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
public class PlotSquaredProtection extends Protection {
PlotSquared plotSquared;
public PlotSquaredProtection(Plugin plugin) {
super(plugin);
plotSquared = PlotSquared.get();
}
@Override
@ -23,21 +20,51 @@ public class PlotSquaredProtection extends Protection {
@Override
public boolean isEnabled() {
return plotSquared != null;
return Bukkit.getPluginManager().isPluginEnabled("PlotSquared");
}
@Override
public boolean canPlace(Player player, Location location) {
return plotSquared.getPlotAreaManager().getApplicablePlotArea(com.plotsquared.core.location.Location.at(location.getWorld().getName(), (int) location.getX(), (int) location.getY(), (int) location.getZ())).getPlots().stream().anyMatch(p -> p.isAdded(player.getUniqueId()));
return isPlayerAddedAtPlotLocation(player, location);
}
@Override
public boolean canBreak(Player player, Location location) {
return plotSquared.getPlotAreaManager().getApplicablePlotArea(com.plotsquared.core.location.Location.at(location.getWorld().getName(), (int) location.getX(), (int) location.getY(), (int) location.getZ())).getPlots().stream().anyMatch(p -> p.isAdded(player.getUniqueId()));
return isPlayerAddedAtPlotLocation(player, location);
}
@Override
public boolean canInteract(Player player, Location location) {
return plotSquared.getPlotAreaManager().getApplicablePlotArea(com.plotsquared.core.location.Location.at(location.getWorld().getName(), (int) location.getX(), (int) location.getY(), (int) location.getZ())).getPlots().stream().anyMatch(p -> p.isAdded(player.getUniqueId()));
return isPlayerAddedAtPlotLocation(player, location);
}
private boolean isPlayerAddedAtPlotLocation(Player player, Location location) {
PlotArea plotArea = getApplicablePlotArea(location);
if (plotArea == null) {
return true;
}
for (Plot p : plotArea.getPlots()) {
if (p.isAdded(player.getUniqueId())) {
return true;
}
}
return false;
}
private PlotArea getApplicablePlotArea(Location location) {
return PlotSquared.get()
.getPlotAreaManager()
.getApplicablePlotArea(getPlotSquaredLocation(location));
}
private com.plotsquared.core.location.Location getPlotSquaredLocation(Location location) {
return com.plotsquared.core.location.Location.at(
location.getWorld().getName(),
(int) location.getX(),
(int) location.getY(),
(int) location.getZ()
);
}
}