Search in sources :

Example 1 with Rail

use of cn.nukkit.utils.Rail in project Nukkit by Nukkit.

the class BlockRail method place.

// Information from http://minecraft.gamepedia.com/Rail
@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
    Block down = this.down();
    if (down == null || down.isTransparent()) {
        return false;
    }
    Map<BlockRail, BlockFace> railsAround = this.checkRailsAroundAffected();
    List<BlockRail> rails = new ArrayList<>(railsAround.keySet());
    List<BlockFace> faces = new ArrayList<>(railsAround.values());
    if (railsAround.size() == 1) {
        BlockRail other = rails.get(0);
        this.setDamage(this.connect(other, railsAround.get(other)).metadata());
    } else if (railsAround.size() == 4) {
        if (this.isAbstract()) {
            this.setDamage(this.connect(rails.get(faces.indexOf(SOUTH)), SOUTH, rails.get(faces.indexOf(EAST)), EAST).metadata());
        } else {
            this.setDamage(this.connect(rails.get(faces.indexOf(EAST)), EAST, rails.get(faces.indexOf(WEST)), WEST).metadata());
        }
    } else if (!railsAround.isEmpty()) {
        if (this.isAbstract()) {
            if (railsAround.size() == 2) {
                BlockRail rail1 = rails.get(0);
                BlockRail rail2 = rails.get(1);
                this.setDamage(this.connect(rail1, railsAround.get(rail1), rail2, railsAround.get(rail2)).metadata());
            } else {
                List<BlockFace> cd = Stream.of(CURVED_SOUTH_EAST, CURVED_NORTH_EAST, CURVED_SOUTH_WEST).filter(o -> o.connectingDirections().stream().allMatch(faces::contains)).findFirst().get().connectingDirections();
                BlockFace f1 = cd.get(0);
                BlockFace f2 = cd.get(1);
                this.setDamage(this.connect(rails.get(faces.indexOf(f1)), f1, rails.get(faces.indexOf(f2)), f2).metadata());
            }
        } else {
            BlockFace f = faces.stream().sorted((f1, f2) -> (f1.getIndex() < f2.getIndex()) ? 1 : ((x == y) ? 0 : -1)).findFirst().get();
            BlockFace fo = f.getOpposite();
            if (faces.contains(fo)) {
                // Opposite connectable
                this.setDamage(this.connect(rails.get(faces.indexOf(f)), f, rails.get(faces.indexOf(fo)), fo).metadata());
            } else {
                this.setDamage(this.connect(rails.get(faces.indexOf(f)), f).metadata());
            }
        }
    }
    this.level.setBlock(this, this, true, true);
    if (!isAbstract()) {
        level.scheduleUpdate(this, this, 0);
    }
    return true;
}
Also used : SOUTH(cn.nukkit.math.BlockFace.SOUTH) ItemTool(cn.nukkit.item.ItemTool) java.util(java.util) BlockFace(cn.nukkit.math.BlockFace) NORTH(cn.nukkit.math.BlockFace.NORTH) Level(cn.nukkit.level.Level) AxisAlignedBB(cn.nukkit.math.AxisAlignedBB) EAST(cn.nukkit.math.BlockFace.EAST) BlockColor(cn.nukkit.utils.BlockColor) Rail(cn.nukkit.utils.Rail) Collectors(java.util.stream.Collectors) WEST(cn.nukkit.math.BlockFace.WEST) Orientation(cn.nukkit.utils.Rail.Orientation) Stream(java.util.stream.Stream) Player(cn.nukkit.Player) Item(cn.nukkit.item.Item) BlockFace(cn.nukkit.math.BlockFace)

Example 2 with Rail

use of cn.nukkit.utils.Rail in project Nukkit by Nukkit.

the class BlockRailActivator method checkSurrounding.

/**
 * Check the surrounding of the rail
 *
 * @param pos      The rail position
 * @param relative The relative of the rail that will be checked
 * @param power    The count of the rail that had been counted
 * @return Boolean of the surrounding area. Where the powered rail on!
 */
