Skip to content

Commit

Permalink
Fix #395 Crashes without BuildCraft
Browse files Browse the repository at this point in the history
  • Loading branch information
mezz committed Dec 21, 2014
1 parent 8d5b197 commit 4432b0d
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 145 deletions.
21 changes: 21 additions & 0 deletions src/main/java/buildcraft/LICENSE-API
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
BuildCraft API License

Copyright (c) 2011-2014 SpaceToad and the BuildCraft team

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
40 changes: 40 additions & 0 deletions src/main/java/buildcraft/api/tools/IToolWrench.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Copyright (c) 2011-2014, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
*
* The BuildCraft API is distributed under the terms of the MIT License.
* Please check the contents of the license, which should be located
* as "LICENSE.API" in the BuildCraft source code distribution.
*/
package buildcraft.api.tools;

import net.minecraft.entity.player.EntityPlayer;

/***
* Implement this interface on subclasses of Item to have that item work as a wrench for buildcraft
*/
public interface IToolWrench {

/***
* Called to ensure that the wrench can be used. To get the ItemStack that is used, check player.inventory.getCurrentItem()
*
* @param player
* - The player doing the wrenching
* @param x
* ,y,z - The coordinates for the block being wrenched
*
* @return true if wrenching is allowed, false if not
*/
boolean canWrench(EntityPlayer player, int x, int y, int z);

/***
* Callback after the wrench has been used. This can be used to decrease durability or for other purposes. To get the ItemStack that was used, check
* player.inventory.getCurrentItem()
*
* @param player
* - The player doing the wrenching
* @param x
* ,y,z - The coordinates of the block being wrenched
*/
void wrenchUsed(EntityPlayer player, int x, int y, int z);
}
11 changes: 11 additions & 0 deletions src/main/java/buildcraft/api/tools/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Copyright (c) 2011-2014, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
*
* The BuildCraft API is distributed under the terms of the MIT License.
* Please check the contents of the license, which should be located
* as "LICENSE.API" in the BuildCraft source code distribution.
*/
@API(apiVersion = "1.0", owner = "BuildCraftAPI|core", provides = "BuildCraftAPI|tools")
package buildcraft.api.tools;
import cpw.mods.fml.common.API;
16 changes: 12 additions & 4 deletions src/main/java/forestry/core/inventory/AdjacentInventoryCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,20 @@
******************************************************************************/
package forestry.core.inventory;

import forestry.core.utils.AdjacentTileCache;
import java.util.*;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;

import net.minecraft.inventory.IInventory;
import net.minecraft.tileentity.TileEntity;

import net.minecraftforge.common.util.ForgeDirection;

import forestry.core.utils.AdjacentTileCache;

/**
*
* @author CovertJaguar <http://www.railcraft.info/>
Expand All @@ -40,12 +48,12 @@ public AdjacentInventoryCache(TileEntity tile, AdjacentTileCache cache, ITileFil
cache.addListener(this);
}

public IInventory getAdjecentInventory(ForgeDirection side) {
public IInventory getAdjacentInventory(ForgeDirection side) {
checkChanged();
return sides[side.ordinal()];
}

public Collection<IInventory> getAdjecentInventories() {
public Collection<IInventory> getAdjacentInventories() {
checkChanged();
return invs;
}
Expand Down
57 changes: 40 additions & 17 deletions src/main/java/forestry/core/inventory/InvTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,14 @@
******************************************************************************/
package forestry.core.inventory;

import buildcraft.api.transport.IPipeTile;
import buildcraft.api.transport.PipeManager;
import forestry.core.inventory.filters.ArrayStackFilter;
import forestry.core.inventory.filters.IStackFilter;
import forestry.core.inventory.filters.InvertedStackFilter;
import forestry.core.inventory.filters.StackFilter;
import forestry.core.inventory.manipulators.InventoryManipulator;
import forestry.core.inventory.wrappers.ChestWrapper;
import forestry.core.inventory.wrappers.IInvSlot;
import forestry.core.inventory.wrappers.InventoryCopy;
import forestry.core.inventory.wrappers.InventoryIterator;
import forestry.core.inventory.wrappers.InventoryMapper;
import forestry.core.inventory.wrappers.SidedInventoryMapper;
import forestry.core.utils.AdjacentTileCache;
import forestry.core.utils.PlainInventory;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import net.minecraft.block.Block;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.inventory.IInventory;
Expand All @@ -43,9 +29,30 @@
import net.minecraft.tileentity.TileEntityChest;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;

import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.oredict.OreDictionary;

import cpw.mods.fml.common.Optional;

import forestry.core.inventory.filters.ArrayStackFilter;
import forestry.core.inventory.filters.IStackFilter;
import forestry.core.inventory.filters.InvertedStackFilter;
import forestry.core.inventory.filters.StackFilter;
import forestry.core.inventory.manipulators.InventoryManipulator;
import forestry.core.inventory.wrappers.ChestWrapper;
import forestry.core.inventory.wrappers.IInvSlot;
import forestry.core.inventory.wrappers.InventoryCopy;
import forestry.core.inventory.wrappers.InventoryIterator;
import forestry.core.inventory.wrappers.InventoryMapper;
import forestry.core.inventory.wrappers.SidedInventoryMapper;
import forestry.core.utils.AdjacentTileCache;
import forestry.core.utils.PlainInventory;
import forestry.plugins.PluginManager;

import buildcraft.api.transport.IPipeTile;
import buildcraft.api.transport.PipeManager;

