org.bukkit.util.BlockVector Java Examples

The following examples show how to use org.bukkit.util.BlockVector. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Renewable.java    From ProjectAres with GNU Affero General Public License v3.0 7 votes vote down vote up
boolean renew(BlockVector pos, MaterialData material) {
    // We need to do the entity check here rather than canRenew, because we are not
    // notified when entities move in our out of the way.
    if(!isClearOfEntities(pos)) return false;

    Location location = pos.toLocation(match.getWorld());
    Block block = location.getBlock();
    BlockState newState = location.getBlock().getState();
    newState.setMaterialData(material);

    BlockRenewEvent event = new BlockRenewEvent(block, newState, this);
    match.callEvent(event); // Our own handler will get this and remove the block from the pool
    if(event.isCancelled()) return false;

    newState.update(true, true);

    if(definition.particles) {
        NMSHacks.playBlockBreakEffect(match.getWorld(), pos, material.getItemType());
    }

    if(definition.sound) {
        NMSHacks.playBlockPlaceSound(match.getWorld(), pos, material.getItemType(), 1f);
    }

    return true;
}
 
Example #2
Source File: Renewable.java    From ProjectAres with GNU Affero General Public License v3.0 6 votes vote down vote up
boolean renew(BlockVector pos) {
    MaterialData material;
    if(isOriginalShuffleable(pos)) {
        // If position is shuffled, first try to find a nearby shuffleable block to swap with.
        // This helps to make shuffling less predictable when the material deficit is small or
        // out of proportion to the original distribution of materials.
        material = sampleShuffledMaterial(pos);

        // If that fails, choose a random material, weighted by the current material deficits.
        if(material == null) material = chooseShuffledMaterial();
    } else {
        material = snapshot().getOriginalMaterial(pos);
    }

    if(material != null) {
        return renew(pos, material);
    }

    return false;
}
 
Example #3
Source File: ControlPointBlockDisplay.java    From ProjectAres with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Change the controller display to the given team's color, or reset the display if team is null
 */
@SuppressWarnings("deprecation")
public void setController(Competitor controllingTeam) {
    if(!Objects.equals(this.controllingTeam, controllingTeam) && this.controllerDisplayRegion != null) {
        if(controllingTeam == null) {
            for(BlockVector block : this.controllerDisplayRegion.getBlockVectors()) {
                this.controllerDisplayImage.restore(block);
            }
        } else {
            byte blockData = BukkitUtils.chatColorToDyeColor(controllingTeam.getColor()).getWoolData();
            for(BlockVector pos : this.controllerDisplayRegion.getBlockVectors()) {
                BlockUtils.blockAt(match.getWorld(), pos).setData(blockData);
            }
        }
        this.controllingTeam = controllingTeam;
    }
}
 
Example #4
Source File: HillObjective.java    From CardinalPGM with MIT License 6 votes vote down vote up
private void renderBlocks() {
    if (progress == null) return;
    byte color1 = capturingTeam != null ? MiscUtil.convertChatColorToDyeColor(capturingTeam.getColor()).getWoolData() : -1;
    byte color2 = team != null ? MiscUtil.convertChatColorToDyeColor(team.getColor()).getWoolData() : -1;
    Vector center = progress.getCenterBlock().getVector();
    double x = center.getX();
    double z = center.getZ();
    double percent = Math.toRadians(getPercent() * 3.6);
    for(Block block : progress.getBlocks()) {
        if (!visualMaterials.evaluate(block).equals(FilterState.ALLOW)) continue;
        double dx = block.getX() - x;
        double dz = block.getZ() - z;
        double angle = Math.atan2(dz, dx);
        if(angle < 0) angle += 2 * Math.PI;
        byte color = angle < percent ? color1 : color2;
        if (color == -1) {
            Pair<Material,Byte> oldBlock = oldProgress.getBlockAt(new BlockVector(block.getLocation().position()));
            if (oldBlock.getLeft().equals(block.getType())) color = oldBlock.getRight();
        }
        if (color != -1) block.setData(color);
    }
}
 
