Skip to content

Commit

Permalink
fix(bluemap/privatelocation): fix for new bluemap versions
Browse files Browse the repository at this point in the history
  • Loading branch information
Chicken committed Aug 21, 2024
1 parent fbdf2a9 commit b34bf6e
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public void forEachSession(SessionOperator op) {
@Override
public void onLoad() {
BlueMapAPI.onEnable(api -> {
reloadConfig();
saveDefaultConfig();
this.config = getConfig();

try {
Expand Down Expand Up @@ -85,9 +87,6 @@ public void onEnable() {
getServer().getPluginManager().registerEvents(this, this);

BlueMapAPI.onEnable(api -> {
reloadConfig();
saveDefaultConfig();

try {
this.http = new WebServer(Objects.requireNonNull(config.getString("ip", "0.0.0.0")), config.getInt("port", 8800));
} catch (IOException ex) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* This file is part of BMUtils, licensed under the MPL2 License (MPL).
* Please keep tabs on https://github.com/TechnicJelle/BMUtils for updates.
*
* Copyright (c) TechnicJelle <https://technicjelle.com>
* Copyright (c) contributors
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

package codes.antti.auth.bluemap.privatelocation;

import de.bluecolored.bluemap.api.BlueMapAPI;
import de.bluecolored.bluemap.api.BlueMapMap;
import de.bluecolored.bluemap.api.plugin.SkinProvider;
import org.jetbrains.annotations.NotNull;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Optional;
import java.util.UUID;

/**
* Utility functions for BlueMap skins and playerheads
*/
public class BMSkin {
private BMSkin() {
throw new IllegalStateException("Utility class");
}

public static void createPlayerHeadIfNotExists(
final @NotNull BlueMapAPI blueMapAPI,
final @NotNull BlueMapMap map,
final @NotNull UUID playerUUID
) throws IOException {
final String assetName = "playerheads/" + playerUUID + ".png";
if (map.getAssetStorage().assetExists(assetName)) return;
final SkinProvider skinProvider = blueMapAPI.getPlugin().getSkinProvider();
try {
final Optional<BufferedImage> oImgSkin = skinProvider.load(playerUUID);
if (oImgSkin.isEmpty()) {
throw new IOException(playerUUID + " doesn't have a skin");
}

try (OutputStream out = map.getAssetStorage().writeAsset(assetName)) {
final BufferedImage head = blueMapAPI.getPlugin().getPlayerMarkerIconFactory()
.apply(playerUUID, oImgSkin.get());
ImageIO.write(head, "png", out);
} catch (IOException e) {
throw new IOException("Failed to write " + playerUUID + "'s head to asset-storage", e);
}
} catch (IOException e) {
throw new IOException("Failed to load skin for player " + playerUUID, e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.google.gson.stream.JsonWriter;
import de.bluecolored.bluemap.api.BlueMapAPI;
import de.bluecolored.bluemap.api.BlueMapMap;
import de.bluecolored.bluemap.api.BlueMapWorld;
import net.luckperms.api.LuckPerms;
import net.luckperms.api.LuckPermsProvider;
import net.luckperms.api.model.user.User;
Expand All @@ -20,10 +21,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.HashMap;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import java.util.*;
import java.util.concurrent.CompletableFuture;

public final class BlueMapPrivateLocationPlugin extends JavaPlugin {
Expand Down Expand Up @@ -59,55 +57,48 @@ public void onEnable() {
return;
}
String mapId = request.getPath().substring(9);
Optional<BlueMapMap> optionalMap = api.getMap(mapId);
if (optionalMap.isPresent()) {
if (!loggedIn.equals("true")) {
request.setBody("{\"players\":[]}", "application/json");
request.respond(200);
return;
}
if (playerUuid == null) {
request.respond(400);
return;
}
World world = getServer().getWorld(UUID.fromString(optionalMap.get().getWorld().getId()));
permCheck(playerUuid).thenAcceptAsync((perm) -> {
String response = getPlayersObject(world, UUID.fromString(playerUuid), perm);
request.setBody(response, "application/json");
try {
request.respond(200);
} catch (IOException ex) {
getLogger().severe("Failed to send response");
ex.printStackTrace();
}
});
} else {
Optional<BlueMapMap> map = api.getMap(mapId);
if (map.isEmpty()) {
request.respond(404);
return;
}
if (!loggedIn.equals("true")) {
request.setBody("{\"players\":[]}", "application/json");
request.respond(200);
return;
}
if (playerUuid == null) {
request.respond(400);
return;
}
BlueMapWorld targetWorld = map.get().getWorld();
Optional<World> world = getServer().getWorlds().stream().filter((w) -> api.getWorld(w).map((bmw) -> bmw.equals(targetWorld)).orElse(false)).findFirst();
if (world.isEmpty()) {
request.respond(404);
return;
}
canSeeEveryone(playerUuid).thenAcceptAsync((showAll) -> {
String response = getPlayersObject(api, map.get(), world.get(), UUID.fromString(playerUuid), showAll);
request.setBody(response, "application/json");
try {
request.respond(200);
} catch (IOException ex) {
getLogger().severe("Failed to send response");
ex.printStackTrace();
}
});
});

this.http.start();
getLogger().info("Webserver bound to " + this.http.getAddress());

try {
Path scriptPath = api.getWebApp().getWebRoot().resolve("assets/bluemap-privatelocation.js");
Files.createDirectories(scriptPath.getParent());
OutputStream out = Files.newOutputStream(scriptPath, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
out.write(Objects.requireNonNull(getResource("bluemap-privatelocation.js")).readAllBytes());
out.close();
api.getWebApp().registerScript("assets/bluemap-privatelocation.js");
} catch (IOException ex) {
getLogger().severe("Couldn't move integration resources to BlueMap!");
ex.printStackTrace();
}
});
BlueMapAPI.onDisable(api -> {
if (this.http != null) this.http.close();
});
getLogger().info("BlueMap-PrivateLocation enabled");
}

public String getPlayersObject(World world, UUID playerUuid, boolean showAll) {
public String getPlayersObject(BlueMapAPI api, BlueMapMap map, World world, UUID playerUuid, boolean showAll) {
try (StringWriter jsonString = new StringWriter();
JsonWriter json = new JsonWriter(jsonString)) {

Expand All @@ -117,6 +108,12 @@ public String getPlayersObject(World world, UUID playerUuid, boolean showAll) {
Player[] players = showAll ? getServer().getOnlinePlayers().toArray(Player[]::new) : new Player[]{ this.getServer().getPlayer(playerUuid) };
for (Player player : players) {
if (player != null && player.isOnline()) {
try {
BMSkin.createPlayerHeadIfNotExists(api, map, playerUuid);
} catch (IOException ex) {
ex.printStackTrace();
}

json.beginObject();
json.name("uuid").value(player.getUniqueId().toString());
json.name("name").value(player.getName());
Expand Down Expand Up @@ -150,7 +147,7 @@ public String getPlayersObject(World world, UUID playerUuid, boolean showAll) {
}
}

public CompletableFuture<Boolean> permCheck(@NotNull String uuid) {
public CompletableFuture<Boolean> canSeeEveryone(@NotNull String uuid) {
Boolean cached = permissionCache.get(uuid);
Long expiry = permissionCacheExpiry.get(uuid);
if (expiry != null && expiry < System.currentTimeMillis()) {
Expand Down

This file was deleted.

0 comments on commit b34bf6e

Please sign in to comment.