package de.jottyfan.quickiemod.block; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Random; import java.util.Set; import net.minecraft.block.AbstractBlock; import net.minecraft.block.Block; import net.minecraft.block.BlockState; import net.minecraft.block.Blocks; import net.minecraft.entity.ExperienceOrbEntity; import net.minecraft.entity.LivingEntity; import net.minecraft.item.ItemStack; import net.minecraft.loot.context.LootWorldContext.Builder; import net.minecraft.registry.RegistryKey; import net.minecraft.registry.RegistryKeys; import net.minecraft.server.world.ServerWorld; import net.minecraft.util.Identifier; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import net.minecraft.world.tick.OrderedTick; /** * * @author jotty * */ public class BlockEmptyLavahoarder extends Block { public BlockEmptyLavahoarder(Identifier identifier) { super(AbstractBlock.Settings.create().hardness(2.5f).registryKey(RegistryKey.of(RegistryKeys.BLOCK, identifier))); } @Override public List getDroppedStacks(BlockState state, Builder builder) { List list = new ArrayList<>(); list.add(new ItemStack(ModBlocks.BLOCK_EMPTYLAVAHOARDER)); return list; } @Override protected void scheduledTick(BlockState state, ServerWorld world, BlockPos pos, net.minecraft.util.math.random.Random random) { boolean found = BlockLavahoarder.suckLava(world, pos.north()); found = found || BlockLavahoarder.suckLava(world, pos.south()); found = found || BlockLavahoarder.suckLava(world, pos.east()); found = found || BlockLavahoarder.suckLava(world, pos.west()); found = found || BlockLavahoarder.suckLava(world, pos.up()); found = found || BlockLavahoarder.suckLava(world, pos.down()); if (found) { world.setBlockState(pos, ModBlocks.BLOCK_LAVAHOARDER.getDefaultState()); world.getBlockTickScheduler().scheduleTick(OrderedTick.create(ModBlocks.BLOCK_LAVAHOARDER, pos)); } else { world.getBlockTickScheduler().scheduleTick(OrderedTick.create(this, pos)); } } private static final String stringOf(BlockPos pos) { StringBuilder buf = new StringBuilder(); buf.append(pos.getX()).append(":"); buf.append(pos.getY()).append(":"); buf.append(pos.getZ()); return buf.toString(); } private static final BlockPos blockPosOf(String s) { if (s.contains(":")) { String[] parts = s.split(":"); if (parts.length > 2) { Integer x = Integer.valueOf(parts[0]); Integer y = Integer.valueOf(parts[1]); Integer z = Integer.valueOf(parts[2]); return new BlockPos(x, y, z); } else { return null; } } else { return null; } } private void findAllAttachedLavaBlocks(Set list, BlockPos pos, World world, Integer counter) { if (counter < 1) { return; } else if (Blocks.LAVA.equals(world.getBlockState(pos).getBlock())) { String p = stringOf(pos); if (!list.contains(p)) { list.add(p); findAllAttachedLavaBlocks(list, pos.up(), world, counter - 1); findAllAttachedLavaBlocks(list, pos.down(), world, counter - 1); findAllAttachedLavaBlocks(list, pos.north(), world, counter - 1); findAllAttachedLavaBlocks(list, pos.south(), world, counter - 1); findAllAttachedLavaBlocks(list, pos.east(), world, counter - 1); findAllAttachedLavaBlocks(list, pos.west(), world, counter - 1); } } } @Override public void onPlaced(World world, BlockPos pos, BlockState state, LivingEntity placer, ItemStack stack) { Set positions = new HashSet<>(); Integer counter = 8; // TODO: make it level up - able findAllAttachedLavaBlocks(positions, pos.up(), world, counter); findAllAttachedLavaBlocks(positions, pos.down(), world, counter); findAllAttachedLavaBlocks(positions, pos.north(), world, counter); findAllAttachedLavaBlocks(positions, pos.south(), world, counter); findAllAttachedLavaBlocks(positions, pos.east(), world, counter); findAllAttachedLavaBlocks(positions, pos.west(), world, counter); Integer amount = positions.size(); for (String p : positions) { world.setBlockState(blockPosOf(p), Blocks.AIR.getDefaultState()); } if (amount > 0) { world.setBlockState(pos, ModBlocks.BLOCK_LAVAHOARDER.getDefaultState()); world.getBlockTickScheduler().scheduleTick(OrderedTick.create(ModBlocks.BLOCK_LAVAHOARDER, pos)); int count = 0; Random random = new Random(); for (int i = 0; i < amount; i++) { if (random.nextFloat() < 0.0125) { count++; } } BlockPos up = pos.up(); if (count > 0) { BlockLavahoarder.spawnRandomItems(world, up, count); world.spawnEntity(new ExperienceOrbEntity(world, (double) pos.getX() + 0.5D, (double) pos.getY() + 0.5D, (double) pos.getZ() + 0.5D, count)); } } else { world.getBlockTickScheduler().scheduleTick(OrderedTick.create(this, pos)); } } }