Jump to content

Check if MainHand is null to use autoclick right


NqwSqw

Recommended Posts

Hi,

I want to when I have blocks in OffHand and item can be eating in MainHand, the autoclick doesn't work and when in offHand there are blocks and MainHand have other things except foods it works

package xyz.fusked.m0dules.mods.combat;

import java.lang.reflect.Method;
import java.util.Random;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.inventory.GuiInventory;
import net.minecraft.client.settings.GameSettings;
import net.minecraft.client.settings.KeyBinding;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent.ClientTickEvent;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import xyz.fusked.m0dules.Module;
import xyz.fusked.m0dules.Module.Category;
import xyz.fusked.m0dules.Module.Modules;
import xyz.fusked.utils.MouseUtil;
import xyz.fusked.utils.Timer;
import xyz.fusked.values.BooleanValue;
import xyz.fusked.values.NumberValue;

public class AutoclickDroit extends Module
{
  private NumberValue maxcps;
  private NumberValue mincps;
  private BooleanValue blocks;
  private long nextLeftUp;
  private long nextLeftDown;
  private long nextRightUp;
  private long nextRightDown;
  private long nextDrop;
  private long nextExhaust;
  private Timer timer;
  private Random random;
  private double dr0pR4t3;
  private boolean dr0pp1ng;
  private Method guiScreenMethod;
  
  public AutoclickDroit()
  {
    super("AutoClickDroit", 0, Module.Category.COMBAT);
    this.maxcps = new NumberValue("MaxCPS", 12.0D, 1.0D, 50.0D);
    this.mincps = new NumberValue("MinCPS", 6.0D, 1.0D, 50.0D);
    this.blocks = new BooleanValue("Blocks", false);
    this.timer = new Timer();
    this.random = new Random();
    addValue(this.maxcps);
    addValue(this.mincps);
    addBoolean(this.blocks);
    try
    {
      this.guiScreenMethod = GuiScreen.class.getDeclaredMethod("mouseClicked", new Class[] { Integer.TYPE, Integer.TYPE, Integer.TYPE });
    }
    catch (NoSuchMethodException e)
    {
      e.printStackTrace();
    }
  }
  
  public boolean check(EntityPlayerSP playerSP)
  {
    return (!this.blocks.getState()) || ((playerSP.getHeldItemOffhand() != null) && (playerSP.getHeldItemOffhand().getItem() instanceof ItemBlock));
  }
  
  @SubscribeEvent
  public void onTick(TickEvent.ClientTickEvent ev3nt, EntityPlayerSP playerSP)
    throws Exception
  {
    if ((this.mc.currentScreen == null) && (check(this.mc.thePlayer)))
    {
      Mouse.poll();
      if (Mouse.isButtonDown(1))
      {
        if ((this.nextLeftDown > 0L) && (this.nextLeftUp > 0L))
        {
          if (System.currentTimeMillis() > this.nextLeftDown)
          {
        		  KeyBinding.setKeyBindState(this.mc.gameSettings.keyBindUseItem.getKeyCode(), true);
                  KeyBinding.onTick(this.mc.gameSettings.keyBindUseItem.getKeyCode());
                  MouseUtil.sendClick(0, true);
                  generateLeftDelay();  
          }
          else if (System.currentTimeMillis() > this.nextLeftUp)
          {
        	KeyBinding.setKeyBindState(this.mc.gameSettings.keyBindUseItem.getKeyCode(), false);
            MouseUtil.sendClick(0, false);
          }
        }
        else {
          generateLeftDelay();
        }
        if (!Mouse.isButtonDown(0))
        {
          long n = 0L;
          this.nextRightUp = 0L;
          this.nextRightDown = 0L;
        }
      }
      else
      {
        long n2 = 0L;
        this.nextRightUp = 0L;
        this.nextRightDown = 0L;
        this.nextLeftUp = 0L;
        this.nextLeftDown = 0L;
      }
    }
    else if ((this.mc.currentScreen instanceof GuiInventory))
    {
      if ((Mouse.isButtonDown(1)) && ((Keyboard.isKeyDown(54)) || (Keyboard.isKeyDown(42))))
      {
        if ((this.nextLeftUp == 0L) || (this.nextLeftDown == 0L))
        {
          generateLeftDelay();
          return;
        }
        if (System.currentTimeMillis() > this.nextLeftDown)
        {
          generateLeftDelay();
          clickInventory(this.mc.currentScreen);
        }
      }
      else
      {
        long n3 = 0L;
        this.nextRightUp = 0L;
        this.nextRightDown = 0L;
        this.nextLeftUp = 0L;
        this.nextLeftDown = 0L;
      }
    }
  }
  
