Jump to content

[1.7.10] Need help with disappearing XP-levels and Durability.


Lellson

Recommended Posts

Hey! I'm pretty new into modding, so this is probably a easy problem...

I made an item, which can store players experience levels, but I have a problem. I made two variants of using this item to store and withdraw xp.

 

The first one is made with onItemRightClick and the second one is made with onUpdate and a custom key. The first one is working nicely, but if I use the second one, the xp and the durability always reset to their normal value, if i receive more xp or after a certain time.

 

Here is my code:

 

package lellson.survivalgear.items;

 

import lellson.survivalgear.util.SurvivalKeys;

import baubles.api.BaubleType;

import baubles.api.IBauble;

import net.minecraft.entity.Entity;

import net.minecraft.entity.EntityLivingBase;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.item.Item;

import net.minecraft.item.ItemStack;

import net.minecraft.world.World;

 

public class ItemXpHolder extends Item {

 

public ItemXpHolder() {

this.setMaxDamage(50);

this.maxStackSize = 1;

}

 

// First variant. works fine!

 

@Override

public ItemStack onItemRightClick(ItemStack item, World world, EntityPlayer player) {

 

if (!player.isSneaking()) {

if (item.getItemDamage() > 0 && item.getItemDamage() <= 50 && ((EntityPlayer) player).experienceLevel > 0) {

player.experienceLevel -= 1;

item.damageItem(-1, player);

}

}

 

if (player.isSneaking()) {

if (item.getItemDamage() < 50) {

player.experienceLevel += 1;

item.damageItem(1, player);

}

}

        return item;

    }

 

// Second variant. The xp and the durability always reset to their normal value

 

@Override

public void onUpdate(ItemStack item, World world, Entity player, int i, boolean b) {

 

if (SurvivalKeys.survivalActivate.isPressed()) {

 

if (!player.isSneaking()) {

if (item.getItemDamage() > 0 && item.getItemDamage() <= 50 && ((EntityPlayer) player).experienceLevel > 0) {

((EntityPlayer) player).experienceLevel -= 1;

item.damageItem(-1, (EntityLivingBase) player);

}

}

 

if (player.isSneaking()) {

if (item.getItemDamage() < 50) {

((EntityPlayer) player).experienceLevel += 1;

item.damageItem(1, (EntityLivingBase) player);

}

}

}

}

 

}

 

Thanks for your help!  ;D

 

Link to comment
Share on other sites

Keybindings are client-only, but the server is in control of the game. Changing things on the client will just get them overwritten by the server's values.

 

You need to handle your keybindings client-side and then send a packet to the server telling it that the key was pressed so it can take the appropriate action. diesieben07 has a tutorial on the Simple Network Wrapper here, there are probably several keybinding tutorials out there.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

Keybindings are client-only, but the server is in control of the game. Changing things on the client will just get them overwritten by the server's values.

 

You need to handle your keybindings client-side and then send a packet to the server telling it that the key was pressed so it can take the appropriate action. diesieben07 has a tutorial on the Simple Network Wrapper here, there are probably several keybinding tutorials out there.

 

Thanks! I'll have to try that.

Link to comment
Share on other sites

Why are you storing the exp with the durability bar. I mean it's a good solution but there are better options. You can use ItemStack NBTTag Data. That way you can add much more information and data to your item. E.G You can make it so only the player who crafted it can use it. By "using it" i mean taking or storing exp in it. You can also have a much better presentation of your EXP stored by using addInformation Method. That way the player can know exactly the amount of exp he has stored on the item just by hovering with mouse over it. That is better than just looking at the durability bar i guess. Or you can have both durability bar and the addInformation.

Link to comment
Share on other sites

If you're willing to entertain a paradigm shift:

 

Create an enchantment that consumes levels to charge your item, then destroys the item to give levels to the player using the item (like bottle o'enchanting that can be bought from villagers). Allow many levels of the enchantment.

 

However, what would be even more useful (and somewhat OP) is if you extended magic books to say, 16 colors of books, each of which would be offered a distinct enchantment when placed in an enchantment table (not sure how to spin that dial). This would not only store magic in the form of spells, it would give a player more rolls of the dice to gain a coveted enchantment.

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

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.