Tricks
This is my small personal collection of a bunch of random tricks that I discovered and decided to write down.
Get a seeded/persistent random-ish float/double from a UUID#
This is a quick and simple way to get a float/double value between 0 and 1. The number will be almost perfectly random, as long as the UUIDs are random, but won't change for the player who the UUID belongs to. This is great for picking e.g. a color for your specific player.
UUID id = ...
long bits = id.getMostSignificantBits() ^ id.getLeastSignificantBits();
float value = (bits >>> 1) / (float) Long.MAX_VALUE; // 0..1
Cosmetic items#
If you ever want to use a e.g. player head or an armor item, but don't want the players to be able to place or equip them, you can make them cosmetic using the minecraft:item_model component and setting their type to a stick.
public static ItemStack makeCosmeticOnly(ItemStack stack) {
Material originalMaterial = stack.getType();
stack = stack.withType(Material.STICK);
stack.editMeta(meta -> meta.setItemModel(originalMaterial.getKey()));
return stack;
}
This only works on heads for 1.21.6+ because of the new minecraft:profile item component, but will work fine for other items on older versions.
Figure out the caller#
Ever wondered who called your method and wanted to quickly debug it? Just print a stacktrace.
new Exception("optional message").printStackTrace();
Remap your Paper JARs before building container images#
The first time the Paper JAR runs it remaps the server. This process is pretty CPU intensive and can take 10-20 seconds. You can also always use the remapped version of the Paper JAR, but you cannot redistribute this. Anyways, in containerized environments like Kubernetes or Docker, you should remap your Paper JAR when building the image, not when running the container. This will speed up your server boot times significantly and won't waste CPU time. This can however be a bit tricky, since it doesn't seem like Paper provides a proper way of doing this... or do they?
RUN apk add --no-cache dumb-init \
&& chmod +x /app/start.sh \
&& mkdir -p /app/plugins \
# We want to prebuild the patches and remap the server before publishing,
# so we have to run the server, wait until it's done and then exit.
# Conveniently, not agreeing to the EULA does exactly that.
&& java \
-Dcom.mojang.eula.agree=false \
-jar paper.jar \
--nogui
It turns out that not agreeing to the EULA makes the server automatically exit, but right as we remap the server.
Bonus trick: You can set -Dcom.mojang.eula.agree=true instead of manually agreeing to the EULA in eula.txt.