mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2024-09-12 06:48:50 +02:00
Sponge TaskSystem, fix to DeathListener
This commit is contained in:
parent
ef05304efd
commit
e6eac6fa5d
@ -7,6 +7,7 @@ import com.djrapitops.plan.system.processing.processors.player.SpongeKillProcess
|
||||
import com.djrapitops.plan.utilities.MiscUtils;
|
||||
import com.djrapitops.plugin.api.utility.log.Log;
|
||||
import com.djrapitops.plugin.utilities.Format;
|
||||
import org.spongepowered.api.data.key.Keys;
|
||||
import org.spongepowered.api.data.type.HandTypes;
|
||||
import org.spongepowered.api.entity.Entity;
|
||||
import org.spongepowered.api.entity.living.Living;
|
||||
@ -77,9 +78,13 @@ public class SpongeDeathListener {
|
||||
}
|
||||
|
||||
private SpongeKillProcessor handleWolfKill(long time, Living dead, Wolf wolf) {
|
||||
Optional<UUID> creator = wolf.getCreator();
|
||||
Optional<Optional<UUID>> owner = wolf.get(Keys.TAMED_OWNER);
|
||||
|
||||
return creator.map(
|
||||
if (!owner.isPresent()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return owner.get().map(
|
||||
uuid -> new SpongeKillProcessor(uuid, time, dead, "Wolf")
|
||||
).orElse(null);
|
||||
}
|
||||
|
@ -5,17 +5,9 @@
|
||||
package com.djrapitops.plan.system.tasks;
|
||||
|
||||
import com.djrapitops.plan.Plan;
|
||||
import com.djrapitops.plan.system.settings.Settings;
|
||||
import com.djrapitops.plan.system.settings.locale.Locale;
|
||||
import com.djrapitops.plan.system.settings.locale.Msg;
|
||||
import com.djrapitops.plan.system.tasks.bukkit.*;
|
||||
import com.djrapitops.plan.utilities.file.export.HtmlExport;
|
||||
import com.djrapitops.plugin.api.Benchmark;
|
||||
import com.djrapitops.plan.system.tasks.server.BukkitTPSCountTimer;
|
||||
import com.djrapitops.plan.system.tasks.server.PaperTPSCountTimer;
|
||||
import com.djrapitops.plugin.api.Check;
|
||||
import com.djrapitops.plugin.api.TimeAmount;
|
||||
import com.djrapitops.plugin.api.utility.log.Log;
|
||||
import com.djrapitops.plugin.task.ITask;
|
||||
import com.djrapitops.plugin.task.RunnableFactory;
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
/**
|
||||
@ -23,62 +15,18 @@ import org.bukkit.Bukkit;
|
||||
*
|
||||
* @author Rsl1122
|
||||
*/
|
||||
public class BukkitTaskSystem extends TaskSystem {
|
||||
|
||||
private ITask bootAnalysisTask;
|
||||
|
||||
private final Plan plugin;
|
||||
public class BukkitTaskSystem extends ServerTaskSystem {
|
||||
|
||||
public BukkitTaskSystem(Plan plugin) {
|
||||
super(plugin);
|
||||
tpsCountTimer = Check.isPaperAvailable()
|
||||
? new PaperTPSCountTimer(plugin)
|
||||
: new BukkitTPSCountTimer(plugin);
|
||||
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enable() {
|
||||
registerTasks();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disable() {
|
||||
super.disable();
|
||||
Bukkit.getScheduler().cancelTasks(plugin);
|
||||
}
|
||||
|
||||
private void registerTasks() {
|
||||
Benchmark.start("Task Registration");
|
||||
|
||||
// Analysis refresh settings
|
||||
int analysisRefreshMinutes = Settings.ANALYSIS_AUTO_REFRESH.getNumber();
|
||||
boolean analysisRefreshTaskIsEnabled = analysisRefreshMinutes > 0;
|
||||
long analysisPeriod = analysisRefreshMinutes * TimeAmount.MINUTE.ticks();
|
||||
|
||||
Log.info(Locale.get(Msg.ENABLE_BOOT_ANALYSIS_INFO).toString());
|
||||
|
||||
registerTask(tpsCountTimer).runTaskTimer(1000, TimeAmount.SECOND.ticks());
|
||||
registerTask(new NetworkPageRefreshTask()).runTaskTimerAsynchronously(20L, 5L * TimeAmount.MINUTE.ticks());
|
||||
bootAnalysisTask = registerTask(new BootAnalysisTask()).runTaskLaterAsynchronously(30L * TimeAmount.SECOND.ticks());
|
||||
|
||||
if (analysisRefreshTaskIsEnabled) {
|
||||
registerTask(new PeriodicAnalysisTask()).runTaskTimerAsynchronously(analysisPeriod, analysisPeriod);
|
||||
}
|
||||
if (Settings.ANALYSIS_EXPORT.isTrue()) {
|
||||
RunnableFactory.createNew(new HtmlExport(plugin)).runTaskAsynchronously();
|
||||
}
|
||||
Benchmark.stop("Enable", "Task Registration");
|
||||
}
|
||||
|
||||
public void cancelBootAnalysis() {
|
||||
try {
|
||||
if (bootAnalysisTask != null) {
|
||||
bootAnalysisTask.cancel();
|
||||
bootAnalysisTask = null;
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
/* Ignored */
|
||||
}
|
||||
Bukkit.getScheduler().cancelTasks((Plan) plugin);
|
||||
}
|
||||
}
|
@ -6,9 +6,9 @@ package com.djrapitops.plan.system.tasks;
|
||||
|
||||
import com.djrapitops.plan.PlanBungee;
|
||||
import com.djrapitops.plan.system.settings.Settings;
|
||||
import com.djrapitops.plan.system.tasks.bukkit.NetworkPageRefreshTask;
|
||||
import com.djrapitops.plan.system.tasks.bungee.BungeeTPSCountTimer;
|
||||
import com.djrapitops.plan.system.tasks.bungee.EnableConnectionTask;
|
||||
import com.djrapitops.plan.system.tasks.server.NetworkPageRefreshTask;
|
||||
import com.djrapitops.plan.utilities.file.export.HtmlExport;
|
||||
import com.djrapitops.plugin.api.TimeAmount;
|
||||
|
||||
|
@ -0,0 +1,69 @@
|
||||
package com.djrapitops.plan.system.tasks;
|
||||
|
||||
import com.djrapitops.plan.PlanPlugin;
|
||||
import com.djrapitops.plan.system.settings.Settings;
|
||||
import com.djrapitops.plan.system.settings.locale.Locale;
|
||||
import com.djrapitops.plan.system.settings.locale.Msg;
|
||||
import com.djrapitops.plan.system.tasks.server.BootAnalysisTask;
|
||||
import com.djrapitops.plan.system.tasks.server.NetworkPageRefreshTask;
|
||||
import com.djrapitops.plan.system.tasks.server.PeriodicAnalysisTask;
|
||||
import com.djrapitops.plan.utilities.file.export.HtmlExport;
|
||||
import com.djrapitops.plugin.api.Benchmark;
|
||||
import com.djrapitops.plugin.api.TimeAmount;
|
||||
import com.djrapitops.plugin.api.utility.log.Log;
|
||||
import com.djrapitops.plugin.task.ITask;
|
||||
import com.djrapitops.plugin.task.RunnableFactory;
|
||||
|
||||
/**
|
||||
* Abstracted TaskSystem implementation for both Bukkit and Sponge.
|
||||
*
|
||||
* @author Rsl1122
|
||||
*/
|
||||
public class ServerTaskSystem extends TaskSystem {
|
||||
|
||||
protected final PlanPlugin plugin;
|
||||
protected ITask bootAnalysisTask;
|
||||
|
||||
public ServerTaskSystem(PlanPlugin plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enable() {
|
||||
registerTasks();
|
||||
}
|
||||
|
||||
private void registerTasks() {
|
||||
Benchmark.start("Task Registration");
|
||||
|
||||
// Analysis refresh settings
|
||||
int analysisRefreshMinutes = Settings.ANALYSIS_AUTO_REFRESH.getNumber();
|
||||
boolean analysisRefreshTaskIsEnabled = analysisRefreshMinutes > 0;
|
||||
long analysisPeriod = analysisRefreshMinutes * TimeAmount.MINUTE.ticks();
|
||||
|
||||
Log.info(Locale.get(Msg.ENABLE_BOOT_ANALYSIS_INFO).toString());
|
||||
|
||||
registerTask(tpsCountTimer).runTaskTimer(1000, TimeAmount.SECOND.ticks());
|
||||
registerTask(new NetworkPageRefreshTask()).runTaskTimerAsynchronously(20L, 5L * TimeAmount.MINUTE.ticks());
|
||||
bootAnalysisTask = registerTask(new BootAnalysisTask()).runTaskLaterAsynchronously(30L * TimeAmount.SECOND.ticks());
|
||||
|
||||
if (analysisRefreshTaskIsEnabled) {
|
||||
registerTask(new PeriodicAnalysisTask()).runTaskTimerAsynchronously(analysisPeriod, analysisPeriod);
|
||||
}
|
||||
if (Settings.ANALYSIS_EXPORT.isTrue()) {
|
||||
RunnableFactory.createNew(new HtmlExport(plugin)).runTaskAsynchronously();
|
||||
}
|
||||
Benchmark.stop("Enable", "Task Registration");
|
||||
}
|
||||
|
||||
public void cancelBootAnalysis() {
|
||||
try {
|
||||
if (bootAnalysisTask != null) {
|
||||
bootAnalysisTask.cancel();
|
||||
bootAnalysisTask = null;
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
/* Ignored */
|
||||
}
|
||||
}
|
||||
}
|
@ -1,9 +1,22 @@
|
||||
package com.djrapitops.plan.system.tasks;
|
||||
|
||||
public class SpongeTaskSystem extends TaskSystem {
|
||||
import com.djrapitops.plan.PlanSponge;
|
||||
import com.djrapitops.plan.system.tasks.server.SpongeTPSCountTimer;
|
||||
import org.spongepowered.api.Sponge;
|
||||
import org.spongepowered.api.scheduler.Task;
|
||||
|
||||
public class SpongeTaskSystem extends ServerTaskSystem {
|
||||
|
||||
public SpongeTaskSystem(PlanSponge plugin) {
|
||||
super(plugin);
|
||||
tpsCountTimer = new SpongeTPSCountTimer(plugin);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enable() {
|
||||
|
||||
public void disable() {
|
||||
super.disable();
|
||||
for (Task task : Sponge.getScheduler().getScheduledTasks(plugin)) {
|
||||
task.cancel();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.djrapitops.plan.system.tasks.bukkit;
|
||||
package com.djrapitops.plan.system.tasks.server;
|
||||
|
||||
import com.djrapitops.plan.PlanPlugin;
|
||||
import com.djrapitops.plan.system.info.InfoSystem;
|
@ -1,4 +1,4 @@
|
||||
package com.djrapitops.plan.system.tasks.bukkit;
|
||||
package com.djrapitops.plan.system.tasks.server;
|
||||
|
||||
import com.djrapitops.plan.Plan;
|
||||
import com.djrapitops.plan.data.container.TPS;
|
@ -1,4 +1,4 @@
|
||||
package com.djrapitops.plan.system.tasks.bukkit;
|
||||
package com.djrapitops.plan.system.tasks.server;
|
||||
|
||||
import com.djrapitops.plan.system.info.InfoSystem;
|
||||
import com.djrapitops.plan.system.info.connection.WebExceptionLogger;
|
@ -1,4 +1,4 @@
|
||||
package com.djrapitops.plan.system.tasks.bukkit;
|
||||
package com.djrapitops.plan.system.tasks.server;
|
||||
|
||||
import com.djrapitops.plan.Plan;
|
||||
import com.djrapitops.plan.data.container.TPS;
|
@ -1,4 +1,4 @@
|
||||
package com.djrapitops.plan.system.tasks.bukkit;
|
||||
package com.djrapitops.plan.system.tasks.server;
|
||||
|
||||
import com.djrapitops.plan.PlanPlugin;
|
||||
import com.djrapitops.plan.system.info.InfoSystem;
|
@ -0,0 +1,103 @@
|
||||
package com.djrapitops.plan.system.tasks.server;
|
||||
|
||||
import com.djrapitops.plan.PlanSponge;
|
||||
import com.djrapitops.plan.data.container.TPS;
|
||||
import com.djrapitops.plan.data.container.builders.TPSBuilder;
|
||||
import com.djrapitops.plan.system.info.server.ServerInfo;
|
||||
import com.djrapitops.plan.system.tasks.TPSCountTimer;
|
||||
import com.djrapitops.plan.utilities.analysis.MathUtils;
|
||||
import com.djrapitops.plugin.api.utility.log.Log;
|
||||
import org.spongepowered.api.Sponge;
|
||||
import org.spongepowered.api.world.Chunk;
|
||||
import org.spongepowered.api.world.World;
|
||||
|
||||
import java.lang.management.ManagementFactory;
|
||||
import java.lang.management.OperatingSystemMXBean;
|
||||
import java.util.Iterator;
|
||||
|
||||
public class SpongeTPSCountTimer extends TPSCountTimer<PlanSponge> {
|
||||
|
||||
private long lastCheckNano;
|
||||
|
||||
public SpongeTPSCountTimer(PlanSponge plugin) {
|
||||
super(plugin);
|
||||
lastCheckNano = -1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addNewTPSEntry(long nanoTime, long now) {
|
||||
long diff = nanoTime - lastCheckNano;
|
||||
|
||||
lastCheckNano = nanoTime;
|
||||
|
||||
if (diff > nanoTime) { // First run's diff = nanoTime + 1, no calc possible.
|
||||
Log.debug("First run of TPSCountTimer Task.");
|
||||
return;
|
||||
}
|
||||
|
||||
history.add(calculateTPS(diff, now));
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the TPS
|
||||
*
|
||||
* @param diff The time difference between the last run and the new run
|
||||
* @param now The time right now
|
||||
* @return the TPS
|
||||
*/
|
||||
private TPS calculateTPS(long diff, long now) {
|
||||
OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
|
||||
int availableProcessors = operatingSystemMXBean.getAvailableProcessors();
|
||||
double averageCPUUsage = MathUtils.round(operatingSystemMXBean.getSystemLoadAverage() / availableProcessors * 100.0);
|
||||
|
||||
if (averageCPUUsage < 0) { // If unavailable, getSystemLoadAverage() returns -1
|
||||
averageCPUUsage = -1;
|
||||
}
|
||||
|
||||
Runtime runtime = Runtime.getRuntime();
|
||||
|
||||
long totalMemory = runtime.totalMemory();
|
||||
long usedMemory = (totalMemory - runtime.freeMemory()) / 1000000;
|
||||
|
||||
double tps = Sponge.getGame().getServer().getTicksPerSecond();
|
||||
int playersOnline = ServerInfo.getServerProperties().getOnlinePlayers();
|
||||
latestPlayersOnline = playersOnline;
|
||||
int loadedChunks = getLoadedChunks();
|
||||
int entityCount = getEntityCount();
|
||||
|
||||
return TPSBuilder.get()
|
||||
.date(now)
|
||||
.tps(tps)
|
||||
.playersOnline(playersOnline)
|
||||
.usedCPU(averageCPUUsage)
|
||||
.usedMemory(usedMemory)
|
||||
.entities(entityCount)
|
||||
.chunksLoaded(loadedChunks)
|
||||
.toTPS();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the amount of loaded chunks
|
||||
*
|
||||
* @return amount of loaded chunks
|
||||
*/
|
||||
private int getLoadedChunks() {
|
||||
int loaded = 0;
|
||||
for (World world : Sponge.getGame().getServer().getWorlds()) {
|
||||
Iterator<Chunk> iterator = world.getLoadedChunks().iterator();
|
||||
while (iterator.hasNext()) {
|
||||
loaded++;
|
||||
}
|
||||
}
|
||||
return loaded;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the amount of entities on the server
|
||||
*
|
||||
* @return amount of entities
|
||||
*/
|
||||
protected int getEntityCount() {
|
||||
return Sponge.getGame().getServer().getWorlds().stream().mapToInt(world -> world.getEntities().size()).sum();
|
||||
}
|
||||
}
|
@ -1,14 +1,12 @@
|
||||
package com.djrapitops.plan.utilities;
|
||||
|
||||
import com.djrapitops.plan.Plan;
|
||||
import com.djrapitops.plan.PlanBungee;
|
||||
import com.djrapitops.plan.PlanPlugin;
|
||||
import com.djrapitops.plan.api.exceptions.database.DBException;
|
||||
import com.djrapitops.plan.system.database.databases.Database;
|
||||
import com.djrapitops.plan.system.settings.Permissions;
|
||||
import com.djrapitops.plan.system.settings.Settings;
|
||||
import com.djrapitops.plan.system.settings.locale.Locale;
|
||||
import com.djrapitops.plan.system.settings.locale.Msg;
|
||||
import com.djrapitops.plugin.api.Check;
|
||||
import com.djrapitops.plugin.api.TimeAmount;
|
||||
import com.djrapitops.plugin.api.utility.log.Log;
|
||||
import com.djrapitops.plugin.command.CommandUtils;
|
||||
@ -142,10 +140,6 @@ public class MiscUtils {
|
||||
}
|
||||
|
||||
public static String getPlanVersion() {
|
||||
if (Check.isBukkitAvailable()) {
|
||||
return Plan.getInstance().getDescription().getVersion();
|
||||
} else {
|
||||
return PlanBungee.getInstance().getDescription().getVersion();
|
||||
}
|
||||
return PlanPlugin.getInstance().getVersion();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user