mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-01 22:15:08 +00:00
Emit party leave events on party disband (#305)
This commit is contained in:
1 parent
4b91b58bd1
commit
28b8866c71
8 files changed
+172
-13
No files matched your search
@@ -31,6 +31,7 @@ import net.draycia.carbon.api.util.KeyedRenderer;
|
||||
import net.draycia.carbon.common.command.commands.WhisperCommand;
|
||||
import net.draycia.carbon.common.event.events.CarbonChatEventImpl;
|
||||
import net.draycia.carbon.common.messaging.packets.ChatMessagePacket;
|
||||
import net.draycia.carbon.common.messaging.packets.DisbandPartyPacket;
|
||||
import net.draycia.carbon.common.messaging.packets.InvalidatePartyInvitePacket;
|
||||
import net.draycia.carbon.common.messaging.packets.LocalPlayerChangePacket;
|
||||
import net.draycia.carbon.common.messaging.packets.LocalPlayersPacket;
|
||||
@@ -92,6 +93,9 @@ public final class CarbonChatPacketHandler extends AbstractMessagingHandler {
|
||||
} else if (packet instanceof InvalidatePartyInvitePacket pkt) {
|
||||
this.partyInvites.handle(pkt);
|
||||
return true;
|
||||
} else if (packet instanceof DisbandPartyPacket pkt) {
|
||||
this.userManager.disbandPartyMessageReceived(pkt);
|
||||
return true;
|
||||
} else if (packet instanceof ChatMessagePacket messagePacket) {
|
||||
return this.handleMessagePacket(messagePacket);
|
||||
} else if (packet instanceof LocalPlayersPacket playersPacket) {
|
||||
|
||||
@@ -40,6 +40,7 @@ import net.draycia.carbon.common.command.commands.WhisperCommand;
|
||||
import net.draycia.carbon.common.config.ConfigManager;
|
||||
import net.draycia.carbon.common.config.MessagingSettings;
|
||||
import net.draycia.carbon.common.messaging.packets.ChatMessagePacket;
|
||||
import net.draycia.carbon.common.messaging.packets.DisbandPartyPacket;
|
||||
import net.draycia.carbon.common.messaging.packets.InvalidatePartyInvitePacket;
|
||||
import net.draycia.carbon.common.messaging.packets.LocalPlayerChangePacket;
|
||||
import net.draycia.carbon.common.messaging.packets.LocalPlayersPacket;
|
||||
@@ -132,6 +133,7 @@ public class MessagingManager {
|
||||
PacketManager.register(PartyChangePacket.class, PartyChangePacket::new);
|
||||
PacketManager.register(PartyInvitePacket.class, PartyInvitePacket::new);
|
||||
PacketManager.register(InvalidatePartyInvitePacket.class, InvalidatePartyInvitePacket::new);
|
||||
PacketManager.register(DisbandPartyPacket.class, DisbandPartyPacket::new);
|
||||
|
||||
this.packetService = new PacketService(4, false, protocolVersion);
|
||||
this.scheduledExecutor = new ExceptionLoggingScheduledThreadPoolExecutor(4,
|
||||
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* CarbonChat
|
||||
*
|
||||
* Copyright (c) 2023 Josua Parks (Vicarious)
|
||||
* Contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package net.draycia.carbon.common.messaging.packets;
|
||||
|
||||
import com.google.inject.assistedinject.Assisted;
|
||||
import com.google.inject.assistedinject.AssistedInject;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import java.util.UUID;
|
||||
import net.draycia.carbon.common.messaging.ServerId;
|
||||
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public final class DisbandPartyPacket extends CarbonPacket {
|
||||
|
||||
private @MonotonicNonNull UUID partyId;
|
||||
|
||||
@AssistedInject
|
||||
public DisbandPartyPacket(
|
||||
final @ServerId UUID serverId,
|
||||
final @Assisted UUID partyId
|
||||
) {
|
||||
super(serverId);
|
||||
this.partyId = partyId;
|
||||
}
|
||||
|
||||
public DisbandPartyPacket(final UUID sender, final ByteBuf data) {
|
||||
super(sender);
|
||||
this.read(data);
|
||||
}
|
||||
|
||||
public UUID partyId() {
|
||||
return this.partyId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void read(final ByteBuf buffer) {
|
||||
this.partyId = this.readUUID(buffer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(final ByteBuf buffer) {
|
||||
this.writeUUID(this.partyId, buffer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "DisbandPartyPacket{" +
|
||||
"partyId=" + this.partyId +
|
||||
", sender=" + this.sender +
|
||||
'}';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -57,4 +57,6 @@ public interface PacketFactory {
|
||||
|
||||
InvalidatePartyInvitePacket invalidatePartyInvite(@Assisted("from") UUID from, @Assisted("to") UUID to);
|
||||
|
||||
DisbandPartyPacket disbandParty(UUID party);
|
||||
|
||||
}
|
||||
@@ -20,6 +20,7 @@
|
||||
package net.draycia.carbon.common.users;
|
||||
|
||||
import com.github.benmanes.caffeine.cache.AsyncCache;
|
||||
import com.github.benmanes.caffeine.cache.Cache;
|
||||
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||
import com.google.inject.Injector;
|
||||
import com.google.inject.Provider;
|
||||
@@ -29,9 +30,12 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -39,6 +43,7 @@ import net.draycia.carbon.api.CarbonServer;
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
import net.draycia.carbon.api.users.Party;
|
||||
import net.draycia.carbon.common.messaging.MessagingManager;
|
||||
import net.draycia.carbon.common.messaging.packets.DisbandPartyPacket;
|
||||
import net.draycia.carbon.common.messaging.packets.PacketFactory;
|
||||
import net.draycia.carbon.common.messaging.packets.PartyChangePacket;
|
||||
import net.draycia.carbon.common.users.db.DatabaseUserManager;
|
||||
@@ -54,6 +59,8 @@ import static net.draycia.carbon.common.users.PlayerUtils.saveExceptionHandler;
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public abstract class CachingUserManager implements UserManagerInternal<CarbonPlayerCommon> {
|
||||
|
||||
private static final int DISBAND_DELAY = 10;
|
||||
|
||||
protected final Logger logger;
|
||||
protected final ProfileResolver profileResolver;
|
||||
private final ExecutorService executor;
|
||||
@@ -64,6 +71,10 @@ public abstract class CachingUserManager implements UserManagerInternal<CarbonPl
|
||||
private final ReentrantLock cacheLock;
|
||||
private final Map<UUID, CompletableFuture<CarbonPlayerCommon>> cache;
|
||||
private final AsyncCache<UUID, Party> partyCache;
|
||||
private final List<Runnable> queuedDisbands = new CopyOnWriteArrayList<>();
|
||||
private final Cache<UUID, Object> recentDisbands = Caffeine.newBuilder()
|
||||
.expireAfterWrite(DISBAND_DELAY + 10, TimeUnit.SECONDS)
|
||||
.build();
|
||||
|
||||
protected CachingUserManager(
|
||||
final Logger logger,
|
||||
@@ -156,6 +167,9 @@ public abstract class CachingUserManager implements UserManagerInternal<CarbonPl
|
||||
@Override
|
||||
public void shutdown() {
|
||||
this.cacheLock.lock();
|
||||
for (final Runnable task : this.queuedDisbands) {
|
||||
task.run();
|
||||
}
|
||||
try {
|
||||
final Map<UUID, CompletableFuture<Void>> collect = List.copyOf(this.cache.keySet()).stream()
|
||||
.collect(Collectors.toMap(Function.identity(), this::loggedOut));
|
||||
@@ -226,6 +240,10 @@ public abstract class CachingUserManager implements UserManagerInternal<CarbonPl
|
||||
|
||||
@Override
|
||||
public CompletableFuture<@Nullable Party> party(final UUID id) {
|
||||
// we delay party deletion for cross-server purposes, so ignore present data when we know it was recently disbanded
|
||||
if (this.recentDisbands.getIfPresent(id) != null) {
|
||||
return CompletableFuture.completedFuture(null);
|
||||
}
|
||||
return this.partyCache.get(id, (uuid, cacheExecutor) -> CompletableFuture.supplyAsync(() -> {
|
||||
final @Nullable PartyImpl party = this.loadParty(uuid);
|
||||
if (party != null) {
|
||||
@@ -253,20 +271,28 @@ public abstract class CachingUserManager implements UserManagerInternal<CarbonPl
|
||||
@Override
|
||||
public final void disbandParty(final UUID id) {
|
||||
this.partyCache.synchronous().invalidate(id);
|
||||
this.executor.execute(() -> this.disbandSync(id));
|
||||
final AtomicBoolean ran = new AtomicBoolean(false);
|
||||
final AtomicReference<Runnable> taskRef = new AtomicReference<>();
|
||||
final Runnable task = () -> {
|
||||
if (ran.compareAndSet(false, true)) {
|
||||
this.disbandSync(id);
|
||||
this.queuedDisbands.remove(taskRef.get());
|
||||
}
|
||||
};
|
||||
taskRef.set(task);
|
||||
this.queuedDisbands.add(task);
|
||||
this.recentDisbands.put(id, new Object());
|
||||
// delay deletion so other servers can post leave events
|
||||
CompletableFuture.delayedExecutor(DISBAND_DELAY, TimeUnit.SECONDS, this.executor).execute(task);
|
||||
this.messagingManager.get().withPacketService(service -> {
|
||||
service.queuePacket(this.packetFactory.disbandParty(id));
|
||||
service.flushQueue();
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void partyChangeMessageReceived(final PartyChangePacket pkt) {
|
||||
@Nullable CompletableFuture<@Nullable Party> future = this.partyCache.getIfPresent(pkt.partyId());
|
||||
if (future == null) {
|
||||
// we want to notify any online members even if the party isn't loaded locally yet
|
||||
for (final CarbonPlayer player : this.server.players()) {
|
||||
if (pkt.partyId().equals(((WrappedCarbonPlayer) player).partyId())) {
|
||||
future = this.party(pkt.partyId());
|
||||
}
|
||||
}
|
||||
}
|
||||
final @Nullable CompletableFuture<@Nullable Party> future = this.partyIfMemberOnline(pkt.partyId());
|
||||
if (future == null) {
|
||||
return;
|
||||
}
|
||||
@@ -287,4 +313,37 @@ public abstract class CachingUserManager implements UserManagerInternal<CarbonPl
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private @Nullable CompletableFuture<@Nullable Party> partyIfMemberOnline(final UUID partyId) {
|
||||
@Nullable CompletableFuture<@Nullable Party> future = this.partyCache.getIfPresent(partyId);
|
||||
if (future == null) {
|
||||
// we want to notify any online members even if the party isn't loaded locally yet
|
||||
for (final CarbonPlayer player : this.server.players()) {
|
||||
if (partyId.equals(((WrappedCarbonPlayer) player).partyId())) {
|
||||
future = this.party(partyId);
|
||||
}
|
||||
}
|
||||
}
|
||||
return future;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disbandPartyMessageReceived(final DisbandPartyPacket pkt) {
|
||||
final @Nullable CompletableFuture<@Nullable Party> future = this.partyIfMemberOnline(pkt.partyId());
|
||||
this.recentDisbands.put(pkt.partyId(), new Object());
|
||||
if (future == null) {
|
||||
return;
|
||||
}
|
||||
future.thenAccept(party -> {
|
||||
if (party == null) {
|
||||
return;
|
||||
}
|
||||
((PartyImpl) party).disbandRaw();
|
||||
this.partyCache.synchronous().invalidate(pkt.partyId());
|
||||
}).whenComplete(($, thr) -> {
|
||||
if (thr != null) {
|
||||
this.logger.warn("Exception handling party change packet {}", pkt, thr);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -154,9 +154,16 @@ public final class PartyImpl implements Party {
|
||||
if (this.disbanded) {
|
||||
throw new IllegalStateException("This party is already disbanded.");
|
||||
}
|
||||
this.server.players().stream().filter(p -> this.members.contains(p.uuid())).forEach(p -> ((WrappedCarbonPlayer) p).party(null));
|
||||
this.disbandRaw();
|
||||
this.userManager.disbandParty(this.id);
|
||||
}
|
||||
|
||||
public void disbandRaw() {
|
||||
this.disbanded = true;
|
||||
this.server.players().stream().filter(p -> this.members.contains(p.uuid())).forEach(p -> ((WrappedCarbonPlayer) p).party(null));
|
||||
for (final UUID member : this.members) {
|
||||
this.emitLeaveEvent(member);
|
||||
}
|
||||
}
|
||||
|
||||
public Set<UUID> rawMembers() {
|
||||
@@ -185,6 +192,12 @@ public final class PartyImpl implements Party {
|
||||
public void removeMemberRaw(final UUID id) {
|
||||
this.members.remove(id);
|
||||
|
||||
this.emitLeaveEvent(id);
|
||||
|
||||
this.notifyLeave(id);
|
||||
}
|
||||
|
||||
private void emitLeaveEvent(final UUID id) {
|
||||
this.events.emit(new PartyLeaveEvent() {
|
||||
|
||||
@Override
|
||||
@@ -197,8 +210,6 @@ public final class PartyImpl implements Party {
|
||||
return PartyImpl.this;
|
||||
}
|
||||
});
|
||||
|
||||
this.notifyLeave(id);
|
||||
}
|
||||
|
||||
public Map<UUID, ChangeType> pollChanges() {
|
||||
|
||||
@@ -27,6 +27,7 @@ import com.google.inject.assistedinject.FactoryModuleBuilder;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import net.draycia.carbon.api.users.Party;
|
||||
import net.draycia.carbon.common.messaging.packets.DisbandPartyPacket;
|
||||
import net.draycia.carbon.common.messaging.packets.PartyChangePacket;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
@@ -113,6 +114,11 @@ public final class PlatformUserManager implements UserManagerInternal<WrappedCar
|
||||
this.backingManager.partyChangeMessageReceived(pkt);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disbandPartyMessageReceived(final DisbandPartyPacket pkt) {
|
||||
this.backingManager.disbandPartyMessageReceived(pkt);
|
||||
}
|
||||
|
||||
public interface PlayerFactory {
|
||||
|
||||
WrappedCarbonPlayer wrap(CarbonPlayerCommon common);
|
||||
|
||||
@@ -23,6 +23,7 @@ import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
import net.draycia.carbon.api.users.UserManager;
|
||||
import net.draycia.carbon.common.messaging.packets.DisbandPartyPacket;
|
||||
import net.draycia.carbon.common.messaging.packets.PartyChangePacket;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
@@ -45,4 +46,6 @@ public interface UserManagerInternal<C extends CarbonPlayer> extends UserManager
|
||||
void disbandParty(UUID id);
|
||||
|
||||
void partyChangeMessageReceived(PartyChangePacket pkt);
|
||||
|
||||
void disbandPartyMessageReceived(DisbandPartyPacket pkt);
|
||||
}
|
||||
Reference in new issue
Block a user