Advertisement
LampBoy

Area dot java

May 25th, 2024
507
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.28 KB | Software | 0 0
  1. interface FunctionalInterface {
  2.     void areaFunction(ArrayList list, int x, int y, int z);
  3. }
  4.  
  5. public class Area {
  6.     private final UUID ownerUuid;
  7.     private final BlockPos startPos;
  8.     private final BlockPos endPos;
  9.     private final World world;
  10.     private final String name;
  11.     private final UUID uuid;
  12.  
  13.     public Area(BlockPos startPos, BlockPos endPos, String name, UUID ownerUuid) {
  14.         this(startPos, endPos, name, UUID.randomUUID(), ownerUuid);
  15.     }
  16.  
  17.     public Area(BlockPos startPos, BlockPos endPos, String name, UUID uuid, UUID ownerUuid) {
  18.         this.startPos = startPos;
  19.         this.endPos = endPos;
  20.         this.world = MinecraftClient.getInstance().world;
  21.         this.name = name;
  22.         this.uuid = uuid;
  23.         this.ownerUuid = ownerUuid;
  24.     }
  25.  
  26.     public ArrayList<Block> getBlocks() {
  27.         ArrayList<Block> blockList = this.executeInArea((list, x, y, z) -> {
  28.             list.add(world.getBlockState(new BlockPos(x, y, z)).getBlock());
  29.         });
  30.         return blockList;
  31.     }
  32.  
  33.     public ArrayList<LivingEntity> getDetectedEntities() {
  34.         ArrayList<LivingEntity> entityList = this.executeInArea((list, x, y, z) -> {
  35.             world.getNonSpectatingEntities(LivingEntity.class, new Box(new BlockPos(x, y, z))).forEach(value -> list.add(value));
  36.         });
  37.         // Removes duplicates from the list
  38.         HashSet<LivingEntity> entitySet = new HashSet<>();
  39.         entityList.forEach(value -> entitySet.add(value));
  40.         entityList.clear();
  41.         entitySet.forEach(value -> entityList.add(value));
  42.         return entityList;
  43.     }
  44.  
  45.     public <T> ArrayList<T> executeInArea(FunctionalInterface function) {
  46.         ArrayList<T> list = new ArrayList<>();
  47.         int x;
  48.         int y;
  49.         int z;
  50.  
  51.         x = startPos.getX();
  52.         while (true) {
  53.             y = startPos.getY();
  54.             while (true) {
  55.                 z = startPos.getZ();
  56.                 while (true) {
  57.                     if (z < endPos.getZ()) {
  58.                         // Runs lambda expressions. defined in FuncitionalInterface.
  59.                         function.areaFunction(list, x, y, z);
  60.                         z++;
  61.                     } else if (z > endPos.getZ()) {
  62.                         function.areaFunction(list, x, y, z);
  63.                         z--;
  64.                     } else {
  65.                         function.areaFunction(list, x, y, z);
  66.                         break;
  67.                     }
  68.                 }
  69.                 if (y < endPos.getY()) {
  70.                     y++;
  71.                 } else if (y > endPos.getY()) {
  72.                     y--;
  73.                 } else {
  74.                     break;
  75.                 }
  76.             }
  77.             if (x < endPos.getX()) {
  78.                 x++;
  79.             } else if (x > endPos.getX()) {
  80.                 x--;
  81.             } else {
  82.                 break;
  83.             }
  84.         }
  85.         return list;
  86.     }
  87.  
  88.     public static NbtElement toNbt(Area area) {
  89.         NbtCompound nbt = new NbtCompound();
  90.         nbt.put("startPos", NbtHelper.fromBlockPos(area.getStartPos()));
  91.         nbt.put("endPos", NbtHelper.fromBlockPos(area.getEndPos()));
  92.         nbt.putString("name", area.getName());
  93.         nbt.putUuid("uuid", area.getUuid());
  94.         return nbt;
  95.     }
  96.  
  97.     public static Area fromNbt(NbtCompound nbt) {
  98.         World world = MinecraftClient.getInstance().world;
  99.         BlockPos startPos = NbtHelper.toBlockPos(nbt.getCompound("startPos"));
  100.         BlockPos endPos = NbtHelper.toBlockPos(nbt.getCompound("endPos"));
  101.         String name = (nbt.getString("name"));
  102.         UUID uuid = (nbt.getUuid("uuid"));
  103.         return new Area(startPos, endPos, name, uuid);
  104.     }
  105.  
  106.     public static ArrayList<Area> generateAreaListFromNbtList(NbtList nbtList) {
  107.         ArrayList<Area> areaList = new ArrayList<>();
  108.         for (NbtElement nbt : nbtList) {
  109.             areaList.add(fromNbt(((NbtCompound) nbt)));
  110.         }
  111.         return areaList;
  112.     }
  113.  
  114.     public String getName() {
  115.         return name;
  116.     }
  117.  
  118.     public BlockPos getStartPos() {
  119.         return startPos;
  120.     }
  121.  
  122.     public BlockPos getEndPos() {
  123.         return endPos;
  124.     }
  125.  
  126.     public World getWorld() {
  127.         return world;
  128.     }
  129.  
  130.     public UUID getUuid() {
  131.         return uuid;
  132.     }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement