为支持频道卡的设备增加读取频道卡owner字段的能力

This commit is contained in:
GaLi 2025-11-25 11:49:34 +08:00
parent 47bb88fcff
commit 1f1ab197ed
4 changed files with 124 additions and 7 deletions

View File

@ -36,6 +36,7 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import java.util.List;
import java.util.UUID;
/**
* 样板供应器频道卡兼容实现
@ -55,6 +56,9 @@ public abstract class PatternProviderLogicCompatMixin implements CompatUpgradePr
@Unique
private long eap$compatLastChannel = -1;
@Unique
private UUID eap$compatLastOwner;
@Unique
private boolean eap$compatClientConnected = false;
@ -102,6 +106,7 @@ public abstract class PatternProviderLogicCompatMixin implements CompatUpgradePr
try {
this.host.saveChanges();
this.eap$compatLastChannel = -1;
this.eap$compatLastOwner = null;
this.eap$compatHasInitialized = false;
this.eap$compatInitializeChannelLink();
this.eap$compatSyncVirtualCraftingState();
@ -183,6 +188,7 @@ public abstract class PatternProviderLogicCompatMixin implements CompatUpgradePr
private void eap$compatOnNodeChange(CallbackInfo ci) {
try {
this.eap$compatLastChannel = -1;
this.eap$compatLastOwner = null;
this.eap$compatHasInitialized = false;
// 直接初始化不使用延迟
this.eap$compatInitializeChannelLink();
@ -244,6 +250,7 @@ public abstract class PatternProviderLogicCompatMixin implements CompatUpgradePr
long newChannel = ChannelCardItem.getChannel(stack);
if (newChannel != this.eap$compatLastChannel) {
this.eap$compatLastChannel = -1; // 强制重新初始化
this.eap$compatLastOwner = null;
this.eap$compatHasInitialized = false;
this.eap$compatInitializeChannelLink();
this.eap$compatSyncVirtualCraftingState();
@ -253,10 +260,13 @@ public abstract class PatternProviderLogicCompatMixin implements CompatUpgradePr
}
} else if (this.eap$compatLastChannel != 0L) {
// 频道卡被移除
this.eap$compatLastChannel = -1;
this.eap$compatHasInitialized = false;
this.eap$compatInitializeChannelLink();
this.eap$compatSyncVirtualCraftingState();
if (UpgradeSlotCompat.shouldEnableUpgradeSlots()) {
this.eap$compatLastChannel = -1;
this.eap$compatLastOwner = null;
this.eap$compatHasInitialized = false;
this.eap$compatInitializeChannelLink();
this.eap$compatSyncVirtualCraftingState();
}
}
} catch (Throwable t) {
}
@ -324,6 +334,7 @@ public abstract class PatternProviderLogicCompatMixin implements CompatUpgradePr
long channel = 0L;
boolean found = false;
UUID owner = null;
IUpgradeInventory upgrades = null;
@ -371,6 +382,10 @@ public abstract class PatternProviderLogicCompatMixin implements CompatUpgradePr
for (ItemStack stack : upgrades) {
if (!stack.isEmpty() && stack.getItem() == ModItems.CHANNEL_CARD.get()) {
channel = ChannelCardItem.getChannel(stack);
owner = ChannelCardItem.getOwnerUUID(stack);
if (owner == null) {
owner = this.eap$getFallbackOwner();
}
found = true;
break;
}
@ -380,10 +395,12 @@ public abstract class PatternProviderLogicCompatMixin implements CompatUpgradePr
if (!found) {
this.eap$compatSyncVirtualCraftingState();
if (this.eap$compatLink != null) {
this.eap$compatLink.setPlacerId(null);
this.eap$compatLink.setFrequency(0L);
this.eap$compatLink.updateStatus();
}
this.eap$compatLastChannel = 0L;
this.eap$compatLastOwner = null;
this.eap$compatHasInitialized = true;
try {
this.host.saveChanges();
@ -409,14 +426,25 @@ public abstract class PatternProviderLogicCompatMixin implements CompatUpgradePr
return;
}
boolean sameOwner = (this.eap$compatLastOwner == null && owner == null)
|| (this.eap$compatLastOwner != null && this.eap$compatLastOwner.equals(owner));
if (this.eap$compatLink != null && this.eap$compatLastChannel == channel && sameOwner) {
if (this.eap$compatLink.isConnected()) {
this.eap$compatHasInitialized = true;
}
return;
}
if (this.eap$compatLink == null) {
var endpoint = new GenericNodeEndpointImpl(() -> this.host.getBlockEntity(), () -> this.mainNode.getNode());
this.eap$compatLink = new WirelessSlaveLink(endpoint);
}
this.eap$compatLink.setPlacerId(owner);
this.eap$compatLink.setFrequency(channel);
this.eap$compatLink.updateStatus();
this.eap$compatLastChannel = channel; // 记录当前频道
this.eap$compatLastOwner = owner;
try {
this.host.saveChanges();
} catch (Throwable ignored) {
@ -441,6 +469,14 @@ public abstract class PatternProviderLogicCompatMixin implements CompatUpgradePr
}
}
@Unique
private UUID eap$getFallbackOwner() {
if (this.mainNode != null && this.mainNode.getNode() != null) {
return this.mainNode.getNode().getOwningPlayerProfileId();
}
return null;
}
// CompatUpgradeProvider 实现仅在未安装 appflux 时由我们提供升级槽
@Unique
private boolean eap$hasChannelCard(IUpgradeInventory inventory) {

View File

@ -9,6 +9,7 @@ import com.extendedae_plus.api.bridge.InterfaceWirelessLinkBridge;
import com.extendedae_plus.init.ModItems;
import com.extendedae_plus.items.materials.ChannelCardItem;
import net.minecraft.world.item.ItemStack;
import java.util.UUID;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
@ -30,6 +31,8 @@ public abstract class InterfaceLogicChannelCardMixin implements InterfaceWireles
@Unique
private long eap$lastChannel = -1;
@Unique
private UUID eap$lastOwner;
@Unique
private boolean eap$clientConnected = false;
@Unique
private boolean eap$hasInitialized = false;
@ -136,9 +139,14 @@ public abstract class InterfaceLogicChannelCardMixin implements InterfaceWireles
try {
long channel = 0L;
boolean found = false;
UUID owner = null;
for (ItemStack stack : this.getUpgrades()) {
if (!stack.isEmpty() && stack.getItem() == ModItems.CHANNEL_CARD.get()) {
channel = ChannelCardItem.getChannel(stack);
owner = ChannelCardItem.getOwnerUUID(stack);
if (owner == null) {
owner = this.eap$getFallbackOwner();
}
found = true;
break;
}
@ -147,10 +155,13 @@ public abstract class InterfaceLogicChannelCardMixin implements InterfaceWireles
if (!found) {
// 无频道卡断开并视为初始化完成
if (this.eap$link != null) {
this.eap$link.setPlacerId(null);
this.eap$link.setFrequency(0L);
this.eap$link.updateStatus();
}
this.eap$hasInitialized = true;
this.eap$lastChannel = 0L;
this.eap$lastOwner = null;
// 保存一次状态
try {
this.host.saveChanges();
@ -165,13 +176,23 @@ public abstract class InterfaceLogicChannelCardMixin implements InterfaceWireles
return;
}
boolean sameOwner = (this.eap$lastOwner == null && owner == null)
|| (this.eap$lastOwner != null && this.eap$lastOwner.equals(owner));
if (this.eap$link != null && this.eap$lastChannel == channel && sameOwner) {
this.eap$hasInitialized = this.eap$link.isConnected();
return;
}
if (this.eap$link == null) {
var endpoint = new InterfaceNodeEndpointImpl(this.host, () -> this.mainNode.getNode());
this.eap$link = new WirelessSlaveLink(endpoint);
}
this.eap$link.setPlacerId(owner);
this.eap$link.setFrequency(channel);
this.eap$link.updateStatus();
this.eap$lastChannel = channel;
this.eap$lastOwner = owner;
try {
this.host.saveChanges();
} catch (Throwable ignored) {
@ -239,5 +260,11 @@ public abstract class InterfaceLogicChannelCardMixin implements InterfaceWireles
}
}
// eap$initializeChannelLink方法已在上面实现
@Unique
private UUID eap$getFallbackOwner() {
if (this.mainNode != null && this.mainNode.getNode() != null) {
return this.mainNode.getNode().getOwningPlayerProfileId();
}
return null;
}
}

View File

@ -11,6 +11,7 @@ import com.extendedae_plus.init.ModItems;
import com.extendedae_plus.items.materials.ChannelCardItem;
import com.extendedae_plus.util.ExtendedAELogger;
import net.minecraft.nbt.CompoundTag;
import java.util.UUID;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
@ -29,6 +30,9 @@ public abstract class IOBusPartChannelCardMixin implements InterfaceWirelessLink
@Unique
private long eap$lastChannel = -1;
@Unique
private UUID eap$lastOwner;
@Unique
private boolean eap$clientConnected = false;
@ -94,16 +98,23 @@ public abstract class IOBusPartChannelCardMixin implements InterfaceWirelessLink
IUpgradeInventory inv = this.getUpgrades();
long channel = 0L;
boolean found = false;
UUID owner = null;
for (var stack : inv) {
if (!stack.isEmpty() && stack.getItem() == ModItems.CHANNEL_CARD.get()) {
channel = ChannelCardItem.getChannel(stack);
owner = ChannelCardItem.getOwnerUUID(stack);
if (owner == null) {
owner = this.eap$getFallbackOwner();
}
found = true;
break;
}
}
// 频道没有变化则跳过
if (this.eap$lastChannel == channel) {
boolean sameOwner = (this.eap$lastOwner == null && owner == null)
|| (this.eap$lastOwner != null && this.eap$lastOwner.equals(owner));
if (this.eap$link != null && this.eap$lastChannel == channel && sameOwner) {
return;
}
this.eap$lastChannel = channel;
@ -112,11 +123,14 @@ public abstract class IOBusPartChannelCardMixin implements InterfaceWirelessLink
if (!found) {
// 无频道卡则断开
if (this.eap$link != null) {
this.eap$link.setPlacerId(null);
this.eap$link.setFrequency(0L);
this.eap$link.updateStatus();
// 立即通知客户端状态变化断开连接无需延迟
((appeng.parts.AEBasePart)(Object)this).getHost().markForUpdate();
}
this.eap$lastChannel = 0L;
this.eap$lastOwner = null;
return;
}
@ -128,8 +142,10 @@ public abstract class IOBusPartChannelCardMixin implements InterfaceWirelessLink
this.eap$link = new WirelessSlaveLink(endpoint);
}
this.eap$link.setPlacerId(owner);
this.eap$link.setFrequency(channel);
this.eap$link.updateStatus();
this.eap$lastOwner = owner;
// 通知客户端状态变化
((appeng.parts.AEBasePart)(Object)this).getHost().markForUpdate();
@ -137,4 +153,15 @@ public abstract class IOBusPartChannelCardMixin implements InterfaceWirelessLink
ExtendedAELogger.LOGGER.error("[服务端] IOBus 初始化频道链接失败", e);
}
}
@Unique
private UUID eap$getFallbackOwner() {
try {
var node = ((IActionHost)(Object)this).getActionableNode();
if (node != null) {
return node.getOwningPlayerProfileId();
}
} catch (Throwable ignored) {}
return null;
}
}

View File

@ -12,6 +12,7 @@ import com.extendedae_plus.init.ModItems;
import com.extendedae_plus.items.materials.ChannelCardItem;
import com.extendedae_plus.util.ExtendedAELogger;
import net.minecraft.nbt.CompoundTag;
import java.util.UUID;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
@ -30,6 +31,9 @@ public abstract class StorageBusPartChannelCardMixin implements InterfaceWireles
@Unique
private long eap$lastChannel = -1;
@Unique
private UUID eap$lastOwner;
@Unique
private boolean eap$clientConnected = false;
@ -92,16 +96,23 @@ public abstract class StorageBusPartChannelCardMixin implements InterfaceWireles
IUpgradeInventory inv = this.getUpgrades();
long channel = 0L;
boolean found = false;
UUID owner = null;
for (var stack : inv) {
if (!stack.isEmpty() && stack.getItem() == ModItems.CHANNEL_CARD.get()) {
channel = ChannelCardItem.getChannel(stack);
owner = ChannelCardItem.getOwnerUUID(stack);
if (owner == null) {
owner = this.eap$getFallbackOwner();
}
found = true;
break;
}
}
// 频道没有变化则跳过
if (this.eap$lastChannel == channel) {
boolean sameOwner = (this.eap$lastOwner == null && owner == null)
|| (this.eap$lastOwner != null && this.eap$lastOwner.equals(owner));
if (this.eap$link != null && this.eap$lastChannel == channel && sameOwner) {
return;
}
this.eap$lastChannel = channel;
@ -109,11 +120,14 @@ public abstract class StorageBusPartChannelCardMixin implements InterfaceWireles
if (!found) {
if (this.eap$link != null) {
this.eap$link.setPlacerId(null);
this.eap$link.setFrequency(0L);
this.eap$link.updateStatus();
// 通知客户端状态变化
((appeng.parts.AEBasePart)(Object)this).getHost().markForUpdate();
}
this.eap$lastChannel = 0L;
this.eap$lastOwner = null;
return;
}
@ -125,8 +139,10 @@ public abstract class StorageBusPartChannelCardMixin implements InterfaceWireles
this.eap$link = new WirelessSlaveLink(endpoint);
}
this.eap$link.setPlacerId(owner);
this.eap$link.setFrequency(channel);
this.eap$link.updateStatus();
this.eap$lastOwner = owner;
// 通知客户端状态变化
((appeng.parts.AEBasePart)(Object)this).getHost().markForUpdate();
@ -134,4 +150,15 @@ public abstract class StorageBusPartChannelCardMixin implements InterfaceWireles
ExtendedAELogger.LOGGER.error("[服务端] StorageBus 初始化频道链接失败", e);
}
}
@Unique
private UUID eap$getFallbackOwner() {
try {
var node = ((IActionHost)(Object)this).getActionableNode();
if (node != null) {
return node.getOwningPlayerProfileId();
}
} catch (Throwable ignored) {}
return null;
}
}