Skip to content

Commit

Permalink
Add method to get entities in claim
Browse files Browse the repository at this point in the history
  • Loading branch information
NahuLD committed Sep 26, 2023
1 parent d3bfbee commit 22a41dd
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 66 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.griefprevention</groupId>
<artifactId>GriefPrevention</artifactId>
<version>16.18.3-SNAPSHOT</version>
<version>16.18.4-SNAPSHOT</version>

<name>GriefPrevention</name>
<description>The official self-service anti-griefing Bukkit plugin for Minecraft servers since 2011.</description>
Expand Down
141 changes: 76 additions & 65 deletions src/main/java/me/ryanhamshire/GriefPrevention/Claim.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Arrays;
import java.util.Collection;
import java.util.function.Consumer;
import java.util.ArrayList;
import java.util.Calendar;
Expand All @@ -52,6 +54,7 @@
import java.util.Set;
import java.util.UUID;
import java.util.function.Supplier;
import java.util.stream.Collectors;

//represents a player claim
//creating an instance doesn't make an effective claim
Expand Down Expand Up @@ -327,7 +330,7 @@ public boolean isNear(Location location, int howNear)
* @return the denial message, or null if the action is allowed
*/
@Deprecated
public @Nullable String allowEdit(@NotNull Player player)
public @Nullable String allowEdit(@NotNull Player player)
{
Supplier<String> supplier = checkPermission(player, ClaimPermission.Edit, null);
return supplier != null ? supplier.get() : null;
Expand Down Expand Up @@ -358,7 +361,7 @@ private static boolean placeableForFarming(Material material)
*/
@Deprecated
//build permission check
public @Nullable String allowBuild(@NotNull Player player, @NotNull Material material)
public @Nullable String allowBuild(@NotNull Player player, @NotNull Material material)
{
Supplier<String> supplier = checkPermission(player, ClaimPermission.Build, new CompatBuildBreakEvent(material, false));
return supplier != null ? supplier.get() : null;
Expand Down Expand Up @@ -386,14 +389,14 @@ public boolean isBreak()
}

@Override
public @NotNull HandlerList getHandlers()
public @NotNull HandlerList getHandlers()
{
return new HandlerList();
}

}

