Jump to content

[1.7.10] Right-click action (place a block) and swimming


TheiTay

Recommended Posts

Hello, I am currently working on a pathfinding mod, which works fine for now, I want to add the modifying-the-world (mine/ build) functionality and I'm trying lately to find an easy way to place a block where I'm looking to, just like when you press right click regulary.

 

Another issue that I'm working on lately is that my pathfinding algorithm doesn't know how to handle water properly, first - it still doesn't know that you have the ability to "swim" and it tries to find the path through the ground underwater, and second - the jump function that I use regularly (EntityPlayer.jump()) so my questions are, is there any way to identify "water surface" which you can swim of? I have a method that can find "standable blocks" but it can't find the difference between water and air. and could you suggest a function that could handle the "swim" action?

 

Thank you.

Link to comment
Share on other sites

Hi

For the title, set entitySwing to true in a ticker to call at a steady pace, this will enable the vanilla "right click action". Do this while tesing if the player is swimming.

Hi, I tried to find something like "entitySwing" but I couldn't, are you sure that it exists in 1.7.10? I found a method of EntityPlayer "swingItem()" but it only swings the item and not actually doing the "right click action" (such as placing the block), I tried to use the function of ItemBlock "placeBlockAt" but it always returns false. I couldn't understand where do I get the metadata from.

Is there any example of using this method or something similar that you can suggest? thanks.

Link to comment
Share on other sites

Hey, I succeeded to place a block using this code:

			ItemStack itemStack = player.getHeldItem();

			player.rotationPitch = 180;
			player.swingItem();

			int x = (int)player.posX;
			int y = (int)player.posY;
			int z = (int)player.posZ;
			ItemBlock block = (ItemBlock)itemStack.getItem();
			World world = Minecraft.getMinecraft().theWorld;
			block.placeBlockAt(itemStack, player, world, x, y - 2, z -1, EnumFacing.UP.ordinal(), 0, 0, 0, player.getHeldItem().getItemDamage());//world.getBlockMetadata(x+1, y-2, z)
			player.getHeldItem().splitStack(1);

 

but the change doesn't affect the block that item that will fall when this block would be broken. in fact - if I'll reload the world it'll change back to the thing it was in the beggining.

 

here's a video to explain:

Link to comment
Share on other sites

Don't know the context of the code you posted, so I cannot give you a  direct answer. But you seem to have a player variable in scope, use the world from the player.

 

Thank you, using this code:

Minecraft mc = Minecraft.getMinecraft();
World world = mc.getIntegratedServer().worldServerForDimension(mc.thePlayer.dimension);		

 

solved my issue.

 

Link to comment
Share on other sites

Oh gawd. Please no.

 

Thank you for the reply.

Since I don't use my mod in multiplayer and getting the world like this didn't do the work (it did the exact same issue that I had before):

 

player = Minecraft.getMinecraft().thePlayer;
World world = player.worldObj;

 

I tried a solution that I found in the forum, which preformed well, why do you suggest to not use it that way?

Link to comment
Share on other sites

Here you posted a code snipped. Post the whole method. Otherwise I cannot help you. As I already said.

 

My mistake.

