Search in sources :

Example 11 with ClickEvent

use of net.minecraft.text.ClickEvent in project Carrier by GabrielOlvH.

the class Carrier method onInitialize.

@Override
public void onInitialize() {
    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    File file = new File(FabricLoader.getInstance().getConfigDir().toFile(), "carrier.json");
    if (!file.exists()) {
        try {
            if (!file.createNewFile())
                throw new IOException("Failed to create file");
            FileUtils.write(file, gson.toJson(CONFIG), StandardCharsets.UTF_8);
        } catch (IOException e) {
            LogManager.getLogger("Carrier").error("Failed to create carrier config");
            throw new RuntimeException(e);
        }
    } else {
        try {
            String lines = String.join("\n", FileUtils.readLines(file, StandardCharsets.UTF_8));
            CONFIG = gson.fromJson(lines, Config.class);
        } catch (IOException e) {
            LogManager.getLogger("Carrier").error("Failed to read config");
            throw new RuntimeException(e);
        }
    }
    ServerTickEvents.END_WORLD_TICK.register(new ServerWorldTickCallback());
    CarriableRegistry.INSTANCE.register(new Identifier(MOD_ID, "minecraft_cow"), new CarriableCow());
    CarriableRegistry.INSTANCE.register(new Identifier(MOD_ID, "minecraft_chicken"), new CarriableChicken());
    CarriableRegistry.INSTANCE.register(new Identifier(MOD_ID, "minecraft_parrot"), new CarriableParrot());
    CarriableRegistry.INSTANCE.register(new Identifier(MOD_ID, "minecraft_pig"), new CarriablePig());
    CarriableRegistry.INSTANCE.register(new Identifier(MOD_ID, "minecraft_rabbit"), new CarriableRabbit());
    CarriableRegistry.INSTANCE.register(new Identifier(MOD_ID, "minecraft_sheep"), new CarriableSheep());
    CarriableRegistry.INSTANCE.register(new Identifier(MOD_ID, "minecraft_turtle"), new CarriableTurtle());
    CarriableRegistry.INSTANCE.register(new Identifier(MOD_ID, "minecraft_wolf"), new CarriableWolf());
    CarriableRegistry.INSTANCE.register(new Identifier(MOD_ID, "minecraft_spawner"), new CarriableSpawner(new Identifier(MOD_ID, "minecraft_spawner")));
    CarriableRegistry.INSTANCE.register(new Identifier(MOD_ID, "minecraft_enchanting_table"), new CarriableEnchantingTable(new Identifier(MOD_ID, "minecraft_enchanting_table")));
    Registry.BLOCK.forEach((block) -> {
        Identifier id = Registry.BLOCK.getId(block);
        Identifier type = new Identifier("carrier", id.getNamespace() + "_" + id.getPath());
        registerGenericCarriable(block, type);
    });
    RegistryEntryAddedCallback.event(Registry.BLOCK).register((rawId, id, block) -> {
        Identifier type = new Identifier("carrier", id.getNamespace() + "_" + id.getPath());
        registerGenericCarriable(block, type);
    });
    Registry.register(Registry.ITEM, new Identifier(MOD_ID, "glove"), ITEM_GLOVE);
    if (CONFIG.doGlovesExist()) {
        RuntimeResourcePack resourcePack = RuntimeResourcePack.create(MOD_ID + ":gloves");
        resourcePack.addRecipe(new Identifier(MOD_ID, "gloves"), JRecipe.shaped(JPattern.pattern("L  ", "LL "), JKeys.keys().key("L", JIngredient.ingredient().item("minecraft:leather")), JResult.item(ITEM_GLOVE)));
        RRPCallback.EVENT.register(packs -> packs.add(resourcePack));
    }
    ServerSidePacketRegistry.INSTANCE.register(SET_CAN_CARRY_PACKET, (ctx, buf) -> {
        boolean canCarry = buf.readBoolean();
        ctx.getTaskQueue().execute(() -> ((CarrierPlayerExtension) ctx.getPlayer()).setCanCarry(canCarry));
    });
    CommandRegistrationCallback.EVENT.register((commandDispatcher, b) -> commandDispatcher.register(CommandManager.literal("carrierinfo").executes((ctx) -> {
        ServerPlayerEntity player = ctx.getSource().getPlayer();
        NbtCompound tag = new NbtCompound();
        HOLDER.get(player).writeToNbt(tag);
        ctx.getSource().sendFeedback(new LiteralText(tag.toString()), false);
        return 1;
    })));
    CommandRegistrationCallback.EVENT.register((commandDispatcher, b) -> commandDispatcher.register(CommandManager.literal("carrierdelete").executes((ctx) -> {
        ServerPlayerEntity player = ctx.getSource().getPlayer();
        CarrierComponent component = HOLDER.get(player);
        NbtCompound tag = new NbtCompound();
        component.writeToNbt(tag);
        component.setCarryingData(null);
        ctx.getSource().sendFeedback(new LiteralText("Deleted ").append(new LiteralText(tag.toString()).setStyle(Style.EMPTY.withClickEvent(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, tag.toString())))), false);
        return 1;
    })));
    CommandRegistrationCallback.EVENT.register((commandDispatcher, b) -> commandDispatcher.register(CommandManager.literal("carrierplace").executes((ctx) -> {
        ServerPlayerEntity player = ctx.getSource().getPlayer();
        CarrierComponent component = HOLDER.get(player);
        Carriable<Object> carriable = CarriableRegistry.INSTANCE.get(component.getCarryingData().getType());
        BlockPos pos = player.getBlockPos().offset(player.getHorizontalFacing());
        ServerWorld world = ctx.getSource().getWorld();
        if (!world.getBlockState(pos).getMaterial().isReplaceable()) {
            ctx.getSource().sendFeedback(new LiteralText("Could not place! Make sure you have empty space in front of you."), false);
            return 1;
        }
        CarriablePlacementContext placementCtx = new CarriablePlacementContext(component, carriable, pos, player.getHorizontalFacing().getOpposite(), player.getHorizontalFacing());
        carriable.tryPlace(component, world, placementCtx);
        component.setCarryingData(null);
        return 1;
    })));
}
Also used : NbtCompound(net.minecraft.nbt.NbtCompound) ClickEvent(net.minecraft.text.ClickEvent) Gson(com.google.gson.Gson) RuntimeResourcePack(net.devtech.arrp.api.RuntimeResourcePack) ServerWorld(net.minecraft.server.world.ServerWorld) Identifier(net.minecraft.util.Identifier) BlockPos(net.minecraft.util.math.BlockPos) LiteralText(net.minecraft.text.LiteralText) GsonBuilder(com.google.gson.GsonBuilder) ServerPlayerEntity(net.minecraft.server.network.ServerPlayerEntity) IOException(java.io.IOException) File(java.io.File)

