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

76 lines
3.9 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;
2024-02-03 22:01:43 +01:00
import de.ellpeck.prettypipes.Registry;
2020-04-14 04:21:28 +02:00
import de.ellpeck.prettypipes.network.PipeNetwork;
2024-03-07 16:00:49 +01:00
import de.ellpeck.prettypipes.packets.*;
import net.minecraft.commands.Commands;
2022-06-27 13:57:06 +02:00
import net.minecraft.network.chat.Component;
2024-03-07 16:00:49 +01:00
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.Mod;
2024-02-03 22:01:43 +01:00
import net.neoforged.neoforge.capabilities.Capabilities;
import net.neoforged.neoforge.capabilities.RegisterCapabilitiesEvent;
2024-02-03 15:17:58 +01:00
import net.neoforged.neoforge.event.server.ServerStartingEvent;
2024-03-07 16:00:49 +01:00
import net.neoforged.neoforge.network.event.RegisterPayloadHandlerEvent;
2020-04-14 04:21:28 +02:00
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
2024-02-03 22:01:43 +01:00
public static void onWorldCaps(RegisterCapabilitiesEvent event) {
event.registerBlockEntity(Registry.pipeConnectableCapability, Registry.pipeBlockEntity, (e, d) -> e);
event.registerBlockEntity(Registry.pipeConnectableCapability, Registry.pressurizerBlockEntity, (e, d) -> e);
event.registerBlockEntity(Registry.pipeConnectableCapability, Registry.itemTerminalBlockEntity, (e, d) -> e);
event.registerBlockEntity(Registry.pipeConnectableCapability, Registry.craftingTerminalBlockEntity, (e, d) -> e);
event.registerBlockEntity(Capabilities.EnergyStorage.BLOCK, Registry.pressurizerBlockEntity, (e, d) -> e.storage);
2020-04-14 04:21:28 +02:00
}
@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);
2023-07-07 19:54:52 +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();
2023-07-07 19:54:52 +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();
2023-07-07 19:54:52 +02:00
source.sendSuccess(() -> Component.literal("Resolved all network locks in the world"), true);
return 0;
})));
}
2024-02-03 22:01:43 +01:00
2024-03-07 16:00:49 +01:00
@SubscribeEvent
public static void onPayloadRegister(final RegisterPayloadHandlerEvent event) {
var registrar = event.registrar(PrettyPipes.ID);
registrar.play(PacketItemEnterPipe.ID, PacketItemEnterPipe::new, PacketItemEnterPipe::onMessage);
registrar.play(PacketButton.ID, PacketButton::new, PacketButton::onMessage);
registrar.play(PacketCraftingModuleTransfer.ID, PacketCraftingModuleTransfer::new, PacketCraftingModuleTransfer::onMessage);
registrar.play(PacketGhostSlot.ID, PacketGhostSlot::new, PacketGhostSlot::onMessage);
registrar.play(PacketNetworkItems.ID, PacketNetworkItems::new, PacketNetworkItems::onMessage);
registrar.play(PacketRequest.ID, PacketRequest::new, PacketRequest::onMessage);
}
2020-04-14 04:21:28 +02:00
}