9
0
mirror of https://github.com/GeyserExtensionists/GeyserUtils.git synced 2025-12-19 15:09:24 +00:00

custom entity

This commit is contained in:
zimzaza4
2024-04-20 13:26:46 +08:00
parent 48389f2fbf
commit 6537d6f247
3 changed files with 29 additions and 19 deletions

View File

@@ -9,8 +9,12 @@ import lombok.Setter;
@NoArgsConstructor @NoArgsConstructor
@Getter @Getter
@Setter @Setter
public class CustomHitBoxPacket extends CustomPayloadPacket { public class CustomEntityDataPacket extends CustomPayloadPacket {
private int entityId; private int entityId;
private float height;
private float width; private Float height;
private Float width;
private Float scale;
} }

View File

@@ -25,6 +25,8 @@ import me.zimzaza4.geyserutils.geyser.form.element.Button;
import me.zimzaza4.geyserutils.geyser.translator.NPCFormResponseTranslator; import me.zimzaza4.geyserutils.geyser.translator.NPCFormResponseTranslator;
import me.zimzaza4.geyserutils.geyser.util.Converter; import me.zimzaza4.geyserutils.geyser.util.Converter;
import org.cloudburstmc.nbt.NbtMap; import org.cloudburstmc.nbt.NbtMap;
import org.cloudburstmc.protocol.bedrock.data.entity.EntityDataTypes;
import org.cloudburstmc.protocol.bedrock.data.entity.EntityFlag;
import org.cloudburstmc.protocol.bedrock.data.skin.ImageData; import org.cloudburstmc.protocol.bedrock.data.skin.ImageData;
import org.cloudburstmc.protocol.bedrock.data.skin.SerializedSkin; import org.cloudburstmc.protocol.bedrock.data.skin.SerializedSkin;
import org.cloudburstmc.protocol.bedrock.packet.*; import org.cloudburstmc.protocol.bedrock.packet.*;
@@ -32,7 +34,6 @@ 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.command.Command; import org.geysermc.geyser.api.command.Command;
import org.geysermc.geyser.api.command.CommandSource;
import org.geysermc.geyser.api.connection.GeyserConnection; import org.geysermc.geyser.api.connection.GeyserConnection;
import org.geysermc.geyser.api.entity.EntityDefinition; import org.geysermc.geyser.api.entity.EntityDefinition;
import org.geysermc.geyser.api.entity.EntityIdentifier; import org.geysermc.geyser.api.entity.EntityIdentifier;
@@ -43,7 +44,6 @@ import org.geysermc.geyser.api.event.lifecycle.GeyserDefineCommandsEvent;
import org.geysermc.geyser.api.event.lifecycle.GeyserDefineEntitiesEvent; import org.geysermc.geyser.api.event.lifecycle.GeyserDefineEntitiesEvent;
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.EntityDefinitions;
import org.geysermc.geyser.entity.type.Entity; import org.geysermc.geyser.entity.type.Entity;
import org.geysermc.geyser.entity.type.player.PlayerEntity; import org.geysermc.geyser.entity.type.player.PlayerEntity;
import org.geysermc.geyser.registry.Registries; import org.geysermc.geyser.registry.Registries;
@@ -57,10 +57,7 @@ import java.io.*;
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.ConcurrentHashMap; import java.util.concurrent.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class GeyserUtils implements Extension { public class GeyserUtils implements Extension {
@@ -136,7 +133,6 @@ public class GeyserUtils implements Extension {
@Subscribe @Subscribe
public void onEntitiesDefine(GeyserDefineEntitiesEvent event) { public void onEntitiesDefine(GeyserDefineEntitiesEvent event) {
loadEntities(); loadEntities();
for (EntityDefinition value : LOADED_ENTITY_DEFINITIONS.values()) { for (EntityDefinition value : LOADED_ENTITY_DEFINITIONS.values()) {
event.register(value); event.register(value);
} }
@@ -247,7 +243,6 @@ public class GeyserUtils implements Extension {
@Override @Override
public void packetReceived(Session tcpSession, Packet packet) { public void packetReceived(Session tcpSession, Packet packet) {
if (packet instanceof ClientboundCustomPayloadPacket payloadPacket) { if (packet instanceof ClientboundCustomPayloadPacket payloadPacket) {
if (payloadPacket.getChannel().equals(GeyserUtilsChannels.MAIN)) { if (payloadPacket.getChannel().equals(GeyserUtilsChannels.MAIN)) {
CustomPayloadPacket customPacket = packetManager.decodePacket(payloadPacket.getData()); CustomPayloadPacket customPacket = packetManager.decodePacket(payloadPacket.getData());
@@ -339,11 +334,12 @@ public class GeyserUtils implements Extension {
sendSkinPacket(session, player, data); sendSkinPacket(session, player, data);
} }
} }
} else if (customPacket instanceof CustomHitBoxPacket customHitBoxPacket) { } else if (customPacket instanceof CustomEntityDataPacket customEntityDataPacket) {
Entity entity = (session.getEntityCache().getEntityByJavaId(customHitBoxPacket.getEntityId())); Entity entity = (session.getEntityCache().getEntityByJavaId(customEntityDataPacket.getEntityId()));
if (entity != null) { if (entity != null) {
entity.setBoundingBoxHeight(customHitBoxPacket.getHeight()); if (customEntityDataPacket.getHeight() != null) entity.setBoundingBoxHeight(customEntityDataPacket.getHeight());
entity.setBoundingBoxWidth(customHitBoxPacket.getWidth()); if (customEntityDataPacket.getWidth() != null) entity.setBoundingBoxWidth(customEntityDataPacket.getWidth());
if (customEntityDataPacket.getScale() != null) entity.getDirtyMetadata().put(EntityDataTypes.SCALE, customEntityDataPacket.getScale());
entity.updateBedrockMetadata(); entity.updateBedrockMetadata();
} }
} else if (customPacket instanceof CustomEntityPacket customEntityPacket) { } else if (customPacket instanceof CustomEntityPacket customEntityPacket) {
@@ -367,9 +363,8 @@ public class GeyserUtils implements Extension {
@Subscribe @Subscribe
public void onEntitySpawn(ServerSpawnEntityEvent event) { public void onEntitySpawn(ServerSpawnEntityEvent event) {
String def = CUSTOM_ENTITIES.get(event.connection()).getIfPresent(event.entityId()); String def = CUSTOM_ENTITIES.get(event.connection()).getIfPresent(event.entityId());
if (event.entityDefinition().entityIdentifier().identifier().endsWith("bat")) {
System.out.println("GEYSER SPAWN: " + event.entityId()); System.out.println("ID: " + event.entityId() + " Type: " + event.entityDefinition().entityIdentifier().identifier());
}
if (def == null) return; if (def == null) return;
System.out.println("FIND DEF:" + def); System.out.println("FIND DEF:" + def);
event.entityDefinition(LOADED_ENTITY_DEFINITIONS.getOrDefault(def, event.entityDefinition())); event.entityDefinition(LOADED_ENTITY_DEFINITIONS.getOrDefault(def, event.entityDefinition()));

View File

@@ -61,7 +61,18 @@ public class PlayerUtils {
} }
public static void sendCustomHitBox(Player player, Entity entity, float height, float width) { public static void sendCustomHitBox(Player player, Entity entity, float height, float width) {
CustomHitBoxPacket packet = new CustomHitBoxPacket(entity.getEntityId(), height, width); CustomEntityDataPacket packet = new CustomEntityDataPacket();
packet.setEntityId(entity.getEntityId());
packet.setWidth(width);
packet.setHeight(height);
player.sendPluginMessage(GeyserUtils.getInstance(), GeyserUtilsChannels.MAIN, GeyserUtils.getPacketManager().encodePacket(packet));
}
public static void sendCustomScale(Player player, Entity entity, float scale) {
CustomEntityDataPacket packet = new CustomEntityDataPacket();
packet.setEntityId(entity.getEntityId());
packet.setScale(scale);
player.sendPluginMessage(GeyserUtils.getInstance(), GeyserUtilsChannels.MAIN, GeyserUtils.getPacketManager().encodePacket(packet)); player.sendPluginMessage(GeyserUtils.getInstance(), GeyserUtilsChannels.MAIN, GeyserUtils.getPacketManager().encodePacket(packet));
} }