Jump to content

[1.10.2] Event not working at all.


Bloopers

Recommended Posts

Hello. I've created a custom event with a tutorial from jabelar, that extends MouseEvent, and I've managed to get rid of all error(as the tutorial was for 1.8, there were plenty of errors) - but when I load the game it just doesn't do anything. I think it has to do with the actual registering of the event.

 

In my CommonProxy's init method, I put in

MouseEventHandler handler = new MouseEventHandler();
    	MinecraftForge.EVENT_BUS.register(handler); 

 

Is there a different way to register events in 1.10? I'm a complete noob when it comes to events, only done very basic things with them.

Link to comment
Share on other sites

  • Replies 52
  • Created
  • Last Reply

Top Posters In This Topic

Hello. I've created a custom event with a tutorial from jabelar, that extends MouseEvent

 

Welp, there's your problem.  You're not supposed to extend the event classes.  You're supposed to create a single function that takes a single parameter (of any

Event

subtype) and mark it with an

@EventHandler

annotation.

 

Can you please link the tutorial you were using?

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Hello. I've created a custom event with a tutorial from jabelar, that extends MouseEvent

 

Welp, there's your problem.  You're not supposed to extend the event classes.  You're supposed to create a single function that takes a single parameter (of any

Event

subtype) and mark it with an

@EventHandler

annotation.

 

Can you please link the tutorial you were using?

http://jabelarminecraft.blogspot.com/p/minecraft-modding-extending-reach-of.html

 

Link to comment
Share on other sites

Aha! Jabelar was extending the reach of a sword, not extending the mouse event class. Go back and take a closer look at the tutorial and then start over.

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

Link to comment
Share on other sites

Aha! Jabelar was extending the reach of a sword, not extending the mouse event class. Go back and take a closer look at the tutorial and then start over.

I removed the part that makes it extend MouseEvent, and then tried launching again and it's the same result. Is there a reason that I should start over?

Link to comment
Share on other sites

Do you have this anywhere:

 

@SubscribeEvent
public void onEvent(MouseEvent event) {
    //...
}

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Do you have this anywhere:

 

@SubscribeEvent
public void onEvent(MouseEvent event) {
    //...
}

Yes.

And inside it is everything the event should do.

Do you know Java or was that a retorical question?

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.

Link to comment
Share on other sites

Post updated code. Are you sure you are subscribing to right MouseEvent?

Are there multiple MouseEvents?

I will post all of the code that was changed from jabelar's tutorial to fit 1.10 in a bit. I'm not on the same computer that has it right now.

MouseEvent should be donenin client proxy because the server doesnt need to know what do you have inside that method? Are you importing the correct MouseEvent?

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.

Link to comment
Share on other sites

Post updated code. Are you sure you are subscribing to right MouseEvent?

Are there multiple MouseEvents?

I will post all of the code that was changed from jabelar's tutorial to fit 1.10 in a bit. I'm not on the same computer that has it right now.

MouseEvent should be donenin client proxy because the server doesnt need to know what do you have inside that method? Are you importing the correct MouseEvent?

net.minecraftforge.client.event.MouseEvent is being imported. I moved it from CommonProxy to ClientProxy, still no difference.

 

 

MouseEventHandler:

package bloopers.spearmod.reach;

import java.awt.List;

import org.lwjgl.input.Mouse;

import bloopers.spearmod.SpearMod;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.math.Vec3d;
import net.minecraftforge.client.event.MouseEvent;
import net.minecraftforge.fml.client.FMLClientHandler;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.eventhandler.EventPriority;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

