Skip to content
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

try fix lma #10

Merged
merged 5 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ root = true
[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
Expand Down
2 changes: 1 addition & 1 deletion dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

dependencies {
api 'com.github.GTNewHorizons:Forgelin:1.10.0-GTNH'
implementation 'com.github.GTNewHorizons:GT5-Unofficial:5.09.49.68:dev'
implementation 'com.github.GTNewHorizons:GT5-Unofficial:5.09.49.113:dev'
implementation 'com.github.GTNewHorizons:Applied-Energistics-2-Unofficial:rv3-beta-451-GTNH:dev'

implementation 'net.industrial-craft:industrialcraft-2:2.2.828-experimental:dev'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.muxiu1997.mxrandom.client.gui

import com.muxiu1997.mxrandom.MODID
import com.muxiu1997.mxrandom.metatileentity.LargeMolecularAssembler
import com.muxiu1997.mxrandom.metatileentity.MTELargeMolecularAssembler
import com.muxiu1997.mxrandom.network.container.ContainerConfigLargeMolecularAssembler
import java.awt.Color
import net.minecraft.client.gui.GuiButton
Expand All @@ -11,7 +11,7 @@ import net.minecraft.util.StatCollector

class GuiConfigLargeMolecularAssembler(GuiContainer: ContainerConfigLargeMolecularAssembler) :
GuiContainer(GuiContainer) {
@Suppress("PrivatePropertyName") private val LMA: LargeMolecularAssembler = GuiContainer.LMA
@Suppress("PrivatePropertyName") private val LMA: MTELargeMolecularAssembler = GuiContainer.LMA
private lateinit var buttonToggleCraftingFX: GuiButton

init {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ package com.muxiu1997.mxrandom.loader

import com.muxiu1997.mxrandom.MODID
import com.muxiu1997.mxrandom.MXRandom.MTE_ID_OFFSET
import com.muxiu1997.mxrandom.metatileentity.LargeMolecularAssembler
import com.muxiu1997.mxrandom.metatileentity.MTELargeMolecularAssembler
import cpw.mods.fml.common.event.FMLInitializationEvent
import gregtech.api.metatileentity.implementations.MTEEnhancedMultiBlockBase
import gregtech.api.metatileentity.implementations.MTEMultiBlockBase

object GTMetaTileEntityLoader {
lateinit var largeMolecularAssembler: LargeMolecularAssembler
lateinit var largeMolecularAssembler: MTELargeMolecularAssembler

fun load(@Suppress("UNUSED_PARAMETER") e: FMLInitializationEvent) {
largeMolecularAssembler = register(1, "Large Molecular Assembler")
}

private inline fun <reified T : MTEEnhancedMultiBlockBase<*>> register(
private inline fun <reified T : MTEMultiBlockBase> register(
id: Int,
name: String,
unlocalizedName: String? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,35 +31,41 @@ import com.muxiu1997.mxrandom.client.gui.GuiConfigLargeMolecularAssembler
import com.muxiu1997.mxrandom.network.container.ContainerConfigLargeMolecularAssembler
import com.muxiu1997.mxrandom.network.message.MessageCraftingFX
import com.muxiu1997.mxrandom.network.message.MessageSyncMetaTileEntityConfig
import cpw.mods.fml.common.FMLCommonHandler
import cpw.mods.fml.common.network.NetworkRegistry
import gregtech.api.GregTechAPI
import gregtech.api.enums.ItemList
import gregtech.api.enums.Textures.BlockIcons
import gregtech.api.interfaces.ITexture
import gregtech.api.interfaces.metatileentity.IMetaTileEntity
import gregtech.api.interfaces.tileentity.IGregTechTileEntity
import gregtech.api.metatileentity.implementations.MTEEnhancedMultiBlockBase
import gregtech.api.metatileentity.implementations.MTEExtendedPowerMultiBlockBase
import gregtech.api.render.TextureFactory
import gregtech.api.util.GTStructureUtility.ofHatchAdder
import gregtech.api.util.GTUtility
import gregtech.api.util.MultiblockTooltipBuilder
import gregtech.common.items.behaviors.BehaviourDataOrb
import gregtech.common.tileentities.machines.MTEHatchCraftingInputME
import io.netty.buffer.ByteBuf
import java.util.*
import kotlin.collections.ArrayList
import kotlin.math.max
import kotlin.math.min
import net.minecraft.entity.player.EntityPlayer
import net.minecraft.entity.player.EntityPlayerMP
import net.minecraft.inventory.InventoryCrafting
import net.minecraft.item.ItemStack
import net.minecraft.nbt.NBTBase
import net.minecraft.nbt.NBTTagCompound
import net.minecraft.nbt.NBTTagList
import net.minecraft.util.EnumChatFormatting
import net.minecraft.world.World
import net.minecraft.world.WorldServer
import net.minecraftforge.common.util.Constants
import net.minecraftforge.common.util.ForgeDirection

@Deprecated("Planned for removal.")
class LargeMolecularAssembler :
MTEEnhancedMultiBlockBase<LargeMolecularAssembler>,
class MTELargeMolecularAssembler :
MTEExtendedPowerMultiBlockBase<MTELargeMolecularAssembler>,
IConfigurableMetaTileEntity,
ICraftingProvider,
IActionHost,
Expand All @@ -69,15 +75,16 @@ class LargeMolecularAssembler :
private var craftingDisplayPoint: CraftingDisplayPoint? = null

private var cachedDataOrb: ItemStack? = null
private var cachedAeJobs: LinkedList<ItemStack>? = LinkedList()
private var cachedAeJobs: MutableList<List<ItemStack>>? = ArrayList()
private var aeJobsDirty = false

private var cachedPatterns: List<ItemStack> = emptyList()
private var cachedRawPatterns: List<ItemStack> = emptyList()
private var cachedPatternDetails: List<ICraftingPatternDetails> = emptyList()
private val patternDetailCache:
MutableMap<ItemStack, Pair<NBTTagCompound, ICraftingPatternDetails>> =
IdentityHashMap()

private var requestSource: BaseActionSource? = null
private var cachedOutputs = AEApi.instance().storage().createItemList()
private var cachedOutputs = AEApi.instance().storage().createPrimitiveItemList()
private var lastOutputFailed = false
private var lastOutputTick: Long = 0
private var tickCounter: Long = 0
Expand All @@ -89,9 +96,9 @@ class LargeMolecularAssembler :

constructor(aName: String) : super(aName)

// region MTEEnhancedMultiBlockBase
// region GT_MetaTileEntity_EnhancedMultiBlockBase
override fun newMetaEntity(iGregTechTileEntity: IGregTechTileEntity): IMetaTileEntity {
return LargeMolecularAssembler(this.mName)
return MTELargeMolecularAssembler(this.mName)
}

override fun getTexture(
Expand Down Expand Up @@ -121,7 +128,7 @@ class LargeMolecularAssembler :
withAeJobs { _, aeJobs ->
mMaxProgresstime = 20
var craftingProgressTime = 20
var craftingEUt = EU_PER_TICK_CRAFTING
var craftingEUt = EU_PER_TICK_CRAFTING.toLong()
mEUt = -EU_PER_TICK_BASIC
// Tier EU_PER_TICK_CRAFTING == 2
var extraTier = max(0, GTUtility.getTier(maxInputVoltage).toInt() - 2)
Expand All @@ -133,24 +140,25 @@ class LargeMolecularAssembler :
extraTier--
}
var times = 2
// Subsequent Overclocks Double the number of Jobs finished at once to a Max of 256"
while (times < 256) {
// Subsequent Overclocks Double the number of Jobs finished at once to a Max of Integer.MAX
while (times > 0) {
boubou19 marked this conversation as resolved.
Show resolved Hide resolved
if (extraTier <= 0) break
times *= 2
craftingEUt *= 4
extraTier--
}
val outputs = LinkedList<ItemStack>()
for (i in 0 until times) {
if (aeJobs.isEmpty()) break
outputs.addFirst(aeJobs.removeFirst())
aeJobsDirty = true
if (times < 0) {
// overflow handling
times = Int.MAX_VALUE
}
val outputs = aeJobs.take(times).flatten()
if (outputs.isNotEmpty()) {
mEUt = -craftingEUt
aeJobs.subList(0, min(times, aeJobs.size)).clear()
aeJobsDirty = true
lEUt = -craftingEUt
mMaxProgresstime = craftingProgressTime
mOutputItems = outputs.toTypedArray()
addCraftingFX(outputs.first)
addCraftingFX(outputs[0])
}
mEfficiency = 10000 - (idealStatus - repairStatus) * 1000
mEfficiencyIncrease = 10000
Expand Down Expand Up @@ -185,7 +193,6 @@ class LargeMolecularAssembler :
fun PURPLE(thing: Any) = "${EnumChatFormatting.DARK_PURPLE}$thing${EnumChatFormatting.GRAY}"
return MultiblockTooltipBuilder().also {
it.addMachineType(MACHINE_TYPE)
.addInfo(EnumChatFormatting.RED.toString() + "DEPRECATED, PLANNED FOR REMOVAL.")
.addInfo("Need a Data Orb to put in the Controller to work")
.addInfo("Basic: ${GREEN(EU_PER_TICK_BASIC)} Eu/t, Unaffected by overclocking")
.addInfo(
Expand Down Expand Up @@ -238,7 +245,7 @@ class LargeMolecularAssembler :
STRUCTURE_DEPTH_OFFSET)
}

override fun getStructureDefinition(): IStructureDefinition<LargeMolecularAssembler> =
override fun getStructureDefinition(): IStructureDefinition<MTELargeMolecularAssembler> =
STRUCTURE_DEFINITION

override fun addOutput(stack: ItemStack): Boolean {
Expand Down Expand Up @@ -273,7 +280,7 @@ class LargeMolecularAssembler :
// endregion

private inline fun withAeJobs(
action: (dataOrb: ItemStack, aeJobs: LinkedList<ItemStack>) -> Unit
action: (dataOrb: ItemStack, aeJobs: MutableList<List<ItemStack>>) -> Unit
) {
if (mInventory[1] === cachedDataOrb && cachedDataOrb != null) {
action(cachedDataOrb!!, cachedAeJobs!!)
Expand All @@ -284,7 +291,7 @@ class LargeMolecularAssembler :
cachedAeJobs = null
return
}
val dataOrb = mInventory[1]
val dataOrb: ItemStack = mInventory[1]
var dataTitle: String = BehaviourDataOrb.getDataTitle(dataOrb)
if (dataTitle.isEmpty()) {
dataTitle = DATA_ORB_TITLE
Expand All @@ -297,8 +304,26 @@ class LargeMolecularAssembler :
return
}
cachedDataOrb = dataOrb
cachedAeJobs =
BehaviourDataOrb.getNBTInventory(dataOrb).filterNotNull().toCollection(LinkedList())
if (dataOrb.stackTagCompound?.hasKey("Inventory", Constants.NBT.TAG_LIST) == true) {
cachedAeJobs =
BehaviourDataOrb.getNBTInventory(dataOrb).asSequence().filterNotNull().mapTo(
mutableListOf()) { listOf(it) }
} else if (dataOrb.stackTagCompound?.hasKey(DATA_ORB_JOBS_KEY, Constants.NBT.TAG_LIST) ==
true) {
cachedAeJobs =
dataOrb.stackTagCompound
.getTagList(DATA_ORB_JOBS_KEY, Constants.NBT.TAG_COMPOUND)
.asCompoundSequence()
.map {
it.getTagList(DATA_ORB_JOBS_JOB_KEY, Constants.NBT.TAG_COMPOUND)
.asCompoundSequence()
.map { GTUtility.loadItem(it) }
.toList()
}
.toMutableList()
} else {
cachedAeJobs = mutableListOf()
}
action(cachedDataOrb!!, cachedAeJobs!!)
}

Expand Down Expand Up @@ -350,27 +375,41 @@ class LargeMolecularAssembler :
private fun saveAeJobsIfNeeded() {
if (!aeJobsDirty) return
withAeJobs { dataOrb, aeJobs ->
BehaviourDataOrb.setNBTInventory(dataOrb, aeJobs.toTypedArray())
dataOrb.stackTagCompound.setTag(
DATA_ORB_JOBS_KEY,
aeJobs.mapToTagList { job ->
NBTTagCompound().also {
it.setTag(DATA_ORB_JOBS_JOB_KEY, job.mapToTagList { GTUtility.saveItem(it) })
}
})
markDirty()
aeJobsDirty = false
}
}

private fun issuePatternChangeIfNeeded(tick: Long) {
if (tick % 20 != 0L) return
compactedInputs.let { inputs ->
if (storedInputs.equals(cachedRawPatterns)) return
cachedRawPatterns = ArrayList(storedInputs)
val patterns = inputs.filter { it.getPattern(baseMetaTileEntity.world)?.isCraftable == true }
if (patterns == cachedPatterns) return
cachedPatterns = patterns
cachedPatternDetails = patterns.map { it.getPattern(baseMetaTileEntity.world)!! }
proxy?.let {
try {
it.grid.postEvent(MENetworkCraftingPatternChange(this, it.node))
} catch (ignored: GridAccessException) {
// Do nothing
}
val inputs =
GTUtility.filterValidMTEs(mInputBusses)
.asSequence()
.filter { it !is MTEHatchCraftingInputME }
.flatMap { (0 until it.sizeInventory).asSequence().map { i -> it.getStackInSlot(i) } }
.filterNotNull()
.toSet()
val patterns =
inputs
.map { it.getPattern(baseMetaTileEntity.world) }
.filterNotNull()
.filter { it.isCraftable }
.toList()
if (patterns == cachedPatternDetails) return
cachedPatternDetails = patterns
patternDetailCache.keys.retainAll(inputs)
proxy?.let {
try {
it.grid.postEvent(MENetworkCraftingPatternChange(this, it.node))
} catch (ignored: GridAccessException) {
// Do nothing
}
}
}
Expand Down Expand Up @@ -429,11 +468,10 @@ class LargeMolecularAssembler :
table: InventoryCrafting
): Boolean {
withAeJobs { _, aeJobs ->
if (aeJobs.size < 256) {
aeJobs.add(patternDetails.getOutput(table, baseMetaTileEntity.world))
aeJobsDirty = true
return true
}
aeJobs.add(
patternDetails.getOutputs(table, baseMetaTileEntity.world)?.toList() ?: return false)
aeJobsDirty = true
return true
}
return false
}
Expand Down Expand Up @@ -479,6 +517,8 @@ class LargeMolecularAssembler :
// endregion

companion object {
private const val DATA_ORB_JOBS_KEY = "MX-CraftingJobs"
private const val DATA_ORB_JOBS_JOB_KEY = "Job"
private const val MACHINE_TYPE = "Molecular Assembler"
private const val EU_PER_TICK_BASIC = 16
private const val EU_PER_TICK_CRAFTING = 64
Expand All @@ -494,7 +534,7 @@ class LargeMolecularAssembler :

// region STRUCTURE_DEFINITION
private val STRUCTURE_DEFINITION =
StructureDefinition.builder<LargeMolecularAssembler>()
StructureDefinition.builder<MTELargeMolecularAssembler>()
.addShape(
STRUCTURE_PIECE_MAIN,
transpose(
Expand All @@ -509,7 +549,9 @@ class LargeMolecularAssembler :
'C',
ofChain(
ofHatchAdder(
LargeMolecularAssembler::addToLargeMolecularAssemblerList, CASING_INDEX, 1),
MTELargeMolecularAssembler::addToLargeMolecularAssemblerList,
CASING_INDEX,
1),
onElementPass({ it.casing++ }, ofBlock(GregTechAPI.sBlockCasings4, 0)),
))
.addElement(
Expand Down Expand Up @@ -567,10 +609,41 @@ class LargeMolecularAssembler :
}
// endregion

private fun ItemStack.getPattern(w: World): ICraftingPatternDetails? {
val item = this.item
if (item !is ItemEncodedPattern) return null
return item.getPatternForItem(this, w)
private fun ICraftingPatternDetails.getOutputs(
ic: InventoryCrafting,
w: World
): Sequence<ItemStack>? {
val mainOutput = getOutput(ic, w) ?: return null
FMLCommonHandler.instance()
.firePlayerCraftingEvent(Platform.getPlayer(w as WorldServer), mainOutput, ic)
val leftover =
(0..ic.sizeInventory)
.asSequence()
.map { Platform.getContainerItem(ic.getStackInSlot(it)) }
.filterNotNull() + mainOutput
return leftover
}

private fun NBTTagList.asCompoundSequence(): Sequence<NBTTagCompound> {
return (0..tagCount()).asSequence().map { getCompoundTagAt(it) }
}

private inline fun <T> Iterable<T>.mapToTagList(action: (T) -> NBTBase): NBTTagList =
NBTTagList().also { for (e in this) it.appendTag(action(e)) }
}

private fun ItemStack.getPattern(w: World): ICraftingPatternDetails? {
val item = this.item
if (item !is ItemEncodedPattern) return null
val (tag, detail) = patternDetailCache[this] ?: return getPatternRaw(w)
if (tag !== this.stackTagCompound) return getPatternRaw(w)
return detail
}

private fun ItemStack.getPatternRaw(w: World): ICraftingPatternDetails {
val item = this.item as ItemEncodedPattern
val detail = item.getPatternForItem(this, w)
patternDetailCache[this] = Pair(stackTagCompound, detail)
return detail
}
}
Loading