Search in sources :

Example 36 with BlockSnapshot

use of org.spongepowered.api.block.BlockSnapshot in project modules-extra by CubeEngine.

the class ChopListener method onChop.

@Listener
public void onChop(final ChangeBlockEvent.Break event, @First Player player) {
    if (!player.getItemInHand(HandTypes.MAIN_HAND).isPresent() || event.getCause().getContext().containsKey(EventContextKeys.PLAYER_SIMULATED)) {
        return;
    }
    ItemStack axe = player.getItemInHand(HandTypes.MAIN_HAND).orElse(null);
    if (axe == null || axe.getType() != DIAMOND_AXE || axe.get(Keys.ITEM_DURABILITY).orElse(0) <= 0 || !axe.get(Keys.ITEM_ENCHANTMENTS).orElse(emptyList()).contains(Enchantment.builder().type(EnchantmentTypes.PUNCH).level(5).build())) {
        return;
    }
    if (!module.use.check(player)) {
        return;
    }
    Sponge.getCauseStackManager().addContext(EventContextKeys.PLAYER_SIMULATED, player.getProfile());
    for (Transaction<BlockSnapshot> transaction : event.getTransactions()) {
        BlockType type = transaction.getOriginal().getState().getType();
        Location<World> orig = transaction.getOriginal().getLocation().get();
        BlockType belowType = orig.getBlockRelative(DOWN).getBlockType();
        if (isLog(type) && isSoil(belowType)) {
            TreeType treeType = transaction.getOriginal().get(TREE_TYPE).get();
            Set<Location<World>> treeBlocks = findTreeBlocks(orig, treeType);
            if (treeBlocks.isEmpty()) {
                return;
            }
            int logs = 0;
            int leaves = 0;
            Set<Location> saplings = new HashSet<>();
            for (Location<World> block : treeBlocks) {
                if (isLog(block.getBlockType())) {
                    if (!block.equals(orig)) {
                        block.getExtent().playSound(SoundTypes.BLOCK_WOOD_STEP, block.getPosition(), 1);
                    }
                    logs++;
                    block.setBlockType(BlockTypes.AIR);
                    BlockType belowTyp = block.getBlockRelative(DOWN).getBlockType();
                    if (isSoil(belowTyp)) {
                        saplings.add(block);
                    }
                }
                if (block.getBlockType() == LEAVES || block.getBlockType() == LEAVES2) {
                    block.setBlockType(BlockTypes.AIR);
                    // TODO leaves sound?
                    block.getExtent().playSound(SoundTypes.BLOCK_GRASS_STEP, block.getPosition(), 1);
                    leaves++;
                }
            }
            ItemStack log = ItemStack.builder().itemType(type.getItem().get()).quantity(logs).build();
            log.offer(TREE_TYPE, treeType);
            int apples = 0;
            if (treeType == JUNGLE) {
                leaves = leaves / 40;
            } else {
                if (treeType == DARK_OAK || treeType == OAK) {
                    apples = leaves / 200;
                }
                leaves = leaves / 20;
            }
            if (leaves == 0) {
                leaves = 1;
            }
            BlockState sapState = BlockTypes.SAPLING.getDefaultState().with(TREE_TYPE, treeType).get();
            if (this.module.autoplant.check(player)) {
                leaves -= saplings.size();
                leaves = Math.max(0, leaves);
                transaction.setCustom(sapState.snapshotFor(transaction.getOriginal().getLocation().get()));
                saplings.forEach(l -> l.setBlock(sapState));
            }
            final int uses = axe.get(Keys.ITEM_DURABILITY).get() - logs;
            axe.offer(Keys.ITEM_DURABILITY, uses);
            player.setItemInHand(HandTypes.MAIN_HAND, axe);
            World world = player.getWorld();
            Entity itemEntity;
            Sponge.getCauseStackManager().removeContext(EventContextKeys.PLAYER_SIMULATED);
            Sponge.getCauseStackManager().pushCause(player);
            if (apples > 0) {
                ItemStack apple = ItemStack.builder().itemType(APPLE).quantity(apples).build();
                itemEntity = world.createEntity(ITEM, orig.getPosition());
                itemEntity.offer(REPRESENTED_ITEM, apple.createSnapshot());
                world.spawnEntity(itemEntity);
            }
            if (leaves > 0) {
                ItemStack sap = ItemStack.builder().fromBlockState(sapState).build();
                sap.setQuantity(leaves);
                itemEntity = world.createEntity(ITEM, orig.getPosition());
                itemEntity.offer(REPRESENTED_ITEM, sap.createSnapshot());
                world.spawnEntity(itemEntity);
            }
            itemEntity = world.createEntity(ITEM, orig.getPosition());
            itemEntity.offer(REPRESENTED_ITEM, log.createSnapshot());
            world.spawnEntity(itemEntity);
            return;
        }
    }
}
Also used : TreeType(org.spongepowered.api.data.type.TreeType) Entity(org.spongepowered.api.entity.Entity) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) World(org.spongepowered.api.world.World) BlockState(org.spongepowered.api.block.BlockState) BlockType(org.spongepowered.api.block.BlockType) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Location(org.spongepowered.api.world.Location) HashSet(java.util.HashSet) Listener(org.spongepowered.api.event.Listener)