Example 12 with ClickEvent

use of net.minecraft.text.ClickEvent in project KahzerxMod by otakucraft.

the class ExchangeCommand method register.

public void register(CommandDispatcher<ServerCommandSource> dispatcher, ShopExtension extension) {
    dispatcher.register(literal("exchange").requires(server -> extension.extensionSettings().isEnabled()).then(literal("confirm").then(argument("count", IntegerArgumentType.integer(0)).then(argument("item", StringArgumentType.string()).executes(context -> {
        ServerPlayerEntity player = context.getSource().getPlayer();
        Item item = player.getMainHandStack().getItem();
        String sentItem = StringArgumentType.getString(context, "item");
        int count = player.getMainHandStack().getCount();
        int sentCount = IntegerArgumentType.getInteger(context, "count");
        if (!basicVerify(context, player)) {
            return 1;
        }
        if (count != sentCount || !item.getTranslationKey().equals(sentItem)) {
            context.getSource().sendFeedback(MarkEnum.CROSS.appendMessage("El stack de tu mano ha cambiado!"), false);
            return 1;
        }
        extension.updateFounds(player, Exchange.getValue(item) * count);
        extension.logExchange(player, item, count);
        player.setStackInHand(Hand.MAIN_HAND, new ItemStack(Items.AIR));
        context.getSource().sendFeedback(MarkEnum.TICK.appendMessage("Añadidos ").append(MarkEnum.OTAKU_COIN.appendMessage(String.valueOf(Exchange.getValue(item) * count))), false);
        return 1;
    })))).then(literal("abort").then(argument("count", IntegerArgumentType.integer(0)).then(argument("item", StringArgumentType.string()).executes(context -> {
        ServerPlayerEntity player = context.getSource().getPlayer();
        Item item = player.getMainHandStack().getItem();
        String sentItem = StringArgumentType.getString(context, "item");
        int count = player.getMainHandStack().getCount();
        int sentCount = IntegerArgumentType.getInteger(context, "count");
        if (count == sentCount || item.getTranslationKey().equals(sentItem)) {
            context.getSource().sendFeedback(MarkEnum.TICK.appendMessage("Exchange cancelado!"), false);
        }
        return 1;
    })))).executes(context -> {
        ServerPlayerEntity player = context.getSource().getPlayer();
        Item item = player.getMainHandStack().getItem();
        int count = player.getMainHandStack().getCount();
        if (!basicVerify(context, player)) {
            return 1;
        }
        context.getSource().sendFeedback(MarkEnum.INFO.appendMessage(String.format("Vas a cambiar %d %s por ", count, item.getName().getString())).append(MarkEnum.OTAKU_COIN.appendMessage(String.format("%d", Exchange.getValue(item) * count))), false);
        context.getSource().sendFeedback(MarkEnum.QUESTION.appendMessage("Confirmar exchange? ").append(new LiteralText("[Sí]").styled(style -> style.withColor(Formatting.GREEN).withBold(true).withClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, String.format("/exchange confirm %d %s", count, item.getTranslationKey()))).withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new LiteralText("confirmar exchange"))))).append(new LiteralText(" ").styled(style -> style.withColor(Formatting.WHITE))).append(new LiteralText("[No]").styled(style -> style.withColor(Formatting.DARK_RED).withBold(true).withClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, String.format("/exchange abort %d %s", count, item.getTranslationKey()))).withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new LiteralText("cancelar exchange"))))), false);
        return 1;
    }).then(literal("info").executes(context -> {
        context.getSource().sendFeedback(MarkEnum.OTAKU_COIN.appendMessage("a"), false);
        return 1;
    })));
}
Also used : LiteralText(net.minecraft.text.LiteralText) ShopExtension(com.kahzerx.kahzerxmod.extensions.shopExtension.ShopExtension) CommandDispatcher(com.mojang.brigadier.CommandDispatcher) ServerCommandSource(net.minecraft.server.command.ServerCommandSource) CommandContext(com.mojang.brigadier.context.CommandContext) MarkEnum(com.kahzerx.kahzerxmod.utils.MarkEnum) Item(net.minecraft.item.Item) Items(net.minecraft.item.Items) CommandManager.literal(net.minecraft.server.command.CommandManager.literal) HoverEvent(net.minecraft.text.HoverEvent) CommandManager.argument(net.minecraft.server.command.CommandManager.argument) StringArgumentType(com.mojang.brigadier.arguments.StringArgumentType) ItemStack(net.minecraft.item.ItemStack) Formatting(net.minecraft.util.Formatting) ServerPlayerEntity(net.minecraft.server.network.ServerPlayerEntity) ClickEvent(net.minecraft.text.ClickEvent) MutableText(net.minecraft.text.MutableText) Hand(net.minecraft.util.Hand) IntegerArgumentType(com.mojang.brigadier.arguments.IntegerArgumentType) Item(net.minecraft.item.Item) HoverEvent(net.minecraft.text.HoverEvent) ClickEvent(net.minecraft.text.ClickEvent) ServerPlayerEntity(net.minecraft.server.network.ServerPlayerEntity) ItemStack(net.minecraft.item.ItemStack) LiteralText(net.minecraft.text.LiteralText)

