Skip to content

Commit

Permalink
Reformat code + follow some IDE suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonHorkles committed Apr 13, 2024
1 parent d2d101c commit 2135abb
Show file tree
Hide file tree
Showing 11 changed files with 187 additions and 156 deletions.
141 changes: 77 additions & 64 deletions src/main/java/net/silverstonemc/entityclearer/ClearTask.java

Large diffs are not rendered by default.

39 changes: 21 additions & 18 deletions src/main/java/net/silverstonemc/entityclearer/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.FileWriter;
Expand All @@ -21,7 +22,7 @@
public class Commands implements CommandExecutor {
private final JavaPlugin plugin = EntityClearer.getInstance();

public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @NotNull String label, String[] args) {
if (args.length > 0) {
switch (args[0].toLowerCase()) {
case "reload" -> reload(sender);
Expand Down Expand Up @@ -59,6 +60,8 @@ private void reload(CommandSender sender) {
}

private void debug(CommandSender sender) {
LogDebug debug = new LogDebug();

if (LogDebug.debugActive) {
sender.sendMessage(ChatColor.RED + "Debug is already active!");
return;
Expand All @@ -72,14 +75,12 @@ private void debug(CommandSender sender) {
LogDebug.fileId = System.currentTimeMillis();
File file = new File(path.toFile(), "debug-" + LogDebug.fileId + ".yml");

if (!file.createNewFile())
if (file.createNewFile()) LogDebug.debugFile = new FileWriter(file, StandardCharsets.UTF_8, true);
else
sender.sendMessage(ChatColor.RED + "Failed to create debug file! Check console for the debug output.");

else LogDebug.debugFile = new FileWriter(file, StandardCharsets.UTF_8, true);

} catch (IOException e) {
new LogDebug().error("SERVER",
"Failed to create debug file! Check console for more information...");
debug.error("SERVER", "Failed to create debug file! Check console for more information...");
e.printStackTrace();
return;
}
Expand All @@ -88,8 +89,10 @@ private void debug(CommandSender sender) {
// Dump config into debug file
plugin.getLogger().info("Dumping config into debug file...");
try {
Scanner scanner = new Scanner(new File(plugin.getDataFolder(), "config.yml"));
Scanner scanner = new Scanner(new File(plugin.getDataFolder(), "config.yml"),
StandardCharsets.UTF_8);
while (scanner.hasNextLine()) LogDebug.debugFile.write(scanner.nextLine() + "\n");
scanner.close();
LogDebug.debugFile.write("""
Expand All @@ -103,24 +106,24 @@ private void debug(CommandSender sender) {
""");
plugin.getLogger().info("Config file dumped!");
} catch (IOException e) {
new LogDebug().error("SERVER",
"Failed to dump config file! Check console for more information...");
debug.error("SERVER", "Failed to dump config file! Check console for more information...");
e.printStackTrace();
}

// Dump stats
new LogDebug().debug(Level.INFO, "", "");
new LogDebug().debug(Level.INFO, "", "╔══════════════════════════════════════╗");
new LogDebug().debug(Level.INFO, "", "║ INFORMATION ║");
new LogDebug().debug(Level.INFO, "", "╚══════════════════════════════════════╝");
debug.debug(Level.INFO, "", "");
debug.debug(Level.INFO, "", "╔══════════════════════════════════════╗");
debug.debug(Level.INFO, "", "║ INFORMATION ║");
debug.debug(Level.INFO, "", "╚══════════════════════════════════════╝");

new LogDebug().debug(Level.INFO, "", "Plugin version: " + plugin.getDescription().getVersion());
new LogDebug().debug(Level.INFO, "", "Server version: " + Bukkit.getVersion());
new LogDebug().debug(Level.INFO, "", "Java version: " + System.getProperty("java.version"));
debug.debug(Level.INFO, "", "Plugin version: " + plugin.getDescription().getVersion());
debug.debug(Level.INFO, "", "Server version: " + Bukkit.getVersion());
//noinspection AccessOfSystemProperties
debug.debug(Level.INFO, "", "Java version: " + System.getProperty("java.version"));

new LogDebug().debug(Level.INFO, "", "Available world list: ");
debug.debug(Level.INFO, "", "Available world list: ");
for (World world : Bukkit.getWorlds())
new LogDebug().debug(Level.INFO, "", " " + world.getName());
debug.debug(Level.INFO, "", " " + world.getName());

new ClearTask().removeEntitiesPreTask(new ConfigUtils().getWorlds("worlds"), false, false);
}
Expand Down
15 changes: 8 additions & 7 deletions src/main/java/net/silverstonemc/entityclearer/Countdown.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

@SuppressWarnings("DataFlowIssue")
public class Countdown {
public static final HashMap<String, BukkitTask> savedCountingDowns = new HashMap<>();
public static final Map<String, BukkitTask> savedCountingDowns = new HashMap<>();

private final BukkitAudiences bukkitAudiences = EntityClearer.getInstance().getAdventure();
private final JavaPlugin plugin = EntityClearer.getInstance();
Expand All @@ -26,15 +26,15 @@ public void countdown(World world) {
List<Integer> times = getCountdownSorted();
int initialTime = times.get(0);

final int[] timeLeft = {initialTime};
int[] timeLeft = {initialTime};
savedCountingDowns.put(worldName, new BukkitRunnable() {
@Override
public void run() {
if (timeLeft[0] <= 0) {
new ClearTask().removeEntitiesPreTask(new ArrayList<>(Collections.singletonList(world)),
false,
false);
this.cancel();
cancel();
savedCountingDowns.remove(worldName);
return;
}
Expand All @@ -56,8 +56,9 @@ public List<Integer> getCountdownSorted() {
}

public void message(int timeLeft, World world) {
//noinspection ProhibitedExceptionCaught
try {
StringBuilder time = new StringBuilder();
StringBuilder time = new StringBuilder(7);
int divideBy = 1;
if (timeLeft > 60) {
divideBy = 60;
Expand All @@ -74,9 +75,9 @@ public void message(int timeLeft, World world) {
}

} catch (NullPointerException e) {
new LogDebug().error(world.getName(),
"Something went wrong sending messages! Is your config outdated?");
new LogDebug().error(
LogDebug debug = new LogDebug();
debug.error(world.getName(), "Something went wrong sending messages! Is your config outdated?");
debug.error(
world.getName(),
"Please see https://github.com/SilverstoneMC/EntityClearer/blob/main/src/main/resources/config.yml for the most recent config.");

Expand Down
10 changes: 5 additions & 5 deletions src/main/java/net/silverstonemc/entityclearer/EntityClearer.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ public void run() {

@Override
public void onDisable() {
if (this.adventure != null) {
this.adventure.close();
this.adventure = null;
if (adventure != null) {
adventure.close();
adventure = null;
}
}

Expand All @@ -76,9 +76,9 @@ public static EntityClearer getInstance() {
}

public BukkitAudiences getAdventure() {
if (this.adventure == null) throw new IllegalStateException(
if (adventure == null) throw new IllegalStateException(
"Tried to access Adventure when the plugin was disabled!");
return this.adventure;
return adventure;
}

public MythicPlugin getMythicPlugin() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class TabComplete implements TabCompleter {
final List<String> arguments = new ArrayList<>();

public List<String> onTabComplete(CommandSender sender, Command cmd, String label, String[] args) {
public List<String> onTabComplete(CommandSender sender, @NotNull Command cmd, @NotNull String label, @NotNull String[] args) {
if (sender.hasPermission("entityclearer.reload")) if (!arguments.contains("reload")) arguments.add(
"reload");
if (sender.hasPermission("entityclearer.debug")) if (!arguments.contains("debug")) arguments.add(
Expand Down
33 changes: 17 additions & 16 deletions src/main/java/net/silverstonemc/entityclearer/TpsMonitoring.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
import org.bukkit.scheduler.BukkitTask;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;

public class TpsMonitoring {
public static boolean tpsTimerRan = false;
public static boolean tpsTimerRan;
public static BukkitTask savedTpsTask;
public static final ArrayList<Integer> tickList = new ArrayList<>();
public static final List<Integer> tickList = new ArrayList<>();

private final BukkitAudiences bukkitAudiences = EntityClearer.getInstance().getAdventure();
private final JavaPlugin plugin = EntityClearer.getInstance();
Expand All @@ -32,35 +33,35 @@ public void run() {

long now = System.currentTimeMillis();
BukkitRunnable countTicks = new BukkitRunnable() {
int ticks = 0;
int ticks;

@Override
public void run() {
ticks++;
if (now + 1000 <= System.currentTimeMillis()) {
this.cancel();
cancel();
averageTPS(ticks);
}
}

private void averageTPS(int tps) {
tickList.add(tps);
if (tickList.size() > 10) tickList.remove(0);
else return;

int sum = 0;
double average;
for (int x : tickList) sum += x;
average = (double) sum / tickList.size();
tpsLow(average);
}
};
countTicks.runTaskTimer(plugin, 0, 1);
}
};
savedTpsTask = taskTimer.runTaskTimerAsynchronously(plugin, delay, 20);
}

private void averageTPS(int tps) {
tickList.add(tps);
if (tickList.size() > 10) tickList.remove(0);
else return;

int sum = 0;
double average;
for (int x : tickList) sum += x;
average = (double) sum / tickList.size();
tpsLow(average);
}

@SuppressWarnings("DataFlowIssue")
private void tpsLow(double tps) {
// If TPS is not below the threshold, return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,29 @@
import org.bukkit.plugin.java.JavaPlugin;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;

public class ConfigUtils {
public static boolean isAll;

private final JavaPlugin plugin = EntityClearer.getInstance();

public ArrayList<World> getWorlds(String path) {
public Iterable<World> getWorlds(String path) {
LogDebug debug = new LogDebug();

// Add all the worlds to a list
ArrayList<World> worlds = new ArrayList<>();
List<World> worlds = new ArrayList<>();
//noinspection DataFlowIssue
ArrayList<String> keys = new ArrayList<>(plugin.getConfig().getConfigurationSection(path)
.getKeys(false));

if (keys.contains("ALL")) {
new LogDebug().debug(Level.INFO, "", "'ALL' found! Adding all worlds to list...");
debug.debug(Level.INFO, "", "'ALL' found! Adding all worlds to list...");
isAll = true;
worlds.addAll(Bukkit.getWorlds());
} else {
new LogDebug().debug(Level.INFO, "", "Adding all worlds defined in config to list...");
debug.debug(Level.INFO, "", "Adding all worlds defined in config to list...");
isAll = false;

int index = -1;
Expand All @@ -34,7 +37,7 @@ public ArrayList<World> getWorlds(String path) {

// If that world doesn't exist, complain
if (Bukkit.getWorld(world) == null) {
new LogDebug().error(
debug.error(
"SERVER",
"Couldn't find the world \"" + keys.get(index) + "\"! Please double check your config.");
continue;
Expand Down
33 changes: 16 additions & 17 deletions src/main/java/net/silverstonemc/entityclearer/utils/EntityData.java
Original file line number Diff line number Diff line change
@@ -1,60 +1,61 @@
package net.silverstonemc.entityclearer.utils;

import org.bukkit.entity.EntityType;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.jetbrains.annotations.Nullable;

import java.util.logging.Level;

public class EntityData {
private EntityType entityType;
private String mythicMobType = null;
private boolean includeNamed = false;
private boolean includeOccupied = false;
private String mythicMobType;
private boolean includeNamed;
private boolean includeOccupied;

public EntityData(String entityType, String worldName) {
LogDebug debug = new LogDebug();

entityType = entityType.toUpperCase();

// If the entityType includes named
if (entityType.contains("-NAMED")) {
this.includeNamed = true;
includeNamed = true;
entityType = entityType.replace("-NAMED", "");
}

// If the entityType includes occupied
if (entityType.contains("-OCCUPIED")) {
this.includeOccupied = true;
includeOccupied = true;
entityType = entityType.replace("-OCCUPIED", "");
}

// If the entityType includes MythicMob
if (entityType.startsWith("MYTHICMOB:")) {
this.mythicMobType = entityType.replace("MYTHICMOB:", "");
mythicMobType = entityType.replace("MYTHICMOB:", "");
entityType = null;
}

// Validate mob type, or skip if MythicMob (will check later)
if (entityType != null) try {
this.entityType = EntityType.valueOf(entityType);
} catch (IllegalArgumentException e) {
new LogDebug().error(
debug.error(
worldName,
"Couldn't find the entity type \"" + entityType + "\"! Please double check your config.");

if (LogDebug.debugActive) {
new LogDebug().debug(Level.SEVERE, worldName, e.toString());
debug.debug(Level.SEVERE, worldName, e.toString());
for (StackTraceElement ste : e.getStackTrace())
new LogDebug().debug(Level.SEVERE, worldName, ste.toString());
debug.debug(Level.SEVERE, worldName, ste.toString());
} else e.printStackTrace();
}

new LogDebug().debug(
debug.debug(
Level.INFO,
worldName,
"Entity " + (this.entityType != null ? this.entityType : this.mythicMobType) + " is specified with the following properties:");
new LogDebug().debug(Level.INFO, worldName, " Include named: " + this.includeNamed);
new LogDebug().debug(Level.INFO, worldName, " Include occupied: " + this.includeOccupied);
new LogDebug().debug(Level.INFO, worldName, " MythicMob: " + (this.mythicMobType != null));
"Entity " + (this.entityType != null ? this.entityType : mythicMobType) + " is specified with the following properties:");
debug.debug(Level.INFO, worldName, " Include named: " + includeNamed);
debug.debug(Level.INFO, worldName, " Include occupied: " + includeOccupied);
debug.debug(Level.INFO, worldName, " MythicMob: " + (mythicMobType != null));
}

@Nullable
Expand All @@ -67,12 +68,10 @@ public String getMythicMobType() {
return mythicMobType;
}

@NonNull
public boolean includeNamed() {
return includeNamed;
}

@NonNull
public boolean includeOccupied() {
return includeOccupied;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
import org.bukkit.scheduler.BukkitTask;

import java.util.HashMap;
import java.util.Map;

public class KillTimer {
public static final HashMap<String, BukkitTask> savedTimeTillKillTasks = new HashMap<>();
public static final HashMap<String, BukkitTask> savedStartCountdowns = new HashMap<>();
public static final Map<String, BukkitTask> savedTimeTillKillTasks = new HashMap<>();
public static final Map<String, BukkitTask> savedStartCountdowns = new HashMap<>();
public static final HashMap<String, Long> nextKillTask = new HashMap<>();

private final JavaPlugin plugin = EntityClearer.getInstance();
Expand Down
Loading

0 comments on commit 2135abb

Please sign in to comment.