Compare commits

..

9 Commits

14 changed files with 51 additions and 67 deletions
+5 -6
View File
@@ -6,7 +6,7 @@
<groupId>me.youhavetrouble</groupId> <groupId>me.youhavetrouble</groupId>
<artifactId>PreventStabby</artifactId> <artifactId>PreventStabby</artifactId>
<version>1.3-pre4</version> <version>1.4</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>PreventStabby</name> <name>PreventStabby</name>
@@ -22,7 +22,7 @@
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version> <version>3.8.1</version>
<configuration> <configuration>
<source>${java.version}</source> <source>${java.version}</source>
<target>${java.version}</target> <target>${java.version}</target>
@@ -47,7 +47,6 @@
</relocation> </relocation>
<relocation> <relocation>
<pattern>org.bstats</pattern> <pattern>org.bstats</pattern>
<!-- Replace this with your package! -->
<shadedPattern>bstats</shadedPattern> <shadedPattern>bstats</shadedPattern>
</relocation> </relocation>
</relocations> </relocations>
@@ -93,13 +92,13 @@
<dependency> <dependency>
<groupId>org.projectlombok</groupId> <groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId> <artifactId>lombok</artifactId>
<version>1.18.12</version> <version>1.18.22</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.reflections</groupId> <groupId>org.reflections</groupId>
<artifactId>reflections</artifactId> <artifactId>reflections</artifactId>
<version>0.9.12</version> <version>0.10.2</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.github.thatsmusic99</groupId> <groupId>com.github.thatsmusic99</groupId>
@@ -109,7 +108,7 @@
<dependency> <dependency>
<groupId>org.bstats</groupId> <groupId>org.bstats</groupId>
<artifactId>bstats-bukkit</artifactId> <artifactId>bstats-bukkit</artifactId>
<version>2.2.1</version> <version>3.0.0</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency> <dependency>
@@ -25,6 +25,7 @@ public final class PreventStabby extends JavaPlugin {
private PlayerManager playerManager; private PlayerManager playerManager;
private DatabaseSQLite sqLite; private DatabaseSQLite sqLite;
private SmartCache smartCache; private SmartCache smartCache;
private static boolean worldGuardHook;
@Override @Override
public void onEnable() { public void onEnable() {
@@ -45,7 +46,7 @@ public final class PreventStabby extends JavaPlugin {
smartCache.runSmartCache(); smartCache.runSmartCache();
// Register listeners // 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); Set<Class<?>> listenerClasses = reflections.getTypesAnnotatedWith(PreventStabbyListener.class);
listenerClasses.forEach((listener)-> { listenerClasses.forEach((listener)-> {
try { try {
@@ -71,9 +72,16 @@ public final class PreventStabby extends JavaPlugin {
try { try {
WorldGuardHook.init(); WorldGuardHook.init();
} catch (NoClassDefFoundError ignored) {} worldGuardHook = true;
} catch (NoClassDefFoundError e) {
worldGuardHook = false;
}
Metrics metrics = new Metrics(this, 10597); Metrics metrics = new Metrics(this, 14074);
}
public static boolean worldGuardHookEnabled() {
return worldGuardHook;
} }
public void reloadPluginConfig() { public void reloadPluginConfig() {
@@ -11,9 +11,7 @@ public class HelpCommand {
public static void help(CommandSender sender, String[] args) { public static void help(CommandSender sender, String[] args) {
Bukkit.getScheduler().runTaskAsynchronously(PreventStabby.getPlugin(), () -> { Bukkit.getScheduler().runTaskAsynchronously(PreventStabby.getPlugin(), () -> {
List<String> helpPage = new ArrayList<>(); List<String> helpPage = new ArrayList<>();
helpPage.add(PluginMessages.parseMessage("&b&m &bPreventStabby Help&b&m ")); helpPage.add(PluginMessages.parseMessage("&b&m &bPreventStabby Help&b&m "));
helpPage.add(PluginMessages.parseMessage("&9/pvp &b&lhelp &f- shows this message")); helpPage.add(PluginMessages.parseMessage("&9/pvp &b&lhelp &f- shows this message"));
if (sender.hasPermission("preventstabby.command.toggle")) { if (sender.hasPermission("preventstabby.command.toggle")) {
@@ -22,7 +20,6 @@ public class HelpCommand {
} }
String[] helpReady = helpPage.toArray(new String[0]); String[] helpReady = helpPage.toArray(new String[0]);
sender.sendMessage(helpReady); sender.sendMessage(helpReady);
}); });
} }
@@ -11,9 +11,7 @@ import org.bukkit.entity.Player;
public class PvpToggleCommand { public class PvpToggleCommand {
public static void toggle(CommandSender sender, String[] args) { public static void toggle(CommandSender sender, String[] args) {
Bukkit.getScheduler().runTaskAsynchronously(PreventStabby.getPlugin(), () -> { Bukkit.getScheduler().runTaskAsynchronously(PreventStabby.getPlugin(), () -> {
if (!sender.hasPermission("preventstabby.command.toggle")) { if (!sender.hasPermission("preventstabby.command.toggle")) {
String message = ChatColor.translateAlternateColorCodes('&', PreventStabby.getPlugin().getConfigCache().getNo_permission()); String message = ChatColor.translateAlternateColorCodes('&', PreventStabby.getPlugin().getConfigCache().getNo_permission());
sender.sendMessage(message); sender.sendMessage(message);
@@ -47,7 +45,6 @@ public class PvpToggleCommand {
sender.sendMessage(message); sender.sendMessage(message);
return; return;
} }
if (sender instanceof Player) { if (sender instanceof Player) {
Player player = (Player) sender; Player player = (Player) sender;
if (CombatTimer.isInCombat(player.getUniqueId())) { if (CombatTimer.isInCombat(player.getUniqueId())) {
@@ -8,7 +8,6 @@ import org.bukkit.command.CommandSender;
public class ReloadCommand { public class ReloadCommand {
public static void reload(CommandSender sender) { public static void reload(CommandSender sender) {
Bukkit.getScheduler().runTaskAsynchronously(PreventStabby.getPlugin(), () -> { Bukkit.getScheduler().runTaskAsynchronously(PreventStabby.getPlugin(), () -> {
if (!sender.hasPermission("preventstabby.command.reload")) { if (!sender.hasPermission("preventstabby.command.reload")) {
String message = ChatColor.translateAlternateColorCodes('&', PreventStabby.getPlugin().getConfigCache().getNo_permission()); String message = ChatColor.translateAlternateColorCodes('&', PreventStabby.getPlugin().getConfigCache().getNo_permission());
@@ -17,7 +16,6 @@ public class ReloadCommand {
} }
PreventStabby.getPlugin().reloadPluginConfig(sender); PreventStabby.getPlugin().reloadPluginConfig(sender);
}); });
} }
} }
@@ -85,9 +85,9 @@ public class ConfigCache {
} }
}; };
configFile.setDescription("PvP toggle that CARES about your pets!"); configFile.setDescription("Prevent people from getting stabbed!");
configFile.addLink("Spigot", "https://www.spigotmc.org/resources/togglepvp.89376/"); configFile.addLink("Spigot", "https://www.spigotmc.org/resources/89376/");
configFile.addLink("Source", "https://github.com/YouHaveTrouble/TogglePvP"); configFile.addLink("Source", "https://github.com/YouHaveTrouble/PreventStabby");
configFile.load(); configFile.load();
FileConfiguration config = configFile.getConfig(); FileConfiguration config = configFile.getConfig();
@@ -135,8 +135,6 @@ public class ConfigCache {
this.entering_combat = config.getString("messages.entering_combat", "&cEntering combat"); this.entering_combat = config.getString("messages.entering_combat", "&cEntering combat");
this.leaving_combat = config.getString("messages.leaving_combat", "&cLeaving combat"); this.leaving_combat = config.getString("messages.leaving_combat", "&cLeaving combat");
this.cant_do_that_during_combat = config.getString("messages.cant_do_that_during_combat", "&cYou can't do that while in combat!"); this.cant_do_that_during_combat = config.getString("messages.cant_do_that_during_combat", "&cYou can't do that while in combat!");
} }
@@ -18,27 +18,23 @@ import org.bukkit.plugin.Plugin;
public class WorldGuardHook { public class WorldGuardHook {
private static WorldGuardPlugin worldGuardPlugin;
private static FlagRegistry flagRegistry; private static FlagRegistry flagRegistry;
public static StateFlag FORCE_PVP_FLAG; public static StateFlag FORCE_PVP_FLAG;
public static void init() { public static void init() {
PreventStabby plugin = PreventStabby.getPlugin(); PreventStabby plugin = PreventStabby.getPlugin();
try { try {
Class.forName("com.sk89q.worldguard.protection.flags.registry.FlagConflictException"); Class.forName("com.sk89q.worldguard.protection.flags.registry.FlagRegistry");
worldGuardPlugin = WorldGuardPlugin.inst(); WorldGuardPlugin worldGuardPlugin = WorldGuardPlugin.inst();
if (WorldGuard.getInstance() == null || worldGuardPlugin == null) return; if (WorldGuard.getInstance() == null || worldGuardPlugin == null) return;
plugin.getLogger().info("Hooking into WorldGuard"); plugin.getLogger().info("Hooking into WorldGuard");
flagRegistry = WorldGuard.getInstance().getFlagRegistry(); flagRegistry = WorldGuard.getInstance().getFlagRegistry();
createForcePvpFlag(plugin); createForcePvpFlag(plugin);
} catch (NoClassDefFoundError | ClassNotFoundException e) { } catch (NoClassDefFoundError | ClassNotFoundException ignored) {
return;
} }
} }
private static void createForcePvpFlag(Plugin plugin) { private static void createForcePvpFlag(Plugin plugin) {
if (!isHooked()) return;
if (flagRegistry == null) return; if (flagRegistry == null) return;
String flagName = "preventstabby-force-pvp"; String flagName = "preventstabby-force-pvp";
try { try {
@@ -56,7 +52,6 @@ public class WorldGuardHook {
} }
public static boolean isPlayerForcedToPvp(Player player) { public static boolean isPlayerForcedToPvp(Player player) {
if (!isHooked()) return false;
RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer(); RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer();
RegionQuery query = container.createQuery(); RegionQuery query = container.createQuery();
org.bukkit.Location loc = player.getLocation(); org.bukkit.Location loc = player.getLocation();
@@ -65,10 +60,4 @@ public class WorldGuardHook {
return set.testState(localPlayer, FORCE_PVP_FLAG); return set.testState(localPlayer, FORCE_PVP_FLAG);
} }
public static boolean isHooked() {
return worldGuardPlugin != null;
}
} }
@@ -15,7 +15,7 @@ import java.util.UUID;
public class MountHitByExplosionListener implements Listener { public class MountHitByExplosionListener implements Listener {
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onPlayerAttackMount(org.bukkit.event.entity.EntityDamageByEntityEvent event) { public void onMountHitByExplosion(org.bukkit.event.entity.EntityDamageByEntityEvent event) {
if (!(event.getDamager() instanceof Player) || event.getEntity().getPassengers().isEmpty()) return; if (!(event.getDamager() instanceof Player) || event.getEntity().getPassengers().isEmpty()) return;
if (!event.getCause().equals(EntityDamageEvent.DamageCause.ENTITY_EXPLOSION)) return; if (!event.getCause().equals(EntityDamageEvent.DamageCause.ENTITY_EXPLOSION)) return;
UUID damager = event.getDamager().getUniqueId(); UUID damager = event.getDamager().getUniqueId();
@@ -16,7 +16,7 @@ import java.util.UUID;
public class MountHitByFireworkListener implements Listener { public class MountHitByFireworkListener implements Listener {
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onPlayerAttackMount(org.bukkit.event.entity.EntityDamageByEntityEvent event) { public void onMountHitByFireWork(org.bukkit.event.entity.EntityDamageByEntityEvent event) {
if (!(event.getDamager() instanceof Firework) || event.getEntity().getPassengers().isEmpty()) return; if (!(event.getDamager() instanceof Firework) || event.getEntity().getPassengers().isEmpty()) return;
if (!event.getCause().equals(EntityDamageEvent.DamageCause.ENTITY_EXPLOSION)) return; if (!event.getCause().equals(EntityDamageEvent.DamageCause.ENTITY_EXPLOSION)) return;
Firework firework = (Firework) event.getDamager(); Firework firework = (Firework) event.getDamager();
@@ -15,10 +15,10 @@ import java.util.UUID;
public class PlayerHitMountWithProjectileListener implements Listener { public class PlayerHitMountWithProjectileListener implements Listener {
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onPlayerAttackMount(org.bukkit.event.entity.EntityDamageByEntityEvent event) { public void onPlayerHitMountWithProjectile(org.bukkit.event.entity.EntityDamageByEntityEvent event) {
if (!(event.getDamager() instanceof Player) || event.getEntity().getPassengers().isEmpty()) return; if (!(event.getDamager() instanceof Projectile)) return;
Projectile projectile = (Projectile) event.getDamager(); Projectile projectile = (Projectile) event.getDamager();
if (!(projectile.getShooter() instanceof Player)) return; if (!(projectile.getShooter() instanceof Player) || event.getEntity().getPassengers().isEmpty()) return;
Entity entity = event.getEntity(); Entity entity = event.getEntity();
UUID damager = event.getDamager().getUniqueId(); UUID damager = event.getDamager().getUniqueId();
if (Util.processMountAttack(damager, entity)) if (Util.processMountAttack(damager, entity))
@@ -17,6 +17,9 @@ public class CombatCommandListener implements Listener {
if (!PreventStabby.getPlugin().getConfigCache().isBlock_commands_in_combat()) if (!PreventStabby.getPlugin().getConfigCache().isBlock_commands_in_combat())
return; return;
if (event.getPlayer().hasPermission("toglepvp.combatcommandblock.bypass"))
return;
PlayerManager playerManager = PreventStabby.getPlugin().getPlayerManager(); PlayerManager playerManager = PreventStabby.getPlugin().getPlayerManager();
if (!playerManager.getPlayer(event.getPlayer().getUniqueId()).isInCombat()) if (!playerManager.getPlayer(event.getPlayer().getUniqueId()).isInCombat())
@@ -27,9 +30,6 @@ public class CombatCommandListener implements Listener {
if (!PreventStabby.getPlugin().getConfigCache().getCombatBlockedCommands().contains(command)) if (!PreventStabby.getPlugin().getConfigCache().getCombatBlockedCommands().contains(command))
return; return;
if (event.getPlayer().hasPermission("toglepvp.combatcommandblock.bypass"))
return;
event.setCancelled(true); event.setCancelled(true);
PluginMessages.sendMessage(event.getPlayer(), PreventStabby.getPlugin().getConfigCache().getCant_do_that_during_combat()); PluginMessages.sendMessage(event.getPlayer(), PreventStabby.getPlugin().getConfigCache().getCant_do_that_during_combat());
@@ -112,24 +112,25 @@ public class PlayerManager {
public boolean canDamage(UUID attacker, UUID victim, boolean sendDenyMessage, boolean checkVictimSpawnProtection) { public boolean canDamage(UUID attacker, UUID victim, boolean sendDenyMessage, boolean checkVictimSpawnProtection) {
if (hasLoginProtection(attacker) || hasTeleportProtection(attacker)) return false; if (hasLoginProtection(attacker) || hasTeleportProtection(attacker)) return false;
if (checkVictimSpawnProtection && hasLoginProtection(victim)) return false; if (checkVictimSpawnProtection && hasLoginProtection(victim)) return false;
if (checkVictimSpawnProtection && hasTeleportProtection(victim)) return false; if (checkVictimSpawnProtection && hasTeleportProtection(victim)) return false;
SmartCache smartCache = PreventStabby.getPlugin().getSmartCache(); SmartCache smartCache = PreventStabby.getPlugin().getSmartCache();
if (!smartCache.getPlayerData(attacker).isPvpEnabled()) {
Player attackerPlayer = Bukkit.getPlayer(attacker); Player attackerPlayer = Bukkit.getPlayer(attacker);
Player victimPlayer = Bukkit.getPlayer(victim); if (PreventStabby.worldGuardHookEnabled() && WorldGuardHook.isPlayerForcedToPvp(attackerPlayer)) return true;
if (!smartCache.getPlayerData(attacker).isPvpEnabled() && (attackerPlayer != null && !isInForcedPvpRegion(attackerPlayer))) {
if (sendDenyMessage) { if (sendDenyMessage) {
ConfigCache config = PreventStabby.getPlugin().getConfigCache(); ConfigCache config = PreventStabby.getPlugin().getConfigCache();
PluginMessages.sendActionBar(attacker, config.getCannot_attack_attacker()); PluginMessages.sendActionBar(attacker, config.getCannot_attack_attacker());
} }
return false; 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) { if (sendDenyMessage) {
ConfigCache config = PreventStabby.getPlugin().getConfigCache(); ConfigCache config = PreventStabby.getPlugin().getConfigCache();
PluginMessages.sendActionBar(attacker, config.getCannot_attack_victim()); PluginMessages.sendActionBar(attacker, config.getCannot_attack_victim());
@@ -157,9 +158,4 @@ public class PlayerManager {
return Instant.now().getEpochSecond() < smartCache.getPlayerData(uuid).getTeleportTimestamp(); return Instant.now().getEpochSecond() < smartCache.getPlayerData(uuid).getTeleportTimestamp();
} }
public boolean isInForcedPvpRegion(Player player) {
if (!WorldGuardHook.isHooked()) return false;
return WorldGuardHook.isPlayerForcedToPvp(player);
}
} }
@@ -38,11 +38,6 @@ public class Util {
ConfigCache config = PreventStabby.getPlugin().getConfigCache(); ConfigCache config = PreventStabby.getPlugin().getConfigCache();
if (!PreventStabby.getPlugin().getPlayerManager().getPlayerPvPState(attacker)) {
PluginMessages.sendActionBar(attacker, config.getCannot_attack_mounts_attacker());
return true;
}
Set<UUID> playerPassengersWithPvpEnabled = new HashSet<>(); Set<UUID> playerPassengersWithPvpEnabled = new HashSet<>();
boolean hasPlayerPassengers = false; boolean hasPlayerPassengers = false;
@@ -54,7 +49,14 @@ public class Util {
playerPassengersWithPvpEnabled.add(player.getUniqueId()); playerPassengersWithPvpEnabled.add(player.getUniqueId());
} }
if (hasPlayerPassengers && playerPassengersWithPvpEnabled.isEmpty()) { if (!hasPlayerPassengers) return false;
if (!PreventStabby.getPlugin().getPlayerManager().getPlayerPvPState(attacker)) {
PluginMessages.sendActionBar(attacker, config.getCannot_attack_mounts_attacker());
return true;
}
if (playerPassengersWithPvpEnabled.isEmpty()) {
PluginMessages.sendActionBar(attacker, config.getCannot_attack_mounts_victim()); PluginMessages.sendActionBar(attacker, config.getCannot_attack_mounts_victim());
return true; return true;
} }
+6 -6
View File
@@ -12,21 +12,21 @@ commands:
aliases: aliases:
- pvp - pvp
permissions: permissions:
toglepvp.combatcommandblock.bypass: preventstabby.combatcommandblock.bypass:
default: op default: op
description: Allows to bypass combat command blocker description: Allows to bypass combat command blocker
toglepvp.combatteleportblock.bypass: preventstabby.combatteleportblock.bypass:
default: op default: op
description: Allows to bypass combat teleport blocker description: Allows to bypass combat teleport blocker
togglepvp.command: preventstabby.command:
default: true default: true
description: Allows usage of /pvp command description: Allows usage of /pvp command
togglepvp.command.toggle: preventstabby.command.toggle:
default: true default: true
description: Allows usage of /pvp <toggle/enable/disable> command description: Allows usage of /pvp <toggle/enable/disable> command
togglepvp.command.toggle.others: preventstabby.command.toggle.others:
default: op default: op
description: Allows usage of /pvp <toggle/enable/disable> <player> command description: Allows usage of /pvp <toggle/enable/disable> <player> command
togglepvp.command.reload: preventstabby.command.reload:
default: op default: op
description: Allows usage of /pvp reload command description: Allows usage of /pvp reload command