Solved Setting individual spawn locations

Discussion in 'Plugin Development' started by Otaku_Riisu, Sep 13, 2015.

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

    Otaku_Riisu

    My goal here is to set the spawn location of the player when they interact with an enchantment table.
    Obviously, the code below doesn't work. I cannot, however, seem to find a method that would set the player's spawn to the table. I have gotten it to work with the world spawn, but that would cause issues, naturally. Is there any way I can remedy this?

    Yes I have tried player.setBedSpawn, but that also failed, as the bed was "missing or obstructed"

    Help would greatly be appreciated.


    Code:
    package me.otakuRiisu;
    
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.enchantment.EnchantItemEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerRespawnEvent;
    
    public class Rebirth implements Listener{
        public Rebirth(MainSpawnClass plugin){
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
        }
       
        @EventHandler
        public void onPray(PlayerInteractEvent prayEvent){
            Player player = prayEvent.getPlayer();
             if (!(prayEvent.getAction() == Action.RIGHT_CLICK_BLOCK)) return;
             if (!prayEvent.getClickedBlock().getType().equals(Material.ENCHANTMENT_TABLE)) return;
                Location spawn = player.getLocation();
    }
       
        public void onRebirth(PlayerRespawnEvent rebirthEvent){
            rebirthEvent.setRespawnLocation(spawn);
        }
    }
    
     
  2. Offline

    Gamesareme

    @Otaku_Riisu I think the best way would be to have spawn location in the config, then use a listener to see when a player logs in or respawns.
     
  3. Maybe use a HashMap:
    Code:java
    1. package me.otakuRiisu;
    2.  
    3. import org.bukkit.Location;
    4. import org.bukkit.Material;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.event.EventHandler;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.event.block.Action;
    9. import org.bukkit.event.enchantment.EnchantItemEvent;
    10. import org.bukkit.event.player.PlayerInteractEvent;
    11. import org.bukkit.event.player.PlayerRespawnEvent;
    12.  
    13. public class Rebirth implements Listener{
    14.  
    15. private Map<String, Location> homes = new HashMap<String, Location>();
    16.  
    17. public Rebirth(MainSpawnClass plugin){
    18. plugin.getServer().getPluginManager().registerEvents(this, plugin);
    19. }
    20.  
    21. @EventHandler
    22. public void onPray(PlayerInteractEvent prayEvent){
    23. Player player = prayEvent.getPlayer();
    24. if (!(prayEvent.getAction() == Action.RIGHT_CLICK_BLOCK)) return;
    25. if (!prayEvent.getClickedBlock().getType().equals(Material.ENCHANTMENT_TABLE)) return;
    26. homes.put(player.getName(), player.getLocation());
    27. }
    28.  
    29. @EventHandler
    30. public void onRebirth(PlayerRespawnEvent rebirthEvent){
    31. rebirthEvent.setRespawnLocation(homes.get(rebirthEvent.getPlayer().getName()));
    32. }
    33. }
    34.  
     
  4. Firstly, as far as I can see, 'spawn' is a local variable and so cannot be accessed outside of the method it's in.

    Secondly, a much easier way would be to add the Location they are at to the config, obviously split into x, y, z, then on respawn, get the x, y and z values, get the world they are in, turn it into a new Location(world, x, y, z) then teleport them to it.

    No need for the setRespawnLocation etc
     
  5. Offline

    Otaku_Riisu

    So I tried the hash map option. I tried it myself, and I used the code above. It still didn't work.

    I'd rather avoid using a config, as setting a spawn would be for each individual player resulting in a messy config file. Not that it matters if the file itself is messy, but it seems like it'd be taking some extra steps that don't need to be taken. I'm going for more of a bed spawn effect

    EDIT: I think it may be more productive to remove the requirement for setting a bed the bed spawn method. Maybe remove the bed check? Is there a way to do this?

    EDIT AGAIN:
    By using the method player.setBedSpawn(Location, true) it worked fine.
     
    Last edited: Sep 20, 2015
Thread Status:
Not open for further replies.

Share This Page