Skip to content

Commit

Permalink
feat: support 1.20.2
Browse files Browse the repository at this point in the history
  • Loading branch information
acrylic-style committed Nov 2, 2023
1 parent 6313903 commit 7729e6b
Show file tree
Hide file tree
Showing 23 changed files with 686 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public static boolean isModernNMS() {
}

public static @NotNull String getServerImplVersion() {
switch (Bukkit.getBukkitVersion()) {
case "1.20.1-R0.1-SNAPSHOT":
case "1.20.2-R0.1-SNAPSHOT":
return "v1_20";
}
return Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
}

Expand All @@ -38,7 +43,7 @@ public static boolean isModernNMS() {

public static @NotNull Class<?> getOBCClass(@NotNull String name) {
try {
return Class.forName("org.bukkit.craftbukkit." + getServerImplVersion() + "." + name);
return Class.forName(Bukkit.getServer().getClass().getPackage().getName() + "." + name);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface Reflected {
}
1 change: 1 addition & 0 deletions plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ dependencies {
api(project(":common"))
api(project(":v1_15_R1"))
api(project(":v1_19_R3"))
api(project(":v1_20"))
}

tasks {
Expand Down
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ include("plugin")
include("common")
include("v1_15_R1")
include("v1_19_R3")
include("v1_20")
10 changes: 10 additions & 0 deletions v1_20/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
repositories {
mavenLocal()
}

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

dependencies {
compileOnly(project(":common"))
compileOnly("org.spigotmc:spigot:1.20.2-R0.1-SNAPSHOT")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package net.azisaba.loreeditor.v1_20.chat;

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

import java.util.Objects;

public record ChatModifierImpl(net.minecraft.network.chat.ChatModifier 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((net.minecraft.network.chat.ChatModifier) handle);
}

@Override
public @NotNull net.minecraft.network.chat.ChatModifier handle() {
return handle;
}

@Override
public @NotNull ChatModifier setItalic(boolean italic) {
return getInstance(handle.b(italic)); // setItalic
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package net.azisaba.loreeditor.v1_20.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.network.chat.IChatBaseComponent;
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(IChatBaseComponent handle) implements Component {
public ComponentImpl(@Nullable IChatBaseComponent handle) {
this.handle = handle;
}

@Contract(value = "_ -> new", pure = true)
@Reflected
public static @NotNull ComponentImpl getInstance(@Nullable Object component) {
return new ComponentImpl((IChatBaseComponent) component);
}

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

@Override
public @Nullable Component deserialize(@NotNull String input) {
return getInstance(IChatBaseComponent.ChatSerializer.a(input)); // deserialize
}

@Override
public @NotNull String serialize(@NotNull Component component) {
return IChatBaseComponent.ChatSerializer.a(((ComponentImpl) component).handle()); // serialize
}

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

@Override
public void addSiblingText(@NotNull String text) {
handle().a(IChatBaseComponent.ChatSerializer.a(text)); // addSibling
}

@Override
public @NotNull Component modifyStyle(@NotNull UnaryOperator<ChatModifier> action) {
ChatModifier cm = new ChatModifierImpl(handle().a()); // getChatModifier
net.minecraft.network.chat.ChatModifier newChatModifier = ((ChatModifierImpl) action.apply(cm)).handle();
return getInstance(handle().a(newChatModifier)); // setChatModifier
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package net.azisaba.loreeditor.v1_20.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_20.entity;

import net.azisaba.loreeditor.api.util.ReflectionUtil;
import net.azisaba.loreeditor.v1_20.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("c")
ServerCommonPacketListenerImpl getPlayerConnection();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package net.azisaba.loreeditor.v1_20.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.v1_20_R2.inventory.CraftItemStack.asNMSCopy(item));
}

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

import net.azisaba.loreeditor.api.item.ItemStack;
import net.azisaba.loreeditor.api.item.tag.CompoundTag;
import net.azisaba.loreeditor.common.util.Reflected;
import net.azisaba.loreeditor.v1_20.item.tag.CompoundTagImpl;
import net.minecraft.nbt.NBTTagCompound;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Objects;

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);
}

@Override
public @NotNull CompoundTag getOrCreateTag() {
return new CompoundTagImpl(handle.w()); // getOrCreateTag
}

@Override
public @Nullable CompoundTag getTag() {
NBTTagCompound handle = this.handle.v(); // getTag
return handle == null ? null : new CompoundTagImpl(handle);
}

@Override
public void setTag(@Nullable CompoundTag tag) {
handle.c(tag == null ? null : ((CompoundTagImpl) tag).getHandle()); // setTag
}

@Override
public int getCount() {
return handle.K(); // getCount (probably)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package net.azisaba.loreeditor.v1_20.item.tag;

import net.azisaba.loreeditor.api.item.tag.CompoundTag;
import net.azisaba.loreeditor.api.item.tag.ListTag;
import net.azisaba.loreeditor.api.item.tag.Tag;
import net.azisaba.loreeditor.common.util.Reflected;
import net.minecraft.nbt.NBTTagCompound;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class CompoundTagImpl extends TagImpl<NBTTagCompound> implements CompoundTag {
public CompoundTagImpl(@NotNull NBTTagCompound handle) {
super(handle);
}

@Contract("_ -> new")
@Reflected
public static @NotNull CompoundTagImpl getInstance(@Nullable Object tag) {
if (tag == null) {
return new CompoundTagImpl(new NBTTagCompound());
}
return new CompoundTagImpl((NBTTagCompound) tag);
}

@Override
public @NotNull CompoundTag constructor() {
return new CompoundTagImpl(new NBTTagCompound());
}

@Override
public int size() {
return getHandle().f(); // size
}

@Override
public boolean hasKeyOfType(@NotNull String key, int type) {
return getHandle().b(key, type); // hasKeyOfType
}

@Override
public void remove(@NotNull String key) {
getHandle().r(key); // remove
}

@Override
public boolean getBoolean(@NotNull String key) {
return getHandle().q(key); // getBoolean
}

@Override
public int getInt(@NotNull String key) {
return getHandle().h(key); // getInt
}

@Override
public @NotNull String getString(@NotNull String key) {
return getHandle().l(key); // getString
}

@Override
public @NotNull CompoundTag getCompound(@NotNull String key) {
return new CompoundTagImpl(getHandle().p(key)); // getCompound
}

@Override
public @NotNull ListTag getList(@NotNull String key, int type) {
return new ListTagImpl(getHandle().c(key, type)); // getList
}

@Override
public void setString(@NotNull String key, @NotNull String value) {
getHandle().a(key, value); // setString
}

@Override
public void setBoolean(@NotNull String key, boolean value) {
getHandle().a(key, value); // setBoolean
}

@Override
public void setInt(@NotNull String key, int value) {
getHandle().a(key, value); // setInt
}

@Override
public void set(@NotNull String key, @NotNull Tag value) {
getHandle().a(key, ((TagImpl<?>) value).getHandle()); // set
}

@Override
public @Nullable Tag get(@NotNull String key) {
return TagImpl.toTag(getHandle().c(key)); // get
}
}
Loading

0 comments on commit 7729e6b

Please sign in to comment.