/** * Generates all neighbours around this rail * * @param deep whether to perform a deep search */ public void genNeighbours(boolean deep) { if (deep) { for (BlockFace direction : BlockFaces.NESW) { MinecartTrackLogic logic = this.getLogic(direction); if (logic != null) { logic.parent = this; logic.direction = direction; // find out what connections this rail has logic.genNeighbours(false); if (logic.neighbours.size() >= 2) { continue; // already connected to two misc track pieces } MinecartTrackLogic self = logic.getLogic(logic.direction.getOpposite()); self.parent = logic; self.direction = logic.direction.getOpposite(); logic.neighbours.add(self); this.neighbours.add(logic); } } } else { for (BlockFace direction : this.getState().getDirections()) { if (direction == this.direction.getOpposite()) { continue; } MinecartTrackLogic logic = this.getLogic(direction); if (logic != null) { logic.parent = this; logic.direction = direction; if (logic.getState().isConnected(direction.getOpposite())) { this.neighbours.add(logic); } } } } }
@Override public void onPlacement( Block block, short data, BlockFace against, Vector3 clickedPos, boolean isClickedBlock, Cause<?> cause) { super.onPlacement(block, data, against, clickedPos, isClickedBlock, cause); BlockFace facing = PlayerUtil.getFacing(cause).getOpposite(); // search for neighbor and align Block neigh; for (BlockFace face : BlockFaces.NESW) { if ((neigh = block.translate(face)).getMaterial().equals(this)) { if (face == facing || face == facing.getOpposite()) { if (facing == BlockFace.NORTH || facing == BlockFace.SOUTH) { facing = BlockFace.WEST; } else { facing = BlockFace.SOUTH; } } this.setFacing(neigh, facing); break; } } this.setFacing(block, facing); }
public void placeDoor(int xx, int yy, int zz, DoorBlock door, BlockFace facing) { final Block bottom = getBlock(xx, yy, zz); door.create( getBlock(xx, yy, zz), bottom.translate(BlockFace.TOP), BlockFace.fromYaw(facing.getDirection().getYaw() + rotation.getYaw()), false, false); }
/** * Gets the block that is powering this block, or null if none * * @param block to get source of power of * @return Source of redstone power, or null if none */ public static Block getReceivingPowerLocation(Block block) { for (BlockFace face : BlockFaces.NESWBT) { Block b = block.translate(face); if (isEmittingPower(b, face.getOpposite())) { return b; } } return null; }
public void attachMaterial(int xx, int yy, int zz, Attachable attachable) { final Block block = getBlock(xx, yy, zz); for (BlockFace face : BlockFaces.BTNSWE) { final Block adjacent = block.translate(face); if (attachable.canAttachTo(adjacent, face.getOpposite())) { block.setMaterial((BlockMaterial) attachable); attachable.setAttachedFace(block, face, null); } } }
private boolean findFrame(Block origin, boolean withEnderEye) { for (BlockFace face : BlockFaces.NESW) { Block frame = origin.translate(face, 2); BlockFace facing = face.getOpposite(); if (!isEndFrame(frame, facing, withEnderEye)) { return false; } if (!isEndFrame(frame.translate(BlockFaces.NESW.previous(face)), facing, withEnderEye)) { return false; } if (!isEndFrame(frame.translate(BlockFaces.NESW.next(face)), facing, withEnderEye)) { return false; } } return true; }
@Override public void onInteract(Entity entity, Block block, Action type, BlockFace clickedface) { super.onInteract(entity, block, type, clickedface); if (type == Action.RIGHT_CLICK) { BlockMaterial clickedmat = block.getMaterial(); if (clickedmat.equals(VanillaMaterials.TNT)) { // Detonate TntBlock VanillaMaterials.TNT.onIgnite(block); return; } else { // Default fire creation Block target = block.translate(clickedface); // Default fire placement clickedface = clickedface.getOpposite(); if (VanillaMaterials.FIRE.canPlace(target, (short) 0)) { if (VanillaMaterials.FIRE.onPlacement(target, (short) 0)) { PlayerQuickbar inv = entity.get(Human.class).getInventory().getQuickbar(); inv.addData(inv.getCurrentSlot(), 1); } } // Handle the creation of portals if (VanillaMaterials.PORTAL.createPortal(target.translate(BlockFace.BOTTOM))) { return; } } } }
@Override public void placeObject(World w, int x, int y, int z) { Block origin = w.getBlock(x, y, z); // Generate the frames for (BlockFace face : BlockFaces.NESW) { Block frame = origin.translate(face, 2); BlockFace facing = face.getOpposite(); // Place the three pieces placeFrame(frame, facing); placeFrame(frame.translate(BlockFaces.NESW.previous(face)), facing); placeFrame(frame.translate(BlockFaces.NESW.next(face)), facing); } // Set to a random state setRandomActive(origin, 0.1f); }
public void setBlockMaterial(int xx, int yy, int zz, BlockMaterial material, short data) { final Vector3 transformed = transform(xx, yy, zz); position .getWorld() .setBlockMaterial( transformed.getFloorX(), transformed.getFloorY(), transformed.getFloorZ(), material, data, null); if (material instanceof Directional) { final Directional directional = (Directional) material; final Block block = position.getWorld().getBlock(transformed); final BlockFace face = directional.getFacing(block); if (face != BlockFace.BOTTOM && face != BlockFace.TOP) { directional.setFacing( block, BlockFace.fromYaw(face.getDirection().getYaw() + rotation.getYaw())); } } else if (material instanceof Attachable) { final Attachable attachable = (Attachable) material; final Block block = position.getWorld().getBlock(transformed); final BlockFace face = attachable.getAttachedFace(block); if (face != BlockFace.BOTTOM && face != BlockFace.TOP) { attachable.setAttachedFace( block, BlockFace.fromYaw(face.getDirection().getYaw() + rotation.getYaw()), null); } } }
/** Tries to connect this rail to the given direction */ public void connect(BlockFace direction) { if (this.neighbours.isEmpty()) { this.setState(RailsState.WEST); return; } MinecartTrackLogic from = this.getNeighbour(direction); if (from == null) { from = this.neighbours.get(0); direction = from.direction; } // try to find a rail on a 90-degree angle for (BlockFace face : BlockFaces.NESW) { if (face != direction && face != direction.getOpposite()) { MinecartTrackLogic logic = this.getNeighbour(face); if (logic != null) { this.connect(from, logic); return; } } } // try to use the opposite, null is handled this.connect(from, this.getNeighbour(direction.getOpposite())); }
@Override public short getRedstonePower(Block block, RedstonePowerMode powerMode) { if (!this.isRedstoneConductor()) { return REDSTONE_POWER_MIN; } short power = 0; Block neigh; BlockMaterial mat; for (BlockFace face : BlockFaces.NESWBT) { neigh = block.translate(face); mat = neigh.getMaterial(); if (mat instanceof RedstoneSource) { power = (short) Math.max( power, ((RedstoneSource) mat) .getDirectRedstonePower(neigh, face.getOpposite(), powerMode)); } } return power; }
@Override public void onInteract(Entity entity, Block block, Action type, BlockFace clickedface) { super.onInteract(entity, block, type, clickedface); Block target = block.translate(clickedface); if (target.getMaterial().equals(VanillaMaterials.AIR)) { clickedface = clickedface.getOpposite(); if (VanillaMaterials.FIRE.canPlace(target, (short) 0, clickedface, false)) { if (VanillaMaterials.FIRE.onPlacement(target, (short) 0, clickedface, false)) { Inventory inv = entity.getInventory(); inv.addCurrentItemData(1); } } } }
/** * Let's this liquid flow from the block to the direction given * * @param block to flow from * @param to flow to * @return True if flowing was successful */ public boolean onFlow(Block block, BlockFace to) { int level; if (to == BlockFace.BOTTOM) { level = this.getMaxLevel(); } else { level = this.getLevel(block) - 1; if (level < 0) { return false; } } Block spread = block.getWorld().getBlock(block.getX(), block.getY(), block.getZ(), this).translate(to); BlockMaterial spreadMat = spread.getMaterial(); if (this.isMaterial(spreadMat)) { if (this.isMaximumLevel(spread)) { // If the block above was a non-flowing source, return false to make it spread outwards // If the block above was not a source, return true to stop spreading return !this.isSource(block); } else { // Compare levels if (level > this.getLevel(spread)) { if (spreadMat != this.getFlowingMaterial()) { // Make sure the material is adjusted spread.setMaterial(this.getFlowingMaterial(), spread.getData()); } this.setLevel(spread, level); if (to == BlockFace.BOTTOM) { this.setFlowingDown(spread, true); } // Update blocks around return true; } } } else if (!isLiquidObstacle(spreadMat)) { // Create a new liquid this.onSpread(spread, level, to.getOpposite()); return true; } return false; }
@Override public Block translate(BlockFace offset) { return this.translate(offset.getOffset()); }
@Override public Block translate(BlockFace offset, int distance) { return this.translate(offset.getOffset().multiply(distance)); }
public static BlockVector toVector(BlockFace face) { return toBlockVector(face.getOffset()); }