-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
49 changed files
with
2,940 additions
and
0 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
nms-utilities/src/main/kotlin/xyz/xenondevs/nmsutils/FactoryFunctions.kt
This file contains 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,16 @@ | ||
package xyz.xenondevs.nmsutils | ||
|
||
import net.minecraft.core.NonNullList | ||
|
||
fun <E> NonNullList(list: List<E>, default: E? = null): NonNullList<E> { | ||
val nonNullList: NonNullList<E> | ||
if (default == null) { | ||
nonNullList = NonNullList.createWithCapacity(list.size) | ||
nonNullList.addAll(list) | ||
} else { | ||
nonNullList = NonNullList.withSize(list.size, default) | ||
list.forEachIndexed { index, e -> nonNullList[index] = e } | ||
} | ||
|
||
return nonNullList | ||
} |
22 changes: 22 additions & 0 deletions
22
nms-utilities/src/main/kotlin/xyz/xenondevs/nmsutils/NMSUtilities.kt
This file contains 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,22 @@ | ||
package xyz.xenondevs.nmsutils | ||
|
||
import org.bukkit.plugin.Plugin | ||
import xyz.xenondevs.nmsutils.network.PacketManager | ||
|
||
internal lateinit var PLUGIN: Plugin | ||
private set | ||
|
||
internal val LOGGER by lazy { PLUGIN.logger } | ||
|
||
object NMSUtilities { | ||
|
||
fun init(plugin: Plugin) { | ||
PLUGIN = plugin | ||
PacketManager.init() | ||
} | ||
|
||
fun disable() { | ||
PacketManager.disable() | ||
} | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
nms-utilities/src/main/kotlin/xyz/xenondevs/nmsutils/advancement/AdvancementLoader.kt
This file contains 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,47 @@ | ||
package xyz.xenondevs.nmsutils.advancement | ||
|
||
import net.minecraft.advancements.AdvancementHolder | ||
import net.minecraft.advancements.AdvancementTree | ||
import net.minecraft.advancements.TreeNodePosition | ||
import org.spigotmc.SpigotConfig | ||
import xyz.xenondevs.nmsutils.internal.util.DEDICATED_SERVER | ||
import xyz.xenondevs.nmsutils.internal.util.ReflectionRegistry | ||
|
||
object AdvancementLoader { | ||
|
||
fun registerAdvancements(vararg advancements: AdvancementHolder, ignoreFilters: Boolean = false) = | ||
registerAdvancements(advancements.asList(), ignoreFilters) | ||
|
||
fun registerAdvancements(advancements: Iterable<AdvancementHolder>, ignoreFilters: Boolean) { | ||
// filter advancements | ||
var filtered: List<AdvancementHolder> = advancements.toList() | ||
if (!ignoreFilters) { | ||
if (SpigotConfig.disableAdvancementSaving || SpigotConfig.disabledAdvancements?.contains("*") == true) | ||
return | ||
|
||
val disabledAdvancements = SpigotConfig.disabledAdvancements | ||
if (disabledAdvancements != null) | ||
filtered = advancements.filterNot { | ||
it.id.toString() in disabledAdvancements || it.id.namespace in disabledAdvancements | ||
} | ||
} | ||
|
||
// combine with existing advancements and build tree | ||
val advancementManager = DEDICATED_SERVER.advancements | ||
val allAdvancements = HashMap(DEDICATED_SERVER.advancements.advancements) | ||
filtered.forEach { allAdvancements[it.id] = it } | ||
val advancementTree = ReflectionRegistry.SERVER_ADVANCEMENT_MANAGER_TREE_FIELD.get(advancementManager) as AdvancementTree? ?: AdvancementTree() | ||
advancementTree.addAll(filtered) | ||
for (root in advancementTree.roots()) { | ||
if (root.holder().value().display().isPresent) { | ||
// TODO: open positioning up to the api | ||
TreeNodePosition.run(root) | ||
} | ||
} | ||
|
||
// set new advancements | ||
advancementManager.advancements = allAdvancements | ||
ReflectionRegistry.SERVER_ADVANCEMENT_MANAGER_TREE_FIELD.set(advancementManager, advancementTree) | ||
} | ||
|
||
} |
237 changes: 237 additions & 0 deletions
237
nms-utilities/src/main/kotlin/xyz/xenondevs/nmsutils/bossbar/BossBar.kt
This file contains 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,237 @@ | ||
package xyz.xenondevs.nmsutils.bossbar | ||
|
||
import net.kyori.adventure.text.Component | ||
import net.md_5.bungee.api.chat.BaseComponent | ||
import net.minecraft.network.protocol.game.ClientboundBossEventPacket | ||
import net.minecraft.world.BossEvent | ||
import xyz.xenondevs.nmsutils.bossbar.operation.AddBossBarOperation | ||
import xyz.xenondevs.nmsutils.bossbar.operation.RemoveBossBarOperation | ||
import xyz.xenondevs.nmsutils.bossbar.operation.UpdateNameBossBarOperation | ||
import xyz.xenondevs.nmsutils.bossbar.operation.UpdateProgressBossBarOperation | ||
import xyz.xenondevs.nmsutils.bossbar.operation.UpdatePropertiesBossBarOperation | ||
import xyz.xenondevs.nmsutils.bossbar.operation.UpdateStyleBossBarOperation | ||
import xyz.xenondevs.nmsutils.internal.util.toAdventureComponent | ||
import xyz.xenondevs.nmsutils.internal.util.toBaseComponentArray | ||
import xyz.xenondevs.nmsutils.internal.util.toNmsComponent | ||
import xyz.xenondevs.nmsutils.network.ClientboundBossEventPacket | ||
import java.util.* | ||
import net.minecraft.network.chat.Component as MojangComponent | ||
|
||
/** | ||
* A boss bar class with cached operations and packets | ||
*/ | ||
class BossBar( | ||
val id: UUID, | ||
name: MojangComponent = MojangComponent.literal(""), | ||
progress: Float = 0.0f, | ||
color: BossEvent.BossBarColor = BossEvent.BossBarColor.WHITE, | ||
overlay: BossEvent.BossBarOverlay = BossEvent.BossBarOverlay.PROGRESS, | ||
darkenScreen: Boolean = false, | ||
playMusic: Boolean = false, | ||
createWorldFog: Boolean = false | ||
) { | ||
|
||
var name: MojangComponent = name | ||
set(value) { | ||
field = value | ||
|
||
_addOperation = null | ||
_updateNameOperation = null | ||
|
||
_addPacket = null | ||
_updateNamePacket = null | ||
} | ||
var progress: Float = progress | ||
set(value) { | ||
field = value | ||
|
||
_addOperation = null | ||
_updateProgressOperation = null | ||
|
||
_addPacket = null | ||
_updateProgressPacket = null | ||
} | ||
var color: BossEvent.BossBarColor = color | ||
set(value) { | ||
field = value | ||
|
||
_addOperation = null | ||
_updateStyleOperation = null | ||
|
||
_addPacket = null | ||
_updateStylePacket = null | ||
} | ||
var overlay: BossEvent.BossBarOverlay = overlay | ||
set(value) { | ||
field = value | ||
|
||
_addOperation = null | ||
_updateStyleOperation = null | ||
|
||
_addPacket = null | ||
_updateStylePacket = null | ||
} | ||
var darkenScreen: Boolean = darkenScreen | ||
set(value) { | ||
field = value | ||
|
||
_addOperation = null | ||
_updatePropertiesOperation = null | ||
|
||
_addPacket = null | ||
_updatePropertiesPacket = null | ||
} | ||
var playMusic: Boolean = playMusic | ||
set(value) { | ||
field = value | ||
|
||
_addOperation = null | ||
_updatePropertiesOperation = null | ||
|
||
_addPacket = null | ||
_updatePropertiesPacket = null | ||
} | ||
var createWorldFog: Boolean = createWorldFog | ||
set(value) { | ||
field = value | ||
|
||
_addOperation = null | ||
_updatePropertiesOperation = null | ||
|
||
_addPacket = null | ||
_updatePropertiesPacket = null | ||
} | ||
|
||
var bungeeName: Array<out BaseComponent> | ||
get() = name.toBaseComponentArray() | ||
set(value) { | ||
name = value.toNmsComponent() | ||
} | ||
|
||
var adventureName: Component | ||
get() = name.toAdventureComponent() | ||
set(value) { | ||
name = value.toNmsComponent() | ||
} | ||
|
||
private var _addOperation: AddBossBarOperation? = null | ||
val addOperation: AddBossBarOperation | ||
get() { | ||
if (_addOperation == null) { | ||
_addOperation = AddBossBarOperation(this.name, progress, color, overlay, darkenScreen, playMusic, createWorldFog) | ||
} | ||
return _addOperation!! | ||
} | ||
|
||
private var _updateNameOperation: UpdateNameBossBarOperation? = null | ||
val updateNameOperation: UpdateNameBossBarOperation | ||
get() { | ||
if (_updateNameOperation == null) { | ||
_updateNameOperation = UpdateNameBossBarOperation(this.name) | ||
} | ||
return _updateNameOperation!! | ||
} | ||
|
||
private var _updateProgressOperation: UpdateProgressBossBarOperation? = null | ||
val updateProgressOperation: UpdateProgressBossBarOperation | ||
get() { | ||
if (_updateProgressOperation == null) { | ||
_updateProgressOperation = UpdateProgressBossBarOperation(progress) | ||
} | ||
return _updateProgressOperation!! | ||
} | ||
|
||
private var _updateStyleOperation: UpdateStyleBossBarOperation? = null | ||
val updateStyleOperation: UpdateStyleBossBarOperation | ||
get() { | ||
if (_updateStyleOperation == null) { | ||
_updateStyleOperation = UpdateStyleBossBarOperation(color, overlay) | ||
} | ||
return _updateStyleOperation!! | ||
} | ||
|
||
private var _updatePropertiesOperation: UpdatePropertiesBossBarOperation? = null | ||
val updatePropertiesOperation: UpdatePropertiesBossBarOperation | ||
get() { | ||
if (_updatePropertiesOperation == null) { | ||
_updatePropertiesOperation = UpdatePropertiesBossBarOperation(darkenScreen, playMusic, createWorldFog) | ||
} | ||
return _updatePropertiesOperation!! | ||
} | ||
|
||
val removeOperation = RemoveBossBarOperation | ||
|
||
private var _addPacket: ClientboundBossEventPacket? = null | ||
val addPacket: ClientboundBossEventPacket | ||
get() { | ||
if (_addPacket == null) { | ||
_addPacket = ClientboundBossEventPacket(id, addOperation) | ||
} | ||
return _addPacket!! | ||
} | ||
|
||
private var _updateNamePacket: ClientboundBossEventPacket? = null | ||
val updateNamePacket: ClientboundBossEventPacket | ||
get() { | ||
if (_updateNamePacket == null) { | ||
_updateNamePacket = ClientboundBossEventPacket(id, updateNameOperation) | ||
} | ||
return _updateNamePacket!! | ||
} | ||
|
||
private var _updateProgressPacket: ClientboundBossEventPacket? = null | ||
val updateProgressPacket: ClientboundBossEventPacket | ||
get() { | ||
if (_updateProgressPacket == null) { | ||
_updateProgressPacket = ClientboundBossEventPacket(id, updateProgressOperation) | ||
} | ||
return _updateProgressPacket!! | ||
} | ||
|
||
private var _updateStylePacket: ClientboundBossEventPacket? = null | ||
val updateStylePacket: ClientboundBossEventPacket | ||
get() { | ||
if (_updateStylePacket == null) { | ||
_updateStylePacket = ClientboundBossEventPacket(id, updateStyleOperation) | ||
} | ||
return _updateStylePacket!! | ||
} | ||
|
||
private var _updatePropertiesPacket: ClientboundBossEventPacket? = null | ||
val updatePropertiesPacket: ClientboundBossEventPacket | ||
get() { | ||
if (_updatePropertiesPacket == null) { | ||
_updatePropertiesPacket = ClientboundBossEventPacket(id, updatePropertiesOperation) | ||
} | ||
return _updatePropertiesPacket!! | ||
} | ||
|
||
val removePacket = ClientboundBossEventPacket(id, removeOperation) | ||
|
||
constructor( | ||
id: UUID, | ||
name: Array<out BaseComponent>, | ||
progress: Float = 0.0f, | ||
color: BossEvent.BossBarColor = BossEvent.BossBarColor.WHITE, | ||
overlay: BossEvent.BossBarOverlay = BossEvent.BossBarOverlay.PROGRESS, | ||
darkenScreen: Boolean = false, | ||
playMusic: Boolean = false, | ||
createWorldFog: Boolean = false | ||
) : this(id, name.toNmsComponent(), progress, color, overlay, darkenScreen, playMusic, createWorldFog) | ||
|
||
companion object { | ||
|
||
fun of(id: UUID, operation: AddBossBarOperation) = BossBar( | ||
id, | ||
operation.name, | ||
operation.progress, | ||
operation.color, | ||
operation.overlay, | ||
operation.darkenScreen, | ||
operation.playMusic, | ||
operation.createWorldFog | ||
) | ||
|
||
} | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
...utilities/src/main/kotlin/xyz/xenondevs/nmsutils/bossbar/operation/AddBossBarOperation.kt
This file contains 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,46 @@ | ||
package xyz.xenondevs.nmsutils.bossbar.operation | ||
|
||
import io.netty.buffer.Unpooled | ||
import net.minecraft.network.FriendlyByteBuf | ||
import net.minecraft.network.chat.Component | ||
import net.minecraft.world.BossEvent | ||
import xyz.xenondevs.nmsutils.internal.util.ReflectionRegistry | ||
|
||
class AddBossBarOperation( | ||
val name: Component, | ||
val progress: Float, | ||
val color: BossEvent.BossBarColor, | ||
val overlay: BossEvent.BossBarOverlay, | ||
val darkenScreen: Boolean, | ||
val playMusic: Boolean, | ||
val createWorldFog: Boolean | ||
) : BossBarOperation() { | ||
|
||
override fun toNMS(): Any { | ||
val buf = FriendlyByteBuf(Unpooled.buffer()) | ||
buf.writeComponent(name) | ||
buf.writeFloat(progress) | ||
buf.writeEnum(color) | ||
buf.writeEnum(overlay) | ||
buf.writeByte(encodeProperties(darkenScreen, playMusic, createWorldFog)) | ||
|
||
return ReflectionRegistry.BOSS_BAR_ADD_OPERATION_CONSTRUCTOR.newInstance(buf) | ||
} | ||
|
||
companion object : Type<AddBossBarOperation> { | ||
|
||
override fun fromNMS(operation: Any): AddBossBarOperation { | ||
return AddBossBarOperation( | ||
ReflectionRegistry.BOSS_BAR_ADD_OPERATION_NAME_FIELD.get(operation) as Component, | ||
ReflectionRegistry.BOSS_BAR_ADD_OPERATION_PROGRESS_FIELD.get(operation) as Float, | ||
ReflectionRegistry.BOSS_BAR_ADD_OPERATION_COLOR_FIELD.get(operation) as BossEvent.BossBarColor, | ||
ReflectionRegistry.BOSS_BAR_ADD_OPERATION_OVERLAY_FIELD.get(operation) as BossEvent.BossBarOverlay, | ||
ReflectionRegistry.BOSS_BAR_ADD_OPERATION_DARKEN_SCREEN_FIELD.get(operation) as Boolean, | ||
ReflectionRegistry.BOSS_BAR_ADD_OPERATION_PLAY_MUSIC_FIELD.get(operation) as Boolean, | ||
ReflectionRegistry.BOSS_BAR_ADD_OPERATION_CREATE_WORLD_FOG_FIELD.get(operation) as Boolean | ||
) | ||
} | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.