Jump to content

Recommended Posts

Posted (edited)

Hi All,

 

Looking for some guidance/expert advice on my code.

 

I created an item that appears to do what I want it to (e.g. teleport the player to the location they are looking on right-click).

 

I used the source for EndermanEnity and EnderPearlEntity as a reference point.

 

I have an uneasy feeling that they way I am doing this is dangerous (or flat out wrong) and I'm looking for expert advice.  While testing the mod on a local server, I get an occasional crash.

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x0000000110ec8aa1, pid=34978, tid=0x0000000000005003
#
# JRE version: Java(TM) SE Runtime Environment (8.0_151-b12) (build 1.8.0_151-b12)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.151-b12 mixed mode bsd-amd64 compressed oops)
# Problematic frame:
# V  [libjvm.dylib+0x4c8aa1]
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# /Users/kwpugh/eclipse-workspace/gobber2-1.14/run/hs_err_pid34978.log
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.java.com/bugreport/crash.jsp
#

 

Link to the client crash report:  https://gist.github.com/kwpugh/36d24c7a315e260a0417c1ebcee09bce

Link to server crash report:  https://gist.github.com/kwpugh/6dc5976977ea55c510c4db09275d3410

 

Here are my classes:

package com.kwpugh.gobber2.items.rings;

import java.util.List;

import com.kwpugh.gobber2.util.RayTraceUtil;

import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ActionResult;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.Hand;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.SoundEvents;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.world.World;

public class ItemCustomRingBlink extends Item
{
	public ItemCustomRingBlink(Properties properties)
	{
		super(properties);
	}

	public ActionResult<ItemStack> onItemRightClick(World world, PlayerEntity player, Hand hand)
	{
		ItemStack stack = player.getHeldItem(hand);
		ItemStack equippedMain = player.getHeldItemMainhand();	
		
        if(equippedMain == stack)   //Only works in the main hand
        {
			if (!world.isRemote)
			{
				if(player instanceof LivingEntity)
				{
					LivingEntity livingentity = player;
					
					if(livingentity instanceof ServerPlayerEntity)
					{
						ServerPlayerEntity serverplayerentity = (ServerPlayerEntity)livingentity;
						
						RayTraceResult pos = RayTraceUtil.getNearestPositionWithAir(world, player, 100);
			            if(pos != null && (pos.getType() == RayTraceResult.Type.BLOCK || player.rotationPitch >= -5))
			            {
			            	int side = pos.getType().ordinal();
			                if(side != -1)
			                {
			                    double x = pos.getHitVec().x-(side == 4 ? 0.5 : 0)+(side == 5 ? 0.5 : 0);
			                    double y = pos.getHitVec().y-(side == 0 ? 2.0 : 0)+(side == 1 ? 0.5 : 0);
			                    double z = pos.getHitVec().z-(side == 2 ? 0.5 : 0)+(side == 3 ? 0.5 : 0);

			                    //Use the existing event for ender pearl teleport for our own purposes
			                    net.minecraftforge.event.entity.living.EnderTeleportEvent event = new net.minecraftforge.event.entity.living.EnderTeleportEvent(serverplayerentity, player.posX, player.posY, player.posZ, 5.0F);
			                    
				           		if (livingentity.isPassenger())
								{
				           			livingentity.stopRiding();
					            }
				           		
				           		//Set the target values for the event to the location the player is looking
				           		event.setTargetX(x);
				           		event.setTargetY(y);
				           		event.setTargetZ(z);
				           		
				           		livingentity.setPositionAndUpdate(event.getTargetX(), event.getTargetY(), event.getTargetZ());			           		
			                   
			                    world.playSound(null, player.posX, player.posY, player.posZ, SoundEvents.ENTITY_ENDERMAN_TELEPORT, SoundCategory.PLAYERS, 1.0F, 1.0F);
			
			                    return ActionResult.newResult(ActionResultType.SUCCESS, stack);
			                }
			            }		 
					}
				}
	        }
        }
        return new ActionResult<ItemStack>(ActionResultType.SUCCESS, stack);
	}
   
    @Override
	public void addInformation(ItemStack stack, World world, List<ITextComponent> list, ITooltipFlag flag)
	{
		super.addInformation(stack, world, list, flag);				
		list.add(new StringTextComponent(TextFormatting.BLUE + "Teleports player to the location looking at"));
		list.add(new StringTextComponent(TextFormatting.GREEN + "Right-click to use"));
		list.add(new StringTextComponent(TextFormatting.YELLOW + "Range limit: 100 blocks"));
	} 

}

 

The RayTraceUtil is outside the item because I use it elsewhere in my mod.

 

package com.kwpugh.gobber2.util;

