Code cleanup

This commit is contained in:
Teriuihi 2025-06-22 21:45:51 +02:00
parent c163885345
commit fa14d001da
13 changed files with 102 additions and 140 deletions

View File

@ -8,17 +8,15 @@ import com.alttd.database.Database;
import com.alttd.listeners.*;
import com.alttd.objects.APartType;
import com.alttd.util.Logger;
import lombok.Getter;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
public class AltitudeParticles extends JavaPlugin {
@Getter
public static AltitudeParticles instance;
public static AltitudeParticles getInstance() {
return instance;
}
@Override
public void onLoad() {
instance = this;

View File

@ -14,7 +14,6 @@ import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
@Getter
public class CommandManager implements CommandExecutor, TabExecutor {

View File

@ -3,8 +3,6 @@ package com.alttd.commands.subcommands;
import com.alttd.AltitudeParticles;
import com.alttd.commands.SubCommand;
import com.alttd.config.Config;
import com.alttd.config.DatabaseConfig;
import com.alttd.config.ParticleConfig;
import org.bukkit.command.CommandSender;
import java.util.ArrayList;

View File

@ -55,7 +55,7 @@ abstract class AbstractConfig {
throw new RuntimeException(ex.getCause());
} catch (Exception ex) {
Logger.severe("Error invoking %.", method.toString());
ex.printStackTrace();
Logger.error("Failed to invoke", ex);
}
}
}
@ -69,7 +69,7 @@ abstract class AbstractConfig {
yaml.save(file);
} catch (IOException ex) {
Logger.severe("Could not save %.", file.toString());
ex.printStackTrace();
Logger.error("Failed to save", ex);
}
}
@ -127,4 +127,4 @@ abstract class AbstractConfig {
ConfigurationSection getConfigurationSection(String path) {
return yaml.getConfigurationSection(path);
}
}
}

View File

