Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.blizzard.bos.world.feature.dungeon;
- import com.blizzard.bos.utils.Rect;
- import com.google.common.collect.Lists;
- import net.minecraft.block.Block;
- import net.minecraft.block.BlockState;
- import net.minecraft.block.Blocks;
- import net.minecraft.network.PacketBuffer;
- import net.minecraft.util.Direction;
- import net.minecraft.util.math.BlockPos;
- import net.minecraft.util.math.Vec3i;
- import net.minecraft.world.World;
- import net.minecraftforge.common.util.Size2i;
- import java.util.*;
- import java.util.function.BiFunction;
- public final class BindingDungeon {
- private static List<BiFunction<Level, PacketBuffer, Level.Room>> ROOMS = Lists.newArrayList(
- Level.Room::new
- );
- private static Level.Room get(Level level, PacketBuffer buf) {
- return ROOMS.get(buf.readInt()).apply(level, buf);
- }
- private final Size2i UNIT = new Size2i(19, 5);
- //private final Vec3i UNIT = new Vec3i(19, 5, 19);
- private final World world;
- private final BlockPos start;
- private final Level[] levels;
- public BindingDungeon(World world, BlockPos start) {
- this.world = world;
- this.start = start;
- Arrays.setAll(levels = new Level[3], y -> new Level(this, y));
- }
- public BindingDungeon(World world, PacketBuffer buf) {
- this.world = world;
- start = buf.readBlockPos();
- levels = new Level[buf.readInt()];
- Arrays.setAll(levels, i -> new Level(this, buf));
- }
- public void save(PacketBuffer buf) {
- buf.writeBlockPos(start);
- buf.writeInt(levels.length);
- for (Level level : levels) level.save(buf);
- }
- public void spawn() { for (Level level : levels) level.spawn(); }
- public final static class Level {
- private final BindingDungeon dungeon;
- private final HashMap<Vec3i, Room> rooms = new HashMap<>();
- private final int y;
- public Level(BindingDungeon dungeon, int y) {
- this.dungeon = dungeon;
- this.y = y;
- for (int x = -1; x <= 1; x++) {
- for (int z = -1; z <= 1; z++) {
- add(new Room(this, x, z, 1, 1));
- }
- }
- add(new Room(this, -1, 2, y + 1, y + 1));
- }
- private Level(BindingDungeon dungeon, PacketBuffer buf) {
- this.dungeon = dungeon;
- y = buf.readInt();
- int count = buf.readInt();
- for (int i = 0; i < count; i++) {
- add(get(this, buf));
- }
- }
- public void save(PacketBuffer buf) {
- buf.writeInt(y);
- Room[] rooms = this.rooms.values().stream().sorted()
- .distinct().toArray(Room[]::new);
- buf.writeInt(rooms.length);
- for (Room room : rooms) room.save(buf);
- }
- private void add(Room room) {
- Rect size = new Rect(room.x, room.z, room.sizeX, room.sizeZ);
- for (int x = size.getMinX(); x < size.getMaxX(); x++) {
- for (int z = size.getMinZ(); z < size.getMaxZ(); z++) {
- Vec3i pos = new Vec3i(x, 0, z);
- rooms.computeIfPresent(pos, (p, v) -> {
- throw new IllegalStateException(room+" overlaps with "+v);
- });
- rooms.put(pos, room);
- }
- }
- }
- private void spawn() { rooms.values().stream().sorted().distinct().forEach(Room::spawn); }
- public static class Room {
- protected final Level level;
- protected final int x, z, sizeX, sizeZ;
- public Room(Level level, int x, int z, int sizeX, int sizeZ) {
- this.level = level;
- this.x = x;
- this.z = z;
- this.sizeX = sizeX;
- this.sizeZ = sizeZ;
- }
- protected Room(Level level, PacketBuffer buf) {
- this.level = level;
- x = buf.readInt();
- z = buf.readInt();
- sizeX = buf.readInt();
- sizeZ = buf.readInt();
- }
- public void save(PacketBuffer buf) {
- buf.writeInt(getType());
- buf.writeInt(x);
- buf.writeInt(z);
- buf.writeInt(sizeX);
- buf.writeInt(sizeZ);
- }
- protected int getType() { return 0; }
- protected final Size2i getUNIT() { return level.dungeon.UNIT; }
- protected final BlockPos getStart() { return level.dungeon.start; }
- protected final int getY() { return level.y; }
- protected final World getWorld() { return level.dungeon.world; }
- public BlockPos getCenter() {
- BlockPos.Mutable center = new BlockPos.Mutable(getStart());
- center.move(
- x * (getUNIT().width),
- getY() * -(getUNIT().height),
- z * (getUNIT().width)
- );
- return center.toImmutable();
- }
- protected Rect getSize() {
- return new Rect(
- -(getUNIT().width / 2), -(getUNIT().width / 2),
- (sizeX * (getUNIT().width)) - 1,
- (sizeZ * (getUNIT().width)) - 1
- );
- }
- protected void spawn() {
- BlockPos center = getCenter();
- setBlockState(center.add(0, getUNIT().height - 1, 0), Color.GRAY);
- Rect size = getSize();
- for (int x = size.getMinX(); x <= size.getMaxX(); x++) {
- for (int z = size.getMinZ(); z <= size.getMaxZ(); z++) {
- Color color = Color.WHITE;
- boolean sX = size.isSideX(x);
- boolean sZ = size.isSideZ(z);
- if (x == 0 && z == 0) color = Color.GRAY;
- else if (sX && sZ) color = Color.PURPLE;
- else if (sX) color = Color.RED;
- else if (sZ) color = Color.BLUE;
- setBlockState(center.add(x, 0, z), color);
- }
- }
- for (int x = 0; x < sizeX; x++) {
- setBlockState(getDoor(Direction.EAST, x), Color.BLACK);
- //int px = x * (UNIT.getX());
- //world.setBlockState(center.add(px, 0, size.getMinZ()), getMarker(Color.BLACK), 0);
- //world.setBlockState(center.add(px, 0, size.getMaxZ()), getMarker(Color.BLACK), 0);
- }
- for (int z = 0; z < sizeZ; z++) {
- //int pz = z * (UNIT.getZ());
- //world.setBlockState(center.add(size.getMinX(), 0, pz), getMarker(Color.BLACK), 0);
- //world.setBlockState(center.add(size.getMaxX(), 0, pz), getMarker(Color.BLACK), 0);
- }
- /*for (int y = 1; y < UNIT.getY() - 1; y++)
- world.setBlockState(center.up(y), Blocks.LADDER.getDefaultState(), 0);*/
- }
- protected boolean setBlockState(BlockPos pos, Color color) {
- return setBlockState(pos, getMarker(color));
- }
- protected boolean setBlockState(BlockPos pos, Block block) {
- return setBlockState(pos, block.getDefaultState());
- }
- protected boolean setBlockState(BlockPos pos, BlockState state) {
- return getWorld().setBlockState(pos, state, 0);
- }
- private IndexOutOfBoundsException error(Direction.Axis axis, int expected, int supplied) {
- return new IndexOutOfBoundsException(String.format(
- "%s index must be less then %d got %d", axis, expected, supplied
- ));
- }
- private IndexOutOfBoundsException error(int supplied) {
- return new IndexOutOfBoundsException(String.format(
- "Index must be non-negative got %d", supplied
- ));
- }
- public BlockPos getDoor(Direction side, int doorIndex) {
- if (doorIndex < 0) throw error(doorIndex);
- BlockPos center = getCenter();
- Vec3i a, b1, b2;
- Rect size = getSize();
- int pos = doorIndex * getUNIT().width;
- switch (side.getAxis()) {
- case X:
- if (doorIndex >= sizeZ) throw error(side.getAxis(), sizeZ, doorIndex);
- a = new Vec3i(0, 0, pos);
- b1 = new Vec3i(size.getMinX(), 0, 0);
- b2 = new Vec3i(size.getMaxX(), 0, 0);
- break;
- case Z:
- if (doorIndex >= sizeX) throw error(side.getAxis(), sizeX, doorIndex);
- a = new Vec3i(pos, 0, 0);
- b1 = new Vec3i(0, 0, size.getMinZ());
- b2 = new Vec3i(0, 0, size.getMaxZ());
- break;
- default: throw new IllegalArgumentException("Expected horizontal axis!");
- }
- if (side.getAxisDirection() == Direction.AxisDirection.NEGATIVE)
- return center.add(a).add(b1);
- else return center.add(a).add(b2);
- }
- @Override
- public String toString() {
- return "Room{" + "x=" + x + ", z=" + z +
- ", sizeX=" + sizeX + ", sizeZ=" + sizeZ + '}';
- }
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- Room room = (Room) o;
- return x == room.x && z == room.z &&
- sizeX == room.sizeX && sizeZ == room.sizeZ;
- }
- @Override
- public int hashCode() {
- return Objects.hash(x, z, sizeX, sizeZ);
- }
- }
- }
- enum Color {
- WHITE,ORANGE,MAGENTA,LIGHT_BLUE,
- YELLOW,LIME,PINK,GRAY,
- LIGHT_GRAY,CYAN,PURPLE,
- BLUE,BROWN,GREEN,RED, BLACK
- }
- private static BlockState getMarker(Color color) {
- switch (color) {
- case WHITE: return Blocks.WHITE_WOOL.getDefaultState();
- case ORANGE: return Blocks.ORANGE_WOOL.getDefaultState();
- case MAGENTA: return Blocks.MAGENTA_WOOL.getDefaultState();
- case LIGHT_BLUE: return Blocks.LIGHT_BLUE_WOOL.getDefaultState();
- case YELLOW: return Blocks.YELLOW_WOOL.getDefaultState();
- case LIME: return Blocks.LIME_WOOL.getDefaultState();
- case PINK: return Blocks.PINK_WOOL.getDefaultState();
- case GRAY: return Blocks.GRAY_WOOL.getDefaultState();
- case LIGHT_GRAY: return Blocks.LIGHT_GRAY_WOOL.getDefaultState();
- case CYAN: return Blocks.CYAN_WOOL.getDefaultState();
- case PURPLE: return Blocks.PURPLE_WOOL.getDefaultState();
- case BLUE: return Blocks.BLUE_WOOL.getDefaultState();
- case BROWN: return Blocks.BROWN_WOOL.getDefaultState();
- case GREEN: return Blocks.GREEN_WOOL.getDefaultState();
- case RED: return Blocks.RED_WOOL.getDefaultState();
- case BLACK: return Blocks.BLACK_WOOL.getDefaultState();
- default: return Blocks.AIR.getDefaultState();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement