- 96
- 2
- 6
Искал в интернете нашел только этот вопрос на форуме forge и в следствии этот гайд, но комманды просто не было в игре!
Main.java:
package ru.cha0sf4me.example;
import net.minecraftforge.fml.common.Mod;
@Mod(Main.MOD_ID)
public class Main {
public static final String MOD_ID = "example";
}
EventHandler.java:
package ru.cha0sf4me.example;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.event.RegisterCommandsEvent;
import net.minecraftforge.event.entity.living.LivingDeathEvent;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import ru.cha0sf4me.cha0sutils.commands.TestCommand;
@Mod.EventBusSubscriber(modid = Main.MOD_ID, bus = Mod.EventBusSubscriber.Bus.MOD, value = Dist.CLIENT)
public class EventHandler
{
@SubscribeEvent
public void onServerStarting(RegisterCommandsEvent e) {
TestCommand.register((IEventBus) e.getDispatcher());
}
@SubscribeEvent
public void onDeath(LivingDeathEvent event)
{
if (event.getEntity() instanceof PlayerEntity) {
PlayerEntity player = (PlayerEntity) event.getEntity();
if (player.getName().equals("Dev")) {
player.dropItem(new ItemStack(Items.DEBUG_STICK, 1), false);
}
}
}
}
TestCommand.java:
package ru.cha0sf4me.example.commands;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
import com.mojang.brigadier.suggestion.SuggestionProvider;
import net.minecraft.command.CommandSource;
import net.minecraft.command.Commands;
import net.minecraft.command.ISuggestionProvider;
import net.minecraft.command.arguments.EntityArgument;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraftforge.eventbus.api.IEventBus;
public class TestCommand {
private static final DynamicCommandExceptionType UNKNOWN_COLOR = new DynamicCommandExceptionType(color -> {
return new TranslationTextComponent("commands.test.unknown_color", color);
});
private static final SuggestionProvider<CommandSource> SUGGEST_COLOR = (source, builder) -> {
return ISuggestionProvider.suggest(TextFormatting.getValidValues(true, false).stream(), builder);
};
public static void register(IEventBus dispatcher) {
dispatcher.register(Commands.literal("hello").executes(source -> {
return hello(source.getSource(), source.getSource().asPlayer());
}).then(Commands.argument("target", EntityArgument.player()).executes(source -> {
return hello(source.getSource(), EntityArgument.getPlayer(source, "target"));
}).then(Commands.argument("color", StringArgumentType.string()).suggests(SUGGEST_COLOR).executes(source -> {
return hello(source.getSource(), EntityArgument.getPlayer(source, "target"), StringArgumentType.getString(source, "color"));
}))));
}
private static int hello(CommandSource source, ServerPlayerEntity player) {
source.sendFeedback(new TranslationTextComponent("commands.hello", player.getDisplayName()), true);
return 1;
}
private static int hello(CommandSource source, ServerPlayerEntity player, String color) throws CommandSyntaxException {
if(TextFormatting.getValueByName(color) == null) {
throw UNKNOWN_COLOR.create(color);
}
source.sendFeedback(new TranslationTextComponent("commands.hello.color", TextFormatting.getValueByName(color).toString(), player.getDisplayName()), true);
return 1;
}
}
Последнее редактирование: