A little stuck logic wise

Discussion in 'Plugin Development' started by johnny_boy, Feb 9, 2018.

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

    johnny_boy

    Okay so my logic here is that I need to save a players coordinates so that I can teleport them back to their coordinates if they fall into the void. That's simple... kind of. I was thinking about a list consisting of all the players and coordinates, like so:

    Code:
    players:
      - John 0 0 60
      - Bob 623 9 74
      - etc, etc. 4 2 0
    
    Here is my code so far, but I'm honestly so stuck on this.. (I've tried for multiple hours trying to do this).

    Code:
    package me.memeplex.novoid;
    
    import java.util.List;
    
    import org.bukkit.Location;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageEvent;
    import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
    import org.bukkit.event.player.PlayerMoveEvent;
    
    public class PlayerListener implements Listener {
      
        Main plugin;
      
        public PlayerListener(Main instance) {
            plugin = instance;
    
        }
      
    
    
      
      
    
        @EventHandler
        public void playerFallInVoid(EntityDamageEvent e) {
          
            if (e.getEntity() instanceof Player) {
                Player pl = (Player) e.getEntity();
              
                if (e.getCause().equals(DamageCause.VOID)) {
                    e.setCancelled(true);
    
                }
            }   
        }
      
        @EventHandler
        public void playerMoveEvent(PlayerMoveEvent e) {
            Player pl = e.getPlayer();
          
            Location playerLoc = pl.getLocation();
          
            int x = playerLoc.getBlockX();
            int y = playerLoc.getBlockY() - 1;
            int z = playerLoc.getBlockZ();
          
            Location blockUnderPlayer = new Location(pl.getWorld(), x, y, z);
            org.bukkit.Material block = blockUnderPlayer.getBlock().getType();
          
            pl.sendMessage(block.toString());
          
            org.bukkit.Material block2 = org.bukkit.Material.AIR;
          
      
            if (block == block2) {
              
            } else {  
                List<String> list = plugin.getConfig().getStringList("players");
              
                pl.sendMessage(list.toString());
              
                for (int i = 0; i < list.size(); i++) {
                    String john = list.get(i);
                  
                    String str = pl.getName() + " " + x + " " + y + " " + z;
                  
                    if (!(str.equals(john))) {
                        list.add(pl.getName() + " " + x + " " + y + " " + z);
                        plugin.getConfig().set("players", list);
                        plugin.saveConfig();
                        list.remove(john);
                    }
                  
          
                  
                  
                }          
            }  
        }
    }
    
     
  2. Offline

    Lorinthio

    You could have a runnable running per player. if player.isOnGround() store the location for the player. Then if they are taking void damage you can grab the last location that was stored and teleport them.

    This way you can simply run a task every 4 ticks as opposed to every time a player moves. More efficient, and simple :)
     
    Banjer_HD likes this.
  3. Offline

    johnny_boy

    Great idea, will try, though haven't used runnables in a while, thanks AGAIN, for like the third time.
     
    Banjer_HD likes this.
  4. Offline

    Lorinthio

    If you have problems with runnable's feel free to reach out and I can help you on this!
     
Thread Status:
Not open for further replies.

Share This Page