Jump to content

Changing an Item on KeyBinding


Briggybros

Recommended Posts

Hello, I'm trying to extend a 'saber' on a KeyBinding I have the KeyBinding as so only on the ClientSide;

package info.mineverse.willow.sabers.client;

import info.mineverse.willow.sabers.SaberBase;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.EnumSet;

import org.lwjgl.input.Keyboard;

import net.minecraft.client.Minecraft;
import net.minecraft.src.EntityClientPlayerMP;
import net.minecraft.src.EntityPlayer;
import net.minecraft.src.EntityPlayerMP;
import net.minecraft.src.InventoryPlayer;
import net.minecraft.src.Item;
import net.minecraft.src.ItemStack;
import net.minecraft.src.KeyBinding;
import net.minecraft.src.ModLoader;
import net.minecraft.src.Packet250CustomPayload;
import net.minecraft.src.Packet5PlayerInventory;
import cpw.mods.fml.client.registry.KeyBindingRegistry.KeyHandler;
import cpw.mods.fml.common.Side;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.TickType;
import cpw.mods.fml.common.network.PacketDispatcher;
import cpw.mods.fml.common.network.Player;

public class SaberExtendKeyHandler extends KeyHandler
{
private Minecraft mc;

    static KeyBinding saberExtend = new KeyBinding("Extend Lightsaber", Keyboard.KEY_F);

    public SaberExtendKeyHandler()
    {
        //the first value is an array of KeyBindings, the second is whether or not the call 
        //keyDown should repeat as long as the key is down
        super(new KeyBinding[]{saberExtend}, new boolean[]{false});
        mc = ModLoader.getMinecraftInstance();
    }

    @Override
    public String getLabel()
    {
        return "ExtendLightsaber";
    }
    
    private void sendPacket(ItemStack item, int slot)
    {
    	int itemID = item.itemID;
    	int itemDamage = item.getItemDamage();
    	
    	
    	ByteArrayOutputStream bos = new ByteArrayOutputStream(12);
    	DataOutputStream outputStream = new DataOutputStream(bos);
    	try
    	{
    		outputStream.writeInt(itemID);
    		outputStream.writeInt(itemDamage);
    		outputStream.writeInt(slot);
    	}
    	catch (Exception ex)
    	{
    		ex.printStackTrace();
    	}

    	Packet250CustomPayload packet = new Packet250CustomPayload();
    	packet.channel = "SaberExtend";
    	packet.data = bos.toByteArray();
    	packet.length = bos.size();
    	
    	PacketDispatcher.sendPacketToServer(packet);
    }

