Jump to content

Preventing the player from punching/attacking on left click


Recommended Posts

Posted

I need a way to temporarily make it so that a player is not able to punch/attack when they hit left click. I don't have any code yet because I'm unsure of where exactly to start.

Posted
  On 8/30/2019 at 5:02 PM, Eilux said:

not able to punch/attack when they hit left click.

Expand  

Define not able to. Do you mean no animation or no actual damage or knockback.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Posted
  On 8/30/2019 at 5:07 PM, Eilux said:

no damage, or knockback

Expand  

I think you can just cancel the LivingDamageEvent(maybe another one) and no damage will be applied. If that doesnt work for knockback then you can use the KnockBack event.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Posted

the function says that it will still consume absorption hearts and armor durability I need a way to prevent this.

* Also note that appropriate resources (like armor durability and absorption extra hearths) have already been consumed.<br>
...
* If this event is canceled, the Entity is not hurt. Used resources WILL NOT be restored.

 

Posted

What version of Minecraft are we looking at?

This is my Forum Signature, I am currently attempting to transform it into a small guide for fixing easier issues using spoiler blocks to keep things tidy.

 

As the most common issue I feel I should put this outside the main bulk:

The only official source for Forge is https://files.minecraftforge.net, and the only site I trust for getting mods is CurseForge.

If you use any site other than these, please take a look at the StopModReposts project and install their browser extension, I would also advise running a virus scan.

 

For players asking for assistance with Forge please expand the spoiler below and read the appropriate section(s) in its/their entirety.

  Reveal hidden contents

 

Posted

Are we talking about stopping the player attacking all entities, specific entities, entities and blocks, etc.?

This is my Forum Signature, I am currently attempting to transform it into a small guide for fixing easier issues using spoiler blocks to keep things tidy.

 

As the most common issue I feel I should put this outside the main bulk:

The only official source for Forge is https://files.minecraftforge.net, and the only site I trust for getting mods is CurseForge.

If you use any site other than these, please take a look at the StopModReposts project and install their browser extension, I would also advise running a virus scan.

 

For players asking for assistance with Forge please expand the spoiler below and read the appropriate section(s) in its/their entirety.

  Reveal hidden contents

 

Posted

I think you'll want to listen for both PlayerInteractEvent.LeftClickBlock and AttackEntityEvent

This is my Forum Signature, I am currently attempting to transform it into a small guide for fixing easier issues using spoiler blocks to keep things tidy.

 

As the most common issue I feel I should put this outside the main bulk:

The only official source for Forge is https://files.minecraftforge.net, and the only site I trust for getting mods is CurseForge.

If you use any site other than these, please take a look at the StopModReposts project and install their browser extension, I would also advise running a virus scan.

 

For players asking for assistance with Forge please expand the spoiler below and read the appropriate section(s) in its/their entirety.

  Reveal hidden contents

 

Posted

I created some test code in my Main however it doesn't seem to work.

 

    @SubscribeEvent
    public void AttackEntityEvent(AttackEntityEvent event){
        event.setCanceled(true);
    }

 

Posted

heres the rest of my main if that helps:

package eilux.mod;



import net.minecraftforge.event.entity.player.AttackEntityEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

@Mod(modid = Main.MODID, name = Main.NAME, version = Main.VERSION, acceptedMinecraftVersions = Main.MC_VERSION)
public class Main {
    public static final String MODID = "mc_general_expansion";
    public static final String NAME = "Minecraft General Expansion";
    public static final String VERSION = "0.0";
    public static final String MC_VERSION = "[1.12.2]";


    public static final Logger LOGGER = LogManager.getLogger(Main.MODID);



    @Mod.EventHandler
    public void preInit(FMLPreInitializationEvent event){

    }

    @Mod.EventHandler
    public void init(FMLInitializationEvent event){
        LOGGER.info(Main.NAME + " says it wants functionality.");
    }

    @Mod.EventHandler
    public void postInit(FMLPostInitializationEvent event){

    }

    @SubscribeEvent
    public void AttackEntityEvent(AttackEntityEvent event) {
        event.setCanceled(true);
    }
}

 

Posted

I have tried putting the event in an event handler class and registering it but it still isn't working.

package eilux.mod.util;


import net.minecraftforge.event.entity.player.AttackEntityEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

@Mod.EventBusSubscriber
public class EventHandler {

    @SubscribeEvent
    public void AttackEntityEvent(AttackEntityEvent event) {
        event.setCanceled(true);
    }
}

 

Posted
  On 9/2/2019 at 8:07 PM, Eilux said:

I have tried putting the event in an event handler class and registering it but it still isn't working.

Expand  

You must not have read it thoroughly enough. Because you need to make your method static if you use the EventBusSubscriber

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Posted

It's working now. I was wondering if there is also a way to prevent the animation from happening and the attack speed from reseting. Thank you everyone for your help so far.

 

Posted (edited)
  On 9/2/2019 at 8:18 PM, Eilux said:

It's working now. I was wondering if there is also a way to prevent the animation from happening and the attack speed from reseting. Thank you everyone for your help so far.

 

Expand  

I think you can cancel MouseEvent when the player left clicks and that solves your issues. Since the client needs to be notified

Edited by Differentiation
Posted
/** The instance of the Minecraft Client, set in the constructor. */
private static Minecraft instance;

...

public static Minecraft getMinecraft()
    {
        return instance;
    }

Here is the code in the Minecraft class.

Posted

when I try to use it with reflection I get the following error: 

code:

            Class<?> minecraftClass = Minecraft.getMinecraft();

error:

Incompatible Types.

required: java.lang.Class

found: net.minecraft.client.Minecraft

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.