-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BlocksRegistry API #40
Comments
More specifically, I would propose the following interface. (This is just my first draft and by no means finalized.) public interface BlocksRegistry<ItemStackT : Any, BlockT : Any> : MimicService {
/** Returns all known block IDs. */
public val knownIds: Collection<String>
/** Returns `true` if given [block] represented with given [blockId]. */
public fun isSameBlock(block: BlockT, blockId: String): Boolean = getBlockId(block) == blockId
/** Returns `true` if given [blockItem] is an item stack containing the block represented with given [blockId]. */
public fun isSameBlockItem(blockItem: ItemStackT, blockId: String): Boolean = getBlockItemId(blockItem) == blockId
/** Returns `true` if block with given [blockId] exists. */
public fun isBlockExists(blockId: String): Boolean
/** Returns ID representing given [block], or `null` if the ID not found in this registry. */
public fun getBlockId(block: BlockT): String?
/** Returns ID representing the block contained in given [blockItem], or `null` if the ID not found in this registry. */
public fun getBlockItemId(blockItem: ItemStackT): String?
/** Returns item containing block by given [blockId], or `null` if the ID not found in this registry. */
public fun getBlockItem(blockId: String): ItemStackT? = getBlockItem(blockId, payload = null, amount = 1)
/**
* Returns item containing block with specified [payload] by given [blockId], or `null` if the ID not found in this registry.
*
* If [payload] is not `null`, block will be configured using it.
*/
public fun getBlockItem(blockId: String, payload: Any?): ItemStackT? = getBlockItem(blockId, payload, amount = 1)
/**
* Returns item containing block with specified [amount] by given [blockId], or `null` if ID not found in this registry.
*
* If given [amount] is greater than maximum possible, will use maximum possible amount.
* Amount shouldn't be less than `1`.
*/
public fun getBlockItem(blockId: String, amount: Int): ItemStackT? = getBlockItem(blockId, payload = null, amount)
/**
* Returns item containing block with specified [amount] and [payload] by given [blockId],
* or `null` if ID not found in this registry.
*
* If given [amount] is greater than maximum possible, will use maximum possible amount.
* Amount shouldn't be less than `1`.
*
* Given [payload] may be used to configure block.
*/
public fun getBlockItem(blockId: String, payload: Any?, amount: Int): ItemStackT?
/**
* Places block by given [blockId] at [destination].
*
* Returns `true` if the block was placed, and `false` if ID not found in this registry.
*/
public fun placeBlock(blockId: String, destination: BlockT): Boolean = placeBlock(blockId, payload = null, destination)
/**
* Places block with specified [payload] by given [blockId] at [destination].
*
* Given [payload] may be used to configure block.
*
* Returns `true` if the block was placed, and `false` if ID not found in this registry.
*/
public fun placeBlock(blockId: String, payload: Any?, destination: BlockT): Boolean
} |
@knokko, the only thing I don't like in this interface is that blocks and items are mixed here. So we can theoretically get the same item both from |
The motivation for the whole block/item distinction is a bit complex, but here we go: There are basically 2 sides here:
Example usage of
|
Hm. Are we sure the item used to place the block is the same item that should be dropped? Maybe it will be better to make methods similar by functionality to Bukkit methods? If we need to access the item used for the block placement, we can slightly change the signature of this method
I agree this method is useful, but I consider renaming it to /**
* Places block associated with the given [itemStack].
* Returns `true` if the block placed successfully, of `false` if there is no associated block.
* Remember to reduce [itemStack] amount if you want to.
*/
fun placeBlock(itemStack: ItemStack): Boolean = placeBlock(getBlockId(itemStack)) |
I agree that the design of /**
* Destroys the custom block at the given [block]. Nothing happens if the given [block] is not a custom block.
*
* If [droppedItems] is `null`, the drops of the custom block are spawned as items at the block location.
*
* If [droppedItems] is not `null`, the drops of the custom block are added to [droppedItems] rather than
* spawned as items.
*/
fun breakBlock(block: Block, droppedItems: Collection<ItemStackT>) This solution solves two problems:
|
Yes, I thought about a similar method but with slightly different second parameter: fun breakBlock(block: Block, dropsConsumer: DropsConsumer = DropsConsumer.drop())
fun interface DropsConsumer {
/**
* Called if as a result of [breakBlock] method, items should be dropeed.
* Should return `true` if the [drops] consumed, and `false` if it is not.
* If `false` is returned, [drops] will be dropped on the ground near the destroeyed block.
*/
fun consume(drops: Collections<ItemStackT>): Boolean
/** Default consumers implementations. */
companion object {
/** Drops will not be consumed, and will be dropped on the ground. */
@JvmStatic
fun drop() = DropsConsumer { false }
/** Drops just disappear and will not be dropped on the ground. */
@JvmStatic
fun disappear() = DropsConsumer { true }
}
} Do we need parameters And maybe instead of returning |
Current API version: public interface BlocksRegistry<ItemStackT : Any, BlockT : Any> : MimicService {
/** Returns all known blocks IDs. */
public val knownIds: Collection<String>
/** Returns `true` if the given [block] represented with the given [blockId]. */
public fun isSameBlock(block: BlockT, blockId: String): Boolean = getBlockId(block) == blockId
/** Returns `true` if the given [blockItem] is an item stack containing the block represented with the given [blockId]. */
public fun isSameBlockItem(blockItem: ItemStackT, blockId: String): Boolean = getBlockIdFromItem(blockItem) == blockId
/** Returns `true` if block with given [blockId] exists. */
public fun isBlockExists(blockId: String): Boolean
/** Returns ID representing given [block], or `null` if the ID not found in this registry. */
public fun getBlockId(block: BlockT): String?
/** Returns ID representing the block contained in given [blockItem], or `null` if the ID not found in this registry. */
public fun getBlockIdFromItem(blockItem: ItemStackT): String?
/**
* Breaks the given [block]. Drops will be dropped on the ground.
* Returns `false` if the block can't be broken or do not belongs to this registry.
*/
public fun breakBlock(block: BlockT): Boolean = breakBlock(block, tool = null, DropsConsumer.drop())
/**
* Breaks the given [block] using the specified [tool]. Drops will be dropped on the ground.
* Returns `false` if the block can't be broken or do not belongs to this registry.
*/
public fun breakBlock(block: BlockT, tool: ItemStackT?): Boolean = breakBlock(block, tool, DropsConsumer.drop())
/**
* Breaks the given [block] using the specified [tool] and passes drops to the given [dropsConsumer].
* Returns `false` if the block can't be broken or do not belongs to this registry.
*/
public fun breakBlock(block: BlockT, tool: ItemStackT?, dropsConsumer: DropsConsumer): Boolean
/**
* Places block by given [blockId] at [destination].
*
* Returns `true` if the block was placed, and `false` if ID not found in this registry.
*/
public fun placeBlock(blockId: String, destination: BlockT): Boolean = placeBlock(blockId, destination, payload = null)
/**
* Places block with specified [payload] by given [blockId] at [destination].
*
* Given [payload] may be used to configure block.
*
* Returns `true` if the block was placed, and `false` if ID not found in this registry.
*/
public fun placeBlock(blockId: String, destination: BlockT, payload: Any?): Boolean
}
/** Resposible for consuming drops. */
public fun interface DropsConsumer {
/**
* Called if in result of [breakBlock] method, items should be dropeed.
* Returns unconsumed items to be dropped on the ground near the destroyed block.
*/
public fun consume(drops: Collection<ItemStackT>): Collection<ItemStackT>
/** Default consumers implementations. */
public companion object {
/** Drops will not be consumed, and will be dropped on the ground. */
@JvmStatic
public fun drop() = DropsConsumer { it }
/** Drops just disappear and will not be dropped on the ground. */
@JvmStatic
public fun disappear() = DropsConsumer { emptyList() }
}
} |
Add API similar to
ItemsRegistry
but for custom blocks.Features:
Block
orItemStack
(for blocks in the hand of a player)ItemStack
for a given block id and amountBlock
The text was updated successfully, but these errors were encountered: