Skip to content

Commit b2400be

Browse files
authored
Merge pull request #188 from Circulate233/BlockPos
添加了getBlockPosInPattern方法用于捕获某种方块在结构中的所有位置
2 parents 85028a5 + 7c53dbb commit b2400be

File tree

5 files changed

+107
-7
lines changed

5 files changed

+107
-7
lines changed

src/main/java/github/kasuminova/mmce/common/helper/IMachineController.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,50 @@ public interface IMachineController {
308308
@ZenMethod
309309
int getBlocksInPattern(final IBlockStatePredicate predicate);
310310

311+
/**
312+
* 获取控制器的结构中指定方块的所有坐标,只能在控制器成型时使用。<br/>
313+
* 会过滤空气方块。<br/>
314+
* 注意:此操作的性能开销稍大,通常情况下只建议在 {@link MachineStructureUpdateEvent} 事件触发时使用。
315+
*
316+
* @param blockStack 要判断的方块对应的物品,会被自动转换成对应的 IBlockState,如果转换出现问题会输出错误日志。
317+
* @return 所有位置。
318+
*/
319+
@ZenMethod
320+
IBlockPos[] getBlockPosInPattern(final IItemStack blockStack);
321+
322+
/**
323+
* 获取控制器的结构中指定方块的所有坐标,只能在控制器成型时使用。<br/>
324+
* 会过滤空气方块。<br/>
325+
* 注意:此操作的<strong>性能开销稍大</strong>,通常情况下只建议在 {@link MachineStructureUpdateEvent} 事件触发时使用。
326+
*
327+
* @param blockStateMatcher 要判断的方块对应的 IBlockStateMatcher
328+
* @return 所有位置。
329+
*/
330+
@ZenMethod
331+
IBlockPos[] getBlockPosInPattern(final IBlockStateMatcher blockStateMatcher);
332+
333+
/**
334+
* 获取控制器的结构中指定方块的所有坐标,只能在控制器成型时使用。<br/>
335+
* 会过滤空气方块。<br/>
336+
* 注意:此操作的<strong>性能开销稍大</strong>,通常情况下只建议在 {@link MachineStructureUpdateEvent} 事件触发时使用。
337+
*
338+
* @param blockName 要判断的方块对应的注册名,解析方式参考机械 JSON 文件的格式。
339+
* @return 所有位置。
340+
*/
341+
@ZenMethod
342+
IBlockPos[] getBlockPosInPattern(final String blockName);
343+
344+
/**
345+
* 获取控制器的结构中指定方块的所有坐标,只能在控制器成型时使用。<br/>
346+
* 会过滤空气方块。<br/>
347+
* 注意:此操作的<strong>性能开销稍大</strong>,通常情况下只建议在 {@link MachineStructureUpdateEvent} 事件触发时使用。
348+
*
349+
* @param predicate 自定义判断逻辑。
350+
* @return 所有位置。
351+
*/
352+
@ZenMethod
353+
IBlockPos[] getBlockPosInPattern(final IBlockStatePredicate predicate);
354+
311355
/**
312356
* 获取控制器的拥有者,如果玩家不在线则返回 null。
313357
*

src/main/java/hellfirepvp/modularmachinery/common/machine/DynamicMachine.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ public Map<String, SmartInterfaceType> getFilteredType(Collection<String> ignore
139139
return filtered;
140140
}
141141

142-
@SuppressWarnings("unchecked")
143142
public <H extends MachineEvent> void addMachineEventHandler(Class<H> hClass, IEventHandler<H> handler) {
144143
machineEventHandlers.putIfAbsent(hClass, new ArrayList<>());
145144
machineEventHandlers.get(hClass).add((IEventHandler<MachineEvent>) handler);

src/main/java/hellfirepvp/modularmachinery/common/tiles/TileFactoryController.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public TileFactoryController(IBlockState state) {
6666
controllerRotation = state.getValue(BlockController.FACING);
6767
parentMachine = parentController.getParentMachine();
6868
} else {
69-
ModularMachinery.log.warn("Invalid factory controller block at " + getPos() + " !");
69+
ModularMachinery.log.warn("Invalid factory controller block at {} !", getPos());
7070
controllerRotation = EnumFacing.NORTH;
7171
}
7272
}
@@ -569,7 +569,7 @@ protected void checkRotation() {
569569
this.parentMachine = parentController.getParentMachine();
570570
this.controllerRotation = state.getValue(BlockController.FACING);
571571
} else {
572-
ModularMachinery.log.warn("Invalid factory controller block at " + getPos() + " !");
572+
ModularMachinery.log.warn("Invalid factory controller block at {} !", getPos());
573573
controllerRotation = EnumFacing.NORTH;
574574
}
575575
}
@@ -628,7 +628,7 @@ protected void readMachineNBT(NBTTagCompound compound) {
628628
if (parentMachine != null) {
629629
parentController = BlockFactoryController.FACTORY_CONTROLLERS.get(parentMachine);
630630
} else {
631-
ModularMachinery.log.info("Couldn't find machine named " + rl + " for controller at " + getPos());
631+
ModularMachinery.log.info("Couldn't find machine named {} for controller at {}", rl, getPos());
632632
}
633633
}
634634
super.readMachineNBT(compound);

src/main/java/hellfirepvp/modularmachinery/common/tiles/TileMachineController.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public TileMachineController(IBlockState state) {
5656
this.controllerRotation = state.getValue(BlockController.FACING);
5757
} else {
5858
// wtf, where is the controller?
59-
ModularMachinery.log.warn("Invalid controller block at " + getPos() + " !");
59+
ModularMachinery.log.warn("Invalid controller block at {} !", getPos());
6060
controllerRotation = EnumFacing.NORTH;
6161
}
6262
}
@@ -244,7 +244,7 @@ protected void checkRotation() {
244244
this.controllerRotation = state.getValue(BlockController.FACING);
245245
} else {
246246
// wtf, where is the controller?
247-
ModularMachinery.log.warn("Invalid controller block at " + getPos() + " !");
247+
ModularMachinery.log.warn("Invalid controller block at {} !", getPos());
248248
controllerRotation = EnumFacing.NORTH;
249249
}
250250
}
@@ -406,7 +406,7 @@ protected void readMachineNBT(NBTTagCompound compound) {
406406
if (parentMachine != null) {
407407
parentController = BlockController.MACHINE_CONTROLLERS.get(parentMachine);
408408
} else {
409-
ModularMachinery.log.info("Couldn't find machine named " + rl + " for controller at " + getPos());
409+
ModularMachinery.log.info("Couldn't find machine named {} for controller at {}", rl, getPos());
410410
}
411411
}
412412

src/main/java/hellfirepvp/modularmachinery/common/tiles/base/TileMultiblockMachineController.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import hellfirepvp.modularmachinery.common.util.MiscUtils;
6262
import hellfirepvp.modularmachinery.common.util.SmartInterfaceData;
6363
import hellfirepvp.modularmachinery.common.util.SmartInterfaceType;
64+
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
6465
import net.minecraft.block.Block;
6566
import net.minecraft.block.state.IBlockState;
6667
import net.minecraft.entity.player.EntityPlayerMP;
@@ -1173,6 +1174,44 @@ public int getBlocksInPattern(final IBlockStatePredicate predicate) {
11731174
return getBlocksInPatternInternal(state -> predicate.test(CraftTweakerMC.getBlockState(state)));
11741175
}
11751176

1177+
@Override
1178+
public IBlockPos[] getBlockPosInPattern(IItemStack blockStack) {
1179+
if (foundPattern == null || blockStack == null) {
1180+
return new IBlockPos[0];
1181+
}
1182+
IBlockDefinition blockDef = blockStack.asBlock().getDefinition();
1183+
if (blockStack.getMetadata() == OreDictionary.WILDCARD_VALUE) {
1184+
return getBlockPosInPattern(blockDef.getDefaultState().matchBlock());
1185+
} else {
1186+
return getBlockPosInPattern(blockDef.getStateFromMeta(blockStack.getMetadata()));
1187+
}
1188+
}
1189+
1190+
@Override
1191+
public IBlockPos[] getBlockPosInPattern(IBlockStateMatcher blockStateMatcher) {
1192+
if (foundPattern == null) {
1193+
return new IBlockPos[0];
1194+
}
1195+
return getBlockPossInPatternInternal(state -> blockStateMatcher.matches(CraftTweakerMC.getBlockState(state)));
1196+
}
1197+
1198+
@Override
1199+
public IBlockPos[] getBlockPosInPattern(String blockName) {
1200+
if (foundPattern == null) {
1201+
return new IBlockPos[0];
1202+
}
1203+
List<IBlockState> applicable = BlockArray.BlockInformation.getDescriptor(blockName).getApplicable();
1204+
return getBlockPossInPatternInternal(applicable::contains);
1205+
}
1206+
1207+
@Override
1208+
public IBlockPos[] getBlockPosInPattern(IBlockStatePredicate predicate) {
1209+
if (foundPattern == null) {
1210+
return new IBlockPos[0];
1211+
}
1212+
return getBlockPossInPatternInternal(state -> predicate.test(CraftTweakerMC.getBlockState(state)));
1213+
}
1214+
11761215
public int getBlocksInPatternInternal(final Predicate<IBlockState> predicate) {
11771216
if (foundPattern == null) {
11781217
return 0;
@@ -1191,6 +1230,24 @@ public int getBlocksInPatternInternal(final Predicate<IBlockState> predicate) {
11911230
return count;
11921231
}
11931232

1233+
public IBlockPos[] getBlockPossInPatternInternal(final Predicate<IBlockState> predicate) {
1234+
if (foundPattern == null) {
1235+
return new IBlockPos[0];
1236+
}
1237+
List<IBlockPos> poss = new ObjectArrayList<>();
1238+
for (final BlockPos pos : foundPattern.getPattern().keySet()) {
1239+
BlockPos realPos = getPos().add(pos.getX(), pos.getY(), pos.getZ());
1240+
IBlockState state = getWorld().getBlockState(realPos);
1241+
if (state.getBlock() == Blocks.AIR) {
1242+
continue;
1243+
}
1244+
if (predicate.test(state)) {
1245+
poss.add(CraftTweakerMC.getIBlockPos(pos));
1246+
}
1247+
}
1248+
return poss.toArray(new IBlockPos[0]);
1249+
}
1250+
11941251
@Nullable
11951252
@Override
11961253
@SuppressWarnings("ConstantValue")

0 commit comments

Comments
 (0)