Search in sources :

Example 1 with InteractBlockEvent

use of org.spongepowered.api.event.block.InteractBlockEvent in project Skree by Skelril.

the class SkyWarsListener method onInteract.

@Listener
public void onInteract(InteractBlockEvent event, @First Player player) {
    Optional<SkyWarsInstance> optInst = manager.getApplicableZone(player);
    if (!optInst.isPresent()) {
        return;
    }
    SkyWarsInstance inst = optInst.get();
    if (inst.getState() != SkyWarsState.IN_PROGRESS) {
        return;
    }
    Optional<SkyWarsPlayerData> optPlayerData = inst.getPlayerData(player);
    if (!optPlayerData.isPresent()) {
        return;
    }
    SkyWarsPlayerData playerData = optPlayerData.get();
    Optional<ItemStack> optStack = player.getItemInHand(HandTypes.MAIN_HAND);
    if (!optStack.isPresent()) {
        return;
    }
    ItemStack stack = optStack.get();
    if (stack.getItem() == CustomItemTypes.SKY_FEATHER) {
        Vector3d vel = EntityDirectionUtil.getFacingVector(player);
        Optional<SkyFeather.Data> optData = SkyFeather.getDataFor(stack);
        if (!optData.isPresent()) {
            return;
        }
        SkyFeather.Data data = optData.get();
        double radius = data.radius;
        double flight = data.flight;
        double pushBack = data.pushBack;
        if (event instanceof InteractBlockEvent.Primary.MainHand) {
            if (!playerData.canFly()) {
                return;
            }
            vel = vel.mul(flight);
            player.setVelocity(vel);
            playerData.stopFlight(250);
        } else if (event instanceof InteractBlockEvent.Secondary.MainHand) {
            if (!playerData.canPushBack()) {
                return;
            }
            vel = vel.mul(pushBack * 2);
            SpongePlayer spongePlayer = SpongeWorldEdit.inst().wrapPlayer(player);
            Collection<Entity> possibleTargets = inst.getContained(Player.class, Chicken.class);
            possibleTargets.remove(player);
            ParticleEffect radiationEffect = ParticleEffect.builder().type(ParticleTypes.FLAME).quantity(1).build();
            TargetBlock targetBlock = new TargetBlock(spongePlayer, 50, .2);
            while (targetBlock.getNextBlock() != null) {
                BlockWorldVector weBlock = targetBlock.getCurrentBlock();
                Location<World> loc = new Location<>(inst.getRegion().getExtent(), weBlock.getX(), weBlock.getY(), weBlock.getZ());
                for (int i = 0; i < 10; ++i) {
                    inst.getRegion().getExtent().spawnParticles(radiationEffect, loc.getPosition().add(Probability.getRangedRandom(0, 1.0), Probability.getRangedRandom(0, 1.0), Probability.getRangedRandom(0, 1.0)));
                }
                for (Entity aEntity : possibleTargets) {
                    if (aEntity.getLocation().getPosition().distanceSquared(loc.getPosition()) <= Math.pow(radius, 2)) {
                        if (aEntity instanceof Player) {
                            Player aPlayer = (Player) aEntity;
                            if (inst.isFriendlyFire(player, aPlayer)) {
                                continue;
                            }
                            // Handle Sender
                            playerData.stopPushBack(250);
                            player.sendMessage(Text.of(TextColors.YELLOW, "You push back: ", aPlayer.getName(), "!"));
                            // Handle Target
                            aPlayer.setVelocity(vel);
                            Optional<SkyWarsPlayerData> optAPlayerData = inst.getPlayerData(aPlayer);
                            if (optAPlayerData.isPresent()) {
                                SkyWarsPlayerData aPlayerData = optAPlayerData.get();
                                if (aPlayerData.canDefrost()) {
                                    aPlayerData.stopFlight();
                                }
                            }
                        } else {
                            inst.awardPowerup(player, stack);
                            aEntity.remove();
                        }
                    }
                }
            }
        }
        tf(stack).attemptDamageItem(1, new Random());
    }
}
Also used : Entity(org.spongepowered.api.entity.Entity) SpongePlayer(com.sk89q.worldedit.sponge.SpongePlayer) SkyFeather(com.skelril.skree.content.registry.item.minigame.SkyFeather) TargetBlock(com.sk89q.worldedit.util.TargetBlock) InteractBlockEvent(org.spongepowered.api.event.block.InteractBlockEvent) Random(java.util.Random) SpongePlayer(com.sk89q.worldedit.sponge.SpongePlayer) Player(org.spongepowered.api.entity.living.player.Player) Optional(java.util.Optional) ParticleEffect(org.spongepowered.api.effect.particle.ParticleEffect) BlockWorldVector(com.sk89q.worldedit.BlockWorldVector) Vector3d(com.flowpowered.math.vector.Vector3d) Collection(java.util.Collection) Chicken(org.spongepowered.api.entity.living.animal.Chicken) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Location(org.spongepowered.api.world.Location) Listener(org.spongepowered.api.event.Listener)

Example 2 with InteractBlockEvent

use of org.spongepowered.api.event.block.InteractBlockEvent in project SpongeForge by SpongePowered.

the class SpongeForgeEventFactory method createPlayerInteractEvent.

// Bulk Event Handling
private static InteractBlockEvent createPlayerInteractEvent(Event event) {
    InteractBlockEvent spongeEvent = (InteractBlockEvent) event;
    Player player = spongeEvent.getCause().first(Player.class).orElse(null);
    // Forge doesn't support left-click AIR
    if (player == null || (spongeEvent instanceof InteractBlockEvent.Primary && spongeEvent.getTargetBlock() == BlockSnapshot.NONE)) {
        return spongeEvent;
    }
    BlockPos pos = VecHelper.toBlockPos(spongeEvent.getTargetBlock().getPosition());
    EnumFacing face = DirectionFacingProvider.getInstance().get(spongeEvent.getTargetSide()).orElse(null);
    Vec3d hitVec = null;
    final EntityPlayerMP entityPlayerMP = EntityUtil.toNative(player);
    if (spongeEvent.getInteractionPoint().isPresent()) {
        hitVec = VecHelper.toVec3d(spongeEvent.getInteractionPoint().get());
    }
    if (spongeEvent instanceof InteractBlockEvent.Primary) {
        PlayerInteractEvent.LeftClickBlock forgeEvent = new PlayerInteractEvent.LeftClickBlock(entityPlayerMP, pos, face, hitVec);
        ((IMixinEventBus) MinecraftForge.EVENT_BUS).post(forgeEvent, true);
        if (forgeEvent.isCanceled()) {
            spongeEvent.setCancelled(true);
        }
    } else if (face != null && spongeEvent instanceof InteractBlockEvent.Secondary) {
        EnumHand hand = spongeEvent instanceof InteractBlockEvent.Secondary.MainHand ? EnumHand.MAIN_HAND : EnumHand.OFF_HAND;
        PlayerInteractEvent.RightClickBlock forgeEvent = new PlayerInteractEvent.RightClickBlock(entityPlayerMP, hand, pos, face, hitVec);
        ((IMixinEventBus) MinecraftForge.EVENT_BUS).post(forgeEvent, true);
        if (forgeEvent.isCanceled()) {
            spongeEvent.setCancelled(true);
        }
        // Mods have higher priority
        if (forgeEvent.getUseItem() != Result.DEFAULT) {
            ((InteractBlockEvent.Secondary) spongeEvent).setUseItemResult(getTristateFromResult(forgeEvent.getUseItem()));
        }
        if (forgeEvent.getUseBlock() != Result.DEFAULT) {
            ((InteractBlockEvent.Secondary) spongeEvent).setUseBlockResult(getTristateFromResult(forgeEvent.getUseBlock()));
        }
    }
    return spongeEvent;
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) EntityPlayer(net.minecraft.entity.player.EntityPlayer) EnumFacing(net.minecraft.util.EnumFacing) PlayerInteractEvent(net.minecraftforge.event.entity.player.PlayerInteractEvent) Vec3d(net.minecraft.util.math.Vec3d) IMixinEventBus(org.spongepowered.mod.interfaces.IMixinEventBus) InteractBlockEvent(org.spongepowered.api.event.block.InteractBlockEvent) EnumHand(net.minecraft.util.EnumHand) BlockPos(net.minecraft.util.math.BlockPos) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP)

