How I can Check if a Player is in my Region and can build/destroy?! Need help! Thx!

Discussion in 'Bukkit Help' started by NiharPlay3r, Jul 23, 2017.

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

    NiharPlay3r

    Hello guys!
    My problem:

    I have a House Plugin, and I want check the Region and If a user bought this Region he should use the region, like build a wall oder destroy anything.

    But If I want check the Region and the Player UUID it doesn t work!

    Sorry for my bad english!

    * I m a new developer, please don t drop any professionel stuff only the fix! *

    Here my Codes:

    THIS IS THE FILEMANAGER CLASS:


    PHP:
    package de.haus.extras;

    import java.io.File;
    import java.io.IOException;
    import java.util.UUID;

    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;

    public class 
    FileManager
    {
        static 
    File file = new File("plugins//Haus//Häuser.yml");
        public static 
    FileConfiguration cfg YamlConfiguration.loadConfiguration(file);

        public 
    Object region;
        public 
    Integer id;
       
        public 
    void savecfg()
        {
            try
            {
                
    cfg.save(file);
            }
            catch (
    IOException e)
            {
            }
        }
       
        public 
    boolean addReagion(Player pLocation loc1Location loc2)
        {
            
    int id cfg.getKeys(false).size();
           
            
    String niemand "niemand";
           
            
    cfg.set(id ".Spieler.name"niemand.toString());
            
    cfg.set(id ".UUID.name"niemand.toString());
            
    cfg.set(id ".pos1.Welt"loc1.getWorld().getName());
            
    cfg.set(id ".pos1.x"loc1.getBlockX());
            
    cfg.set(id ".pos1.y"loc1.getBlockY());
            
    cfg.set(id ".pos1.z"loc1.getBlockZ());
           
            
    cfg.set(id ".pos2.Welt"loc2.getWorld().getName());
            
    cfg.set(id ".pos2.x"loc2.getBlockX());
            
    cfg.set(id ".pos2.y"loc2.getBlockY());
            
    cfg.set(id ".pos2.z"loc2.getBlockZ());
           
            
    savecfg();
           
            
    p.sendMessage("§7•••••••••••••••••••••••••••••••••••");
            
    p.sendMessage("");
            
    p.sendMessage("§8[ §2§l✔ Haus Erstellt. §8]");
            
    p.sendMessage("");
            
    p.sendMessage("§cHaus Nr:§7 " id);
            
    p.sendMessage("§cStatus:§7 Nicht verkauft");
            
    p.sendMessage("");
            
    p.sendMessage("§7•••••••••••••••••••••••••••••••••••");
           
            return 
    false;
        }
       
        public 
    void registerRegions()
        {
            for(
    int i 0cfg.getKeys(false).size(); i++)
            {
                
    de.haus.extras.Tool.region.add(new Cuboid(getLoc(i"pos1"), getLoc(i"pos2")));
            }
        }


       
        public 
    Location getLoc(int idString name)
        {
            
    World w Bukkit.getWorld(cfg.getString(id"."+name+".Welt"));
            return new 
    Location(wcfg.getInt(id"."+name+".x"), cfg.getInt(id"."+name+".y"), cfg.getInt(id"."+name+".z"));
        }
       
        public 
    void getPlayer(Player pint idUUID name)
        {
            
    cfg.get(id"."+name.toString()+".name");
            
    cfg.get(id"."+p.getName()+".Spieler.name");
        }
    }

    Code:
    package de.haus.extras;
    
    import java.io.File;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.UUID;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.block.Block;
    import org.bukkit.configuration.MemorySection;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.block.BlockPlaceEvent;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.Plugin;
    
    public class Tool implements Listener
    {
        static public ArrayList<Cuboid> region = new ArrayList<Cuboid>();
        static public ArrayList<String> move = new ArrayList<String>();
        static public ArrayList<String> tool = new ArrayList<String>();
       
        public HashMap<String, Location> loc1 = new HashMap<>();
    
        public HashMap<Player, Location> pos1 = new HashMap<Player, Location>();
        public HashMap<Player, Location> pos2 = new HashMap<Player, Location>();
    
        public static boolean isInRegion(Location loc)
        {
            for(Cuboid c : region)
            {
                if(c.contains(loc))
                {
                    return true;
                }
            }
            return false;
           
        }
       
        @EventHandler
        public void onBreak(BlockBreakEvent e)
        {
            Player p = (Player)e.getPlayer();
            e.setCancelled(false);
           
            if(isInRegion(e.getBlock().getLocation()))
            {
                for(String region : de.haus.main.main.fm.cfg.getConfigurationSection("").getKeys(false))
                {
                    int regio = Integer.valueOf(region);
                   
                    String owner = de.haus.main.main.fm.cfg.getString(region + ".UUID.name");
                    if(!owner.equalsIgnoreCase(p.getUniqueId().toString()))
                    {
                        if(!p.isOp())
                        {
                            p.sendMessage("§8[§cHaus§8]§c Hier für hast du keine Berechtigung.");
                            e.setCancelled(true);
                            return;
                        }
                    }
                }
            }
        }
       
        @EventHandler
        public void onPlace(BlockPlaceEvent e)
        {
            Player p = (Player)e.getPlayer();
           
            if(isInRegion(p.getLocation()))
            {
                for(String region : de.haus.main.main.fm.cfg.getConfigurationSection("").getKeys(false))
                {
                    Integer g = Integer.valueOf(region);
                   
                    String owner = de.haus.main.main.fm.cfg.getString(region + ".UUID.name");
                   
                    if(!owner.equalsIgnoreCase(p.getUniqueId().toString()))
                    {
                        e.setCancelled(true);
                    }
                }
            }
        }
    }
    
     
  2. Offline

    Machine Maker

    @NiharPlay3r
    1. Its unnessecary to case a player object to a player object
    2. Did you really make your own region utility? I would just use worldedit/worldguard as they are probably more in depth (I can't tell how complex yours is becuase its not here).

    3. You might be comparing the owner name ie: Notch to his uuid which would never be true
    If the String owner is his name then the following will always be false
     
  3. Offline

    NiharPlay3r

    Okay, thanks for your anwser. I will test it with your tipps. And i dont want use world edit or World guard.
     
Thread Status:
Not open for further replies.

Share This Page