mirror of
https://github.com/YouHaveTrouble/PreventStabby.git
synced 2026-05-12 13:26:56 +00:00
massive changes
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
package me.youhavetrouble.preventstabby.util;
|
||||
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.util.BoundingBox;
|
||||
|
||||
public class BoundingBoxUtil {
|
||||
|
||||
public static BoundingBox getBoundingBox(Location location, double radius) {
|
||||
|
||||
double x1 = location.getX()+radius;
|
||||
double y1 = location.getY()+radius;
|
||||
double z1 = location.getZ()+radius;
|
||||
|
||||
double x2 = location.getX()-radius;
|
||||
double y2 = location.getY()-radius;
|
||||
double z2 = location.getZ()-radius;
|
||||
|
||||
return new BoundingBox(x1, y1, z1, x2, y2, z2);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package me.youhavetrouble.preventstabby.util;
|
||||
|
||||
import me.youhavetrouble.preventstabby.PreventStabby;
|
||||
import me.youhavetrouble.preventstabby.api.event.PlayerEnterCombatEvent;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import java.time.Instant;
|
||||
import java.util.UUID;
|
||||
|
||||
public class CombatTimer {
|
||||
|
||||
public static void refreshPlayersCombatTime(UUID uuid) {
|
||||
try {
|
||||
long now = Instant.now().getEpochSecond();
|
||||
long combattime = PreventStabby.getPlugin().getSmartCache().getPlayerData(uuid).getCombattime();
|
||||
|
||||
Player player = Bukkit.getPlayer(uuid);
|
||||
if (player == null || !player.isOnline()) return;
|
||||
PlayerEnterCombatEvent playerEnterCombatEvent = new PlayerEnterCombatEvent(player);
|
||||
Bukkit.getScheduler().runTask(PreventStabby.getPlugin(), () -> {
|
||||
Bukkit.getPluginManager().callEvent(playerEnterCombatEvent);
|
||||
if (playerEnterCombatEvent.isCancelled()) return;
|
||||
PreventStabby.getPlugin().getPlayerManager().refreshPlayersCombatTime(uuid);
|
||||
|
||||
if (combattime <= now) {
|
||||
PluginMessages.sendActionBar(uuid, PreventStabby.getPlugin().getConfigCache().getEntering_combat());
|
||||
}
|
||||
});
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
public static void refreshPlayersCombatTime(UUID... uuid) {
|
||||
for (UUID id : uuid) {
|
||||
refreshPlayersCombatTime(id);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isInCombat(UUID uuid) {
|
||||
try {
|
||||
return PreventStabby.getPlugin().getPlayerManager().getPlayer(uuid).getCombattime() >= Instant.now().getEpochSecond();
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package me.youhavetrouble.preventstabby.util;
|
||||
|
||||
import me.youhavetrouble.preventstabby.PreventStabby;
|
||||
import me.youhavetrouble.preventstabby.players.PlayerData;
|
||||
|
||||
import java.io.File;
|
||||
import java.sql.*;
|
||||
import java.util.UUID;
|
||||
|
||||
public class DatabaseSQLite {
|
||||
|
||||
private final String url;
|
||||
private final File folder;
|
||||
|
||||
public DatabaseSQLite(String url, File folder) {
|
||||
this.url = url;
|
||||
this.folder = folder;
|
||||
|
||||
}
|
||||
|
||||
public boolean createDatabaseFile() {
|
||||
this.folder.mkdir();
|
||||
try (Connection conn = DriverManager.getConnection(url)) {
|
||||
if (conn != null) {
|
||||
DatabaseMetaData meta = conn.getMetaData();
|
||||
System.out.println("The driver name is " + meta.getDriverName());
|
||||
Statement statement = conn.createStatement();
|
||||
String sql = "CREATE TABLE IF NOT EXISTS `players` (`player_uuid` varchar(36) UNIQUE PRIMARY KEY, `pvpenabled` boolean);";
|
||||
statement.execute(sql);
|
||||
conn.close();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
System.out.println(e.getMessage());
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean testConnection() {
|
||||
Connection conn = null;
|
||||
try {
|
||||
conn = DriverManager.getConnection(url);
|
||||
System.out.println("Connection to SQLite has been established.");
|
||||
if (conn != null) {
|
||||
conn.close();
|
||||
return true;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public PlayerData getPlayerInfo(UUID uuid) {
|
||||
try {
|
||||
Connection conn = DriverManager.getConnection(url);
|
||||
Statement insertnewuser = conn.createStatement();
|
||||
try {
|
||||
String newuserdata = "INSERT OR IGNORE INTO `players` (player_uuid, pvpenabled) VALUES ('" + uuid.toString() + "', " + PreventStabby.getPlugin().getConfigCache().isPvp_enabled_by_default() + ")";
|
||||
insertnewuser.execute(newuserdata);
|
||||
} catch (SQLException e) {
|
||||
if (e.getErrorCode() != 19) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
Statement statement = conn.createStatement();
|
||||
String sql = "SELECT * FROM `players` WHERE `player_uuid` = '" + uuid.toString() + "';";
|
||||
statement.execute(sql);
|
||||
ResultSet result = statement.getResultSet();
|
||||
boolean state = result.getBoolean("pvpenabled");
|
||||
conn.close();
|
||||
return new PlayerData(state);
|
||||
} catch (SQLException throwables) {
|
||||
throwables.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void updatePlayerInfo(UUID uuid, PlayerData data) {
|
||||
try {
|
||||
Connection conn = DriverManager.getConnection(url);
|
||||
Statement insertnewuser = conn.createStatement();
|
||||
try {
|
||||
String newuserdata = "UPDATE `players` SET pvpenabled = "+data.isPvpEnabled()+" WHERE `player_uuid` = '"+uuid.toString()+"';";
|
||||
insertnewuser.execute(newuserdata);
|
||||
} catch (SQLException e) {
|
||||
PreventStabby.getPlugin().getLogger().severe("Error while saving player data!");
|
||||
e.printStackTrace();
|
||||
}
|
||||
} catch (SQLException throwables) {
|
||||
throwables.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package me.youhavetrouble.preventstabby.util;
|
||||
|
||||
import net.md_5.bungee.api.ChatMessageType;
|
||||
import net.md_5.bungee.api.chat.BaseComponent;
|
||||
import net.md_5.bungee.api.chat.TextComponent;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
|
||||
public class PluginMessages {
|
||||
|
||||
public static String parseMessage(String message) {
|
||||
//TODO PAPI support
|
||||
return ChatColor.translateAlternateColorCodes('&', message);
|
||||
}
|
||||
|
||||
public static void sendMessage(Player player, String message) {
|
||||
String parsedMessage = ChatColor.translateAlternateColorCodes('&', message);
|
||||
player.sendMessage(parsedMessage);
|
||||
}
|
||||
|
||||
public static void sendActionBar(Player player, String message) {
|
||||
// TODO use adventure
|
||||
BaseComponent[] component = TextComponent.fromLegacyText(parseMessage(message));
|
||||
player.spigot().sendMessage(ChatMessageType.ACTION_BAR, component);
|
||||
}
|
||||
|
||||
public static void sendActionBar(UUID uuid, String message) {
|
||||
try {
|
||||
Player player = Bukkit.getPlayer(uuid);
|
||||
sendActionBar(player, message);
|
||||
} catch (NullPointerException ignored) {}
|
||||
}
|
||||
|
||||
public static String parsePlayerName(Player player, String message) {
|
||||
message = message.replaceAll("%player%", player.getDisplayName());
|
||||
return parseMessage(message);
|
||||
}
|
||||
|
||||
public static void broadcastMessage(Player player, String message) {
|
||||
message = parsePlayerName(player, message);
|
||||
message = parseMessage(message);
|
||||
BaseComponent[] component = TextComponent.fromLegacyText(parseMessage(message));
|
||||
Bukkit.spigot().broadcast(component);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package me.youhavetrouble.preventstabby.util;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.TYPE})
|
||||
|
||||
public @interface PreventStabbyListener {}
|
||||
@@ -0,0 +1,69 @@
|
||||
package me.youhavetrouble.preventstabby.util;
|
||||
|
||||
import me.youhavetrouble.preventstabby.PreventStabby;
|
||||
import me.youhavetrouble.preventstabby.config.ConfigCache;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.potion.PotionEffectType;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
public class Util {
|
||||
|
||||
private static final HashSet<PotionEffectType> harmfulPotions = new HashSet<>();
|
||||
|
||||
public static void initData() {
|
||||
harmfulPotions.add(PotionEffectType.BLINDNESS);
|
||||
harmfulPotions.add(PotionEffectType.CONFUSION);
|
||||
harmfulPotions.add(PotionEffectType.HARM);
|
||||
harmfulPotions.add(PotionEffectType.HUNGER);
|
||||
harmfulPotions.add(PotionEffectType.POISON);
|
||||
harmfulPotions.add(PotionEffectType.SLOW_DIGGING);
|
||||
harmfulPotions.add(PotionEffectType.WEAKNESS);
|
||||
harmfulPotions.add(PotionEffectType.SLOW);
|
||||
harmfulPotions.add(PotionEffectType.WITHER);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param attacker Player attacking the mount
|
||||
* @param mount Entity being ridden
|
||||
* @return true if event should be cancelled
|
||||
*/
|
||||
public static boolean processMountAttack(UUID attacker, Entity mount) {
|
||||
|
||||
// Don't cancel attacks on players that have passengers
|
||||
if (mount instanceof Player) return false;
|
||||
|
||||
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<>();
|
||||
|
||||
for (Entity passenger : mount.getPassengers()) {
|
||||
if (!(passenger instanceof Player)) continue;
|
||||
Player player = (Player) passenger;
|
||||
if (PreventStabby.getPlugin().getPlayerManager().getPlayerPvPState(player.getUniqueId()))
|
||||
playerPassengersWithPvpEnabled.add(player.getUniqueId());
|
||||
}
|
||||
|
||||
if (playerPassengersWithPvpEnabled.isEmpty()) {
|
||||
PluginMessages.sendActionBar(attacker, config.getCannot_attack_mounts_victim());
|
||||
return true;
|
||||
}
|
||||
|
||||
playerPassengersWithPvpEnabled.forEach(CombatTimer::refreshPlayersCombatTime);
|
||||
CombatTimer.refreshPlayersCombatTime(attacker);
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean isPotionEffectHarmful(PotionEffectType effect) {
|
||||
return harmfulPotions.contains(effect);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user