use of com.lowdragmc.lowdraglib.utils.BlockInfo in project Multiblocked by Low-Drag-MC.
the class TemplateBuilderWidget method getComponentDefinition.
public static ComponentDefinition getComponentDefinition(ComponentDefinition definition, Set<BlockInfo> candidates) {
if (candidates.size() == 1) {
definition = new PartDefinition(new ResourceLocation(Multiblocked.MODID, "i_renderer"));
definition.baseRenderer = new MBDBlockStateRenderer(candidates.toArray(new BlockInfo[0])[0]);
} else if (!candidates.isEmpty()) {
definition = new PartDefinition(new ResourceLocation(Multiblocked.MODID, "i_renderer"));
definition.baseRenderer = new CycleBlockStateRenderer(candidates.toArray(new BlockInfo[0]));
}
return definition;
}
use of com.lowdragmc.lowdraglib.utils.BlockInfo in project Multiblocked by Low-Drag-MC.
the class PatternWidget method gatherBlockDrops.
private Map<ItemStackKey, PartInfo> gatherBlockDrops(Map<BlockPos, BlockInfo> blocks) {
Map<ItemStackKey, PartInfo> partsMap = new HashMap<>();
for (Map.Entry<BlockPos, BlockInfo> entry : blocks.entrySet()) {
BlockPos pos = entry.getKey();
BlockState blockState = ((World) PatternWidget.world).getBlockState(pos);
ItemStack itemStack = blockState.getBlock().getPickBlock(blockState, BlockRayTraceResult.miss(new Vector3d(0.5, 1, 0.5).add(pos.getX(), pos.getY(), pos.getZ()), Direction.UP, pos), PatternWidget.world, pos, Minecraft.getInstance().player);
ItemStackKey itemStackKey = new ItemStackKey(itemStack);
PartInfo partInfo = partsMap.get(itemStackKey);
if (partInfo == null) {
partInfo = new PartInfo(itemStackKey, entry.getValue());
partsMap.put(itemStackKey, partInfo);
}
++partInfo.amount;
}
return partsMap;
}
use of com.lowdragmc.lowdraglib.utils.BlockInfo in project Multiblocked by Low-Drag-MC.
the class PatternWidget method initializePattern.
private MBPattern initializePattern(MultiblockShapeInfo shapeInfo, HashSet<ItemStackKey> blockDrops) {
Map<BlockPos, BlockInfo> blockMap = new HashMap<>();
ControllerTileEntity controllerBase = null;
BlockPos multiPos = locateNextRegion(500);
BlockInfo[][][] blocks = shapeInfo.getBlocks();
for (int x = 0; x < blocks.length; x++) {
BlockInfo[][] aisle = blocks[x];
for (int y = 0; y < aisle.length; y++) {
BlockInfo[] column = aisle[y];
for (int z = 0; z < column.length; z++) {
TileEntity tileEntity = column[z].getTileEntity();
BlockState blockState = column[z].getBlockState();
if (tileEntity == null && blockState.getBlock().hasTileEntity(blockState)) {
tileEntity = blockState.getBlock().createTileEntity(blockState, world);
}
if (tileEntity instanceof ControllerTileEntity) {
controllerBase = (ControllerTileEntity) tileEntity;
}
blockMap.put(multiPos.offset(x, y, z), new BlockInfo(blockState, tileEntity));
}
}
}
world.addBlocks(blockMap);
Map<ItemStackKey, PartInfo> parts = gatherBlockDrops(blockMap);
blockDrops.addAll(parts.keySet());
Map<BlockPos, TraceabilityPredicate> predicateMap = new HashMap<>();
if (controllerBase != null) {
loadControllerFormed(predicateMap.keySet(), controllerBase);
predicateMap = controllerBase.state.getMatchContext().get("predicates");
}
return controllerBase == null ? null : new MBPattern(blockMap, parts.values().stream().sorted((one, two) -> {
if (one.isController)
return -1;
if (two.isController)
return +1;
if (one.isTile && !two.isTile)
return -1;
if (two.isTile && !one.isTile)
return +1;
if (one.blockId != two.blockId)
return two.blockId - one.blockId;
return two.amount - one.amount;
}).map(PartInfo::getItemStack).toArray(ItemStack[]::new), predicateMap, controllerBase);
}
use of com.lowdragmc.lowdraglib.utils.BlockInfo in project Multiblocked by Low-Drag-MC.
the class PredicateComponent method buildPredicate.
@Override
public SimplePredicate buildPredicate() {
predicate = state -> {
TileEntity tileEntity = state.getTileEntity();
if (tileEntity instanceof IComponent) {
return ((IComponent) tileEntity).getDefinition().location.equals(location);
}
return false;
};
candidates = () -> {
if (MbdComponents.COMPONENT_BLOCKS_REGISTRY.containsKey(location)) {
return new BlockInfo[] { new BlockInfo(MbdComponents.COMPONENT_BLOCKS_REGISTRY.get(location).defaultBlockState(), MbdComponents.DEFINITION_REGISTRY.get(location).createNewTileEntity()) };
} else {
if (definition == null)
return new BlockInfo[0];
if (definition instanceof ControllerDefinition) {
ControllerTileTesterEntity te = new ControllerTileTesterEntity(ControllerTileTesterEntity.DEFAULT_DEFINITION);
te.setDefinition((ControllerDefinition) definition);
return new BlockInfo[] { new BlockInfo(MbdComponents.COMPONENT_BLOCKS_REGISTRY.get(ControllerTileTesterEntity.DEFAULT_DEFINITION.location).defaultBlockState(), te) };
} else {
DummyComponentTileEntity te = new DummyComponentTileEntity(MbdComponents.DummyComponentBlock.definition);
te.setDefinition(definition);
return new BlockInfo[] { new BlockInfo(MbdComponents.DummyComponentBlock.defaultBlockState(), te) };
}
}
};
return this;
}
use of com.lowdragmc.lowdraglib.utils.BlockInfo in project Multiblocked by Low-Drag-MC.
the class BlockPattern method autoBuild.
public void autoBuild(PlayerEntity player, MultiblockState worldState) {
World world = player.level;
int minZ = -centerOffset[4];
worldState.clean();
ControllerTileEntity controller = worldState.getController();
BlockPos centerPos = controller.getBlockPos();
Direction facing = controller.getFrontFacing();
Map<SimplePredicate, Integer> cacheGlobal = worldState.globalCount;
Map<BlockPos, Object> blocks = new HashMap<>();
blocks.put(centerPos, controller);
for (int c = 0, z = minZ++, r; c < this.fingerLength; c++) {
for (r = 0; r < aisleRepetitions[c][0]; r++) {
for (int b = 0, y = -centerOffset[1]; b < this.thumbLength; b++, y++) {
for (int a = 0, x = -centerOffset[0]; a < this.palmLength; a++, x++) {
TraceabilityPredicate predicate = this.blockMatches[c][b][a];
BlockPos pos = setActualRelativeOffset(x, y, z, facing).offset(centerPos.getX(), centerPos.getY(), centerPos.getZ());
worldState.update(pos, predicate);
if (!world.isEmptyBlock(pos)) {
blocks.put(pos, world.getBlockState(pos));
for (SimplePredicate limit : predicate.limited) {
limit.testLimited(worldState);
}
} else {
boolean find = false;
BlockInfo[] infos = new BlockInfo[0];
for (SimplePredicate limit : predicate.limited) {
if (limit.minCount > 0) {
if (!cacheGlobal.containsKey(limit)) {
cacheGlobal.put(limit, 1);
} else if (cacheGlobal.get(limit) < limit.minCount && (limit.maxCount == -1 || cacheGlobal.get(limit) < limit.maxCount)) {
cacheGlobal.put(limit, cacheGlobal.get(limit) + 1);
} else {
continue;
}
} else {
continue;
}
infos = limit.candidates == null ? null : limit.candidates.get();
find = true;
break;
}
if (!find) {
// no limited
for (SimplePredicate limit : predicate.limited) {
if (limit.maxCount != -1 && cacheGlobal.getOrDefault(limit, Integer.MAX_VALUE) == limit.maxCount)
continue;
if (cacheGlobal.containsKey(limit)) {
cacheGlobal.put(limit, cacheGlobal.get(limit) + 1);
} else {
cacheGlobal.put(limit, 1);
}
infos = ArrayUtils.addAll(infos, limit.candidates == null ? null : limit.candidates.get());
}
for (SimplePredicate common : predicate.common) {
infos = ArrayUtils.addAll(infos, common.candidates == null ? null : common.candidates.get());
}
}
List<ItemStack> candidates = new ArrayList<>();
if (infos != null) {
for (BlockInfo info : infos) {
if (info.getBlockState().getBlock() != Blocks.AIR) {
BlockState blockState = info.getBlockState();
if (blockState.getBlock() instanceof BlockComponent && ((BlockComponent) blockState.getBlock()).definition != null) {
if (((BlockComponent) blockState.getBlock()).definition.baseRenderer instanceof CycleBlockStateRenderer) {
CycleBlockStateRenderer renderer = (CycleBlockStateRenderer) ((BlockComponent) blockState.getBlock()).definition.baseRenderer;
for (BlockInfo blockInfo : renderer.blockInfos) {
candidates.add(blockInfo.getItemStackForm());
}
} else {
candidates.add(info.getItemStackForm());
}
} else {
candidates.add(info.getItemStackForm());
}
}
}
}
// check inventory
ItemStack found = null;
if (!player.isCreative()) {
for (ItemStack itemStack : player.inventory.items) {
if (candidates.stream().anyMatch(candidate -> candidate.equals(itemStack, false)) && !itemStack.isEmpty() && itemStack.getItem() instanceof BlockItem) {
found = itemStack.copy();
itemStack.setCount(itemStack.getCount() - 1);
break;
}
}
} else {
for (ItemStack candidate : candidates) {
found = candidate.copy();
if (!found.isEmpty() && found.getItem() instanceof BlockItem) {
break;
}
found = null;
}
}
if (found == null)
continue;
BlockItem itemBlock = (BlockItem) found.getItem();
BlockItemUseContext context = new BlockItemUseContext(world, player, Hand.MAIN_HAND, found, BlockRayTraceResult.miss(player.getEyePosition(0), Direction.UP, pos));
itemBlock.place(context);
TileEntity tileEntity = world.getBlockEntity(pos);
if (tileEntity instanceof ComponentTileEntity) {
blocks.put(pos, tileEntity);
} else {
blocks.put(pos, world.getBlockState(pos));
}
}
}
}
z++;
}
}
Direction frontFacing = controller.getFrontFacing();
blocks.forEach((pos, block) -> {
// adjust facing
if (block instanceof BlockState) {
resetFacing(pos, (BlockState) block, frontFacing, (p, f) -> {
Object object = blocks.get(p.relative(f));
return object == null || (object instanceof BlockState && ((BlockState) object).getBlock() == Blocks.AIR);
}, state -> world.setBlock(pos, state, 3));
} else if (block instanceof ComponentTileEntity) {
resetFacing(pos, ((ComponentTileEntity<?>) block).getBlockState(), frontFacing, (p, f) -> {
Object object = blocks.get(p.relative(f));
if (object == null || (object instanceof BlockState && ((BlockState) object).getBlock() == Blocks.AIR)) {
return ((ComponentTileEntity<?>) block).isValidFrontFacing(f);
}
return false;
}, state -> world.setBlock(pos, state, 3));
}
});
}
Aggregations