From 9c315e755706fb8afa6e4d03c2d00ce0601f569f Mon Sep 17 00:00:00 2001 From: tterrag1098 Date: Mon, 31 Aug 2015 00:26:28 -0400 Subject: [PATCH] Add Immibis Microblocks support for conduits --- .../core/api/multipart/ICoverSystem.java | 27 ++++ .../core/api/multipart/ICoverableTile.java | 11 ++ .../IMultipartRenderingBlockMarker.java | 15 ++ .../core/api/multipart/IMultipartSystem.java | 65 ++++++++ .../core/api/multipart/IPartContainer.java | 128 +++++++++++++++ .../core/api/multipart/PartBreakEvent.java | 24 +++ .../core/api/multipart/PartCoordinates.java | 35 ++++ .../multipart/util/BlockMultipartBase.java | 100 ++++++++++++ .../java/mods/immibis/core/api/util/Dir.java | 13 ++ .../microblocks/api/EnumAxisPosition.java | 16 ++ .../microblocks/api/EnumPartClass.java | 10 ++ .../immibis/microblocks/api/EnumPosition.java | 131 +++++++++++++++ .../microblocks/api/EnumPositionClass.java | 10 ++ .../api/IMicroblockCoverSystem.java | 82 ++++++++++ .../api/IMicroblockIntegratedTile.java | 27 ++++ .../api/IMicroblockPermissionHandler.java | 35 ++++ .../api/IMicroblockSupporterTile.java | 27 ++++ .../microblocks/api/IMicroblockSystem.java | 48 ++++++ .../microblocks/api/IMicroblockSystem2.java | 32 ++++ .../microblocks/api/MicroblockAPIUtils.java | 70 ++++++++ .../mods/immibis/microblocks/api/Part.java | 100 ++++++++++++ .../immibis/microblocks/api/PartType.java | 105 ++++++++++++ .../immibis/microblocks/api/PartType2.java | 8 + .../api/util/TileCoverableBase.java | 78 +++++++++ .../api/util/TileCoverableMultipartBase.java | 60 +++++++ .../enderio/conduit/AbstractConduit.java | 23 +++ .../enderio/conduit/BlockConduitBundle.java | 102 ++++++++++-- .../enderio/conduit/IConduitBundle.java | 6 +- .../enderio/conduit/MicroblocksUtil.java | 40 +++++ .../enderio/conduit/TileConduitBundle.java | 151 +++++++++++++++++- 30 files changed, 1560 insertions(+), 19 deletions(-) create mode 100644 src/api/java/mods/immibis/core/api/multipart/ICoverSystem.java create mode 100644 src/api/java/mods/immibis/core/api/multipart/ICoverableTile.java create mode 100644 src/api/java/mods/immibis/core/api/multipart/IMultipartRenderingBlockMarker.java create mode 100644 src/api/java/mods/immibis/core/api/multipart/IMultipartSystem.java create mode 100644 src/api/java/mods/immibis/core/api/multipart/IPartContainer.java create mode 100644 src/api/java/mods/immibis/core/api/multipart/PartBreakEvent.java create mode 100644 src/api/java/mods/immibis/core/api/multipart/PartCoordinates.java create mode 100644 src/api/java/mods/immibis/core/api/multipart/util/BlockMultipartBase.java create mode 100644 src/api/java/mods/immibis/core/api/util/Dir.java create mode 100644 src/api/java/mods/immibis/microblocks/api/EnumAxisPosition.java create mode 100644 src/api/java/mods/immibis/microblocks/api/EnumPartClass.java create mode 100644 src/api/java/mods/immibis/microblocks/api/EnumPosition.java create mode 100644 src/api/java/mods/immibis/microblocks/api/EnumPositionClass.java create mode 100644 src/api/java/mods/immibis/microblocks/api/IMicroblockCoverSystem.java create mode 100644 src/api/java/mods/immibis/microblocks/api/IMicroblockIntegratedTile.java create mode 100644 src/api/java/mods/immibis/microblocks/api/IMicroblockPermissionHandler.java create mode 100644 src/api/java/mods/immibis/microblocks/api/IMicroblockSupporterTile.java create mode 100644 src/api/java/mods/immibis/microblocks/api/IMicroblockSystem.java create mode 100644 src/api/java/mods/immibis/microblocks/api/IMicroblockSystem2.java create mode 100644 src/api/java/mods/immibis/microblocks/api/MicroblockAPIUtils.java create mode 100644 src/api/java/mods/immibis/microblocks/api/Part.java create mode 100644 src/api/java/mods/immibis/microblocks/api/PartType.java create mode 100644 src/api/java/mods/immibis/microblocks/api/PartType2.java create mode 100644 src/api/java/mods/immibis/microblocks/api/util/TileCoverableBase.java create mode 100644 src/api/java/mods/immibis/microblocks/api/util/TileCoverableMultipartBase.java create mode 100644 src/main/java/crazypants/enderio/conduit/MicroblocksUtil.java diff --git a/src/api/java/mods/immibis/core/api/multipart/ICoverSystem.java b/src/api/java/mods/immibis/core/api/multipart/ICoverSystem.java new file mode 100644 index 00000000..001ee36b --- /dev/null +++ b/src/api/java/mods/immibis/core/api/multipart/ICoverSystem.java @@ -0,0 +1,27 @@ +package mods.immibis.core.api.multipart; + + +/** + * An object that allows extra parts to be added to multipart tiles. + * For example, you can add covers to InfiniTubes transport conduits + * because they use a MicroblockCoverSystem. + * + * This interface's existence is a bit unintuitive, but it seemed to "naturally fall out" of the design. + */ +public interface ICoverSystem extends IPartContainer { + /** + * Converts the block containing this cover system into + * a block containing only parts from this cover system, + * or to air if the cover system has no parts. + * + * Call it when, for example, all wires in a RedLogic wire block are destroyed, + * to replace it with a microblock container block if there were any microblocks + * in the wire block. + * + * TODO should this be called on the client, server or both? + * + * Note: For custom implementations, this should be capable of being called even after + * the block and tile entity are removed (and should resurrect them) + */ + public void convertToContainerBlock(); +} diff --git a/src/api/java/mods/immibis/core/api/multipart/ICoverableTile.java b/src/api/java/mods/immibis/core/api/multipart/ICoverableTile.java new file mode 100644 index 00000000..d17d44fa --- /dev/null +++ b/src/api/java/mods/immibis/core/api/multipart/ICoverableTile.java @@ -0,0 +1,11 @@ +package mods.immibis.core.api.multipart; + + +/** Multipart tiles can also implement IPartContainer4 if they have their own parts (as well as cover system parts) */ +public interface ICoverableTile { + /** + * Returns an ICoverSystem object, or null if this tile does not support a cover system + * @see mods.immibis.core.api.multipart.ICoverSystem + */ + public ICoverSystem getCoverSystem(); +} diff --git a/src/api/java/mods/immibis/core/api/multipart/IMultipartRenderingBlockMarker.java b/src/api/java/mods/immibis/core/api/multipart/IMultipartRenderingBlockMarker.java new file mode 100644 index 00000000..7b95965d --- /dev/null +++ b/src/api/java/mods/immibis/core/api/multipart/IMultipartRenderingBlockMarker.java @@ -0,0 +1,15 @@ +package mods.immibis.core.api.multipart; + +import net.minecraft.client.renderer.RenderBlocks; + +/** + * If a Block class implements this, {@link IMultipartSystem#renderMultiparts(net.minecraft.world.IBlockAccess, int, int, int, net.minecraft.client.renderer.RenderBlocks)} + * will automatically be called for that block type (after the normal block renderer). + * This is implemented by a hooking {@link RenderBlocks#renderBlockByRenderType(net.minecraft.block.Block, int, int, int)}. + * + * (Instead of this, there used to be a transformer that would modify your getRenderType and use a wrapping ISBRH. + * That was unreliable, and could have affected unrelated things like mob AI, and didn't work with Railcraft, so + * it was dropped) + */ +public interface IMultipartRenderingBlockMarker { +} diff --git a/src/api/java/mods/immibis/core/api/multipart/IMultipartSystem.java b/src/api/java/mods/immibis/core/api/multipart/IMultipartSystem.java new file mode 100644 index 00000000..71cae502 --- /dev/null +++ b/src/api/java/mods/immibis/core/api/multipart/IMultipartSystem.java @@ -0,0 +1,65 @@ +package mods.immibis.core.api.multipart; + +import java.util.ArrayList; +import java.util.List; + +import javax.annotation.Nonnull; + +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; +import net.minecraft.client.particle.EffectRenderer; +import net.minecraft.client.renderer.RenderBlocks; +import net.minecraft.entity.Entity; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; +import net.minecraft.util.AxisAlignedBB; +import net.minecraft.util.MovingObjectPosition; +import net.minecraft.util.Vec3; +import net.minecraft.world.IBlockAccess; +import net.minecraft.world.World; +import net.minecraftforge.common.util.ForgeDirection; + +/** + * Provides methods allowing blocks to implement microblock ("covering") support. + * All of the methods in this class do nothing, but are overridden if Immibis Core is installed. + */ +public class IMultipartSystem { + + /** Immibis Core sets this to an actual implementation during load time */ + public static @Nonnull IMultipartSystem instance = new IMultipartSystem(); + + /** Call this from addCollisionBoxesToList for cover-supporting blocks. + * Returns true if the tile entity implements IPartContainer. + */ + public boolean hook_addCollisionBoxesToList(World world, int x, int y, int z, AxisAlignedBB mask, List list, Entity entity) {return false;} + + /** Call this from getPickBlock for cover-supporting blocks. If it returns a non-null value, then return that. */ + public ItemStack hook_getPickBlock(MovingObjectPosition trace, World world, int blockX, int blockY, int blockZ, EntityPlayer player) {return null;} + + /** Call this from isSideSolid for cover-supporting blocks. If it returns true, then return true. */ + public boolean hook_isSideSolid(IBlockAccess world, int x, int y, int z, ForgeDirection side) {return false;} + + /** Call this from addHitEffects for cover-supporting blocks. If it returns true, then return true without adding any custom effects. */ + @SideOnly(Side.CLIENT) + public boolean hook_addHitEffects(World world, MovingObjectPosition trace, EffectRenderer effectRenderer) {return false;} + + /** Call this from addDestroyEffects for cover-supporting blocks. If it returns true, then return true without adding any custom effects. */ + @SideOnly(Side.CLIENT) + public boolean hook_addDestroyEffects(World world, int x, int y, int z, int meta, EffectRenderer effectRenderer) {return false;} + + /** Call this from collisionRayTrace for cover-supporting blocks, before returning. Pass it the result you were going to return (even if that was null). */ + public MovingObjectPosition hook_collisionRayTrace(MovingObjectPosition normalResult, World world, int x, int y, int z, Vec3 src, Vec3 dst) {return normalResult;} + + /** Call this from getDrops for cover-supporting blocks, before returning. Pass it the result you were going to return. */ + public ArrayList hook_getDrops(List drops, World world, int x, int y, int z, int metadata, int fortune) {return drops instanceof ArrayList ? (ArrayList)drops : new ArrayList(drops);} + + /** Call this from your renderer's renderWorldBlock for cover-supporting blocks, before returning. + * If it returns true (indicating it rendered something), then return true. + * (Render your custom stuff regardless of what this returns) + * + * If there is a tile entity implementing ICoverableTile with a non-null cover system, it will call renderPartContainer on that. + * If there is a tile entity implementing IPartContainer, it will call renderPartContainer on that. + */ + @SideOnly(Side.CLIENT) + public boolean renderMultiparts(IBlockAccess world, int x, int y, int z, RenderBlocks renderer) {return false;} +} diff --git a/src/api/java/mods/immibis/core/api/multipart/IPartContainer.java b/src/api/java/mods/immibis/core/api/multipart/IPartContainer.java new file mode 100644 index 00000000..451c37d6 --- /dev/null +++ b/src/api/java/mods/immibis/core/api/multipart/IPartContainer.java @@ -0,0 +1,128 @@ +package mods.immibis.core.api.multipart; + +import java.util.List; + +import net.minecraft.client.particle.EffectRenderer; +import net.minecraft.client.renderer.RenderBlocks; +import net.minecraft.entity.Entity; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; +import net.minecraft.util.AxisAlignedBB; +import net.minecraft.util.MovingObjectPosition; +import net.minecraft.util.Vec3; +import net.minecraftforge.common.util.ForgeDirection; +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; + +/** + * Contains common methods from ICoverSystem and IMultipartTile. + */ +public interface IPartContainer { + + /** + * Gets the speed at which a player breaks a part. + * See Block.getPlayerRelativeBlockHardness. + * @param ply The player. + * @param The part. + * @return The speed. + */ + float getPlayerRelativePartHardness(EntityPlayer ply, int part); + + /** + * Called when the player middle-clicks on a part. + * @param rayTrace The location under the player's crosshair. + * @param part The part number they middle-clicked on. + * @return The itemstack to pick. + */ + public ItemStack pickPart(MovingObjectPosition rayTrace, int part); + + /** + * Checks if a given face of this block is solid. + * Used to determine the result of Block.isBlockSolidOnSide. + */ + public boolean isPartContainerSideSolid(ForgeDirection side); + + /** + * Called when a player finishes breaking a part. + * @param ply The player breaking the part. + * @param subhit The part being broken. + * @param harvest Whether items/XP/etc should be dropped. + */ + public void removePartByPlayer(EntityPlayer ply, int part, boolean harvest); + + /** + * Returns the AABB of a part, in tile-local coordinates. + * Currently used for drawing the selection box. + * May return null, in which case no selection box is drawn. + * + * @param part The part ID to check. + * @return The AABB of the part, or null. + */ + public AxisAlignedBB getPartAABBFromPool(int part); + + /** + * Renders all the parts. + * @param render The render context. + * @return True if anything was rendered. + */ + @SideOnly(Side.CLIENT) + public boolean renderPartContainer(RenderBlocks render); + + /** + * Renders one part. + * @param render The render context. + * @param part The part number. + * @return True if anything was rendered. + */ + @SideOnly(Side.CLIENT) + public boolean renderPart(RenderBlocks render, int part); + + /** + * Creates particles when a part is destroyed. + * If the part number is invalid, returns false. + * Otherwise, creates particles if applicable, and returns true. + */ + @SideOnly(Side.CLIENT) + public boolean addPartDestroyEffects(int part, EffectRenderer er); + + /** + * Returns true if the given player can currently harvest the given part (using their selected item) + * @param player The player to check. + * @param part The index of the part the player is trying to harvest. + * @return True iff the player can harvest the part. + */ + public boolean canPlayerHarvestPart(EntityPlayer entityPlayer, int part); + + /** + * Adds the complete drops from this cover system to the given list. + * Note that is not normally used when breaking microblocks. + * @param drops The list to add the drops to. + * @param fortune The fortune level involved. + */ + public void getPartContainerDrops(List drops, int fortune); + + /** + * Creates particles when a part is punched. + * Returns false if anything goes wrong, which indicates to use the default particle effect. + * Otherwise, creates particles (if applicable), and returns true. + */ + @SideOnly(Side.CLIENT) + public boolean addPartHitEffects(int part, int sideHit, EffectRenderer er); + + /** + * Finds the closest intercept of the given ray with parts in this container. + * @param src The start of the ray, in world coordinates. + * @param dst The end of the ray, in world coordinates. + * @return A ray-trace result, or null if the ray does not intersect any parts. + */ + public MovingObjectPosition collisionRayTrace(Vec3 src, Vec3 dst); + + /** + * Returns all collision boxes that intersect the given mask, in world coordinates. + * @param mask The mask AABB, in world coordinates. + * @param list The list to add the returned boxes to. + * + * Used to implement Block.addCollisionBoxesToList. + */ + public void getCollidingBoundingBoxes(AxisAlignedBB mask, List list, Entity entity); +} diff --git a/src/api/java/mods/immibis/core/api/multipart/PartBreakEvent.java b/src/api/java/mods/immibis/core/api/multipart/PartBreakEvent.java new file mode 100644 index 00000000..3b2e3f4c --- /dev/null +++ b/src/api/java/mods/immibis/core/api/multipart/PartBreakEvent.java @@ -0,0 +1,24 @@ +package mods.immibis.core.api.multipart; + +import cpw.mods.fml.common.eventhandler.Cancelable; +import cpw.mods.fml.common.eventhandler.Event; +import net.minecraft.entity.EntityLivingBase; +import net.minecraft.world.World; + +/** + * Fired on the Forge event bus when a part finishes being broken. + * + * This is fired on both the client and server. + */ +@Cancelable +public class PartBreakEvent extends Event { + public final World world; + public final PartCoordinates coords; + public final EntityLivingBase player; + + public PartBreakEvent(World world, PartCoordinates coords, EntityLivingBase player) { + this.world = world; + this.coords = coords; + this.player = player; + } +} diff --git a/src/api/java/mods/immibis/core/api/multipart/PartCoordinates.java b/src/api/java/mods/immibis/core/api/multipart/PartCoordinates.java new file mode 100644 index 00000000..1a348d7c --- /dev/null +++ b/src/api/java/mods/immibis/core/api/multipart/PartCoordinates.java @@ -0,0 +1,35 @@ +package mods.immibis.core.api.multipart; + +public final class PartCoordinates { + public final int x, y, z, part; + public final boolean isCoverSystemPart; + public PartCoordinates(int x, int y, int z, int part, boolean isCoverSystemPart) { + this.x = x; + this.y = y; + this.z = z; + this.part = part; + this.isCoverSystemPart = isCoverSystemPart; + } + + @Override + public int hashCode() { + return ((x * 257 + y) * 257 + z) * 257 + part; + } + + @Override + public boolean equals(Object obj) { + if(obj == null) + return false; + try { + PartCoordinates o = (PartCoordinates)obj; + return x == o.x && y == o.y && z == o.z && part == o.part && isCoverSystemPart == o.isCoverSystemPart; + } catch(ClassCastException e) { + return false; + } + } + + @Override + public String toString() { + return "[" + x + "," + y + "," + z + ":" + part + "]"; + } +} diff --git a/src/api/java/mods/immibis/core/api/multipart/util/BlockMultipartBase.java b/src/api/java/mods/immibis/core/api/multipart/util/BlockMultipartBase.java new file mode 100644 index 00000000..25c9184a --- /dev/null +++ b/src/api/java/mods/immibis/core/api/multipart/util/BlockMultipartBase.java @@ -0,0 +1,100 @@ +package mods.immibis.core.api.multipart.util; + + +import java.util.ArrayList; +import java.util.List; + +import mods.immibis.core.api.multipart.IMultipartRenderingBlockMarker; +import mods.immibis.core.api.multipart.IMultipartSystem; +import net.minecraft.block.BlockContainer; +import net.minecraft.block.material.Material; +import net.minecraft.client.particle.EffectRenderer; +import net.minecraft.entity.Entity; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; +import net.minecraft.util.AxisAlignedBB; +import net.minecraft.util.MovingObjectPosition; +import net.minecraft.util.Vec3; +import net.minecraft.world.IBlockAccess; +import net.minecraft.world.World; +import net.minecraftforge.common.util.ForgeDirection; +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; + + +public abstract class BlockMultipartBase extends BlockContainer implements IMultipartRenderingBlockMarker { + + protected BlockMultipartBase(Material mat) { + super(mat); + + setHardness(1); // the block hardness must not be zero. + // if using IPartContainer tiles, the block hardness should be irrelevant, but still can't be zero. + } + + + + + @Override public final boolean isOpaqueCube() {return false;} + @Override public final boolean renderAsNormalBlock() {return false;} + @Override public boolean shouldSideBeRendered(IBlockAccess par1iBlockAccess, int par2, int par3, int par4, int par5) {return true;} + + + + + + @Override + public ItemStack getPickBlock(MovingObjectPosition target, World world, int x, int y, int z, EntityPlayer player) { + ItemStack coverPick = IMultipartSystem.instance.hook_getPickBlock(target, world, x, y, z, player); + if(coverPick != null) + return coverPick; + + return super.getPickBlock(target, world, x, y, z, player); + } + + @Override + public boolean isSideSolid(IBlockAccess world, int x, int y, int z, ForgeDirection side) { + if(IMultipartSystem.instance.hook_isSideSolid(world, x, y, z, side)) + return true; + + return super.isSideSolid(world, x, y, z, side); + } + + @SideOnly(Side.CLIENT) + @Override + public boolean addDestroyEffects(World world, int x, int y, int z, int meta, EffectRenderer effectRenderer) { + if(IMultipartSystem.instance.hook_addDestroyEffects(world, x, y, z, meta, effectRenderer)) + return true; + + return super.addDestroyEffects(world, x, y, z, meta, effectRenderer); + } + + @SideOnly(Side.CLIENT) + @Override + public boolean addHitEffects(World worldObj, MovingObjectPosition target, EffectRenderer effectRenderer) { + if(IMultipartSystem.instance.hook_addHitEffects(worldObj, target, effectRenderer)) + return true; + + return super.addHitEffects(worldObj, target, effectRenderer); + } + + @Override + public MovingObjectPosition collisionRayTrace(World world, int x, int y, int z, Vec3 src, Vec3 dst) { + return IMultipartSystem.instance.hook_collisionRayTrace(super.collisionRayTrace(world, x, y, z, src, dst), world, x, y, z, src, dst); + } + + @SuppressWarnings({ "rawtypes", "unchecked" }) + @Override + public void addCollisionBoxesToList(World world, int x, int y, int z, AxisAlignedBB mask, List list, Entity entity) { + // Note: if you're implementing this yourself (instead of extending this class) you don't need to check the + // return value of hook_addCollisionBlocksToList. + // The check is to allow BlockMultipartBase to work with both multipart and normal blocks. + if(!IMultipartSystem.instance.hook_addCollisionBoxesToList(world, x, y, z, mask, list, entity)) + super.addCollisionBoxesToList(world, x, y, z, mask, list, entity); + } + + + @Override + public ArrayList getDrops(World world, int x, int y, int z, int metadata, int fortune) { + return IMultipartSystem.instance.hook_getDrops(super.getDrops(world, x, y, z, metadata, fortune), world, x, y, z, metadata, fortune); + } +} diff --git a/src/api/java/mods/immibis/core/api/util/Dir.java b/src/api/java/mods/immibis/core/api/util/Dir.java new file mode 100644 index 00000000..142fbd9d --- /dev/null +++ b/src/api/java/mods/immibis/core/api/util/Dir.java @@ -0,0 +1,13 @@ +package mods.immibis.core.api.util; + +public final class Dir { + private Dir() {} + + public static final int NY = 0; + public static final int PY = 1; + public static final int NZ = 2; + public static final int PZ = 3; + public static final int NX = 4; + public static final int PX = 5; + +} diff --git a/src/api/java/mods/immibis/microblocks/api/EnumAxisPosition.java b/src/api/java/mods/immibis/microblocks/api/EnumAxisPosition.java new file mode 100644 index 00000000..1949ca3b --- /dev/null +++ b/src/api/java/mods/immibis/microblocks/api/EnumAxisPosition.java @@ -0,0 +1,16 @@ +package mods.immibis.microblocks.api; + +public enum EnumAxisPosition { + Negative, + Positive, + Centre, + Span; + + public boolean touchesNegative() { + return this == Negative || this == Span; + } + + public boolean touchesPositive() { + return this == Positive || this == Span; + } +} diff --git a/src/api/java/mods/immibis/microblocks/api/EnumPartClass.java b/src/api/java/mods/immibis/microblocks/api/EnumPartClass.java new file mode 100644 index 00000000..859b8904 --- /dev/null +++ b/src/api/java/mods/immibis/microblocks/api/EnumPartClass.java @@ -0,0 +1,10 @@ +package mods.immibis.microblocks.api; + + +public enum EnumPartClass { + Centre, + Panel, + HollowPanel, + Strip, + Corner +} diff --git a/src/api/java/mods/immibis/microblocks/api/EnumPosition.java b/src/api/java/mods/immibis/microblocks/api/EnumPosition.java new file mode 100644 index 00000000..09ce9ae4 --- /dev/null +++ b/src/api/java/mods/immibis/microblocks/api/EnumPosition.java @@ -0,0 +1,131 @@ +package mods.immibis.microblocks.api; + +import static mods.immibis.microblocks.api.EnumAxisPosition.*; +import static mods.immibis.microblocks.api.EnumPositionClass.*; +import mods.immibis.core.api.util.Dir; + +public enum EnumPosition { + // DO NOT CHANGE ORDER - Ordinals are saved in NBT + // Changing order WILL break existing saves! + + Centre(EnumAxisPosition.Centre, EnumAxisPosition.Centre, EnumAxisPosition.Centre, EnumPositionClass.Centre), + FaceNX(Negative, Span, Span, Face), + FacePX(Positive, Span, Span, Face), + FaceNY(Span, Negative, Span, Face), + FacePY(Span, Positive, Span, Face), + FaceNZ(Span, Span, Negative, Face), + FacePZ(Span, Span, Positive, Face), + EdgeNXNY(Negative, Negative, Span, Edge), + EdgeNXPY(Negative, Positive, Span, Edge), + EdgePXNY(Positive, Negative, Span, Edge), + EdgePXPY(Positive, Positive, Span, Edge), + EdgeNXNZ(Negative, Span, Negative, Edge), + EdgeNXPZ(Negative, Span, Positive, Edge), + EdgePXNZ(Positive, Span, Negative, Edge), + EdgePXPZ(Positive, Span, Positive, Edge), + EdgeNYNZ(Span, Negative, Negative, Edge), + EdgeNYPZ(Span, Negative, Positive, Edge), + EdgePYNZ(Span, Positive, Negative, Edge), + EdgePYPZ(Span, Positive, Positive, Edge), + CornerNXNYNZ(Negative, Negative, Negative, Corner), + CornerNXNYPZ(Negative, Negative, Positive, Corner), + CornerNXPYNZ(Negative, Positive, Negative, Corner), + CornerNXPYPZ(Negative, Positive, Positive, Corner), + CornerPXNYNZ(Positive, Negative, Negative, Corner), + CornerPXNYPZ(Positive, Negative, Positive, Corner), + CornerPXPYNZ(Positive, Positive, Negative, Corner), + CornerPXPYPZ(Positive, Positive, Positive, Corner), + + PostX(Span, EnumAxisPosition.Centre, EnumAxisPosition.Centre, Post), + PostY(EnumAxisPosition.Centre, Span, EnumAxisPosition.Centre, Post), + PostZ(EnumAxisPosition.Centre, EnumAxisPosition.Centre, Span, Post), + + ; + + private EnumPosition(EnumAxisPosition x, EnumAxisPosition y, EnumAxisPosition z, EnumPositionClass clazz) { + this.x = x; + this.y = y; + this.z = z; + this.clazz = clazz; + } + + public final EnumAxisPosition x, y, z; + public final EnumPositionClass clazz; + + public static EnumPosition getCornerPosition(int x, int y, int z) { + if(x < 0) + if(y < 0) + if(z < 0) + return CornerNXNYNZ; + else + return CornerNXNYPZ; + else + if(z < 0) + return CornerNXPYNZ; + else + return CornerNXPYPZ; + else + if(y < 0) + if(z < 0) + return CornerPXNYNZ; + else + return CornerPXNYPZ; + else + if(z < 0) + return CornerPXPYNZ; + else + return CornerPXPYPZ; + } + + private static EnumPosition edgePositionLookup[][] = { + {null, null, EdgeNYNZ, EdgeNYPZ, EdgeNXNY, EdgePXNY}, + {null, null, EdgePYNZ, EdgePYPZ, EdgeNXPY, EdgePXPY}, + {EdgeNYNZ, EdgePYNZ, null, null, EdgeNXNZ, EdgePXNZ}, + {EdgeNYPZ, EdgePYPZ, null, null, EdgeNXPZ, EdgePXPZ}, + {EdgeNXNY, EdgeNXPY, EdgeNXNZ, EdgeNXPZ, null, null}, + {EdgePXNY, EdgePXPY, EdgePXNZ, EdgePXPZ, null, null}, + }; + + private static boolean edgeTouches(EnumPosition edge, int face) { + switch(face) { + case Dir.NX: return edge.x == EnumAxisPosition.Negative; + case Dir.PX: return edge.x == EnumAxisPosition.Positive; + case Dir.NY: return edge.y == EnumAxisPosition.Negative; + case Dir.PY: return edge.y == EnumAxisPosition.Positive; + case Dir.NZ: return edge.z == EnumAxisPosition.Negative; + case Dir.PZ: return edge.z == EnumAxisPosition.Positive; + } + throw new IllegalArgumentException("Invalid face "+face); + } + + static { + for(int a = 0; a < 6; a++) + for(int b = 0; b < 6; b++) + assert(edgePositionLookup[a][b] == null || (edgeTouches(edgePositionLookup[a][b], a) && edgeTouches(edgePositionLookup[a][b], b))); + } + + /** + * Returns the edge microblock position that intersects both the given faces. + * + * @param side1 One of the faces. + * @param side2 The other face. + * @return The matching microblock position, or null for invalid face combinations (eg +Y and -Y) + */ + public static EnumPosition getEdgePosition(int side1, int side2) { + if(side1 < 0 || side2 < 0 || side1 > 5 || side2 > 5) + return null; + return edgePositionLookup[side2][side1]; + } + + public static EnumPosition getFacePosition(int side) { + switch(side) { + case Dir.NX: return FaceNX; + case Dir.PX: return FacePX; + case Dir.NY: return FaceNY; + case Dir.PY: return FacePY; + case Dir.NZ: return FaceNZ; + case Dir.PZ: return FacePZ; + default: throw new IllegalArgumentException("Invalid direction: "+side); + } + } +} diff --git a/src/api/java/mods/immibis/microblocks/api/EnumPositionClass.java b/src/api/java/mods/immibis/microblocks/api/EnumPositionClass.java new file mode 100644 index 00000000..cb4baba5 --- /dev/null +++ b/src/api/java/mods/immibis/microblocks/api/EnumPositionClass.java @@ -0,0 +1,10 @@ +package mods.immibis.microblocks.api; + + +public enum EnumPositionClass { + Centre, + Face, + Edge, + Corner, + Post, +} diff --git a/src/api/java/mods/immibis/microblocks/api/IMicroblockCoverSystem.java b/src/api/java/mods/immibis/microblocks/api/IMicroblockCoverSystem.java new file mode 100644 index 00000000..527e1145 --- /dev/null +++ b/src/api/java/mods/immibis/microblocks/api/IMicroblockCoverSystem.java @@ -0,0 +1,82 @@ +package mods.immibis.microblocks.api; + + +import java.io.DataInput; +import java.io.DataOutput; +import java.io.IOException; +import java.util.Collection; + +import mods.immibis.core.api.multipart.ICoverSystem; +import net.minecraft.nbt.NBTTagCompound; + +/** Not intended to be implemented outside of the Immibis's Microblocks mod. */ +public interface IMicroblockCoverSystem extends ICoverSystem, IMicroblockIntegratedTile { + /** + * Returns the position of a part. + * + * @param part The part number. + * @return The part's position, or null if it's an invalid part. + */ + @Override + EnumPosition getPartPosition(int part); + + boolean addPart(Part part); + + /** + * Returns true if tubes/cables/etc that use the centre of the block + * can connect through the specified side - ie if it is not blocked by a non-hollow cover. + */ + boolean isSideOpen(int side); + + /** + * Serializes the microblocks to a byte array. + */ + byte[] writeDescriptionBytes(); + + /** + * Unserializes the microblocks from a byte array. + */ + void readDescriptionBytes(byte[] data, int start); + + /** + * Serializes the microblocks to a DataOutput. + */ + void writeDescriptionPacket(DataOutput data) throws IOException; + + /** + * Unserializes the microblocks from a DataInput. + */ + void readDescriptionPacket(DataInput data) throws IOException; + + /** + * Serializes the microblocks to NBT. + * Data will be stored in the "ICMP" key of the given compound tag. + */ + void writeToNBT(NBTTagCompound tag); + + /** + * Unserializes the microblocks from NBT. + * Data will be read from the "ICMP" key of the given compound tag. + */ + void readFromNBT(NBTTagCompound tag); + + /** + * Returns true if there is a part in the specified position. + */ + boolean isPositionOccupied(EnumPosition pos); + + /** + * Returns all parts in this block. + */ + Collection getAllParts(); + + /** + * Checks if an edge is unused (for example, if wire connections can connect around corners through it) + * Returns the same result if face1 and face2 are swapped. + * + * @param face1 One face bordering the edge to check. + * @param face2 The other face bordering the edge to check. + * @return True if the edge is unused. + */ + public boolean isEdgeOpen(int face1, int face2); +} diff --git a/src/api/java/mods/immibis/microblocks/api/IMicroblockIntegratedTile.java b/src/api/java/mods/immibis/microblocks/api/IMicroblockIntegratedTile.java new file mode 100644 index 00000000..22c08811 --- /dev/null +++ b/src/api/java/mods/immibis/microblocks/api/IMicroblockIntegratedTile.java @@ -0,0 +1,27 @@ +package mods.immibis.microblocks.api; + +/** + * If you have an IMicroblockSupporterTile that is also an IPartContainer, + * you should implement this for better integration. + * + * You might, in rare circumstances, want to implement it just for isPositionOccupied, without being an IPartContainer. + */ +public interface IMicroblockIntegratedTile { + /** + * Returns the microblock position a tile-owned part mainly occupies, or null if + * unknown or the part occupies the whole block. + * Used to determine the position of new microblocks placed by the player. + * + * If this tile doesn't implement IPartContainer as well, this is never called. + * + * @param subHit The part index. + * @return The position the part occupies. + */ + public EnumPosition getPartPosition(int index); + + /** + * Returns true if the given position is occupied by the tile. + * Does not check for collisions with other positions. + */ + public boolean isPositionOccupied(EnumPosition pos); +} diff --git a/src/api/java/mods/immibis/microblocks/api/IMicroblockPermissionHandler.java b/src/api/java/mods/immibis/microblocks/api/IMicroblockPermissionHandler.java new file mode 100644 index 00000000..ff00b764 --- /dev/null +++ b/src/api/java/mods/immibis/microblocks/api/IMicroblockPermissionHandler.java @@ -0,0 +1,35 @@ +package mods.immibis.microblocks.api; + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.world.World; + +public interface IMicroblockPermissionHandler { + /** + * Returns true if this permission handler wants to prevent placing a microblock. + * This is called on both the client and the server. + * + * @param player The player placing a part. + * @param w The world. + * @param x The x coordinate of the microblock container block. + * @param y The y coordinate of the microblock container block. + * @param z The z coordinate of the microblock container block. + * @param p The part being placed. + */ + public boolean doesPreventPlacing(EntityPlayer player, World w, int x, int y, int z, Part p); + + /** + * Returns true if this permission handler wants to prevent breaking a microblock. + * This is called on both the client and the server. + * + * Not currently implemented. If you want to use this, get immibis to finish it. + * + * @param player The player breaking a part. + * @param w The world. + * @param x The x coordinate of the microblock container block. + * @param y The y coordinate of the microblock container block. + * @param z The z coordinate of the microblock container block. + * @param tile The microblock container tile entity. + * @param p The part being broken. + */ + public boolean doesPreventBreaking(EntityPlayer player, World w, int x, int y, int z, IMicroblockSupporterTile tile, Part p); +} diff --git a/src/api/java/mods/immibis/microblocks/api/IMicroblockSupporterTile.java b/src/api/java/mods/immibis/microblocks/api/IMicroblockSupporterTile.java new file mode 100644 index 00000000..2fd00a5a --- /dev/null +++ b/src/api/java/mods/immibis/microblocks/api/IMicroblockSupporterTile.java @@ -0,0 +1,27 @@ +package mods.immibis.microblocks.api; + +import mods.immibis.core.api.multipart.ICoverableTile; + +/** + * A multipart tile that is compatible with microblocks. + * Only tile entities should implement this. + */ +public interface IMicroblockSupporterTile extends ICoverableTile { + /** + * Returns true if the tile is "in the way" of this part and will prevent it being placed. + * @param type The type of part being placed. + * @param pos The position the part is being placed in. + * @return False to prevent part placement, otherwise true. + */ + public boolean isPlacementBlocked(PartType type, EnumPosition pos); + + @Override + public IMicroblockCoverSystem getCoverSystem(); + + /** + * Called after microblocks are added or removed. + * + * You should probably at least call World.markBlockForUpdate and world.notifyBlocksOfNeighborChange. + */ + public void onMicroblocksChanged(); +} diff --git a/src/api/java/mods/immibis/microblocks/api/IMicroblockSystem.java b/src/api/java/mods/immibis/microblocks/api/IMicroblockSystem.java new file mode 100644 index 00000000..d5919532 --- /dev/null +++ b/src/api/java/mods/immibis/microblocks/api/IMicroblockSystem.java @@ -0,0 +1,48 @@ +package mods.immibis.microblocks.api; + +import net.minecraft.block.Block; +import net.minecraft.item.ItemStack; + +public interface IMicroblockSystem { + + /** + * Creates a microblock ICoverSystem. + * @param tile The tile entity this IMicroblockCoverSystem is for. + */ + IMicroblockCoverSystem createMicroblockCoverSystem(IMicroblockSupporterTile tile); + + /** + * Allows the given block to be cut up. + * Your block must have a reasonable implementation of getBlockTextureFromSideAndMetadata. + * Part IDs will be assigned automatically based on the block ID and metadata value. + */ + void addCuttableBlock(Block block, int meta); + + /** + * Returns a part type, given its ID. + */ + PartType getPartTypeByID(int id); + + Block getMicroblockContainerBlock(); + + + /** + * Registers a custom part type. + * @throw IllegalArgumentException if the part type ID is already in use. + */ + void registerPartType(PartType type) throws IllegalArgumentException; + + + /** + * Creates an item stack of microblocks with the given part type ID and stack size. + * @throw IllegalArgumentException if there is no part type with the given ID. + */ + ItemStack partTypeIDToItemStack(int id, int stackSize) throws IllegalArgumentException; + + /** + * Gets the part ID from an item stack. + * @throw NullPointerException if stack == null + * @throw IllegalArgumentException if stack is not a stack of microblocks + */ + int itemStackToPartID(ItemStack stack) throws NullPointerException, IllegalArgumentException; +} diff --git a/src/api/java/mods/immibis/microblocks/api/IMicroblockSystem2.java b/src/api/java/mods/immibis/microblocks/api/IMicroblockSystem2.java new file mode 100644 index 00000000..14b57bee --- /dev/null +++ b/src/api/java/mods/immibis/microblocks/api/IMicroblockSystem2.java @@ -0,0 +1,32 @@ +package mods.immibis.microblocks.api; + +public interface IMicroblockSystem2 extends IMicroblockSystem { + /** + * Adds a microblock permission handler. + * + * You cannot register two permission handlers with the same name. + * You are recommended to make the name start with your mod ID. + * You are recommended to make the name human readable. + * + * This method may be called from any thread at any time. + * + * @param name A name identifying this permission handler. + * @param handler The permission handler object. + * + * @throws IllegalArgumentException If name is null, handler is null, or + * a permission handler is already registered with the same name, or + * the permission handler is already registered. + */ + public void addPermissionHandler(String name, IMicroblockPermissionHandler handler); + + /** + * Removes a permission handler. + * + * This method may be called from any thread at any time. + * + * @param handler The permission handler object. + * + * @throws IllegalArgumentException If handler is null, or the permission handler is not registered. + */ + public void removePermissionHandler(IMicroblockPermissionHandler handler); +} diff --git a/src/api/java/mods/immibis/microblocks/api/MicroblockAPIUtils.java b/src/api/java/mods/immibis/microblocks/api/MicroblockAPIUtils.java new file mode 100644 index 00000000..84de4798 --- /dev/null +++ b/src/api/java/mods/immibis/microblocks/api/MicroblockAPIUtils.java @@ -0,0 +1,70 @@ +package mods.immibis.microblocks.api; + +import net.minecraft.block.Block; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; +import net.minecraft.world.World; + +public class MicroblockAPIUtils { + private static IMicroblockSystem cachedIMS; + private static boolean haveCachedIMS; + public static synchronized IMicroblockSystem getMicroblockSystem() { + if(!haveCachedIMS) { + try { + cachedIMS = (IMicroblockSystem)Class.forName("mods.immibis.microblocks.MicroblockSystem").getField("instance").get(null); + haveCachedIMS = true; + } catch(ClassNotFoundException e) { + cachedIMS = null; + haveCachedIMS = true; + } catch(RuntimeException e) { + throw e; + } catch(Exception e) { + throw (AssertionError)new AssertionError("should not happen").initCause(e); + } + } + return cachedIMS; + } + + public static IMicroblockCoverSystem createMicroblockCoverSystem(IMicroblockSupporterTile tile) { + IMicroblockSystem ims = getMicroblockSystem(); + return ims == null ? null : ims.createMicroblockCoverSystem(tile); + } + + public static Block getMicroblockContainerBlock() { + IMicroblockSystem ims = getMicroblockSystem(); + return ims == null ? null : ims.getMicroblockContainerBlock(); + } + + public static PartType getPartTypeByID(int i) { + IMicroblockSystem ims = getMicroblockSystem(); + return ims == null ? null : ims.getPartTypeByID(i); + } + + /** + * If there is a microblock container block at the specified coordinates, this function will save the parts in that block, + * place a new block, restore the parts, and return true. + * If there is not a microblock container block at the specified coordinates, or placing the block fails, it will + * return false. + * The new block must have a tile entity which implements IMicroblockSupporterTile and has a non-null IMicroblockCoverSystem. + */ + public static boolean mergeIntoMicroblockContainer(ItemStack itemstack, EntityPlayer entityplayer, World world, int x, int y, int z, int l, Block newBlock, int newMetadata) + { + Block microblockContainerBlock = getMicroblockContainerBlock(); + if(microblockContainerBlock == null || world.getBlock(x, y, z) != microblockContainerBlock) + return false; + + IMicroblockSupporterTile tm = (IMicroblockSupporterTile)world.getTileEntity(x, y, z); + IMicroblockCoverSystem oldCI = tm.getCoverSystem(); + if(!world.setBlock(x, y, z, newBlock, newMetadata, 2)) + return false; + + IMicroblockSupporterTile tcb = (IMicroblockSupporterTile)world.getTileEntity(x, y, z); + IMicroblockCoverSystem newCI = tcb.getCoverSystem(); + + for(Part p : oldCI.getAllParts()) + newCI.addPart(p); + + newBlock.onBlockPlacedBy(world, x, y, z, entityplayer, itemstack); + return true; + } +} diff --git a/src/api/java/mods/immibis/microblocks/api/Part.java b/src/api/java/mods/immibis/microblocks/api/Part.java new file mode 100644 index 00000000..c29c97ea --- /dev/null +++ b/src/api/java/mods/immibis/microblocks/api/Part.java @@ -0,0 +1,100 @@ +package mods.immibis.microblocks.api; + +import java.io.DataOutput; + +import net.minecraft.client.particle.EffectRenderer; +import net.minecraft.nbt.NBTBase; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.util.AxisAlignedBB; +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; + +public class Part { + public final PartType type; + public final EnumPosition pos; + + public Part(PartType type, EnumPosition pos) { + this.type = type; + this.pos = pos; + } + + private static double getMinFromAxisPosition(EnumAxisPosition ap, double size) { + switch(ap) { + case Span: + case Negative: + return 0; + case Positive: + return 1 - size; + case Centre: + return (1 - size) / 2; + default: + throw new IllegalArgumentException("ap"); + } + } + private static double getMaxFromAxisPosition(EnumAxisPosition ap, double size) { + switch(ap) { + case Span: + case Positive: + return 1; + case Negative: + return size; + case Centre: + return (1 + size) / 2; + default: + throw new IllegalArgumentException("ap"); + } + } + + public AxisAlignedBB getBoundingBoxFromPool() { + return getBoundingBoxFromPool(pos, type.getSize()); + } + + public static AxisAlignedBB getBoundingBoxFromPool(EnumPosition pos, double size) { + double minx = getMinFromAxisPosition(pos.x, size); + double miny = getMinFromAxisPosition(pos.y, size); + double minz = getMinFromAxisPosition(pos.z, size); + double maxx = getMaxFromAxisPosition(pos.x, size); + double maxy = getMaxFromAxisPosition(pos.y, size); + double maxz = getMaxFromAxisPosition(pos.z, size); + return AxisAlignedBB.getBoundingBox(minx, miny, minz, maxx, maxy, maxz); + } + + public NBTBase writeToNBT() { + NBTTagCompound c = new NBTTagCompound(); + c.setInteger("type", type.getID()); + c.setByte("pos", (byte)pos.ordinal()); + return c; + } + + public static Part readFromNBT(NBTBase tag) { + NBTTagCompound c = (NBTTagCompound)tag; + PartType type = MicroblockAPIUtils.getMicroblockSystem().getPartTypeByID(c.getInteger("type")); + if(type == null) + return null; + EnumPosition pos = EnumPosition.values()[c.getByte("pos")]; + Part p = type.createPart(pos, c); + return p; + } + + /** + * You can override this in subclasses of Part to send extra data to clients in the description packet. + * As this data is included in every description packet, try to keep it short. + * The data is received by {@link PartType#createPart(EnumPosition, java.io.DataInput)}. + */ + public void writeExtraData(DataOutput data) { + } + + /** + * Implement this to have block destroy particles. + */ + @SideOnly(Side.CLIENT) + public void addPartDestroyEffects(IMicroblockSupporterTile tile, EffectRenderer er) { + } + + /** + * Implement this to have block hit particles. + */ + @SideOnly(Side.CLIENT) + public void addPartHitEffects(IMicroblockSupporterTile tile, int sideHit, EffectRenderer er) { + } +} diff --git a/src/api/java/mods/immibis/microblocks/api/PartType.java b/src/api/java/mods/immibis/microblocks/api/PartType.java new file mode 100644 index 00000000..e7cc336d --- /dev/null +++ b/src/api/java/mods/immibis/microblocks/api/PartType.java @@ -0,0 +1,105 @@ +package mods.immibis.microblocks.api; + +import java.io.DataInput; + +import net.minecraft.client.renderer.RenderBlocks; +import net.minecraft.client.renderer.RenderGlobal; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; + +public interface PartType { + public EnumPartClass getPartClass(); + public double getSize(); + public int getID(); + + public String getLocalizedName(ItemStack stack); + + public boolean canHarvest(EntityPlayer ply, PartClass part); + + /** + * Renders the preview of a part of this type. + * Previews are shown when the player is holding a microblock stack of this type. + * + * The modelview matrix is set so that 0,0,0 and 1,1,1 are the corners of the block to render in. + * No other OpenGL state is set - in particular blending is disabled. + * + * @param context The RenderGlobal object. + * @param pos The microblock position to render the preview at. + * @param stack The item stack the player is holding. + */ + @SideOnly(Side.CLIENT) + public void renderPreview(RenderGlobal context, EnumPosition pos, ItemStack stack); + + /** + * Renders the part as an item. + * Rendering should be centered within the box (0,0,0) to (1,1,1). + * + * @param context The RenderBlocks object. + * @param stack The item stack. + */ + @SideOnly(Side.CLIENT) + void renderPartInv(RenderBlocks context, ItemStack stack); + + /** + * Returns the item stack dropped when a part with this type is broken. + * + * @param part The part being broken. + * @param ply The player who broke the part, or null if unknown. + */ + public ItemStack getDroppedStack(Part part, EntityPlayer ply); + + /** + * @see net.minecraft.block.Block#getPlayerRelativeBlockHardness(EntityPlayer, net.minecraft.world.World, int, int, int) + */ + public float getPlayerRelativeHardness(Part part, EntityPlayer ply); + + /** + * Returns the item picked when a player middle-clicks on this part in creative. + */ + public ItemStack getPickItem(Part part); + + /** + * Renders a part with this type in the world. + * @param render The RenderBlocks object. + * @param p The part to render. + * @param x The X coordinate of the block containing the part. + * @param y The Y coordinate of the block containing the part. + * @param z The Z coordinate of the block containing the part. + * @param dontRenderSides An array of booleans corresponding to directions. + * If dontRenderSides[direction] is true, then that side should not be rendered if it touches the edge of the block space. + */ + @SideOnly(Side.CLIENT) + public void renderPartWorld(RenderBlocks render, Part p, int x, int y, int z, boolean[] dontRenderSides); + + /** + * Creates a part with this type and deserializes any extra data that you may have written in {@link Part#writeExtraData()}. + * Intended to allow mods to use custom subclasses of Part. + */ + @SideOnly(Side.CLIENT) + public Part createPart(EnumPosition pos, DataInput data); + + /** + * Creates a part with this type. + * Intended to allow mods to use custom subclasses of Part. + */ + public Part createPart(EnumPosition pos); + + /** + * Creates a part with this type and deserializes any extra data from NBT. + * Intended to allow mods to use custom subclasses of Part. + */ + public Part createPart(EnumPosition pos, NBTTagCompound c); + + /** + * Returns true if this part is completely opaque. + */ + public boolean isOpaque(); + + /** + * Returns true if this player can harvest this part. + */ + public boolean canHarvest(Part part, EntityPlayer entityPlayer); +} diff --git a/src/api/java/mods/immibis/microblocks/api/PartType2.java b/src/api/java/mods/immibis/microblocks/api/PartType2.java new file mode 100644 index 00000000..be32d49b --- /dev/null +++ b/src/api/java/mods/immibis/microblocks/api/PartType2.java @@ -0,0 +1,8 @@ +package mods.immibis.microblocks.api; + +public interface PartType2 extends PartType { + /** + * @return The light level emitted by this part, from 0 to 15. + */ + public int getLightLevel(); +} diff --git a/src/api/java/mods/immibis/microblocks/api/util/TileCoverableBase.java b/src/api/java/mods/immibis/microblocks/api/util/TileCoverableBase.java new file mode 100644 index 00000000..5dadfc7e --- /dev/null +++ b/src/api/java/mods/immibis/microblocks/api/util/TileCoverableBase.java @@ -0,0 +1,78 @@ +package mods.immibis.microblocks.api.util; + + +import mods.immibis.microblocks.api.IMicroblockCoverSystem; +import mods.immibis.microblocks.api.IMicroblockSupporterTile; +import mods.immibis.microblocks.api.IMicroblockSystem; +import mods.immibis.microblocks.api.MicroblockAPIUtils; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.network.NetworkManager; +import net.minecraft.network.Packet; +import net.minecraft.network.play.server.S35PacketUpdateTileEntity; +import net.minecraft.tileentity.TileEntity; + +/** + * Convenience class for microblock supporting tiles. + * Handles creating an ICoverSystem, saving and loading it, + * and implements getCollidingBoundingBoxes and collisionRayTrace. + */ +public abstract class TileCoverableBase extends TileEntity implements IMicroblockSupporterTile { + + protected IMicroblockCoverSystem cover; + + public TileCoverableBase() { + IMicroblockSystem ims = MicroblockAPIUtils.getMicroblockSystem(); + if(ims != null) + cover = ims.createMicroblockCoverSystem(this); + } + + @Override + public void writeToNBT(NBTTagCompound tag) { + super.writeToNBT(tag); + if(cover != null) + cover.writeToNBT(tag); + } + + @Override + public Packet getDescriptionPacket() { + if(cover == null) + return null; + + NBTTagCompound tag = new NBTTagCompound(); + tag.setByteArray("C", cover.writeDescriptionBytes()); + S35PacketUpdateTileEntity p = new S35PacketUpdateTileEntity(xCoord, yCoord, zCoord, 0, tag); + return p; + } + + @Override + public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt) { + if(cover != null) + cover.readDescriptionBytes(pkt.func_148857_g().getByteArray("C"), 0); + } + + @Override + public void readFromNBT(NBTTagCompound tag) { + super.readFromNBT(tag); + if(cover != null) + cover.readFromNBT(tag); + } + + @Override + public IMicroblockCoverSystem getCoverSystem() { + return cover; + } + + @Override + public void onMicroblocksChanged() { + worldObj.notifyBlocksOfNeighborChange(xCoord, yCoord, zCoord, getBlockType()); + worldObj.markBlockForUpdate(xCoord, yCoord, zCoord); + } + + public void convertToCoverContainerBlock() { + if(getCoverSystem() != null) + getCoverSystem().convertToContainerBlock(); + else + worldObj.setBlockToAir(xCoord, yCoord, zCoord); + } + +} diff --git a/src/api/java/mods/immibis/microblocks/api/util/TileCoverableMultipartBase.java b/src/api/java/mods/immibis/microblocks/api/util/TileCoverableMultipartBase.java new file mode 100644 index 00000000..7ec86ce5 --- /dev/null +++ b/src/api/java/mods/immibis/microblocks/api/util/TileCoverableMultipartBase.java @@ -0,0 +1,60 @@ +package mods.immibis.microblocks.api.util; + +import java.util.List; + +import net.minecraft.entity.Entity; +import net.minecraft.util.AxisAlignedBB; +import net.minecraft.util.MovingObjectPosition; +import net.minecraft.util.Vec3; +import mods.immibis.core.api.multipart.IPartContainer; +import mods.immibis.microblocks.api.IMicroblockIntegratedTile; + +public abstract class TileCoverableMultipartBase extends TileCoverableBase implements IPartContainer, IMicroblockIntegratedTile { + protected abstract int getNumTileOwnedParts(); + + private static MovingObjectPosition getCloserMOP(MovingObjectPosition a, MovingObjectPosition b, Vec3 src) { + if(a == null) return b; + if(b == null) return a; + + double ciDistSq = a.hitVec.squareDistanceTo(src); + double normalDistSq = b.hitVec.squareDistanceTo(src); + + return ciDistSq < normalDistSq ? a : b; + } + + @Override + public MovingObjectPosition collisionRayTrace(Vec3 src, Vec3 dst) { + MovingObjectPosition result = null; + + for(int k = 0, num = getNumTileOwnedParts(); k < num; k++) { + AxisAlignedBB partBB = getPartAABBFromPool(k); + if(partBB != null) { + partBB.offset(xCoord, yCoord, zCoord); + MovingObjectPosition intersect = partBB.calculateIntercept(src, dst); + if(intersect != null) { + intersect.subHit = k; + intersect.typeOfHit = MovingObjectPosition.MovingObjectType.BLOCK; + intersect.blockX = xCoord; + intersect.blockY = yCoord; + intersect.blockZ = zCoord; + result = getCloserMOP(result, intersect, src); + } + } + } + + return result; + } + + @Override + public void getCollidingBoundingBoxes(AxisAlignedBB mask, List list, Entity entity) { + for(int k = 0, num = getNumTileOwnedParts(); k < num; k++) { + AxisAlignedBB partBB = getPartAABBFromPool(k); + if(partBB != null) { + partBB.offset(xCoord, yCoord, zCoord); + if(partBB.intersectsWith(mask)) + list.add(partBB); + } + } + } + +} diff --git a/src/main/java/crazypants/enderio/conduit/AbstractConduit.java b/src/main/java/crazypants/enderio/conduit/AbstractConduit.java index 03569c99..1c919f88 100644 --- a/src/main/java/crazypants/enderio/conduit/AbstractConduit.java +++ b/src/main/java/crazypants/enderio/conduit/AbstractConduit.java @@ -9,6 +9,10 @@ import java.util.Iterator; import java.util.List; import java.util.Set; +import mods.immibis.microblocks.api.EnumPartClass; +import mods.immibis.microblocks.api.EnumPosition; +import mods.immibis.microblocks.api.IMicroblockCoverSystem; +import mods.immibis.microblocks.api.Part; import net.minecraft.block.Block; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; @@ -233,6 +237,9 @@ public abstract class AbstractConduit implements IConduit { if(conduit == null) { return false; } + if (MicroblocksUtil.supportMicroblocks() && isBlockedByMicroblocks(direction, conduit)) { + return false; + } return getConnectionMode(direction) != ConnectionMode.DISABLED && conduit.getConnectionMode(direction.getOpposite()) != ConnectionMode.DISABLED; } @@ -241,6 +248,22 @@ public abstract class AbstractConduit implements IConduit { return false; } + protected boolean isBlockedByMicroblocks(ForgeDirection direction, IConduit conduit) { + IMicroblockCoverSystem covers = getBundle().getCoverSystem(); + for (Part part : covers.getAllParts()) { + if (part.type.getPartClass() == EnumPartClass.Panel) { + return part.pos == EnumPosition.getFacePosition(direction.ordinal()); + } + } + covers = conduit.getBundle().getCoverSystem(); + for (Part part : covers.getAllParts()) { + if (part.type.getPartClass() == EnumPartClass.Panel) { + return part.pos == EnumPosition.getFacePosition(direction.getOpposite().ordinal()); + } + } + return false; + } + @Override public Set getExternalConnections() { return externalConnections; diff --git a/src/main/java/crazypants/enderio/conduit/BlockConduitBundle.java b/src/main/java/crazypants/enderio/conduit/BlockConduitBundle.java index cdb32b48..76576542 100644 --- a/src/main/java/crazypants/enderio/conduit/BlockConduitBundle.java +++ b/src/main/java/crazypants/enderio/conduit/BlockConduitBundle.java @@ -6,6 +6,8 @@ import java.util.Iterator; import java.util.List; import java.util.Random; +import mods.immibis.core.api.multipart.IMultipartRenderingBlockMarker; +import mods.immibis.core.api.multipart.IMultipartSystem; import net.minecraft.block.Block; import net.minecraft.client.Minecraft; import net.minecraft.client.audio.ISound; @@ -36,6 +38,7 @@ import com.enderio.core.common.util.Util; import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.Optional; +import cpw.mods.fml.common.Optional.Interface; import cpw.mods.fml.common.eventhandler.SubscribeEvent; import cpw.mods.fml.common.network.IGuiHandler; import cpw.mods.fml.relauncher.Side; @@ -72,8 +75,11 @@ import crazypants.enderio.network.PacketHandler; import crazypants.enderio.tool.ToolUtil; import crazypants.util.IFacade; -@Optional.Interface(iface = "powercrystals.minefactoryreloaded.api.rednet.IRedNetOmniNode", modid = "MineFactoryReloaded") -public class BlockConduitBundle extends BlockEio implements IGuiHandler, IFacade, IRotatableFacade, IRedNetOmniNode { +@Optional.InterfaceList({ + @Interface(iface = "powercrystals.minefactoryreloaded.api.rednet.IRedNetOmniNode", modid = "MineFactoryReloaded"), + @Interface(iface = "mods.immibis.core.api.multipart.IMultipartRenderingBlockMarker", modid = "ImmibisMicroblocks") +}) +public class BlockConduitBundle extends BlockEio implements IGuiHandler, IFacade, IRotatableFacade, IRedNetOmniNode, IMultipartRenderingBlockMarker { private static final String KEY_CONNECTOR_ICON = "enderIO:conduitConnector"; private static final String KEY_CONNECTOR_ICON_EXTERNAL = "enderIO:conduitConnectorExternal"; @@ -132,6 +138,10 @@ public class BlockConduitBundle extends BlockEio implements IGuiHandler, IFacade @SideOnly(Side.CLIENT) @Override public boolean addHitEffects(World world, MovingObjectPosition target, EffectRenderer effectRenderer) { + if (MicroblocksUtil.supportMicroblocks() && IM__addHitEffects(world, target, effectRenderer)) { + return true; + } + IIcon tex = null; TileConduitBundle cb = (TileConduitBundle) world.getTileEntity(target.blockX, target.blockY, target.blockZ); @@ -157,6 +167,10 @@ public class BlockConduitBundle extends BlockEio implements IGuiHandler, IFacade @Override @SideOnly(Side.CLIENT) public boolean addDestroyEffects(World world, int x, int y, int z, int meta, EffectRenderer effectRenderer) { + if (MicroblocksUtil.supportMicroblocks() && IM__addDestroyEffects(world, x, y, z, meta, effectRenderer)) { + return true; + } + IIcon tex = lastRemovedComponetIcon; byte b0 = 4; for (int j1 = 0; j1 < b0; ++j1) { @@ -291,8 +305,12 @@ public class BlockConduitBundle extends BlockEio implements IGuiHandler, IFacade @Override public boolean isSideSolid(IBlockAccess world, int x, int y, int z, ForgeDirection side) { + if (MicroblocksUtil.supportMicroblocks() && IM__isSideSolid(world, x, y, z, side)) { + return true; + } + TileEntity te = world.getTileEntity(x, y, z); - if(!(te instanceof IConduitBundle)) { + if (!(te instanceof IConduitBundle)) { return false; } IConduitBundle con = (IConduitBundle) te; @@ -446,18 +464,26 @@ public class BlockConduitBundle extends BlockEio implements IGuiHandler, IFacade breakBlock = te.getConduits().isEmpty() && !te.hasFacade(); - if(!world.isRemote && !player.capabilities.isCreativeMode) { + if(!breakBlock) { + world.markBlockForUpdate(x, y, z); + } + + // TODO no microblock sounds...not sure if fixable, need to contact immibis + if (MicroblocksUtil.supportMicroblocks()) { + IM__getDrops(drop, world, x, y, z, te.getEntity().getBlockMetadata(), 0); + } + + if (!world.isRemote && !player.capabilities.isCreativeMode) { for (ItemStack st : drop) { Util.dropItems(world, st, x, y, z, false); } } - if(!breakBlock) { - world.markBlockForUpdate(x, y, z); - return false; + if (breakBlock) { + world.setBlockToAir(x, y, z); + return true; } - world.setBlockToAir(x, y, z); - return true; + return false; } private boolean breakConduit(IConduitBundle te, List drop, RaytraceResult rt, EntityPlayer player) { @@ -761,6 +787,10 @@ public class BlockConduitBundle extends BlockEio implements IGuiHandler, IFacade @Override public void addCollisionBoxesToList(World world, int x, int y, int z, AxisAlignedBB axisalignedbb, @SuppressWarnings("rawtypes") List arraylist, Entity par7Entity) { + + if (MicroblocksUtil.supportMicroblocks()) { + IM__addCollisionBoxesToList(world, x, y, z, axisalignedbb, arraylist, par7Entity); + } TileEntity te = world.getTileEntity(x, y, z); if(!(te instanceof IConduitBundle)) { @@ -849,16 +879,21 @@ public class BlockConduitBundle extends BlockEio implements IGuiHandler, IFacade @Override public MovingObjectPosition collisionRayTrace(World world, int x, int y, int z, Vec3 origin, Vec3 direction) { + RaytraceResult raytraceResult = doRayTrace(world, x, y, z, origin, direction, null); - if(raytraceResult == null) { - return null; + MovingObjectPosition ret = null; + if (raytraceResult != null) { + ret = raytraceResult.movingObjectPosition; + if (ret != null) { + ret.hitInfo = raytraceResult.component; + } } - if(raytraceResult.movingObjectPosition != null) { - raytraceResult.movingObjectPosition.hitInfo = raytraceResult.component; - + if (MicroblocksUtil.supportMicroblocks()) { + return IM__collisionRayTrace(ret, world, x, y, z, origin, direction); } - return raytraceResult.movingObjectPosition; + + return ret; } public RaytraceResult doRayTrace(World world, int x, int y, int z, EntityPlayer entityPlayer) { @@ -1032,4 +1067,41 @@ public class BlockConduitBundle extends BlockEio implements IGuiHandler, IFacade IConduitBundle bundle = (IConduitBundle) te; return bundle.getConduit(IRedstoneConduit.class); } + + @Override + public ItemStack getPickBlock(MovingObjectPosition target, World world, int x, int y, int z, EntityPlayer player) { + ItemStack coverPick = IMultipartSystem.instance.hook_getPickBlock(target, world, x, y, z, player); + if(coverPick != null) + return coverPick; + + return super.getPickBlock(target, world, x, y, z, player); + } + + // IM Hooks + + private boolean IM__isSideSolid(IBlockAccess world, int x, int y, int z, ForgeDirection side) { + return IMultipartSystem.instance.hook_isSideSolid(world, x, y, z, side); + } + + private boolean IM__addDestroyEffects(World world, int x, int y, int z, int meta, EffectRenderer effectRenderer) { + return IMultipartSystem.instance.hook_addDestroyEffects(world, x, y, z, meta, effectRenderer); + } + + @SideOnly(Side.CLIENT) + private boolean IM__addHitEffects(World worldObj, MovingObjectPosition target, EffectRenderer effectRenderer) { + return IMultipartSystem.instance.hook_addHitEffects(worldObj, target, effectRenderer); + } + + private MovingObjectPosition IM__collisionRayTrace(MovingObjectPosition cur, World world, int x, int y, int z, Vec3 src, Vec3 dst) { + return IMultipartSystem.instance.hook_collisionRayTrace(cur, world, x, y, z, src, dst); + } + + @SuppressWarnings({ "rawtypes", "unchecked" }) + private void IM__addCollisionBoxesToList(World world, int x, int y, int z, AxisAlignedBB mask, List list, Entity entity) { + IMultipartSystem.instance.hook_addCollisionBoxesToList(world, x, y, z, mask, list, entity); + } + + private ArrayList IM__getDrops(List cur, World world, int x, int y, int z, int metadata, int fortune) { + return IMultipartSystem.instance.hook_getDrops(cur, world, x, y, z, metadata, fortune); + } } diff --git a/src/main/java/crazypants/enderio/conduit/IConduitBundle.java b/src/main/java/crazypants/enderio/conduit/IConduitBundle.java index 644a8fe5..7e8534a3 100644 --- a/src/main/java/crazypants/enderio/conduit/IConduitBundle.java +++ b/src/main/java/crazypants/enderio/conduit/IConduitBundle.java @@ -5,6 +5,7 @@ import java.util.List; import java.util.Set; import mekanism.api.gas.IGasHandler; +import mods.immibis.microblocks.api.IMicroblockSupporterTile; import net.minecraft.block.Block; import net.minecraft.tileentity.TileEntity; import net.minecraft.world.IBlockAccess; @@ -27,9 +28,10 @@ import crazypants.enderio.power.IInternalPowerHandler; @InterfaceList({ @Interface(iface = "appeng.api.networking.IGridHost", modid = "appliedenergistics2"), - @Interface(iface = "mekanism.api.gas.IGasHandler", modid = "MekanismAPI|gas") + @Interface(iface = "mekanism.api.gas.IGasHandler", modid = "MekanismAPI|gas"), + @Interface(iface = "mods.immibis.microblocks.api.IMicroblockSupporterTile", modid = "ImmibisMicroblocks") }) -public interface IConduitBundle extends IInternalPowerHandler, IFluidHandler, IItemDuct, IGasHandler, IGridHost { +public interface IConduitBundle extends IInternalPowerHandler, IFluidHandler, IItemDuct, IGasHandler, IGridHost, IMicroblockSupporterTile { TileEntity getEntity(); diff --git a/src/main/java/crazypants/enderio/conduit/MicroblocksUtil.java b/src/main/java/crazypants/enderio/conduit/MicroblocksUtil.java new file mode 100644 index 00000000..e76b71a1 --- /dev/null +++ b/src/main/java/crazypants/enderio/conduit/MicroblocksUtil.java @@ -0,0 +1,40 @@ +package crazypants.enderio.conduit; + +import mods.immibis.microblocks.api.EnumPosition; +import net.minecraftforge.common.util.ForgeDirection; +import cpw.mods.fml.common.Loader; +import cpw.mods.fml.common.Optional.Method; + +public class MicroblocksUtil { + + private static boolean useCheckPerformed = false; + private static boolean supportMicroblocks = false; + + public static boolean supportMicroblocks() { + if (!useCheckPerformed) { + supportMicroblocks = Loader.isModLoaded("ImmibisMicroblocks"); + useCheckPerformed = true; + } + return supportMicroblocks; + } + + @Method(modid = "ImmibisMicroblocks") + public static ForgeDirection posToDir(EnumPosition pos) { + switch(pos) { + case FaceNX: + return ForgeDirection.WEST; + case FaceNY: + return ForgeDirection.DOWN; + case FaceNZ: + return ForgeDirection.NORTH; + case FacePX: + return ForgeDirection.EAST; + case FacePY: + return ForgeDirection.UP; + case FacePZ: + return ForgeDirection.SOUTH; + default: + return null; + } + } +} diff --git a/src/main/java/crazypants/enderio/conduit/TileConduitBundle.java b/src/main/java/crazypants/enderio/conduit/TileConduitBundle.java index 88bfefe7..a92db7c1 100644 --- a/src/main/java/crazypants/enderio/conduit/TileConduitBundle.java +++ b/src/main/java/crazypants/enderio/conduit/TileConduitBundle.java @@ -9,10 +9,21 @@ import java.util.Set; import mekanism.api.gas.Gas; import mekanism.api.gas.GasStack; +import mods.immibis.microblocks.api.EnumPartClass; +import mods.immibis.microblocks.api.EnumPosition; +import mods.immibis.microblocks.api.IMicroblockCoverSystem; +import mods.immibis.microblocks.api.IMicroblockSystem; +import mods.immibis.microblocks.api.MicroblockAPIUtils; +import mods.immibis.microblocks.api.Part; +import mods.immibis.microblocks.api.PartType; import net.minecraft.block.Block; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; +import net.minecraft.network.NetworkManager; +import net.minecraft.network.Packet; +import net.minecraft.network.play.server.S35PacketUpdateTileEntity; +import net.minecraft.tileentity.TileEntity; import net.minecraft.world.IBlockAccess; import net.minecraft.world.World; import net.minecraftforge.common.util.ForgeDirection; @@ -23,7 +34,9 @@ import appeng.api.networking.IGridNode; import appeng.api.util.AECableType; import com.enderio.core.client.render.BoundingBox; +import com.google.common.collect.Sets; +import cpw.mods.fml.common.Loader; import cpw.mods.fml.common.Optional.Method; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; @@ -72,9 +85,13 @@ public class TileConduitBundle extends TileEntityEio implements IConduitBundle { private FacadeRenderState facadeRenderAs; private ConduitDisplayMode lastMode = ConduitDisplayMode.ALL; - + + private boolean supportsMicroblocks; + private Object covers; + public TileConduitBundle() { - blockType = EnderIO.blockConduitBundle; + this.blockType = EnderIO.blockConduitBundle; + initMicroblocks(); } @Override @@ -108,6 +125,10 @@ public class TileConduitBundle extends TileEntityEio implements IConduitBundle { } nbtRoot.setInteger("facadeMeta", facadeMeta); nbtRoot.setShort("nbtVersion", NBT_VERSION); + + if (supportsMicroblocks) { + writeMicroblocksToNBT(nbtRoot); + } } @Override @@ -143,6 +164,9 @@ public class TileConduitBundle extends TileEntityEio implements IConduitBundle { clientUpdated = true; } + if (supportsMicroblocks) { + readMicroblocksFromNBT(nbtRoot); + } } @Override @@ -890,4 +914,127 @@ public class TileConduitBundle extends TileEntityEio implements IConduitBundle { public boolean displayPower() { return true; } + + // Immibis Microblocks + + private void initMicroblocks() { + this.supportsMicroblocks = Loader.isModLoaded("ImmibisMicroblocks"); + if (this.supportsMicroblocks) { + createCovers(); + } + } + + private void createCovers() { + IMicroblockSystem ims = MicroblockAPIUtils.getMicroblockSystem(); + if (ims != null) { + covers = ims.createMicroblockCoverSystem(this); + } + } + + @Override + @Method(modid = "ImmibisMicroblocks") + public boolean isPlacementBlocked(PartType part, EnumPosition pos) { + EnumPartClass type = part.getPartClass(); + if (type == EnumPartClass.Strip) { + // No slab strips or pillars + return part.getSize() > 0.3 || pos == EnumPosition.PostX || pos == EnumPosition.PostY || pos == EnumPosition.PostZ; + } else if (type == EnumPartClass.Panel || type == EnumPartClass.HollowPanel) { + return part.getSize() > 0.3; + } + return false; + } + + @Override + @Method(modid = "ImmibisMicroblocks") + public IMicroblockCoverSystem getCoverSystem() { + return (IMicroblockCoverSystem) covers; + } + + private void writeMicroblocksToNBT(NBTTagCompound tag) { + if (covers != null) { + ((IMicroblockCoverSystem) covers).writeToNBT(tag); + } + } + + private void readMicroblocksFromNBT(NBTTagCompound tag) { + if (covers != null) { + ((IMicroblockCoverSystem) covers).readFromNBT(tag); + } + } + + @Override + @Method(modid = "ImmibisMicroblocks") + public Packet getDescriptionPacket() { + if (covers == null) + return null; + + S35PacketUpdateTileEntity p = (S35PacketUpdateTileEntity) super.getDescriptionPacket(); + + NBTTagCompound tag = p.func_148857_g(); + tag.setByteArray("C", ((IMicroblockCoverSystem) covers).writeDescriptionBytes()); + return p; + } + + @Override + @Method(modid = "ImmibisMicroblocks") + public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt) { + super.onDataPacket(net, pkt); + if (covers != null) { + ((IMicroblockCoverSystem) covers).readDescriptionBytes(pkt.func_148857_g().getByteArray("C"), 0); + } + } + + @Override + public void onMicroblocksChanged() { + Set needUpdates = EnumSet.allOf(ForgeDirection.class); + for (Part p : getCoverSystem().getAllParts()) { + if (p.type.getPartClass() == EnumPartClass.Panel) { + ForgeDirection dir = MicroblocksUtil.posToDir(p.pos); + updateConnections(dir, true); + needUpdates.remove(dir); + } + } + for (ForgeDirection dir : needUpdates) { + updateConnections(dir, false); + } + + worldObj.notifyBlocksOfNeighborChange(xCoord, yCoord, zCoord, getBlockType()); + updateBlock(); + } + + private void updateConnections(ForgeDirection dir, boolean remove) { + TileEntity neighbor = getLocation().getLocation(dir).getTileEntity(worldObj); + IConduitBundle neighborBundle = (IConduitBundle) (neighbor instanceof IConduitBundle ? neighbor : null); + for (IConduit c : getConduits()) { + if (remove) { + removeConnection(dir, c); + } else if (neighborBundle != null) { + addConnection(dir, c, neighborBundle.getConduit(c.getBaseConduitType())); + } + c.connectionsChanged(); + } + dir = dir.getOpposite(); + if (neighbor instanceof IConduitBundle) { + for (IConduit c : ((TileConduitBundle) neighbor).getConduits()) { + if (remove) { + removeConnection(dir, c); + } else if (neighborBundle != null) { + addConnection(dir, neighborBundle.getConduit(c.getBaseConduitType()), c); + } + c.connectionsChanged(); + } + } + } + + private void removeConnection(ForgeDirection dir, IConduit c) { + if (c.getConduitConnections().contains(dir)) { + c.conduitConnectionRemoved(dir); + } + } + + private void addConnection(ForgeDirection dir, IConduit c, IConduit connectingTo) { + if (!c.getConduitConnections().contains(dir) && connectingTo.canConnectToConduit(dir, c)) { + c.conduitConnectionAdded(dir); + } + } }