Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Destroy / Remove blocks (10x10x10) when the block is clicked with an item.

Featured Replies

  • Author

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);
    }
}

 

  • Replies 57
  • Views 40.2k
  • Created
  • Last Reply

Top Posters In This Topic

Most Popular Posts

  • i think you forgot the !, since removing Blocks is always done on server

  • you removing 10*10*10 Blocks each click, this are 1000 Blocks i think this is a performance issue, you can use a BlockPos.MutableBlockPos instead of evertime a new BlockPos Do you know basic ja

  • create before the loops a new BlockPos.MutableBlockPos, and before you remove the Block you move the BlockPos.MutableBlockPos via BlockPos.MutableBlockPos#move to it's new Position i would re

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?

  • Author

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?

 

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.

  • Author

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?

  • Author

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?

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 by Luis_ST

  • Author

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.

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

  • Author

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. 

  • Author

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?

The teleport command used ServerPlayer#connection and ServerGamePacketListenerImpl#teleport,
but i also remember that Player#teleportToWithTicket should work

  • Author

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 :)

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.

  • Author

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);
}
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

  • Author

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?

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

  • Author

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);
    }
}

 

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?

  • Author

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.

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.

  • Author

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 by Gepardius

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);
	}

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.