public class MouseEventHandler {

    

@SideOnly(Side.CLIENT)
@SubscribeEvent
@EventHandler
public void onEvent(MouseEvent event)
{ 
	int button = event.getButton();
    boolean buttonstate = event.isButtonstate();
    
    
    if (button == 0 && buttonstate)
    {
        Minecraft mc = Minecraft.getMinecraft();
        EntityPlayer thePlayer = mc.thePlayer;
        if (thePlayer != null)
        {
            ItemStack itemstack = thePlayer.getHeldItemMainhand();
            IExtendedReach ieri;
            if (itemstack != null)
            {
                if (itemstack.getItem() instanceof IExtendedReach)
                {
                    ieri = (IExtendedReach) itemstack.getItem();
                } else
                {
                    ieri = null;
                }
   
                if (ieri != null)
                {
                    float reach = ieri.getReach();
                    RayTraceResult mov = getMouseOverExtended(reach); 
                      
                    if (mov != null)
                    {
                        if (mov.entityHit != null && mov.entityHit.hurtResistantTime == 0)
                        {
                            if (mov.entityHit != thePlayer )
                            {
                                SpearMod.network.sendToServer(new MessageExtendedReachAttack(
                                      mov.entityHit.getEntityId()));
                            }
                        }
                    }
                }
            }
        }
    }
}
        
// This is mostly copied from the EntityRenderer#getMouseOver() method
public static RayTraceResult getMouseOverExtended(float dist)
{
    Minecraft mc = FMLClientHandler.instance().getClient();
    Entity theRenderViewEntity = mc.getRenderViewEntity();
    AxisAlignedBB theViewBoundingBox = new AxisAlignedBB(
            theRenderViewEntity.posX-0.5D,
            theRenderViewEntity.posY-0.0D,
            theRenderViewEntity.posZ-0.5D,
            theRenderViewEntity.posX+0.5D,
            theRenderViewEntity.posY+1.5D,
            theRenderViewEntity.posZ+0.5D
            );
    RayTraceResult returnMOP = null;
    if (mc.theWorld != null)
    {
        double var2 = dist;
        returnMOP = theRenderViewEntity.rayTrace(var2, 0);
        double calcdist = var2;
        Vec3d pos = theRenderViewEntity.getPositionEyes(0);
        var2 = calcdist;
        if (returnMOP != null)
        {
            calcdist = returnMOP.hitVec.distanceTo(pos);
        }
         
        Vec3d lookvec = theRenderViewEntity.getLook(0);
        Vec3d var8 = pos.addVector(lookvec.xCoord * var2, 
              lookvec.yCoord * var2, 
              lookvec.zCoord * var2);
        Entity pointedEntity = null;
        float var9 = 1.0F;
        @SuppressWarnings("unchecked")
        java.util.List<Entity> list = mc.theWorld.getEntitiesWithinAABBExcludingEntity(
              theRenderViewEntity, 
              theViewBoundingBox.addCoord(
                    lookvec.xCoord * var2, 
                    lookvec.yCoord * var2, 
                    lookvec.zCoord * var2).expand(var9, var9, var9));
        double d = calcdist;
            
        for (Entity entity : list)
        {
            if (entity.canBeCollidedWith())
            {
                float bordersize = entity.getCollisionBorderSize();
                AxisAlignedBB aabb = new AxisAlignedBB(
                      entity.posX-entity.width/2, 
                      entity.posY, 
                      entity.posZ-entity.width/2, 
                      entity.posX+entity.width/2, 
                      entity.posY+entity.height, 
                      entity.posZ+entity.width/2);
                aabb.expand(bordersize, bordersize, bordersize);
                RayTraceResult mop0 = aabb.calculateIntercept(pos, var8);
                    
                if (aabb.isVecInside(pos))
                {
                    if (0.0D < d || d == 0.0D)
                    {
                        pointedEntity = entity;
                        d = 0.0D;
                    }
                } else if (mop0 != null)
                {
                    double d1 = pos.distanceTo(mop0.hitVec);
                        
                    if (d1 < d || d == 0.0D)
                    {
                        pointedEntity = entity;
                        d = d1;
                    }
                }
            }
        }
           
        if (pointedEntity != null && (d < calcdist || returnMOP == null))
        {
             returnMOP = new RayTraceResult(pointedEntity);
        }
    }
    return returnMOP;
}

}

 

MessageExtendedReachAttack:

package bloopers.spearmod.reach;

import bloopers.spearmod.SpearMod;
import io.netty.buffer.ByteBuf;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraftforge.fml.common.network.ByteBufUtils;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;

public class MessageExtendedReachAttack implements IMessage 
{
    private int entityId ;

    public MessageExtendedReachAttack() 
    { 
     // need this constructor
    }

    public MessageExtendedReachAttack(int parEntityId) 
    {
     entityId = parEntityId;
        // DEBUG
        System.out.println("Constructor");
    }

