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;
}
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);
}
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;
}
}
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;
}
}
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();
}
Aggregations