Search in sources :

Example 66 with Listener

use of org.spongepowered.api.event.Listener 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 67 with Listener

use of org.spongepowered.api.event.Listener in project modules-extra by CubeEngine.

the class HideListener method onJoin.

@Listener
public void onJoin(ClientConnectionEvent.Join event) {
    Player player = event.getTargetEntity();
    if (module.isHidden(player)) {
        module.hidePlayer(player, true);
    }
    if (player.hasPermission(module.perms().AUTO_HIDE.getId())) {
        event.setMessageCancelled(true);
        module.hidePlayer(player, true);
        i18n.send(player, POSITIVE, "You were automatically hidden!");
    }
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) Listener(org.spongepowered.api.event.Listener)

Example 68 with Listener

use of org.spongepowered.api.event.Listener in project modules-extra by CubeEngine.

the class FlyListener method playerInteract.

@Listener
public void playerInteract(final InteractBlockEvent.Secondary event, @First Player player) {
    if (!player.getItemInHand(HandTypes.MAIN_HAND).map(i -> i.getType().equals(ItemTypes.FEATHER)).orElse(false)) {
        return;
    }
    if (!player.hasPermission(FLY_FEATHER.getId())) {
        i18n.send(player, NEGATIVE, "You dont have permission to use this!");
        // Disable when player is flying
        player.offer(Keys.CAN_FLY, false);
        player.offer(Keys.IS_FLYING, false);
        return;
    }
    // I Believe I Can Fly ...
    player.offer(Keys.CAN_FLY, !player.get(Keys.CAN_FLY).get());
    if (player.get(Keys.CAN_FLY).get()) {
        final ItemStack feather = ItemStack.of(ItemTypes.FEATHER, -1);
        player.getInventory().query(QueryOperationTypes.ITEM_STACK_IGNORE_QUANTITY.of(feather)).poll(1);
        player.setVelocity(player.getVelocity().add(0, 1, 0));
        // make sure the player stays flying
        player.setLocation(player.getLocation().add(0, 0.05, 0));
        player.offer(Keys.IS_FLYING, true);
        i18n.send(player, POSITIVE, "You can now fly!");
        Task flymore = new Task(Fly.class, tm) {

            public // 2 feather/min
            void run() {
                if (!player.get(Keys.IS_FLYING).get()) {
                    player.offer(Keys.CAN_FLY, false);
                    this.cancelTask();
                    return;
                }
                if (player.getInventory().contains(ItemTypes.FEATHER)) {
                    player.getInventory().query(feather).poll(1);
                } else {
                    player.offer(Keys.CAN_FLY, false);
                    this.cancelTask();
                }
            }
        };
        flymore.scheduleAsyncRepeatingTask(1000 * 30, 1000 * 30);
        Task oldTask = this.tasks.put(player, flymore);
        if (oldTask != null) {
            oldTask.cancelTask();
        }
    } else {
        // or not
        player.offer(Keys.FALL_DISTANCE, 0f);
        i18n.send(player, NEUTRAL, "You cannot fly anymore!");
    }
}
Also used : Task(org.cubeengine.libcube.service.task.Task) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Listener(org.spongepowered.api.event.Listener)

Example 69 with Listener

use of org.spongepowered.api.event.Listener in project modules-extra by CubeEngine.

the class Fun method onEnable.

@Listener
public void onEnable(GamePreInitializationEvent event) {
    cm.addCommands(this, new ThrowCommands(this, em, pm, tm, entityMatcher, i18n));
    cm.addCommands(this, new NukeCommand(this, i18n, em));
    cm.addCommands(this, new PlayerCommands(this, i18n, materialMatcher));
    cm.addCommands(this, new DiscoCommand(this, i18n, tm));
    cm.addCommands(this, new InvasionCommand(this, i18n, entityMatcher));
    cm.addCommands(this, new RocketCommand(this, em, tm));
}
Also used : DiscoCommand(org.cubeengine.module.fun.commands.DiscoCommand) NukeCommand(org.cubeengine.module.fun.commands.NukeCommand) ThrowCommands(org.cubeengine.module.fun.commands.ThrowCommands) PlayerCommands(org.cubeengine.module.fun.commands.PlayerCommands) InvasionCommand(org.cubeengine.module.fun.commands.InvasionCommand) RocketCommand(org.cubeengine.module.fun.commands.RocketCommand) Listener(org.spongepowered.api.event.Listener)

Example 70 with Listener

use of org.spongepowered.api.event.Listener in project SkinsRestorerX by DoNotSpamPls.

the class SkinsRestorer method onInitialize.

@Listener
public void onInitialize(GameInitializationEvent e) {
    instance = this;
    directory = Sponge.getGame().getConfigManager().getPluginConfig(this).getDirectory().toString();
    try {
        reloadConfigs();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    if (!Sponge.getServer().getOnlineMode())
        Sponge.getEventManager().registerListener(this, ClientConnectionEvent.Login.class, new LoginListener());
    CommandSpec skinCommand = CommandSpec.builder().description(Text.of("Set your skin")).arguments(GenericArguments.onlyOne(GenericArguments.string(Text.of("skin")))).executor(new SkinCommand()).build();
    Sponge.getCommandManager().register(this, skinCommand, "skin");
}
Also used : LoginListener(skinsrestorer.sponge.listeners.LoginListener) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) SkinCommand(skinsrestorer.sponge.commands.SkinCommand) LoginListener(skinsrestorer.sponge.listeners.LoginListener) Listener(org.spongepowered.api.event.Listener)

Aggregations

Listener (org.spongepowered.api.event.Listener)360 Player (org.spongepowered.api.entity.living.player.Player)118 World (org.spongepowered.api.world.World)97 BlockSnapshot (org.spongepowered.api.block.BlockSnapshot)65 Entity (org.spongepowered.api.entity.Entity)58 Region (br.net.fabiozumbi12.RedProtect.Sponge.Region)46 Text (org.spongepowered.api.text.Text)46 ItemStack (org.spongepowered.api.item.inventory.ItemStack)45 Location (org.spongepowered.api.world.Location)45 Sponge (org.spongepowered.api.Sponge)22 BlockType (org.spongepowered.api.block.BlockType)22 Task (org.spongepowered.api.scheduler.Task)17 IOException (java.io.IOException)16 Optional (java.util.Optional)16 Living (org.spongepowered.api.entity.living.Living)15 ItemType (org.spongepowered.api.item.ItemType)15 UUID (java.util.UUID)14 Monster (org.spongepowered.api.entity.living.monster.Monster)14 ItemStackSnapshot (org.spongepowered.api.item.inventory.ItemStackSnapshot)14 Map (java.util.Map)13