Compare commits

...

16 Commits

Author SHA1 Message Date
YouHaveTrouble 52fb010270 return earlier if player has bypass permission 2022-03-17 21:00:41 +01:00
YouHaveTrouble cd26d621e5 allow damaging mounts with non-player riders 2022-03-17 20:58:39 +01:00
YouHaveTrouble 4c08041ee6 better names for methods 2022-03-17 20:58:12 +01:00
YouHaveTrouble 643511df31 make sure damager is projectile in projectile event 2022-03-17 20:58:03 +01:00
YouHaveTrouble 94f97568d5 update depends 2022-03-17 20:57:01 +01:00
YouHaveTrouble 9a9a44939a prepare for finishing rebranding 2022-01-26 14:41:12 +01:00
YouHaveTrouble 1e7adf398a fix worldguard hook errors 2021-09-18 01:40:11 +02:00
YouHaveTrouble c0ca85c44d add isHooked() back 2021-09-18 00:23:41 +02:00
YouHaveTrouble a86485a089 fix issues with servers not having worldguard 2021-09-18 00:21:19 +02:00
YouHaveTrouble bea0d9c7df make sure mount has player rider before checking for attack cancel 2021-09-17 15:28:34 +02:00
YouHaveTrouble 0eccf1b1af fix unable to attack mounts with no player on them 2021-09-17 15:26:40 +02:00
YouHaveTrouble 2669c538cd remove debug 2 electric boogaloo 2021-08-03 17:45:30 +02:00
YouHaveTrouble 36861084cc remove debug 2021-08-03 17:31:35 +02:00
YouHaveTrouble f182c2c859 add worldguard flag for force pvp 2021-08-03 17:24:17 +02:00
YouHaveTrouble 60b360f170 bump version 2021-07-29 13:37:32 +02:00
YouHaveTrouble 6de41b68bd yeat it kinda fixes the whole thing 2021-07-29 13:34:55 +02:00
18 changed files with 153 additions and 70 deletions
+19 -11
View File
@@ -6,7 +6,7 @@
<groupId>me.youhavetrouble</groupId>
<artifactId>PreventStabby</artifactId>
<version>1.3-pre1</version>
<version>1.4</version>
<packaging>jar</packaging>
<name>PreventStabby</name>
@@ -22,7 +22,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<version>3.8.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
@@ -47,7 +47,6 @@
</relocation>
<relocation>
<pattern>org.bstats</pattern>
<!-- Replace this with your package! -->
<shadedPattern>bstats</shadedPattern>
</relocation>
</relocations>
@@ -74,8 +73,12 @@
<url>https://oss.sonatype.org/content/groups/public/</url>
</repository>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
<id>bs-repo</id>
<url>https://ci.pluginwiki.us/plugin/repository/everything/</url>
</repository>
<repository>
<id>sk89q-repo</id>
<url>https://maven.enginehub.org/repo/</url>
</repository>
</repositories>
@@ -89,25 +92,30 @@
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<version>1.18.22</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.9.12</version>
<version>0.10.2</version>
</dependency>
<dependency>
<groupId>com.github.Thatsmusic99</groupId>
<groupId>com.github.thatsmusic99</groupId>
<artifactId>ConfigurationMaster</artifactId>
<version>v1.0-RC-2</version>
<scope>compile</scope>
<version>v1.0.3</version>
</dependency>
<dependency>
<groupId>org.bstats</groupId>
<artifactId>bstats-bukkit</artifactId>
<version>2.2.1</version>
<version>3.0.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.sk89q.worldguard</groupId>
<artifactId>worldguard-bukkit</artifactId>
<version>7.0.4-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
@@ -2,6 +2,7 @@ package me.youhavetrouble.preventstabby;
import me.youhavetrouble.preventstabby.commands.MainCommand;
import me.youhavetrouble.preventstabby.config.ConfigCache;
import me.youhavetrouble.preventstabby.hooks.WorldGuardHook;
import me.youhavetrouble.preventstabby.players.PlayerManager;
import me.youhavetrouble.preventstabby.players.SmartCache;
import me.youhavetrouble.preventstabby.util.PreventStabbyListener;
@@ -21,9 +22,10 @@ public final class PreventStabby extends JavaPlugin {
@Getter private static PreventStabby plugin;
private ConfigCache configCache;
protected PlayerManager playerManager;
protected DatabaseSQLite sqLite;
protected SmartCache smartCache;
private PlayerManager playerManager;
private DatabaseSQLite sqLite;
private SmartCache smartCache;
private static boolean worldGuardHook;
@Override
public void onEnable() {
@@ -31,7 +33,7 @@ public final class PreventStabby extends JavaPlugin {
Util.initData();
reloadPluginConfig();
File dbFile = new File("plugins/PreventStabby");
sqLite = new DatabaseSQLite("jdbc:sqlite:plugins/PreventStabby/TogglePvP.db", dbFile);
sqLite = new DatabaseSQLite("jdbc:sqlite:plugins/PreventStabby/database.db", dbFile);
sqLite.createDatabaseFile();
if (!sqLite.testConnection()) {
getLogger().severe("Error with accessing database. Check if server has write rights.");
@@ -44,7 +46,7 @@ public final class PreventStabby extends JavaPlugin {
smartCache.runSmartCache();
// Register listeners
Reflections reflections = new Reflections(new String[]{"eu.endermite.preventstabby"});
Reflections reflections = new Reflections((Object[]) new String[]{"me.youhavetrouble.preventstabby"});
Set<Class<?>> listenerClasses = reflections.getTypesAnnotatedWith(PreventStabbyListener.class);
listenerClasses.forEach((listener)-> {
try {
@@ -68,7 +70,18 @@ public final class PreventStabby extends JavaPlugin {
pvpCommand.setExecutor(mainCommand);
pvpCommand.setTabCompleter(mainCommand);
Metrics metrics = new Metrics(this, 10597);
try {
WorldGuardHook.init();
worldGuardHook = true;
} catch (NoClassDefFoundError e) {
worldGuardHook = false;
}
Metrics metrics = new Metrics(this, 14074);
}
public static boolean worldGuardHookEnabled() {
return worldGuardHook;
}
public void reloadPluginConfig() {
@@ -78,7 +91,7 @@ public final class PreventStabby extends JavaPlugin {
public void reloadPluginConfig(CommandSender commandSender) {
getServer().getScheduler().runTaskAsynchronously(this, () -> {
reloadPluginConfig();
commandSender.sendMessage("TogglePvP configuration reloaded.");
commandSender.sendMessage("PreventStabby configuration reloaded.");
});
}
@@ -11,10 +11,8 @@ public class HelpCommand {
public static void help(CommandSender sender, String[] args) {
Bukkit.getScheduler().runTaskAsynchronously(PreventStabby.getPlugin(), () -> {
List<String> helpPage = new ArrayList<>();
helpPage.add(PluginMessages.parseMessage("&b&m &bTogglePvP 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"));
if (sender.hasPermission("preventstabby.command.toggle")) {
helpPage.add(PluginMessages.parseMessage("&9/pvp &b&l[on/off] &f- enables or disables PvP"));
@@ -22,7 +20,6 @@ public class HelpCommand {
}
String[] helpReady = helpPage.toArray(new String[0]);
sender.sendMessage(helpReady);
});
}
@@ -11,9 +11,7 @@ import org.bukkit.entity.Player;
public class PvpToggleCommand {
public static void toggle(CommandSender sender, String[] args) {
Bukkit.getScheduler().runTaskAsynchronously(PreventStabby.getPlugin(), () -> {
if (!sender.hasPermission("preventstabby.command.toggle")) {
String message = ChatColor.translateAlternateColorCodes('&', PreventStabby.getPlugin().getConfigCache().getNo_permission());
sender.sendMessage(message);
@@ -47,7 +45,6 @@ public class PvpToggleCommand {
sender.sendMessage(message);
return;
}
if (sender instanceof Player) {
Player player = (Player) sender;
if (CombatTimer.isInCombat(player.getUniqueId())) {
@@ -8,7 +8,6 @@ import org.bukkit.command.CommandSender;
public class ReloadCommand {
public static void reload(CommandSender sender) {
Bukkit.getScheduler().runTaskAsynchronously(PreventStabby.getPlugin(), () -> {
if (!sender.hasPermission("preventstabby.command.reload")) {
String message = ChatColor.translateAlternateColorCodes('&', PreventStabby.getPlugin().getConfigCache().getNo_permission());
@@ -17,7 +16,6 @@ public class ReloadCommand {
}
PreventStabby.getPlugin().reloadPluginConfig(sender);
});
}
}
@@ -85,9 +85,9 @@ public class ConfigCache {
}
};
configFile.setDescription("PvP toggle that CARES about your pets!");
configFile.addLink("Spigot", "https://www.spigotmc.org/resources/togglepvp.89376/");
configFile.addLink("Source", "https://github.com/YouHaveTrouble/TogglePvP");
configFile.setDescription("Prevent people from getting stabbed!");
configFile.addLink("Spigot", "https://www.spigotmc.org/resources/89376/");
configFile.addLink("Source", "https://github.com/YouHaveTrouble/PreventStabby");
configFile.load();
FileConfiguration config = configFile.getConfig();
@@ -135,8 +135,6 @@ public class ConfigCache {
this.entering_combat = config.getString("messages.entering_combat", "&cEntering 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!");
}
@@ -0,0 +1,63 @@
package me.youhavetrouble.preventstabby.hooks;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.util.Location;
import com.sk89q.worldguard.LocalPlayer;
import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.sk89q.worldguard.protection.ApplicableRegionSet;
import com.sk89q.worldguard.protection.flags.Flag;
import com.sk89q.worldguard.protection.flags.StateFlag;
import com.sk89q.worldguard.protection.flags.registry.FlagConflictException;
import com.sk89q.worldguard.protection.flags.registry.FlagRegistry;
import com.sk89q.worldguard.protection.regions.RegionContainer;
import com.sk89q.worldguard.protection.regions.RegionQuery;
import me.youhavetrouble.preventstabby.PreventStabby;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
public class WorldGuardHook {
private static FlagRegistry flagRegistry;
public static StateFlag FORCE_PVP_FLAG;
public static void init() {
PreventStabby plugin = PreventStabby.getPlugin();
try {
Class.forName("com.sk89q.worldguard.protection.flags.registry.FlagRegistry");
WorldGuardPlugin worldGuardPlugin = WorldGuardPlugin.inst();
if (WorldGuard.getInstance() == null || worldGuardPlugin == null) return;
plugin.getLogger().info("Hooking into WorldGuard");
flagRegistry = WorldGuard.getInstance().getFlagRegistry();
createForcePvpFlag(plugin);
} catch (NoClassDefFoundError | ClassNotFoundException ignored) {
}
}
private static void createForcePvpFlag(Plugin plugin) {
if (flagRegistry == null) return;
String flagName = "preventstabby-force-pvp";
try {
StateFlag flag = new StateFlag(flagName, false);
flagRegistry.register(flag);
FORCE_PVP_FLAG = flag;
} catch (FlagConflictException e) {
Flag<?> existing = flagRegistry.get(flagName);
if (existing instanceof StateFlag) {
FORCE_PVP_FLAG = (StateFlag) existing;
} else {
plugin.getLogger().severe("There is a conflict between flag names!");
}
}
}
public static boolean isPlayerForcedToPvp(Player player) {
RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer();
RegionQuery query = container.createQuery();
org.bukkit.Location loc = player.getLocation();
LocalPlayer localPlayer = WorldGuardPlugin.inst().wrapPlayer(player);
ApplicableRegionSet set = query.getApplicableRegions(new Location(BukkitAdapter.adapt(loc.getWorld()), loc.getX(), loc.getY(), loc.getZ()));
return set.testState(localPlayer, FORCE_PVP_FLAG);
}
}
@@ -15,7 +15,7 @@ import java.util.UUID;
public class MountHitByExplosionListener implements Listener {
@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.getCause().equals(EntityDamageEvent.DamageCause.ENTITY_EXPLOSION)) return;
UUID damager = event.getDamager().getUniqueId();
@@ -16,7 +16,7 @@ import java.util.UUID;
public class MountHitByFireworkListener implements Listener {
@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.getCause().equals(EntityDamageEvent.DamageCause.ENTITY_EXPLOSION)) return;
Firework firework = (Firework) event.getDamager();
@@ -15,10 +15,10 @@ import java.util.UUID;
public class PlayerHitMountWithProjectileListener implements Listener {
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onPlayerAttackMount(org.bukkit.event.entity.EntityDamageByEntityEvent event) {
if (!(event.getDamager() instanceof Player) || event.getEntity().getPassengers().isEmpty()) return;
public void onPlayerHitMountWithProjectile(org.bukkit.event.entity.EntityDamageByEntityEvent event) {
if (!(event.getDamager() instanceof Projectile)) return;
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();
UUID damager = event.getDamager().getUniqueId();
if (Util.processMountAttack(damager, entity))
@@ -26,7 +26,7 @@ public class PlayerJoinAndLeaveListener implements Listener {
PlayerData playerData = PreventStabby.getPlugin().getPlayerManager().getPlayer(player.getUniqueId());
long time = Instant.now().getEpochSecond();
if (playerData == null) {
PreventStabby.getPlugin().getPlayerManager().addPlayer(uuid, new PlayerData(false));
PreventStabby.getPlugin().getPlayerManager().addPlayer(uuid, new PlayerData(uuid,false));
Bukkit.getScheduler().runTaskAsynchronously(PreventStabby.getPlugin(), () -> {
PlayerData data = PreventStabby.getPlugin().getSqLite().getPlayerInfo(uuid);
PreventStabby.getPlugin().getPlayerManager().addPlayer(uuid, data);
@@ -17,6 +17,9 @@ public class CombatCommandListener implements Listener {
if (!PreventStabby.getPlugin().getConfigCache().isBlock_commands_in_combat())
return;
if (event.getPlayer().hasPermission("toglepvp.combatcommandblock.bypass"))
return;
PlayerManager playerManager = PreventStabby.getPlugin().getPlayerManager();
if (!playerManager.getPlayer(event.getPlayer().getUniqueId()).isInCombat())
@@ -27,9 +30,6 @@ public class CombatCommandListener implements Listener {
if (!PreventStabby.getPlugin().getConfigCache().getCombatBlockedCommands().contains(command))
return;
if (event.getPlayer().hasPermission("toglepvp.combatcommandblock.bypass"))
return;
event.setCancelled(true);
PluginMessages.sendMessage(event.getPlayer(), PreventStabby.getPlugin().getConfigCache().getCant_do_that_during_combat());
@@ -7,11 +7,12 @@ import java.util.UUID;
public class PlayerData {
private UUID playerUuid;
private final UUID playerUuid;
private long cachetime, combattime, loginTimestamp, teleportTimestamp;
private boolean pvpEnabled, lastCombatCheck, inCombat;
public PlayerData(boolean pvpEnabled) {
public PlayerData(UUID playerUuid, boolean pvpEnabled) {
this.playerUuid = playerUuid;
this.pvpEnabled = pvpEnabled;
this.combattime = Instant.now().getEpochSecond()-1;
this.loginTimestamp = Instant.now().getEpochSecond()-1;
@@ -3,6 +3,7 @@ package me.youhavetrouble.preventstabby.players;
import me.youhavetrouble.preventstabby.PreventStabby;
import me.youhavetrouble.preventstabby.api.event.PlayerLeaveCombatEvent;
import me.youhavetrouble.preventstabby.config.ConfigCache;
import me.youhavetrouble.preventstabby.hooks.WorldGuardHook;
import me.youhavetrouble.preventstabby.util.CombatTimer;
import me.youhavetrouble.preventstabby.util.PluginMessages;
import lombok.Getter;
@@ -36,8 +37,7 @@ public class PlayerManager {
if (!CombatTimer.isInCombat(uuid)) {
if (playerData.getLastCombatCheck()) {
Player player = Bukkit.getPlayer(uuid);
if (player == null)
return;
if (player == null) continue;
PlayerLeaveCombatEvent playerLeaveCombatEvent = new PlayerLeaveCombatEvent(player);
Bukkit.getScheduler().runTask(PreventStabby.getPlugin(), () -> {
Bukkit.getPluginManager().callEvent(playerLeaveCombatEvent);
@@ -111,18 +111,16 @@ public class PlayerManager {
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;
if (hasLoginProtection(attacker) || hasTeleportProtection(attacker)) return false;
if (checkVictimSpawnProtection && hasLoginProtection(victim)) return false;
if (checkVictimSpawnProtection && hasTeleportProtection(victim)) return false;
SmartCache smartCache = PreventStabby.getPlugin().getSmartCache();
if (!smartCache.getPlayerData(attacker).isPvpEnabled()) {
Player attackerPlayer = Bukkit.getPlayer(attacker);
if (PreventStabby.worldGuardHookEnabled() && WorldGuardHook.isPlayerForcedToPvp(attackerPlayer)) return true;
if (sendDenyMessage) {
ConfigCache config = PreventStabby.getPlugin().getConfigCache();
PluginMessages.sendActionBar(attacker, config.getCannot_attack_attacker());
@@ -130,6 +128,9 @@ public class PlayerManager {
return false;
}
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());
@@ -42,7 +42,7 @@ public class SmartCache {
return playerData;
} catch (NullPointerException ex) {
// Return false if database call fails
return new PlayerData(false);
return new PlayerData(uuid,false);
}
}
}
@@ -53,7 +53,7 @@ public class SmartCache {
PreventStabby.getPlugin().getPlayerManager().getPlayer(uuid).setPvpEnabled(state);
}
// Update the database aswell
PreventStabby.getPlugin().getSqLite().updatePlayerInfo(uuid, new PlayerData(state));
PreventStabby.getPlugin().getSqLite().updatePlayerInfo(uuid, new PlayerData(uuid, state));
}
@@ -64,12 +64,12 @@ public class DatabaseSQLite {
}
}
Statement statement = conn.createStatement();
String sql = "SELECT * FROM `players` WHERE `player_uuid` = '" + uuid.toString() + "';";
String sql = "SELECT * FROM `players` WHERE `player_uuid` = '" + uuid + "';";
statement.execute(sql);
ResultSet result = statement.getResultSet();
boolean state = result.getBoolean("pvpenabled");
conn.close();
return new PlayerData(state);
return new PlayerData(uuid, state);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
@@ -38,20 +38,24 @@ public class Util {
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<>();
boolean hasPlayerPassengers = false;
for (Entity passenger : mount.getPassengers()) {
if (!(passenger instanceof Player)) continue;
Player player = (Player) passenger;
hasPlayerPassengers = true;
if (PreventStabby.getPlugin().getPlayerManager().getPlayerPvPState(player.getUniqueId()))
playerPassengersWithPvpEnabled.add(player.getUniqueId());
}
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());
return true;
+11 -8
View File
@@ -1,29 +1,32 @@
name: TogglePvP
name: PreventStabby
version: ${project.version}
main: eu.endermite.togglepvp.PreventStabby
main: me.youhavetrouble.preventstabby.PreventStabby
authors: [YouHaveTrouble]
api-version: 1.13
description: Stop people from getting stabbed!
soft-depend:
- WorldGuard
commands:
preventstabby:
permission: preventstabby.command
aliases:
- pvp
permissions:
toglepvp.combatcommandblock.bypass:
preventstabby.combatcommandblock.bypass:
default: op
description: Allows to bypass combat command blocker
toglepvp.combatteleportblock.bypass:
preventstabby.combatteleportblock.bypass:
default: op
description: Allows to bypass combat teleport blocker
togglepvp.command:
preventstabby.command:
default: true
description: Allows usage of /pvp command
togglepvp.command.toggle:
preventstabby.command.toggle:
default: true
description: Allows usage of /pvp <toggle/enable/disable> command
togglepvp.command.toggle.others:
preventstabby.command.toggle.others:
default: op
description: Allows usage of /pvp <toggle/enable/disable> <player> command
togglepvp.command.reload:
preventstabby.command.reload:
default: op
description: Allows usage of /pvp reload command