-
Notifications
You must be signed in to change notification settings - Fork 10
Add PylonProcessor + various small changes #523
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
6 commits
Select commit
Hold shift + click to select a range
4efa529
Add PylonProcessor, PylonRecipeProcessor, and adapt blocks to use them
LordIdra df88469
Smol fix
LordIdra 68967d9
pr comments
LordIdra 81eb903
Merge branch 'pylon-recipe-processor' into recipe-processor-conversion
LordIdra 39e4d9b
fix tests
LordIdra 55d9cc6
PR comments
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
85 changes: 0 additions & 85 deletions
85
pylon-core/src/main/kotlin/io/github/pylonmc/pylon/core/block/TickManager.kt
This file was deleted.
Oops, something went wrong.
148 changes: 148 additions & 0 deletions
148
pylon-core/src/main/kotlin/io/github/pylonmc/pylon/core/block/base/PylonProcessor.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,148 @@ | ||
| package io.github.pylonmc.pylon.core.block.base | ||
|
|
||
| import io.github.pylonmc.pylon.core.datatypes.PylonSerializers | ||
| import io.github.pylonmc.pylon.core.event.PylonBlockDeserializeEvent | ||
| import io.github.pylonmc.pylon.core.event.PylonBlockLoadEvent | ||
| import io.github.pylonmc.pylon.core.event.PylonBlockSerializeEvent | ||
| import io.github.pylonmc.pylon.core.event.PylonBlockUnloadEvent | ||
| import io.github.pylonmc.pylon.core.util.gui.ProgressItem | ||
| import io.github.pylonmc.pylon.core.util.pylonKey | ||
| import org.bukkit.event.EventHandler | ||
| import org.bukkit.event.Listener | ||
| import org.jetbrains.annotations.ApiStatus | ||
| import java.util.IdentityHashMap | ||
|
|
||
| /** | ||
| * An interface that tracks progress of some kind of process, such as processing a | ||
| * recipe, burning a piece of fuel, enchanting an item, etc | ||
| * | ||
| * This interface overrides [PylonTickingBlock.tick], meaning the rate at which the | ||
| * block progresses is determined by [PylonTickingBlock.setTickInterval]. | ||
| */ | ||
| interface PylonProcessor { | ||
|
|
||
| @ApiStatus.Internal | ||
| data class ProcessorData( | ||
| var processTimeTicks: Int?, | ||
| var processTicksRemaining: Int?, | ||
| var progressItem: ProgressItem?, | ||
| ) | ||
| private val processorData: ProcessorData | ||
| get() = processorBlocks.getOrPut(this) { ProcessorData(null, null, null)} | ||
|
|
||
| val processTimeTicks: Int? | ||
| @ApiStatus.NonExtendable | ||
| get() = processorData.processTimeTicks | ||
|
|
||
| val processTicksRemaining: Int? | ||
| @ApiStatus.NonExtendable | ||
| get() = processorData.processTicksRemaining | ||
|
|
||
| val isProcessing: Boolean | ||
| @ApiStatus.NonExtendable | ||
| get() = processTimeTicks != null | ||
|
|
||
| /** | ||
| * Set the progress item that should be updated as the process progresses. Optional. | ||
| * | ||
| * Does not persist; you must call this whenever the block is initialised (e.g. | ||
| * in [io.github.pylonmc.pylon.core.block.PylonBlock.postInitialise]) | ||
| */ | ||
| fun setProgressItem(item: ProgressItem) { | ||
| processorData.progressItem = item | ||
| } | ||
|
|
||
| /** | ||
| * Starts a new process with duration [ticks], with [ticks] being the number of server | ||
| * ticks the process will take. | ||
| */ | ||
| fun startProcess(ticks: Int) { | ||
| processorData.processTimeTicks = ticks | ||
| processorData.processTicksRemaining = ticks | ||
| processorData.progressItem?.setTotalTimeTicks(ticks) | ||
| processorData.progressItem?.setRemainingTimeTicks(ticks) | ||
| } | ||
|
|
||
| fun stopProcess() { | ||
| val data = processorData | ||
| data.processTimeTicks = null | ||
| data.processTicksRemaining = null | ||
| data.progressItem?.totalTime = null | ||
| } | ||
|
|
||
| fun finishProcess() { | ||
| check(isProcessing) { | ||
| "Cannot finish process because there is no process ongoing" | ||
| } | ||
| onProcessFinished() | ||
| stopProcess() | ||
| } | ||
|
|
||
| fun onProcessFinished() {} | ||
|
|
||
| @ApiStatus.Internal | ||
| fun progressProcess(ticks: Int) { | ||
| val data = processorData | ||
| if (data.processTimeTicks == null) { | ||
| return | ||
| } | ||
|
|
||
| data.processTicksRemaining = data.processTicksRemaining!! - ticks | ||
| data.progressItem?.setRemainingTimeTicks(data.processTicksRemaining!!) | ||
| if (data.processTicksRemaining!! <= 0) { | ||
| finishProcess() | ||
| } | ||
| } | ||
|
|
||
| @ApiStatus.Internal | ||
| companion object : Listener { | ||
LordIdra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| private val processorKey = pylonKey("processor_data") | ||
|
|
||
| private val processorBlocks = IdentityHashMap<PylonProcessor, ProcessorData>() | ||
|
|
||
| @EventHandler | ||
| private fun onDeserialize(event: PylonBlockDeserializeEvent) { | ||
| val block = event.pylonBlock | ||
| if (block !is PylonProcessor) { | ||
| return | ||
| } | ||
|
|
||
| val data = event.pdc.get(processorKey, PylonSerializers.PROCESSOR_DATA) | ||
| ?: error("Processor data not found for ${block.key}") | ||
| processorBlocks[block] = data | ||
| } | ||
|
|
||
| @EventHandler | ||
| private fun onLoad(event: PylonBlockLoadEvent) { | ||
| // This separate listener is needed because when [PylonBlockDeserializeEvent] fires, then the | ||
| // block may not have been fully initialised yet (e.g. postInitialise may not have been called) | ||
| // which means progressItem may not have been set yet | ||
| val block = event.pylonBlock | ||
| if (block is PylonProcessor) { | ||
| val data = processorBlocks[block]!! | ||
| data.progressItem?.setTotalTimeTicks(data.processTimeTicks) | ||
| data.processTicksRemaining?.let { data.progressItem?.setRemainingTimeTicks(it) } | ||
| } | ||
| } | ||
|
|
||
| @EventHandler | ||
| private fun onSerialize(event: PylonBlockSerializeEvent) { | ||
| val block = event.pylonBlock | ||
| if (block is PylonProcessor) { | ||
| val data = processorBlocks[block] ?: error { | ||
| "No recipe processor data found for ${block.key}" | ||
| } | ||
| event.pdc.set(processorKey, PylonSerializers.PROCESSOR_DATA, data) | ||
| } | ||
| } | ||
|
|
||
| @EventHandler | ||
| private fun onUnload(event: PylonBlockUnloadEvent) { | ||
| val block = event.pylonBlock | ||
| if (block is PylonProcessor) { | ||
| processorBlocks.remove(block) | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.