Migrate reflection in MetaItemStack to a provider (#4592)

This commit is contained in:
Josh Roy 2021-10-24 10:17:06 -04:00 committed by GitHub
parent ce88c0e1e4
commit 70045c30f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 82 additions and 35 deletions

View File

@ -22,6 +22,9 @@ dependencies {
exclude group: "org.bukkit", module: "bukkit"
}
api(project(':providers:1_8Provider')) {
exclude group: "org.spigotmc", module: "spigot"
}
api(project(':providers:1_12Provider')) {
exclude group: "org.bukkit", module: "bukkit"
}
}
@ -40,6 +43,7 @@ shadowJar {
include (project(':providers:PaperProvider'))
include (project(':providers:NMSReflectionProvider'))
include (project(':providers:1_8Provider'))
include (project(':providers:1_12Provider'))
}
relocate 'io.papermc.lib', 'com.earth2me.essentials.paperlib'
relocate 'org.bstats', 'com.earth2me.essentials.libs.bstats'

View File

@ -57,6 +57,7 @@ import net.ess3.nms.refl.providers.ReflSpawnerBlockProvider;
import net.ess3.nms.refl.providers.ReflSyncCommandsProvider;
import net.ess3.provider.ContainerProvider;
import net.ess3.provider.FormattedCommandAliasProvider;
import net.ess3.provider.ItemUnbreakableProvider;
import net.ess3.provider.KnownCommandsProvider;
import net.ess3.provider.MaterialTagProvider;
import net.ess3.provider.PersistentDataProvider;
@ -73,8 +74,10 @@ import net.ess3.provider.providers.BlockMetaSpawnerItemProvider;
import net.ess3.provider.providers.BukkitMaterialTagProvider;
import net.ess3.provider.providers.BukkitSpawnerBlockProvider;
import net.ess3.provider.providers.FlatSpawnEggProvider;
import net.ess3.provider.providers.LegacyItemUnbreakableProvider;
import net.ess3.provider.providers.LegacyPotionMetaProvider;
import net.ess3.provider.providers.LegacySpawnEggProvider;
import net.ess3.provider.providers.ModernItemUnbreakableProvider;
import net.ess3.provider.providers.ModernPersistentDataProvider;
import net.ess3.provider.providers.PaperContainerProvider;
import net.ess3.provider.providers.PaperKnownCommandsProvider;
@ -166,6 +169,7 @@ public class Essentials extends JavaPlugin implements net.ess3.api.IEssentials {
private transient SyncCommandsProvider syncCommandsProvider;
private transient PersistentDataProvider persistentDataProvider;
private transient ReflOnlineModeProvider onlineModeProvider;
private transient ItemUnbreakableProvider unbreakableProvider;
private transient Kits kits;
private transient RandomTeleport randomTeleport;
private transient UpdateChecker updateChecker;
@ -409,6 +413,12 @@ public class Essentials extends JavaPlugin implements net.ess3.api.IEssentials {
onlineModeProvider = new ReflOnlineModeProvider();
if (VersionUtil.getServerBukkitVersion().isHigherThanOrEqualTo(VersionUtil.v1_11_2_R01)) {
unbreakableProvider = new ModernItemUnbreakableProvider();
} else {
unbreakableProvider = new LegacyItemUnbreakableProvider();
}
execTimer.mark("Init(Providers)");
reload();
@ -1269,6 +1279,11 @@ public class Essentials extends JavaPlugin implements net.ess3.api.IEssentials {
return onlineModeProvider;
}
@Override
public ItemUnbreakableProvider getItemUnbreakableProvider() {
return unbreakableProvider;
}
@Override
public PluginCommand getPluginCommand(final String cmd) {
return this.getCommand(cmd);

View File

@ -10,6 +10,7 @@ import com.earth2me.essentials.updatecheck.UpdateChecker;
import net.ess3.nms.refl.providers.ReflOnlineModeProvider;
import net.ess3.provider.ContainerProvider;
import net.ess3.provider.FormattedCommandAliasProvider;
import net.ess3.provider.ItemUnbreakableProvider;
import net.ess3.provider.KnownCommandsProvider;
import net.ess3.provider.MaterialTagProvider;
import net.ess3.provider.PersistentDataProvider;
@ -159,5 +160,7 @@ public interface IEssentials extends Plugin {
ReflOnlineModeProvider getOnlineModeProvider();
ItemUnbreakableProvider getItemUnbreakableProvider();
PluginCommand getPluginCommand(String cmd);
}

View File

@ -33,7 +33,6 @@ import org.bukkit.potion.Potion;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
@ -48,9 +47,6 @@ import static com.earth2me.essentials.I18n.tl;
public class MetaItemStack {
private static final Map<String, DyeColor> colorMap = new HashMap<>();
private static final Map<String, FireworkEffect.Type> fireworkShape = new HashMap<>();
private static int bukkitUnbreakableSupport = -1;
private static Method spigotMethod;
private static Method setUnbreakableMethod;
private static boolean useNewSkullMethod = true;
static {
@ -212,7 +208,7 @@ public class MetaItemStack {
stack.setItemMeta(meta);
} else if (split[0].equalsIgnoreCase("unbreakable") && hasMetaPermission(sender, "unbreakable", false, true, ess)) {
final boolean value = split.length <= 1 || Boolean.parseBoolean(split[1]);
setUnbreakable(stack, value);
setUnbreakable(ess, stack, value);
} else if (split.length > 1 && (split[0].equalsIgnoreCase("player") || split[0].equalsIgnoreCase("owner")) && hasMetaPermission(sender, "head", false, true, ess)) {
if (MaterialUtil.isPlayerHead(stack.getType(), stack.getDurability())) {
final String owner = split[1];
@ -686,35 +682,9 @@ public class MetaItemStack {
}
}
private void setUnbreakable(final ItemStack is, final boolean unbreakable) {
private void setUnbreakable(final IEssentials ess, final ItemStack is, final boolean unbreakable) {
final ItemMeta meta = is.getItemMeta();
try {
if (bukkitUnbreakableSupport == -1) {
try {
ItemMeta.class.getDeclaredMethod("setUnbreakable", boolean.class);
bukkitUnbreakableSupport = 1;
} catch (final NoSuchMethodException | SecurityException ex) {
bukkitUnbreakableSupport = 0;
}
}
if (bukkitUnbreakableSupport == 1) {
meta.setUnbreakable(unbreakable);
} else {
if (spigotMethod == null) {
spigotMethod = meta.getClass().getDeclaredMethod("spigot");
spigotMethod.setAccessible(true);
}
final Object itemStackSpigot = spigotMethod.invoke(meta);
if (setUnbreakableMethod == null) {
setUnbreakableMethod = itemStackSpigot.getClass().getDeclaredMethod("setUnbreakable", Boolean.TYPE);
setUnbreakableMethod.setAccessible(true);
}
setUnbreakableMethod.invoke(itemStackSpigot, unbreakable);
}
ess.getItemUnbreakableProvider().setUnbreakable(meta, unbreakable);
is.setItemMeta(meta);
} catch (final Throwable t) {
t.printStackTrace();
}
}
}

View File

@ -0,0 +1,12 @@
plugins {
id("essentials.base-conventions")
}
dependencies {
api project(':providers:NMSReflectionProvider')
}
essentials {
injectBukkitApi.set(false)
injectBstats.set(false)
}

View File

@ -3,7 +3,10 @@ plugins {
}
dependencies {
api project(':providers:NMSReflectionProvider')
implementation(project(':providers:BaseProviders')) {
exclude group: "org.spigotmc", module: "spigot-api"
}
implementation 'org.spigotmc:spigot-api:1.8.8-R0.1-SNAPSHOT'
}
essentials {

View File

@ -0,0 +1,16 @@
package net.ess3.provider.providers;
import net.ess3.provider.ItemUnbreakableProvider;
import org.bukkit.inventory.meta.ItemMeta;
public class LegacyItemUnbreakableProvider implements ItemUnbreakableProvider {
@Override
public void setUnbreakable(ItemMeta meta, boolean unbreakable) {
meta.spigot().setUnbreakable(unbreakable);
}
@Override
public String getDescription() {
return "Legacy ItemMeta Unbreakable Provider";
}
}

View File

@ -0,0 +1,7 @@
package net.ess3.provider;
import org.bukkit.inventory.meta.ItemMeta;
public interface ItemUnbreakableProvider extends Provider {
void setUnbreakable(ItemMeta meta, boolean unbreakable);
}

View File

@ -0,0 +1,16 @@
package net.ess3.provider.providers;
import net.ess3.provider.ItemUnbreakableProvider;
import org.bukkit.inventory.meta.ItemMeta;
public class ModernItemUnbreakableProvider implements ItemUnbreakableProvider {
@Override
public void setUnbreakable(ItemMeta meta, boolean unbreakable) {
meta.setUnbreakable(unbreakable);
}
@Override
public String getDescription() {
return "1.11+ ItemMeta Unbreakable Provider";
}
}

View File

@ -50,3 +50,4 @@ include(":providers:BaseProviders")
include(":providers:NMSReflectionProvider")
include(":providers:PaperProvider")
include(":providers:1_8Provider")
include(":providers:1_12Provider")