Triggering events as OfflinePlayers.

Discussion in 'Plugin Development' started by BlueMustache, Jul 14, 2014.

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

    BlueMustache

    Hello Everyone,
    I'll keep this short.
    If I want to trigger a BlockBreakEvent, as the player "Notch", which was never on my server, which I added a fake Notch.dat file.
    I know how to trigger the event, but I am trying to use an offlineplayer as a player, but it doesn't work. If their anyway to do this? (Essentially, call the event as a FakePlayer with a configurable name.)
    Here is my code, please help asap!
    -Blue

    Code:java
    1. public boolean callFakeBreak(Block block) {
    2. Player fake = Bukkit.getOfflinePlayer("Notch").getPlayer();
    3. BlockBreakEvent event = new BlockBreakEvent(block, fake);
    4. Bukkit.getServer().getPluginManager().callEvent(event);
    5. if(!event.isCancelled()) {
    6. return true;
    7. }
    8. return false;
    9. }


    P.S. Yes, I know that this is not in UUID (stop shouting), this is for a plugin that is for pre-uuid servers. (No reason you can't help.)
     
  2. Offline

    fireblast709

    BlueMustache you would need a custom class implementing Player
     
  3. Offline

    BlueMustache

  4. Offline

    fireblast709

    BlueMustache no NMS needed. Just something like
    Code:java
    1. public class FakePlayer implements Player
    2. {
    3. // implement the methods
    4. }
    should do the trick
     
  5. Offline

    BlueMustache

    fireblast709
    I'm following. Good so far.
    I hate to ask, but what might I put in the new class?
    I'm trying to learn for next time, but I don't know what to do. :/

    EDIT: I'm just trying to call the BlockBreakEvent, as a player with a name I choose like "Notch542". Even if he is not online or ever been on the server.
    I'm trying to trick some code using my plugin. (Good intentions. :))
     
  6. Offline

    fireblast709

    BlueMustache when you implement Player, your IDE will complain that there are methods that need to be implemented. How those are implemented will be your task. For example you can have the name in the constructor and return this with getName(), or have empty methods for damage (damage has no meaning since the player isn't offline), and so on.
     
  7. Offline

    BlueMustache

    fireblast709
    I did that, but would I leave the class as FakePlayer or change it to OfflinePlayer?
    I would take it I implement the methods as an OfflinePlayer that the Player has.
     
  8. Offline

    fireblast709

    BlueMustache you create a new FakePlayer for the events you fire
     
  9. Offline

    BlueMustache

    fireblast709
    I still am very confused.
    I have the class FakePlayer, an made a new instance.
    How do I take that, and put in the FakePlayer's inputted name, and hook in to the event?
     
  10. Offline

    fireblast709

    BlueMustache events need a Player object, which you don't have. Thus you create a FakePlayer to fill that gap. How you implement it is completely your task, and shouldn't be too hard with basic Java knowledge.
     
  11. Offline

    BlueMustache

    fireblast709
    I taught myself java. SO I am missing a few things.
    I don't know what to google for help either.

    EDIT: I am following you now.
    I am confused on what I would give the FakePlayer to tell the event.
    Such as a setName()?
     
  12. Offline

    fireblast709

    BlueMustache all you need to do is implement all the methods that Player asks you to implement
     
  13. Offline

    BlueMustache

    fireblast709
    I did that, but where in the new class can I define what the FakePlayer's name will be?
    Do I just do say "setName("Notch")", and in the setName method, have it do "Bukkit.getOfflinePlayer("Notch")"?
     
  14. Offline

    fireblast709

    BlueMustache if you have an OfflinePlayer, just create a constructor that takes an OfflinePlayer and store that as a member variable (field)
     
  15. Offline

    BlueMustache

    fireblast709
    I know you guys hate spoonfeeding, and I respect that.
    I usually don't ask, but could you please provide an example of what you mean.
    Where as the FakePlayer has one main input, "FakePlayerNameHere", and assuming I have premade the fake player's.dat file.
    That would make my day, and then I can go to bed.
     
  16. Offline

    fireblast709

  17. Offline

    BlueMustache

  18. Offline

    fireblast709

  19. Offline

    BlueMustache

    fireblast709
    I have never worked with making my own api before. I have touched a little on using them.
    How can I get to from my point A to B?
     
  20. Offline

    Onlineids

  21. Offline

    BlueMustache

    Onlineids
    :(
    I'm not a noob. I have decent experience in many areas of bukkit.
    I just have never touched here.
    I just need some help with this one thing.
     
  22. Offline

    Onlineids

  23. Offline

    BlueMustache

    Onlineids
    If you were intelligent enough to read the rest of the thread, you would see I rarely ask for "spoonfed" examples.
    I have asked for in depth help like this once, maybe twice at most.
    I have written plugins, and re-written complex mods.

    And I for one feel like this right now.

    [​IMG]

    :(
    -Blue

    EDIT: Removed embarrassing sleep deprived rant.
     
  24. Offline

    Onlineids

  25. Offline

    BlueMustache

    Onlineids
    Thanks. :D

    I don't mean to be pushy, but is their anyone else?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  26. You can just put the OfflinePlayer instance there.
    EDIT: Lol, we posted at the same time
    EDIT 2: After testing with an IDE, I see that you can't.
     
  27. Offline

    BlueMustache

    TheBigSuperCraft
    I ave tried both.

    Code:
    Player p = Bukkit.getOfflinePlayer("Notch").getPlayer();
     
    and
     
    Player p = (Player) Bukkit.getOfflinePlayer("Notch");
    Neither work.
    I am stumped, and I am pretty much the only thread on this.
    Any better ideas?

    How can I get from point A:

    FakePlayer fake = new FakePlayer("Notch");

    Now assuming I have that input, how can I trick the event into thinking its a Player with that name, even though I am already extending Player.

    Point B:
    BlockBreakEvent event = new BlockBreakEvent(block, fake);
    Please help. :(

    TheBigSuperCraft
    Basically, how do I store that string, then trick the event into thinking it is a Player with that name.

    TheBigSuperCraft
    Also, lol, dangit.
    I finally find someone my age that understands me, and you are two months older! :confused:

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

    BlueMustache

    xTrollxDudex
    Sorry to tahg you.
    Thought you might be able to push me in the right direction.
     
  30. Offline

    Wizehh

    Remind me: what are you trying to do?
     
Thread Status:
Not open for further replies.

Share This Page