Search in sources :

Example 1 with ICarvingGroup

use of team.chisel.api.carving.ICarvingGroup in project Chisel by Chisel-Team.

the class ContainerChiselHitech method setSelection.

public void setSelection(@Nullable Slot slot) {
    this.selection = slot;
    if (slot == null || !slot.getHasStack()) {
        currentGroup = null;
        selectionDuplicates = ImmutableList.of();
        setTarget(null);
    } else {
        ImmutableList.Builder<Slot> builder = ImmutableList.builder();
        for (int i = getInventoryChisel().size + 1; i < inventorySlots.size(); i++) {
            Slot s = getSlot(i);
            if (slot != s && ItemStack.areItemsEqual(slot.getStack(), s.getStack())) {
                builder.add(s);
            }
        }
        selectionDuplicates = builder.build();
        ICarvingGroup group = carving.getGroup(slot.getStack().getItem()).orElse(null);
        if (currentGroup != null && group != currentGroup) {
            setTarget(null);
        }
        currentGroup = group;
    }
    ItemStack stack = slot == null ? ItemStack.EMPTY : slot.getStack();
    getInventoryChisel().setStackInSpecialSlot(stack);
    getInventoryChisel().updateItems();
    NBTUtil.setHitechSelection(chisel, Optional.fromNullable(getSelection()).transform(s -> s.slotNumber).or(-1));
}
Also used : ICarvingGroup(team.chisel.api.carving.ICarvingGroup) ImmutableList(com.google.common.collect.ImmutableList) Slot(net.minecraft.inventory.container.Slot) ItemStack(net.minecraft.item.ItemStack)

Example 2 with ICarvingGroup

use of team.chisel.api.carving.ICarvingGroup in project Chisel by Chisel-Team.

the class ChiselController method onPlayerInteract.

@SubscribeEvent
public static void onPlayerInteract(PlayerInteractEvent.LeftClickBlock event) {
    PlayerEntity player = event.getPlayer();
    ItemStack held = event.getItemStack();
    if (held.getItem() instanceof IChiselItem) {
        ItemStack target = NBTUtil.getChiselTarget(held);
        IChiselItem chisel = (IChiselItem) held.getItem();
        IVariationRegistry registry = CarvingUtils.getChiselRegistry();
        BlockState state = event.getWorld().getBlockState(event.getPos());
        if (!chisel.canChiselBlock(event.getWorld(), player, event.getHand(), event.getPos(), state)) {
            return;
        }
        ICarvingGroup blockGroup = state.getBlock() instanceof ICarvable ? ((ICarvable) state.getBlock()).getVariation().getGroup() : registry.getGroup(state.getBlock()).orElse(null);
        if (blockGroup == null) {
            return;
        }
        IChiselMode mode = NBTUtil.getChiselMode(held);
        Iterable<? extends BlockPos> candidates = mode.getCandidates(player, event.getPos(), event.getFace());
        if (!target.isEmpty()) {
            ICarvingGroup sourceGroup = registry.getGroup(target.getItem()).orElse(null);
            if (blockGroup == sourceGroup) {
                ICarvingVariation variation = registry.getVariation(target.getItem()).orElse(null);
                if (variation != null) {
                    if (variation.getBlock() != null) {
                        setAll(candidates, player, state, variation);
                    }
                } else {
                    Chisel.logger.warn("Found itemstack {} in group {}, but it has no variation!", target, sourceGroup.getId());
                }
            }
        } else {
            List<Block> variations = new ArrayList<>(blockGroup.getBlockTag().map(ITag::getAllElements).orElse(Collections.emptyList()));
            variations = variations.stream().filter(v -> v.getBlock() != null).collect(Collectors.toList());
            int index = variations.indexOf(state.getBlock());
            index = player.isSneaking() ? index - 1 : index + 1;
            index = (index + variations.size()) % variations.size();
            ICarvingVariation next = registry.getVariation(variations.get(index)).orElse(null);
            setAll(candidates, player, state, next);
        }
    }
}
Also used : IChiselItem(team.chisel.api.IChiselItem) IVariationRegistry(team.chisel.api.carving.IVariationRegistry) ICarvingGroup(team.chisel.api.carving.ICarvingGroup) ICarvable(team.chisel.api.block.ICarvable) ArrayList(java.util.ArrayList) PlayerEntity(net.minecraft.entity.player.PlayerEntity) BlockState(net.minecraft.block.BlockState) ITag(net.minecraft.tags.ITag) IChiselMode(team.chisel.api.carving.IChiselMode) Block(net.minecraft.block.Block) ICarvingVariation(team.chisel.api.carving.ICarvingVariation) ItemStack(net.minecraft.item.ItemStack) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Example 3 with ICarvingGroup

use of team.chisel.api.carving.ICarvingGroup in project Chisel by Chisel-Team.

the class PacketChiselButton method chiselAll.

public static void chiselAll(PlayerEntity player, int[] slots) {
    if (player.openContainer instanceof ContainerChiselHitech) {
        ContainerChiselHitech container = (ContainerChiselHitech) player.openContainer;
        ItemStack chisel = container.getChisel();
        ItemStack originalChisel = chisel.copy();
        ItemStack target = container.getTargetStack();
        if (!(chisel.getItem() instanceof IChiselItem)) {
            return;
        }
        @SuppressWarnings("null") @Nonnull IVariationRegistry carving = CarvingUtils.getChiselRegistry();
        if (chisel.isEmpty() || target.isEmpty()) {
            return;
        }
        boolean playSound = false;
        for (int i : slots) {
            ItemStack s = player.inventory.getStackInSlot(i);
            if (!s.isEmpty()) {
                if (!carving.getGroup(target.getItem()).map(ICarvingGroup::getId).flatMap(id -> carving.getGroup(s.getItem()).map(g -> g.getId().equals(id))).orElse(false)) {
                    return;
                }
                container.getInventoryChisel().setStackInSpecialSlot(s);
                ItemStack res = SlotChiselSelection.craft(container, player, target.copy(), false);
                if (!res.isEmpty()) {
                    player.inventory.setInventorySlotContents(i, res);
                    playSound = true;
                }
            }
            if (chisel.isEmpty()) {
                return;
            }
        }
        container.getInventoryChisel().setStackInSpecialSlot(container.getSelectionStack());
        container.getInventoryChisel().updateItems();
        if (playSound) {
            SoundUtil.playSound(player, originalChisel, target);
        }
    }
}
Also used : SlotChiselSelection(team.chisel.common.inventory.SlotChiselSelection) IChiselItem(team.chisel.api.IChiselItem) PlayerEntity(net.minecraft.entity.player.PlayerEntity) RequiredArgsConstructor(lombok.RequiredArgsConstructor) NetworkEvent(net.minecraftforge.fml.network.NetworkEvent) IVariationRegistry(team.chisel.api.carving.IVariationRegistry) Supplier(java.util.function.Supplier) ContainerChiselHitech(team.chisel.common.inventory.ContainerChiselHitech) ItemStack(net.minecraft.item.ItemStack) CarvingUtils(team.chisel.api.carving.CarvingUtils) ICarvingGroup(team.chisel.api.carving.ICarvingGroup) SoundUtil(team.chisel.common.util.SoundUtil) Nonnull(javax.annotation.Nonnull) PacketBuffer(net.minecraft.network.PacketBuffer) NoArgsConstructor(lombok.NoArgsConstructor) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) IChiselItem(team.chisel.api.IChiselItem) ContainerChiselHitech(team.chisel.common.inventory.ContainerChiselHitech) IVariationRegistry(team.chisel.api.carving.IVariationRegistry) Nonnull(javax.annotation.Nonnull) ItemStack(net.minecraft.item.ItemStack)

Example 4 with ICarvingGroup

use of team.chisel.api.carving.ICarvingGroup in project Chisel by Chisel-Team.

the class ChiselBlockBuilder method build.

