Search in sources :

Example 41 with LanternPlayer

use of org.lanternpowered.server.entity.living.player.LanternPlayer in project LanternServer by LanternPowered.

the class CommandBanIp method completeSpec.

@Override
public void completeSpec(PluginContainer pluginContainer, CommandSpec.Builder specBuilder) {
    specBuilder.arguments(GenericArguments.string(Text.of("address")), GenericArguments.optional(GenericArguments2.remainingString(Text.of("reason")))).executor((src, args) -> {
        final String target = args.<String>getOne("address").get();
        final String reason = args.<String>getOne("reason").orElse(null);
        InetAddress address;
        if (IP_PATTERN.matcher(target).matches()) {
            try {
                address = InetAddress.getByName(target);
            } catch (UnknownHostException e) {
                throw new IllegalStateException("Unable to parse a valid InetAddress: " + target, e);
            }
        } else {
            // Ip address failed, try to find a player
            Optional<Player> player = Sponge.getGame().getServer().getPlayer(target);
            if (!player.isPresent()) {
                throw new CommandException(t("commands.banip.invalid"));
            }
            address = player.get().getConnection().getAddress().getAddress();
        }
        final BanService banService = Sponge.getServiceManager().provideUnchecked(BanService.class);
        final Ban ban = Ban.builder().type(BanTypes.IP).address(address).reason(reason == null ? null : Text.of(reason)).source(src).build();
        banService.addBan(ban);
        final List<LanternPlayer> playersToKick = Lantern.getServer().getRawOnlinePlayers().stream().filter(player -> player.getConnection().getAddress().getAddress().equals(address)).collect(Collectors.toList());
        if (!playersToKick.isEmpty()) {
            final Text kickReason = t("multiplayer.disconnect.ip_banned");
            for (LanternPlayer player : playersToKick) {
                player.kick(kickReason);
            }
        }
        src.sendMessage(t("commands.banip.success", address.toString()));
        return CommandResult.success();
    });
}
Also used : CommandResult(org.spongepowered.api.command.CommandResult) TranslationHelper.t(org.lanternpowered.server.text.translation.TranslationHelper.t) Sponge(org.spongepowered.api.Sponge) BanTypes(org.spongepowered.api.util.ban.BanTypes) GenericArguments(org.spongepowered.api.command.args.GenericArguments) UnknownHostException(java.net.UnknownHostException) Collectors(java.util.stream.Collectors) Ban(org.spongepowered.api.util.ban.Ban) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) CommandException(org.spongepowered.api.command.CommandException) InetAddress(java.net.InetAddress) List(java.util.List) Text(org.spongepowered.api.text.Text) Lantern(org.lanternpowered.server.game.Lantern) GenericArguments2(org.lanternpowered.server.command.element.GenericArguments2) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer) Optional(java.util.Optional) Player(org.spongepowered.api.entity.living.player.Player) Pattern(java.util.regex.Pattern) PluginContainer(org.spongepowered.api.plugin.PluginContainer) BanService(org.spongepowered.api.service.ban.BanService) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer) Player(org.spongepowered.api.entity.living.player.Player) UnknownHostException(java.net.UnknownHostException) Text(org.spongepowered.api.text.Text) CommandException(org.spongepowered.api.command.CommandException) Ban(org.spongepowered.api.util.ban.Ban) BanService(org.spongepowered.api.service.ban.BanService) InetAddress(java.net.InetAddress) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer)

Example 42 with LanternPlayer

use of org.lanternpowered.server.entity.living.player.LanternPlayer in project LanternServer by LanternPowered.

the class LanternChannelRegistrar method sendPayload.

void sendPayload(Player player, String channel, Consumer<ByteBuffer> payload) {
    checkNotNull(player, "player");
    checkNotNull(payload, "payload");
    final NetworkSession session = ((LanternPlayer) player).getConnection();
    if (session.getRegisteredChannels().contains(channel)) {
        final ByteBuffer buf = ByteBufferAllocator.unpooled().buffer();
        payload.accept(buf);
        session.send(new MessagePlayInOutChannelPayload(channel, buf));
    }
}
Also used : NetworkSession(org.lanternpowered.server.network.NetworkSession) MessagePlayInOutChannelPayload(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInOutChannelPayload) ByteBuffer(org.lanternpowered.server.network.buffer.ByteBuffer) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer)

Example 43 with LanternPlayer

use of org.lanternpowered.server.entity.living.player.LanternPlayer in project LanternServer by LanternPowered.

the class LanternChannelRegistrar method sendPayloadToAll.

