Command Block output

Discussion in 'Plugin Development' started by OLEGSHA, Dec 26, 2014.

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

    OLEGSHA

    Hello everyone,

    I'm trying to make a command that would give a spiecific output when run in a command block, like /testfor does (it outputs the number of players matching). Changing Metadata doesn't seem to have affect, nor command return boolean (as onCommand(Command command, CommandSender sender, String alias, String[] args) is boolean method). So how do I do it correctly?

    Thank you,
    OLEGSHA
     
  2. Offline

    Windy Day

    In your on command check if the sender is a commandblock, just like how you can check if it is a player or console.
    Code:
    if(sender instanceof BlockCommandSender) {
      // it is a command block
    }
     
  3. Offline

    OLEGSHA

    Yes, I do that, and then what?.. I mean, when you use vanilla /testfor command in a command block any comparator facing away from it will return some signal. Say if there are 5 players curently online /testfor @a in a command block will make the comparator produce signal with strength of signal 5. I'm asking about that in fact.
    I already tried changing metadata "SuccessCount" like this:
    Code:
    ((BlockCommandSender) sender).getBlock().getState().setMetadata("SuccessCount", new MetadataValueOutput(state));
    ((BlockCommandSender) sender).getBlock().getState().update();
    where MetadataValueOutput.asInt(), asLong(), asShort(), asByte(), asFloat(), asDouble(), asString() will all return either 0 or 15, asBoolean() will return false if 0 else true and value() returns null. I also tried making a delay after the change was triggered before changing Metadata so I don't think it's some other plugin overriding.
    However none of these worked (the comparator always outputs 15) although the command works fine for players (it should return either (String) "true" or "false" for non-commandblock senders and it returns everything correctly)
     
  4. OLEGSHA, could I please see the code you used, I'm trying to do the same thing, I made a few 'quest' using command blocks in game but I wanted to make it in to a plugin, I'm currently having problems with the 'testfor' commands like you did, all I need to do is to be able to detect the 'feedback' of a command as you would in game with comparators.
     
  5. Offline

    OLEGSHA

    Well, I think I have lost that code. Plus mine does not work anyway, the current version is completely wrong. However I was thinking that maybe when a command willing to return smthing is run we could handle it using CommandPreProcessEvent and make some testfor command execute instead. Say "/return false" should return signal 0, so why not make the command block execute "/testfor p#layer" (such command will return false as p#layer nickname is invalid). In fact I'm gonna try it now.
     
  6. Offline

    ZanderMan9

    If you want it to output a redstone signal, you could try some trickery with replacing it temporarily with a redstone block for a tick, then changing it back. I'm sure if you cast sender to CommandBlock you could somehow get its location.
     
  7. I fount a work around, made it use a set of command blocks with comparators and redstone in game for each if statement I use.


    Code:
        private static int forTestFunction = 11;
      
        private static boolean test(String i)
        {
            if(!(Bukkit.getServer().getWorld("world").getBlockAt(forTestFunction, 32, 704).getType() == Material.COMMAND))
            {
                command("setblock " + forTestFunction +  " 32 704 minecraft:command_block");
                command("setblock " + forTestFunction +  " 32 705 minecraft:unpowered_comparator 2");
                command("setblock " + forTestFunction +  " 32 706 minecraft:redstone_wire");
            }
          
            command("blockdata " + forTestFunction + " 32 704 {Command:\"" + i + "\"}");
            command("setblock "  + forTestFunction + " 33 704 minecraft:stone");
            command("setblock "  + forTestFunction + " 33 704 minecraft:redstone_block");
          
            boolean output = Bukkit.getServer().getWorld("world").getBlockAt(forTestFunction, 32, 706).isBlockPowered();
          
            forTestFunction += 2;
          
            return output;
        }
     
    Last edited: Feb 17, 2015
  8. Offline

    timtower Administrator Administrator Moderator

Thread Status:
Not open for further replies.

Share This Page