Skip to content

Commit

Permalink
Supported to template 'path_find_through'.
Browse files Browse the repository at this point in the history
Modified entity and block settings code style.
  • Loading branch information
cao-awa committed Nov 20, 2024
1 parent 8ecd03d commit df18182
Show file tree
Hide file tree
Showing 15 changed files with 316 additions and 87 deletions.
5 changes: 5 additions & 0 deletions sample/datapacks/tests/data/awa/block/conium_block.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
5,
5
]
},
"path_find_through": {
"land": true,
"water": true,
"air": true
}
}
}
20 changes: 13 additions & 7 deletions src/main/java/com/github/cao/awa/conium/block/ConiumBlock.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package com.github.cao.awa.conium.block

import com.github.cao.awa.conium.block.builder.ConiumBlockBuilder
import com.github.cao.awa.conium.block.setting.ConiumBlockSettings
import net.minecraft.block.Block
import net.minecraft.block.BlockState
import net.minecraft.block.ShapeContext
import net.minecraft.client.color.block.BlockColorProvider
import net.minecraft.entity.ai.pathing.NavigationType
import net.minecraft.util.math.BlockPos
import net.minecraft.util.shape.VoxelShape
import net.minecraft.util.shape.VoxelShapes
import net.minecraft.world.BlockRenderView
import net.minecraft.world.BlockView

