From 3129084f71cb10317e99c9211ab1ec97e9f7c6fc Mon Sep 17 00:00:00 2001 From: Sollace Date: Thu, 31 Jan 2019 15:57:50 +0200 Subject: [PATCH] Added a potion effect and death message for food poisoning --- .../com/minelittlepony/unicopia/UBlocks.java | 2 +- .../com/minelittlepony/unicopia/UEffects.java | 37 +++++++ .../com/minelittlepony/unicopia/UItems.java | 4 +- .../com/minelittlepony/unicopia/Unicopia.java | 12 ++- .../edibles/BushToxicityDeterminent.java | 6 +- .../edibles/FlowerToxicityDeterminent.java | 4 +- .../unicopia/edibles/ItemEdible.java | 16 +-- .../unicopia/edibles/Toxicity.java | 8 ++ .../unicopia/potion/UPotion.java | 100 ++++++++++++++++++ .../resources/assets/unicopia/lang/en_US.lang | 1 + 10 files changed, 167 insertions(+), 23 deletions(-) create mode 100644 src/main/java/com/minelittlepony/unicopia/UEffects.java create mode 100644 src/main/java/com/minelittlepony/unicopia/potion/UPotion.java diff --git a/src/main/java/com/minelittlepony/unicopia/UBlocks.java b/src/main/java/com/minelittlepony/unicopia/UBlocks.java index 3bb780c7..715f651e 100644 --- a/src/main/java/com/minelittlepony/unicopia/UBlocks.java +++ b/src/main/java/com/minelittlepony/unicopia/UBlocks.java @@ -55,7 +55,7 @@ public class UBlocks { .setHarvestFruit(w -> UItems.apple.getRandomApple(w.rand, null)) .setUnharvestFruit(w -> new ItemStack(UItems.rotten_apple)); - static void registerBlocks(IForgeRegistry registry) { + static void init(IForgeRegistry registry) { registry.registerAll(cloud, cloud_stairs, cloud_double_slab, cloud_slab, mist_door, anvil, cloud_farmland, sugar_block, alfalfa, diff --git a/src/main/java/com/minelittlepony/unicopia/UEffects.java b/src/main/java/com/minelittlepony/unicopia/UEffects.java new file mode 100644 index 00000000..1d9cc170 --- /dev/null +++ b/src/main/java/com/minelittlepony/unicopia/UEffects.java @@ -0,0 +1,37 @@ +package com.minelittlepony.unicopia; + +import com.minelittlepony.unicopia.potion.UPotion; + +import net.minecraft.init.MobEffects; +import net.minecraft.potion.Potion; +import net.minecraft.potion.PotionEffect; +import net.minecraft.util.DamageSource; +import net.minecraftforge.registries.IForgeRegistry; + +public class UEffects { + + public static final DamageSource food_poisoning = new DamageSource("food_poisoning").setDamageBypassesArmor(); + + public static final Potion FOOD_POISONING = new UPotion(Unicopia.MODID, "food_poisoning", true, 3484199) + .setIconIndex(3, 1) + .setSilent() + .setEffectiveness(0.25) + .setApplicator((p, e, i) -> { + + PotionEffect nausea = e.getActivePotionEffect(MobEffects.NAUSEA); + if (nausea == null) { + PotionEffect foodEffect = e.getActivePotionEffect(p); + nausea = new PotionEffect(MobEffects.NAUSEA, foodEffect.getDuration(), foodEffect.getAmplifier(), foodEffect.getIsAmbient(), foodEffect.doesShowParticles()); + + e.addPotionEffect(nausea); + } + + e.attackEntityFrom(food_poisoning, i); + }); + + static void init(IForgeRegistry registry) { + registry.registerAll( + FOOD_POISONING + ); + } +} diff --git a/src/main/java/com/minelittlepony/unicopia/UItems.java b/src/main/java/com/minelittlepony/unicopia/UItems.java index 635263e3..995dbec5 100644 --- a/src/main/java/com/minelittlepony/unicopia/UItems.java +++ b/src/main/java/com/minelittlepony/unicopia/UItems.java @@ -170,7 +170,7 @@ public class UItems { .setUseAction(EnumAction.DRINK) .setContainerItem(Items.GLASS_BOTTLE); - static void registerItems(IForgeRegistry registry) { + static void init(IForgeRegistry registry) { RegistryLockSpinner.open(Item.REGISTRY, Items.class, r -> r .replace(Items.APPLE, apple) .replace(Items.STICK, stick) @@ -237,7 +237,7 @@ public class UItems { } @FUF(reason = "There is no way to register custom recipe types that support nbt data. Waiting for mixins...") - static void registerRecipes(IForgeRegistry registry) { + static void initRecipes(IForgeRegistry registry) { Ingredient dewdrop = Ingredient.fromItem(dew_drop); Ingredient cloud = Ingredient.fromStacks(new ItemStack(cloud_block, 1, 0)); diff --git a/src/main/java/com/minelittlepony/unicopia/Unicopia.java b/src/main/java/com/minelittlepony/unicopia/Unicopia.java index 82f2acb2..12975abb 100644 --- a/src/main/java/com/minelittlepony/unicopia/Unicopia.java +++ b/src/main/java/com/minelittlepony/unicopia/Unicopia.java @@ -10,6 +10,7 @@ import net.minecraft.item.EnumAction; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.item.crafting.IRecipe; +import net.minecraft.potion.Potion; import net.minecraft.util.SoundEvent; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; @@ -121,17 +122,22 @@ public class Unicopia implements IGuiHandler { @SubscribeEvent public static void registerItems(RegistryEvent.Register event) { - UItems.registerItems(event.getRegistry()); + UItems.init(event.getRegistry()); } @SubscribeEvent public static void registerBlocks(RegistryEvent.Register event) { - UBlocks.registerBlocks(event.getRegistry()); + UBlocks.init(event.getRegistry()); } @SubscribeEvent public static void registerRecipes(RegistryEvent.Register event) { - UItems.registerRecipes(event.getRegistry()); + UItems.initRecipes(event.getRegistry()); + } + + @SubscribeEvent + public static void registerPotions(RegistryEvent.Register event) { + UEffects.init(event.getRegistry()); } @SubscribeEvent diff --git a/src/main/java/com/minelittlepony/unicopia/edibles/BushToxicityDeterminent.java b/src/main/java/com/minelittlepony/unicopia/edibles/BushToxicityDeterminent.java index 56c064a9..fabe6175 100644 --- a/src/main/java/com/minelittlepony/unicopia/edibles/BushToxicityDeterminent.java +++ b/src/main/java/com/minelittlepony/unicopia/edibles/BushToxicityDeterminent.java @@ -33,15 +33,15 @@ public class BushToxicityDeterminent implements IEdible { if ((type == ROSE || type == FERN) && player.world.rand.nextInt(30) == 0) { - player.addPotionEffect(new PotionEffect(MobEffects.INSTANT_DAMAGE, 1, 1)); + player.addPotionEffect(new PotionEffect(MobEffects.INSTANT_DAMAGE, 1, 1, false, false)); } if (type == GRASS) { - player.addPotionEffect(new PotionEffect(MobEffects.NAUSEA, 30, 1)); + player.addPotionEffect(new PotionEffect(MobEffects.NAUSEA, 30, 1, false, false)); } if (type == FERN) { - player.addPotionEffect(new PotionEffect(MobEffects.STRENGTH, 30, 1)); + player.addPotionEffect(new PotionEffect(MobEffects.STRENGTH, 30, 1, false, false)); } } } diff --git a/src/main/java/com/minelittlepony/unicopia/edibles/FlowerToxicityDeterminent.java b/src/main/java/com/minelittlepony/unicopia/edibles/FlowerToxicityDeterminent.java index e191f42a..d6d8c0c6 100644 --- a/src/main/java/com/minelittlepony/unicopia/edibles/FlowerToxicityDeterminent.java +++ b/src/main/java/com/minelittlepony/unicopia/edibles/FlowerToxicityDeterminent.java @@ -42,11 +42,11 @@ public class FlowerToxicityDeterminent implements IEdible { BlockFlower.EnumFlowerType type = getType(stack); if (type == HOUSTONIA && player.world.rand.nextInt(30) == 0) { - player.addPotionEffect(new PotionEffect(MobEffects.GLOWING, 10, 1)); + player.addPotionEffect(new PotionEffect(MobEffects.GLOWING, 10, 1, false, false)); } if (type == OXEYE_DAISY) { - player.addPotionEffect(new PotionEffect(MobEffects.BLINDNESS, 30, 1)); + player.addPotionEffect(new PotionEffect(MobEffects.BLINDNESS, 30, 1, false, false)); } } } diff --git a/src/main/java/com/minelittlepony/unicopia/edibles/ItemEdible.java b/src/main/java/com/minelittlepony/unicopia/edibles/ItemEdible.java index e0148872..b390b978 100644 --- a/src/main/java/com/minelittlepony/unicopia/edibles/ItemEdible.java +++ b/src/main/java/com/minelittlepony/unicopia/edibles/ItemEdible.java @@ -6,11 +6,11 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; import com.minelittlepony.unicopia.Race; +import com.minelittlepony.unicopia.UEffects; import com.minelittlepony.unicopia.forgebullshit.IMultiItem; import com.minelittlepony.unicopia.player.PlayerSpeciesList; import net.minecraft.advancements.CriteriaTriggers; -import net.minecraft.client.resources.I18n; import net.minecraft.client.util.ITooltipFlag; import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.player.EntityPlayer; @@ -27,7 +27,6 @@ import net.minecraft.util.ActionResult; import net.minecraft.util.EnumActionResult; import net.minecraft.util.EnumHand; import net.minecraft.util.SoundCategory; -import net.minecraft.util.text.TextFormatting; import net.minecraft.world.World; public class ItemEdible extends ItemFood implements IEdible, IMultiItem { @@ -78,11 +77,7 @@ public class ItemEdible extends ItemFood implements IEdible, IMultiItem { @Override public void addInformation(ItemStack stack, @Nullable World worldIn, List tooltip, ITooltipFlag flagIn) { - Toxicity toxicity = getToxicityLevel(stack); - - TextFormatting color = toxicity.toxicWhenCooked() ? TextFormatting.RED : toxicity.toxicWhenRaw() ? TextFormatting.DARK_PURPLE : TextFormatting.GRAY; - - tooltip.add(color + I18n.format(toxicity.getTranslationKey())); + tooltip.add(getToxicityLevel(stack).getTooltip()); } @Override @@ -103,8 +98,6 @@ public class ItemEdible extends ItemFood implements IEdible, IMultiItem { if (entityplayer instanceof EntityPlayerMP) { CriteriaTriggers.CONSUME_ITEM.trigger((EntityPlayerMP)entityplayer, stack); } - - } if (entityplayer == null || !entityplayer.capabilities.isCreativeMode) { @@ -143,10 +136,9 @@ public class ItemEdible extends ItemFood implements IEdible, IMultiItem { } if (toxicity.isLethal()) { - player.addPotionEffect(new PotionEffect(MobEffects.NAUSEA, 300, 7)); - player.addPotionEffect(new PotionEffect(MobEffects.WITHER, 300, 7)); + player.addPotionEffect(new PotionEffect(UEffects.FOOD_POISONING, 300, 7, false, false)); } else if (toxicity.toxicWhenCooked()) { - player.addPotionEffect(new PotionEffect(MobEffects.NAUSEA, 3, 1)); + player.addPotionEffect(new PotionEffect(MobEffects.NAUSEA, 3, 1, false, false)); } toxicityDeterminant.addSecondaryEffects(player, toxicity, stack); diff --git a/src/main/java/com/minelittlepony/unicopia/edibles/Toxicity.java b/src/main/java/com/minelittlepony/unicopia/edibles/Toxicity.java index 24dafade..d256b582 100644 --- a/src/main/java/com/minelittlepony/unicopia/edibles/Toxicity.java +++ b/src/main/java/com/minelittlepony/unicopia/edibles/Toxicity.java @@ -1,7 +1,9 @@ package com.minelittlepony.unicopia.edibles; +import net.minecraft.client.resources.I18n; import net.minecraft.init.MobEffects; import net.minecraft.potion.PotionEffect; +import net.minecraft.util.text.TextFormatting; public enum Toxicity { SAFE(0, 0), @@ -44,6 +46,12 @@ public enum Toxicity { return String.format("toxicity.%s.name", name().toLowerCase()); } + public String getTooltip() { + TextFormatting color = toxicWhenCooked() ? TextFormatting.RED : toxicWhenRaw() ? TextFormatting.DARK_PURPLE : TextFormatting.GRAY; + + return color + I18n.format(getTranslationKey()); + } + public static Toxicity byMetadata(int metadata) { return values[metadata % values.length]; } diff --git a/src/main/java/com/minelittlepony/unicopia/potion/UPotion.java b/src/main/java/com/minelittlepony/unicopia/potion/UPotion.java new file mode 100644 index 00000000..c899fedb --- /dev/null +++ b/src/main/java/com/minelittlepony/unicopia/potion/UPotion.java @@ -0,0 +1,100 @@ +package com.minelittlepony.unicopia.potion; + +import javax.annotation.Nonnull; + +import net.minecraft.entity.EntityLivingBase; +import net.minecraft.potion.Potion; +import net.minecraft.potion.PotionEffect; + +public class UPotion extends Potion { + + private boolean isSilent; + private int tickDelay = 40; + + @Nonnull + private IEffectApplicator applicator = IEffectApplicator.NONE; + + public UPotion(String domain, String name, boolean isNegative, int tint) { + super(isNegative, tint); + + setRegistryName(domain, name); + setPotionName("effect." + name); + } + + public UPotion setSilent() { + isSilent = true; + + return this; + } + + public UPotion setApplicator(@Nonnull IEffectApplicator applicator) { + this.applicator = applicator; + + return this; + } + + public UPotion setTickDelay(int delay) { + tickDelay = delay; + + return this; + } + + @Override + public UPotion setIconIndex(int u, int v) { + super.setIconIndex(u, v); + + return this; + } + + @Override + public UPotion setEffectiveness(double effectiveness) { + super.setEffectiveness(effectiveness); + + return this; + } + + @Override + public boolean shouldRender(PotionEffect effect) { + return isSilent; + } + + @Override + public boolean shouldRenderInvText(PotionEffect effect) { + return isSilent; + } + + @Override + public boolean shouldRenderHUD(PotionEffect effect) { + return isSilent; + } + + @Override + public void performEffect(EntityLivingBase entity, int amplifier) { + applicator.performEffect(this, entity, amplifier); + } + + @Override + public boolean isInstant() { + return tickDelay > 0; + } + + @Override + public boolean isReady(int duration, int amplifier) { + if (!isInstant()) { + int i = tickDelay >> amplifier; + + if (i > 0) { + return duration % i == 0; + } + } + + return true; + } + + @FunctionalInterface + public interface IEffectApplicator { + IEffectApplicator NONE = (p, e, i) -> {}; + + void performEffect(Potion effect, EntityLivingBase target, int amplifier); + } +} diff --git a/src/main/resources/assets/unicopia/lang/en_US.lang b/src/main/resources/assets/unicopia/lang/en_US.lang index 5c57afc5..06d5f8af 100644 --- a/src/main/resources/assets/unicopia/lang/en_US.lang +++ b/src/main/resources/assets/unicopia/lang/en_US.lang @@ -194,6 +194,7 @@ death.attack.fire.player=%1$s was burnt to a crisp by %2$s death.attack.fire.own=%1$s was burnt to a crisp by their own spell death.attack.zap=%1$s ate a Zap Apple death.attack.paradox=%1$s imploded +death.attack.food_poisoning=%1%s died of food poisoning advancements.adventure.bag_of_holding.title=Read the Manual advancements.adventure.bag_of_holding.description=Successfuly die using the Bag of Holding