9
0
mirror of https://github.com/HibiscusMC/HibiscusCommons.git synced 2025-12-19 15:09:26 +00:00

feat: add 1.21.4 support

This commit is contained in:
LoJoSho
2024-12-29 10:32:25 -06:00
parent 42e777444b
commit e8f5962e47
14 changed files with 310 additions and 9 deletions

30
v1_21_R3/build.gradle.kts Normal file
View File

@@ -0,0 +1,30 @@
plugins {
id("java")
id("io.papermc.paperweight.userdev")
}
dependencies {
paperweight.paperDevBundle("1.21.4-R0.1-SNAPSHOT")
implementation(project(":common"))
}
tasks {
build {
dependsOn(reobfJar)
}
compileJava {
options.encoding = Charsets.UTF_8.name()
}
java {
toolchain.languageVersion.set(JavaLanguageVersion.of(21));
}
javadoc {
options.encoding = Charsets.UTF_8.name()
}
processResources {
filteringCharset = Charsets.UTF_8.name()
}
}

View File

@@ -0,0 +1,17 @@
package me.lojosho.hibiscuscommons.nms.v1_21_R3;
import net.minecraft.network.protocol.Packet;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.network.ServerPlayerConnection;
import org.bukkit.craftbukkit.entity.CraftPlayer;
import org.bukkit.entity.Player;
public class NMSCommon {
public void sendPacket(Player player, Packet packet) {
ServerPlayer serverPlayer = ((CraftPlayer) player).getHandle();
ServerPlayerConnection connection = serverPlayer.connection;
connection.send(packet);
}
}

View File

