#Hiding/Showing JEI items/recipes dynamically

68 messages · Page 1 of 1 (latest)

south spoke
#

I'm doing a similar post in AStages discord, but i'm trying to show and hide recipes/items dynamically in JEI with kubejs and AStages without having to reload the client everytime.
I managed to hide the itens with AStages and show them without reloading, BUT, it's still possible to see the craft of the items by simply clicking U (for example I hid a diamond chestplate in JEI, but by simply clicking U in a diamond i can see the diamond chestplate recipe)

Any ideia to make this happen?
I was checking and maybe i'll have to use Java in kubejs, idk if that's the only way, seeing that i can do most of the things I want with AStages. But if it's the only way, how can I do it?

My idea is that players don't have recipes unlocked, but can still craft (i'll change most of them, so if they know a recipe by heart they won't be able to craft) and use the items, so if they getting some recipe correctly by chance is intended.
Also i'll have ways for them to unlock recipes (by right clicking a scroll)

sly pumiceBOT
#

Once your ticket has been resolved, please close it with </ticket close:1054771505520717835> command!

south spoke
#

this is my code by know


const diamond_items = [
  (items that i want to hide)
  'minecraft:diamond_chestplate'
  'minecraft:diamond_sword'
]

AStages.addRestrictionForItem('astages/item1_1', 'diamond_stage', diamond_itens)
    .setCanAttack(true)
    .setCanBeStoredInInventory(true)
    .setCanBeStoredInContainers(true)
    .setCanBeEquipped(true)
    .setCanPickedUp(true)
    .setHideTooltip(false) 
    .setRenderItemName(true)
    .setCanBePlaced(true)
    .setCanItemBeLeftClicked(true)
    .setCanItemBeRightClicked(true)
    .setCanInteractWithBlock(true)
    .setCanBeDig(true)


ItemEvents.rightClicked(e => {
    let player = e.player.name.getString()
    let item = e.item.id

    switch (item){
        case "kubejs:pergaminho_diamante":
            e.server.runCommandSilent(`astages add ${player} diamond_stage`)
            break
        }
    })
south spoke
#

ok, i was doing some research in here

mild quarryBOT
south spoke
#

and i think this could work, but idk how to make it work with astages

#

i'll read some more and see what i can do

south spoke
#

ok, the code from @/mumsspaghetti works, now i need to adapt it

#

it removes the recipes (crafting table only) from the JEI

#

maybe i could use this with astages

#

i'll do this later, but it's a good start

mild quarryBOT
#

