Search in sources :

Example 1 with MultiblockState

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);
    }
}
Also used : BlockPattern(com.lowdragmc.multiblocked.api.pattern.BlockPattern) LongSet(it.unimi.dsi.fastutil.longs.LongSet) MultiblockState(com.lowdragmc.multiblocked.api.pattern.MultiblockState) BlockPos(net.minecraft.util.math.BlockPos)

Example 2 with MultiblockState

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;
}
Also used : ControllerTileEntity(com.lowdragmc.multiblocked.api.tile.ControllerTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) JsonBlockPattern(com.lowdragmc.multiblocked.api.pattern.JsonBlockPattern) JsonBlockPattern(com.lowdragmc.multiblocked.api.pattern.JsonBlockPattern) BlockPattern(com.lowdragmc.multiblocked.api.pattern.BlockPattern) MultiblockState(com.lowdragmc.multiblocked.api.pattern.MultiblockState) ItemStack(net.minecraft.item.ItemStack) PlayerEntity(net.minecraft.entity.player.PlayerEntity) ControllerTileEntity(com.lowdragmc.multiblocked.api.tile.ControllerTileEntity)

Example 3 with MultiblockState

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);
        }
    }
}
Also used : ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) ServerWorld(net.minecraft.world.server.ServerWorld) Arrays(java.util.Arrays) ComponentTileEntity(com.lowdragmc.multiblocked.api.tile.ComponentTileEntity) OnlyIn(net.minecraftforge.api.distmarker.OnlyIn) Constants(net.minecraftforge.common.util.Constants) CompoundNBT(net.minecraft.nbt.CompoundNBT) HashMap(java.util.HashMap) Unpooled(io.netty.buffer.Unpooled) Dist(net.minecraftforge.api.distmarker.Dist) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ByteBuf(io.netty.buffer.ByteBuf) Minecraft(net.minecraft.client.Minecraft) Object2ObjectOpenHashMap(it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap) Map(java.util.Map) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) BlockState(net.minecraft.block.BlockState) WeakReference(java.lang.ref.WeakReference) ThreadFactory(java.util.concurrent.ThreadFactory) Nonnull(javax.annotation.Nonnull) DummyWorld(com.lowdragmc.lowdraglib.utils.DummyWorld) World(net.minecraft.world.World) Collection(java.util.Collection) MultiblockState(com.lowdragmc.multiblocked.api.pattern.MultiblockState) ChunkPos(net.minecraft.util.math.ChunkPos) Set(java.util.Set) BlockPos(net.minecraft.util.math.BlockPos) WorldSavedData(net.minecraft.world.storage.WorldSavedData) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) Multiblocked(com.lowdragmc.multiblocked.Multiblocked) Collections(java.util.Collections) PacketBuffer(net.minecraft.network.PacketBuffer) ClientWorld(net.minecraft.client.world.ClientWorld) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) MultiblockState(com.lowdragmc.multiblocked.api.pattern.MultiblockState) BlockPos(net.minecraft.util.math.BlockPos) ChunkPos(net.minecraft.util.math.ChunkPos) PacketBuffer(net.minecraft.network.PacketBuffer) HashSet(java.util.HashSet)

Example 4 with MultiblockState

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;
}
Also used : ITextComponent(net.minecraft.util.text.ITextComponent) MultiblockState(com.lowdragmc.multiblocked.api.pattern.MultiblockState) TranslationTextComponent(net.minecraft.util.text.TranslationTextComponent) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) ItemStack(net.minecraft.item.ItemStack)

Example 5 with MultiblockState

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");
    }
}
Also used : BlockPattern(com.lowdragmc.multiblocked.api.pattern.BlockPattern) MultiblockState(com.lowdragmc.multiblocked.api.pattern.MultiblockState) CapabilityProxy(com.lowdragmc.multiblocked.api.capability.proxy.CapabilityProxy)

Aggregations

MultiblockState (com.lowdragmc.multiblocked.api.pattern.MultiblockState)8 BlockPos (net.minecraft.util.math.BlockPos)4 BlockPattern (com.lowdragmc.multiblocked.api.pattern.BlockPattern)3 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 Map (java.util.Map)3 Set (java.util.Set)3 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)2 DummyWorld (com.lowdragmc.lowdraglib.utils.DummyWorld)2 Multiblocked (com.lowdragmc.multiblocked.Multiblocked)2 ComponentTileEntity (com.lowdragmc.multiblocked.api.tile.ComponentTileEntity)2 ByteBuf (io.netty.buffer.ByteBuf)2 Unpooled (io.netty.buffer.Unpooled)2 Object2ObjectOpenHashMap (it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap)2 WeakReference (java.lang.ref.WeakReference)2 Arrays (java.util.Arrays)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2