How to get off my high horse?

Discussion in 'Plugin Development' started by hpdvs2, Mar 9, 2012.

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

    hpdvs2

    I've a great deal of C# experience, and now trying my hand at Java. As one of the first commands, I created a dragon, and set the player as a passenger. Worked great. Until you try to get off. No command worked. /home brings and the dragon home. /warp brings you and the dragon.

    I tried the following code:
    Vehicle v = player.getVehicle();
    v.eject();
    v.remove();

    Which I expected to eject me from the vehicle, and remove the dragon from the game. Alas it did not. Can someone help me off my high horse?
     
  2. Offline

    BloodShura

    @off:
    LOL, high horse. :p

    @on:
    Try casting the EnderDragon, like:
    Code:
    EnderDragon a = (EnderDragon)ENTITY;
     
    a.remove();
    In "Entity", you set the EnderDragon (my idea is, when the player starts riding the EnderDragon, you add the EnderDragon to a variable/hashmap/whatever, and you get the enderdragon in variable and put in ENTITY.

    If this don't work or is complicated, try setting the vehicle to null, or setting the passenger to null, or something like that. O.O

    I maked a plugin to ride mobs, but it doesn't works with EnderDragon. :/ I'll look around for some code. If I got something useful, I post here. :)
     
  3. Offline

    hpdvs2

    Thanks, but I'm still on the dragon...

    Vehicle v = player.getVehicle();
    if (v instanceof EnderDragon)
    {
    //v.eject();
    //v.remove();
    ((EnderDragon)v).remove();
     
  4. Offline

    BloodShura

    Did this code works? because I'm not so sure if EnderDragon can be cast to vehicle... D:
     
  5. Offline

    hpdvs2

    It compiles donut?

    Thanks! You pegged it. It compiled, it didn't throw an error when executing. Appearantly, my initial code:
    if(player.getVehicle() == null) // which determines if you are already on one,
    always returns null if it can't be cast as a vehicle. So it always thinks its null, and gives me another ender dragon, with the previous one(s) chasing me.

    OK, so new question:
    What command should I use to get the dragon that I am a passenger of?

    this is the code I used to create the dragon and set myself as the passenger if it helps.
    EnderDragon ed = (EnderDragon)player.getWorld().spawnCreature(loca, CreatureType.ENDER_DRAGON);
    ed.setPassenger(player);

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

    Taco

    Just an idea here, possibly try ed.setPassenger(null);
     
  7. Offline

    hpdvs2

    I guess I should explain the plugin a bit better.
    It has an if statement to check if you already have a vehicle. if not, it gives you a dragon. if you do, it removes it (or its supposed to), this means that 'ed' is not available any more.

    if(player.getVehicle() == null)
    {
    Location loca = player.getLocation();
    loca.setY(loca.getY() + 3);
    EnderDragon ed = (EnderDragon)player.getWorld().spawnCreature(loca, CreatureType.ENDER_DRAGON);
    ed.setPassenger(player);
    return true;
    } else {
    Vehicle v = player.getVehicle();
    player.sendMessage(v.toString());
    if (v instanceof EnderDragon)
    {
    ((EnderDragon)v).remove();

    OMG, I just realized my 'high horse' is the famous Mr. Ed! :D

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

    Technius

    You could try iterating through all the entities, and then remove it if the rider is a player.
    Code:java
    1. Server s = //the server
    2. for(World w: s.getWorlds())
    3. {
    4. for(Entity e:w)
    5. {
    6. if(e instanceof EnderDragon && e.getPassenger() == player)
    7. {
    8. e.eject();
    9. e.remove();
    10. }
    11. }
    12. }


    I didn't test it, so it may not compile. But simple syntax errors should be easy to fix. ^_^
    If this doesn't work, then it could possibly be a Bukkit bug.
     
  9. Offline

    Taco

    Another, more efficient way, of doing this would be to keep the enderdragons and their riders in a hashmap, and simply iterate through that to remove the dragon.
     
  10. Offline

    Technius

    What if there is another plugin, like DragonTravel? That would conflict.
     
  11. Offline

    hpdvs2

    I came up with another route, since it would only be a few players using this, and rarely, I created a generic dictionary to hold a dragon to a player's name. (the player object itself dissolves when logging off and on again)

    Thanks everyone, for the help, and heres the code I ended up with.

    This works (and lands the player safely):
    Code:
    // At the front of the class
    Map<String,EnderDragon> thereBeDragons = new HashMap<String,EnderDragon>();
     
    // in the onCommand method...
    if (cmd.getName().equalsIgnoreCase("mule"))
    {
      if(thereBeDragons.containsKey(player.getName()))
      {
        EnderDragon ed = thereBeDragons.get(player.getName());
        ed.eject();
        ed.remove();
        thereBeDragons.remove(player.getName());
        Location loca = player.getLocation();
        for(int y = (int)loca.getY(); y > 0; y--)
        {
          loca.setY(y);
          if(world.getBlockAt(loca).getType() != Material.AIR)
          {
            loca.setY(y+2);
            player.teleport(loca);
            return true;
          }
        }
        return true;
      }
      else
      {
        Location loca = player.getLocation();
        loca.setY(loca.getY() + 3);
        EnderDragon ed = (EnderDragon)player.getWorld().spawnCreature(loca, CreatureType.ENDER_DRAGON);
        ed.setPassenger(player);
        thereBeDragons.put(player.getName(), ed);
        return true;
      }
    }
    Technius, I see you recommended HashTags, thanks. Clearly it worked, and in a way that *should* not conflict with other plugins.

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

Share This Page