Skip to content

Commit

Permalink
Merge branch 'master' into feature/1.21.50
Browse files Browse the repository at this point in the history
  • Loading branch information
onebeastchris authored Dec 3, 2024
2 parents 1cb51b5 + d956354 commit 0df3bf2
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 1 deletion.
13 changes: 13 additions & 0 deletions core/src/main/java/org/geysermc/geyser/item/type/PotionItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
import org.geysermc.geyser.registry.type.ItemMapping;
import org.geysermc.geyser.registry.type.ItemMappings;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.translator.item.BedrockItemBuilder;
import org.geysermc.geyser.translator.item.CustomItemTranslator;
import org.geysermc.geyser.translator.item.ItemTranslator;
import org.geysermc.mcprotocollib.protocol.data.game.item.component.DataComponentType;
import org.geysermc.mcprotocollib.protocol.data.game.item.component.DataComponents;
import org.geysermc.mcprotocollib.protocol.data.game.item.component.PotionContents;
Expand Down Expand Up @@ -68,6 +70,17 @@ public ItemData.Builder translateToBedrock(GeyserSession session, int count, Dat
return super.translateToBedrock(session, count, components, mapping, mappings);
}

@Override
public void translateComponentsToBedrock(@NonNull GeyserSession session, @NonNull DataComponents components, @NonNull BedrockItemBuilder builder) {
// Make custom effect information visible
PotionContents potionContents = components.get(DataComponentType.POTION_CONTENTS);
if (potionContents != null) {
ItemTranslator.addPotionEffectLore(potionContents, builder, session.locale());
}

super.translateComponentsToBedrock(session, components, builder);
}

