Skip to content

Commit

Permalink
use Spigot API for native attack not NMS
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Sep 6, 2022
1 parent 6daf985 commit 2224751
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 35 deletions.
1 change: 1 addition & 0 deletions src/main/java/org/mcmonkey/sentinel/SentinelTrait.java
Expand Up @@ -1526,6 +1526,7 @@ else if (npc.getNavigator().isNavigating()) {
if (spawnPoint != null && lastLocationOnUpdate.distanceSquared(spawnPoint) < 1) {
didCorrection = true;
String prefix = SentinelCommand.prefixBad + "NPC " + npc.getId() + " (" + npc.getName() + SentinelCommand.colorBad + ") ";
// TODO: Remove auto-corrector if Citizens patch is reliable
if (SentinelPlugin.instance.autoCorrectpathfinderMode) {
SentinelUtilities.broadcastToSelected(npc, prefix + "is unable to move - likely due to a minecraft navigator bug, and so will have its "
+ "pathfinder mode autocorrected to use-new-finder=true (if you don't want this correction logic, disable Config option 'auto correct pathfinder mode')."
Expand Down
10 changes: 4 additions & 6 deletions src/main/java/org/mcmonkey/sentinel/SentinelWeaponHelper.java
Expand Up @@ -324,18 +324,16 @@ public void punch(LivingEntity entity) {
else {
if (sentinel.damage < 0 && SentinelVersionCompat.v1_15 && SentinelPlugin.instance.doNativeAttack) {
if (SentinelTarget.NATIVE_COMBAT_CAPABLE_TYPES.contains(getLivingEntity().getType())) {
debug("Punch/native/mob");
debug("Punch/native/living-entity");
getLivingEntity().attack(entity);
return;
}
else if (getLivingEntity() instanceof Player && SentinelVersionCompat.v1_17) {
debug("Punch/native/player");
ItemStack hand = getLivingEntity().getEquipment().getItemInMainHand().clone();
if (SentinelNMSHelper.doPlayerAttack((Player) getLivingEntity(), entity)) {
getLivingEntity().getEquipment().setItemInMainHand(hand); // Copy back the raw held item, as modern native attack can take durability
return;
}
debug("Native player punch failed. Error?");
getLivingEntity().attack(entity);
getLivingEntity().getEquipment().setItemInMainHand(hand); // Copy back the raw held item, as modern native attack can take durability
return;
}
}
double damage = sentinel.getDamage(false);
Expand Down
Expand Up @@ -3,9 +3,7 @@
import net.citizensnpcs.util.NMS;
import org.bukkit.Bukkit;
import org.bukkit.entity.Enderman;
import org.bukkit.entity.Entity;
import org.bukkit.entity.IronGolem;
import org.bukkit.entity.Player;

import java.lang.invoke.MethodHandle;
import java.lang.reflect.Field;
Expand All @@ -15,8 +13,7 @@
*/
public class SentinelNMSHelper {

public static MethodHandle CRAFTENTITY_GETHANDLE, NMSENTITY_WORLDGETTER, NMSWORLD_BROADCASTENTITYEFFECT, NMSENTITY_GETDATAWATCHER, DATWATCHER_SET,
SERVERPLAYER_ATTACK, LIVINGENTITY_ATTACKSTRENGTHTICKS;
public static MethodHandle CRAFTENTITY_GETHANDLE, NMSENTITY_WORLDGETTER, NMSWORLD_BROADCASTENTITYEFFECT, NMSENTITY_GETDATAWATCHER, DATWATCHER_SET, LIVINGENTITY_ATTACKSTRENGTHTICKS;

public static Object ENTITYENDERMAN_DATAWATCHER_ANGRY;

Expand All @@ -43,14 +40,12 @@ public static void init() {
nmsDataWatcher = Class.forName("net.minecraft.network.syncher.DataWatcher"); // SynchedEntityData
nmsDataWatcherObject = Class.forName("net.minecraft.network.syncher.DataWatcherObject"); // EntityDataAccessor
nmsEntityEnderman = Class.forName("net.minecraft.world.entity.monster.EntityEnderman");
nmsHuman = Class.forName("net.minecraft.world.entity.player.EntityHuman");
nmsLivingEntity = Class.forName("net.minecraft.world.entity.EntityLiving");
String playerAttackMethod = null, attackStrengthField = null;
String attackStrengthField = null;
boolean isCompat = false;
if (SentinelVersionCompat.v1_19 && !SentinelVersionCompat.vFuture) { // 1.19 names
// https://minidigger.github.io/MiniMappingViewer/#/mojang/server/1.19
endermanAngryField = "bZ"; // net.minecraft.world.entity.monster.EnderMan#DATA_CREEPY
playerAttackMethod = "d"; // net.minecraft.world.entity.player.Player#attack(Entity)
attackStrengthField = "aQ"; // net.minecraft.world.entity.LivingEntity#attackStrengthTicker
broadcastEffectMethod = "a"; // net.minecraft.world.level.Level#broadcastEntityEvent(Entity,byte)
dataWatcherSet = "b"; // net.minecraft.network.syncher.SynchedEntityData#set
Expand All @@ -59,20 +54,17 @@ public static void init() {
else if (SentinelVersionCompat.v1_18 && !SentinelVersionCompat.v1_19) { // 1.18 names
// https://minidigger.github.io/MiniMappingViewer/#/mojang/server/1.18.2
endermanAngryField = "bX"; // net.minecraft.world.entity.monster.EnderMan#DATA_CREEPY
playerAttackMethod = "d"; // net.minecraft.world.entity.player.Player#attack(Entity)
attackStrengthField = "aQ"; // net.minecraft.world.entity.LivingEntity#attackStrengthTicker
broadcastEffectMethod = "a"; // net.minecraft.world.level.Level#broadcastEntityEvent(Entity,byte)
dataWatcherSet = "b"; // net.minecraft.network.syncher.SynchedEntityData#set
isCompat = true;
}
else if (!SentinelVersionCompat.v1_18) { // 1.17 names
endermanAngryField = "bV"; // EnderMan#DATA_CREEPY
playerAttackMethod = "attack"; // Player#attack(Entity)
attackStrengthField = "aQ"; // LivingEntity#attackStrengthTicker
isCompat = true;
}
if (isCompat) {
SERVERPLAYER_ATTACK = NMS.getMethodHandle(nmsHuman, playerAttackMethod, true, nmsEntity);
LIVINGENTITY_ATTACKSTRENGTHTICKS = NMS.getSetter(nmsLivingEntity, attackStrengthField);
}
}
Expand Down Expand Up @@ -105,25 +97,6 @@ else if (!SentinelVersionCompat.v1_18) { // 1.17 names
}
}

public static boolean doPlayerAttack(Player attacker, Entity victim) {
if (!nmsWorks || SERVERPLAYER_ATTACK == null) {
return false;
}
try {
Object nmsHandleAttacker = CRAFTENTITY_GETHANDLE.invoke(attacker);
Object nmsHandleVictim = CRAFTENTITY_GETHANDLE.invoke(victim);
// 100 is just a random high value - attack strength doesn't tick itself for NPCs, so just set it a value too high to matter.
LIVINGENTITY_ATTACKSTRENGTHTICKS.invoke(nmsHandleAttacker, 100);
SERVERPLAYER_ATTACK.invoke(nmsHandleAttacker, nmsHandleVictim);
return true;
}
catch (Throwable ex) {
nmsWorks = false;
ex.printStackTrace();
return false;
}
}

public static void animateIronGolemSwing(IronGolem entity) {
if (!nmsWorks) {
return;
Expand Down

0 comments on commit 2224751

Please sign in to comment.