Skip to content

Commit

Permalink
Merge branch 'Dev-branch' into Dependabot
Browse files Browse the repository at this point in the history
  • Loading branch information
CaptainRexPL authored Jan 15, 2024
2 parents adbb9fe + badcd28 commit c144b6f
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 42 deletions.
2 changes: 1 addition & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ updates:
directory: "/" # Location of package manifests
schedule:
interval: "daily"
target-branch: "Dependabot-updates"
target-branch: "Dependabot"
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ public boolean handleResponse(ProtocolResponse resp) {
// Update the location
playerLocations.put(response.sender, location.get());
} else {
// Remove if stooped sharing
// Remove if stopped sharing
playerLocations.remove(response.sender);
}
collar.configuration.eventBus.dispatch(new LocationUpdatedEvent(collar, response.sender, location.orElse(Location.UNKNOWN)));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.collarmc.client.api.messaging;

import com.collarmc.api.groups.Group;
import com.collarmc.api.identity.ClientIdentity;
import com.collarmc.api.messaging.Message;
import com.collarmc.api.session.Player;
import com.collarmc.client.Collar;
Expand Down Expand Up @@ -95,14 +96,14 @@ public boolean handleResponse(ProtocolResponse resp) {
try {
byte[] contents = groupSession.decrypt(response.message, response.sender);
message = Utils.messagePackMapper().readValue(contents, Message.class);
} catch (IOException | CipherException e) {
// We don't throw an exception here in case someone is doing something naughty to disrupt the group and cause the client to exit
LOGGER.error(collar.identity() + "could not read group message from group " + group.id, e);
message = null;
}
if (message != null) {
collar.configuration.eventBus.dispatch(new GroupMessageReceivedEvent(collar, group, response.player, message));
}});
} catch (IOException | CipherException e) {
// We don't throw an exception here in case someone is doing something naughty to disrupt the group and cause the client to exit
LOGGER.error(collar.identity() + "could not read group message from group " + group.id, e);
message = null;
}
if (message != null) {
collar.configuration.eventBus.dispatch(new GroupMessageReceivedEvent(collar, group, new Player((ClientIdentity)response.sender, null), message));
}});
});
} else if (response.sender != null) {
Message message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.collarmc.api.minecraft.MinecraftPlayer;

/**
* Fired when a private message was attempted with another player but there was not sufficent trust to deliver
* Fired when a private message was attempted with another player but there was not sufficient trust to deliver
*/
public final class UntrustedPrivateMessageReceivedEvent extends AbstractCollarEvent {
public final MinecraftPlayer player;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,11 @@ public Optional<ProtocolResponse> playerIsOffline(Player player) {
BatchProtocolResponse response = new BatchProtocolResponse();
store.findGroupsContaining(player.identity.id()).forEach(group -> {
PublicProfile profile = profiles.getById(player.identity.id()).orElseThrow(() -> new IllegalStateException("could not load profile " + player.identity.id())).toPublic();
group = group.updatePlayer(new MemberSource(player, profile));
if (group.type == GroupType.NEARBY) {
group = group.removeMember(player);
} else {
group = group.updatePlayer(new MemberSource(player, profile));
}
// Let everyone else in the group know that this identity has gone offline
Group finalGroup = group;
BatchProtocolResponse updates = createMemberMessages(
Expand Down Expand Up @@ -341,6 +345,7 @@ public Optional<BatchProtocolResponse> updateNearbyGroups(NearbyGroups.Result re
group = group.removeMember(source.player);
}
store.delete(group.id);
LOGGER.info("[GroupService] deleted " + group.type + " group: " + group.id);
}));
return response.optional();
}
Expand Down Expand Up @@ -414,7 +419,7 @@ public Optional<ProtocolResponse> transferOwnership(ClientIdentity identity, Tra
}

private void updateState(Group group) {
if (group != null && group.members.isEmpty()) {
if (group != null && (group.members.isEmpty() || (group.type == GroupType.NEARBY && group.members.size() == 1))) {
LOGGER.info("Removed group " + group.id + " as it has no members.");
store.delete(group.id);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
* State machine for managing dynamically created {@link Group}'s based on hashing
* every players player entity list and comparing them.
*/
public final class NearbyGroups {

private static final Logger LOGGER = LogManager.getLogger(NearbyGroups.class.getName());

private final ConcurrentMap<MemberSource, Set<String>> playerHashes = new ConcurrentHashMap<>();
private final ConcurrentMap<NearbyGroup, UUID> nearbyGroups = new ConcurrentHashMap<>();
private final ConcurrentMap<MemberSource, Set<NearbyGroup>> playerToGroups = new ConcurrentHashMap<>();
Expand All @@ -33,37 +38,39 @@ public Result updateNearbyGroups(MemberSource source, Set<String> hashes) {
playerHashes.keySet().stream()
.filter(anotherSource ->
anotherSource.player.minecraftPlayer != null &&
source.player.minecraftPlayer != null &&
anotherSource.player.minecraftPlayer.inServerWith(source.player.minecraftPlayer)
&& !anotherSource.equals(source)
source.player.minecraftPlayer != null &&
anotherSource.player.minecraftPlayer.inServerWith(source.player.minecraftPlayer)
&& !anotherSource.equals(source)
).forEach(anotherPlayer -> {
Set<String> otherPlayersHashes = playerHashes.get(anotherPlayer);
NearbyGroup group = new NearbyGroup(Set.of(source, anotherPlayer));
if (Sets.difference(hashes, otherPlayersHashes).isEmpty()) {
nearbyGroups.compute(group, (nearbyGroup, uuid) -> {
if (uuid == null) {
uuid = UUID.randomUUID();
add.put(uuid, group);
Set<String> otherPlayersHashes = playerHashes.get(anotherPlayer);
NearbyGroup group = new NearbyGroup(Set.of(source, anotherPlayer));
if (Sets.difference(hashes, otherPlayersHashes).isEmpty()) {
nearbyGroups.compute(group, (nearbyGroup, uuid) -> {
if (uuid == null) {
uuid = UUID.randomUUID();
add.put(uuid, group);
}
return uuid;
});
playerToGroups.compute(source, (thePlayer, playersGroups) -> {
playersGroups = playersGroups == null ? new HashSet<>() : playersGroups;
playersGroups.add(group);
return playersGroups;
});
} else {
UUID groupId = nearbyGroups.get(group);
nearbyGroups.remove(group);
if (groupId != null) {
remove.put(groupId, group);
playerToGroups.compute(source, (thePlayer, playersGroups) -> {
playersGroups = playersGroups == null ? new HashSet<>() : playersGroups;
playersGroups.remove(group);
return nearbyGroups.isEmpty() ? null : playersGroups;
});
}
}
return uuid;
});
playerToGroups.compute(source, (thePlayer, playersGroups) -> {
playersGroups = playersGroups == null ? new HashSet<>() : playersGroups;
playersGroups.add(group);
return playersGroups;
});
} else {
UUID groupId = nearbyGroups.get(group);
if (groupId != null) {
remove.put(groupId, group);
playerToGroups.compute(source, (thePlayer, playersGroups) -> {
playersGroups = playersGroups == null ? new HashSet<>() : playersGroups;
playersGroups.remove(group);
return nearbyGroups.isEmpty() ? null : playersGroups;
});
}
}
});
nearbyGroups.entrySet().removeIf(entry -> entry.getKey().players.size() < 2);
return new Result(add, remove);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public Optional<BatchProtocolResponse> updateLocation(ClientIdentity identity, U
}

private Optional<BatchProtocolResponse> stopSharing(UUID groupId, Player player) {
LOGGER.info("Player " + player + " started sharing location with group " + groupId);
LOGGER.info("Player " + player + " stopped sharing location with group " + groupId);
LocationUpdatedResponse locationUpdatedResponse = new LocationUpdatedResponse(groupId, player, null);
Optional<BatchProtocolResponse> responses = createLocationResponses(player, locationUpdatedResponse);
playersSharing.compute(groupId, (uuid, players) -> {
Expand Down

0 comments on commit c144b6f

Please sign in to comment.