Refactored NMS API Loading

This commit is contained in:
Auxilor 2020-12-11 18:27:50 +00:00
parent 2d11d614ab
commit 1f958e5eb9
7 changed files with 84 additions and 159 deletions

View File

@ -2,9 +2,10 @@ package com.willfp.ecoenchants.nms;
import com.willfp.ecoenchants.EcoEnchantsPlugin;
import com.willfp.ecoenchants.nms.api.BlockBreakWrapper;
import com.willfp.ecoenchants.util.internal.Logger;
import org.bukkit.Bukkit;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.ApiStatus;
/**
* Utility class to break a block as if the player had done it manually
@ -12,20 +13,6 @@ import org.jetbrains.annotations.ApiStatus;
public class BlockBreak {
private static BlockBreakWrapper blockBreakWrapper;
@ApiStatus.Internal
public static boolean init() {
try {
final Class<?> class2 = Class.forName("com.willfp.ecoenchants." + EcoEnchantsPlugin.NMS_VERSION + ".BlockBreak");
if (BlockBreakWrapper.class.isAssignableFrom(class2)) {
blockBreakWrapper = (BlockBreakWrapper) class2.getConstructor().newInstance();
}
} catch (Exception e) {
e.printStackTrace();
blockBreakWrapper = null;
}
return blockBreakWrapper != null;
}
/**
* Break a block as a player
*
@ -33,7 +20,18 @@ public class BlockBreak {
* @param block The block to break
*/
public static void breakBlock(Player player, Block block) {
assert blockBreakWrapper != null;
blockBreakWrapper.breakBlock(player, block);
}
static {
try {
final Class<?> class2 = Class.forName("com.willfp.ecoenchants." + EcoEnchantsPlugin.NMS_VERSION + ".BlockBreak");
if (BlockBreakWrapper.class.isAssignableFrom(class2)) {
blockBreakWrapper = (BlockBreakWrapper) class2.getConstructor().newInstance();
}
} catch (Exception e) {
Logger.error("&cYou're running an unsupported server version: " + EcoEnchantsPlugin.NMS_VERSION);
Bukkit.getPluginManager().disablePlugin(EcoEnchantsPlugin.getInstance());
}
}
}

View File

@ -3,8 +3,9 @@ package com.willfp.ecoenchants.nms;
import com.willfp.ecoenchants.EcoEnchantsPlugin;
import com.willfp.ecoenchants.nms.api.ChatComponentWrapper;
import com.willfp.ecoenchants.util.internal.Logger;
import org.bukkit.Bukkit;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.ApiStatus;
/**
* Utility class to manage chat components
@ -12,20 +13,6 @@ import org.jetbrains.annotations.ApiStatus;
public class ChatComponent {
private static ChatComponentWrapper chatComponentWrapper;
@ApiStatus.Internal
public static boolean init() {
try {
final Class<?> class2 = Class.forName("com.willfp.ecoenchants." + EcoEnchantsPlugin.NMS_VERSION + ".ChatComponent");
if (ChatComponentWrapper.class.isAssignableFrom(class2)) {
chatComponentWrapper = (ChatComponentWrapper) class2.getConstructor().newInstance();
}
} catch (Exception e) {
e.printStackTrace();
chatComponentWrapper = null;
}
return chatComponentWrapper != null;
}
/**
* Modify NMS chat component
* <p>
@ -35,7 +22,18 @@ public class ChatComponent {
* @return The NMS chat component, having been modified
*/
public static Object modifyComponent(Object object) {
assert chatComponentWrapper != null;
return chatComponentWrapper.modifyComponent(object);
}
static {
try {
final Class<?> class2 = Class.forName("com.willfp.ecoenchants." + EcoEnchantsPlugin.NMS_VERSION + ".ChatComponent");
if (ChatComponentWrapper.class.isAssignableFrom(class2)) {
chatComponentWrapper = (ChatComponentWrapper) class2.getConstructor().newInstance();
}
} catch (Exception e) {
Logger.error("&cYou're running an unsupported server version: " + EcoEnchantsPlugin.NMS_VERSION);
Bukkit.getPluginManager().disablePlugin(EcoEnchantsPlugin.getInstance());
}
}
}

View File

@ -3,8 +3,9 @@ package com.willfp.ecoenchants.nms;
import com.willfp.ecoenchants.EcoEnchantsPlugin;
import com.willfp.ecoenchants.nms.api.CooldownWrapper;
import com.willfp.ecoenchants.util.internal.Logger;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.ApiStatus;
/**
* Utility class to get the attack cooldown of a player
@ -12,20 +13,6 @@ import org.jetbrains.annotations.ApiStatus;
public class Cooldown {
private static CooldownWrapper cooldown;
@ApiStatus.Internal
public static boolean init() {
try {
final Class<?> class2 = Class.forName("com.willfp.ecoenchants." + EcoEnchantsPlugin.NMS_VERSION + ".Cooldown");
if (CooldownWrapper.class.isAssignableFrom(class2)) {
cooldown = (CooldownWrapper) class2.getConstructor().newInstance();
}
} catch (Exception e) {
e.printStackTrace();
cooldown = null;
}
return cooldown != null;
}
/**
* Get a player's attack cooldown
*
@ -33,7 +20,18 @@ public class Cooldown {
* @return A value between 0 and 1, with 1 representing max strength
*/
public static double getCooldown(Player player) {
assert cooldown != null;
return cooldown.getAttackCooldown(player);
}
static {
try {
final Class<?> class2 = Class.forName("com.willfp.ecoenchants." + EcoEnchantsPlugin.NMS_VERSION + ".Cooldown");
if (CooldownWrapper.class.isAssignableFrom(class2)) {
cooldown = (CooldownWrapper) class2.getConstructor().newInstance();
}
} catch (Exception e) {
Logger.error("&cYou're running an unsupported server version: " + EcoEnchantsPlugin.NMS_VERSION);
Bukkit.getPluginManager().disablePlugin(EcoEnchantsPlugin.getInstance());
}
}
}