void sendPayloadToAll(String channel, Consumer<ByteBuffer> payload) {
    checkNotNull(payload, "payload");
    final Iterator<Player> players = this.server.getOnlinePlayers().stream().filter(player -> ((LanternPlayer) player).getConnection().getRegisteredChannels().contains(channel)).iterator();
    if (players.hasNext()) {
        final ByteBuffer buf = ByteBufferAllocator.unpooled().buffer();
        payload.accept(buf);
        final Message msg = new MessagePlayInOutChannelPayload(channel, buf);
        players.forEachRemaining(player -> ((LanternPlayer) player).getConnection().send(msg));
    }
}
Also used : RemoteConnection(org.spongepowered.api.network.RemoteConnection) ByteBufferAllocator(org.lanternpowered.server.network.buffer.ByteBufferAllocator) IndexedMessageChannel(org.spongepowered.api.network.ChannelBinding.IndexedMessageChannel) Inject(com.google.inject.Inject) MessagePlayInOutChannelPayload(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInOutChannelPayload) Platform(org.spongepowered.api.Platform) MessagePlayInOutUnregisterChannels(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInOutUnregisterChannels) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) NetworkSession(org.lanternpowered.server.network.NetworkSession) ChannelRegistrar(org.spongepowered.api.network.ChannelRegistrar) Map(java.util.Map) MessagePlayInOutRegisterChannels(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInOutRegisterChannels) Server(org.spongepowered.api.Server) Conditions.checkNotNullOrEmpty(org.lanternpowered.server.util.Conditions.checkNotNullOrEmpty) ByteBuffer(org.lanternpowered.server.network.buffer.ByteBuffer) PluginContainer(org.spongepowered.api.plugin.PluginContainer) ImmutableSet(com.google.common.collect.ImmutableSet) Iterator(java.util.Iterator) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Sets(com.google.common.collect.Sets) Message(org.lanternpowered.server.network.message.Message) ChannelBinding(org.spongepowered.api.network.ChannelBinding) Consumer(java.util.function.Consumer) ChannelRegistrationException(org.spongepowered.api.network.ChannelRegistrationException) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer) Optional(java.util.Optional) Player(org.spongepowered.api.entity.living.player.Player) Conditions.checkPlugin(org.lanternpowered.server.util.Conditions.checkPlugin) Collections(java.util.Collections) Singleton(com.google.inject.Singleton) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer) Player(org.spongepowered.api.entity.living.player.Player) Message(org.lanternpowered.server.network.message.Message) MessagePlayInOutChannelPayload(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInOutChannelPayload) ByteBuffer(org.lanternpowered.server.network.buffer.ByteBuffer) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer)

Example 44 with LanternPlayer

use of org.lanternpowered.server.entity.living.player.LanternPlayer in project LanternServer by LanternPowered.

the class AbstractEntityProtocol method buildUpdateContextData.

@Nullable
TrackerUpdateContextData buildUpdateContextData(Set<LanternPlayer> players) {
    players = new HashSet<>(players);
    final Set<LanternPlayer> removed = new HashSet<>();
    final Set<LanternPlayer> added = new HashSet<>();
    final Vector3d pos = this.entity.getPosition();
    final Iterator<LanternPlayer> trackerIt = this.trackers.iterator();
    while (trackerIt.hasNext()) {
        final LanternPlayer tracker = trackerIt.next();
        final boolean flag = players.remove(tracker);
        if (tracker != this.entity && (!flag || !isVisible(pos, tracker))) {
            trackerIt.remove();
            removed.add(tracker);
        }
    }
    for (LanternPlayer tracker : players) {
        if (tracker == this.entity || isVisible(pos, tracker)) {
            added.add(tracker);
        }
    }
    boolean flag0 = this.tickCounter++ % this.tickRate == 0 && !this.trackers.isEmpty();
    boolean flag1 = !added.isEmpty();
    boolean flag2 = !removed.isEmpty();
    if (!flag0 && !flag1 && !flag2) {
        return null;
    }
    final TrackerUpdateContextData contextData = new TrackerUpdateContextData(this);
    if (flag0 || flag1) {
        contextData.update = new HashSet<>(this.trackers);
    }
    if (flag1) {
        contextData.added = added;
        this.trackers.addAll(added);
    }
    if (flag2) {
        contextData.removed = removed;
    }
    return contextData;
}
Also used : Vector3d(com.flowpowered.math.vector.Vector3d) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer) HashSet(java.util.HashSet) Nullable(javax.annotation.Nullable)

