EffectAirBlockChanger.java 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package me.km.effects;
  2. import me.km.KajetansMod;
  3. import java.util.ArrayList;
  4. import java.util.function.Consumer;
  5. import me.km.api.Location;
  6. import net.minecraft.block.state.IBlockState;
  7. import net.minecraft.util.math.BlockPos;
  8. import net.minecraft.world.World;
  9. public class EffectAirBlockChanger
  10. {
  11. private final World w;
  12. private final ArrayList<ChangeBlock> list;
  13. public EffectAirBlockChanger(World w)
  14. {
  15. this.w = w;
  16. list = new ArrayList<>();
  17. }
  18. public void addBlock(BlockPos pos)
  19. {
  20. if(!w.isAirBlock(pos) && w.getTileEntity(pos) == null)
  21. {
  22. list.add(new ChangeBlock(pos));
  23. }
  24. }
  25. public void run(int clean)
  26. {
  27. list.forEach(c -> c.run());
  28. KajetansMod.scheduler.scheduleTask(() -> list.forEach(c -> c.clear()), clean);
  29. }
  30. public World getWorld()
  31. {
  32. return w;
  33. }
  34. public void forEachLocation(Consumer<? super Location> c)
  35. {
  36. list.stream().map(l -> new Location(w, l.pos)).forEach(c);
  37. }
  38. private class ChangeBlock
  39. {
  40. private final BlockPos pos;
  41. private final IBlockState state;
  42. public ChangeBlock(BlockPos pos)
  43. {
  44. this.pos = pos;
  45. this.state = w.getBlockState(pos);
  46. }
  47. public void run()
  48. {
  49. w.setBlockToAir(pos);
  50. }
  51. public void clear()
  52. {
  53. w.setBlockState(pos, state);
  54. }
  55. }
  56. }