Skip to content

Commit

Permalink
Merge pull request #1 from Micalhl/main
Browse files Browse the repository at this point in the history
Update canInteractEntity & canDamage (Untested)
  • Loading branch information
Xiao-MoMi authored Apr 20, 2024
2 parents 113ba24 + 8e1a049 commit 8533cf5
Show file tree
Hide file tree
Showing 27 changed files with 460 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.momirealms.antigrieflib;

import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;

public interface AntiGriefPlugin {
Expand All @@ -13,5 +14,9 @@ public interface AntiGriefPlugin {

boolean canInteract(Player player, Location location);

boolean canInteractEntity(Player player, Entity entity);

boolean canDamage(Player player, Entity entity);

String getAntiGriefPluginName();
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.massivecraft.factions.zcore.fperms.PermissableAction;
import net.momirealms.antigrieflib.AbstractComp;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

Expand Down Expand Up @@ -42,4 +43,18 @@ public boolean canInteract(Player player, Location location) {
.map(faction -> faction.getAccess(FPlayers.getInstance().getByPlayer(player), PermissableAction.CONTAINER) == Access.ALLOW)
.orElse(true);
}

@Override
public boolean canInteractEntity(Player player, Entity entity) {
return Optional.ofNullable(Board.getInstance().getFactionAt(FLocation.wrap(entity.getLocation())))
.map(faction -> faction.getAccess(FPlayers.getInstance().getByPlayer(player), PermissableAction.CONTAINER) == Access.ALLOW)
.orElse(true);
}

@Override
public boolean canDamage(Player player, Entity entity) {
return Optional.ofNullable(Board.getInstance().getFactionAt(FLocation.wrap(entity.getLocation())))
.map(faction -> faction.getAccess(FPlayers.getInstance().getByPlayer(player), PermissableAction.CONTAINER) == Access.ALLOW)
.orElse(true);
}
}
3 changes: 2 additions & 1 deletion project/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ dependencies {
compileOnly("net.william278.husktowns:husktowns-bukkit:3.0.2") // HuskTowns
compileOnly("net.william278.huskclaims:huskclaims-bukkit:1.0.3") // HuskClaims
compileOnly("com.intellectualsites.plotsquared:plotsquared-bukkit:7.3.5") // PlotSquared
compileOnly("com.bgsoftware:SuperiorSkyblockAPI:2023.3") // SuperiorSkyBlock2
compileOnly("com.bgsoftware:SuperiorSkyblockAPI:2023.3") // SuperiorSkyBlock2 - API
compileOnly("com.palmergames.bukkit.towny:towny:0.100.1.17") // Towny
compileOnly("com.craftaro:FabledSkyBlock:3.0.4") // FabledSkyBlock
compileOnly("com.craftaro:UltimateClaims:2.2.0") // FabledSkyBlock
Expand All @@ -45,6 +45,7 @@ dependencies {
compileOnly(files("libs/KingdomsX-pruned.jar")) // KingdomsX
compileOnly(files("libs/CrashClaim-pruned.jar")) // CrashClaim
compileOnly(files("libs/XClaim.jar")) // XClaim
compileOnly(files("libs/SuperiorSkyblock2-2023.3.jar")) // SuperiorSkyBlock2 - Plugin

compileOnly("io.github.fabiozumbi12.RedProtect:RedProtect-Core:8.1.1") { exclude(group = "*") } // RedProtect
compileOnly("io.github.fabiozumbi12.RedProtect:RedProtect-Spigot:8.1.1") { exclude(group = "*") } // RedProtect
Expand Down
Binary file added project/libs/SuperiorSkyblock2-2023.3.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.momirealms.antigrieflib.comp.*;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
Expand Down Expand Up @@ -102,6 +103,40 @@ public boolean canInteract(Player player, Location location) {
return true;
}

/**
* Detects if a player has permission to interact with an entity
*
* @param player player
* @param entity entity
* @return has perm or not
*/
public boolean canInteractEntity(Player player, Entity entity) {
if (ignoreOP && player.isOp()) return true;
for (AntiGriefPlugin antiGriefPlugin : plugins) {
if (!antiGriefPlugin.canInteractEntity(player, entity)) {
return false;
}
}
return true;
}

/**
* Detects if a player has permission to damage an entity
*
* @param player player
* @param entity entity
* @return has perm or not
*/
public boolean canDamage(Player player, Entity entity) {
if (ignoreOP && player.isOp()) return true;
for (AntiGriefPlugin antiGriefPlugin : plugins) {
if (!antiGriefPlugin.canDamage(player, entity)) {
return false;
}
}
return true;
}

public static class Builder {

private final AntiGriefLib lib;
Expand Down Expand Up @@ -151,7 +186,7 @@ public AntiGriefLib build() {
}
}

private void registerNewCompatibility(AntiGriefPlugin antiGriefPlugin) {
public void registerNewCompatibility(AntiGriefPlugin antiGriefPlugin) {
this.plugins.add(antiGriefPlugin);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.momirealms.antigrieflib.AbstractComp;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import world.bentobox.bentobox.BentoBox;
Expand Down Expand Up @@ -44,4 +45,49 @@ public boolean canInteract(Player player, Location location) {
.map(island -> island.isAllowed(User.getInstance(player), Flags.CONTAINER))
.orElse(true);
}

@Override
public boolean canInteractEntity(Player player, Entity entity) {
// I am not sure if I should use Flags.HURT_xxx or Flags.CONTAINER
return entityOperation(player, entity);
}

@Override
public boolean canDamage(Player player, Entity entity) {
return entityOperation(player, entity) && (!(entity instanceof Player) || entity.getWorld().getPVP());
}

private boolean entityOperation(final Player player, final Entity entity) {
return BentoBox.getInstance()
.getIslands()
.getIslandAt(entity.getLocation())
.map(island -> island.isAllowed(User.getInstance(player), switch (getType(entity)) {
case VILLAGER -> Flags.HURT_VILLAGERS;
case HOSTILE -> Flags.HURT_MONSTERS;
case PASSIVE -> Flags.HURT_ANIMALS;
case PLAYER -> switch (entity.getWorld().getEnvironment()) {
case NORMAL, CUSTOM -> Flags.PVP_OVERWORLD;
case NETHER -> Flags.PVP_NETHER;
case THE_END -> Flags.PVP_END;
};
default -> Flags.CONTAINER;
}))
.orElse(true);
}

private enum GameEntityType {
PASSIVE, VILLAGER, HOSTILE, PLAYER, OTHER
}

private static GameEntityType getType(Entity entity) {
return switch (entity.getType()) {
case SKELETON, STRAY, WITHER_SKELETON, BLAZE, CAVE_SPIDER, CREEPER, DROWNED, ELDER_GUARDIAN, ENDERMAN, GIANT, GUARDIAN, HUSK, PILLAGER, ILLUSIONER, PIGLIN, PIGLIN_BRUTE, ZOMBIFIED_PIGLIN, EVOKER, VINDICATOR, RAVAGER, WITCH, SILVERFISH, SPIDER, VEX, WARDEN, WITHER, ZOGLIN, ZOMBIE, ZOMBIE_VILLAGER, GHAST, PHANTOM, SLIME, MAGMA_CUBE, SHULKER, ENDER_DRAGON, ENDERMITE, PUFFERFISH ->
GameEntityType.HOSTILE;
case CAMEL, DONKEY, HORSE, LLAMA, MULE, SKELETON_HORSE, TRADER_LLAMA, ZOMBIE_HORSE, AXOLOTL, BEE, CAT, CHICKEN, COW, FOX, FROG, GOAT, HOGLIN, MUSHROOM_COW, OCELOT, PANDA, PARROT, PIG, POLAR_BEAR, RABBIT, SHEEP, SNIFFER, STRIDER, WOLF, IRON_GOLEM, SNOWMAN, COD, DOLPHIN, SALMON, TADPOLE, TROPICAL_FISH, GLOW_SQUID, SQUID, BAT, ALLAY, TURTLE ->
GameEntityType.PASSIVE;
case VILLAGER, WANDERING_TRADER -> GameEntityType.VILLAGER;
case PLAYER -> GameEntityType.PLAYER;
default -> GameEntityType.OTHER;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.crashcraft.crashclaim.permissions.PermissionRoute;
import net.momirealms.antigrieflib.AbstractComp;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

Expand Down Expand Up @@ -35,4 +36,14 @@ public boolean canBreak(Player player, Location location) {
public boolean canInteract(Player player, Location location) {
return api.getPermissionHelper().hasPermission(player.getUniqueId(), location, PermissionRoute.INTERACTIONS);
}

@Override
public boolean canInteractEntity(Player player, Entity entity) {
return api.getPermissionHelper().hasPermission(player.getUniqueId(), entity.getLocation(), PermissionRoute.ENTITIES);
}

@Override
public boolean canDamage(Player player, Entity entity) {
return api.getPermissionHelper().hasPermission(player.getUniqueId(), entity.getLocation(), PermissionRoute.ENTITIES) && (!(entity instanceof Player) || entity.getWorld().getPVP());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.craftaro.skyblock.api.island.IslandRole;
import net.momirealms.antigrieflib.AbstractComp;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

Expand Down Expand Up @@ -34,6 +35,16 @@ public boolean canInteract(Player player, Location location) {
return isIslandMember(player, location);
}

@Override
public boolean canInteractEntity(Player player, Entity entity) {
return isIslandMember(player, entity.getLocation());
}

@Override
public boolean canDamage(Player player, Entity entity) {
return isIslandMember(player, entity.getLocation()) && (!(entity instanceof Player) || entity.getWorld().getPVP());
}

private boolean isIslandMember(Player player, Location location) {
return Optional.ofNullable(SkyBlockAPI.getIslandManager().getIslandAtLocation(location))
.map(island -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import com.massivecraft.factions.FactionsPlugin;
import com.massivecraft.factions.listeners.FactionsBlockListener;
import com.massivecraft.factions.listeners.FactionsEntityListener;
import com.massivecraft.factions.perms.PermissibleActions;
import net.momirealms.antigrieflib.AbstractComp;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

Expand Down Expand Up @@ -38,4 +40,16 @@ public boolean canInteract(Player player, Location location) {
if (!plugin.worldUtil().isEnabled(location.getWorld())) return true;
return FactionsBlockListener.playerCanBuildDestroyBlock(player, location, PermissibleActions.CONTAINER, false);
}

@Override
public boolean canInteractEntity(Player player, Entity entity) {
if (!plugin.worldUtil().isEnabled(entity.getWorld())) return true;
return FactionsEntityListener.canDamage(player, entity, false);
}

@Override
public boolean canDamage(Player player, Entity entity) {
if (!plugin.worldUtil().isEnabled(entity.getWorld())) return !(entity instanceof Player) || entity.getWorld().getPVP();
return FactionsEntityListener.canDamage(player, entity, false) && (!(entity instanceof Player) || entity.getWorld().getPVP());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.griefdefender.api.claim.TrustTypes;
import net.momirealms.antigrieflib.AbstractComp;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

Expand Down Expand Up @@ -39,4 +40,18 @@ public boolean canInteract(Player player, Location location) {
.map(user -> user.canUseBlock(location, TrustTypes.CONTAINER, false, false))
.orElse(false);
}

@Override
public boolean canInteractEntity(Player player, Entity entity) {
return Optional.ofNullable(GriefDefender.getCore().getUser(player.getUniqueId()))
.map(user -> user.canInteractWithEntity(player.getInventory(), entity, TrustTypes.CONTAINER))
.orElse(false);
}

@Override
public boolean canDamage(Player player, Entity entity) {
return Optional.ofNullable(GriefDefender.getCore().getUser(player.getUniqueId()))
.map(user -> user.canHurtEntity(player.getInventory().getItemInMainHand(), entity))
.orElse(false) && (!(entity instanceof Player) || entity.getWorld().getPVP());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import me.ryanhamshire.GriefPrevention.PlayerData;
import net.momirealms.antigrieflib.AbstractComp;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

Expand Down Expand Up @@ -34,6 +35,16 @@ public boolean canInteract(Player player, Location location) {
return checkPermission(player, location, ClaimPermission.Inventory);
}

@Override
public boolean canInteractEntity(Player player, Entity entity) {
return checkPermission(player, entity.getLocation(), ClaimPermission.Inventory);
}

@Override
public boolean canDamage(Player player, Entity entity) {
return checkPermission(player, entity.getLocation(), ClaimPermission.Inventory) && (!(entity instanceof Player) || entity.getWorld().getPVP());
}

private boolean checkPermission(Player player, Location location, ClaimPermission permission) {
PlayerData playerData = GriefPrevention.instance.dataStore.getPlayerData(player.getUniqueId());
Claim claim = GriefPrevention.instance.dataStore.getClaimAt(location, false, playerData.lastClaim);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import net.william278.huskclaims.api.BukkitHuskClaimsAPI;
import net.william278.huskclaims.libraries.cloplib.operation.OperationType;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

Expand Down Expand Up @@ -34,4 +35,14 @@ public boolean canBreak(Player player, Location location) {
public boolean canInteract(Player player, Location location) {
return api.isOperationAllowed(api.getOnlineUser(player), OperationType.BLOCK_INTERACT, api.getPosition(location));
}

@Override
public boolean canInteractEntity(Player player, Entity entity) {
return api.isOperationAllowed(api.getOnlineUser(player), OperationType.ENTITY_INTERACT, api.getPosition(entity.getLocation()));
}

@Override
public boolean canDamage(Player player, Entity entity) {
return api.isOperationAllowed(api.getOnlineUser(player), OperationType.PLAYER_DAMAGE_ENTITY, api.getPosition(entity.getLocation())) && (!(entity instanceof Player) || entity.getWorld().getPVP());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.momirealms.antigrieflib.AbstractComp;
import net.william278.husktowns.api.BukkitHuskTownsAPI;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

Expand Down Expand Up @@ -34,6 +35,16 @@ public boolean canInteract(Player player, Location location) {
return isTownMember(player, location);
}

@Override
public boolean canInteractEntity(Player player, Entity entity) {
return isTownMember(player, entity.getLocation());
}

@Override
public boolean canDamage(Player player, Entity entity) {
return isTownMember(player, entity.getLocation()) && (!(entity instanceof Player) || entity.getWorld().getPVP());
}

public boolean isTownMember(Player player, Location location) {
return api.getClaimAt(api.getPosition(location))
.map(townClaim -> townClaim.town().getMembers().containsKey(player.getUniqueId()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import net.momirealms.antigrieflib.CustomFlag;
import net.momirealms.antigrieflib.Flag;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

Expand Down Expand Up @@ -97,6 +98,26 @@ public boolean canInteract(Player player, Location location) {
);
}

@Override
public boolean canInteractEntity(Player player, Entity entity) {
return api.getIslandViaLocation(entity.getLocation())
.map(island -> api.getIslandPermission(
island,
api.getUser(player),
PermissionType.OPEN_CONTAINERS) // FIXME: or use KILL_MOBS?
).orElse(true);
}

@Override
public boolean canDamage(Player player, Entity entity) {
return api.getIslandViaLocation(entity.getLocation())
.map(island -> api.getIslandPermission(
island,
api.getUser(player),
PermissionType.KILL_MOBS)
).orElse(true) && (!(entity instanceof Player) || entity.getWorld().getPVP());
}

@Override
@SuppressWarnings("DuplicatedCode")
public void registerOnEnable(Flag customPlaceFlag, Flag customBreakFlag, Flag customInteractFlag) {
Expand Down
Loading

0 comments on commit 8533cf5

Please sign in to comment.