Skip to content

Commit 52d8f2c

Browse files
authored
Merge pull request #4 from HaHaWTH/main
General cb performance improvement & dumpitem command
2 parents 874c2b7 + 037c6be commit 52d8f2c

22 files changed

+362
-50
lines changed

patches/minecraft/net/minecraft/world/Explosion.java.patch

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,12 +188,13 @@
188188
- d5 /= d13;
189189
- d7 /= d13;
190190
- d9 /= d13;
191+
- double d14 = (double)this.field_77287_j.func_72842_a(vec3d, entity.func_174813_aQ());
192+
- double d10 = (1.0 - d12) * d14;
193+
- entity.func_70097_a(DamageSource.func_94539_a(this), (float)((int)((d10 * d10 + d10) / 2.0 * 7.0 * (double)f3 + 1.0)));
191194
+ d5 = d5 / d13;
192195
+ d7 = d7 / d13;
193196
+ d9 = d9 / d13;
194-
double d14 = (double)this.field_77287_j.func_72842_a(vec3d, entity.func_174813_aQ());
195-
- double d10 = (1.0 - d12) * d14;
196-
- entity.func_70097_a(DamageSource.func_94539_a(this), (float)((int)((d10 * d10 + d10) / 2.0 * 7.0 * (double)f3 + 1.0)));
197+
+ double d14 = this.getBlockDensity(vec3d, entity.func_174813_aQ()); // Paper - Optimize explosions
197198
+ double d10 = (1.0D - d12) * d14;
198199
+ // entity.attackEntityFrom(DamageSource.causeExplosionDamage(this), (float)((int)((d10 * d10 + d10) / 2.0D * 7.0D * (double)f3 + 1.0D)));
199200
+ CraftEventFactory.entityDamage = field_77283_e;

src/main/java/catserver/server/CatServer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public static String getNativeVersion() {
2828

2929
public static void onServerStart() {
3030
RealtimeThread.INSTANCE.start();
31-
new VersionCheck();
31+
// new VersionCheck(); // CatRoom
3232
}
3333

3434
public static void onServerStop() {

src/main/java/catserver/server/command/internal/CommandCatserver.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,29 @@
11
package catserver.server.command.internal;
22

33
import catserver.server.CatServer;
4+
// CatRoom start - Dump item command
5+
import catserver.server.utils.ItemStackUtils;
6+
import net.md_5.bungee.api.chat.ClickEvent;
7+
import net.md_5.bungee.api.chat.TextComponent;
8+
import net.minecraft.item.ItemStack;
9+
// CatRoom end - Dump item command
410
import net.minecraft.world.World;
511
import net.minecraftforge.common.DimensionManager;
612
import org.bukkit.Bukkit;
713
import org.bukkit.ChatColor;
814
import org.bukkit.command.Command;
915
import org.bukkit.command.CommandSender;
1016
import org.bukkit.craftbukkit.v1_12_R1.CraftServer;
17+
// CatRoom start - Dump item command
18+
import org.bukkit.craftbukkit.v1_12_R1.entity.CraftPlayer;
19+
import org.bukkit.entity.Player;
20+
// CatRoom end - Dump item command
1121

1222
public class CommandCatserver extends Command {
1323
public CommandCatserver(String name) {
1424
super(name);
1525
this.description = "CatServer related commands";
16-
this.usageMessage = "/catserver worlds|reload|reloadall";
26+
this.usageMessage = "/catserver worlds|reload|reloadall|dumpitem"; // CatRoom - Dump item command
1727
setPermission("catserver.command.catserver");
1828
}
1929

@@ -40,6 +50,22 @@ public boolean execute(CommandSender sender, String commandLabel, String[] args)
4050
CatServer.getConfig().loadConfig();
4151
((CraftServer) Bukkit.getServer()).reloadConfig();
4252
sender.sendMessage(ChatColor.GREEN + "Configuration reload complete.");
53+
} else if (args[0].equals("dumpitem")) { // CatRoom start - Dump item command
54+
if (!(sender instanceof Player player)) {
55+
sender.sendMessage(ChatColor.RED + "Only players can use this command.");
56+
return true;
57+
}
58+
var itemInHand = ((CraftPlayer) player).getHandle().getHeldItemMainhand();
59+
if (itemInHand == ItemStack.EMPTY) {
60+
sender.sendMessage(ChatColor.RED + "You are not holding any item.");
61+
return true;
62+
}
63+
sender.sendMessage(ItemStackUtils.formatItemStackToPrettyString(itemInHand));
64+
TextComponent message = new TextComponent("[Click to insert give command]");
65+
message.setColor(net.md_5.bungee.api.ChatColor.GREEN);
66+
message.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, ItemStackUtils.itemStackToGiveCommand(itemInHand)));
67+
sender.spigot().sendMessage(message);
68+
// CatRoom end - Dump item command
4369
}
4470

4571
return true;

src/main/java/catserver/server/command/internal/CommandChunkStats.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,15 @@ public boolean execute(CommandSender sender, String commandLabel, String[] args)
7676

