Skip to content

Commit

Permalink
Game rule command
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedro270707 committed Jan 16, 2024
1 parent d91b8a8 commit 19f35f4
Show file tree
Hide file tree
Showing 17 changed files with 138 additions and 89 deletions.
13 changes: 12 additions & 1 deletion src/main/java/net/pedroricardo/commander/Commander.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import turniplabs.halplibe.helper.NetworkHelper;
import turniplabs.halplibe.util.GameStartEntrypoint;

import java.util.ArrayList;
import java.util.List;

public class Commander implements ModInitializer, DedicatedServerModInitializer {
public class Commander implements ModInitializer, DedicatedServerModInitializer, GameStartEntrypoint {
public static final String MOD_ID = "commander";
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);

Expand Down Expand Up @@ -43,4 +44,14 @@ public void onInitialize() {
public void onInitializeServer() {
LOGGER.info("Commander is installed on the server. Packets with suggestions will be sent to clients with Commander.");
}

@Override
public void beforeGameStart() {

}

@Override
public void afterGameStart() {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public void init() {
HelpCommand.register(DISPATCHER);
ChunkCommand.register(DISPATCHER);
GiveCommand.register(DISPATCHER);
GameRuleCommand.register(DISPATCHER);

if (this.isServer) {
StopCommand.register(DISPATCHER);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package net.pedroricardo.commander.content.commands;

import com.mojang.brigadier.Command;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.BoolArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import com.mojang.brigadier.tree.CommandNode;
import net.minecraft.core.data.gamerule.GameRule;
import net.minecraft.core.data.gamerule.GameRuleBoolean;
import net.minecraft.core.data.registry.Registries;
import net.pedroricardo.commander.content.CommanderCommandSource;
import net.pedroricardo.commander.content.exceptions.CommanderExceptions;

@SuppressWarnings("unchecked")
public class GameRuleCommand {
public static void register(CommandDispatcher<CommanderCommandSource> dispatcher) {
final LiteralArgumentBuilder<CommanderCommandSource> argumentBuilder = (LiteralArgumentBuilder) LiteralArgumentBuilder.literal("gamerule").requires(c -> ((CommanderCommandSource)c).hasAdmin());
for (final GameRule<?> gameRule : Registries.GAME_RULES) {
RequiredArgumentBuilder<CommanderCommandSource, ?> gameRuleValueArgument;
if (gameRule instanceof GameRuleBoolean) {
gameRuleValueArgument = RequiredArgumentBuilder.<CommanderCommandSource, Boolean>argument("value", BoolArgumentType.bool())
.executes(c -> {
c.getSource().getWorld().getLevelData().getGameRules().setValue((GameRule<? super Object>) gameRule, BoolArgumentType.getBool(c, "value"));
c.getSource().sendTranslatableMessage("commands.commander.gamerule.set", gameRule.getKey(), BoolArgumentType.getBool(c, "value"));
return Command.SINGLE_SUCCESS;
});
} else {
gameRuleValueArgument = RequiredArgumentBuilder.<CommanderCommandSource, String>argument("value", StringArgumentType.greedyString())
.executes(c -> {
Object o = gameRule.parseFromString(StringArgumentType.getString(c, "value"));
if (o == null) throw CommanderExceptions.invalidGameRuleValue().create();
c.getSource().getWorld().getLevelData().getGameRules().setValue((GameRule<? super Object>) gameRule, o);
c.getSource().sendTranslatableMessage("commands.commander.gamerule.set", gameRule.getKey(), o);
return Command.SINGLE_SUCCESS;
});
}
argumentBuilder.then(LiteralArgumentBuilder.<CommanderCommandSource>literal(gameRule.getKey())
.executes(c -> {
c.getSource().sendTranslatableMessage("commands.commander.gamerule.get", gameRule.getKey(), ((CommanderCommandSource)c).getWorld().getGameRule(gameRule));
return Command.SINGLE_SUCCESS;
})
.then(gameRuleValueArgument));
}
CommandNode commandNode = dispatcher.register(argumentBuilder);
dispatcher.register((LiteralArgumentBuilder) LiteralArgumentBuilder.literal("gr")
.requires(source -> ((CommanderCommandSource)source).hasAdmin())
.redirect(commandNode));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class CommanderExceptions {
private static final SimpleCommandExceptionType PLAYER_ONLY = new SimpleCommandExceptionType(() -> I18n.getInstance().translateKey("argument_types.commander.entity.invalid_selector.player_only"));
private static final SimpleCommandExceptionType SINGLE_PLAYER_WORLD_ONLY = new SimpleCommandExceptionType(() -> I18n.getInstance().translateKey("exceptions.commander.single_player_world_only"));
private static final SimpleCommandExceptionType MULTIPLAYER_WORLD_ONLY = new SimpleCommandExceptionType(() -> I18n.getInstance().translateKey("exceptions.commander.multiplayer_world_only"));
private static final SimpleCommandExceptionType INVALID_GAME_RULE_VALUE = new SimpleCommandExceptionType(() -> I18n.getInstance().translateKey("exceptions.commander.invalid_game_rule_value"));

public static SimpleCommandExceptionType incomplete() {
return INCOMPLETE_ARGUMENT;
Expand Down Expand Up @@ -49,4 +50,8 @@ public static SimpleCommandExceptionType singlePlayerWorldOnly() {
public static SimpleCommandExceptionType multiplayerWorldOnly() {
return MULTIPLAYER_WORLD_ONLY;
}

public static SimpleCommandExceptionType invalidGameRuleValue() {
return INVALID_GAME_RULE_VALUE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

import net.pedroricardo.commander.content.CommanderCommandManager;

public interface EnvironmentWithManager {
public interface ClassWithManager {
CommanderCommandManager getManager();
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import net.pedroricardo.commander.content.CommanderCommandManager;
import net.pedroricardo.commander.content.CommanderCommandSource;
import net.pedroricardo.commander.content.RequestCommandManagerPacket;
import net.pedroricardo.commander.duck.EnvironmentWithManager;
import net.pedroricardo.commander.duck.ClassWithManager;
import org.jetbrains.annotations.Nullable;
import org.lwjgl.input.Keyboard;
import turniplabs.halplibe.helper.ModVersionHelper;
Expand Down Expand Up @@ -67,7 +67,7 @@ public GuiChatSuggestions(Minecraft mc, TextFieldEditor textFieldEditor, GuiChat
}

public CommanderCommandManager getManager() {
return ((EnvironmentWithManager)this.mc).getManager();
return this.mc.theWorld == null ? new CommanderCommandManager(false) : ((ClassWithManager)this.mc.theWorld).getManager();
}

public void drawScreen() {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ private void drawSuggestionPreview(int x, int y, float renderPartialTicks, Callb
int mouseY = GuiHelper.getScaledMouseY(((GuiScreenAccessor)((GuiChat)(Object)this)).mc()) - 1;

if (this.shouldDrawSuggestionPreview()) {
Suggestion suggestionToRender;
Suggestion suggestionToRender = null;
if (this.commander$suggestionsGui.isHoveringOverSuggestions(mouseX, mouseY)) {
suggestionToRender = this.commander$suggestionsGui.getSuggestions().get(this.commander$suggestionsGui.getIndexOfSuggestionBeingHoveredOver(mouseX, mouseY).get());
} else {
} else if (!this.commander$suggestionsGui.getSuggestions().isEmpty()) {
suggestionToRender = this.commander$suggestionsGui.getSuggestions().get(0);
}

if (suggestionToRender.getText().startsWith(((TextFieldEditorAccessor)((GuiChat)(Object)this)).editor().getText().substring(Math.min(suggestionToRender.getRange().getStart(), ((TextFieldEditorAccessor)((GuiChat)(Object)this)).editor().getText().length())))) {
if (suggestionToRender != null && suggestionToRender.getText().startsWith(((TextFieldEditorAccessor)((GuiChat)(Object)this)).editor().getText().substring(Math.min(suggestionToRender.getRange().getStart(), ((TextFieldEditorAccessor)((GuiChat)(Object)this)).editor().getText().length())))) {
int leftMargin = 17 + ((GuiScreenAccessor) ((GuiChat) (Object) this)).fontRenderer().getStringWidth(this.commander$suggestionsGui.getMessage().substring(0, Math.min(suggestionToRender.getRange().getStart(), this.commander$suggestionsGui.getMessage().length())));
((GuiScreenAccessor) ((GuiChat) (Object) this)).fontRenderer().drawStringWithShadow(TextFormatting.LIGHT_GRAY + suggestionToRender.getText(), leftMargin + 1, ((GuiChat) (Object) this).height - 12, 0xE0E0E0);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
package net.pedroricardo.commander.mixin;

import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.player.EntityPlayerSP;
import net.minecraft.core.net.ServerCommand;
import net.minecraft.core.net.command.TextFormatting;
import net.minecraft.server.MinecraftServer;
import net.pedroricardo.commander.content.CommanderClientCommandSource;
import net.pedroricardo.commander.content.CommanderConsoleCommandSource;
import net.pedroricardo.commander.duck.EnvironmentWithManager;
import net.pedroricardo.commander.duck.ClassWithManager;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
Expand All @@ -24,7 +19,7 @@ private void commandLineParser(CallbackInfo ci, ServerCommand serverCommand) {
String command = serverCommand.command;
CommanderConsoleCommandSource source = new CommanderConsoleCommandSource((MinecraftServer)(Object)this);
try {
((EnvironmentWithManager)((MinecraftServer)(Object)this)).getManager().execute(command, source);
((ClassWithManager)((MinecraftServer)(Object)this).getDimensionWorld(0)).getManager().execute(command, source);
} catch (CommandSyntaxException e) {
source.sendMessage(e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.entity.player.EntityPlayerMP;
import net.minecraft.server.net.handler.NetServerHandler;
import net.pedroricardo.commander.content.CommanderCommandManager;
import net.pedroricardo.commander.content.CommanderServerCommandSource;
import net.pedroricardo.commander.duck.EnvironmentWithManager;
import net.pedroricardo.commander.duck.ClassWithManager;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;
import org.spongepowered.asm.mixin.injection.At;
Expand All @@ -31,7 +30,7 @@ private interface NetServerHandlerAccessor {
private void handleSlashCommand(String s, CallbackInfo ci) {
CommanderServerCommandSource serverCommandSource = new CommanderServerCommandSource(((NetServerHandlerAccessor)((NetServerHandler)(Object)this)).mcServer(), ((NetServerHandlerAccessor)((NetServerHandler)(Object)this)).playerEntity());
try {
((EnvironmentWithManager)(((NetServerHandlerAccessor)((NetServerHandler)(Object)this)).mcServer())).getManager().execute(s.substring(1), serverCommandSource);
((ClassWithManager)((NetServerHandlerAccessor)((NetServerHandler)(Object)this)).playerEntity().world).getManager().execute(s.substring(1), serverCommandSource);
} catch (CommandSyntaxException e) {
((NetServerHandlerAccessor)((NetServerHandler)(Object)this)).playerEntity().playerNetServerHandler.sendPacket(new Packet3Chat(TextFormatting.RED + e.getMessage(), AES.keyChain.get(((NetServerHandlerAccessor)((NetServerHandler)(Object)this)).playerEntity().username)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
import net.minecraft.client.entity.player.EntityPlayerSP;
import net.minecraft.core.net.command.TextFormatting;
import net.pedroricardo.commander.content.CommanderClientCommandSource;
import net.pedroricardo.commander.content.CommanderCommandManager;
import net.pedroricardo.commander.duck.EnvironmentWithManager;
import net.pedroricardo.commander.duck.ClassWithManager;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;
import org.spongepowered.asm.mixin.injection.At;
Expand All @@ -27,7 +26,7 @@ private void sendChatMessage(String s, CallbackInfo ci) {
String command = s.substring(1);
CommanderClientCommandSource clientCommandSource = new CommanderClientCommandSource(((EntityPlayerSPAccessor)((EntityPlayerSP)(Object)this)).mc());
try {
((EnvironmentWithManager)((EntityPlayerSPAccessor)((EntityPlayerSP)(Object)this)).mc()).getManager().execute(command, clientCommandSource);
((ClassWithManager)((EntityPlayerSP)(Object)this).world).getManager().execute(command, clientCommandSource);
} catch (CommandSyntaxException e) {
((EntityPlayerSPAccessor)((EntityPlayerSP)(Object)this)).mc().thePlayer.sender.sendMessage(TextFormatting.RED + e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.core.entity.player.EntityPlayer;
import net.minecraft.core.net.handler.NetHandler;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.entity.player.EntityPlayerMP;
import net.minecraft.server.net.handler.NetServerHandler;
import net.pedroricardo.commander.Commander;
import net.pedroricardo.commander.content.CommandManagerPacket;
import net.pedroricardo.commander.content.CommanderServerCommandSource;
import net.pedroricardo.commander.content.RequestCommandManagerPacket;
import net.pedroricardo.commander.duck.EnvironmentWithManager;
import net.pedroricardo.commander.duck.ClassWithManager;
import net.pedroricardo.commander.duck.RequestCommandManagerPacketHandler;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
Expand All @@ -24,6 +22,6 @@ public abstract class ReceiveRequestCommandManagerPacketMixin extends NetHandler
@Override
public void commander$handleRequestCommandManagerPacket(RequestCommandManagerPacket packet) {
EntityPlayerMP player = this.mcServer.playerList.getPlayerEntity(packet.username);
player.playerNetServerHandler.sendPacket(new CommandManagerPacket(((EnvironmentWithManager)this.mcServer).getManager().getDispatcher(), new CommanderServerCommandSource(this.mcServer, player), packet.text, packet.cursor));
player.playerNetServerHandler.sendPacket(new CommandManagerPacket(((ClassWithManager)this.mcServer.getDimensionWorld(player.dimension)).getManager().getDispatcher(), new CommanderServerCommandSource(this.mcServer, player), packet.text, packet.cursor));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.core.net.packet.Packet1Login;
import net.minecraft.core.world.chunk.ChunkCoordinates;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.entity.player.EntityPlayerMP;
import net.minecraft.server.net.handler.NetLoginHandler;
import net.minecraft.server.net.handler.NetServerHandler;
import net.minecraft.server.world.WorldServer;
import net.pedroricardo.commander.content.CommandManagerPacket;
import net.pedroricardo.commander.content.CommanderServerCommandSource;
import net.pedroricardo.commander.duck.EnvironmentWithManager;
import net.pedroricardo.commander.duck.ClassWithManager;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
Expand All @@ -26,6 +23,6 @@ public class SendCommandManagerPacketMixin {

@Inject(method = "doLogin", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/net/handler/NetServerHandler;sendPacket(Lnet/minecraft/core/net/packet/Packet;)V", ordinal = 7), locals = LocalCapture.CAPTURE_FAILSOFT)
private void commander$sendCommandManagerPacket(Packet1Login packet1login, CallbackInfo ci, EntityPlayerMP player) {
player.playerNetServerHandler.sendPacket(new CommandManagerPacket(((EnvironmentWithManager)this.mcServer).getManager().getDispatcher(), new CommanderServerCommandSource(this.mcServer, player), "", 0));
player.playerNetServerHandler.sendPacket(new CommandManagerPacket(((ClassWithManager)this.mcServer.getDimensionWorld(player.dimension)).getManager().getDispatcher(), new CommanderServerCommandSource(this.mcServer, player), "", 0));
}
}

This file was deleted.

Loading

0 comments on commit 19f35f4

Please sign in to comment.