Example #5
Source File: RegionSave.java    From CardinalPGM with MIT License 6 votes vote down vote up
public RegionSave(RegionModule region) {
    BlockVector min = blockAlign(region.getMin());
    BlockVector max = blockAlign(region.getMax());
    BlockVector size = max.minus(min).toBlockVector();

    this.min = min;
    this.size = size;

    List<Pair<Material,Byte>> blocks = new ArrayList<>();
    for (int z = min.getBlockZ(); z < max.getBlockZ(); z++) {
        for (int y = min.getBlockY(); y < max.getBlockY(); y++) {
            for (int x = min.getBlockX(); x < max.getBlockX(); x++) {
                Block block = new Location(GameHandler.getGameHandler().getMatchWorld(), x, y, z).getBlock();
                blocks.add(new ImmutablePair<>(block.getType(), block.getData()));
            }
        }
    }
    this.blocks = blocks;
}
 
Example #6
Source File: BlockVectorSet.java    From ProjectAres with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public Iterator<BlockVector> iterator() {
    final TLongIterator iter = this.set.iterator();

    return new Iterator<BlockVector>() {
        @Override
        public boolean hasNext() {
            return iter.hasNext();
        }

        @Override
        public BlockVector next() {
            return decodePos(iter.next());
        }

        @Override
        public void remove() {
            iter.remove();
        }
    };
}
 
Example #7
Source File: Renewable.java    From PGM with GNU Affero General Public License v3.0 6 votes vote down vote up
boolean renew(BlockVector pos) {
  MaterialData material;
  if (isOriginalShuffleable(pos)) {
    // If position is shuffled, first try to find a nearby shuffleable block to swap with.
    // This helps to make shuffling less predictable when the world deficit is small or
    // out of proportion to the original distribution of world.
    material = sampleShuffledMaterial(pos);

    // If that fails, choose a random world, weighted by the current world deficits.
    if (material == null) material = chooseShuffledMaterial();
  } else {
    material = snapshot().getOriginalMaterial(pos);
  }

  if (material != null) {
    return renew(pos, material);
  }

  return false;
}
 
Example #8
Source File: SnapshotMatchModule.java    From PGM with GNU Affero General Public License v3.0 6 votes vote down vote up
public MaterialData getOriginalMaterial(int x, int y, int z) {
  if (y < 0 || y >= 256) return new MaterialData(Material.AIR);

  ChunkVector chunkVector = ChunkVector.ofBlock(x, y, z);
  ChunkSnapshot chunkSnapshot = chunkSnapshots.get(chunkVector);
  if (chunkSnapshot != null) {
    BlockVector chunkPos = chunkVector.worldToChunk(x, y, z);
    return new MaterialData(
        chunkSnapshot.getBlockTypeId(
            chunkPos.getBlockX(), chunkPos.getBlockY(), chunkPos.getBlockZ()),
        (byte)
            chunkSnapshot.getBlockData(
                chunkPos.getBlockX(), chunkPos.getBlockY(), chunkPos.getBlockZ()));
  } else {
    return match.getWorld().getBlockAt(x, y, z).getState().getMaterialData();
  }
}
 
Example #9
Source File: ControlPointBlockDisplay.java    From ProjectAres with GNU Affero General Public License v3.0 6 votes vote down vote up
protected void setProgress(Competitor controllingTeam, Competitor capturingTeam, double capturingProgress) {
    if(this.progressDisplayRegion != null) {
        Vector center = this.progressDisplayRegion.getBounds().center();

        // capturingProgress can be zero, but it can never be one, so invert it to avoid
        // a zero-area SectorRegion that can cause glitchy rendering
        SectorRegion sectorRegion = new SectorRegion(center.getX(), center.getZ(), 0, (1 - capturingProgress) * 2 * Math.PI);

        for(BlockVector pos : this.progressDisplayRegion.getBlockVectors()) {
            if(sectorRegion.contains(pos)) {
                this.setBlock(pos, controllingTeam);
            } else {
                this.setBlock(pos, capturingTeam);
            }
        }
    }
}
 
Example #10
Source File: XMLUtils.java    From ProjectAres with GNU Affero General Public License v3.0 6 votes vote down vote up
public static BlockVector parseBlockVector(Node node, BlockVector def) throws InvalidXMLException {
    if(node == null) return def;

    String[] components = node.getValue().trim().split("\\s*,\\s*");
    if(components.length != 3) {
        throw new InvalidXMLException("Invalid block location", node);
    }
    try {
        return new BlockVector(Integer.parseInt(components[0]),
                               Integer.parseInt(components[1]),
                               Integer.parseInt(components[2]));
    }
    catch(NumberFormatException e) {
        throw new InvalidXMLException("Invalid block location", node);
    }
}
 
