Search in sources :

Example 1 with ElevatorData

use of org.cubeengine.module.elevator.data.ElevatorData 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)

Example 2 with ElevatorData

use of org.cubeengine.module.elevator.data.ElevatorData in project modules-extra by CubeEngine.

the class ElevatorListener method findNextSign.

private Vector3i findNextSign(Location<World> loc, Vector3i previous, Vector3i startPos, boolean up) {
    startPos = previous == null ? startPos : previous;
    // Search for next Elevator sign
    BlockRay<World> ray = BlockRay.from(loc.getExtent(), startPos.toDouble()).direction(new Vector3d(0, up ? 1 : -1, 0)).narrowPhase(false).stopFilter(b -> b.getBlockY() <= loc.getExtent().getBlockMax().getY()).stopFilter(b -> b.getBlockY() >= loc.getExtent().getBlockMin().getY()).build();
    while (ray.hasNext()) {
        BlockRayHit<World> next = ray.next();
        if (next.getBlockPosition().equals(startPos)) {
            continue;
        }
        Optional<ElevatorData> targetData = next.getLocation().get(ElevatorData.class);
        if (targetData.isPresent() && !next.getBlockPosition().equals(loc.getBlockPosition())) {
            return next.getBlockPosition();
        }
    }
    // nothing found? Return same location as before when it is valid
    Optional<ElevatorData> targetData = loc.getExtent().get(startPos, ElevatorData.class);
    return targetData.isPresent() ? previous : null;
}
Also used : Keys(org.spongepowered.api.data.key.Keys) ItemTypes(org.spongepowered.api.item.ItemTypes) Vector3d(com.flowpowered.math.vector.Vector3d) BlockRay(org.spongepowered.api.util.blockray.BlockRay) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Text(org.spongepowered.api.text.Text) ACTION_BAR(org.spongepowered.api.text.chat.ChatTypes.ACTION_BAR) InteractBlockEvent(org.spongepowered.api.event.block.InteractBlockEvent) BlockRayHit(org.spongepowered.api.util.blockray.BlockRayHit) Location(org.spongepowered.api.world.Location) I18n(org.cubeengine.libcube.service.i18n.I18n) BlockTypes(org.spongepowered.api.block.BlockTypes) IElevatorData(org.cubeengine.module.elevator.data.IElevatorData) ElevatorData(org.cubeengine.module.elevator.data.ElevatorData) POSITIVE(org.cubeengine.libcube.service.i18n.formatter.MessageType.POSITIVE) Root(org.spongepowered.api.event.filter.cause.Root) List(java.util.List) Vector3i(com.flowpowered.math.vector.Vector3i) World(org.spongepowered.api.world.World) BlockType(org.spongepowered.api.block.BlockType) HandTypes(org.spongepowered.api.data.type.HandTypes) Optional(java.util.Optional) Player(org.spongepowered.api.entity.living.player.Player) NEGATIVE(org.cubeengine.libcube.service.i18n.formatter.MessageType.NEGATIVE) Listener(org.spongepowered.api.event.Listener) Vector3d(com.flowpowered.math.vector.Vector3d) World(org.spongepowered.api.world.World) IElevatorData(org.cubeengine.module.elevator.data.IElevatorData) ElevatorData(org.cubeengine.module.elevator.data.ElevatorData)

Aggregations

Vector3d (com.flowpowered.math.vector.Vector3d)2 Vector3i (com.flowpowered.math.vector.Vector3i)2 ElevatorData (org.cubeengine.module.elevator.data.ElevatorData)2 IElevatorData (org.cubeengine.module.elevator.data.IElevatorData)2 BlockType (org.spongepowered.api.block.BlockType)2 Listener (org.spongepowered.api.event.Listener)2 InteractBlockEvent (org.spongepowered.api.event.block.InteractBlockEvent)2 ItemStack (org.spongepowered.api.item.inventory.ItemStack)2 Text (org.spongepowered.api.text.Text)2 Location (org.spongepowered.api.world.Location)2 World (org.spongepowered.api.world.World)2 List (java.util.List)1 Optional (java.util.Optional)1 I18n (org.cubeengine.libcube.service.i18n.I18n)1 NEGATIVE (org.cubeengine.libcube.service.i18n.formatter.MessageType.NEGATIVE)1 POSITIVE (org.cubeengine.libcube.service.i18n.formatter.MessageType.POSITIVE)1 BlockTypes (org.spongepowered.api.block.BlockTypes)1 Keys (org.spongepowered.api.data.key.Keys)1 HandTypes (org.spongepowered.api.data.type.HandTypes)1 Player (org.spongepowered.api.entity.living.player.Player)1