Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
ShirasawaSama committed Aug 17, 2021
1 parent fc26100 commit f45bbcc
Show file tree
Hide file tree
Showing 12 changed files with 191 additions and 101 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nekomaid",
"version": "0.0.8",
"version": "0.0.9",
"description": "A plugin can use Web to manage your server.",
"main": "index.js",
"private": true,
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/cn/apisium/nekomaid/NekoMaid.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ public void onEnable() {
.put("plugins", pluginScripts)
.put("version", getServer().getVersion())
.put("onlineMode", getServer().getOnlineMode())
.put("hasWhitelist", getServer().hasWhitelist())
.put("pluginVersion", getDescription().getVersion());
if (Utils.IS_PAPER) GLOBAL_DATA.put("isPaper", true);
try {
Expand Down Expand Up @@ -167,6 +166,10 @@ public void onEnable() {
} catch (Throwable e) {e.printStackTrace();}
});
connectListeners.forEach((k, v) -> v.accept(getClient(k, client)));
GLOBAL_DATA
.put("hasWhitelist", getServer().hasWhitelist())
.put("maxPlayers", getServer().getMaxPlayers())
.put("spawnRadius", getServer().getSpawnRadius());
client.send("globalData", GLOBAL_DATA);
}).on("error", System.out::println);
Uniporter.registerHandler("NekoMaid", new MainHandler(), true);
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/cn/apisium/nekomaid/builtin/BuiltinPlugins.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public final class BuiltinPlugins {
public BuiltinPlugins(NekoMaid main) {
new Dashboard(main);
console = new Console(main);
PlayerList.initPlayerList(main);
PlayerList.init(main);
Options.init(main);
files = new FilesManager(main);
main.getServer().getScheduler().runTask(main, () -> {
new Plugins(main);
Expand All @@ -19,7 +20,7 @@ public BuiltinPlugins(NekoMaid main) {
if (pm.getPlugin("Vault") != null) new Vault(main);
new OpenInv(main);
new Worlds(main);
if (pm.getPlugin("NBTAPI") != null) ItemEditor.initItemEditor(main);
if (pm.getPlugin("NBTAPI") != null) ItemEditor.init(main);
});
}
public void disable() {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cn/apisium/nekomaid/builtin/ItemEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class ItemEditor {
data[1] = Utils.serializeToString(new String[0]);
}
}
public static void initItemEditor(NekoMaid main) {
public static void init(NekoMaid main) {
main.GLOBAL_DATA.put("hasNBTAPI", true);
main.onConnected(main, client -> client.onWithMultiArgsAck("item:fetch", () -> data));
}
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/cn/apisium/nekomaid/builtin/Options.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cn.apisium.nekomaid.builtin;

import cn.apisium.nekomaid.NekoMaid;
import org.bukkit.Server;

final class Options {
private static boolean canSetMaxPlayers;
static {
try {
Server.class.getMethod("setMaxPlayers", int.class);
canSetMaxPlayers = true;
} catch (Throwable ignored) { }
}
public static void init(NekoMaid main) {
if (canSetMaxPlayers) main.GLOBAL_DATA.put("canSetMaxPlayers", true);
main.onConnected(main, client -> client
.on("server:set", args -> main.getServer().getScheduler().runTask(main, () -> {
switch ((String) args[0]) {
case "maxPlayers": if (canSetMaxPlayers) main.getServer().setMaxPlayers((int) args[1]); break;
case "spawnRadius": main.getServer().setSpawnRadius((int) args[1]); break;
case "hasWhitelist": main.getServer().setWhitelist((boolean) args[1]); break;
}
})));
}
}
2 changes: 1 addition & 1 deletion src/main/java/cn/apisium/nekomaid/builtin/PlayerList.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public List(long a, Object[] c) {
}
}
@SuppressWarnings({"deprecation", "ConstantConditions"})
public static void initPlayerList(NekoMaid main) {
public static void init(NekoMaid main) {
Server server = main.getServer();
main.onConnected(main, client -> client.onWithAck("playerList:fetchPage", args -> {
Stream<OfflinePlayer> list;
Expand Down
179 changes: 97 additions & 82 deletions src/main/java/cn/apisium/nekomaid/builtin/Worlds.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.bukkit.event.player.PlayerChangedWorldEvent;
import org.bukkit.event.weather.ThunderChangeEvent;
import org.bukkit.event.weather.WeatherChangeEvent;
import org.bukkit.event.world.TimeSkipEvent;
import org.bukkit.event.world.WorldLoadEvent;
import org.bukkit.event.world.WorldUnloadEvent;
import org.bukkit.plugin.Plugin;
Expand All @@ -22,8 +21,8 @@
import java.util.UUID;
import java.util.concurrent.FutureTask;

final class Worlds implements Listener {
private boolean hasWorldGameRuleChangeEvent = false, canSetViewDistance;
final class Worlds {
private boolean hasWorldGameRuleChangeEvent = false, canSetViewDistance, hasSeparateViewDistance;
private final Plugin mv;
private final NekoMaid main;
private final static class World {
Expand All @@ -38,67 +37,74 @@ public Worlds(NekoMaid main) {
this.main = main;
mv = main.getServer().getPluginManager().getPlugin("Multiverse-Core");
if (mv != null) main.GLOBAL_DATA.put("hasMultiverse", true);
main.onConnected(main, client -> client.onWithAck("worlds:fetch", this::getWorlds)
.onWithAck("worlds:weather", args -> {
org.bukkit.World world = main.getServer().getWorld(UUID.fromString((String) args[0]));
if (world == null) return;
main.getServer().getScheduler().runTask(main, () -> {
if (world.isThundering()) {
world.setThundering(false);
world.setStorm(false);
} else if (world.hasStorm()) world.setThundering(true);
else world.setStorm(true);
});
}).onWithAck("worlds:rule", args -> {
org.bukkit.World world = main.getServer().getWorld(UUID.fromString((String) args[0]));
if (world == null) return;
String k = (String) args[1], v = (String) args[2];
main.getServer().getScheduler().runTask(main, () -> {
world.setGameRuleValue(k, v);
if (!hasWorldGameRuleChangeEvent) update();
});
}).onWithAck("worlds:difficulty", args -> {
org.bukkit.World world = main.getServer().getWorld(UUID.fromString((String) args[0]));
if (world == null) return;
String value = (String) args[1];
Difficulty diff = Difficulty.valueOf(value);
main.getServer().getScheduler().runTask(main, () -> {
world.setDifficulty(diff);
if (mv != null) try {
MVWorldManager wm = ((MultiverseCore) mv).getMVWorldManager();
wm.getMVWorld(world).setPropertyValue("difficulty", value);
((MultiverseCore) mv).getMVWorldManager().saveWorldsConfig();
} catch (Throwable e) {
e.printStackTrace();
}
update();
});
}).onWithAck("worlds:pvp", args -> {
main.onConnected(main, client -> {
client.onWithAck("worlds:fetch", this::getWorlds)
.onWithAck("worlds:weather", args -> {
org.bukkit.World world = main.getServer().getWorld(UUID.fromString((String) args[0]));
if (world == null) return;
main.getServer().getScheduler().runTask(main, () -> {
if (world.isThundering()) {
world.setThundering(false);
world.setStorm(false);
} else if (world.hasStorm()) world.setThundering(true);
else world.setStorm(true);
});
}).onWithAck("worlds:rule", args -> {
org.bukkit.World world = main.getServer().getWorld(UUID.fromString((String) args[0]));
if (world == null) return;
String k = (String) args[1], v = (String) args[2];
main.getServer().getScheduler().runTask(main, () -> {
world.setGameRuleValue(k, v);
if (!hasWorldGameRuleChangeEvent) update();
});
}).onWithAck("worlds:difficulty", args -> {
org.bukkit.World world = main.getServer().getWorld(UUID.fromString((String) args[0]));
if (world == null) return;
String value = (String) args[1];
Difficulty diff = Difficulty.valueOf(value);
main.getServer().getScheduler().runTask(main, () -> {
world.setDifficulty(diff);
if (mv != null) try {
MVWorldManager wm = ((MultiverseCore) mv).getMVWorldManager();
wm.getMVWorld(world).setPropertyValue("difficulty", value);
((MultiverseCore) mv).getMVWorldManager().saveWorldsConfig();
} catch (Throwable e) {
e.printStackTrace();
}
update();
});
}).onWithAck("worlds:pvp", args -> {
org.bukkit.World world = main.getServer().getWorld(UUID.fromString((String) args[0]));
if (world == null) return;
boolean value = (boolean) args[1];
main.getServer().getScheduler().runTask(main, () -> {
world.setPVP(value);
if (mv != null) try {
MVWorldManager wm = ((MultiverseCore) mv).getMVWorldManager();
wm.getMVWorld(world).setPropertyValue("pvp", String.valueOf(value));
((MultiverseCore) mv).getMVWorldManager().saveWorldsConfig();
} catch (Throwable e) {
e.printStackTrace();
}
update();
});
}).onWithAck("worlds:viewDistance", args -> {
org.bukkit.World world = main.getServer().getWorld(UUID.fromString((String) args[0]));
if (world == null || !canSetViewDistance) return;
main.getServer().getScheduler().runTask(main, () -> {
world.setViewDistance((int) args[1]);
update();
});
}).onWithAck("worlds:save", args -> {
org.bukkit.World world = main.getServer().getWorld(UUID.fromString((String) args[0]));
if (world == null) return;
main.getServer().getScheduler().runTask(main, world::save);
});
if (mv != null) {
MVWorldManager wm = ((MultiverseCore) mv).getMVWorldManager();
client.onWithAck("worlds:set", args -> {
org.bukkit.World world = main.getServer().getWorld(UUID.fromString((String) args[0]));
if (world == null) return;
boolean value = (boolean) args[1];
main.getServer().getScheduler().runTask(main, () -> {
world.setPVP(value);
if (mv != null) try {
MVWorldManager wm = ((MultiverseCore) mv).getMVWorldManager();
wm.getMVWorld(world).setPropertyValue("pvp", String.valueOf(value));
((MultiverseCore) mv).getMVWorldManager().saveWorldsConfig();
} catch (Throwable e) {
e.printStackTrace();
}
update();
});
}).onWithAck("worlds:viewDistance", args -> {
org.bukkit.World world = main.getServer().getWorld(UUID.fromString((String) args[0]));
if (world == null || !canSetViewDistance) return;
main.getServer().getScheduler().runTask(main, () -> {
world.setViewDistance((int) args[1]);
update();
});
}).onWithAck("worlds:set", args -> {
org.bukkit.World world = main.getServer().getWorld(UUID.fromString((String) args[0]));
if (world == null || mv == null) return;
MVWorldManager wm = ((MultiverseCore) mv).getMVWorldManager();
main.getServer().getScheduler().runTask(main, () -> {
try {
wm.getMVWorld(world).setPropertyValue((String) args[1], (String) args[2]);
Expand All @@ -108,41 +114,50 @@ public Worlds(NekoMaid main) {
e.printStackTrace();
}
});
}).onWithAck("worlds:save", args -> {
org.bukkit.World world = main.getServer().getWorld(UUID.fromString((String) args[0]));
if (world == null) return;
main.getServer().getScheduler().runTask(main, world::save);
}));
});
}
});
Events events = new Events();
main.getServer().getScheduler()
.runTask(main, () -> main.getServer().getPluginManager().registerEvents(this, main));
.runTask(main, () -> main.getServer().getPluginManager().registerEvents(events, main));
try {
Class<? extends Event> clazz = (Class<? extends Event>)
Class.forName("io.papermc.paper.event.world.WorldGameRuleChangeEvent");
main.getServer().getPluginManager().registerEvent(clazz, this,
main.getServer().getPluginManager().registerEvent(clazz, events,
EventPriority.MONITOR, (a, b) -> update(), main, true);
hasWorldGameRuleChangeEvent = true;
} catch (Throwable ignored) { }
try {
Class<? extends Event> clazz = (Class<? extends Event>)
Class.forName("org.bukkit.event.world.TimeSkipEvent");
main.getServer().getPluginManager().registerEvent(clazz, events,
EventPriority.MONITOR, (a, b) -> update(), main, true);
} catch (Throwable ignored) { }
try {
org.bukkit.World.class.getMethod("setViewDistance", int.class);
canSetViewDistance = true;
main.GLOBAL_DATA.put("canSetViewDistance", true);
} catch (Throwable ignored) { }
try {
org.bukkit.World.class.getMethod("getViewDistance");
hasSeparateViewDistance = true;
} catch (Throwable ignored) { }
}

private void update() { main.broadcastInPage(main, "worlds", "worlds:update"); }

@EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR)
public void onWeatherChange(WeatherChangeEvent e) { update(); }
@EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR)
public void onThunderChange(ThunderChangeEvent e) { update(); }
@EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR)
public void onTimeSkip(TimeSkipEvent e) { update(); }
@EventHandler
public void onPlayerChangedWorld(PlayerChangedWorldEvent e) { update(); }
@EventHandler
public void onWorldLoad(WorldLoadEvent e) { update(); }
@EventHandler
public void onWorldUnload(WorldUnloadEvent e) { update(); }
private final class Events implements Listener {
@EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR)
public void onWeatherChange(WeatherChangeEvent e) { update(); }
@EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR)
public void onThunderChange(ThunderChangeEvent e) { update(); }
@EventHandler
public void onPlayerChangedWorld(PlayerChangedWorldEvent e) { update(); }
@EventHandler
public void onWorldLoad(WorldLoadEvent e) { update(); }
@EventHandler
public void onWorldUnload(WorldUnloadEvent e) { update(); }
}

@SuppressWarnings("deprecation")
private Object[] getWorlds() {
Expand All @@ -155,7 +170,7 @@ private Object[] getWorlds() {
w.players = it.getPlayers().size();
w.chunks = chunks.length;
w.weather = it.isThundering() ? 2 : it.hasStorm() ? 1 : 0;
w.viewDistance = it.getViewDistance();
w.viewDistance = hasSeparateViewDistance ? it.getViewDistance() : main.getServer().getViewDistance();
w.allowMonsters = it.getAllowMonsters();
w.allowAnimals = it.getAllowAnimals();
w.pvp = it.getPVP();
Expand Down
2 changes: 1 addition & 1 deletion vscode-material-icon-theme
5 changes: 4 additions & 1 deletion web/Plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,18 @@ export interface GlobalInfo {
canLoadPlugin: boolean
pluginVersion?: string
icon?: string
isPaper: boolean
isPaper?: boolean
hasVault?: boolean
hasVaultPermission?: boolean
hasVaultGroups?: boolean
hasVaultChat?: boolean
hasOpenInv?: boolean
hasNBTAPI?: boolean
hasMultiverse?: boolean
canSetMaxPlayers?: boolean
canSetViewDistance?: boolean
maxPlayers: number
spawnRadius: number
plugins: Record<string, string[]>
vaultEconomy?: {
singular: string
Expand Down
Loading

0 comments on commit f45bbcc

Please sign in to comment.