use of com.lowdragmc.multiblocked.api.capability.proxy.CapabilityProxy 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.proxy.CapabilityProxy in project Multiblocked by Low-Drag-MC.
the class ControllerTileEntity method asyncThreadLogic.
@Override
public void asyncThreadLogic(long periodID) {
if (!isFormed() && getDefinition().catalyst == null && (getOffset() + periodID) % 4 == 0) {
BlockPattern pattern = getPattern();
if (pattern != null && pattern.checkPatternAt(new MultiblockState(level, worldPosition), false)) {
ServerLifecycleHooks.getCurrentServer().execute(() -> {
if (state == null)
state = new MultiblockState(level, worldPosition);
if (checkPattern()) {
// formed
MultiblockWorldSavedData.getOrCreate(level).addMapping(state);
onStructureFormed();
}
});
}
}
try {
if (hasProxies()) {
// should i add locks for proxies?
for (Long2ObjectOpenHashMap<CapabilityProxy<?>> map : getCapabilitiesProxy().values()) {
if (map != null) {
for (CapabilityProxy<?> proxy : map.values()) {
if (proxy != null) {
proxy.updateChangedState(periodID);
}
}
}
}
}
} catch (Exception e) {
Multiblocked.LOGGER.error("something run while checking proxy changes");
}
}
use of com.lowdragmc.multiblocked.api.capability.proxy.CapabilityProxy 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;
}
use of com.lowdragmc.multiblocked.api.capability.proxy.CapabilityProxy in project Multiblocked by Low-Drag-MC.
the class IOPageWidget method handleClientAction.
@Override
public void handleClientAction(int id, PacketBuffer buffer) {
if (id == -1) {
MultiblockCapability<?> capability = MbdCapabilities.get(buffer.readUtf());
Table<IO, MultiblockCapability<?>, Long2ObjectOpenHashMap<CapabilityProxy<?>>> capabilities = controller.getCapabilitiesProxy();
if (buffer.readBoolean()) {
IO io = buffer.readEnum(IO.class);
capabilities.get(io, capability).remove(pos.asLong());
if (capabilities.get(io, capability).isEmpty()) {
capabilities.remove(io, capability);
}
}
if (buffer.readBoolean()) {
IO io = buffer.readEnum(IO.class);
TileEntity entity = controller.getLevel().getBlockEntity(pos);
if (entity != null && capability.isBlockHasCapability(io, entity)) {
if (!capabilities.contains(io, capability)) {
capabilities.put(io, capability, new Long2ObjectOpenHashMap<>());
}
CapabilityProxy<?> proxy = capability.createProxy(io, entity);
proxy.facing = buffer.readEnum(Direction.class);
capabilities.get(io, capability).put(pos.asLong(), proxy);
}
}
controller.markAsDirty();
} else if (id == -2) {
MultiblockCapability<?> capability = MbdCapabilities.get(buffer.readUtf());
Table<IO, MultiblockCapability<?>, Long2ObjectOpenHashMap<CapabilityProxy<?>>> capabilities = controller.getCapabilitiesProxy();
IO io = buffer.readEnum(IO.class);
TileEntity entity = controller.getLevel().getBlockEntity(pos);
if (entity != null && capability.isBlockHasCapability(io, entity)) {
if (!capabilities.contains(io, capability)) {
capabilities.put(io, capability, new Long2ObjectOpenHashMap<>());
}
CapabilityProxy<?> proxy = capability.createProxy(io, entity);
proxy.facing = buffer.readEnum(Direction.class);
capabilities.get(io, capability).put(pos.asLong(), proxy);
}
controller.markAsDirty();
} else {
super.handleClientAction(id, buffer);
}
}
Aggregations