Search in sources :

Example 21 with BlockType

use of org.spongepowered.api.block.BlockType in project guardian by ichorpowered.

the class MaterialCapture method update.

@Override
public void update(@Nonnull PlayerEntry entry, @Nonnull CaptureContainer captureContainer) {
    if (!entry.getEntity(Player.class).isPresent() || !captureContainer.get(GuardianSequence.INITIAL_LOCATION).isPresent())
        return;
    final Player player = entry.getEntity(Player.class).get();
    final Location<World> location = player.getLocation();
    final MapValue<String, Integer> activeMaterials = GuardianMapValue.builder(MaterialCapture.ACTIVE_MATERIAL_TICKS).defaultElement(Maps.newHashMap()).element(Maps.newHashMap()).create();
    activeMaterials.put(GAS, 0);
    activeMaterials.put(LIQUID, 0);
    activeMaterials.put(SOLID, 0);
    captureContainer.offerIfEmpty(activeMaterials);
    captureContainer.offerIfEmpty(GuardianValue.builder(MaterialCapture.SPEED_MODIFIER).defaultElement(1d).element(1d).create());
    final Value<Double> playerBoxWidth = ContentUtil.getFirst(ContentKeys.BOX_PLAYER_WIDTH, entry, this.getDetection().getContentContainer()).orElse(GuardianValue.empty());
    final Value<Double> playerBoxHeight = ContentUtil.getFirst(ContentKeys.BOX_PLAYER_HEIGHT, entry, this.getDetection().getContentContainer()).orElse(GuardianValue.empty());
    final Value<Double> playerBoxSafety = ContentUtil.getFirst(ContentKeys.BOX_PLAYER_SAFETY, entry, this.getDetection().getContentContainer()).orElse(GuardianValue.empty());
    final double playerWidth = playerBoxWidth.getDirect().orElse(1.2) + playerBoxSafety.getDirect().orElse(0.05);
    final double playerHeight = playerBoxHeight.getDirect().orElse(1.8) + playerBoxSafety.getDirect().orElse(0.05);
    final boolean isSneaking = player.get(Keys.IS_SNEAKING).isPresent() && player.get(Keys.IS_SNEAKING).get();
    final BoundingBox playerBox = WorldUtil.getBoundingBox(playerWidth, isSneaking ? (playerHeight - 0.15) : playerHeight);
    if (!WorldUtil.containsBlocksUnder(location, playerBox, 1.25)) {
        final double gasSpeed = this.matterSpeed.get(GAS);
        captureContainer.getValue(MaterialCapture.SPEED_MODIFIER).ifPresent(value -> value.transform(original -> original * gasSpeed));
        captureContainer.getValue(MaterialCapture.ACTIVE_MATERIAL_TICKS).ifPresent(value -> value.put(GAS, value.get().get(GAS) + 1));
    } else if (WorldUtil.anyLiquidAtDepth(location, playerBox, 1d) || WorldUtil.anyLiquidAtDepth(location, playerBox, 0) || WorldUtil.anyLiquidAtDepth(location, playerBox, isSneaking ? -(playerHeight - 0.25) : -playerHeight)) {
        final double liquidSpeed = this.matterSpeed.get(LIQUID);
        captureContainer.getValue(MaterialCapture.SPEED_MODIFIER).ifPresent(value -> value.transform(original -> original * liquidSpeed));
        captureContainer.getValue(MaterialCapture.ACTIVE_MATERIAL_TICKS).ifPresent(value -> value.put(LIQUID, value.get().get(LIQUID) + 1));
    } else {
        final List<BlockType> surroundingBlockTypes = WorldUtil.getBlocksUnder(location, playerBox, 1.25);
        for (final BlockType blockType : surroundingBlockTypes) {
            final double speedModifier = this.materialSpeed.getOrDefault(blockType.getName().toLowerCase(), this.matterSpeed.get(SOLID));
            captureContainer.getValue(MaterialCapture.SPEED_MODIFIER).ifPresent(value -> value.transform(original -> original * speedModifier));
        }
        captureContainer.getValue(MaterialCapture.ACTIVE_MATERIAL_TICKS).ifPresent(value -> value.put(SOLID, value.get().get(SOLID) + 1));
    }
}
Also used : ContentKeys(com.ichorpowered.guardianapi.content.ContentKeys) Keys(org.spongepowered.api.data.key.Keys) GuardianCaptureKey(com.ichorpowered.guardian.sequence.capture.GuardianCaptureKey) TypeToken(com.google.common.reflect.TypeToken) GuardianValue(com.ichorpowered.guardian.util.item.mutable.GuardianValue) GuardianMapValue(com.ichorpowered.guardian.util.item.mutable.GuardianMapValue) Value(com.ichorpowered.guardianapi.util.item.value.mutable.Value) Map(java.util.Map) Nonnull(javax.annotation.Nonnull) Location(org.spongepowered.api.world.Location) GuardianSequence(com.ichorpowered.guardian.sequence.GuardianSequence) ContentUtil(com.ichorpowered.guardian.util.ContentUtil) CaptureKey(com.ichorpowered.guardianapi.detection.capture.CaptureKey) Maps(com.google.common.collect.Maps) BoundingBox(com.ichorpowered.guardian.util.entity.BoundingBox) Detection(com.ichorpowered.guardianapi.detection.Detection) List(java.util.List) AbstractCapture(com.ichorpowered.guardian.sequence.capture.AbstractCapture) CaptureContainer(com.ichorpowered.guardianapi.detection.capture.CaptureContainer) MapValue(com.ichorpowered.guardianapi.util.item.value.mutable.MapValue) World(org.spongepowered.api.world.World) BlockType(org.spongepowered.api.block.BlockType) PlayerEntry(com.ichorpowered.guardianapi.entry.entity.PlayerEntry) WorldUtil(com.ichorpowered.guardian.util.WorldUtil) Player(org.spongepowered.api.entity.living.player.Player) Player(org.spongepowered.api.entity.living.player.Player) World(org.spongepowered.api.world.World) BlockType(org.spongepowered.api.block.BlockType) BoundingBox(com.ichorpowered.guardian.util.entity.BoundingBox) List(java.util.List)

