Skip to content

Commit

Permalink
Fix reflection issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ceze88 committed May 26, 2024
1 parent 03b6c3d commit ed1f103
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,13 @@ public Class<?> getClazz(String sub) {
//We don't have 1_20_R4 like packages in paper, so skip that part
try {
if (this.className.startsWith("Craft")) {
return Class.forName("org.bukkit.craftbukkit." + (this.packageName == null ? "" : "." + this.packageName) + "." + name);
return Class.forName("org.bukkit.craftbukkit" + (this.packageName == null ? "" : "." + this.packageName) + "." + name);
}

return Class.forName("net.minecraft." + (this.packageName != null ? this.packageName : "server.") + "." + name);
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}

} else {
try {
if (this.className.startsWith("Craft")) {
Expand Down
26 changes: 21 additions & 5 deletions Core/src/main/java/com/craftaro/core/utils/ItemUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import com.craftaro.core.compatibility.CompatibleHand;
import com.craftaro.core.compatibility.CompatibleMaterial;
import com.craftaro.core.compatibility.MethodMapping;
import com.craftaro.core.compatibility.ServerProject;
import com.craftaro.core.compatibility.ServerVersion;
import com.cryptomorin.xseries.XMaterial;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property;
import de.tr7zw.changeme.nbtapi.NBTItem;
import org.apache.commons.lang3.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.Material;
Expand Down Expand Up @@ -99,7 +101,9 @@ static String itemName(Material mat) {
methodAsBukkitCopy = clazzCraftItemStack.getMethod("asBukkitCopy", clazzItemStack);
methodAsNMSCopy = MethodMapping.CB_ITEM_STACK__AS_NMS_COPY.getMethod(clazzCraftItemStack);

if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_19)) {
if (ServerVersion.isServerVersion(ServerVersion.V1_20)) {
//Do nothing
} else if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_19)) {
Class<?> clazzRandomSource = ClassMapping.RANDOM_SOURCE.getClazz();
methodA = clazzEnchantmentManager.getMethod("a", clazzRandomSource.getMethod("c").getReturnType(), clazzItemStack, int.class, boolean.class);
randomInstance = ClassMapping.SINGLE_THREADED_RANDOM_SOURCE.getClazz().getConstructor(long.class).newInstance(ThreadLocalRandom.current().nextLong());
Expand Down Expand Up @@ -226,10 +230,12 @@ public static boolean hasEnoughDurability(ItemStack tool, int requiredAmount) {
static {
if (cb_ItemStack != null) {
try {
mc_ItemStack_getTag = MethodMapping.MC_ITEM_STACK__GET_TAG.getMethod(mc_ItemStack);
mc_ItemStack_setTag = MethodMapping.MC_ITEM_STACK__SET_TAG.getMethod(mc_ItemStack);
mc_NBTTagCompound_set = MethodMapping.MC_NBT_TAG_COMPOUND__SET.getMethod(mc_NBTTagCompound);
mc_NBTTagCompound_remove = MethodMapping.MC_NBT_TAG_COMPOUND__REMOVE.getMethod(mc_NBTTagCompound);
if (ServerVersion.isServerVersionBelow(ServerVersion.V1_20)) {
mc_ItemStack_getTag = MethodMapping.MC_ITEM_STACK__GET_TAG.getMethod(mc_ItemStack);
mc_ItemStack_setTag = MethodMapping.MC_ITEM_STACK__SET_TAG.getMethod(mc_ItemStack);
mc_NBTTagCompound_set = MethodMapping.MC_NBT_TAG_COMPOUND__SET.getMethod(mc_NBTTagCompound);
mc_NBTTagCompound_remove = MethodMapping.MC_NBT_TAG_COMPOUND__REMOVE.getMethod(mc_NBTTagCompound);
}
// mc_NBTTagCompound_setShort = MethodMapping.MC_NBT_TAG_COMPOUND__SET_SHORT.getMethod(mc_NBTTagCompound);
// mc_NBTTagCompound_setString = MethodMapping.MC_NBT_TAG_COMPOUND__SET_STRING.getMethod(mc_NBTTagCompound);
cb_CraftItemStack_asNMSCopy = MethodMapping.CB_ITEM_STACK__AS_NMS_COPY.getMethod(cb_ItemStack);
Expand Down Expand Up @@ -263,6 +269,16 @@ public static ItemStack addGlow(ItemStack item) {
return item;
}

if (ServerProject.isServer(ServerProject.PAPER) && (ServerVersion.getMinecraftVersion().equals("1.20.5") || ServerVersion.getMinecraftVersion().equals("1.20.6"))) {
if (item == null || item.getType() == Material.AIR) {
return item;
}
NBTItem nbtItem = new NBTItem(item);
nbtItem.addCompound("minecraft:enchantments");
nbtItem.applyNBT(item);
return item;
}

// hack a fake enchant onto the item
// Confirmed works on 1.8, 1.9, 1.10
// Does not work 1.11+ (minecraft ignores the glitched enchantment)
Expand Down

0 comments on commit ed1f103

Please sign in to comment.