Example #11
Source File: BlockVectorSet.java    From ProjectAres with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Return an iterator that reuses a single BlockVector instance,
 * mutating it for each iteration.
 */
public Iterator<BlockVector> mutableIterator() {
    final TLongIterator iter = set.iterator();
    return new Iterator<BlockVector>() {
        final BlockVector value = new BlockVector();

        @Override
        public boolean hasNext() {
            return iter.hasNext();
        }

        @Override
        public BlockVector next() {
            return decodePos(iter.next(), value);
        }
    };
}
 
Example #12
Source File: BlockImage.java    From PGM with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Copy the block at the given position from the image to the world
 *
 * @param pos Block position in world coordinates
 */
@SuppressWarnings("deprecation")
public void restore(BlockVector pos) {
  int offset = this.offset(pos);
  pos.toLocation(this.world)
      .getBlock()
      .setTypeIdAndData(this.blockIds[offset], this.blockData[offset], true);
}
 
Example #13
Source File: BlockVectorSet.java    From PGM with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public boolean addAll(Collection<? extends BlockVector> vectors) {
  for (BlockVector v : vectors) {
    this.add(v);
  }
  return false;
}
 
Example #14
Source File: BlockMaterialMap.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
Entry<BlockVector, MaterialData> castEntry(Object obj) {
    if(!(obj instanceof Entry)) return null;
    Entry entry = (Entry) obj;
    if(entry.getKey() instanceof BlockVector && entry.getValue() instanceof MaterialData) {
        return (Entry<BlockVector, MaterialData>) entry;
    } else {
        return null;
    }
}
 
Example #15
Source File: Renewable.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
boolean isOriginalShuffleable(BlockVector pos) {
    if(!definition.region.contains(pos)) return false;
    Filter.QueryResponse response = shuffleableCache.get(pos);
    if(response == null) {
        response = definition.shuffleableBlocks.query(new BlockQuery(snapshot().getOriginalBlock(pos)));
    }
    return response.isAllowed();
}
 
Example #16
Source File: BlockUtils.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
public static TLongSet encodePosSet(Collection<?> vectors) {
    TLongSet encoded = new TLongHashSet(vectors.size());
    for(Object o : vectors) {
        if(o instanceof BlockVector) {
            encoded.add(encodePos((BlockVector) o));
        }
    }
    return encoded;
}
 
Example #17
Source File: SnapshotMatchModule.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
public BlockState getOriginalBlock(int x, int y, int z) {
    BlockState state = getMatch().getWorld().getBlockAt(x, y, z).getState();
    if(y < 0 || y >= 256) return state;

    ChunkVector chunkVector = ChunkVector.ofBlock(x, y, z);
    ChunkSnapshot chunkSnapshot = chunkSnapshots.get(chunkVector);
    if(chunkSnapshot != null) {
        BlockVector chunkPos = chunkVector.worldToChunk(x, y, z);
        state.setMaterialData(new MaterialData(chunkSnapshot.getBlockTypeId(chunkPos.getBlockX(), chunkPos.getBlockY(), chunkPos.getBlockZ()),
                                               (byte) chunkSnapshot.getBlockData(chunkPos.getBlockX(), chunkPos.getBlockY(), chunkPos.getBlockZ())));
    }
    return state;
}
 
Example #18
Source File: Bounds.java    From PGM with GNU Affero General Public License v3.0 5 votes vote down vote up
public BlockVector getRandomBlock(Random random) {
  BlockVector min = this.getBlockMin();
  BlockVector size = this.getBlockSize();
  return new BlockVector(
      min.getX() + random.nextInt(size.getBlockX()),
      min.getY() + random.nextInt(size.getBlockY()),
      min.getZ() + random.nextInt(size.getBlockZ()));
}
 
Example #19
Source File: Bounds.java    From PGM with GNU Affero General Public License v3.0 5 votes vote down vote up
/** Iterate over all the block locations within these bounds, in Z-major order. */
public Iterator<BlockVector> getBlockIterator() {
  if (!this.isBlockFinite()) {
    throw new UnsupportedOperationException("Cannot get all blocks from an infinite region");
  }
  return new CuboidBlockIterator(getBlockMin(), getBlockMaxOutside());
}
 
Example #20
Source File: Bounds.java    From PGM with GNU Affero General Public License v3.0 5 votes vote down vote up
public Iterable<BlockVector> getBlocks() {
  return new Iterable<BlockVector>() {
    @Override
    public Iterator<BlockVector> iterator() {
      return getBlockIterator();
    }
  };
}
 
