Reflection Help

Discussion in 'Programming' started by beccar_mt, Jul 28, 2020.

  1. Hi, i'm trying to make this code run for multiple server versions but i'm getting very confused with reflection. Every attempt i made to write it with reflection didn't work so i wonder if someone can help.
    Here is the code i want to run:
    Code (Java):
    public void sendItemSlotChange(Player player, int slot, ItemStack stack) {
            CraftPlayer craftPlayer = (CraftPlayer) player;
            PacketPlayOutSetSlot packet = new PacketPlayOutSetSlot(0,slot + 36 , CraftItemStack.asNMSCopy(stack));
            craftPlayer.getHandle().playerConnection.sendPacket(packet);
    }
    Here is the PacketPlayOutSetSlot class:
    Code (Text):
    package net.minecraft.server.v1_7_R1;

    public class PacketPlayOutSetSlot extends Packet {
        private int a;
        private int b;
        private ItemStack c;

        public PacketPlayOutSetSlot() {
        }

        public PacketPlayOutSetSlot(int var1, int var2, ItemStack var3) {
            this.a = var1;
            this.b = var2;
            this.c = var3 == null ? null : var3.cloneItemStack();
        }

        public void a(PacketPlayOutListener var1) {
            var1.a(this);
        }

        public void a(PacketDataSerializer var1) {
            this.a = var1.readUnsignedByte();
            this.b = var1.readShort();
            this.c = var1.c();
        }

        public void b(PacketDataSerializer var1) {
            var1.writeByte(this.a);
            var1.writeShort(this.b);
            var1.a(this.c);
        }
    }
     
     
  2. uh none of this is reflection. so, can you show what you tried using reflection?

    also, you can just use the spigot api to set the slot of an item in a player's inventory.
     
  3. Yes, i probably should have worded that better, i know this isn't reflection, i want someone to write this code in reflection for me, if possible.
    Thats not really what i want. What i'm trying to do is use an api that will send messages in the item tooltip. The PacketPlayOutSetSlot makes the client think that the item held has changed, so it displays the name of the item again, so i can use that to display a message there.

    (Just so I'm a bit more clear since english is not my native language: This code is part of a class that works and does just what i said about the tooltip message, but it only works for one version of the game since it has to import net.minecraft.server.v1_8, so thats why i need it to be written with reflection)
     
    #3 beccar_mt, Jul 29, 2020
    Last edited: Jul 29, 2020
  4. You said you want multi-version support, have you looked at ProtocolLib? A LOT easier for packet sending across multiple versions compared to reflection.
     
  5. Yes but i don't want my plugin to have dependencies, so thats why i'm using the packets.
     
  6. Well i guess i'm going to have to do it the hard way then....