Skip to content

Commit

Permalink
fix: memory leak + reference
Browse files Browse the repository at this point in the history
  • Loading branch information
RealBauHD committed Jan 18, 2024
1 parent c86ad51 commit 3f7ddf3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -417,8 +417,9 @@ public void addPlayer(final SculkPlayer player) {
this.players.put(player.uniqueId(), player);
}

public void removePlayer(final UUID uniqueId) {
this.players.remove(uniqueId);
public void removePlayer(final SculkPlayer player) {
this.players.remove(player.uniqueId());
this.entities.remove(player.id());
}

public void addEntity(final AbstractEntity abstractEntity) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import io.github.sculkpowered.server.command.CommandSource;
import io.github.sculkpowered.server.connection.Connection;
import io.github.sculkpowered.server.entity.player.GameProfile;
import io.github.sculkpowered.server.entity.player.GameProfile.Property;
import io.github.sculkpowered.server.entity.player.PlayerInfoEntry;
import io.github.sculkpowered.server.entity.player.SculkPlayer;
import io.github.sculkpowered.server.event.player.PlayerDisconnectEvent;
Expand Down Expand Up @@ -50,7 +51,6 @@
import io.github.sculkpowered.server.protocol.packet.play.UpdateTeams;
import io.github.sculkpowered.server.protocol.packet.play.command.Commands;
import io.github.sculkpowered.server.util.MojangUtil;
import io.github.sculkpowered.server.entity.player.GameProfile.Property;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
Expand Down Expand Up @@ -119,30 +119,33 @@ public void channelRead(@NotNull ChannelHandlerContext ctx, @NotNull Object mess
@Override
public void channelInactive(@NotNull ChannelHandlerContext ctx) {
if (this.player != null) {
this.server.removePlayer(this.player.uniqueId());
this.server.removePlayer(this.player);
this.player.onDisconnect();

final var world = this.player.world();
final var position = this.player.position();
if (world != null) {
this.forChunksInRange(chunkCoordinate(position.x()), chunkCoordinate(position.z()),
this.server.addTask(() -> this.forChunksInRange(
chunkCoordinate(position.x()), chunkCoordinate(position.z()),
this.player.settings().viewDistance(), (x, z) -> {
final var chunk = world.chunk(x, z);
chunk.viewers().remove(this.player);
this.server.addTask(() -> {
for (final var entity : chunk.entities()) {
entity.removeViewer(this.player);
}
chunk.entities().remove(this.player);
});
});
for (final var entity : chunk.entities()) {
entity.removeViewer(this.player);
}
chunk.entities().remove(this.player);
}));
}
this.player.sendViewers(new RemoveEntities(this.player.id()));
this.server.sendAll(new PlayerInfoRemove(List.of(this.player)));
if (this.player.openedContainer() != null) {
this.player.openedContainer().removeViewer(this.player);
}

for (final var bossBar : this.player.activeBossBars()) {
this.player.hideBossBar(bossBar);
}

LOGGER.info(this.username + " has disconnected.");
this.server.eventHandler().call(new PlayerDisconnectEvent(this.player));
}
Expand Down Expand Up @@ -230,7 +233,10 @@ public void play() {
world.dimension().name()));

this.sendCommands();
this.player.calculateChunks(position, position, false, false);
this.send(new SpawnPosition(position));
this.send(new SynchronizePlayerPosition(position));

this.send(PlayerInfo.add((List<? extends PlayerInfoEntry>) this.server.onlinePlayers()));
final var playerInfo = PlayerInfo.add(List.of(this.player));
for (final var other : this.server.onlinePlayers()) {
Expand All @@ -243,9 +249,6 @@ public void play() {
this.send(new UpdateTeams(team, (byte) 0, team.entries().toArray(new String[]{})));
}

this.player.calculateChunks(position, position, false, false);
this.send(new SynchronizePlayerPosition(position));

this.server.eventHandler().justCall(new PlayerJoinEvent(this.player));
}, this.executor()).exceptionally(throwable -> {
LOGGER.error("Exception during login of player {}", this.player.name(), throwable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public void biome(int x, int y, int z, @NotNull Biome biome) {
}

public void send(SculkPlayer player) {
if (this.packet == null) {
if (this.packet == null || this.packet.get() == null) {
final var buf = new Buffer(
ByteBufAllocator.DEFAULT.buffer(this.sections.length * 8)); // minimum amount
final var skyMask = new BitSet();
Expand Down

0 comments on commit 3f7ddf3

Please sign in to comment.