Код:
package me.elseif.unityautobuy;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import net.minecraft.command.CommandSource;
import net.minecraft.command.Commands;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.util.text.StringTextComponent;
import net.minecraftforge.event.RegisterCommandsEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
@Mod.EventBusSubscriber(modid = "unityautobuy", bus = Mod.EventBusSubscriber.Bus.FORGE)
public class CommandHandler {
public static boolean isAutoBuyEnabled = false;
public static int targetPrice = -1;
@SubscribeEvent
public static void onRegisterCommands(RegisterCommandsEvent event) {
CommandDispatcher<CommandSource> dispatcher = event.getDispatcher();
dispatcher.register(
Commands.literal("autobuy")
.then(Commands.literal("add")
.then(Commands.argument("price", IntegerArgumentType.integer(0))
.executes(context -> {
ServerPlayerEntity player = context.getSource().asPlayer();
if (player != null) {
int price = IntegerArgumentType.getInteger(context, "price");
targetPrice = price;
player.sendMessage(new StringTextComponent("Добавлен товар с ценой: $" + price), player.getUniqueID());
return 1;
} else {
context.getSource().sendErrorMessage(new StringTextComponent("Ошибка: Игрок не найден."));
return 0;
}
})))
.then(Commands.literal("on")
.executes(context -> {
ServerPlayerEntity player = context.getSource().asPlayer();
if (player != null) {
isAutoBuyEnabled = true;
player.sendMessage(new StringTextComponent("AutoBuy включен."), player.getUniqueID());
return 1;
} else {
context.getSource().sendErrorMessage(new StringTextComponent("Ошибка: Игрок не найден."));
return 0;
}
}))
.then(Commands.literal("off")
.executes(context -> {
ServerPlayerEntity player = context.getSource().asPlayer();
if (player != null) {
isAutoBuyEnabled = false;
player.sendMessage(new StringTextComponent("AutoBuy выключен."), player.getUniqueID());
return 1;
} else {
context.getSource().sendErrorMessage(new StringTextComponent("Ошибка: Игрок не найден."));
return 0;
}
}))
);
}
}