public abstract class InvTools {

/*private static final String TAG_ID = "id";
Expand Down Expand Up @@ -144,8 +151,10 @@ public static IInventory getInventoryFromTile(TileEntity tile, ForgeDirection si
if (tile == null || !(tile instanceof IInventory))
return null;

if (!PipeManager.canExtractItems(null, tile.getWorldObj(), tile.xCoord, tile.yCoord, tile.zCoord))
return null;
if (PluginManager.Module.BUILDCRAFT_TRANSPORT.isEnabled()) {
if(!internal_canExtractFromBuildCraftPipe(tile))
return null;
}

if (tile instanceof TileEntityChest) {
TileEntityChest chest = (TileEntityChest) tile;
Expand All @@ -154,6 +163,11 @@ public static IInventory getInventoryFromTile(TileEntity tile, ForgeDirection si
return getInventory((IInventory) tile, side);
}

@Optional.Method(modid = "BuildCraftAPI|transport")
private static boolean internal_canExtractFromBuildCraftPipe(TileEntity tile) {
return PipeManager.canExtractItems(null, tile.getWorldObj(), tile.xCoord, tile.yCoord, tile.zCoord);
}

public static IInventory getInventory(IInventory inv, ForgeDirection side) {
if (inv == null)
return null;
Expand Down Expand Up @@ -741,6 +755,15 @@ public static ItemStack removeOneItem(Iterable<IInventory> invs, IStackFilter fi
* @return true if an item was inserted, otherwise false.
*/
public static boolean moveOneItemToPipe(IInventory source, AdjacentTileCache tileCache) {
if (PluginManager.Module.BUILDCRAFT_TRANSPORT.isEnabled()) {
return internal_moveOneItemToPipe(source, tileCache);
}

return false;
}

@Optional.Method(modid = "BuildCraftAPI|transport")
private static boolean internal_moveOneItemToPipe(IInventory source, AdjacentTileCache tileCache) {
IInventory invClone = new InventoryCopy(source);
ItemStack stackToMove = removeOneItem(invClone);
if (stackToMove == null)
Expand Down
81 changes: 0 additions & 81 deletions src/main/java/forestry/core/utils/BlockUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
package forestry.core.utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;

import net.minecraft.block.Block;
import net.minecraft.block.BlockCocoa;
Expand All @@ -28,11 +26,6 @@
import forestry.core.config.Defaults;
import forestry.core.vect.Vect;

import buildcraft.api.core.Position;
import buildcraft.api.transport.IPipeConnection;
import buildcraft.api.transport.IPipeConnection.ConnectOverride;
import buildcraft.api.transport.IPipeTile;
import buildcraft.api.transport.IPipeTile.PipeType;
import cofh.api.energy.IEnergyHandler;

public class BlockUtil {
Expand All @@ -45,80 +38,6 @@ public static ArrayList<ItemStack> getBlockItemStack(World world, Vect posBlock)

}

/**
* Returns a list of adjacent pipes.
*/
public static ForgeDirection[] getPipeDirections(World world, Vect blockPos, ForgeDirection from) {
LinkedList<ForgeDirection> possiblePipes = new LinkedList<ForgeDirection>();

for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) {
if (from != ForgeDirection.UNKNOWN && from != dir.getOpposite())
continue;

Position posPipe = new Position(blockPos.x, blockPos.y, blockPos.z, dir);
posPipe.moveForwards(1.0);

TileEntity pipeEntry = world.getTileEntity((int) posPipe.x, (int) posPipe.y, (int) posPipe.z);

if (pipeEntry instanceof IPipeTile) {
IPipeTile pipe = (IPipeTile) pipeEntry;

if (from != ForgeDirection.UNKNOWN && pipeEntry instanceof IPipeConnection) {
if (((IPipeConnection) pipeEntry).overridePipeConnection(PipeType.ITEM, from) != ConnectOverride.DISCONNECT)
possiblePipes.add(dir);
} else if (pipe.getPipeType() == PipeType.ITEM && pipe.isPipeConnected(dir.getOpposite()))
possiblePipes.add(dir);
}
}

return possiblePipes.toArray(new ForgeDirection[possiblePipes.size()]);

}

public static ArrayList<ForgeDirection> filterPipeDirections(ForgeDirection[] allDirections, ForgeDirection[] exclude) {
ArrayList<ForgeDirection> filtered = new ArrayList<ForgeDirection>();
ArrayList<ForgeDirection> excludeList = new ArrayList<ForgeDirection>(Arrays.asList(exclude));

for (ForgeDirection direction : allDirections) {
if (!excludeList.contains(direction))
filtered.add(direction);
}

return filtered;

}

public static boolean putFromStackIntoPipe(TileEntity tile, ArrayList<ForgeDirection> pipes, ItemStack stack) {

if (stack == null)
return false;
if (stack.stackSize <= 0)
return false;
if (pipes.size() <= 0)
return false;

int choice = tile.getWorldObj().rand.nextInt(pipes.size());
Position itemPos = new Position(tile.xCoord, tile.yCoord, tile.zCoord, pipes.get(choice));

itemPos.x += 0.5;
itemPos.y += 0.25;
itemPos.z += 0.5;
itemPos.moveForwards(0.5);

Position pipePos = new Position(tile.xCoord, tile.yCoord, tile.zCoord, pipes.get(choice));
pipePos.moveForwards(1.0);

IPipeTile pipe = (IPipeTile) tile.getWorldObj().getTileEntity((int) pipePos.x, (int) pipePos.y, (int) pipePos.z);

ItemStack payload = stack.splitStack(1);
if (pipe.injectItem(payload, true, itemPos.orientation.getOpposite()) > 0)
return true;
else
pipes.remove(choice);

return false;
}

public static boolean isRFTile(ForgeDirection side, TileEntity tile) {
if (tile == null)
return false;
Expand Down
Loading

0 comments on commit 4432b0d

Please sign in to comment.