Jump to content

Changing one item into another (not metadata)


LittleBreadLoaf

Recommended Posts

Hello, this is LittleBreadLoaf. I'll get right into it.

In my mod, the player is given a type and texture for their sword that is unique to the player. After they unlock it, they can "upgrade" their sword for short periods of time as long as they have energy. For activating the second-tier sword, a simple method is used:

/**
     * called when the player releases the use item button. Args: itemstack, world, entityplayer, itemInUseCount
     */
    public void onPlayerStoppedUsing(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer, int par4)
    {
if(props.getZType() == 1)
	        		{
                		ItemStack shikai = new ItemStack(Items.shikaifire, 1);
                		par1ItemStack.itemID = shikai.itemID;
}
}

This part works just fine. To answer your question that I know you have, why not just use metadata, this is why: Depending on the player's type, they can get 12 different types of tier 2 swords, each with 5 textures available. I have the first sword go to different tier 2 swords, and from there to whatever texture they have, instead of one item with 60 different subtypes (or even 12, if I did textures differently)

 

However, to deactivate the tier 2 sword, I used a keybind and relatively the same code:

    @Override
         public void keyDown(EnumSet<TickType> types, KeyBinding kb, boolean tickEnd, boolean isRepeat)
         {
                this.keyPressed = true;
               EntityPlayer player = mc.thePlayer;
       		ItemStack heldItem = player.getCurrentEquippedItem();
    		ItemStack var13 = new ItemStack(Items.zanpakuto, 1);
               if(heldItem != null )
               {
            	   if(heldItem.getItem() == Items.shikaifire)
            	   {
            		   heldItem.itemID = var13.itemID;
            	   }
}
}

 

All well and good, but on servers it will sometimes revert the texture to the original tier 1 sword (zanpakuto), but keep the effects and damage of the tier two sword, and it still drains the player's energy.

 

So my questions are these:

1) Is there a better way of changing one item into another than switching item IDs?

2) If not, and my methods are ridiculous and terrible coding, do I need to make a bunch of different metadatas for this item?

3) It is registering the change of ID clientside, AKA changing the texture, but serverside keeps the tier 2 sword when deactivated. Is there a way I can keep my method but use packets to alert the server of the change? 

 

If you want the rest of the item classes, just ask. I figure that's all that's needed to answer the question, but the rest is available.

Thanks for any help that may or may not come,

LittleBreadLoaf

Link to comment
Share on other sites

A couple things:

 

1. You don't need to create a whole new ItemStack just to switch the ID on the original. Just go with "par1ItemStack.itemID = Items.shikaifire.itemID;", it will make your code a bit more concise.

2. You probably shouldn't use a direct comparison between the ItemStack's Item object and your singleton sword Item. In theory it should work, but there are all sorts of little reasons a straight-up comparison like that might not behave as expected. Try pulling the item ID straight off the held ItemStack and compare that against the sword's item ID instead. Comparing IDs is always a better idea than comparing the Item itself.

Link to comment
Share on other sites

A couple things:

 

1. You don't need to create a whole new ItemStack just to switch the ID on the original. Just go with "par1ItemStack.itemID = Items.shikaifire.itemID;", it will make your code a bit more concise.

2. You probably shouldn't use a direct comparison between the ItemStack's Item object and your singleton sword Item. In theory it should work, but there are all sorts of little reasons a straight-up comparison like that might not behave as expected. Try pulling the item ID straight off the held ItemStack and compare that against the sword's item ID instead. Comparing IDs is always a better idea than comparing the Item itself.

Thanks, I'll keep that in mind.  :D

Link to comment
Share on other sites

In your key handler:

//perform item checks...
Packet packet = new CustomPacket(data);//build a packet with data, like a boolean to switch between tiers and the player entityid to identify him
player.sendQueue.addToSendQueue(packet);//send packet to the server

In your packet handler:

//handle the packet, read data, use it to check and perform changes...
if(player instanceof EntityPlayerMP)//avoid endless loop
		{
			((EntityPlayerMP)player).playerNetServerHandler.sendPacketToPlayer(packet);//send packet to the player client
		}

Link to comment
Share on other sites

Alright... Sorry I'm so hopeless, but what do I put in the CustomPacket's methods? Like readPacketData and such? In the key handler, I have

Packet packet = new DeactivatePacket(true, player.username);

but I don't know how to make it read this, or understand what I want it to do

Also, player doesn't have the method sendQueue. Is there a way to get player differently than (EntityPlayer) player = Minecraft.getMinecraft().thePlayer;?

 

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.