forked from ChomeNS/chipmunkmod
sync
This commit is contained in:
parent
ae6f3b21ac
commit
9ab781e1c6
9 changed files with 176 additions and 53 deletions
|
@ -1,5 +1,7 @@
|
||||||
package land.chipmunk.chipmunkmod;
|
package land.chipmunk.chipmunkmod;
|
||||||
|
|
||||||
|
import com.google.gson.GsonBuilder;
|
||||||
|
import land.chipmunk.chipmunkmod.util.gson.BlockPosTypeAdapter;
|
||||||
import net.fabricmc.api.ModInitializer;
|
import net.fabricmc.api.ModInitializer;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
|
@ -9,6 +11,8 @@ import java.io.BufferedWriter;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileWriter;
|
import java.io.FileWriter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import net.minecraft.util.math.BlockPos;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
|
@ -40,7 +44,9 @@ public class ChipmunkMod implements ModInitializer {
|
||||||
private static Configuration loadConfig () throws IOException {
|
private static Configuration loadConfig () throws IOException {
|
||||||
CONFIG_DIR.mkdirs();
|
CONFIG_DIR.mkdirs();
|
||||||
|
|
||||||
final Gson gson = new Gson();
|
final Gson gson = new GsonBuilder()
|
||||||
|
.registerTypeAdapter(BlockPos.class, new BlockPosTypeAdapter())
|
||||||
|
.create();
|
||||||
final File file = CONFIG_FILE;
|
final File file = CONFIG_FILE;
|
||||||
|
|
||||||
if (!file.exists()) {
|
if (!file.exists()) {
|
||||||
|
|
|
@ -11,16 +11,36 @@ import net.minecraft.text.Text;
|
||||||
import net.minecraft.text.Texts;
|
import net.minecraft.text.Texts;
|
||||||
import net.minecraft.text.MutableText;
|
import net.minecraft.text.MutableText;
|
||||||
import net.minecraft.util.Formatting;
|
import net.minecraft.util.Formatting;
|
||||||
|
import net.minecraft.command.CommandRegistryAccess;
|
||||||
import net.minecraft.client.MinecraftClient;
|
import net.minecraft.client.MinecraftClient;
|
||||||
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
|
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
|
||||||
import land.chipmunk.chipmunkmod.ChipmunkMod;
|
|
||||||
import land.chipmunk.chipmunkmod.commands.*;
|
import land.chipmunk.chipmunkmod.commands.*;
|
||||||
|
|
||||||
public class CommandManager {
|
public class CommandManager {
|
||||||
public static CommandDispatcher<FabricClientCommandSource> dispatcher = new CommandDispatcher<>();
|
public CommandDispatcher<FabricClientCommandSource> dispatcher = new CommandDispatcher<>();
|
||||||
public static String prefix = ChipmunkMod.CONFIG.commands.prefix;
|
public String prefix;
|
||||||
|
|
||||||
public static void executeCommand (String command) {
|
public static CommandManager INSTANCE;
|
||||||
|
|
||||||
|
public CommandManager (String prefix, CommandRegistryAccess commandRegistryAccess) {
|
||||||
|
this.prefix = prefix;
|
||||||
|
|
||||||
|
TestCommand.register(this.dispatcher);
|
||||||
|
CoreCommand.register(this.dispatcher);
|
||||||
|
UsernameCommand.register(this.dispatcher);
|
||||||
|
CloopCommand.register(this.dispatcher);
|
||||||
|
ValidateCommand.register(this.dispatcher);
|
||||||
|
ItemCommand.register(this.dispatcher, commandRegistryAccess);
|
||||||
|
CustomChatCommand.register(this.dispatcher);
|
||||||
|
EvalCommand.register(this.dispatcher);
|
||||||
|
FullBrightCommand.register(this.dispatcher);
|
||||||
|
MusicCommand.register(this.dispatcher);
|
||||||
|
RainbowNameCommand.register(this.dispatcher);
|
||||||
|
SayCommand.register(this.dispatcher);
|
||||||
|
SelfCareCommand.register(this.dispatcher);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void executeCommand (String command) {
|
||||||
final MinecraftClient client = MinecraftClient.getInstance();
|
final MinecraftClient client = MinecraftClient.getInstance();
|
||||||
|
|
||||||
final FabricClientCommandSource commandSource = (FabricClientCommandSource) client.getNetworkHandler().getCommandSource();
|
final FabricClientCommandSource commandSource = (FabricClientCommandSource) client.getNetworkHandler().getCommandSource();
|
||||||
|
@ -38,7 +58,7 @@ public class CommandManager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Text getContext (CommandSyntaxException exception) {
|
public Text getContext (CommandSyntaxException exception) {
|
||||||
final int _cursor = exception.getCursor();
|
final int _cursor = exception.getCursor();
|
||||||
final String input = exception.getInput();
|
final String input = exception.getInput();
|
||||||
|
|
||||||
|
@ -65,19 +85,4 @@ public class CommandManager {
|
||||||
|
|
||||||
public static LiteralArgumentBuilder<FabricClientCommandSource> literal (String name) { return LiteralArgumentBuilder.literal(name); }
|
public static LiteralArgumentBuilder<FabricClientCommandSource> literal (String name) { return LiteralArgumentBuilder.literal(name); }
|
||||||
public static <T> RequiredArgumentBuilder<FabricClientCommandSource, T> argument (String name, ArgumentType<T> type) { return RequiredArgumentBuilder.argument(name, type); }
|
public static <T> RequiredArgumentBuilder<FabricClientCommandSource, T> argument (String name, ArgumentType<T> type) { return RequiredArgumentBuilder.argument(name, type); }
|
||||||
|
|
||||||
static {
|
|
||||||
TestCommand.register(dispatcher);
|
|
||||||
CoreCommand.register(dispatcher);
|
|
||||||
UsernameCommand.register(dispatcher);
|
|
||||||
ValidateCommand.register(dispatcher);
|
|
||||||
CloopCommand.register(dispatcher);
|
|
||||||
CustomChatCommand.register(dispatcher);
|
|
||||||
SayCommand.register(dispatcher);
|
|
||||||
SelfCareCommand.register(dispatcher);
|
|
||||||
FullBrightCommand.register(dispatcher);
|
|
||||||
MusicCommand.register(dispatcher);
|
|
||||||
RainbowNameCommand.register(dispatcher);
|
|
||||||
EvalCommand.register(dispatcher);
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -0,0 +1,58 @@
|
||||||
|
package land.chipmunk.chipmunkmod.commands;
|
||||||
|
|
||||||
|
import com.mojang.brigadier.Command;
|
||||||
|
import com.mojang.brigadier.CommandDispatcher;
|
||||||
|
import com.mojang.brigadier.context.CommandContext;
|
||||||
|
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||||
|
import static land.chipmunk.chipmunkmod.command.CommandManager.literal;
|
||||||
|
import static land.chipmunk.chipmunkmod.command.CommandManager.argument;
|
||||||
|
import static com.mojang.brigadier.arguments.IntegerArgumentType.integer;
|
||||||
|
import static com.mojang.brigadier.arguments.IntegerArgumentType.getInteger;
|
||||||
|
import static net.minecraft.command.argument.ItemStackArgumentType.itemStack;
|
||||||
|
import static net.minecraft.command.argument.ItemStackArgumentType.getItemStackArgument;
|
||||||
|
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
|
||||||
|
import net.minecraft.command.CommandRegistryAccess;
|
||||||
|
import net.minecraft.client.MinecraftClient;
|
||||||
|
import net.minecraft.network.packet.c2s.play.CreativeInventoryActionC2SPacket;
|
||||||
|
import net.minecraft.item.ItemStack;
|
||||||
|
import net.minecraft.text.Text;
|
||||||
|
|
||||||
|
public class ItemCommand {
|
||||||
|
public static void register (CommandDispatcher<FabricClientCommandSource> dispatcher, CommandRegistryAccess commandRegistryAccess) {
|
||||||
|
dispatcher.register(
|
||||||
|
literal("item")
|
||||||
|
.then(
|
||||||
|
argument("item", itemStack(commandRegistryAccess))
|
||||||
|
.executes(c -> setItem(c, 1))
|
||||||
|
.then(
|
||||||
|
argument("count", integer(1, 64))
|
||||||
|
.executes(c -> setItem(c))
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int setItem (CommandContext<FabricClientCommandSource> context) throws CommandSyntaxException {
|
||||||
|
return setItem(context, getInteger(context, "count"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int setItem (CommandContext<FabricClientCommandSource> context, int count) throws CommandSyntaxException {
|
||||||
|
final FabricClientCommandSource source = context.getSource();
|
||||||
|
final MinecraftClient client = source.getClient();
|
||||||
|
|
||||||
|
final ItemStack stack = getItemStackArgument(context, "item").createStack(count, false);
|
||||||
|
|
||||||
|
int slot = 36 + client.player.getInventory().selectedSlot;
|
||||||
|
|
||||||
|
client.getNetworkHandler().getConnection().send(new CreativeInventoryActionC2SPacket(slot, stack));
|
||||||
|
|
||||||
|
source.sendFeedback(
|
||||||
|
Text.translatable(
|
||||||
|
"Replaced your held item with %s %s",
|
||||||
|
Text.literal(String.valueOf(count)),
|
||||||
|
stack.toHoverableText()
|
||||||
|
)
|
||||||
|
);
|
||||||
|
return Command.SINGLE_SUCCESS;
|
||||||
|
}
|
||||||
|
}
|
|
@ -147,7 +147,10 @@ public class MusicCommand {
|
||||||
|
|
||||||
public int list (CommandContext<FabricClientCommandSource> context, Path path) throws CommandSyntaxException {
|
public int list (CommandContext<FabricClientCommandSource> context, Path path) throws CommandSyntaxException {
|
||||||
final FabricClientCommandSource source = context.getSource();
|
final FabricClientCommandSource source = context.getSource();
|
||||||
final String prefix = CommandManager.prefix;
|
|
||||||
|
final CommandManager commandManager = CommandManager.INSTANCE;
|
||||||
|
|
||||||
|
final String prefix = commandManager.prefix;
|
||||||
|
|
||||||
final File directory = path.toFile();
|
final File directory = path.toFile();
|
||||||
final String[] filenames = directory.list();
|
final String[] filenames = directory.list();
|
||||||
|
|
|
@ -42,15 +42,17 @@ public class ChatInputSuggestorMixin {
|
||||||
public void refresh (CallbackInfo ci) {
|
public void refresh (CallbackInfo ci) {
|
||||||
if (slashOptional) return;
|
if (slashOptional) return;
|
||||||
|
|
||||||
|
final CommandManager commandManager = CommandManager.INSTANCE;
|
||||||
|
|
||||||
final String text = this.textField.getText();
|
final String text = this.textField.getText();
|
||||||
final int cursor = this.textField.getCursor();
|
final int cursor = this.textField.getCursor();
|
||||||
|
|
||||||
if (cursor < CommandManager.prefix.length() || !text.startsWith(CommandManager.prefix)) return;
|
if (cursor < commandManager.prefix.length() || !text.startsWith(commandManager.prefix)) return;
|
||||||
|
|
||||||
final StringReader reader = new StringReader(text);
|
final StringReader reader = new StringReader(text);
|
||||||
reader.setCursor(CommandManager.prefix.length()); // Skip the prefix
|
reader.setCursor(commandManager.prefix.length()); // Skip the prefix
|
||||||
|
|
||||||
final CommandDispatcher<FabricClientCommandSource> dispatcher = CommandManager.dispatcher;
|
final CommandDispatcher<FabricClientCommandSource> dispatcher = commandManager.dispatcher;
|
||||||
final MinecraftClient client = MinecraftClient.getInstance();
|
final MinecraftClient client = MinecraftClient.getInstance();
|
||||||
final FabricClientCommandSource commandSource = (FabricClientCommandSource) client.getNetworkHandler().getCommandSource();
|
final FabricClientCommandSource commandSource = (FabricClientCommandSource) client.getNetworkHandler().getCommandSource();
|
||||||
|
|
||||||
|
|
|
@ -17,8 +17,10 @@ public class ChatScreenMixin {
|
||||||
|
|
||||||
@Inject(at = @At("HEAD"), method = "sendMessage", cancellable = true)
|
@Inject(at = @At("HEAD"), method = "sendMessage", cancellable = true)
|
||||||
public void sendMessage(String chatText, boolean addToHistory, CallbackInfoReturnable<Boolean> cir) {
|
public void sendMessage(String chatText, boolean addToHistory, CallbackInfoReturnable<Boolean> cir) {
|
||||||
if (chatText.startsWith(CommandManager.prefix)) {
|
final CommandManager commandManager = CommandManager.INSTANCE;
|
||||||
CommandManager.executeCommand(chatText.substring(CommandManager.prefix.length()));
|
|
||||||
|
if (chatText.startsWith(commandManager.prefix)) {
|
||||||
|
commandManager.executeCommand(chatText.substring(commandManager.prefix.length()));
|
||||||
|
|
||||||
if (addToHistory) MinecraftClient.getInstance().inGameHud.getChatHud().addToMessageHistory(chatText);
|
if (addToHistory) MinecraftClient.getInstance().inGameHud.getChatHud().addToMessageHistory(chatText);
|
||||||
|
|
||||||
|
|
|
@ -1,23 +1,29 @@
|
||||||
package land.chipmunk.chipmunkmod.mixin;
|
package land.chipmunk.chipmunkmod.mixin;
|
||||||
|
|
||||||
import land.chipmunk.chipmunkmod.modules.*;
|
|
||||||
import net.minecraft.network.packet.s2c.play.GameJoinS2CPacket;
|
|
||||||
import org.spongepowered.asm.mixin.Mixin;
|
import org.spongepowered.asm.mixin.Mixin;
|
||||||
|
import org.spongepowered.asm.mixin.Shadow;
|
||||||
import org.spongepowered.asm.mixin.injection.At;
|
import org.spongepowered.asm.mixin.injection.At;
|
||||||
import org.spongepowered.asm.mixin.injection.Inject;
|
import org.spongepowered.asm.mixin.injection.Inject;
|
||||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||||
|
import net.minecraft.network.packet.s2c.play.GameJoinS2CPacket;
|
||||||
|
import net.minecraft.command.CommandRegistryAccess;
|
||||||
|
import net.minecraft.resource.featuretoggle.FeatureSet;
|
||||||
|
import net.minecraft.registry.CombinedDynamicRegistries;
|
||||||
|
import net.minecraft.client.network.ClientDynamicRegistryType;
|
||||||
|
import land.chipmunk.chipmunkmod.ChipmunkMod;
|
||||||
|
import land.chipmunk.chipmunkmod.command.CommandManager;
|
||||||
|
import land.chipmunk.chipmunkmod.modules.SelfCare;
|
||||||
|
|
||||||
@Mixin(net.minecraft.client.network.ClientPlayNetworkHandler.class)
|
@Mixin(net.minecraft.client.network.ClientPlayNetworkHandler.class)
|
||||||
public class ClientPlayNetworkHandlerMixin {
|
public class ClientPlayNetworkHandlerMixin {
|
||||||
|
@Shadow private FeatureSet enabledFeatures;
|
||||||
|
@Shadow private CombinedDynamicRegistries<ClientDynamicRegistryType> combinedDynamicRegistries;
|
||||||
|
|
||||||
@Inject(method = "onGameJoin", at = @At("TAIL"))
|
@Inject(method = "onGameJoin", at = @At("TAIL"))
|
||||||
private void onGameJoin (GameJoinS2CPacket packet, CallbackInfo ci) {
|
private void onGameJoin (GameJoinS2CPacket packet, CallbackInfo ci) {
|
||||||
SelfCare.INSTANCE.init();
|
final CommandRegistryAccess commandRegistryAccess = CommandRegistryAccess.of(this.combinedDynamicRegistries.getCombinedRegistryManager(), this.enabledFeatures);
|
||||||
SongPlayer.INSTANCE.coreReady();
|
|
||||||
RainbowName.INSTANCE.init();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Inject(method = "onGameJoin", at = @At("HEAD"))
|
CommandManager.INSTANCE = new CommandManager(ChipmunkMod.CONFIG.commands.prefix, commandRegistryAccess);
|
||||||
private void onGameJoin2 (GameJoinS2CPacket packet, CallbackInfo ci) {
|
SelfCare.INSTANCE.init();
|
||||||
Players.INSTANCE.init();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package land.chipmunk.chipmunkmod.modules;
|
package land.chipmunk.chipmunkmod.modules;
|
||||||
|
|
||||||
|
import land.chipmunk.chipmunkmod.ChipmunkMod;
|
||||||
import land.chipmunk.chipmunkmod.data.BlockArea;
|
import land.chipmunk.chipmunkmod.data.BlockArea;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
|
@ -22,7 +23,7 @@ public class CommandCore {
|
||||||
@Getter private final BlockArea relativeArea;
|
@Getter private final BlockArea relativeArea;
|
||||||
@Getter @Setter private BlockPos currentBlockRelative;
|
@Getter @Setter private BlockPos currentBlockRelative;
|
||||||
|
|
||||||
public static CommandCore INSTANCE = new CommandCore(MinecraftClient.getInstance(), new BlockArea(new BlockPos(0, 0, 0), new BlockPos(15, 2, 15)));
|
public static CommandCore INSTANCE = new CommandCore(MinecraftClient.getInstance(), ChipmunkMod.CONFIG.core.relativeArea);
|
||||||
|
|
||||||
public CommandCore (MinecraftClient client, BlockArea relativeArea) {
|
public CommandCore (MinecraftClient client, BlockArea relativeArea) {
|
||||||
this.client = client;
|
this.client = client;
|
||||||
|
|
|
@ -0,0 +1,40 @@
|
||||||
|
package land.chipmunk.chipmunkmod.util.gson;
|
||||||
|
|
||||||
|
import net.minecraft.util.math.BlockPos;
|
||||||
|
import com.google.gson.TypeAdapter;
|
||||||
|
import com.google.gson.stream.JsonReader;
|
||||||
|
import com.google.gson.stream.JsonToken;
|
||||||
|
import com.google.gson.stream.JsonWriter;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class BlockPosTypeAdapter extends TypeAdapter<BlockPos> {
|
||||||
|
@Override
|
||||||
|
public BlockPos read (JsonReader reader) throws IOException {
|
||||||
|
int x = 0;
|
||||||
|
int y = 0;
|
||||||
|
int z = 0;
|
||||||
|
|
||||||
|
reader.beginObject();
|
||||||
|
|
||||||
|
while (!reader.peek().equals(JsonToken.END_OBJECT)) {
|
||||||
|
if (reader.peek().equals(JsonToken.NAME)) {
|
||||||
|
String name = reader.nextName();
|
||||||
|
|
||||||
|
// ? Is there a better way to do this?
|
||||||
|
if (name.equals("x")) x = reader.nextInt();
|
||||||
|
else if (name.equals("y")) y = reader.nextInt();
|
||||||
|
else if (name.equals("z")) z = reader.nextInt();
|
||||||
|
else reader.skipValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
reader.endObject();
|
||||||
|
|
||||||
|
return new BlockPos(x, y, z);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void write (JsonWriter out, BlockPos vec) throws IOException {
|
||||||
|
// TODO: make this do something lmfaooo
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue