JheffersonMarques / MorePeripherals

Adds new peripherals for CC:Tweaked
MIT License
8 stars 2 forks source link

Interface States not correct #22

Closed crazyvinvin closed 1 year ago

crazyvinvin commented 1 year ago

We are using the honey interface to collect honey from 8 beehives. We wrap each peripheral, get the honey level, and harvest it if it's equal to 5. Very basic stuff. However, in our loop, each time it prints the same honey level for each hive, most of the time. Sometimes something updates while it's looping and the honeylevel of the rest of the beehives changes. It's like the bees are updating all the hives at the same time.

Here is how we got the issue again in a creative world: image The beehive on the left has a honey level of 5, the beehive on the right has a honey level of 0, with the other honey levels in between . The beehive left is "beehive_interface_7".

Here is the lua code:

local beehivesList = {}

local function findAllBeeHives()
    local names = peripheral.getNames()
    local beehives = {}
    for key, name in pairs(names) do
        if peripheral.getType(name) == "beehive_interface" then
            table.insert(beehives, name)
        end
    end
    return beehives
end

local function printAllHoneyLevels(beehives)
    for key, beehiveName in pairs(beehives) do
        local beehive = peripheral.wrap(beehiveName)
        local honeyLevel = beehive.getHoneyLevel()
        print(beehiveName .. " has " .. honeyLevel .. " honey level")
    end
end
beehivesList = findAllBeeHives()

printAllHoneyLevels(beehivesList)

which results in:

image

If I turn on the modems from left to right and run the script each time, it results in this: image

Still the same problem.

But here comes the interesting part!! I have surrounded the hives in a glass cage: image

Now when I spawn a bee, which goes into a hive. It updates this specific hive and all the honey levels switch to that specific hives honey level (atleast according to the lua code, f3 screen still gives the correct honey levels for each hive). Here is the result when i spawn one in the middle, which goes into the hive instantly because it's night: image

According to the "getBees" function beehive_interface_5 has one bee, which is also the beehive with 3 honey level.

Ps: when calling all the beehives directly in the lua shell, they all return the same honey level of the hive with the bee in it (last updated maybe?)

crazyvinvin commented 1 year ago

After some more testing I found out that the state of each beehive (state according to the mod, not to the f3 screen) changes to the state of the hive where a bee last popped in or out. Which looks to me like these entities:

    BeehiveBlockEntity beehive;
    BlockState beehiveBlock;

In the mods Java code, are always the last updated beehive, not the beehive with the same interface ID / connected beehive

crazyvinvin commented 1 year ago

After even more testing and looking at the Java code, we found out this is also the case with the spawner interface (and possibly with all of the mod's peripherals and interfaces). For the spawner interface, the state of the spawner which mob was last updated (We tested it in creative, so updated it by right clicking with a mob egg) is the state of all spawners, not just the ones connected to the same network, but every peripheral of the same type (spawner_interface) in the world.

JheffersonMarques commented 1 year ago

From what i see, the issue is that it stores the data as a reference from the last value, probably i should store the block position, and check it at function runtime to get the pertinent data

Gonna look into it, if it doesn't work, i probably will need to rollback to a separate block for that

JheffersonMarques commented 1 year ago

so, the way it was implemented makes it impossible to it separate block data (unless i did some forsaken coding), so i reverted the beehives and spawner interfaces to their original system (with a interface block that manages the states), and the issue as solved

I did the fix on 1.20.1 but, i'm gonna backport it to 1.19.2

JheffersonMarques commented 1 year ago

Fixed in Dev