Example 37 with BlockSnapshot

use of org.spongepowered.api.block.BlockSnapshot in project SpongeVanilla by SpongePowered.

the class MixinEntityPlayer method wakeUpPlayer.

/**
 * @author Minecrell
 * @reason Implements the post sleeping events.
 */
@Overwrite
public void wakeUpPlayer(boolean immediately, boolean updateWorldFlag, boolean setSpawn) {
    // Sponge start (Set size after event call)
    // this.setSize(0.6F, 1.8F);
    Transform<org.spongepowered.api.world.World> newLocation = null;
    // Sponge end
    IBlockState iblockstate = this.world.getBlockState(this.bedLocation);
    if (this.bedLocation != null && iblockstate.getBlock() == Blocks.BED) {
        // Sponge start (Change block state after event call)
        // this.world.setBlockState(this.playerLocation, iblockstate.withProperty(BlockBed.OCCUPIED, Boolean.valueOf(false)), 4);
        // Sponge end
        BlockPos blockpos = BlockBed.getSafeExitLocation(this.world, this.bedLocation, 0);
        if (blockpos == null) {
            blockpos = this.bedLocation.up();
        }
        // Sponge start (Store position for later)
        /*this.setPosition((double) ((float) blockpos.getX() + 0.5F), (double) ((float) blockpos.getY() + 0.1F),
                    (double) ((float) blockpos.getZ() + 0.5F));*/
        newLocation = getTransform().setPosition(new Vector3d(blockpos.getX() + 0.5F, blockpos.getY() + 0.1F, blockpos.getZ() + 0.5F));
    // Sponge end
    }
    // Sponge start
    BlockSnapshot bed = getWorld().createSnapshot(VecHelper.toVector3i(this.bedLocation));
    try (final CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
        Sponge.getCauseStackManager().pushCause(this);
        SleepingEvent.Post event = SpongeEventFactory.createSleepingEventPost(Sponge.getCauseStackManager().getCurrentCause(), bed, Optional.ofNullable(newLocation), this, setSpawn);
        if (SpongeImpl.postEvent(event)) {
            return;
        }
        // Moved from above
        this.setSize(0.6F, 1.8F);
        if (newLocation != null) {
            // Set property only if bed still existsthis.world.setBlockState(this.bedLocation, iblockstate.withProperty(BlockBed.OCCUPIED, false), 4);}
            // Teleport player
            event.getSpawnTransform().ifPresent(this::setLocationAndAngles);
            // Sponge end
            this.sleeping = false;
            if (!this.world.isRemote && updateWorldFlag) {
                this.world.updateAllPlayersSleepingFlag();
            }
            this.sleepTimer = immediately ? 0 : 100;
            if (setSpawn) {
                this.setSpawnPoint(this.bedLocation, false);
            }
            // Sponge start
            SpongeImpl.postEvent(SpongeEventFactory.createSleepingEventFinish(Sponge.getCauseStackManager().getCurrentCause(), bed, this));
        }
    // Sponge end
    }
}
Also used : IBlockState(net.minecraft.block.state.IBlockState) Vector3d(com.flowpowered.math.vector.Vector3d) CauseStackManager(org.spongepowered.api.event.CauseStackManager) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) BlockPos(net.minecraft.util.math.BlockPos) SleepingEvent(org.spongepowered.api.event.action.SleepingEvent) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Example 38 with BlockSnapshot

