Skip to content

Commit

Permalink
fix worldguard hook errors
Browse files Browse the repository at this point in the history
  • Loading branch information
YouHaveTrouble committed Sep 17, 2021
1 parent c0ca85c commit 1e7adf3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 21 deletions.
12 changes: 10 additions & 2 deletions src/main/java/me/youhavetrouble/preventstabby/PreventStabby.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public final class PreventStabby extends JavaPlugin {
private PlayerManager playerManager;
private DatabaseSQLite sqLite;
private SmartCache smartCache;
private static boolean worldGuardHook;

@Override
public void onEnable() {
Expand All @@ -45,7 +46,7 @@ public void onEnable() {
smartCache.runSmartCache();

// Register listeners
Reflections reflections = new Reflections(new String[]{"me.youhavetrouble.preventstabby"});
Reflections reflections = new Reflections((Object[]) new String[]{"me.youhavetrouble.preventstabby"});
Set<Class<?>> listenerClasses = reflections.getTypesAnnotatedWith(PreventStabbyListener.class);
listenerClasses.forEach((listener)-> {
try {
Expand All @@ -71,11 +72,18 @@ public void onEnable() {

try {
WorldGuardHook.init();
} catch (NoClassDefFoundError ignored) {}
worldGuardHook = true;
} catch (NoClassDefFoundError e) {
worldGuardHook = false;
}

Metrics metrics = new Metrics(this, 10597);
}

public static boolean worldGuardHookEnabled() {
return worldGuardHook;
}

public void reloadPluginConfig() {
configCache = new ConfigCache();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public class WorldGuardHook {

private static FlagRegistry flagRegistry;
public static StateFlag FORCE_PVP_FLAG;
private static boolean enabled = false;

public static void init() {
PreventStabby plugin = PreventStabby.getPlugin();
Expand All @@ -31,13 +30,11 @@ public static void init() {
plugin.getLogger().info("Hooking into WorldGuard");
flagRegistry = WorldGuard.getInstance().getFlagRegistry();
createForcePvpFlag(plugin);
enabled = true;
} catch (NoClassDefFoundError | ClassNotFoundException ignored) {
}
}

private static void createForcePvpFlag(Plugin plugin) {
if (!enabled) return;
if (flagRegistry == null) return;
String flagName = "preventstabby-force-pvp";
try {
Expand All @@ -55,7 +52,6 @@ private static void createForcePvpFlag(Plugin plugin) {
}

public static boolean isPlayerForcedToPvp(Player player) {
if (!enabled) return false;
RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer();
RegionQuery query = container.createQuery();
org.bukkit.Location loc = player.getLocation();
Expand All @@ -64,8 +60,4 @@ public static boolean isPlayerForcedToPvp(Player player) {
return set.testState(localPlayer, FORCE_PVP_FLAG);
}

public static boolean isHooked() {
return enabled;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -112,24 +112,25 @@ public boolean canDamage(UUID attacker, UUID victim, boolean sendDenyMessage) {
public boolean canDamage(UUID attacker, UUID victim, boolean sendDenyMessage, boolean checkVictimSpawnProtection) {

if (hasLoginProtection(attacker) || hasTeleportProtection(attacker)) return false;

if (checkVictimSpawnProtection && hasLoginProtection(victim)) return false;

if (checkVictimSpawnProtection && hasTeleportProtection(victim)) return false;

SmartCache smartCache = PreventStabby.getPlugin().getSmartCache();

Player attackerPlayer = Bukkit.getPlayer(attacker);
Player victimPlayer = Bukkit.getPlayer(victim);
if (!smartCache.getPlayerData(attacker).isPvpEnabled()) {
Player attackerPlayer = Bukkit.getPlayer(attacker);
if (PreventStabby.worldGuardHookEnabled() && WorldGuardHook.isPlayerForcedToPvp(attackerPlayer)) return true;

if (!smartCache.getPlayerData(attacker).isPvpEnabled() && (attackerPlayer != null && !isInForcedPvpRegion(attackerPlayer))) {
if (sendDenyMessage) {
ConfigCache config = PreventStabby.getPlugin().getConfigCache();
PluginMessages.sendActionBar(attacker, config.getCannot_attack_attacker());
}
return false;
}
if (!smartCache.getPlayerData(victim).isPvpEnabled() && (victimPlayer != null && !isInForcedPvpRegion(victimPlayer))) {
if (!smartCache.getPlayerData(victim).isPvpEnabled()) {
Player victimPlayer = Bukkit.getPlayer(victim);
if (PreventStabby.worldGuardHookEnabled() && WorldGuardHook.isPlayerForcedToPvp(victimPlayer)) return true;

if (sendDenyMessage) {
ConfigCache config = PreventStabby.getPlugin().getConfigCache();
PluginMessages.sendActionBar(attacker, config.getCannot_attack_victim());
Expand Down Expand Up @@ -157,9 +158,4 @@ public boolean hasTeleportProtection(UUID uuid) {
return Instant.now().getEpochSecond() < smartCache.getPlayerData(uuid).getTeleportTimestamp();
}

public boolean isInForcedPvpRegion(Player player) {
if (!WorldGuardHook.isHooked()) return false;
return WorldGuardHook.isPlayerForcedToPvp(player);
}

}

0 comments on commit 1e7adf3

Please sign in to comment.