Example 45 with LanternPlayer

use of org.lanternpowered.server.entity.living.player.LanternPlayer in project LanternServer by LanternPowered.

the class ChestPlacementBehavior method tryPlace.

@Override
public BehaviorResult tryPlace(BehaviorPipeline<Behavior> pipeline, BehaviorContext context) {
    final BlockSnapshot snapshot = context.getContext(ContextKeys.BLOCK_SNAPSHOT).orElseThrow(() -> new IllegalStateException("The BlockSnapshotProviderPlaceBehavior's BlockSnapshot isn't present."));
    final Location<World> location = context.requireContext(ContextKeys.BLOCK_LOCATION);
    final LanternPlayer player = (LanternPlayer) context.getContext(ContextKeys.PLAYER).orElse(null);
    // Get the direction the chest should face
    Direction facing;
    if (player != null) {
        facing = player.getHorizontalDirection(Direction.Division.CARDINAL).getOpposite();
    } else {
        facing = Direction.NORTH;
    }
    Location<World> otherChestLoc;
    // and fail if this is the case
    for (Direction directionToCheck : HORIZONTAL_DIRECTIONS) {
        otherChestLoc = location.getRelative(directionToCheck);
        // We found a chest
        if (otherChestLoc.getBlock().getType() == snapshot.getState().getType()) {
            // Check if it isn't already double
            for (Direction directionToCheck1 : HORIZONTAL_DIRECTIONS) {
                final Location<World> loc1 = otherChestLoc.getRelative(directionToCheck1);
                if (loc1.getBlock().getType() == snapshot.getState().getType()) {
                    return BehaviorResult.FAIL;
                }
            }
            final Direction otherFacing = otherChestLoc.get(Keys.DIRECTION).get();
            // The chests don't have the same facing direction, we need to fix it
            boolean flag = directionToCheck != facing && directionToCheck.getOpposite() != facing;
            if (!(facing == otherFacing && flag)) {
                // The rotation of the chests is completely wrong, fix it
                if (!flag) {
                    if (player != null) {
                        final Vector3d dir = player.getHorizontalDirectionVector();
                        if (directionToCheck == Direction.EAST || directionToCheck == Direction.WEST) {
                            facing = dir.getZ() >= 0 ? Direction.NORTH : Direction.SOUTH;
                        } else {
                            facing = dir.getX() >= 0 ? Direction.WEST : Direction.EAST;
                        }
                    } else {
                        facing = directionToCheck == Direction.EAST || directionToCheck == Direction.WEST ? Direction.SOUTH : Direction.EAST;
                    }
                }
                context.addBlockChange(BlockSnapshot.builder().from(otherChestLoc.createSnapshot()).add(Keys.DIRECTION, facing).build());
            }
            break;
        }
    }
    context.addBlockChange(BlockSnapshotBuilder.create().from(snapshot).location(location).add(Keys.DIRECTION, facing).build());
    return BehaviorResult.SUCCESS;
}
Also used : Vector3d(com.flowpowered.math.vector.Vector3d) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) World(org.spongepowered.api.world.World) Direction(org.spongepowered.api.util.Direction) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer)

Aggregations

LanternPlayer (org.lanternpowered.server.entity.living.player.LanternPlayer)48 ArrayList (java.util.ArrayList)13 CauseStack (org.lanternpowered.server.event.CauseStack)12 AbstractSlot (org.lanternpowered.server.inventory.AbstractSlot)11 ItemStack (org.spongepowered.api.item.inventory.ItemStack)11 World (org.spongepowered.api.world.World)11 Optional (java.util.Optional)10 Vector3d (com.flowpowered.math.vector.Vector3d)9 List (java.util.List)9 Collectors (java.util.stream.Collectors)9 Nullable (javax.annotation.Nullable)9 Sponge (org.spongepowered.api.Sponge)9 SlotTransaction (org.spongepowered.api.item.inventory.transaction.SlotTransaction)9 Lantern (org.lanternpowered.server.game.Lantern)8 LanternItemStack (org.lanternpowered.server.inventory.LanternItemStack)8 Player (org.spongepowered.api.entity.living.player.Player)8 ChangeInventoryEvent (org.spongepowered.api.event.item.inventory.ChangeInventoryEvent)8 AbstractInventory (org.lanternpowered.server.inventory.AbstractInventory)7 PlayerInventoryContainer (org.lanternpowered.server.inventory.PlayerInventoryContainer)7 ClientContainer (org.lanternpowered.server.inventory.client.ClientContainer)7