-
Notifications
You must be signed in to change notification settings - Fork 17
Simple processor implementation #863
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
8 commits
Select commit
Hold shift + click to select a range
99b8eeb
processor changes
LordIdra 0922730
Merge branch 'master' into idra/processors
LordIdra fd0beb2
Merge branch 'master' into idra/processors
LordIdra face152
tweaks
LordIdra 73a12c0
finish boilers
LordIdra c71e31d
Merge branch 'master' into idra/processors
JustAHuman-xD b9ad1ff
add contract to item choice
JustAHuman-xD 1d5d582
Mark unfinished
Seggan 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
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
38 changes: 38 additions & 0 deletions
38
rebar/src/main/kotlin/io/github/pylonmc/rebar/datatypes/ProcessorPersistentDataType.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,38 @@ | ||
| package io.github.pylonmc.rebar.datatypes | ||
|
|
||
| import io.github.pylonmc.rebar.processor.RebarProcessor | ||
| import io.github.pylonmc.rebar.util.rebarKey | ||
| import io.github.pylonmc.rebar.util.setNullable | ||
| import org.bukkit.Bukkit | ||
| import org.bukkit.persistence.PersistentDataAdapterContext | ||
| import org.bukkit.persistence.PersistentDataContainer | ||
| import org.bukkit.persistence.PersistentDataType | ||
|
|
||
| object ProcessorPersistentDataType : PersistentDataType<PersistentDataContainer, RebarProcessor> { | ||
| val durationTicksKey = rebarKey("duration_ticks") | ||
| val remainingTicksKey = rebarKey("remaining_ticks") | ||
|
|
||
| override fun getPrimitiveType(): Class<PersistentDataContainer> = PersistentDataContainer::class.java | ||
|
|
||
| override fun getComplexType(): Class<RebarProcessor> = RebarProcessor::class.java | ||
|
|
||
| override fun fromPrimitive( | ||
| primitive: PersistentDataContainer, | ||
| context: PersistentDataAdapterContext | ||
| ): RebarProcessor { | ||
| val durationTicks = primitive.get(durationTicksKey, RebarSerializers.INTEGER) | ||
| val remainingTicks = primitive.get(remainingTicksKey, RebarSerializers.INTEGER) | ||
| val processor = RebarProcessor() | ||
| if (durationTicks != null && remainingTicks != null) { | ||
| processor.process = RebarProcessor.RebarProcess(durationTicks, remainingTicks) | ||
| } | ||
| return processor | ||
| } | ||
|
|
||
| override fun toPrimitive(complex: RebarProcessor, context: PersistentDataAdapterContext): PersistentDataContainer { | ||
| val pdc = context.newPersistentDataContainer() | ||
| pdc.setNullable(durationTicksKey, RebarSerializers.INTEGER, complex.durationTicks) | ||
| pdc.setNullable(remainingTicksKey, RebarSerializers.INTEGER, complex.remainingTicks) | ||
| return pdc | ||
| } | ||
| } |
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
82 changes: 82 additions & 0 deletions
82
rebar/src/main/kotlin/io/github/pylonmc/rebar/processor/RebarProcessor.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,82 @@ | ||
| package io.github.pylonmc.rebar.processor | ||
|
|
||
| import org.jetbrains.annotations.ApiStatus | ||
|
|
||
| /** | ||
| * Unfinished, do not use | ||
| */ | ||
| @ApiStatus.Experimental | ||
| class RebarProcessor { | ||
|
|
||
| internal class RebarProcess( | ||
| var durationTicks: Int, | ||
| var remainingTicks: Int | ||
| ) | ||
|
|
||
| @JvmSynthetic | ||
| internal var process: RebarProcess? = null | ||
|
|
||
| private var onStart: Runnable? = null | ||
| private var onCancel: Runnable? = null | ||
| private var onFinish: Runnable? = null | ||
|
|
||
| val isRunning | ||
| get() = process != null | ||
|
|
||
| val durationTicks | ||
| get() = process?.durationTicks | ||
|
|
||
| val durationSeconds | ||
| get() = durationTicks?.let { it / 20.0 } | ||
|
|
||
| val elapsedTicks | ||
| get() = process?.let { it.durationTicks - it.remainingTicks } | ||
|
|
||
| val elapsedSeconds | ||
| get() = elapsedTicks?.let { it / 20.0 } | ||
|
|
||
| /** | ||
| * The proportion from 0-1 of the process that has been completed so far | ||
| */ | ||
| val elapsedProportion | ||
| get() = remainingProportion?.let { 1.0 - it } | ||
|
|
||
| val remainingTicks | ||
| get() = process?.remainingTicks | ||
|
|
||
| val remainingSeconds | ||
| get() = remainingTicks?.let { it / 20.0 } | ||
|
|
||
| /** | ||
| * The proportion from 0-1 of the process that is left to go | ||
| */ | ||
| val remainingProportion | ||
| get() = process?.let { it.remainingTicks.toDouble() / it.durationTicks } | ||
|
|
||
| fun onStart(runnable: Runnable) { onStart = runnable } | ||
| fun onCancel(runnable: Runnable) { onCancel = runnable } | ||
| fun onFinish(runnable: Runnable) { onFinish = runnable } | ||
|
|
||
| fun start(durationTicks: Int) { | ||
| process = RebarProcess(durationTicks, durationTicks) | ||
| onStart?.run() | ||
| } | ||
|
|
||
| fun cancel() { | ||
| process = null | ||
| onCancel?.run() | ||
| } | ||
|
|
||
| fun tick(ticks: Int) { | ||
| if (process == null) { | ||
| return | ||
| } | ||
|
|
||
| process!!.remainingTicks -= ticks | ||
|
|
||
| if (process!!.remainingTicks <= 0) { | ||
| process = null | ||
| onFinish?.run() | ||
| } | ||
| } | ||
| } | ||
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
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.