Private furnace and private brewing stand

Discussion in 'Spigot Plugin Development' started by DatsMaboi, May 29, 2018.

  1. Hello there basically what i want is to make it so only the person that placed a furnace or brewing stand can use it or break it.

    how can this be done? can someone point me in the right direction please.

    i would also like it if i could have a 1 block radius of if you can place another furnace or brewing stand

    I WANT THE PROTECTED BLOCKS TO BE RESET WHEN SERVER STOPS
    so i do not think i need a config.

    what i have tried
    Code (Java):
    public void setBlockOwner(final Block b, final UUID player) {
            MetadataValue val = (MetadataValue)new FixedMetadataValue((Plugin)AnnihilationMain.getInstance(), (Object)player.toString());
            b.setMetadata("Owner", val);
        }
     
        public UUID getBlocksOwner(final Block b) {
            List<MetadataValue> list = (List<MetadataValue>)b.getMetadata("Owner");
            if (list == null || list.isEmpty()) {
                return null;
            }
            return UUID.fromString(list.get(0).asString());
        }
     
        @EventHandler
        public void onBlockPlace(BlockPlaceEvent e) {  
         
        Player p = e.getPlayer();
        Block pblock = e.getBlock();
     
        if(pblock.getType() == Material.FURNACE || pblock.getType() == Material.BURNING_FURNACE || pblock.getType() == Material.BREWING_STAND) {
            this.setBlockOwner(pblock, p.getUniqueId());
            int radius = 1;
            for (int x = radius; x >= -radius; x--) {
                for (int y = radius; y >= -radius; y--) {
                    for (int z = radius; z >= -radius; z--) {
                        Block f = pblock.getRelative(x, y, z);
                        if (f.getType() == Material.FURNACE || f.getType() == Material.BURNING_FURNACE || f.getType() == Material.BREWING_STAND) {
                            if(y == 0 && x == 0 && z == 0) {
                                continue;
                            }
                         
                                e.setCancelled(true);
                                p.sendMessage(ChatColor.RED + "You may not place a furnace or brewing stand here!");
                                break;
                             
                        }
                    }
                }
            }
        }
         
        }
     
        @EventHandler
        public void onBlockUse(PlayerInteractEvent e) {
         
            Block pblock = e.getClickedBlock();
            UUID p = e.getPlayer().getUniqueId();
         
            if(e.getAction() != null || e.getAction() == Action.RIGHT_CLICK_BLOCK) {
            if(pblock.getType() == Material.FURNACE || pblock.getType() == Material.BURNING_FURNACE || pblock.getType() == Material.BREWING_STAND) {
                UUID owner = this.getBlocksOwner(pblock);
                if(owner != null && owner != p) {
                    e.setCancelled(true);
                    e.getPlayer().sendMessage(ChatColor.RED + "You do not own this block!");
                 
                }
            }
            }
        }
     
        @EventHandler
        public void onBlockBreak(BlockBreakEvent e) {
         
            Block pblock = e.getBlock();
            UUID p = e.getPlayer().getUniqueId();
         
            if(pblock.getType() == Material.FURNACE || pblock.getType() == Material.BURNING_FURNACE || pblock.getType() == Material.BREWING_STAND) {
                UUID owner = this.getBlocksOwner(pblock);
                if(owner != null && owner != p) {
                    e.setCancelled(true);
                    e.getPlayer().sendMessage(ChatColor.RED + "You do not own this block!");
                 
                }
             
            }
         
        }
         
       
    Everything works well bt the blockuse and blockbreak event
     
    #1 DatsMaboi, May 29, 2018
    Last edited: May 29, 2018
  2. Use the BlockPlaceEvent and a Config.

    Convert the Location of the placed Block to a String (like: x;y;z) and use this as Path for the UUID of the Player
     
  3. ok but can't i just check for the owner of the block?
     
    • Agree Agree x 1
  4. i think im on to something hopefully this works

    Code (Java):
    public void setBlockOwner(final Block b, final UUID player) {
            MetadataValue val = (MetadataValue)new FixedMetadataValue((Plugin)AnnihilationMain.getInstance(), (Object)player.toString());
            b.setMetadata("Owner", val);
        }
       
        public UUID getBlocksOwner(final Block b) {
            List<MetadataValue> list = (List<MetadataValue>)b.getMetadata("Owner");
            if (list == null || list.isEmpty()) {
                return null;
            }
            return UUID.fromString(list.get(0).asString());
        }
       
        @EventHandler
        public void onBlockPlace(BlockPlaceEvent e) {  
           
        Player p = e.getPlayer();
        Block pblock = e.getBlock();
           
        if(pblock != null && pblock.getType() == Material.FURNACE || pblock.getType() == Material.BURNING_FURNACE || pblock.getType() == Material.BREWING_STAND) {
            int radius = 1;
            for (int x = radius; x >= -radius; x--) {
                for (int y = radius; y >= -radius; y--) {
                    for (int z = radius; z >= -radius; z--) {
                        Block f = pblock.getRelative(x, y, z);
                        if (f.getType() != null && f.getType() == Material.FURNACE || f.getType() == Material.BURNING_FURNACE || f.getType() == Material.BREWING_STAND) {
                           
                            e.setCancelled(true);
                            p.sendMessage(ChatColor.RED + "You may not place a furnace or brewing stand here!");
                           
                        }
                        }
                    }
                }
            }
           
        }
       
        @EventHandler
        public void onBlockUse(PlayerInteractEvent e) {
           
            Block pblock = e.getClickedBlock();
            UUID p = e.getPlayer().getUniqueId();
           
            if(pblock != null && pblock.getType() == Material.FURNACE || pblock.getType() == Material.BURNING_FURNACE || pblock.getType() == Material.BREWING_STAND) {
                UUID owner = this.getBlocksOwner(e.getClickedBlock());
                if(owner != null && owner != p) {
                    e.setCancelled(true);
                    e.getPlayer().sendMessage(ChatColor.RED + "You do not own this block!");
                   
                }
               
            }
        }
       
        @EventHandler
        public void onBlockBreak(BlockBreakEvent e) {
           
            Block pblock = e.getBlock();
            UUID p = e.getPlayer().getUniqueId();
           
            if(pblock != null && pblock.getType() == Material.FURNACE || pblock.getType() == Material.BURNING_FURNACE || pblock.getType() == Material.BREWING_STAND) {
                UUID owner = this.getBlocksOwner(e.getBlock());
                if(owner != null && owner != p) {
                    e.setCancelled(true);
                    e.getPlayer().sendMessage(ChatColor.RED + "You do not own this block!");
                   
                }
               
            }
           
        }
     
  5. A Block doesn't have an Owner in vanilla. You must save the coordinates with the UUID of the placer in a Config and Check if the players UUID is similar to the saved UUID
     
  6. Ok, then I have learned something new:ROFLMAO:
     
  7. Nvm my code did not work
    i dont think i need to use a config because i want it so that all protected stuff get reset when server stops
    (its a mini game)
     
  8. bump anybody got any help please
     
  9. Use the blocks metadata to store the players UUID. Then on break check if the broken blocks metadata contains the players uuid
     
  10. how would id o that?
     
  11. ok i tried this
    Code (Java):
    public void setBlockOwner(Block b, String name)
        {
            b.setMetadata("owner", new FixedMetadataValue(Main.getInstance(),name));
        }
        public String getBlockOwner(Block b)
        {
            String owner = "";
            List<MetadataValue> metas = b.getMetadata("owner");
            for(MetadataValue meta : metas)
            {
                if(meta.getOwningPlugin().getName().equals(Main.getInstance().getName()))
                {
                    owner = meta.asString();
                }
            }
            return owner;
        }
       
        @EventHandler
        public void onBlockPlace(BlockPlaceEvent e) {  
           
        Player p = e.getPlayer();
        Block pblock = e.getBlock();
       
        setBlockOwner(pblock, p.getName());
       
        if(pblock.getType() == Material.FURNACE || pblock.getType() == Material.BURNING_FURNACE || pblock.getType() == Material.BREWING_STAND) {
            int radius = 1;
            for (int x = radius; x >= -radius; x--) {
                for (int y = radius; y >= -radius; y--) {
                    for (int z = radius; z >= -radius; z--) {
                        Block f = pblock.getRelative(x, y, z);
                        if (f.getType() == Material.FURNACE || f.getType() == Material.BURNING_FURNACE || f.getType() == Material.BREWING_STAND) {
                            if(y == 0 && x == 0 && z == 0) {
                                continue;
                            }
                           
                                e.setCancelled(true);
                                p.sendMessage(ChatColor.RED + "You may not place a furnace or brewing stand here!");
                                break;
                               
                        }
                    }
                }
            }
    However nothing appears to happen
        }
           
        }
       
        @EventHandler
        public void onBlockUse(PlayerInteractEvent e) {
           
            Block pblock = e.getClickedBlock();
            String p = e.getPlayer().getName();
           
            if(e.getClickedBlock() != null && e.getAction() == Action.RIGHT_CLICK_BLOCK) {
            if(pblock.getType() == Material.FURNACE || pblock.getType() == Material.BURNING_FURNACE || pblock.getType() == Material.BREWING_STAND) {
            //    if(getBlockOwner(pblock) != null) {
                    if(!p.equals(getBlockOwner(pblock))) {
                    e.setCancelled(true);
                    e.getPlayer().sendMessage(ChatColor.RED + "You do not own this block!");
                   
                }
                }
            }
        //    }
        }
       
        @EventHandler
        public void onBlockBreak(BlockBreakEvent e) {
           
            Block pblock = e.getBlock();
            String p = e.getPlayer().getName();
           
            if(pblock.getType() == Material.FURNACE || pblock.getType() == Material.BURNING_FURNACE || pblock.getType() == Material.BREWING_STAND) {
                if(getBlockOwner(pblock) != null) {
                    if(!p.equals(getBlockOwner(pblock))) {
                        e.setCancelled(true);
                        e.getPlayer().sendMessage(ChatColor.RED + "You do not own this block!");
                   
                }
                }
               
            }
           
        }
     
  12. Anyone got a fix?
     
  13. Try to use a simple hashmap instead. Like <Block/Location, UUID>. Then use if(!hashmap.getOrDefault(block, null).equals(playerUUID)) -> not the player's block.
     
  14. add debug messages to your code to see what code is getting ran and what the vaues of your code is at certain points . this will give you a better understanding of whats going on.