Skip to content

Commit e9b8675

Browse files
committed
Finish first forge-mod implementation
1 parent 8d9220f commit e9b8675

File tree

7 files changed

+244
-48
lines changed

7 files changed

+244
-48
lines changed

BlueMapBukkit/src/main/java/de/bluecolored/bluemap/bukkit/BukkitCommands.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public boolean execute(CommandSender sender, CommandSource source, String[] args
7373
}
7474
});
7575

76-
commands.add(new Command("bluemap.rendertask.create.world", "render") {
76+
commands.add(new Command("bluemap.render", "render") {
7777
@Override
7878
public boolean execute(CommandSender sender, CommandSource source, String[] args) {
7979
if (sender instanceof Player) {
@@ -113,7 +113,7 @@ public boolean execute(CommandSender sender, CommandSource source, String[] args
113113
}
114114
});
115115

116-
commands.add(new Command("bluemap.rendertask.prioritize", "render", "prioritize") {
116+
commands.add(new Command("bluemap.render", "render", "prioritize") {
117117
@Override
118118
public boolean execute(CommandSender sender, CommandSource source, String[] args) {
119119
if (args.length != 1) return false;
@@ -129,7 +129,7 @@ public boolean execute(CommandSender sender, CommandSource source, String[] args
129129
}
130130
});
131131

132-
commands.add(new Command("bluemap.rendertask.remove", "render", "remove") {
132+
commands.add(new Command("bluemap.render", "render", "remove") {
133133
@Override
134134
public boolean execute(CommandSender sender, CommandSource source, String[] args) {
135135
if (args.length != 1) return false;

BlueMapBukkit/src/main/resources/plugin.yml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ permissions:
2323
bluemap.reload: true
2424
bluemap.pause: true
2525
bluemap.resume: true
26-
bluemap.rendertask.create.world: true
27-
bluemap.rendertask.prioritize: true
28-
bluemap.rendertask.remove: true
26+
bluemap.render: true
2927
bluemap.debug: true
3028
default: op
3129
bluemap.status:
@@ -36,11 +34,7 @@ permissions:
3634
default: op
3735
bluemap.resume:
3836
default: op
39-
bluemap.rendertask.create.world:
40-
default: op
41-
bluemap.rendertask.prioritize:
42-
default: op
43-
bluemap.rendertask.remove:
37+
bluemap.render:
4438
default: op
4539
bluemap.debug:
4640
default: op

BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/plugin/Commands.java

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,58 @@ public boolean executeResumeCommand(CommandSource source) {
155155
}
156156
}
157157

158+
/**
159+
* Command: /bluemap render [world,map]
160+
*/
161+
public boolean executeRenderCommand(CommandSource source, String mapOrWorld) {
162+
return executeRenderCommand(source, mapOrWorld, null, -1);
163+
}
164+
165+
/**
166+
* Command: /bluemap render [world,map] [block-radius]
167+
*/
168+
public boolean executeRenderCommand(CommandSource source, String mapOrWorld, Vector2i center, int blockRadius) {
169+
if (!checkLoaded(source)) return false;
170+
171+
MapType map = null;
172+
World world = null;
173+
for (MapType m : bluemap.getMapTypes()) {
174+
if (mapOrWorld.equalsIgnoreCase(m.getId()) || mapOrWorld.equalsIgnoreCase(m.getName())){
175+
map = m;
176+
world = map.getWorld();
177+
break;
178+
}
179+
}
180+
181+
if (world == null) {
182+
for (World w : bluemap.getWorlds()) {
183+
if (mapOrWorld.equalsIgnoreCase(w.getName())){
184+
world = w;
185+
break;
186+
}
187+
}
188+
}
189+
190+
if (world == null) {
191+
source.sendMessage(Text.of(TextColor.RED, "Could not find a world or map with this name or id ", TextColor.GRAY, "('" + mapOrWorld + "')", TextColor.RED, "! Maybe it is not configured in BlueMap's config?"));
192+
}
193+
194+
world.invalidateChunkCache();
195+
196+
final World worldToRender = world;
197+
final MapType mapToRender = map;
198+
if (mapToRender == null) {
199+
new Thread(() -> {
200+
createWorldRenderTask(source, worldToRender, center, blockRadius);
201+
}).start();
202+
} else {
203+
new Thread(() -> {
204+
createMapRenderTask(source, mapToRender, center, blockRadius);
205+
}).start();
206+
}
207+
208+
return true;
209+
}
158210

159211
/**
160212
* Command: /bluemap render [world]
@@ -172,7 +224,7 @@ public boolean executeRenderWorldCommand(CommandSource source, UUID worldUuid, V
172224
World world = bluemap.getWorld(worldUuid);
173225

174226
if (world == null) {
175-
source.sendMessage(Text.of(TextColor.RED, "This world is not loaded with BlueMap! Maybe it is not configured?"));
227+
source.sendMessage(Text.of(TextColor.RED, "This world is not loaded with BlueMap! Maybe it is not configured in BlueMap's config?"));
176228
return false;
177229
}
178230

@@ -307,6 +359,36 @@ private void createWorldRenderTask(CommandSource source, World world, Vector2i c
307359
source.sendMessage(Text.of(TextColor.GREEN, "All render tasks created! Use /bluemap to view the progress!"));
308360
}
309361

362+
private void createMapRenderTask(CommandSource source, MapType map, Vector2i center, long blockRadius) {
363+
source.sendMessage(Text.of(TextColor.GOLD, "Collecting chunks to render..."));
364+
365+
String taskName = "world-render";
366+
367+
Predicate<Vector2i> filter;
368+
if (center == null || blockRadius < 0) {
369+
filter = c -> true;
370+
} else {
371+
filter = c -> c.mul(16).distanceSquared(center) <= blockRadius * blockRadius;
372+
taskName = "radius-render";
373+
}
374+
375+
Collection<Vector2i> chunks = map.getWorld().getChunkList(filter);
376+
377+
source.sendMessage(Text.of(TextColor.GREEN, chunks.size() + " chunks found!"));
378+
source.sendMessage(Text.of(TextColor.GOLD, "Collecting tiles for map '" + map.getId() + "'"));
379+
380+
HiresModelManager hmm = map.getTileRenderer().getHiresModelManager();
381+
Collection<Vector2i> tiles = hmm.getTilesForChunks(chunks);
382+
383+
RenderTask task = new RenderTask(taskName, map);
384+
task.addTiles(tiles);
385+
task.optimizeQueue();
386+
bluemap.getRenderManager().addRenderTask(task);
387+
388+
source.sendMessage(Text.of(TextColor.GREEN, tiles.size() + " tiles found! Task created."));
389+
source.sendMessage(Text.of(TextColor.GREEN, "All render tasks created! Use /bluemap to view the progress!"));
390+
}
391+
310392
private boolean checkLoaded(CommandSource source) {
311393
if (!bluemap.isLoaded()) {
312394
source.sendMessage(Text.of(TextColor.RED, "BlueMap is not loaded!", TextColor.GRAY, "(Try /bluemap reload)"));

BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/plugin/Plugin.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,10 @@ public World getWorld(UUID uuid){
361361
return worlds.get(uuid);
362362
}
363363

364+
public Collection<World> getWorlds(){
365+
return worlds.values();
366+
}
367+
364368
public Collection<MapType> getMapTypes(){
365369
return maps.values();
366370
}

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

Lines changed: 101 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package de.bluecolored.bluemap.forge;
22

3+
import java.io.IOException;
4+
import java.util.UUID;
5+
6+
import com.flowpowered.math.vector.Vector2i;
7+
import com.flowpowered.math.vector.Vector3i;
38
import com.mojang.brigadier.Command;
49
import com.mojang.brigadier.CommandDispatcher;
510
import com.mojang.brigadier.LiteralMessage;
@@ -9,94 +14,163 @@
914
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
1015
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
1116
import com.mojang.brigadier.context.CommandContext;
12-
import com.mojang.brigadier.exceptions.CommandExceptionType;
1317
import com.mojang.brigadier.exceptions.CommandSyntaxException;
14-
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
1518
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
1619

1720
import de.bluecolored.bluemap.common.plugin.Commands;
1821
import de.bluecolored.bluemap.common.plugin.Plugin;
1922
import de.bluecolored.bluemap.common.plugin.text.Text;
2023
import de.bluecolored.bluemap.common.plugin.text.TextColor;
2124
import net.minecraft.command.CommandSource;
25+
import net.minecraft.entity.Entity;
2226
import net.minecraft.entity.player.PlayerEntity;
27+
import net.minecraft.util.math.BlockPos;
28+
import net.minecraft.world.server.ServerWorld;
29+
import net.minecraftforge.server.permission.DefaultPermissionLevel;
2330
import net.minecraftforge.server.permission.PermissionAPI;
2431

2532
public class ForgeCommands {
2633

2734
private ForgeMod mod;
28-
private Plugin bluemap;
2935
private Commands commands;
3036

3137
public ForgeCommands(ForgeMod mod, Plugin bluemap) {
3238
this.mod = mod;
33-
this.bluemap = bluemap;
3439
this.commands = bluemap.getCommands();
3540
}
3641

3742
public void registerCommands(CommandDispatcher<CommandSource> dispatcher) {
38-
3943
LiteralArgumentBuilder<CommandSource> base = literal("bluemap");
40-
44+
45+
PermissionAPI.registerNode("bluemap.status", DefaultPermissionLevel.OP, "Permission for using /bluemap");
4146
base.executes(c -> {
4247
if (!checkPermission(c, "bluemap.status")) return 0;
4348

4449
commands.executeRootCommand(new ForgeCommandSource(c.getSource()));
4550
return 1;
4651
});
47-
48-
base.then(literal("reload")).executes(c -> {
52+
53+
PermissionAPI.registerNode("bluemap.reload", DefaultPermissionLevel.OP, "Permission for using /bluemap reload");
54+
base.then(literal("reload").executes(c -> {
4955
if (!checkPermission(c, "bluemap.reload")) return 0;
5056

5157
commands.executeReloadCommand(new ForgeCommandSource(c.getSource()));
5258
return 1;
53-
});
54-
55-
base.then(literal("pause")).executes(c -> {
59+
}));
60+
61+
PermissionAPI.registerNode("bluemap.pause", DefaultPermissionLevel.OP, "Permission for using /bluemap pause");
62+
base.then(literal("pause").executes(c -> {
5663
if (!checkPermission(c, "bluemap.pause")) return 0;
5764

5865
commands.executePauseCommand(new ForgeCommandSource(c.getSource()));
5966
return 1;
60-
});
61-
62-
base.then(literal("resume")).executes(c -> {
67+
}));
68+
69+
PermissionAPI.registerNode("bluemap.resume", DefaultPermissionLevel.OP, "Permission for using /bluemap resume");
70+
base.then(literal("resume").executes(c -> {
6371
if (!checkPermission(c, "bluemap.resume")) return 0;
6472

6573
commands.executeResumeCommand(new ForgeCommandSource(c.getSource()));
6674
return 1;
67-
});
68-
75+
}));
76+
77+
PermissionAPI.registerNode("bluemap.render", DefaultPermissionLevel.OP, "Permission for using /bluemap render");
6978
Command<CommandSource> renderCommand = c -> {
7079
if (!checkPermission(c, "bluemap.render")) return 0;
7180

7281
String worldName = null;
7382
try {
74-
c.getArgument("world", String.class);
83+
worldName = c.getArgument("world", String.class);
7584
} catch (IllegalArgumentException ex) {}
7685

7786
int blockRadius = -1;
7887
try {
79-
c.getArgument("block-radius", Integer.class);
88+
blockRadius = c.getArgument("block-radius", Integer.class);
8089
} catch (IllegalArgumentException ex) {}
8190

8291
PlayerEntity player = null;
8392
try {
8493
player = c.getSource().asPlayer();
8594
} catch (CommandSyntaxException ex) {}
8695

87-
if (player == null) {
88-
if (worldName == null) throw new SimpleCommandExceptionType(new LiteralMessage("There is no world with this name: " + worldName)).create();
96+
if (player == null && blockRadius != -1) {
97+
throw new SimpleCommandExceptionType(new LiteralMessage("You can only use a block-radius if you are a player!")).create();
98+
}
99+
100+
if (worldName == null) {
101+
if (player == null) throw new SimpleCommandExceptionType(new LiteralMessage("You need to define a world! (/bluemap render <world>)")).create();
102+
Vector2i center = new Vector2i(player.getPosition().getX(), player.getPosition().getZ());
103+
104+
UUID world;
105+
try {
106+
world = mod.getUUIDForWorld((ServerWorld) player.getEntityWorld());
107+
} catch (IOException ex) {
108+
throw new SimpleCommandExceptionType(new LiteralMessage("Could not detect the world you are currently in, try to define a world using /bluemap render <world>")).create();
109+
}
110+
111+
return commands.executeRenderWorldCommand(new ForgeCommandSource(c.getSource()), world, center, blockRadius) ? 1 : 0;
89112
} else {
90-
113+
if (player == null) {
114+
return commands.executeRenderCommand(new ForgeCommandSource(c.getSource()), worldName) ? 1 : 0;
115+
} else {
116+
Vector2i center = new Vector2i(player.getPosition().getX(), player.getPosition().getZ());
117+
return commands.executeRenderCommand(new ForgeCommandSource(c.getSource()), worldName, center, blockRadius) ? 1 : 0;
118+
}
91119
}
92120

93-
return 1;
94121
};
122+
123+
base.then(literal("render")
124+
.executes(renderCommand)
125+
.then(argument("block-radius", IntegerArgumentType.integer(0)).executes(renderCommand))
126+
.then(argument("world", StringArgumentType.word())
127+
.executes(renderCommand)
128+
.then(argument("block-radius", IntegerArgumentType.integer(0))).executes(renderCommand)
129+
)
130+
131+
.then(literal("prioritize").then(argument("task-uuid", StringArgumentType.word()).executes(c -> {
132+
if (!checkPermission(c, "bluemap.render")) return 0;
133+
134+
try {
135+
UUID taskUUID = UUID.fromString(c.getArgument("task-uuid", String.class));
136+
commands.executePrioritizeRenderTaskCommand(new ForgeCommandSource(c.getSource()), taskUUID);
137+
return 1;
138+
} catch (IllegalArgumentException ex) {
139+
throw new SimpleCommandExceptionType(new LiteralMessage("Invalid task-uuid!")).create();
140+
}
141+
})))
142+
143+
.then(literal("remove").then(argument("task-uuid", StringArgumentType.word()).executes(c -> {
144+
if (!checkPermission(c, "bluemap.render")) return 0;
145+
146+
try {
147+
UUID taskUUID = UUID.fromString(c.getArgument("task-uuid", String.class));
148+
commands.executeRemoveRenderTaskCommand(new ForgeCommandSource(c.getSource()), taskUUID);
149+
return 1;
150+
} catch (IllegalArgumentException ex) {
151+
throw new SimpleCommandExceptionType(new LiteralMessage("Invalid task-uuid!")).create();
152+
}
153+
})))
154+
);
95155

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);
156+
PermissionAPI.registerNode("bluemap.debug", DefaultPermissionLevel.OP, "Permission for using /bluemap debug");
157+
base.then(literal("debug").executes(c -> {
158+
if (!checkPermission(c, "bluemap.debug")) return 0;
159+
160+
Entity entity = c.getSource().assertIsEntity();
161+
BlockPos mcPos = entity.getPosition();
162+
Vector3i pos = new Vector3i(mcPos.getX(), mcPos.getY(), mcPos.getZ());
163+
164+
UUID world;
165+
try {
166+
world = mod.getUUIDForWorld((ServerWorld) entity.getEntityWorld());
167+
} catch (IOException e) {
168+
throw new SimpleCommandExceptionType(new LiteralMessage("Could not detect the world you are currently in!")).create();
169+
}
170+
171+
commands.executeDebugCommand(new ForgeCommandSource(c.getSource()), world, pos);
172+
return 1;
173+
}));
100174

101175
dispatcher.register(base);
102176
}
@@ -110,7 +184,7 @@ private boolean checkPermission(CommandContext<CommandSource> command, String pe
110184
hasPermission = true;
111185
}
112186
} catch (CommandSyntaxException ex) {
113-
if (command.getSource().hasPermissionLevel(2)) {
187+
if (command.getSource().hasPermissionLevel(1)) {
114188
hasPermission = true;
115189
}
116190
}

0 commit comments

Comments
 (0)