Skip to content

Commit 003cf5a

Browse files
committed
📝 fix NMS 1.21.11 and 1.21.9
1 parent 46d114b commit 003cf5a

12 files changed

Lines changed: 680 additions & 0 deletions

File tree

NMS/V1_21_11/build.gradle.kts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
plugins {
2+
id("io.papermc.paperweight.userdev")
3+
}
4+
5+
group "NMS:V1_21_11"
6+
7+
dependencies {
8+
compileOnly(project(":API"))
9+
compileOnly("net.kyori:adventure-api:4.26.1")
10+
paperweight.paperDevBundle("1.21.11-R0.1-SNAPSHOT")
11+
}
12+
13+
paperweight.reobfArtifactConfiguration = io.papermc.paperweight.userdev.ReobfArtifactConfiguration.REOBF_PRODUCTION
14+
15+
java {
16+
toolchain.languageVersion = JavaLanguageVersion.of(21)
17+
}
18+
19+
tasks.assemble {
20+
dependsOn(tasks.reobfJar)
21+
}
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
package fr.maxlego08.essentials.nms.v1_21_11;
2+
3+
import com.mojang.math.Transformation;
4+
import fr.maxlego08.essentials.api.EssentialsPlugin;
5+
import fr.maxlego08.essentials.api.hologram.Hologram;
6+
import fr.maxlego08.essentials.api.hologram.HologramType;
7+
import fr.maxlego08.essentials.api.hologram.configuration.BlockHologramConfiguration;
8+
import fr.maxlego08.essentials.api.hologram.configuration.HologramConfiguration;
9+
import fr.maxlego08.essentials.api.hologram.configuration.ItemHologramConfiguration;
10+
import fr.maxlego08.essentials.api.hologram.configuration.TextHologramConfiguration;
11+
import fr.maxlego08.essentials.api.utils.ReflectionUtils;
12+
import fr.maxlego08.essentials.api.utils.SafeLocation;
13+
import io.papermc.paper.adventure.PaperAdventure;
14+
import net.minecraft.network.protocol.Packet;
15+
import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
16+
import net.minecraft.network.protocol.game.ClientboundRemoveEntitiesPacket;
17+
import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket;
18+
import net.minecraft.network.protocol.game.ClientboundTeleportEntityPacket;
19+
import net.minecraft.network.syncher.EntityDataAccessor;
20+
import net.minecraft.network.syncher.SynchedEntityData;
21+
import net.minecraft.server.level.ServerEntity;
22+
import net.minecraft.server.level.ServerLevel;
23+
import net.minecraft.server.level.ServerPlayer;
24+
import net.minecraft.util.Brightness;
25+
import net.minecraft.world.entity.Display;
26+
import net.minecraft.world.entity.EntityType;
27+
import net.minecraft.world.entity.PositionMoveRotation;
28+
import net.minecraft.world.item.ItemDisplayContext;
29+
import net.minecraft.world.item.ItemStack;
30+
import net.minecraft.world.level.block.Block;
31+
import org.bukkit.craftbukkit.CraftWorld;
32+
import org.bukkit.craftbukkit.block.CraftBlockType;
33+
import org.bukkit.craftbukkit.entity.CraftPlayer;
34+
import org.bukkit.entity.Player;
35+
import org.joml.Quaternionf;
36+
37+
import java.util.ArrayList;
38+
import java.util.Set;
39+
40+
public class CraftHologram extends Hologram {
41+
42+
private Display display = null;
43+
44+
public CraftHologram(EssentialsPlugin plugin, HologramType hologramType, HologramConfiguration configuration, String fileName, String name, SafeLocation location) {
45+
super(plugin, hologramType, name, fileName, location, configuration);
46+
}
47+
48+
@Override
49+
public void create(Player player) {
50+
ServerPlayer serverPlayer = ((CraftPlayer) player).getHandle();
51+
ServerEntity serverEntity = new ServerEntity(serverPlayer.level(), display, 0, false, null, Set.of());
52+
send(player, new ClientboundAddEntityPacket(display, serverEntity));
53+
54+
this.update(player);
55+
}
56+
57+
@Override
58+
public void delete(Player player) {
59+
this.send(player, new ClientboundRemoveEntitiesPacket(display.getId()));
60+
}
61+
62+
@Override
63+
public void update(Player player) {
64+
65+
send(player, new ClientboundTeleportEntityPacket(display.getId(), PositionMoveRotation.of(display), Set.of(), display.onGround));
66+
67+
if (display instanceof Display.TextDisplay textDisplay) {
68+
textDisplay.setText(PaperAdventure.asVanilla(getComponentText(player)));
69+
}
70+
71+
final var values = new ArrayList<SynchedEntityData.DataValue<?>>();
72+
for (final var item : ((SynchedEntityData.DataItem<?>[]) ReflectionUtils.getValue(display.getEntityData(), "itemsById"))) {
73+
values.add(item.value());
74+
}
75+
send(player, new ClientboundSetEntityDataPacket(display.getId(), values));
76+
}
77+
78+
@Override
79+
public void create() {
80+
ServerLevel serverLevel = ((CraftWorld) location.getLocation().getWorld()).getHandle();
81+
switch (hologramType) {
82+
case BLOCK -> this.display = new Display.BlockDisplay(EntityType.BLOCK_DISPLAY, serverLevel);
83+
case ITEM -> this.display = new Display.ItemDisplay(EntityType.ITEM_DISPLAY, serverLevel);
84+
case TEXT -> this.display = new Display.TextDisplay(EntityType.TEXT_DISPLAY, serverLevel);
85+
}
86+
87+
display.getEntityData().set((EntityDataAccessor<Integer>) ReflectionUtils.getStaticValue(Display.class, "DATA_TRANSFORMATION_INTERPOLATION_DURATION_ID"), 1); // Transformation duration
88+
display.getEntityData().set((EntityDataAccessor<Integer>) ReflectionUtils.getStaticValue(Display.class, "DATA_TRANSFORMATION_INTERPOLATION_START_DELTA_TICKS_ID"), 0); // Interpolation start
89+
90+
this.updateLocation();
91+
this.update();
92+
}
93+
94+
@Override
95+
public void updateLocation() {
96+
display.setPosRaw(location.getX(), location.getY(), location.getZ());
97+
display.setYRot(location.getYaw());
98+
display.setXRot(location.getPitch());
99+
}
100+
101+
@Override
102+
public void update() {
103+
104+
display.setBillboardConstraints(switch (this.configuration.getBillboard()) {
105+
case FIXED -> Display.BillboardConstraints.FIXED;
106+
case VERTICAL -> Display.BillboardConstraints.VERTICAL;
107+
case HORIZONTAL -> Display.BillboardConstraints.HORIZONTAL;
108+
case CENTER -> Display.BillboardConstraints.CENTER;
109+
});
110+
111+
if (display instanceof Display.TextDisplay textDisplay && configuration instanceof TextHologramConfiguration textHologramConfiguration) {
112+
113+
display.getEntityData().set((EntityDataAccessor<Integer>) ReflectionUtils.getStaticValue(Display.TextDisplay.class, "DATA_LINE_WIDTH_ID"), Hologram.LINE_WIDTH);
114+
115+
var backgroundColor = (EntityDataAccessor<Integer>) ReflectionUtils.getStaticValue(Display.TextDisplay.class, "DATA_BACKGROUND_COLOR_ID");
116+
var background = textHologramConfiguration.getBackground();
117+
int backgroundValue = (background == null) ? Display.TextDisplay.INITIAL_BACKGROUND : (background == Hologram.TRANSPARENT) ? 0 : background.value() | 0xC8000000;
118+
display.getEntityData().set(backgroundColor, backgroundValue);
119+
120+
byte flags = textDisplay.getFlags();
121+
flags = (byte) (textHologramConfiguration.isTextShadow() ? (flags | Display.TextDisplay.FLAG_SHADOW) : (flags & ~Display.TextDisplay.FLAG_SHADOW));
122+
flags = (byte) (textHologramConfiguration.getTextAlignment() == org.bukkit.entity.TextDisplay.TextAlignment.LEFT ? (flags | Display.TextDisplay.FLAG_ALIGN_LEFT) : (flags & ~Display.TextDisplay.FLAG_ALIGN_LEFT));
123+
flags = (byte) (textHologramConfiguration.isSeeThrough() ? (flags | Display.TextDisplay.FLAG_SEE_THROUGH) : (flags & ~Display.TextDisplay.FLAG_SEE_THROUGH));
124+
flags = (byte) (textHologramConfiguration.getTextAlignment() == org.bukkit.entity.TextDisplay.TextAlignment.RIGHT ? (flags | Display.TextDisplay.FLAG_ALIGN_RIGHT) : (flags & ~Display.TextDisplay.FLAG_ALIGN_RIGHT));
125+
textDisplay.setFlags(flags);
126+
} else if (display instanceof Display.ItemDisplay itemDisplay && configuration instanceof ItemHologramConfiguration itemHologramConfiguration) {
127+
128+
var context = switch (itemHologramConfiguration.getItemDisplayTransform()) {
129+
case THIRDPERSON_LEFTHAND -> ItemDisplayContext.THIRD_PERSON_LEFT_HAND;
130+
case THIRDPERSON_RIGHTHAND -> ItemDisplayContext.THIRD_PERSON_RIGHT_HAND;
131+
case FIRSTPERSON_LEFTHAND -> ItemDisplayContext.FIRST_PERSON_LEFT_HAND;
132+
case FIRSTPERSON_RIGHTHAND -> ItemDisplayContext.FIRST_PERSON_RIGHT_HAND;
133+
default -> ItemDisplayContext.valueOf(itemHologramConfiguration.getItemDisplayTransform().name());
134+
};
135+
itemDisplay.setItemTransform(context);
136+
itemDisplay.setItemStack(ItemStack.fromBukkitCopy(itemHologramConfiguration.getItemStack()));
137+
} else if (display instanceof Display.BlockDisplay blockDisplay && configuration instanceof BlockHologramConfiguration blockData) {
138+
139+
Block block = CraftBlockType.bukkitToMinecraft(blockData.getMaterial());
140+
blockDisplay.setBlockState(block.defaultBlockState());
141+
}
142+
143+
if (this.configuration.getBrightness() != null) {
144+
display.setBrightnessOverride(new Brightness(this.configuration.getBrightness().getBlockLight(), this.configuration.getBrightness().getSkyLight()));
145+
}
146+
147+
display.setTransformation(new Transformation(this.configuration.getTranslation(), new Quaternionf(), this.configuration.getScale(), new Quaternionf()));
148+
149+
display.setShadowRadius(this.configuration.getShadowRadius());
150+
display.setShadowStrength(this.configuration.getShadowStrength());
151+
}
152+
153+
private void send(Player player, Packet<?> packet) {
154+
((CraftPlayer) player).getHandle().connection.send(packet);
155+
}
156+
157+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package fr.maxlego08.essentials.nms.v1_21_11;
2+
3+
import fr.maxlego08.essentials.api.EssentialsPlugin;
4+
import fr.maxlego08.essentials.api.nms.PlayerUtil;
5+
import org.bukkit.OfflinePlayer;
6+
import org.bukkit.entity.Player;
7+
8+
public class PlayerUtils implements PlayerUtil {
9+
10+
private final EssentialsPlugin plugin;
11+
12+
public PlayerUtils(EssentialsPlugin plugin) {
13+
this.plugin = plugin;
14+
}
15+
16+
@Override
17+
public boolean openEnderChest(Player player, OfflinePlayer offlinePlayer) {
18+
19+
/*CraftPlayerManager craftPlayerManager = new CraftPlayerManager(plugin.getLogger());
20+
var loadPlayer = craftPlayerManager.loadPlayer(offlinePlayer);
21+
22+
if (loadPlayer != null) {
23+
EnderChestHolder enderChestHolder = new EnderChestHolder(loadPlayer);
24+
player.openInventory(enderChestHolder.getInventory());
25+
return true;
26+
}*/
27+
return false;
28+
}
29+
30+
@Override
31+
public boolean openPlayerInventory(Player player, OfflinePlayer offlinePlayer) {
32+
/*CraftPlayerManager craftPlayerManager = new CraftPlayerManager(plugin.getLogger());
33+
var loadPlayer = craftPlayerManager.loadPlayer(offlinePlayer);
34+
35+
if (loadPlayer != null) {
36+
PlayerInventoryHolder enderChestHolder = new PlayerInventoryHolder(loadPlayer);
37+
player.openInventory(enderChestHolder.getInventory());
38+
return true;
39+
}*/
40+
return false;
41+
}
42+
43+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package fr.maxlego08.essentials.nms.v1_21_11;
2+
3+
import fr.maxlego08.essentials.api.waypoint.WayPointHelper;
4+
import fr.maxlego08.essentials.api.waypoint.WayPointIcon;
5+
import net.minecraft.core.BlockPos;
6+
import net.minecraft.network.protocol.Packet;
7+
import net.minecraft.network.protocol.game.ClientboundTrackedWaypointPacket;
8+
import net.minecraft.world.level.ChunkPos;
9+
import net.minecraft.world.waypoints.Waypoint;
10+
import net.minecraft.world.waypoints.WaypointStyleAssets;
11+
import org.bukkit.Chunk;
12+
import org.bukkit.Location;
13+
import org.bukkit.craftbukkit.entity.CraftPlayer;
14+
import org.bukkit.entity.Player;
15+
16+
import java.util.Optional;
17+
import java.util.UUID;
18+
19+
public class WayPointPacket implements WayPointHelper {
20+
21+
@Override
22+
public void addWayPoint(Player player, UUID uniqueId, Location location, WayPointIcon wayPointIcon) {
23+
24+
var icon = toIcon(wayPointIcon);
25+
var blockPos = new BlockPos(location.getBlockX(), location.getBlockY(), location.getBlockZ());
26+
var packet = ClientboundTrackedWaypointPacket.addWaypointPosition(uniqueId, icon, blockPos);
27+
28+
sendPacket(player, packet);
29+
}
30+
31+
@Override
32+
public void updateWayPointPosition(Player player, UUID uniqueId, Location location, WayPointIcon wayPointIcon) {
33+
34+
var icon = toIcon(wayPointIcon);
35+
var blockPos = new BlockPos(location.getBlockX(), location.getBlockY(), location.getBlockZ());
36+
var packet = ClientboundTrackedWaypointPacket.updateWaypointPosition(uniqueId, icon, blockPos);
37+
38+
sendPacket(player, packet);
39+
}
40+
41+
@Override
42+
public void addWayPoint(Player player, UUID uniqueId, Chunk chunk, WayPointIcon wayPointIcon) {
43+
44+
var icon = toIcon(wayPointIcon);
45+
var chunkPos = new ChunkPos(chunk.getX(), chunk.getZ());
46+
var packet = ClientboundTrackedWaypointPacket.addWaypointChunk(uniqueId, icon, chunkPos);
47+
48+
sendPacket(player, packet);
49+
}
50+
51+
@Override
52+
public void updateWayPointPosition(Player player, UUID uniqueId, Chunk chunk, WayPointIcon wayPointIcon) {
53+
54+
var icon = toIcon(wayPointIcon);
55+
var chunkPos = new ChunkPos(chunk.getX(), chunk.getZ());
56+
var packet = ClientboundTrackedWaypointPacket.updateWaypointChunk(uniqueId, icon, chunkPos);
57+
58+
sendPacket(player, packet);
59+
}
60+
61+
@Override
62+
public void removeWayPoint(Player player, UUID uniqueId) {
63+
64+
var packet = ClientboundTrackedWaypointPacket.removeWaypoint(uniqueId);
65+
sendPacket(player, packet);
66+
}
67+
68+
private void sendPacket(Player player, Packet<?> packet) {
69+
((CraftPlayer) player).getHandle().connection.send(packet);
70+
}
71+
72+
/**
73+
* Converts a {@link WayPointIcon} to a {@link Waypoint.Icon}.
74+
* <p>
75+
* If the given {@link WayPointIcon} has a texture, then the style of the resulting icon will be set to the
76+
* death waypoint style. Otherwise, the style will be left unchanged.
77+
* <p>
78+
* The color of the resulting icon will be set to the color of the given {@link WayPointIcon}.
79+
*
80+
* @param wayPointIcon the icon to convert
81+
* @return the converted icon
82+
*/
83+
private Waypoint.Icon toIcon(WayPointIcon wayPointIcon) {
84+
var icon = new Waypoint.Icon();
85+
if (wayPointIcon.texture() != null) {
86+
icon.style = WaypointStyleAssets.createId(wayPointIcon.texture());
87+
}
88+
icon.color = Optional.of(wayPointIcon.color().getRGB() & 0xFFFFFF);
89+
return icon;
90+
}
91+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package fr.maxlego08.essentials.nms.v1_21_11.enderchest;
2+
3+
import org.apache.commons.lang3.NotImplementedException;
4+
import org.bukkit.OfflinePlayer;
5+
import org.bukkit.entity.Player;
6+
import org.jetbrains.annotations.NotNull;
7+
import org.jetbrains.annotations.Nullable;
8+
9+
/**
10+
* Based on: <a href="https://github.com/Jikoo/OpenInv/tree/master">OpenInv</a>
11+
*/
12+
public class CraftPlayerManager implements fr.maxlego08.essentials.api.nms.PlayerManager {
13+
14+
15+
@Override
16+
public @Nullable Player loadPlayer(@NotNull OfflinePlayer offline) {
17+
throw new NotImplementedException();
18+
}
19+
20+
@Override
21+
public @NotNull Player inject(@NotNull Player player) {
22+
throw new NotImplementedException();
23+
}
24+
}

NMS/V1_21_9/build.gradle.kts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
plugins {
2+
id("io.papermc.paperweight.userdev")
3+
}
4+
5+
group "NMS:V1_21_9"
6+
7+
dependencies {
8+
compileOnly(project(":API"))
9+
compileOnly("net.kyori:adventure-api:4.26.1")
10+
paperweight.paperDevBundle("1.21.9-R0.1-SNAPSHOT")
11+
}
12+
13+
paperweight.reobfArtifactConfiguration = io.papermc.paperweight.userdev.ReobfArtifactConfiguration.REOBF_PRODUCTION
14+
15+
java {
16+
toolchain.languageVersion = JavaLanguageVersion.of(21)
17+
}
18+
19+
tasks.assemble {
20+
dependsOn(tasks.reobfJar)
21+
}

0 commit comments

Comments
 (0)