Gepardius Posted July 26, 2022 Author Posted July 26, 2022 Of course, here you go: import net.minecraft.client.Minecraft; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.nbt.CompoundTag; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; import net.minecraft.world.InteractionResultHolder; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.context.UseOnContext; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.TargetBlock; import net.minecraft.world.phys.HitResult; import net.minecraft.world.phys.Vec3; import net.minecraftforge.event.entity.ProjectileImpactEvent; import static net.minecraftforge.client.gui.ForgeIngameGui.rayTraceDistance; public class teleportItem extends Item { public teleportItem(Properties pProperties) { super(pProperties); } @Override public InteractionResultHolder<ItemStack> use(Level pLevel, Player pPlayer, InteractionHand pUsedHand) { // entity.pick(rayTraceDistance, 0.0F, false); Entity entity = Minecraft.getInstance().getCameraEntity(); rayTraceDistance = 500.0D; HitResult viewedBlock = entity.pick(rayTraceDistance, 0.0F, false); Double x = viewedBlock.getLocation().x; Double y = viewedBlock.getLocation().y; Double z = viewedBlock.getLocation().z; y += 1; if (!pLevel.isClientSide){ pPlayer.teleportTo(x, y, z); } pPlayer.fallDistance = 0.0f; rayTraceDistance = 20.0D; return super.use(pLevel, pPlayer, pUsedHand); } } Quote
Luis_ST Posted July 26, 2022 Posted July 26, 2022 This does not work you need in this case server raytrace, you can take a look at BucketItem#use for an example. why did you use ForgeIngameGui.rayTraceDistance this makes for me no sense, do you know what it is used for? Quote
Gepardius Posted July 26, 2022 Author Posted July 26, 2022 It gets me the x, y, z of my crosshair position. Initial value of the rayTraceDistance is 20.0D, therefore you can only get x, y, z in that specter, however once I increase it, I can get positions from much further. I used it from here: https://github.com/MinecraftForge/MinecraftForge/blob/bb4818630c0c6cdd7c4178b4d77dc406153c2bcb/src/main/java/net/minecraftforge/client/gui/overlay/ForgeGui.java#L680 So you reckon I have to change the way I get x, y and z? Quote
Luis_ST Posted July 26, 2022 Posted July 26, 2022 56 minutes ago, Gepardius said: It gets me the x, y, z of my crosshair position. Initial value of the rayTraceDistance is 20.0D, therefore you can only get x, y, z in that specter, however once I increase it, I can get positions from much further. I used it from here: ForgeIngameGui.rayTraceDistance is just a double value there is nothing special with it 57 minutes ago, Gepardius said: So you reckon I have to change the way I get x, y and z? yes: 1 hour ago, Luis_ST said: you need in this case server raytrace, you can take a look at BucketItem#use for an example. Quote
Gepardius Posted July 26, 2022 Author Posted July 26, 2022 If I use this one: HitResult viewedBlock = getPlayerPOVHitResult(pLevel, pPlayer, ClipContext.Fluid.NONE); I can only teleport around 5-10 blocks. Is there another server side method to get rayTrace? Quote
Gepardius Posted July 26, 2022 Author Posted July 26, 2022 Btw. If I use pPlayer instead of assigning Entity through Minecraf.getInstance() as below: @Override public InteractionResultHolder<ItemStack> use(Level pLevel, Player pPlayer, InteractionHand pUsedHand) { // entity.pick(rayTraceDistance, 0.0F, false); // Entity entity = Minecraft.getInstance().getCameraEntity(); rayTraceDistance = 500.0D; HitResult viewedBlock = pPlayer.pick(rayTraceDistance, 0.0F, false); Double x = viewedBlock.getLocation().x; Double y = viewedBlock.getLocation().y; Double z = viewedBlock.getLocation().z; y += 1; if (!pLevel.isClientSide){ pPlayer.teleportTo(x, y, z); } pPlayer.fallDistance = 0.0f; rayTraceDistance = 20.0D; return super.use(pLevel, pPlayer, pUsedHand); } Then it works as before and it solves it right? Quote
Luis_ST Posted July 26, 2022 Posted July 26, 2022 (edited) 12 minutes ago, Gepardius said: Then it works as before and it solves it right? i'm not sure as far as I can see in the code, yes test it on a server via runServer 12 minutes ago, Gepardius said: I can only teleport around 5-10 blocks. Is there another server side method to get rayTrace? it use the reach distance of the player which is 4.5 by default Edited July 26, 2022 by Luis_ST Quote
Gepardius Posted July 26, 2022 Author Posted July 26, 2022 The server starts without interruptions, but I'd have to upload the mod into the run folder in order to real-life test it right, which will take some time? For the moment I'll just have to believe it will work on the server as well. Quote
Luis_ST Posted July 26, 2022 Posted July 26, 2022 20 minutes ago, Gepardius said: but I'd have to upload the mod into the run folder in order to real-life test it right, which will take some time? No you have to run the runServer gradle task Note the first time you run the server it stops, since you need to accept the eula, the second time it generate all missing files but you need to stop the server, since you need to disable the online mode in the server.properties file Quote
Gepardius Posted July 26, 2022 Author Posted July 26, 2022 Hmmm... I've accepted the eula, changed online mode to false and now I only have Minecraft server window open. How do I connect to it? I tried through the client but I must be doing something wrong. Quote
Luis_ST Posted July 26, 2022 Posted July 26, 2022 Run a client (via runClient) join the server with ip localhost and port 25565 1 Quote
Gepardius Posted July 27, 2022 Author Posted July 27, 2022 Aha. It worked when I connected just with: localhost. Everything works normally, except the teleporting. I guess it is not possible to teleport player on the server with: player.setPos(x, y, z); // or player.teleportTo(x, y, z); Is there another server teleport method? Quote
Luis_ST Posted July 27, 2022 Posted July 27, 2022 The teleport command used ServerPlayer#connection and ServerGamePacketListenerImpl#teleport, but i also remember that Player#teleportToWithTicket should work 1 Quote
Gepardius Posted July 27, 2022 Author Posted July 27, 2022 I tried with Player#teleportToWithTicket, unfortunately it does not work: if (!level.isClientSide) { player.teleportToWithTicket(x, y, z); } But I can't figure out how to try the ServerPlayer#connection and ServerGamePacketListenerImpl#teleport yet, so I'll have to get back to you once I do Quote
Luis_ST Posted July 27, 2022 Posted July 27, 2022 29 minutes ago, Gepardius said: But I can't figure out how to try the ServerPlayer#connection and ServerGamePacketListenerImpl#teleport yet, so I'll have to get back to you once I do i guess your variable "player" is a Player, then you need an instance of check if the Player is an ServerPlayer. Then you can cast your Player to a ServerPlayer. Btw this is basic java. 1 Quote
Gepardius Posted July 27, 2022 Author Posted July 27, 2022 I am checking the instance now, but I don't know what to input as the last two elements. if (player instanceof ServerPlayer){ ((ServerPlayer) player).connection.teleport(x, y, z, 0, 0); } Quote
Luis_ST Posted July 27, 2022 Posted July 27, 2022 1 hour ago, Gepardius said: I am checking the instance now, but I don't know what to input as the last two elements. the first one is the y rotation of the player head and the second one is the x rotation of the player head, you can use 0 if you don't want a specific head look direction 1 Quote
Gepardius Posted July 27, 2022 Author Posted July 27, 2022 Very basic question again... I am not applying changes to the server, when using the InteractionResultHolder method: @Override public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand pUsedHand) { rayTraceDistance = 500.D; HitResult viewedBlock = player.pick(rayTraceDistance, 0.0F, false); Double x = viewedBlock.getLocation().x; Double y = viewedBlock.getLocation().y; Double z = viewedBlock.getLocation().z; y += 1; if (player instanceof ServerPlayer){ System.out.println("nothing happens"); // player.teleportToWithTicket(x, y, z); } if (!level.isClientSide) { System.out.println("nothing happens"); ((ServerPlayer) player).connection.teleport(x, y, z, 1, 1); // player.teleportToWithTicket(x, y, z); } rayTraceDistance = 20.0D; return super.use(level, player, pUsedHand); } In theory shouldn't both of these ifs be triggered from the server? Quote
Luis_ST Posted July 27, 2022 Posted July 27, 2022 18 minutes ago, Gepardius said: In theory shouldn't both of these ifs be triggered from the server? yes, does it not work? 18 minutes ago, Gepardius said: rayTraceDistance = 500.D; why did you still use this, you can pass the value (500) directly into Player#pick Quote
Gepardius Posted July 27, 2022 Author Posted July 27, 2022 On the client yes, on the server no. Have you got any idea why? rayTraceDistance = 500.D; I know but later on I might change it through custom leveling. Full code: import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResultHolder; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.Level; import net.minecraft.world.phys.HitResult; import static net.minecraftforge.client.gui.ForgeIngameGui.rayTraceDistance; public class teleportItem extends Item { public teleportItem(Properties pProperties) { super(pProperties); } @Override public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand pUsedHand) { rayTraceDistance = 500.D; HitResult viewedBlock = player.pick(rayTraceDistance, 0.0F, false); Double x = viewedBlock.getLocation().x; Double y = viewedBlock.getLocation().y; Double z = viewedBlock.getLocation().z; y += 1; if (player instanceof ServerPlayer){ System.out.println("nothing happens"); // player.teleportToWithTicket(x, y, z); } if (!level.isClientSide) { System.out.println("nothing happens"); ((ServerPlayer) player).connection.teleport(x, y, z, 1, 1); // player.teleportToWithTicket(x, y, z); } rayTraceDistance = 20.0D; return super.use(level, player, pUsedHand); } } Quote
Luis_ST Posted July 27, 2022 Posted July 27, 2022 2 minutes ago, Gepardius said: On the client yes, on the server no. Have you got any idea why? Did you mean Singleplayer with "client"? 1 minute ago, Gepardius said: I know but later on I might change it through custom leveling. Could you please explain that in more detail? Quote
Gepardius Posted July 27, 2022 Author Posted July 27, 2022 Yes singleplayer with client. No multiplayer through client (localhost). This one "I know but later on I might change it through custom leveling." is not important for now. Later on I want to implement custom levels, which will increase/decrease this double figure. Quote
Luis_ST Posted July 27, 2022 Posted July 27, 2022 1 minute ago, Gepardius said: Yes singleplayer with client. No multiplayer through client (localhost). Then rayTraceDistance is your issue it's in a class which should be only used on client, remove it since you can not store data like this in a Item, the value will be shared. If you want to store data in a Item you need to store the data in the related ItemStack. Therefore i would recommend you to use a Capability. Quote
Gepardius Posted July 27, 2022 Author Posted July 27, 2022 (edited) Nope still nothing happens the ifs are never accessed: if (player instanceof ServerPlayer){ System.out.println("nothing happens"); // player.teleportToWithTicket(x, y, z); } if (!level.isClientSide) { System.out.println("nothing happens"); ((ServerPlayer) player).connection.teleport(x, y, z, 1, 1); // player.teleportToWithTicket(x, y, z); } I thank you for your effort and time. I'll be gone for a few days now, so I won't be able to reply. I appreciate your help! Edited July 27, 2022 by Gepardius Quote
Luis_ST Posted July 27, 2022 Posted July 27, 2022 I'm not be able to reproduce your problem, this code works fine in Singleplayer and on Server. The Logger debug statements are correctly printed on console Please post the full class of your Item. @Override public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) { if (player instanceof ServerPlayer serverPlayer) { LogUtils.getLogger().debug("ServerPlayer"); serverPlayer.connection.teleport(serverPlayer.getX(), 100, serverPlayer.getZ(), 0.0F, 0.0F); } if (!level.isClientSide) { LogUtils.getLogger().debug("!isClientSide"); } return super.use(level, player, hand); } 1 Quote
Recommended Posts
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.