Example #21
Source File: AbstractRegion.java    From PGM with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public Iterator<BlockVector> getBlockVectorIterator() {
  return Iterators.filter(
      this.getBounds().getBlockIterator(),
      new Predicate<BlockVector>() {
        @Override
        public boolean apply(BlockVector vector) {
          return contains(vector);
        }
      });
}
 
Example #22
Source File: AbstractRegion.java    From PGM with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public Iterable<BlockVector> getBlockVectors() {
  return new Iterable<BlockVector>() {
    @Override
    public Iterator<BlockVector> iterator() {
      return getBlockVectorIterator();
    }
  };
}
 
Example #23
Source File: BlockImage.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Copy the block at the given position from the image to the world
 * @param pos   Block position in world coordinates
 */
@SuppressWarnings("deprecation")
public void restore(BlockVector pos) {
    int offset = this.offset(pos);
    pos.toLocation(this.world).getBlock().setTypeIdAndData(this.blockIds[offset],
                                                           this.blockData[offset],
                                                           true);
}
 
Example #24
Source File: Renewable.java    From PGM with GNU Affero General Public License v3.0 5 votes vote down vote up
boolean isOriginalRenewable(BlockVector pos) {
  if (!definition.region.contains(pos)) return false;
  Filter.QueryResponse response = renewableCache.get(pos);
  if (response == null) {
    response = definition.renewableBlocks.query(new BlockQuery(snapshot().getOriginalBlock(pos)));
  }
  return response.isAllowed();
}
 
Example #25
Source File: Renewable.java    From PGM with GNU Affero General Public License v3.0 5 votes vote down vote up
boolean isClearOfEntities(BlockVector pos) {
  if (definition.avoidPlayersRange > 0d) {
    double rangeSquared = definition.avoidPlayersRange * definition.avoidPlayersRange;
    pos = BlockVectors.center(pos);
    for (MatchPlayer player : match.getParticipants()) {
      Location location = player.getBukkit().getLocation().add(0, 1, 0);
      if (location.toVector().distanceSquared(pos) < rangeSquared) {
        return false;
      }
    }
  }
  return true;
}
 
Example #26
Source File: CraftStructureBlock.java    From Kettle with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void setRelativePosition(BlockVector vector) {
    Validate.isTrue(isBetween(vector.getBlockX(), -MAX_SIZE, MAX_SIZE), "Structure Size (X) must be between -" + MAX_SIZE + " and " + MAX_SIZE);
    Validate.isTrue(isBetween(vector.getBlockY(), -MAX_SIZE, MAX_SIZE), "Structure Size (Y) must be between -" + MAX_SIZE + " and " + MAX_SIZE);
    Validate.isTrue(isBetween(vector.getBlockZ(), -MAX_SIZE, MAX_SIZE), "Structure Size (Z) must be between -" + MAX_SIZE + " and " + MAX_SIZE);
    getSnapshot().position = new BlockPos(vector.getBlockX(), vector.getBlockY(), vector.getBlockZ()); // PAIL: rename relativePosition
}
 
Example #27
Source File: HillObjective.java    From CardinalPGM with MIT License 5 votes vote down vote up
private void renderCaptured() {
    if (captured == null) return;
    for(Block block : captured.getBlocks()) {
        if (!visualMaterials.evaluate(block).equals(FilterState.ALLOW)) continue;
        byte color = team != null ? MiscUtil.convertChatColorToDyeColor(team.getColor()).getWoolData() : -1;
        if (color == -1) {
            Pair<Material,Byte> oldBlock = oldCaptured.getBlockAt(new BlockVector(block.getLocation().position()));
            if (oldBlock.getLeft().equals(block.getType())) color = oldBlock.getRight();
        }
        if (color != -1) block.setData(color);
    }
}
 
Example #28
Source File: ChunkVector.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
public BlockVector getBlockMin() {
    return new BlockVector(getBlockMinX(), 0, getBlockMinZ());
}
 
Example #29
Source File: BlockMaterialMap.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public void putAll(Map<? extends BlockVector, ? extends MaterialData> m) {
    for(Entry<? extends BlockVector, ? extends MaterialData> entry : m.entrySet()) {
        put(entry.getKey(), entry.getValue());
    }
}
 
Example #30
Source File: BlockMaterialMap.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
public int putEncoded(BlockVector pos, MaterialData material) {
    return putEncoded(encodePos(pos), material);
}