Solved Movement of Player on Block help

Discussion in 'Plugin Development' started by AstramG, Sep 28, 2012.

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

    AstramG

    I want to make sure that when a player walks at a location defined in an arraylist, it will send the player some nice chat. Here is how I defined the arraylist:

    Code:
    public ArrayList<Location> chatZone = new ArrayList<Location>();
    
    My current code for seeing if a player is in the "chatzone" location (Yes its a single block):

    Code:
    @EventHandler
    public void onWalkInZone(PlayerMoveEvent event) {
    Player player = event.getPlayer();
    Location ploc = player.getLocation();
    if (chatZone.contains(ploc)) {
     
    player.sendMessage(ChatColor.GOLD + "You'r in the ploc :3");
     
    }
    
    Here is how I added to the arraylist:
    Code:
    @EventHandler
    public void makeZone(PlayerInteractEvent event) {
    Location l = event.getClickedBlock().getLocation();
    if (event.getPlayer().getItemInHand().getType() == Material.SLIME_BALL && event.getAction() == Action.RIGHT_CLICK_BLOCK) {
     
    event.getPlayer().sendMessage(ChatColor.GOLD + "ChatZone Created!");
    chatZone.add(l);
     
    }
     
    }
    
    The other parts of the code should be working fine, but I don't wish to share them. No errors pop up when I walk on the block that I add to the arraylist, the adding is fine. Just when the player walks on it, isn't.
     
  2. Offline

    gregthegeek

    Is the method you put the first block of code in being called? What event do you use?
     
  3. Offline

    AstramG

    Sorry I forgot to add that to the OP, I already had that but I didn't post it :p Oops, I editted it.
     
  4. Offline

    Tirelessly

    This would be a very specific area.. I'm not exactly sure, but I think you'd need to be on the exact spot.
     
  5. Offline

    AstramG

    How would I make it on the block? I don't know how :p Can you explain?
     
  6. Offline

    gregthegeek

    When you check the array, do
    Code:
    chatZone.contains(new Location(pLoc.getBlockX(), pLoc.getBlockY(), pLoc.getBlockZ()));
     
  7. Offline

    AstramG

    The code in Eclipse returns an error from this line
    Code:
    if (chatZone.contains(new Location(pLoc.getBlockX(), pLoc.getBlockY(), pLoc.getBlockZ()))) {
    
    It says: to 1 make the code look like this:
    Code:
    if (chatZone.contains(new Location(null, pLoc.getBlockX(), pLoc.getBlockY(), pLoc.getBlockZ()))) {
    
    Which doesn't work....

    Then for the 2nd choice it says make the code
    Code:
    if (chatZone.contains(new Location(null, pLoc.getBlockX(), pLoc.getBlockY(), pLoc.getBlockZ(), 0, 0))) {
    
    Which also doesn't work X_X...

    Someone, please help ugh...

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 28, 2016
  8. Offline

    TheE

    You are checking for locations, which means also pitch and yaw are checked - doesn't make much sense for your needs. You could do something like this:
    Code:java
    1. for (Location loc : chatZone){
    2. if (loc.getBlockX() == pLoc.getBlockX() && loc.getBlockY() == pLoc.getBlockY() && loc.getBlockZ() == pLoc.getBlockZ()){
    3. player.sendMessage("You are in the ChatZone!");
    4. }

    However, it might be better to change your array list so it only saves coordinates and not whole locations.
     
  9. Offline

    AstramG

    How do I change the arraylist to do that? And getX(), getY(), and getZ() don't work in that if statement
     
  10. Offline

    TheE

    Depends on the way your plugin is written. Personally I would create a new class ChatLocation that contains three integers (X, Y and Z) and one string (the chat message). Than make your array list store ChatLocation-objects instead of locations - like this you have all informations you may need stored together.

    But again, this is up to you and the way your plugin should work. The solution I posted above may be enough for your needs.

    edit: I just changed the code, you need to use getBlockX() instead of getX() etc. Sorry.
     
  11. Offline

    AstramG

    The code still didn't work, when I walk there it does nothing.

    Nevermind, it works! It was just that I needed to add +1 to the y because when I stood on the block nothing happened because the location WAS the block and not the space where I walk (the block above)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 28, 2016
Thread Status:
Not open for further replies.

Share This Page