Skip to content

Commit d172d0f

Browse files
committed
Start implementing the forge-mod
1 parent 1ddc229 commit d172d0f

File tree

7 files changed

+404
-19
lines changed

7 files changed

+404
-19
lines changed

BlueMapForge/build.gradle

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,24 @@ build.dependsOn shadowJar {
3535
relocate 'com.typesafe', 'de.bluecolored.bluemap.typesafe'
3636
relocate 'net.querz', 'de.bluecolored.bluemap.querz'
3737
relocate 'ninja', 'de.bluecolored.bluemap.ninja'
38-
relocate 'org.apache', 'de.bluecolored.bluemap.apache'
38+
relocate 'org.apache.commons', 'de.bluecolored.bluemap.apache.commons'
3939
relocate 'org.yaml', 'de.bluecolored.bluemap.yaml'
4040
}
4141

4242
processResources {
4343
from(sourceSets.main.resources.srcDirs) {
44-
include 'mcmod.info'
44+
include 'mcmod.info','META-INF/mods.toml'
4545

4646
expand (
4747
version: project.version
4848
)
4949
}
5050
}
51+
52+
afterEvaluate {
53+
reobf {
54+
shadowJar {
55+
mappings = createMcpToSrg.output
56+
}
57+
}
58+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package de.bluecolored.bluemap.forge;
2+
3+
import de.bluecolored.bluemap.common.plugin.serverinterface.CommandSource;
4+
import de.bluecolored.bluemap.common.plugin.text.Text;
5+
import net.minecraft.util.text.ITextComponent;
6+
7+
public class ForgeCommandSource implements CommandSource {
8+
9+
private net.minecraft.command.CommandSource delegate;
10+
11+
public ForgeCommandSource(net.minecraft.command.CommandSource delegate) {
12+
this.delegate = delegate;
13+
}
14+
15+
@Override
16+
public void sendMessage(Text text) {
17+
delegate.sendFeedback(ITextComponent.Serializer.fromJson(text.toJSONString()), false);
18+
}
19+
20+
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
package de.bluecolored.bluemap.forge;
2+
3+
import com.mojang.brigadier.Command;
4+
import com.mojang.brigadier.CommandDispatcher;
5+
import com.mojang.brigadier.LiteralMessage;
6+
import com.mojang.brigadier.arguments.ArgumentType;
7+
import com.mojang.brigadier.arguments.IntegerArgumentType;
8+
import com.mojang.brigadier.arguments.StringArgumentType;
9+
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
10+
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
11+
import com.mojang.brigadier.context.CommandContext;
12+
import com.mojang.brigadier.exceptions.CommandExceptionType;
13+
import com.mojang.brigadier.exceptions.CommandSyntaxException;
14+
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
15+
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
16+
17+
import de.bluecolored.bluemap.common.plugin.Commands;
18+
import de.bluecolored.bluemap.common.plugin.Plugin;
19+
import de.bluecolored.bluemap.common.plugin.text.Text;
20+
import de.bluecolored.bluemap.common.plugin.text.TextColor;
21+
import net.minecraft.command.CommandSource;
22+
import net.minecraft.entity.player.PlayerEntity;
23+
import net.minecraftforge.server.permission.PermissionAPI;
24+
25+
public class ForgeCommands {
26+
27+
private ForgeMod mod;
28+
private Plugin bluemap;
29+
private Commands commands;
30+
31+
public ForgeCommands(ForgeMod mod, Plugin bluemap) {
32+
this.mod = mod;
33+
this.bluemap = bluemap;
34+
this.commands = bluemap.getCommands();
35+
}
36+
37+
public void registerCommands(CommandDispatcher<CommandSource> dispatcher) {
38+
39+
LiteralArgumentBuilder<CommandSource> base = literal("bluemap");
40+
41+
base.executes(c -> {
42+
if (!checkPermission(c, "bluemap.status")) return 0;
43+
44+
commands.executeRootCommand(new ForgeCommandSource(c.getSource()));
45+
return 1;
46+
});
47+
48+
base.then(literal("reload")).executes(c -> {
49+
if (!checkPermission(c, "bluemap.reload")) return 0;
50+
51+
commands.executeReloadCommand(new ForgeCommandSource(c.getSource()));
52+
return 1;
53+
});
54+
55+
base.then(literal("pause")).executes(c -> {
56+
if (!checkPermission(c, "bluemap.pause")) return 0;
57+
58+
commands.executePauseCommand(new ForgeCommandSource(c.getSource()));
59+
return 1;
60+
});
61+
62+
base.then(literal("resume")).executes(c -> {
63+
if (!checkPermission(c, "bluemap.resume")) return 0;
64+
65+
commands.executeResumeCommand(new ForgeCommandSource(c.getSource()));
66+
return 1;
67+
});
68+
69+
Command<CommandSource> renderCommand = c -> {
70+
if (!checkPermission(c, "bluemap.render")) return 0;
71+
72+
String worldName = null;
73+
try {
74+
c.getArgument("world", String.class);
75+
} catch (IllegalArgumentException ex) {}
76+
77+
int blockRadius = -1;
78+
try {
79+
c.getArgument("block-radius", Integer.class);
80+
} catch (IllegalArgumentException ex) {}
81+
82+
PlayerEntity player = null;
83+
try {
84+
player = c.getSource().asPlayer();
85+
} catch (CommandSyntaxException ex) {}
86+
87+
if (player == null) {
88+
if (worldName == null) throw new SimpleCommandExceptionType(new LiteralMessage("There is no world with this name: " + worldName)).create();
89+
} else {
90+
91+
}
92+
93+
return 1;
94+
};
95+
96+
base.then(literal("render")).executes(renderCommand);
97+
base.then(literal("render")).then(argument("world", StringArgumentType.word())).executes(renderCommand);
98+
base.then(literal("render")).then(argument("block-radius", IntegerArgumentType.integer(0))).executes(renderCommand);
99+
base.then(literal("render")).then(argument("world", StringArgumentType.word())).then(argument("block-radius", IntegerArgumentType.integer(0))).executes(renderCommand);
100+
101+
dispatcher.register(base);
102+
}
103+
104+
private boolean checkPermission(CommandContext<CommandSource> command, String permission) {
105+
ForgeCommandSource cs = new ForgeCommandSource(command.getSource());
106+
107+
boolean hasPermission = false;
108+
try {
109+
if (PermissionAPI.hasPermission(command.getSource().asPlayer(), permission)) {
110+
hasPermission = true;
111+
}
112+
} catch (CommandSyntaxException ex) {
113+
if (command.getSource().hasPermissionLevel(2)) {
114+
hasPermission = true;
115+
}
116+
}
117+
118+
if (!hasPermission) {
119+
cs.sendMessage(Text.of(TextColor.RED, "You don't have the permissions to use this command!"));
120+
}
121+
122+
return hasPermission;
123+
}
124+
125+
public static LiteralArgumentBuilder<CommandSource> literal(String name){
126+
return LiteralArgumentBuilder.<CommandSource>literal(name);
127+
}
128+
129+
public static <S extends CommandSource, T> RequiredArgumentBuilder<S, T> argument(String name, ArgumentType<T> type){
130+
return RequiredArgumentBuilder.<S, T>argument(name, type);
131+
}
132+
133+
}

BlueMapForge/src/main/java/de/bluecolored/bluemap/forge/ForgeMod.java

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
import de.bluecolored.bluemap.common.plugin.serverinterface.ServerEventListener;
1313
import de.bluecolored.bluemap.common.plugin.serverinterface.ServerInterface;
1414
import de.bluecolored.bluemap.core.logger.Logger;
15-
import net.minecraft.server.MinecraftServer;
16-
import net.minecraft.world.World;
1715
import net.minecraft.world.server.ServerWorld;
1816
import net.minecraftforge.common.MinecraftForge;
1917
import net.minecraftforge.eventbus.api.SubscribeEvent;
@@ -26,31 +24,40 @@ public class ForgeMod implements ServerInterface {
2624

2725
private Plugin bluemap;
2826

29-
private MinecraftServer server;
3027
private Map<String, UUID> worldUUIDs;
3128

29+
private ForgeCommands commands;
30+
3231
public ForgeMod() {
33-
Logger.global = new Log4jLogger(LogManager.getLogger());
32+
Logger.global = new Log4jLogger(LogManager.getLogger(Plugin.PLUGIN_NAME));
3433

3534
this.bluemap = new Plugin("forge", this);
35+
this.commands = new ForgeCommands(this, bluemap);
3636
this.worldUUIDs = new HashMap<>();
3737

3838
MinecraftForge.EVENT_BUS.register(this);
3939
}
4040

4141
@SubscribeEvent
4242
public void onServerStarting(FMLServerStartingEvent event) {
43-
this.server = event.getServer();
4443
this.worldUUIDs.clear();
4544

4645
for (ServerWorld world : event.getServer().getWorlds()) {
46+
try {
47+
registerWorld(world);
48+
} catch (IOException e) {
49+
Logger.global.logError("Failed to register world: " + world.getProviderName(), e);
50+
}
51+
4752
try {
4853
world.save(null, false, false);
4954
} catch (Throwable t) {
5055
Logger.global.logError("Failed to save world: " + world.getProviderName(), t);
5156
}
5257
}
5358

59+
this.commands.registerCommands(event.getCommandDispatcher());
60+
5461
new Thread(() -> {
5562
try {
5663
Logger.global.logInfo("Loading...");
@@ -62,6 +69,10 @@ public void onServerStarting(FMLServerStartingEvent event) {
6269
}).start();
6370
}
6471

72+
private void registerWorld(ServerWorld world) throws IOException {
73+
getUUIDForWorld(world);
74+
}
75+
6576
@SubscribeEvent
6677
public void onServerStopping(FMLServerStoppingEvent event) {
6778
Logger.global.logInfo("Stopping...");
@@ -83,20 +94,21 @@ public void unregisterAllListeners() {
8394

8495
@Override
8596
public UUID getUUIDForWorld(File worldFolder) throws IOException {
86-
87-
88-
worldFolder = worldFolder.getCanonicalFile();
89-
90-
for (ServerWorld world : server.getWorlds()) {
91-
if (worldFolder.equals(world.getSaveHandler().getWorldDirectory().getCanonicalFile())) return getUUIDForWorld(world);
97+
synchronized (worldUUIDs) {
98+
String key = worldFolder.getCanonicalPath();
99+
100+
UUID uuid = worldUUIDs.get(key);
101+
if (uuid == null) {
102+
throw new IOException("There is no world with this folder loaded: " + worldFolder.getPath());
103+
}
104+
105+
return uuid;
92106
}
93-
94-
throw new IOException("There is no world with this folder loaded: " + worldFolder.getPath());
95107
}
96108

97-
public UUID getUUIDForWorld(World world) {
109+
public UUID getUUIDForWorld(ServerWorld world) throws IOException {
98110
synchronized (worldUUIDs) {
99-
String key = world.getWorldInfo().getWorldName();
111+
String key = getFolderForWorld(world).getPath();
100112

101113
UUID uuid = worldUUIDs.get(key);
102114
if (uuid == null) {
@@ -107,11 +119,21 @@ public UUID getUUIDForWorld(World world) {
107119
return uuid;
108120
}
109121
}
122+
123+
private File getFolderForWorld(ServerWorld world) throws IOException {
124+
File worldFolder = world.getSaveHandler().getWorldDirectory();
125+
126+
int dimensionId = world.getDimension().getType().getId();
127+
if (dimensionId != 0) {
128+
worldFolder = new File(worldFolder, "DIM" + dimensionId);
129+
}
130+
131+
return worldFolder.getCanonicalFile();
132+
}
110133

111134
@Override
112135
public File getConfigFolder() {
113-
//TODO
114-
return new File(server.getDataDirectory(), "config");
136+
return new File("config/bluemap");
115137
}
116138

117139
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
modLoader="javafml"
2+
loaderVersion="[28,)"
3+
issueTrackerURL="https://github.com/BlueMap-Minecraft/BlueMap/issues"
4+
[[mods]]
5+
modId="bluemap"
6+
version="${version}"
7+
displayName="BlueMap"
8+
displayURL="https://github.com/BlueMap-Minecraft/BlueMap"
9+
authors="Blue (TBlueF, Lukas Rieger)"
10+
description='''
11+
A 3d-map of your Minecraft worlds view-able in your browser using three.js (WebGL)
12+
'''
13+
14+
[[dependencies.bluemap]]
15+
modId="forge"
16+
mandatory=true
17+
versionRange="[28,)"
18+
ordering="NONE"
19+
side="SERVER"
20+
[[dependencies.bluemap]]
21+
modId="minecraft"
22+
mandatory=true
23+
versionRange="[1.15.2]"
24+
ordering="NONE"
25+
side="SERVER"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
accept-download: false
2+
metrics: true
3+
renderThreadCount: -2
4+
data: "bluemap"
5+
webroot: "bluemap/web"
6+
useCookies: true
7+
webserver {
8+
enabled: true
9+
port: 8100
10+
maxConnectionCount: 100
11+
}

0 commit comments

Comments
 (0)