Skip to content

Commit

Permalink
add support for 1.21.3
Browse files Browse the repository at this point in the history
  • Loading branch information
acrylic-style committed Dec 2, 2024
1 parent 6c5452e commit b18c7bc
Show file tree
Hide file tree
Showing 24 changed files with 798 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ public static boolean isModernNMS() {
case "1.20.2-R0.1-SNAPSHOT":
return "v1_20";
case "1.21.1-R0.1-SNAPSHOT":
return "v1_21_1";
case "1.21.2-R0.1-SNAPSHOT":
case "1.21.3-R0.1-SNAPSHOT":
return "v1_21_1";
return "v1_21_3";
}
return Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
}
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {

allprojects {
group = "net.azisaba.loreeditor"
version = "1.2.3"
version = "1.3.0"

apply {
plugin("java")
Expand Down
1 change: 1 addition & 0 deletions plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ dependencies {
api(project(":v1_19_R3"))
api(project(":v1_20"))
api(project(":v1_21_1"))
api(project(":v1_21_3"))
}

tasks {
Expand Down
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ include("v1_16_R3")
include("v1_19_R3")
include("v1_20")
include("v1_21_1")
include("v1_21_3")
15 changes: 15 additions & 0 deletions v1_21_3/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
plugins {
id("io.papermc.paperweight.userdev")
}

repositories {
mavenLocal()
}

java.toolchain.languageVersion.set(JavaLanguageVersion.of(21))

dependencies {
compileOnly(project(":common"))
compileOnly("io.papermc.paper:paper-api:1.21.3-R0.1-SNAPSHOT")
paperweight.paperDevBundle("1.21.3-R0.1-SNAPSHOT")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package net.azisaba.loreeditor.v1_21_3.chat;

import net.azisaba.loreeditor.common.chat.ChatModifier;
import net.azisaba.loreeditor.common.util.Reflected;
import net.minecraft.network.chat.Style;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;

public record ChatModifierImpl(Style handle) implements ChatModifier {
public ChatModifierImpl {
Objects.requireNonNull(handle, "handle");
}

@Contract(value = "_ -> new", pure = true)
@Reflected
public static @NotNull ChatModifierImpl getInstance(@NotNull Object handle) {
return new ChatModifierImpl((Style) handle);
}

@Override
public @NotNull Style handle() {
return handle;
}

@Override
public @NotNull ChatModifier setItalic(boolean italic) {
return getInstance(handle.withItalic(italic)); // setItalic
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package net.azisaba.loreeditor.v1_21_3.chat;

import net.azisaba.loreeditor.common.chat.ChatModifier;
import net.azisaba.loreeditor.common.chat.Component;
import net.azisaba.loreeditor.common.util.Reflected;
import net.minecraft.core.RegistryAccess;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.network.chat.Style;
import org.bukkit.Bukkit;
import org.bukkit.craftbukkit.CraftServer;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Objects;
import java.util.function.UnaryOperator;

@Reflected
public record ComponentImpl(MutableComponent handle) implements Component {
public ComponentImpl(@Nullable net.minecraft.network.chat.Component handle) {
this(handle == null ? null : handle.copy());
}

@Contract(value = "_ -> new", pure = true)
@Reflected
public static @NotNull ComponentImpl getInstance(@Nullable Object component) {
return new ComponentImpl((net.minecraft.network.chat.Component) component);
}

@Override
public @NotNull MutableComponent handle() {
return Objects.requireNonNull(handle, "cannot reference handle in static context");
}

public static MutableComponent deserializeFromJson(@NotNull String input) {
RegistryAccess registries = ((CraftServer) Bukkit.getServer()).getServer().registryAccess();
return net.minecraft.network.chat.Component.Serializer.fromJson(input, registries);
}

public static String serializeToJson(@NotNull net.minecraft.network.chat.Component component) {
RegistryAccess registries = ((CraftServer) Bukkit.getServer()).getServer().registryAccess();
return net.minecraft.network.chat.Component.Serializer.toJson(component, registries);
}

@Override
public @Nullable Component deserialize(@NotNull String input) {
return getInstance(deserializeFromJson(input));
}

@Override
public @NotNull String serialize(@NotNull Component component) {
return serializeToJson(((ComponentImpl) component).handle());
}

@Override
public @NotNull List<?> getSiblings() {
return handle().getSiblings();
}

@Override
public void addSiblingText(@NotNull String text) {
handle().append(deserializeFromJson(text));
}

@Override
public @NotNull Component modifyStyle(@NotNull UnaryOperator<ChatModifier> action) {
ChatModifier cm = new ChatModifierImpl(handle().getStyle());
Style newChatModifier = ((ChatModifierImpl) action.apply(cm)).handle();
return getInstance(handle().setStyle(newChatModifier));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package net.azisaba.loreeditor.v1_21_3.entity;

import net.azisaba.loreeditor.api.util.ReflectionUtil;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import xyz.acrylicstyle.util.reflector.CastTo;
import xyz.acrylicstyle.util.reflector.Reflector;
import xyz.acrylicstyle.util.reflector.ReflectorHandler;

import java.util.Objects;

public interface CraftPlayer {
static @NotNull CraftPlayer getInstance(@NotNull Player player) {
return Reflector.newReflector(null, CraftPlayer.class,
new ReflectorHandler(ReflectionUtil.getOBCClass("entity.CraftPlayer"), Objects.requireNonNull(player, "player")));
}

@NotNull
@CastTo(EntityPlayer.class)
EntityPlayer getHandle();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package net.azisaba.loreeditor.v1_21_3.entity;

import net.azisaba.loreeditor.api.util.ReflectionUtil;
import net.azisaba.loreeditor.v1_21_3.network.ServerCommonPacketListenerImpl;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import xyz.acrylicstyle.util.reflector.CastTo;
import xyz.acrylicstyle.util.reflector.FieldGetter;
import xyz.acrylicstyle.util.reflector.Reflector;
import xyz.acrylicstyle.util.reflector.ReflectorHandler;

public interface EntityPlayer {
static @NotNull EntityPlayer getInstance(@Nullable Object o) {
return Reflector.newReflector(null, EntityPlayer.class,
new ReflectorHandler(ReflectionUtil.getNMSClass("net.minecraft.server.level.EntityPlayer"), o));
}

@NotNull
@CastTo(value = ServerCommonPacketListenerImpl.class, createInstance = true)
@FieldGetter("connection")
ServerCommonPacketListenerImpl getPlayerConnection();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package net.azisaba.loreeditor.v1_21_3.item;

import net.azisaba.loreeditor.api.item.CraftItemStack;
import net.azisaba.loreeditor.api.item.ItemStack;
import net.azisaba.loreeditor.common.util.Reflected;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class CraftItemStackImpl implements CraftItemStack {
@Contract(value = "_ -> new", pure = true)
@Reflected
public static @NotNull CraftItemStackImpl getInstance(@Nullable Object item) {
return new CraftItemStackImpl();
}

@Override
public @Nullable ItemStack asNMSCopy(org.bukkit.inventory.@Nullable ItemStack item) {
return new ItemStackImpl(org.bukkit.craftbukkit.inventory.CraftItemStack.asNMSCopy(item));
}

@NotNull
@Override
public org.bukkit.inventory.ItemStack asCraftMirror(@NotNull ItemStack item) {
return org.bukkit.craftbukkit.inventory.CraftItemStack.asCraftMirror(((ItemStackImpl) item).handle());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package net.azisaba.loreeditor.v1_21_3.item;

import net.azisaba.loreeditor.api.item.ItemStack;
import net.azisaba.loreeditor.api.item.tag.CompoundTag;
import net.azisaba.loreeditor.api.item.tag.ListTag;
import net.azisaba.loreeditor.common.util.Reflected;
import net.azisaba.loreeditor.v1_21_3.chat.ComponentImpl;
import net.azisaba.loreeditor.v1_21_3.item.tag.CompoundTagImpl;
import net.azisaba.loreeditor.v1_21_3.item.tag.ListTagImpl;
import net.minecraft.core.component.DataComponentMap;
import net.minecraft.core.component.DataComponents;
import net.minecraft.nbt.StringTag;
import net.minecraft.nbt.Tag;
import net.minecraft.network.chat.Component;
import net.minecraft.world.item.component.CustomData;
import net.minecraft.world.item.component.ItemLore;
import org.bukkit.craftbukkit.inventory.CraftItemStack;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public record ItemStackImpl(net.minecraft.world.item.ItemStack handle) implements ItemStack {
public ItemStackImpl(net.minecraft.world.item.ItemStack handle) {
this.handle = Objects.requireNonNull(handle, "handle");
}

@Override
public @NotNull net.minecraft.world.item.ItemStack handle() {
return handle;
}

@Contract("_ -> new")
@Reflected
public static @NotNull ItemStackImpl getInstance(@NotNull Object item) {
Objects.requireNonNull(item, "item");
return new ItemStackImpl((net.minecraft.world.item.ItemStack) item);
}

@SuppressWarnings("deprecation")
@Override
public @NotNull CompoundTag getOrCreateTag() {
CustomData customData = handle.get(DataComponents.CUSTOM_DATA);
if (customData == null) {
customData = CustomData.of(new net.minecraft.nbt.CompoundTag());
handle.set(DataComponents.CUSTOM_DATA, customData);
}
return handleLore(new CompoundTagImpl(customData.getUnsafe()));
}

@SuppressWarnings("deprecation")
@Override
public @Nullable CompoundTag getTag() {
CustomData customData = handle.get(DataComponents.CUSTOM_DATA);
if (customData == null) {
return handleLore(null);
}
return handleLore(new CompoundTagImpl(customData.getUnsafe()));
}

private CompoundTag handleLore(@Nullable CompoundTag base) {
var handle = new net.minecraft.nbt.ListTag();
ListTag listTag = new ListTagImpl(handle);
if (handle().has(DataComponents.LORE)) {
Objects.requireNonNull(handle().get(DataComponents.LORE)).lines()
.forEach(component -> handle.add(StringTag.valueOf(ComponentImpl.serializeToJson(component))));
} else {
return base;
}
if (handle.isEmpty()) {
return base;
}
if (base == null) {
base = new CompoundTagImpl(new net.minecraft.nbt.CompoundTag());
}
CompoundTag displayTag = base.getCompound("display");
displayTag.set("Lore", listTag);
base.set("display", displayTag);
return base;
}

@Override
public void setTag(@Nullable CompoundTag tag) {
handle.set(DataComponents.CUSTOM_DATA, tag == null ? null : CustomData.of(((CompoundTagImpl) tag).getHandle()));
if (tag != null) {
if (!tag.hasKeyOfType("display", 10)) {
handle.set(DataComponents.LORE, ItemLore.EMPTY);
return;
}
CompoundTag displayTag = tag.getCompound("display");
ListTag listTag = displayTag.getList("Lore", 8);
if (listTag.size() > 0) {
List<Component> lore =
((ListTagImpl) listTag).getHandle()
.stream()
.map(Tag::getAsString)
.map(ComponentImpl::deserializeFromJson)
.collect(Collectors.toUnmodifiableList());
handle.set(DataComponents.LORE, new ItemLore(lore));
} else {
handle.set(DataComponents.LORE, ItemLore.EMPTY);
}
} else {
handle.set(DataComponents.LORE, ItemLore.EMPTY);
}
}

@Override
public int getCount() {
return handle.getCount();
}

@Override
public @NotNull ItemStack copy() {
org.bukkit.inventory.ItemStack originalStack = handle.getBukkitStack();
org.bukkit.inventory.ItemStack stack = new org.bukkit.inventory.ItemStack(originalStack.getType(), handle.getCount());
if (originalStack.hasItemMeta()) {
stack.setItemMeta(originalStack.getItemMeta().clone());
}
return new ItemStackImpl(CraftItemStack.asNMSCopy(stack));
}

public DataComponentMap getComponents() {
return handle.getComponents();
}
}
Loading

0 comments on commit b18c7bc

Please sign in to comment.