use of org.bukkit.block.BlockState in project Prism-Bukkit by prism.
the class BlockAction method removeBlock.
private ChangeResult removeBlock(Player player, PrismParameters parameters, boolean isPreview, Block block) {
BlockStateChangeImpl stateChange;
if (!block.getType().equals(AIR)) {
// Ensure it's acceptable to remove the current block
if (!Utilities.isAcceptableForBlockPlace(block.getType()) && !Utilities.areBlockIdsSameCoreItem(block.getType(), getMaterial()) && !parameters.hasFlag(Flag.OVERWRITE)) {
return new ChangeResultImpl(ChangeResultType.SKIPPED, null);
}
// Capture the block before we change it
final BlockState originalBlock = block.getState();
if (!isPreview) {
// Set
block.setType(AIR);
// Capture the new state
final BlockState newBlock = block.getState();
// Store the state change
stateChange = new BlockStateChangeImpl(originalBlock, newBlock);
} else {
// Otherwise, save the state so we can cancel if needed
// Note: we save the original state as both old/new so we can
// re-use blockStateChanges
stateChange = new BlockStateChangeImpl(originalBlock, originalBlock);
// Preview it
EntityUtils.sendBlockChange(player, block.getLocation(), Bukkit.createBlockData(AIR));
// Send preview to shared players
for (final CommandSender sharedPlayer : parameters.getSharedPlayers()) {
if (sharedPlayer instanceof Player) {
EntityUtils.sendBlockChange((Player) sharedPlayer, block.getLocation(), Bukkit.createBlockData(AIR));
}
}
}
return new ChangeResultImpl(ChangeResultType.APPLIED, stateChange);
}
return new ChangeResultImpl(ChangeResultType.SKIPPED, null);
}
use of org.bukkit.block.BlockState in project Prism-Bukkit by prism.
the class PrismWorldEvents method onStructureGrow.
/**
* StructureGrowEvent.
* @param event StructureGrowEvent
*/
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onStructureGrow(final StructureGrowEvent event) {
String type = "tree-grow";
final TreeType species = event.getSpecies();
if (species.name().toLowerCase().contains("mushroom")) {
type = "mushroom-grow";
}
if (!Prism.getIgnore().event(type, event.getWorld())) {
return;
}
for (final BlockState block : event.getBlocks()) {
if (Utilities.isGrowableStructure(block.getType())) {
if (event.getPlayer() != null) {
RecordingQueue.addToQueue(ActionFactory.createGrow(type, block, event.getPlayer()));
} else {
RecordingQueue.addToQueue(ActionFactory.createGrow(type, block, "Environment"));
}
}
}
}
use of org.bukkit.block.BlockState in project Prism-Bukkit by prism.
the class PrismWorldEvents method onPortalCreate.
/**
* Track portal creation events.
* @param event PortalCreateEvent.
*/
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPortalCreate(final PortalCreateEvent event) {
final String type = "portal-create";
if (!Prism.getIgnore().event(type, event.getWorld())) {
return;
}
if (event.getReason() == PortalCreateEvent.CreateReason.FIRE) {
for (final BlockState newBlock : event.getBlocks()) {
// Include only the nether portal blocks that were created.
if (newBlock.getType() == Material.NETHER_PORTAL) {
final Entity e = event.getEntity();
final BlockState oldBlock = event.getWorld().getBlockAt(newBlock.getLocation()).getState();
if (e != null) {
// Run the second after the fire was placed (20 ticks), so that it is recorded after the fire.
// We have to do this because the database only records changes per second, not tick or instant,
// which can result in the fire being recorded after the nether portal blocks.
Bukkit.getScheduler().runTaskLater(Prism.getInstance(), () -> {
recordCreatePortal(event, type, newBlock, e, oldBlock);
}, 20);
}
}
}
} else if (event.getReason() == PortalCreateEvent.CreateReason.NETHER_PAIR) {
// Record both the obsidian and portal blocks that were created.
for (final BlockState newBlock : event.getBlocks()) {
final Entity e = event.getEntity();
final BlockState oldBlock = event.getWorld().getBlockAt(newBlock.getLocation()).getState();
if (e != null) {
if (newBlock.getType() == Material.NETHER_PORTAL) {
// Run the second after the fire was placed (20 ticks), so that it is recorded after the fire.
// We have to do this because the database only records changes per second, not tick or instant,
// which can result in the fire being recorded after the nether portal blocks.
Bukkit.getScheduler().runTaskLater(Prism.getInstance(), () -> {
recordCreatePortal(event, type, newBlock, e, oldBlock);
}, 20);
} else {
recordCreatePortal(event, type, newBlock, e, oldBlock);
}
}
}
}
}
use of org.bukkit.block.BlockState in project Prism-Bukkit by prism.
the class Utilities method removeMaterialsFromRadius.
/**
* Remove materials in an radius.
*
* @param materials Material array
* @param loc Location
* @param radius integer
*/
@SuppressWarnings("WeakerAccess")
public static ArrayList<BlockStateChange> removeMaterialsFromRadius(Material[] materials, final Location loc, int radius) {
final ArrayList<BlockStateChange> blockStateChanges = new ArrayList<>();
if (loc != null && radius > 0 && materials != null && materials.length > 0) {
final int x1 = loc.getBlockX();
final int y1 = loc.getBlockY();
final int z1 = loc.getBlockZ();
final World world = loc.getWorld();
for (int x = x1 - radius; x <= x1 + radius; x++) {
for (int y = y1 - radius; y <= y1 + radius; y++) {
for (int z = z1 - radius; z <= z1 + radius; z++) {
Location testLocation = new Location(world, x, y, z);
final Block b = testLocation.getBlock();
if (b.getType().equals(Material.AIR)) {
continue;
}
if (Arrays.asList(materials).contains(testLocation.getBlock().getType())) {
final BlockState originalBlock = testLocation.getBlock().getState();
testLocation.getBlock().setType(Material.AIR);
final BlockState newBlock = testLocation.getBlock().getState();
blockStateChanges.add(new BlockStateChangeImpl(originalBlock, newBlock));
}
}
}
}
}
return blockStateChanges;
}
use of org.bukkit.block.BlockState in project Prism-Bukkit by prism.
the class PrismBlockEvents method onBlockSpread.
/**
* BlockSpreadEvent.
* @param event BlockSpreadEvent
*/
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onBlockSpread(final BlockSpreadEvent event) {
// If fire, do we track fire spread? If not, do we track block-spread
String type = "block-spread";
if (event.getNewState().getType().equals(Material.FIRE)) {
if (!Prism.getIgnore().event("fire-spread")) {
return;
}
type = "fire-spread";
} else {
if (!Prism.getIgnore().event("block-spread", event.getBlock())) {
return;
}
}
final Block b = event.getBlock();
final BlockState s = event.getNewState();
RecordingQueue.addToQueue(ActionFactory.createBlockChange(type, b.getLocation(), b.getType(), b.getBlockData(), s.getType(), s.getBlockData(), "Environment"));
}
Aggregations