-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
ProfileCommand.java
127 lines (121 loc) · 4.06 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
package me.nallar.tickthreading.minecraft.commands;
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.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 canCommandSenderUseCommand(ICommandSender commandSender) {
return !TickThreading.instance.requireOpForProfileCommand || super.canCommandSenderUseCommand(commandSender);
}
@Override
public void processCommand(final ICommandSender commandSender, List<String> arguments) {
World world = DimensionManager.getWorld(0);
long 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)));
} else if (commandSender instanceof Entity) {
world = ((Entity) commandSender).worldObj;
}
} catch (Exception e) {
world = null;
}
if (world == null) {
sendChat(commandSender, "Usage: /profile [type=a/e/(c [x] [z])] [time=7] [dimensionid=current dimension]");
return;
}
final TickManager manager = TickThreading.instance.getManager(world);
final long time = time_;
final boolean entity = entity_;
final boolean location = location_;
final int hashCode = x != null ? manager.getHashCode(x, z) : (commandSender instanceof Entity ? manager.getHashCode((Entity) commandSender) : 0);
if (entity) {
if (manager.profilingEnabled) {
sendChat(commandSender, "Someone else is currently profiling, please wait and try again.");
return;
}
if (location) {
try {
manager.getEntityRegion(hashCode).profilingEnabled = true;
manager.getTileEntityRegion(hashCode).profilingEnabled = true;
} catch (NullPointerException ignored) {
}
} else {
manager.profilingEnabled = true;
}
} else {
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) {
}
if (entity) {
manager.profilingEnabled = false;
if (location) {
try {
manager.getEntityRegion(hashCode).profilingEnabled = false;
manager.getTileEntityRegion(hashCode).profilingEnabled = false;
} catch (NullPointerException ignored) {
}
}
} else {
Timings.enabled = false;
}
try {
Thread.sleep(100 * time);
} catch (InterruptedException ignored) {
}
if (entity) {
sendChat(commandSender, String.valueOf(manager.entityTickProfiler.writeData(new TableFormatter(commandSender))));
manager.entityTickProfiler.clear();
} else {
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 in " + Log.name(world) + (location ? " at h:" + hashCode : ""));
profilingThread.start();
}
}