The method is from a class named "PathNavigator" which navigate the player through a steps-list that it receives. (It's a path finder mod)

 

@SubscribeEvent
public void onPlayerTick(TickEvent.PlayerTickEvent event) {
	if (!run)
		return;
	if (null == currentStep && steps.isEmpty()){
		run = false;
		return;
	}
	if (null == currentStep){
		currentStep = steps.poll();
		targetVec = currentStep.getLocation();
		isJumpedFlag = false;
	}


	EntityPlayer player = Minecraft.getMinecraft().thePlayer;

	//integer positions are the corner of the block - in order to get to the center we add 0.5
	Vec3 direction = 
			Vec3.createVectorHelper(targetVec.xCoord + 0.5 - player.posX,
					targetVec.yCoord - player.posY + PLAYER_HEIGHT,
					targetVec.zCoord + 0.5 - player.posZ);
	//System.out.println((targetVec.xCoord - player.posX) + ", " + (targetVec.yCoord - player.posY) + ", " + (targetVec.zCoord - player.posZ));

	direction.yCoord = 0;




//		if (direction.lengthVector() <= MINIMAL_DISTANCE){
	if(direction.dotProduct(Vec3.createVectorHelper(player.motionX, 0, player.motionZ)) < 0 && Step.StepType.Pole != currentStep.getType()) {
		currentStep = steps.poll();
		if(null != currentStep)
			targetVec = currentStep.getLocation();
		//System.out.println((targetVec.xCoord - player.posX) + ", " + (targetVec.yCoord - player.posY) + ", " + (targetVec.zCoord - player.posZ));
		isJumpedFlag = false;
		player.setVelocity(0, 0, 0);
		return;
	} else if (Step.StepType.Pole == currentStep.getType()){
		if (player.motionY <= 0.01){
			if (pollFlag == 0)
				player.jump();
			pollFlag++;
		}
		if(pollFlag == 2)
		{
			Minecraft mc = Minecraft.getMinecraft();
			World world = mc.getIntegratedServer().worldServerForDimension(mc.thePlayer.dimension);

			ItemStack itemStack = player.getHeldItem();

			player.rotationPitch = 180;
			player.swingItem();

			int x = (int)Math.floor(player.posX);
			int y = (int)Math.floor(player.posY);
			int z = (int)Math.floor(player.posZ);
			ItemBlock blockItem = (ItemBlock)player.getHeldItem().getItem();
			Block block = blockItem.field_150939_a;

			world.playSoundEffect(x, y, z, block.stepSound.getBreakSound(), 1.0F, world.rand.nextFloat() * 0.1F + 0.9F);

			blockItem.placeBlockAt(itemStack, player, world, x, y - 2, z, EnumFacing.UP.ordinal(), 0, 0, 0, player.getHeldItem().getItemDamage());//world.getBlockMetadata(x+1, y-2, z)
			player.getHeldItem().splitStack(1);
			pollFlag++;
		}

		if (pollFlag >= 3 && player.onGround){
			pollFlag = 0;
			player.rotationPitch = 0;

			currentStep = steps.poll();
		}

		return;
	}

 

and this is the whole class, if required

 

package com.custommods.walkmod;

import java.util.Queue;

import net.minecraft.block.Block;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.Vec3;
import net.minecraft.world.World;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.TickEvent;

public class PathNavigator {

private static PathNavigator pathNavigator;

private final double NORMAL_SPEED_SIZE = 0.21585;
private final double WATER_SPEED_SIZE = NORMAL_SPEED_SIZE / 5;
private final double PLAYER_HEIGHT = 1.62;
private final double MINIMAL_DISTANCE = 0.15;
private final double TURNING_DISTANCE = 1;


private Queue<Step> steps;
private Step currentStep;
private boolean run = false;
private boolean isJumpedFlag = false;
private Vec3 targetVec = null;
private int pollFlag = 0;


public static PathNavigator getPathNavigator(){
	if (pathNavigator == null)
		pathNavigator = new PathNavigator();
	return pathNavigator;
}

private PathNavigator(){
	this.steps = null;
	this.currentStep = null;
}
@SubscribeEvent
public void onPlayerTick(TickEvent.PlayerTickEvent event) {
	if (!run)
		return;
	if (null == currentStep && steps.isEmpty()){
		run = false;
		return;
	}
	if (null == currentStep){
		currentStep = steps.poll();
		targetVec = currentStep.getLocation();
		isJumpedFlag = false;
	}


	EntityPlayer player = Minecraft.getMinecraft().thePlayer;

	//integer positions are the corner of the block - in order to get to the center we add 0.5
	Vec3 direction = 
			Vec3.createVectorHelper(targetVec.xCoord + 0.5 - player.posX,
					targetVec.yCoord - player.posY + PLAYER_HEIGHT,
					targetVec.zCoord + 0.5 - player.posZ);
	//System.out.println((targetVec.xCoord - player.posX) + ", " + (targetVec.yCoord - player.posY) + ", " + (targetVec.zCoord - player.posZ));

	direction.yCoord = 0;

//		Vec3 delta = null;
//		Step nextStep = steps.peek();
//		if(nextStep != null) {
//			Vec3 deltaBeforeNorm = nextStep.getLocation().subtract(targetVec);
//			delta = deltaBeforeNorm.normalize();
//			if(Vec3.createVectorHelper(
//					TARGET_ADVANCE_SPEED*delta.xCoord, 
//					TARGET_ADVANCE_SPEED*delta.yCoord, 
//					TARGET_ADVANCE_SPEED*delta.zCoord).lengthVector() > deltaBeforeNorm.lengthVector())
//				delta = null;
//		}


//		if (direction.lengthVector() <= MINIMAL_DISTANCE){
	if(direction.dotProduct(Vec3.createVectorHelper(player.motionX, 0, player.motionZ)) < 0 && Step.StepType.Pole != currentStep.getType()) {
		currentStep = steps.poll();
		if(null != currentStep)
			targetVec = currentStep.getLocation();
		//System.out.println((targetVec.xCoord - player.posX) + ", " + (targetVec.yCoord - player.posY) + ", " + (targetVec.zCoord - player.posZ));
		isJumpedFlag = false;
		player.setVelocity(0, 0, 0);
		return;
	} else if (Step.StepType.Pole == currentStep.getType()){
		if (player.motionY <= 0.01){
			if (pollFlag == 0)
				player.jump();
			pollFlag++;
		}
		if(pollFlag == 2)
		{
			Minecraft mc = Minecraft.getMinecraft();
			World world = mc.getIntegratedServer().worldServerForDimension(mc.thePlayer.dimension);

			ItemStack itemStack = player.getHeldItem();

			player.rotationPitch = 180;
			player.swingItem();

			int x = (int)Math.floor(player.posX);
			int y = (int)Math.floor(player.posY);
			int z = (int)Math.floor(player.posZ);
			ItemBlock blockItem = (ItemBlock)player.getHeldItem().getItem();
			Block block = blockItem.field_150939_a;

			world.playSoundEffect(x, y, z, block.stepSound.getBreakSound(), 1.0F, world.rand.nextFloat() * 0.1F + 0.9F);

			blockItem.placeBlockAt(itemStack, player, world, x, y - 2, z, EnumFacing.UP.ordinal(), 0, 0, 0, player.getHeldItem().getItemDamage());//world.getBlockMetadata(x+1, y-2, z)
			player.getHeldItem().splitStack(1);
			pollFlag++;
		}

		if (pollFlag >= 3 && player.onGround){
			pollFlag = 0;
			player.rotationPitch = 0;

			currentStep = steps.poll();
		}

		return;
	}




	//		else if (direction.lengthVector() < TURNING_DISTANCE) {
//			if(delta != null) {
//				targetVec = targetVec.addVector(
//						TARGET_ADVANCE_SPEED*delta.xCoord, 
//						TARGET_ADVANCE_SPEED*delta.yCoord, 
//						TARGET_ADVANCE_SPEED*delta.zCoord); 
//			}
//		}
	//Since we set yCoord to 0, it checks only if the length in x and z is samller than MINIMAL_DISTANCE, 
	// to make sure that the player won't "shake" during the fall


	direction = direction.normalize();
	double multiplyFactor = (player.isInWater()) ? WATER_SPEED_SIZE : NORMAL_SPEED_SIZE;
	direction = Vec3.createVectorHelper(
			direction.xCoord*multiplyFactor,
			direction.yCoord*multiplyFactor,
			direction.zCoord*multiplyFactor);
	player.setVelocity(direction.xCoord, player.motionY, direction.zCoord);
	player.rotationYaw = -(float)(Math.atan2(player.motionX, player.motionZ) * 360 / 2/ Math.PI);

	if(!isJumpedFlag && Step.StepType.Jump == currentStep.getType()  && (player.onGround || player.isInWater())){ 
		player.jump();
		isJumpedFlag = true;
	}

}
public void run(){
	run = true;
}
public void pause(){
	run = false;
}
public void stop(){
	steps = null;
}
public void setStepsQueue(Queue<Step> steps){
	this.steps = steps;

}

}

 

Thanks  :)

 

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.

×
×
  • Create New...

Important Information

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