aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/com/notnite/gloppers/Gloppers.kt
blob: bdea77917ce61c1aaeb010badd7924d20fdb43f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.notnite.gloppers

import net.fabricmc.api.DedicatedServerModInitializer
import net.minecraft.block.entity.HopperBlockEntity
import net.minecraft.inventory.Inventory
import net.minecraft.item.ItemStack

object Gloppers : DedicatedServerModInitializer {
    override fun onInitializeServer() {}

    private fun matchesGlob(glob: String, str: String): Boolean {
        val regex = glob
            .replace(".", "\\.")
            .replace("*", ".*")
            .replace("?", ".")
        return str.matches(regex.toRegex())
    }

    fun canTransfer(to: Inventory, stack: ItemStack): Boolean {
        if (to is HopperBlockEntity) {
            val hopperName = to.name.copyContentOnly().string
            val itemName = stack.registryEntry.key.get().value.path

            if (hopperName.startsWith("!")) {
                val globs = hopperName.substring(1).split(",")
                for (glob in globs) {
                    if (matchesGlob(glob, itemName)) {
                        // Glob matched, transfer
                        return true
                    }
                }

                // No globs matched, so don't transfer
                return false
            }
        }

        // Doesn't have a glob, so transfer
        return true
    }
}