use of com.lowdragmc.multiblocked.api.pattern.MultiblockState in project Multiblocked by Low-Drag-MC.
the class PatternWidget method loadControllerFormed.
private void loadControllerFormed(Collection<BlockPos> poses, ControllerTileEntity controllerBase) {
controllerBase.state = new MultiblockState(world, controllerBase.getBlockPos());
BlockPattern pattern = controllerBase.getPattern();
if (pattern != null && pattern.checkPatternAt(controllerBase.state, true)) {
controllerBase.onStructureFormed();
}
if (controllerBase.isFormed()) {
LongSet set = controllerBase.state.getMatchContext().getOrDefault("renderMask", LongSets.EMPTY_SET);
Set<BlockPos> modelDisabled = set.stream().map(BlockPos::of).collect(Collectors.toSet());
if (!modelDisabled.isEmpty()) {
sceneWidget.setRenderedCore(poses.stream().filter(pos -> !modelDisabled.contains(pos)).collect(Collectors.toList()), null);
} else {
sceneWidget.setRenderedCore(poses, null);
}
} else {
Multiblocked.LOGGER.warn("Pattern formed checking failed: {}", controllerBase.getDefinition().location);
}
}
use of com.lowdragmc.multiblocked.api.pattern.MultiblockState in project Multiblocked by Low-Drag-MC.
the class ItemMultiblockBuilder method onItemUseFirst.
@Override
public ActionResultType onItemUseFirst(ItemStack stack, ItemUseContext context) {
PlayerEntity player = context.getPlayer();
if (player != null) {
if (!player.level.isClientSide) {
TileEntity tileEntity = player.level.getBlockEntity(context.getClickedPos());
ItemStack hold = player.getItemInHand(context.getHand());
if (isItemMultiblockBuilder(hold) && tileEntity instanceof ControllerTileEntity) {
if (isRaw(hold)) {
BlockPattern pattern = ((ControllerTileEntity) tileEntity).getPattern();
if (pattern != null) {
pattern.autoBuild(player, new MultiblockState(player.level, context.getClickedPos()));
}
return ActionResultType.SUCCESS;
} else {
String json = hold.getOrCreateTagElement("pattern").getString("json");
String controller = hold.getOrCreateTagElement("pattern").getString("controller");
if (!json.isEmpty() && !controller.isEmpty()) {
if (controller.equals(((ControllerTileEntity) tileEntity).getDefinition().location.toString())) {
JsonBlockPattern jsonBlockPattern = Multiblocked.GSON.fromJson(json, JsonBlockPattern.class);
jsonBlockPattern.build().autoBuild(player, new MultiblockState(player.level, context.getClickedPos()));
return ActionResultType.SUCCESS;
}
}
}
}
}
}
return ActionResultType.PASS;
}
use of com.lowdragmc.multiblocked.api.pattern.MultiblockState in project Multiblocked by Low-Drag-MC.
the class MultiblockWorldSavedData method load.
@Override
public void load(CompoundNBT nbt) {
for (String key : nbt.getAllKeys()) {
BlockPos pos = BlockPos.of(Long.parseLong(key));
MultiblockState state = new MultiblockState(worldRef.get(), pos);
state.deserialize(new PacketBuffer(Unpooled.copiedBuffer(nbt.getByteArray(key))));
this.mapping.put(pos, state);
for (BlockPos blockPos : state.getCache()) {
chunkPosMapping.computeIfAbsent(new ChunkPos(blockPos), c -> new HashSet<>()).add(state);
}
}
}
use of com.lowdragmc.multiblocked.api.pattern.MultiblockState in project Multiblocked by Low-Drag-MC.
the class ControllerTileEntity method use.
@Override
public ActionResultType use(PlayerEntity player, Hand hand, BlockRayTraceResult hit) {
if (Multiblocked.isKubeJSLoaded()) {
RightClickEvent event = new RightClickEvent(this, player, hand, hit);
if (event.post(ScriptType.SERVER, RightClickEvent.ID, getSubID())) {
return ActionResultType.SUCCESS;
}
}
if (isRemote() && !this.isFormed() && player.isCrouching() && player.getItemInHand(hand).isEmpty()) {
MultiblockPreviewRenderer.renderMultiBlockPreview(this, 60000);
return ActionResultType.SUCCESS;
}
if (!isRemote()) {
if (!isFormed() && definition.catalyst != null) {
if (state == null)
state = new MultiblockState(level, getBlockPos());
ItemStack held = player.getItemInHand(hand);
if (definition.catalyst.isEmpty() || held.equals(definition.catalyst, false)) {
if (checkPattern()) {
// formed
player.swing(hand);
ITextComponent formedMsg = new TranslationTextComponent(getUnlocalizedName()).append(new TranslationTextComponent("multiblocked.multiblock.formed"));
player.sendMessage(formedMsg, NIL_UUID);
if (!player.isCreative() && !definition.catalyst.isEmpty()) {
held.shrink(1);
}
MultiblockWorldSavedData.getOrCreate(level).addMapping(state);
if (!needAlwaysUpdate()) {
MultiblockWorldSavedData.getOrCreate(level).addLoading(this);
}
onStructureFormed();
return ActionResultType.SUCCESS;
}
}
}
if (!player.isCrouching()) {
if (!isRemote() && player instanceof ServerPlayerEntity) {
TileEntityUIFactory.INSTANCE.openUI(this, (ServerPlayerEntity) player);
}
}
}
return ActionResultType.SUCCESS;
}
use of com.lowdragmc.multiblocked.api.pattern.MultiblockState 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");
}
}
Aggregations