Jump to content

Recommended Posts

Posted

Hi, I am trying to make an item spawn lightning, and it all works just fine. But where the lightning spawns it doesn't set anything on fire, neither does it deal damage to entities it hits.

What am I doing wrong? Before it didn't spawn the lightning but spawned fire now it is the opposite.

ย 

Here is the code:

public class ItemTalisman extends ItemPC {

public ItemStack onItemRightClick(ItemStack itemStack, World world, EntityPlayer player) {

	if (!world.isRemote) {

		return itemStack;
	}

	if (ExtendedPlayer.get(player).getCulture() >= 100) {

		ExtendedPlayer.get(player).consumeCulture(100);

		// player.addPotionEffect(new PotionEffect(Potion.moveSpeed.id, 2400, 0, false));

		MovingObjectPosition objectMouseOver;
		Minecraft mc = Minecraft.getMinecraft();

		objectMouseOver = mc.thePlayer.rayTrace(300, 1);

		if (objectMouseOver != null && objectMouseOver.typeOfHit == MovingObjectType.BLOCK) {

			int i = objectMouseOver.blockX;
			int j = objectMouseOver.blockY;
			int k = objectMouseOver.blockZ;
			world.spawnEntityInWorld(new EntityLightningBolt(world, i, j, k));
			player.addChatComponentMessage(new ChatComponentText("Pew! X: " + i + ", Y: " + j + ", Z: " + k));
		}

	} else {

		player.addChatComponentMessage(new ChatComponentText("You don't have enough culture."));
		ExtendedPlayer.get(player).setCulture(100);
	}

	return itemStack;
}
}

Posted

You have to spawn lightning on server and client. On the server because the damage, and the client to have the rendering :)

ย 

So, you just have to remove: if(!world.isRemote) { return itemstack }

I am fairly new to Java and modding, so my answers are not always 100% correct. Sorry for that!

Posted

Hmm...

@Override
public ItemStack onItemRightClick(ItemStack itemStack, World world, EntityPlayer Entityplayer) {

Vec3 look = Entityplayer.getLookVec();
MovingObjectPosition Coord = Entityplayer.rayTrace(300, 1);
if(Coord != null && Coord.typeOfHit == EnumMovingObjectType.TILE){
ย  ย  ย  ย   EntityLightningBolt Lightning = new EntityLightningBolt(world, 1, 1, 1);
ย  ย  ย  ย   Lightning.setPosition(Coord.blockX,Coord.blockY,Coord.blockZ);
ย  ย  ย  ย   world.spawnEntityInWorld(Lightning);
ย  }
return itemStack;

Posted

You have to spawn lightning on server and client. On the server because the damage, and the client to have the rendering :)

ย 

So, you just have to remove: if(!world.isRemote) { return itemstack }

Thanks that helped alot, I just had to change some stuff so that it doesn't print stuff when it shouldn't.

ย 

For people here is the code that worked:


public ItemStack onItemRightClick(ItemStack itemStack, World world, EntityPlayer player) {

ย 

if (ExtendedPlayer.get(player).getCulture() >= 100) {

ย 

ExtendedPlayer.get(player).consumeCulture(100);

ย 

// player.addPotionEffect(new PotionEffect(Potion.moveSpeed.id, 2400, 0, false));

ย 

MovingObjectPosition objectMouseOver;

Minecraft mc = Minecraft.getMinecraft();

ย 

objectMouseOver = mc.thePlayer.rayTrace(300, 1);

ย 

if (objectMouseOver != null && objectMouseOver.typeOfHit == MovingObjectType.BLOCK) {

ย 

int i = objectMouseOver.blockX;

int j = objectMouseOver.blockY;

int k = objectMouseOver.blockZ;

world.spawnEntityInWorld(new EntityLightningBolt(world, i, j, k));

if (world.isRemote) {

player.addChatComponentMessage(new ChatComponentText("Pew! X: " + i + ", Y: " + j + ", Z: " + k));

}

}

ย 

} else {

ย 

if (world.isRemote) {

player.addChatComponentMessage(new ChatComponentText("You don't have enough culture."));

}

ExtendedPlayer.get(player).setCulture(100);

}

ย 

return itemStack;

}

Posted

Hi

ย 

