New Binding API stuffs.

This commit is contained in:
Lizbeth Rika committed 2014-10-09 15:34:39 +01:00
1 parent d05e5cbb12
commit 6c08955468
6 files changed
+144 -59

No files matched your search

+1 -1
View File
@@ -1,4 +1,4 @@
@API(owner = "Botania", apiVersion = "15", provides = "BotaniaAPI")
@API(owner = "Botania", apiVersion = "16", provides = "BotaniaAPI")
package vazkii.botania.api;
import cpw.mods.fml.common.API;
@@ -21,6 +21,7 @@ import net.minecraft.util.ChunkCoordinates;
import net.minecraft.util.IIcon;
import vazkii.botania.api.BotaniaAPI;
import vazkii.botania.api.lexicon.LexiconEntry;
import vazkii.botania.api.wand.IWandBindable;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
@@ -99,6 +100,20 @@ public class SubTileEntity {
public ChunkCoordinates getBinding() {
return null;
}
/**
* @see IWandBindable#canSelect(EntityPlayer, ItemStack, int, int, int, int)
*/
public boolean canSelect(EntityPlayer player, ItemStack wand, int x, int y, int z, int side) {
return false;
}
/**
* @see IWandBindable#bindTo(EntityPlayer, ItemStack, int, int, int, int)
*/
public boolean bindTo(EntityPlayer player, ItemStack wand, int x, int y, int z, int side) {
return false;
}
/**
* Called on the client when the block being pointed at is the one with this sub tile.
@@ -0,0 +1,36 @@
/**
* 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 a
* Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License
* (http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en_GB)
*
* File Created @ [Oct 9, 2014, 3:01:58 PM (GMT)]
*/
package vazkii.botania.api.wand;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.MovingObjectPosition;
/**
* A TileEntity that implements this can be bound to another block
* via the Wand of the Forest. Also see IWireframeAABBProvider to change
* the displayed bounding box.
*/
public interface IWandBindable extends ITileBound {
/**
* Return true if the Wand can select this tile.
*/
public boolean canSelect(EntityPlayer player, ItemStack wand, int x, int y, int z, int side);
/**
* Call to bind the TileEntity to where the player clicked. Return true to deselect
* the TileEntity for another bind or false case the TileEntity should stay selected.
*/
public boolean bindTo(EntityPlayer player, ItemStack wand, int x, int y, int z, int side);
}
@@ -24,8 +24,9 @@ import vazkii.botania.api.lexicon.LexiconEntry;
import vazkii.botania.api.subtile.ISubTileContainer;
import vazkii.botania.api.subtile.SubTileEntity;
import vazkii.botania.api.wand.ITileBound;
import vazkii.botania.api.wand.IWandBindable;
public class TileSpecialFlower extends TileMod implements ITileBound, ISubTileContainer {
public class TileSpecialFlower extends TileMod implements IWandBindable, ISubTileContainer {
private static final String TAG_SUBTILE_NAME = "subTileName";
private static final String TAG_SUBTILE_CMP = "subTileCmp";
@@ -121,4 +122,18 @@ public class TileSpecialFlower extends TileMod implements ITileBound, ISubTileCo
return null;
return subTile.getBinding();
}
@Override
public boolean canSelect(EntityPlayer player, ItemStack wand, int x, int y, int z, int side) {
if(subTile == null)
return false;
return subTile.canSelect(player, wand, x, y, z, side);
}
@Override
public boolean bindTo(EntityPlayer player, ItemStack wand, int x, int y, int z, int side) {
if(subTile == null)
return false;
return subTile.bindTo(player, wand, x, y, z, side);
}
}
@@ -24,6 +24,7 @@ import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.ChunkCoordinates;
import net.minecraft.util.MathHelper;
import net.minecraft.util.MovingObjectPosition;
@@ -43,7 +44,7 @@ import vazkii.botania.api.mana.IManaCollector;
import vazkii.botania.api.mana.IManaPool;
import vazkii.botania.api.mana.IManaReceiver;
import vazkii.botania.api.mana.ManaNetworkEvent;
import vazkii.botania.api.wand.ITileBound;
import vazkii.botania.api.wand.IWandBindable;
import vazkii.botania.client.core.handler.HUDHandler;
import vazkii.botania.client.lib.LibResources;
import vazkii.botania.common.block.ModBlocks;
@@ -54,7 +55,7 @@ import vazkii.botania.common.entity.EntityManaBurst;
import vazkii.botania.common.entity.EntityManaBurst.PositionProperties;
import vazkii.botania.common.lib.LibBlockNames;
public class TileSpreader extends TileSimpleInventory implements IManaCollector, ITileBound, IKeyLocked {
public class TileSpreader extends TileSimpleInventory implements IManaCollector, IWandBindable, IKeyLocked {
private static final int MAX_MANA = 1000;
private static final int ULTRA_MAX_MANA = 6400;
@@ -535,4 +536,41 @@ public class TileSpreader extends TileSimpleInventory implements IManaCollector,
public String getOutputKey() {
return outputKey;
}
@Override
public boolean canSelect(EntityPlayer player, ItemStack wand, int x, int y, int z, int side) {
return true;
}
@Override
public boolean bindTo(EntityPlayer player, ItemStack wand, int x, int y, int z, int side) {
Vector3 thisVec = Vector3.fromTileEntityCenter(this);
Vector3 blockVec = new Vector3(x + 0.5, y + 0.5, z + 0.5);
AxisAlignedBB axis = player.worldObj.getBlock(x, y, z).getCollisionBoundingBoxFromPool(player.worldObj, x, y, z);
if(axis == null)
axis = AxisAlignedBB.getBoundingBox(x, y, z, x + 1, y + 1, z + 1);
if(!blockVec.isInside(axis))
blockVec = new Vector3(axis.minX + (axis.maxX - axis.minX) / 2, axis.minY + (axis.maxY - axis.minY) / 2, axis.minZ + (axis.maxZ - axis.minZ) / 2);
Vector3 diffVec = blockVec.copy().sub(thisVec);
Vector3 diffVec2D = new Vector3(diffVec.x, diffVec.z, 0);
Vector3 rotVec = new Vector3(0, 1, 0);
double angle = rotVec.angle(diffVec2D) / Math.PI * 180.0;
if(blockVec.x < thisVec.x)
angle = -angle;
rotationX = (float) angle + 90;
rotVec = new Vector3(diffVec.x, 0, diffVec.z);
angle = diffVec.angle(rotVec) * 180F / Math.PI;
if(blockVec.y < thisVec.y)
angle = -angle;
rotationY = (float) angle;
checkForReceiver();
return true;
}
}
@@ -34,6 +34,7 @@ import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import vazkii.botania.api.wand.ICoordBoundItem;
import vazkii.botania.api.wand.ITileBound;
import vazkii.botania.api.wand.IWandBindable;
import vazkii.botania.api.wand.IWandable;
import vazkii.botania.client.core.helper.IconHelper;
import vazkii.botania.common.Botania;
@@ -44,6 +45,7 @@ import vazkii.botania.common.block.tile.mana.TileSpreader;
import vazkii.botania.common.core.handler.ConfigHandler;
import vazkii.botania.common.core.helper.ItemNBTHelper;
import vazkii.botania.common.core.helper.Vector3;
import vazkii.botania.common.item.equipment.tool.ToolCommons;
import vazkii.botania.common.lib.LibItemNames;
public class ItemTwigWand extends Item16Colors implements ICoordBoundItem {
@@ -53,9 +55,9 @@ public class ItemTwigWand extends Item16Colors implements ICoordBoundItem {
private static final String TAG_COLOR1 = "color1";
private static final String TAG_COLOR2 = "color2";
private static final String TAG_TICKS_SINCE_SPREADER = "ticksSinceSpreader";
private static final String TAG_SPREADER_X = "spreaderX";
private static final String TAG_SPREADER_Y = "spreaderY";
private static final String TAG_SPREADER_Z = "spreaderZ";
private static final String TAG_BOUND_TILE_X = "boundTileX";
private static final String TAG_BOUND_TILE_Y = "boundTileY";
private static final String TAG_BOUND_TILE_Z = "boundTileZ";
public ItemTwigWand() {
super(LibItemNames.TWIG_WAND);
@@ -65,43 +67,18 @@ public class ItemTwigWand extends Item16Colors implements ICoordBoundItem {
@Override
public boolean onItemUse(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, World par3World, int par4, int par5, int par6, int par7, float par8, float par9, float par10) {
Block block = par3World.getBlock(par4, par5, par6);
ChunkCoordinates boundSpreader = getBoundSpreader(par1ItemStack);
ChunkCoordinates boundTile = getBoundTile(par1ItemStack);
if(boundSpreader.posY != -1 && par2EntityPlayer.isSneaking() && (boundSpreader.posX != par4 || boundSpreader.posY != par5 || boundSpreader.posZ != par6)) {
TileEntity tile = par3World.getTileEntity(boundSpreader.posX, boundSpreader.posY, boundSpreader.posZ);
if(tile instanceof TileSpreader) {
TileSpreader spreader = (TileSpreader) tile;
if(boundTile.posY != -1 && par2EntityPlayer.isSneaking() && (boundTile.posX != par4 || boundTile.posY != par5 || boundTile.posZ != par6)) {
TileEntity tile = par3World.getTileEntity(boundTile.posX, boundTile.posY, boundTile.posZ);
if(tile instanceof IWandBindable) {
if(((IWandBindable) tile).bindTo(par2EntityPlayer, par1ItemStack, par4, par5, par6, par7)) {
par3World.markBlockForUpdate(boundTile.posX, boundTile.posY, boundTile.posZ);
setBoundTile(par1ItemStack, 0, -1, 0);
}
Vector3 spreaderVec = Vector3.fromTileEntityCenter(spreader);
Vector3 blockVec = new Vector3(par4 + 0.5, par5 + 0.5, par6 + 0.5);
AxisAlignedBB axis = par3World.getBlock(par4, par5, par6).getCollisionBoundingBoxFromPool(par3World, par4, par5, par6);
if(axis == null)
axis = AxisAlignedBB.getBoundingBox(par4, par5, par6, par4 + 1, par5 + 1, par6 + 1);
if(!blockVec.isInside(axis))
blockVec = new Vector3(axis.minX + (axis.maxX - axis.minX) / 2, axis.minY + (axis.maxY - axis.minY) / 2, axis.minZ + (axis.maxZ - axis.minZ) / 2);
Vector3 diffVec = blockVec.copy().sub(spreaderVec);
Vector3 diffVec2D = new Vector3(diffVec.x, diffVec.z, 0);
Vector3 rotVec = new Vector3(0, 1, 0);
double angle = rotVec.angle(diffVec2D) / Math.PI * 180.0;
if(blockVec.x < spreaderVec.x)
angle = -angle;
spreader.rotationX = (float) angle + 90;
rotVec = new Vector3(diffVec.x, 0, diffVec.z);
angle = diffVec.angle(rotVec) * 180F / Math.PI;
if(blockVec.y < spreaderVec.y)
angle = -angle;
spreader.rotationY = (float) angle;
spreader.checkForReceiver();
par3World.markBlockForUpdate(boundSpreader.posX, boundSpreader.posY, boundSpreader.posZ);
return true;
} else setBoundSpreader(par1ItemStack, 0, -1, 0);
} else setBoundTile(par1ItemStack, 0, -1, 0);
} else if(par2EntityPlayer.isSneaking()) {
block.rotateBlock(par3World, par4, par5, par6, ForgeDirection.getOrientation(par7));
if(par3World.isRemote)
@@ -133,19 +110,23 @@ public class ItemTwigWand extends Item16Colors implements ICoordBoundItem {
}
}
} else if(block instanceof IWandable) {
boolean spreader = par3World.getTileEntity(par4, par5, par6) instanceof TileSpreader;
TileEntity tile = par3World.getTileEntity(par4, par5, par6);
boolean spreader = tile instanceof TileSpreader;
boolean bindable = tile instanceof IWandBindable;
int ticks = getTicksSinceSpreaderUse(par1ItemStack);
boolean wanded = false;
if(spreader && ticks == 5 && par2EntityPlayer.isSneaking()) {
if(boundSpreader.posX == par4 && boundSpreader.posY == par5 && boundSpreader.posZ == par6)
setBoundSpreader(par1ItemStack, 0, -1, 0);
else setBoundSpreader(par1ItemStack, par4, par5, par6);
if(bindable && (!spreader || ticks == 5) && par2EntityPlayer.isSneaking() && ((IWandBindable) tile).canSelect(par2EntityPlayer, par1ItemStack, par4, par5, par6, par7)) {
if(boundTile.posX == par4 && boundTile.posY == par5 && boundTile.posZ == par6)
setBoundTile(par1ItemStack, 0, -1, 0);
else setBoundTile(par1ItemStack, par4, par5, par6);
if(par3World.isRemote)
par2EntityPlayer.swingItem();
par3World.playSoundAtEntity(par2EntityPlayer, "botania:ding", 0.1F, 1F);
setTicksSinceSpreaderUse(par1ItemStack, 0);
if(spreader)
setTicksSinceSpreaderUse(par1ItemStack, 0);
wanded = true;
} else {
wanded = ((IWandable) block).onUsedByWand(par2EntityPlayer, par1ItemStack, par3World, par4, par5, par6, par7);
@@ -180,10 +161,10 @@ public class ItemTwigWand extends Item16Colors implements ICoordBoundItem {
if(ticks < 5)
setTicksSinceSpreaderUse(par1ItemStack, ticks + 1);
ChunkCoordinates coords = getBoundSpreader(par1ItemStack);
ChunkCoordinates coords = getBoundTile(par1ItemStack);
TileEntity tile = par2World.getTileEntity(coords.posX, coords.posY, coords.posZ);
if(tile == null || !(tile instanceof TileSpreader))
setBoundSpreader(par1ItemStack, 0, -1, 0);
if(tile == null || !(tile instanceof IWandBindable))
setBoundTile(par1ItemStack, 0, -1, 0);
}
@Override
@@ -262,22 +243,22 @@ public class ItemTwigWand extends Item16Colors implements ICoordBoundItem {
return ItemNBTHelper.getInt(stack, TAG_TICKS_SINCE_SPREADER, 20);
}
public static void setBoundSpreader(ItemStack stack, int x, int y, int z) {
ItemNBTHelper.setInt(stack, TAG_SPREADER_X, x);
ItemNBTHelper.setInt(stack, TAG_SPREADER_Y, y);
ItemNBTHelper.setInt(stack, TAG_SPREADER_Z, z);
public static void setBoundTile(ItemStack stack, int x, int y, int z) {
ItemNBTHelper.setInt(stack, TAG_BOUND_TILE_X, x);
ItemNBTHelper.setInt(stack, TAG_BOUND_TILE_Y, y);
ItemNBTHelper.setInt(stack, TAG_BOUND_TILE_Z, z);
}
public static ChunkCoordinates getBoundSpreader(ItemStack stack) {
int x = ItemNBTHelper.getInt(stack, TAG_SPREADER_X, 0);
int y = ItemNBTHelper.getInt(stack, TAG_SPREADER_Y, -1);
int z = ItemNBTHelper.getInt(stack, TAG_SPREADER_Z, 0);
public static ChunkCoordinates getBoundTile(ItemStack stack) {
int x = ItemNBTHelper.getInt(stack, TAG_BOUND_TILE_X, 0);
int y = ItemNBTHelper.getInt(stack, TAG_BOUND_TILE_Y, -1);
int z = ItemNBTHelper.getInt(stack, TAG_BOUND_TILE_Z, 0);
return new ChunkCoordinates(x, y, z);
}
@Override
public ChunkCoordinates getBinding(ItemStack stack) {
ChunkCoordinates bound = getBoundSpreader(stack);
ChunkCoordinates bound = getBoundTile(stack);
if(bound.posY != -1)
return bound;