Skip to content

Commit

Permalink
Add Test potion
Browse files Browse the repository at this point in the history
  • Loading branch information
Choonster committed Jul 12, 2016
1 parent b044c89 commit 2036dae
Show file tree
Hide file tree
Showing 14 changed files with 480 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/main/java/choonster/testmod3/TestMod3.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public void preInit(FMLPreInitializationEvent event) {
ModBiomes.registerBiomes();
ModMapGen.registerMapGen();
ModEntities.registerEntities();
ModPotions.registerPotions();
ModPotionTypes.registerPotionTypes();

SnowBuildup.init();

Expand Down
50 changes: 50 additions & 0 deletions src/main/java/choonster/testmod3/client/gui/GuiUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package choonster.testmod3.client.gui;

import choonster.testmod3.Logger;
import choonster.testmod3.util.ReflectionUtil;
import net.minecraft.client.gui.Gui;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

import java.lang.invoke.MethodHandle;

/**
* Utility methods for GUI rendering.
*
* @author Choonster
*/
@SideOnly(Side.CLIENT)
public class GuiUtils {
private static final MethodHandle Z_LEVEL = ReflectionUtil.findFieldGetter(Gui.class, "zLevel", "field_73735_i");

/**
* Get the value of the {@link Gui#zLevel} field for the specified {@link Gui}.
*
* @param gui The Gui.
* @return The z level, or 0 if an exception was thrown.
*/
@SideOnly(Side.CLIENT)
private static float getZLevel(Gui gui) {
try {
return (float) Z_LEVEL.invoke(gui);
} catch (Throwable throwable) {
Logger.fatal(throwable, "Failed to get z level of GUI");
return 0.0f;
}
}

/**
* Draw a textured rectangle at the {@link Gui}'s z level.
*
* @param x The x coordinate to draw at
* @param y The y coordinate to draw at
* @param textureX The x coordinate within the texture
* @param textureY The y coordinate within the texture
* @param width The width of the rectangle
* @param height The height of the rectangle
* @param gui The Gui to get the z level from
*/
public static void drawTexturedModalRect(int x, int y, int textureX, int textureY, int width, int height, Gui gui) {
net.minecraftforge.fml.client.config.GuiUtils.drawTexturedModalRect(x, y, textureX, textureY, width, height, getZLevel(gui));
}
}
79 changes: 79 additions & 0 deletions src/main/java/choonster/testmod3/init/ModPotionTypes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package choonster.testmod3.init;

import net.minecraft.potion.Potion;
import net.minecraft.potion.PotionEffect;
import net.minecraft.potion.PotionType;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.common.registry.GameRegistry;

import javax.annotation.Nullable;

/**
* Registers this mod's {@link PotionType}s.
*
* @author Choonster
*/
@SuppressWarnings("WeakerAccess")
public class ModPotionTypes {
private static final String LONG_PREFIX = "long_";
private static final String STRONG_PREFIX = "strong_";

private static final int HELPFUL_DURATION_STANDARD = 3600;
private static final int HELPFUL_DURATION_LONG = 9600;
private static final int HELPFUL_DURATION_STRONG = 1800;

private static final int HARMFUL_DURATION_STANDARD = 1800;
private static final int HARMFUL_DURATION_LONG = 4800;
private static final int HARMFUL_DURATION_STRONG = 900;

public static final PotionType TEST;
public static final PotionType LONG_TEST;
public static final PotionType STRONG_TEST;

static {
TEST = createPotionType(new PotionEffect(ModPotions.TEST, HELPFUL_DURATION_STANDARD));
LONG_TEST = createPotionType(new PotionEffect(ModPotions.TEST, HELPFUL_DURATION_LONG), LONG_PREFIX);
STRONG_TEST = createPotionType(new PotionEffect(ModPotions.TEST, HELPFUL_DURATION_STRONG, 1), STRONG_PREFIX);
}

/**
* Dummy method to ensure the static initialiser runs.
*/
public static void registerPotionTypes() {

}

/**
* Create a {@link PotionType} from the specified {@link PotionEffect}.
* <p>
* Uses the {@link Potion}'s registry name as the {@link PotionType}'s registry name and name.
*
* @param potionEffect The PotionEffect
* @return The PotionType
*/
private static PotionType createPotionType(PotionEffect potionEffect) {
return createPotionType(potionEffect, null);
}

/**
* Create a {@link PotionType} from the specified {@link PotionEffect}
* <p>
* Uses the {@link Potion}'s registry name as the {@link PotionType}'s registry name (with an optional prefix) and name (with no prefix).
*
* @param potionEffect The PotionEffect
* @param namePrefix The name prefix, if any
* @return The PotionType
*/
private static PotionType createPotionType(PotionEffect potionEffect, @Nullable String namePrefix) {
final ResourceLocation potionName = potionEffect.getPotion().getRegistryName();

final ResourceLocation potionTypeName;
if (namePrefix != null) {
potionTypeName = new ResourceLocation(potionName.getResourceDomain(), namePrefix + potionName.getResourcePath());
} else {
potionTypeName = potionName;
}

return GameRegistry.register(new PotionType(potionName.toString(), potionEffect).setRegistryName(potionTypeName));
}
}
37 changes: 37 additions & 0 deletions src/main/java/choonster/testmod3/init/ModPotions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package choonster.testmod3.init;

import choonster.testmod3.potion.PotionTestMod3;
import net.minecraft.potion.Potion;
import net.minecraftforge.fml.common.registry.GameRegistry;

/**
* Registers this mod's {@link Potion}s.
*
* @author Choonster
*/
@SuppressWarnings("WeakerAccess")
public class ModPotions {
public static final PotionTestMod3 TEST;

static {
TEST = registerPotion(new PotionTestMod3(false, 2, 2, 2, "test"));
}

/**
* Dummy method to ensure the static initialiser runs.
*/
public static void registerPotions() {

}

/**
* Register a {@link Potion}.
*
* @param potion The Potion instance
* @param <POTION> The Potion type
* @return The Potion instance
*/
private static <POTION extends Potion> POTION registerPotion(POTION potion) {
return GameRegistry.register(potion);
}
}
31 changes: 31 additions & 0 deletions src/main/java/choonster/testmod3/init/ModRecipes.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,22 @@
import choonster.testmod3.recipe.ShapedArmourUpgradeRecipe;
import choonster.testmod3.recipe.ShapelessCuttingRecipe;
import choonster.testmod3.recipe.ShapelessNBTRecipe;
import choonster.testmod3.recipe.brewing.PotionTypeConversionBrewingRecipe;
import net.minecraft.block.Block;
import net.minecraft.block.BlockPlanks;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.init.PotionTypes;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.CraftingManager;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.item.crafting.RecipeFireworks;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.potion.PotionType;
import net.minecraftforge.common.ForgeModContainer;
import net.minecraftforge.common.brewing.BrewingRecipeRegistry;
import net.minecraftforge.fluids.UniversalBucket;
import net.minecraftforge.fml.common.registry.GameRegistry;
import net.minecraftforge.oredict.OreDictionary;
Expand All @@ -32,6 +36,7 @@ public class ModRecipes {
public static void registerRecipes() {
registerRecipeClasses();
addCraftingRecipes();
addBrewingRecipes();
}

private static void registerRecipeClasses() {
Expand Down Expand Up @@ -62,6 +67,32 @@ private static void addCraftingRecipes() {
GameRegistry.addSmelting(ModItems.SUBSCRIPTS, new ItemStack(ModItems.DIMENSION_REPLACEMENT), 0.35f);
}

/**
* Add this mod's brewing recipes.
*/
private static void addBrewingRecipes() {
addStandardConversionRecipes(ModPotionTypes.TEST, ModPotionTypes.LONG_TEST, ModPotionTypes.STRONG_TEST, new ItemStack(ModItems.ARROW));
}

/**
* Add the standard conversion recipes for the specified {@link PotionType}s:
* <ul>
* <li>Awkward + Ingredient = Standard</li>
* <li>Standard + Redstone = Long</li>
* <li>Standard + Glowstone = Strong</li>
* </ul>
*
* @param standardPotionType The standard PotionType
* @param longPotionType The long PotionType
* @param strongPotionType The strong PotionType
* @param ingredient The ingredient
*/
private static void addStandardConversionRecipes(PotionType standardPotionType, PotionType longPotionType, PotionType strongPotionType, ItemStack ingredient) {
BrewingRecipeRegistry.addRecipe(new PotionTypeConversionBrewingRecipe(PotionTypes.AWKWARD, ingredient, standardPotionType));
BrewingRecipeRegistry.addRecipe(new PotionTypeConversionBrewingRecipe(standardPotionType, new ItemStack(Items.REDSTONE), longPotionType));
BrewingRecipeRegistry.addRecipe(new PotionTypeConversionBrewingRecipe(standardPotionType, new ItemStack(Items.GLOWSTONE_DUST), strongPotionType));
}

public static void removeCraftingRecipes() {
removeRecipeClass(RecipeFireworks.class);
removeRecipe(Items.DYE);
Expand Down
86 changes: 86 additions & 0 deletions src/main/java/choonster/testmod3/potion/PotionTestMod3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package choonster.testmod3.potion;

import choonster.testmod3.TestMod3;
import choonster.testmod3.client.gui.GuiUtils;
import net.minecraft.client.Minecraft;
import net.minecraft.potion.Potion;
import net.minecraft.potion.PotionEffect;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

import java.awt.*;


/**
* A base class for this mod's potions.
*
* @author Choonster
*/
public class PotionTestMod3 extends Potion {
/**
* The icon texture to use in the HUD and inventory GUI.
*/
private final ResourceLocation iconTexture;

public PotionTestMod3(boolean isBadEffect, int liquidColor, String name) {
super(isBadEffect, liquidColor);
setPotionName(this, name);
iconTexture = new ResourceLocation(TestMod3.MODID, "textures/potions/" + name + ".png");
}

public PotionTestMod3(boolean isBadEffect, int liquidR, int liquidG, int liquidB, String name) {
this(isBadEffect, new Color(liquidR, liquidG, liquidB).getRGB(), name);
}

/**
* Set the registry name of {@code potion} to {@code potionName} and the unlocalised name to the full registry name.
*
* @param potion The potion
* @param potionName The potion's name
*/
public static void setPotionName(Potion potion, String potionName) {
potion.setRegistryName(potionName);
potion.setPotionName("effect." + potion.getRegistryName().toString());
}

@Override
public boolean hasStatusIcon() {
return false;
}

/**
* Called to draw the this Potion onto the player's inventory when it's active.
* This can be used to e.g. render Potion icons from your own texture.
*
* @param x the x coordinate
* @param y the y coordinate
* @param effect the active PotionEffect
* @param mc the Minecraft instance, for convenience
*/
@SideOnly(Side.CLIENT)
@Override
public void renderInventoryEffect(int x, int y, PotionEffect effect, Minecraft mc) {
if (mc.currentScreen != null) {
mc.getTextureManager().bindTexture(iconTexture);
GuiUtils.drawTexturedModalRect(x + 6, y + 7, 0, 0, 18, 18, mc.currentScreen);
}
}

/**
* Called to draw the this Potion onto the player's ingame HUD when it's active.
* This can be used to e.g. render Potion icons from your own texture.
*
* @param x the x coordinate
* @param y the y coordinate
* @param effect the active PotionEffect
* @param mc the Minecraft instance, for convenience
* @param alpha the alpha value, blinks when the potion is about to run out
*/
@SideOnly(Side.CLIENT)
@Override
public void renderHUDEffect(int x, int y, PotionEffect effect, Minecraft mc, float alpha) {
mc.getTextureManager().bindTexture(iconTexture);
GuiUtils.drawTexturedModalRect(x + 3, y + 3, 0, 0, 18, 18, mc.ingameGUI);
}
}
7 changes: 7 additions & 0 deletions src/main/java/choonster/testmod3/potion/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@ParametersAreNonnullByDefault
@MethodsReturnNonnullByDefault
package choonster.testmod3.potion;

import mcp.MethodsReturnNonnullByDefault;

import javax.annotation.ParametersAreNonnullByDefault;

0 comments on commit 2036dae

Please sign in to comment.