View File

@ -2,8 +2,9 @@ package com.willfp.ecoenchants.nms;
import com.willfp.ecoenchants.EcoEnchantsPlugin;
import com.willfp.ecoenchants.nms.api.OpenInventoryWrapper;
import com.willfp.ecoenchants.util.internal.Logger;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.ApiStatus;
/**
* Utility class to get the NMS implementation of a players' currently open inventory
@ -11,20 +12,6 @@ import org.jetbrains.annotations.ApiStatus;
public class OpenInventory {
private static OpenInventoryWrapper openInventoryWrapper;
@ApiStatus.Internal
public static boolean init() {
try {
final Class<?> class2 = Class.forName("com.willfp.ecoenchants." + EcoEnchantsPlugin.NMS_VERSION + ".OpenInventory");
if (OpenInventoryWrapper.class.isAssignableFrom(class2)) {
openInventoryWrapper = (OpenInventoryWrapper) class2.getConstructor().newInstance();
}
} catch (Exception e) {
e.printStackTrace();
openInventoryWrapper = null;
}
return openInventoryWrapper != null;
}
/**
* Get the NMS container of the inventory
*
@ -32,7 +19,18 @@ public class OpenInventory {
* @return The NMS container
*/
public static Object getOpenInventory(Player player) {
assert openInventoryWrapper != null;
return openInventoryWrapper.getOpenInventory(player);
}
static {
try {
final Class<?> class2 = Class.forName("com.willfp.ecoenchants." + EcoEnchantsPlugin.NMS_VERSION + ".OpenInventory");
if (OpenInventoryWrapper.class.isAssignableFrom(class2)) {
openInventoryWrapper = (OpenInventoryWrapper) class2.getConstructor().newInstance();
}
} catch (Exception e) {
Logger.error("&cYou're running an unsupported server version: " + EcoEnchantsPlugin.NMS_VERSION);
Bukkit.getPluginManager().disablePlugin(EcoEnchantsPlugin.getInstance());
}
}
}

View File

@ -2,8 +2,9 @@ package com.willfp.ecoenchants.nms;
import com.willfp.ecoenchants.EcoEnchantsPlugin;
import com.willfp.ecoenchants.nms.api.RepairCostWrapper;
import com.willfp.ecoenchants.util.internal.Logger;
import org.bukkit.Bukkit;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.ApiStatus;
/**
* Utility class to get and set the anvil rework penalty of an item
@ -11,20 +12,6 @@ import org.jetbrains.annotations.ApiStatus;
public class RepairCost {
private static RepairCostWrapper repairCostWrapper;
@ApiStatus.Internal
public static boolean init() {
try {
final Class<?> class2 = Class.forName("com.willfp.ecoenchants." + EcoEnchantsPlugin.NMS_VERSION + ".RepairCost");
if (RepairCostWrapper.class.isAssignableFrom(class2)) {
repairCostWrapper = (RepairCostWrapper) class2.getConstructor().newInstance();
}
} catch (Exception e) {
e.printStackTrace();
repairCostWrapper = null;
}
return repairCostWrapper != null;
}
/**
* Get the rework penalty of an ItemStack
*
@ -32,7 +19,6 @@ public class RepairCost {
* @return The anvil rework penalty
*/
public static int getRepairCost(ItemStack itemStack) {
assert repairCostWrapper != null;
return repairCostWrapper.getRepairCost(itemStack);
}
@ -44,7 +30,18 @@ public class RepairCost {
* @return The ItemStack, with the repair cost set
*/
public static ItemStack setRepairCost(ItemStack itemStack, int cost) {
assert repairCostWrapper != null;
return repairCostWrapper.setRepairCost(itemStack, cost);
}
static {
try {
final Class<?> class2 = Class.forName("com.willfp.ecoenchants." + EcoEnchantsPlugin.NMS_VERSION + ".RepairCost");
if (RepairCostWrapper.class.isAssignableFrom(class2)) {
repairCostWrapper = (RepairCostWrapper) class2.getConstructor().newInstance();
}
} catch (Exception e) {
Logger.error("&cYou're running an unsupported server version: " + EcoEnchantsPlugin.NMS_VERSION);
Bukkit.getPluginManager().disablePlugin(EcoEnchantsPlugin.getInstance());
}
}
}

