Advertisement
JackOUT

Untitled

Jan 8th, 2022
588
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.98 KB | None | 0 0
  1. package games.coob.core.block;
  2.  
  3. import org.bukkit.Bukkit;
  4. import org.bukkit.Chunk;
  5. import org.bukkit.Location;
  6. import org.bukkit.World;
  7. import org.bukkit.block.Block;
  8. import org.bukkit.block.BlockFace;
  9. import org.bukkit.entity.Arrow;
  10. import org.bukkit.entity.Player;
  11. import org.bukkit.scheduler.BukkitRunnable;
  12. import org.bukkit.util.Vector;
  13. import org.mineacademy.fo.EntityUtil;
  14. import org.mineacademy.fo.RandomUtil;
  15. import org.mineacademy.fo.remain.CompMaterial;
  16.  
  17. public class BlockTask extends BukkitRunnable {
  18.  
  19.     // pick a random location
  20.     // call a method for it
  21.     // e.g. > crops grow FASTER than in normal MC
  22.  
  23.     @Override
  24.     public void run() {
  25.         //System.out.println("Task running");
  26.  
  27.         // Homework : create a two-phased system
  28.         // Project : shooting blocks
  29.  
  30.         final BlockRegistry blockRegistry = BlockRegistry.getInstance();
  31.  
  32.         for (final World world : Bukkit.getWorlds())
  33.             for (final Chunk chunk : world.getLoadedChunks()) {
  34.                 final Block randomBlock = chunk.getBlock(
  35.                         RandomUtil.nextBetween(0, 15),
  36.                         RandomUtil.nextBetween(0, world.getMaxHeight() - 1),
  37.                         RandomUtil.nextBetween(0, 15));
  38.  
  39.                 //System.out.println("Ticking " + randomBlock);
  40.  
  41.                 /*if (!BlockVisualizer.isVisualized(randomBlock))
  42.                     BlockVisualizer.visualize(randomBlock, CompMaterial.BEACON, "Ticked Right Now");
  43.  
  44.                 Common.runLater(20, () -> {
  45.                     if (BlockVisualizer.isVisualized(randomBlock))
  46.                         BlockVisualizer.stopVisualizing(randomBlock);
  47.                 });*/
  48.  
  49.                 if (blockRegistry.isRegistered(randomBlock)) {
  50.                     final Block blockAbove = randomBlock.getRelative(BlockFace.UP);
  51.  
  52.                     if (randomBlock.getType() == CompMaterial.FARMLAND.getMaterial() & blockAbove.getType() == CompMaterial.WHEAT.getMaterial()) {
  53.                         if (blockAbove.getBlockData() instanceof final org.bukkit.block.data.Ageable ageable) {
  54.                             ageable.setAge(ageable.getMaximumAge());
  55.                             blockAbove.setBlockData(ageable);
  56.                         }
  57.                     }
  58.                 }
  59.             }
  60.  
  61.         // Get the block level
  62.         // If level 1 : shoot an arrow to the nearest player
  63.         // Level 2 : shoot a poisonness arrow to the nearest player
  64.         for (final Location location : blockRegistry.getLocations()) {
  65.             final Block block = location.getBlock();
  66.  
  67.             if (block.getType() == CompMaterial.DIAMOND_BLOCK.getMaterial()) {
  68.                 if (blockRegistry.getLevel(block) == 1) {
  69.                     final Player closestPlayer = EntityUtil.findNearestEntity(location, 10, Player.class);
  70.  
  71.                     shootArrow(closestPlayer, block);
  72.                 }
  73.             }
  74.         }
  75.     }
  76.  
  77.  
  78.     public void shootArrow(final Player target, final Block block) {
  79.         if (target != null) {
  80.             final Location location = block.getLocation();
  81.  
  82.             location.setY(location.getY() + 1.2);
  83.             location.setX(location.getX() + 0.5);
  84.             location.setZ(location.getZ() + 0.5);
  85.  
  86.             final Vector vector = target.getLocation().subtract(block.getLocation()).toVector().normalize();
  87.             final Arrow arrow = block.getWorld().spawnArrow(location, vector, 1, 0);
  88.  
  89.             arrow.setVelocity(vector.multiply(2).add(new Vector(0, 0.1, 0)));
  90.         }
  91.     }
  92. }
  93.  
  94.  
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement