use of com.lowdragmc.multiblocked.api.capability.MultiblockCapability in project Multiblocked by Low-Drag-MC.
the class PredicateAnyCapability method getConfigWidget.
@Override
public List<WidgetGroup> getConfigWidget(List<WidgetGroup> groups) {
super.getConfigWidget(groups);
WidgetGroup group = new WidgetGroup(0, 0, 100, 20);
groups.add(group);
MultiblockCapability<?> current = MbdCapabilities.get(capability);
group.addWidget(new SelectorWidget(0, 0, 120, 20, MbdCapabilities.CAPABILITY_REGISTRY.values().stream().map(MultiblockCapability::getUnlocalizedName).collect(Collectors.toList()), -1).setValue(current == null ? "" : current.getUnlocalizedName()).setOnChanged(capability -> {
this.capability = capability.replace("multiblocked.capability.", "");
buildPredicate();
}).setButtonBackground(ResourceBorderTexture.BUTTON_COMMON).setBackground(new ColorRectTexture(0xffaaaaaa)).setHoverTooltips("multiblocked.gui.predicate.capability"));
return groups;
}
use of com.lowdragmc.multiblocked.api.capability.MultiblockCapability in project Multiblocked by Low-Drag-MC.
the class ComponentTileEntity method load.
@Override
public void load(@Nonnull BlockState blockState, @Nonnull CompoundNBT compound) {
super.load(blockState, compound);
if (needAlwaysUpdate()) {
MultiblockWorldSavedData.getOrCreate(level).addLoading(this);
}
if (compound.contains("owner")) {
this.owner = compound.getUUID("owner");
}
CompoundNBT traitTag = compound.getCompound("trait");
for (Map.Entry<MultiblockCapability<?>, CapabilityTrait> entry : traits.entrySet()) {
entry.getValue().readFromNBT(traitTag.getCompound(entry.getKey().name));
}
if (compound.contains("persisted")) {
persistedData = compound.get("persisted");
}
}
use of com.lowdragmc.multiblocked.api.capability.MultiblockCapability in project Multiblocked by Low-Drag-MC.
the class ControllerTileEntity method save.
@Nonnull
@Override
public CompoundNBT save(@Nonnull CompoundNBT compound) {
super.save(compound);
if (!asyncRecipeSearching) {
compound.putBoolean("ars", false);
}
if (recipeLogic != null)
compound.put("recipeLogic", recipeLogic.writeToNBT(new CompoundNBT()));
if (capabilities != null) {
ListNBT tagList = new ListNBT();
for (Table.Cell<IO, MultiblockCapability<?>, Long2ObjectOpenHashMap<CapabilityProxy<?>>> cell : capabilities.cellSet()) {
IO io = cell.getRowKey();
MultiblockCapability<?> cap = cell.getColumnKey();
Long2ObjectOpenHashMap<CapabilityProxy<?>> value = cell.getValue();
if (io != null && cap != null && value != null) {
for (Map.Entry<Long, CapabilityProxy<?>> entry : value.entrySet()) {
CompoundNBT tag = new CompoundNBT();
tag.putInt("io", io.ordinal());
tag.putInt("facing", entry.getValue().facing.ordinal());
tag.putString("cap", cap.name);
tag.putLong("pos", entry.getKey());
tagList.add(tag);
}
}
}
compound.put("capabilities", tagList);
}
return compound;
}
use of com.lowdragmc.multiblocked.api.capability.MultiblockCapability in project Multiblocked by Low-Drag-MC.
the class ControllerTileEntity method onStructureFormed.
/**
* Called when its formed, server side only.
*/
public void onStructureFormed() {
if (recipeLogic == null) {
recipeLogic = new RecipeLogic(this);
}
if (status.equals("unformed")) {
setStatus("idle");
}
// init capabilities
Map<Long, EnumMap<IO, Set<MultiblockCapability<?>>>> capabilityMap = state.getMatchContext().get("capabilities");
if (capabilityMap != null) {
capabilities = Tables.newCustomTable(new EnumMap<>(IO.class), Object2ObjectOpenHashMap::new);
for (Map.Entry<Long, EnumMap<IO, Set<MultiblockCapability<?>>>> entry : capabilityMap.entrySet()) {
TileEntity tileEntity = level.getBlockEntity(BlockPos.of(entry.getKey()));
if (tileEntity != null) {
if (settings != null) {
Map<MultiblockCapability<?>, Tuple<IO, Direction>> caps = settings.get(entry.getKey());
if (caps != null) {
for (Map.Entry<MultiblockCapability<?>, Tuple<IO, Direction>> ioEntry : caps.entrySet()) {
MultiblockCapability<?> capability = ioEntry.getKey();
Tuple<IO, Direction> tuple = ioEntry.getValue();
if (tuple == null || capability == null)
continue;
IO io = tuple.getA();
Direction facing = tuple.getB();
if (capability.isBlockHasCapability(io, tileEntity)) {
if (!capabilities.contains(io, capability)) {
capabilities.put(io, capability, new Long2ObjectOpenHashMap<>());
}
CapabilityProxy<?> proxy = capability.createProxy(io, tileEntity);
proxy.facing = facing;
capabilities.get(io, capability).put(entry.getKey().longValue(), proxy);
}
}
}
} else {
entry.getValue().forEach((io, set) -> {
for (MultiblockCapability<?> capability : set) {
if (capability.isBlockHasCapability(io, tileEntity)) {
if (!capabilities.contains(io, capability)) {
capabilities.put(io, capability, new Long2ObjectOpenHashMap<>());
}
CapabilityProxy<?> proxy = capability.createProxy(io, tileEntity);
capabilities.get(io, capability).put(entry.getKey().longValue(), proxy);
}
}
});
}
}
}
}
settings = null;
// init parts
parts = state.getMatchContext().get("parts");
if (parts != null) {
for (Long pos : parts) {
TileEntity tileEntity = level.getBlockEntity(BlockPos.of(pos));
if (tileEntity instanceof IPartComponent) {
((IPartComponent) tileEntity).addedToController(this);
}
}
}
writeCustomData(-1, this::writeState);
if (Multiblocked.isKubeJSLoaded()) {
new StructureFormedEvent(this).post(ScriptType.SERVER, StructureFormedEvent.ID, getSubID());
}
}
use of com.lowdragmc.multiblocked.api.capability.MultiblockCapability in project Multiblocked by Low-Drag-MC.
the class Recipe method handleRecipe.
@SuppressWarnings("ALL")
public boolean handleRecipe(IO io, ICapabilityProxyHolder holder, ImmutableMap<MultiblockCapability<?>, ImmutableList<Tuple<Object, Float>>> contents) {
Table<IO, MultiblockCapability<?>, Long2ObjectOpenHashMap<CapabilityProxy<?>>> capabilityProxies = holder.getCapabilitiesProxy();
for (Map.Entry<MultiblockCapability<?>, ImmutableList<Tuple<Object, Float>>> entry : contents.entrySet()) {
Set<CapabilityProxy<?>> used = new HashSet<>();
List content = new ArrayList<>();
for (Tuple<Object, Float> tuple : entry.getValue()) {
if (tuple.getB() == 1 || Multiblocked.RNG.nextFloat() < tuple.getB()) {
// chance input
content.add(tuple.getA());
}
}
if (content.isEmpty())
continue;
if (capabilityProxies.contains(io, entry.getKey())) {
for (CapabilityProxy<?> proxy : capabilityProxies.get(io, entry.getKey()).values()) {
// search same io type
if (used.contains(proxy))
continue;
used.add(proxy);
content = proxy.handleRecipe(io, this, content);
if (content == null)
break;
}
}
if (content == null)
continue;
if (capabilityProxies.contains(IO.BOTH, entry.getKey())) {
for (CapabilityProxy<?> proxy : capabilityProxies.get(IO.BOTH, entry.getKey()).values()) {
// search both type
if (used.contains(proxy))
continue;
used.add(proxy);
content = proxy.handleRecipe(io, this, content);
if (content == null)
break;
}
}
if (content != null) {
Multiblocked.LOGGER.warn("io error while handling a recipe {} outputs. holder: {}", uid, holder);
return false;
}
}
return true;
}
Aggregations