Example 22 with BlockType

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

the class ChopListener method isLog.

private boolean isLog(Location<World> block, TreeType species) {
    BlockType type = block.getBlockType();
    boolean match = module.getConfig().logTypes.contains(type);
    return match && (!block.supports(TREE_TYPE) || block.get(TREE_TYPE).get() == species);
}
Also used : BlockType(org.spongepowered.api.block.BlockType)

Example 23 with BlockType

use of org.spongepowered.api.block.BlockType 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 24 with BlockType

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

the class ChopListener method isLeaf.

private boolean isLeaf(Location<World> block, TreeType species) {
    BlockType type = block.getBlockType();
    boolean match = module.getConfig().leafTypes.contains(type);
    return match && (!block.supports(TREE_TYPE) || block.get(TREE_TYPE).get() == species);
}
Also used : BlockType(org.spongepowered.api.block.BlockType)

Example 25 with BlockType

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

the class RPCommands method handletp.

private static void handletp(Player p, String rname, String wname, Player play) {
    World w = null;
    if (RedProtect.get().serv.getWorld(wname).isPresent()) {
        w = RedProtect.get().serv.getWorld(wname).get();
    }
    if (w == null) {
        RPLang.sendMessage(p, "cmdmanager.region.invalidworld");
        return;
    }
    Region region = RedProtect.get().rm.getRegion(rname, w);
    if (region == null) {
        RPLang.sendMessage(p, RPLang.get("cmdmanager.region.doesntexist") + ": " + rname);
        return;
    }
    if (play == null) {
        if (!RedProtect.get().ph.hasRegionPermMember(p, "teleport", region)) {
            RPLang.sendMessage(p, "no.permission");
            return;
        }
    } else {
        if (!RedProtect.get().ph.hasPerm(p, "redprotect.teleport.other")) {
            RPLang.sendMessage(p, "no.permission");
            return;
        }
    }
    Location<World> loc = null;
    if (region.getTPPoint() != null) {
        loc = new Location<>(w, region.getTPPoint().getBlockX() + 0.500, region.getTPPoint().getBlockY(), region.getTPPoint().getBlockZ() + 0.500);
    } else {
        int limit = w.getBlockMax().getY();
        if (w.getDimension().equals(DimensionTypes.NETHER)) {
            limit = 124;
        }
        for (int i = limit; i > 0; i--) {
            BlockType mat = w.createSnapshot(region.getCenterX(), i, region.getCenterZ()).getState().getType();
            BlockType mat1 = w.createSnapshot(region.getCenterX(), i + 1, region.getCenterZ()).getState().getType();
            BlockType mat2 = w.createSnapshot(region.getCenterX(), i + 2, region.getCenterZ()).getState().getType();
            if (!mat.equals(BlockTypes.LAVA) && !mat.equals(BlockTypes.AIR) && mat1.equals(BlockTypes.AIR) && mat2.equals(BlockTypes.AIR)) {
                loc = new Location<>(w, region.getCenterX() + 0.500, i + 1, region.getCenterZ() + 0.500);
                break;
            }
        }
    }
    if (loc != null) {
        if (play != null) {
            play.setLocation(loc);
            RPLang.sendMessage(play, RPLang.get("cmdmanager.region.teleport") + " " + rname);
            RPLang.sendMessage(p, RPLang.get("cmdmanager.region.tpother") + " " + rname);
        } else {
            tpWait(p, loc, rname);
        }
    }
}
Also used : BlockType(org.spongepowered.api.block.BlockType) World(org.spongepowered.api.world.World)

Aggregations

BlockType (org.spongepowered.api.block.BlockType)91 World (org.spongepowered.api.world.World)34 BlockState (org.spongepowered.api.block.BlockState)23 Listener (org.spongepowered.api.event.Listener)21 Location (org.spongepowered.api.world.Location)17 BlockSnapshot (org.spongepowered.api.block.BlockSnapshot)16 ItemStack (org.spongepowered.api.item.inventory.ItemStack)14 Vector3i (com.flowpowered.math.vector.Vector3i)12 Optional (java.util.Optional)12 Player (org.spongepowered.api.entity.living.player.Player)12 List (java.util.List)10 Set (java.util.Set)10 Direction (org.spongepowered.api.util.Direction)10 Vector3d (com.flowpowered.math.vector.Vector3d)9 ArrayList (java.util.ArrayList)9 Keys (org.spongepowered.api.data.key.Keys)9 Sponge (org.spongepowered.api.Sponge)8 LanternBlockType (org.lanternpowered.server.block.LanternBlockType)7 Text (org.spongepowered.api.text.Text)7 HashMap (java.util.HashMap)6