    @Override
    public void keyDown(EnumSet<TickType> types, KeyBinding kb, boolean tickEnd, boolean isRepeat)
    {
    	if(kb.equals(saberExtend))
    	{
    	ItemStack itemstack = mc.thePlayer.inventory.getCurrentItem();
        if(itemstack != null)
        {
            if(itemstack.itemID == SaberBase.saberPurpleOn.shiftedIndex)
            {
                InventoryPlayer inventoryplayer = mc.thePlayer.inventory;
                inventoryplayer.setInventorySlotContents(inventoryplayer.currentItem, new ItemStack(SaberBase.saberPurpleOff.shiftedIndex, 1, itemstack.getItemDamage()));
                mc.theWorld.playSoundEffect(mc.thePlayer.posX, mc.thePlayer.posY, mc.thePlayer.posZ, "willow.sabers.saberoff", 1F,  mc.theWorld.rand.nextFloat() * 0.1F + 0.9F);
                System.out.println("DEBUG: playing sound: mod.sound.saberoff");
                System.out.println("DEBUG: extending lightsaber");
                sendPacket(itemstack, mc.thePlayer.inventory.currentItem);
            }
            
            if(itemstack.itemID == SaberBase.saberPurpleOff.shiftedIndex)
            {
                InventoryPlayer inventoryplayer = mc.thePlayer.inventory;
                inventoryplayer.setInventorySlotContents(inventoryplayer.currentItem, new ItemStack(SaberBase.saberPurpleOn.shiftedIndex, 1, itemstack.getItemDamage()));
                mc.theWorld.playSoundEffect(mc.thePlayer.posX, mc.thePlayer.posY, mc.thePlayer.posZ, "willow.sabers.saberoff", 1F,  mc.theWorld.rand.nextFloat() * 0.1F + 0.9F);
                System.out.println("DEBUG: playing sound: mod.sound.saberon");
                System.out.println("DEBUG: extending lightsaber");
                
            }
            
            if(itemstack.itemID == SaberBase.saberRedOn.shiftedIndex)
            {
                InventoryPlayer inventoryplayer = mc.thePlayer.inventory;
                inventoryplayer.setInventorySlotContents(inventoryplayer.currentItem, new ItemStack(SaberBase.saberRedOff.shiftedIndex, 1, itemstack.getItemDamage()));
                mc.sndManager.playSoundFX("mod.sound.saberoff", 1.0F, 1.0F);
                System.out.println("DEBUG: playing sound: mod.sound.saberoff");
                System.out.println("DEBUG: extending lightsaber");
                
            }
            if(itemstack.itemID == SaberBase.saberRedOff.shiftedIndex)
            {
                InventoryPlayer inventoryplayer = mc.thePlayer.inventory;
                inventoryplayer.setInventorySlotContents(inventoryplayer.currentItem, new ItemStack(SaberBase.saberRedOn.shiftedIndex, 1, itemstack.getItemDamage()));
                mc.sndManager.playSoundFX("mod.sound.saberon", 1.0F, 1.0F);
                System.out.println("DEBUG: playing sound: mod.sound.saberon");
                System.out.println("DEBUG: extending lightsaber");
                
            }
            
            if(itemstack.itemID == SaberBase.saberYellowOn.shiftedIndex)
            {
                InventoryPlayer inventoryplayer = mc.thePlayer.inventory;
                inventoryplayer.setInventorySlotContents(inventoryplayer.currentItem, new ItemStack(SaberBase.saberYellowOff.shiftedIndex, 1, itemstack.getItemDamage()));
                mc.sndManager.playSoundFX("mod.sound.saberoff", 1.0F, 1.0F);
                System.out.println("DEBUG: playing sound: mod.sound.saberoff");
                System.out.println("DEBUG: extending lightsaber");
                
            }
            if(itemstack.itemID == SaberBase.saberYellowOff.shiftedIndex)
            {
                InventoryPlayer inventoryplayer = mc.thePlayer.inventory;
                inventoryplayer.setInventorySlotContents(inventoryplayer.currentItem, new ItemStack(SaberBase.saberYellowOn.shiftedIndex, 1, itemstack.getItemDamage()));
                mc.sndManager.playSoundFX("mod.sound.saberon", 1.0F, 1.0F);
                System.out.println("DEBUG: playing sound: mod.sound.saberon");
                System.out.println("DEBUG: extending lightsaber");
                
            }
            
            if(itemstack.itemID == SaberBase.saberGreenOn.shiftedIndex)
            {
                InventoryPlayer inventoryplayer = mc.thePlayer.inventory;
                inventoryplayer.setInventorySlotContents(inventoryplayer.currentItem, new ItemStack(SaberBase.saberGreenOff.shiftedIndex, 1, itemstack.getItemDamage()));
                mc.sndManager.playSoundFX("mod.sound.saberoff", 1.0F, 1.0F);
                System.out.println("DEBUG: playing sound: mod.sound.saberoff");
                System.out.println("DEBUG: extending lightsaber");
                
            }
            if(itemstack.itemID == SaberBase.saberGreenOff.shiftedIndex)
            {
                InventoryPlayer inventoryplayer = mc.thePlayer.inventory;
                inventoryplayer.setInventorySlotContents(inventoryplayer.currentItem, new ItemStack(SaberBase.saberGreenOn.shiftedIndex, 1, itemstack.getItemDamage()));
                mc.sndManager.playSoundFX("mod.sound.saberon", 1.0F, 1.0F);
                System.out.println("DEBUG: playing sound: mod.sound.saberon");
                System.out.println("DEBUG: extending lightsaber");
                
            }
            
            if(itemstack.itemID == SaberBase.saberBlackOn.shiftedIndex)
            {
                InventoryPlayer inventoryplayer = mc.thePlayer.inventory;
                inventoryplayer.setInventorySlotContents(inventoryplayer.currentItem, new ItemStack(SaberBase.saberBlackOff.shiftedIndex, 1, itemstack.getItemDamage()));
                mc.sndManager.playSoundFX("mod.sound.saberoff", 1.0F, 1.0F);
                System.out.println("DEBUG: playing sound: mod.sound.saberoff");
                System.out.println("DEBUG: extending lightsaber");
                
            }
            if(itemstack.itemID == SaberBase.saberBlackOff.shiftedIndex)
            {
                InventoryPlayer inventoryplayer = mc.thePlayer.inventory;
                inventoryplayer.setInventorySlotContents(inventoryplayer.currentItem, new ItemStack(SaberBase.saberBlackOn.shiftedIndex, 1, itemstack.getItemDamage()));
                mc.sndManager.playSoundFX("mod.sound.saberon", 1.0F, 1.0F);
                System.out.println("DEBUG: playing sound: mod.sound.saberon");
                System.out.println("DEBUG: extending lightsaber");
                
            }
            
            if(itemstack.itemID == SaberBase.saberIndigoOn.shiftedIndex)
            {
                InventoryPlayer inventoryplayer = mc.thePlayer.inventory;
                inventoryplayer.setInventorySlotContents(inventoryplayer.currentItem, new ItemStack(SaberBase.saberIndigoOff.shiftedIndex, 1, itemstack.getItemDamage()));
                mc.sndManager.playSoundFX("mod.sound.saberoff", 1.0F, 1.0F);
                System.out.println("DEBUG: playing sound: mod.sound.saberoff");
                System.out.println("DEBUG: extending lightsaber");
                
            }
            if(itemstack.itemID == SaberBase.saberIndigoOff.shiftedIndex)
            {
                InventoryPlayer inventoryplayer = mc.thePlayer.inventory;
                inventoryplayer.setInventorySlotContents(inventoryplayer.currentItem, new ItemStack(SaberBase.saberIndigoOn.shiftedIndex, 1, itemstack.getItemDamage()));
                mc.sndManager.playSoundFX("mod.sound.saberon", 1.0F, 1.0F);
                System.out.println("DEBUG: playing sound: mod.sound.saberon");
                System.out.println("DEBUG: extending lightsaber");
                
            }
            
            if(itemstack.itemID == SaberBase.saberDarkBlueOn.shiftedIndex)
            {
                InventoryPlayer inventoryplayer = mc.thePlayer.inventory;
                inventoryplayer.setInventorySlotContents(inventoryplayer.currentItem, new ItemStack(SaberBase.saberDarkBlueOff.shiftedIndex, 1, itemstack.getItemDamage()));
                mc.sndManager.playSoundFX("mod.sound.saberoff", 1.0F, 1.0F);
                System.out.println("DEBUG: playing sound: mod.sound.saberoff");
                System.out.println("DEBUG: extending lightsaber");
                
            }
            if(itemstack.itemID == SaberBase.saberDarkBlueOff.shiftedIndex)
            {
                InventoryPlayer inventoryplayer = mc.thePlayer.inventory;
                inventoryplayer.setInventorySlotContents(inventoryplayer.currentItem, new ItemStack(SaberBase.saberDarkBlueOn.shiftedIndex, 1, itemstack.getItemDamage()));
                mc.sndManager.playSoundFX("mod.sound.saberon", 1.0F, 1.0F);
                System.out.println("DEBUG: playing sound: mod.sound.saberon");
                System.out.println("DEBUG: extending lightsaber");
                
            }
            
            if(itemstack.itemID == SaberBase.saberGoldOn.shiftedIndex)
            {
                InventoryPlayer inventoryplayer = mc.thePlayer.inventory;
                inventoryplayer.setInventorySlotContents(inventoryplayer.currentItem, new ItemStack(SaberBase.saberGoldOff.shiftedIndex, 1, itemstack.getItemDamage()));
                mc.sndManager.playSoundFX("mod.sound.saberoff", 1.0F, 1.0F);
                System.out.println("DEBUG: playing sound: mod.sound.saberoff");
                System.out.println("DEBUG: extending lightsaber");
                
            }
            if(itemstack.itemID == SaberBase.saberGoldOff.shiftedIndex)
            {
                InventoryPlayer inventoryplayer = mc.thePlayer.inventory;
                inventoryplayer.setInventorySlotContents(inventoryplayer.currentItem, new ItemStack(SaberBase.saberGoldOn.shiftedIndex, 1, itemstack.getItemDamage()));
                mc.sndManager.playSoundFX("mod.sound.saberon", 1.0F, 1.0F);
                System.out.println("DEBUG: playing sound: mod.sound.saberon");
                System.out.println("DEBUG: extending lightsaber");
                
            }
            
            if(itemstack.itemID == SaberBase.saberLightBlueOn.shiftedIndex)
            {
                InventoryPlayer inventoryplayer = mc.thePlayer.inventory;
                inventoryplayer.setInventorySlotContents(inventoryplayer.currentItem, new ItemStack(SaberBase.saberLightBlueOff.shiftedIndex, 1, itemstack.getItemDamage()));
                mc.sndManager.playSoundFX("mod.sound.saberoff", 1.0F, 1.0F);
                System.out.println("DEBUG: playing sound: mod.sound.saberoff");
                System.out.println("DEBUG: extending lightsaber");
                
            }
            if(itemstack.itemID == SaberBase.saberLightBlueOff.shiftedIndex)
            {
                InventoryPlayer inventoryplayer = mc.thePlayer.inventory;
                inventoryplayer.setInventorySlotContents(inventoryplayer.currentItem, new ItemStack(SaberBase.saberLightBlueOn.shiftedIndex, 1, itemstack.getItemDamage()));
                mc.sndManager.playSoundFX("mod.sound.saberon", 1.0F, 1.0F);
                System.out.println("DEBUG: playing sound: mod.sound.saberon");
                System.out.println("DEBUG: extending lightsaber");
                
            }
        }
    	}
    }

@Override
public void keyUp(EnumSet<TickType> types, KeyBinding kb, boolean tickEnd)
{

}

@Override
public EnumSet<TickType> ticks()
{
	return EnumSet.of(TickType.CLIENT);
}
}

 

