PrettyPipes/src/main/java/de/ellpeck/prettypipes/misc/Events.java

57 lines
2.5 KiB
Java
Raw Normal View History

2020-04-16 04:42:42 +02:00
package de.ellpeck.prettypipes.misc;
2020-04-14 04:21:28 +02:00
import de.ellpeck.prettypipes.PrettyPipes;
import de.ellpeck.prettypipes.network.PipeNetwork;
import net.minecraft.commands.Commands;
2022-06-27 13:57:06 +02:00
import net.minecraft.network.chat.Component;
2021-12-02 12:31:04 +01:00
import net.minecraft.resources.ResourceLocation;
2021-12-02 16:55:04 +01:00
import net.minecraft.world.level.Level;
2020-04-14 04:21:28 +02:00
import net.minecraftforge.event.AttachCapabilitiesEvent;
import net.minecraftforge.event.server.ServerStartingEvent;
2020-04-14 04:21:28 +02:00
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
2020-04-14 04:21:28 +02:00
@Mod.EventBusSubscriber
public final class Events {
@SubscribeEvent
2021-12-02 16:55:04 +01:00
public static void onWorldCaps(AttachCapabilitiesEvent<Level> event) {
2020-04-14 04:21:28 +02:00
event.addCapability(new ResourceLocation(PrettyPipes.ID, "network"), new PipeNetwork(event.getObject()));
}
@SubscribeEvent
public static void onServerStarting(ServerStartingEvent event) {
event.getServer().getCommands().getDispatcher().register(Commands.literal(PrettyPipes.ID).requires(s -> s.hasPermission(2))
.then(Commands.literal("dump").executes(c -> {
var source = c.getSource();
var file = Paths.get('_' + PrettyPipes.ID + "dump.txt");
var dump = PipeNetwork.get(source.getLevel()).toString();
try {
Files.writeString(file, dump, StandardCharsets.UTF_8);
2022-06-27 13:57:06 +02:00
source.sendSuccess(Component.literal("Wrote network dump to file " + file.toAbsolutePath()), true);
} catch (IOException e) {
2022-06-27 13:57:06 +02:00
source.sendFailure(Component.literal("Failed to write network dump to file " + file.toAbsolutePath()));
return -1;
}
return 0;
}))
.then(Commands.literal("uncache").executes(c -> {
var source = c.getSource();
PipeNetwork.get(source.getLevel()).clearCaches();
2022-06-27 13:57:06 +02:00
source.sendSuccess(Component.literal("Cleared all pipe caches in the world"), true);
return 0;
}))
.then(Commands.literal("unlock").executes(c -> {
var source = c.getSource();
PipeNetwork.get(source.getLevel()).unlock();
2022-06-27 13:57:06 +02:00
source.sendSuccess(Component.literal("Resolved all network locks in the world"), true);
return 0;
})));
}
2020-04-14 04:21:28 +02:00
}