Example 13 with ClickEvent

use of net.minecraft.text.ClickEvent in project meteor-client by MeteorDevelopment.

the class NbtCommand method build.

@Override
public void build(LiteralArgumentBuilder<CommandSource> builder) {
    builder.then(literal("add").then(argument("nbt_data", CompoundNbtTagArgumentType.nbtTag()).executes(s -> {
        ItemStack stack = mc.player.getInventory().getMainHandStack();
        if (validBasic(stack)) {
            NbtCompound tag = CompoundNbtTagArgumentType.getTag(s, "nbt_data");
            NbtCompound source = stack.getOrCreateNbt();
            if (tag != null) {
                source.copyFrom(tag);
                setStack(stack);
            } else {
                error("Some of the NBT data could not be found, try using: " + Config.get().prefix.get() + "nbt set {nbt}");
            }
        }
        return SINGLE_SUCCESS;
    })));
    builder.then(literal("set").then(argument("nbt_data", CompoundNbtTagArgumentType.nbtTag()).executes(s -> {
        ItemStack stack = mc.player.getInventory().getMainHandStack();
        if (validBasic(stack)) {
            NbtCompound tag = s.getArgument("nbt_data", NbtCompound.class);
            stack.setNbt(tag);
            setStack(stack);
        }
        return SINGLE_SUCCESS;
    })));
    builder.then(literal("remove").then(argument("nbt_path", NbtPathArgumentType.nbtPath()).executes(s -> {
        ItemStack stack = mc.player.getInventory().getMainHandStack();
        if (validBasic(stack)) {
            NbtPathArgumentType.NbtPath path = s.getArgument("nbt_path", NbtPathArgumentType.NbtPath.class);
            path.remove(stack.getNbt());
        }
        return SINGLE_SUCCESS;
    })));
    builder.then(literal("get").executes(s -> {
        ItemStack stack = mc.player.getInventory().getMainHandStack();
        if (stack == null) {
            error("You must hold an item in your main hand.");
        } else {
            NbtCompound tag = stack.getNbt();
            String nbt = tag == null ? "{}" : tag.asString();
            BaseText copyButton = new LiteralText("NBT");
            copyButton.setStyle(copyButton.getStyle().withFormatting(Formatting.UNDERLINE).withClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, this.toString("copy"))).withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new LiteralText("Copy the NBT data to your clipboard."))));
            BaseText text = new LiteralText("");
            text.append(copyButton);
            text.append(new LiteralText(": " + nbt));
            info(text);
        }
        return SINGLE_SUCCESS;
    }));
    builder.then(literal("copy").executes(s -> {
        ItemStack stack = mc.player.getInventory().getMainHandStack();
        if (stack == null) {
            error("You must hold an item in your main hand.");
        } else {
            NbtCompound tag = stack.getOrCreateNbt();
            mc.keyboard.setClipboard(tag.toString());
            BaseText nbt = new LiteralText("NBT");
            nbt.setStyle(nbt.getStyle().withFormatting(Formatting.UNDERLINE).withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new LiteralText(tag.toString()))));
            BaseText text = new LiteralText("");
            text.append(nbt);
            text.append(new LiteralText(" data copied!"));
            info(text);
        }
        return SINGLE_SUCCESS;
    }));
    builder.then(literal("count").then(argument("count", IntegerArgumentType.integer(-127, 127)).executes(context -> {
        ItemStack stack = mc.player.getInventory().getMainHandStack();
        if (validBasic(stack)) {
            int count = IntegerArgumentType.getInteger(context, "count");
            stack.setCount(count);
            setStack(stack);
            info("Set mainhand stack count to %s.", count);
        }
        return SINGLE_SUCCESS;
    })));
}
Also used : LiteralText(net.minecraft.text.LiteralText) CompoundNbtTagArgumentType(meteordevelopment.meteorclient.systems.commands.arguments.CompoundNbtTagArgumentType) CreativeInventoryActionC2SPacket(net.minecraft.network.packet.c2s.play.CreativeInventoryActionC2SPacket) Config(meteordevelopment.meteorclient.systems.config.Config) HoverEvent(net.minecraft.text.HoverEvent) LiteralArgumentBuilder(com.mojang.brigadier.builder.LiteralArgumentBuilder) CommandSource(net.minecraft.command.CommandSource) SINGLE_SUCCESS(com.mojang.brigadier.Command.SINGLE_SUCCESS) ItemStack(net.minecraft.item.ItemStack) NbtCompound(net.minecraft.nbt.NbtCompound) BaseText(net.minecraft.text.BaseText) Formatting(net.minecraft.util.Formatting) NbtPathArgumentType(net.minecraft.command.argument.NbtPathArgumentType) Command(meteordevelopment.meteorclient.systems.commands.Command) ClickEvent(net.minecraft.text.ClickEvent) IntegerArgumentType(com.mojang.brigadier.arguments.IntegerArgumentType) BaseText(net.minecraft.text.BaseText) HoverEvent(net.minecraft.text.HoverEvent) NbtCompound(net.minecraft.nbt.NbtCompound) NbtPathArgumentType(net.minecraft.command.argument.NbtPathArgumentType) ClickEvent(net.minecraft.text.ClickEvent) ItemStack(net.minecraft.item.ItemStack) LiteralText(net.minecraft.text.LiteralText)