    @Override
    public void fromBytes(ByteBuf buf) 
    {
     entityId = ByteBufUtils.readVarInt(buf, 4);
     // DEBUG
     System.out.println("fromBytes");
    }

    @Override
    public void toBytes(ByteBuf buf) 
    {
     ByteBufUtils.writeVarInt(buf, entityId, 4);
        // DEBUG
        System.out.println("toBytes encoded");
    }

    public static class Handler implements IMessageHandler<MessageExtendedReachAttack, 
          IMessage> 
    {
        @Override
        public IMessage onMessage(final MessageExtendedReachAttack message, 
              MessageContext ctx) 
        {
            // DEBUG
            System.out.println("Message received");
            // Know it will be on the server so make it thread-safe
            final EntityPlayerMP thePlayer = (EntityPlayerMP) SpearMod.proxy.
                  getPlayerEntityFromContext(ctx);
            thePlayer.getServer().addScheduledTask(
                  new Runnable()
                  {
                      @Override
                      public void run() 
                      {
                          Entity theEntity = thePlayer.worldObj.
                                getEntityByID(message.entityId);
                          // DEBUG
                          System.out.println("Entity = "+theEntity);
                          
                          // Need to ensure that hackers can't cause trick kills, 
                          // so double check weapon type and reach
                          if (thePlayer.getHeldItemMainhand() == null)
                          {
                              return;
                          }
                          if (thePlayer.getHeldItemMainhand().getItem() instanceof 
                                IExtendedReach)
                          {
                              IExtendedReach theExtendedReachWeapon = 
                                    (IExtendedReach)thePlayer.getHeldItemMainhand().
                                    getItem();
                              double distanceSq = thePlayer.getDistanceSqToEntity(
                                    theEntity);
                              double reachSq =theExtendedReachWeapon.getReach()*
                                    theExtendedReachWeapon.getReach();
                              if (reachSq >= distanceSq)
                              {
                                  thePlayer.attackTargetEntityWithCurrentItem(
                                        theEntity);
                              }
                          }
                          return; // no response in this case
                      }
                }
            );
            return null; // no response message
        }
    }
}

 

IExtendedReach:

package bloopers.spearmod.reach;

public interface IExtendedReach {

    public float getReach(); // default is 1.0D

}

 

And then in item classes to set the reach:

@Override
    public float getReach() 
    {
        return 15.0F;
    }

 

At 15 for testing.

Link to comment
Share on other sites

Post updated code. Are you sure you are subscribing to right MouseEvent?

Are there multiple MouseEvents?

I will post all of the code that was changed from jabelar's tutorial to fit 1.10 in a bit. I'm not on the same computer that has it right now.

MouseEvent should be donenin client proxy because the server doesnt need to know what do you have inside that method? Are you importing the correct MouseEvent?

net.minecraftforge.client.event.MouseEvent is being imported. I moved it from CommonProxy to ClientProxy, still no difference.

 

 

MouseEventHandler:

package bloopers.spearmod.reach;

import java.awt.List;

import org.lwjgl.input.Mouse;

import bloopers.spearmod.SpearMod;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.math.Vec3d;
import net.minecraftforge.client.event.MouseEvent;
import net.minecraftforge.fml.client.FMLClientHandler;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.eventhandler.EventPriority;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

