Search in sources :

Example 1 with GlowChunk

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);
    }
}
Also used : BlockPistonExtendEvent(org.bukkit.event.block.BlockPistonExtendEvent) GlowBlock(net.glowstone.block.GlowBlock) PistonBaseMaterial(org.bukkit.material.PistonBaseMaterial) Getter(lombok.Getter) ItemTable(net.glowstone.block.ItemTable) GlowPlayer(net.glowstone.entity.GlowPlayer) BlockFace(org.bukkit.block.BlockFace) MaterialValueManager(net.glowstone.block.MaterialValueManager) ArrayList(java.util.ArrayList) Block(org.bukkit.block.Block) BlockPistonRetractEvent(org.bukkit.event.block.BlockPistonRetractEvent) Location(org.bukkit.Location) GameRules(net.glowstone.constants.GameRules) World(org.bukkit.World) BlockActionMessage(net.glowstone.net.message.play.game.BlockActionMessage) GlowWorld(net.glowstone.GlowWorld) Material(org.bukkit.Material) SoundCategory(org.bukkit.SoundCategory) EventFactory(net.glowstone.EventFactory) GlowChunk(net.glowstone.chunk.GlowChunk) Collection(java.util.Collection) Sound(org.bukkit.Sound) ItemStack(org.bukkit.inventory.ItemStack) List(java.util.List) PistonMoveBehavior(net.glowstone.block.PistonMoveBehavior) BlockFace(org.bukkit.block.BlockFace) ArrayList(java.util.ArrayList) BlockPistonRetractEvent(org.bukkit.event.block.BlockPistonRetractEvent) BlockPistonExtendEvent(org.bukkit.event.block.BlockPistonExtendEvent) GlowWorld(net.glowstone.GlowWorld) GlowBlock(net.glowstone.block.GlowBlock) Block(org.bukkit.block.Block) PistonBaseMaterial(org.bukkit.material.PistonBaseMaterial) BlockActionMessage(net.glowstone.net.message.play.game.BlockActionMessage) GlowChunk(net.glowstone.chunk.GlowChunk)

Example 2 with GlowChunk

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);
}
Also used : BlockChangeMessage(net.glowstone.net.message.play.game.BlockChangeMessage) GlowServer(net.glowstone.GlowServer) BlockDataManager(net.glowstone.block.data.BlockDataManager) GlowChunk(net.glowstone.chunk.GlowChunk) StatefulBlockData(net.glowstone.block.data.states.StatefulBlockData)

Example 3 with GlowChunk

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);
            }
        }
    }
}
Also used : GlowBlock(net.glowstone.block.GlowBlock) ItemTable(net.glowstone.block.ItemTable) BlockType(net.glowstone.block.blocktype.BlockType) Location(org.bukkit.Location) GlowChunk(net.glowstone.chunk.GlowChunk)

Example 4 with GlowChunk

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);
    }
}
Also used : GlowPlayer(net.glowstone.entity.GlowPlayer) WorldSaveEvent(org.bukkit.event.world.WorldSaveEvent) GlowChunk(net.glowstone.chunk.GlowChunk)

Example 5 with GlowChunk

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;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) Chunk(org.bukkit.Chunk) GlowChunk(net.glowstone.chunk.GlowChunk) GlowChunk(net.glowstone.chunk.GlowChunk) NotNull(org.jetbrains.annotations.NotNull) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull)

Aggregations

GlowChunk (net.glowstone.chunk.GlowChunk)13 GlowBlock (net.glowstone.block.GlowBlock)5 ArrayList (java.util.ArrayList)4 Location (org.bukkit.Location)4 GlowWorld (net.glowstone.GlowWorld)3 ItemTable (net.glowstone.block.ItemTable)3 BlockChangeMessage (net.glowstone.net.message.play.game.BlockChangeMessage)3 Chunk (org.bukkit.Chunk)3 Material (org.bukkit.Material)3 World (org.bukkit.World)3 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)2 Collection (java.util.Collection)2 List (java.util.List)2 Getter (lombok.Getter)2 EventFactory (net.glowstone.EventFactory)2 BlockEntity (net.glowstone.block.entity.BlockEntity)2 Key (net.glowstone.chunk.GlowChunk.Key)2 GameRules (net.glowstone.constants.GameRules)2 GlowPlayer (net.glowstone.entity.GlowPlayer)2 NamespacedKey (org.bukkit.NamespacedKey)2