Various Queries

Discussion in 'CommandHelper' started by Chloe Sanders, Jan 25, 2018.

  1. Chloe Sanders

    Chloe Sanders New Member

    I'm creating this thread called 'Various Queries' as there were a lot of questions I had about Command Helper but was too afraid to ask as they were really small questions and I didn't want to keep spamming threads, for that I'm creating this thread that I'll keep posting in for when I need help with something. I really can't say enough that I appriciate all the support given to me so far here you have given me access to such an amazing plugin with constant support and never ask for anything in return, thanks!

    Question 1:
    With the effect that was added into CHNaughty 'open_sign' requiring a block to be set it gave me a cool idea for allowing people to edit signs by right clicking them like so:
    PHP:
    if (extension_exists('CHNaughty') == true) {
      bind(player_interact, null, array('button': 'right', 'block': '63'), @sign_interact) {
        open_sign(player(), @sign_interact['location'])
      }
    }
    and it works great! people can right click a sign to edit them without having to break and replace (super handy!) the problem I've noticed however is the ID of a sign that is placed on a block and the ID of a sign placed on the ground is different as shown in this picture:
    [​IMG]
    This isn't a big deal I know how to easily fix this without using Prefilters my question though is can I check if the block is 63 OR 68 in a prefilter? I'm still very new to command helper all I've tried is bind(player_interact, null, array('button': 'right', 'block': '63', 'block': '68'), @sign_interact) { but all that does is recognize the last 'block'

    Question 2:

    Going back to the
    PHP:
    if (extension_exists('CHNaughty') == true) {
      bind(player_interact, null, array('button': 'right', 'block': '63'), @sign_interact) {
        open_sign(player(), @sign_interact['location'])
      }
    }
    I want to also add a condition to make sure the player who right clicks the block is not sneaking, this way the sign UI will not pop up when a player is trying to place a block above the sign
    Last edited: Jan 25, 2018
  2. PseudoKnight

    PseudoKnight Well-Known Member Developer

    1: Only macro or regex prefilters can have OR checks using regex. That prefilter is an item match, so you just have to either have two binds or check for the block type in the script instead of the prefilter.

    2. Use !psneaking() in an if() condition.
  3. Chloe Sanders

    Chloe Sanders New Member

    Awesome that works thanks :p

    wait so how do I do it in 1 line? I thought it was like:
    PHP:
    if (extension_exists('CHNaughty') == true) {
      bind(player_interact, null, array('button': 'right'), @sign_interact) {
        if (@sign_interact['block'] == '63' or '68') {
          msg('test')
          if(!psneaking()) {
            open_sign(player(), @sign_interact['location'])
          }
        }
      }
    }
    as it throws no errors but everything I right click says test
  4. PseudoKnight

    PseudoKnight Well-Known Member Developer

    I'm not sure why that condition would always evaluate true, but it's written incorrectly to check for two block types. So, blocks there are represented in the "0:0" format. This will be improved in the future for the impending 1.13 release, but I haven't gotten to this event yet. So this is a string and it'll be different depending on the sign type and rotation (eg. "68:3"). To check only if it's 63 or 68, we have to separate the rest of the string from those numbers. There are a few ways to do this, but this is how I commonly do it:
    PHP:
    @id = split(':', @event['block'])[0]; //fixed
    That splits the string on the colon and returns an array of both halves of the string. Then we get the first index [0] to get the id. Now we can do our conditional check using the "or" operator.
    PHP:
    if(@id == '63' || @id == '68') {
    While I find this a little cumbersome for just checking the block id, this type of string handling is still a useful tool in general.

    Note the block and item prefilters ignore the colon and data values. They basically do the split step internally. So you can say array('block': '68:0') and it'll ignore the ':0'.
    Last edited: Jan 26, 2018
  5. Chloe Sanders

    Chloe Sanders New Member

    I tried your code I was using:
    PHP:
    if (extension_exists('CHNaughty') == true) {
      bind(player_interact, null, array('button': 'right'), @sign_interact) {
        @id = split(@sign_interact['block'], ':')[0];
        msg(@id . @sign_interact['block'])
        if(@id == '63' || @id == '68') {
          if(!psneaking()) {
            open_sign(player(), @sign_interact['location'])
          }
        }
      }
    }
    it wasn't working so I had that message to see what @id was and @sign_interact['block'] was and @id always seems to equal a colon while @sign_interact['block'] is the correct id:data :(

    when I remove the [0] the entire array seems to just return as {:}
  6. Chloe Sanders

    Chloe Sanders New Member

    Nevermind. I remember you showed me how to do this previously in my farming help thread and the split was the wrong way around lol split(':', @sign_interact['block'])[0] that's working now

    Finished Code:
    PHP:
    if (extension_exists('CHNaughty') == true) {
      bind(player_interact, null, array('button': 'right'), @sign_interact) {
        @id = split(':', @sign_interact['block'])[0]
        if(@id == '63' || @id == '68') {
          if(!psneaking()) {
            open_sign(player(), @sign_interact['location'])
          }
        }
      }
    }
  7. Chloe Sanders

    Chloe Sanders New Member

    Question 3:
    How can I check what item someone is holding? I was looking at pheld_slot() for getting the slot number but couldn't find how to check the item, also how do I do the same for their offhand and armour?
  8. PseudoKnight

    PseudoKnight Well-Known Member Developer

    I sometimes screw up split() and a couple others because most string functions are the other way around, starting with the subject string.

    3: pinv(player(), null) would be the equivalent of pinv(player(), pheld_slot());
    Chloe Sanders likes this.
  9. Chloe Sanders

    Chloe Sanders New Member

    I've not noticed any anvil events in command helper so I'm not sure how to do this but I've seen it in plugins before so I know it's possible, how do I make it so that you can use colour codes in an anvil and the result item is coloured?
  10. PseudoKnight

    PseudoKnight Well-Known Member Developer

    On inventory_click event you can change the result item to parse color codes.
  11. Chloe Sanders

    Chloe Sanders New Member

    I tried this out
    PHP:
    bind(inventory_click, null, null, @anvil_click) {
      if(@anvil_click['inventorytype'] == 'ANVIL'){
        if(@anvil_click['rawslot'] == 2){
          @anvil_click['inventory'][0]['meta']['display'] = colorize(@anvil_click['inventory'][0]['meta']['display'])
        }
      }
    }
    And it doesn't work. I tried
    PHP:
    bind(inventory_click, null, null, @anvil_click) {
      if(@anvil_click['inventorytype'] == 'ANVIL'){
        if(@anvil_click['rawslot'] == 2){
          broadcast(@anvil_click['inventory'][0]['meta']['display'])
        }
      }
    }
    And it only works with the old name not the one you're trying to change it to

    Also even if this did work people wouldn't be able to visually see it work until after they edit it, the plugin I'm refering to allows you to see the result coloured the second you change the name in the anvil UI there is an event for this in spigot https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/inventory/PrepareAnvilEvent.html but doesn't seem to be one in command helper

    This is the plugin I'm refering to: https://www.spigotmc.org/resources/coloredanvils.2216/
  12. PseudoKnight

    PseudoKnight Well-Known Member Developer

    Inventory_click is a little difficult. What you're doing there is checking for any type of clicking with an anvil open. What I do is prefilter the RESULT slottype, colorize the display meta of the slotitem if it has a display, then modify_event('slotitem', @newitem). This way, when they pull the result item out, it colorizes it.
  13. Chloe Sanders

    Chloe Sanders New Member

  14. PseudoKnight

    PseudoKnight Well-Known Member Developer

    I'm not sure if CHGriefPrevention is up-to-date with GriefPrevention changes. It's not my extension. But you can download most extensions from this build site: https://letsbuild.net/jenkins/
    Chloe Sanders likes this.
  15. Chloe Sanders

    Chloe Sanders New Member