Example 14 with ClickEvent

use of net.minecraft.text.ClickEvent in project meteor-client by MeteorDevelopment.

the class CommandsCommand method getCommandText.

private BaseText getCommandText(Command command) {
    // Hover tooltip
    BaseText tooltip = new LiteralText("");
    tooltip.append(new LiteralText(Utils.nameToTitle(command.getName())).formatted(Formatting.BLUE, Formatting.BOLD)).append("\n");
    BaseText aliases = new LiteralText(Config.get().prefix.get() + command.getName());
    if (command.getAliases().size() > 0) {
        aliases.append(", ");
        for (String alias : command.getAliases()) {
            if (alias.isEmpty())
                continue;
            aliases.append(Config.get().prefix.get() + alias);
            if (!alias.equals(command.getAliases().get(command.getAliases().size() - 1)))
                aliases.append(", ");
        }
    }
    tooltip.append(aliases.formatted(Formatting.GRAY)).append("\n\n");
    tooltip.append(new LiteralText(command.getDescription()).formatted(Formatting.WHITE));
    // Text
    BaseText text = new LiteralText(Utils.nameToTitle(command.getName()));
    if (command != Commands.get().getAll().get(Commands.get().getAll().size() - 1))
        text.append(new LiteralText(", ").formatted(Formatting.GRAY));
    text.setStyle(text.getStyle().withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, tooltip)).withClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, Config.get().prefix.get() + command.getName())));
    return text;
}
Also used : BaseText(net.minecraft.text.BaseText) HoverEvent(net.minecraft.text.HoverEvent) ClickEvent(net.minecraft.text.ClickEvent) LiteralText(net.minecraft.text.LiteralText)

