use of net.glowstone.chunk.GlowChunk in project Glowstone by GlowstoneMC.
the class BlockPiston method onRedstoneUpdate.
@Override
public void onRedstoneUpdate(GlowBlock me) {
PistonBaseMaterial piston = (PistonBaseMaterial) me.getState().getData();
BlockFace pistonBlockFace = piston.getFacing();
int rawFace = BlockDirectional.getRawFace(pistonBlockFace);
BlockActionMessage message = new BlockActionMessage(me.getX(), me.getY(), me.getZ(), me.isBlockIndirectlyPowered() ? 0 : 1, rawFace, me.getType().getId());
GlowChunk chunk = me.getChunk();
GlowChunk.Key chunkKey = GlowChunk.Key.of(chunk.getX(), chunk.getZ());
GlowWorld world = me.getWorld();
if (me.isBlockIndirectlyPowered() && !isPistonExtended(me)) {
List<Block> blocksToMove = new ArrayList<>();
List<Block> blocksToBreak = new ArrayList<>();
boolean allowMovement = addBlock(me, pistonBlockFace, me.getRelative(pistonBlockFace), pistonBlockFace.getOppositeFace(), blocksToMove, blocksToBreak);
if (!allowMovement) {
return;
}
BlockPistonExtendEvent event = EventFactory.getInstance().callEvent(new BlockPistonExtendEvent(me, blocksToMove, pistonBlockFace));
if (event.isCancelled()) {
return;
}
world.getRawPlayers().stream().filter(player -> player.canSeeChunk(chunkKey)).forEach(player -> player.getSession().send(message));
world.playSound(me.getLocation(), Sound.BLOCK_PISTON_EXTEND, SoundCategory.BLOCKS, 0.5f, 0.75f);
// extended state for piston base
me.setData((byte) (me.getData() | 0x08));
performMovement(pistonBlockFace, blocksToMove, blocksToBreak);
// set piston head block when extended
setType(me.getRelative(pistonBlockFace), Material.MOVING_PISTON, sticky ? me.getData() | 0x08 : rawFace);
return;
}
if (!isPistonExtended(me)) {
return;
}
List<Block> blocksToMove = new ArrayList<>();
List<Block> blocksToBreak = new ArrayList<>();
if (sticky) {
addBlock(me, pistonBlockFace.getOppositeFace(), me.getRelative(pistonBlockFace, 2), pistonBlockFace.getOppositeFace(), blocksToMove, blocksToBreak);
}
BlockPistonRetractEvent event = EventFactory.getInstance().callEvent(new BlockPistonRetractEvent(me, blocksToMove, pistonBlockFace));
if (event.isCancelled()) {
return;
}
world.getRawPlayers().stream().filter(player -> player.canSeeChunk(chunkKey)).forEach(player -> player.getSession().send(message));
world.playSound(me.getLocation(), Sound.BLOCK_PISTON_CONTRACT, SoundCategory.BLOCKS, 0.5f, 0.75f);
// normal state for piston
setType(me, me.getType(), me.getData() & ~0x08);
if (sticky && blocksToMove.size() > 0) {
performMovement(pistonBlockFace.getOppositeFace(), blocksToMove, blocksToBreak);
} else {
// remove piston head
me.getRelative(pistonBlockFace).setTypeIdAndData(0, (byte) 0, true);
}
}
use of net.glowstone.chunk.GlowChunk in project Glowstone by GlowstoneMC.
the class GlowBlock method setData.
public void setData(byte data, boolean applyPhysics) {
byte oldData = getData();
((GlowChunk) world.getChunkAt(this)).setMetaData(x & 0xf, z & 0xf, y, data);
if (applyPhysics) {
applyPhysics(getType(), getType(), oldData, data);
}
GlowChunk.Key key = GlowChunk.Key.of(x >> 4, z >> 4);
BlockDataManager blockDataManager = ((GlowServer) Bukkit.getServer()).getBlockDataManager();
StatefulBlockData blockData = blockDataManager.createBlockData(getType());
BlockChangeMessage bcmsg = new BlockChangeMessage(x, y, z, blockDataManager.convertToBlockId(blockData), data);
world.broadcastBlockChangeInRange(key, bcmsg);
}
use of net.glowstone.chunk.GlowChunk in project Glowstone by GlowstoneMC.
the class GlowWorld method pulseTickMap.
private void pulseTickMap() {
ItemTable itemTable = ItemTable.instance();
for (Location location : tickMap) {
GlowChunk chunk = (GlowChunk) location.getChunk();
if (!chunk.isLoaded()) {
continue;
}
int typeId = chunk.getType(location.getBlockX() & 0xF, location.getBlockZ() & 0xF, location.getBlockY());
BlockType type = itemTable.getBlock(typeId);
if (type == null) {
cancelPulse(location);
continue;
}
GlowBlock block = new GlowBlock(chunk, location.getBlockX(), location.getBlockY(), location.getBlockZ());
int speed = type.getPulseTickSpeed(block);
boolean once = type.isPulseOnce(block);
if (speed == 0) {
continue;
}
if (fullTime % speed == 0) {
type.receivePulse(block);
if (once) {
cancelPulse(location);
}
}
}
}
use of net.glowstone.chunk.GlowChunk in project Glowstone by GlowstoneMC.
the class GlowWorld method save.
/**
* Saves world to disk synchronously or asynchronously.
*
* @param async if true, save asynchronously
*/
public void save(boolean async) {
EventFactory.getInstance().callEvent(new WorldSaveEvent(this));
// save metadata
writeWorldData(async);
// save chunkManager
maybeAsync(async, () -> {
for (GlowChunk chunk : chunkManager.getLoadedChunks()) {
chunkManager.performSave(chunk);
}
});
// save players
for (GlowPlayer player : getRawPlayers()) {
player.saveData(async);
}
}
use of net.glowstone.chunk.GlowChunk in project Glowstone by GlowstoneMC.
the class GlowWorld method getChunkAtAsync.
@NotNull
@Override
public CompletableFuture<Chunk> getChunkAtAsync(int x, int z, boolean gen, boolean urgent) {
// TODO: Support 'urgent'
CompletableFuture<Chunk> future = new CompletableFuture<>();
ServerProvider.getServer().getScheduler().runTaskAsynchronously(null, () -> {
GlowChunk chunk = chunkManager.getChunk(x, z);
chunk.load(gen);
future.complete(chunk);
});
return future;
}
Aggregations