|
2 | 2 |
|
3 | 3 | import com.mojang.brigadier.CommandDispatcher; |
4 | 4 | import net.minecraft.commands.CommandSourceStack; |
5 | | -import net.minecraft.network.chat.Component; |
6 | | -import net.minecraft.resources.ResourceLocation; |
7 | | -import net.minecraft.server.level.ServerLevel; |
8 | | -import net.minecraft.server.packs.resources.Resource; |
9 | | -import net.minecraft.server.packs.resources.ResourceManager; |
10 | | -import org.embeddedt.modernfix.ModernFix; |
11 | | -import org.embeddedt.modernfix.structure.CachingStructureManager; |
12 | | - |
13 | | -import java.io.InputStream; |
14 | | -import java.util.Map; |
15 | | -import java.util.regex.Matcher; |
16 | | -import java.util.regex.Pattern; |
17 | | - |
18 | | -import static net.minecraft.commands.Commands.literal; |
19 | 5 |
|
20 | 6 | public class ModernFixCommands { |
21 | 7 | public static void register(CommandDispatcher<CommandSourceStack> dispatcher) { |
22 | | - dispatcher.register(literal("modernfix") |
23 | | - .then(literal("upgradeStructures") |
24 | | - .requires(source -> source.hasPermission(3)) |
25 | | - .executes(context -> { |
26 | | - ServerLevel level = context.getSource().getLevel(); |
27 | | - if(level == null) { |
28 | | - context.getSource().sendFailure(Component.literal("Couldn't find server level")); |
29 | | - return 0; |
30 | | - } |
31 | | - |
32 | | - ResourceManager manager = level.getServer().resources.resourceManager(); |
33 | | - Map<ResourceLocation, Resource> structures = manager.listResources("structures", p -> p.getPath().endsWith(".nbt")); |
34 | | - int upgradedNum = 0; |
35 | | - Pattern pathPattern = Pattern.compile("^structures/(.*)\\.nbt$"); |
36 | | - for(Map.Entry<ResourceLocation, Resource> entry : structures.entrySet()) { |
37 | | - upgradedNum++; |
38 | | - ResourceLocation found = entry.getKey(); |
39 | | - Matcher matcher = pathPattern.matcher(found.getPath()); |
40 | | - if(!matcher.matches()) |
41 | | - continue; |
42 | | - ResourceLocation structureLocation = ResourceLocation.fromNamespaceAndPath(found.getNamespace(), matcher.group(1)); |
43 | | - try(InputStream resource = entry.getValue().open()) { |
44 | | - CachingStructureManager.readStructureTag(structureLocation, level.getServer().getFixerUpper(), resource); |
45 | | - Component msg = Component.literal("checked " + structureLocation + " (" + upgradedNum + "/" + structures.size() + ")"); |
46 | | - context.getSource().sendSuccess(() -> msg, false); |
47 | | - } catch(Throwable e) { |
48 | | - ModernFix.LOGGER.error("Couldn't upgrade structure " + found, e); |
49 | | - context.getSource().sendFailure(Component.literal("error reading " + structureLocation + " (" + upgradedNum + "/" + structures.size() + ")")); |
50 | | - } |
51 | | - } |
52 | | - |
53 | | - context.getSource().sendSuccess(() -> Component.literal("All structures upgraded"), false); |
54 | 8 |
|
55 | | - return 1; |
56 | | - })) |
57 | | - ); |
58 | 9 | } |
59 | 10 | } |
0 commit comments