[➤](#1223741772698746932 message)

const $FLUIDSTACK = Java.loadClass("net.minecraftforge.fluids.FluidStack");
const $VanillaTypes = Java.loadClass('mezz.jei.api.constants.VanillaTypes');
const $ForgeTypes = Java.loadClass('mezz.jei.api.forge.ForgeTypes');

NetworkEvents.dataReceived('channel1', event => {
  console.log('####### Data Received! #######')
  event.player.tell('Data Received! SHOW')
  event.player.playSound('entity.experience_orb.pickup')
  global.jeiRuntime.getIngredientManager().addIngredientsAtRuntime($VanillaTypes.ITEM_STACK, [Item.of('minecraft:apple')]);
  const UNBOUND_FLUIDSTACK_WATER = Fluid.of('minecraft:water');
  console.log("Unbound Fluidstack Water: ", UNBOUND_FLUIDSTACK_WATER);
  const MINECRAFT_FLUID = UNBOUND_FLUIDSTACK_WATER.getFluid();
  console.log("Minecraft Fluid Water: ", MINECRAFT_FLUID);
  const MINECRAFT_FLUID_STACK = new $FLUIDSTACK(MINECRAFT_FLUID, amount);
  console.log("Minecraft FluidStack Water: ", MINECRAFT_FLUID_STACK);
  
  global.jeiRuntime.getIngredientManager().addIngredientsAtRuntime($ForgeTypes.FLUID_STACK, [MINECRAFT_FLUID_STACK]);
  console.log("Successfully hidden Water Fluidstack");
});
mild quarryBOT
#

[➤](#1206847745852444692 message)
Client Script:

const $VanillaTypes = Java.loadClass('mezz.jei.api.constants.VanillaTypes')

NetworkEvents.dataReceived('channel1', event => {
    console.log('####### Data Received! #######')
    event.player.tell('Data Received! SHOW')
 global.jeiRuntime.getIngredientManager().addIngredientsAtRuntime($VanillaTypes.ITEM_STACK, [Item.of('minecraft:apple')])
    
  })

  NetworkEvents.dataReceived('channel2', event => {
    console.log('####### Data Received! #######')
    event.player.tell('Data Received! HIDE')
    global.jeiRuntime.getIngredientManager().removeIngredientsAtRuntime($VanillaTypes.ITEM_STACK, [Item.of('minecraft:apple')])

  })

south spoke
#

Ok, i got a working prototype

south spoke
#

OK

#

GOT SOMETHING AGAIN

#

for some reason the network wasn't receiving data

#

anyway

#

client script

const $VanillaTypes = Java.loadClass('mezz.jei.api.constants.VanillaTypes')

NetworkEvents.dataReceived('channel_remove_all', e => {
    console.log('received remove')
    e.player.tell('received remove')

    global.jeiRuntime.getIngredientManager().removeIngredientsAtRuntime($VanillaTypes.ITEM_STACK, [Item.of('minecraft:apple')])
})

NetworkEvents.dataReceived('channel_add_all', e => {
    console.log('received add')
    e.player.tell('received add')

    global.jeiRuntime.getIngredientManager().addIngredientsAtRuntime($VanillaTypes.ITEM_STACK, [Item.of('minecraft:apple')])
})
#

server script (just a way to activate the client side part)

ItemEvents.rightClicked(e => {
    switch (e.item.id) {
        case "kubejs:pergaminho_unknowledge":
            e.player.tell('Data Sent! Remove')
            console.log('Data Sent! Remove items')
            e.player.sendData('channel_remove_all')
            break

        case "kubejs:pergaminho_diamante":
            e.player.tell('Data Sent! Add')
            console.log('Data Sent! Add items')
            e.player.sendData('channel_add_all')
            break
    }
})
south spoke
#

know i need to refine this script

#

and add some more features

mild quarryBOT
south spoke
#

this one hides recipes

#

ok, i think i'm gonna use AStages for hiding items in JEI and getRecipeManager.hideRecipes() for hiding recipes (so players can't just check other recipes and search for the item that they want to craft)

south spoke
#

and it doesn't help that my probe isn't loading

south spoke
#

i'll post an update when i've a substantial quantity of code

#

but that's the plan, AStages + jeiRuntime for handling the recipes part

south spoke
#

ok, new problem

#

i adapted the code from @/mumsspaghetti_ and it works

#

in some ways

#
var modids = ['terramity']

function hideRecipes() {
    let recipeCategories = global.jeiRuntime.recipeManager.createRecipeCategoryLookup().get().toList();
    recipeCategories.forEach(category =>{
            console.log(category.getRecipeType().getUid())
            let recipeType = category.getRecipeType()

            var recipesToHide = global.jeiRuntime.recipeManager.createRecipeLookup(recipeType).get().toArray().filter( recipe => {
                let recipeId = recipe.getId().toString();
                let namespace = recipeId.split(':')[0];
                return modids.includes(namespace);
            })
            global.jeiRuntime.recipeManager.hideRecipes(recipeType, recipesToHide)
    })
}

NetworkEvents.dataReceived('remove_recipe', e =>{
    e.player.tell('Data received! REMOVE RECIPE')
    console.log('Data received! REMOVE RECIPE')
    hideRecipes()
})

NetworkEvents.dataReceived('show_recipe', event =>{

    function showRecipes() {
        event.player.tell('Data received! ADD RECIPE')
        console.log('Data received! ADD RECIPE')

        let recipeCategories = global.jeiRuntime.recipeManager.createRecipeCategoryLookup().includeHidden().get().toList();
        recipeCategories.forEach(category =>{
                console.log(category.getRecipeType().getUid())
                let recipeType = category.getRecipeType()

                var recipesToHide = global.jeiRuntime.recipeManager.createRecipeLookup(recipeType).includeHidden().get().toArray().filter( recipe => {
                    let recipeId = recipe.getId().toString();
                    let namespace = recipeId.split(':')[0];
                    return modids.includes(namespace);
                })
                global.jeiRuntime.recipeManager.unhideRecipes(recipeType, recipesToHide)
        })
    }
    
    showRecipes();
})
#

this works, but for some reason it only remove recipes from crafting

#

i think the problem is this error

#

Error in 'NetworkEvents.dataReceived': TypeError: Cannot find function getId in object AnvilRecipe[leftInputs=[1 wooden_sword], rightInputs=[1 wooden_sword], outputs=[1 wooden_sword], uid=minecraft:self_repair.minecraft.wooden_sword].

#

the problem in "let recipeId = recipe.getId().toString();"

#

idk how to solve it

#

if i discover it i'll post here

#

i'll have to adapt the code more, for it to work with specific items

#

not only mods

south spoke
#

ok, new problems

#

some recipes (like brewing ones and the anvil one that caused the error) can't be hidden normally

#

i'll hide by category these ones

#

note if you do by this way it'll hide ALL recipes

#

category is this right here

#

if you hide a category all recipes in this category vanish

#

ok, it works

#

but for some reason it only works with brewing

#
const categoryToHide = [
    'minecraft:brewing',
]

function hideByCategory() {
    let recipeCategories = global.jeiRuntime.recipeManager.createRecipeCategoryLookup().get().toList();
    recipeCategories.forEach(category =>{
        let categoryType = category.getRecipeType().getUid().toString()
        if(categoryType.includes(categoryToHide)){
            global.jeiRuntime.recipeManager.hideRecipeCategory(category.getRecipeType())
        }
    })
}

function showByCategory() {
    let recipeCategories = global.jeiRuntime.recipeManager.createRecipeCategoryLookup().includeHidden().get().toList();
    recipeCategories.forEach(category =>{
        let categoryType = category.getRecipeType().getUid().toString()
        if(categoryType.includes(categoryToHide)){
            global.jeiRuntime.recipeManager.unhideRecipeCategory(category.getRecipeType())
        }
    })
}

this works, but with 'minecraft:crafting' in the array it brokes

#

well, i don't think i'll hide anything by category except brewing

south spoke
#

i'll try to use tags for hiding recipes, i think it'll be easier for client-server integration, and the players won't be able to see the items that're hidden
i'm trying to make a function that hides and show items checking if they has a tag or not
but i'm not being that luck...
if i don't manage to make it work i'll try to make a list and use global

#
function hideByTag() {
    let recipeCategories = global.jeiRuntime.recipeManager.createRecipeCategoryLookup().get().toList()
    recipeCategories.forEach(category =>{
    let recipeType = category.getRecipeType()
    let recipeToHide = global.jeiRuntime.recipeManager.createRecipeLookup(recipeType).get().toList().filter(recipeToFilter =>{
        let recipeOutput = recipeToFilter.getOutput()
        let recipeTag = recipeOutput.getTags()
        if (item_tags.includes(recipeTag)) return true
    })
    if (recipeToHide.length > 0){
        global.jeiRuntime.recipeManager.hideRecipes(recipeType, recipeToHide)
        }
    })
}

function showByTag() {
    let recipeCategories = global.jeiRuntime.recipeManager.createRecipeCategoryLookup().includeHidden().get().toList()
    recipeCategories.forEach(category =>{
    let recipeType = category.getRecipeType()
    let recipeToShow = global.jeiRuntime.recipeManager.createRecipeLookup(recipeType).includeHidden().get().toList().filter(recipeToFilter =>{
        let recipeOutput = recipeToFilter.getOutput()
        let recipeTag = recipeOutput.getTags()
        if (item_tags.includes(recipeTag)) return true
    })
    if (recipeToShow.length > 0){
        global.jeiRuntime.recipeManager.unhideRecipes(recipeType, recipeToShow)
        }
    })
}

this isn't working, for the record, if anyone knows how to make it work, pleeeese tell me 😭

#

😭🙏

#

anyway, back to trial and error

twin acorn
#

@south spoke Ping me tomorrow morning, im cet +1. Maybe i can help

south spoke
#

OH SHIT

#

I GOT SOMETHING

#
var itemGroups = {
    scroll_diamond: [
        'terramity:diamond_ring',
        'terramity:diamond_studded_belt',
        'terramity:diamond_earrings',
        'terramity:diamond_pendant',
        'terramity:diamond_gauntlet'
    ]
}

function recipeProducesItem(recipe, itemList) {
    try {
        if (!recipe.getId) return false

        let recipeId = recipe.getId().toString()

        for (let itemId of itemList) {
            let itemName = itemId.split(':')[1]
            if (recipeId.includes(itemName)) {
                return true
            }
        }
    } catch (e) {}

    return false
}

function hideItemGroup(groupName) {
    let jei = global.jeiRuntime
    let itemList = itemGroups[groupName]
    if (!jei || !itemList) return

    let categories = jei.recipeManager
        .createRecipeCategoryLookup()
        .get()
        .toList()

    categories.forEach(category => {
        let type = category.getRecipeType()

        let recipes = jei.recipeManager
            .createRecipeLookup(type)
            .get()
            .toArray()

        let toHide = recipes.filter(r => recipeProducesItem(r, itemList))

        if (toHide.length > 0) {
            jei.recipeManager.hideRecipes(type, toHide)
        }
    })
}

function showItemGroup(groupName) {
    let jei = global.jeiRuntime
    let itemList = itemGroups[groupName]
    if (!jei || !itemList) return

    let categories = jei.recipeManager
        .createRecipeCategoryLookup()
        .includeHidden()
        .get()
        .toList()

    categories.forEach(category => {
        let type = category.getRecipeType()

        let recipes = jei.recipeManager
            .createRecipeLookup(type)
            .includeHidden()
            .get()
            .toArray()

        let toShow = recipes.filter(r => recipeProducesItem(r, itemList))

        if (toShow.length > 0) {
            jei.recipeManager.unhideRecipes(type, toShow)
        }
    })
}

this works.

#

i'll make this code more readable

#

(for me atleast)

south spoke
mild quarryBOT
#

Paste version of pergaminho_client.js, pergaminhos.js, items_registry.js from @south spoke

south spoke
#

i think it's done

#

astages + jei for hiding/showing items and recipes

#

i'm free.