From 59303fea2f8116095ceca51d1c574e7b2392817b Mon Sep 17 00:00:00 2001 From: grimmauld Date: Thu, 11 Mar 2021 18:39:12 +0100 Subject: [PATCH] Extend /create ponder command functionality --- .../foundation/command/PonderCommand.java | 30 ++++++++++++- .../foundation/networking/AllPackets.java | 1 + .../foundation/networking/PonderPacket.java | 45 +++++++++++++++++++ 3 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/simibubi/create/foundation/networking/PonderPacket.java diff --git a/src/main/java/com/simibubi/create/foundation/command/PonderCommand.java b/src/main/java/com/simibubi/create/foundation/command/PonderCommand.java index b69b5664b..ddc439925 100644 --- a/src/main/java/com/simibubi/create/foundation/command/PonderCommand.java +++ b/src/main/java/com/simibubi/create/foundation/command/PonderCommand.java @@ -1,13 +1,26 @@ package com.simibubi.create.foundation.command; +import com.google.common.collect.ImmutableList; import com.mojang.brigadier.builder.ArgumentBuilder; +import com.mojang.brigadier.suggestion.SuggestionProvider; import com.simibubi.create.foundation.networking.AllPackets; +import com.simibubi.create.foundation.networking.PonderPacket; +import com.simibubi.create.foundation.ponder.PonderRegistry; import net.minecraft.command.CommandSource; import net.minecraft.command.Commands; +import net.minecraft.command.ISuggestionProvider; +import net.minecraft.command.arguments.EntityArgument; +import net.minecraft.command.arguments.ResourceLocationArgument; +import net.minecraft.command.arguments.SuggestionProviders; import net.minecraft.entity.player.ServerPlayerEntity; +import net.minecraft.util.ResourceLocation; +import net.minecraftforge.common.util.FakePlayer; import net.minecraftforge.fml.network.PacketDistributor; +import java.util.Collection; + public class PonderCommand { + public static final SuggestionProvider ALL_PONDERS = SuggestionProviders.register(new ResourceLocation("all_ponders"), (iSuggestionProviderCommandContext, builder) -> ISuggestionProvider.func_212476_a(PonderRegistry.all.keySet().stream(), builder)); static ArgumentBuilder register() { return Commands.literal("ponder") @@ -20,6 +33,21 @@ public class PonderCommand { new ConfigureConfigPacket(ConfigureConfigPacket.Actions.ponderIndex.name(), "")); return 1; - }); + }) + .then(Commands.argument("scene", ResourceLocationArgument.resourceLocation()).suggests(ALL_PONDERS) + .executes(context -> openScene(ResourceLocationArgument.getResourceLocation(context, "scene"), ImmutableList.of(context.getSource().asPlayer()))) + .then(Commands.argument("targets", EntityArgument.players()) + .requires(cs -> cs.hasPermissionLevel(2)) + .executes(context -> openScene(ResourceLocationArgument.getResourceLocation(context, "scene"), EntityArgument.getPlayers(context, "targets"))))); + + } + + private static int openScene(ResourceLocation scene, Collection players) { + for (ServerPlayerEntity player : players) { + if (player instanceof FakePlayer) + continue; + AllPackets.channel.send(PacketDistributor.PLAYER.with(() -> player), new PonderPacket(scene)); + } + return 1; } } diff --git a/src/main/java/com/simibubi/create/foundation/networking/AllPackets.java b/src/main/java/com/simibubi/create/foundation/networking/AllPackets.java index 044e806d5..4ead9e86e 100644 --- a/src/main/java/com/simibubi/create/foundation/networking/AllPackets.java +++ b/src/main/java/com/simibubi/create/foundation/networking/AllPackets.java @@ -91,6 +91,7 @@ public enum AllPackets { BLOCK_HIGHLIGHT(HighlightPacket.class, HighlightPacket::new, PLAY_TO_CLIENT), TUNNEL_FLAP(TunnelFlapPacket.class, TunnelFlapPacket::new, PLAY_TO_CLIENT), FUNNEL_FLAP(FunnelFlapPacket.class, FunnelFlapPacket::new, PLAY_TO_CLIENT), + OPEN_PONDER(PonderPacket.class, PonderPacket::new, PLAY_TO_CLIENT) ; diff --git a/src/main/java/com/simibubi/create/foundation/networking/PonderPacket.java b/src/main/java/com/simibubi/create/foundation/networking/PonderPacket.java new file mode 100644 index 000000000..c9de06967 --- /dev/null +++ b/src/main/java/com/simibubi/create/foundation/networking/PonderPacket.java @@ -0,0 +1,45 @@ +package com.simibubi.create.foundation.networking; + +import com.simibubi.create.Create; +import com.simibubi.create.foundation.gui.ScreenOpener; +import com.simibubi.create.foundation.ponder.PonderRegistry; +import com.simibubi.create.foundation.ponder.PonderUI; +import mcp.MethodsReturnNonnullByDefault; +import net.minecraft.network.PacketBuffer; +import net.minecraft.util.ResourceLocation; +import net.minecraftforge.fml.network.NetworkDirection; +import net.minecraftforge.fml.network.NetworkEvent; + +import javax.annotation.ParametersAreNonnullByDefault; +import java.util.function.Supplier; + +@ParametersAreNonnullByDefault +@MethodsReturnNonnullByDefault +public class PonderPacket extends SimplePacketBase { + private final ResourceLocation scene; + + public PonderPacket(ResourceLocation scene) { + this.scene = scene; + } + + public PonderPacket(PacketBuffer buffer) { + this.scene = buffer.readResourceLocation(); + } + + @Override + public void write(PacketBuffer buffer) { + buffer.writeResourceLocation(scene); + } + + @Override + public void handle(Supplier context) { + NetworkEvent.Context ctx = context.get(); + if (ctx.getDirection() != NetworkDirection.PLAY_TO_CLIENT) + return; + if (PonderRegistry.all.containsKey(scene)) + ScreenOpener.transitionTo(new PonderUI(PonderRegistry.compile(PonderRegistry.all.get(scene)))); + else + Create.logger.error("Could not find ponder scene: " + scene); + ctx.setPacketHandled(true); + } +}