Jump to content

[1.15.2][Solved] Getting entity on Client Side


Papa_Prime

Recommended Posts

Hello, I am currently trying to sync the client with the server. I am currently unable to find a way in which I can encode entity identifiers from the server to the client. This is my current code:

public class ActionControlCSPacket {

    static Map map;

    public ActionControlCSPacket(Map controlMap)
    {
        map = controlMap;
    }

    public static void encode(ActionControlCSPacket msg, PacketBuffer buf)
    {
        PlayerEntity[] players = (PlayerEntity[]) map.keySet().toArray(new PlayerEntity[0]);
        CompoundNBT tag = new CompoundNBT();
        for(int t = 0; t < players.length; t++) {
            tag.put(String.valueOf(t), players[t].serializeNBT());
        }
        Integer[] values1 = (Integer[]) map.values().toArray(new Integer[0]);
        int[] values = new int[0];
        for(int i = 0; i < values1.length; i++)
        {
            values[i] = values1[i].intValue();
        }
        buf.writeCompoundTag(tag);
        buf.writeVarIntArray(values);
    }

    public static ActionControlCSPacket decode(PacketBuffer buf)
    {
        CompoundNBT tag = buf.readCompoundTag();
        int[] values = buf.readVarIntArray();
        Map hmap = new HashMap<PlayerEntity, Integer>();

        for(int i = 0; i < values.length; i++)
        {

            hmap.put(?????, values[i]);
        }

        return new ActionControlCSPacket(hmap);
    }

    public static void handle(ActionControlCSPacket message, Supplier<NetworkEvent.Context> ctx) {

        ActionControl.setControl(map);
        System.out.print("called");
        ctx.get().setPacketHandled(true);
    }
}

I have tried using the decoded tag in a number of ways but was unable to use the INBT to identify an entity. An idea would be to encode player IDs and then find them on the client but I am not aware of a method which allows me to do this although I am certain that it exists. Any help would be appreciated.

Edit: I have located such a method in the world class. Was obvious, still working on a solution.

Edited by Papa_Prime
Link to comment
Share on other sites

Here is the update code, not fully functional yet but getting there:

public class ActionControlCSPacket {

    static Map map;

    public ActionControlCSPacket(Map controlMap)
    {
        map = controlMap;
    }

    public static void encode(ActionControlCSPacket msg, PacketBuffer buf)
    {
        Integer[] players = (Integer[]) map.keySet().toArray(new Integer[0]);
        int[] id = new int[players.length];
        for(int i = 0; i < players.length; i++)
        {
            id[i] = players[i].intValue();
        }
        Integer[] preValues = (Integer[]) map.values().toArray(new Integer[0]);
        int[] values = new int[players.length];
        for(int i = 0; i < players.length; i++)
        {
            values[i] = preValues[i].intValue();
        }
        buf.writeVarIntArray(id);
        buf.writeVarIntArray(values);
    }

    public static ActionControlCSPacket decode(PacketBuffer buf)
    {
        Minecraft mc = Minecraft.getInstance();
        World world = mc.player.world;

        int[] id = buf.readVarIntArray();
        int[] values = buf.readVarIntArray();

        Map hmap = new HashMap<Integer, Integer>();

        for(int i = 0; i < id.length; i++)
        {
            hmap.put(id[i], values[i]);
        }

        return new ActionControlCSPacket(hmap);
    }

    public static void handle(ActionControlCSPacket message, Supplier<NetworkEvent.Context> ctx) {

        ActionControl.setControl(map);
        ctx.get().setPacketHandled(true);
    }
}

Just need to fix the array out of bounds exception which is easy. Leaving this here in case anyone finds more mistakes in my code. Going offline for now.

Edit: Updated to a version which seems to work

Edited by Papa_Prime
clean code
Link to comment
Share on other sites

On 6/13/2020 at 10:07 AM, diesieben07 said:

What on earth.

Please learn basic Java (generics, how collections work, etc.). Your code is a mess.

I caught on to that when I went to test it. Going to update it here now, this is what I got to work.

Edited by Papa_Prime
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.