  private void generateLeftDelay()
  {
    if (this.mincps.getValue() > this.maxcps.getValue()) {
      return;
    }
    long d3l4y = (int)Math.round(900.0D / this.mincps.getValue() + this.random.nextDouble() * (this.maxcps.getValue() - this.mincps.getValue()));
    if (System.currentTimeMillis() > this.nextDrop)
    {
      if ((!this.dr0pp1ng) && (this.random.nextInt(100) >= 85))
      {
        this.dr0pp1ng = true;
        this.dr0pR4t3 = (1.1D + this.random.nextDouble() * 0.15D);
      }
      else
      {
        this.dr0pp1ng = false;
      }
      this.nextDrop = (System.currentTimeMillis() + 500L + this.random.nextInt(1500));
    }
    if (this.dr0pp1ng) {
      d3l4y *= this.dr0pR4t3;
    }
    if (System.currentTimeMillis() > this.nextExhaust)
    {
      if (this.random.nextInt(100) >= 80) {
        d3l4y += 50L + this.random.nextInt(150);
      }
      this.nextExhaust = (System.currentTimeMillis() + 500L + this.random.nextInt(1500));
    }
    this.nextLeftDown = (System.currentTimeMillis() + d3l4y);
    this.nextLeftUp = (System.currentTimeMillis() + d3l4y / 2L - this.random.nextInt(10));
  }
  private void clickInventory(GuiScreen screen)
  {
    int v4r1 = Mouse.getX() * screen.width / this.mc.displayWidth;
    int v4r2 = screen.height - Mouse.getY() * screen.height / this.mc.displayHeight - 1;
    int v4r3 = 0;
    try
    {
      this.guiScreenMethod.setAccessible(true);
      this.guiScreenMethod.invoke(screen, new Object[] { Integer.valueOf(v4r1), Integer.valueOf(v4r2), Integer.valueOf(0) });
      this.guiScreenMethod.setAccessible(false);
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }

 

Link to comment
Share on other sites

Try checking getHeldItem(MainHand).isEmpty()

Also

12 hours ago, NqwSqw said:

playerSP.getHeldItemOffhand() != null

Is always true. Look at Problematic code 12#

My Projects:

Cruelars Triforcemod (1.12 release; 1.14 alpha soon coming)

 

Important:

As my mod is on at least 10 different third party sites without my permission, I want to warn you about that with a link to StopModReposts

Link to comment
Share on other sites

But why this doesn't work

package xyz.fusked.m0dules.mods.combat;

import java.lang.reflect.Method;
import java.util.Random;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.inventory.GuiInventory;
import net.minecraft.client.settings.GameSettings;
import net.minecraft.client.settings.KeyBinding;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemFood;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent.ClientTickEvent;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import xyz.fusked.m0dules.Module;
import xyz.fusked.m0dules.Module.Category;
import xyz.fusked.m0dules.Module.Modules;
import xyz.fusked.utils.MouseUtil;
import xyz.fusked.utils.Timer;
import xyz.fusked.values.BooleanValue;
import xyz.fusked.values.NumberValue;

public class AutoclickDroit extends Module
{
  private NumberValue maxcps;
  private NumberValue mincps;
  private BooleanValue blocks;
  private long nextLeftUp;
  private long nextLeftDown;
  private long nextRightUp;
  private long nextRightDown;
  private long nextDrop;
  private long nextExhaust;
  private Timer timer;
  private Random random;
  private double dr0pR4t3;
  private boolean dr0pp1ng;
  private Method guiScreenMethod;
  
  public AutoclickDroit()
  {
    super("AutoClickDroit", 0, Module.Category.COMBAT);
    this.maxcps = new NumberValue("MaxCPS", 12.7D, 1.0D, 20.0D);
    this.mincps = new NumberValue("MinCPS", 12.0D, 1.0D, 20.0D);
    this.blocks = new BooleanValue("Blocks", false);
    this.timer = new Timer();
    this.random = new Random();
    addValue(this.maxcps);
    addValue(this.mincps);
    addBoolean(this.blocks);
    try
    {
      this.guiScreenMethod = GuiScreen.class.getDeclaredMethod("mouseClicked", new Class[] { Integer.TYPE, Integer.TYPE, Integer.TYPE });
    }
    catch (NoSuchMethodException e)
    {
      e.printStackTrace();
    }
  }
  
  public boolean check(EntityPlayerSP playerSP)
  {
	  if ((playerSP.getHeldItemMainhand() != null) && (playerSP.getHeldItemMainhand().getItem() instanceof ItemBlock)){
	      return true;
	  }
	  else if ((playerSP.getHeldItemOffhand() != null) && (playerSP.getHeldItemOffhand().getItem() instanceof ItemBlock)){
	      return true;
	  }
	  else if((playerSP.getHeldItemOffhand() != null) && (playerSP.getHeldItemOffhand().getItem() instanceof ItemBlock ) && (playerSP.getHeldItemMainhand().getItem() instanceof ItemFood )){
		  return false;
	  }
	  else {
		  return false;
	  }
  }
  
  @SubscribeEvent
  public void onTick(TickEvent.ClientTickEvent ev3nt)
    throws Exception
  {
    if ((this.mc.currentScreen == null) && (check(this.mc.thePlayer)))
    {
      Mouse.poll();
      if (Mouse.isButtonDown(1))
      {
        if ((this.nextLeftDown > 0L) && (this.nextLeftUp > 0L))
        {
          if (System.currentTimeMillis() > this.nextLeftDown)
          {
        		  KeyBinding.setKeyBindState(this.mc.gameSettings.keyBindUseItem.getKeyCode(), true);
                  KeyBinding.onTick(this.mc.gameSettings.keyBindUseItem.getKeyCode());
                  MouseUtil.sendClick(0, true);
                  generateLeftDelay();  
          }
          else if (System.currentTimeMillis() > this.nextLeftUp)
          {
        	KeyBinding.setKeyBindState(this.mc.gameSettings.keyBindUseItem.getKeyCode(), false);
            MouseUtil.sendClick(0, false);
          }
        }
        else {
          generateLeftDelay();
        }
        if (!Mouse.isButtonDown(0))
        {
          long n = 0L;
          this.nextRightUp = 0L;
          this.nextRightDown = 0L;
        }
      }
      else
      {
        long n2 = 0L;
        this.nextRightUp = 0L;
        this.nextRightDown = 0L;
        this.nextLeftUp = 0L;
        this.nextLeftDown = 0L;
      }
    }
    else if ((this.mc.currentScreen instanceof GuiInventory))
    {
      if ((Mouse.isButtonDown(1)) && ((Keyboard.isKeyDown(54)) || (Keyboard.isKeyDown(42))))
      {
        if ((this.nextLeftUp == 0L) || (this.nextLeftDown == 0L))
        {
          generateLeftDelay();
          return;
        }
        if (System.currentTimeMillis() > this.nextLeftDown)
        {
          generateLeftDelay();
          clickInventory(this.mc.currentScreen);
        }
      }
      else
      {
        long n3 = 0L;
        this.nextRightUp = 0L;
        this.nextRightDown = 0L;
        this.nextLeftUp = 0L;
        this.nextLeftDown = 0L;
      }
    }
  }
  
  private void generateLeftDelay()
  {
    if (this.mincps.getValue() > this.maxcps.getValue()) {
      return;
    }
    long d3l4y = (int)Math.round(900.0D / this.mincps.getValue() + this.random.nextDouble() * (this.maxcps.getValue() - this.mincps.getValue()));
    if (System.currentTimeMillis() > this.nextDrop)
    {
      if ((!this.dr0pp1ng) && (this.random.nextInt(100) >= 85))
      {
        this.dr0pp1ng = true;
        this.dr0pR4t3 = (1.1D + this.random.nextDouble() * 0.15D);
      }
      else
      {
        this.dr0pp1ng = false;
      }
      this.nextDrop = (System.currentTimeMillis() + 500L + this.random.nextInt(1500));
    }
    if (this.dr0pp1ng) {
      d3l4y *= this.dr0pR4t3;
    }
    if (System.currentTimeMillis() > this.nextExhaust)
    {
      if (this.random.nextInt(100) >= 80) {
        d3l4y += 50L + this.random.nextInt(150);
      }
      this.nextExhaust = (System.currentTimeMillis() + 500L + this.random.nextInt(1500));
    }
    this.nextLeftDown = (System.currentTimeMillis() + d3l4y);
    this.nextLeftUp = (System.currentTimeMillis() + d3l4y / 2L - this.random.nextInt(10));
  }
  private void clickInventory(GuiScreen screen)
  {
    int v4r1 = Mouse.getX() * screen.width / this.mc.displayWidth;
    int v4r2 = screen.height - Mouse.getY() * screen.height / this.mc.displayHeight - 1;
    int v4r3 = 0;
    try
    {
      this.guiScreenMethod.setAccessible(true);
      this.guiScreenMethod.invoke(screen, new Object[] { Integer.valueOf(v4r1), Integer.valueOf(v4r2), Integer.valueOf(0) });
      this.guiScreenMethod.setAccessible(false);
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }
}

 

at

 

public boolean check(EntityPlayerSP playerSP)
  {
	  if ((playerSP.getHeldItemMainhand() != null) && (playerSP.getHeldItemMainhand().getItem() instanceof ItemBlock)){
	      return true;
	  }
	  else if ((playerSP.getHeldItemOffhand() != null) && (playerSP.getHeldItemOffhand().getItem() instanceof ItemBlock)){
	      return true;
	  }
	  else if((playerSP.getHeldItemOffhand() != null) && (playerSP.getHeldItemOffhand().getItem() instanceof ItemBlock ) && (playerSP.getHeldItemMainhand().getItem() instanceof ItemFood )){
		  return false;
	  }
	  else {
		  return false;
	  }
  }

 

if I want to when I have blocks in OffHand and item can be eating in MainHand, the autoclick doesn't work and when in offHand there are blocks and MainHand have other things except foods it works ?

 

 

Link to comment
Share on other sites

Quote

public boolean check(EntityPlayerSP playerSP)
  {
	  if ((playerSP.getHeldItemMainhand() != null) && (playerSP.getHeldItemMainhand().getItem() instanceof ItemBlock)){
	      return true;
	  }
	  else if ((playerSP.getHeldItemOffhand() != null) && (playerSP.getHeldItemOffhand().getItem() instanceof ItemBlock)){
	      return true;
	  }
	  else if((playerSP.getHeldItemOffhand() != null) && (playerSP.getHeldItemOffhand().getItem() instanceof ItemBlock ) && (playerSP.getHeldItemMainhand().getItem() instanceof ItemFood )){
		  return false;
	  }
	  else {
		  return false;
	  }
  }

Have you even looked at the info in the link? Both Vanilla and Forge code expect ItemStacks to be not null so they use Itemstack.EMPTY use ItemStack.isEmpty() to check for that.

My Projects:

Cruelars Triforcemod (1.12 release; 1.14 alpha soon coming)

 

Important:

As my mod is on at least 10 different third party sites without my permission, I want to warn you about that with a link to StopModReposts

Link to comment
Share on other sites

3 hours ago, _Cruelar_ said:

Have you even looked at the info in the link? Both Vanilla and Forge code expect ItemStacks to be not null so they use Itemstack.EMPTY use ItemStack.isEmpty() to check for that.

I try it but it give unexpected method for ItemStack

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • They were already updated, and just to double check I even did a cleanup and fresh update from that same page. I'm quite sure drivers are not the problem here. 
    • i tried downloading the drivers but it says no AMD graphics hardware has been detected    
    • Update your AMD/ATI drivers - get the drivers from their website - do not update via system  
    • As the title says i keep on crashing on forge 1.20.1 even without any mods downloaded, i have the latest drivers (nvidia) and vanilla minecraft works perfectly fine for me logs: https://pastebin.com/5UR01yG9
    • Hello everyone, I'm making this post to seek help for my modded block, It's a special block called FrozenBlock supposed to take the place of an old block, then after a set amount of ticks, it's supposed to revert its Block State, Entity, data... to the old block like this :  The problem I have is that the system breaks when handling multi blocks (I tried some fix but none of them worked) :  The bug I have identified is that the function "setOldBlockFields" in the item's "setFrozenBlock" function gets called once for the 1st block of multiblock getting frozen (as it should), but gets called a second time BEFORE creating the first FrozenBlock with the data of the 1st block, hence giving the same data to the two FrozenBlock :   Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=head] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@73681674 BlockEntityData : id:"minecraft:bed",x:3,y:-60,z:-6} Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=3, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=2, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} here is the code inside my custom "freeze" item :    @Override     public @NotNull InteractionResult useOn(@NotNull UseOnContext pContext) {         if (!pContext.getLevel().isClientSide() && pContext.getHand() == InteractionHand.MAIN_HAND) {             BlockPos blockPos = pContext.getClickedPos();             BlockPos secondBlockPos = getMultiblockPos(blockPos, pContext.getLevel().getBlockState(blockPos));             if (secondBlockPos != null) {                 createFrozenBlock(pContext, secondBlockPos);             }             createFrozenBlock(pContext, blockPos);             return InteractionResult.SUCCESS;         }         return super.useOn(pContext);     }     public static void createFrozenBlock(UseOnContext pContext, BlockPos blockPos) {         BlockState oldState = pContext.getLevel().getBlockState(blockPos);         BlockEntity oldBlockEntity = oldState.hasBlockEntity() ? pContext.getLevel().getBlockEntity(blockPos) : null;         CompoundTag oldBlockEntityData = oldState.hasBlockEntity() ? oldBlockEntity.serializeNBT() : null;         if (oldBlockEntity != null) {             pContext.getLevel().removeBlockEntity(blockPos);         }         BlockState FrozenBlock = setFrozenBlock(oldState, oldBlockEntity, oldBlockEntityData);         pContext.getLevel().setBlockAndUpdate(blockPos, FrozenBlock);     }     public static BlockState setFrozenBlock(BlockState blockState, @Nullable BlockEntity blockEntity, @Nullable CompoundTag blockEntityData) {         BlockState FrozenBlock = BlockRegister.FROZEN_BLOCK.get().defaultBlockState();         ((FrozenBlock) FrozenBlock.getBlock()).setOldBlockFields(blockState, blockEntity, blockEntityData);         return FrozenBlock;     }  
  • Topics

×
×
  • Create New...

Important Information

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