Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- interface FunctionalInterface {
- void areaFunction(ArrayList list, int x, int y, int z);
- }
- public class Area {
- private final UUID ownerUuid;
- private final BlockPos startPos;
- private final BlockPos endPos;
- private final World world;
- private final String name;
- private final UUID uuid;
- public Area(BlockPos startPos, BlockPos endPos, String name, UUID ownerUuid) {
- this(startPos, endPos, name, UUID.randomUUID(), ownerUuid);
- }
- public Area(BlockPos startPos, BlockPos endPos, String name, UUID uuid, UUID ownerUuid) {
- this.startPos = startPos;
- this.endPos = endPos;
- this.world = MinecraftClient.getInstance().world;
- this.name = name;
- this.uuid = uuid;
- this.ownerUuid = ownerUuid;
- }
- public ArrayList<Block> getBlocks() {
- ArrayList<Block> blockList = this.executeInArea((list, x, y, z) -> {
- list.add(world.getBlockState(new BlockPos(x, y, z)).getBlock());
- });
- return blockList;
- }
- public ArrayList<LivingEntity> getDetectedEntities() {
- ArrayList<LivingEntity> entityList = this.executeInArea((list, x, y, z) -> {
- world.getNonSpectatingEntities(LivingEntity.class, new Box(new BlockPos(x, y, z))).forEach(value -> list.add(value));
- });
- // Removes duplicates from the list
- HashSet<LivingEntity> entitySet = new HashSet<>();
- entityList.forEach(value -> entitySet.add(value));
- entityList.clear();
- entitySet.forEach(value -> entityList.add(value));
- return entityList;
- }
- public <T> ArrayList<T> executeInArea(FunctionalInterface function) {
- ArrayList<T> list = new ArrayList<>();
- int x;
- int y;
- int z;
- x = startPos.getX();
- while (true) {
- y = startPos.getY();
- while (true) {
- z = startPos.getZ();
- while (true) {
- if (z < endPos.getZ()) {
- // Runs lambda expressions. defined in FuncitionalInterface.
- function.areaFunction(list, x, y, z);
- z++;
- } else if (z > endPos.getZ()) {
- function.areaFunction(list, x, y, z);
- z--;
- } else {
- function.areaFunction(list, x, y, z);
- break;
- }
- }
- if (y < endPos.getY()) {
- y++;
- } else if (y > endPos.getY()) {
- y--;
- } else {
- break;
- }
- }
- if (x < endPos.getX()) {
- x++;
- } else if (x > endPos.getX()) {
- x--;
- } else {
- break;
- }
- }
- return list;
- }
- public static NbtElement toNbt(Area area) {
- NbtCompound nbt = new NbtCompound();
- nbt.put("startPos", NbtHelper.fromBlockPos(area.getStartPos()));
- nbt.put("endPos", NbtHelper.fromBlockPos(area.getEndPos()));
- nbt.putString("name", area.getName());
- nbt.putUuid("uuid", area.getUuid());
- return nbt;
- }
- public static Area fromNbt(NbtCompound nbt) {
- World world = MinecraftClient.getInstance().world;
- BlockPos startPos = NbtHelper.toBlockPos(nbt.getCompound("startPos"));
- BlockPos endPos = NbtHelper.toBlockPos(nbt.getCompound("endPos"));
- String name = (nbt.getString("name"));
- UUID uuid = (nbt.getUuid("uuid"));
- return new Area(startPos, endPos, name, uuid);
- }
- public static ArrayList<Area> generateAreaListFromNbtList(NbtList nbtList) {
- ArrayList<Area> areaList = new ArrayList<>();
- for (NbtElement nbt : nbtList) {
- areaList.add(fromNbt(((NbtCompound) nbt)));
- }
- return areaList;
- }
- public String getName() {
- return name;
- }
- public BlockPos getStartPos() {
- return startPos;
- }
- public BlockPos getEndPos() {
- return endPos;
- }
- public World getWorld() {
- return world;
- }
- public UUID getUuid() {
- return uuid;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement