Jump to content

Recommended Posts

Posted (edited)

I am trying to make a merchant-like entity who will sell the player weapons in return for gold.
I am trying to render a list of his offers in the gui using the itemRenderer in my screen to render the items based on a list of the offers that the merchant has.

There is a problem though. I randomly generate a list of item offers when the entity is first spawned on the world and all the data of his offers is on the server. The client doesn't know anything so I had to use a packet to notify the client. I am trying to update an arraylist field that is located in the container of the entity, which holds all the offers of this merchant, however I am hitting a NullPointerException at the getSender() of the context in the handle() method of the packet.

My guess is that I have done something wrong in the registration of the packet, but I am not quite sure what because everything looks fine.
I have never worked with packets before, so any help is appreciated.
Also, I am sending the information to the client right after I open the GUI (Look at the entity class).

Main Class
Entity Class
Packet Handler Class
Packet Class
The Trade Class (if requested for some reason)

Thank you in advance.

Edited by Cerandior
Posted (edited)
6 minutes ago, diesieben07 said:

Okay, first of all: Java Serialization is terrible, why are you using it?!

This whole thing started because I couldn't think of any other way to the save the list of offers of the entity in the NBT Data. Even the Trade Class itself is sort of a mess if you look at it.

If you don't mind me asking. Why is the Java Serialization terrible?
 

Quote

getSender only makes sense on the server, it returns the player who sent it. On the client the "sender" is always the server.

So do I just pass an instance of my entity to the packet and update its offer list in the handle() method?

Edited by Cerandior
Posted (edited)
11 minutes ago, diesieben07 said:

The client player is always Minecraft#player. That gives you the container.

 

Write the length of the list as an int. Then loop through the list and write each entry in turn.

When reading, read the length, then use a loop to read that many entries.

To write an object, write each of its fields in turn.


Thank you, the client does have the right information right now, however it seems like I am still unable to render the items in the GUI.

Have I done something wrong at the Screen render() :

    public void render(int p_render_1_, int p_render_2_, float p_render_3_) {
        this.renderBackground();
        super.render(p_render_1_, p_render_2_, p_render_3_);
        int cnt=0;

        for(AssassinOffer ao : container.wanderingAssassin.getOfferList()){
            ItemStack gold = new ItemStack(Items.GOLD_INGOT);
            gold.setCount(ao.getPrice());
            ItemStack item = ao.getItem();
            this.itemRenderer.renderItemAndEffectIntoGUI(gold, guiLeft + 5 + 22, guiTop + 18 + 4 + (cnt*20));
            this.itemRenderer.renderItemOverlayIntoGUI(this.font, gold, guiLeft + 5 + 22, guiTop + 18 + 4 + (cnt*20), (String)null);
            this.itemRenderer.renderItemAndEffectIntoGUI(item, guiLeft + 5 + 66, guiTop + 18 + 4 + (cnt*20));
            this.itemRenderer.renderItemOverlayIntoGUI(this.font, item, guiLeft + 5 + 66, guiTop + 18 + 4 + (cnt*20), (String)null);
            cnt++;
        }
        this.renderHoveredToolTip(p_render_1_, p_render_2_);
    }



Also, I am getting this warning in the console
 

Spoiler

[23:30:21] [Client thread/WARN] [minecraft/ClientPlayNetHandler]: Unknown custom packet identifier: vanillaextended:main

 

Edited by Cerandior
Posted
Just now, diesieben07 said:

Is there a reason you are creating your packet handler in a static initializer instead of in the common setup event?

Apart from seeing at the docs, no. not really.
I am going to change that now.

Posted (edited)

Alright, so I fixed rendering stuff now although I am still getting that warning in the console (I did remove the static initializer).
I tried to send a packet to the client at the render function just to test something, passing in the container.wandererAssassin to the packet.
Doing that however, just keeps spamming the console with that warning and the items are not rendered anymore.

Also, I don't quite understand the change that I made to get the rendering to work. If you look in the sample that I provided above, I am looping through the container.wandererAssassin.getOfferList() , which should return a list of the offers of that entity.
All I did to make it work is to loop through container.getOfferList() instead.

I am confused though, because getOfferList() returns an ArrayList (which is called offerList) and that ArrayList is assigned the value of wandererAssassin.getOfferList() in the constructor of the container.
And container.wandererAssassin also gets the value of the wandererAsssassin which is the instance of the entity that is sent to the constructor when I request to open the GUI.

Aren't the two things that I was doing essentially the same thing?

So why wasn't it working before

Edited by Cerandior

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.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Also check the worldsave / serverconfig folder If there is no such file, make a test without this mod  
    • Hi, I've been having trouble trying to use forge as it shows a black screen when I open the game, but I can still interact with it and hear the music.  I've done all of the step by steps and most common fixes like updating drivers, keeping up to date with Java, deleting and reinstalling minecraft, restarting my computer MANY times, even smaller things like splash.properties (I didn't have that file so I added it and set it to false thinking it would do something, definitely not) and making sure to prioritize my rtx 3070 in the settings but with no luck. Minecraft works as intended when I uninstall forge and I also don't have any mods currently, it just gives me this issue when I install forge. I also increased the ram usage, made sure my hardware isn't full or anything, and even changed the resolution in hopes it would fix things. I checked my antivirus and firewall but that isn't the issue either. Trust me, I've done everything I can think of. For some reason the black screen does flicker a little into the main menu, but obviously unplayable. I couldn't even make my way to the settings with how little it flickered. I'm not sure if it flickered randomly or if it was because I was messing around moving and clicking a bunch, I didn't really test it that much.  
    • I've had a really weird issue recently,  I wanted to add the Depper and Darker mod on my dedicated server (MC 1.21 with Fabric 0.16.9, hosted on nitroserv.com) but whenever I do add the mod the sever stops doing anything after listing the mods, and I get no crash or error or anything, just a stuck server. Here's a normal log of the server booting up: https://pastebin.com/JipFF2Eh and here's the log of the server doing the weird thing: https://pastebin.com/W4JBh3eX I just don't understand it. I've tried removing other mods (somewhat randomly) but deeper and darker still breaks my server whenever I add it. NitroServ support staff is about as confused as I am and I've had no response from the Deeper and Darker support staff... Now I know this is the Forge support not the Fabric support but I'm just trying to know if anyone has any kind of idea to fix this (aside from not using the mod obviously) Also I still have a bunch of errors and warnings whenever the server does start properly, are there any of them I should be worried about?
  • Topics

×
×
  • Create New...

Important Information

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