use of org.spongepowered.api.block.BlockSnapshot in project RedProtect by FabioZumbi12.

the class RPContainer method canOpen.

public boolean canOpen(BlockSnapshot b, Player p) {
    if (!RedProtect.get().cfgs.getBool("private.use")) {
        return true;
    }
    List<Direction> dirs = Arrays.asList(Direction.EAST, Direction.NORTH, Direction.SOUTH, Direction.WEST);
    String blocktype = b.getState().getType().getName();
    Location<World> loc = b.getLocation().get();
    World w = loc.getExtent();
    List<String> blocks = RedProtect.get().cfgs.getStringList("private.allowed-blocks");
    if (blocks.contains(blocktype)) {
        for (Direction dir : dirs) {
            Location<World> loc1 = getRelative(loc, dir);
            if (isSign(loc1.getBlockType())) {
                BlockSnapshot sign1 = w.createSnapshot(loc1.getBlockPosition());
                if (!validateOpenBlock(sign1, p)) {
                    return false;
                }
            }
            if (blocks.contains(loc1.getBlockType().getName()) && loc1.getBlockType().equals(b.getState().getType())) {
                for (Direction dir2 : dirs) {
                    Location<World> loc3 = getRelative(loc1, dir2);
                    if (isSign(loc3.getBlockType())) {
                        BlockSnapshot sign2 = w.createSnapshot(loc3.getBlockPosition());
                        if (!validateOpenBlock(sign2, p)) {
                            return false;
                        }
                    }
                }
            }
        }
    }
    return true;
}
Also used : BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) World(org.spongepowered.api.world.World) Direction(org.spongepowered.api.util.Direction)

Example 39 with BlockSnapshot

use of org.spongepowered.api.block.BlockSnapshot in project RedProtect by FabioZumbi12.

the class RPContainer method canWorldBreak.

public boolean canWorldBreak(BlockSnapshot b) {
    if (!RedProtect.get().cfgs.getBool("private.use")) {
        return true;
    }
    Region reg = RedProtect.get().rm.getTopRegion(b.getLocation().get());
    if (reg == null && !RedProtect.get().cfgs.getBool("private.allow-outside")) {
        return true;
    }
    List<Direction> dirs = Arrays.asList(Direction.EAST, Direction.NORTH, Direction.SOUTH, Direction.WEST, Direction.UP, Direction.DOWN);
    String blocktype = b.getState().getType().getName();
    Location<World> loc = b.getLocation().get();
    World w = loc.getExtent();
    List<String> blocks = RedProtect.get().cfgs.getStringList("private.allowed-blocks");
    if (isSign(loc.getBlockType())) {
        BlockSnapshot sign1 = w.createSnapshot(loc.getBlockPosition());
        if (!validatePrivateSign(sign1)) {
            return false;
        }
    }
    if (blocks.contains(blocktype)) {
        for (Direction dir : dirs) {
            Location<World> loc1 = getRelative(loc, dir);
            if (isSign(loc1.getBlockType())) {
                BlockSnapshot sign1 = w.createSnapshot(loc1.getBlockPosition());
                if (!validatePrivateSign(sign1)) {
                    return false;
                }
            }
            if (blocks.contains(loc1.getBlockType().getName()) && loc1.getBlockType().equals(b.getState().getType())) {
                for (Direction dir2 : dirs) {
                    Location<World> loc3 = getRelative(loc1, dir2);
                    if (isSign(loc3.getBlockType())) {
                        BlockSnapshot sign2 = w.createSnapshot(loc3.getBlockPosition());
                        if (!validatePrivateSign(sign2)) {
                            return false;
                        }
                    }
                }
            }
        }
    }
    return true;
}
Also used : BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) World(org.spongepowered.api.world.World) Direction(org.spongepowered.api.util.Direction)

