Cerandior Posted March 15, 2020 Posted March 15, 2020 (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 ClassEntity ClassPacket Handler ClassPacket ClassThe Trade Class (if requested for some reason) Thank you in advance. Edited March 15, 2020 by Cerandior Quote
Cerandior Posted March 15, 2020 Author Posted March 15, 2020 (edited) On 3/15/2020 at 10:13 PM, diesieben07 said: Okay, first of all: Java Serialization is terrible, why are you using it?! Expand 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. Expand So do I just pass an instance of my entity to the packet and update its offer list in the handle() method? Edited March 15, 2020 by Cerandior Quote
Cerandior Posted March 15, 2020 Author Posted March 15, 2020 (edited) On 3/15/2020 at 10:21 PM, 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. Expand 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 : Reveal hidden contents [23:30:21] [Client thread/WARN] [minecraft/ClientPlayNetHandler]: Unknown custom packet identifier: vanillaextended:main Edited March 15, 2020 by Cerandior Quote
Cerandior Posted March 15, 2020 Author Posted March 15, 2020 On 3/15/2020 at 10:35 PM, diesieben07 said: Is there a reason you are creating your packet handler in a static initializer instead of in the common setup event? Expand Apart from seeing at the docs, no. not really. I am going to change that now. Quote
Cerandior Posted March 15, 2020 Author Posted March 15, 2020 (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 March 15, 2020 by Cerandior Quote
Cerandior Posted March 16, 2020 Author Posted March 16, 2020 I am mostly worried about that warning. It's nog popping up any errors right now, but I want to make sure I got the thing setup correctly. Quote
Recommended Posts
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.