Posted June 9, 201411 yr 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
June 10, 201411 yr Author So, how exactly do I do this? Create a class implementing it, and call it's read/write methods when I need to?
June 10, 201411 yr 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/
June 11, 201411 yr Author How exactly do I change and read the values from outside my IExtendedEntityProps though?
June 11, 201411 yr 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. -S- (if I helped, please click Thank and applaud) http://6upnqa.dm2301.livefilestore.com/y2mtf-vG7Tqq1TiiVpIm53KWj7294NDPoHfSHHb4PzZiMAUfRCfK0UY0MwOu7Q3zTBNVTKqWjr2-xgBfFRpQT5p-QivtvknPpoABMNUw9br9WuZcBFkjePhnAbW500gVm-P/sequiturian.png[/img]
June 11, 201411 yr Author I was doing that, but how do I write? I get lots of console spam when re-registering, and I don't think that is the right thing to do. Also, I can't do think #1 I am writing to players.
June 11, 201411 yr I read another post on this, but it didn't make sense at the time. How do you recommend sync'n any of these values with the client? Long time Bukkit & Forge Programmer Happy to try and help
June 12, 201411 yr 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/ -S- (if I helped, please click Thank and applaud) http://6upnqa.dm2301.livefilestore.com/y2mtf-vG7Tqq1TiiVpIm53KWj7294NDPoHfSHHb4PzZiMAUfRCfK0UY0MwOu7Q3zTBNVTKqWjr2-xgBfFRpQT5p-QivtvknPpoABMNUw9br9WuZcBFkjePhnAbW500gVm-P/sequiturian.png[/img]
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.