Solved Problems with Traps

Discussion in 'Plugin Development' started by xWatermelon, Apr 6, 2013.

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

    xWatermelon

    I'm adding in a feature to my plugin so when you place a stone pressure plate on top of gravel, it will create a trap so when someone walks on it it will explode. I have created a custom class to do this:
    Code:java
    1. public class DemomanTrap {
    2. private Location loc;
    3.  
    4. public DemomanTrap(Location loc){
    5. this.loc = loc;
    6. }
    7.  
    8. public Location getLocation(){
    9. return this.loc;
    10. }
    11.  
    12. public void explode(){
    13. TNTPrimed tnt = (TNTPrimed) loc.getWorld().spawnEntity(loc, EntityType.PRIMED_TNT);
    14. tnt.setFuseTicks(0);
    15. }
    16. }

    And on my BlockPlaceEvent, I add a new DemomanTrap to a List<DemomanTrap>. However, on my PlayerMoveEvent, it will not explode. This is what I have:
    Code:java
    1. for(DemomanTrap trap : demomanTraps){
    2. if(trap.getLocation() == player.getLocation()){
    3. trap.explode();
    4. }
    5. }

    But this doesn't work. I have registered my events, added @EventHandler, and even am doing another thing in my PlayerMoveEvent method that works. Can anyone explain what's going wrong and how I can fix it? Thanks!
     
  2. Offline

    TheButlah

    first of all, you cant compare 2 locations by doing trap.getLocation() == player.getLocation(). The proper way to do this is to do trap.getLocation().equals(player.getLocation())

    Second of all, the x,y,and z components of locations are stored as doubles. if the player is even a hair off of the location of the trap, they wont be equal and will therefore not go off. Try instead doing
    Code:
    if (trap.getLocation().distance(player.getLocation()) <= 1) {
        trap.explode()
    }
    Third, make sure you are registering the events and adding EventHandlers, if you arent already
     
    xWatermelon likes this.
  3. Offline

    chasechocolate

    Correct. xWatermelon do something like this:
    Code:java
    1. Location loc = new Location(player.getWorld(), player.getLocation().getBlockX(), player.getLocation().getBlockY(), player.getLocation().getBlockZ());
    2.  
    3. if(trap.getLocation().equals(loc)){
    4. trap.explode();
    5. }

    Not always, if you save a block's location, it will be stored as world, int, int int. An easy fix would be to use the code I gave above.
     
    xWatermelon likes this.
Thread Status:
Not open for further replies.

Share This Page