7777
sender.sendMessage("Chunks Time:");
7878
for (ChunkTime chunkTime : chunkList) {
79+
if (chunkTime.chunk == null) continue;
7980
int chunkX = chunkTime.chunk.x;
8081
int chunkZ = chunkTime.chunk.z;
8182
int posX = chunkX << 4;
8283
int posZ = chunkZ << 4;
8384
int time = (int) (chunkTime.time / 1000 / 1000);
8485
int avg = totalTick > 0 ? time / totalTick : 0;
8586

86-
TextComponent component = new TextComponent(String.format("[%s: %d,%d at chunk %d,%d] has running time: %d ms (Arg %d ms/tick)", chunkTime.chunk.world.getWorld().getName(), posX, posZ, chunkX, chunkZ, time, avg));
87+
TextComponent component = new TextComponent(String.format("[%s: %d,%d at chunk %d,%d] has running time: %d ms (Avg %d ms/tick)", chunkTime.chunk.world.getWorld().getName(), posX, posZ, chunkX, chunkZ, time, avg));
8788
component.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, String.format("/minecraft:tp %d 128 %d", posX, posZ)));
8889
component.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new TextComponent[]{new TextComponent(String.format("Execute command: /minecraft:tp %d 128 %d", posX, posZ))}));
8990
sender.spigot().sendMessage(component);

src/main/java/catserver/server/executor/MethodHandleEventExecutor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.lang.invoke.MethodHandles;
1010
import java.lang.reflect.Method;
1111

12+
@Deprecated // CatRoom - We have hidden class executors :3
1213
public class MethodHandleEventExecutor implements EventExecutor {
1314
private final Class<? extends Event> eventClass;
1415
private final MethodHandle handle;

src/main/java/catserver/server/executor/StaticMethodHandleEventExecutor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.lang.reflect.Method;
1212
import java.lang.reflect.Modifier;
1313

14+
@Deprecated // CatRoom - We have hidden class executors :3
1415
public class StaticMethodHandleEventExecutor implements EventExecutor {
1516
private final Class<? extends Event> eventClass;
1617
private final MethodHandle handle;

src/main/java/catserver/server/executor/asm/ASMEventExecutorGenerator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import static org.objectweb.asm.Opcodes.*;
1212

13+
@Deprecated // CatRoom - We have hidden class executors :3
1314
public class ASMEventExecutorGenerator {
1415
public static byte[] generateEventExecutor(Method m, String name) {
1516
ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);

src/main/java/catserver/server/executor/asm/ClassDefiner.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package catserver.server.executor.asm;
22

3+
@Deprecated // CatRoom - We have hidden class executors :3
34
public interface ClassDefiner {
45

56
/**

src/main/java/catserver/server/executor/asm/SafeClassDefiner.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import java.util.concurrent.ConcurrentMap;
77

8+
@Deprecated // CatRoom - We have hidden class executors :3
89
public class SafeClassDefiner implements ClassDefiner {
910
/* default */ static final SafeClassDefiner INSTANCE = new SafeClassDefiner();
1011

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package catserver.server.executor.hiddenclass;
2+
3+
import org.bukkit.event.Event;
4+
import org.bukkit.event.Listener;
5+
import org.bukkit.plugin.EventExecutor;
6+
import java.io.IOException;
7+
import java.io.InputStream;
8+
import java.lang.constant.ConstantDescs;
9+
import java.lang.invoke.MethodHandle;
10+
import java.lang.invoke.MethodHandles;
11+
import java.lang.invoke.MethodType;
12+
import java.lang.reflect.Method;
13+
import java.lang.reflect.Modifier;
14+
import java.util.List;
15+
import java.util.Objects;
16+
17+
public final class BukkitEventExecutorFactory {
18+
private static final byte[] TEMPLATE_CLASS_BYTES;
19+
20+
static {
21+
try (final InputStream is = BukkitEventExecutorFactory.class.getResourceAsStream("MethodHandleEventExecutorTemplate.class")) {
22+
TEMPLATE_CLASS_BYTES = Objects.requireNonNull(is, "template class is missing").readAllBytes();
23+
} catch (IOException e) {
24+
throw new AssertionError(e);
25+
}
26+
}
27+
28+
private BukkitEventExecutorFactory() {
29+
}
30+
31+
/**
32+
* @return an {@link EventExecutor} implemented by a hidden class calling a method handle
33+
*
34+
* @param method the method to be invoked by the created event executor
35+
* @param eventClass the class of the event to handle
36+
*/
37+
public static EventExecutor create(final Method method, final Class<? extends Event> eventClass) {
38+
final List<?> classData = List.of(method, eventClass);
39+
try {
40+
final MethodHandles.Lookup newClass = MethodHandles.lookup().defineHiddenClassWithClassData(TEMPLATE_CLASS_BYTES, classData, true);
41+
return newClass.lookupClass().asSubclass(EventExecutor.class).getDeclaredConstructor().newInstance();
42+
} catch (ReflectiveOperationException e) {
43+
throw new AssertionError(e);
44+
}
45+
}
46+
47+
record ClassData(Method method, MethodHandle methodHandle, Class<? extends Event> eventClass) {
48+
49+
}
50+
51+
/**
52+
* Extracts the class data and creates an adjusted MethodHandle directly usable by the lookup class.
53+
* The logic is kept here to minimize memory usage per created class.
54+
*/
55+
static ClassData classData(final MethodHandles.Lookup lookup) {
56+
try {
57+
final Method method = MethodHandles.classDataAt(lookup, ConstantDescs.DEFAULT_NAME, Method.class, 0);
58+
MethodHandle mh = lookup.unreflect(method);
59+
if (Modifier.isStatic(method.getModifiers())) {
60+
mh = MethodHandles.dropArguments(mh, 0, Listener.class);
61+
}
62+
mh = mh.asType(MethodType.methodType(void.class, Listener.class, Event.class));
63+
final Class<?> eventClass = MethodHandles.classDataAt(lookup, ConstantDescs.DEFAULT_NAME, Class.class, 1);
64+
return new ClassData(method, mh, eventClass.asSubclass(Event.class));
65+
} catch (ReflectiveOperationException e) {
66+
throw new AssertionError(e);
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)