Fake player interact.

Discussion in 'Plugin Development' started by Digi, Jul 15, 2011.

  1. Offline

    Digi

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Is there a way to trigger player interact on a block ?

    I tried:
    Code:
    new PlayerInteractEvent(event.getPlayer(), Action.LEFT_CLICK_BLOCK, new ItemStack(Material.AIR), block, BlockFace.NORTH);
    But it doesn't seem to work... the code is triggered and no errors in the console.
  2. Offline

    cholo71796

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Wait, what are you trying to do?
  3. Offline

    Digi

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Faking player interaction, as if a player would click a block or use something, can't it be done ?

    Currently I'm trying to use it on a door, but I have other uses for it aswell.
  4. Offline

    cholo71796

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Oh. Well, if you're trying to open a door, why not use right click? I'm not sure the exact specifications of what you're trying to do, but BlockDamageEvents also occur on left click, if that helps.
  5. Offline

    Digi

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Because I'm trying to open the door using the plugin, not the player... but I want the player to fake-interact the door because it would be more efficient than trying to get all the door parts, it's alignment and all those.

    I also tried:
    Code:
    Event fakeEvent = new PlayerInteractEvent(event.getPlayer(), Action.LEFT_CLICK_BLOCK, new ItemStack(Material.AIR), block, BlockFace.NORTH);
    
    getServer().getPluginManager().callEvent(fakeEvent);
    That won't work either... stil no errors and I'm sure the code triggers because I have a output message before it.


    Still... I may have perceived these events wrongly, they could be a 3rd party informative only addition that can control specific things, calling them could not actually trigger the thing they're hooked on... so I dunno, I'm searching for someone who's done something like this.
  6. Offline

    shadrxninga

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Code:
    PlayerInteractEvent playerInteractEvent = new PlayerInteractEvent(event.getPlayer(), Action.LEFT_CLICK_BLOCK, new ItemStack(Material.AIR), block, BlockFace.NORTH);
    plugin.getServer().getPluginManager().callEvent(playerInteractEvent);
    Something like that should work. I haven't tested it though.
    Hope it helps :D
  7. Offline

    Digi

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Nope that doesn't work either, I also tried using RIGHT_CLICK_BLOCK, same result, nothing.
  8. Offline

    Evenprime

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Selfcreated events don't have any influence on the game. Events work as follows (in your example):

    (1.) Server gets right-click command from player
    (2.) Server creates event with that information and hands it over to the plugins for review, modification, cancelling.
    (3.) After all plugins did their thing, the data of the event is used to really execute the players right-click command (or not, if it was cancelled)

    Just creating an event at some random point won't lead to step 3 being executed - the events data isn't used at all. If you want to fake a right-click of a player you'd have to find the location of (1.) and execute that method (which will then lead to the creation of an event aso.), or bypass that and try to find where (3.) is done and execute that (which will also prevent other plugins from knowing about this action, which may be good or bad, depending on your intents).
    _ralts likes this.
  9. Offline

    Digi

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Okay I understand, and the informational event can be faked using the methods above for other plugins only, right ?

    So I need to do it the hard way for doors.

    Thanks for the input :p
  10. Offline

    nisovin

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    If you want to compile against CraftBukkit rather than just Bukkit, you could try something like this:

    net.minecraft.server.Block.WOODEN_DOOR.interact(world, x, y, z, ((CraftPlayer)player).getHandle());

    I haven't actually tested this, but it works for levers and buttons.
  11. Offline

    Digi

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    How did you input the world ? It requires cast to "net.minecraft.server.World" but in-game it says it can't cast to that :-?

    Edit: nvm,
    Code:
    ((CraftWorld)world).getHandle()
    and it seems to work, interesting :-?
  12. Offline

    Shamebot

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    If you want to open a door you simply can do:
    Code:java
    1. BockState state = block.getState();
    2. ((Door)state.getData()).setOpen(true);
    3. state.update();
  13. Offline

    Digi

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Yes I've seen that method but I couldn't get Block to Door so I looked into it's source, it just sets bit 0x4 on that block, but I also need support for reversed doors and that I don't really have a clue how to quickly detect, I've seen sources of plugins managing doors but I can't really get it.

    isOpen() also detects wrongly, it just checks for the 0x8 bit.

    setOpen() also sets open that specfic block of the door as open/closed, not the whole door :p
  14. Offline

    Shamebot

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Only one block turns?
    If you want to toggle the door try
    Code:java
    1. BockState state = block.getState();
    2. Door door = (Door)state.getData();
    3. door.setOpen(!door.isOpen());
    4.  
    5. state.update();

    if you need the other block you simply can do
    Code:java
    1. block.getFace(door.isTopHalf()?BlockFace.DOWN:BlockFace.UP);
  15. Offline

    Digi

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    isOpen() doesn't work properly, like I said, it doesn't detect reversed doors, that's my main dilema atm.
  16. Offline

    Shamebot

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Player interact toggles the door, doesn't it?
    isOpen() does detect whether the door is turned around it's hinge, but of course it can't regard the orientation of the door.
    So why wouldn't
    Code:java
    1. door.setOpen(!door.isOpen());

    toggle the door and therefore do the same as player interact?
  17. Offline

    Digi

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    It's extra code rather than using minecraft's internal functions.
    It's also not the same thing since you manually force the block to change states instead of interacting with it... I belive it would be better plugin compatible to fake interaction rather than forcing a block to change. I think interact() also triggers the needed events aswell, I dunno for sure tough.

    Anyway, I'll figure out myself how to efficiently detect if a door is *really* open regardless of orientation.
  18. Offline

    Shamebot

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    I try to not use craftbukkit if it's possible, there isn't an api for nothing.
    And it depends on the purpose, you need a player for a fake interact.
    What's your definition of open?
  19. Offline

    Digi

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Well, open, when you placed it it's closed :} the opposite of that.
    Reversed doors already have the 0x8 bit set when placed, meaning they're detected as open but they're actually closed.

    I'm saying "reversed doors" and not "double doors" because you can place a single door with the hinge in the other side... by having a wall in that side or something like glass in the other side... if you understand what I'm trying to say.... if not, well, I'll post a screenshot with my testing doors and you'll surely understand.
  20. Offline

    Shamebot

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    So do you listen to onBlockPlace and save the value? Is there another way to know how a door was initially placed?
  21. Offline

    Digi

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    The flaw there is for doors that were already placed... how do you know if they're not already open when world loads ? xD

    PS: I've edited the message because I knew it wouldn't be a good ideea... re-read it please :p
  22. Offline

    Shamebot

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    So how do you know if a door is reversed/notted however you want to call it?
    Is there something like a default hinge?
    Or do you check whether there's glass around the door?
  23. Offline

    Digi

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Hmm, there can't be an efficient way to detect if a door is actually closed or open since you can place them in any way you want and only you know how you meant the door to be open or closed.... not to mention that you can place/remove blocks around it and the door will stick to that state, so there's really no way to figure out how the door was meant to be without saving them when placed, I found that when you place the 2nd door of a double door it gets opened and it's detectable by onBlockPlace event, but I dunno, this isn't efficient in terms of plugins, you'd have to re-place all your doors so the plugin would efficiently detect them.... too damn complicated :}

    So until notch adds a reversed door bit or someting, I'm just gonna quit managing doors :}
  24. Offline

    JoshuaBehrens

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Call a playerInteract-Event on the door ?
  25. Offline

    Digi

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Look at the above posts, it's been explained that calling an event won't actually trigger anything in the game.
  26. Offline

    JoshuaBehrens

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Oh, I just read the last 3 or 4 sorry. But why isn't it working ?
  27. Offline

    Astusvis

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Maybe you could have a look at the doors redstone code, that can tell the difference between open and closed.
  28. Offline

    Digi

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft's redstone code ? That won't do any good since the game itself can't figure out if a reversed door is open or not.
  29. Offline

    Astusvis

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Umm, yes it can?
    No matter what rotation, a door is always open when powered, always closed when unpowered. How do you think it does that?
  30. Offline

    Digi

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    You obviously never really played with placing doors, do this: place a wall on the right and no wall on the left, then place a door, that spawns a reversed door which is basically a rotated open door that acts like a closed reversed door, then place a pressure plate near it, you'll see that the pressure plate opens the door when inactive and closes it when you step on it, which is exacly what isOpen() detects aswell.

    So, like I said but in other words, you can't really detect the way the user intended the door to be when placed, you can have alot of combinations by placing doors with temporary walls near them, placing them fron different angles, and so on.
    The problem is that the reversed doors aren't actually special in any way, they're just placed in a weird angle... but that headache can easily be fixed by adding a bit value to it, making it reversed, then redstone/pressureplates/buttons would be able to work flawlessly.

Share This Page