Example 3 with InteractBlockEvent

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

the class MarketSignListener method onClick.

@Listener
public void onClick(InteractBlockEvent event, @First Player player) {
    if (!event.getTargetBlock().getLocation().isPresent()) {
        return;
    }
    Location<World> loc = event.getTargetBlock().getLocation().get();
    BlockType blockType = loc.getBlockType();
    if (!Arrays.asList(STANDING_SIGN, WALL_SIGN).contains(blockType)) {
        // Not even a sign -> ignore
        return;
    }
    boolean editMode = module.getEditModeCommand().hasUser(player);
    Boolean sneaking = player.get(Keys.IS_SNEAKING).get();
    boolean punch = event instanceof Primary;
    Optional<MarketSignData> mSignData = event.getTargetBlock().get(ImmutableMarketSignData.class).map(ImmutableMarketSignData::asMutable);
    if (mSignData.isPresent()) {
        manager.updateSignText(mSignData.get(), loc);
    }
    if (editMode) {
        if (!player.hasPermission(module.perms().EDIT_USE.getId())) {
            i18n.send(player, NEGATIVE, "You are not allowed to edit MarketSigns here");
            manager.exitEditMode(player);
            module.getEditModeCommand().removeUser(player);
            return;
        }
        if (// Its the active MarketSign -> Break or Modify Item
        manager.isActive(loc, player)) {
            if (// Do nothing if sneaking
            sneaking) {
                return;
            }
            if (punch) {
                if (manager.tryBreakActive(player)) {
                    return;
                } else {
                    // TODO resend signtext?
                    event.setCancelled(true);
                }
            }
            if (!punch && player.getItemInHand(HandTypes.MAIN_HAND).isPresent()) {
                manager.modifyItemActive(player, player.getItemInHand(HandTypes.MAIN_HAND).get());
            }
            event.setCancelled(true);
            return;
        }
        if (// Its another MarketSign
        mSignData.isPresent()) {
            // Set Current Sign
            manager.setSign(loc, player);
            event.setCancelled(true);
            return;
        }
        // Its a sign ; but no data yet
        if (// Sneak Interact -> Allow Interaction
        sneaking && !punch) {
            return;
        }
        if (// Sneak AND Punch to convert sign
        sneaking && punch) {
            // sneak + punch -> convert it!
            MarketSignData data = new MarketSignData();
            data.setID(UUID.randomUUID());
            data.setOwner(player.getUniqueId());
            // Sign converted! Now set active
            loc.offer(data);
            manager.setSign(loc, player);
            event.setCancelled(true);
            return;
        }
        i18n.send(player, NEGATIVE, "That is not a market sign!");
        i18n.send(player, NEUTRAL, "Sneak and punch the sign convert it.");
        event.setCancelled(true);
        return;
    }
    if (!mSignData.isPresent()) {
        return;
    }
    MarketSignData data = mSignData.get();
    manager.executeSignAction(data, loc, player, event instanceof InteractBlockEvent.Secondary);
    if (blockType != AIR) {
        // TODO sign somehow is retained /w some invalid data
        loc.offer(data);
        manager.updateSignText(data, loc);
    // event.setCancelled(true);
    }
}
Also used : ImmutableMarketSignData(org.cubeengine.module.signmarket.data.ImmutableMarketSignData) InteractBlockEvent(org.spongepowered.api.event.block.InteractBlockEvent) BlockType(org.spongepowered.api.block.BlockType) Primary(org.spongepowered.api.event.block.InteractBlockEvent.Primary) MarketSignData(org.cubeengine.module.signmarket.data.MarketSignData) ImmutableMarketSignData(org.cubeengine.module.signmarket.data.ImmutableMarketSignData) World(org.spongepowered.api.world.World) Listener(org.spongepowered.api.event.Listener)

Example 4 with InteractBlockEvent

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

the class CuboidSelector method onInteract.

