mirror of
https://github.com/GeyserExtensionists/GeyserUtils.git
synced 2025-12-19 15:09:24 +00:00
register channel
This commit is contained in:
@@ -1,11 +1,7 @@
|
|||||||
package me.zimzaza4.geyserutils.geyser;
|
package me.zimzaza4.geyserutils.geyser;
|
||||||
|
|
||||||
import com.github.steveice10.mc.protocol.data.game.entity.type.EntityType;
|
|
||||||
import com.github.steveice10.mc.protocol.packet.common.clientbound.ClientboundCustomPayloadPacket;
|
import com.github.steveice10.mc.protocol.packet.common.clientbound.ClientboundCustomPayloadPacket;
|
||||||
import com.github.steveice10.mc.protocol.packet.common.serverbound.ServerboundCustomPayloadPacket;
|
import com.github.steveice10.mc.protocol.packet.common.serverbound.ServerboundCustomPayloadPacket;
|
||||||
import com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundLoginPacket;
|
|
||||||
import com.github.steveice10.mc.protocol.packet.ingame.clientbound.entity.ClientboundDamageEventPacket;
|
|
||||||
import com.github.steveice10.mc.protocol.packet.ingame.clientbound.entity.spawn.ClientboundAddEntityPacket;
|
|
||||||
import com.github.steveice10.packetlib.Session;
|
import com.github.steveice10.packetlib.Session;
|
||||||
import com.github.steveice10.packetlib.event.session.PacketSendingEvent;
|
import com.github.steveice10.packetlib.event.session.PacketSendingEvent;
|
||||||
import com.github.steveice10.packetlib.event.session.SessionAdapter;
|
import com.github.steveice10.packetlib.event.session.SessionAdapter;
|
||||||
@@ -34,7 +30,7 @@ import org.cloudburstmc.protocol.bedrock.packet.*;
|
|||||||
import org.geysermc.event.subscribe.Subscribe;
|
import org.geysermc.event.subscribe.Subscribe;
|
||||||
import org.geysermc.geyser.GeyserImpl;
|
import org.geysermc.geyser.GeyserImpl;
|
||||||
import org.geysermc.geyser.api.bedrock.camera.CameraShake;
|
import org.geysermc.geyser.api.bedrock.camera.CameraShake;
|
||||||
import org.geysermc.geyser.api.event.bedrock.SessionJoinEvent;
|
import org.geysermc.geyser.api.event.bedrock.SessionLoginEvent;
|
||||||
import org.geysermc.geyser.api.event.lifecycle.GeyserPostInitializeEvent;
|
import org.geysermc.geyser.api.event.lifecycle.GeyserPostInitializeEvent;
|
||||||
import org.geysermc.geyser.api.extension.Extension;
|
import org.geysermc.geyser.api.extension.Extension;
|
||||||
import org.geysermc.geyser.entity.type.Entity;
|
import org.geysermc.geyser.entity.type.Entity;
|
||||||
@@ -43,6 +39,7 @@ import org.geysermc.geyser.registry.Registries;
|
|||||||
import org.geysermc.geyser.session.GeyserSession;
|
import org.geysermc.geyser.session.GeyserSession;
|
||||||
import org.geysermc.geyser.skin.SkinProvider;
|
import org.geysermc.geyser.skin.SkinProvider;
|
||||||
import org.geysermc.geyser.util.DimensionUtils;
|
import org.geysermc.geyser.util.DimensionUtils;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import javax.imageio.ImageIO;
|
import javax.imageio.ImageIO;
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
@@ -52,6 +49,7 @@ import java.io.IOException;
|
|||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
public class GeyserUtils implements Extension {
|
public class GeyserUtils implements Extension {
|
||||||
|
|
||||||
@@ -74,6 +72,7 @@ public class GeyserUtils implements Extension {
|
|||||||
loadSkins();
|
loadSkins();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void loadSkins() {
|
public void loadSkins() {
|
||||||
LOADED_SKIN_DATA.clear();
|
LOADED_SKIN_DATA.clear();
|
||||||
File folder = this.dataFolder().resolve("skins").toFile();
|
File folder = this.dataFolder().resolve("skins").toFile();
|
||||||
@@ -117,129 +116,135 @@ public class GeyserUtils implements Extension {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Subscribe
|
@Subscribe
|
||||||
public void onSessionJoin(SessionJoinEvent event) {
|
public void onSessionJoin(SessionLoginEvent event) {
|
||||||
if (event.connection() instanceof GeyserSession session) {
|
if (event.connection() instanceof GeyserSession session) {
|
||||||
session.getDownstream().getSession().addListener(new SessionAdapter() {
|
GeyserImpl.getInstance().getScheduledThread()
|
||||||
@Override
|
.schedule(() -> session.getDownstream().getSession().addListener(new SessionAdapter() {
|
||||||
public void packetSending(PacketSendingEvent event) {
|
|
||||||
Packet packet = event.getPacket();
|
|
||||||
if (packet instanceof ServerboundCustomPayloadPacket payloadPacket) {
|
|
||||||
if (payloadPacket.getChannel().equals("minecraft:register")) {
|
|
||||||
String channels = new String(payloadPacket.getData(), StandardCharsets.UTF_8);
|
|
||||||
channels = channels + "\0" + GeyserUtilsChannels.MAIN;
|
|
||||||
event.setPacket(new ServerboundCustomPayloadPacket("minecraft:register", channels.getBytes(StandardCharsets.UTF_8)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void packetReceived(Session tcpSession, Packet packet) {
|
public void packetSending(PacketSendingEvent event) {
|
||||||
|
Packet packet = event.getPacket();
|
||||||
if (packet instanceof ClientboundCustomPayloadPacket payloadPacket) {
|
if (packet instanceof ServerboundCustomPayloadPacket payloadPacket) {
|
||||||
if (payloadPacket.getChannel().equals(GeyserUtilsChannels.MAIN)) {
|
if (payloadPacket.getChannel().equals("minecraft:register")) {
|
||||||
;
|
String channels = new String(payloadPacket.getData(), StandardCharsets.UTF_8);
|
||||||
CustomPayloadPacket customPacket = packetManager.decodePacket(payloadPacket.getData());
|
channels = channels + "\0" + GeyserUtilsChannels.MAIN;
|
||||||
if (customPacket instanceof CameraShakeCustomPayloadPacket cameraShakePacket) {
|
event.setPacket(new ServerboundCustomPayloadPacket("minecraft:register", channels.getBytes(StandardCharsets.UTF_8)));
|
||||||
event.connection().camera().shakeCamera(cameraShakePacket.getIntensity(), cameraShakePacket.getDuration(), CameraShake.values()[cameraShakePacket.getType()]);
|
}
|
||||||
} else if (customPacket instanceof NpcDialogueFormDataCustomPayloadPacket formData) {
|
|
||||||
|
|
||||||
if (formData.action().equals("CLOSE")) {
|
|
||||||
NpcDialogueForm openForm = NpcDialogueForms.getOpenNpcDialogueForms(session);
|
|
||||||
if (openForm != null) {
|
|
||||||
openForm.close(session);
|
|
||||||
}
|
}
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
NpcDialogueForm form = new NpcDialogueForm();
|
@Override
|
||||||
form.title(formData.title())
|
public void packetReceived(Session tcpSession, Packet packet) {
|
||||||
.dialogue(formData.dialogue())
|
|
||||||
.bindEntity(session.getEntityCache().getEntityByJavaId(formData.bindEntity()))
|
|
||||||
.hasNextForm(formData.hasNextForm());
|
|
||||||
|
|
||||||
if (formData.skinData() != null) {
|
if (packet instanceof ClientboundCustomPayloadPacket payloadPacket) {
|
||||||
form.skinData(formData.skinData());
|
if (payloadPacket.getChannel().equals(GeyserUtilsChannels.MAIN)) {
|
||||||
}
|
CustomPayloadPacket customPacket = packetManager.decodePacket(payloadPacket.getData());
|
||||||
|
if (customPacket instanceof CameraShakeCustomPayloadPacket cameraShakePacket) {
|
||||||
|
event.connection().camera().shakeCamera(cameraShakePacket.getIntensity(), cameraShakePacket.getDuration(), CameraShake.values()[cameraShakePacket.getType()]);
|
||||||
|
} else if (customPacket instanceof NpcDialogueFormDataCustomPayloadPacket formData) {
|
||||||
|
|
||||||
|
if (formData.action().equals("CLOSE")) {
|
||||||
|
NpcDialogueForm openForm = NpcDialogueForms.getOpenNpcDialogueForms(session);
|
||||||
|
if (openForm != null) {
|
||||||
|
openForm.close(session);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
NpcDialogueForm form = new NpcDialogueForm();
|
||||||
|
form.title(formData.title())
|
||||||
|
.dialogue(formData.dialogue())
|
||||||
|
.bindEntity(session.getEntityCache().getEntityByJavaId(formData.bindEntity()))
|
||||||
|
.hasNextForm(formData.hasNextForm());
|
||||||
|
|
||||||
|
if (formData.skinData() != null) {
|
||||||
|
form.skinData(formData.skinData());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
List<Button> buttons = new ArrayList<>();
|
List<Button> buttons = new ArrayList<>();
|
||||||
|
|
||||||
if (formData.buttons() != null) {
|
if (formData.buttons() != null) {
|
||||||
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
for (NpcDialogueButton button : formData.buttons()) {
|
for (NpcDialogueButton button : formData.buttons()) {
|
||||||
|
|
||||||
|
|
||||||
int finalI = i;
|
int finalI = i;
|
||||||
buttons.add(new Button(button.text(), button.commands(),
|
buttons.add(new Button(button.text(), button.commands(),
|
||||||
button.mode(), () -> {
|
button.mode(), () -> {
|
||||||
if (button.mode() == NpcDialogueButton.ButtonMode.BUTTON_MODE) {
|
if (button.mode() == NpcDialogueButton.ButtonMode.BUTTON_MODE) {
|
||||||
session.sendDownstreamPacket(new ServerboundCustomPayloadPacket(GeyserUtilsChannels.MAIN, packetManager.encodePacket(new NpcFormResponseCustomPayloadPacket(formData.formId(), finalI))));
|
session.sendDownstreamPacket(new ServerboundCustomPayloadPacket(GeyserUtilsChannels.MAIN, packetManager.encodePacket(new NpcFormResponseCustomPayloadPacket(formData.formId(), finalI))));
|
||||||
|
}
|
||||||
|
}, button.hasNextForm()));
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
form.closeHandler(() -> session.sendDownstreamPacket(new ServerboundCustomPayloadPacket(GeyserUtilsChannels.MAIN, packetManager.encodePacket(new NpcFormResponseCustomPayloadPacket(formData.formId(), -1)))));
|
||||||
|
form.buttons(buttons);
|
||||||
|
|
||||||
|
form.createAndSend(session);
|
||||||
|
|
||||||
|
} else if (customPacket instanceof AnimateEntityCustomPayloadPacket animateEntityCustomPayloadPacket) {
|
||||||
|
AnimateEntityPacket animateEntityPacket = getAnimateEntityPacket(animateEntityCustomPayloadPacket);
|
||||||
|
for (int id : animateEntityCustomPayloadPacket.getEntityJavaIds()) {
|
||||||
|
Entity entity = session.getEntityCache().getEntityByJavaId(id);
|
||||||
|
if (entity != null) {
|
||||||
|
animateEntityPacket.getRuntimeEntityIds().add(entity.getGeyserId());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
session.sendUpstreamPacket(animateEntityPacket);
|
||||||
|
} else if (customPacket instanceof CameraInstructionCustomPayloadPacket cameraInstructionPacket) {
|
||||||
|
if (cameraInstructionPacket.getInstruction() instanceof SetInstruction instruction) {
|
||||||
|
session.camera().sendCameraPosition(Converter.serializeSetInstruction(instruction));
|
||||||
|
session.getCameraData().forceCameraPerspective(Converter.serializeCameraPerspective(instruction.getPreset()));
|
||||||
|
|
||||||
|
} else if (cameraInstructionPacket.getInstruction() instanceof FadeInstruction instruction) {
|
||||||
|
session.camera().sendCameraFade(Converter.serializeFadeInstruction(instruction));
|
||||||
|
} else if (cameraInstructionPacket.getInstruction() instanceof ClearInstruction) {
|
||||||
|
session.camera().clearCameraInstructions();
|
||||||
|
}
|
||||||
|
|
||||||
|
} else if (customPacket instanceof CustomParticleEffectPayloadPacket customParticleEffectPacket) {
|
||||||
|
SpawnParticleEffectPacket spawnParticleEffectPacket = new SpawnParticleEffectPacket();
|
||||||
|
spawnParticleEffectPacket.setDimensionId(DimensionUtils.javaToBedrock(session.getDimension()));
|
||||||
|
spawnParticleEffectPacket.setPosition(Converter.serializePos(customParticleEffectPacket.getPos()));
|
||||||
|
spawnParticleEffectPacket.setIdentifier(customParticleEffectPacket.getParticle().identifier());
|
||||||
|
spawnParticleEffectPacket.setMolangVariablesJson(Optional.ofNullable(customParticleEffectPacket.getParticle().molangVariablesJson()));
|
||||||
|
session.sendUpstreamPacket(spawnParticleEffectPacket);
|
||||||
|
} else if (customPacket instanceof CustomSkinPayloadPacket customSkinPayloadPacket) {
|
||||||
|
if (session.getEntityCache().getEntityByJavaId(customSkinPayloadPacket.getEntityId()) instanceof PlayerEntity player) {
|
||||||
|
SkinProvider.SkinData data = LOADED_SKIN_DATA.get(customSkinPayloadPacket.getSkinId());
|
||||||
|
if (data != null) {
|
||||||
|
sendSkinPacket(session, player, data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (customPacket instanceof CustomHitBoxPacket customHitBoxPacket) {
|
||||||
|
Entity entity = (session.getEntityCache().getEntityByJavaId(customHitBoxPacket.getEntityId()));
|
||||||
|
if (entity != null) {
|
||||||
|
entity.setBoundingBoxHeight(customHitBoxPacket.getHeight());
|
||||||
|
entity.setBoundingBoxWidth(customHitBoxPacket.getWidth());
|
||||||
|
entity.updateBedrockMetadata();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}, button.hasNextForm()));
|
}
|
||||||
i++;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
form.closeHandler(() -> session.sendDownstreamPacket(new ServerboundCustomPayloadPacket(GeyserUtilsChannels.MAIN, packetManager.encodePacket(new NpcFormResponseCustomPayloadPacket(formData.formId(), -1)))));
|
@NotNull
|
||||||
form.buttons(buttons);
|
private static AnimateEntityPacket getAnimateEntityPacket(AnimateEntityCustomPayloadPacket animateEntityCustomPayloadPacket) {
|
||||||
|
AnimateEntityPacket animateEntityPacket = new AnimateEntityPacket();
|
||||||
form.createAndSend(session);
|
animateEntityPacket.setAnimation(animateEntityCustomPayloadPacket.getAnimation());
|
||||||
|
animateEntityPacket.setController(animateEntityCustomPayloadPacket.getController());
|
||||||
} else if (customPacket instanceof AnimateEntityCustomPayloadPacket animateEntityCustomPayloadPacket) {
|
animateEntityPacket.setBlendOutTime(animateEntityCustomPayloadPacket.getBlendOutTime());
|
||||||
AnimateEntityPacket animateEntityPacket = new AnimateEntityPacket();
|
animateEntityPacket.setNextState(animateEntityCustomPayloadPacket.getNextState());
|
||||||
animateEntityPacket.setAnimation(animateEntityCustomPayloadPacket.getAnimation());
|
animateEntityPacket.setStopExpressionVersion(animateEntityCustomPayloadPacket.getStopExpressionVersion());
|
||||||
animateEntityPacket.setController(animateEntityCustomPayloadPacket.getController());
|
animateEntityPacket.setStopExpression(animateEntityCustomPayloadPacket.getStopExpression());
|
||||||
animateEntityPacket.setBlendOutTime(animateEntityCustomPayloadPacket.getBlendOutTime());
|
return animateEntityPacket;
|
||||||
animateEntityPacket.setNextState(animateEntityCustomPayloadPacket.getNextState());
|
|
||||||
animateEntityPacket.setStopExpressionVersion(animateEntityCustomPayloadPacket.getStopExpressionVersion());
|
|
||||||
animateEntityPacket.setStopExpression(animateEntityCustomPayloadPacket.getStopExpression());
|
|
||||||
for (int id : animateEntityCustomPayloadPacket.getEntityJavaIds()) {
|
|
||||||
Entity entity = session.getEntityCache().getEntityByJavaId(id);
|
|
||||||
if (entity != null) {
|
|
||||||
animateEntityPacket.getRuntimeEntityIds().add(entity.getGeyserId());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
session.sendUpstreamPacket(animateEntityPacket);
|
}), 100, TimeUnit.MILLISECONDS);
|
||||||
} else if (customPacket instanceof CameraInstructionCustomPayloadPacket cameraInstructionPacket) {
|
|
||||||
if (cameraInstructionPacket.getInstruction() instanceof SetInstruction instruction) {
|
|
||||||
session.camera().sendCameraPosition(Converter.serializeSetInstruction(instruction));
|
|
||||||
session.getCameraData().forceCameraPerspective(Converter.serializeCameraPerspective(instruction.getPreset()));
|
|
||||||
|
|
||||||
} else if (cameraInstructionPacket.getInstruction() instanceof FadeInstruction instruction) {
|
|
||||||
session.camera().sendCameraFade(Converter.serializeFadeInstruction(instruction));
|
|
||||||
} else if (cameraInstructionPacket.getInstruction() instanceof ClearInstruction) {
|
|
||||||
session.camera().clearCameraInstructions();
|
|
||||||
}
|
|
||||||
|
|
||||||
} else if (customPacket instanceof CustomParticleEffectPayloadPacket customParticleEffectPacket) {
|
|
||||||
SpawnParticleEffectPacket spawnParticleEffectPacket = new SpawnParticleEffectPacket();
|
|
||||||
spawnParticleEffectPacket.setDimensionId(DimensionUtils.javaToBedrock(session.getDimension()));
|
|
||||||
spawnParticleEffectPacket.setPosition(Converter.serializePos(customParticleEffectPacket.getPos()));
|
|
||||||
spawnParticleEffectPacket.setIdentifier(customParticleEffectPacket.getParticle().identifier());
|
|
||||||
spawnParticleEffectPacket.setMolangVariablesJson(Optional.ofNullable(customParticleEffectPacket.getParticle().molangVariablesJson()));
|
|
||||||
session.sendUpstreamPacket(spawnParticleEffectPacket);
|
|
||||||
} else if (customPacket instanceof CustomSkinPayloadPacket customSkinPayloadPacket) {
|
|
||||||
if (session.getEntityCache().getEntityByJavaId(customSkinPayloadPacket.getEntityId()) instanceof PlayerEntity player) {
|
|
||||||
SkinProvider.SkinData data = LOADED_SKIN_DATA.get(customSkinPayloadPacket.getSkinId());
|
|
||||||
if (data != null) {
|
|
||||||
sendSkinPacket(session, player, data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if (customPacket instanceof CustomHitBoxPacket customHitBoxPacket) {
|
|
||||||
Entity entity = (session.getEntityCache().getEntityByJavaId(customHitBoxPacket.getEntityId()));
|
|
||||||
if (entity != null) {
|
|
||||||
entity.setBoundingBoxHeight(customHitBoxPacket.getHeight());
|
|
||||||
entity.setBoundingBoxWidth(customHitBoxPacket.getWidth());
|
|
||||||
entity.updateBedrockMetadata();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user