public class MouseEventHandler {

    

@SideOnly(Side.CLIENT)
@SubscribeEvent
@EventHandler
public void onEvent(MouseEvent event)
{ 
	int button = event.getButton();
    boolean buttonstate = event.isButtonstate();
    
    
    if (button == 0 && buttonstate)
    {
        Minecraft mc = Minecraft.getMinecraft();
        EntityPlayer thePlayer = mc.thePlayer;
        if (thePlayer != null)
        {
            ItemStack itemstack = thePlayer.getHeldItemMainhand();
            IExtendedReach ieri;
            if (itemstack != null)
            {
                if (itemstack.getItem() instanceof IExtendedReach)
                {
                    ieri = (IExtendedReach) itemstack.getItem();
                } else
                {
                    ieri = null;
                }
   
                if (ieri != null)
                {
                    float reach = ieri.getReach();
                    RayTraceResult mov = getMouseOverExtended(reach); 
                      
                    if (mov != null)
                    {
                        if (mov.entityHit != null && mov.entityHit.hurtResistantTime == 0)
                        {
                            if (mov.entityHit != thePlayer )
                            {
                                SpearMod.network.sendToServer(new MessageExtendedReachAttack(
                                      mov.entityHit.getEntityId()));
                            }
                        }
                    }
                }
            }
        }
    }
}
        
// This is mostly copied from the EntityRenderer#getMouseOver() method
public static RayTraceResult getMouseOverExtended(float dist)
{
    Minecraft mc = FMLClientHandler.instance().getClient();
    Entity theRenderViewEntity = mc.getRenderViewEntity();
    AxisAlignedBB theViewBoundingBox = new AxisAlignedBB(
            theRenderViewEntity.posX-0.5D,
            theRenderViewEntity.posY-0.0D,
            theRenderViewEntity.posZ-0.5D,
            theRenderViewEntity.posX+0.5D,
            theRenderViewEntity.posY+1.5D,
            theRenderViewEntity.posZ+0.5D
            );
    RayTraceResult returnMOP = null;
    if (mc.theWorld != null)
    {
        double var2 = dist;
        returnMOP = theRenderViewEntity.rayTrace(var2, 0);
        double calcdist = var2;
        Vec3d pos = theRenderViewEntity.getPositionEyes(0);
        var2 = calcdist;
        if (returnMOP != null)
        {
            calcdist = returnMOP.hitVec.distanceTo(pos);
        }
         
        Vec3d lookvec = theRenderViewEntity.getLook(0);
        Vec3d var8 = pos.addVector(lookvec.xCoord * var2, 
              lookvec.yCoord * var2, 
              lookvec.zCoord * var2);
        Entity pointedEntity = null;
        float var9 = 1.0F;
        @SuppressWarnings("unchecked")
        java.util.List<Entity> list = mc.theWorld.getEntitiesWithinAABBExcludingEntity(
              theRenderViewEntity, 
              theViewBoundingBox.addCoord(
                    lookvec.xCoord * var2, 
                    lookvec.yCoord * var2, 
                    lookvec.zCoord * var2).expand(var9, var9, var9));
        double d = calcdist;
            
        for (Entity entity : list)
        {
            if (entity.canBeCollidedWith())
            {
                float bordersize = entity.getCollisionBorderSize();
                AxisAlignedBB aabb = new AxisAlignedBB(
                      entity.posX-entity.width/2, 
                      entity.posY, 
                      entity.posZ-entity.width/2, 
                      entity.posX+entity.width/2, 
                      entity.posY+entity.height, 
                      entity.posZ+entity.width/2);
                aabb.expand(bordersize, bordersize, bordersize);
                RayTraceResult mop0 = aabb.calculateIntercept(pos, var8);
                    
                if (aabb.isVecInside(pos))
                {
                    if (0.0D < d || d == 0.0D)
                    {
                        pointedEntity = entity;
                        d = 0.0D;
                    }
                } else if (mop0 != null)
                {
                    double d1 = pos.distanceTo(mop0.hitVec);
                        
                    if (d1 < d || d == 0.0D)
                    {
                        pointedEntity = entity;
                        d = d1;
                    }
                }
            }
        }
           
        if (pointedEntity != null && (d < calcdist || returnMOP == null))
        {
             returnMOP = new RayTraceResult(pointedEntity);
        }
    }
    return returnMOP;
}

}

 

MessageExtendedReachAttack:

package bloopers.spearmod.reach;

import bloopers.spearmod.SpearMod;
import io.netty.buffer.ByteBuf;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraftforge.fml.common.network.ByteBufUtils;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;

public class MessageExtendedReachAttack implements IMessage 
{
    private int entityId ;

    public MessageExtendedReachAttack() 
    { 
     // need this constructor
    }

    public MessageExtendedReachAttack(int parEntityId) 
    {
     entityId = parEntityId;
        // DEBUG
        System.out.println("Constructor");
    }

    @Override
    public void fromBytes(ByteBuf buf) 
    {
     entityId = ByteBufUtils.readVarInt(buf, 4);
     // DEBUG
     System.out.println("fromBytes");
    }

