9
0
mirror of https://github.com/GeyserExtensionists/GeyserUtils.git synced 2025-12-19 15:09:24 +00:00
This commit is contained in:
zimzaza4
2024-06-22 21:53:08 +08:00
parent 58285ffd7f
commit e30a9b5f83
2 changed files with 80 additions and 35 deletions

View File

@@ -2,9 +2,6 @@ package me.zimzaza4.geyserutils.geyser;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import it.unimi.dsi.fastutil.bytes.ByteArrays;
@@ -25,7 +22,7 @@ import me.zimzaza4.geyserutils.geyser.replace.JavaAddEntityTranslatorReplace;
import me.zimzaza4.geyserutils.geyser.scoreboard.EntityScoreboard;
import me.zimzaza4.geyserutils.geyser.translator.NPCFormResponseTranslator;
import me.zimzaza4.geyserutils.geyser.util.Converter;
import net.kyori.adventure.key.Key;
import me.zimzaza4.geyserutils.geyser.util.ReflectionUtils;
import org.cloudburstmc.math.vector.Vector3f;
import org.cloudburstmc.nbt.NbtMap;
import org.cloudburstmc.nbt.NbtType;
@@ -67,7 +64,10 @@ import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.level.Clien
import org.jetbrains.annotations.NotNull;
import javax.imageio.ImageIO;
import java.io.*;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.*;
@@ -103,7 +103,7 @@ public class GeyserUtils implements Extension {
Registries.BEDROCK_PACKET_TRANSLATORS.register(NpcRequestPacket.class, new NPCFormResponseTranslator());
loadSkins();
ReflectionUtils.init();
CameraPreset.load();
replaceTranslator();
@@ -145,30 +145,6 @@ public class GeyserUtils implements Extension {
LOADED_ENTITY_DEFINITIONS.put(id, def);
}
public void loadEntities() {
Gson gson = new Gson();
this.dataFolder().toFile().mkdirs();
File file = this.dataFolder().resolve("entities.json").toFile();
if (!file.exists()) {
try {
file.createNewFile();
gson.toJson(new JsonArray(),new FileWriter(file));
} catch (IOException e) {
e.printStackTrace();
}
}
try {
List<String> list = gson.fromJson(new FileReader(file), new TypeToken<List<String>>(){}.getType());
for (String s : list) {
logger().info("Registered: " + s);
addCustomEntity(s);
}
} catch (Exception e) {
}
}
public void replaceTranslator() {
Registries.JAVA_PACKET_TRANSLATORS
@@ -270,10 +246,10 @@ public class GeyserUtils implements Extension {
public void packetSending(PacketSendingEvent event) {
Packet packet = event.getPacket();
if (packet instanceof ServerboundCustomPayloadPacket payloadPacket) {
if (payloadPacket.getChannel().equals("minecraft:register")) {
if (ReflectionUtils.getChannel(payloadPacket).toString().equals("minecraft:register")) {
String channels = new String(payloadPacket.getData(), StandardCharsets.UTF_8);
channels = channels + "\0" + GeyserUtilsChannels.MAIN;
event.setPacket(new ServerboundCustomPayloadPacket(Key.key("minecraft:register"), channels.getBytes(StandardCharsets.UTF_8)));
event.setPacket(ReflectionUtils.buildServerboundPayloadPacket("minecraft:register", channels.getBytes(StandardCharsets.UTF_8)));
}
}
}
@@ -281,7 +257,7 @@ public class GeyserUtils implements Extension {
@Override
public void packetReceived(Session tcpSession, Packet packet) {
if (packet instanceof ClientboundCustomPayloadPacket payloadPacket) {
if (payloadPacket.getChannel().equals(GeyserUtilsChannels.MAIN)) {
if (ReflectionUtils.getChannel(payloadPacket).toString().equals(GeyserUtilsChannels.MAIN)) {
CustomPayloadPacket customPacket = packetManager.decodePacket(payloadPacket.getData());
if (customPacket instanceof CameraShakeCustomPayloadPacket cameraShakePacket) {
session.camera().shakeCamera(cameraShakePacket.getIntensity(), cameraShakePacket.getDuration(), CameraShake.values()[cameraShakePacket.getType()]);
@@ -318,14 +294,14 @@ public class GeyserUtils implements Extension {
buttons.add(new Button(button.text(), button.commands(),
button.mode(), () -> {
if (button.mode() == NpcDialogueButton.ButtonMode.BUTTON_MODE) {
session.sendDownstreamPacket(new ServerboundCustomPayloadPacket(Key.key(GeyserUtilsChannels.MAIN), packetManager.encodePacket(new NpcFormResponseCustomPayloadPacket(formData.formId(), finalI))));
session.sendDownstreamPacket(ReflectionUtils.buildServerboundPayloadPacket(GeyserUtilsChannels.MAIN, packetManager.encodePacket(new NpcFormResponseCustomPayloadPacket(formData.formId(), finalI))));
}
}, button.hasNextForm()));
i++;
}
}
form.closeHandler(() -> session.sendDownstreamPacket(new ServerboundCustomPayloadPacket(Key.key(GeyserUtilsChannels.MAIN), packetManager.encodePacket(new NpcFormResponseCustomPayloadPacket(formData.formId(), -1)))));
form.closeHandler(() -> session.sendDownstreamPacket(ReflectionUtils.buildServerboundPayloadPacket(GeyserUtilsChannels.MAIN, packetManager.encodePacket(new NpcFormResponseCustomPayloadPacket(formData.formId(), -1)))));
form.buttons(buttons);
form.createAndSend(session);

View File

@@ -0,0 +1,69 @@
package me.zimzaza4.geyserutils.geyser.util;
import lombok.SneakyThrows;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.api.util.PlatformType;
import org.geysermc.mcprotocollib.protocol.packet.common.clientbound.ClientboundCustomPayloadPacket;
import org.geysermc.mcprotocollib.protocol.packet.common.serverbound.ServerboundCustomPayloadPacket;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
public class ReflectionUtils {
public static String prefix;
public static Class<?> KEY_CLASS;
public static Class<?> CLIENTBOUND_PAYLOAD_PACKET_CLASS;
public static Class<?> SERVERBOUND_PAYLOAD_PACKET_CLASS;
public static Constructor<?> SERVERBOUND_PAYLOAD_PACKET_CONSTRUCTOR;
public static Method KEY_BUILD_METHOD;
public static Method SERVERBOUND_GET_CHANNEL_METHOD;
public static Method CLIENTBOUND_GET_CHANNEL_METHOD;
@SneakyThrows
public static void init() {
PlatformType type = GeyserImpl.getInstance().platformType();
if (type == PlatformType.STANDALONE) {
prefix = "";
} else {
prefix = "org.geysermc.geyser.platform." + type.platformName().toLowerCase() + ".";
}
CLIENTBOUND_PAYLOAD_PACKET_CLASS = ClientboundCustomPayloadPacket.class;
SERVERBOUND_PAYLOAD_PACKET_CLASS = ServerboundCustomPayloadPacket.class;
KEY_CLASS = Class.forName(prefix + "net.kyori.adventure.key.Key");
CLIENTBOUND_GET_CHANNEL_METHOD = CLIENTBOUND_PAYLOAD_PACKET_CLASS.getMethod("getChannel");
SERVERBOUND_GET_CHANNEL_METHOD = SERVERBOUND_PAYLOAD_PACKET_CLASS.getMethod("getChannel");
try {
SERVERBOUND_PAYLOAD_PACKET_CONSTRUCTOR = SERVERBOUND_PAYLOAD_PACKET_CLASS.getConstructor(KEY_CLASS, byte[].class);
} catch (NoSuchMethodException e) {
SERVERBOUND_PAYLOAD_PACKET_CONSTRUCTOR = SERVERBOUND_PAYLOAD_PACKET_CLASS.getConstructor(String.class, byte[].class);
}
KEY_BUILD_METHOD = KEY_CLASS.getMethod("key", String.class);
}
@SneakyThrows
public static Object getChannel(ClientboundCustomPayloadPacket packet) {
return CLIENTBOUND_GET_CHANNEL_METHOD.invoke(packet);
}
@SneakyThrows
public static Object getChannel(ServerboundCustomPayloadPacket packet) {
return SERVERBOUND_GET_CHANNEL_METHOD.invoke(packet);
}
@SneakyThrows
public static ServerboundCustomPayloadPacket buildServerboundPayloadPacket(String key, byte[] data) {
return (ServerboundCustomPayloadPacket) SERVERBOUND_PAYLOAD_PACKET_CONSTRUCTOR.newInstance(buildKey(key), data);
}
@SneakyThrows
public static Object buildKey(String key) {
return KEY_BUILD_METHOD.invoke(null, key);
}
}