protected boolean checkSurrounding(Vector3 pos, boolean relative, int power) {
    if (power >= 8) {
        return false;
    }
    int dx = pos.getFloorX();
    int dy = pos.getFloorY();
    int dz = pos.getFloorZ();
    BlockRail block;
    Block block2 = level.getBlock(new Vector3(dx, dy, dz));
    if (Rail.isRailBlock(block2)) {
        block = (BlockRail) block2;
    } else {
        return false;
    }
    Rail.Orientation base = null;
    boolean onStraight = true;
    switch(block.getOrientation()) {
        case STRAIGHT_NORTH_SOUTH:
            if (relative) {
                dz++;
            } else {
                dz--;
            }
            break;
        case STRAIGHT_EAST_WEST:
            if (relative) {
                dx--;
            } else {
                dx++;
            }
            break;
        case ASCENDING_EAST:
            if (relative) {
                dx--;
            } else {
                dx++;
                dy++;
                onStraight = false;
            }
            base = Rail.Orientation.STRAIGHT_EAST_WEST;
            break;
        case ASCENDING_WEST:
            if (relative) {
                dx--;
                dy++;
                onStraight = false;
            } else {
                dx++;
            }
            base = Rail.Orientation.STRAIGHT_EAST_WEST;
            break;
        case ASCENDING_NORTH:
            if (relative) {
                dz++;
            } else {
                dz--;
                dy++;
                onStraight = false;
            }
            base = Rail.Orientation.STRAIGHT_NORTH_SOUTH;
            break;
        case ASCENDING_SOUTH:
            if (relative) {
                dz++;
                dy++;
                onStraight = false;
            } else {
                dz--;
            }
            base = Rail.Orientation.STRAIGHT_NORTH_SOUTH;
            break;
        default:
            return false;
    }
    return canPowered(new Vector3(dx, dy, dz), base, power, relative) || onStraight && canPowered(new Vector3(dx, dy - 1, dz), base, power, relative);
}
Also used : Rail(cn.nukkit.utils.Rail) Vector3(cn.nukkit.math.Vector3)

Example 3 with Rail

use of cn.nukkit.utils.Rail in project Nukkit by Nukkit.

the class BlockRailPowered method checkSurrounding.

/**
 * Check the surrounding of the rail
 *
 * @param pos      The rail position
 * @param relative The relative of the rail that will be checked
 * @param power    The count of the rail that had been counted
 * @return Boolean of the surrounding area. Where the powered rail on!
 */
protected boolean checkSurrounding(Vector3 pos, boolean relative, int power) {
    // The powered rail can power up to 8 blocks only
    if (power >= 8) {
        return false;
    }
    // The position of the floor numbers
    int dx = pos.getFloorX();
    int dy = pos.getFloorY();
    int dz = pos.getFloorZ();
    // First: get the base block
    BlockRail block;
    Block block2 = level.getBlock(new Vector3(dx, dy, dz));
    // Second: check if the rail is Powered rail
    if (Rail.isRailBlock(block2)) {
        block = (BlockRail) block2;
    } else {
        return false;
    }
    // Used to check if the next ascending rail should be what
    Rail.Orientation base = null;
    boolean onStraight = true;
    // Third: Recalculate the base position
    switch(block.getOrientation()) {
        case STRAIGHT_NORTH_SOUTH:
            if (relative) {
                dz++;
            } else {
                dz--;
            }
            break;
        case STRAIGHT_EAST_WEST:
            if (relative) {
                dx--;
            } else {
                dx++;
            }
            break;
        case ASCENDING_EAST:
            if (relative) {
                dx--;
            } else {
                dx++;
                dy++;
                onStraight = false;
            }
            base = Rail.Orientation.STRAIGHT_EAST_WEST;
            break;
        case ASCENDING_WEST:
            if (relative) {
                dx--;
                dy++;
                onStraight = false;
            } else {
                dx++;
            }
            base = Rail.Orientation.STRAIGHT_EAST_WEST;
            break;
        case ASCENDING_NORTH:
            if (relative) {
                dz++;
            } else {
                dz--;
                dy++;
                onStraight = false;
            }
            base = Rail.Orientation.STRAIGHT_NORTH_SOUTH;
            break;
        case ASCENDING_SOUTH:
            if (relative) {
                dz++;
                dy++;
                onStraight = false;
            } else {
                dz--;
            }
            base = Rail.Orientation.STRAIGHT_NORTH_SOUTH;
            break;
        default:
            // Wrong rail?
            return false;
    }
    // Next check the if rail is on power state
    return canPowered(new Vector3(dx, dy, dz), base, power, relative) || onStraight && canPowered(new Vector3(dx, dy - 1, dz), base, power, relative);
}
Also used : Rail(cn.nukkit.utils.Rail) Vector3(cn.nukkit.math.Vector3)

Aggregations

Rail (cn.nukkit.utils.Rail)3 Vector3 (cn.nukkit.math.Vector3)2 Player (cn.nukkit.Player)1 Item (cn.nukkit.item.Item)1 ItemTool (cn.nukkit.item.ItemTool)1 Level (cn.nukkit.level.Level)1 AxisAlignedBB (cn.nukkit.math.AxisAlignedBB)1 BlockFace (cn.nukkit.math.BlockFace)1 EAST (cn.nukkit.math.BlockFace.EAST)1 NORTH (cn.nukkit.math.BlockFace.NORTH)1 SOUTH (cn.nukkit.math.BlockFace.SOUTH)1 WEST (cn.nukkit.math.BlockFace.WEST)1 BlockColor (cn.nukkit.utils.BlockColor)1 Orientation (cn.nukkit.utils.Rail.Orientation)1 java.util (java.util)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1