    @Override
    public void toBytes(ByteBuf buf) 
    {
     ByteBufUtils.writeVarInt(buf, entityId, 4);
        // DEBUG
        System.out.println("toBytes encoded");
    }

    public static class Handler implements IMessageHandler<MessageExtendedReachAttack, 
          IMessage> 
    {
        @Override
        public IMessage onMessage(final MessageExtendedReachAttack message, 
              MessageContext ctx) 
        {
            // DEBUG
            System.out.println("Message received");
            // Know it will be on the server so make it thread-safe
            final EntityPlayerMP thePlayer = (EntityPlayerMP) SpearMod.proxy.
                  getPlayerEntityFromContext(ctx);
            thePlayer.getServer().addScheduledTask(
                  new Runnable()
                  {
                      @Override
                      public void run() 
                      {
                          Entity theEntity = thePlayer.worldObj.
                                getEntityByID(message.entityId);
                          // DEBUG
                          System.out.println("Entity = "+theEntity);
                          
                          // Need to ensure that hackers can't cause trick kills, 
                          // so double check weapon type and reach
                          if (thePlayer.getHeldItemMainhand() == null)
                          {
                              return;
                          }
                          if (thePlayer.getHeldItemMainhand().getItem() instanceof 
                                IExtendedReach)
                          {
                              IExtendedReach theExtendedReachWeapon = 
                                    (IExtendedReach)thePlayer.getHeldItemMainhand().
                                    getItem();
                              double distanceSq = thePlayer.getDistanceSqToEntity(
                                    theEntity);
                              double reachSq =theExtendedReachWeapon.getReach()*
                                    theExtendedReachWeapon.getReach();
                              if (reachSq >= distanceSq)
                              {
                                  thePlayer.attackTargetEntityWithCurrentItem(
                                        theEntity);
                              }
                          }
                          return; // no response in this case
                      }
                }
            );
            return null; // no response message
        }
    }
}

 

IExtendedReach:

package bloopers.spearmod.reach;

public interface IExtendedReach {

    public float getReach(); // default is 1.0D

}

 

And then in item classes to set the reach:

@Override
    public float getReach() 
    {
        return 15.0F;
    }

 

At 15 for testing.

What is button 0?

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.

Link to comment
Share on other sites

Post updated code. Are you sure you are subscribing to right MouseEvent?

Are there multiple MouseEvents?

I will post all of the code that was changed from jabelar's tutorial to fit 1.10 in a bit. I'm not on the same computer that has it right now.

MouseEvent should be donenin client proxy because the server doesnt need to know what do you have inside that method? Are you importing the correct MouseEvent?

net.minecraftforge.client.event.MouseEvent is being imported. I moved it from CommonProxy to ClientProxy, still no difference.

 

 

MouseEventHandler:

package bloopers.spearmod.reach;

import java.awt.List;

import org.lwjgl.input.Mouse;

import bloopers.spearmod.SpearMod;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.math.Vec3d;
import net.minecraftforge.client.event.MouseEvent;
import net.minecraftforge.fml.client.FMLClientHandler;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.eventhandler.EventPriority;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

