From ae89d0f18d26ecd99141986a262c1b5a8dcb81ad Mon Sep 17 00:00:00 2001 From: Vincent Lee Date: Mon, 29 Feb 2016 18:56:08 -0600 Subject: [PATCH] Shim to avoid potion idmap shifting on startup. Closes #148 --- .../java/vazkii/botania/api/brew/Brew.java | 18 ++++---- .../botania/api/brew/PotionEffectShim.java | 26 +++++++++++ .../vazkii/botania/common/brew/BrewMod.java | 4 +- .../botania/common/brew/BrewModPotion.java | 5 ++- .../vazkii/botania/common/brew/ModBrews.java | 43 ++++++++++--------- 5 files changed, 63 insertions(+), 33 deletions(-) create mode 100644 src/main/java/vazkii/botania/api/brew/PotionEffectShim.java diff --git a/src/main/java/vazkii/botania/api/brew/Brew.java b/src/main/java/vazkii/botania/api/brew/Brew.java index 4da6fc655..9e1d92ff6 100644 --- a/src/main/java/vazkii/botania/api/brew/Brew.java +++ b/src/main/java/vazkii/botania/api/brew/Brew.java @@ -13,7 +13,9 @@ package vazkii.botania.api.brew; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.stream.Collectors; +import com.google.common.collect.ImmutableList; import net.minecraft.item.ItemStack; import net.minecraft.potion.PotionEffect; @@ -22,11 +24,11 @@ import net.minecraft.potion.PotionEffect; */ public class Brew { - String key; - String name; - int color; - int cost; - List effects; + private final String key; + private final String name; + private final int color; + private final int cost; + private final List effects; boolean canInfuseBloodPendant = true; boolean canInfuseIncense = true; @@ -37,12 +39,12 @@ public class Brew { * @param cost The cost, in Mana for this brew. * @param effects A list of effects to apply to the player when they drink it. */ - public Brew(String key, String name, int color, int cost, PotionEffect... effects) { + public Brew(String key, String name, int color, int cost, PotionEffectShim... effects) { this.key = key; this.name = name; this.color = color; this.cost = cost; - this.effects = new ArrayList<>(Arrays.asList(effects)); + this.effects = ImmutableList.copyOf(effects); } /** @@ -120,7 +122,7 @@ public class Brew { * Vial or an Alfglass Flask at all times. */ public List getPotionEffects(ItemStack stack) { - return effects; + return effects.stream().map(PotionEffectShim::toVanilla).collect(Collectors.toList()); } } diff --git a/src/main/java/vazkii/botania/api/brew/PotionEffectShim.java b/src/main/java/vazkii/botania/api/brew/PotionEffectShim.java new file mode 100644 index 000000000..6235cad3c --- /dev/null +++ b/src/main/java/vazkii/botania/api/brew/PotionEffectShim.java @@ -0,0 +1,26 @@ +package vazkii.botania.api.brew; + +import net.minecraft.potion.Potion; +import net.minecraft.potion.PotionEffect; +import net.minecraftforge.fml.common.registry.GameData; + +// Version of PotionEffect that doesn't use number ID's +// See https://github.com/MinecraftForge/MinecraftForge/issues/2374 +// todo 1.9 remove? +public class PotionEffectShim { + + public final Potion potion; + public final int duration; + public final int amplifier; + + public PotionEffectShim(Potion potion, int duration, int amplifier) { + this.potion = potion; + this.duration = duration; + this.amplifier = amplifier; + } + + public PotionEffect toVanilla() { + return new PotionEffect(GameData.getPotionRegistry().getId(potion), duration, amplifier, true, true); + } + +} diff --git a/src/main/java/vazkii/botania/common/brew/BrewMod.java b/src/main/java/vazkii/botania/common/brew/BrewMod.java index 765c957c1..107dd0a8f 100644 --- a/src/main/java/vazkii/botania/common/brew/BrewMod.java +++ b/src/main/java/vazkii/botania/common/brew/BrewMod.java @@ -10,13 +10,13 @@ */ package vazkii.botania.common.brew; -import net.minecraft.potion.PotionEffect; import vazkii.botania.api.BotaniaAPI; import vazkii.botania.api.brew.Brew; +import vazkii.botania.api.brew.PotionEffectShim; public class BrewMod extends Brew { - public BrewMod(String key, int color, int cost, PotionEffect... effects) { + public BrewMod(String key, int color, int cost, PotionEffectShim... effects) { super(key, key, color, cost, effects); BotaniaAPI.registerBrew(this); } diff --git a/src/main/java/vazkii/botania/common/brew/BrewModPotion.java b/src/main/java/vazkii/botania/common/brew/BrewModPotion.java index 878ec1bbb..e87683198 100644 --- a/src/main/java/vazkii/botania/common/brew/BrewModPotion.java +++ b/src/main/java/vazkii/botania/common/brew/BrewModPotion.java @@ -12,11 +12,12 @@ package vazkii.botania.common.brew; import net.minecraft.potion.PotionEffect; import net.minecraftforge.fml.common.registry.GameData; +import vazkii.botania.api.brew.PotionEffectShim; public class BrewModPotion extends BrewMod { - public BrewModPotion(String key, int cost, PotionEffect... effects) { - super(key, GameData.getPotionRegistry().getObjectById(effects[0].getPotionID()).getLiquidColor(), cost, effects); + public BrewModPotion(String key, int cost, PotionEffectShim... effects) { + super(key, effects[0].potion.getLiquidColor(), cost, effects); } } diff --git a/src/main/java/vazkii/botania/common/brew/ModBrews.java b/src/main/java/vazkii/botania/common/brew/ModBrews.java index 2bdc1cc7f..92937149d 100644 --- a/src/main/java/vazkii/botania/common/brew/ModBrews.java +++ b/src/main/java/vazkii/botania/common/brew/ModBrews.java @@ -13,6 +13,7 @@ package vazkii.botania.common.brew; import net.minecraft.potion.Potion; import net.minecraft.potion.PotionEffect; import vazkii.botania.api.brew.Brew; +import vazkii.botania.api.brew.PotionEffectShim; import vazkii.botania.common.lib.LibBrewNames; public class ModBrews { @@ -42,27 +43,27 @@ public class ModBrews { public static Brew warpWard; public static void init() { - speed = new BrewMod(LibBrewNames.SPEED, 0x59B7FF, 4000, new PotionEffect(Potion.moveSpeed.id, 1800, 1)); - strength = new BrewMod(LibBrewNames.STRENGTH, 0xEE3F3F, 4000, new PotionEffect(Potion.damageBoost.id, 1800, 1)); - haste = new BrewMod(LibBrewNames.HASTE, 0xF4A432, 4000, new PotionEffect(Potion.digSpeed.id, 1800, 1)); - healing = new BrewMod(LibBrewNames.HEALING, 0xFF5ECC, 6000, new PotionEffect(Potion.heal.id, 1, 1)); - jumpBoost = new BrewMod(LibBrewNames.JUMP_BOOST, 0x32F46D, 4000, new PotionEffect(Potion.jump.id, 1800, 1)); - regen = new BrewMod(LibBrewNames.REGEN, 0xFD6488, 7000, new PotionEffect(Potion.regeneration.id, 500, 1)); - regenWeak = new BrewMod(LibBrewNames.REGEN_WEAK, 0xFD6488, 9000, new PotionEffect(Potion.regeneration.id, 2400, 0)); - resistance = new BrewMod(LibBrewNames.RESISTANCE, 0xB44E17, 4000, new PotionEffect(Potion.resistance.id, 1800, 1)); - fireResistance = new BrewMod(LibBrewNames.FIRE_RESISTANCE, 0xF86900, 4000, new PotionEffect(Potion.fireResistance.id, 9600, 0)); - waterBreathing = new BrewMod(LibBrewNames.WATER_BREATHING, 0x84A7CF, 4000, new PotionEffect(Potion.waterBreathing.id, 9600, 0)); - invisibility = new BrewMod(LibBrewNames.INVISIBILITY, 0xAEAEAE, 8000, new PotionEffect(Potion.invisibility.id, 9600, 0)); - nightVision = new BrewMod(LibBrewNames.NIGHT_VISION, 0x7C4BEB, 4000, new PotionEffect(Potion.nightVision.id, 9600, 0)); - absorption = new BrewMod(LibBrewNames.ABSORPTION, 0xF2EB23, 7000, new PotionEffect(Potion.absorption.id, 1800, 3)).setNotBloodPendantInfusable().setNotIncenseInfusable(); + speed = new BrewMod(LibBrewNames.SPEED, 0x59B7FF, 4000, new PotionEffectShim(Potion.moveSpeed, 1800, 1)); + strength = new BrewMod(LibBrewNames.STRENGTH, 0xEE3F3F, 4000, new PotionEffectShim(Potion.damageBoost, 1800, 1)); + haste = new BrewMod(LibBrewNames.HASTE, 0xF4A432, 4000, new PotionEffectShim(Potion.digSpeed, 1800, 1)); + healing = new BrewMod(LibBrewNames.HEALING, 0xFF5ECC, 6000, new PotionEffectShim(Potion.heal, 1, 1)); + jumpBoost = new BrewMod(LibBrewNames.JUMP_BOOST, 0x32F46D, 4000, new PotionEffectShim(Potion.jump, 1800, 1)); + regen = new BrewMod(LibBrewNames.REGEN, 0xFD6488, 7000, new PotionEffectShim(Potion.regeneration, 500, 1)); + regenWeak = new BrewMod(LibBrewNames.REGEN_WEAK, 0xFD6488, 9000, new PotionEffectShim(Potion.regeneration, 2400, 0)); + resistance = new BrewMod(LibBrewNames.RESISTANCE, 0xB44E17, 4000, new PotionEffectShim(Potion.resistance, 1800, 1)); + fireResistance = new BrewMod(LibBrewNames.FIRE_RESISTANCE, 0xF86900, 4000, new PotionEffectShim(Potion.fireResistance, 9600, 0)); + waterBreathing = new BrewMod(LibBrewNames.WATER_BREATHING, 0x84A7CF, 4000, new PotionEffectShim(Potion.waterBreathing, 9600, 0)); + invisibility = new BrewMod(LibBrewNames.INVISIBILITY, 0xAEAEAE, 8000, new PotionEffectShim(Potion.invisibility, 9600, 0)); + nightVision = new BrewMod(LibBrewNames.NIGHT_VISION, 0x7C4BEB, 4000, new PotionEffectShim(Potion.nightVision, 9600, 0)); + absorption = new BrewMod(LibBrewNames.ABSORPTION, 0xF2EB23, 7000, new PotionEffectShim(Potion.absorption, 1800, 3)).setNotBloodPendantInfusable().setNotIncenseInfusable(); - overload = new BrewMod(LibBrewNames.OVERLOAD, 0x232323, 12000, new PotionEffect(Potion.damageBoost.id, 1800, 3), new PotionEffect(Potion.moveSpeed.id, 1800, 2), new PotionEffect(Potion.weakness.id, 3600, 2), new PotionEffect(Potion.hunger.id, 200, 2)); - soulCross = new BrewModPotion(LibBrewNames.SOUL_CROSS, 10000, new PotionEffect(ModPotions.soulCross.id, 1800, 0)); - featherfeet = new BrewModPotion(LibBrewNames.FEATHER_FEET, 7000, new PotionEffect(ModPotions.featherfeet.id, 1800, 0)); - emptiness = new BrewModPotion(LibBrewNames.EMPTINESS, 30000, new PotionEffect(ModPotions.emptiness.id, 7200, 0)); - bloodthirst = new BrewModPotion(LibBrewNames.BLOODTHIRST, 20000, new PotionEffect(ModPotions.bloodthrst.id, 7200, 0)); - allure = new BrewModPotion(LibBrewNames.ALLURE, 2000, new PotionEffect(ModPotions.allure.id, 4800, 0)); - clear = new BrewModPotion(LibBrewNames.CLEAR, 4000, new PotionEffect(ModPotions.clear.id, 0, 0)); + overload = new BrewMod(LibBrewNames.OVERLOAD, 0x232323, 12000, new PotionEffectShim(Potion.damageBoost, 1800, 3), new PotionEffectShim(Potion.moveSpeed, 1800, 2), new PotionEffectShim(Potion.weakness, 3600, 2), new PotionEffectShim(Potion.hunger, 200, 2)); + soulCross = new BrewModPotion(LibBrewNames.SOUL_CROSS, 10000, new PotionEffectShim(ModPotions.soulCross, 1800, 0)); + featherfeet = new BrewModPotion(LibBrewNames.FEATHER_FEET, 7000, new PotionEffectShim(ModPotions.featherfeet, 1800, 0)); + emptiness = new BrewModPotion(LibBrewNames.EMPTINESS, 30000, new PotionEffectShim(ModPotions.emptiness, 7200, 0)); + bloodthirst = new BrewModPotion(LibBrewNames.BLOODTHIRST, 20000, new PotionEffectShim(ModPotions.bloodthrst, 7200, 0)); + allure = new BrewModPotion(LibBrewNames.ALLURE, 2000, new PotionEffectShim(ModPotions.allure, 4800, 0)); + clear = new BrewModPotion(LibBrewNames.CLEAR, 4000, new PotionEffectShim(ModPotions.clear, 0, 0)); } public static void initTC() { @@ -70,6 +71,6 @@ public class ModBrews { // thaumcraft 5 is registering its potions under the Minecraft namespace if(warpWardPotion != null) - warpWard = new BrewMod(LibBrewNames.WARP_WARD, 0xFBBDFF, 25000, new PotionEffect(warpWardPotion.id, 12000, 0)).setNotBloodPendantInfusable(); + warpWard = new BrewMod(LibBrewNames.WARP_WARD, 0xFBBDFF, 25000, new PotionEffectShim(warpWardPotion, 12000, 0)).setNotBloodPendantInfusable(); } }