Jakllp / MyPet

This is not the main project - Please visit https://github.com/MyPetORG/MyPet
https://www.spigotmc.org/resources/mypet.12725/
GNU Lesser General Public License v3.0
14 stars 8 forks source link

RC1 disconnecting players #50

Closed Ghost-chu closed 3 years ago

Ghost-chu commented 3 years ago
[16:40:07 ERROR]: Error whilst processing packet net.minecraft.network.protocol.game.PacketPlayInUseEntity@71eaf0f1 for Fly_Renasy[/36.62.81.170:38023]
java.lang.NullPointerException: Cannot invoke "net.minecraft.world.entity.ai.attributes.AttributeProvider.a(java.util.function.Consumer, net.minecraft.world.entity.ai.attributes.AttributeBase)" because "this.d" is null
        at net.minecraft.world.entity.ai.attributes.AttributeMapBase.lambda$new$0(AttributeMapBase.java:29) ~[patched_1.17.1.jar:git-Airplane-73]
        at java.util.HashMap.computeIfAbsent(HashMap.java:1224) ~[?:?]
        at net.minecraft.world.entity.ai.attributes.AttributeMapBase.a(AttributeMapBase.java:52) ~[patched_1.17.1.jar:git-Airplane-73]
        at org.bukkit.craftbukkit.v1_17_R1.attribute.CraftAttributeMap.getAttribute(CraftAttributeMap.java:36) ~[patched_1.17.1.jar:git-Airplane-73]
        at org.bukkit.craftbukkit.v1_17_R1.entity.CraftLivingEntity.getAttribute(CraftLivingEntity.java:722) ~[patched_1.17.1.jar:git-Airplane-73]
        at org.bukkit.craftbukkit.v1_17_R1.event.CraftEventFactory.populateFields(CraftEventFactory.java:911) ~[patched_1.17.1.jar:git-Airplane-73]
        at org.bukkit.craftbukkit.v1_17_R1.event.CraftEventFactory.callEntityDeathEvent(CraftEventFactory.java:856) ~[patched_1.17.1.jar:git-Airplane-73]
        at net.minecraft.world.entity.EntityLiving.dropAllDeathLoot(EntityLiving.java:1710) ~[patched_1.17.1.jar:git-Airplane-73]
        at net.minecraft.world.entity.EntityLiving.die(EntityLiving.java:1627) ~[patched_1.17.1.jar:git-Airplane-73]
        at de.Keyle.MyPet.compat.v1_17_R1.entity.EntityMyPet.die(EntityMyPet.java:746) ~[MyPet-3.12-RC1.jar:?]
        at net.minecraft.world.entity.EntityLiving.damageEntity(EntityLiving.java:1455) ~[patched_1.17.1.jar:git-Airplane-73]
        at net.minecraft.world.entity.player.EntityHuman.attack(EntityHuman.java:1289) ~[patched_1.17.1.jar:git-Airplane-73]
        at net.minecraft.server.level.EntityPlayer.attack(EntityPlayer.java:2021) ~[patched_1.17.1.jar:git-Airplane-73]
        at net.minecraft.server.network.PlayerConnection$5.a(PlayerConnection.java:2478) ~[patched_1.17.1.jar:git-Airplane-73]
        at net.minecraft.network.protocol.game.PacketPlayInUseEntity$1.a(PacketPlayInUseEntity.java:24) ~[patched_1.17.1.jar:git-Airplane-73]
        at net.minecraft.network.protocol.game.PacketPlayInUseEntity.a(PacketPlayInUseEntity.java:80) ~[patched_1.17.1.jar:git-Airplane-73]
        at net.minecraft.server.network.PlayerConnection.a(PlayerConnection.java:2405) ~[patched_1.17.1.jar:git-Airplane-73]
        at net.minecraft.network.protocol.game.PacketPlayInUseEntity.a(PacketPlayInUseEntity.java:67) ~[patched_1.17.1.jar:git-Airplane-73]
        at net.minecraft.network.protocol.game.PacketPlayInUseEntity.a(PacketPlayInUseEntity.java:12) ~[patched_1.17.1.jar:git-Airplane-73]
        at net.minecraft.network.protocol.PlayerConnectionUtils.lambda$ensureRunningOnSameThread$1(PlayerConnectionUtils.java:56) ~[patched_1.17.1.jar:git-Airplane-73]
        at net.minecraft.server.TickTask.run(TickTask.java:18) ~[patched_1.17.1.jar:git-Airplane-73]
        at net.minecraft.util.thread.IAsyncTaskHandler.executeTask(IAsyncTaskHandler.java:149) ~[patched_1.17.1.jar:git-Airplane-73]
        at net.minecraft.util.thread.IAsyncTaskHandlerReentrant.executeTask(IAsyncTaskHandlerReentrant.java:23) ~[patched_1.17.1.jar:git-Airplane-73]
        at net.minecraft.server.MinecraftServer.executeTask(MinecraftServer.java:1421) ~[patched_1.17.1.jar:git-Airplane-73]
        at net.minecraft.server.MinecraftServer.executeTask(MinecraftServer.java:192) ~[patched_1.17.1.jar:git-Airplane-73]
        at net.minecraft.util.thread.IAsyncTaskHandler.executeNext(IAsyncTaskHandler.java:122) ~[patched_1.17.1.jar:git-Airplane-73]
        at net.minecraft.server.MinecraftServer.bf(MinecraftServer.java:1399) ~[patched_1.17.1.jar:git-Airplane-73]
        at net.minecraft.server.MinecraftServer.executeNext(MinecraftServer.java:1392) ~[patched_1.17.1.jar:git-Airplane-73]
        at net.minecraft.util.thread.IAsyncTaskHandler.awaitTasks(IAsyncTaskHandler.java:132) ~[patched_1.17.1.jar:git-Airplane-73]
        at net.minecraft.server.MinecraftServer.sleepForTick(MinecraftServer.java:1368) ~[patched_1.17.1.jar:git-Airplane-73]
        at net.minecraft.server.MinecraftServer.x(MinecraftServer.java:1279) ~[patched_1.17.1.jar:git-Airplane-73]
        at net.minecraft.server.MinecraftServer.lambda$spin$0(MinecraftServer.java:319) ~[patched_1.17.1.jar:git-Airplane-73]
        at java.lang.Thread.run(Thread.java:831) [?:?]