public class MouseEventHandler {

    

@SideOnly(Side.CLIENT)
@SubscribeEvent
@EventHandler
public void onEvent(MouseEvent event)
{ 
	int button = event.getButton();
    boolean buttonstate = event.isButtonstate();
    
    
    if (button == 0 && buttonstate)
    {
        Minecraft mc = Minecraft.getMinecraft();
        EntityPlayer thePlayer = mc.thePlayer;
        if (thePlayer != null)
        {
            ItemStack itemstack = thePlayer.getHeldItemMainhand();
            IExtendedReach ieri;
            if (itemstack != null)
            {
                if (itemstack.getItem() instanceof IExtendedReach)
                {
                    ieri = (IExtendedReach) itemstack.getItem();
                } else
                {
                    ieri = null;
                }
   
                if (ieri != null)
                {
                    float reach = ieri.getReach();
                    RayTraceResult mov = getMouseOverExtended(reach); 
                      
                    if (mov != null)
                    {
                        if (mov.entityHit != null && mov.entityHit.hurtResistantTime == 0)
                        {
                            if (mov.entityHit != thePlayer )
                            {
                                SpearMod.network.sendToServer(new MessageExtendedReachAttack(
                                      mov.entityHit.getEntityId()));
                            }
                        }
                    }
                }
            }
        }
    }
}
        
// This is mostly copied from the EntityRenderer#getMouseOver() method
public static RayTraceResult getMouseOverExtended(float dist)
{
    Minecraft mc = FMLClientHandler.instance().getClient();
    Entity theRenderViewEntity = mc.getRenderViewEntity();
    AxisAlignedBB theViewBoundingBox = new AxisAlignedBB(
            theRenderViewEntity.posX-0.5D,
            theRenderViewEntity.posY-0.0D,
            theRenderViewEntity.posZ-0.5D,
            theRenderViewEntity.posX+0.5D,
            theRenderViewEntity.posY+1.5D,
            theRenderViewEntity.posZ+0.5D
            );
    RayTraceResult returnMOP = null;
    if (mc.theWorld != null)
    {
        double var2 = dist;
        returnMOP = theRenderViewEntity.rayTrace(var2, 0);
        double calcdist = var2;
        Vec3d pos = theRenderViewEntity.getPositionEyes(0);
        var2 = calcdist;
        if (returnMOP != null)
        {
            calcdist = returnMOP.hitVec.distanceTo(pos);
        }
         
        Vec3d lookvec = theRenderViewEntity.getLook(0);
        Vec3d var8 = pos.addVector(lookvec.xCoord * var2, 
              lookvec.yCoord * var2, 
              lookvec.zCoord * var2);
        Entity pointedEntity = null;
        float var9 = 1.0F;
        @SuppressWarnings("unchecked")
        java.util.List<Entity> list = mc.theWorld.getEntitiesWithinAABBExcludingEntity(
              theRenderViewEntity, 
              theViewBoundingBox.addCoord(
                    lookvec.xCoord * var2, 
                    lookvec.yCoord * var2, 
                    lookvec.zCoord * var2).expand(var9, var9, var9));
        double d = calcdist;
            
        for (Entity entity : list)
        {
            if (entity.canBeCollidedWith())
            {
                float bordersize = entity.getCollisionBorderSize();
                AxisAlignedBB aabb = new AxisAlignedBB(
                      entity.posX-entity.width/2, 
                      entity.posY, 
                      entity.posZ-entity.width/2, 
                      entity.posX+entity.width/2, 
                      entity.posY+entity.height, 
                      entity.posZ+entity.width/2);
                aabb.expand(bordersize, bordersize, bordersize);
                RayTraceResult mop0 = aabb.calculateIntercept(pos, var8);
                    
                if (aabb.isVecInside(pos))
                {
                    if (0.0D < d || d == 0.0D)
                    {
                        pointedEntity = entity;
                        d = 0.0D;
                    }
                } else if (mop0 != null)
                {
                    double d1 = pos.distanceTo(mop0.hitVec);
                        
                    if (d1 < d || d == 0.0D)
                    {
                        pointedEntity = entity;
                        d = d1;
                    }
                }
            }
        }
           
        if (pointedEntity != null && (d < calcdist || returnMOP == null))
        {
             returnMOP = new RayTraceResult(pointedEntity);
        }
    }
    return returnMOP;
}

}

 

MessageExtendedReachAttack:

package bloopers.spearmod.reach;

import bloopers.spearmod.SpearMod;
import io.netty.buffer.ByteBuf;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraftforge.fml.common.network.ByteBufUtils;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;

public class MessageExtendedReachAttack implements IMessage 
{
    private int entityId ;

    public MessageExtendedReachAttack() 
    { 
     // need this constructor
    }

    public MessageExtendedReachAttack(int parEntityId) 
    {
     entityId = parEntityId;
        // DEBUG
        System.out.println("Constructor");
    }

    @Override
    public void fromBytes(ByteBuf buf) 
    {
     entityId = ByteBufUtils.readVarInt(buf, 4);
     // DEBUG
     System.out.println("fromBytes");
    }

    @Override
    public void toBytes(ByteBuf buf) 
    {
     ByteBufUtils.writeVarInt(buf, entityId, 4);
        // DEBUG
        System.out.println("toBytes encoded");
    }

