Need help with compass tracking and chest randomizing.

Discussion in 'Plugin Development' started by ThePluginGuy, Aug 21, 2014.

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

    ThePluginGuy

    Hi, so I need help with a little something. I'm trying to make a compass that tracks the nearest player.
    Compass code:
    Code:java
    1. @EventHandler
    2. public void onCompassTrack(PlayerInteractEvent e) {
    3. Player p = e.getPlayer();
    4. Player target = //What do I put here??
    5. if((e.getAction() ==
    6. Action.RIGHT_CLICK_AIR || e.getAction() ==
    7. Action.RIGHT_CLICK_BLOCK) && p.getItemInHand().getType() == Material.COMPASS) {
    8. p.setCompassTarget(target.getLocation());
    9. }

    And also I am trying to randomize chest through out my minecraft server world with items like weapons, food, armor, etc.
    If somebody can post some code below telling me about both of these things, it would be highly appreciated.
     
  2. Offline

    Esophose

    ThePluginGuy
    For getting the closest nearby player you can use entity.getNearbyEntities()

    For the chest thing, there are several different ways to do this. I usually do it by listening to a PlayerInteractEvent, checking if they interacted with a chest, then looping through the chest slots and filling them with random items.
     
  3. ThePluginGuy You need a search funktcion, this is how you do it:
    Code:java
    1. private Location getLocationOfNearestPlayer(double range, Player searcher) {
    2. List<Entity> entityList = searcher.getNearbyEntities(range, range, range);
    3. for (Entity e : entityList) {
    4. if (e instanceof Player) {
    5. return e.getLocation();
    6. }
    7. }
    8. return null;
    9. }
    10.  
    11. @EventHandler
    12. public void onCompassTrack(PlayerInteractEvent e) {
    13. Player p = e.getPlayer();
    14. if (p.getItemInHand().getType() == Material.COMPASS) {
    15. Location targetLocation = getLocationOfNearestPlayer(100.0, p);
    16. if (targetLocation != null) {
    17. Action action = e.getAction();
    18. if ((action == Action.RIGHT_CLICK_AIR || action == Action.RIGHT_CLICK_BLOCK)) {
    19. p.setCompassTarget(targetLocation);
    20. }
    21. }
    22. }
    23. }


    Esophose are you sure that this works? because if 2 players access the same chest it will be filled twice, won't it? Also i guess that he wants to spawn chest somewhere in the world with randomized items and not fill them if player access it. But i'm not quiet sure what he meant so it's just guessing :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  4. Offline

    Esophose


    You bring up a valid point here. I store the location of the opened chest in an arraylist to see if the chest has been opened before :)
     
  5. Esophose possible way but i think there is a better way to do it :D but unless it works for you you won't care about, right? :D
     
  6. Offline

    ThePluginGuy

    The chest thingy I want to be sort of like minez.
     
Thread Status:
Not open for further replies.

Share This Page