Teleport event

Discussion in 'Plugin Development' started by _Shifted, Jul 21, 2020.

Thread Status:
Not open for further replies.
  1. Offline

    _Shifted

    Hi everyone.
    I just started to learn programming and I'm having problems with an event where I want to teleport the player upwards if he stands on a specific block.

    Heres what my code looks like.
    Thanks in advance.

    Code:
    public class PlayerInteractionEvent implements Listener{
        @EventHandler
        public static void PlayerLaunch(PlayerMoveEvent event) {
            Player player = event.getPlayer();
            int x = player.getLocation().getBlockX();
            int y = player.getLocation().getBlockY();
            int z = player.getLocation().getBlockZ();
    
            Material block = player.getWorld().getBlockAt(x, y-1, z).getType();
            if (block == Material.STONE) {
                Location location;
                location = null;
                location.setX(x);
                location.setY(y+100);
                location.setZ(z);
                player.teleport(location);
                player.sendMessage("TEST");
            }
        }
    }
     
  2. Offline

    KarimAKL

    @_Shifted
    1. The method shouldn't be static.
    2. Did you register the listener?
    3. You're using player#getLocation() instead of event#getTo().
    4. You can check the block below the location using Location#getBlock()#getRelative(BlockFace#DOWN).
    5. You need to teleport the player to a new location, which you haven't created yet. You're simply declaring it, then setting it to null (which it already is, and which will cause an error).
     
  3. Offline

    _Shifted

    Thanks for your response.
    1. I removed the static from the method.
    2. My main class has
    Code:
    getServer().getPluginManager().registerEvents(new PlayerInteractionEvent(), this);
    3. Is it right if I use
    Code:
    int x = event.getTo().getBlockX();
    int y = event.getTo().getBlockY();
    int z = event.getTo().getBlockZ();
    4. I did that but what do I need to put in the "if" statement? because if (block == Material.STONE) {} doesn't work as it demands a block and not a Material.
    I haven't yet looked into your 5th point.

    main.java:
    Code:
    package com.marcandrifuchs.plugin;
    
    import com.marcandrifuchs.plugin.event.MobSpawnEvent;
    import com.marcandrifuchs.plugin.event.PlayerInteractionEvent;
    import org.bukkit.ChatColor;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class main extends JavaPlugin {
    
        @Override
        public void onEnable() {
    
            getServer().getPluginManager().registerEvents(new MobSpawnEvent(), this);
            getServer().getPluginManager().registerEvents(new PlayerInteractionEvent(), this);
    
    
    
            getServer().getConsoleSender().sendMessage(ChatColor.AQUA +"harderminecraft : loaded!");
        }
    
        @Override
        public void onDisable() {
    
    
            getServer().getConsoleSender().sendMessage(ChatColor.YELLOW +"harderminecraft : unloaded!");
        }
    }
    
    PlayerInteractionEvent.java:
    Code:
    package com.marcandrifuchs.plugin.event;
    
    import org.bukkit.Location;
    import org.bukkit.block.Block;
    import org.bukkit.block.BlockFace;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.player.PlayerItemConsumeEvent;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    public class PlayerInteractionEvent implements Listener{
        @EventHandler
        public boolean PlayerLaunch(PlayerMoveEvent event) {
            Player player = event.getPlayer();
            int x = event.getTo().getBlockX();
            int y = event.getTo().getBlockY();
            int z = event.getTo().getBlockZ();
    
    
            Location location;
            location = null;
            location.setX(x);
            location.setY(y);
            location.setZ(z);
            Block block = location.getBlock().getRelative(BlockFace.DOWN);
            if (Part 4.) {
    
                Location teleportLocation = null;
                teleportLocation.setX(x);
                teleportLocation.setY(y+100);
                teleportLocation.setZ(z);
                player.teleport(teleportLocation);
                return true;
            }
    
    
            return false;
        }
    
        @EventHandler
        public static void PlayerClear(PlayerItemConsumeEvent event) {
            Player player = event.getPlayer();
            if (event.getItem().getType().name().toLowerCase().contains("potion")) {
                return;
            }
            if (event.getItem().getType().name().toLowerCase().contains("bucket")) {
                return;
            }
            if (event.getItem().getType().name().toLowerCase().contains("beef")) {
                player.addPotionEffect(new PotionEffect(PotionEffectType.POISON, 200, 1));
                player.sendMessage("§cYou're vegan!");
            }
    
            if (event.getItem().getType().name().toLowerCase().contains("porkchop")) {
                player.addPotionEffect(new PotionEffect(PotionEffectType.POISON, 200, 1));
                player.sendMessage("§cYou're vegan!");
            }
            if (event.getItem().getType().name().toLowerCase().contains("cod")) {
                player.addPotionEffect(new PotionEffect(PotionEffectType.POISON, 200, 1));
                player.sendMessage("§cYou're vegan!");
            }
            if (event.getItem().getType().name().toLowerCase().contains("salmon")) {
                player.addPotionEffect(new PotionEffect(PotionEffectType.POISON, 200, 1));
                player.sendMessage("§cYou're vegan!");
            }
            if (event.getItem().getType().name().toLowerCase().contains("chicken")) {
                player.addPotionEffect(new PotionEffect(PotionEffectType.POISON, 200, 1));
                player.sendMessage("§cYou're vegan!");
            }
            if (event.getItem().getType().name().toLowerCase().contains("rabbit")) {
                player.addPotionEffect(new PotionEffect(PotionEffectType.POISON, 200, 1));
                player.sendMessage("§cYou're vegan!");
            }
            if (event.getItem().getType().name().toLowerCase().contains("mutton")) {
                player.addPotionEffect(new PotionEffect(PotionEffectType.POISON, 200, 1));
                player.sendMessage("§cYou're vegan!");
            }
            if (event.getItem().getType().name().toLowerCase().contains("golden_apple")) {
                player.getInventory().getItemInMainHand().setAmount(0);
                player.setHealth(1);
                player.setSaturation(0);
    
            } else {
                double health = player.getHealth();
                player.setHealth(health / 2);
            }
    
    
        }
    }
    
     
  4. Offline

    timtower Administrator Administrator Moderator

  5. Offline

    KarimAKL

    @_Shifted Your PlayerClear method is also static. Event methods should never be static. You should probably try avoiding static.

    As @timtower said, you have to use getType() on the block to get the Material, just like you did before.

    Do you have any problems with the 5th point? If so, just ask.
     
Thread Status:
Not open for further replies.

Share This Page