    public static class Handler implements IMessageHandler<MessageExtendedReachAttack, 
          IMessage> 
    {
        @Override
        public IMessage onMessage(final MessageExtendedReachAttack message, 
              MessageContext ctx) 
        {
            // DEBUG
            System.out.println("Message received");
            // Know it will be on the server so make it thread-safe
            final EntityPlayerMP thePlayer = (EntityPlayerMP) SpearMod.proxy.
                  getPlayerEntityFromContext(ctx);
            thePlayer.getServer().addScheduledTask(
                  new Runnable()
                  {
                      @Override
                      public void run() 
                      {
                          Entity theEntity = thePlayer.worldObj.
                                getEntityByID(message.entityId);
                          // DEBUG
                          System.out.println("Entity = "+theEntity);
                          
                          // Need to ensure that hackers can't cause trick kills, 
                          // so double check weapon type and reach
                          if (thePlayer.getHeldItemMainhand() == null)
                          {
                              return;
                          }
                          if (thePlayer.getHeldItemMainhand().getItem() instanceof 
                                IExtendedReach)
                          {
                              IExtendedReach theExtendedReachWeapon = 
                                    (IExtendedReach)thePlayer.getHeldItemMainhand().
                                    getItem();
                              double distanceSq = thePlayer.getDistanceSqToEntity(
                                    theEntity);
                              double reachSq =theExtendedReachWeapon.getReach()*
                                    theExtendedReachWeapon.getReach();
                              if (reachSq >= distanceSq)
                              {
                                  thePlayer.attackTargetEntityWithCurrentItem(
                                        theEntity);
                              }
                          }
                          return; // no response in this case
                      }
                }
            );
            return null; // no response message
        }
    }
}

 

IExtendedReach:

package bloopers.spearmod.reach;

public interface IExtendedReach {

    public float getReach(); // default is 1.0D

}

 

And then in item classes to set the reach:

@Override
    public float getReach() 
    {
        return 15.0F;
    }

 

At 15 for testing.

What is button 0?

Jabelar's tutorial says it means left click, so I assume button 1 would also mean right click.

Link to comment
Share on other sites

Why do you have both @SubscribeEvent and @EventHandler?

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Question:

Is your event not working or is your code not working?

Put a print at start of event.

2nd: Did you register event?

There are no errors in eclipse, but when I start up the game and test it out, it's as if it doesn't exist.

What is a print?

Yes, I am registering it in the FMLInitizializationEvent method in my CommonProxy. Anime fan suggested client proxy instead but that didn't change anything so I went back to CommonProxy

Link to comment
Share on other sites

Question:

Is your event not working or is your code not working?

Put a print at start of event.

2nd: Did you register event?

Ohh, you mean print to console? I just tested it now, apparently the init method on my CommonProxy doesn't load at all. How can I fix this?

Obviously you need to call it from your main init method in your main mod class.

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.

Link to comment
Share on other sites

Question:

Is your event not working or is your code not working?

Put a print at start of event.

2nd: Did you register event?

Ohh, you mean print to console? I just tested it now, apparently the init method on my CommonProxy doesn't load at all. How can I fix this?

Obviously you need to call it from your main init method in your main mod class.

Okay. Now it's in my main mod class init method. Launched the game, tried to hit a mob from far away, and the game crashed. The console tells me that it's line 65 in MouseEventHandler, which is:

