Fix #273 Crash with Dimensional Control mod

This commit is contained in:
mezz committed 2017-08-16 22:03:17 -07:00
1 parent a41e6d32a1
commit ca4da294b4
11 files changed
+75 -202

No files matched your search

@@ -219,4 +219,8 @@ public class MachineUtil {
public boolean isServer() {
return !this.machine.getWorld().isRemote;
}
public World getWorld() {
return this.machine.getWorld();
}
}
@@ -1,64 +0,0 @@
package binnie.core.util;
import javax.annotation.Nullable;
import net.minecraft.block.state.IBlockState;
import net.minecraft.init.Blocks;
import net.minecraft.profiler.Profiler;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.DimensionType;
import net.minecraft.world.GameType;
import net.minecraft.world.World;
import net.minecraft.world.WorldProvider;
import net.minecraft.world.WorldSettings;
import net.minecraft.world.WorldType;
import net.minecraft.world.chunk.IChunkProvider;
import net.minecraft.world.storage.ISaveHandler;
import net.minecraft.world.storage.WorldInfo;
public class FakeCraftingWorld extends World {
private static final WorldSettings worldSettings = new WorldSettings(0, GameType.SURVIVAL, false, false, WorldType.DEFAULT);
private static final WorldInfo worldInfo = new WorldInfo(worldSettings, "fake");
private static final ISaveHandler saveHandler = new FakeSaveHandler();
private static final WorldProvider worldProvider = new WorldProvider() {
@Override
public DimensionType getDimensionType() {
return DimensionType.OVERWORLD;
}
};
@Nullable
private static FakeCraftingWorld INSTANCE;
private FakeCraftingWorld() {
super(saveHandler, worldInfo, worldProvider, new Profiler(), true);
this.provider.setWorld(this);
this.mapStorage = new FakeMapStorage();
}
public static FakeCraftingWorld getInstance() {
if (INSTANCE == null) {
INSTANCE = new FakeCraftingWorld();
}
return INSTANCE;
}
@Override
public BlockPos getSpawnPoint() {
return new BlockPos(0, 0, 0);
}
@Override
public IBlockState getBlockState(BlockPos pos) {
return Blocks.AIR.getDefaultState();
}
@Override
protected IChunkProvider createChunkProvider() {
throw new UnsupportedOperationException();
}
@Override
protected boolean isChunkLoaded(int x, int z, boolean allowEmpty) {
return false;
}
}
@@ -1,32 +0,0 @@
package binnie.core.util;
import javax.annotation.Nullable;
import net.minecraft.world.storage.MapStorage;
import net.minecraft.world.storage.WorldSavedData;
public class FakeMapStorage extends MapStorage {
public FakeMapStorage() {
super(null);
}
@Override
@Nullable
public WorldSavedData getOrLoadData(Class<? extends WorldSavedData> clazz, String dataIdentifier) {
return this.loadedDataMap.get(dataIdentifier);
}
@Override
public void setData(String dataIdentifier, WorldSavedData data) {
this.loadedDataMap.put(dataIdentifier, data);
}
@Override
public void saveAllData() {
}
@Override
public int getUniqueDataId(String key) {
return 0;
}
}
@@ -1,60 +0,0 @@
package binnie.core.util;
import java.io.File;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.MinecraftException;
import net.minecraft.world.WorldProvider;
import net.minecraft.world.chunk.storage.IChunkLoader;
import net.minecraft.world.gen.structure.template.TemplateManager;
import net.minecraft.world.storage.IPlayerFileData;
import net.minecraft.world.storage.ISaveHandler;
import net.minecraft.world.storage.WorldInfo;
public class FakeSaveHandler implements ISaveHandler {
@Override
public WorldInfo loadWorldInfo() {
return null;
}
@Override
public void checkSessionLock() throws MinecraftException {
}
@Override
public IChunkLoader getChunkLoader(WorldProvider provider) {
throw new UnsupportedOperationException();
}
@Override
public void saveWorldInfoWithPlayer(WorldInfo worldInformation, NBTTagCompound tagCompound) {
}
@Override
public void saveWorldInfo(WorldInfo worldInformation) {
}
@Override
public IPlayerFileData getPlayerNBTManager() {
throw new UnsupportedOperationException();
}
@Override
public void flush() {
}
@Override
public File getMapFileFromName(String mapName) {
throw new UnsupportedOperationException();
}
@Override
public File getWorldDirectory() {
throw new UnsupportedOperationException();
}
@Override
public TemplateManager getStructureTemplateManager() {
throw new UnsupportedOperationException();
}
}
@@ -212,7 +212,7 @@ public class TreeBreedingSystem extends BreedingSystem {
ITreeGenome genome = TreeManager.treeRoot.templateAsGenome(TreeManager.treeRoot.getTemplate(tSpecies));
IAlleleTreeSpecies treeSpecies = genome.getPrimary();
final ItemStack woodStack = treeSpecies.getWoodProvider().getWoodStack();
ItemStack plankProduct = LumbermillRecipeManager.getPlankProduct(woodStack);
ItemStack plankProduct = LumbermillRecipeManager.getPlankProduct(woodStack, world);
if (!plankProduct.isEmpty() && fruit.isItemEqual(plankProduct)) {
found.add(species);
}
@@ -27,7 +27,7 @@ public class LumbermillLogic extends ComponentProcessSetCost implements IProcess
if (logStack.isEmpty()) {
return new ErrorState(ExtraTreesErrorCode.LUMBERMILL_NO_WOOD, LumbermillMachine.SLOT_LOG);
}
ItemStack plankResult = LumbermillRecipeManager.getPlankProduct(logStack);
ItemStack plankResult = LumbermillRecipeManager.getPlankProduct(logStack, util.getWorld());
if (!util.isSlotEmpty(LumbermillMachine.SLOT_PLANKS) && !plankResult.isEmpty()) {
final ItemStack currentPlank = util.getStack(LumbermillMachine.SLOT_PLANKS);
if (!plankResult.isItemEqual(currentPlank) || plankResult.getCount() + currentPlank.getCount() > currentPlank.getMaxStackSize()) {
@@ -61,8 +61,8 @@ public class LumbermillLogic extends ComponentProcessSetCost implements IProcess
protected void onFinishTask() {
MachineUtil util = getUtil();
final ItemStack logStack = util.getStack(LumbermillMachine.SLOT_LOG);
final ItemStack result = LumbermillRecipeManager.getPlankProduct(logStack);
if (result == null) {
final ItemStack result = LumbermillRecipeManager.getPlankProduct(logStack, util.getWorld());
if (result.isEmpty()) {
return;
}
util.addStack(LumbermillMachine.SLOT_PLANKS, result);
@@ -34,7 +34,7 @@ public class LumbermillMachine extends ExtraTreeMachine.PackageExtraTreeMachine
final ComponentInventorySlots inventory = new ComponentInventorySlots(machine);
InventorySlot slotLog = inventory.addSlot(SLOT_LOG, "input");
slotLog.setValidator(new SlotValidatorLog());
slotLog.setValidator(new SlotValidatorLog(machine.getWorld()));
slotLog.forbidExtraction();
inventory.addSlot(SLOT_PLANKS, "output").setReadOnly();
@@ -1,34 +1,40 @@
package binnie.extratrees.machines.lumbermill.recipes;
import javax.annotation.Nullable;
import binnie.core.util.UniqueItemStackSet;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import java.util.Collection;
import java.util.List;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.InventoryCrafting;
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.util.NonNullList;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.registry.ForgeRegistries;
import net.minecraftforge.oredict.OreDictionary;
import binnie.core.util.FakeCraftingWorld;
import binnie.core.util.OreDictionaryUtil;
import binnie.extratrees.api.recipes.ILumbermillManager;
import binnie.extratrees.api.recipes.ILumbermillRecipe;
public class LumbermillRecipeManager implements ILumbermillManager {
//Map<input log item, Pair<input log, output planks>>
private static Multimap<Item, ILumbermillRecipe> recipes = ArrayListMultimap.create();
private static boolean calculatedRecipes = false;
public static ItemStack getPlankProduct(final ItemStack logStack) {
if (recipes.isEmpty()) {
calculateProducts();
public static ItemStack getPlankProduct(ItemStack logStack, World world) {
if (!calculatedRecipes) {
calculateProducts(world);
calculatedRecipes = true;
}
Item logItem = logStack.getItem();
@@ -40,44 +46,60 @@ public class LumbermillRecipeManager implements ILumbermillManager {
return ItemStack.EMPTY;
}
public static void calculateProducts() {
private static void calculateProducts(World world) {
InventoryCrafting fakeCraftingInventory = new InventoryCrafting(new FakeCraftingHandler(), 3, 3){};
NonNullList<ItemStack> oreDictLogs = OreDictionary.getOres("logWood");
UniqueItemStackSet logsSet = new UniqueItemStackSet();
for (ItemStack logStack : oreDictLogs) {
List<ItemStack> subtypes = getSubtypes(logStack);
logsSet.addAll(subtypes);
}
NonNullList<ItemStack> logs = OreDictionary.getOres("logWood");
for (ItemStack logStack : logs) {
if (logStack.getItemDamage() == OreDictionary.WILDCARD_VALUE) {
for (int i = 0; i < 16; i++) {
ItemStack logCopy = logStack.copy();
logCopy.setCount(1);
logCopy.setItemDamage(i);
for (ItemStack log : logsSet) {
ItemStack logCopy = log.copy();
logCopy.setCount(1);
fakeCraftingInventory.clear();
fakeCraftingInventory.setInventorySlotContents(0, logCopy.copy());
fakeCraftingInventory.clear();
fakeCraftingInventory.setInventorySlotContents(0, logCopy);
IRecipe recipe = CraftingManager.findMatchingRecipe(fakeCraftingInventory, FakeCraftingWorld.getInstance());
if (recipe != null) {
ItemStack recipeOutput = recipe.getCraftingResult(fakeCraftingInventory);
if (!recipeOutput.isEmpty()) {
if (OreDictionaryUtil.hasOreName(recipeOutput, "plankWood")) {
addLogToPlankRecipe(logCopy.copy(), recipeOutput.copy());
}
}
}
}
} else {
fakeCraftingInventory.clear();
fakeCraftingInventory.setInventorySlotContents(0, logStack.copy());
ItemStack recipeOutput = getRecipeWithPlanksOutput(fakeCraftingInventory, world);
if (recipeOutput != null) {
addLogToPlankRecipe(logCopy, recipeOutput.copy());
}
}
}
IRecipe recipe = CraftingManager.findMatchingRecipe(fakeCraftingInventory, FakeCraftingWorld.getInstance());
if (recipe != null) {
ItemStack recipeOutput = recipe.getCraftingResult(fakeCraftingInventory);
if (!recipeOutput.isEmpty()) {
if (OreDictionaryUtil.hasOreName(recipeOutput, "plankWood")) {
addLogToPlankRecipe(logStack.copy(), recipeOutput.copy());
}
@Nullable
private static ItemStack getRecipeWithPlanksOutput(InventoryCrafting fakeCraftingInventory, World world) {
for (IRecipe recipe : ForgeRegistries.RECIPES.getValues()) {
boolean matches;
try {
matches = recipe.matches(fakeCraftingInventory, world);
} catch (RuntimeException | LinkageError e) {
matches = false;
}
if (matches) {
ItemStack recipeOutput = recipe.getCraftingResult(fakeCraftingInventory);
if (!recipeOutput.isEmpty()) {
if (OreDictionaryUtil.hasOreName(recipeOutput, "plankWood")) {
return recipeOutput;
}
}
}
}
return null;
}
private static List<ItemStack> getSubtypes(ItemStack itemStack) {
NonNullList<ItemStack> subtypes = NonNullList.create();
if (itemStack.getItemDamage() == OreDictionary.WILDCARD_VALUE && itemStack.getHasSubtypes()) {
Item item = itemStack.getItem();
for (CreativeTabs creativeTab : item.getCreativeTabs()) {
item.getSubItems(creativeTab, subtypes);
}
} else {
subtypes.add(itemStack);
}
return subtypes;
}
private static void addLogToPlankRecipe(ItemStack logStack, ItemStack plankStack){
@@ -79,8 +79,8 @@ public class ControlLumbermillProgress extends ControlProgressBase {
//CraftGUI.Render.iconBlock(new IPoint(1 + i * 16, 1), icon);
RenderUtil.drawSprite(new Point(1 + i * 16, 1), icon);
}
final ItemStack result = LumbermillRecipeManager.getPlankProduct(item);
if (result == null) {
final ItemStack result = LumbermillRecipeManager.getPlankProduct(item, Minecraft.getMinecraft().world);
if (result.isEmpty()) {
return;
}
Block block2 = null;
@@ -5,15 +5,19 @@ import net.minecraft.item.ItemStack;
import binnie.core.machines.inventory.SlotValidator;
import binnie.core.util.I18N;
import binnie.extratrees.machines.lumbermill.recipes.LumbermillRecipeManager;
import net.minecraft.world.World;
public class SlotValidatorLog extends SlotValidator {
public SlotValidatorLog() {
private final World world;
public SlotValidatorLog(World world) {
super(SlotValidator.spriteBlock);
this.world = world;
}
@Override
public boolean isValid(final ItemStack itemStack) {
ItemStack plank = LumbermillRecipeManager.getPlankProduct(itemStack);
ItemStack plank = LumbermillRecipeManager.getPlankProduct(itemStack, world);
return !plank.isEmpty();
}
@@ -72,6 +72,5 @@ public class ModuleMachine extends Module {
recipeUtil.addRecipe("panelworker", ExtraTreeMachine.Panelworker.get(1), "wGw", "GsG", "ggg", 'G', Blocks.GLASS, 'g', ExtraTreeItems.PROVEN_GEAR.get(1), 'w', Blocks.WOODEN_SLAB, 's', Mods.Forestry.stack("impregnated_casing"));
recipeUtil.addRecipe("glassworker", ExtraTreeMachine.Glassworker.get(1), "wGw", "GsG", "ggg", 'G', Blocks.GLASS, 'g', ExtraTreeItems.PROVEN_GEAR.get(1), 'w', Blocks.GLASS, 's', Mods.Forestry.stack("impregnated_casing"));
}
LumbermillRecipeManager.calculateProducts();
}
}