How to summon an iron golem when right clicking chest with specific content.

Discussion in 'Plugin Development' started by CraterHater, Jun 13, 2015.

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

    CraterHater

    I have no idea but how do i summon an iron golem with the custom name of "Guard" when a player right clicks on a chest which holds the following items:

    Nether star named "Golem Soul"
    4 Iron Blocks
    1 pumpkin named "Golem Head"
    1 Diamond sword

    NOTE: the custom named items must be custom named others the golem won't spawn
    It should also remove the chest and despawn its content.
    How to do this, it looks pretty though to me.

    Help would be appreciated and tanks in advance
     
    1. PlayerInteractEvent
    2. Check if block is instanceof Chest
    3. Cast block to chest
    4. Get the inv with chest.getBlockInventory()
    5. Google on ItemMeta and displayNames to solve the item name checking
    6. Remove the chest with block.setType(Material.AIR);
    7. Spawn ironGolem with
    8. Entity irongolem = world.spawnEntity(location, EntityType.IRON_GOLEM);
    9. Set the name with entity.setCustomName("Name");
     
    Xerox262 likes this.
  2. Offline

    CraterHater

    Can you give an example on how to implement all of that, i already did this:
    Code:
    @EventHandler
    public void clickblock(PlayerInteractEvent e) {
        Player p = e.getPlayer();
        if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                Block b = e.getClickedBlock();
                if (b.getType() == Material.CHEST){
    I added some more and it isn't giving any errors but it does not say that the chest has the content once i place one iron block in the chest. The other message does work.
    here's my code:
    Code:
    @EventHandler
    public void clickblock(PlayerInteractEvent e) {
        Player p = e.getPlayer();
        if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                Block b = e.getClickedBlock();
                if (b.getType() == Material.CHEST){
                    Chest chest = (Chest) b.getState();
                    Inventory inv = chest.getInventory();
                     for (ItemStack stack : inv.getContents()) {
                         ItemStack x = new ItemStack (Material.IRON_BLOCK);
                         if (stack == x) {   
                             p.sendMessage("This chest has the content");
                         }else{
                             p.sendMessage("This chest does not have the content...");
                
                         }
                        
                        
                        
                     }
                     }
    
    }
    }
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  3. Try stack.equals(x)
     
  4. Offline

    CraterHater

    How to implement that in my code?
     
  5. Something like this:
    Code:java
    1. int found = 0;
    2. ItemStack netherStar = new ItemStack(Material.NETHER_STAR);
    3. ItemMeta meta = netherStar.getItemMeta();
    4. meta.setDisplayName("Golem Soul");
    5. netherStar.setItemMeta(meta);
    6. ItemStack ironBlocks = new ItemStack(Material.IRON_BLOCK, 4);
    7. //And so on for every item.
    8. // loop through inv contents
    9. //check if item.equals the items above.
    10. //increment "found".
    11. //After the loop, check if "found" >= 4 (because you have 4 different items)
     
  6. Offline

    CraterHater

    And how to put this in the EventHandler, where to place it

    Ow wait i think i am almost there. It gives two errors: 1. yellow line under inv 2. yellow line under ironBlocks
    Code:
    @EventHandler
    public void clickblock(PlayerInteractEvent e) {
        Player p = e.getPlayer();
        if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                Block b = e.getClickedBlock();
                if (b.getType() == Material.CHEST){
                    Chest chest = (Chest) b.getState();
                    Inventory inv = chest.getInventory();
                    int found = 0;
                    ItemStack netherStar = new ItemStack(Material.NETHER_STAR);
                    ItemMeta meta = netherStar.getItemMeta();
                    meta.setDisplayName("Golem Soul");
                    netherStar.setItemMeta(meta);
                    ItemStack ironBlocks = new ItemStack(Material.IRON_BLOCK, 4);
                    if (found >= 4)
                             p.sendMessage("This chest has the content");
                         }else{
                             p.sendMessage("This chest does not have the content...");
                
                         }
                        
                        
                        
                     }
                     }
    
    
    }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  7. Hover over it and it shows you why. Yellow is just a warning. I assume it is: The variable "ironBlocks" is never used
     
  8. Offline

    CraterHater

    Yes indeed. Why is it never used, and for inv the same story
     
  9. because you did nothing with the variable
     
  10. Offline

    CraterHater

    You're code makes it give a message when right clicking any block except a chest, odd... what did i do wrong
    Code:
    @EventHandler
    public void clickblock(PlayerInteractEvent e) {
        Player p = e.getPlayer();
        if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                Block b = e.getClickedBlock();
                if (b.getType() == Material.CHEST){
                    Chest chest = (Chest) b.getState();
                    chest.getInventory();
                    int found = 0;
                    ItemStack netherStar = new ItemStack(Material.NETHER_STAR);
                    ItemMeta meta = netherStar.getItemMeta();
                    meta.setDisplayName("Golem Soul");
                    netherStar.setItemMeta(meta);
                    new ItemStack(Material.IRON_BLOCK, 4);
                    if (found >= 4)
                             p.sendMessage("This chest has the content");
                         }else{
                             p.sendMessage("This chest does not have the content...");
             
                         }
     
  11. Offline

    87pen

    -snip- Read his message wrong, I'm guessing it's sending the message because of placement of brackets. Can't really tell without the whole thing.
     
    Last edited: Jun 14, 2015
  12. Offline

    CraterHater

    I now have this code: Why isn't it working when i place 1 iron block in the chest?
    Code:
    @EventHandler
    public void clickblock(PlayerInteractEvent e) {
        Player p = e.getPlayer();
        if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                Block b = e.getClickedBlock();
                if (b.getType() == Material.CHEST){
                    Chest chest = (Chest) b.getState();
                    Inventory inv = chest.getInventory();
                     for (@SuppressWarnings("unused") ItemStack stack : inv.getContents()) {
                         int found = 0;
                         new ItemStack(Material.IRON_BLOCK, 1);
                         if (found >= 1){
                         //And so on for every item.
                         // loop through inv contents
                         //check if item.equals the items above.
                         //increment "found".
                         //After the loop, check if "found" >= 4 (because you have 4 different items)
                             p.sendMessage("This chest has the content");
                             return;
                         }else{
                             p.sendMessage("This chest does not have the content...");
                             return;
              
                         }
                      
                      
                      
                     }
                     }
    }
    }
    }
    
    I changed my code up and it isn't doing that anymore but now it just says "This chest does not have the content" when it has. the content should be one iron block

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  13. You just copied the "code" without even thinking how you should do it
     
  14. Offline

    CraterHater

    Can you help me with understanding the code? I made a lot of other stuff but nothing like this before.
     
  15. Offline

    87pen

    You would do something like this. But I suggest learning Java before tackling Bukkit.
    Copy and pasting will get you no where, and will just waste time.
    http://bukkit.org/threads/plugin-dev-sticky-learning-java-where-to-learn.350132/

    Code:
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e){
            Player p = e.getPlayer();
            if(e.getAction() == Action.RIGHT_CLICK_BLOCK){
                Block b = e.getClickedBlock();
                if(b.getType().equals(Material.CHEST)){
                    Chest chest = (Chest)b.getState();
                    Inventory ci = chest.getInventory();
                  
                    for(ItemStack i : ci.getContents()){
                        if(i != null && i.equals(new ItemStack(Material.IRON_BLOCK))){
                            p.sendMessage("contains ironblock");
                            p.closeInventory();
                            ci.clear();
                            b.setType(Material.AIR);
                        }
                    }
                }
            }
        }
        
     
  16. I came up with a more simple version of this:
    Code:
    //Define all ItemStacks you need
    if (inv.contains(itemStack1) && inv.contains(itemStack2) && ...
    An example (but with different Items):
    Code:
    ItemStack apple = new ItemStack(Material.APPLE);
    ItemStack goldBlock = new ItemStack(Material.GOLD_BLOCK, 4);
    ItemStack redstoneTorch = new ItemStack(Material.REDSTONE_TORCH, 4);
    if(inv.contains(apple) && inv.contains(goldBlock) && inv.contains(redstoneTorch)) {
     
    87pen likes this.
  17. Offline

    CraterHater

    Ok that works fine, how to summon the iron golem? I got this but that gives errors
    Code:
                       p.sendMessage("contains Golem Soul");
                        p.closeInventory();
                        inv.clear();
                        b.setType(Material.AIR);
                        Location loc = player.getLocation();
                        Entity irongolem = World.spawnEntity(loc, EntityType.IRON_GOLEM);
     
  18. loc.getWorld().spawnEntity
     
Thread Status:
Not open for further replies.

Share This Page