replace java enum set with kutil enum set if possible

Its faster, more flexible and uses less memory
This commit is contained in:
Moritz Zwerger 2023-11-13 15:48:15 +01:00
parent a34e7c907b
commit 565c79c677
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
10 changed files with 27 additions and 17 deletions

View File

@ -89,7 +89,7 @@ abstract class PlayerEntity(
return field
}
val skinParts: MutableSet<SkinParts> by observedSet(mutableSetOf())
val skinParts: MutableSet<SkinParts> by observedSet(SkinParts.set())
override val isNameVisible get() = true
override val name: ChatComponent? get() = additional.tabDisplayName // minecraft does use the plain name

View File

@ -22,7 +22,6 @@ import de.bixilon.minosoft.data.registries.blocks.types.Block
import de.bixilon.minosoft.data.registries.blocks.types.building.dirt.SnowyBlock
import de.bixilon.minosoft.data.registries.blocks.types.building.snow.SnowLayerBlock
import de.bixilon.minosoft.data.registries.blocks.types.fluid.FluidBlock
import java.util.*
@Deprecated("Fallback data")
object BlockProperties {
@ -36,7 +35,7 @@ object BlockProperties {
val WATERLOGGED = BooleanProperty("waterlogged").register()
val STAIR_DIRECTIONAL = EnumProperty("shape", Shapes).register()
val SLAB_HALF = EnumProperty("half", Halves).register()
val STAIR_HALF = EnumProperty("half", Halves, EnumSet.of(Halves.UPPER, Halves.LOWER))
val STAIR_HALF = EnumProperty("half", Halves, Halves.set(Halves.UPPER, Halves.LOWER))
val SLAB_TYPE = EnumProperty("type", Halves).register()
val FLUID_LEVEL = FluidBlock.LEVEL.register()
val MOISTURE_LEVEL = IntProperty("moisture").register()
@ -119,7 +118,7 @@ object BlockProperties {
val AXIS = EnumProperty("axis", Axes).register()
val FACING = EnumProperty("facing", Directions).register()
val FACING_HORIZONTAL = EnumProperty("facing", Directions, EnumSet.of(Directions.NORTH, Directions.SOUTH, Directions.WEST, Directions.EAST))
val FACING_HORIZONTAL = EnumProperty("facing", Directions, Directions.set(Directions.NORTH, Directions.SOUTH, Directions.WEST, Directions.EAST))
val ROTATION = IntProperty("rotation").register()
val ORIENTATION = EnumProperty("orientation", Orientations).register()

View File

@ -13,12 +13,13 @@
package de.bixilon.minosoft.data.registries.blocks.properties
import de.bixilon.kutil.enums.BitEnumSet
import de.bixilon.kutil.enums.ValuesEnum
class EnumProperty<T : Enum<*>>(
name: String,
val values: ValuesEnum<T>,
val allowed: Set<T>? = null,
val allowed: BitEnumSet<T>? = null,
) : BlockProperty<T>(name) {
override fun parse(value: Any): T {

View File

@ -65,7 +65,6 @@ import de.bixilon.minosoft.gui.rendering.util.VecUtil.plus
import de.bixilon.minosoft.input.interaction.InteractionResults
import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection
import de.bixilon.minosoft.protocol.versions.Version
import java.util.*
abstract class DoorBlock(identifier: ResourceLocation, settings: BlockSettings) : Block(identifier, settings), BlockWithItem<Item>, ModelChooser, DoubleSizeBlock, InteractBlockHandler, OutlinedBlock, CollidableBlock, BlockStateBuilder, LightedBlock {
override val item: Item = this::item.inject(identifier)
@ -168,7 +167,7 @@ abstract class DoorBlock(identifier: ResourceLocation, settings: BlockSettings)
}
companion object {
val HALF = EnumProperty("half", Halves, EnumSet.of(Halves.UPPER, Halves.LOWER))
val HALF = EnumProperty("half", Halves, Halves.set(Halves.UPPER, Halves.LOWER))
val HINGE = EnumProperty("hinge", Sides)
val POWERED = BlockProperties.POWERED
val FACING = BlockProperties.FACING_HORIZONTAL

View File

@ -94,7 +94,7 @@ abstract class DoublePlant(identifier: ResourceLocation, settings: BlockSettings
}
companion object {
val HALF = EnumProperty("half", Halves, setOf(Halves.UPPER, Halves.LOWER))
val HALF = EnumProperty("half", Halves, Halves.set(Halves.UPPER, Halves.LOWER))
}
open class Sunflower(identifier: ResourceLocation = Companion.identifier, settings: BlockSettings) : DoublePlant(identifier, settings) {

View File

@ -1,6 +1,6 @@
/*
* Minosoft
* Copyright (C) 2020-2022 Moritz Zwerger
* Copyright (C) 2020-2023 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
@ -13,10 +13,18 @@
package de.bixilon.minosoft.gui.rendering.system.base
import de.bixilon.kutil.enums.ValuesEnum
import de.bixilon.kutil.enums.ValuesEnum.Companion.names
enum class RenderingCapabilities {
DEPTH_TEST,
BLENDING,
FACE_CULLING,
POLYGON_OFFSET,
;
companion object : ValuesEnum<RenderingCapabilities> {
override val VALUES = values()
override val NAME_MAP: Map<String, RenderingCapabilities> = names()
}
}

View File

@ -46,7 +46,6 @@ import org.lwjgl.opengl.GL43.GL_DEBUG_OUTPUT
import org.lwjgl.opengl.GL43.glDebugMessageCallback
import java.nio.ByteBuffer
import java.nio.FloatBuffer
import java.util.*
class OpenGLRenderSystem(
private val context: RenderContext,
@ -54,7 +53,7 @@ class OpenGLRenderSystem(
private var thread: Thread? = null
override val nativeShaders: MutableSet<NativeShader> = mutableSetOf()
override val shaders: MutableSet<Shader> = mutableSetOf()
private val capabilities: EnumSet<RenderingCapabilities> = EnumSet.noneOf(RenderingCapabilities::class.java)
private val capabilities: MutableSet<RenderingCapabilities> = RenderingCapabilities.set()
override lateinit var vendor: OpenGLVendor
private set
override var active: Boolean = false

View File

@ -35,7 +35,7 @@ class TabListS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
init {
val actions = if (buffer.versionId < ProtocolVersions.V_22W42A) LegacyActions[buffer.readVarInt()].actions else buffer.readEnumSet(Actions.VALUES_22W42A).actions()
val actions = if (buffer.versionId < ProtocolVersions.V_22W42A) LegacyActions[buffer.readVarInt()].actions else buffer.readEnumSet(Actions, Actions.VALUES_22W42A).actions()
val entries: MutableMap<UUID, AdditionalDataUpdate?> = mutableMapOf()
for (index in 0 until buffer.readVarInt()) {

View File

@ -1,6 +1,6 @@
/*
* Minosoft
* Copyright (C) 2020-2022 Moritz Zwerger
* Copyright (C) 2020-2023 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
@ -13,6 +13,9 @@
package de.bixilon.minosoft.protocol.packets.s2c.play.tab.actions
import de.bixilon.kutil.enums.ValuesEnum
import de.bixilon.kutil.enums.ValuesEnum.Companion.names
enum class Actions(val action: AbstractAction) {
INITIALIZE(InitializeAction),
CHAT(ChatAction),
@ -22,7 +25,9 @@ enum class Actions(val action: AbstractAction) {
DISPLAY_NAME(DisplayNameAction),
;
companion object {
companion object : ValuesEnum<Actions> {
override val VALUES = values()
override val NAME_MAP = names()
val VALUES_22W42A = arrayOf(INITIALIZE, CHAT, GAMEMODE, LISTED, LATENCY, DISPLAY_NAME)
}

View File

@ -328,13 +328,12 @@ class PlayInByteBuffer : InByteBuffer {
}
}
@Deprecated("use values enum")
inline fun <reified T : Enum<T>> readEnumSet(values: Array<T>): Set<T> {
inline fun <reified T : Enum<T>> readEnumSet(universe: ValuesEnum<T>, values: Array<T>): Set<T> {
val bitset = readBitSet(values.size)
if (bitset.isEmpty) {
return emptySet()
}
val set = EnumSet.noneOf(T::class.java)
val set = universe.set()
readEnumSet(bitset, set, values)
return set
}