diff --git a/Server/Plugins/APIDump/APIDesc.lua b/Server/Plugins/APIDump/APIDesc.lua index 193a58285..a3ebde9bb 100644 --- a/Server/Plugins/APIDump/APIDesc.lua +++ b/Server/Plugins/APIDump/APIDesc.lua @@ -16518,6 +16518,46 @@ end { Notes = "A flag in the metadata of droppers and dispensers that indicates that the dropper or dispenser is looking in the positive Z direction.", }, + E_META_END_PORTAL_FRAME_EYE = + { + Notes = "A flag in the metadata of end portal frames that indicates that the portal frame has an eye in it.", + }, + E_META_END_PORTAL_FRAME_NO_EYE = + { + Notes = "The lack of the flag in the metadata of end portal frames indicating that the portal frame has an eye in it.", + }, + E_META_END_PORTAL_FRAME_XM = + { + Notes = "A flag in the metadata of end portal frames that indicates that the portal frame is facing the negative X direction.", + }, + E_META_END_PORTAL_FRAME_XP = + { + Notes = "A flag in the metadata of end portal frames that indicates that the portal frame is facing the positive X direction.", + }, + E_META_END_PORTAL_FRAME_ZM = + { + Notes = "A flag in the metadata of end portal frames that indicates that the portal frame is facing the negative Z direction.", + }, + E_META_END_PORTAL_FRAME_ZP = + { + Notes = "A flag in the metadata of end portal frames that indicates that the portal frame is facing the positive Z direction.", + }, + E_META_END_PORTAL_FRAME_XM_EYE = + { + Notes = "A flag in the metadata of end portal frames that indicates that the portal frame is facing the negative X direction and has an ender eye in it.", + }, + E_META_END_PORTAL_FRAME_XP_EYE = + { + Notes = "A flag in the metadata of end portal frames that indicates that the portal frame is facing the positive X direction and has an ender eye in it.", + }, + E_META_END_PORTAL_FRAME_ZM_EYE = + { + Notes = "A flag in the metadata of end portal frames that indicates that the portal frame is facing the negative Z direction and has an ender eye in it.", + }, + E_META_END_PORTAL_FRAME_ZP_EYE = + { + Notes = "A flag in the metadata of end portal frames that indicates that the portal frame is facing the positive Z direction and has an ender eye in it.", + }, E_META_HEAD_CREEPER = { Notes = "A flag in the metadata of heads that indicates that the head is a creeper head.", diff --git a/src/BlockID.h b/src/BlockID.h index 6a1eefac4..d7b362c41 100644 --- a/src/BlockID.h +++ b/src/BlockID.h @@ -659,6 +659,18 @@ enum ENUM_BLOCK_META : NIBBLETYPE E_META_DOUBLE_STONE_SLAB_SMOOTH_SANDSTONE = 9, E_META_DOUBLE_STONE_SLAB_TILE_QUARTZ = 10, + // E_BLOCK_END_PORTAL_FRAME metas: + E_META_END_PORTAL_FRAME_ZP = 0, // Faces towards centre of portal + E_META_END_PORTAL_FRAME_XM = 1, + E_META_END_PORTAL_FRAME_ZM = 2, + E_META_END_PORTAL_FRAME_XP = 3, + E_META_END_PORTAL_FRAME_ZP_EYE = 4, // Frames with ender eye + E_META_END_PORTAL_FRAME_XM_EYE = 5, + E_META_END_PORTAL_FRAME_ZM_EYE = 6, + E_META_END_PORTAL_FRAME_XP_EYE = 7, + E_META_END_PORTAL_FRAME_NO_EYE = 0, // Just the eye bitflag + E_META_END_PORTAL_FRAME_EYE = 4, + // E_BLOCK_FLOWER metas: E_META_FLOWER_POPPY = 0, E_META_FLOWER_BLUE_ORCHID = 1, diff --git a/src/Blocks/BlockEndPortalFrame.h b/src/Blocks/BlockEndPortalFrame.h new file mode 100644 index 000000000..e1741f5f6 --- /dev/null +++ b/src/Blocks/BlockEndPortalFrame.h @@ -0,0 +1,258 @@ + +#pragma once + +#include "BlockHandler.h" + + + + + +class cBlockEndPortalFrameHandler : + public cMetaRotator +{ +public: + cBlockEndPortalFrameHandler(BLOCKTYPE a_BlockType): + cMetaRotator(a_BlockType) + { + } + + + + + + virtual bool GetPlacementBlockTypeMeta( + cChunkInterface & a_ChunkInterface, cPlayer & a_Player, + int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace, + int a_CursorX, int a_CursorY, int a_CursorZ, + BLOCKTYPE & a_BlockType, NIBBLETYPE & a_BlockMeta + ) override + { + a_BlockType = m_BlockType; + a_BlockMeta = YawToMetaData(a_Player.GetYaw()); + return true; + } + + + + + + inline static NIBBLETYPE YawToMetaData(double a_Rotation) + { + a_Rotation += 90 + 45; // So its not aligned with axis + if (a_Rotation > 360) + { + a_Rotation -= 360; + } + + if ((a_Rotation >= 0) && (a_Rotation < 90)) + { + return E_META_END_PORTAL_FRAME_XM; + } + else if ((a_Rotation >= 180) && (a_Rotation < 270)) + { + return E_META_END_PORTAL_FRAME_XP; + } + else if ((a_Rotation >= 90) && (a_Rotation < 180)) + { + return E_META_END_PORTAL_FRAME_ZM; + } + else + { + return E_META_END_PORTAL_FRAME_ZP; + } + } + + + + + + virtual void OnPlaced(cChunkInterface & a_ChunkInterface, cWorldInterface & a_WorldInterface, int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) override + { + // E_META_END_PORTAL_FRAME_EYE is the bit which signifies the eye of ender is in it. + // LOG("PortalPlaced, meta %d", a_BlockMeta); + if ((a_BlockMeta & E_META_END_PORTAL_FRAME_EYE) == E_META_END_PORTAL_FRAME_EYE) + { + // LOG("Location is %d %d %d", a_BlockX, a_BlockY, a_BlockZ); + // Direction is the first two bits, masked by 0x3 + FindAndSetPortal(Vector3i(a_BlockX, a_BlockY, a_BlockZ), a_BlockMeta & 3, a_ChunkInterface, a_WorldInterface); + } + } + + + + + + /** Returns false if portal cannot be made, true if portal was made. */ + bool FindAndSetPortal(Vector3i a_FirstFrame, NIBBLETYPE a_Direction, cChunkInterface & a_ChunkInterface, cWorldInterface & a_WorldInterface) + { + /* + PORTAL FINDING ALGORITH + ======================= + - Get clicked base block + - Check diagonally (clockwise) for another portal block + - if exists, and has eye, Continue. Abort if any are facing the wrong direction. + - if doesn't exist, check horizontally (the block to the left of this block). Abort if there is no horizontal block. + - After a corner has been met, traverse the portal clockwise, ensuring valid portal frames connect the rectangle. + - Track the NorthWest Corner, and the dimensions. + - If dimensions are valid, create the portal. + */ + + static_assert((E_META_END_PORTAL_FRAME_ZM - E_META_END_PORTAL_FRAME_XM) == 1, "Should be going clockwise"); + + const int MIN_PORTAL_WIDTH = 3; + const int MAX_PORTAL_WIDTH = 4; + + // Directions to use for the clockwise traversal. + static const Vector3i Left[] = + { + { 1, 0, 0}, // 0, South, left block is East / XP + { 0, 0, 1}, // 1, West, left block is South / ZP + {-1, 0, 0}, // 2, North, left block is West / XM + { 0, 0, -1}, // 3, East, left block is North / ZM + }; + static const Vector3i LeftForward[] = + { + { 1, 0, 1}, // 0, South, left block is SouthEast / XP ZP + {-1, 0, 1}, // 1, West, left block is SouthWest / XM ZP + {-1, 0, -1}, // 2, North, left block is NorthWest / XM ZM + { 1, 0, -1}, // 3, East, left block is NorthEast / XP ZM + }; + + + int EdgesComplete = -1; // We start search _before_ finding the first edge + Vector3i NorthWestCorner; + int EdgeWidth[4] = { 1, 1, 1, 1 }; + NIBBLETYPE CurrentDirection = a_Direction; + Vector3i CurrentPos = a_FirstFrame; + + // Scan clockwise until we have seen all 4 edges + while (EdgesComplete < 4) + { + // Check if we are at a corner + Vector3i NextPos = CurrentPos + LeftForward[CurrentDirection]; + if (IsPortalFrame(a_ChunkInterface.GetBlock(NextPos))) + { + // We have found the corner, move clockwise to next edge + if (CurrentDirection == E_META_END_PORTAL_FRAME_XP) + { + // We are on the NW (XM, ZM) Corner + // Relative to the previous frame, the portal should appear to the right of this portal frame. + NorthWestCorner = NextPos - Left[CurrentDirection]; + } + + if (EdgesComplete == -1) + { + // Reset current width, we will revisit it last + EdgeWidth[CurrentDirection] = 1; + } + + // Rotate 90 degrees clockwise + CurrentDirection = (CurrentDirection + 1) % 4; + EdgesComplete++; + } + else + { + // We are not at a corner, keep walking the edge + NextPos = CurrentPos + Left[CurrentDirection]; + + EdgeWidth[CurrentDirection]++; + if (EdgeWidth[CurrentDirection] > MAX_PORTAL_WIDTH) + { + // Don't build a portal that is too long. + return false; + } + } + + if (!IsValidFrameAtPos(a_ChunkInterface, NextPos, CurrentDirection)) + { + // Neither the edge nor the corner are valid portal blocks. + return false; + } + + CurrentPos = NextPos; + } + + if ((EdgeWidth[0] != EdgeWidth[2]) || (EdgeWidth[1] != EdgeWidth[3])) + { + // Mismatched Portal Dimensions. + return false; + } + if ((EdgeWidth[0] < MIN_PORTAL_WIDTH) || (EdgeWidth[1] < MIN_PORTAL_WIDTH)) + { + // Portal too small. + return false; + } + + // LOG("NW corner (low corner) %d %d %d", Corner.x, Corner.y, Corner.z); + // LOG("%d by %d", Width[0], Width[1]); + for (int i = 0; i < EdgeWidth[0]; i++) + { + for (int j = 0; j < EdgeWidth[1]; j++) + { + a_ChunkInterface.SetBlock(NorthWestCorner.x + i, NorthWestCorner.y, NorthWestCorner.z + j, E_BLOCK_END_PORTAL, 0); + // TODO: Create block entity so portal doesn't become invisible on relog. + } + } + return true; + } + + + + + + /** Return true if this block is a portal frame, has an eye, and is facing the correct direction. */ + bool IsValidFrameAtPos(cChunkInterface & a_ChunkInterface, Vector3i a_BlockPos, NIBBLETYPE a_ShouldFace) + { + BLOCKTYPE BlockType; + NIBBLETYPE BlockMeta; + + return ( + a_ChunkInterface.GetBlockTypeMeta(a_BlockPos, BlockType, BlockMeta) && + (BlockType == E_BLOCK_END_PORTAL_FRAME) && + (BlockMeta == (a_ShouldFace | E_META_END_PORTAL_FRAME_EYE)) + ); + } + + + + + /** Return true if this block is a portal frame. */ + bool IsPortalFrame(BLOCKTYPE BlockType) + { + return (BlockType == E_BLOCK_END_PORTAL_FRAME); + } + + + + + + virtual bool IsClickedThrough(void) override + { + // TODO: Colision + return true; + } + + + + + + virtual ColourID GetMapBaseColourID(NIBBLETYPE a_Meta) override + { + UNUSED(a_Meta); + return 27; + } +}; + + + + diff --git a/src/Blocks/BlockHandler.cpp b/src/Blocks/BlockHandler.cpp index cb17bcf82..923d02a80 100644 --- a/src/Blocks/BlockHandler.cpp +++ b/src/Blocks/BlockHandler.cpp @@ -28,6 +28,7 @@ #include "BlockDropSpenser.h" #include "BlockEnchantmentTable.h" #include "BlockEnderchest.h" +#include "BlockEndPortalFrame.h" #include "BlockEntity.h" #include "BlockFarmland.h" #include "BlockFence.h" @@ -230,6 +231,7 @@ cBlockHandler * cBlockHandler::CreateBlockHandler(BLOCKTYPE a_BlockType) case E_BLOCK_DROPPER: return new cBlockDropSpenserHandler (a_BlockType); case E_BLOCK_EMERALD_ORE: return new cBlockOreHandler (a_BlockType); case E_BLOCK_ENCHANTMENT_TABLE: return new cBlockEnchantmentTableHandler(a_BlockType); + case E_BLOCK_END_PORTAL_FRAME: return new cBlockEndPortalFrameHandler (a_BlockType); case E_BLOCK_ENDER_CHEST: return new cBlockEnderchestHandler (a_BlockType); case E_BLOCK_FARMLAND: return new cBlockFarmlandHandler (a_BlockType); case E_BLOCK_FENCE: return new cBlockFenceHandler (a_BlockType); diff --git a/src/Blocks/CMakeLists.txt b/src/Blocks/CMakeLists.txt index 07ab87eea..4420871ae 100644 --- a/src/Blocks/CMakeLists.txt +++ b/src/Blocks/CMakeLists.txt @@ -34,6 +34,7 @@ SET (HDRS BlockDropSpenser.h BlockEnchantmentTable.h BlockEnderchest.h + BlockEndPortalFrame.h BlockEntity.h BlockFarmland.h BlockFence.h diff --git a/src/Items/ItemEyeOfEnder.h b/src/Items/ItemEyeOfEnder.h new file mode 100644 index 000000000..f911955a1 --- /dev/null +++ b/src/Items/ItemEyeOfEnder.h @@ -0,0 +1,59 @@ + +#pragma once + +#include "ItemHandler.h" +#include "ItemThrowable.h" + + + + + +class cItemEyeOfEnderHandler : + public cItemThrowableHandler +{ + typedef cItemThrowableHandler super; +public: + cItemEyeOfEnderHandler(void) : + super(E_ITEM_EYE_OF_ENDER, cProjectileEntity::pkSnowball, 30) + { + } + + virtual bool OnItemUse( + cWorld * a_World, cPlayer * a_Player, cBlockPluginInterface & a_PluginInterface, const cItem & a_Item, + int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace + ) override + { + BLOCKTYPE FacingBlock; + NIBBLETYPE FacingMeta; + a_World->GetBlockTypeMeta(a_BlockX, a_BlockY, a_BlockZ, FacingBlock, FacingMeta); + switch (FacingBlock) + { + case E_BLOCK_END_PORTAL_FRAME: + { + // Fill the portal frame. E_META_END_PORTAL_EYE is the bit for holding the eye of ender. + if ((FacingMeta & E_META_END_PORTAL_FRAME_EYE) != E_META_END_PORTAL_FRAME_EYE) + { + a_World->SetBlock(a_BlockX, a_BlockY, a_BlockZ, E_BLOCK_END_PORTAL_FRAME, FacingMeta | E_META_END_PORTAL_FRAME_EYE); + if (!a_Player->IsGameModeCreative()) + { + a_Player->GetInventory().RemoveOneEquippedItem(); + } + } + break; + } + default: + { + // TODO: Create projectile for Eye Of Ender + // return cItemThrowableHandler::OnItemUse(a_World, a_Player, a_PluginInterface, a_Item, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace); + } + } + + return false; + } + +} ; + + + + + diff --git a/src/Items/ItemHandler.cpp b/src/Items/ItemHandler.cpp index 812003cbb..489ef4f28 100644 --- a/src/Items/ItemHandler.cpp +++ b/src/Items/ItemHandler.cpp @@ -24,6 +24,7 @@ #include "ItemDoor.h" #include "ItemDye.h" #include "ItemEmptyMap.h" +#include "ItemEyeOfEnder.h" #include "ItemFishingRod.h" #include "ItemFlowerPot.h" #include "ItemFood.h" @@ -131,6 +132,7 @@ cItemHandler * cItemHandler::CreateItemHandler(int a_ItemType) case E_ITEM_EGG: return new cItemEggHandler(); case E_ITEM_EMPTY_MAP: return new cItemEmptyMapHandler(); case E_ITEM_ENDER_PEARL: return new cItemEnderPearlHandler(); + case E_ITEM_EYE_OF_ENDER: return new cItemEyeOfEnderHandler(); case E_ITEM_FIRE_CHARGE: return new cItemLighterHandler(a_ItemType); case E_ITEM_FIREWORK_ROCKET: return new cItemFireworkHandler(); case E_ITEM_FISHING_ROD: return new cItemFishingRodHandler(a_ItemType); diff --git a/src/Items/ItemThrowable.h b/src/Items/ItemThrowable.h index 85234088f..706143896 100644 --- a/src/Items/ItemThrowable.h +++ b/src/Items/ItemThrowable.h @@ -1,5 +1,5 @@ -// Declares the itemhandlers for throwable items: eggs, snowballs and ender pearls +// Declares the itemhandlers for throwable items: eggs, snowballs, ender pearls, and eyes of ender. #pragma once