-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
ProfileCommand.java
133 lines (126 loc) · 4.35 KB
/
ProfileCommand.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package me.nallar.tickthreading.minecraft.commands;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javassist.is.faulty.Timings;
import me.nallar.tickthreading.Log;
import me.nallar.tickthreading.minecraft.TickManager;
import me.nallar.tickthreading.minecraft.TickThreading;
import me.nallar.tickthreading.minecraft.profiling.EntityTickProfiler;
import me.nallar.tickthreading.util.TableFormatter;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.Entity;
import net.minecraft.world.World;
import net.minecraftforge.common.DimensionManager;
public class ProfileCommand extends Command {
public static String name = "profile";
@Override
public String getCommandName() {
return name;
}
@Override
public boolean requireOp() {
return TickThreading.instance.requireOpForProfileCommand;
}
@Override
public void processCommand(final ICommandSender commandSender, List<String> arguments) {
World world = null;
int time_ = 7;
boolean entity_ = false;
boolean location_ = false;
Integer x = null;
Integer z = null;
try {
if (arguments.isEmpty()) {
throw new Exception();
}
entity_ = "e".equals(arguments.get(0));
if ("c".equals(arguments.get(0))) {
entity_ = true;
location_ = true;
if (arguments.size() > 2) {
x = Integer.valueOf(arguments.remove(1));
z = Integer.valueOf(arguments.remove(1));
}
}
if (arguments.size() > 1) {
time_ = Integer.valueOf(arguments.get(1));
}
if (arguments.size() > 2) {
world = DimensionManager.getWorld(Integer.valueOf(arguments.get(2)));
if (world == null) {
throw new NullPointerException();
}
}
} catch (Exception e) {
sendChat(commandSender, "Usage: /profile [type=a/e/(c [x] [z])] [time=7] [dimensionid=current dimension]");
return;
}
final TickManager manager = TickThreading.instance.getManager(world);
final int time = time_;
final boolean entity = entity_;
final boolean location = location_;
if (location) {
world = commandSender instanceof Entity ? ((Entity) commandSender).worldObj : DimensionManager.getWorld(0);
}
final List<World> worlds = new ArrayList<World>();
if (world == null) {
Collections.addAll(worlds, DimensionManager.getWorlds());
} else {
worlds.add(world);
}
final int hashCode = x != null ? manager.getHashCode(x, z) : (commandSender instanceof Entity ? manager.getHashCode((Entity) commandSender) : 0);
if (entity) {
final EntityTickProfiler entityTickProfiler = EntityTickProfiler.ENTITY_TICK_PROFILER;
if (!entityTickProfiler.startProfiling(new Runnable() {
@Override
public void run() {
if (location) {
manager.getEntityRegion(hashCode).profilingEnabled = false;
manager.getTileEntityRegion(hashCode).profilingEnabled = false;
}
sendChat(commandSender, entityTickProfiler.writeData(new TableFormatter(commandSender)).toString());
}
}, location ? ProfilingState.CHUNK : ProfilingState.GLOBAL, time, worlds)) {
sendChat(commandSender, "Someone else is currently profiling.");
}
if (location) {
manager.profilingEnabled = false;
manager.getEntityRegion(hashCode).profilingEnabled = true;
manager.getTileEntityRegion(hashCode).profilingEnabled = true;
}
sendChat(commandSender, "Profiling for " + time + " seconds in " + (world == null ? "all worlds " : Log.name(world)) + (location ? " at " + x + ',' + z : ""));
return;
}
if (Timings.enabled) {
sendChat(commandSender, "Someone else is currently profiling, please wait and try again.");
return;
}
Timings.enabled = true;
Runnable profilingRunnable = new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000 * time);
} catch (InterruptedException ignored) {
}
Timings.enabled = false;
try {
Thread.sleep(100 * time);
} catch (InterruptedException ignored) {
}
sendChat(commandSender, String.valueOf(Timings.writeData(new TableFormatter(commandSender))));
Timings.clear();
}
};
Thread profilingThread = new Thread(profilingRunnable);
profilingThread.setName("TT Profiler");
sendChat(commandSender, "Profiling for " + time + " seconds");
profilingThread.start();
}
public static enum ProfilingState {
NONE,
GLOBAL,
CHUNK
}
}