Click block event won't work

Discussion in 'Plugin Development' started by wesley27, Jul 2, 2014.

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

    wesley27

    Hello, my plugin blocks several things in creative mode, but this part isn't working. If the player is in creative mode, it is supposed to stop them from selling items to an admin shop(right clicking on a sign that has [Sell] on the first line).

    The plugin compiles fine, no errors in eclipse, compile, or in game/console. But this one part just won't work. You can still sell to admin shops in creative mode. Can anyone help me with what I've done wrong?

    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent event){
    3. Player player = event.getPlayer();
    4.  
    5. if (player.getGameMode() == GameMode.CREATIVE){
    6. Block blk = event.getClickedBlock();
    7.  
    8. if (blk != null && (blk.getType() == Material.SIGN || blk.getType() == Material.SIGN_POST)){
    9. Sign sign = (Sign) blk.getState();
    10.  
    11. if (sign.getLine(0).contains("[Sell]")){
    12. event.getPlayer().sendMessage(ChatColor.DARK_PURPLE+"[OUtilities] §2You are NOT able to sell items in creative mode.");
    13. event.setCancelled(true);
    14. }
    15. }
    16. }
    17. }


    Thanks :)
     
  2. Offline

    The Fancy Whale

    That isn't the entire playerinteract event is it? Also, add some debug messages and see where the code stops. I'm guessing it'll be line 8
     
  3. Offline

    tommyhoogstra

    Do us a favour and remove the font tags :)
     
  4. Offline

    The Fancy Whale

    Font tags? What do you mean?
     
  5. Offline

    tommyhoogstra

    I think my page glitched out, the source code was full of "font = arial" tags
     
  6. Offline

    teej107

    Your code looks fine. What I don't see is that you registered your events. Did you?
     
  7. Offline

    The Fancy Whale

    Looks fine but I believe it is edited a lot. It seems as though there is a whole sign shop plugin but I only see the code for if they are in creative mode.
     
  8. Offline

    wesley27

    The Fancy Whale Within that class, the playerinteract event is split into several functions because of what I wanted it to do, it made it easier to read and work on when split up. All of the others work fine, here is an example of another within that class that blocks something in creative mode.
    Code:java
    1. @EventHandler
    2. public void onInventoryOpen(InventoryOpenEvent event){
    3. Player player = (Player) event.getPlayer();
    4.  
    5. if (player.hasPermission(NO_RESTRICTIONS)){
    6. return;
    7. }
    8.  
    9. boolean isChest = !(event.getInventory().getHolder() instanceof PlayerInventory || event.getInventory().getHolder() instanceof CraftingInventory);
    10.  
    11. if (isChest && player.getGameMode() == GameMode.CREATIVE) {
    12. player.sendMessage(ChatColor.DARK_PURPLE+"[OUtilities] §2You are NOT able to store items in creative mode.");
    13. event.setCancelled(true);
    14. }
    15. }


    And, yes it did stop on line 8. How do you assume that?

    Also, the sign shop plugin is simply the admin shops that you make with essentials.
     
  9. Offline

    desht

    Let me guess: it works for signs placed on the ground, but not for signs on walls?

    Material.SIGN is not a wall sign. You need Material.WALL_SIGN for that.
     
  10. Offline

    wesley27

    desht thanks for the idea, but actually the code does account for wall signs.

    Material.SIGN_POST is signs on the wall and ground.

    I have an update - after more testing I realized that the code is in fact working. It is blocking a player who's in creative from destroying(left clicking) a sign with [Sell] on the first line. That's the problem. It needs to block players in creative from RIGHT CLICKING a sign with [Sell] on the first line. Is there a separate event for this?
     
  11. Offline

    AoH_Ruthless

    wesley27
    SignChangeEvent listens for addition of text to sign. Work with that instead.
     
  12. Offline

    wesley27

    AoH_Ruthless my event isn't supposed to listen to addition or changes text on a sign. Read my OP, it explains this. It is supposed to stop players in creative mode from right clicking a sign that has [Sell] on the first line. This is to prevent players from spawning items in creative and selling them to the admin shops.

    Currently, it is stopping players in creative from left clicking signs that have [Sign] on the first line. I want it to stop players from right clicking signs like that. So I'm assuming there's a separate event for right clicks and left clicks? I couldn't find it.
     
  13. Offline

    AoH_Ruthless

    wesley27
    Oh I misunderstood; yeah, you have to use PlayerInteractEvent. desht is actually the correct one here. Sign_Post and Wall_Sign aren't interchangeable. You have to listen for both.
     
  14. Offline

    teej107

    I heard that MC doesn't listen to right clicks if the player's hand is empty. Can anybody confirm this?
     
  15. Offline

    maved145

    teej107 Im pretty sure thats wrong considering most shops have RIGHT clickable signs.
     
  16. Offline

    teej107

    maved145 I didn't believe it either but yet again, I haven't tried it.
     
  17. Offline

    wesley27

    AoH_Ruthless okay, thanks. Ill add that but that still doesn't solve my initial problem, because we tested it with a normal sign. Its saying no to left clicks. I want it to.block right clicks. I've read of action events for right clicks but would that work for something like this?

    teej107 that seems extremely unbelievable. I've never heard that before, could anyone confirm this or explain an event to get right clicks?
     
  18. Offline

    maved145

    i forgot what event it was, it might be player interact event but you can use:

    Code:java
    1. Action a = event.getAction();
    2.  
    3. if(a == Action.RIGHT_CLICK)
    4. {
    5. //do something.
    6. }
    7.  
    8.  


    i think its something like that.
     
  19. Offline

    teej107

  20. Offline

    desht

    Wrong.
     
  21. Offline

    wesley27

    desht that's nice of you? I based my reply off of the fact that my current code works with left clicks for wall and ground signs.

    I'm here for a learning experience, not a reprimand. Thanks

    teej107 thanks! Ill check it out when I get home and let you know if it helped.
     
  22. Offline

    desht

    Sorry if that was a bit terse, but you did contradict me with an incorrect statement (namely: Material.SIGN_POST is certainly not both ground and wall signs).
     
  23. Offline

    wesley27

    teej107 Okay, so after just testing some more, here's what happens. If a player right clicks the [Sell] sign while in creative: Both when they have an item in their hand and when their hand is empty, it gives them the message "[OUtilities] §2You are NOT able to store items in creative mode." as if it will stop them, however the "event.setCancelled(true)" does not seem to register. While it gives them the message, they still sell the items to the sign and receive money. This is on RIGHT clicking the sign.

    So apparently, regardless of what that thread you linked says, my plugin is registering that the player right clicked the sign(with or without item in hand), and it is executing the first thing that it is supposed to, which is the message. But it then isn't executing the next line, the event.setCancelled(true), and it is allowing them to still sell to the sign.

    Any ideas why this is happening? Could it have something to do with the order of how I wrote the code, or where parts of it are? (the code is in my OP)
     
  24. Offline

    teej107

    wesley27 Do you have other plugins running? Can you show the method that gives the player money? Regardless of being cancelled or not, called methods/events will still be ran. If the method is an event listener, check to see if it has been cancelled and have that event be at a higher priority.
     
  25. Offline

    unrealdesign

    To see if a block is a sign you do

    Code:java
    1. if(blk.getState() instanceof Sign)

    Instead of checking the material. This will fix the problem.
     
  26. Offline

    xTigerRebornx

    wesley27 The sell shop sign's listener is probably at a higher priority then your listener, try putting it at the highest priority.
     
  27. Offline

    wesley27

    teej107 I didn't think of that, the plugin giving the players money upon using the shop is essentials. I will look into that if unrealdesign's suggestion does not work.

    unrealdesign Thanks! I will try this out, but I do have a question about that. If I use that, is there a separate instanceof for signs and wall signs? And if I would apply that change, I'm slightly confused on how I would change my code to use that... would it be like this?
    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent event){
    3. Player player = event.getPlayer();
    4.  
    5. if (player.getGameMode() == GameMode.CREATIVE){
    6. Block blk = event.getClickedBlock();
    7.  
    8. if (blk.getState() instanceof Sign){
    9. Sign sign = (Sign);
    10.  
    11. if (sign.getLine(0).contains("[Sell]")){
    12. event.getPlayer().sendMessage(ChatColor.DARK_PURPLE+"[OUtilities] §2You are NOT able to sell items in creative mode.");
    13. event.setCancelled(true);
    14. }
    15. }
    16. }
    17. }

    Would that work?

    xTigerRebornx Okay I changed the priority, what you said makes sense, I didn't even think of it. However, it still doesn't work. Here's the code, I'm near positive I did that correctly?
    Code:java
    1. @EventHandler(priority = EventPriority.HIGHEST)
    2. public void onPlayerInteract(PlayerInteractEvent event){
    3. Player player = event.getPlayer();
    4.  
    5. if (player.getGameMode() == GameMode.CREATIVE){
    6. Block blk = event.getClickedBlock();
    7.  
    8. if (blk != null && (blk.getType() == Material.SIGN || blk.getType() == Material.SIGN_POST || blk.getType() == Material.WALL_SIGN)){
    9. Sign sign = (Sign) blk.getState();
    10.  
    11. if (sign.getLine(0).contains("[Sell]")){
    12. event.getPlayer().sendMessage(ChatColor.DARK_PURPLE+"[OUtilities] §2You are NOT able to sell items in creative mode.");
    13. event.setCancelled(true);
    14. }
    15. }
    16. }
    17. }

    The same thing is still happening. If I right click the sign, I'm getting the message, but it is still taking the item and giving me money. It should be stopping that.
    [​IMG]

    And teej107 I believe you were saying the same thing as xTigerRebornx. I believe this is the method in essentials that pays the user and takes the item when they right click the sign.
    Code:java
    1. package com.earth2me.essentials.signs;
    2.  
    3. import com.earth2me.essentials.ChargeException;
    4. import com.earth2me.essentials.Trade;
    5. import com.earth2me.essentials.Trade.OverflowType;
    6. import com.earth2me.essentials.User;
    7. import net.ess3.api.IEssentials;
    8. import net.ess3.api.MaxMoneyException;
    9.  
    10.  
    11. public class SignSell extends EssentialsSign
    12. {
    13. public SignSell()
    14. {
    15. super("Sell");
    16. }
    17.  
    18. @Override
    19. protected boolean onSignCreate(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException
    20. {
    21. validateTrade(sign, 1, 2, player, ess);
    22. validateTrade(sign, 3, ess);
    23. return true;
    24. }
    25.  
    26. @Override
    27. protected boolean onSignInteract(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException, MaxMoneyException
    28. {
    29. final Trade charge = getTrade(sign, 1, 2, player, ess);
    30. final Trade money = getTrade(sign, 3, ess);
    31. charge.isAffordableFor(player);
    32. money.pay(player, OverflowType.DROP);
    33. charge.charge(player);
    34. Trade.log("Sign", "Sell", "Interact", username, charge, username, money, sign.getBlock().getLocation(), ess);
    35. return true;
    36. }
    37. }


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

    xTigerRebornx

    wesley27 If you are using Essentials, you are able to force it to take a lower priority (and it should already be taking the lower priority)
     
  29. Offline

    wesley27

    xTigerRebornx How, is that in the essentials config? The only priority force option that I found in the essentials config is the respawn-listener-priority, which is for using multiverse or essentials for spawning.
     
  30. Offline

    teej107

    wesley27 No, I meant to have the method where the player sells items at a higher priority. Priority goes from execution lowest first - highest/monitor last. Have
    Code:java
    1. Code:java
    2. @EventHandler(priority = EventPriority.HIGHEST)
    3. public void onPlayerInteract(PlayerInteractEvent event){
    4. Player player = event.getPlayer();
    5.  
    6. if (player.getGameMode() == GameMode.CREATIVE){
    7. Block blk = event.getClickedBlock();
    8.  
    9. if (blk != null && (blk.getType() == Material.SIGN || blk.getType() == Material.SIGN_POST || blk.getType() == Material.WALL_SIGN)){
    10. Sign sign = (Sign) blk.getState();
    11.  
    12. if (sign.getLine(0).contains("[Sell]")){
    13. event.getPlayer().sendMessage(ChatColor.DARK_PURPLE+"[OUtilities] §2You are NOT able to sell items in creative mode.");
    14. event.setCancelled(true);
    15. }
    16. }
    17. }
    18. }
    19.  

    this method priority lower than the method where the player sells/gets paid/etc.
     
Thread Status:
Not open for further replies.

Share This Page