Example 15 with ClickEvent

use of net.minecraft.text.ClickEvent in project carpet-discarpet by replaceitem.

the class DiscarpetCommand method register.

public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
    dispatcher.register(literal("discarpet").requires(serverCommandSource -> serverCommandSource.hasPermissionLevel(2)).executes(commandContext -> {
        String version = FabricLoader.getInstance().getModContainer("discarpet").get().getMetadata().getVersion().getFriendlyString();
        MutableText text = new LiteralText("Discarpet version " + version).formatted(Formatting.BLUE);
        text.append("\nFor help, see the ");
        text.append(new LiteralText("documentation").setStyle(Style.EMPTY.withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, "https://github.com/replaceitem/carpet-discarpet/blob/master/docs/Full.md")).withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new LiteralText("Click to get to the Discarpet documentation"))).withFormatting(Formatting.UNDERLINE).withColor(Formatting.DARK_BLUE)));
        commandContext.getSource().sendFeedback(text, true);
        return 1;
    }).then(literal("list").executes(commandContext -> {
        Set<String> botIDs = Discarpet.discordBots.keySet();
        final LiteralText t;
        if (botIDs.size() == 0) {
            t = (LiteralText) new LiteralText("There are no bots active:\n").formatted(Formatting.RED);
        } else {
            t = (LiteralText) new LiteralText("There are " + botIDs.size() + " bots active\n").formatted(Formatting.GREEN);
        }
        botIDs.forEach(id -> t.append(new LiteralText(id + "\n").formatted(Formatting.BLUE)));
        commandContext.getSource().sendFeedback(t, true);
        return botIDs.size();
    })).then(literal("getInvite").then(CommandManager.argument("id", StringArgumentType.string()).suggests(BOTS).executes(commandContext -> {
        String id = StringArgumentType.getString(commandContext, "id");
        String invite = Discarpet.discordBots.get(id).getInvite();
        Text text = ((new LiteralText("Click here to get the invite link for the bot")).styled((style) -> {
            return style.withColor(Formatting.BLUE).withFormatting(Formatting.UNDERLINE).withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, invite)).withHoverEvent(new HoverEvent(net.minecraft.text.HoverEvent.Action.SHOW_TEXT, new LiteralText("Click to open the invite link"))).withInsertion(invite);
        }));
        commandContext.getSource().sendFeedback(text, false);
        return 1;
    }))).then(literal("reload").executes(commandContext -> {
        Discarpet.loadConfig(commandContext.getSource());
        return 1;
    })));
}
Also used : Discarpet(Discarpet.Discarpet) FabricLoader(net.fabricmc.loader.api.FabricLoader) LiteralText(net.minecraft.text.LiteralText) CommandDispatcher(com.mojang.brigadier.CommandDispatcher) ServerCommandSource(net.minecraft.server.command.ServerCommandSource) Set(java.util.Set) CommandManager.literal(net.minecraft.server.command.CommandManager.literal) HoverEvent(net.minecraft.text.HoverEvent) CommandManager(net.minecraft.server.command.CommandManager) CommandSource(net.minecraft.command.CommandSource) StringArgumentType(com.mojang.brigadier.arguments.StringArgumentType) Style(net.minecraft.text.Style) Formatting(net.minecraft.util.Formatting) ClickEvent(net.minecraft.text.ClickEvent) MutableText(net.minecraft.text.MutableText) Text(net.minecraft.text.Text) SuggestionProvider(com.mojang.brigadier.suggestion.SuggestionProvider) MutableText(net.minecraft.text.MutableText) HoverEvent(net.minecraft.text.HoverEvent) ClickEvent(net.minecraft.text.ClickEvent) LiteralText(net.minecraft.text.LiteralText) MutableText(net.minecraft.text.MutableText) Text(net.minecraft.text.Text) LiteralText(net.minecraft.text.LiteralText)

