This is intended to be a place where you post general code snippets to do crap
Basic Classes
How about I start us off with the most basic classes of all, the base mod class, my counterpart for it the Reference class, and the proxy classes
Base Mod class for you (Requires Reference and Proxy classes)
For all your referencing needs
For all your proxy needs
Utility
Also the LogHelper class is very helpful when doing anything logging related (requires Reference Class)
KeyHelper; detects shift/control with proper Mac support contributed by @squeek502
ColorHelper; converts between the common color types used within Minecraft code (hexadecimal color using an int, rgba using ints from 0-255, and rgba using floats from 0.0-1.0) contributed by @squeek502
If you are working with Configs you may want a basic ConfigurationHandler class (Requires Reference Class)
GameRegistryItems Class - Makes ItemRegistering less of a hassle = less typing contributed by @SoraZodia
NBT
NBTHelper Class - Used to make NBT not as much of a hassle
NBTUpgradeList Class - Wrapper for NBTTagList to make them better contributed by @chbachman
GUI
GuiFactory Class - used for doing GUI stuff (I haven't used much, but I hear it is useful)
ModGuiConfig Class - Again haven't used much, but I am told it is useful (Requires Reference/ConfigurarionHandler)
Block Related Classes
BlockTool Class - a class with tools to use with your blocks Contributed by @McJty
OreDictionary
OreHelper Class - useful when working with the ore dictionary contributed by @goldenapple and taken from Flaxbeard's Steam Power Mod (Requires LogHelper Class for full functionality)
If y'all have any code snippets you would like to share please post them down below and I may add them. Specify a section you want them in (in bold) or if there are none ask for me to create a new one
Basic Classes
How about I start us off with the most basic classes of all, the base mod class, my counterpart for it the Reference class, and the proxy classes
Base Mod class for you (Requires Reference and Proxy classes)
Code:
@Mod(modid = Reference.MOD_ID, name = Reference.MOD_NAME, version = Reference.VERSION)
public class ModName
{
@Mod.Instance(Reference.MOD_ID)
public static ModName instance;
@SidedProxy(clientSide = Reference.CLIENT_PROXY_CLASS, serverSide = Reference.SERVER_PROXY_CLASS)
public static CommonProxy proxy;
@Mod.EventHandler
public void preInit(FMLPreInitializationEvent event)
{
//Optional if you have ConfigurationHandler setup
//ConfigurationHandler.init(event.getSuggestedConfigurationFile());
//FMLCommonHandler.instance().bus().register(new ConfigurationHandler());
//Optional if you use the LogHelper
//LogHelper.info("Pre Initialization Complete");
}
@Mod.EventHandler
public void Init(FMLInitializationEvent event)
{
//Optional if you use the LogHelper
//LogHelper.info("Initialization Complete");
}
@Mod.EventHandler
public void postInit(FMLPostInitializationEvent event)
{
//Optional if you use the LogHelper
//LogHelper.info("Post Initialization Complete");
}
}
Code:
public class Reference
{
public static final String MOD_ID = "ModID";
public static final String MOD_NAME = "ModName";
public static final String VERSION = "VERSION";
public static final String CLIENT_PROXY_CLASS = "mods.modname.proxy.ClientProxy";
public static final String SERVER_PROXY_CLASS = "mods.modname.proxy.ServerProxy";
}
Code:
public interface IProxy
{
public abstract void registerKeyBindings();
}
Code:
public abstract class CommonProxy implements IProxy {
public void initSounds()
{
}
public void initRenderers()
{
}
}
Code:
public class ServerProxy extends CommonProxy {
@Override
public void registerKeyBindings() {
//NOOP
}
}
Code:
public class ClientProxy extends CommonProxy {
@Override
public void initSounds()
{
}
@Override
public void initRenderers()
{
}
@Override
public void registerKeyBindings()
{
}
}
Utility
Also the LogHelper class is very helpful when doing anything logging related (requires Reference Class)
Code:
public class LogHelper {
public static void log(Level logLevel, Object object)
{
FMLLog.log(Reference.MOD_NAME, logLevel, String.valueOf(object));
}
public static void all(Object object)
{
log(Level.ALL, object);
}
public static void debug(Object object)
{
log(Level.DEBUG, object);
}
public static void error(Object object)
{
log(Level.ERROR, object);
}
public static void fatal(Object object)
{
log(Level.FATAL, object);
}
public static void info(Object object)
{
log(Level.INFO, object);
}
public static void off(Object object)
{
log(Level.OFF, object);
}
public static void trace(Object object)
{
log(Level.TRACE, object);
}
public static void warn(Object object)
{
log(Level.WARN, object);
}
}
KeyHelper; detects shift/control with proper Mac support contributed by @squeek502
Code:
public class KeyHelper
{
public static boolean isCtrlKeyDown()
{
// prioritize CONTROL, but allow OPTION as well on Mac (note: GuiScreen's isCtrlKeyDown only checks for the OPTION key on Mac)
boolean isCtrlKeyDown = Keyboard.isKeyDown(Keyboard.KEY_LCONTROL) || Keyboard.isKeyDown(Keyboard.KEY_RCONTROL);
if (!isCtrlKeyDown && Minecraft.isRunningOnMac)
isCtrlKeyDown = Keyboard.isKeyDown(Keyboard.KEY_LMETA) || Keyboard.isKeyDown(Keyboard.KEY_RMETA);
return isCtrlKeyDown;
}
public static boolean isShiftKeyDown()
{
return Keyboard.isKeyDown(Keyboard.KEY_LSHIFT) || Keyboard.isKeyDown(Keyboard.KEY_RSHIFT);
}
}
ColorHelper; converts between the common color types used within Minecraft code (hexadecimal color using an int, rgba using ints from 0-255, and rgba using floats from 0.0-1.0) contributed by @squeek502
Code:
public class ColorHelper
{
public static float[] toNormalizedRGB(int color)
{
return toNormalizedRGBA(color | 255 << 24);
}
public static float[] toNormalizedRGBA(int color)
{
int[] rgba = toRGBA(color);
return new float[]{rgba[0] / 255f, rgba[1] / 255f, rgba[2] / 255f, rgba[3] / 255f};
}
public static int[] toRGBA(int color)
{
int alpha = color >> 24 & 255;
int red = color >> 16 & 255;
int green = color >> 8 & 255;
int blue = color & 255;
return new int[]{red, green, blue, alpha};
}
public static int fromRGBA(int r, int g, int b, int a)
{
return (a & 255) << 24 | (r & 255) << 16 | (g & 255) << 8 | b & 255;
}
public static int fromRGB(int r, int g, int b)
{
return fromRGBA(r, g, b, 255);
}
public static int fromNormalizedRGBA(float r, float g, float b, float a)
{
return fromRGBA((int) r * 255, (int) g * 255, (int) b * 255, (int) a * 255);
}
public static int fromNormalizedRGB(float r, float g, float b)
{
return fromNormalizedRGBA(r, g, b, 1f);
}
}
If you are working with Configs you may want a basic ConfigurationHandler class (Requires Reference Class)
Code:
public class ConfigurationHandler {
public static Configuration configuration;
public static void init(File configFile) {
//create configuration object from the given file
if (configuration == null)
{
configuration = new Configuration(configFile);
loadConfiguration();
}
}
@SubscribeEvent
public void onConfigurationChangedEvent(ConfigChangedEvent.OnConfigChangedEvent event)
{
if (event.modID.equalsIgnoreCase(Reference.MOD_ID))
{
loadConfiguration();
}
}
private static void loadConfiguration()
{
if (configuration.hasChanged())
{
configuration.save();
}
}
}
GameRegistryItems Class - Makes ItemRegistering less of a hassle = less typing contributed by @SoraZodia
Code:
/** Item registry class, have methods to make
* item registation in the GameRegistry a bit easier/less
* typing.
*
* THE init() METHOD MUST BE CALLED FOR IT TO WORK*/
public class GameRegistryItems {
private static String MODID;
private static CreativeTabs tabs;
/**
* Intitizate the MODID and CreativeTabs variable for the rest of the class to use.
* This must be called before any other methods in this class or funny stuff may happen.
* @param MOD_ID_String
* @param CreativeTabs
*/
public static void init(String modID, CreativeTabs tab){
MODID = modID;
tabs = tab;
}
/** A more steamline way to register your items, does all of the
* extra stuff for you, just make sure your item object is initializated*/
public static void registerItems(Item item, String itemName, String imageName){
GameRegistry.registerItem(item, itemName, MODID)
.setCreativeTab(tabs)
.setUnlocalizedName(itemName)
.setTextureName(MODID + ":"+ imageName);
}
/** This method is used when the item's name is the same
* as its texture name. Helps reduce repetivite typing #lazy:P.
* Please make sure your item object is initializated */
public static void registerItems(Item item, String name){
GameRegistry.registerItem(item, name, MODID)
.setCreativeTab(tabs)
.setUnlocalizedName(name)
.setTextureName(MODID + ":"+ name);
}
}
NBT
NBTHelper Class - Used to make NBT not as much of a hassle
Code:
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
public class NBTHelper
{
public static boolean hasTag(ItemStack itemStack, String keyName)
{
return itemStack != null && itemStack.stackTagCompound != null && itemStack.stackTagCompound.hasKey(keyName);
}
public static void removeTag(ItemStack itemStack, String keyName)
{
if (itemStack.stackTagCompound != null)
{
itemStack.stackTagCompound.removeTag(keyName);
}
}
/**
* Initializes the NBT Tag Compound for the given ItemStack if it is null
*
* @param itemStack
* The ItemStack for which its NBT Tag Compound is being checked for initialization
*/
private static void initNBTTagCompound(ItemStack itemStack)
{
if (itemStack.stackTagCompound == null)
{
itemStack.setTagCompound(new NBTTagCompound());
}
}
public static void setLong(ItemStack itemStack, String keyName, long keyValue)
{
initNBTTagCompound(itemStack);
itemStack.stackTagCompound.setLong(keyName, keyValue);
}
// String
public static String getString(ItemStack itemStack, String keyName)
{
initNBTTagCompound(itemStack);
if (!itemStack.stackTagCompound.hasKey(keyName))
{
setString(itemStack, keyName, "");
}
return itemStack.stackTagCompound.getString(keyName);
}
public static void setString(ItemStack itemStack, String keyName, String keyValue)
{
initNBTTagCompound(itemStack);
itemStack.stackTagCompound.setString(keyName, keyValue);
}
// boolean
public static boolean getBoolean(ItemStack itemStack, String keyName)
{
initNBTTagCompound(itemStack);
if (!itemStack.stackTagCompound.hasKey(keyName))
{
setBoolean(itemStack, keyName, false);
}
return itemStack.stackTagCompound.getBoolean(keyName);
}
public static void setBoolean(ItemStack itemStack, String keyName, boolean keyValue)
{
initNBTTagCompound(itemStack);
itemStack.stackTagCompound.setBoolean(keyName, keyValue);
}
// byte
public static byte getByte(ItemStack itemStack, String keyName)
{
initNBTTagCompound(itemStack);
if (!itemStack.stackTagCompound.hasKey(keyName))
{
setByte(itemStack, keyName, (byte) 0);
}
return itemStack.stackTagCompound.getByte(keyName);
}
public static void setByte(ItemStack itemStack, String keyName, byte keyValue)
{
initNBTTagCompound(itemStack);
itemStack.stackTagCompound.setByte(keyName, keyValue);
}
// short
public static short getShort(ItemStack itemStack, String keyName)
{
initNBTTagCompound(itemStack);
if (!itemStack.stackTagCompound.hasKey(keyName))
{
setShort(itemStack, keyName, (short) 0);
}
return itemStack.stackTagCompound.getShort(keyName);
}
public static void setShort(ItemStack itemStack, String keyName, short keyValue)
{
initNBTTagCompound(itemStack);
itemStack.stackTagCompound.setShort(keyName, keyValue);
}
// int
public static int getInt(ItemStack itemStack, String keyName)
{
initNBTTagCompound(itemStack);
if (!itemStack.stackTagCompound.hasKey(keyName))
{
setInteger(itemStack, keyName, 0);
}
return itemStack.stackTagCompound.getInteger(keyName);
}
public static void setInteger(ItemStack itemStack, String keyName, int keyValue)
{
initNBTTagCompound(itemStack);
itemStack.stackTagCompound.setInteger(keyName, keyValue);
}
// long
public static long getLong(ItemStack itemStack, String keyName)
{
initNBTTagCompound(itemStack);
if (!itemStack.stackTagCompound.hasKey(keyName))
{
setLong(itemStack, keyName, 0);
}
return itemStack.stackTagCompound.getLong(keyName);
}
// float
public static float getFloat(ItemStack itemStack, String keyName)
{
initNBTTagCompound(itemStack);
if (!itemStack.stackTagCompound.hasKey(keyName))
{
setFloat(itemStack, keyName, 0);
}
return itemStack.stackTagCompound.getFloat(keyName);
}
public static void setFloat(ItemStack itemStack, String keyName, float keyValue)
{
initNBTTagCompound(itemStack);
itemStack.stackTagCompound.setFloat(keyName, keyValue);
}
// double
public static double getDouble(ItemStack itemStack, String keyName)
{
initNBTTagCompound(itemStack);
if (!itemStack.stackTagCompound.hasKey(keyName))
{
setDouble(itemStack, keyName, 0);
}
return itemStack.stackTagCompound.getDouble(keyName);
}
public static void setDouble(ItemStack itemStack, String keyName, double keyValue)
{
initNBTTagCompound(itemStack);
itemStack.stackTagCompound.setDouble(keyName, keyValue);
}
}
NBTUpgradeList Class - Wrapper for NBTTagList to make them better contributed by @chbachman
Code:
public class NBTList<E extends INBTAble> implements List<E> {
public final NBTTagList list;
public NBTList() {
this.list = new NBTTagList();
}
public NBTList(NBTTagList list) {
this.list = list;
}
public NBTList(NBTTagCompound nbt) {
this.list = nbt.getTagList("List", Constants.NBT.TAG_COMPOUND);
}
public NBTTagCompound addNBTTagCompound(NBTTagCompound nbt) {
nbt.setTag("UpgradeList", this.list);
return nbt;
}
@Override
public int size() {
return this.list.tagCount();
}
private E getUpgrade(NBTTagCompound nbt) {
try {
// E upgrade = //TODO: add getFromNBTMethodHERE
// return upgrade;
} catch (IndexOutOfBoundsException e) {
return null;
}
}
@Override
public E get(int index) {
NBTTagCompound nbt = this.list.getCompoundTagAt(index);
if (nbt == null) {
return null;
}
E upgrade = this.getUpgrade(nbt);
if (upgrade == null) {
this.list.removeTag(index);
return this.get(index);
}
return upgrade;
}
@Override
public boolean add(E upgrade) {
this.list.appendTag(upgrade.getNBT());
return true;
}
@Override
public E set(int index, E element) {
this.list.func_150304_a(index, element.getNBT());
return element;
}
@Override
public E remove(int index) {
E upgrade = this.get(index);
this.list.removeTag(index);
return upgrade;
}
@Override
public boolean addAll(Collection<? extends E> c) {
for (E upgrade : c) {
this.add(upgrade);
}
return true;
}
@Override
public void clear() {
for (int i = 0; i < this.size(); i++) {
this.remove(i);
}
}
@Override
public boolean contains(Object o) {
for (E upgrade : this) {
if (upgrade.equals(o)) {
return true;
}
}
return false;
}
@Override
public boolean containsAll(Collection<?> c) {
for (Object o : this) {
if (this.contains(o)) {
return true;
}
}
return false;
}
@Override
public int indexOf(Object o) {
for (int i = 0; i < this.size(); i++) {
E upgrade = this.get(i);
if (upgrade.equals(o)) {
return i;
}
}
return -1;
}
@Override
public boolean isEmpty() {
return this.size() == 0;
}
@Override
public int lastIndexOf(Object o) {
for (int i = this.size() - 1; i >= 0; i--) {
E upgrade = this.get(i);
if (upgrade.equals(o)) {
return i;
}
}
return -1;
}
@Override
public boolean remove(Object o) {
for (int i = 0; i < this.size(); i++) {
if (this.get(i).equals(o)) {
this.remove(i);
return true;
}
}
return false;
}
@Override
public boolean removeAll(Collection<?> c) {
for (Object o : c) {
if (!this.remove(o)) {
return false;
}
}
return true;
}
@Override
public boolean retainAll(Collection<?> c) {
return false;
}
@Override
public List<E> subList(int fromIndex, int toIndex) {
List<E> list = new ArrayList<E>();
for (int i = fromIndex; i < toIndex; i++) {
list.add(this.get(i));
}
return list;
}
@Override
public Object[] toArray() {
Object[] objects = new Object[this.size()];
for (int i = 0; i < this.size(); i++) {
objects[i] = this.get(i);
}
return objects;
}
@SuppressWarnings("unchecked")
@Override
public <T> T[] toArray(T[] a) {
if (a.length < this.size()) {
a = (T[]) this.toArray();
} else {
System.arraycopy(this.toArray(), 0, a, 0, this.size());
}
if (a.length > this.size()) {
a[this.size()] = null;
}
return a;
}
public class IteratorUpgradeList implements ListIterator<E> {
NBTList<E> list;
int index = 0;
public IteratorUpgradeList(NBTList<E> list) {
this.list = list;
}
@Override
public boolean hasNext() {
return this.index != this.list.size();
}
@Override
public E next() {
return this.list.get(this.index++);
}
@Override
public boolean hasPrevious() {
return this.index != 0;
}
@Override
public E previous() {
return this.list.get(--this.index);
}
@Override
public int nextIndex() {
return this.index + 1;
}
@Override
public int previousIndex() {
return this.index - 1;
}
@Override
public void remove() {
this.list.remove(this.index);
}
@Override
public void set(E e) {
this.list.set(this.index, e);
}
@Override
public void add(E e) {
this.list.add(this.index, e);
}
}
@Override
public void add(int index, E element) {
this.add(element);
}
@Override
public boolean addAll(int index, Collection<? extends E> c) {
for (E upgrade : c) {
this.add(index, upgrade);
}
return true;
}
@Override
public ListIterator<E> listIterator() {
return new IteratorUpgradeList(this);
}
@Override
public Iterator<E> iterator() {
return new IteratorUpgradeList(this);
}
@Override
public ListIterator<E> listIterator(int index) {
return new IteratorUpgradeList(this);
}
}
GuiFactory Class - used for doing GUI stuff (I haven't used much, but I hear it is useful)
Code:
public class GuiFactory implements IModGuiFactory {
@Override
public void initialize(Minecraft minecraftInstance) {
}
@Override
public Class<? extends GuiScreen> mainConfigGuiClass() {
return ModGuiConfig.class;
}
@Override
public Set<RuntimeOptionCategoryElement> runtimeGuiCategories() {
return null;
}
@Override
public RuntimeOptionGuiHandler getHandlerFor(RuntimeOptionCategoryElement element) {
return null;
}
}
ModGuiConfig Class - Again haven't used much, but I am told it is useful (Requires Reference/ConfigurarionHandler)
Code:
public class ModGuiConfig extends GuiConfig{
public ModGuiConfig(GuiScreen guiScreen)
{
super(guiScreen,
new ConfigElement(ConfigurationHandler.configuration.getCategory(Configuration.CATEGORY_GENERAL)).getChildElements(),
Reference.MOD_ID,
false,
false,
GuiConfig.getAbridgedConfigPath(ConfigurationHandler.configuration.toString()));
}
}
Block Related Classes
BlockTool Class - a class with tools to use with your blocks Contributed by @McJty
Code:
public class BlockTools {
public static final int MASK_ORIENTATION = 0x7;
public static final int MASK_REDSTONE = 0x8;
public static int getOrientation(int metadata) {
return metadata & MASK_ORIENTATION;
}
public static int setOrientation(int metadata, int orientation) {
return (metadata & ~MASK_ORIENTATION) | orientation;
}
public static boolean getRedstoneSignal(int metadata) {
return (metadata & MASK_REDSTONE) != 0;
}
public static int setRedstoneSignal(int metadata, boolean signal) {
if (signal) {
return metadata | MASK_REDSTONE;
} else {
return metadata & ~MASK_REDSTONE;
}
}
public static int determineOrientation(World world, int x, int y, int z, EntityLivingBase entityLivingBase) {
if (MathHelper.abs((float) entityLivingBase.posX - x) < 2.0F && MathHelper.abs((float)entityLivingBase.posZ - z) < 2.0F) {
double d0 = entityLivingBase.posY + 1.82D - entityLivingBase.yOffset;
if (d0 - y > 2.0D) {
return 1;
}
if (y - d0 > 0.0D) {
return 0;
}
}
int l = MathHelper.floor_double((entityLivingBase.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3;
return l == 0 ? 2 : (l == 1 ? 5 : (l == 2 ? 3 : (l == 3 ? 4 : 0)));
}
}
OreDictionary
OreHelper Class - useful when working with the ore dictionary contributed by @goldenapple and taken from Flaxbeard's Steam Power Mod (Requires LogHelper Class for full functionality)
Code:
public class OreHelper {
public static boolean isItemThisOre(ItemStack item, String ore){
for (int o : OreDictionary.getOreIDs(item)) {
if (OreDictionary.getOreName(o).equals(ore)) {
return true;
}
}
return false;
}
//Requires the LogHelper class
public static void dumpAllOres(){
for (String l : OreDictionary.getOreNames()){
LogHelper.info(l);
}
}
}
If y'all have any code snippets you would like to share please post them down below and I may add them. Specify a section you want them in (in bold) or if there are none ask for me to create a new one
Last edited: