-
Notifications
You must be signed in to change notification settings - Fork 11
Add support for falling blocks #520
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
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
0009b0b
Add support for falling blocks
Intybyte 0a8e035
Expose entity cuz why not
Intybyte 8676e61
Add proper events and handling
Intybyte 8d2229c
Add more context data to entity
Intybyte a266a19
Add more description
Intybyte 77f96d3
Prevent dropping wrong item
Intybyte 1a3186b
Rename class
Intybyte 9e47822
Add interface doc
Intybyte 7c31846
Update BlockStorage#loadBlock docs
Intybyte 114f300
Update BlockStorage#loadBlock docs pt2
Intybyte b4480c6
Apply jvm static
Intybyte 8c76a8d
Use PylonSerializers instead
Intybyte 99885b3
Use PylonSerializers.BLOCK_POSITION instead
Intybyte cbe6eb5
Remove println
Intybyte 60cccb0
Call method after deserialization
Intybyte 990673c
Rename attribute
Intybyte fad740f
Remove dead code
Intybyte e8d7c84
use variable
Intybyte 2924f3d
Add interface method for dropping items
Intybyte 0277727
Merge branch 'master' into vaan/api/falling-py-block
LordIdra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
pylon-core/src/main/kotlin/io/github/pylonmc/pylon/core/block/base/PylonFallingBlock.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| package io.github.pylonmc.pylon.core.block.base | ||
|
|
||
| import io.github.pylonmc.pylon.core.PylonCore | ||
| import io.github.pylonmc.pylon.core.block.BlockStorage | ||
| import io.github.pylonmc.pylon.core.block.PylonBlock | ||
| import io.github.pylonmc.pylon.core.block.PylonBlockSchema | ||
| import io.github.pylonmc.pylon.core.datatypes.NamespacedKeyPersistentDataType | ||
| import io.github.pylonmc.pylon.core.datatypes.PylonSerializers | ||
| import io.github.pylonmc.pylon.core.entity.PylonEntity | ||
| import io.github.pylonmc.pylon.core.registry.PylonRegistry | ||
| import io.github.pylonmc.pylon.core.util.position.BlockPosition | ||
| import org.bukkit.NamespacedKey | ||
| import org.bukkit.entity.FallingBlock | ||
| import org.bukkit.event.entity.EntityChangeBlockEvent | ||
| import org.bukkit.event.entity.EntityDropItemEvent | ||
| import org.bukkit.persistence.PersistentDataContainer | ||
|
|
||
| /** | ||
| * Interface meant to be used for all pylon blocks affected by gravity, like sand, gravel etc. | ||
| * | ||
| * If you implement this interface, and the block can't fall, then the methods will never be called. | ||
| * | ||
| * Beware of how you modify the passed data in the entity, because of the order of serialization and deserialization | ||
| * some modification need to be applied directly to the PDC stored in the entity, or they will be lost. | ||
| * | ||
| * Also at some steps, the entity or the block might not exist yet in their relative storage, | ||
| * handle this interface with caution as it needs to handle state using internals most of the time. | ||
| * | ||
| * As a suggestion, don't make important blocks with lots of data affected by gravity, | ||
| * or it might become a nightmare to apply the correct changes. | ||
| */ | ||
| interface PylonFallingBlock { | ||
Intybyte marked this conversation as resolved.
Show resolved
Hide resolved
Intybyte marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /** | ||
| * When calling this, the entity doesn't exist yet in [io.github.pylonmc.pylon.core.entity.EntityStorage] | ||
| * Called after serialization | ||
| */ | ||
| fun onFallStart(event: EntityChangeBlockEvent, spawnedEntity: PylonFallingBlockEntity) | ||
|
|
||
| /** | ||
| * Called after deserialization | ||
| * Cancelling the event at this step does nothing, and the entity is about to be removed | ||
| */ | ||
| fun onFallStop(event: EntityChangeBlockEvent, entity: PylonFallingBlockEntity) | ||
|
|
||
| /** | ||
| * When called the block doesn't exist in the world and in [BlockStorage] | ||
| */ | ||
| fun onItemDrop(event: EntityDropItemEvent, entity: PylonFallingBlockEntity) = PylonRegistry.ITEMS[(this as PylonBlock).key]?.getItemStack() | ||
|
|
||
| class PylonFallingBlockEntity : PylonEntity<FallingBlock> { | ||
| val fallStartPosition: BlockPosition | ||
| val blockSchema: PylonBlockSchema | ||
| val blockData: PersistentDataContainer | ||
|
|
||
| constructor(blockSchema: PylonBlockSchema, blockData: PersistentDataContainer, fallingStart: BlockPosition, entity: FallingBlock) : super(KEY, entity) { | ||
| this.blockSchema = blockSchema | ||
| this.blockData = blockData | ||
| this.fallStartPosition = fallingStart | ||
| } | ||
|
|
||
| constructor(entity: FallingBlock) : super(entity) { | ||
| val pdc = entity.persistentDataContainer | ||
|
|
||
| val fallingBlockType = pdc.get(FALLING_BLOCK_TYPE, NamespacedKeyPersistentDataType)!! | ||
| this.blockSchema = PylonRegistry.BLOCKS[fallingBlockType]!! | ||
| this.blockData = pdc.get(FALLING_BLOCK_DATA, PylonSerializers.TAG_CONTAINER)!! | ||
| this.fallStartPosition = pdc.get(FALLING_BLOCK_START, PylonSerializers.BLOCK_POSITION)!! | ||
| } | ||
|
|
||
| override fun write(pdc: PersistentDataContainer) { | ||
| pdc.set(FALLING_BLOCK_TYPE, NamespacedKeyPersistentDataType, blockSchema.key) | ||
| pdc.set(FALLING_BLOCK_DATA, PylonSerializers.TAG_CONTAINER, blockData) | ||
| pdc.set(FALLING_BLOCK_START, PylonSerializers.BLOCK_POSITION, fallStartPosition) | ||
| } | ||
| } | ||
|
|
||
| companion object { | ||
| @JvmField | ||
| val KEY = NamespacedKey(PylonCore, "falling_pylon_block") | ||
|
|
||
| @JvmField | ||
| val FALLING_BLOCK_DATA = NamespacedKey(PylonCore, "falling_pylon_block_data") | ||
|
|
||
| @JvmField | ||
| val FALLING_BLOCK_TYPE = NamespacedKey(PylonCore, "falling_pylon_block_type") | ||
|
|
||
| @JvmField | ||
| val FALLING_BLOCK_START = NamespacedKey(PylonCore, "falling_pylon_block_start") | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.