Skip to content

Commit

Permalink
Jammed in a synchronized at a few places and updated some strings
Browse files Browse the repository at this point in the history
  • Loading branch information
RasmusAntons committed Sep 15, 2017
1 parent 5d6a5d1 commit 77e3946
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

public class ServerInstance extends eu.the5zig.mod.server.ServerInstance {
public static boolean isCubeCraft(String host) {
return host.equals("play.cubecraftgames.net") || host.equals("play.cubecraft.net");
return host.endsWith(".cubecraftgames.net") || host.endsWith(".cubecraft.net");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ public void render(int x, int y, RenderLocation renderLocation, boolean dummy) {
return;
drawHeader(x, y);
int lineNumber = 1;
for (StalkedPlayer player : stalkedPlayers) {
if (player.getKills() == 0 && player.getDeaths() == 0)
continue;
drawPlayer(player.getName(), player.getThreatColor(), player.getKills(), player.getDeaths(), x, y, lineNumber++);
synchronized (stalkedPlayers) {
for (StalkedPlayer player : stalkedPlayers) {
if (player.getKills() == 0 && player.getDeaths() == 0)
continue;
drawPlayer(player.getName(), player.getThreatColor(), player.getKills(), player.getDeaths(), x, y, lineNumber++);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,27 @@
import net.frozenbit.plugin5zig.cubecraft.CubeCraftPlayer;
import net.frozenbit.plugin5zig.cubecraft.Main;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArrayList;

public class Stalker {

private Storage storage;
private String ownName;
private GameMode gameMode;
private final List<StalkedPlayer> stalkedPlayerList;
private final List<StalkedPlayer> provisionalStalkedPlayerList;
private int maxNameWidth = 0;
private int shownPlayerCount = 0;
private int playerListIteration = 0;

public Stalker(GameMode gameMode) {
this.gameMode = gameMode;
storage = new Storage(gameMode);
ownName = The5zigAPI.getAPI().getGameProfile().getName();
stalkedPlayerList = new CopyOnWriteArrayList<>();
stalkedPlayerList = new ArrayList<>();
provisionalStalkedPlayerList = new ArrayList<>();
}

public void onKill(String victim, String killer) {
Expand All @@ -32,10 +35,12 @@ public void onKill(String victim, String killer) {
return;
String otherName = kill ? victim : killer;
StalkedPlayer otherPlayer = null;
for (StalkedPlayer player : stalkedPlayerList) {
if (player.getName().equals(otherName)) {
otherPlayer = player;
break;
synchronized (stalkedPlayerList) {
for (StalkedPlayer player : stalkedPlayerList) {
if (player.getName().equals(otherName)) {
otherPlayer = player;
break;
}
}
}
if (otherPlayer == null) {
Expand All @@ -49,32 +54,30 @@ public void onKill(String victim, String killer) {
else
otherPlayer.onDeath();
final StalkedPlayer storedPlayer = otherPlayer;
new Thread() {
@Override
public void run() {
storage.storePlayer(storedPlayer);
}
}.start();
new Thread(() -> storage.storePlayer(storedPlayer)).start();
}

public void onPlayerListUpdate(final List<CubeCraftPlayer> playerList) {
maxNameWidth = 0;
stalkedPlayerList.clear();
final int frozenPlayerListIteration = ++playerListIteration;
shownPlayerCount = 0;
new Thread() {
@Override
public void run() {
new Thread(() -> {
provisionalStalkedPlayerList.clear();
synchronized (provisionalStalkedPlayerList) {
for (CubeCraftPlayer cubeCraftPlayer : playerList) {
if (playerListIteration != frozenPlayerListIteration)
return;
maxNameWidth = Math.max(maxNameWidth, The5zigAPI.getAPI().getRenderHelper().getStringWidth(cubeCraftPlayer.getName()));
StalkedPlayer stalkedPlayer = storage.getStalkedPlayer(cubeCraftPlayer);
if (stalkedPlayer.getKills() != 0 || stalkedPlayer.getDeaths() != 0)
++shownPlayerCount;
provisionalStalkedPlayerList.add(stalkedPlayer);
}
synchronized (stalkedPlayerList) {
for (CubeCraftPlayer cubeCraftPlayer : playerList) {
maxNameWidth = Math.max(maxNameWidth, The5zigAPI.getAPI().getRenderHelper().getStringWidth(cubeCraftPlayer.getName()));
StalkedPlayer stalkedPlayer = storage.getStalkedPlayer(cubeCraftPlayer);
if (stalkedPlayer.getKills() != 0 || stalkedPlayer.getDeaths() != 0)
++shownPlayerCount;
stalkedPlayerList.add(stalkedPlayer);
}
stalkedPlayerList.addAll(provisionalStalkedPlayerList);
}
}
}.start();
}).start();
}

public List<StalkedPlayer> getStalkedPlayerList() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

import java.io.Closeable;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.*;
Expand Down Expand Up @@ -107,12 +106,7 @@ private interface DbModifier {
private static List<String> getPLayerStats(UUID id, DbModifier modifier) {
List<String> playerStats = new ArrayList<>();
File stalkerDir = Main.PLUGIN_PATH.resolve("stalker").toFile();
File[] directories = stalkerDir.listFiles(new FileFilter() {
@Override
public boolean accept(File file) {
return file.isDirectory();
}
});
File[] directories = stalkerDir.listFiles(File::isDirectory);
if (directories == null)
throw new RuntimeException("Illegal database state: No directories");
for (File directory : directories) {
Expand Down Expand Up @@ -149,12 +143,9 @@ public static List<String> getPlayerStats(UUID id) {
}

public static List<String> deletePlayerStats(UUID id, final String gamemode) {
List<String> output = getPLayerStats(id, new DbModifier() {
@Override
public void modify(DB db, byte[] key, String currentGamemode) {
if (gamemode.equals(GAMEMODE_WILDCARD) || gamemode.equals(currentGamemode))
db.delete(key);
}
List<String> output = getPLayerStats(id, (db, key, currentGamemode) -> {
if (gamemode.equals(GAMEMODE_WILDCARD) || gamemode.equals(currentGamemode))
db.delete(key);
});
updatedPlayers.add(id);
return output;
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/core/messages/cubecraft.properties
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ sg.draw.health=Selected (Hardcore|Normal|Double|Triple) Health health (?:randoml
skywars.starting=Teleporting to map...
skywars.vote.loot=(%p) voted for (Overpowered|Basic|Normal) chests! %d votes?!
skywars.draw.loot=Selected (Overpowered|Basic|Normal) chests.+
skywars.vote.time=(%p) voted for (Day|Night|Sunset)(?: Time)?! %d votes?!
skywars.vote.time=(%p) voted for (Day|Night|Sunset)(?: T|time)?! %d votes?!
skywars.draw.time=Selected (Day(?: time)|Night(?: time)|Sunset).*
skywars.countdown.lobby=(?:Team )?SkyWars starting in (%d) seconds
skywars.countdown.starting=You will be released in (%d) seconds
Expand Down

0 comments on commit 77e3946

Please sign in to comment.