Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

Hey Guys!

I've created some extended properties for the player's mana.

I want the player to have 200 Mana on the first spawn. How can I do this?

 

This is my code:

 

package com.bedrockminer.magicum.entity;

 

import java.util.HashMap;

import java.util.Map.Entry;

 

import com.bedrockminer.magicum.Main;

import com.bedrockminer.magicum.network.ExtendedPropertiesEditPacket;

 

import net.minecraft.entity.Entity;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.entity.player.EntityPlayerMP;

import net.minecraft.nbt.NBTTagCompound;

import net.minecraft.world.World;

import net.minecraftforge.common.IExtendedEntityProperties;

 

public class ExtendedProperties implements IExtendedEntityProperties {

 

private EntityPlayer player;

private boolean serverSide;

 

public static final String IDENTIFIER = "magicumPlayer";

 

public ExtendedProperties(EntityPlayer player) {

this.player = player;

this.serverSide = player instanceof EntityPlayerMP;

 

this.maxValues.put("mana", 200);

}

 

///////////////////////////////////////////////////////////////////////////////////////////////

 

public HashMap<String, Integer> values = new HashMap<String, Integer>();

public HashMap<String, Integer> maxValues = new HashMap<String, Integer>();

 

@Override

public void saveNBTData(NBTTagCompound compound) {

for (Entry<String, Integer> entry : this.maxValues.entrySet()) {

compound.setInteger(entry.getKey(), this.values.get(entry.getKey()));

}

}

 

@Override

public void loadNBTData(NBTTagCompound compound) {

for (Entry<String, Integer> entry : this.maxValues.entrySet()) {

this.values.put(entry.getKey(), compound.getInteger(entry.getKey()));

}

}

 

@Override

public void init(Entity entity, World world) {

 

}

}

 

 

PS: init doesn't work, its only called on client side.

Couldn't you just set "values.put("mana", 200)" in the constructor?

 

I'm a bit confused with your code, to be honest; you seem to be making it much more complicated than you need to - do you really need hashmaps, rather than simply a couple integer fields? Why don't you save the max value as well, does it not ever change?

Why not just check in load from nbt , is there alredy mana value or no? If there is no that value , then make it 200.I think there is even function for that in NBT....get stuff , but if its dont exists it will return "first value".

Why would you check during loading from NBT? If you want a default value, set it in the constructor - that's what constructors are for. If you want the maximum value to change, you can do so just fine without a HashMap, and if you only have a few pre-defined values (such as 'mana', 'xp', etc.), you aren't gaining anything by using a HashMap versus several local fields or even an array or List.

Why would you check during loading from NBT? If you want a default value, set it in the constructor - that's what constructors are for. If you want the maximum value to change, you can do so just fine without a HashMap, and if you only have a few pre-defined values (such as 'mana', 'xp', etc.), you aren't gaining anything by using a HashMap versus several local fields or even an array or List.

beacos if player just reconects - what will be? It will load that default values..Or actually it will be worse - it will load from NBT value 0.Beacos getInteger returns 0 , if its cant find that tag.

 

Or why dont do both of that? 1.If there is no that tag , what we a searching in NBT , dont modify HashMap. 2. If there is that value in NBT , load it from NBT.

 

So if you do default values , it will load them , beacos player dont have that tag.

Why would you check during loading from NBT? If you want a default value, set it in the constructor - that's what constructors are for. If you want the maximum value to change, you can do so just fine without a HashMap, and if you only have a few pre-defined values (such as 'mana', 'xp', etc.), you aren't gaining anything by using a HashMap versus several local fields or even an array or List.

beacos if player just reconects - what will be? It will load that default values..Or actually it will be worse - it will load from NBT value 0.Beacos getInteger returns 0 , if its cant find that tag.

 

Or why dont do both of that? 1.If there is no that tag , what we a searching in NBT , dont modify HashMap. 2. If there is that value in NBT , load it from NBT.

 

So if you do default values , it will load them , beacos player dont have that tag.

If you are saving and loading NBT properly, the value you load will be correct; loading from NBT is not the place to set a default value. When the player first starts a world, the constructor is called without reading from NBT afterward, so the default value you set there will then be saved and loaded just fine.

Why would you check during loading from NBT? If you want a default value, set it in the constructor - that's what constructors are for. If you want the maximum value to change, you can do so just fine without a HashMap, and if you only have a few pre-defined values (such as 'mana', 'xp', etc.), you aren't gaining anything by using a HashMap versus several local fields or even an array or List.

beacos if player just reconects - what will be? It will load that default values..Or actually it will be worse - it will load from NBT value 0.Beacos getInteger returns 0 , if its cant find that tag.

 

Or why dont do both of that? 1.If there is no that tag , what we a searching in NBT , dont modify HashMap. 2. If there is that value in NBT , load it from NBT.

 

So if you do default values , it will load them , beacos player dont have that tag.

If you are saving and loading NBT properly, the value you load will be correct; loading from NBT is not the place to set a default value. When the player first starts a world, the constructor is called without reading from NBT afterward, so the default value you set there will then be saved and loaded just fine.

 

hmm , didnt know that *lafing at bad documented wiki*. What about if player was alredy playing on server?

Well, I did it now in the constructor and it worked.

The HashMaps have the advantage of easier packet handling: one packet for all values without a switch statement to get the correct one.

Sure (you could send all the data in one packet without a HashMap, too), but the disadvantage is you then have to send all of the data every time any value needs updating, so you are wasting a lot of bandwidth and processing time handling your packets this way, depending of course on how many different values you plan on storing in your maps. You are trading performance for convenience, which of course is your decision to make, but it's something to consider, at least.

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.