Jump to content

Gepardius

Members
  • Posts

    34
  • Joined

  • Last visited

Posts posted by Gepardius

  1. Another topic that I have: 

    If I want to create skill points, example teleport. I've created a global static variable inside the ability class:

    public class Ability {
    
        public static int teleport;

    and then I increase the variable with:

    Ability.teleport += 1;

    On the server the teleport variable is stored as long as the server is running, once I reset the server the variable is reset and null again. Which in theory should not be an issue since the server is always running. And on the client it is the same, as long as I don't shut down the client the variable is stored and I can enter/exit as much as I like and don't lose the value of the variable. However, how would I store the value of a variable inside the world save?

  2. So the update: Your code works on both client and server. The issue before was that I kept the server running, made changes in the editor and only restarted the client. Then I expected the same code to magically run on the server as well... Very dumb mistake on my part.

    @Override
        public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) {
    
            Double distance = 500.0D;
    
            if (player instanceof ServerPlayer serverPlayer) {
    
                HitResult viewedBlock = player.pick(distance, 0.0F, false);
                Double x = viewedBlock.getLocation().x;
                Double y = viewedBlock.getLocation().y;
                Double z = viewedBlock.getLocation().z;
                y += 1;
                
                player.teleportToWithTicket(x, y, z);
                // serverPlayer.connection.teleport(x, y, z, 0.0F, 0.0F);
            }
    
            return super.use(level, player, hand);
        }

    So I ended up using the .teleportToWithTicket as Luis_ST initially suggested, so I don't have to adapt the head positioning.

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

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

     

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

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

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

     

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

     

  9. I am having issues with setting effects to the entities, am I doing something wrong? I am trying to freeze the movement of the given entity.

     

    List<Entity> ent = level.getEntities(player, minMax);
    for (Entity entko : ent) {
      entko.setTicksFrozen(100);
      entko.hurt(DamageSource.IN_WALL, entityDamage/2);
    }

    .setTicksFrozen(int) should freeze the entity right?

    .hurt works as it should, therefore why wouldn't other methods work on the same entity?

  10. For anyone else with the same question. This gets me the block that is in the Player's crosshair. I'm increasing the rayTraceDistance, which is 20.0D by default, so the Player is able to teleport farther. 

    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;
    
    rayTraceDistance = 20.0D;
×
×
  • Create New...

Important Information

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