I also have the packet handler as so;

package info.mineverse.willow.sabers;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import net.minecraft.src.Entity;
import net.minecraft.src.EntityClientPlayerMP;
import net.minecraft.src.EntityList;
import net.minecraft.src.EntityPlayer;
import net.minecraft.src.EntityPlayerMP;
import net.minecraft.src.EntityPlayerSP;
import net.minecraft.src.INetworkManager;
import net.minecraft.src.ItemStack;
import net.minecraft.src.Packet250CustomPayload;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.Side;
import cpw.mods.fml.common.network.IPacketHandler;
import cpw.mods.fml.common.network.PacketDispatcher;
import cpw.mods.fml.common.network.Player;

public class PacketHandler implements IPacketHandler
{
@Override
public void onPacketData(INetworkManager manager, Packet250CustomPayload packet, Player player)
{
	if (packet.channel.equals("SaberExtend"))
	{
		handlePacket(packet, player);
	}
}

private void handlePacket(Packet250CustomPayload packet, Player player)
{
	DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(packet.data));

    	int itemID;
    	int itemDamage;
    	int slot;
    	
	try
	{
		itemID = inputStream.readInt();
		itemDamage = inputStream.readInt();
		slot = inputStream.readInt();
	}
	catch (IOException e)
	{
		e.printStackTrace();
		return;
	}

