#Dynamic tag reading for crop growth

22 messages · Page 1 of 1 (latest)

novel loom
#

I'm currently trying to make a system for allowing crops to be grown in certain conditions, especially biomes.

onForgeEvent('net.minecraftforge.event.world.BlockEvent$CropGrowEvent$Pre', event => {
    const { x, y, z } = event.pos
    const { world } = event
    let level = Utils.getLevel(event.world)
    let block = level.getBlock(x, y, z)
    let hasTag = /kubejs:croprequire\/.*/
    block.tags.forEach(tag => {
        if (!hasTag.test(tag)
        || event.isCanceled()
        || !tag.contains('/')) {
            event.setResult('allow'); return
        }
        if (hasTag.test(tag)) {
            let aa = tag.split(':')[1]
            let ba = aa.split('/')[1]
            let ca = ba.replace('.', ':')
            let cb = ca.split(':')
            let cc = cb[0]
            let cd = cb[1]
            if (world.getBiome(BlockPos(x, y, z)).value().getRegistryName() == new ResourceLocation(cc, cd)) {
                event.setResult('allow')
            } else {
                event.setResult('deny')
            }
        } 
    })
})

Above is my current code, in which @past basin has said here that it has some things wrong with it. I can definitely see the problems, but I don't know how I'll get started fixing them or even getting it working in the first place.

sonic spearBOT
#

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

novel loom
#

addendum: #off-topic message

cinder topazBOT
#

[Quote ➤](#off-topic message) the main problem with all that is that it's supposed to be dynamic, which implies there should be no need for a list of tags
an example:
if someone adds the block tag kubejs:croprequire/minecraft.plains, it would translate that to minecraft:plains and compare it to the crop's current biome

past basin
#

Sorry for the late response

#

I've been busy with school

novel loom
#

alright, that's fine

past basin
#

I think this should simplify your code drastically.

onForgeEvent('net.minecraftforge.event.world.BlockEvent$CropGrowEvent$Pre', event => {
    const { x, y, z } = event.pos
    const { world } = event
    let level = Utils.getLevel(event.world);
    let block = level.getBlock(x, y, z);
    let biomeName = world.getBiome(BlockPos(x, y, z)).value().getRegistryName();
    let biomeTag = `kubejs:croprequire/${biomeName}`
    if (block.tags.contains(biomeTag) {
        event.setResult('allow')
    } else {
        event.setResult('deny')
    }
});
#

I assume this was the result you were intending for

#

Where you check if a crop has a specific tag, based on it's biome.

#

It's possible that it may not work due to .contains accepting an object, so it may not type cast string to resource location

#

If that is the case, you can create a new ResourceLocation before passing it to the contains method

#

Lmk if this has any issues with your usecase though

novel loom
#

i see an issue with that but it's an easy fix- the biome name doesn't have the : converted to a .

#

reason i did that is because the tag would have 2 : if i didn't

#
onForgeEvent('net.minecraftforge.event.world.BlockEvent$CropGrowEvent$Pre', event => {
    const { x, y, z } = event.pos
    const { world } = event
    let level = Utils.getLevel(event.world);
    let block = level.getBlock(x, y, z);
    let biomeName = world.getBiome(BlockPos(x, y, z)).value().getRegistryName();
    let biome = biomeName.toString().split(':')
    biomeName = `${biome[0]}.${biome[1]}`
    let biomeTag = `kubejs:croprequire/${biomeName}`
    if (block.tags.contains(biomeTag)) {
        event.setResult('allow')
    } else {
        event.setResult('deny')
    }
});
past basin
#

You could split, or you could instead just replace all : with a .

novel loom
#

getRegistryName returns a new ResourceLocation()

#

so i turned it into a string

#

i might have to convert it back though

#

nvm

novel loom
#

ah, well this worked:

global.CropRequirements = function(event) {
    // console.log('test')
    const { x, y, z } = event.pos
    const { world } = event
    let level = Utils.getLevel(event.world);
    let block = level.getBlock(x, y, z);
    let biomeName = world.getBiome(BlockPos(x, y, z)).value().getRegistryName();
    let biome = biomeName.toString().split(':')
    biomeName = `${biome[0]}.${biome[1]}`
    let biomeTag = `kubejs:croprequire/${biomeName}`
    let tags = []
    let hasRequirements = false
    block.tags.forEach(tag => {
        tags.push(tag.toString())
    })
    tags.forEach(tag => {
        if (tag.includes('croprequire')) {
            hasRequirements = true
        }
    })
    if (!hasRequirements) {
        // console.log(`allow, ${block.id} is not a crop or isn't tagged with croprequire`)
        event.setResult('allow')
    } else {
        if (block.tags.stream().anyMatch(tag => tag == biomeTag)) {
            // console.log(`allow, ${block.id} is in the right biome`)
            event.setResult('allow')
        } else {
            // console.log(`deny, ${block.id} is not in the right biome`)
            event.setResult('deny')
        }
    }
}

onForgeEvent('net.minecraftforge.event.world.BlockEvent$CropGrowEvent$Pre', event => {
    global.CropRequirements(event)
});