SpearMod.network.sendToServer(new MessageExtendedReachAttack(mov.entityHit.getEntityId()));

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

    • S1288POKER Rasakan sensasi bertaruh Mix Parlay yang tak terlupakan dengan bonus fantastis di situs judi terpercaya kami! Kami menawarkan bonus Mix Parlay terbesar di Asia dengan berbagai pilihan taruhan dan minimal deposit rendah. Dapatkan bonus hingga jutaan rupiah untuk taruhan Mix Parlay Anda dan nikmati keuntungan berlipat ganda.
    • Situs Akun Pro Peru adalah pilihan terbaik bagi Anda yang mencari situs terpercaya untuk bermain slot dan taruhan bola. Berikut adalah beberapa alasan mengapa Anda harus memilih Situs Akun Pro Peru: Slot Populer Kami menyajikan koleksi slot yang populer dan menarik dengan berbagai tema yang beragam. Dari slot klasik hingga slot video modern, setiap pemain dapat menemukan permainan favorit mereka dengan mudah. Taruhan Bola Terpercaya Selain slot, kami juga menyediakan taruhan bola dengan pasaran terlengkap dan odds yang kompetitif. Dengan berbagai pertandingan dan liga yang tersedia, Anda dapat menikmati pengalaman taruhan bola yang seru dan menguntungkan. Transaksi Mudah dengan Bank BTN Kami menyediakan layanan transaksi yang mudah dan aman melalui Bank BTN. Dengan dukungan dari sistem pembayaran yang terpercaya, Anda dapat melakukan deposit dan penarikan dana dengan cepat dan tanpa hambatan.  
    • Ngamenslot adalah pilihan terbaik bagi Anda yang mencari situs terpercaya untuk bermain slot dan taruhan bola. Berikut adalah beberapa alasan mengapa Anda harus memilih Ngamenslot: Kombinasi Slot & Bola Kami menyajikan kombinasi terbaik antara permainan slot yang seru dan taruhan bola yang menegangkan. Dengan pilihan permainan yang beragam, setiap pemain dapat menikmati pengalaman bermain yang unik dan menyenangkan. Transaksi Mudah melalui Bank Ekonomi Kami menyediakan layanan transaksi yang mudah dan aman melalui Bank Ekonomi. Dengan dukungan dari sistem pembayaran yang terpercaya, Anda dapat melakukan deposit dan penarikan dana dengan cepat dan tanpa hambatan. Terpercaya dan Terjamin Ngamenslot telah terbukti sebagai situs terpercaya dengan ribuan pemain yang puas. Kami mengutamakan keamanan dan kenyamanan para pemain, sehingga Anda dapat bermain dengan tenang dan fokus pada permainan.  
    • Tambang88 adalah pilihan terbaik bagi Anda yang mencari pengalaman bermain slot gacor dengan transaksi mudah menggunakan Bank Sinarmas. Berikut adalah beberapa alasan mengapa Anda harus memilih Tambang88: Raja Slot Gacor Kami merupakan raja slot gacor dengan koleksi permainan terbaik yang menawarkan kesenangan bermain dan peluang kemenangan besar. Dengan fitur-fitur unggulan dan tema-tema menarik, setiap putaran permainan akan memberikan Anda pengalaman yang tak terlupakan. Transaksi Mudah dengan Bank Sinarmas Kami menyediakan layanan transaksi mudah melalui Bank Sinarmas untuk kenyamanan dan keamanan Anda. Dengan proses yang cepat dan efisien, Anda dapat melakukan deposit dan penarikan dana dengan lancar dan tanpa hambatan. Profesional dan Menguntungkan Tambang88 mengutamakan profesionalitas dalam memberikan layanan kepada para pemainnya. Kami juga menawarkan kesempatan untuk meraih keuntungan yang besar dengan jackpot dan hadiah-hadiah menarik lainnya.    
    • TRIK POLA SL0T GAC0R MAHJONG WAYS 1 DAN 2 HARI INI Sekarang Anda dapat meningkatkan peluang Bermain dengan langsung menggunakan situs resmi dari permainan Mahjong (PG SOFT). Cukup daftar dan siapkan hanya mulai dari 50K hingga 200K, Anda sudah memiliki peluang untuk memenangkan hadiah besar. Jika tidak memiliki dana sebanyak itu, Anda dapat mencoba dengan dana 50K dan memanfaatkan bonus member baru untuk menambah modal. Berikut adalah Situs Resmi Mahjong yang dapat Anda kunjungi: >> SITUS RESMI MAHJONG YANG TERBUKTI << >> SITUS RESMI MAHJONG YANG TERBUKTI << Setelah mendaftar, Anda dapat menggunakan pola yang sering saya gunakan: 🔥 25 Manual TURBO OFF 🔥 15 Manual TURBO ON 🔥 30 Auto TURBO OFF 🔥 10 Manual TURBO ON Pola ini biasanya memberikan hasil yang konsisten.
  • Topics

×
×
  • Create New...

Important Information

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