@Override
public @NonNull GeyserItemStack translateToJava(GeyserSession session, @NonNull ItemData itemData, @NonNull ItemMapping mapping, @NonNull ItemMappings mappings) {
Potion potion = Potion.getByBedrockId(itemData.getDamage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@

package org.geysermc.geyser.item.type;

import org.checkerframework.checker.nullness.qual.NonNull;
import org.cloudburstmc.protocol.bedrock.data.inventory.ItemData;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.inventory.item.Potion;
import org.geysermc.geyser.registry.type.ItemMapping;
import org.geysermc.geyser.registry.type.ItemMappings;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.translator.item.BedrockItemBuilder;
import org.geysermc.geyser.translator.item.ItemTranslator;
import org.geysermc.mcprotocollib.protocol.data.game.item.component.DataComponentType;
import org.geysermc.mcprotocollib.protocol.data.game.item.component.DataComponents;
import org.geysermc.mcprotocollib.protocol.data.game.item.component.PotionContents;
Expand All @@ -57,4 +60,15 @@ public ItemData.Builder translateToBedrock(GeyserSession session, int count, Dat
}
return super.translateToBedrock(session, count, components, mapping, mappings);
}

@Override
public void translateComponentsToBedrock(@NonNull GeyserSession session, @NonNull DataComponents components, @NonNull BedrockItemBuilder builder) {
// Make custom effect information visible
PotionContents potionContents = components.get(DataComponentType.POTION_CONTENTS);
if (potionContents != null) {
ItemTranslator.addPotionEffectLore(potionContents, builder, session.locale());
}

super.translateComponentsToBedrock(session, components, builder);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
package org.geysermc.geyser.translator.item;

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TranslatableComponent;
import net.kyori.adventure.text.format.NamedTextColor;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
Expand Down Expand Up @@ -55,6 +56,7 @@
import org.geysermc.mcprotocollib.auth.GameProfile;
import org.geysermc.mcprotocollib.auth.GameProfile.Texture;
import org.geysermc.mcprotocollib.auth.GameProfile.TextureType;
import org.geysermc.mcprotocollib.protocol.data.game.entity.Effect;
import org.geysermc.mcprotocollib.protocol.data.game.entity.attribute.AttributeType;
import org.geysermc.mcprotocollib.protocol.data.game.entity.attribute.ModifierOperation;
import org.geysermc.mcprotocollib.protocol.data.game.item.ItemStack;
Expand All @@ -63,12 +65,16 @@
import org.geysermc.mcprotocollib.protocol.data.game.item.component.DataComponents;
import org.geysermc.mcprotocollib.protocol.data.game.item.component.HolderSet;
import org.geysermc.mcprotocollib.protocol.data.game.item.component.ItemAttributeModifiers;
import org.geysermc.mcprotocollib.protocol.data.game.item.component.MobEffectDetails;
import org.geysermc.mcprotocollib.protocol.data.game.item.component.MobEffectInstance;
import org.geysermc.mcprotocollib.protocol.data.game.item.component.PotionContents;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

public final class ItemTranslator {
Expand Down Expand Up @@ -324,6 +330,65 @@ private static String attributeToLore(int attribute, ItemAttributeModifiers.Attr
return MessageTranslator.convertMessage(attributeComponent, language);
}

private static final List<Effect> negativeEffectList = List.of(
Effect.SLOWNESS,
Effect.MINING_FATIGUE,
Effect.INSTANT_DAMAGE,
Effect.NAUSEA,
Effect.BLINDNESS,
Effect.HUNGER,
Effect.WEAKNESS,
Effect.POISON,
Effect.WITHER,
Effect.LEVITATION,
Effect.UNLUCK,
Effect.DARKNESS,
Effect.WIND_CHARGED,
Effect.WEAVING,
Effect.OOZING,
Effect.INFESTED
);

public static void addPotionEffectLore(PotionContents contents, BedrockItemBuilder builder, String language) {
List<MobEffectInstance> effectInstanceList = contents.getCustomEffects();
for (MobEffectInstance effectInstance : effectInstanceList) {
Effect effect = effectInstance.getEffect();
MobEffectDetails details = effectInstance.getDetails();
int amplifier = details.getAmplifier();
int durations = details.getDuration();
TranslatableComponent appendTranslatable = Component.translatable("effect.minecraft." + effect.toString().toLowerCase(Locale.ROOT));
if (amplifier != 0) {
appendTranslatable = Component.translatable("potion.withAmplifier",
appendTranslatable,
Component.translatable("potion.potency." + amplifier));
}
if (durations > 20) {
int seconds = durations / 20;
int secondsFormat = seconds % 60;
int minutes = seconds / 60;
int minutesFormat = minutes % 60;
int hours = minutes / 60;
String text = ((minutesFormat > 9) ? "" : "0") + minutesFormat + ":" + ((secondsFormat > 9) ? "" : "0") + secondsFormat;
if (minutes >= 60) {
text = ((hours > 9) ? "" : "0") + hours + ":" + text;
}
appendTranslatable = Component.translatable("potion.withDuration",
appendTranslatable,
Component.text(text));
} else if (durations == -1) {
appendTranslatable = Component.translatable("potion.withDuration",
appendTranslatable,
Component.translatable("effect.duration.infinite"));
}
Component component = Component.text()
.resetStyle()
.color((negativeEffectList.contains(effect)) ? NamedTextColor.RED : NamedTextColor.BLUE)
.append(appendTranslatable)
.build();
builder.getOrCreateLore().add(MessageTranslator.convertMessage(component, language));
}
}

private static void addAdvancedTooltips(@Nullable DataComponents components, BedrockItemBuilder builder, Item item, String language) {
int maxDurability = item.maxDamage();

Expand Down Expand Up @@ -448,6 +513,19 @@ public static String getCustomName(GeyserSession session, DataComponents compone
if (customName != null) {
return MessageTranslator.convertMessage(customName, session.locale());
}
PotionContents potionContents = components.get(DataComponentType.POTION_CONTENTS);
if (potionContents != null) {
// "custom_name" tag in "potion_contents" component
String customPotionName = potionContents.getCustomName();
if (customPotionName != null) {
Component component = Component.text()
.resetStyle()
.color(NamedTextColor.WHITE)
.append(Component.translatable(mapping.getJavaItem().translationKey() + ".effect." + customPotionName))
.build();
return MessageTranslator.convertMessage(component, session.locale());
}
}
customName = components.get(DataComponentType.ITEM_NAME);
if (customName != null) {
// Get the translated name and prefix it with a reset char to prevent italics - matches Java Edition
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ viaversion = "4.9.2"
adapters = "1.15-SNAPSHOT"
cloud = "2.0.0-rc.2"
cloud-minecraft = "2.0.0-beta.9"
cloud-minecraft-modded = "2.0.0-beta.9"
cloud-minecraft-modded = "2.0.0-beta.10"
commodore = "2.2"
bungeecord = "a7c6ede"
velocity = "3.3.0-SNAPSHOT"
Expand Down

0 comments on commit 0df3bf2

Please # to comment.