@Listener
public void onInteract(InteractBlockEvent event, @First Player player) {
    if (!(event instanceof InteractBlockEvent.Primary.MainHand) && !(event instanceof InteractBlockEvent.Secondary.MainHand)) {
        return;
    }
    if (event.getTargetBlock() == BlockSnapshot.NONE) {
        return;
    }
    Location block = event.getTargetBlock().getLocation().get();
    if ((int) block.getPosition().length() == 0) {
        return;
    }
    Optional<ItemStack> itemInHand = player.getItemInHand(HandTypes.MAIN_HAND);
    if (!itemInHand.isPresent() || !"Selector-Tool".equals(itemInHand.get().get(Keys.DISPLAY_NAME).map(Text::toPlain).orElse(""))) {
        return;
    }
    if (block.getBlockType() == AIR || !player.hasPermission(module.getSelectPerm().getId())) {
        return;
    }
    SelectorData data = selectorData.computeIfAbsent(player.getUniqueId(), k -> new SelectorData());
    if (event instanceof InteractBlockEvent.Primary) {
        data.setPoint(0, block);
        Text selected = getText(player, data);
        i18n.send(player, POSITIVE, "Position 1 ({integer}, {integer}, {integer}). {txt}", block.getBlockX(), block.getBlockY(), block.getBlockZ(), selected);
    } else if (event instanceof InteractBlockEvent.Secondary) {
        data.setPoint(1, block);
        Text selected = getText(player, data);
        i18n.send(player, POSITIVE, "Position 2 ({integer}, {integer}, {integer}). {txt}", block.getBlockX(), block.getBlockY(), block.getBlockZ(), selected);
    }
    event.setCancelled(true);
}
Also used : InteractBlockEvent(org.spongepowered.api.event.block.InteractBlockEvent) Text(org.spongepowered.api.text.Text) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Location(org.spongepowered.api.world.Location) Listener(org.spongepowered.api.event.Listener)

Example 5 with InteractBlockEvent

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

the class ElevatorListener method onInteractBlock.

@Listener
public void onInteractBlock(InteractBlockEvent event, @Root Player player) {
    if (!(event instanceof InteractBlockEvent.Primary.MainHand) && !(event instanceof InteractBlockEvent.Secondary.MainHand)) {
        return;
    }
    BlockType type = event.getTargetBlock().getState().getType();
    if (type != BlockTypes.STANDING_SIGN && type != BlockTypes.WALL_SIGN) {
        return;
    }
    Location<World> loc = event.getTargetBlock().getLocation().get();
    ElevatorData data = loc.get(ElevatorData.class).orElse(null);
    Boolean sneak = player.get(Keys.IS_SNEAKING).orElse(false);
    if (sneak) {
        Optional<ItemStack> itemInHand = player.getItemInHand(HandTypes.MAIN_HAND);
        if (data == null) {
            if (!(event instanceof InteractBlockEvent.Primary)) {
                // Only Punch to activate
                return;
            }
            if (itemInHand.isPresent()) {
                if (player.hasPermission(module.getPerm().CREATE.getId()) && itemInHand.get().getType().equals(module.getConfig().creationItem)) {
                    data = new ElevatorData();
                    data.setOwner(player.getUniqueId());
                    loc.offer(data);
                    ItemStack item = itemInHand.get();
                    item.setQuantity(item.getQuantity() - 1);
                    player.setItemInHand(HandTypes.MAIN_HAND, item);
                    List<Text> list = loc.get(Keys.SIGN_LINES).get();
                    // Set First Line with name of renamed Item
                    list.set(0, itemInHand.get().get(Keys.DISPLAY_NAME).orElse(list.get(0)));
                    loc.offer(Keys.SIGN_LINES, list);
                    i18n.send(ACTION_BAR, player, POSITIVE, "Elevator created!");
                    updateSign(loc, data);
                    event.setCancelled(true);
                }
            }
        } else if (// Sign has Elevator Data and hand is empty
        !itemInHand.isPresent()) {
            if (player.hasPermission(module.getPerm().ADJUST.getId())) {
                // Search order dependent on click
                Vector3i target = data.getTarget();
                target = findNextSign(loc, target, loc.getBlockPosition(), event instanceof InteractBlockEvent.Primary);
                data.setTarget(target);
                updateSign(loc, data);
                event.setCancelled(true);
            }
        } else if (itemInHand.get().getType() == ItemTypes.PAPER && event instanceof InteractBlockEvent.Primary) {
            if (player.hasPermission(module.getPerm().RENAME.getId())) {
                List<Text> list = loc.get(Keys.SIGN_LINES).get();
                // Set First Line with name of renamed Item
                list.set(0, itemInHand.get().get(Keys.DISPLAY_NAME).orElse(list.get(0)));
                loc.offer(Keys.SIGN_LINES, list);
                i18n.send(ACTION_BAR, player, POSITIVE, "Elevator name changed!");
                event.setCancelled(true);
            }
        }
        return;
    }
    if (event instanceof InteractBlockEvent.Secondary && player.hasPermission(module.getPerm().USE.getId())) {
        Optional<Vector3i> target = event.getTargetBlock().get(IElevatorData.TARGET);
        if (target.isPresent()) {
            if (loc.getExtent().get(target.get(), ElevatorData.class).isPresent()) {
                Vector3i sign = target.get();
                Vector3d pPos = player.getLocation().getPosition();
                Location<World> targetLoc = new Location<>(player.getWorld(), pPos.getX(), sign.getY() - 1, pPos.getZ());
                if (!player.setLocationSafely(targetLoc)) {
                    i18n.send(ACTION_BAR, player, NEGATIVE, "Target obstructed");
                }
                event.setCancelled(true);
            } else {
                i18n.send(ACTION_BAR, player, NEGATIVE, "Target sign was destroyed!");
                event.setCancelled(true);
            }
        }
    }
    if (event instanceof InteractBlockEvent.Secondary) {
        Optional<ItemStack> itemInHand = player.getItemInHand(HandTypes.MAIN_HAND);
        if (itemInHand.isPresent()) {
            if (player.hasPermission(module.getPerm().CREATE.getId()) && itemInHand.get().getType().equals(module.getConfig().creationItem)) {
                event.setCancelled(true);
            }
        }
    }
}
Also used : Text(org.spongepowered.api.text.Text) World(org.spongepowered.api.world.World) IElevatorData(org.cubeengine.module.elevator.data.IElevatorData) ElevatorData(org.cubeengine.module.elevator.data.ElevatorData) InteractBlockEvent(org.spongepowered.api.event.block.InteractBlockEvent) BlockType(org.spongepowered.api.block.BlockType) Vector3d(com.flowpowered.math.vector.Vector3d) Vector3i(com.flowpowered.math.vector.Vector3i) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Location(org.spongepowered.api.world.Location) Listener(org.spongepowered.api.event.Listener)

Aggregations

InteractBlockEvent (org.spongepowered.api.event.block.InteractBlockEvent)5 Listener (org.spongepowered.api.event.Listener)4 ItemStack (org.spongepowered.api.item.inventory.ItemStack)3 Location (org.spongepowered.api.world.Location)3 Vector3d (com.flowpowered.math.vector.Vector3d)2 BlockType (org.spongepowered.api.block.BlockType)2 Player (org.spongepowered.api.entity.living.player.Player)2 Text (org.spongepowered.api.text.Text)2 World (org.spongepowered.api.world.World)2 Vector3i (com.flowpowered.math.vector.Vector3i)1 BlockWorldVector (com.sk89q.worldedit.BlockWorldVector)1 SpongePlayer (com.sk89q.worldedit.sponge.SpongePlayer)1 TargetBlock (com.sk89q.worldedit.util.TargetBlock)1 SkyFeather (com.skelril.skree.content.registry.item.minigame.SkyFeather)1 Collection (java.util.Collection)1 Optional (java.util.Optional)1 Random (java.util.Random)1 EntityPlayer (net.minecraft.entity.player.EntityPlayer)1 EntityPlayerMP (net.minecraft.entity.player.EntityPlayerMP)1 EnumFacing (net.minecraft.util.EnumFacing)1