@ -2,6 +2,7 @@ package com.alttd.config;
import java.io.File;
@SuppressWarnings("unused")
public final class Config extends AbstractConfig {
static Config config;

View File

@ -1,9 +1,8 @@
package com.alttd.config;
import com.alttd.database.Database;
import java.io.File;
@SuppressWarnings("unused")
public class DatabaseConfig extends AbstractConfig {
static DatabaseConfig config;

View File

@ -24,7 +24,7 @@ public class Database {
try {
openConnection();
} catch (SQLException e) {
e.printStackTrace();
Logger.error("Unable to open connection", e);
}
// Tables
@ -48,7 +48,7 @@ public class Database {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
Logger.error("Error while trying to open connection", e);
}
connection = DriverManager.getConnection(
@ -66,7 +66,7 @@ public class Database {
try {
instance.openConnection();
} catch (Exception e) {
e.printStackTrace();
Logger.error("Error while trying to get connection", e);
}
return connection;
@ -74,15 +74,17 @@ public class Database {
private static void createActiveParticlesTable() {
try {
String sql = "CREATE TABLE IF NOT EXISTS active_particles(" +
"uuid VARCHAR(36) NOT NULL, " +
"particle_type VARCHAR(36) NOT NULL, " +
"particle_id VARCHAR(36) NOT NULL, " +
"PRIMARY KEY (uuid, particle_type)" +
")";
String sql = """
CREATE TABLE IF NOT EXISTS active_particles(
uuid VARCHAR(36) NOT NULL,
particle_type VARCHAR(36) NOT NULL,
particle_id VARCHAR(36) NOT NULL,
PRIMARY KEY (uuid, particle_type)
)""";
connection.prepareStatement(sql).executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
Logger.error("Error while trying to create user point table", e);
Logger.severe("Error while trying to create user point table");
Logger.severe("Shutting down AltitudeParticles");
Bukkit.getPluginManager().disablePlugin(AltitudeParticles.getInstance());
@ -91,16 +93,16 @@ public class Database {
private static void createUserSettingsTable() {
try {
String sql = "CREATE TABLE IF NOT EXISTS user_settings(" +
"uuid VARCHAR(36) NOT NULL, " +
"particles_active BIT(1) NOT NULL DEFAULT b'1', " +
"seeing_particles BIT(1) NOT NULL DEFAULT b'1', " +
"PRIMARY KEY (uuid)" +
")";
String sql = """
CREATE TABLE IF NOT EXISTS user_settings(
uuid VARCHAR(36) NOT NULL,
particles_active BIT(1) NOT NULL DEFAULT b'1',
seeing_particles BIT(1) NOT NULL DEFAULT b'1',
PRIMARY KEY (uuid)
)""";
connection.prepareStatement(sql).executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
Logger.severe("Error while trying to create user point table");
Logger.error("Error while trying to create user point table", e);
Logger.severe("Shutting down AltitudeParticles");
Bukkit.getPluginManager().disablePlugin(AltitudeParticles.getInstance());
}

View File

@ -16,44 +16,44 @@ import java.util.UUID;
public class Queries {
public static void setSeeingParticles(UUID uuid, boolean seeingParticles) {
String sql = "UPDATE user_settings " +
"SET seeing_particles = ?" +
"WHERE uuid = ?";
String sql = """
UPDATE user_settings
SET seeing_particles = ?
WHERE uuid = ?""";
try {
PreparedStatement preparedStatement = Database.getConnection().prepareStatement(sql);
try (PreparedStatement preparedStatement = Database.getConnection().prepareStatement(sql)) {
preparedStatement.setInt(1, seeingParticles ? 1 : 0);
preparedStatement.setString(2, uuid.toString());
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
Logger.error(String.format("unable to set seeing particles for %s", uuid.toString()), e);
}
}
public static void setParticlesActive(UUID uuid, boolean particlesActive) {
String sql = "UPDATE user_settings " +
"SET particles_active = ? " +
"WHERE uuid = ?";
String sql = """
UPDATE user_settings
SET particles_active = ?
WHERE uuid = ?""";
try {
PreparedStatement preparedStatement = Database.getConnection().prepareStatement(sql);
try (PreparedStatement preparedStatement = Database.getConnection().prepareStatement(sql)) {
preparedStatement.setInt(1, particlesActive ? 1 : 0);
preparedStatement.setString(2, uuid.toString());
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
Logger.error(String.format("unable to set particles for %s", uuid.toString()), e);
}
}
public static void addParticle(UUID uuid, APartType aPartType, String particleId) {
String sql = "INSERT INTO active_particles (uuid, particle_type, particle_id) " +
"VALUES (?, ?, ?) " +
"ON DUPLICATE KEY UPDATE particle_id = ?";
String sql = """
INSERT INTO active_particles (uuid, particle_type, particle_id)
VALUES (?, ?, ?)
ON DUPLICATE KEY UPDATE particle_id = ?""";
try {
PreparedStatement preparedStatement = Database.getConnection().prepareStatement(sql);
try (PreparedStatement preparedStatement = Database.getConnection().prepareStatement(sql)) {
preparedStatement.setString(1, uuid.toString());
preparedStatement.setString(2, aPartType.getName());
preparedStatement.setString(3, particleId);
@ -61,43 +61,45 @@ public class Queries {
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
Logger.error(String.format("unable to add particle for %s", uuid.toString()), e);
}
}
public static void removeParticle(UUID uuid, APartType aPartType) {
String sql = "DELETE FROM active_particles " +
"WHERE uuid = ? " +
"AND particle_type = ?";
String sql = """
DELETE FROM active_particles
WHERE uuid = ?
AND particle_type = ?""";
try {
PreparedStatement preparedStatement = Database.getConnection().prepareStatement(sql);
try (PreparedStatement preparedStatement = Database.getConnection().prepareStatement(sql)) {
preparedStatement.setString(1, uuid.toString());
preparedStatement.setString(2, aPartType.getName());
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
Logger.error(String.format("unable to remove particles for %s", uuid.toString()), e);
}
}
public static void clearParticles(UUID uuid) {
String sql = "DELETE FROM active_particles " +
"WHERE uuid = ?";
try {
PreparedStatement preparedStatement = Database.getConnection().prepareStatement(sql);
String sql = """
DELETE FROM active_particles
WHERE uuid = ?""";
try (PreparedStatement preparedStatement = Database.getConnection().prepareStatement(sql)) {
preparedStatement.setString(1, uuid.toString());
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
Logger.error(String.format("unable to clear particles for %s", uuid.toString()), e);
}
}
public static PlayerSettings getPlayerSettings(UUID uuid) {
String sql = "SELECT * FROM user_settings WHERE uuid = ?";
try {
PreparedStatement preparedStatement = Database.getConnection().prepareStatement(sql);
String sql = """
SELECT * FROM user_settings
WHERE uuid = ?""";
try (PreparedStatement preparedStatement = Database.getConnection().prepareStatement(sql)) {
preparedStatement.setString(1, uuid.toString());
ResultSet resultSet = preparedStatement.executeQuery();
@ -109,18 +111,18 @@ public class Queries {
return new PlayerSettings(particlesActive, seeingParticles, uuid, activeParticles);
}
} catch (SQLException e) {
e.printStackTrace();
Logger.error(String.format("unable to get player settings for %s", uuid.toString()), e);
}
return createNewPlayerSettings(uuid);
}
private static HashMap<APartType, ParticleSet> getActiveParticles(UUID uuid) {
HashMap<APartType, ParticleSet> activeParticles = new HashMap<>();
String sql = "SELECT * FROM active_particles " +
"WHERE uuid = ?";
String sql = """
SELECT * FROM active_particles
WHERE uuid = ?""";
try {
PreparedStatement preparedStatement = Database.getConnection().prepareStatement(sql);
try (PreparedStatement preparedStatement = Database.getConnection().prepareStatement(sql)) {
preparedStatement.setString(1, uuid.toString());
ResultSet resultSet = preparedStatement.executeQuery();
@ -145,19 +147,19 @@ public class Queries {
activeParticles.put(aPartType, first.get());
}
} catch (SQLException e) {
e.printStackTrace();
Logger.error(String.format("unable to get player settings for %s", uuid.toString()), e);
}
return activeParticles;
}
private static PlayerSettings createNewPlayerSettings(UUID uuid) {
String sql = "INSERT INTO user_settings (uuid, particles_active, seeing_particles)" +
"VALUES (?, ?, ?)" +
"ON DUPLICATE KEY UPDATE particles_active = ?, seeing_particles = ?";
String sql = """
INSERT INTO user_settings (uuid, particles_active, seeing_particles)
VALUES (?, ?, ?)
ON DUPLICATE KEY UPDATE particles_active = ?, seeing_particles = ?""";
try {
PreparedStatement preparedStatement = Database.getConnection().prepareStatement(sql);
try (PreparedStatement preparedStatement = Database.getConnection().prepareStatement(sql)) {
preparedStatement.setString(1, uuid.toString());
preparedStatement.setInt(2, 1);
preparedStatement.setInt(3, 1);
@ -166,7 +168,7 @@ public class Queries {
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
Logger.error(String.format("unable to get player settings for %s", uuid.toString()), e);
}
return new PlayerSettings(true, true, uuid);

View File

@ -19,7 +19,6 @@ import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.List;
import java.util.stream.Collectors;
public class ChooseParticleGUI extends DefaultGUI {
@ -38,7 +37,7 @@ public class ChooseParticleGUI extends DefaultGUI {
super(name);
List<ParticleSet> availableParticles = ParticleStorage.getParticleSets(aPartType).stream()
.filter(particleSet -> player.hasPermission(particleSet.getPackPermission()) || player.hasPermission(particleSet.getPermission()))
.collect(Collectors.toList());
.toList();
PlayerSettings playerSettings = PlayerSettings.getPlayer(player.getUniqueId());
int i = 0;
for (ParticleSet particleSet : availableParticles) {

View File

@ -1,6 +1,8 @@
package com.alttd.objects;
import com.alttd.config.Config;
import lombok.Getter;
import lombok.Setter;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
@ -15,37 +17,19 @@ public enum APartType { //TODO add description?
CLICK_BLOCK("CLICK_BLOCK", "Right click block", Material.DIAMOND_BLOCK, TimeUnit.SECONDS.toMillis(Config.CLICK_BLOCK_COOL_DOWN), null, true),
TELEPORT_ARRIVE("TELEPORT", "Teleport", Material.DRAGON_EGG, TimeUnit.SECONDS.toMillis(Config.TELEPORT_ARRIVE_COOL_DOWN), null, true);
@Getter
private final String name;
@Getter
private final String displayName;
@Getter
private final Material material;
@Getter
private final long delay;
@Setter
@Getter
private ItemStack itemStack;
private final boolean event;
public String getName() {
return name;
}
public String getDisplayName() {
return displayName;
}
public Material getMaterial() {
return material;
}
public long getDelay() {
return delay;
}
public ItemStack getItemStack() {
return itemStack;
}
public void setItemStack(ItemStack itemStack) {
this.itemStack = itemStack;
}
public boolean hasEvent() {
return event;
}

View File

@ -1,8 +1,6 @@
package com.alttd.objects;
import com.alttd.config.Config;
import com.alttd.storage.PlayerSettings;
import com.alttd.util.Logger;
import org.bukkit.Bukkit;
import org.bukkit.Location;
@ -24,18 +22,18 @@ public class Frame {
*/
public void spawn(Location location, float rotation) {
Location tmpLocation = location.clone();
AParticles.forEach(AParticle -> {
AParticles.forEach(aParticle -> {
ThreadLocalRandom current = ThreadLocalRandom.current();
double offsetX = ((AParticle.offset_range() == 0) ? 0 : current.nextDouble(-AParticle.offset_range(), AParticle.offset_range()));
double offsetZ = ((AParticle.offset_range() == 0) ? 0 : current.nextDouble(-AParticle.offset_range(), AParticle.offset_range()));
double offsetY = ((AParticle.offset_range() == 0) ? 0 : current.nextDouble(-AParticle.offset_range(), AParticle.offset_range()));
XZ xz = new XZ(location.getX(), location.getX() + AParticle.x() + offsetX,
location.getZ(), location.getZ() + AParticle.z() + offsetZ,
double offsetX = ((aParticle.offset_range() == 0) ? 0 : current.nextDouble(-aParticle.offset_range(), aParticle.offset_range()));
double offsetZ = ((aParticle.offset_range() == 0) ? 0 : current.nextDouble(-aParticle.offset_range(), aParticle.offset_range()));
double offsetY = ((aParticle.offset_range() == 0) ? 0 : current.nextDouble(-aParticle.offset_range(), aParticle.offset_range()));
XZ xz = new XZ(location.getX(), location.getX() + aParticle.x() + offsetX,
location.getZ(), location.getZ() + aParticle.z() + offsetZ,
rotation);
AParticle.particleBuilder()
aParticle.particleBuilder()
.location(tmpLocation.set(
xz.getRotatedX(),
location.getY() + AParticle.y() + offsetY,
location.getY() + aParticle.y() + offsetY,
xz.getRotatedZ()))
.receivers(Bukkit.getOnlinePlayers().stream()
.filter(player -> {
@ -51,7 +49,7 @@ public class Frame {
});
}
private class XZ {
private static class XZ {
private final double cx, cz; //Coordinates to rotate around
private double x, z; //Coordinated to rotate

View File

@ -7,6 +7,7 @@ import com.alttd.frameSpawners.FrameSpawnerPlayer;
import com.alttd.storage.PlayerSettings;
import com.alttd.util.Logger;
import de.myzelyam.api.vanish.VanishAPI;
import lombok.Getter;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.GameMode;
import org.bukkit.Location;
@ -22,10 +23,14 @@ public class ParticleSet {
private final List<Frame> frames;
private final int frameDelay, repeat, repeatDelay;
@Getter
private final APartType aPartType;
private final String uniqueId;
@Getter
private final String permission;
@Getter
private final String packPermission;
@Getter
private final ItemStack itemStack;
private final boolean stationary;
@ -77,22 +82,6 @@ public class ParticleSet {
return true;
}
public APartType getAPartType() {
return aPartType;
}
public String getPermission() {
return permission;
}
public String getPackPermission() {
return packPermission;
}
public ItemStack getItemStack() {
return itemStack;
}
public String getParticleId() {
return uniqueId;
}

View File

@ -3,6 +3,7 @@ package com.alttd.storage;
import com.alttd.database.Queries;
import com.alttd.objects.APartType;
import com.alttd.objects.ParticleSet;
import lombok.Getter;
import java.util.Date;
import java.util.HashMap;
@ -12,8 +13,12 @@ public class PlayerSettings {
private static final HashMap<UUID, PlayerSettings> playerSettingsMap = new HashMap<>();
private boolean particlesActive, seeingParticles;
private boolean particlesActive;
@Getter
private boolean seeingParticles;
@Getter
private final UUID uuid;
@Getter
private final HashMap<APartType, ParticleSet> particles;
private final HashMap<APartType, Long> lastUsed;
@ -54,23 +59,11 @@ public class PlayerSettings {
return particlesActive;
}
public boolean isSeeingParticles() {
return seeingParticles;
}
public boolean toggleSeeingParticles() {
seeingParticles = !seeingParticles;
return seeingParticles;
}
public UUID getUuid() {
return uuid;
}
public HashMap<APartType, ParticleSet> getParticles() {
return particles;
}
public void addParticle(APartType aPartType, ParticleSet particleSet) {
particles.put(aPartType, particleSet);
}