View File

@ -3,9 +3,10 @@ package com.willfp.ecoenchants.nms;
import com.willfp.ecoenchants.EcoEnchantsPlugin;
import com.willfp.ecoenchants.nms.api.TridentStackWrapper;
import com.willfp.ecoenchants.util.internal.Logger;
import org.bukkit.Bukkit;
import org.bukkit.entity.Trident;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.ApiStatus;
/**
* Utility class to get the {@link ItemStack} of a given {@link Trident}
@ -13,20 +14,6 @@ import org.jetbrains.annotations.ApiStatus;
public class TridentStack {
private static TridentStackWrapper tridentStackWrapper;
@ApiStatus.Internal
public static boolean init() {
try {
final Class<?> class2 = Class.forName("com.willfp.ecoenchants." + EcoEnchantsPlugin.NMS_VERSION + ".TridentStack");
if (TridentStackWrapper.class.isAssignableFrom(class2)) {
tridentStackWrapper = (TridentStackWrapper) class2.getConstructor().newInstance();
}
} catch (Exception e) {
e.printStackTrace();
tridentStackWrapper = null;
}
return tridentStackWrapper != null;
}
/**
* Get the {@link ItemStack} of a given {@link Trident}
*
@ -34,7 +21,18 @@ public class TridentStack {
* @return The ItemStack associated with the trident
*/
public static ItemStack getTridentStack(Trident trident) {
assert tridentStackWrapper != null;
return tridentStackWrapper.getTridentStack(trident);
}
static {
try {
final Class<?> class2 = Class.forName("com.willfp.ecoenchants." + EcoEnchantsPlugin.NMS_VERSION + ".TridentStack");
if (TridentStackWrapper.class.isAssignableFrom(class2)) {
tridentStackWrapper = (TridentStackWrapper) class2.getConstructor().newInstance();
}
} catch (Exception e) {
Logger.error("&cYou're running an unsupported server version: " + EcoEnchantsPlugin.NMS_VERSION);
Bukkit.getPluginManager().disablePlugin(EcoEnchantsPlugin.getInstance());
}
}
}

View File

@ -45,12 +45,6 @@ import com.willfp.ecoenchants.integrations.mcmmo.McmmoManager;
import com.willfp.ecoenchants.integrations.mcmmo.plugins.McmmoIntegrationImpl;
import com.willfp.ecoenchants.integrations.placeholder.PlaceholderManager;
import com.willfp.ecoenchants.integrations.placeholder.plugins.PlaceholderIntegrationPAPI;
import com.willfp.ecoenchants.nms.BlockBreak;
import com.willfp.ecoenchants.nms.ChatComponent;
import com.willfp.ecoenchants.nms.Cooldown;
import com.willfp.ecoenchants.nms.OpenInventory;
import com.willfp.ecoenchants.nms.RepairCost;
import com.willfp.ecoenchants.nms.TridentStack;
import com.willfp.ecoenchants.util.interfaces.Callable;
import com.willfp.ecoenchants.util.interfaces.EcoRunnable;
import com.willfp.ecoenchants.util.internal.updater.PlayerJoinListener;
@ -108,62 +102,6 @@ public class Loader {
Logger.info("");
/*
Load NMS
*/
Logger.info("Loading NMS APIs...");
if (Cooldown.init()) {
Logger.info("Cooldown: &aSUCCESS");
} else {
Logger.info("Cooldown: &cFAILURE");
Logger.error("&cAborting...");
Bukkit.getPluginManager().disablePlugin(EcoEnchantsPlugin.getInstance());
}
if (TridentStack.init()) {
Logger.info("Trident API: &aSUCCESS");
} else {
Logger.info("Trident API: &cFAILURE");
Logger.error("&cAborting...");
Bukkit.getPluginManager().disablePlugin(EcoEnchantsPlugin.getInstance());
}
if (BlockBreak.init()) {
Logger.info("Block Break: &aSUCCESS");
} else {
Logger.info("Block Break: &cFAILURE");
Logger.error("&cAborting...");
Bukkit.getPluginManager().disablePlugin(EcoEnchantsPlugin.getInstance());
}
if (RepairCost.init()) {
Logger.info("Repair Cost: &aSUCCESS");
} else {
Logger.info("Repair Cost: &cFAILURE");
Logger.error("&cAborting...");
Bukkit.getPluginManager().disablePlugin(EcoEnchantsPlugin.getInstance());
}
if (OpenInventory.init()) {
Logger.info("Open Inventory: &aSUCCESS");
} else {
Logger.info("Open Inventory: &cFAILURE");
Logger.error("&cAborting...");
Bukkit.getPluginManager().disablePlugin(EcoEnchantsPlugin.getInstance());
}
if (ChatComponent.init()) {
Logger.info("Chat Component: &aSUCCESS");
} else {
Logger.info("Chat Component: &cFAILURE");
Logger.error("&cAborting...");
Bukkit.getPluginManager().disablePlugin(EcoEnchantsPlugin.getInstance());
}
Logger.info("");
/*
Register Events
*/