mirror of
https://github.com/GTNewHorizons/Binnie.git
synced 2026-11-17 11:05:32 +00:00
improve null checks for Mods.java
This commit is contained in:
1 parent
975309598a
commit
307374ca2b
4 files changed
+90
-55
No files matched your search
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,7 @@ import forestry.api.apiculture.IBeeListener;
|
||||
import forestry.api.apiculture.IBeeModifier;
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@@ -40,7 +41,7 @@ public class AlvearyMutator {
|
||||
return 1.0f;
|
||||
}
|
||||
|
||||
public static void addMutationItem(final ItemStack item, final float chance) {
|
||||
public static void addMutationItem(@Nullable final ItemStack item, final float chance) {
|
||||
if (item == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import forestry.api.recipes.RecipeManagers;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraftforge.oredict.OreDictionary;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -143,7 +144,7 @@ public enum EnumHoneyComb implements IItemEnum {
|
||||
return ExtraBees.proxy.localise("item.comb." + this.name().toLowerCase());
|
||||
}
|
||||
|
||||
public boolean addProduct(final ItemStack item, final Float chance) {
|
||||
public boolean addProduct(@Nullable final ItemStack item, final Float chance) {
|
||||
if (item == null) {
|
||||
return false;
|
||||
}
|
||||
@@ -151,7 +152,7 @@ public enum EnumHoneyComb implements IItemEnum {
|
||||
return true;
|
||||
}
|
||||
|
||||
public void tryAddProduct(final ItemStack item, final Float chance) {
|
||||
public void tryAddProduct(@Nullable final ItemStack item, final Float chance) {
|
||||
this.active = this.addProduct(item, chance);
|
||||
}
|
||||
|
||||
|
||||
@@ -257,7 +257,7 @@ public class ItemHoneyComb extends ItemProduct implements IColoredItem {
|
||||
QUARTZ;
|
||||
|
||||
public ItemStack get() {
|
||||
return new ItemStack(Mods.Forestry.item("beeCombs"), 1, this.ordinal());
|
||||
return Mods.Forestry.stack("beeCombs", 1, this.ordinal());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user