improve null checks for Mods.java

This commit is contained in:
mezz committed 2016-10-30 20:56:24 -07:00
1 parent 975309598a
commit 307374ca2b
4 files changed
+90 -55

No files matched your search

+84 -51
View File
@@ -1,88 +1,121 @@
package binnie.core;
import net.minecraft.block.Block;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.common.Loader;
import net.minecraftforge.fml.common.registry.GameRegistry;
import net.minecraftforge.fml.common.versioning.DefaultArtifactVersion;
import net.minecraftforge.fml.common.versioning.Restriction;
import net.minecraftforge.fml.common.registry.ForgeRegistries;
import java.util.Objects;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
public class Mods {
public static Mod Forestry = new Mod("forestry") {
@Override
public boolean dev() {
final String forVersion = Loader.instance().getIndexedModList().get("forestry").getVersion();
final Restriction rest = new Restriction(new DefaultArtifactVersion("3.6"), true, null, false);
return rest.containsVersion(new DefaultArtifactVersion(forVersion));
}
};
public static ModNonnull Forestry = new ModNonnull("forestry");
public static Mod IC2 = new Mod("IC2");
public static Mod Botania = new Mod("Botania");
private static boolean WARN = true;
private static Item findItem(final String modId, final String name) {
final Item stack = Item.REGISTRY.getObject(new ResourceLocation(modId, name));
if (stack == null && Mods.WARN && Objects.equals(modId, "forestry")) {
throw new RuntimeException("Item not found: " + modId + ":" + name);
}
return stack;
}
private static ItemStack findItemStack(final String modId, final String name, final int stackSize) {
final ItemStack stack = GameRegistry.makeItemStack(modId + ":" + name, 0, stackSize, null);
if ((stack == null || stack.getItem() == null) && Mods.WARN && Objects.equals(modId, "forestry")) {
throw new RuntimeException("Stack not found: " + modId + ":" + name);
}
return stack;
}
private static Block findBlock(final String modId, final String name) {
final Block block = Block.REGISTRY.getObject(new ResourceLocation(modId + ":" + name));
if (block == Blocks.AIR && Mods.WARN && Objects.equals(modId, "forestry")) {
throw new RuntimeException("Block not found: " + modId + ":" + name);
}
return block;
}
public static class Mod {
private String id;
protected final String id;
private Mod(final String id) {
this.id = id;
}
@Nullable
public Item item(final String name) {
return findItem(this.id, name);
ResourceLocation key = new ResourceLocation(this.id, name);
if (!ForgeRegistries.ITEMS.containsKey(key)) {
return null;
}
return ForgeRegistries.ITEMS.getValue(key);
}
@Nullable
public Block block(final String name) {
return findBlock(this.id, name);
}
public ItemStack stack(final String name, final int stackSize) {
return findItemStack(this.id, name, stackSize);
ResourceLocation key = new ResourceLocation(this.id, name);
if (!ForgeRegistries.BLOCKS.containsKey(key)) {
throw new RuntimeException("Block not found: " + key);
}
return ForgeRegistries.BLOCKS.getValue(key);
}
@Nullable
public ItemStack stack(final String name) {
return this.stack(name, 1);
Item item = this.item(name);
if (item == null) {
return null;
}
return new ItemStack(item);
}
public ItemStack stack(final String string, final int amount, final int meta) {
return new ItemStack(this.item(string), amount, meta);
@Nullable
public ItemStack stack(final String name, final int amount) {
Item item = this.item(name);
if (item == null) {
return null;
}
return new ItemStack(item, amount);
}
@Nullable
public ItemStack stack(final String name, final int amount, final int meta) {
Item item = this.item(name);
if (item == null) {
return null;
}
return new ItemStack(item, amount, meta);
}
public boolean active() {
return Loader.isModLoaded(this.id);
}
}
public boolean dev() {
return false;
public static class ModNonnull extends Mod {
private ModNonnull(final String id) {
super(id);
}
@Nonnull
@Override
public Item item(final String name) {
Item item = super.item(name);
if (item == null) {
throw new RuntimeException("Item not found: " + this.id + ":" + name);
}
return item;
}
@Nonnull
@Override
public Block block(final String name) {
Block block = super.block(name);
if (block == null) {
throw new RuntimeException("Block not found: " + this.id + ":" + name);
}
return block;
}
@Nonnull
@Override
public ItemStack stack(final String name) {
Item item = this.item(name);
return new ItemStack(item);
}
@Nonnull
@Override
public ItemStack stack(final String name, final int amount) {
Item item = this.item(name);
return new ItemStack(item, amount);
}
@Nonnull
@Override
public ItemStack stack(final String name, final int amount, final int meta) {
Item item = this.item(name);
return new ItemStack(item, amount, meta);
}
}
}