-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
480 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
src/main/java/choonster/testmod3/potion/PotionTestMod3.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.