Search in sources :

Example 16 with TextComponent

use of net.minecraft.network.chat.TextComponent in project MinecraftForge by MinecraftForge.

the class ClientCommandTest method testCommand.

private int testCommand(CommandContext<CommandSourceStack> context) {
    context.getSource().sendSuccess(new TextComponent("Input: " + ResourceLocationArgument.getId(context, "block")), false);
    context.getSource().sendSuccess(new TextComponent("Teams: " + context.getSource().getAllTeams()), false);
    context.getSource().sendSuccess(new TextComponent("Players: " + context.getSource().getOnlinePlayerNames()), false);
    context.getSource().sendSuccess(new TextComponent("First recipe: " + context.getSource().getRecipeNames().findFirst().get()), false);
    context.getSource().sendSuccess(new TextComponent("Levels: " + context.getSource().levels()), false);
    context.getSource().sendSuccess(new TextComponent("Registry Access: " + context.getSource().registryAccess()), false);
    return 0;
}
Also used : TextComponent(net.minecraft.network.chat.TextComponent)

Example 17 with TextComponent

use of net.minecraft.network.chat.TextComponent in project MinecraftForge by MinecraftForge.

the class CommandEventTest method onCommand.

@SubscribeEvent
public static void onCommand(CommandEvent event) {
    CommandDispatcher<CommandSourceStack> dispatcher = event.getParseResults().getContext().getDispatcher();
    List<ParsedCommandNode<CommandSourceStack>> nodes = event.getParseResults().getContext().getNodes();
    CommandSourceStack source = event.getParseResults().getContext().getSource();
    // test: when the /time command is used with no arguments, automatically add default arguments (/time set day)
    if (nodes.size() == 1 && nodes.get(0).getNode() == dispatcher.getRoot().getChild("time")) {
        event.setParseResults(dispatcher.parse("time set day", source));
        return;
    }
    // test: whenever a player uses the /give command, let everyone on the server know
    if (nodes.size() > 0 && nodes.get(0).getNode() == dispatcher.getRoot().getChild("give")) {
        String msg = source.getTextName() + " used the give command: " + event.getParseResults().getReader().getString();
        source.getServer().getPlayerList().getPlayers().forEach(player -> player.sendMessage(new TextComponent(msg), player.getUUID()));
        return;
    }
// this is annoying so I disabled it
// test: when the /kill command is used with no arguments, throw a custom exception
// if (nodes.size() == 1 && nodes.get(0).getNode() == dispatcher.getRoot().getChild("kill"))
// {
// event.setException(new CommandRuntimeException(new TextComponent("You tried to use the /kill command with no arguments")));
// event.setCanceled(true);
// return;
// }
}
Also used : TextComponent(net.minecraft.network.chat.TextComponent) ParsedCommandNode(com.mojang.brigadier.context.ParsedCommandNode) CommandSourceStack(net.minecraft.commands.CommandSourceStack) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Example 18 with TextComponent

use of net.minecraft.network.chat.TextComponent in project MinecraftForge by MinecraftForge.

the class HandshakeHandler method handleRegistryLoading.

private boolean handleRegistryLoading(final Supplier<NetworkEvent.Context> contextSupplier) {
    // We use a countdown latch to suspend the impl thread pending the client thread processing the registry data
    AtomicBoolean successfulConnection = new AtomicBoolean(false);
    CountDownLatch block = new CountDownLatch(1);
    contextSupplier.get().enqueueWork(() -> {
        LOGGER.debug(FMLHSMARKER, "Injecting registry snapshot from server.");
        final Multimap<ResourceLocation, ResourceLocation> missingData = GameData.injectSnapshot(registrySnapshots, false, false);
        LOGGER.debug(FMLHSMARKER, "Snapshot injected.");
        if (!missingData.isEmpty()) {
            LOGGER.error(FMLHSMARKER, "Missing registry data for impl connection:\n{}", LogMessageAdapter.adapt(sb -> missingData.forEach((reg, entry) -> sb.append("\t").append(reg).append(": ").append(entry).append('\n'))));
        }
        successfulConnection.set(missingData.isEmpty());
        block.countDown();
    });
    LOGGER.debug(FMLHSMARKER, "Waiting for registries to load.");
    try {
        block.await();
    } catch (InterruptedException e) {
        Thread.interrupted();
    }
    if (successfulConnection.get()) {
        LOGGER.debug(FMLHSMARKER, "Registry load complete, continuing handshake.");
    } else {
        LOGGER.error(FMLHSMARKER, "Failed to load registry, closing connection.");
        this.manager.disconnect(new TextComponent("Failed to synchronize registry data from server, closing connection"));
    }
    return successfulConnection.get();
}
Also used : ResourceLocation(net.minecraft.resources.ResourceLocation) java.util(java.util) ForgeRegistry(net.minecraftforge.registries.ForgeRegistry) ServerLoginPacketListenerImpl(net.minecraft.server.network.ServerLoginPacketListenerImpl) ServerboundCustomQueryPacket(net.minecraft.network.protocol.login.ServerboundCustomQueryPacket) Connection(net.minecraft.network.Connection) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Multimap(com.google.common.collect.Multimap) LogMessageAdapter(net.minecraftforge.common.util.LogMessageAdapter) REGISTRIES(net.minecraftforge.registries.ForgeRegistry.REGISTRIES) Maps(com.google.common.collect.Maps) Supplier(java.util.function.Supplier) CountDownLatch(java.util.concurrent.CountDownLatch) TextComponent(net.minecraft.network.chat.TextComponent) MarkerManager(org.apache.logging.log4j.MarkerManager) Logger(org.apache.logging.log4j.Logger) GameData(net.minecraftforge.registries.GameData) SimpleChannel(net.minecraftforge.network.simple.SimpleChannel) BiConsumer(java.util.function.BiConsumer) ClientIntentionPacket(net.minecraft.network.protocol.handshake.ClientIntentionPacket) LogManager(org.apache.logging.log4j.LogManager) Marker(org.apache.logging.log4j.Marker) IntSupplier(java.util.function.IntSupplier) ClientboundCustomQueryPacket(net.minecraft.network.protocol.login.ClientboundCustomQueryPacket) TextComponent(net.minecraft.network.chat.TextComponent) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ResourceLocation(net.minecraft.resources.ResourceLocation) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 19 with TextComponent

