Skip to content

Commit

Permalink
Implement leveling the kuarry up
Browse files Browse the repository at this point in the history
There are currently three levels levels, 0, 1 and 2 (1, 2 and 3 for
the user). The levels add more energy and upgrade slots for the
upcoming upgrades, since there might not be enough space when they're
implemented. While the third level gives 10 upgrade slots, which
really won't have anything to fill them for a while, the system for
expanding the slot amount easily is implemented, so when some upgrades
DO come, there will already be place for them.

The upgrades are done by the two new upgrade items. They have slightly
different colors and names, inspired by the upgrades in
Thermal Expansion.

The block model now selects the the appropriate data for the
blockstate in getActualState, getting it from the tile entity. Same
thing happens for the ItemStack name and the custom mesh definition.

For reasons unknown, the "inventory" variation has to stay in the
blockstate, because oterwise the game throws an exception about it not
beign there, albeit everything else working normally.
  • Loading branch information
vaartis committed Dec 9, 2019
1 parent 9aec427 commit 31356f8
Show file tree
Hide file tree
Showing 18 changed files with 284 additions and 23 deletions.
35 changes: 31 additions & 4 deletions src/main/kotlin/org/kotobank/kuarry/KuarryModItems.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,38 @@ object KuarryModItems {
)
}

private val levelUpgrades by lazy {
listOf(
Pair("level_2_upgrade", HardenedUpgrade()),
Pair("level_3_upgrade", ReinforcedUpgrade())
)
}

private val items by lazy {
listOf(
Pair("kuarry", ItemBlock(KuarryModBlocks.kuarry)),
Pair("denatured_stone", ItemBlock(KuarryModBlocks.denatured_stone)),

Pair("kuarry_casing", Item())
)
}

internal val allItems by lazy { items + upgrades }
/** Items that can easily be registered with the same code */
internal val commonlyRegisteredItems by lazy { items + upgrades + levelUpgrades }

// region Items that need special code for registration

internal val luckUpgrade by lazy { KuarryLuckUpgrade() }

internal val kuarry by lazy { KuarryItemBlock(KuarryModBlocks.kuarry) }

// endregion

@SubscribeEvent
fun registerItems(event: Register<Item>) {
// Set each upgrade's max stack size
upgrades.forEach { (_, item) -> item.setMaxStackSize(item.stackSize) }

allItems.forEach { (name, item) ->
commonlyRegisteredItems.forEach { (name, item) ->
item.apply {
setRegistryName(KuarryMod.MODID, name)
setUnlocalizedName(name)
Expand All @@ -61,6 +74,20 @@ object KuarryModItems {
setUnlocalizedName("luck_upgrade")
event.registry.register(this)
}

with(kuarry) {
setRegistryName(KuarryMod.MODID, "kuarry")
setUnlocalizedName("kuarry")

ModelLoader.setCustomMeshDefinition(this) { stack ->
val level = stack.tagCompound?.getInteger("upgrade_level") ?: 0

// Return a custom model based on the level from the ItemStack
ModelResourceLocation(this.registryName!!, "facing=north,level=${level}")
}

event.registry.register(this)
}
}

// The fields are named by their IDs
Expand All @@ -83,7 +110,7 @@ object ItemModelLoader {
@SubscribeEvent(priority = EventPriority.LOW)
@Suppress("UNUSED_PARAMETER")
fun loadModels(event: Register<Item>) {
KuarryModItems.allItems.forEach { (_, item) ->
KuarryModItems.commonlyRegisteredItems.forEach { (_, item) ->
item.apply {
ModelLoader.setCustomModelResourceLocation(this, 0, ModelResourceLocation(this.registryName!!, "inventory"))
}
Expand Down
27 changes: 25 additions & 2 deletions src/main/kotlin/org/kotobank/kuarry/block/KuarryBlock.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.kotobank.kuarry.block

import java.util.ArrayList
import net.minecraft.block.Block
import net.minecraft.block.material.Material
import net.minecraft.block.properties.PropertyDirection
import net.minecraft.block.properties.PropertyInteger
import net.minecraft.block.state.BlockStateContainer
import net.minecraft.block.state.IBlockState
import net.minecraft.client.gui.GuiScreen
Expand All @@ -25,6 +27,7 @@ import net.minecraftforge.fml.relauncher.Side
import net.minecraftforge.fml.relauncher.SideOnly
import net.minecraftforge.items.ItemStackHandler
import org.kotobank.kuarry.helper.TranslationHelper
import org.kotobank.kuarry.item.LevelUpgrade
import org.kotobank.kuarry.KuarryMod
import org.kotobank.kuarry.KuarryModGUIHandler
import org.kotobank.kuarry.tile_entity.KuarryTileEntity
Expand All @@ -36,6 +39,8 @@ class KuarryBlock(material: Material, registryName: String) : Block(material), I
val FACING: PropertyDirection =
PropertyDirection.create("facing", EnumFacing.Plane.HORIZONTAL)

val LEVEL: PropertyInteger = PropertyInteger.create("level", 0, 2)

val tileEntityClass = KuarryTileEntity::class.java
}

Expand All @@ -58,13 +63,21 @@ class KuarryBlock(material: Material, registryName: String) : Block(material), I
playerIn: EntityPlayer, hand: EnumHand, facing: EnumFacing,
hitX: Float, hitY: Float, hitZ: Float): Boolean {
if (!worldIn.isRemote) {
val item = playerIn.getHeldItem(hand).item
val te = worldIn.getTileEntity(pos)
// If the item used is an upgrade and the tile entity exists, DON'T open the GUI,
// because the block will be upgraded
if (item is LevelUpgrade && te is KuarryTileEntity && item.shouldUpgrade(te)) {
return false
}

playerIn.openGui(KuarryMod, KuarryModGUIHandler.KUARRY, worldIn, pos.x, pos.y, pos.z)
}

return true;
return true
}

override fun createBlockState() = BlockStateContainer(this, FACING)
override fun createBlockState() = BlockStateContainer(this, FACING, LEVEL)

override fun getMetaFromState(state: IBlockState) = state.getValue(FACING).index

Expand Down Expand Up @@ -156,6 +169,16 @@ class KuarryBlock(material: Material, registryName: String) : Block(material), I
}
}

override fun getActualState(state: IBlockState, worldIn: IBlockAccess, pos: BlockPos): IBlockState {
val te = worldIn.getTileEntity(pos)
return if (te is KuarryTileEntity) {
// If there tile entity already exists, return the upgrade level from it
state.withProperty(LEVEL, te.upgradeLevel)
} else {
state
}
}

// region IDismantleable implementation

@Optional.Method(modid = "cofhcore")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class KuarryContainer(inventoryPlayer: InventoryPlayer, val tileEntity: KuarryTi

// Add the upgrade inventory
val upgradeInventory = tileEntity.upgradeInventory
InventoryHelper.forEachPositionInInventory(KuarryTileEntity.upgradeInventoryWidth, KuarryTileEntity.upgradeInventoryHeight) {
InventoryHelper.forEachPositionInInventory(tileEntity.upgradeInventoryWidth, tileEntity.upgradeInventoryHeight) {
positionInInventory: Int, widthPos: Int, heightPos: Int ->

addSlotToContainer(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,29 @@ import org.kotobank.kuarry.packet.SwitchKuarrySetting
import org.kotobank.kuarry.tile_entity.KuarryTileEntity.ActivationMode
import org.kotobank.kuarry.container.BaseGUIContainer
import org.kotobank.kuarry.integration.autopushing.Autopusher
import org.kotobank.kuarry.item.LevelValues


class KuarryGUIContainer(override val container: KuarryContainer) : BaseGUIContainer(container) {
companion object {
private const val energyBarTopY = 76
private const val energyBarTopY = 184
private const val energyBarPlaceX = 194
private const val energyBarTextureX = 240

private const val energyBarHeight = 38
private const val energyBarWidth = 14

private const val upgradeInvX = 182
private const val upgradeInvTopY = 0
private const val upgradeInvWidth = 50
private const val upgradeInvSurroundersHeight = 7
private const val upgradeInvSlotLineHeight = 18

private const val upgradeInvSlotLineTextureX = 182
private const val upgradeInvSlotLineTextureY = 230
private const val upgradeInvSurroundersTextureX = 131
private const val upgradeInvHeaderTextureY = 230
private const val upgradeInvBottomTextureY = 241
}

override val actualXSize = 231
Expand Down Expand Up @@ -58,6 +71,32 @@ class KuarryGUIContainer(override val container: KuarryContainer) : BaseGUIConta
energyBarTextureX, energyBarTopY + energyBarHeight - scaledHeight,
energyBarWidth, scaledHeight
)

// Upgrade inventory header
drawTexturedModalRect(
guiLeft + upgradeInvX, guiTop + upgradeInvTopY,
upgradeInvSurroundersTextureX, upgradeInvHeaderTextureY,
upgradeInvWidth, upgradeInvSurroundersHeight
)
val upgradeSlotLines = LevelValues[container.tileEntity.upgradeLevel].upgradeSlotLines
// Draw all the lines
for (i in 0 until upgradeSlotLines) {
drawTexturedModalRect(
guiLeft + upgradeInvX,
// Add the header + all the lines' heights that were before this line
guiTop + upgradeInvTopY + upgradeInvSurroundersHeight + (i * upgradeInvSlotLineHeight),
upgradeInvSlotLineTextureX, upgradeInvSlotLineTextureY,
upgradeInvWidth, upgradeInvSlotLineHeight
)
}
// Draw the inventory bottom after all the lines
drawTexturedModalRect(
guiLeft + upgradeInvX,
guiTop + upgradeInvTopY + upgradeInvSurroundersHeight +
(upgradeSlotLines * upgradeInvSlotLineHeight),
upgradeInvSurroundersTextureX, upgradeInvBottomTextureY,
upgradeInvWidth, upgradeInvSurroundersHeight
)
}
}

Expand Down
18 changes: 18 additions & 0 deletions src/main/kotlin/org/kotobank/kuarry/item/KuarryItemBlock.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.kotobank.kuarry.item

import net.minecraft.block.Block
import net.minecraft.item.ItemBlock
import net.minecraft.item.ItemStack

class KuarryItemBlock(block: Block) : ItemBlock(block) {
override fun getUnlocalizedName(stack: ItemStack): String {
val blockName = super.getUnlocalizedName(stack)

// Get the name based on the level
return when (val level = stack.tagCompound?.getInteger("upgrade_level") ?: 0) {
1, 2 ->
"${blockName}.level_${level + 1}"
else -> super.getUnlocalizedName(stack)
}
}
}
62 changes: 62 additions & 0 deletions src/main/kotlin/org/kotobank/kuarry/item/LevelUpgrade.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package org.kotobank.kuarry.item

import net.minecraft.entity.player.EntityPlayer
import net.minecraft.init.SoundEvents
import net.minecraft.item.Item
import net.minecraft.util.*
import net.minecraft.util.math.BlockPos
import net.minecraft.world.World
import org.kotobank.kuarry.tile_entity.KuarryTileEntity

/** Data storage for various level-dependant things of the [KuarryTileEntity]. */
data class LevelProperties(val energy: Int, val upgradeSlotLines: Int)

/** Data for all levels. */
val LevelValues = arrayOf(
// Base
LevelProperties(100_000, 3),
// Hardened
LevelProperties(200_000, 4),
// Reinforced
LevelProperties(300_000, 5)
)

sealed class LevelUpgrade : Item() {
abstract val level: Int

/** Whether the upgrade should apply.
*
* Currently the only condition is that the machine is of the level directly below the upgrade level.
*/
fun shouldUpgrade(te: KuarryTileEntity) = level - 1 == te.upgradeLevel

override fun onItemUse(player: EntityPlayer, worldIn: World, pos: BlockPos, hand: EnumHand, facing: EnumFacing, hitX: Float, hitY: Float, hitZ: Float): EnumActionResult {
val itemstack = player.getHeldItem(hand)

if (itemstack.item == this) {
val te = worldIn.getTileEntity(pos)
if (te is KuarryTileEntity) {
if (shouldUpgrade(te)) {
// Set the level and shrink the amount of upgrades
te.upgradeLevel = level
itemstack.shrink(1)

// Play the upgrade sound, pass null as a player so it plays for everyone
worldIn.playSound(null, pos, SoundEvents.BLOCK_ANVIL_USE, SoundCategory.PLAYERS, 1f, 1f)

return EnumActionResult.SUCCESS
}
}
}

return EnumActionResult.PASS
}
}

class HardenedUpgrade : LevelUpgrade() {
override val level = 1
}

class ReinforcedUpgrade : LevelUpgrade() {
override val level = 2
}

0 comments on commit 31356f8

Please sign in to comment.