Hey all! So, Im trying to make it so only a certain amount of locations can be in a chunk. I removed most of the code, and only left the meaty parts of it so you don't need to look through all of it for 10 min. Code (Text): public ArrayList < Location > GeneratorLocations = new ArrayList < Location > (); @EventHandler public void onsSpawnerPlace(BlockPlaceEvent event) { Player player = event.getPlayer(); if (event.getBlock().getType() == Material.IRON_BLOCK && player.getInventory().getItemInHand().getItemMeta().getDisplayName().equals("Generator!")) { //there would be a if statement checking if there more then, // lets say 10 gens in one chunk, and if so, return it. GeneratorLocations.add(event.getBlock().getLocation()); } } So here is what I have. Additions/and or tips and greatly welcomed!
I have that, I just didn't feel people would need to know for the problem, but thanks for telling me!
Blocks have item meta :O? oh nvm, that was the player's item in hand :3 ----------- Maybe check if there are multiple iron blocks in the chunk. Check each location in the chunk with for loops, and check if the block is an iron block.
I got this, based off other posts with a similar problem: Code (Text): public ArrayList < Location > GeneratorLocations = new ArrayList < Location > (); int amountofgenchunk = 0; @EventHandler public void onBlockPlace(BlockPlaceEvent event) { Chunk chunk = player.getLocation().getChunk(); for (int x = 0; x <= 15; x++) { for (int y = 0; y <= 256; y++) { for (int z = 0; z <= 15; z++) { for (Location location1: GeneratorLocations) { if (chunk.getBlock(x, y, z).getLocation().equals(location1)) { amountofgenchunk++; } } } } } }
Code (Text): private Set<Location> generators = new HashSet<>(); @EventHandler public void onBlockPlace(BlockPlaceEvent event) { Block toPlace = event.getBlock(); Player player = event.getPlayer(); if (toPlace.getType() != Material.IRON_BLOCK || !player.getInventory().getItemInHand().getItemMeta().getDisplayName().equals("Generator!")) { return; } Chunk chunk = toPlace.getLocation().getChunk(); int count = 0; x: for (int x = 0; x <= 15; x++) { for (int y = 0; y <= 256; y++) { for (int z = 0; z <= 15; z++) { Block b = chunk.getBlock(x, y, z); if (b.getType() == Material.IRON_BLOCK && generators.contains(b.getLocation())) { count ++; if (count >= 10) { // Found 10 generators, dont need to check anymore. break x; } } } } } if (count >= 10) { player.sendMessage("10 generators"); event.setCancelled(true); } else { generators.add(toPlace.getLocation()); } } p.s: ArrayList is not recommended for this case.
Thanks @DevLeeo ! This worked perfectly. For anyone looking for this also: Code (Text): private Set<Location> generators = new HashSet<>(); @EventHandler public void onBlockPlace(BlockPlaceEvent event) { Block toPlace = event.getBlock(); Player player = event.getPlayer(); if (toPlace.getType() != Material.IRON_BLOCK || !player.getInventory().getItemInHand().getItemMeta().getDisplayName().equals("Generator!")) { return; } Chunk chunk = toPlace.getLocation().getChunk(); int count = 0; x: for (int x = 0; x <= 15; x++) { for (int y = 0; y <= 256; y++) { for (int z = 0; z <= 15; z++) { Block b = chunk.getBlock(x, y, z); if (b.getType() == Material.IRON_BLOCK && generators.contains(b.getLocation())) { count ++; if (count >= 10) { // Found 10 generators, dont need to check anymore. break x; } } } } } if (count >= 10) { player.sendMessage("10 generators"); event.setCancelled(true); } else { generators.add(toPlace.getLocation()); } } SOLVED PS: I know this is a copy of DevLeoo code(Thanks once again!), but most people just scroll down to the bottom, and if they don't see code, they find a new post.