public boolean hasExplicitPermission(@NotNull UUID uuid, @NotNull ClaimPermission level)
public boolean hasExplicitPermission(@NotNull UUID uuid, @NotNull ClaimPermission level)
{
if (uuid.equals(this.getOwnerID())) return true;

Expand All @@ -402,7 +405,7 @@ public boolean hasExplicitPermission(@NotNull UUID uuid, @NotNull ClaimPermissio
return level.isGrantedBy(this.playerIDToClaimPermissionMap.get(uuid.toString()));
}

public boolean hasExplicitPermission(@NotNull Player player, @NotNull ClaimPermission level)
public boolean hasExplicitPermission(@NotNull Player player, @NotNull ClaimPermission level)
{
// Check explicit ClaimPermission for UUID
if (this.hasExplicitPermission(player.getUniqueId(), level)) return true;
Expand Down Expand Up @@ -437,51 +440,51 @@ public boolean hasExplicitPermission(@NotNull Player player, @NotNull ClaimPermi
}

/**
* Check whether a Player has a certain level of trust.
* Check whether a Player has a certain level of trust.
*
* @param player the Player being checked for permissions
* @param permission the ClaimPermission level required
* @param event the Event triggering the permission check
* @return the denial message or null if permission is granted
*/
public @Nullable Supplier<String> checkPermission(
@NotNull Player player,
@NotNull ClaimPermission permission,
@Nullable Event event)
public @Nullable Supplier<String> checkPermission(
@NotNull Player player,
@NotNull ClaimPermission permission,
@Nullable Event event)
{
return checkPermission(player, permission, event, null);
}

/**
* Check whether a Player has a certain level of trust. For internal use; allows changing default message.
* Check whether a Player has a certain level of trust. For internal use; allows changing default message.
*
* @param player the Player being checked for permissions
* @param permission the ClaimPermission level required
* @param event the Event triggering the permission check
* @param denialOverride a message overriding the default denial for clarity
* @return the denial message or null if permission is granted
*/
@Nullable Supplier<String> checkPermission(
@NotNull Player player,
@NotNull ClaimPermission permission,
@Nullable Event event,
@Nullable Supplier<String> denialOverride)
@Nullable Supplier<String> checkPermission(
@NotNull Player player,
@NotNull ClaimPermission permission,
@Nullable Event event,
@Nullable Supplier<String> denialOverride)
{
return callPermissionCheck(new ClaimPermissionCheckEvent(player, this, permission, event), denialOverride);
}

/**
* Check whether a UUID has a certain level of trust.
* Check whether a UUID has a certain level of trust.
*
* @param uuid the UUID being checked for permissions
* @param permission the ClaimPermission level required
* @param event the Event triggering the permission check
* @return the denial reason or null if permission is granted
*/
public @Nullable Supplier<String> checkPermission(
@NotNull UUID uuid,
@NotNull ClaimPermission permission,
@Nullable Event event)
public @Nullable Supplier<String> checkPermission(
@NotNull UUID uuid,
@NotNull ClaimPermission permission,
@Nullable Event event)
{
return callPermissionCheck(new ClaimPermissionCheckEvent(uuid, this, permission, event), null);
}
Expand All @@ -493,9 +496,9 @@ public boolean hasExplicitPermission(@NotNull Player player, @NotNull ClaimPermi
* @param denialOverride a message overriding the default denial for clarity
* @return the denial reason or null if permission is granted
*/
private @Nullable Supplier<String> callPermissionCheck(
@NotNull ClaimPermissionCheckEvent event,
@Nullable Supplier<String> denialOverride)
private @Nullable Supplier<String> callPermissionCheck(
@NotNull ClaimPermissionCheckEvent event,
@Nullable Supplier<String> denialOverride)
{
// Set denial message (if any) using default behavior.
Supplier<String> defaultDenial = getDefaultDenial(event.getCheckedPlayer(), event.getCheckedUUID(),
Expand All @@ -521,11 +524,11 @@ public boolean hasExplicitPermission(@NotNull Player player, @NotNull ClaimPermi
* @param event the Event triggering the permission check
* @return the denial reason or null if permission is granted
*/
private @Nullable Supplier<String> getDefaultDenial(
@Nullable Player player,
@NotNull UUID uuid,
@NotNull ClaimPermission permission,
@Nullable Event event)
private @Nullable Supplier<String> getDefaultDenial(
@Nullable Player player,
@NotNull UUID uuid,
@NotNull ClaimPermission permission,
@Nullable Event event)
{
if (player != null)
{
Expand All @@ -541,9 +544,9 @@ else if (permission == ClaimPermission.Edit && player.hasPermission("griefpreven
}

// Claim owner and admins in ignoreclaims mode have access.
if (uuid.equals(this.getOwnerID())
|| GriefPrevention.instance.dataStore.getPlayerData(uuid).ignoreClaims
&& hasBypassPermission(player, permission))
if (uuid.equals(this.getOwnerID())
|| GriefPrevention.instance.dataStore.getPlayerData(uuid).ignoreClaims
&& hasBypassPermission(player, permission))
return null;

// Look for explicit individual permission.
Expand Down Expand Up @@ -590,37 +593,37 @@ && hasBypassPermission(player, permission))
return () ->
{
String reason = GriefPrevention.instance.dataStore.getMessage(permission.getDenialMessage(), this.getOwnerName());
if (hasBypassPermission(player, permission))
if (hasBypassPermission(player, permission))
reason += " " + GriefPrevention.instance.dataStore.getMessage(Messages.IgnoreClaimsAdvertisement);
return reason;
};
}

/**
* Check if the {@link Player} has bypass permissions for a {@link ClaimPermission}. Owner-exclusive edit actions
* require {@code griefprevention.deleteclaims}. All other actions require {@code griefprevention.ignoreclaims}.
*
* @param player the {@code Player}
* @param permission the {@code ClaimPermission} whose bypass permission is being checked
* @return whether the player has the bypass node
*/
@Contract("null, _ -> false")
private boolean hasBypassPermission(@Nullable Player player, @NotNull ClaimPermission permission)
{
if (player == null) return false;

if (permission == ClaimPermission.Edit) return player.hasPermission("griefprevention.deleteclaims");

return player.hasPermission("griefprevention.ignoreclaims");
}

/**
* Check if the {@link Player} has bypass permissions for a {@link ClaimPermission}. Owner-exclusive edit actions
* require {@code griefprevention.deleteclaims}. All other actions require {@code griefprevention.ignoreclaims}.
*
* @param player the {@code Player}
* @param permission the {@code ClaimPermission} whose bypass permission is being checked
* @return whether the player has the bypass node
*/
@Contract("null, _ -> false")
private boolean hasBypassPermission(@Nullable Player player, @NotNull ClaimPermission permission)
{
if (player == null) return false;

if (permission == ClaimPermission.Edit) return player.hasPermission("griefprevention.deleteclaims");

return player.hasPermission("griefprevention.ignoreclaims");
}

/**
* @deprecated Check {@link ClaimPermission#Build} with {@link #checkPermission(Player, ClaimPermission, Event)}.
* @param player the Player
* @return the denial message, or null if the action is allowed
*/
@Deprecated
public @Nullable String allowBreak(@NotNull Player player, @NotNull Material material)
public @Nullable String allowBreak(@NotNull Player player, @NotNull Material material)
{
Supplier<String> supplier = checkPermission(player, ClaimPermission.Build, new CompatBuildBreakEvent(material, true));
return supplier != null ? supplier.get() : null;
Expand All @@ -632,7 +635,7 @@ private boolean hasBypassPermission(@Nullable Player player, @NotNull ClaimPermi
* @return the denial message, or null if the action is allowed
*/
@Deprecated
public @Nullable String allowAccess(@NotNull Player player)
public @Nullable String allowAccess(@NotNull Player player)
{
Supplier<String> supplier = checkPermission(player, ClaimPermission.Access, null);
return supplier != null ? supplier.get() : null;
Expand All @@ -644,7 +647,7 @@ private boolean hasBypassPermission(@Nullable Player player, @NotNull ClaimPermi
* @return the denial message, or null if the action is allowed
*/
@Deprecated
public @Nullable String allowContainers(@NotNull Player player)
public @Nullable String allowContainers(@NotNull Player player)
{
Supplier<String> supplier = checkPermission(player, ClaimPermission.Inventory, null);
return supplier != null ? supplier.get() : null;
Expand All @@ -656,37 +659,37 @@ private boolean hasBypassPermission(@Nullable Player player, @NotNull ClaimPermi
* @return the denial message, or null if the action is allowed
*/
@Deprecated
public @Nullable String allowGrantPermission(@NotNull Player player)
public @Nullable String allowGrantPermission(@NotNull Player player)
{
Supplier<String> supplier = checkPermission(player, ClaimPermission.Manage, null);
return supplier != null ? supplier.get() : null;
}

@Contract("null -> null")
public @Nullable ClaimPermission getPermission(@Nullable String playerID)
@Contract("null -> null")
public @Nullable ClaimPermission getPermission(@Nullable String playerID)
{
if (playerID == null || playerID.isEmpty()) return null;

return this.playerIDToClaimPermissionMap.get(playerID.toLowerCase());
}

//grants a permission for a player or the public
public void setPermission(@Nullable String playerID, @Nullable ClaimPermission permissionLevel)
public void setPermission(@Nullable String playerID, @Nullable ClaimPermission permissionLevel)
{
if (permissionLevel == ClaimPermission.Edit) throw new IllegalArgumentException("Cannot add editors!");

if (playerID == null || playerID.isEmpty()) return;

if (permissionLevel == null)
dropPermission(playerID);
else if (permissionLevel == ClaimPermission.Manage)
if (permissionLevel == null)
dropPermission(playerID);
else if (permissionLevel == ClaimPermission.Manage)
this.managers.add(playerID.toLowerCase());
else
this.playerIDToClaimPermissionMap.put(playerID.toLowerCase(), permissionLevel);
}

//revokes a permission for a player or the public
public void dropPermission(@NotNull String playerID)
public void dropPermission(@NotNull String playerID)
{
playerID = playerID.toLowerCase();
this.playerIDToClaimPermissionMap.remove(playerID);
Expand Down Expand Up @@ -829,6 +832,14 @@ boolean overlaps(Claim otherClaim)
return new BoundingBox(this).intersects(new BoundingBox(otherClaim));
}

public Collection<Entity> getEntities() {
return getChunks().stream().map(Chunk::getEntities).flatMap(Arrays::stream).toList();
}

public <T extends Entity> Collection<T> getEntities(Class<? extends T> entityClass) {
return getEntities().stream().filter(entity -> entity.getClass().isAssignableFrom(entityClass)).map(entity -> (T) entity).toList();
}

//whether more entities may be added to a claim
public String allowMoreEntities(boolean remove)
{
Expand Down

0 comments on commit 22a41dd

Please sign in to comment.