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();
});
}
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));
}
}
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));
}
}
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;
}
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;
}
Aggregations