Example 40 with BlockSnapshot

use of org.spongepowered.api.block.BlockSnapshot in project RedProtect by FabioZumbi12.

the class RPDoor method ChangeDoor.

public static void ChangeDoor(BlockSnapshot b, Region r) {
    if ((!RedProtect.get().cfgs.isFlagEnabled("smart-door") && !RedProtect.get().cfgs.getBool("flags.smart-door")) || !r.getFlagBool("smart-door")) {
        return;
    }
    Location<World> loc = b.getLocation().get();
    World w = loc.getExtent();
    if (isDoor(b)) {
        changeDoorState(b);
        if (getDoorState(b)) {
            w.playSound(SoundTypes.BLOCK_CHEST_CLOSE, loc.getPosition(), 1);
        } else {
            w.playSound(SoundTypes.BLOCK_CHEST_OPEN, loc.getPosition(), 1);
        }
    }
    // check side block if is door
    BlockSnapshot[] block = new BlockSnapshot[4];
    block[0] = w.createSnapshot(loc.getBlockX() + 1, loc.getBlockY(), loc.getBlockZ());
    block[1] = w.createSnapshot(loc.getBlockX() - 1, loc.getBlockY(), loc.getBlockZ());
    block[2] = w.createSnapshot(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ() + 1);
    block[3] = w.createSnapshot(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ() - 1);
    for (BlockSnapshot b2 : block) {
        if (b.getExtendedState().getType().getName().equals(b2.getExtendedState().getType().getName())) {
            // b.getWorld().playEffect(b.getLocation(), Effect.DOOR_TOGGLE, 0);
            changeDoorState(b);
            break;
        }
    }
}
Also used : BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) World(org.spongepowered.api.world.World)

Aggregations

BlockSnapshot (org.spongepowered.api.block.BlockSnapshot)144 World (org.spongepowered.api.world.World)64 Listener (org.spongepowered.api.event.Listener)61 Location (org.spongepowered.api.world.Location)27 BlockState (org.spongepowered.api.block.BlockState)21 BlockPos (net.minecraft.util.math.BlockPos)20 Entity (org.spongepowered.api.entity.Entity)20 CauseStackManager (org.spongepowered.api.event.CauseStackManager)19 Region (br.net.fabiozumbi12.RedProtect.Sponge.Region)18 IBlockState (net.minecraft.block.state.IBlockState)18 Transaction (org.spongepowered.api.data.Transaction)17 BlockType (org.spongepowered.api.block.BlockType)15 Direction (org.spongepowered.api.util.Direction)15 SpongeBlockSnapshot (org.spongepowered.common.block.SpongeBlockSnapshot)14 Vector3d (com.flowpowered.math.vector.Vector3d)13 ArrayList (java.util.ArrayList)13 ChangeBlockEvent (org.spongepowered.api.event.block.ChangeBlockEvent)13 Player (org.spongepowered.api.entity.living.player.Player)12 User (org.spongepowered.api.entity.living.player.User)11 LocatableBlock (org.spongepowered.api.world.LocatableBlock)11