	Side side = FMLCommonHandler.instance().getEffectiveSide();
	if (side == Side.SERVER)
	{
		EntityPlayerMP executingPlayer = (EntityPlayerMP)player;
		executingPlayer.inventory.inventoryChanged = true;
		sendPacket(executingPlayer, itemID, itemDamage, slot);
		closeInventoryChange(executingPlayer);
	}
	else if (side == Side.CLIENT)
	{
		EntityClientPlayerMP executingPlayer = (EntityClientPlayerMP)player;
		executingPlayer.inventory.inventoryChanged = true;
		closeInventoryChange(executingPlayer);
	}
}

private void closeInventoryChange(EntityClientPlayerMP player)
{
	player.inventory.inventoryChanged = false;
}

private void closeInventoryChange(EntityPlayerMP player)
{
	player.inventory.inventoryChanged = false;
}

    private void sendPacket(EntityPlayerMP entityPlayer, int itemID, int itemDamage, int slot)
    {
    	int playerID = entityPlayer.entityId;
    	
    	ByteArrayOutputStream bos = new ByteArrayOutputStream(16);
    	DataOutputStream outputStream = new DataOutputStream(bos);
    	try
    	{
    		outputStream.writeInt(playerID);
    		outputStream.writeInt(itemID);
    		outputStream.writeInt(itemDamage);
    		outputStream.writeInt(slot);
    	}
    	catch (Exception ex)
    	{
    		ex.printStackTrace();
    	}

    	Packet250CustomPayload packet = new Packet250CustomPayload();
    	packet.channel = "SaberExtend";
    	packet.data = bos.toByteArray();
    	packet.length = bos.size();
    	PacketDispatcher.sendPacketToPlayer(packet, (Player)entityPlayer);
    }
}

 

When I press the KeyBinding the item dissappears and comes back quickly and doesn't change. I can't see what's wrong.

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.



×
×
  • Create New...

Important Information

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