2020-08-25 19:25:31 +02:00
|
|
|
/*
|
|
|
|
* This file is part of BlueMap, licensed under the MIT License (MIT).
|
|
|
|
*
|
|
|
|
* Copyright (c) Blue (Lukas Rieger) <https://bluecolored.de>
|
|
|
|
* Copyright (c) contributors
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
package de.bluecolored.bluemap.fabric;
|
|
|
|
|
|
|
|
import com.github.benmanes.caffeine.cache.Caffeine;
|
|
|
|
import com.github.benmanes.caffeine.cache.LoadingCache;
|
|
|
|
import de.bluecolored.bluemap.common.plugin.Plugin;
|
|
|
|
import de.bluecolored.bluemap.common.plugin.commands.Commands;
|
2022-06-06 22:51:26 +02:00
|
|
|
import de.bluecolored.bluemap.common.serverinterface.Player;
|
|
|
|
import de.bluecolored.bluemap.common.serverinterface.ServerEventListener;
|
|
|
|
import de.bluecolored.bluemap.common.serverinterface.ServerInterface;
|
|
|
|
import de.bluecolored.bluemap.common.serverinterface.ServerWorld;
|
2020-09-15 12:02:12 +02:00
|
|
|
import de.bluecolored.bluemap.core.BlueMap;
|
2020-08-25 19:25:31 +02:00
|
|
|
import de.bluecolored.bluemap.core.MinecraftVersion;
|
|
|
|
import de.bluecolored.bluemap.core.logger.Logger;
|
2023-10-03 12:31:45 +02:00
|
|
|
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
|
2020-08-25 19:25:31 +02:00
|
|
|
import net.fabricmc.api.ModInitializer;
|
|
|
|
import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback;
|
|
|
|
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
|
|
|
|
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
|
|
|
|
import net.minecraft.server.MinecraftServer;
|
|
|
|
import net.minecraft.server.network.ServerPlayerEntity;
|
2022-07-24 12:10:00 +02:00
|
|
|
import net.minecraft.util.Identifier;
|
|
|
|
import net.minecraft.world.dimension.DimensionType;
|
2021-05-23 10:12:34 +02:00
|
|
|
import org.apache.logging.log4j.LogManager;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
2022-04-20 22:54:27 +02:00
|
|
|
import java.nio.file.Path;
|
2021-05-23 10:12:34 +02:00
|
|
|
import java.util.*;
|
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
2020-08-25 19:25:31 +02:00
|
|
|
|
|
|
|
public class FabricMod implements ModInitializer, ServerInterface {
|
|
|
|
|
2022-04-20 22:54:27 +02:00
|
|
|
private final Plugin pluginInstance;
|
2021-09-19 22:15:50 +02:00
|
|
|
private MinecraftServer serverInstance = null;
|
|
|
|
|
2022-04-20 22:54:27 +02:00
|
|
|
private final FabricEventForwarder eventForwarder;
|
2021-09-19 22:15:50 +02:00
|
|
|
|
2022-04-20 22:54:27 +02:00
|
|
|
private final LoadingCache<net.minecraft.server.world.ServerWorld, ServerWorld> worlds;
|
2021-09-19 22:15:50 +02:00
|
|
|
|
|
|
|
private int playerUpdateIndex = 0;
|
2022-04-20 22:54:27 +02:00
|
|
|
private final Map<UUID, Player> onlinePlayerMap;
|
|
|
|
private final List<FabricPlayer> onlinePlayerList;
|
2021-09-19 22:15:50 +02:00
|
|
|
|
|
|
|
public FabricMod() {
|
2023-07-01 09:44:19 +02:00
|
|
|
Logger.global.clear();
|
|
|
|
Logger.global.put(new Log4jLogger(LogManager.getLogger(Plugin.PLUGIN_NAME)));
|
2021-09-19 22:15:50 +02:00
|
|
|
|
|
|
|
this.onlinePlayerMap = new ConcurrentHashMap<>();
|
|
|
|
this.onlinePlayerList = Collections.synchronizedList(new ArrayList<>());
|
|
|
|
|
2022-04-20 22:54:27 +02:00
|
|
|
pluginInstance = new Plugin("fabric-1.15.2", this);
|
2021-09-19 22:15:50 +02:00
|
|
|
|
|
|
|
this.eventForwarder = new FabricEventForwarder(this);
|
2022-04-20 22:54:27 +02:00
|
|
|
this.worlds = Caffeine.newBuilder()
|
2021-09-19 22:15:50 +02:00
|
|
|
.executor(BlueMap.THREAD_POOL)
|
|
|
|
.weakKeys()
|
|
|
|
.maximumSize(1000)
|
2022-04-20 22:54:27 +02:00
|
|
|
.build(FabricWorld::new);
|
2021-09-19 22:15:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onInitialize() {
|
|
|
|
|
|
|
|
//register commands
|
2022-04-20 22:54:27 +02:00
|
|
|
CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) ->
|
|
|
|
new Commands<>(pluginInstance, dispatcher, fabricSource ->
|
|
|
|
new FabricCommandSource(this, pluginInstance, fabricSource)
|
|
|
|
)
|
|
|
|
);
|
2021-09-19 22:15:50 +02:00
|
|
|
|
|
|
|
ServerLifecycleEvents.SERVER_STARTED.register((MinecraftServer server) -> {
|
|
|
|
this.serverInstance = server;
|
|
|
|
|
|
|
|
new Thread(()->{
|
|
|
|
Logger.global.logInfo("Loading BlueMap...");
|
|
|
|
|
|
|
|
try {
|
|
|
|
pluginInstance.load();
|
|
|
|
if (pluginInstance.isLoaded()) Logger.global.logInfo("BlueMap loaded!");
|
2022-05-28 21:55:41 +02:00
|
|
|
} catch (IOException e) {
|
2021-09-19 22:15:50 +02:00
|
|
|
Logger.global.logError("Failed to load bluemap!", e);
|
|
|
|
pluginInstance.unload();
|
|
|
|
}
|
2022-12-13 16:54:31 +01:00
|
|
|
}, "BlueMap-Plugin-Loading").start();
|
2021-09-19 22:15:50 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
ServerLifecycleEvents.SERVER_STOPPING.register((MinecraftServer server) -> {
|
|
|
|
pluginInstance.unload();
|
|
|
|
Logger.global.logInfo("BlueMap unloaded!");
|
|
|
|
});
|
|
|
|
|
2023-10-03 12:31:45 +02:00
|
|
|
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> {
|
|
|
|
this.onPlayerJoin(server, handler.player);
|
|
|
|
});
|
|
|
|
|
|
|
|
ServerPlayConnectionEvents.DISCONNECT.register((handler, server) -> {
|
|
|
|
this.onPlayerLeave(server, handler.player);
|
|
|
|
});
|
2021-09-19 22:15:50 +02:00
|
|
|
|
|
|
|
ServerTickEvents.END_SERVER_TICK.register((MinecraftServer server) -> {
|
|
|
|
if (server == this.serverInstance) this.updateSomePlayers();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-20 22:54:27 +02:00
|
|
|
@Override
|
|
|
|
public MinecraftVersion getMinecraftVersion() {
|
|
|
|
return new MinecraftVersion(1, 15, 2);
|
|
|
|
}
|
|
|
|
|
2021-09-19 22:15:50 +02:00
|
|
|
@Override
|
|
|
|
public void registerListener(ServerEventListener listener) {
|
|
|
|
eventForwarder.addEventListener(listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void unregisterAllListeners() {
|
|
|
|
eventForwarder.removeAllListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-04-20 22:54:27 +02:00
|
|
|
public Collection<ServerWorld> getLoadedWorlds() {
|
|
|
|
Collection<ServerWorld> loadedWorlds = new ArrayList<>(3);
|
|
|
|
for (net.minecraft.server.world.ServerWorld serverWorld : serverInstance.getWorlds()) {
|
|
|
|
loadedWorlds.add(worlds.get(serverWorld));
|
2021-09-19 22:15:50 +02:00
|
|
|
}
|
2022-04-20 22:54:27 +02:00
|
|
|
return loadedWorlds;
|
2021-09-19 22:15:50 +02:00
|
|
|
}
|
|
|
|
|
2022-07-24 12:10:00 +02:00
|
|
|
@Override
|
|
|
|
public Optional<ServerWorld> getWorld(Object world) {
|
|
|
|
if (world instanceof Path)
|
|
|
|
return getWorld((Path) world);
|
|
|
|
|
2022-08-03 12:15:55 +02:00
|
|
|
if (world instanceof String) {
|
|
|
|
Identifier identifier = Identifier.tryParse((String) world);
|
|
|
|
if (identifier != null) world = identifier;
|
|
|
|
}
|
|
|
|
|
2022-07-24 12:10:00 +02:00
|
|
|
if (world instanceof Identifier) {
|
|
|
|
DimensionType dimensionType = DimensionType.byId((Identifier) world);
|
|
|
|
if (dimensionType != null) world = dimensionType;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (world instanceof DimensionType) {
|
|
|
|
var serverWorld = serverInstance.getWorld((DimensionType) world);
|
|
|
|
if (serverWorld != null) world = serverWorld;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (world instanceof net.minecraft.server.world.ServerWorld)
|
|
|
|
return Optional.of(getWorld((net.minecraft.server.world.ServerWorld) world));
|
|
|
|
|
|
|
|
return Optional.empty();
|
|
|
|
}
|
|
|
|
|
2022-04-20 22:54:27 +02:00
|
|
|
public ServerWorld getWorld(net.minecraft.server.world.ServerWorld serverWorld) {
|
|
|
|
return worlds.get(serverWorld);
|
2021-09-19 22:15:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-04-20 22:54:27 +02:00
|
|
|
public Path getConfigFolder() {
|
|
|
|
return Path.of("config", "bluemap");
|
2021-09-19 22:15:50 +02:00
|
|
|
}
|
|
|
|
|
2022-06-01 00:03:35 +02:00
|
|
|
@Override
|
|
|
|
public Optional<Path> getModsFolder() {
|
|
|
|
return Optional.of(Path.of("mods"));
|
|
|
|
}
|
|
|
|
|
2021-09-19 22:15:50 +02:00
|
|
|
public void onPlayerJoin(MinecraftServer server, ServerPlayerEntity playerInstance) {
|
|
|
|
if (this.serverInstance != server) return;
|
|
|
|
|
2022-10-10 17:46:57 +02:00
|
|
|
FabricPlayer player = new FabricPlayer(playerInstance.getUuid(), this);
|
2021-09-19 22:15:50 +02:00
|
|
|
onlinePlayerMap.put(player.getUuid(), player);
|
|
|
|
onlinePlayerList.add(player);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onPlayerLeave(MinecraftServer server, ServerPlayerEntity player) {
|
|
|
|
if (this.serverInstance != server) return;
|
|
|
|
|
|
|
|
UUID playerUUID = player.getUuid();
|
|
|
|
onlinePlayerMap.remove(playerUUID);
|
|
|
|
synchronized (onlinePlayerList) {
|
|
|
|
onlinePlayerList.removeIf(p -> p.getUuid().equals(playerUUID));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public MinecraftServer getServer() {
|
|
|
|
return this.serverInstance;
|
|
|
|
}
|
|
|
|
|
2022-10-10 17:46:57 +02:00
|
|
|
public Plugin getPluginInstance() {
|
|
|
|
return pluginInstance;
|
|
|
|
}
|
|
|
|
|
2021-09-19 22:15:50 +02:00
|
|
|
@Override
|
|
|
|
public Collection<Player> getOnlinePlayers() {
|
|
|
|
return onlinePlayerMap.values();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Optional<Player> getPlayer(UUID uuid) {
|
|
|
|
return Optional.ofNullable(onlinePlayerMap.get(uuid));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Only update some of the online players each tick to minimize performance impact on the server-thread.
|
|
|
|
* Only call this method on the server-thread.
|
|
|
|
*/
|
|
|
|
private void updateSomePlayers() {
|
|
|
|
int onlinePlayerCount = onlinePlayerList.size();
|
|
|
|
if (onlinePlayerCount == 0) return;
|
|
|
|
|
|
|
|
int playersToBeUpdated = onlinePlayerCount / 20; //with 20 tps, each player is updated once a second
|
|
|
|
if (playersToBeUpdated == 0) playersToBeUpdated = 1;
|
|
|
|
|
|
|
|
for (int i = 0; i < playersToBeUpdated; i++) {
|
|
|
|
playerUpdateIndex++;
|
|
|
|
if (playerUpdateIndex >= 20 && playerUpdateIndex >= onlinePlayerCount) playerUpdateIndex = 0;
|
|
|
|
|
|
|
|
if (playerUpdateIndex < onlinePlayerCount) {
|
|
|
|
onlinePlayerList.get(playerUpdateIndex).update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-25 19:25:31 +02:00
|
|
|
}
|