Jump to content

Recommended Posts

Posted

Hello Everyone I have a problem, I've been trying to make an item that lets you zoom when certain key is pressed. I have 2 versions

 

Version 1:

 

package kaiKaii99Proxy;

 

import java.util.EnumSet;

 

import kaiKaii99.MainClass;

 

import org.lwjgl.input.Keyboard;

 

import net.minecraft.client.Minecraft;

import net.minecraft.client.gui.Gui;

import net.minecraft.client.renderer.EntityRenderer;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.item.ItemStack;

import cpw.mods.fml.client.FMLClientHandler;

import cpw.mods.fml.common.FMLCommonHandler;

import cpw.mods.fml.common.ITickHandler;

import cpw.mods.fml.common.TickType;

import cpw.mods.fml.relauncher.ReflectionHelper;

import cpw.mods.fml.relauncher.Side;

import cpw.mods.fml.relauncher.SideOnly;

 

@SideOnly(Side.CLIENT)

public class ClientTickHandler extends Gui implements ITickHandler

{

 

public boolean isZoomed = false;

 

public boolean canZoom = false;

Class c = EntityRenderer.class;

Object o = Minecraft.getMinecraft().entityRenderer;

public ClientTickHandler()

{

super();

}

@Override

public EnumSet<TickType> ticks()

{

return EnumSet.of(TickType.RENDER);

}

 

@Override

public void tickEnd(EnumSet<TickType> type, Object... tickData)

{

               

               

                if(type.contains(TickType.RENDER)){

               

               

               

        if(Keyboard.isKeyDown(Keyboard.KEY_Z)){

        ReflectionHelper.setPrivateValue(c, o, 5D, 36);

        }

        if(!Keyboard.isKeyDown(Keyboard.KEY_Z)){

        ReflectionHelper.setPrivateValue(c, o, 1D, 36);

        }

      }

  }

 

 

 

 

@Override

public void tickStart(EnumSet<TickType> type, Object... tickData)

{

 

}

protected void drawGradientRect(int par1, int par2, int par3, int par4, int par5, int par6)

{

 

}

public void drawScreen(int par1, int par2, float par3)

{

 

}

public static void inizializePlayer(EntityPlayer par1EntityPlayer)

{

 

}

@Override

public String getLabel() {

 

return null;

}

}

 

 

and Version 2:

 

 

package kaiKaii99Proxy;

 

import java.util.EnumSet;

 

import kaiKaii99.MainClass;

 

import org.lwjgl.input.Keyboard;

 

import net.minecraft.client.Minecraft;

import net.minecraft.client.gui.Gui;

import net.minecraft.client.renderer.EntityRenderer;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.item.ItemStack;

import cpw.mods.fml.client.FMLClientHandler;

import cpw.mods.fml.common.FMLCommonHandler;

import cpw.mods.fml.common.ITickHandler;

import cpw.mods.fml.common.TickType;

import cpw.mods.fml.relauncher.ReflectionHelper;

import cpw.mods.fml.relauncher.Side;

import cpw.mods.fml.relauncher.SideOnly;

 

@SideOnly(Side.CLIENT)

public class ClientTickHandler extends Gui implements ITickHandler

{

 

public boolean isZoomed = false;

 

public boolean canZoom = false;

Class c = EntityRenderer.class;

Object o = Minecraft.getMinecraft().entityRenderer;

public ClientTickHandler()

{

super();

}

@Override

public EnumSet<TickType> ticks()

{

return EnumSet.of(TickType.RENDER);

}

 

@Override

public void tickEnd(EnumSet<TickType> type, Object... tickData)

{

                Minecraft mc = Minecraft.getMinecraft();

               

                if(type.contains(TickType.RENDER)){

               

                if(mc.thePlayer.getCurrentEquippedItem() != null)

                {

                ItemStack currItem = mc.thePlayer.getCurrentEquippedItem();

                if(currItem.getItem() == MainClass.MagicGauntlet){

                canZoom = true;

                }else{

                canZoom = false;

                }

               

                }

               

        if(Keyboard.isKeyDown(Keyboard.KEY_Z) && canZoom == true){

        ReflectionHelper.setPrivateValue(c, o, 5D, 36);

        }

        if(!Keyboard.isKeyDown(Keyboard.KEY_Z) || canZoom != true){

        ReflectionHelper.setPrivateValue(c, o, 1D, 36);

        }

      }

  }

 

 

 

 

@Override

public void tickStart(EnumSet<TickType> type, Object... tickData)

{

 

}

protected void drawGradientRect(int par1, int par2, int par3, int par4, int par5, int par6)

{

 

}

public void drawScreen(int par1, int par2, float par3)

{

 

}

public static void inizializePlayer(EntityPlayer par1EntityPlayer)

{

 

}

@Override

public String getLabel() {

 

return null;

}

}

 

 

 

In version 1 it works almost perfectly, well, i can zoom but with any item in my hand.

and in version 2 when i press the key and i have the correct item in my hand, when i release the key, it stays there, the value does not go back.

 

If you help me i will add you to my mod's credits list.

 

Thanks in advance

  • 1 month later...
Posted
I notice your zoom code is inside the "if(type.contains(TickType.RENDER)){" in the first version, but outside it in the second.

I double checked; That is just weird formatting.
Posted

What I would do:

Minecraft mc = Minecraft.getMinecraft();
boolean changed = false;
@Override
public void tickEnd(EnumSet<TickType> type, Object... tickData)
{ 
      if(type.contains(TickType.RENDER) && mc.thePlayer!=null){         
              if(mc.thePlayer.getCurrentEquippedItem() != null){
                      ItemStack currItem = mc.thePlayer.getCurrentEquippedItem();
                      if(currItem.getItem().itemID == MainClass.MagicGauntlet.itemID){
                             if(Keyboard.isKeyDown(Keyboard.KEY_Z)){
                                   if(!changed){
                                         ReflectionHelper.setPrivateValue(c, o, 5D, 36);
                                         changed = true;
                                   }
                                   return;
                             }
                       }
                 }
              if(changed){
                       ReflectionHelper.setPrivateValue(c, o, 1D, 36);
                       changed = false;
             }
        }
}

This is to avoid all unnecessary reflection uses.

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.