@@ -0,0 +1,214 @@
package me.lojosho.hibiscuscommons.nms.v1_21_R3;
import com.mojang.datafixers.util.Pair;
import io.netty.buffer.Unpooled;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.*;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.PositionMoveRotation;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.level.portal.TeleportTransition;
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.scores.PlayerTeam;
import net.minecraft.world.scores.Team;
import org.bukkit.Bukkit;
import org.bukkit.craftbukkit.CraftEquipmentSlot;
import org.bukkit.craftbukkit.entity.CraftPlayer;
import org.bukkit.craftbukkit.inventory.CraftItemStack;
import org.bukkit.craftbukkit.scoreboard.CraftScoreboard;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.inventory.ItemStack;
import java.lang.reflect.Constructor;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
public class NMSPackets extends NMSCommon implements me.lojosho.hibiscuscommons.nms.NMSPackets {
static Constructor<ClientboundSetPassengersPacket> passengerConstructor;
static Constructor<ClientboundSetEntityLinkPacket> linkConstructor;
static Constructor<ClientboundSetCameraPacket> cameraConstructor;
static Constructor<ClientboundPlayerLookAtPacket> lookAtConstructor;
static {
try {
passengerConstructor = ClientboundSetPassengersPacket.class.getDeclaredConstructor(FriendlyByteBuf.class);
passengerConstructor.setAccessible(true);
} catch (Exception e) {
e.printStackTrace();
}
try {
linkConstructor = ClientboundSetEntityLinkPacket.class.getDeclaredConstructor(FriendlyByteBuf.class);
linkConstructor.setAccessible(true);
} catch (Exception e) {
e.printStackTrace();
}
try {
cameraConstructor = ClientboundSetCameraPacket.class.getDeclaredConstructor(FriendlyByteBuf.class);
cameraConstructor.setAccessible(true);
} catch (Exception e) {
e.printStackTrace();
}
try {
lookAtConstructor = ClientboundPlayerLookAtPacket.class.getDeclaredConstructor(FriendlyByteBuf.class);
lookAtConstructor.setAccessible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void sendEquipmentSlotUpdate(
int entityId,
org.bukkit.inventory.EquipmentSlot slot,
ItemStack item,
List<Player> sendTo
) {
EquipmentSlot nmsSlot = null;
net.minecraft.world.item.ItemStack nmsItem = null;
// Converting EquipmentSlot and ItemStack to NMS ones.
nmsSlot = CraftEquipmentSlot.getNMS(slot);
nmsItem = CraftItemStack.asNMSCopy(item);
if (nmsSlot == null) return;
Pair<EquipmentSlot, net.minecraft.world.item.ItemStack> pair = new Pair<>(nmsSlot, nmsItem);
List<Pair<EquipmentSlot, net.minecraft.world.item.ItemStack>> pairs = Collections.singletonList(pair);
ClientboundSetEquipmentPacket packet = new ClientboundSetEquipmentPacket(entityId, pairs);
for (Player p : sendTo) sendPacket(p, packet);
}
@Override
public void sendEquipmentSlotUpdate(
int entityId,
HashMap<org.bukkit.inventory.EquipmentSlot, ItemStack> equipment,
List<Player> sendTo
) {
List<Pair<EquipmentSlot, net.minecraft.world.item.ItemStack>> pairs = new ArrayList<>();
for (org.bukkit.inventory.EquipmentSlot slot : equipment.keySet()) {
EquipmentSlot nmsSlot = CraftEquipmentSlot.getNMS(slot);
net.minecraft.world.item.ItemStack nmsItem = CraftItemStack.asNMSCopy(equipment.get(slot));
Pair<EquipmentSlot, net.minecraft.world.item.ItemStack> pair = new Pair<>(nmsSlot, nmsItem);
pairs.add(pair);
}
ClientboundSetEquipmentPacket packet = new ClientboundSetEquipmentPacket(entityId, pairs);
for (Player p : sendTo) sendPacket(p, packet);
}
@Override
public void sendSlotUpdate(
Player player,
int slot
) {
int index = 0;
ServerPlayer player1 = ((CraftPlayer) player).getHandle();
if (index < Inventory.getSelectionSize()) {
index += 36;
} else if (index > 39) {
index += 5; // Off hand
} else if (index > 35) {
index = 8 - (index - 36);
}
ItemStack item = player.getInventory().getItem(slot);
Packet packet = new ClientboundContainerSetSlotPacket(player1.inventoryMenu.containerId, player1.inventoryMenu.incrementStateId(), index, CraftItemStack.asNMSCopy(item));
sendPacket(player, packet);
}
@Override
public void sendScoreboardHideNamePacket(Player player, String name) {
//Creating the team
PlayerTeam team = new PlayerTeam(((CraftScoreboard) Bukkit.getScoreboardManager().getMainScoreboard()).getHandle(), name);
//Setting name visibility
team.setNameTagVisibility(Team.Visibility.NEVER);
//Remove the Team (i assume so if it exists)
ClientboundSetPlayerTeamPacket removeTeamPacket = ClientboundSetPlayerTeamPacket.createRemovePacket(team);
sendPacket(player, removeTeamPacket);
//Creating the Team
ClientboundSetPlayerTeamPacket createTeamPacket = ClientboundSetPlayerTeamPacket.createAddOrModifyPacket(team, true);
sendPacket(player, createTeamPacket);
//Adding players to the team (You have to use the NPC's name, and add it to a list)
ClientboundSetPlayerTeamPacket createPlayerTeamPacket = ClientboundSetPlayerTeamPacket.createMultiplePlayerPacket(team, new ArrayList<String>() {{
add(name);
}}, ClientboundSetPlayerTeamPacket.Action.ADD);
sendPacket(player, createPlayerTeamPacket);
}
@Override
public void sendMountPacket(int mountId, int[] passengerIds, List<Player> sendTo) {
FriendlyByteBuf byteBuf = new FriendlyByteBuf(Unpooled.buffer());
byteBuf.writeVarInt(mountId);
byteBuf.writeVarIntArray(passengerIds);
try {
ClientboundSetPassengersPacket packet = passengerConstructor.newInstance(byteBuf);
for (Player p : sendTo) sendPacket(p, packet);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void sendLeashPacket(int leashEntity, int entityId, List<Player> sendTo) {
FriendlyByteBuf byteBuf = new FriendlyByteBuf(Unpooled.buffer());
byteBuf.writeInt(leashEntity);
byteBuf.writeInt(entityId);
try {
ClientboundSetEntityLinkPacket packet = linkConstructor.newInstance(byteBuf);
for (Player p : sendTo) sendPacket(p, packet);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void sendTeleportPacket(
int entityId,
double x,
double y,
double z,
float yaw,
float pitch,
boolean onGround,
List<Player> sendTo
) {
try {
ClientboundTeleportEntityPacket packet = ClientboundTeleportEntityPacket.teleport(entityId, new PositionMoveRotation(new Vec3(x, y, z), Vec3.ZERO, yaw, pitch), java.util.Set.of(), onGround);
for (Player p : sendTo) sendPacket(p, packet);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void sendRotationPacket(int entityId, float yaw, boolean onGround, List<Player> sendTo) {
FriendlyByteBuf byteBuf = new FriendlyByteBuf(Unpooled.buffer());
byteBuf.writeVarInt(entityId);
byteBuf.writeFloat(yaw);
byteBuf.writeBoolean(onGround);
try {
ClientboundPlayerLookAtPacket packet = lookAtConstructor.newInstance(byteBuf);
for (Player p : sendTo) sendPacket(p, packet);
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@@ -0,0 +1,29 @@
package me.lojosho.hibiscuscommons.nms.v1_21_R3;
import net.minecraft.server.level.ServerLevel;
import org.bukkit.Bukkit;
import org.bukkit.craftbukkit.CraftServer;
public class NMSUtils extends NMSCommon implements me.lojosho.hibiscuscommons.nms.NMSUtils {
@Override
public int getNextEntityId() {
return net.minecraft.world.entity.Entity.nextEntityId();
}
@Override
public org.bukkit.entity.Entity getEntity(int entityId) {
net.minecraft.world.entity.Entity entity = getNMSEntity(entityId);
if (entity == null) return null;
return entity.getBukkitEntity();
}
private net.minecraft.world.entity.Entity getNMSEntity(int entityId) {
for (ServerLevel world : ((CraftServer) Bukkit.getServer()).getHandle().getServer().getAllLevels()) {
net.minecraft.world.entity.Entity entity = world.getEntity(entityId);
if (entity == null) continue;
return entity;
}
return null;
}
}