Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[1.12.2] Switching Weapon Modes With Keybindings


Recommended Posts

To start off. The item in question is working, the problem lies in my eventhandler that is responsible for switching the firing modes when a button is pressed. 

	private static boolean first_keypress = true;
	private static EntityLivingBase player;
	private static ItemZeusLightningCharged z_lightning;

These are the variables that i'm using. The first_keypress is used to keep the key triggered once, and then you can trigger it once more after you release it. 

The player is to check if the entity is a player.

The lightning is used as the weapon reference.

	@SubscribeEvent(priority = EventPriority.NORMAL, receiveCanceled = true)
	public static void onItemCheck(LivingUpdateEvent event) 
	{
		if (event.getEntityLiving() instanceof EntityPlayerMP) 
		{
			player = (EntityPlayerMP) event.getEntityLiving();
			
			if (event.getEntity() == player && player.getHeldItemMainhand().getItem() == ModItems.ZEUS_LIGHTNING_CHARGED) 
			{
				z_lightning = (ItemZeusLightningCharged) player.getHeldItemMainhand().getItem();
			}
			else 
			{
				z_lightning = null;
			}
		}
	}

This is the method that checks if the entity is an instance of entityplayer, and if it is, it changes the player variable to the entity. 

And under that we have a check to see if the player is holding the lightning weapon. And if it is, it changes the weapon variable to the weapon the player is holding. And if it isn't holding the weapon, the weapon reference is null. 

	@SubscribeEvent(priority=EventPriority.NORMAL, receiveCanceled=true)
	public static void onEvent(KeyInputEvent event)
	{
	    KeyBinding[] keyBindings = ClientProxy.keyBindings;
	   
	    if (keyBindings[0].isPressed()) 
	    {
	    	if (first_keypress) 
	    	{
	    		first_keypress = false;
	    		
	    		Utils.getLogger().info("Trying to check Item.");
	    		checkItem();
	    	}
	    }
	    else if (!keyBindings[0].isPressed()) 
	    {
	    	first_keypress = true;
	    }
	}
	
	private static void checkItem() 
	{
		if (z_lightning != null) 
		{
			Utils.getLogger().info("Checked Item.");
    		z_lightning.lightningIndexController();
		}
	}

Next we have the keycheck. If the player presses the designated keybinding and it's the first keypress, then it calls checkItem();. It then changes the first_keypress to false and when you release the button, it changes to true. I am probably going to change the boolean to something as a timer instead if i have more of these events. 

Anyway, the checkItem(); checks if the weapon reference isn't null, and if it isn't null, it then gets the weapon reference and calls indexController(); on the weapon reference.

	public void lightningIndexController() 
	{
        if (this.lightning_index < 2) 
        {
        	this.lightning_index += 1;
        }
        if (this.lightning_index == 2) 
        {
        	this.lightning_index = 0;
        }
        
		Utils.getLogger().info("Lightning Index: " + this.lightning_index);
	}

The index controller adds a number to the index if it is lower than the maximum number, and resets to 0 if it goes to the maximum number. 

 

Now, the problem is that this doesn't work on servers, it does however work on singleplayer worlds. I have done several checks, and the problem seems to be that the weapon reference doesn't get stored, it only changes to the current weapon inside the weaponcheck. After the weaponcheck inside the updateevent, it just goes back to null. I don't know why this is the case on servers, but not on singleplayer worlds. Would appreciate some pointers here. ^^

 

EDIT: SOLVED IT! I created and used packets to fix it. 

Edited by Triphion
Link to post
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.

Guest
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 Privacy Policy.