/**
 * Builds the block(s), performing the passed action on each.
 *
 * @param after
 *            The consumer to call after creating each block. Use this to easily set things like hardness/light/etc. This is called after block registration, but prior to model/variation
 *            registration.
 * @return An array of blocks created. More blocks are automatically created if the unbaked variations will not fit into one block.
 */
@SuppressWarnings("null")
public Map<String, BlockEntry<T>> build(NonNullUnaryOperator<Block.Properties> after) {
    if (variations.size() == 0) {
        throw new IllegalArgumentException("Must have at least one variation!");
    }
    Variation[] data = new Variation[variations.size()];
    for (int i = 0; i < variations.size(); i++) {
        data[i] = variations.get(i).doBuild();
    }
    Map<String, BlockEntry<T>> ret = new HashMap<>(data.length);
    ICarvingGroup group = CarvingUtils.itemGroup(this.group, this.groupName);
    for (int i = 0; i < data.length; i++) {
        if (Strings.emptyToNull(data[i].getName()) != null) {
            final int index = i;
            final Variation var = data[index];
            final VariationBuilder<T> builder = variations.get(index);
            ret.put(var.getName(), registrate.object(blockName + "/" + var.getName()).block(material, p -> provider.createBlock(p, new VariationDataImpl(ret.get(var.getName()), var.getName(), var.getDisplayName(), group))).initialProperties(initialProperties == null ? NonNullSupplier.of(Blocks.STONE.delegate) : initialProperties).addLayer(layer).transform(this::addTags).properties(color == null ? after : after.andThen(p -> {
                p.blockColors = $ -> color;
                return p;
            })).blockstate((ctx, prov) -> builder.model.accept(prov, ctx.getEntry())).setData(ProviderType.LANG, NonNullBiConsumer.noop()).recipe((ctx, prov) -> builder.recipe.accept(prov, ctx.getEntry())).loot(loot).item(provider::createBlockItem).model((ctx, prov) -> prov.withExistingParent("item/" + prov.name(ctx::getEntry), new ResourceLocation(prov.modid(ctx::getEntry), "block/" + prov.name(ctx::getEntry)))).transform(this::addTags).build().register());
        }
    }
    if (this.group != null) {
        CarvingUtils.getChiselRegistry().addGroup(group);
        if (!otherBlocks.isEmpty() || !otherTags.isEmpty()) {
            addExtraTagEntries(ProviderType.BLOCK_TAGS, t -> t, ForgeRegistries.BLOCKS::getValue);
            this.<Item>addExtraTagEntries(ProviderType.ITEM_TAGS, t -> parent.getItemTag(t.getName()), ForgeRegistries.ITEMS::getValue);
        }
    }
    return ret;
}
Also used : BlockEntry(com.tterrag.registrate.util.entry.BlockEntry) Arrays(java.util.Arrays) Setter(lombok.Setter) Accessors(lombok.experimental.Accessors) Getter(lombok.Getter) Item(net.minecraft.item.Item) BlockBuilder(com.tterrag.registrate.builders.BlockBuilder) HashMap(java.util.HashMap) MaterialColor(net.minecraft.block.material.MaterialColor) ProviderType(com.tterrag.registrate.providers.ProviderType) Supplier(java.util.function.Supplier) RegistrateBlockLootTables(com.tterrag.registrate.providers.loot.RegistrateBlockLootTables) ParametersAreNonnullByDefault(javax.annotation.ParametersAreNonnullByDefault) Registrate(com.tterrag.registrate.Registrate) ArrayList(java.util.ArrayList) BooleanSupplier(java.util.function.BooleanSupplier) Value(lombok.Value) TranslationTextComponent(net.minecraft.util.text.TranslationTextComponent) HashSet(java.util.HashSet) ItemBuilder(com.tterrag.registrate.builders.ItemBuilder) RegistrateTagsProvider(com.tterrag.registrate.providers.RegistrateTagsProvider) Strings(com.google.common.base.Strings) NonNullSupplier(com.tterrag.registrate.util.nullness.NonNullSupplier) AccessLevel(lombok.AccessLevel) Block(net.minecraft.block.Block) VariantTemplates(team.chisel.client.data.VariantTemplates) ICarvingGroup(team.chisel.api.carving.ICarvingGroup) Map(java.util.Map) NonNullFunction(com.tterrag.registrate.util.nullness.NonNullFunction) INamedTag(net.minecraft.tags.ITag.INamedTag) TagsProvider(net.minecraft.data.TagsProvider) Nullable(javax.annotation.Nullable) MethodsReturnNonnullByDefault(mcp.MethodsReturnNonnullByDefault) ModelTemplates(team.chisel.client.data.ModelTemplates) Collection(java.util.Collection) Set(java.util.Set) RenderType(net.minecraft.client.renderer.RenderType) RegistrateLangProvider(com.tterrag.registrate.providers.RegistrateLangProvider) Blocks(net.minecraft.block.Blocks) Objects(java.util.Objects) List(java.util.List) NonNullBiConsumer(com.tterrag.registrate.util.nullness.NonNullBiConsumer) CarvingUtils(team.chisel.api.carving.CarvingUtils) NonNullUnaryOperator(com.tterrag.registrate.util.nullness.NonNullUnaryOperator) Material(net.minecraft.block.material.Material) ResourceLocation(net.minecraft.util.ResourceLocation) ForgeRegistries(net.minecraftforge.registries.ForgeRegistries) NonFinal(lombok.experimental.NonFinal) ICarvingGroup(team.chisel.api.carving.ICarvingGroup) HashMap(java.util.HashMap) Item(net.minecraft.item.Item) BlockEntry(com.tterrag.registrate.util.entry.BlockEntry) ResourceLocation(net.minecraft.util.ResourceLocation)

