Merge pull request #11 from Dilnu/master

Implement Botania compatibility. Closes #9
This commit is contained in:
James McKay authored and GitHub committed 2016-11-07 16:43:01 +11:00
commit 65a4d9ed2d
3 files changed
+100 -11

No files matched your search

@@ -0,0 +1,40 @@
/**
* This class was created by <Vazkii>. It's distributed as
* part of the Botania Mod. Get the Source Code in github:
* https://github.com/Vazkii/Botania
*
* Botania is Open Source and distributed under the
* Botania License: http://botaniamod.net/license.php
*
* File Created @ [Aug 20, 2015, 10:31:54 PM (GMT)]
*/
package vazkii.botania.api.item;
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
/**
* An Item that implements this can provide blocks to other items that use them.
* For example, the Black Hole Talisman implements this in order to allow for
* the Rod of the Shifting Crust to pull blocks from it.
*/
public interface IBlockProvider {
/**
* Provides the requested item. The doit paremeter specifies whether this is
* just a test (false) or if the item should actually be removed (true).
* If you need to use calls to ManaItemHandler.requestMana[Exact], use
* the requestor as the ItemStack passed in.
*/
public boolean provideBlock(EntityPlayer player, ItemStack requestor, ItemStack stack, Block block, int meta, boolean doit);
/**
* Gets the amount of blocks of the type passed stored in this item. You must
* check for the block passed in to not give the counter for a wrong block. Returning
* -1 states that the item can provide infinite of the item passed in (for example,
* the Rod of the Lands would return -1 if the block is dirt).
*/
public int getBlockCount(EntityPlayer player, ItemStack requestor, ItemStack stack, Block block, int meta);
}
@@ -77,16 +77,18 @@ public abstract class ItemBasicWand extends Item implements IWandItem{
placedIntArray[i * 3 + 1] = currentPoint.y;
placedIntArray[i * 3 + 2] = currentPoint.z;
}
NBTTagCompound itemNBT = itemstack.getTagCompound();
NBTTagCompound bbwCompond = new NBTTagCompound();
if(itemNBT.hasKey("bbw", Constants.NBT.TAG_COMPOUND)) {
bbwCompond = itemNBT.getCompoundTag("bbw");
NBTTagCompound bbwCompound = new NBTTagCompound();
if(itemstack.hasTagCompound()) {
NBTTagCompound itemNBT = itemstack.getTagCompound();
if (itemNBT.hasKey("bbw", Constants.NBT.TAG_COMPOUND)) {
bbwCompound = itemNBT.getCompoundTag("bbw");
}
}
bbwCompond.setIntArray("lastPlaced", placedIntArray);
bbwCompond.setString("lastBlock", Item.itemRegistry.getNameForObject(sourceItems.getItem()));
bbwCompond.setInteger("lastDamage", sourceItems.getItemDamage());
bbwCompond.setInteger("lastPerBlock", sourceItems.stackSize);
itemstack.setTagInfo("bbw", bbwCompond);
bbwCompound.setIntArray("lastPlaced", placedIntArray);
bbwCompound.setString("lastBlock", Item.itemRegistry.getNameForObject(sourceItems.getItem()));
bbwCompound.setInteger("lastDamage", sourceItems.getItemDamage());
bbwCompound.setInteger("lastPerBlock", sourceItems.stackSize);
itemstack.setTagInfo("bbw", bbwCompound);
}
}
@@ -1,20 +1,42 @@
package portablejim.bbw.shims;
import cpw.mods.fml.common.FMLCommonHandler;
import net.minecraft.block.Block;
import net.minecraft.init.Blocks;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.MinecraftForge;
import portablejim.bbw.basics.Point3d;
import vazkii.botania.api.item.IBlockProvider;
import java.util.ArrayList;
import java.util.List;
/**
* Wrap a player to provide basic functions.
*/
public class BasicPlayerShim implements IPlayerShim {
private EntityPlayer player;
private boolean providersEnabled;
public BasicPlayerShim(EntityPlayer player) {
this.player = player;
this.providersEnabled = areProvidersEnabled();
}
private static Block getBlock(ItemStack stack) {
return Block.getBlockFromItem(stack.getItem());
}
private static int getBlockMeta(ItemStack stack) {
return stack.getHasSubtypes() ? stack.getItemDamage() : 0;
}
private static boolean areProvidersEnabled() {
try {
boolean disable = new Object() instanceof IBlockProvider;
return true;
} catch (NoClassDefFoundError e) {
return false;
}
}
@Override
@@ -24,11 +46,20 @@ public class BasicPlayerShim implements IPlayerShim {
return 0;
}
Block block = getBlock(itemStack);
int meta = getBlockMeta(itemStack);
for(ItemStack inventoryStack : player.inventory.mainInventory) {
if(inventoryStack != null && itemStack.isItemEqual(inventoryStack)) {
total += Math.max(0, inventoryStack.stackSize);
}
else if(providersEnabled && inventoryStack != null && inventoryStack.getItem() instanceof IBlockProvider) {
IBlockProvider prov = (IBlockProvider) inventoryStack.getItem();
int provCount = prov.getBlockCount(player, itemStack, inventoryStack, block, meta);
if(provCount == -1)
return Integer.MAX_VALUE;
total += provCount;
}
}
return itemStack.stackSize > 0 ? total / itemStack.stackSize : 0;
@@ -42,6 +73,7 @@ public class BasicPlayerShim implements IPlayerShim {
// Reverse direction to leave hotbar to last.
int toUse = itemStack.stackSize;
List<ItemStack> providers = new ArrayList<ItemStack>();
for(int i = player.inventory.mainInventory.length - 1; i >= 0; i--) {
ItemStack inventoryStack = player.inventory.mainInventory[i];
if(inventoryStack != null && itemStack.isItemEqual(inventoryStack)) {
@@ -61,7 +93,22 @@ public class BasicPlayerShim implements IPlayerShim {
return true;
}
}
else if(providersEnabled && inventoryStack != null && inventoryStack.getItem() instanceof IBlockProvider) {
providers.add(inventoryStack);
}
}
// IBlockProvider does not support removing more than one item in an atomic operation.
if (toUse == 1) {
Block block = getBlock(itemStack);
int meta = getBlockMeta(itemStack);
for(ItemStack provStack : providers) {
IBlockProvider prov = (IBlockProvider) provStack.getItem();
if(prov.provideBlock(player, itemStack, provStack, block, meta, true))
return true;
}
}
return false;
}