diff --git a/src/main/java/com/chikoritalover/caffeinated/item/CoffeeBottleItem.java b/src/main/java/com/chikoritalover/caffeinated/item/CoffeeBottleItem.java index 5be2add..3934e3d 100644 --- a/src/main/java/com/chikoritalover/caffeinated/item/CoffeeBottleItem.java +++ b/src/main/java/com/chikoritalover/caffeinated/item/CoffeeBottleItem.java @@ -2,29 +2,37 @@ import com.chikoritalover.caffeinated.Caffeinated; import com.chikoritalover.caffeinated.registry.CaffeinatedSoundEvents; +import com.chikoritalover.caffeinated.registry.CaffeinatedStatusEffects; import com.mojang.datafixers.util.Pair; import net.minecraft.advancement.criterion.Criteria; import net.minecraft.client.item.TooltipContext; import net.minecraft.entity.LivingEntity; +import net.minecraft.entity.attribute.EntityAttribute; +import net.minecraft.entity.attribute.EntityAttributeModifier; +import net.minecraft.entity.effect.StatusEffect; import net.minecraft.entity.effect.StatusEffectInstance; +import net.minecraft.entity.effect.StatusEffectUtil; import net.minecraft.entity.effect.StatusEffects; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.item.ItemUsage; import net.minecraft.item.Items; -import net.minecraft.potion.PotionUtil; import net.minecraft.registry.Registries; +import net.minecraft.screen.ScreenTexts; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.sound.SoundEvent; import net.minecraft.stat.Stats; +import net.minecraft.text.MutableText; import net.minecraft.text.Text; import net.minecraft.util.*; import net.minecraft.world.World; import org.apache.commons.compress.utils.Lists; import org.jetbrains.annotations.Nullable; +import java.util.ArrayList; import java.util.List; +import java.util.Map; public class CoffeeBottleItem extends Item { private static final int MAX_USE_TIME = 40; @@ -90,10 +98,59 @@ public void appendTooltip(ItemStack stack, @Nullable World world, List too list.add(statusEffect.getFirst()); } if (!list.isEmpty()) { - PotionUtil.buildTooltip(list, tooltip, 1.0F); + buildTooltip(list, tooltip, 1.0F); } } + private static void buildTooltip(List statusEffects, List list, float durationMultiplier) { + ArrayList> list2 = Lists.newArrayList(); + int i = 0; + for (StatusEffectInstance statusEffectInstance : statusEffects) { + MutableText mutableText = Text.translatable(statusEffectInstance.getTranslationKey()); + StatusEffect statusEffect = statusEffectInstance.getEffectType(); + Map map = statusEffect.getAttributeModifiers(); + if (!map.isEmpty()) { + for (Map.Entry entry : map.entrySet()) { + EntityAttributeModifier entityAttributeModifier = entry.getValue(); + EntityAttributeModifier entityAttributeModifier2 = new EntityAttributeModifier(entityAttributeModifier.getName(), statusEffect.adjustModifierAmount(statusEffectInstance.getAmplifier(), entityAttributeModifier), entityAttributeModifier.getOperation()); + list2.add(new Pair<>(entry.getKey(), entityAttributeModifier2)); + } + } + if (statusEffectInstance.getAmplifier() > 0) { + mutableText = Text.translatable("potion.withAmplifier", mutableText, Text.translatable("potion.potency." + statusEffectInstance.getAmplifier())); + if (statusEffectInstance.getEffectType() == CaffeinatedStatusEffects.CAFFEINE) { + i = statusEffectInstance.getAmplifier(); + } + } + if (!statusEffectInstance.isDurationBelow(20)) { + mutableText = Text.translatable("potion.withDuration", mutableText, StatusEffectUtil.getDurationText(statusEffectInstance, durationMultiplier)); + } + list.add(mutableText.formatted(statusEffect.getCategory().getFormatting())); + } + list.add(ScreenTexts.EMPTY); + list.add(Text.translatable("potion.whenDrank").formatted(Formatting.DARK_PURPLE)); + list.add(Text.translatable("attribute.modifier.take.2", ItemStack.MODIFIER_FORMAT.format(getEffectChance(i) * 100.0F), Text.translatable("item.modifiers.exhaustion")).formatted(Formatting.BLUE)); + for (Pair pair : list2) { + EntityAttributeModifier entityAttributeModifier3 = pair.getSecond(); + double d = entityAttributeModifier3.getValue(); + double e = entityAttributeModifier3.getOperation() == EntityAttributeModifier.Operation.MULTIPLY_BASE || entityAttributeModifier3.getOperation() == EntityAttributeModifier.Operation.MULTIPLY_TOTAL ? entityAttributeModifier3.getValue() * 100.0 : entityAttributeModifier3.getValue(); + if (d > 0.0) { + list.add(Text.translatable("attribute.modifier.plus." + entityAttributeModifier3.getOperation().getId(), ItemStack.MODIFIER_FORMAT.format(e), Text.translatable(pair.getFirst().getTranslationKey())).formatted(Formatting.BLUE)); + continue; + } + if (!(d < 0.0)) continue; + list.add(Text.translatable("attribute.modifier.take." + entityAttributeModifier3.getOperation().getId(), ItemStack.MODIFIER_FORMAT.format(-e), Text.translatable(pair.getFirst().getTranslationKey())).formatted(Formatting.RED)); + } + } + + public static float getEffectChance(StatusEffectInstance statusEffectInstance) { + return getEffectChance(statusEffectInstance.getAmplifier()); + } + + public static float getEffectChance(int i) { + return (i + 1) * 0.2F; + } + @Override public String getTranslationKey() { return TRANSLATION_KEY; diff --git a/src/main/java/com/chikoritalover/caffeinated/mixin/PlayerEntityMixin.java b/src/main/java/com/chikoritalover/caffeinated/mixin/PlayerEntityMixin.java index 9d3438d..2760706 100644 --- a/src/main/java/com/chikoritalover/caffeinated/mixin/PlayerEntityMixin.java +++ b/src/main/java/com/chikoritalover/caffeinated/mixin/PlayerEntityMixin.java @@ -1,20 +1,28 @@ package com.chikoritalover.caffeinated.mixin; +import com.chikoritalover.caffeinated.item.CoffeeBottleItem; import com.chikoritalover.caffeinated.registry.CaffeinatedStatusEffects; +import net.minecraft.entity.EntityType; +import net.minecraft.entity.LivingEntity; +import net.minecraft.entity.effect.StatusEffectInstance; import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.world.World; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @Mixin(PlayerEntity.class) -public class PlayerEntityMixin { +public abstract class PlayerEntityMixin extends LivingEntity { + protected PlayerEntityMixin(EntityType entityType, World world) { + super(entityType, world); + } + @Inject(method = "addExhaustion", at = @At(value = "HEAD"), cancellable = true) private void addExhaustion(float exhaustion, CallbackInfo ci) { - PlayerEntity player = PlayerEntity.class.cast(this); - if (player.hasStatusEffect(CaffeinatedStatusEffects.CAFFEINE)) { - int amplifier = player.getStatusEffect(CaffeinatedStatusEffects.CAFFEINE).getAmplifier(); - if (player.getRandom().nextBetween(0, amplifier + 2) > 1) { + if (this.hasStatusEffect(CaffeinatedStatusEffects.CAFFEINE)) { + StatusEffectInstance statusEffectInstance = this.getStatusEffect(CaffeinatedStatusEffects.CAFFEINE); + if (this.getRandom().nextFloat() < CoffeeBottleItem.getEffectChance(statusEffectInstance)) { ci.cancel(); } } diff --git a/src/main/java/com/chikoritalover/caffeinated/registry/CaffeinatedStatusEffects.java b/src/main/java/com/chikoritalover/caffeinated/registry/CaffeinatedStatusEffects.java index c019415..743eb86 100644 --- a/src/main/java/com/chikoritalover/caffeinated/registry/CaffeinatedStatusEffects.java +++ b/src/main/java/com/chikoritalover/caffeinated/registry/CaffeinatedStatusEffects.java @@ -9,10 +9,10 @@ import net.minecraft.util.Identifier; public class CaffeinatedStatusEffects { - public static final StatusEffect CAFFEINE = register("caffeine", StatusEffectCategory.BENEFICIAL, 4927777); + public static final StatusEffect CAFFEINE = register("caffeine", new CaffeineStatusEffect(StatusEffectCategory.BENEFICIAL, 0x4B3121)); - private static StatusEffect register(String id, StatusEffectCategory category, int i){ - return Registry.register(Registries.STATUS_EFFECT, new Identifier(Caffeinated.MODID, id), new CaffeineStatusEffect(category, i)); + private static StatusEffect register(String id, StatusEffect effect) { + return Registry.register(Registries.STATUS_EFFECT, new Identifier(Caffeinated.MODID, id), effect); } public static void register() { diff --git a/src/main/resources/assets/caffeinated/lang/en_us.json b/src/main/resources/assets/caffeinated/lang/en_us.json index 3fdeb39..630e6cd 100644 --- a/src/main/resources/assets/caffeinated/lang/en_us.json +++ b/src/main/resources/assets/caffeinated/lang/en_us.json @@ -47,6 +47,8 @@ "item.caffeinated.latte_coffee_bottle.desc": "Latte", "item.caffeinated.tiramisu_slice": "Tiramisu Slice", + "item.modifiers.exhaustion": "Exhaustion", + "itemGroup.caffeinated.caffeinated": "Caffeinated", "subtitles.block.cauldron_campfire.brew": "Cauldron brews",