Jump to content

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


Gepardius

Recommended Posts

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

 

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

  • Like 1
Link to comment
Share on other sites

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

  • Like 1
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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);
	}
  • Like 1
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.




  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Description of issue: Tried to run MC client, crashing with java.lang.NoSuchFieldError. I Went into SRG fields, found that f_271198 does exist, but Forge thinks this field does not exist.   Crash Report: ---- Minecraft Crash Report ---- // I let you down. Sorry Time: 2024-02-22 09:21:29 Description: Mod loading error has occurred java.lang.Exception: Mod Loading has failed     at net.minecraftforge.logging.CrashReportExtender.dumpModLoadingCrashReport(CrashReportExtender.java:48) ~[forge-1.20.4-49.0.30_mapped_official_1.20.4.jar:?] {re:classloading}     at net.minecraftforge.client.loading.ClientModLoader.completeModLoading(ClientModLoader.java:119) ~[forge-1.20.4-49.0.30_mapped_official_1.20.4.jar:?] {re:classloading,pl:runtimedistcleaner:A}     at net.minecraft.client.Minecraft.lambda$new$6(Minecraft.java:611) ~[forge-1.20.4-49.0.30_mapped_official_1.20.4.jar:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}     at net.minecraft.Util.ifElse(Util.java:432) ~[forge-1.20.4-49.0.30_mapped_official_1.20.4.jar:?] {re:classloading}     at net.minecraft.client.Minecraft.lambda$new$7(Minecraft.java:602) ~[forge-1.20.4-49.0.30_mapped_official_1.20.4.jar:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}     at net.minecraftforge.client.loading.ForgeLoadingOverlay.render(ForgeLoadingOverlay.java:146) ~[forge-1.20.4-49.0.30_mapped_official_1.20.4.jar:?] {re:classloading}     at net.minecraft.client.renderer.GameRenderer.render(GameRenderer.java:931) ~[forge-1.20.4-49.0.30_mapped_official_1.20.4.jar:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}     at net.minecraft.client.Minecraft.runTick(Minecraft.java:1250) ~[forge-1.20.4-49.0.30_mapped_official_1.20.4.jar:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}     at net.minecraft.client.Minecraft.run(Minecraft.java:801) ~[forge-1.20.4-49.0.30_mapped_official_1.20.4.jar:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}     at net.minecraft.client.main.Main.main(Main.java:234) ~[forge-1.20.4-49.0.30_mapped_official_1.20.4.jar:?] {re:classloading,pl:runtimedistcleaner:A}     at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?] {}     at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[?:?] {}     at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?] {}     at java.lang.reflect.Method.invoke(Method.java:568) ~[?:?] {}     at net.minecraftforge.fml.loading.targets.CommonLaunchHandler.runTarget(CommonLaunchHandler.java:91) ~[fmlloader-1.20.4-49.0.30.jar:?] {}     at net.minecraftforge.fml.loading.targets.CommonLaunchHandler.lambda$makeService$0(CommonLaunchHandler.java:75) ~[fmlloader-1.20.4-49.0.30.jar:?] {}     at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:54) ~[modlauncher-10.1.2.jar:?] {}     at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:74) ~[modlauncher-10.1.2.jar:?] {}     at cpw.mods.modlauncher.Launcher.run(Launcher.java:114) ~[modlauncher-10.1.2.jar:?] {}     at cpw.mods.modlauncher.Launcher.main(Launcher.java:73) ~[modlauncher-10.1.2.jar:?] {}     at cpw.mods.modlauncher.BootstrapEntry.main(BootstrapEntry.java:17) ~[modlauncher-10.1.2.jar:?] {}     at net.minecraftforge.bootstrap.Bootstrap.moduleMain(Bootstrap.java:97) ~[bootstrap-2.0.0.jar!/:?] {}     at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?] {}     at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[?:?] {}     at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?] {}     at java.lang.reflect.Method.invoke(Method.java:568) ~[?:?] {}     at net.minecraftforge.bootstrap.Bootstrap.start(Bootstrap.java:48) ~[bootstrap-2.0.0.jar!/:?] {}     at net.minecraftforge.bootstrap.ForgeBootstrap.main(ForgeBootstrap.java:18) ~[bootstrap-2.0.0.jar!/:?] {} A detailed walkthrough of the error, its code path and all known details is as follows: --------------------------------------------------------------------------------------- -- Head -- Thread: Render thread Suspected Mod:      Better Biomes (betterbiomes), Version: 0.1.4.3         at TRANSFORMER/betterbiomes@0.1.4.3/net.crypticverse.betterbiomes.util.BetterBiomeWoodTypes.<clinit>(BetterBiomeWoodTypes.java:8) Stacktrace:     at net.crypticverse.betterbiomes.util.BetterBiomeWoodTypes.<clinit>(BetterBiomeWoodTypes.java:8) ~[betterbiomes-forge-2.1.4.6-1.20.4.jar!/:2.1.4.6-1.20.4] {re:classloading} -- MOD betterbiomes -- Details:     Mod File: /C:/Users/myUser/BetterBiomes-Forge/run/mods/betterbiomes-forge-2.1.4.6-1.20.4.jar     Failure message: Better Biomes (betterbiomes) has failed to load correctly         java.lang.NoSuchFieldError: f_271198_     Mod Version: 0.1.4.3     Mod Issue URL: NOT PROVIDED     Exception message: java.lang.NoSuchFieldError: f_271198_ Stacktrace:     at net.crypticverse.betterbiomes.util.BetterBiomeWoodTypes.<clinit>(BetterBiomeWoodTypes.java:8) ~[main/:?] {re:classloading}     at net.crypticverse.betterbiomes.BetterBiomes.<init>(BetterBiomes.java:55) ~[main/:?] {re:classloading}     at jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:?] {}     at jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77) ~[?:?] {}     at jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[?:?] {}     at java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499) ~[?:?] {}     at java.lang.reflect.Constructor.newInstance(Constructor.java:480) ~[?:?] {}     at net.minecraftforge.fml.javafmlmod.FMLModContainer.constructMod(FMLModContainer.java:66) ~[javafmllanguage-1.20.4-49.0.30.jar!/:49.0.30] {}     at net.minecraftforge.fml.ModContainer.lambda$buildTransitionHandler$5(ModContainer.java:125) ~[fmlcore-1.20.4-49.0.30.jar!/:1.0] {}     at java.util.concurrent.CompletableFuture$AsyncRun.run(CompletableFuture.java:1804) ~[?:?] {}     at java.util.concurrent.CompletableFuture$AsyncRun.exec(CompletableFuture.java:1796) ~[?:?] {}     at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:373) ~[?:?] {}     at java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1182) ~[?:?] {}     at java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1655) ~[?:?] {}     at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1622) ~[?:?] {}     at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:165) ~[?:?] {} -- System Details -- Details:     Minecraft Version: 1.20.4     Minecraft Version ID: 1.20.4     Operating System: Windows 10 (amd64) version 10.0     Java Version: 17.0.9, Eclipse Adoptium     Java VM Version: OpenJDK 64-Bit Server VM (mixed mode, sharing), Eclipse Adoptium     Memory: 578548144 bytes (551 MiB) / 1128267776 bytes (1076 MiB) up to 8522825728 bytes (8128 MiB)     CPUs: 20     Processor Vendor: GenuineIntel     Processor Name: 12th Gen Intel(R) Core(TM) i7-12700K     Identifier: Intel64 Family 6 Model 151 Stepping 2     Microarchitecture: Alder Lake     Frequency (GHz): 3.61     Number of physical packages: 1     Number of physical CPUs: 12     Number of logical CPUs: 20     Graphics card #0 name: NVIDIA GeForce RTX 3070 Ti     Graphics card #0 vendor: NVIDIA (0x10de)     Graphics card #0 VRAM (MB): 4095.00     Graphics card #0 deviceId: 0x2482     Graphics card #0 versionInfo: DriverVersion=31.0.15.3699     Memory slot #0 capacity (MB): 16384.00     Memory slot #0 clockSpeed (GHz): 4.80     Memory slot #0 type: Unknown     Memory slot #1 capacity (MB): 16384.00     Memory slot #1 clockSpeed (GHz): 4.80     Memory slot #1 type: Unknown     Virtual memory max (MB): 37373.42     Virtual memory used (MB): 22391.79     Swap memory total (MB): 4864.00     Swap memory used (MB): 95.69     JVM Flags: 1 total; -XX:HeapDumpPath=MojangTricksIntelDriversForPerformance_javaw.exe_minecraft.exe.heapdump     ModLauncher: 10.1.2     ModLauncher launch target: forge_userdev_client     ModLauncher naming: mcp     ModLauncher services:          / slf4jfixer PLUGINSERVICE          / runtimedistcleaner PLUGINSERVICE          / runtime_enum_extender PLUGINSERVICE          / object_holder_definalize PLUGINSERVICE          / capability_token_subclass PLUGINSERVICE          / accesstransformer PLUGINSERVICE          / eventbus PLUGINSERVICE          / mixin PLUGINSERVICE          / fml TRANSFORMATIONSERVICE          / mixin TRANSFORMATIONSERVICE      FML Language Providers:          lowcodefml@49         minecraft@1.0         javafml@49.0.30     Mod List:                                                            |Minecraft                     |minecraft                     |1.20.4              |COMMON_SET|Manifest: NOSIGNATURE         TerraBlender-forge-1.20.4-3.3.0.12_mapped_official|TerraBlender                  |terrablender                  |3.3.0.12            |COMMON_SET|Manifest: NOSIGNATURE                                                           |Forge                         |forge                         |49.0.30             |COMMON_SET|Manifest: NOSIGNATURE         betterbiomes-forge-2.1.4.6-1.20.4.jar             |Better Biomes                 |betterbiomes                  |0.1.4.3             |ERROR     |Manifest: NOSIGNATURE         Notebook-api-forge-1.20.4-1.1.0.4_mapped_official_|Notebook                      |notebook                      |1.1.0.4             |COMMON_SET|Manifest: NOSIGNATURE     Crash Report UUID: ad19c004-038a-4777-adbc-0504acf15d93     FML: 0.0     Forge: net.minecraftforge:49.0.30
    • Some kind of issue/conflict with eureka - also try other builds of it
    • doesn't work But after trying to make a boat with Valkyrien Skies, I got this : https://pastebin.com/bxnDcHr3 maybe the problem comes from valkyrien skies itself ?
    • yeah, I was about to try other builds for Valkyrien Skies but i'm scared of breaking everything again
  • Topics

×
×
  • Create New...

Important Information

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