Posted March 8, 201411 yr 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; } }
March 8, 201411 yr Author I'm not talking about the item taking damage, but that the lightning, when it strikes doesn't hurt entities at all, neither set things on fire.
March 8, 201411 yr 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!
March 8, 201411 yr 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;
March 9, 201411 yr Author 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; }
March 9, 201411 yr 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
March 9, 201411 yr Author 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...
March 9, 201411 yr 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
March 9, 201411 yr Author 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.
March 9, 201411 yr 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.