mirror of
https://github.com/HibiscusMC/HibiscusCommons.git
synced 2025-12-19 15:09:26 +00:00
refactor: move even more packets from hmcc to nms
This commit is contained in:
@@ -3,18 +3,20 @@ package me.lojosho.hibiscuscommons.nms.v1_21_R3;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.mojang.authlib.GameProfile;
|
||||
import com.mojang.datafixers.util.Pair;
|
||||
import com.mojang.serialization.JsonOps;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import io.papermc.paper.adventure.PaperAdventure;
|
||||
import it.unimi.dsi.fastutil.ints.IntList;
|
||||
import me.lojosho.hibiscuscommons.HibiscusCommonsPlugin;
|
||||
import me.lojosho.hibiscuscommons.util.AdventureUtils;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
||||
import net.kyori.adventure.text.serializer.json.JSONComponentSerializer;
|
||||
import net.minecraft.advancements.*;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.advancements.Advancement;
|
||||
import net.minecraft.advancements.AdvancementHolder;
|
||||
import net.minecraft.network.protocol.Packet;
|
||||
import net.minecraft.network.protocol.game.*;
|
||||
import net.minecraft.network.syncher.EntityDataSerializer;
|
||||
import net.minecraft.network.syncher.EntityDataSerializers;
|
||||
import net.minecraft.network.syncher.SynchedEntityData;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
@@ -26,7 +28,7 @@ import net.minecraft.world.entity.EquipmentSlot;
|
||||
import net.minecraft.world.entity.PositionMoveRotation;
|
||||
import net.minecraft.world.entity.decoration.ArmorStand;
|
||||
import net.minecraft.world.entity.player.Inventory;
|
||||
import net.minecraft.world.level.portal.TeleportTransition;
|
||||
import net.minecraft.world.level.GameType;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import net.minecraft.world.scores.PlayerTeam;
|
||||
import net.minecraft.world.scores.Team;
|
||||
@@ -35,7 +37,6 @@ import org.bukkit.Color;
|
||||
import org.bukkit.GameMode;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.craftbukkit.CraftEquipmentSlot;
|
||||
import org.bukkit.craftbukkit.entity.CraftEntity;
|
||||
import org.bukkit.craftbukkit.entity.CraftEntityType;
|
||||
import org.bukkit.craftbukkit.entity.CraftPlayer;
|
||||
import org.bukkit.craftbukkit.inventory.CraftItemStack;
|
||||
@@ -44,22 +45,82 @@ import org.bukkit.entity.Display;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.bukkit.entity.ItemDisplay;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.player.PlayerTeleportEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.bukkit.inventory.meta.components.CustomModelDataComponent;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.joml.Quaternionf;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class NMSPackets extends NMSCommon implements me.lojosho.hibiscuscommons.nms.NMSPackets {
|
||||
|
||||
private static ServerLevel level = MinecraftServer.getServer().overworld();
|
||||
private static Entity fakeNmsEntity = new ArmorStand(net.minecraft.world.entity.EntityType.ARMOR_STAND, level);
|
||||
|
||||
@Override @SuppressWarnings("unchecked")
|
||||
public void sendSharedEntityData(int entityId, Map<Integer, Number> dataValues, List<Player> sendTo) {
|
||||
List<SynchedEntityData.DataValue<?>> nmsDataValues = dataValues.entrySet().stream().map(entry -> {
|
||||
int index = entry.getKey();
|
||||
Number value = entry.getValue();
|
||||
return switch (value) {
|
||||
case Byte byteVal -> new SynchedEntityData.DataValue<>(index, EntityDataSerializers.BYTE, byteVal);
|
||||
case Float floatVal -> new SynchedEntityData.DataValue<>(index, EntityDataSerializers.FLOAT, floatVal);
|
||||
case Integer intVal -> new SynchedEntityData.DataValue<>(index, EntityDataSerializers.INT, intVal);
|
||||
default ->
|
||||
throw new IllegalArgumentException("Unsupported data value type: " + value.getClass().getSimpleName());
|
||||
};
|
||||
}).collect(Collectors.toList());
|
||||
|
||||
ClientboundSetEntityDataPacket packet = new ClientboundSetEntityDataPacket(entityId, nmsDataValues);
|
||||
for (Player player : sendTo) sendPacket(player, packet);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendFakePlayerInfoPacket(
|
||||
final Player skinnedPlayer,
|
||||
final int entityId,
|
||||
final UUID uuid,
|
||||
final String npcName,
|
||||
final List<Player> sendTo
|
||||
) {
|
||||
ServerPlayer player = ((CraftPlayer) skinnedPlayer).getHandle();
|
||||
String name = npcName.substring(0, 15);
|
||||
GameProfile profile = new GameProfile(uuid, name);
|
||||
|
||||
Component component = AdventureUtils.MINI_MESSAGE.deserialize(name);
|
||||
net.minecraft.network.chat.Component nmsComponent = HibiscusCommonsPlugin.isOnPaper() ? PaperAdventure.asVanilla(component) : net.minecraft.network.chat.Component.literal(name);
|
||||
|
||||
ClientboundPlayerInfoUpdatePacket.Entry entry = new ClientboundPlayerInfoUpdatePacket.Entry(uuid, profile, false, 0, GameType.CREATIVE, nmsComponent, true, player.listOrder, player.getChatSession().asData());
|
||||
EnumSet<ClientboundPlayerInfoUpdatePacket.Action> actions = EnumSet.of(ClientboundPlayerInfoUpdatePacket.Action.ADD_PLAYER);
|
||||
ClientboundPlayerInfoUpdatePacket packet = new ClientboundPlayerInfoUpdatePacket(actions, entry);
|
||||
for (Player p : sendTo) sendPacket(p, packet);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendPlayerInfoRemovePacket(final UUID uuid, final List<Player> sendTo) {
|
||||
ClientboundPlayerInfoRemovePacket packet = new ClientboundPlayerInfoRemovePacket(List.of(uuid));
|
||||
for (Player player : sendTo) sendPacket(player, packet);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMovePacket(
|
||||
final int entityId,
|
||||
final @NotNull Location from,
|
||||
final @NotNull Location to,
|
||||
final boolean onGround,
|
||||
@NotNull List<Player> sendTo
|
||||
) {
|
||||
byte dx = (byte) (to.getX() - from.getX());
|
||||
byte dy = (byte) (to.getY() - from.getY());
|
||||
byte dz = (byte) (to.getZ() - from.getZ());
|
||||
|
||||
ClientboundMoveEntityPacket.Pos packet = new ClientboundMoveEntityPacket.Pos(entityId, dx, dy, dz, onGround);
|
||||
for (Player p : sendTo) sendPacket(p, packet);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendGamemodeChange(Player player, GameMode gameMode) {
|
||||
ClientboundGameEventPacket.Type type = ClientboundGameEventPacket.CHANGE_GAME_MODE;
|
||||
@@ -78,6 +139,15 @@ public class NMSPackets extends NMSCommon implements me.lojosho.hibiscuscommons.
|
||||
for (Player p : sendTo) sendPacket(p, packet);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendRotationPacket(int entityId, float yaw, float pitch, boolean onGround, List<Player> sendTo) {
|
||||
float ROTATION_FACTOR = 256.0F / 360.0F;
|
||||
yaw = (byte) (yaw * ROTATION_FACTOR);
|
||||
pitch = (byte) (pitch * ROTATION_FACTOR);
|
||||
ClientboundMoveEntityPacket.Rot packet = new ClientboundMoveEntityPacket.Rot(entityId, (byte) yaw, (byte) pitch, onGround);
|
||||
for (Player p : sendTo) sendPacket(p, packet);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendRotationPacket(int entityId, Location location, boolean onGround, List<Player> sendTo) {
|
||||
float ROTATION_FACTOR = 256.0F / 360.0F;
|
||||
|
||||
Reference in New Issue
Block a user