Skip to content

Commit 2447be4

Browse files
committed
能源节点链接黑名单化,允许通用节点黑名单能源使得部分能源必须使用专门的白名单节点运行
1 parent 1bfad1b commit 2447be4

6 files changed

Lines changed: 70 additions & 30 deletions

File tree

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,4 @@
88
- 交互速率可以显示为所有的注册单位
99

1010
## TODO
11-
- 节点应该允许具有独立黑白名单,用于专门的设备链接
12-
- 能源节点链接黑名单化,允许通用节点黑名单能源使得部分能源必须使用专门的白名单节点运行
1311
- 允许将其他mod的TileEntity设置为节点

src/main/java/com/circulation/circulation_networks/CFNConfig.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,20 @@ public final class CFNConfig {
2424
})
2525
public static String[] classNames = new String[]{"sonar.fluxnetworks.common.tileentity.TileFluxCore"};
2626

27+
@Config.Name("EnergySupplyBlacklist")
28+
@Config.Comment({
29+
"通用能源供应节点的能源设备黑名单(完全限定名或类名前缀)",
30+
"被匹配的能源设备只能由专用节点(覆写了 isBlacklisted 方法的节点)建立连接,",
31+
"普通供应节点对这些设备无效。",
32+
"",
33+
"Energy device blacklist for generic supply nodes (non-specialized).",
34+
"Matched devices can ONLY be connected by specialized nodes that override isBlacklisted.",
35+
"Examples:",
36+
" - 'com.example.AdvancedEnergyTile' Exact match",
37+
" - 'com.example.advanced' Prefix match"
38+
})
39+
public static String[] supplyClassNames = new String[]{};
40+
2741
public static class Node {
2842

2943
@Config.Name("EnergyInductionTower")
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.circulation.circulation_networks.api.node;
22

3+
import com.circulation.circulation_networks.registry.RegistryEnergyHandler;
4+
import net.minecraft.tileentity.TileEntity;
35
import net.minecraft.util.math.BlockPos;
46

57
/**
@@ -9,16 +11,13 @@ public interface IEnergySupplyNode extends INode {
911

1012
double getEnergyScope();
1113

12-
/**
13-
* 返回能量范围的平方,用于距离检测。<br>
14-
* 实现类应缓存此值以避免每次调用时重复乘法运算。
15-
*/
16-
default double getEnergyScopeSq() {
17-
double s = getEnergyScope();
18-
return s * s;
19-
}
14+
double getEnergyScopeSq();
2015

2116
default boolean supplyScopeCheck(BlockPos pos) {
2217
return this.distanceSq(pos) <= getEnergyScopeSq();
2318
}
19+
20+
default boolean isBlacklisted(TileEntity tileEntity) {
21+
return RegistryEnergyHandler.isSupplyBlack(tileEntity);
22+
}
2423
}

