Fix/various p2p binding issues (#39)

Co-authored-by: Martin Robertz <dream-master@gmx.net>
This commit is contained in:
hiroschoandMartin Robertz authored and GitHub committed 2025-10-18 11:22:16 +00:00
1 parent 67e3d6571c
commit 3ec42b791a
3 files changed
+22 -117

No files matched your search

+2 -2
View File
@@ -1,13 +1,13 @@
// Add your dependencies here
dependencies {
api('com.github.GTNewHorizons:Applied-Energistics-2-Unofficial:rv3-beta-700-GTNH:dev')
api('com.github.GTNewHorizons:Applied-Energistics-2-Unofficial:rv3-beta-716-GTNH:dev')
api('com.github.GTNewHorizons:Forgelin:2.0.3-GTNH')
api('com.github.GTNewHorizons:BuildCraft:7.1.45:dev')
// Some optional P2P tunnel support
compileOnly('net.industrial-craft:industrialcraft-2:2.2.828-experimental:api')
devOnlyNonPublishable('com.github.GTNewHorizons:OpenComputers:1.12.0-GTNH:dev') {transitive = false}
compileOnly('pneumaticCraft:PneumaticCraft-1.7.10:1.12.7-152:api') {transitive = false}
api('com.github.GTNewHorizons:AE2FluidCraft-Rework:1.5.1-gtnh:dev') {transitive = false}
api('com.github.GTNewHorizons:AE2FluidCraft-Rework:1.5.6-gtnh:dev') {transitive = false}
api('com.github.GTNewHorizons:Baubles-Expanded:2.2.0-GTNH:api')
}
+1 -1
View File
@@ -17,7 +17,7 @@ pluginManagement {
}
plugins {
id 'com.gtnewhorizons.gtnhsettingsconvention' version '1.0.43'
id 'com.gtnewhorizons.gtnhsettingsconvention' version '1.0.44'
}
@@ -3,15 +3,10 @@ package com.projecturanus.betterp2p.network.data
import appeng.api.config.SecurityPermissions
import appeng.api.networking.IGrid
import appeng.api.networking.security.ISecurityGrid
import appeng.api.parts.IPart
import appeng.api.parts.PartItemStack
import appeng.helpers.IInterfaceHost
import appeng.me.GridAccessException
import appeng.me.cache.P2PCache
import appeng.parts.automation.UpgradeInventory
import appeng.parts.p2p.PartP2PTunnel
import appeng.parts.p2p.PartP2PTunnelStatic
import appeng.tile.inventory.AppEngInternalInventory
import appeng.util.Platform
import com.projecturanus.betterp2p.BetterP2P
import com.projecturanus.betterp2p.util.p2p.TunnelInfo
@@ -21,7 +16,6 @@ import net.minecraft.entity.player.EntityPlayer
import net.minecraft.item.ItemStack
import net.minecraft.nbt.NBTTagCompound
import net.minecraft.util.ChatComponentTranslation
import net.minecraftforge.common.util.ForgeDirection
/**
* When the player uses the adv memory card, this is cached on the server side
@@ -133,31 +127,23 @@ class GridServerCache(private val grid: IGrid, val player: EntityPlayer, var typ
return null
}
var frequency = input.frequency
val cache = input.proxy.p2P
// Generate a new frequency if needed
if (input.frequency == 0L || input.isOutput) {
frequency = System.currentTimeMillis()
}
// If tunnel was already bound, unbind that one
if (cache.getInput(frequency) != null) {
val originalInput = cache.getInput(frequency)
if (originalInput != input) {
updateP2P(originalInput.toLoc(), originalInput, frequency, true, input.customName)
}
}
// Perform the link
val inputResult: PartP2PTunnel<*> = updateP2P(inputIndex, input, frequency, false, input.customName)
val outputResult: PartP2PTunnel<*> = updateP2P(outputIndex, output, frequency, true, input.customName)
val memoryCardData : NBTTagCompound
// Special case for interfaces
if (input is IInterfaceHost && output is IInterfaceHost) {
val drops = mutableListOf<ItemStack>()
handleInterface(input, output, inputResult as IInterfaceHost, outputResult as IInterfaceHost, drops)
Platform.spawnDrops(player.worldObj, output.location.x, output.location.y, output.location.z, drops)
val inputResult = input.convertToInput(player, null)
if(inputResult != null) {
memoryCardData = inputResult.memoryCardData
markDirty(inputIndex, inputResult)
} else {
throw IllegalStateException("Cannot bind")
}
val newType = ItemStack.loadItemStackFromNBT(memoryCardData)
val freq = memoryCardData.getLong("freq")
val outputResult = output.convertToOutput(player, newType, freq)
if (outputResult != null) {
markDirty(outputIndex, outputResult)
} else {
throw IllegalStateException("Cannot bind")
}
return inputResult to outputResult
@@ -165,50 +151,11 @@ class GridServerCache(private val grid: IGrid, val player: EntityPlayer, var typ
fun unlinkP2P(p2pIndex: P2PLocation): PartP2PTunnel<*>? {
val tunnel = listP2P[p2pIndex] ?: return null
val oldFreq = tunnel.frequency
if (oldFreq == 0L) {
return tunnel
}
return updateP2P(p2pIndex, tunnel, 0L, false, tunnel.customName)
}
/**
* Sets the p2p tunnel to the frequency, output, and custom name. Removes the old one and replaces it, which lets
* AE2 trigger the Grid refresh for us (though we need to update the tunnels ourselves)
*/
private fun updateP2P(key: P2PLocation, tunnel: PartP2PTunnel<*>, frequency: Long, output: Boolean, name: String): PartP2PTunnel<*> {
val side = tunnel.side
val data = NBTTagCompound()
tunnel.host.removePart(side, true)
val p2pItem: ItemStack = tunnel.getItemStack(PartItemStack.Wrench)
p2pItem.writeToNBT(data)
data.setLong("freq", frequency)
val newType = ItemStack.loadItemStackFromNBT(data)
val dir: ForgeDirection = tunnel.host?.addPart(newType, side, player) ?: throw RuntimeException("Cannot bind")
val newBus: IPart = tunnel.host.getPart(dir)
if (newBus is PartP2PTunnel<*>) {
newBus.outputProperty = output
if (!name.isBlank()) {
newBus.customName = name
}
try {
val p2p = newBus.proxy.p2P
p2p.updateFreq(newBus, frequency)
} catch (e: GridAccessException) {
// :P
}
newBus.onTunnelNetworkChange()
markDirty(key, newBus)
return newBus
} else {
throw IllegalStateException("Cannot bind")
val newTunnel = tunnel.unbind(player)
if(newTunnel != tunnel) {
markDirty(p2pIndex, newTunnel)
}
return newTunnel
}
/**
@@ -304,46 +251,4 @@ class GridServerCache(private val grid: IGrid, val player: EntityPlayer, var typ
}
return false
}
/**
* Handles a p2p interface/dual interface case.
*/
private fun handleInterface(oldIn: IInterfaceHost, oldOut: IInterfaceHost,
newIn: IInterfaceHost, newOut: IInterfaceHost,
drops: MutableList<ItemStack>) {
// For input and output, retain upgrades, items, and settings.
val upgradesIn = oldIn.interfaceDuality.getInventoryByName("upgrades") as UpgradeInventory
upgradesIn.forEachIndexed { index, stack ->
(newIn.interfaceDuality.getInventoryByName("upgrades") as UpgradeInventory).setInventorySlotContents(index, stack)
}
val upgradesOut = oldOut.interfaceDuality.getInventoryByName("upgrades") as UpgradeInventory
upgradesOut.forEachIndexed { index, stack ->
(newOut.interfaceDuality.getInventoryByName("upgrades") as UpgradeInventory).setInventorySlotContents(index, stack)
}
val itemsIn = oldIn.interfaceDuality.storage as AppEngInternalInventory
itemsIn.forEachIndexed { index, stack ->
(newIn.interfaceDuality.storage as AppEngInternalInventory).setInventorySlotContents(index, stack)
}
val itemsOut = oldOut.interfaceDuality.storage as AppEngInternalInventory
itemsOut.forEachIndexed { index, stack ->
(newOut.interfaceDuality.storage as AppEngInternalInventory).setInventorySlotContents(index, stack)
}
val settingsIn = oldIn.interfaceDuality.configManager
settingsIn.settings.forEach {
newIn.configManager.putSetting(it, settingsIn.getSetting(it))
}
val settingsOut = oldOut.interfaceDuality.configManager
settingsOut.settings.forEach {
newOut.configManager.putSetting(it, settingsOut.getSetting(it))
}
// For input, just copy the patterns over
val patternsIn = oldIn.interfaceDuality.patterns as AppEngInternalInventory
patternsIn.forEachIndexed { index, stack ->
(newIn.interfaceDuality.patterns as AppEngInternalInventory).setInventorySlotContents(index, stack)
}
// For output, drop items
val patternsOut = oldOut.interfaceDuality.patterns as AppEngInternalInventory
drops.addAll(patternsOut)
}
}