I'm a bit confused about where to put all of this, and about what works for 1.7 and what doesn't. the code here also refers to a lot of other classes that i don't have. Please help?
I'm a bit confused about where to put all of this, and about what works for 1.7 and what doesn't. the code here also refers to a lot of other classes that i don't have. Please help?
The tutorial is pretty self-explanatory in those regards - the section on updating to 1.7 tells you all you need to change, and creating new classes is about as basic of Java as it can get; if you are having trouble with such things, you may want to start with something simpler, such as basic Items, Blocks, etc. until you are more comfortable with Java and modding in general.
Sorry, but I've got one more thing. I noticed that my data was always reset when I left the game so I checked your tutorial again and noticed that I hadn't replaced my sync() method with the line in the loadProxyData() method since I'm using 1.7.2. I replaced the method and now my data doesn't reset when I log off, but the experience bar that I had added no longer renders. My mana bar still renders though so I figured I must have forgotten to register the experience bar somewhere but I can't find anything. Some help would be greatly appreciated. Here's my code:
public class ExtendedPlayer implements IExtendedEntityProperties
{
public final static String EXT_PROP_NAME = "ExtendedPlayer";
private final EntityPlayer player;
private static int currentKi;
private static int maxKi;
private static int level;
private static int exp;
private static int expToLevel;
public final InventoryCustomPlayer inventory = new InventoryCustomPlayer();
public ExtendedPlayer(EntityPlayer player)
{
this.player = player;
this.currentKi = this.maxKi = 650;
this.level = 1;
this.exp = 0;
this.expToLevel = 10;
}
public static final void register(EntityPlayer player)
{
player.registerExtendedProperties(ExtendedPlayer.EXT_PROP_NAME, new ExtendedPlayer(player));
}
public static final ExtendedPlayer get(EntityPlayer player)
private static int currentKi;
private static int maxKi;
private static int level;
private static int exp;
private static int expToLevel;
I suggest you do some research on what "static" means - you do NOT want to use it without knowing exactly what it means, as the above fields will not behave as you think they will.
Regarding your experience bar, if it was working before, then what else did you change besides the sync() -> pipeline.sendTo ?
expToLevel was initialized as 10 and so far I don't have anything in my program that changes it. The only thing I changed was the sync() method. I tried changing it back and my exp bar started rendering again.
Edit:
I did some debugging and figured it out. You were right, my getExpToLevel was being set to 0 by a typo in my LoadNBTData method. Thanks dude.
Where should I call save and load proxy data to sync the client and server for 1.7?
Exactly the same place. Please read the section on updating to 1.7 - it covers ALL of the things you need to change to get your extended properties working just as they were in 1.6, and if it's not there, you don't need to change it.
I can't find any differences between my code and yours. There is definetly something wrong with my packets though. I have an item that is supposed to spawn an entity and consume mana and I have a tick handler that's supposed to replenish mana when a key is pushed. When I use the item it doesn't effect my mana bar but after I've used it enough times it acts like I've run out of mana. My key binding can refill my mana bar but won't let me use my item more. It's as if they are pulling data from two different mana variables. I assume my packets aren't syncing the client and the server for some reason.
public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
I can't find any differences between my code and yours. There is definetly something wrong with my packets though. I have an item that is supposed to spawn an entity and consume mana and I have a tick handler that's supposed to replenish mana when a key is pushed. When I use the item it doesn't effect my mana bar but after I've used it enough times it acts like I've run out of mana. My key binding can refill my mana bar but won't let me use my item more. It's as if they are pulling data from two different mana variables. I assume my packets aren't syncing the client and the server for some reason.
You must not have read the entire tutorial, because your consumeMana and replenishMana methods are most certainly not the same as mine, though you are correct in your assessment of your problem.
First, never change the mana on the client. NEVER. Key handling is all done on the client, but the server has no idea that you are replenishing mana there, so when you try to use your item, of course you cannot. Instead, send a packet to the server every tick while the key is pressed, and let the server decide how long it has been pressed for and how much mana it should replenish. Better yet, remove the key altogether and just let mana replenish naturally over time (unless, for whatever reason, you really want that key press). I show how to do this in the tutorial.
Second, every time you change the current amount of mana, whether it is increasing or decreasing, do so only on the server, and be sure to send a packet with the new amount to the client or you will not see any change in your mana bar. This is the step you forgot, but it is also pretty well documented in the tutorial. Please read every section carefully.
The Meaning of Life, the Universe, and Everything.
Join Date:
8/9/2013
Posts:
211
Minecraft:
HappyKiller1O1
Xbox:
HappyKiller101
Member Details
So, I have been trying to add gold as in your tutorial and, it keeps resetting on death and on log off. I think I get the problem though; where do I put the "loadProxyData" and "saveProxyData" methods? Sorry if it is a simple question; I am just getting a bit frustrated.
The Meaning of Life, the Universe, and Everything.
Join Date:
8/9/2013
Posts:
211
Minecraft:
HappyKiller1O1
Xbox:
HappyKiller101
Member Details
Please ignore my last two posts; I was calling the wrong load/save NBT methods. Anyway, another question is, in the pickup handler, how would I make it where, when my item is picked up, it will not show in the inventory? I know how to add gold when it is picked up just, not how to make it not be in the inventory.
Please ignore my last two posts; I was calling the wrong load/save NBT methods. Anyway, another question is, in the pickup handler, how would I make it where, when my item is picked up, it will not show in the inventory? I know how to add gold when it is picked up just, not how to make it not be in the inventory.
Actually, I got it to work last night. (Silly me.) XD And well, now it works by using the "player.inventory.clearinventory(item, meta)" method. Anyway, one more question; I have realized that, when I dropped my items in clumps and, they got picked up at the same time. It only provided me with one silver but, I fixed this by simply measuring the stack size of the item being picked up and converting it into the amount of silver added. But, when a mob, with custom drops adding through my class, drops them in clumps it seems to not be registering the stack size. So, I believe, if every item just spread out it would be fine. So, how would I make it where, every coin item spreads out from the killed mob?
The Meaning of Life, the Universe, and Everything.
Join Date:
8/9/2013
Posts:
211
Minecraft:
HappyKiller1O1
Xbox:
HappyKiller101
Member Details
Has anyone setup a shop type GUI with the Gold tutorial? If so, can someone give me a few pointers on how to do it? I tried setting up one and, the Gold seems to reset and, when the player is giving the item and, you log off of the world and back on, it takes it away. Maybe cool could add a section explaining how to do it.
Has anyone setup a shop type GUI with the Gold tutorial? If so, can someone give me a few pointers on how to do it? I tried setting up one and, the Gold seems to reset and, when the player is giving the item and, you log off of the world and back on, it takes it away. Maybe cool could add a section explaining how to do it.
Well, because you are doing stuff within a GUI, it is only called on the client meaning it won't update on the server, instead you should possibly send a packet to the server and do the stuff?
I've encountered an interesting bug - I'm trying to create a counter which decreases when you kill a mob. I have it (sort of) working, in that when I kill a mob, it prints the new value of the counter to the console. However, the value then resets to its default when I kill another mob (constantly displays the same value).
Providing your code makes the life of all those who wishes to help easier.
Yep, sorry, I was trying to fix it on my own and sort of converted it into a leveling system as I was going. Still has the same issue though. Code below:
This is not how IExtendedEntityProperties are supposed to work - they are created once when the entity is constructed, and then you retrieve those properties for the entity whenever you need them; aside from the initial registration, you should never have to create a new instance of extended properties.
Well, solved one problem, created another. The leveling is working fine, but my GUI constantly displays 1, no matter what I change the actual level to. Logging in the console shows my level changing (5 right now), yet the GUI does not change.
hello, check this out!
The tutorial is pretty self-explanatory in those regards - the section on updating to 1.7 tells you all you need to change, and creating new classes is about as basic of Java as it can get; if you are having trouble with such things, you may want to start with something simpler, such as basic Items, Blocks, etc. until you are more comfortable with Java and modding in general.
public class ExtendedPlayer implements IExtendedEntityProperties
{
public final static String EXT_PROP_NAME = "ExtendedPlayer";
private final EntityPlayer player;
private static int currentKi;
private static int maxKi;
private static int level;
private static int exp;
private static int expToLevel;
public final InventoryCustomPlayer inventory = new InventoryCustomPlayer();
public ExtendedPlayer(EntityPlayer player)
{
this.player = player;
this.currentKi = this.maxKi = 650;
this.level = 1;
this.exp = 0;
this.expToLevel = 10;
}
public static final void register(EntityPlayer player)
{
player.registerExtendedProperties(ExtendedPlayer.EXT_PROP_NAME, new ExtendedPlayer(player));
}
public static final ExtendedPlayer get(EntityPlayer player)
{
return (ExtendedPlayer) player.getExtendedProperties(EXT_PROP_NAME);
}
@Override
public void saveNBTData(NBTTagCompound compound)
{
NBTTagCompound properties = new NBTTagCompound();
properties.setInteger("CurrentKi", this.currentKi);
properties.setInteger("MaxKi", this.maxKi);
properties.setInteger("Level", this.level);
properties.setInteger("Exp", this.exp);
properties.setInteger("ExpToLevel", this.expToLevel);
compound.setTag(EXT_PROP_NAME, properties);
this.inventory.writeToNBT(properties);
}
@Override
public void loadNBTData(NBTTagCompound compound)
{
NBTTagCompound properties = (NBTTagCompound) compound.getTag(EXT_PROP_NAME);
this.currentKi = properties.getInteger("CurrentKi");
this.maxKi = properties.getInteger("MaxKi");
this.level = properties.getInteger("Level");
this.exp = properties.getInteger("Exp");
this.expToLevel = properties.getInteger("ExtToLevel");
this.inventory.readFromNBT(properties);
}
private static final String getSaveKey(EntityPlayer player)
{
return player.getCommandSenderName() + ":" + EXT_PROP_NAME;
}
public static final void saveProxyData(EntityPlayer player)
{
ExtendedPlayer playerData = ExtendedPlayer.get(player);
NBTTagCompound savedData = new NBTTagCompound();
playerData.saveNBTData(savedData);
CommonProxy.storeEntityData(getSaveKey(player), savedData);
}
public static final void loadProxyData(EntityPlayer player)
{
ExtendedPlayer playerData = ExtendedPlayer.get(player);
NBTTagCompound savedData = CommonProxy.getEntityData(getSaveKey(player));
if (savedData != null)
{
playerData.loadNBTData(savedData);
}
Minecraftz.packetPipeline.sendTo(new SyncPlayerPropsPacket(player), (EntityPlayerMP) player);
}
@Override
public void init(Entity entity, World world)
{
}
public int consumeKi(int amount)
{
boolean sufficient = amount <= this.currentKi;
int powerIn = amount;
if (sufficient)
{
this.currentKi -= amount;
}
else
{
powerIn = currentKi;
this.currentKi = 0;
}
return powerIn;
}
public int replenishKi(int amount)
{
ExtendedPlayer.currentKi += amount;
return currentKi;
}
public final int getMaxKi()
{
return maxKi;
}
public final int getCurrentKi()
{
return currentKi;
}
public final void setMaxKi(int amount)
{
this.maxKi = (amount > 0 ? amount : 0);
}
public final void setCurrentKi(int amount)
{
this.currentKi = (amount < this.maxKi ? amount : this.maxKi);
}
public final int getExp()
{
return exp;
}
public final int getExpToLevel()
{
return expToLevel;
}
public final int getLevel()
{
return level;
}
public final void addExp(int amount)
{
this.exp += amount;
if (this.exp >= expToLevel)
{
this.level += 1;
this.exp -= expToLevel;
this.expToLevel += level * 5;
}
}
}
public
class GuiExpBar extends Gui
{
private Minecraft mc;
private static final ResourceLocation texturepath = new ResourceLocation("minecraftz", "textures/gui/mcz_icons.png");
public GuiExpBar(Minecraft mc)
{
super();
this.mc = mc;
}
@SubscribeEvent(priority = EventPriority.NORMAL)
public void onRenderExperienceBar(RenderGameOverlayEvent event)
{
if (event.isCancelable() || event.type != ElementType.EXPERIENCE)
{
return;
}
ExtendedPlayer props = ExtendedPlayer.get(
this.mc.thePlayer);
if (props == null || props.getExpToLevel() == 0)
{
return;
}
int xPos = (event.resolution.getScaledWidth() - 182) / 2;
int yPos = (event.resolution.getScaledHeight() - 49);
GL11.glEnable(GL11.GL_BLEND);
GL11.glDisable(GL11.GL_DEPTH_TEST);
GL11.glDepthMask(false);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA,GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
GL11.glDisable(GL11.GL_ALPHA_TEST);
GL11.glDisable(GL11.GL_LIGHTING);
this.mc.getTextureManager().bindTexture(texturepath);
int expbarwidth = (int)(((float) props.getExp() * 182) / props.getExpToLevel());
if (expbarwidth > 182)
{
expbarwidth = 182;
}
this.drawTexturedModalRect(xPos, yPos, 0, 25, 182, 10);
this.drawTexturedModalRect(xPos, yPos, 0, 35, expbarwidth, 10);
GL11.glDisable(GL11.GL_BLEND);
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glDepthMask(true);
}
}
public class OpenGuiPacket extends AbstractPacket
{
private int id;
public OpenGuiPacket()
{
}
public OpenGuiPacket(int id)
{
this.id = id;
}
@Override
public void encodeInto(ChannelHandlerContext ctx, ByteBuf buffer)
{
buffer.writeInt(id);
}
@Override
public void decodeInto(ChannelHandlerContext ctx, ByteBuf buffer)
{
id = buffer.readInt();
}
@Override
public void handlerClientSide(EntityPlayer player)
{
}
@Override
public void handlerServerSide(EntityPlayer player)
{
player.openGui(Minecraftz.instance, id, player.worldObj, (int) player.posX, (int) player.posY, (int) player.posZ);
}
}
public class ClientProxy extends CommonProxy
{
@Override
public void registerRenderers()
{
RenderingRegistry.registerEntityRenderingHandler(EntityKiblast.class, new RenderKiblast(Minecraftz.kiblastItem));
RenderingRegistry.registerEntityRenderingHandler(EntityBigblast.class, new RenderBigblast(Minecraftz.bigblastItem));
MinecraftForge.EVENT_BUS.register(new GuiKiBar(Minecraft.getMinecraft()));
MinecraftForge.EVENT_BUS.register(new GuiExpBar(Minecraft.getMinecraft()));
}
@Override
public void initialize()
{
super.initialize();
FMLCommonHandler.instance().bus().register(new ClientTickHandler(Minecraft.getMinecraft()));
}
}
I suggest you do some research on what "static" means - you do NOT want to use it without knowing exactly what it means, as the above fields will not behave as you think they will.
Regarding your experience bar, if it was working before, then what else did you change besides the sync() -> pipeline.sendTo ?
Is your exp to level zero? What does the openGuiPacket have to do with any of this?
Edit:
I did some debugging and figured it out. You were right, my getExpToLevel was being set to 0 by a typo in my LoadNBTData method. Thanks dude.
Exactly the same place. Please read the section on updating to 1.7 - it covers ALL of the things you need to change to get your extended properties working just as they were in 1.6, and if it's not there, you don't need to change it.
public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
{
ExtendedPlayer props = ExtendedPlayer.get(par3EntityPlayer);
if((!par2World.isRemote) && (props.getCurrentKi() > 0))
{
int z = props.consumeKi(30);
par2World.spawnEntityInWorld(
new EntityBigblast(par2World, par3EntityPlayer, z));
}
return par1ItemStack;
}
public class ExtendedPlayer implements IExtendedEntityProperties
{
public final static String EXT_PROP_NAME = "ExtendedPlayer";
private final EntityPlayer player;
private int currentKi;
private int maxKi;
private int level;
private int exp;
private int expToLevel;
public final InventoryCustomPlayer inventory = new InventoryCustomPlayer();
public ExtendedPlayer(EntityPlayer player)
{
this.player = player;
this.currentKi = this.maxKi = 650;
this.level = 1;
this.exp = 0;
this.expToLevel = 10;
}
public static final void register(EntityPlayer player)
{
player.registerExtendedProperties(ExtendedPlayer.EXT_PROP_NAME, new ExtendedPlayer(player));
}
public static final ExtendedPlayer get(EntityPlayer player)
{
return (ExtendedPlayer) player.getExtendedProperties(EXT_PROP_NAME);
}
@Override
public void saveNBTData(NBTTagCompound compound)
{
NBTTagCompound properties = new NBTTagCompound();
properties.setInteger("CurrentKi", this.currentKi);
properties.setInteger("MaxKi", this.maxKi);
properties.setInteger("Level", this.level);
properties.setInteger("Exp", this.exp);
properties.setInteger("ExpToLevel", this.expToLevel);
compound.setTag(EXT_PROP_NAME, properties);
this.inventory.writeToNBT(properties);
}
@Override
public void loadNBTData(NBTTagCompound compound)
{
NBTTagCompound properties = (NBTTagCompound) compound.getTag(EXT_PROP_NAME);
this.currentKi = properties.getInteger("CurrentKi");
this.maxKi = properties.getInteger("MaxKi");
this.level = properties.getInteger("Level");
this.exp = properties.getInteger("Exp");
this.expToLevel = properties.getInteger("ExpToLevel");
this.inventory.readFromNBT(properties);
}
private static final String getSaveKey(EntityPlayer player)
{
return player.getCommandSenderName() + ":" + EXT_PROP_NAME;
}
public static final void saveProxyData(EntityPlayer player)
{
ExtendedPlayer playerData = ExtendedPlayer.get(player);
NBTTagCompound savedData = new NBTTagCompound();
playerData.saveNBTData(savedData);
CommonProxy.storeEntityData(getSaveKey(player), savedData);
}
public static final void loadProxyData(EntityPlayer player)
{
ExtendedPlayer playerData = ExtendedPlayer.get(player);
NBTTagCompound savedData = CommonProxy.getEntityData(getSaveKey(player));
if (savedData != null)
{
playerData.loadNBTData(savedData);
}
Minecraftz.packetPipeline.sendTo(new SyncPlayerPropsPacket(player), (EntityPlayerMP) player);
}
@Override
public void init(Entity entity, World world)
{
}
public int consumeKi(int amount)
{
boolean sufficient = amount <= this.currentKi;
int powerIn = amount;
if (sufficient)
{
this.currentKi -= amount;
}
else
{
powerIn = currentKi;
this.currentKi = 0;
}
return powerIn;
}
public int replenishKi(int amount)
{
this.currentKi += amount;
return currentKi;
}
public final int getMaxKi()
{
return maxKi;
}
public final int getCurrentKi()
{
return currentKi;
}
public final void setMaxKi(int amount)
{
this.maxKi = (amount > 0 ? amount : 0);
}
public final void setCurrentKi(int amount)
{
this.currentKi = (amount < this.maxKi ? amount : this.maxKi);
}
public final int getExp()
{
return exp;
}
public final int getExpToLevel()
{
return expToLevel;
}
public final int getLevel()
{
return level;
}
public final void addExp(int amount)
{
this.exp += amount;
if (this.exp >= expToLevel)
{
this.level += 1;
this.exp -= expToLevel;
this.expToLevel += level * 5;
}
}
}
@SideOnly
(Side.CLIENT)
public class ClientTickHandler
{
private Minecraft mc;
public ClientTickHandler(Minecraft mc)
{
this.mc = mc;
}
@SubscribeEvent
public void onChargeTick(ClientTickEvent event)
{
if (MCZKeyHandler.keys[MCZKeyHandler.CHARGE_KI].getIsKeyPressed())
{
ExtendedPlayer props = ExtendedPlayer.get(mc.thePlayer);
if (props.getMaxKi() > 400)
{
props.replenishKi(props.getMaxKi() / 400);
}
else
{
props.replenishKi(1);
}
}
}
}
You must not have read the entire tutorial, because your consumeMana and replenishMana methods are most certainly not the same as mine, though you are correct in your assessment of your problem.
First, never change the mana on the client. NEVER. Key handling is all done on the client, but the server has no idea that you are replenishing mana there, so when you try to use your item, of course you cannot. Instead, send a packet to the server every tick while the key is pressed, and let the server decide how long it has been pressed for and how much mana it should replenish. Better yet, remove the key altogether and just let mana replenish naturally over time (unless, for whatever reason, you really want that key press). I show how to do this in the tutorial.
Second, every time you change the current amount of mana, whether it is increasing or decreasing, do so only on the server, and be sure to send a packet with the new amount to the client or you will not see any change in your mana bar. This is the step you forgot, but it is also pretty well documented in the tutorial. Please read every section carefully.
ExtendedLivingBase Class
HCEventHandler
Common Proxy
Have you tried canceling the event?
Actually, I got it to work last night. (Silly me.) XD And well, now it works by using the "player.inventory.clearinventory(item, meta)" method. Anyway, one more question; I have realized that, when I dropped my items in clumps and, they got picked up at the same time. It only provided me with one silver but, I fixed this by simply measuring the stack size of the item being picked up and converting it into the amount of silver added. But, when a mob, with custom drops adding through my class, drops them in clumps it seems to not be registering the stack size. So, I believe, if every item just spread out it would be fine. So, how would I make it where, every coin item spreads out from the killed mob?
Well, because you are doing stuff within a GUI, it is only called on the client meaning it won't update on the server, instead you should possibly send a packet to the server and do the stuff?
Providing your code makes the life of all those who wishes to help easier.
Art by me: MrPancakeWolfie@DeviantArt
That's because you are creating a new instance of your properties each time:
This is not how IExtendedEntityProperties are supposed to work - they are created once when the entity is constructed, and then you retrieve those properties for the entity whenever you need them; aside from the initial registration, you should never have to create a new instance of extended properties.
You need to synchronize the client with the server values, i.e. send a packet.