src/main/java/com/circulation/circulation_networks/manager/EnergyMachineManager.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ public void addMachine(TileEntity tileEntity) {
221221
if (s == null) s = new ReferenceOpenHashSet<>();
222222
for (var node : set) {
223223
if (!node.supplyScopeCheck(pos)) continue;
224+
if (node.isBlacklisted(tileEntity)) continue;
224225

225226
var set1 = gridMachineMap.get(node);
226227
if (set1 == gridMachineMap.defaultReturnValue()) {
@@ -319,6 +320,7 @@ public void addNode(INode node) {
319320
for (TileEntity tileEntity : chunk.getTileEntityMap().values()) {
320321
if (!energySupplyNode.supplyScopeCheck(tileEntity.getPos())) continue;
321322
if (RegistryEnergyHandler.isBlack(tileEntity)) continue;
323+
if (energySupplyNode.isBlacklisted(tileEntity)) continue;
322324
if (RegistryEnergyHandler.isEnergyTileEntity(tileEntity)) {
323325
if (set2 == gridMachineMap.defaultReturnValue()) {
324326
gridMachineMap.put(energySupplyNode, set2 = Collections.newSetFromMap(new WeakHashMap<>()));

src/main/java/com/circulation/circulation_networks/network/nodes/machine_node/MachineNode.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,33 @@
1111
public abstract class MachineNode extends Node implements IMachineNode {
1212

1313
protected final double energyScope;
14+
protected final double energyScopeSq;
1415
@Getter
1516
@Setter
1617
private long maxEnergy;
1718

1819
public MachineNode(NBTTagCompound compound) {
1920
super(compound);
2021
this.energyScope = compound.getDouble("energyScope");
22+
energyScopeSq = energyScope * energyScope;
2123
}
2224

2325
public MachineNode(IMachineNodeTileEntity tileEntity, double energyScope, double linkScope) {
2426
super(tileEntity,linkScope);
2527
this.energyScope = energyScope;
28+
energyScopeSq = energyScope * energyScope;
2629
}
2730

2831
@Override
2932
public double getEnergyScope() {
3033
return energyScope;
3134
}
3235

36+
@Override
37+
public double getEnergyScopeSq() {
38+
return energyScopeSq;
39+
}
40+
3341
@Override
3442
public NBTTagCompound serialize() {
3543
var nbt = super.serialize();

src/main/java/com/circulation/circulation_networks/registry/RegistryEnergyHandler.java

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
public final class RegistryEnergyHandler {
2121

2222
private static Class<?>[] blackListClass;
23+
private static Class<?>[] supplyBlackListClass;
2324
private static List<IEnergyHandlerManager> list = new ObjectArrayList<>();
2425

2526
/**
@@ -39,6 +40,14 @@ public static boolean isBlack(TileEntity tileEntity) {
3940
return false;
4041
}
4142

43+
public static boolean isSupplyBlack(TileEntity tileEntity) {
44+
if (supplyBlackListClass == null) return false;
45+
for (Class<?> listClass : supplyBlackListClass) {
46+
if (listClass.isInstance(tileEntity)) return true;
47+
}
48+
return false;
49+
}
50+
4251
public static boolean isEnergyItemStack(ItemStack stack) {
4352
if (stack.isEmpty()) return false;
4453
for (IEnergyHandlerManager manager : list) {
@@ -72,34 +81,44 @@ public static void lock() {
7281
list.sort(Comparator.reverseOrder());
7382
list = ImmutableList.copyOf(list);
7483

75-
if (CFNConfig.classNames != null && CFNConfig.classNames.length > 0) {
76-
final List<String> blackNameList = new ObjectArrayList<>();
77-
final ReferenceSet<Class<?>> blackListClass = new ReferenceOpenHashSet<>();
78-
79-
for (String className : CFNConfig.classNames) {
80-
if (className == null || className.trim().isEmpty()) continue;
81-
className = className.trim();
84+
final List<String> blackPrefixes = new ObjectArrayList<>();
85+
final List<String> supplyPrefixes = new ObjectArrayList<>();
86+
final ReferenceSet<Class<?>> blackSet = new ReferenceOpenHashSet<>();
87+
final ReferenceSet<Class<?>> supplySet = new ReferenceOpenHashSet<>();
8288

83-
try {
84-
Class<?> cls = Class.forName(className);
85-
blackListClass.add(cls);
86-
} catch (ClassNotFoundException e) {
87-
blackNameList.add(className);
88-
}
89-
}
89+
collectExactClasses(CFNConfig.classNames, blackSet, blackPrefixes);
90+
collectExactClasses(CFNConfig.supplyClassNames, supplySet, supplyPrefixes);
9091

92+
if (!blackPrefixes.isEmpty() || !supplyPrefixes.isEmpty()) {
9193
for (var aClass : TileEntity.REGISTRY) {
92-
if (blackListClass.contains(aClass)) continue;
9394
var className = aClass.getName();
94-
for (String s : blackNameList) {
95-
if (className.startsWith(s)) {
96-
blackListClass.add(aClass);
97-
break;
95+
if (!blackPrefixes.isEmpty() && !blackSet.contains(aClass)) {
96+
for (String s : blackPrefixes) {
97+
if (className.startsWith(s)) { blackSet.add(aClass); break; }
98+
}
99+
}
100+
if (!supplyPrefixes.isEmpty() && !supplySet.contains(aClass)) {
101+
for (String s : supplyPrefixes) {
102+
if (className.startsWith(s)) { supplySet.add(aClass); break; }
98103
}
99104
}
100105
}
106+
}
101107

102-
RegistryEnergyHandler.blackListClass = blackListClass.toArray(new Class[0]);
108+
RegistryEnergyHandler.blackListClass = blackSet.isEmpty() ? null : blackSet.toArray(new Class[0]);
109+
RegistryEnergyHandler.supplyBlackListClass = supplySet.isEmpty() ? null : supplySet.toArray(new Class[0]);
110+
}
111+
112+
private static void collectExactClasses(String[] names, ReferenceSet<Class<?>> set, List<String> prefixes) {
113+
if (names == null) return;
114+
for (String className : names) {
115+
if (className == null || className.trim().isEmpty()) continue;
116+
className = className.trim();
117+
try {
118+
set.add(Class.forName(className));
119+
} catch (ClassNotFoundException e) {
120+
prefixes.add(className);
121+
}
103122
}
104123
}
105124

0 commit comments

Comments
 (0)