Extend /create ponder command functionality

This commit is contained in:
grimmauld 2021-03-11 18:39:12 +01:00
parent 4445872959
commit 59303fea2f
3 changed files with 75 additions and 1 deletions

View file

@ -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<CommandSource> ALL_PONDERS = SuggestionProviders.register(new ResourceLocation("all_ponders"), (iSuggestionProviderCommandContext, builder) -> ISuggestionProvider.func_212476_a(PonderRegistry.all.keySet().stream(), builder));
static ArgumentBuilder<CommandSource, ?> 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<? extends ServerPlayerEntity> players) {
for (ServerPlayerEntity player : players) {
if (player instanceof FakePlayer)
continue;
AllPackets.channel.send(PacketDistributor.PLAYER.with(() -> player), new PonderPacket(scene));
}
return 1;
}
}

View file

@ -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)
;

View file

@ -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<NetworkEvent.Context> 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);
}
}