Generating a /setblock command using Bukkit

Discussion in 'Plugin Development' started by Digbywood, Apr 6, 2015.

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

    Digbywood

    Hey, Im planning on making a plugin that can generate a vanilla command that can go into a command block easily for Map Makers. I want to see if it is possible to have a player look at an entity or block and type a command then give the player a generated command that if put into a command block with place/summon the block or entity.

    e.g. Lets say you have created a chest with a carrot on a stick in slot 17 and you want to have a vanilla command that if triggered in a command block will place the chest with all the contents in it. The command generated should look something like this:

    /setblock 35 54 109 chest 0 replace {Items:[{id:"carrot_on_a_stick",Damage:0,Count:1,Slot:17,tag:{display:{Name:"Magic Carrot on a Stick",Lore:[]},ench:[{id:34,lvl:1}]}}]}

    If you go on single player and type that command in to a command block and trigger it. It will place a chest at 35 54 109 with a Carrot on a Stick with the Display name as: "Magic Carrot on a Stick".

    Any Good way of generating a command like this in the Bukkit API?
     
  2. Offline

    Zombie_Striker

    Well, you can use the easy way about this and copy a bunch of possible choices into a config and just have a command that can get the command from the config (e.g. /getCommand fill and it will return a generic /fill command that the player can input the data), Or do the very complex thing of adding all the possible things you can do and making a massive plugin to construct the command from scratch.

    I think you were referring to the latter, but unless you really know what you're doing and have the possible hour to input all the data and make an algorithm for constructing it all, I would suggest doing the first.
     
  3. Code:
    /setblock 35 54 109 chest 0 replace {Items:[{id:"carrot_on_a_stick",Damage:0,Count:1,Slot:17,tag:{display:{Name:"Magic Carrot on a Stick",Lore:[]},ench:[{id:34,lvl:1}]}}]}
    Will be something like this:
    1. Set the block of the location to a Chest
    2. Cast the block to a chest
    3. Get the inventory of the chest
    4. Make a new item
    5. Set its displayname
    6. Add the enchantment
    7. Set the item to the inventory at slot 17

    Open this if you can't solve it yourself (open)

    DON'T COPY & PASTE!
    Look at the code a few times and then try it your own.
    Code:java
    1. Location loc = new Location(world, 35, 54, 109);
    2. loc.getBlock().setType(Material.CHEST);
    3. Chest c = (Chest) loc.getBlock();
    4. Inventory inv = c.getBlockInventory();
    5.  
    6. ItemStack item = new ItemStack(Material.CARROT_STICK, 1);
    7. ItemMeta meta = item.getItemMeta();
    8. meta.setDisplayName("Magic Carrot on a Stick");
    9. item.setItemMeta(meta);
    10. item.addEnchantment(Enchantment.DURABILITY, 1);
    11.  
    12. p.getInventory().setItem(17, item);
     
  4. Offline

    Zombie_Striker

    I thought about it some more. You could do something like this
    Code:
    String message = "/setblock ~~X ~~Y ~~Z chest 0 replace {Items:[{id:"~~TY",Damage:~~DAM,Count:~~COUNT,Slot:~~SLOT,tag:{display:{Name:\"~~NAME\",Lore:[]},ench:[{id:~~ENCH,lvl:~~LVL}]}}]}"
    
    message.replaceChars(~~TY, args[0]) (which would be the type)
    message.replaceChars(~~DAM, args[1]) (which would be the Dammage)
    This will continue though all the variables and then you just have this send it to the player.
     
  5. Offline

    Digbywood

    @Zombie_Striker I like your idea about have a String and replacing parts of it to generate the command. It seems like a good way of organising something like this.

    @FisheyLP I don't think you understand my question. What I'm trying to do is generate the command from the chest, not the other way around. So you look a chest, type a command and it will spit out a vanilla command that will generate that chest.

    I will leave this thread open if anybody else has any good ideas for how to do it.
     
Thread Status:
Not open for further replies.

Share This Page