Multiple events in one class? Need help!

Discussion in 'Plugin Development' started by DubstepR3mix, May 25, 2016.

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

    DubstepR3mix

    I am new to development and recently ran into a problem. I am trying to make a chest protection plugin and am wondering how/if possible to have different event types in one class and work.

    Code:
        public void onSignChange(SignChangeEvent event) {
            if (event.getLine(0).equalsIgnoreCase("[Lock]")) {
                event.setLine(0, "§c[Locked]");
    
                Player player = event.getPlayer();
                event.setLine(1, (ChatColor.AQUA + player.getName()));
    Code:
        public void OnBlockPlaced(BlockPlaceEvent event) {
            if (event.getBlockPlaced().getType() == Material.SIGN && event.getBlockAgainst().getType() == Material.CHEST) {
                Player player = event.getPlayer();
                player.sendMessage(ChatColor.YELLOW + "Test");
    Also how would I get the event of someone trying to open a chest? Thanks in advance :)
     
  2. Offline

    timtower Administrator Administrator Moderator

    @DubstepR3mix openinventoryevent ? Playerinteractevent?
    And they are just methods, just put them below each other.
     
  3. Offline

    DubstepR3mix

    Code:
    public void OnBlockPlaced(BlockPlaceEvent event) {
            if (event.getBlockPlaced().getType() == Material.SIGN && event.getBlockAgainst().getType() == Material.CHEST) {
                Player player = event.getPlayer();
                player.sendMessage(ChatColor.YELLOW + "Test");
            }
        }
    
        @EventHandler
        public void onSignChange(SignChangeEvent event) {
            if (event.getLine(0).equalsIgnoreCase("[Lock]")) {
                event.setLine(0, "§c[Locked]");
    
                Player player = event.getPlayer();
                event.setLine(1, (ChatColor.AQUA + player.getName()));
    So like this?
     
  4. Offline

    timtower Administrator Administrator Moderator

    @DubstepR3mix If the BlockPlaceEvent also has @EventHandler: yes
     
  5. Offline

    DubstepR3mix

    Alright, I have the signs working but cannot figure out for the life of me how to only get it to work on a chest
    Code:
        @EventHandler
        public void onSignChange(SignChangeEvent event) {
            if (event.getLine(0).equalsIgnoreCase("[Lock]")) {
                event.setLine(0, "§c[Locked]");
                Player player = event.getPlayer();
                if (event.getLine(1).equalsIgnoreCase(player.getName()))
                    player.sendMessage(ChatColor.YELLOW + "Test");
                else event.setLine(0, (ChatColor.DARK_RED + "ERROR: {Name}"));
     
  6. Offline

    timtower Administrator Administrator Moderator

    @DubstepR3mix Assuming that you want to check the sign when somebody opens the chest:
    Listen to the interactevent, if chest: find signs around it, check signs.
    If not: just ignore the event.
     
  7. Offline

    DubstepR3mix

    I'm a noob, how would one do that?
     
  8. Offline

    Zombie_Striker

    @DubstepR3mix
    No one here will spoonfeed you code if that is what you're asking for.

    Here is a more detailed explanation of what you need to do.
    1. Set up the interact event and checking if the clicked block is a chest.
    2. Create a for loop for all four sides of the block, and use the Block#getRelative(BlockFace) method to get the blocks north,south,east, and west of the chest.
    3. For each block, test if the block's type is equal to WALL_SIGN (Not to be confused with SIGN, which is the item).
    4. If the block is a WALL_SIGN, get the Sign instance by using the following
      Code:
      Sign sign = (Sign) BLOCK#getState();
      What this does is return the Sign's data.
    5. Using that sign instance. Use sign.getLine( int ) to get a line of the sign. Note that int represents the line you wish to get, where the first line is '0'.
    6. After that, do the checks you want.
     
  9. Offline

    MadMaxCookie

    make sure every events has their own EventHandler to make all events working in just 1 class.
     
Thread Status:
Not open for further replies.

Share This Page