Search in sources :

Example 6 with Component

use of net.minecraft.network.chat.Component in project MyPet by xXKeyleXx.

the class IconMenuInventory method createItemStack.

protected ItemStack createItemStack(IconMenuItem icon) {
    // TODO Check if this works properly
    ItemStack is = CraftItemStack.asNMSCopy(new org.bukkit.inventory.ItemStack(icon.getMaterial(), icon.getAmount()));
    if (is == null) {
        is = CraftItemStack.asNMSCopy(new org.bukkit.inventory.ItemStack(Material.STONE));
    }
    if (is.getTag() == null) {
        is.setTag(new CompoundTag());
    }
    if (icon.getBukkitMeta() != null) {
        try {
            applyToItemMethod.invoke(icon.getBukkitMeta(), is.getTag());
        } catch (InvocationTargetException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }
    // add enchantment glowing
    if (icon.isGlowing()) {
        TagCompound enchTag = new TagCompound();
        enchTag.put("id", new TagString("minecraft:feather_falling"));
        enchTag.put("lvl", new TagShort(1));
        TagList enchList = new TagList();
        enchList.addTag(enchTag);
        is.getTag().put("Enchantments", ItemStackNBTConverter.compoundToVanillaCompound(enchList));
    } else {
        is.getTag().remove("Enchantments");
    }
    // hide item attributes like attack damage
    is.getTag().putInt("HideFlags", 63);
    // Prepare display tag
    CompoundTag display;
    if (is.getTag().contains("display")) {
        display = is.getTag().getCompound("display");
    } else {
        display = new CompoundTag();
        is.getTag().put("display", display);
    }
    // set Title
    if (!icon.getTitle().equals("")) {
        display.putString("Name", "{\"text\":\"" + icon.getTitle() + "\"}");
    }
    if (icon.getLore().size() > 0) {
        // set Lore
        ListTag loreTag = new ListTag();
        display.put("Lore", loreTag);
        for (String loreLine : icon.getLore()) {
            Component cm = CraftChatMessage.fromStringOrNull(loreLine);
            loreTag.add(StringTag.valueOf(Component.Serializer.toJson(cm)));
        }
    }
    if (icon.hasMeta()) {
        TagCompound tag = new TagCompound();
        icon.getMeta().applyTo(tag);
        CompoundTag vanillaTag = (CompoundTag) ItemStackNBTConverter.compoundToVanillaCompound(tag);
        for (String key : vanillaTag.getAllKeys()) {
            is.getTag().put(key, vanillaTag.get(key));
        }
    }
    if (icon.getTags() != null) {
        CompoundTag vanillaTag = (CompoundTag) ItemStackNBTConverter.compoundToVanillaCompound(icon.getTags());
        for (String key : vanillaTag.getAllKeys()) {
            is.getTag().put(key, vanillaTag.get(key));
        }
    }
    return is;
}
Also used : TagShort(de.keyle.knbt.TagShort) TagString(de.keyle.knbt.TagString) TagString(de.keyle.knbt.TagString) TagCompound(de.keyle.knbt.TagCompound) ListTag(net.minecraft.nbt.ListTag) InvocationTargetException(java.lang.reflect.InvocationTargetException) TagList(de.keyle.knbt.TagList) ItemStack(net.minecraft.world.item.ItemStack) CraftItemStack(org.bukkit.craftbukkit.v1_17_R1.inventory.CraftItemStack) Component(net.minecraft.network.chat.Component) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 7 with Component

use of net.minecraft.network.chat.Component in project Denizen-For-Bukkit by DenizenScript.

the class PacketHelperImpl method showTabListHeaderFooter.

@Override
public void showTabListHeaderFooter(Player player, String header, String footer) {
    Component cHeader = Handler.componentToNMS(FormattedTextHelper.parse(header, ChatColor.WHITE));
    Component cFooter = Handler.componentToNMS(FormattedTextHelper.parse(footer, ChatColor.WHITE));
    ClientboundTabListPacket packet = new ClientboundTabListPacket(cHeader, cFooter);
    send(player, packet);
}
Also used : Component(net.minecraft.network.chat.Component)

Example 8 with Component

use of net.minecraft.network.chat.Component in project Denizen-For-Bukkit by DenizenScript.

the class ProfileEditorImpl method handleAlteredProfiles.

public static boolean handleAlteredProfiles(ClientboundPlayerInfoPacket packet, DenizenNetworkManagerImpl manager) {
    if (ProfileEditor.mirrorUUIDs.isEmpty() && !RenameCommand.hasAnyDynamicRenames()) {
        return true;
    }
    ClientboundPlayerInfoPacket.Action action = packet.getAction();
    if (action != ClientboundPlayerInfoPacket.Action.ADD_PLAYER && action != ClientboundPlayerInfoPacket.Action.UPDATE_DISPLAY_NAME) {
        return true;
    }
    List<ClientboundPlayerInfoPacket.PlayerUpdate> dataList = packet.getEntries();
    if (dataList == null) {
        return true;
    }
    try {
        boolean any = false;
        for (ClientboundPlayerInfoPacket.PlayerUpdate data : dataList) {
            if (ProfileEditor.mirrorUUIDs.contains(data.getProfile().getId()) || RenameCommand.customNames.containsKey(data.getProfile().getId())) {
                any = true;
            }
        }
        if (!any) {
            return true;
        }
        GameProfile ownProfile = manager.player.getGameProfile();
        for (ClientboundPlayerInfoPacket.PlayerUpdate data : dataList) {
            if (!ProfileEditor.mirrorUUIDs.contains(data.getProfile().getId()) && !RenameCommand.customNames.containsKey(data.getProfile().getId())) {
                ClientboundPlayerInfoPacket newPacket = new ClientboundPlayerInfoPacket(action);
                List<ClientboundPlayerInfoPacket.PlayerUpdate> newPacketDataList = newPacket.getEntries();
                newPacketDataList.add(data);
                manager.oldManager.send(newPacket);
            } else {
                String rename = RenameCommand.getCustomNameFor(data.getProfile().getId(), manager.player.getBukkitEntity(), false);
                ClientboundPlayerInfoPacket newPacket = new ClientboundPlayerInfoPacket(action);
                List<ClientboundPlayerInfoPacket.PlayerUpdate> newPacketDataList = newPacket.getEntries();
                GameProfile patchedProfile = new GameProfile(data.getProfile().getId(), rename != null ? (rename.length() > 16 ? rename.substring(0, 16) : rename) : data.getProfile().getName());
                if (ProfileEditor.mirrorUUIDs.contains(data.getProfile().getId())) {
                    patchedProfile.getProperties().putAll(ownProfile.getProperties());
                } else {
                    patchedProfile.getProperties().putAll(data.getProfile().getProperties());
                }
                String listRename = RenameCommand.getCustomNameFor(data.getProfile().getId(), manager.player.getBukkitEntity(), true);
                Component displayName = listRename != null ? Handler.componentToNMS(FormattedTextHelper.parse(listRename, ChatColor.WHITE)) : data.getDisplayName();
                ClientboundPlayerInfoPacket.PlayerUpdate newData = new ClientboundPlayerInfoPacket.PlayerUpdate(patchedProfile, data.getLatency(), data.getGameMode(), displayName);
                newPacketDataList.add(newData);
                manager.oldManager.send(newPacket);
            }
        }
        return false;
    } catch (Exception e) {
        Debug.echoError(e);
        return true;
    }
}
Also used : GameProfile(com.mojang.authlib.GameProfile) Component(net.minecraft.network.chat.Component) ClientboundPlayerInfoPacket(net.minecraft.network.protocol.game.ClientboundPlayerInfoPacket)

Example 9 with Component

use of net.minecraft.network.chat.Component in project Denizen-For-Bukkit by DenizenScript.

the class ProfileEditorImpl method handleAlteredProfiles.

public static boolean handleAlteredProfiles(ClientboundPlayerInfoPacket packet, DenizenNetworkManagerImpl manager) {
    if (ProfileEditor.mirrorUUIDs.isEmpty() && !RenameCommand.hasAnyDynamicRenames()) {
        return true;
    }
    ClientboundPlayerInfoPacket.Action action = packet.getAction();
    if (action != ClientboundPlayerInfoPacket.Action.ADD_PLAYER && action != ClientboundPlayerInfoPacket.Action.UPDATE_DISPLAY_NAME) {
        return true;
    }
    List<ClientboundPlayerInfoPacket.PlayerUpdate> dataList = packet.getEntries();
    if (dataList == null) {
        return true;
    }
    try {
        boolean any = false;
        for (ClientboundPlayerInfoPacket.PlayerUpdate data : dataList) {
            if (ProfileEditor.mirrorUUIDs.contains(data.getProfile().getId()) || RenameCommand.customNames.containsKey(data.getProfile().getId())) {
                any = true;
            }
        }
        if (!any) {
            return true;
        }
        GameProfile ownProfile = manager.player.getGameProfile();
        for (ClientboundPlayerInfoPacket.PlayerUpdate data : dataList) {
            if (!ProfileEditor.mirrorUUIDs.contains(data.getProfile().getId()) && !RenameCommand.customNames.containsKey(data.getProfile().getId())) {
                ClientboundPlayerInfoPacket newPacket = new ClientboundPlayerInfoPacket(action);
                List<ClientboundPlayerInfoPacket.PlayerUpdate> newPacketDataList = newPacket.getEntries();
                newPacketDataList.add(data);
                manager.oldManager.send(newPacket);
            } else {
                String rename = RenameCommand.getCustomNameFor(data.getProfile().getId(), manager.player.getBukkitEntity(), false);
                ClientboundPlayerInfoPacket newPacket = new ClientboundPlayerInfoPacket(action);
                List<ClientboundPlayerInfoPacket.PlayerUpdate> newPacketDataList = newPacket.getEntries();
                GameProfile patchedProfile = new GameProfile(data.getProfile().getId(), rename != null ? (rename.length() > 16 ? rename.substring(0, 16) : rename) : data.getProfile().getName());
                if (ProfileEditor.mirrorUUIDs.contains(data.getProfile().getId())) {
                    patchedProfile.getProperties().putAll(ownProfile.getProperties());
                } else {
                    patchedProfile.getProperties().putAll(data.getProfile().getProperties());
                }
                String listRename = RenameCommand.getCustomNameFor(data.getProfile().getId(), manager.player.getBukkitEntity(), true);
                Component displayName = listRename != null ? Handler.componentToNMS(FormattedTextHelper.parse(listRename, ChatColor.WHITE)) : data.getDisplayName();
                ClientboundPlayerInfoPacket.PlayerUpdate newData = new ClientboundPlayerInfoPacket.PlayerUpdate(patchedProfile, data.getLatency(), data.getGameMode(), displayName);
                newPacketDataList.add(newData);
                manager.oldManager.send(newPacket);
            }
        }
        return false;
    } catch (Exception e) {
        Debug.echoError(e);
        return true;
    }
}
Also used : GameProfile(com.mojang.authlib.GameProfile) Component(net.minecraft.network.chat.Component) ClientboundPlayerInfoPacket(net.minecraft.network.protocol.game.ClientboundPlayerInfoPacket)

Example 10 with Component

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

the class ForgeHooksClient method gatherTooltipComponents.

public static List<ClientTooltipComponent> gatherTooltipComponents(ItemStack stack, List<? extends FormattedText> textElements, Optional<TooltipComponent> itemComponent, int mouseX, int screenWidth, int screenHeight, @Nullable Font forcedFont, Font fallbackFont) {
    Font font = getTooltipFont(forcedFont, stack, fallbackFont);
    List<Either<FormattedText, TooltipComponent>> elements = textElements.stream().map((Function<FormattedText, Either<FormattedText, TooltipComponent>>) Either::left).collect(Collectors.toCollection(ArrayList::new));
    itemComponent.ifPresent(c -> elements.add(1, Either.right(c)));
    var event = new RenderTooltipEvent.GatherComponents(stack, screenWidth, screenHeight, elements, -1);
    MinecraftForge.EVENT_BUS.post(event);
    if (event.isCanceled())
        return List.of();
    // text wrapping
    int tooltipTextWidth = event.getTooltipElements().stream().mapToInt(either -> either.map(font::width, component -> 0)).max().orElse(0);
    boolean needsWrap = false;
    int tooltipX = mouseX + 12;
    if (tooltipX + tooltipTextWidth + 4 > screenWidth) {
        tooltipX = mouseX - 16 - tooltipTextWidth;
        if (// if the tooltip doesn't fit on the screen
        tooltipX < 4) {
            if (mouseX > screenWidth / 2)
                tooltipTextWidth = mouseX - 12 - 8;
            else
                tooltipTextWidth = screenWidth - 16 - mouseX;
            needsWrap = true;
        }
    }
    if (event.getMaxWidth() > 0 && tooltipTextWidth > event.getMaxWidth()) {
        tooltipTextWidth = event.getMaxWidth();
        needsWrap = true;
    }
    int tooltipTextWidthF = tooltipTextWidth;
    if (needsWrap) {
        return event.getTooltipElements().stream().flatMap(either -> either.map(text -> font.split(text, tooltipTextWidthF).stream().map(ClientTooltipComponent::create), component -> Stream.of(ClientTooltipComponent.create(component)))).toList();
    }
    return event.getTooltipElements().stream().map(either -> either.map(text -> ClientTooltipComponent.create(text instanceof Component ? ((Component) text).getVisualOrderText() : Language.getInstance().getVisualOrder(text)), ClientTooltipComponent::create)).toList();
}
Also used : Camera(net.minecraft.client.Camera) Font(net.minecraft.client.gui.Font) Arrays(java.util.Arrays) Connection(net.minecraft.network.Connection) ForgeMod(net.minecraftforge.common.ForgeMod) Dist(net.minecraftforge.api.distmarker.Dist) net.minecraftforge.fml(net.minecraftforge.fml) Pair(org.apache.commons.lang3.tuple.Pair) MultiPlayerGameMode(net.minecraft.client.multiplayer.MultiPlayerGameMode) Map(java.util.Map) AbstractClientPlayer(net.minecraft.client.player.AbstractClientPlayer) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent) TooltipComponent(net.minecraft.world.inventory.tooltip.TooltipComponent) WorldPreset(net.minecraft.client.gui.screens.worldselection.WorldPreset) TranslatableComponent(net.minecraft.network.chat.TranslatableComponent) Window(com.mojang.blaze3d.platform.Window) ForgeVersion(net.minecraftforge.versions.forge.ForgeVersion) SoundInstance(net.minecraft.client.resources.sounds.SoundInstance) Screen(net.minecraft.client.gui.screens.Screen) ResourceManager(net.minecraft.server.packs.resources.ResourceManager) Set(java.util.Set) Language(net.minecraft.locale.Language) NetworkRegistry(net.minecraftforge.network.NetworkRegistry) TextComponent(net.minecraft.network.chat.TextComponent) Logger(org.apache.logging.log4j.Logger) Stream(java.util.stream.Stream) FormattedText(net.minecraft.network.chat.FormattedText) FluidState(net.minecraft.world.level.material.FluidState) ForgeI18n(net.minecraftforge.common.ForgeI18n) ItemStack(net.minecraft.world.item.ItemStack) GameType(net.minecraft.world.level.GameType) ForgeRegistries(net.minecraftforge.registries.ForgeRegistries) BETA(net.minecraftforge.fml.VersionChecker.Status.BETA) Resource(net.minecraft.server.packs.resources.Resource) WorldGenSettings(net.minecraft.world.level.levelgen.WorldGenSettings) com.mojang.blaze3d.vertex(com.mojang.blaze3d.vertex) HumanoidArm(net.minecraft.world.entity.HumanoidArm) BlockState(net.minecraft.world.level.block.state.BlockState) ClientLevel(net.minecraft.client.multiplayer.ClientLevel) Supplier(java.util.function.Supplier) Vector3f(com.mojang.math.Vector3f) ArrayList(java.util.ArrayList) I18n(net.minecraft.client.resources.language.I18n) MouseHandler(net.minecraft.client.MouseHandler) AttributeInstance(net.minecraft.world.entity.ai.attributes.AttributeInstance) ModelManager(net.minecraft.client.resources.model.ModelManager) TitleScreen(net.minecraft.client.gui.screens.TitleScreen) Marker(org.apache.logging.log4j.Marker) Nullable(javax.annotation.Nullable) TextureAtlasSprite(net.minecraft.client.renderer.texture.TextureAtlasSprite) BlockAndTintGetter(net.minecraft.world.level.BlockAndTintGetter) ServerData(net.minecraft.client.multiplayer.ServerData) BakedModel(net.minecraft.client.resources.model.BakedModel) Transformation(com.mojang.math.Transformation) ItemTransforms(net.minecraft.client.renderer.block.model.ItemTransforms) Component(net.minecraft.network.chat.Component) MobEffectInstance(net.minecraft.world.effect.MobEffectInstance) TransformationHelper(net.minecraftforge.common.model.TransformationHelper) IOException(java.io.IOException) NarratorChatListener(net.minecraft.client.gui.chat.NarratorChatListener) File(java.io.File) ItemRenderer(net.minecraft.client.renderer.entity.ItemRenderer) LerpingBossEvent(net.minecraft.client.gui.components.LerpingBossEvent) MinecraftForge(net.minecraftforge.common.MinecraftForge) EquipmentSlot(net.minecraft.world.entity.EquipmentSlot) RenderSystem(com.mojang.blaze3d.systems.RenderSystem) ClientTooltipComponent(net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipComponent) InteractionHand(net.minecraft.world.InteractionHand) Material(net.minecraft.client.resources.model.Material) net.minecraftforge.client.event(net.minecraftforge.client.event) ResourceLocation(net.minecraft.resources.ResourceLocation) Either(com.mojang.datafixers.util.Either) LivingEntity(net.minecraft.world.entity.LivingEntity) TextureAtlas(net.minecraft.client.renderer.texture.TextureAtlas) Direction(net.minecraft.core.Direction) Matrix4f(com.mojang.math.Matrix4f) Random(java.util.Random) ServerStatus(net.minecraft.network.protocol.status.ServerStatus) ForgeTextureMetadata(net.minecraftforge.client.textures.ForgeTextureMetadata) GameData(net.minecraftforge.registries.GameData) ChatFormatting(net.minecraft.ChatFormatting) BETA_OUTDATED(net.minecraftforge.fml.VersionChecker.Status.BETA_OUTDATED) FogMode(net.minecraft.client.renderer.FogRenderer.FogMode) Event(net.minecraftforge.eventbus.api.Event) Input(net.minecraft.client.player.Input) ImmutableMap(com.google.common.collect.ImmutableMap) BlockHitResult(net.minecraft.world.phys.BlockHitResult) Matrix3f(com.mojang.math.Matrix3f) Collectors(java.util.stream.Collectors) Player(net.minecraft.world.entity.player.Player) Objects(java.util.Objects) MarkerManager(org.apache.logging.log4j.MarkerManager) List(java.util.List) RecipeManager(net.minecraft.world.item.crafting.RecipeManager) BlockPos(net.minecraft.core.BlockPos) Optional(java.util.Optional) BOSSINFO(net.minecraftforge.client.event.RenderGameOverlayEvent.ElementType.BOSSINFO) Level(net.minecraft.world.level.Level) net.minecraft.client.renderer(net.minecraft.client.renderer) PlayerInfo(net.minecraft.client.multiplayer.PlayerInfo) GuiComponent(net.minecraft.client.gui.GuiComponent) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) SoundEngine(net.minecraft.client.sounds.SoundEngine) PlaySoundEvent(net.minecraftforge.client.event.sound.PlaySoundEvent) Function(java.util.function.Function) Stack(java.util.Stack) ItemColors(net.minecraft.client.color.item.ItemColors) BlockRenderDispatcher(net.minecraft.client.renderer.block.BlockRenderDispatcher) ParticleRenderType(net.minecraft.client.particle.ParticleRenderType) Minecraft(net.minecraft.client.Minecraft) JoinMultiplayerScreen(net.minecraft.client.gui.screens.multiplayer.JoinMultiplayerScreen) Fluid(net.minecraft.world.level.material.Fluid) Mod(net.minecraftforge.fml.common.Mod) HumanoidModel(net.minecraft.client.model.HumanoidModel) NativeImage(com.mojang.blaze3d.platform.NativeImage) Nonnull(javax.annotation.Nonnull) EntityHitResult(net.minecraft.world.phys.EntityHitResult) NetworkConstants(net.minecraftforge.network.NetworkConstants) LayerDefinition(net.minecraft.client.model.geom.builders.LayerDefinition) LocalPlayer(net.minecraft.client.player.LocalPlayer) ModelLayerLocation(net.minecraft.client.model.geom.ModelLayerLocation) HitResult(net.minecraft.world.phys.HitResult) Entity(net.minecraft.world.entity.Entity) Model(net.minecraft.client.model.Model) BlockColors(net.minecraft.client.color.block.BlockColors) KeyMapping(net.minecraft.client.KeyMapping) Comparator(java.util.Comparator) LogManager(org.apache.logging.log4j.LogManager) ForgeModelBakery(net.minecraftforge.client.model.ForgeModelBakery) FormattedText(net.minecraft.network.chat.FormattedText) ClientTooltipComponent(net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipComponent) Font(net.minecraft.client.gui.Font) Function(java.util.function.Function) Either(com.mojang.datafixers.util.Either) TooltipComponent(net.minecraft.world.inventory.tooltip.TooltipComponent) ClientTooltipComponent(net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipComponent) TooltipComponent(net.minecraft.world.inventory.tooltip.TooltipComponent) TranslatableComponent(net.minecraft.network.chat.TranslatableComponent) TextComponent(net.minecraft.network.chat.TextComponent) Component(net.minecraft.network.chat.Component) ClientTooltipComponent(net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipComponent) GuiComponent(net.minecraft.client.gui.GuiComponent)

Aggregations

Component (net.minecraft.network.chat.Component)12 TextComponent (net.minecraft.network.chat.TextComponent)5 ItemStack (net.minecraft.world.item.ItemStack)4 GameProfile (com.mojang.authlib.GameProfile)2 TagCompound (de.keyle.knbt.TagCompound)2 TagList (de.keyle.knbt.TagList)2 TagShort (de.keyle.knbt.TagShort)2 TagString (de.keyle.knbt.TagString)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Minecraft (net.minecraft.client.Minecraft)2 TranslatableComponent (net.minecraft.network.chat.TranslatableComponent)2 ClientboundPlayerInfoPacket (net.minecraft.network.protocol.game.ClientboundPlayerInfoPacket)2 Player (net.minecraft.world.entity.player.Player)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 NativeImage (com.mojang.blaze3d.platform.NativeImage)1 Window (com.mojang.blaze3d.platform.Window)1 RenderSystem (com.mojang.blaze3d.systems.RenderSystem)1 com.mojang.blaze3d.vertex (com.mojang.blaze3d.vertex)1 Either (com.mojang.datafixers.util.Either)1 Matrix3f (com.mojang.math.Matrix3f)1