I reckon when you try to run this on a server there's a good chance it will crash.ย  In fact, in might occasionally crash on single player as well when you get unlucky (the client thread and the server thread try to both access Minecraft object at the same time)

ย 

ย  ย  ย  ย   Minecraft mc = Minecraft.getMinecraft();

ย 

This only exists on the client side and if you try to access it from the server side it will cause problems

ย 

MinecraftServer.getServer is what you need on the server side.

Which will leave you back with your "feet height vs eye height" problem from before.ย  You can correct for that by adding the difference on the server, 1.6 or something like that (use your println(ypos) from before to figure it out exactly...)

ย 

-TGG

ย 

Posted

Hi

ย 

I reckon when you try to run this on a server there's a good chance it will crash.ย  In fact, in might occasionally crash on single player as well when you get unlucky (the client thread and the server thread try to both access Minecraft object at the same time)

ย 

ย  ย  ย  ย   Minecraft mc = Minecraft.getMinecraft();

ย 

This only exists on the client side and if you try to access it from the server side it will cause problems

ย 

MinecraftServer.getServer is what you need on the server side.

Which will leave you back with your "feet height vs eye height" problem from before.ย  You can correct for that by adding the difference on the server, 1.6 or something like that (use your println(ypos) from before to figure it out exactly...)

ย 

-TGG

I found something that I think is a really bad fix. But at least it works:

ย 

	public ItemStack onItemRightClick(ItemStack itemStack, World world, EntityPlayer player) {

	if (ExtendedPlayer.get(player).getCulture() >= 100) {

		ExtendedPlayer.get(player).consumeCulture(100);

		player.addPotionEffect(new PotionEffect(Potion.moveSpeed.id, 2400, 0, false));

		MovingObjectPosition objectMouseOver;

		player.eyeHeight += 1.6F;

		objectMouseOver = player.rayTrace(300, 1);

		player.eyeHeight -= 1.6F;

		Vec3 vec3 = player.getPosition(1);
		System.out.println((world.isRemote ? "client y=" : "server y =") + vec3.yCoord);

		if (objectMouseOver != null && objectMouseOver.typeOfHit == MovingObjectType.BLOCK) {

			int i = objectMouseOver.blockX;
			int j = objectMouseOver.blockY;
			int k = objectMouseOver.blockZ;

			world.spawnEntityInWorld(new EntityLightningBolt(world, i, j, k));
		}

	} else {

		if (world.isRemote) {

			player.addChatComponentMessage(new ChatComponentText("You don't have enough culture."));
		}

		ExtendedPlayer.get(player).setCulture(100);
	}

	return itemStack;
}

ย 

This didn't seem to work that well...

Posted

Hi

ย 

I'd suggest using the code out of rayTrace instead of fiddling with the player object (and actually I don't think modifying player.eyeHeight will work, rayTrace doesn't seem to use it)

ย 

final double EYE_HEIGHT = 1.6;ย  // this is a guess... need to confirm it

ย  ย  ย  ย  Vec3 startPos = this.getPosition(partialTick);
if (!world.isRemote) startPos = startPos.addVector(0, EYE_HEIGHT, 0);
ย  ย  ย  ย  Vec3 look = this.getLook(partialTick);
ย  ย  ย  ย  Vec3 endPos = vec3.addVector(look.xCoord * reachDistance, look.yCoord * reachDistance, look.zCoord * reachDistance);
MovingObjectPosition mop = world.func_147447_a(startPos, endPos, false, false, true);

System.out.println("HitVec:[" + mop.hitVec.xCoord + ", " + mop.hitVec.yCoord + ", " + mop.hitVec.zCoord + "]");

ย 

-TGG

Posted

Hi

ย 

I'd suggest using the code out of rayTrace instead of fiddling with the player object (and actually I don't think modifying player.eyeHeight will work, rayTrace doesn't seem to use it)

ย 

final double EYE_HEIGHT = 1.6;ย  // this is a guess... need to confirm it

ย  ย  ย  ย  Vec3 startPos = this.getPosition(partialTick);
if (!world.isRemote) startPos = startPos.addVector(0, EYE_HEIGHT, 0);
ย  ย  ย  ย  Vec3 look = this.getLook(partialTick);
ย  ย  ย  ย  Vec3 endPos = vec3.addVector(look.xCoord * reachDistance, look.yCoord * reachDistance, look.zCoord * reachDistance);
MovingObjectPosition mop = world.func_147447_a(startPos, endPos, false, false, true);

