Skip to content
This repository has been archived by the owner on Sep 9, 2024. It is now read-only.

Commit

Permalink
Add 1.16 support
Browse files Browse the repository at this point in the history
  • Loading branch information
Puyodead1 committed Jul 6, 2021
1 parent 3f7eeec commit c1286a4
Show file tree
Hide file tree
Showing 7 changed files with 214 additions and 5 deletions.
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,16 @@
<target>
<copy file="target/${project.name}-${project.version}.jar"
toFile="C:\Users\23562\Desktop\Plugin Test Server\1.17\plugins\${project.name}-${project.version}.jar"/>
<copy file="target/${project.name}-${project.version}.jar"
toFile="C:\Users\23562\Desktop\Plugin Test Server\1.16.5\plugins\${project.name}-${project.version}.jar"/>
<copy file="target/${project.name}-${project.version}.jar"
toFile="C:\Users\23562\Desktop\Plugin Test Server\1.16.4\plugins\${project.name}-${project.version}.jar"/>
<copy file="target/${project.name}-${project.version}.jar"
toFile="C:\Users\23562\Desktop\Plugin Test Server\1.16.3\plugins\${project.name}-${project.version}.jar"/>
<copy file="target/${project.name}-${project.version}.jar"
toFile="C:\Users\23562\Desktop\Plugin Test Server\1.16.2\plugins\${project.name}-${project.version}.jar"/>
<copy file="target/${project.name}-${project.version}.jar"
toFile="C:\Users\23562\Desktop\Plugin Test Server\1.16.1\plugins\${project.name}-${project.version}.jar"/>
</target>
</configuration>
<goals>
Expand Down
24 changes: 22 additions & 2 deletions src/main/java/me/puyodead/enchantcrystals/EnchantCrystals.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import me.puyodead.enchantcrystals.Events.ItemEnchantEvent;
import me.puyodead.enchantcrystals.Events.OpenInventoryEvent;
import me.puyodead.enchantcrystals.NMS.NMSBase;
import me.puyodead.enchantcrystals.NMS.NMS_v1_16;
import me.puyodead.enchantcrystals.NMS.NMS_v1_17_R1;
import me.puyodead.enchantcrystals.NMS.Version;
import org.bukkit.command.PluginCommand;
import org.bukkit.plugin.java.JavaPlugin;