import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.RayTraceContext;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;

public class RayTraceUtil
{
	public static RayTraceResult getNearestPositionWithAir(World world, PlayerEntity player, int reach)
    {
        return getMovingObjectPosWithReachDistance(world, player, reach, false, false, true);
    }

    private static RayTraceResult getMovingObjectPosWithReachDistance(World world, PlayerEntity player, double distance, boolean p1, boolean p2, boolean p3)
    {
        float f = player.rotationPitch;
        float f1 = player.rotationYaw;
        double d0 = player.posX;
        double d1 = player.posY + (double) player.getEyeHeight();
        double d2 = player.posZ;
        Vec3d vec3 = new Vec3d(d0, d1, d2);
        float f2 = MathHelper.cos(-f1 * 0.017453292F - (float) Math.PI);
        float f3 = MathHelper.sin(-f1 * 0.017453292F - (float) Math.PI);
        float f4 = -MathHelper.cos(-f * 0.017453292F);
        float f5 = MathHelper.sin(-f * 0.017453292F);
        float f6 = f3 * f4;
        float f7 = f2 * f4;
        Vec3d vec31 = vec3.add((double) f6 * distance, (double) f5 * distance, (double) f7 * distance);
        
        return world.rayTraceBlocks(new RayTraceContext(vec3, vec31, RayTraceContext.BlockMode.COLLIDER, RayTraceContext.FluidMode.ANY, player));
    }
}

 

Thank you in advance for any helpful advice.

 

Regards.

Edited by kwpugh
Posted

Ok, so I stopped looking at the screen and took a nap.

 

Looking at it again with fresh eyes, I was able to (hopefully) simplify it a bit.   I also noticed the .connection.setLocationAndUpdate in the ServerPlayNetHandler.   I was thinking my problem was I was moving the player around and not sending notice to the server of that fact (I think).  I was hoping the ".connection" was sufficient to let the server know.

 

I spent some time playing SP and on the local server, no crashes, no excess ticking.

 

Here is the current class:

package com.kwpugh.gobber2.items.rings;

import java.util.List;

import com.kwpugh.gobber2.util.RayTraceUtil;

import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ActionResult;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.Hand;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.SoundEvents;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.world.World;

public class ItemCustomRingBlink extends Item
{
	public ItemCustomRingBlink(Properties properties)
	{
		super(properties);
	}

	public ActionResult<ItemStack> onItemRightClick(World world, PlayerEntity player, Hand hand)
	{
		ItemStack stack = player.getHeldItem(hand);
		ItemStack equippedMain = player.getHeldItemMainhand();	
		
        if(equippedMain == stack)   //Only works in the main hand
        {
			if (!world.isRemote)
			{						
				RayTraceResult pos = RayTraceUtil.getNearestPositionWithAir(world, player, 100);
	            if(pos != null && (pos.getType() == RayTraceResult.Type.BLOCK || player.rotationPitch >= -5))
	            {
	            	int side = pos.getType().ordinal();
	                if(side != -1)
	                {
	                    double x = pos.getHitVec().x-(side == 4 ? 0.5 : 0)+(side == 5 ? 0.5 : 0);
	                    double y = pos.getHitVec().y-(side == 0 ? 2.0 : 0)+(side == 1 ? 0.5 : 0);
	                    double z = pos.getHitVec().z-(side == 2 ? 0.5 : 0)+(side == 3 ? 0.5 : 0);
		           		
		           		player.stopRiding();
		           		((ServerPlayerEntity)player).connection.setPlayerLocation(x, y, z, player.rotationYaw, player.rotationPitch);
	                   
	                    world.playSound(null, player.posX, player.posY, player.posZ, SoundEvents.ENTITY_ENDERMAN_TELEPORT, SoundCategory.PLAYERS, 1.0F, 1.0F);
	
	                    return ActionResult.newResult(ActionResultType.SUCCESS, stack);
	                }
	            }		 
	        }
        }
        return new ActionResult<ItemStack>(ActionResultType.SUCCESS, stack);
	}
	
    @Override
	public void addInformation(ItemStack stack, World world, List<ITextComponent> list, ITooltipFlag flag)
	{
		super.addInformation(stack, world, list, flag);				
		list.add(new StringTextComponent(TextFormatting.BLUE + "Teleports player to the location looking at"));
		list.add(new StringTextComponent(TextFormatting.GREEN + "Right-click to use"));
		list.add(new StringTextComponent(TextFormatting.YELLOW + "Range limit: 100 blocks"));
	} 

}

 

I did not make any changes to the RayTraceUtil that I had previously posted.

 

Is there anything glaringly wrong I'm doing or is this ok?

 

Regards.

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.