Example 5 with ICarvingGroup

use of team.chisel.api.carving.ICarvingGroup in project Chisel by Chisel-Team.

the class TileAutoChisel method tick.

@Override
public void tick() {
    if (getWorld() == null || getWorld().isRemote) {
        return;
    }
    if (energyStorage.getEnergyStored() == 0 && Configurations.autoChiselNeedsPower) {
        return;
    }
    @Nonnull ItemStack target = getTarget();
    @Nonnull ItemStack chisel = getChisel();
    @Nonnull ItemStack source = sourceSlot < 0 ? ItemStack.EMPTY : getInputInv().getStackInSlot(sourceSlot);
    chisel = chisel.isEmpty() ? ItemStack.EMPTY : chisel.copy();
    ICarvingVariation v = target.isEmpty() || chisel.isEmpty() ? null : CarvingUtils.getChiselRegistry().getVariation(target.getItem()).orElse(null);
    ICarvingGroup g = target.isEmpty() || chisel.isEmpty() ? null : CarvingUtils.getChiselRegistry().getGroup(target.getItem()).orElse(null);
    if (chisel.isEmpty() || v == null) {
        setSourceSlot(-1);
        progress = 0;
        updateClientSlot();
        return;
    }
    // Force a source slot recalc if the stack has changed to something that cannot be converted to the target
    if (!source.isEmpty() && !CarvingUtils.getChiselRegistry().getGroup(source.getItem()).equals(g)) {
        source = ItemStack.EMPTY;
    }
    IChiselItem chiselitem = (IChiselItem) chisel.getItem();
    // Make sure to run this block if the source stack is removed, so a new one can be found
    if ((sourceSlot < 0 && getWorld().getGameTime() % 20 == 0) || sourceSlot >= 0) {
        // Reset source slot if it's been removed
        if (source.isEmpty()) {
            setSourceSlot(-1);
        }
        // Make sure we can output this stack
        ItemStack res = new ItemStack(v.getItem());
        if (!source.isEmpty()) {
            res.setCount(source.getCount());
        }
        if (source.isEmpty() || canOutput(res)) {
            for (int i = 0; sourceSlot < 0 && i < getInputInv().getSlots(); i++) {
                ItemStack stack = getInputInv().getStackInSlot(i);
                if (!stack.isEmpty() && g.equals(CarvingUtils.getChiselRegistry().getGroup(stack.getItem()).orElse(null))) {
                    res.setCount(stack.getCount());
                    if (canOutput(res) && chiselitem.canChisel(getWorld(), FakePlayerFactory.getMinecraft((ServerWorld) getWorld()), chisel, v)) {
                        setSourceSlot(i);
                        source = res.copy();
                    }
                }
            }
        } else {
            setSourceSlot(-1);
        }
    }
    if (sourceSlot >= 0) {
        source = getInputInv().getStackInSlot(sourceSlot);
        Validate.notNull(source);
        ICarvingVariation sourceVar = CarvingUtils.getChiselRegistry().getVariation(source.getItem()).orElse(null);
        if (sourceVar != v) {
            if (progress < MAX_PROGRESS) {
                if (!Configurations.autoChiselNeedsPower) {
                    // Add constant progress
                    progress = Math.min(MAX_PROGRESS, progress + BASE_PROGRESS);
                }
                // Compute progress added by FE
                int toUse = Math.min(MAX_PROGRESS - progress, getPowerProgressPerTick());
                // Compute FE usage
                int powerToUse = getUsagePerTick();
                // Avoid NaN
                if (toUse > 0 && powerToUse > 0) {
                    if (Configurations.autoChiselPowered) {
                        int used = energyStorage.extractEnergy(powerToUse, false);
                        progress += toUse * ((float) used / powerToUse);
                    } else {
                        progress += toUse;
                    }
                }
            } else {
                ItemStack res = new ItemStack(v.getItem());
                source = source.copy();
                chisel = chisel.copy();
                ServerPlayerEntity player = FakePlayerFactory.getMinecraft((ServerWorld) getWorld());
                player.inventory.mainInventory.set(player.inventory.currentItem, chisel);
                // TODO should this send an explicit packet for item break? currently just checks for empty stack on the client
                res = chiselitem.craftItem(chisel, source, res, player, $ -> {
                });
                player.inventory.mainInventory.set(player.inventory.currentItem, ItemStack.EMPTY);
                chiselitem.onChisel(getWorld(), player, chisel, v);
                inputInv.setStackInSlot(sourceSlot, source);
                Chisel.network.send(PacketDistributor.NEAR.with(targetNearby()), new MessageAutochiselFX(getPos(), chisel, sourceVar.getBlock().getDefaultState()));
                otherInv.setStackInSlot(0, chisel);
                mergeOutput(res);
                // Try the next slot, if this is invalid it will be fixed next update
                setSourceSlot((sourceSlot + 1) % getInputInv().getSlots());
                progress = 0;
            }
        } else {
            // This is the same variation, so just move it to the output
            inputInv.setStackInSlot(sourceSlot, ItemStack.EMPTY);
            mergeOutput(source);
        }
    } else {
        progress = 0;
    }
    updateClientSlot();
}
Also used : ServerWorld(net.minecraft.world.server.ServerWorld) IItemHandler(net.minecraftforge.items.IItemHandler) ContainerAutoChisel(team.chisel.common.inventory.ContainerAutoChisel) CompoundNBT(net.minecraft.nbt.CompoundNBT) Direction(net.minecraft.util.Direction) ParametersAreNonnullByDefault(javax.annotation.ParametersAreNonnullByDefault) TranslationTextComponent(net.minecraft.util.text.TranslationTextComponent) PacketDistributor(net.minecraftforge.fml.network.PacketDistributor) BlockParticleData(net.minecraft.particles.BlockParticleData) IItemHandlerModifiable(net.minecraftforge.items.IItemHandlerModifiable) BlockState(net.minecraft.block.BlockState) SoundCategory(net.minecraft.util.SoundCategory) IWorldPosCallable(net.minecraft.util.IWorldPosCallable) EnergyStorage(net.minecraftforge.energy.EnergyStorage) TargetPoint(net.minecraftforge.fml.network.PacketDistributor.TargetPoint) PlayerEntity(net.minecraft.entity.player.PlayerEntity) EnumMap(java.util.EnumMap) Capability(net.minecraftforge.common.capabilities.Capability) Vector3d(net.minecraft.util.math.vector.Vector3d) ItemStackHandler(net.minecraftforge.items.ItemStackHandler) CapabilityItemHandler(net.minecraftforge.items.CapabilityItemHandler) SoundUtil(team.chisel.common.util.SoundUtil) IEnergyStorage(net.minecraftforge.energy.IEnergyStorage) ITickableTileEntity(net.minecraft.tileentity.ITickableTileEntity) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) Setter(lombok.Setter) Getter(lombok.Getter) Container(net.minecraft.inventory.container.Container) INamedContainerProvider(net.minecraft.inventory.container.INamedContainerProvider) NetworkManager(net.minecraft.network.NetworkManager) Supplier(java.util.function.Supplier) ITextComponent(net.minecraft.util.text.ITextComponent) LazyOptional(net.minecraftforge.common.util.LazyOptional) ItemStack(net.minecraft.item.ItemStack) ICarvingVariation(team.chisel.api.carving.ICarvingVariation) SUpdateTileEntityPacket(net.minecraft.network.play.server.SUpdateTileEntityPacket) Chisel(team.chisel.Chisel) Minecraft(net.minecraft.client.Minecraft) ICarvingGroup(team.chisel.api.carving.ICarvingGroup) Chunk(net.minecraft.world.chunk.Chunk) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) DiggingParticle(net.minecraft.client.particle.DiggingParticle) CapabilityEnergy(net.minecraftforge.energy.CapabilityEnergy) INameable(net.minecraft.util.INameable) Configurations(team.chisel.common.config.Configurations) PlayerInventory(net.minecraft.entity.player.PlayerInventory) IChiselItem(team.chisel.api.IChiselItem) ChiselTileEntities(team.chisel.common.init.ChiselTileEntities) IIntArray(net.minecraft.util.IIntArray) SoundEvents(net.minecraft.util.SoundEvents) FakePlayerFactory(net.minecraftforge.common.util.FakePlayerFactory) ParticleTypes(net.minecraft.particles.ParticleTypes) CarvingUtils(team.chisel.api.carving.CarvingUtils) Validate(org.apache.commons.lang3.Validate) MathHelper(net.minecraft.util.math.MathHelper) TileEntity(net.minecraft.tileentity.TileEntity) Particle(net.minecraft.client.particle.Particle) IChiselItem(team.chisel.api.IChiselItem) ICarvingGroup(team.chisel.api.carving.ICarvingGroup) Nonnull(javax.annotation.Nonnull) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) ICarvingVariation(team.chisel.api.carving.ICarvingVariation) ItemStack(net.minecraft.item.ItemStack) TargetPoint(net.minecraftforge.fml.network.PacketDistributor.TargetPoint)

Aggregations

ICarvingGroup (team.chisel.api.carving.ICarvingGroup)5 ItemStack (net.minecraft.item.ItemStack)4 Supplier (java.util.function.Supplier)3 PlayerEntity (net.minecraft.entity.player.PlayerEntity)3 IChiselItem (team.chisel.api.IChiselItem)3 CarvingUtils (team.chisel.api.carving.CarvingUtils)3 ArrayList (java.util.ArrayList)2 Nonnull (javax.annotation.Nonnull)2 Nullable (javax.annotation.Nullable)2 ParametersAreNonnullByDefault (javax.annotation.ParametersAreNonnullByDefault)2 Getter (lombok.Getter)2 Setter (lombok.Setter)2 Block (net.minecraft.block.Block)2 BlockState (net.minecraft.block.BlockState)2 ServerPlayerEntity (net.minecraft.entity.player.ServerPlayerEntity)2 TranslationTextComponent (net.minecraft.util.text.TranslationTextComponent)2 ICarvingVariation (team.chisel.api.carving.ICarvingVariation)2 IVariationRegistry (team.chisel.api.carving.IVariationRegistry)2 SoundUtil (team.chisel.common.util.SoundUtil)2 Strings (com.google.common.base.Strings)1