Aggregations

ClickEvent (net.minecraft.text.ClickEvent)24 LiteralText (net.minecraft.text.LiteralText)24 HoverEvent (net.minecraft.text.HoverEvent)16 MutableText (net.minecraft.text.MutableText)8 BaseText (net.minecraft.text.BaseText)6 ItemStack (net.minecraft.item.ItemStack)5 NbtCompound (net.minecraft.nbt.NbtCompound)5 Formatting (net.minecraft.util.Formatting)5 IntegerArgumentType (com.mojang.brigadier.arguments.IntegerArgumentType)4 IOException (java.io.IOException)4 CommandSource (net.minecraft.command.CommandSource)4 ServerPlayerEntity (net.minecraft.server.network.ServerPlayerEntity)4 Style (net.minecraft.text.Style)4 CommandDispatcher (com.mojang.brigadier.CommandDispatcher)3 LiteralArgumentBuilder (com.mojang.brigadier.builder.LiteralArgumentBuilder)3 CommandContext (com.mojang.brigadier.context.CommandContext)3 NbtPathArgumentType (net.minecraft.command.argument.NbtPathArgumentType)3 CreativeInventoryActionC2SPacket (net.minecraft.network.packet.c2s.play.CreativeInventoryActionC2SPacket)3 CommandManager.literal (net.minecraft.server.command.CommandManager.literal)3 ServerCommandSource (net.minecraft.server.command.ServerCommandSource)3