Skip to content

Commit

Permalink
Added lifetime tracker
Browse files Browse the repository at this point in the history
ported from Carpet TIS Addition 1.20.0
  • Loading branch information
Fallen-Breath committed Jun 21, 2021
1 parent 07d9580 commit f9677df
Show file tree
Hide file tree
Showing 47 changed files with 2,448 additions and 160 deletions.
2 changes: 2 additions & 0 deletions carpetmodSrc/carpet/CarpetServer.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package carpet;

import carpet.commands.lifetime.LifeTimeTracker;
import carpet.helpers.StackTraceDeobfuscator;
import carpet.network.PluginChannelManager;
import carpet.network.ToggleableChannelHandler;
Expand Down Expand Up @@ -69,6 +70,7 @@ public static void onServerLoaded(MinecraftServer server)
public static void onLoadAllWorlds(MinecraftServer server)
{
TickingArea.loadConfig(server);
LifeTimeTracker.attachServer(server);
for (WorldServer world : server.worlds) {
int dim = world.provider.getDimensionType().getId();
try {
Expand Down
7 changes: 6 additions & 1 deletion carpetmodSrc/carpet/CarpetSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public class CarpetSettings
@Rule(desc = "Enables /lazychunkbehavior command", category = COMMANDS, extra = {
"Makes a chunk act like a lazy chunk for entities and falling sand"
})

public static boolean commandEntityInfo = true;

@Rule(desc = "Enables /unload command to inspect chunk unloading order", category = COMMANDS)
Expand Down Expand Up @@ -155,6 +155,11 @@ public class CarpetSettings
@Rule(desc = "Reduces the permition level to kick players for everyone.", category = COMMANDS)
public static boolean publicKick;

@Rule(desc = "Enables /lifetime for tracking entities lifetime etc.", category = COMMANDS, extra = {
"rule optimizedDespawnRange is suggested to be enabled to avoid 0gt immediately despawn spamming"
})
public static boolean commandLifeTime = true;

// ===== CREATIVE TOOLS ===== //

@Rule(desc = "Emerald ore receiving a block update will throw a StackOverflowError, simulating an update suppressor.", category = CREATIVE)
Expand Down
2 changes: 2 additions & 0 deletions carpetmodSrc/carpet/commands/CarpetCommands.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package carpet.commands;

import carpet.CarpetServer;
import carpet.commands.lifetime.CommandLifeTime;
import narcolepticfrog.rsmm.MeterCommand;
import net.minecraft.command.CommandHandler;

Expand All @@ -21,6 +22,7 @@ public static void register(CommandHandler handler) {
handler.registerCommand(new CommandGrow());
handler.registerCommand(new CommandLagSpike());
handler.registerCommand(new CommandLazyChunkBehavior());
handler.registerCommand(new CommandLifeTime());
handler.registerCommand(new CommandLight());
handler.registerCommand(new CommandLoadChunk());
handler.registerCommand(new CommandLog());
Expand Down
214 changes: 214 additions & 0 deletions carpetmodSrc/carpet/commands/lifetime/AbstractTracker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
package carpet.commands.lifetime;

import carpet.CarpetServer;
import carpet.commands.lifetime.utils.GameUtil;
import carpet.utils.Messenger;
import net.minecraft.command.ICommandSender;

public abstract class AbstractTracker
{
private final String name;
private boolean tracking;
private long startTick;
private long startMillis;

public AbstractTracker(String name)
{
this.name = name;
}

/*
* ---------------------
* tracker name things
* ---------------------
*/

public String getName()
{
return this.name;
}

public String getCommandPrefix()
{
return this.name.toLowerCase();
}

// Xxx
public String getTranslatedName()
{
return this.name;
}

// Xxx Tracker
public String getTranslatedNameFull()
{
return String.format("%s Tracker", this.getTranslatedName());
}

/*
* -----------------------
* status / info getters
* -----------------------
*/

public boolean isTracking()
{
return this.tracking;
}

public long getStartMillis()
{
return this.startMillis;
}

public long getStartTick()
{
return this.startTick;
}

/*
* ------------------------
* for command executions
* ------------------------
*/

public int startTracking(ICommandSender source, boolean showFeedback)
{
if (this.isTracking())
{
if (showFeedback)
{
Messenger.m(source, Messenger.c(
"r " + String.format("%s is already running", this.getTranslatedNameFull())
));
}
return 1;
}
this.tracking = true;
this.startTick = GameUtil.getGameTime();
this.startMillis = System.currentTimeMillis();
if (showFeedback)
{
Messenger.m(source, Messenger.c(
"w " + String.format("%s started", this.getTranslatedNameFull())
));
}
this.initTracker();
return 1;
}

public int stopTracking(ICommandSender source, boolean showFeedback)
{
if (source != null)
{
if (this.isTracking())
{
this.reportTracking(source, false);
if (showFeedback)
{
Messenger.m(source, Messenger.c(
"w \n",
"w " + String.format("%s stopped", this.getTranslatedNameFull())
));
}
}
else if (showFeedback)
{
Messenger.m(source, Messenger.c(
"r " + String.format("%s has not started", this.getTranslatedNameFull())
));
}
}
this.tracking = false;
return 1;
}

public int restartTracking(ICommandSender source)
{
boolean wasTracking = this.isTracking();
this.stopTracking(source, false);
this.startTracking(source, false);
if (wasTracking)
{
source.sendMessage(Messenger.s(null, " "));
}
Messenger.m(source, Messenger.s(null, String.format("%s restarted", this.getTranslatedNameFull())));
return 1;
}

protected int doWhenTracking(ICommandSender source, Runnable runnable)
{
if (this.isTracking())
{
runnable.run();
}
else
{
Messenger.m(source, Messenger.c(
"r " + String.format("%s has not started", this.getTranslatedNameFull())
));
}
return 1;
}

public int reportTracking(ICommandSender source, boolean realtime)
{
return this.doWhenTracking(source, () -> this.printTrackingResult(source, realtime));
}

/*
* -------
* Utils
* -------
*/

protected long getTrackedTick(boolean realtime)
{
return Math.max(1, realtime ? (System.currentTimeMillis() - this.getStartMillis()) / 50 : GameUtil.getGameTime() - this.getStartTick());
}

// send general header for tracking report and return the processed "ticks"
protected long sendTrackedTime(ICommandSender source, boolean realtime)
{
long ticks = this.getTrackedTick(realtime);
source.sendMessage(Messenger.c(
"w \n",
"g ----------- ",
"w " + this.getTranslatedNameFull(),
"g -----------\n",
String.format(
"w Tracked %.2f min (%s)",
(double)ticks / (20 * 60),
realtime ? "real time" : "in game"
)
));
return ticks;
}

/*
* ------------
* Interfaces
* ------------
*/

// /**
// * Stop tracking, call this when server stops
// * e.g. inside {@link carpet.CarpetServer#onServerClosed}
// */
// public void stop()
// {
// this.stopTracking(null, false);
// }

/**
* Called when the tracker starts tracking
* Go initialize necessary statistics
*/
protected abstract void initTracker();

/**
* Show tracking result to the command source
* @param realtime use real time or not. if not, use in-game time
*/
protected abstract void printTrackingResult(ICommandSender source, boolean realtime);
}
Loading

0 comments on commit f9677df

Please sign in to comment.