System.out.println("HitVec:[" + mop.hitVec.xCoord + ", " + mop.hitVec.yCoord + ", " + mop.hitVec.zCoord + "]");

ย 

-TGG

Thanks alot, this is exactly what I need. The only problem with something like this (at least when I tried it out now) it gives me a total random y and z coordinate as well as it only change when moving around and not when I look around using the mouse.

ย 

Sorry to bother you this much, guess I went some over my limit here.

Posted

No worries :-)

ย 

I tried this code in 1.6.4 and it seems to work fine:

ย 

 @Override
ย  public ItemStack onItemRightClick(ItemStack itemStack, World world, EntityPlayer player) {

ย  ย  if (world.isRemote) {
ย  ย  ย  System.out.println("Client: player y= " + player.getPosition(1.0F).yCoord);
ย  ย  } else {
ย  ย  ย  System.out.println("Server: player y=" + player.getPosition(1.0F).yCoord);
ย  ย  }

ย  ย  final double EYE_HEIGHT = 1.62;ย  // this is a guess... need to confirm it
ย  ย  final double reachDistance = 300;
ย  ย  Vec3 startPos = player.getPosition(1.0F);
ย  ย  if (!world.isRemote) startPos = startPos.addVector(0, EYE_HEIGHT, 0);
ย  ย  Vec3 look = player.getLook(1.0F);
ย  ย  Vec3 endPos = startPos.addVector(look.xCoord * reachDistance, look.yCoord * reachDistance, look.zCoord * reachDistance);
ย  ย  MovingObjectPosition mop = world.clip(startPos, endPos);

ย  ย  System.out.println((world.isRemote ? "Client-" : "Server-") + " HitVec:[" + mop.hitVec.xCoord + ", " + mop.hitVec.yCoord + ", " + mop.hitVec.zCoord + "]");

ย  ย  if (mop != null && mop.typeOfHit == EnumMovingObjectType.TILE) {

ย  ย  ย  int i = mop.blockX;
ย  ย  ย  int j = mop.blockY;
ย  ย  ย  int k = mop.blockZ;
ย  ย  ย  System.out.println((world.isRemote ? "Client-" : "Server-") + "HitBlock:[" + i + ", " + j + ", " + k + "]");

ย  ย  ย  world.spawnEntityInWorld(new EntityLightningBolt(world, i, j, k));
ย  ย  }


ย  ย  return itemStack;
ย  }

ย 

Right-click gives the following output, i.e. client and server hitvec and hitblock match, and are equal to where my cursor is pointing

ย 

2014-03-10 10:22:10 [iNFO] [sTDOUT] Client: player y= 5.620000004768372
2014-03-10 10:22:10 [iNFO] [sTDOUT] Client- HitVec:[-809.4795666515165, 4.0, -1480.5625485323928]
2014-03-10 10:22:10 [iNFO] [sTDOUT] Client-HitBlock:[-810, 3, -1481]
2014-03-10 10:22:10 [iNFO] [sTDOUT] Server: player y=4.0
2014-03-10 10:22:10 [iNFO] [sTDOUT] Server- HitVec:[-809.4795666515766, 4.0, -1480.5625485380956]
2014-03-10 10:22:10 [iNFO] [sTDOUT] Server-HitBlock:[-810, 3, -1481]

ย 

If I move my mouse to move the look to the next block along in the x direction, it gives the expected result:

2014-03-10 10:23:17 [iNFO] [sTDOUT] Client: player y= 5.620000004768372
2014-03-10 10:23:17 [iNFO] [sTDOUT] Client- HitVec:[-810.474262374682, 4.0, -1480.5878117751354]
2014-03-10 10:23:17 [iNFO] [sTDOUT] Client-HitBlock:[-811, 3, -1481]
2014-03-10 10:23:17 [iNFO] [sTDOUT] Server: player y=4.0
2014-03-10 10:23:17 [iNFO] [sTDOUT] Server- HitVec:[-810.4742623718143, 4.0, -1480.587811780764]
2014-03-10 10:23:17 [iNFO] [sTDOUT] Server-HitBlock:[-811, 3, -1481]

ย 

-TGG

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.