Expand All @@ -22,9 +24,24 @@ public final class EnchantCrystals extends JavaPlugin {
@Override
public void onEnable() {
plugin = this;
nms = new NMS_v1_17_R1();

EnchantCrystalsUtils.sendConsole(PREFIX + "&b=============================================================");
if (Version.getCurrentVersion().isOlder(Version.v1_8_R3)) {
EnchantCrystalsUtils.sendConsole(PREFIX + "&cThis server is running 1.8.3 or older which is not supported, plugin will be disabled.");
getServer().getPluginManager().disablePlugin(this);
return;
}

switch (Version.getCurrentVersion()) {
case v1_17_R1:
nms = new NMS_v1_17_R1();
break;
case v1_16_R1:
case v1_16_R2:
case v1_16_R3:
nms = new NMS_v1_16();
break;
}

initConfig();
initEvents();
Expand All @@ -34,12 +51,15 @@ public void onEnable() {
EnchantCrystalsUtils.sendConsole(PREFIX + "&bAuthor: &ePuyodead1");
EnchantCrystalsUtils.sendConsole(PREFIX + "&b" + this.getName() + " Version: &e" + Objects.requireNonNull(getServer().getPluginManager().getPlugin(this.getDescription().getName())).getDescription().getVersion());
EnchantCrystalsUtils.sendConsole(PREFIX + "&bMinecraft Version: &e" + getServer().getVersion());
EnchantCrystalsUtils.sendConsole(PREFIX + "&bBukkit Version: &e" + getServer().getBukkitVersion());
EnchantCrystalsUtils.sendConsole(PREFIX + "&bNMS Version: &e" + Version.getCurrentVersion().getVersionInteger());
EnchantCrystalsUtils.sendConsole(PREFIX + "&b=============================================================");
}

@Override
public void onDisable() {
//
plugin = null;
nms = null;
}

public void initConfig() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,4 @@ public static boolean isCrystal(final NBTItem nbtItem) {
// TODO: Since we add custom NBT to tell if an item is a crystal, we could convert crystal materials if they are changed in the config
return nbtItem.hasKey("enchantcrystals:isEnchantCrystal");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ public void onEnchantItemEvent(EnchantItemEvent e) {

// play enchantment sound
e.getEnchanter().getWorld().playSound(e.getEnchanter().getLocation(), Sound.BLOCK_ENCHANTMENT_TABLE_USE, 1.0f, 1.0f);

// TODO: reroll enchants

try {
final int cost = e.getEnchanter().getGameMode().equals(GameMode.CREATIVE) ? 0 : e.getExpLevelCost();
plugin.getNMS().onEnchantmentPerformed(e.getEnchanter(), cost, e.getView());
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/me/puyodead/enchantcrystals/NMS/NMS_v1_16.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package me.puyodead.enchantcrystals.NMS;

import org.bukkit.entity.Player;
import org.bukkit.inventory.InventoryView;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

/**
* 1.16.4-1.16.5
*/
public class NMS_v1_16 implements NMSBase {

@Override
public void onEnchantmentPerformed(Player player, int cost, InventoryView view) throws ClassNotFoundException, InvocationTargetException, NoSuchMethodException, IllegalAccessException, NoSuchFieldException {
Class<?> CraftInventoryView = ReflectionUtil.getOBCClass("inventory.CraftInventoryView");
Class<?> CraftPlayer = ReflectionUtil.getOBCClass("entity.CraftPlayer");
Class<?> ItemStack = ReflectionUtil.getNMSClass("ItemStack");

// get the entity player
Object craftPlayer = CraftPlayer.cast(player);
Object entityPlayer = ReflectionUtil.getHandle(craftPlayer);

// get container as EnchantMenu
Object container = CraftInventoryView.cast(view);
Object enchantmentMenu = ReflectionUtil.getHandle(container);

// change the enchantment seed
ReflectionUtil.invokeMethod(entityPlayer, "enchantDone", new Class[]{ItemStack, int.class}, new Object[]{null, cost});

Object newEnchantmentSeed = ReflectionUtil.getField(entityPlayer, "bG");

// change enchantment seed on enchant menu container property
Field enchantmentSeedField = enchantmentMenu.getClass().getDeclaredField("i"); // container property
enchantmentSeedField.setAccessible(true);
Object dataSlot = enchantmentSeedField.get(enchantmentMenu);

ReflectionUtil.invokeMethod(dataSlot, "set", new Class[]{int.class}, new Object[]{newEnchantmentSeed});
}
}
11 changes: 11 additions & 0 deletions src/main/java/me/puyodead/enchantcrystals/NMS/ReflectionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ public static Class<?> getOBCClass(String name) throws ClassNotFoundException {
return Class.forName("org.bukkit.craftbukkit." + getVersion() + "." + name);
}

/**
* Gets a class from net.minecraft.server (1.16 and lower)
*
* @param name class name
* @return
* @throws ClassNotFoundException
*/
public static Class<?> getNMSClass(String name) throws ClassNotFoundException {
return Class.forName("net.minecraft.server." + getVersion() + "." + name);
}

public static Object getHandle(Object obj) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
return invokeMethod(obj, "getHandle", new Class[0], new Object[0]);
}
Expand Down
130 changes: 130 additions & 0 deletions src/main/java/me/puyodead/enchantcrystals/NMS/Version.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package me.puyodead.enchantcrystals.NMS;

import org.bukkit.Bukkit;

public enum Version {
TOO_OLD(-1),
v1_7_R1(171), v1_7_R2(172), v1_7_R3(173), v1_7_R4(174),
v1_8_R1(181), v1_8_R2(182), v1_8_R3(183),
v1_9_R1(191), v1_9_R2(192),
v1_10_R1(1101),
v1_11_R1(1111),
v1_12_R1(1121),
v1_13_R2(1132),
v1_14_R1(1141),
v1_15_R1(1151),
v1_16_R1(1161), v1_16_R2(1162), v1_16_R3(1163),
v1_17_R1(1171),
TOO_NEW(-2);

private static Version currentVersion;
private static Version latest;
private int versionInteger;

private Version(int versionInteger) {
this.versionInteger = versionInteger;
}

/**
* @return Get the server's Minecraft version.
*/
public static Version getCurrentVersion() {
if (currentVersion == null) {
String ver = Bukkit.getServer().getClass().getPackage().getName();
int v = Integer.parseInt(ver.substring(ver.lastIndexOf('.') + 1).replace("_", "").replace("R", "").replace("v", ""));
for (Version version : values()) {
if (version.getVersionInteger() == v) {
currentVersion = version;
break;
}
}
if (v > Version.getLatestVersion().getVersionInteger()) {
currentVersion = Version.getLatestVersion();
}
if (currentVersion == null) {
currentVersion = Version.TOO_NEW;
}
}
return currentVersion;
}

/**
* Get the latest version allowed by the Version class.
*
* @return The latest version.
*/
public static Version getLatestVersion() {
if (latest == null) {
Version v = Version.TOO_OLD;
for (Version version : values()) {
if (version.comparedTo(v) == 1) {
v = version;
}
}
return v;
} else {
return latest;

}
}

/**
* @return The server's minecraft version as an integer.
*/
public int getVersionInteger() {
return this.versionInteger;
}

/**
* This checks if the current version is older, newer, or is the checked version.
*
* @param version The version you are checking.
* @return -1 if older, 0 if the same, and 1 if newer.
*/
public int comparedTo(Version version) {
int result = -1;
int current = this.getVersionInteger();
int check = version.getVersionInteger();
if (current > check || check == -2) {// check is newer then current
result = 1;
} else if (current == check) {// check is the same as current
result = 0;
} else if (check == -1) {// check is older then current
result = -1;
}
return result;
}

/**
* Checks to see if the current version is newer then the checked version.
*
* @param version The version you are checking.
* @return True if newer then the checked version and false if the same or older.
*/
public static boolean isNewer(Version version) {
if (currentVersion == null) getCurrentVersion();
return currentVersion.versionInteger > version.versionInteger || currentVersion.versionInteger == -2;
}

/**
* Checks to see if the current version is the same as the checked version.
*
* @param version The version you are checking.
* @return True if both the current and checked version is the same and false if otherwise.
*/
public static boolean isSame(Version version) {
if (currentVersion == null) getCurrentVersion();
return currentVersion.versionInteger == version.versionInteger;
}

/**
* Checks to see if the current version is older then the checked version.
*
* @param version The version you are checking.
* @return True if older then the checked version and false if the same or newer.
*/
public static boolean isOlder(Version version) {
if (currentVersion == null) getCurrentVersion();
return currentVersion.versionInteger < version.versionInteger || currentVersion.versionInteger == -1;
}
}

0 comments on commit c1286a4

Please sign in to comment.