Prevent api driven rooms from automatic ownership change

This commit is contained in:
Elias Müller 2023-11-28 10:28:49 +01:00
parent 8b47eb1139
commit 894b5464dd

@ -81,15 +81,18 @@ public class Room extends MineNetInstance implements Spawnable {
private Player owner;
public final UUID uuid = UUID.randomUUID();
public final boolean apiDriven;
private GameSelector gameSelector;
private Room(Player owner) {
super(Dimension.THE_END.DIMENSION);
this.apiDriven = false;
construct();
setOwner(owner);
}
protected Room() {
super(Dimension.THE_END.DIMENSION);
this.apiDriven = true;
construct();
}
@ -112,19 +115,21 @@ public class Room extends MineNetInstance implements Spawnable {
this.owner = newOwner;
this.owner.eventNode().addListener(PlayerDisconnectEvent.class, playerDisconnectEvent -> {
if(this.apiDriven) return; // prevent giving ownership on apiDriven rooms
Player p = playerDisconnectEvent.getPlayer();
if(p != this.owner) return; // return if the current handling player is really the current owner
if(p != this.owner) return;
getAllMembers().stream()
.filter(player -> player != p) // exclude the current leaving owner
.findFirst() // find the first user meeting the requirement
.findFirst()
.ifPresentOrElse(
this::setOwner, // set the new owner
() -> Room.getRoom(p).ifPresent(Room::deleteRoom) // or else delete the room (no players in the room)
this::setOwner,
() -> Room.getRoom(p).ifPresent(Room::deleteRoom) // delete room if no players are left
);
Room.unsetRoom(p); // remove the player itself from the room
Room.unsetRoom(p);
new ChatMessage(Icon.ERROR).appendStatic("The room leader left!").send(getAllMembers());
new ChatMessage(Icon.SCIENCE).appendStatic(this.owner.getUsername()).appendStatic(" is the new Leader!").send(getAllMembers().stream().filter(player -> player != this.owner).collect(Collectors.toSet()));