use of net.minecraft.network.chat.TextComponent in project MinecraftForge by MinecraftForge.

the class ContainerTypeTest method onRightClick.

private void onRightClick(PlayerInteractEvent.RightClickBlock event) {
    if (!event.getWorld().isClientSide && event.getHand() == InteractionHand.MAIN_HAND) {
        if (event.getWorld().getBlockState(event.getPos()).getBlock() == Blocks.SPONGE) {
            String text = "Hello World!";
            NetworkHooks.openGui((ServerPlayer) event.getPlayer(), new MenuProvider() {

                @Override
                public AbstractContainerMenu createMenu(int p_createMenu_1_, Inventory p_createMenu_2_, Player p_createMenu_3_) {
                    SimpleContainer inv = new SimpleContainer(9);
                    for (int i = 0; i < inv.getContainerSize(); i++) {
                        inv.setItem(i, new ItemStack(Items.DIAMOND));
                    }
                    return new TestContainer(p_createMenu_1_, inv, text);
                }

                @Override
                public Component getDisplayName() {
                    return new TextComponent("Test");
                }
            }, extraData -> {
                extraData.writeUtf(text);
            });
        }
    }
}
Also used : TextComponent(net.minecraft.network.chat.TextComponent) ServerPlayer(net.minecraft.server.level.ServerPlayer) Player(net.minecraft.world.entity.player.Player) AbstractContainerMenu(net.minecraft.world.inventory.AbstractContainerMenu) SimpleContainer(net.minecraft.world.SimpleContainer) ItemStack(net.minecraft.world.item.ItemStack) Component(net.minecraft.network.chat.Component) TextComponent(net.minecraft.network.chat.TextComponent) MenuProvider(net.minecraft.world.MenuProvider) Inventory(net.minecraft.world.entity.player.Inventory)

Aggregations

TextComponent (net.minecraft.network.chat.TextComponent)19 Component (net.minecraft.network.chat.Component)4 List (java.util.List)3 Minecraft (net.minecraft.client.Minecraft)3 CommandSourceStack (net.minecraft.commands.CommandSourceStack)3 TranslatableComponent (net.minecraft.network.chat.TranslatableComponent)3 ServerPlayer (net.minecraft.server.level.ServerPlayer)3 AbstractContainerMenu (net.minecraft.world.inventory.AbstractContainerMenu)3 Commands (net.minecraft.commands.Commands)2 ClientboundOpenScreenPacket (net.minecraft.network.protocol.game.ClientboundOpenScreenPacket)2 ClientboundLoginDisconnectPacket (net.minecraft.network.protocol.login.ClientboundLoginDisconnectPacket)2 ResourceLocation (net.minecraft.resources.ResourceLocation)2 Player (net.minecraft.world.entity.player.Player)2 ForgeI18n (net.minecraftforge.common.ForgeI18n)2 SubscribeEvent (net.minecraftforge.eventbus.api.SubscribeEvent)2 Strings (com.google.common.base.Strings)1 Maps (com.google.common.collect.Maps)1 Multimap (com.google.common.collect.Multimap)1 PoseStack (com.mojang.blaze3d.vertex.PoseStack)1 ArgumentBuilder (com.mojang.brigadier.builder.ArgumentBuilder)1