Jump to content

Per-Player Item use cooldown with access from EntitiyPlayer Instance?


Alix_The_Alicorn

Recommended Posts

As the title suggests, I am having trouble setting up a cooldown for using my item that I can access from a player instance for armor rendering. I am thinking NBT, but I can't seem to understand how to read/write NBT to a player. Do I use methods from EntityPlayey, EntityLivingBase, or what? I have a ~PlayerTickEvent set up, but since I don't know how I should use the NBT, it does nothing. Any help would be appreciated <3

 

--Alix the Alicorn

Link to comment
Share on other sites

Create Entity Extended Properties

For most custom entities, you'll add fields to hold information that is specific to your entity. This information is not automatically saved or synced, so you need to make sure that happens.

 

You can also create extended version of vanilla enities, including the player -- check out CoolAlias' tutorial: http://www.minecraftforum.net/topic/1952901-172164-eventhandler-and-iextendedentityproperties/

 

There is an interface provided called IExtendedEntityProperties that helps handle these properties.  I explain how to use that here.

 

Create A Class That Implements IExtendedEntityProperties

In that class, create protected fields for the entity and the world.

 

Eclipse should give you a warning about unimplemented methods, so accept its suggested fix to create those methods for your.

 

In the init() method copy the entity and world parameters to the associated fields

 

In the saveNBTData() method, use the compound.setxxx() type methods (where xxx should be replaced with the data type) to take each entity field getter and store it in NBT tag of similar name.

 

In the loadNBTData() method, use the entity’s setter methods and grab the compound.getxxx() methods (where xxx should be replaced with the data type) to retrieve each tagged data.

 

Example (in this case scaleFactor, rearingCounter, etc. are my custom properties, and the EntityHerdAnimal and EntityElephant are my custom entities -- replace with your own stuff):

 

     public class ExtendedPropertiesHerdAnimal implements IExtendedEntityProperties
     {
        public final static String extendedPropertiesName = 
           "extendedPropertiesWildAnimal";
        protected EntityHerdAnimal theEntity;
        protected World theWorld;

        @Override
        public void saveNBTData(NBTTagCompound parCompound)
        {
           // DEBUG
           System.out.println("ExtendedPropertiesHerdAnimal saveNBTData()");

           // good idea to keep your extended properties in a sub-compound to 
           // avoid conflicts with other possible extended properties,
           // even from other mods (like if a mod extends all EntityAnimal)

           NBTTagCompound compound = new NBTTagCompound();
           parCompound.setTag(extendedPropertiesName, compound); // set as a sub-compound
           compound.setFloat("scaleFactor", theEntity.getScaleFactor());
           compound.setInteger("rearingCounter", theEntity.getRearingCounter());
           compound.setInteger("rearingTicksMax", theEntity.getRearingTicksMax());
           compound.setBoolean("isRearing", theEntity.isRearing());
        }

        @Override
        public void loadNBTData(NBTTagCompound parCompound)
        {
           // DEBUG
           System.out.println("ExtendedPropertiesHerdAnimal loadNBTData()");

           // Get the sub-compound
           NBTTagCompound compound = (NBTTagCompound) 
              parCompound.getTag(extendedPropertiesName);

           theEntity.setScaleFactor(compound.getFloat("scaleFactor"));
           theEntity.setRearingCounter(compound.getInteger("rearingCounter"));
           theEntity.setRearingTicksMax(compound.getInteger("rearingTicksMax"));
           theEntity.setRearing(compound.getBoolean("isRearing"));
        }

        @Override
        public void init(Entity entity, World world)
        {
           // DEBUG
           System.out.println("ExtendedPropertiesHerdAnimal init()");
           theEntity = (EntityElephant)entity;
           theWorld = world;
        }
     }

 

Register Entity Extended Properties

 

To ensure that the extended properties are activated, you need to register them.  In your mod’s custom event handler class (the one that is subscribed to the EVENT_BUS) subscribe to the onEntityConstructing() event.

 

Inside the method subscribed to the event, for each entity that you want to have extended properties check that the entity is of the type you want to register, then register the associated extended properties class.

 

Example:

     @SubscribeEvent
     public void onEntityConstructing(EntityConstructing event)
     {
        // Register extended entity properties
        // Herd animals

        if (event.entity instanceof EntityHerdAnimal)
        {
           // DEBUG
           System.out.println("OnEntityConstructing register HerdAnimals 
              extended properties");

           event.entity.registerExtendedProperties("ExtendedPropertiesHerdAnimal", 
              new ExtendedPropertiesHerdAnimal());
           }
        }
     }

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

How exactly do I change and read the values from outside my IExtendedEntityProps though?

Not advised: Just add instance fields to your entity for each property and maybe getter/setter methods to read and change them.

 

Advised: Use Entity#getExtendedProperties(String propname) to read them...

                Entity#registerExtendedEntityProperties(String propname, IExtendedEntityProperties object) to set them up.

 

Link to comment
Share on other sites

Any object that implements IExtendedEntityProperties should exist on client and server both, and the methods (unless that effect the client only) should be executed on both sides. This is normally true, unless the calls to change its values originates from client-only code. So, if you want to change it from clientside-only code, schedule a server update with your own packet handler and pass in any changed data keys and values. You have to handle the update in your own handler.

 

Check this tutorial http://www.minecraftforum.net/topic/1952901-172164-eventhandler-and-iextendedentityproperties/

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.

Announcements



×
×
  • Create New...

Important Information

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