[16:40:07 INFO]: Fly_Renasy lost connection: Packet processing error
Jakllp commented 3 years ago

Can you provide info on what the player did and what pet this occurred with?

Ghost-chu commented 3 years ago

Player get offline, so as far as I know, player just random disconnect and this is only what I found in console.

Ghost-chu commented 3 years ago

okay reproduce: summon your pet, then killed it

Ghost-chu commented 3 years ago

AttributeSupplier possibly null. (AttributeSupplier =AttributeProvider)

So I guess something wrong in there:

protected void replaceCraftAttributes() {
        //TODO check this
        Field craftAttributesField = ReflectionUtil.getField(LivingEntity.class, "craftAttributes");
        CraftAttributeMap craftAttributes = new CraftAttributeMap(this.getAttributes());
        ReflectionUtil.setFinalFieldValue(craftAttributesField, this, craftAttributes);
    }
Ghost-chu commented 3 years ago
    public static boolean setFinalFieldValue(@NonNull Field field, Object target, Object value) {
        try {
            field.setAccessible(true);

            Field modifiersField = Field.class.getDeclaredField("modifiers");
            modifiersField.setAccessible(true);
            modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);

            field.set(target, value);
            return true;
        } catch (Throwable ignored) {
        }
        return false;
    }

This method exceptions has been hidden, and I guess this is why I didn't see any error in console.

Jakllp commented 3 years ago

Good find, just edited it to print the StackTrace -> throws an error

Jakllp commented 3 years ago

Tested this some more - Seems to be a java 16 related.

How was this tested: Ran a 1.16.5 server with MyPet3.11-SNAPSHOT-B1642 on Java16: Same Error Ran the same server on Java 11: No error

Ghost-chu commented 3 years ago

Make a quick search on our local Minecraft forum, try this out: (Java 9+):

public static void grantAccess(Class<?> clazz){
        Field theUnsafe = null;
        try {
            theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
            theUnsafe.setAccessible(true);
            Unsafe unsafe = (Unsafe) theUnsafe.get(null);
            Field module = Class.class.getDeclaredField("module");
            long offset = unsafe.objectFieldOffset(module);
            unsafe.putObject(clazz, offset, Object.class.getModule());
        } catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }

like this:

 public static boolean setFinalFieldValue(String fieldName, Object target, Object value) {
        try {
            Field field = getField(target.getClass(), fieldName);
            if (field != null) {

                FooBar.grantAccess(this);
                field.setAccessible(true);

                Field modifiersField = Field.class.getDeclaredField("modifiers");
                modifiersField.setAccessible(true);
                modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);

                field.set(target, value);
                return true;
            }
        } catch (Throwable ignored) {
        }
        return false;
    }

I didn't test it yet, trying to fix some maven issue.

Jakllp commented 3 years ago

Done in https://github.com/Jakllp/MyPet/commit/474496fd8ba5bf12738f0626552002dd7252acfb

If you wanna build the plugin yourself you can look at the BuildGuide

Jakllp commented 3 years ago

(Note: We are just not doing whatever it was doing to the modifier anymore...)

minecraft8997 commented 2 years ago

Hello! I'm writing a plugin that adds custom entity and I get the same exception message:

java.lang.NullPointerException: Cannot invoke "net.minecraft.world.entity.ai.attributes.AttributeProvider.a(java.util.function.Consumer, net.minecraft.world.entity.ai.attributes.AttributeBase)" because "this.d" is null
        at net.minecraft.world.entity.ai.attributes.AttributeMap.lambda$getInstance$1(AttributeMap.java:50) ~[?:?]
        at java.util.HashMap.computeIfAbsent(HashMap.java:1219) ~[?:?]
        at net.minecraft.world.entity.ai.attributes.AttributeMap.getInstance(AttributeMap.java:49) ~[?:?]
        ...

How did you solve it?

Jakllp commented 2 years ago

See: https://github.com/Jakllp/MyPet/commit/474496fd8ba5bf12738f0626552002dd7252acfb

I had to change setFinalFieldValue() which is used to replace the craftAttributes-Field here

Thing is: I only needed to change it bc you don't need to modify the "modifiers" field in java 16+ anymore. You should check if you're replacing the craftAttributes first

minecraft8997 commented 2 years ago

Thanks for the answer! I can't replace it because the plugin crashes at doing super(type, world). I'm registering the entity by changing the value of an existing private static final field in the EntityTypes class (and it doesn't work without removing the final field, I'm on MC 1.18.1 and Java 17).

Jakllp commented 2 years ago

In case you're talking about net.minecraft.world.entity.EntityType: Take a look at how this plugin registers it's EntityTypes here

In case you're talking about org.bukkit.entity.EntityType: That's an enum. Can't change that.

minecraft8997 commented 2 years ago

Is it possible to register custom entity (by replacing an existing one) without registering custom net.minecraft.world.entity.EntityTypes?

Jakllp commented 2 years ago

maybe, idk