Really should rewrite this whole thing at a later date

This commit is contained in:
nossr50 2021-01-26 16:00:08 -08:00
parent 9a5b1406b3
commit 68c7de16a8
5 changed files with 287 additions and 284 deletions

View File

@ -1,7 +1,6 @@
Version 2.1.174 Version 2.1.174
Some legacy color codes in our locale file were swapped to &-code equivalents (thanks ViaSnake) Some legacy color codes in our locale file were swapped to &-code equivalents (thanks ViaSnake)
Updated hu_HU locale (thanks andris155) Updated hu_HU locale (thanks andris155)
Version 2.1.173 Version 2.1.173
The experience orbs dropped by Knock on Wood rank 2 during Tree Feller are now much less frequent but provide more XP The experience orbs dropped by Knock on Wood rank 2 during Tree Feller are now much less frequent but provide more XP
mcMMO no longer damages fishing rods or drains a player's hunger when fishing mcMMO no longer damages fishing rods or drains a player's hunger when fishing

View File

@ -14,6 +14,7 @@ import com.gmail.nossr50.util.nms.NMSVersion;
import com.gmail.nossr50.util.platform.MinecraftGameVersion; import com.gmail.nossr50.util.platform.MinecraftGameVersion;
import com.gmail.nossr50.util.text.StringUtils; import com.gmail.nossr50.util.text.StringUtils;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.HashMap; import java.util.HashMap;
@ -102,7 +103,6 @@ public class CompatibilityManager {
private void initPersistentDataLayer() { private void initPersistentDataLayer() {
if(minecraftGameVersion.getMinorVersion().asInt() >= 14 || minecraftGameVersion.getMajorVersion().asInt() >= 2) { if(minecraftGameVersion.getMinorVersion().asInt() >= 14 || minecraftGameVersion.getMajorVersion().asInt() >= 2) {
persistentDataLayer = new SpigotPersistentDataLayer_1_14(); persistentDataLayer = new SpigotPersistentDataLayer_1_14();
} else { } else {
@ -138,11 +138,11 @@ public class CompatibilityManager {
return isFullyCompatibleServerSoftware; return isFullyCompatibleServerSoftware;
} }
public NMSVersion getNmsVersion() { public @NotNull NMSVersion getNmsVersion() {
return nmsVersion; return nmsVersion;
} }
private NMSVersion determineNMSVersion() { private @NotNull NMSVersion determineNMSVersion() {
if (minecraftGameVersion.getMajorVersion().asInt() == 1) { if (minecraftGameVersion.getMajorVersion().asInt() == 1) {
switch (minecraftGameVersion.getMinorVersion().asInt()) { switch (minecraftGameVersion.getMinorVersion().asInt()) {
case 12: case 12:
@ -160,8 +160,10 @@ public class CompatibilityManager {
return NMSVersion.NMS_1_16_2; return NMSVersion.NMS_1_16_2;
} else if(minecraftGameVersion.getPatchVersion().asInt() == 3) { } else if(minecraftGameVersion.getPatchVersion().asInt() == 3) {
return NMSVersion.NMS_1_16_3; return NMSVersion.NMS_1_16_3;
} else if(minecraftGameVersion.getPatchVersion().asInt() >= 4) { } else if(minecraftGameVersion.getPatchVersion().asInt() == 4) {
return NMSVersion.NMS_1_16_4; return NMSVersion.NMS_1_16_4;
} else if(minecraftGameVersion.getPatchVersion().asInt() >= 5) {
return NMSVersion.NMS_1_16_5;
} }
} }
} }

View File

@ -1,42 +1,42 @@
package com.gmail.nossr50.util.compat.layers.attackcooldown; //package com.gmail.nossr50.util.compat.layers.attackcooldown;
//
import com.gmail.nossr50.util.nms.NMSVersion; //import com.gmail.nossr50.util.nms.NMSVersion;
import org.bukkit.entity.Player; //import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull; //import org.jetbrains.annotations.NotNull;
//
import java.lang.reflect.InvocationTargetException; //import java.lang.reflect.InvocationTargetException;
//
public class DummyPlayerAttackCooldownToolLayer extends PlayerAttackCooldownToolLayer { //public class DummyPlayerAttackCooldownToolLayer extends PlayerAttackCooldownToolLayer {
public DummyPlayerAttackCooldownToolLayer() { // public DummyPlayerAttackCooldownToolLayer() {
super(NMSVersion.UNSUPPORTED); // super(NMSVersion.UNSUPPORTED);
} // }
//
@Override // @Override
public boolean initializeLayer() { // public boolean initializeLayer() {
return noErrorsOnInitialize; // return noErrorsOnInitialize;
} // }
//
@Override // @Override
public float getAttackStrength(@NotNull Player player) throws InvocationTargetException, IllegalAccessException { // public float getAttackStrength(@NotNull Player player) throws InvocationTargetException, IllegalAccessException {
return 1.0F; //Always full strength // return 1.0F; //Always full strength
} // }
//
@Override // @Override
public float getCooldownValue(@NotNull Player player) throws InvocationTargetException, IllegalAccessException { // public float getCooldownValue(@NotNull Player player) throws InvocationTargetException, IllegalAccessException {
return 0F; // return 0F;
} // }
//
@Override // @Override
public void resetAttackStrength(@NotNull Player player) throws InvocationTargetException, IllegalAccessException { // public void resetAttackStrength(@NotNull Player player) throws InvocationTargetException, IllegalAccessException {
//Do nothing // //Do nothing
} // }
//
@Override // @Override
public int getCooldownFieldValue(@NotNull Player player) throws InvocationTargetException, IllegalAccessException { // public int getCooldownFieldValue(@NotNull Player player) throws InvocationTargetException, IllegalAccessException {
return 0; // return 0;
} // }
//
@Override // @Override
public void setCooldownFieldValue(@NotNull Player player, int fieldValue) throws InvocationTargetException, IllegalAccessException { // public void setCooldownFieldValue(@NotNull Player player, int fieldValue) throws InvocationTargetException, IllegalAccessException {
} // }
} //}

View File

@ -1,237 +1,238 @@
package com.gmail.nossr50.util.compat.layers.attackcooldown; //package com.gmail.nossr50.util.compat.layers.attackcooldown;
//
import com.gmail.nossr50.mcMMO; //import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.util.compat.layers.AbstractNMSCompatibilityLayer; //import com.gmail.nossr50.util.compat.layers.AbstractNMSCompatibilityLayer;
import com.gmail.nossr50.util.nms.NMSConstants; //import com.gmail.nossr50.util.nms.NMSConstants;
import com.gmail.nossr50.util.nms.NMSVersion; //import com.gmail.nossr50.util.nms.NMSVersion;
import org.bukkit.entity.Player; //import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull; //import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; //import org.jetbrains.annotations.Nullable;
//
import java.lang.reflect.Field; //import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException; //import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; //import java.lang.reflect.Method;
//
/** ///**
* // *
* These classes are a band-aid solution for adding NMS support into 2.1.XXX // * These classes are a band-aid solution for adding NMS support into 2.1.XXX
* In 2.2 we are switching to modules and that will clean things up significantly // * In 2.2 we are switching to modules and that will clean things up significantly
* // *
*/ // */
public class PlayerAttackCooldownToolLayer extends AbstractNMSCompatibilityLayer implements PlayerAttackCooldownMethods { //public class PlayerAttackCooldownToolLayer extends AbstractNMSCompatibilityLayer implements PlayerAttackCooldownMethods {
//
private final String cbNMSVersionPath; // private final String cbNMSVersionPath;
//
protected Class<?> craftPlayerClass; // protected Class<?> craftPlayerClass;
protected Class<?> entityHumanClass; // protected Class<?> entityHumanClass;
protected Class<?> entityLivingClass; // protected Class<?> entityLivingClass;
//
protected Method playerAttackCooldownMethod; // protected Method playerAttackCooldownMethod;
protected Method playerAttackStrengthMethod; // protected Method playerAttackStrengthMethod;
protected Method resetPlayerAttackCooldownMethod; // protected Method resetPlayerAttackCooldownMethod;
protected Method setPlayerAttackStrengthMethod; // protected Method setPlayerAttackStrengthMethod;
protected Method getHandleMethod; // protected Method getHandleMethod;
protected Field attackCooldownField; // protected Field attackCooldownField;
protected String attackStrengthFieldName; // protected String attackStrengthFieldName;
//
public PlayerAttackCooldownToolLayer(@NotNull NMSVersion nmsVersion) { // public PlayerAttackCooldownToolLayer(@NotNull NMSVersion nmsVersion) {
super(nmsVersion); // super(nmsVersion);
mcMMO.p.getLogger().info("Loading Compatibility Layer... (Player Attack Cooldown Exploit Prevention)"); // mcMMO.p.getLogger().info("Loading Compatibility Layer... (Player Attack Cooldown Exploit Prevention)");
if(!isCompatibleWithMinecraftVersion(nmsVersion)) { // if(!isCompatibleWithMinecraftVersion(nmsVersion)) {
mcMMO.p.getLogger().severe("this version of mcMMO does not support NMS for this version of Minecraft, try updating mcMMO or updating Minecraft. Not all versions of Minecraft will have NMS support built into mcMMO."); // mcMMO.p.getLogger().severe("this version of mcMMO does not support NMS for this version of Minecraft, try updating mcMMO or updating Minecraft. Not all versions of Minecraft will have NMS support built into mcMMO.");
cbNMSVersionPath = ""; // cbNMSVersionPath = "";
} else { // } else {
if(NMSConstants.getCraftBukkitVersionPath(nmsVersion) != null) { // if(NMSConstants.getCraftBukkitVersionPath(nmsVersion) != null) {
cbNMSVersionPath = NMSConstants.getCraftBukkitVersionPath(nmsVersion); // cbNMSVersionPath = NMSConstants.getCraftBukkitVersionPath(nmsVersion);
noErrorsOnInitialize = initializeLayer(); // noErrorsOnInitialize = initializeLayer();
//
if(noErrorsOnInitialize) { // if(noErrorsOnInitialize) {
mcMMO.p.getLogger().info("Successfully Loaded Compatibility Layer! (Player Attack Cooldown Exploit Prevention)"); // mcMMO.p.getLogger().info("Successfully Loaded Compatibility Layer! (Player Attack Cooldown Exploit Prevention)");
} // }
} else { // } else {
mcMMO.p.getLogger().info("Failed to load - CL (Player Attack Cooldown Exploit Prevention) Could not find CB NMS path for CL"); // mcMMO.p.getLogger().info("Failed to load - CL (Player Attack Cooldown Exploit Prevention) Could not find CB NMS path for CL");
flagErrorsDuringStartup(); // flagErrorsDuringStartup();
mcMMO.p.getLogger().warning("Could not wire NMS package path for CraftBukkit!"); // mcMMO.p.getLogger().warning("Could not wire NMS package path for CraftBukkit!");
cbNMSVersionPath = ""; // cbNMSVersionPath = "";
} // }
} // }
} // }
//
public static boolean isCompatibleWithMinecraftVersion(@NotNull NMSVersion nmsVersion) { // public static boolean isCompatibleWithMinecraftVersion(@NotNull NMSVersion nmsVersion) {
switch(nmsVersion) { // switch(nmsVersion) {
case NMS_1_13_2: // case NMS_1_13_2:
case NMS_1_14_4: // case NMS_1_14_4:
case NMS_1_15_2: // case NMS_1_15_2:
case NMS_1_16_4: // case NMS_1_16_4:
return true; // case NMS_1_16_5:
default: // return true;
return false; // default:
} // return false;
} // }
// }
/** //
* Cache all reflection methods/types/classes needed for the NMS of this CompatibilityLayer // /**
* @param cooldownMethodName the cooldown method name // * Cache all reflection methods/types/classes needed for the NMS of this CompatibilityLayer
* @param attackStrengthMethodName the attack strength method name // * @param cooldownMethodName the cooldown method name
* @param resetAttackCooldownMethodName the reset attack cooldown method name // * @param attackStrengthMethodName the attack strength method name
* @param getHandleMethodName the get handle method name // * @param resetAttackCooldownMethodName the reset attack cooldown method name
* @return true if NMS was successfully wired // * @param getHandleMethodName the get handle method name
*/ // * @return true if NMS was successfully wired
public boolean wireNMS(@NotNull String cooldownMethodName, @NotNull String attackStrengthMethodName, @NotNull String resetAttackCooldownMethodName, @NotNull String getHandleMethodName, @NotNull String attackStrengthFieldName) { // */
entityHumanClass = initEntityHumanClass(); // public boolean wireNMS(@NotNull String cooldownMethodName, @NotNull String attackStrengthMethodName, @NotNull String resetAttackCooldownMethodName, @NotNull String getHandleMethodName, @NotNull String attackStrengthFieldName) {
// entityHumanClass = initEntityHumanClass();
if (entityHumanClass != null) { //
entityLivingClass = entityHumanClass.getSuperclass(); // if (entityHumanClass != null) {
} // entityLivingClass = entityHumanClass.getSuperclass();
// }
craftPlayerClass = initCraftPlayerClass(); //
this.attackStrengthFieldName = attackStrengthFieldName; // craftPlayerClass = initCraftPlayerClass();
// this.attackStrengthFieldName = attackStrengthFieldName;
try { //
this.attackCooldownField = entityLivingClass.getDeclaredField(attackStrengthFieldName); // try {
this.attackCooldownField.setAccessible(true); // this.attackCooldownField = entityLivingClass.getDeclaredField(attackStrengthFieldName);
} catch (NoSuchFieldException e) { // this.attackCooldownField.setAccessible(true);
e.printStackTrace(); // } catch (NoSuchFieldException e) {
} // e.printStackTrace();
// }
try { //
this.playerAttackCooldownMethod = entityHumanClass.getMethod(cooldownMethodName); // try {
this.playerAttackStrengthMethod = entityHumanClass.getMethod(attackStrengthMethodName, float.class); // this.playerAttackCooldownMethod = entityHumanClass.getMethod(cooldownMethodName);
this.resetPlayerAttackCooldownMethod = entityHumanClass.getMethod(resetAttackCooldownMethodName); // this.playerAttackStrengthMethod = entityHumanClass.getMethod(attackStrengthMethodName, float.class);
// this.resetPlayerAttackCooldownMethod = entityHumanClass.getMethod(resetAttackCooldownMethodName);
if (craftPlayerClass != null) { //
this.getHandleMethod = craftPlayerClass.getMethod(getHandleMethodName); // if (craftPlayerClass != null) {
} else { // this.getHandleMethod = craftPlayerClass.getMethod(getHandleMethodName);
return false; // } else {
} // return false;
return true; // }
} catch (NoSuchMethodException e) { // return true;
flagErrorsDuringStartup(); // } catch (NoSuchMethodException e) {
e.printStackTrace(); // flagErrorsDuringStartup();
return false; // e.printStackTrace();
} // return false;
} // }
// }
/** //
* Get the cached player attack cooldown method // /**
* @return the cached player attack cooldown method // * Get the cached player attack cooldown method
*/ // * @return the cached player attack cooldown method
private @Nullable Method getPlayerAttackCooldownMethod() { // */
return playerAttackCooldownMethod; // private @Nullable Method getPlayerAttackCooldownMethod() {
} // return playerAttackCooldownMethod;
// }
/** //
* Get the cached player attack strength method // /**
* @return the cached player attack strength method // * Get the cached player attack strength method
*/ // * @return the cached player attack strength method
private @Nullable Method getPlayerAttackStrengthMethod() { // */
return playerAttackStrengthMethod; // private @Nullable Method getPlayerAttackStrengthMethod() {
} // return playerAttackStrengthMethod;
// }
/** //
* Get the cached player attack cooldown reset method // /**
* @return the cached player attack cooldown reset method // * Get the cached player attack cooldown reset method
*/ // * @return the cached player attack cooldown reset method
private @Nullable Method getResetPlayerAttackCooldownMethod() { // */
return resetPlayerAttackCooldownMethod; // private @Nullable Method getResetPlayerAttackCooldownMethod() {
} // return resetPlayerAttackCooldownMethod;
// }
/** //
* Grab the CraftPlayer class type from NMS // /**
* @return the CraftPlayer class type from NMS // * Grab the CraftPlayer class type from NMS
*/ // * @return the CraftPlayer class type from NMS
private @Nullable Class<?> initCraftPlayerClass() { // */
try { // private @Nullable Class<?> initCraftPlayerClass() {
return Class.forName(NMSConstants.getCraftPlayerClassPath(cbNMSVersionPath)); // try {
} catch (ClassNotFoundException e) { // return Class.forName(NMSConstants.getCraftPlayerClassPath(cbNMSVersionPath));
flagErrorsDuringStartup(); // } catch (ClassNotFoundException e) {
e.printStackTrace(); // flagErrorsDuringStartup();
return null; // e.printStackTrace();
} // return null;
} // }
// }
/** //
* Grab the EntityHuman class type from NMS // /**
* @return the EntityHuman class type from NMS // * Grab the EntityHuman class type from NMS
*/ // * @return the EntityHuman class type from NMS
private @Nullable Class<?> initEntityHumanClass() { // */
try { // private @Nullable Class<?> initEntityHumanClass() {
return Class.forName(NMSConstants.getEntityHumanClassPath(cbNMSVersionPath)); // try {
} catch (ClassNotFoundException e) { // return Class.forName(NMSConstants.getEntityHumanClassPath(cbNMSVersionPath));
flagErrorsDuringStartup(); // } catch (ClassNotFoundException e) {
e.printStackTrace(); // flagErrorsDuringStartup();
return null; // e.printStackTrace();
} // return null;
} // }
// }
private void flagErrorsDuringStartup() { //
noErrorsOnInitialize = false; // private void flagErrorsDuringStartup() {
} // noErrorsOnInitialize = false;
// }
/** //
* Grabs the attack strength for a player // /**
* Should be noted that as of today there is no way to capture a players current attack strength in spigot when they attack an entity outside of network packet listening // * Grabs the attack strength for a player
* @param player target player // * Should be noted that as of today there is no way to capture a players current attack strength in spigot when they attack an entity outside of network packet listening
* @return the float value of the player's attack strength // * @param player target player
*/ // * @return the float value of the player's attack strength
@Override // */
public float getAttackStrength(@NotNull Player player) throws InvocationTargetException, IllegalAccessException { // @Override
Object craftPlayer = craftPlayerClass.cast(player); // public float getAttackStrength(@NotNull Player player) throws InvocationTargetException, IllegalAccessException {
Object entityHuman = entityHumanClass.cast(getHandleMethod.invoke(craftPlayer)); // Object craftPlayer = craftPlayerClass.cast(player);
// Object entityHuman = entityHumanClass.cast(getHandleMethod.invoke(craftPlayer));
return (float) playerAttackStrengthMethod.invoke(entityHuman, 0F); //Add no adjustment ticks //
} // return (float) playerAttackStrengthMethod.invoke(entityHuman, 0F); //Add no adjustment ticks
// }
@Override //
public float getCooldownValue(@NotNull Player player) throws InvocationTargetException, IllegalAccessException { // @Override
Object craftPlayer = craftPlayerClass.cast(player); // public float getCooldownValue(@NotNull Player player) throws InvocationTargetException, IllegalAccessException {
Object entityHuman = entityHumanClass.cast(getHandleMethod.invoke(craftPlayer)); // Object craftPlayer = craftPlayerClass.cast(player);
// Object entityHuman = entityHumanClass.cast(getHandleMethod.invoke(craftPlayer));
return (float) playerAttackCooldownMethod.invoke(entityHuman); //Add no adjustment ticks //
} // return (float) playerAttackCooldownMethod.invoke(entityHuman); //Add no adjustment ticks
// }
@Override //
public void resetAttackStrength(@NotNull Player player) throws InvocationTargetException, IllegalAccessException { // @Override
Object craftPlayer = craftPlayerClass.cast(player); // public void resetAttackStrength(@NotNull Player player) throws InvocationTargetException, IllegalAccessException {
Object entityHuman = entityHumanClass.cast(getHandleMethod.invoke(craftPlayer)); // Object craftPlayer = craftPlayerClass.cast(player);
Object entityLiving = entityLivingClass.cast(entityHuman); // Object entityHuman = entityHumanClass.cast(getHandleMethod.invoke(craftPlayer));
// Object entityLiving = entityLivingClass.cast(entityHuman);
resetPlayerAttackCooldownMethod.invoke(entityLiving); //
} // resetPlayerAttackCooldownMethod.invoke(entityLiving);
// }
@Override //
public int getCooldownFieldValue(@NotNull Player player) throws InvocationTargetException, IllegalAccessException { // @Override
Object craftPlayer = craftPlayerClass.cast(player); // public int getCooldownFieldValue(@NotNull Player player) throws InvocationTargetException, IllegalAccessException {
Object entityHuman = entityHumanClass.cast(getHandleMethod.invoke(craftPlayer)); // Object craftPlayer = craftPlayerClass.cast(player);
Object entityLiving = entityLivingClass.cast(entityHuman); // Object entityHuman = entityHumanClass.cast(getHandleMethod.invoke(craftPlayer));
// Object entityLiving = entityLivingClass.cast(entityHuman);
return attackCooldownField.getInt(entityLiving); //
} // return attackCooldownField.getInt(entityLiving);
// }
@Override //
public void setCooldownFieldValue(@NotNull Player player, int fieldValue) throws InvocationTargetException, IllegalAccessException { // @Override
Object craftPlayer = craftPlayerClass.cast(player); // public void setCooldownFieldValue(@NotNull Player player, int fieldValue) throws InvocationTargetException, IllegalAccessException {
Object entityHuman = entityHumanClass.cast(getHandleMethod.invoke(craftPlayer)); // Object craftPlayer = craftPlayerClass.cast(player);
// Object entityHuman = entityHumanClass.cast(getHandleMethod.invoke(craftPlayer));
attackCooldownField.setInt(entityHuman, fieldValue); //
} // attackCooldownField.setInt(entityHuman, fieldValue);
// }
@Override //
public boolean initializeLayer() { // @Override
switch(nmsVersion) { // public boolean initializeLayer() {
case NMS_1_12_2: // switch(nmsVersion) {
return wireNMS("dr", "n", "ds", "getHandle", "at"); // case NMS_1_12_2:
case NMS_1_13_2: // return wireNMS("dr", "n", "ds", "getHandle", "at");
return wireNMS("dG", "r", "dH", "getHandle", "at"); // case NMS_1_13_2:
case NMS_1_14_4: // return wireNMS("dG", "r", "dH", "getHandle", "at");
return wireNMS("dY", "s", "dZ", "getHandle", "at"); // case NMS_1_14_4:
case NMS_1_15_2: // return wireNMS("dY", "s", "dZ", "getHandle", "at");
return wireNMS("ex", "s", "ey", "getHandle", "at"); // case NMS_1_15_2:
case NMS_1_16_4: // return wireNMS("ex", "s", "ey", "getHandle", "at");
return wireNMS("eR", "getAttackCooldown", "resetAttackCooldown", "getHandle", "at"); // case NMS_1_16_4:
default: // return wireNMS("eR", "getAttackCooldown", "resetAttackCooldown", "getHandle", "at");
throw new RuntimeException("Unexpected NMS version support in PlayerAttackCooldown compatibility layer initialization!"); // default:
} // throw new RuntimeException("Unexpected NMS version support in PlayerAttackCooldown compatibility layer initialization!");
} // }
} // }
//}
//

View File

@ -21,6 +21,7 @@ public enum NMSVersion {
NMS_1_16_2("1.16.2"), NMS_1_16_2("1.16.2"),
NMS_1_16_3("1.16.3"), NMS_1_16_3("1.16.3"),
NMS_1_16_4("1.16.4"), NMS_1_16_4("1.16.4"),
NMS_1_16_5("1.16.5"),
//Version not known to this build of mcMMO //Version not known to this build of mcMMO
UNSUPPORTED("unsupported"); UNSUPPORTED("unsupported");