Jump to content

[1.12.2][SOLVED] Passing NBT/Instance data to functions


Recommended Posts

Posted (edited)

I have an entity with a custom NBT value "type". This is a String, and can be equal to "normal" or "shadow".

Several aspects of this entity rely on the NBT value "type", and I want them to be updated when the value is changed. 

I want to change the model of the entity (or rather, just the texture which is rendered on the entity's model).

The entity also has an inventory capability, allowing it to store items. The size of this inventory, along with the corresponding elements of the inventory GUI, should change with the NBT value.

 

My problem is that I don't know how to do this. When I set the "type" variable to static within the entity's class, all of this works as I want. However, all entities are affected, as expected with a static variable.

When I remove the static modifier from "type", none of the aforementioned things happen.

 

My full source code is available here(GitHub link).

 

The following are the relevant parts of the files to do with this issue:

My entity's class:

  Reveal hidden contents

The entity's render class:

  Reveal hidden contents

The capability provider class:

  Reveal hidden contents

 

Edited by RiNickolous
Posted

Consider changing your field from a string to an enum and serialize/deserialize it by an ordinal. It will save on both the NBT data size and the network packet size(more on that later).

If you want some data of the entity to affect something on the client you need to create a new DataParameter, store your data in the entity's DataManager with that DataParameter as the key and retrieve the data by using that DataParameter as the key. See pretty much any minecraft entity for an example, an EntitySheep for example.

Additionally since it is your own entity you do not need a capability provider, the entity itself is one. Directly have the inventory stored in your entity as a field, override Entity#hasCapability and Entity#getCapability to check if the capability parameter is your capability and return your inventory if it is, otherwise default to a super implementation.

By the way if you are wondering the reason the static field worked was only because you were reaching across logical sides. It would not work if you would have launched a dedicated server and connected to it.

Posted (edited)
  On 9/19/2018 at 8:03 PM, V0idWa1k3r said:

Consider changing your field from a string to an enum and serialize/deserialize it by an ordinal.

Expand  

I'm not very experienced with Java, but I think I did this correctly.

EnumChesterType.class

  Reveal hidden contents

 

  On 9/19/2018 at 8:03 PM, V0idWa1k3r said:

If you want some data of the entity to affect something on the client you need to create a new DataParameter, store your data in the entity's DataManager with that DataParameter as the key and retrieve the data by using that DataParameter as the key.

Expand  

I think I did this as well? This is all very foreign to me.

 

In EntityChester:

  Reveal hidden contents

 

  On 9/19/2018 at 8:03 PM, V0idWa1k3r said:

Additionally since it is your own entity you do not need a capability provider, the entity itself is one. Directly have the inventory stored in your entity as a field, override Entity#hasCapability and Entity#getCapability to check if the capability parameter is your capability and return your inventory if it is, otherwise default to a super implementation.

Expand  

This is what confuses me. Could you elaborate on this part, please?

 

For reference:

My CapabilityProvider class:

  Reveal hidden contents

My Capability class:

  Reveal hidden contents

and the Inventory itself:

  Reveal hidden contents

 

Edited by RiNickolous
added code snippets
Posted

Okay, as it turns out, I have no idea what I'm doing.

 

My most recent crashlog:

  Reveal hidden contents

 

Posted (edited)

You do not need all this "metadata" nonsense, get the enum by it's ordinal(Enum.values()[ordinal], Enum#ordinal).

As for your capability question create a field of ChesterInventory in your entity class, (de)serialize it when needed(readFrom/writeToNBT), override Entity#hasCapability and Entity#getCapability. In those methods check if the capability is yours exactly the same way you are checking it in your provider. If it is return your ChesterInventory, otherwise return a super implementation(return cap == YOUR_CAP ? yourField : super.getCapability(cap, facing) and return cap == YOUR_CAP || super.hasCapability(cap, facing))

Edited by V0idWa1k3r
Posted
  On 9/19/2018 at 9:49 PM, V0idWa1k3r said:

You do not need all this "metadata" nonsense, get the enum by it's ordinal(Enum.values()[ordinal], Enum#ordinal).

Expand  

I'm sorry, I still don't think I quite understand. I'm not at all familiar with Enums. Where does this go? How do I make use of this? Looking at the EntitySheep code has left me quite confused.

Posted

(De)serialize the enum as the ordinal. Use the ordinal in your DataManager.

Pseudo-code:

Deserialization: setType(EnumChesterType.values()[tag.readByte("type")])

Serialization: tag.writeByte("type", this.getType().ordinal())

getType: EnumChesterType.values()[dataManager.get(TYPE)]

setType: dataManager.set(TYPE, type.ordinal())

Posted (edited)

I don't know what is going on.

 

EnumChesterType:

  Reveal hidden contents

 

In EntityChester:

  Reveal hidden contents

 

crash-log:

  Reveal hidden contents

 

Edited by RiNickolous
Posted
  On 9/19/2018 at 11:50 PM, RiNickolous said:

this.dataManager.register(TYPE, EnumChesterType.NORMAL);

Expand  

Well here you register the value for the TYPE as an EnumChesterType. But later you use this data manager key as if it was associated with a byte. Stay consistent. If only java had a way to automatically check these kind of errors for you... You know, if only you could put a constraint on the TYPE parameter instead of it being a generic Object accepting anything. Something like DataParameter<Byte> TYPE = ... 

  • Like 1
Posted (edited)
  On 9/20/2018 at 12:18 AM, V0idWa1k3r said:

Well here you register the value for the TYPE as an EnumChesterType. But later you use this data manager key as if it was associated with a byte. Stay consistent. If only java had a way to automatically check these kind of errors for you... You know, if only you could put a constraint on the TYPE parameter instead of it being a generic Object accepting anything. Something like DataParameter<Byte> TYPE = ... 

Expand  

Wow, it all works! Thank you so much for being so patient with me. I really appreciate the help.

 

All NBT data is now stored correctly, and functions relevant to them now respond correctly.

Edited by RiNickolous

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



×
×
  • Create New...

Important Information

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