Limit entity & block
unknown
java
2 years ago
5.6 kB
3
Indexable
Never
@SubscribeEvent public void onChunkHandle(TickEvent.WorldTickEvent event) { WorldServer worldServer = (WorldServer) event.world; //Loop all loaded chunks for (Chunk chunk : worldServer.getChunkProvider().getLoadedChunks()) { int count = 0; /* Get all entities inside chunk */ ClassInheritanceMultiMap<Entity>[] entities = chunk.getEntityLists(); if (entities.length < 1) continue; for (ClassInheritanceMultiMap<Entity> list : entities) { if (list.isEmpty()) continue; for (Entity entity : list.stream().filter(new Predicate<Entity>() { @Override public boolean test(Entity entity) { EntityEntry registry = EntityRegistry.getEntry(entity.getClass()); if (registry == null) return false; List<String> entities = config.getStringList("entities", "index"); if (entities.isEmpty()) return false; ResourceLocation resource = registry.getRegistryName(); if (resource == null) return false; return entities.contains(resource.toString()); } }).collect(Collectors.toList())) { int amount = config.getInt("entities", "amount"); if (count >= amount) { entity.setDead(); continue; } count++; } } } } @SubscribeEvent public void onPlayerInteract(PlayerInteractEvent.RightClickBlock event) { EntityPlayer player = event.getEntityPlayer(); World world = event.getWorld(); ItemStack item = player.getHeldItem(event.getHand()); if (item == ItemStack.EMPTY || item.getItem() == Items.AIR) return; //Get blocks that will be limited List<String> blocks = config.getStringList("blocks", "index"); if (blocks.isEmpty()) return; ResourceLocation itemResource = Item.REGISTRY.getNameForObject(item.getItem()); //Check if item in hand is checkable if (itemResource == null || !blocks.contains(itemResource.toString())) return; Chunk chunk = world.getChunk(player.getPosition()); //Get player chunk List<Block> check = Lists.newArrayList(); for (int x = 0; x < 16; x++) { for (int z = 0; z < 16; z++) { for (int y = 0; y < 257; y++) { //Get block state at position IBlockState blockState = chunk.getBlockState(chunk.getPos().getBlock(x, y, z)); Block block = blockState.getBlock(); if (blockState.getMaterial() == Material.AIR) continue; String name = ""; /* Use to check redstone repeater and comparator is item block but didn't work */ Item itemBlock = Item.getItemFromBlock(block); player.sendMessage(new TextComponentString("itemBlock: " + (itemBlock == Items.AIR ? "Air" : "No air"))); // Debug if (itemBlock == Items.AIR) //If block is not item block then get block name from registry name = Block.REGISTRY.getNameForObject(block).toString(); else { //If item block is exist then get its name //Detected redstone torch but not repeater and comparator ResourceLocation resource = itemBlock.getRegistryName(); if (resource != null) name = resource.toString(); } //Debug player.sendMessage(new TextComponentString("Name: " + (name.isEmpty() ? "Null" : name))); if (name.isEmpty()) continue; //Check if block is in limit list if (blocks.contains(name) && itemResource.toString().equalsIgnoreCase(name)) check.add(block); } } } player.sendMessage(new TextComponentString("Collected blocks: " + check.size())); if (check.isEmpty()) return; /* Get total block is placeable in chunk if the next placed block is greater than amount in config then event is cancelled */ int amount = config.getInt("blocks", "amount"); player.sendMessage(new TextComponentString("Amount: " + amount)); if (check.size() < amount) return; event.setCanceled(true); } }