class ConiumBlock(settings: Settings) : Block(settings), BlockColorProvider {
class ConiumBlock(private val setting: ConiumBlockSettings) : Block(setting.vanillaSettings), BlockColorProvider {
companion object {
fun create(builder: ConiumBlockBuilder, settings: Settings): ConiumBlock {
fun create(builder: ConiumBlockBuilder, settings: ConiumBlockSettings): ConiumBlock {
builder.templates.forEach {
it.prepare(settings)
}
Expand All @@ -32,10 +33,15 @@ class ConiumBlock(settings: Settings) : Block(settings), BlockColorProvider {
}
}

var color: Int = 0
var outlineShape: VoxelShape = VoxelShapes.fullCube()
override fun getColor(state: BlockState?, world: BlockRenderView?, pos: BlockPos?, tintIndex: Int): Int = this.setting.color

override fun getColor(state: BlockState?, world: BlockRenderView?, pos: BlockPos?, tintIndex: Int): Int = this.color
override fun getOutlineShape(state: BlockState, world: BlockView, pos: BlockPos, context: ShapeContext): VoxelShape = this.setting.outlineShape

override fun getOutlineShape(state: BlockState, world: BlockView, pos: BlockPos, context: ShapeContext): VoxelShape = this.outlineShape
override fun canPathfindThrough(state: BlockState, type: NavigationType): Boolean {
return when (type) {
NavigationType.LAND -> this.setting.landPathThrough
NavigationType.WATER -> this.setting.waterPathThrough
NavigationType.AIR -> this.setting.airPathThrough
}
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.github.cao.awa.conium.block.builder

import com.github.cao.awa.conium.block.ConiumBlock
import com.github.cao.awa.conium.block.setting.ConiumBlockSettings
import com.github.cao.awa.conium.block.template.ConiumBlockTemplate
import com.github.cao.awa.conium.template.ConiumBuilderWithTemplates
import net.minecraft.block.AbstractBlock
import net.minecraft.block.Block
import net.minecraft.util.Identifier

abstract class ConiumBlockBuilder(val identifier: Identifier) : ConiumBuilderWithTemplates<
ConiumBlockBuilder,
AbstractBlock.Settings,
ConiumBlockSettings,
Block,
ConiumBlockTemplate>(
ConiumBlock::create
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package com.github.cao.awa.conium.block.setting

import com.github.cao.awa.conium.block.template.ConiumBlockTemplate
import com.github.cao.awa.conium.setting.ConiumSettings
import net.minecraft.block.AbstractBlock
import net.minecraft.block.AbstractBlock.Settings
import net.minecraft.client.color.block.BlockColorProvider
import net.minecraft.util.shape.VoxelShape
import net.minecraft.util.shape.VoxelShapes
import net.minecraft.world.RaycastContext

object ConiumBlockSettingsValue {
val color: Int = 0
val outlineShape: VoxelShape = VoxelShapes.fullCube()
val landPathThrough: Boolean = false
val waterPathThrough: Boolean = false
val airPathThrough: Boolean = false
}

class ConiumBlockSettings(val vanillaSettings: Settings) : ConiumSettings<ConiumBlockSettings>() {
companion object {
@JvmStatic
fun create(templates: MutableList<ConiumBlockTemplate>, settings: Settings): ConiumBlockSettings {
return ConiumBlockSettings(settings).also {
templates.forEach { template ->
template.prepare(it)
}
}
}
}

/**
* Setting the color code of block.
*
* Default is 0.
*
* @see BlockColorProvider.getColor
*
* @author cao_awa
*
* @since 1.0.0
*/
var color: Int
get() = this._color ?: ConiumBlockSettingsValue.color
set(value) {
this._color = value

}

// The delegate.
private var _color: Int? = null

/**
* Setting the outline shape of block.
*
* Default is ``fullCube``.
*
* @see AbstractBlock.getOutlineShape
* @see AbstractBlock.AbstractBlockState.getOutlineShape
* @see RaycastContext.ShapeType.OUTLINE
*
* @author cao_awa
*
* @since 1.0.0
*/
var outlineShape: VoxelShape
get() = this._outlineShape ?: ConiumBlockSettingsValue.outlineShape
set(value) {
this._outlineShape = value
}

// The delegate.
private var _outlineShape: VoxelShape? = null

/**
* Setting the land path through-able of block.
*
* Default is ``false``.
*
* @see AbstractBlock.canPathfindThrough
*
* @author cao_awa
*
* @since 1.0.0
*/
var landPathThrough: Boolean
get() = this._landPathThrough ?: ConiumBlockSettingsValue.landPathThrough
set(value) {
this._landPathThrough = value
}

// The delegate.
private var _landPathThrough: Boolean? = null

/**
* Setting the water path through-able of block.
*
* Default is ``false``.
*
* @see AbstractBlock.canPathfindThrough
*
* @author cao_awa
*
* @since 1.0.0
*/
var waterPathThrough: Boolean
get() = this._waterPathThrough ?: ConiumBlockSettingsValue.waterPathThrough
set(value) {
this._waterPathThrough = value
}

// The delegate.
private var _waterPathThrough: Boolean? = null

/**
* Setting the air path through-able of block.
*
* Default is ``false``.
*
* @see AbstractBlock.canPathfindThrough
*
* @author cao_awa
*
* @since 1.0.0
*/
var airPathThrough: Boolean
get() = this._airPathThrough ?: ConiumBlockSettingsValue.airPathThrough
set(value) {
this._airPathThrough = value
}

// The delegate.
private var _airPathThrough: Boolean? = null

override fun migrateTo(settings: ConiumBlockSettings): ConiumBlockSettings {
return settings.also {
// Apply settings(only configured, no default).
}
}

override fun newInstance(): ConiumBlockSettings = ConiumBlockSettings(this.vanillaSettings)
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.github.cao.awa.conium.block.template

import com.github.cao.awa.conium.block.ConiumBlock
import com.github.cao.awa.conium.block.setting.ConiumBlockSettings
import com.github.cao.awa.conium.template.ConiumTemplate
import net.minecraft.block.AbstractBlock

abstract class ConiumBlockTemplate(name: String) : ConiumTemplate<ConiumBlock, AbstractBlock.Settings>(name) {
abstract class ConiumBlockTemplate(name: String) : ConiumTemplate<ConiumBlock, ConiumBlockSettings>(name) {
override fun attach(target: ConiumBlock) {
// Do nothing.
}
Expand All @@ -13,7 +14,8 @@ abstract class ConiumBlockTemplate(name: String) : ConiumTemplate<ConiumBlock, A
// Do nothing.
}

override fun prepare(target: AbstractBlock.Settings) {
override fun prepare(target: ConiumBlockSettings) {
settings(target.vanillaSettings)
settings(target)
}

Expand All @@ -22,4 +24,10 @@ abstract class ConiumBlockTemplate(name: String) : ConiumTemplate<ConiumBlock, A
open fun settings(settings: AbstractBlock.Settings) {
// Do nothing.
}

// Do not call settings directly.
// Use 'prepare'.
open fun settings(settings: ConiumBlockSettings) {
// Do nothing.
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.github.cao.awa.conium.block.template.collision

import com.github.cao.awa.conium.block.ConiumBlock
import com.github.cao.awa.conium.block.setting.ConiumBlockSettings
import com.github.cao.awa.conium.block.template.ConiumBlockTemplate
import com.github.cao.awa.conium.kotlin.extent.json.eachInt
import com.github.cao.awa.conium.kotlin.extent.json.objectOrBoolean
Expand Down Expand Up @@ -65,8 +65,14 @@ class ConiumBlockCollisionTemplate(
}
}

override fun complete(target: ConiumBlock) {
target.outlineShape = VoxelShapes.cuboid(
override fun settings(settings: AbstractBlock.Settings) {
if (this.noCollision) {
settings.noCollision()
}
}

override fun settings(settings: ConiumBlockSettings) {
settings.outlineShape = VoxelShapes.cuboid(
this.px1 / 16.0,
this.py1 / 16.0,
this.pz1 / 16.0,
Expand All @@ -75,10 +81,4 @@ class ConiumBlockCollisionTemplate(
this.pz2 / 16.0,
)
}

override fun settings(settings: AbstractBlock.Settings) {
if (this.noCollision) {
settings.noCollision()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.github.cao.awa.conium.block.template.path.through

import com.github.cao.awa.conium.block.setting.ConiumBlockSettings
import com.github.cao.awa.conium.block.template.ConiumBlockTemplate
import com.github.cao.awa.conium.kotlin.extent.json.ifBoolean
import com.github.cao.awa.conium.kotlin.extent.json.ifJsonObject
import com.github.cao.awa.conium.template.ConiumTemplates.Block.PATH_FIND_THROUGH
import com.google.gson.JsonElement
import net.minecraft.registry.RegistryWrapper.WrapperLookup

class ConiumBlockPathFindThroughTemplate(
private val landPathThrough: Boolean,
private val waterPathThrough: Boolean,
private val airPathThrough: Boolean
) : ConiumBlockTemplate(PATH_FIND_THROUGH) {
companion object {
@JvmStatic
fun create(element: JsonElement, registryLookup: WrapperLookup): ConiumBlockPathFindThroughTemplate {
return element.ifJsonObject({
ConiumBlockPathFindThroughTemplate(
it["land"]?.asBoolean ?: false,
it["water"]?.asBoolean ?: false,
it["air"]?.asBoolean ?: false,
)
}) {
it.ifBoolean { pathThrough ->
ConiumBlockPathFindThroughTemplate(
pathThrough,
pathThrough,
pathThrough
)
}
}!!
}
}

override fun settings(settings: ConiumBlockSettings) {
settings.landPathThrough = this.landPathThrough
settings.waterPathThrough = this.waterPathThrough
settings.airPathThrough = this.airPathThrough
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ import com.github.cao.awa.conium.event.type.ConiumEventArgTypes
import com.github.cao.awa.conium.event.type.ConiumEventType
import com.github.cao.awa.conium.parameter.ParameterSelective
import com.github.cao.awa.conium.parameter.ParameterSelective1
import com.github.cao.awa.conium.parameter.ParameterSelective4
import net.minecraft.entity.Entity
import net.minecraft.entity.LivingEntity
import net.minecraft.entity.damage.DamageSource
import net.minecraft.world.World

class ConiumEntityTickEvent : ConiumEvent<ParameterSelective1<Boolean, Entity>>() {
override fun requirement(): ConiumEventContext<out ParameterSelective> {
Expand Down
Loading

0 comments on commit df18182

Please sign in to comment.