[Help] Is there a way to get the the currently played minecraft version via computer craft

  • Please make sure you are posting in the correct place. Server ads go here and modpack bugs go here
  • The FTB Forum is now read-only, and is here as an archive. To participate in our community discussions, please join our Discord! https://ftb.team/discord

SReject

New Member
Jul 29, 2019
433
0
0
So I'm working on a CC/Turtle wheat farm and have ran into a bit of a snag. I want my code to work with mc versions before and after the bonemeal nerf. So Im wondering if there is a way to get the current minecraft version the script is being ran under.


To expand this a bit further here's the bit of my code that this pretains to:

Code:
    -- Use Bone Meal
    turtle.select(2)
    if (turtle.place() == false) then
        print("Unable to bone meal, waiting...")
        while (turtle.place() == false) do
            sleep(1)
        end
    end

    -- Bone Meal nerf fix - I dislike doing this
    while (turtle.place() == true) do
        getBoneMeal()
    end

Currently to switch between 1.4.x and 1.5.x the user has to (un)comment the while loop.

"You could just leave the code as is, the loop would fail in 1.4.x and the code would continue". I could, yes, but this would slow the turtle/code, and if I can avoid it I'd rather not have it as such
 
Last edited:

casilleroatr

New Member
Jul 29, 2019
1,360
0
0
If you are worried about the time it takes to farm the wheat why not just the reduce the sleep time and leave the loop as is? And given that the while loop contains the condition in it in the first place the if statement is redundant.

This should work

Code:
turtle.select(2)
while (turtle.place() == true) do -- So long as there is something to bonemeal it will do it. Then it will stop
  sleep(0.05) -- Or whatever suitably short duration to prevent yielding errors not that it should be a big problem
end
harvest() -- Whatever code you wrote to harvest the wheat goes here

Does that help? If not, then it will probably be better if you post the whole code with comments so we know exactly where the problem lies.

Also, why are you checking if turtle.place() returns false and then waiting seemingly forever. Is that when you have no bonemeal in your inventory? What does getBoneMeal() do?
 

SReject

New Member
Jul 29, 2019
433
0
0
If you are worried about the time it takes to farm the wheat why not just the reduce the sleep time and leave the loop as is? And given that the while loop contains the condition in it in the first place the if statement is redundant.

This should work

Code:
turtle.select(2)
while (turtle.place() == true) do -- So long as there is something to bonemeal it will do it. Then it will stop
  sleep(0.05) -- Or whatever suitably short duration to prevent yielding errors not that it should be a big problem
end
harvest() -- Whatever code you wrote to harvest the wheat goes here

Does that help? If not, then it will probably be better if you post the whole code with comments so we know exactly where the problem lies.

Also, why are you checking if turtle.place() returns false and then waiting seemingly forever. Is that when you have no bonemeal in your inventory? What does getBoneMeal() do?
For the first part of your post, Its not that I don't have code that will work for before and after the bonemeal nerf, but rather me wondering if there is a way to optimize the code dependant on the MC version

as far as the 'seemingly forever' wait, its for if the turtle can't initially use bone meal (IE: no seeds are planted directly infront the of turtle). It will try then wait to bone meal until it succeeds in doing so


As far as the code is concerned:

Code:
--[[
    WheatFarm.lua by SReject
    2014 All Rights Reserved
 
 
    Purpose:
        Instructs a turtle to plant seeds, bone meal them, then harvest the wheat repeatedly
        If required resources are missing(Seeds/bonemeal/improper soil) the program waits until they are available
     
    Setup:
        Inventory directly below the turtle for refilling bone meal
        Inventory directly above the turtle for wheat and extra seeds
        Watered & Tilled soil directly infront of but lowered one block from the turtle
     
    Starting
        Place a stack of seeds in first slot
        Place a stack of bone meal in second slot
        At the turtle's CC prompt type: WheatFarm.lua
]]--


-- Empties the turtles inventory slots 3 through 16
function emptyInventory(forceEmpty)

    -- Arguments:
    --   forceEmpty:
    --     Makes the turtle empty items in invo slots 3 through 16, reguardless if there's an item in slot 16

    -- Check if there is an item in the 16th slot or 'forceEmpty' has been specified
    if (turtle.getItemCount(16) > 0 or forceEmpty == true) then
 
        -- If so, call the refillSeeds function (w/ the forceRefill argument being true)
        refillSeeds(true)
     
        -- Loop through inventory slots 3 through 16 and place items in the chest above
        for n = 3, 16, 1 do
            turtle.select(n)
            turtle.dropUp()
        end
     
    -- If there's not an item in the 16th slot and 'forceEmpty' Isnt true
    else
     
        -- Refill seeds (w/ the forceRefill argument being false)
        refillSeeds(false)
    end
end


-- Refills first slot with seeds from inventory slots 3 to 16 as needed:
--     Before the turtle empties its inventory
--     When the turtle has less than 16 seeds in the first slot
function refillSeeds(forceRefill)

    -- Arguments:
    --   forceRefill:
    --     Refills seeds in slot #1 with seeds from slots 3 through 16 regardless of the amount of seeds in slot #1

 
    -- Variables:
    --   seedCount: current item count of slot #1(seeds)
    --   refillCount: Number of seeds required to fill slot #1 to 64 seeds
 
    local seedCount, refillCount = turtle.getItemCount(1), 0
    refillCount = 64 - seedCount
 
    -- Check if slot #1(seedCount) is empty
    if (seedCount == 0) then
 
        -- if slot #1(seedCount) is empty, print we need seeds
        print("Out of seeds: place seeds in first slot to continue")
     
        -- wait 1 second
        repeat
            sleep(1)
         
        -- check if slot one is NOT empty, if it still is loop back
        until (turtle.getItemCount(1) > 0)
 
    -- If we have seeds in slot#1, and either forceRefill is true and we have less than 64 seeds in slot one OR we have less than 16 seeds in slot #1
    elseif (forceRefill == true and seedCount < 64) or (seedCount < 16) then
 
        -- Select the first slot for comparing to other slots that may contain seeds
        turtle.select(1)
     
        -- Loop over slots 3 through 16
        for n = 3, 16, 1 do
     
            -- See if the selected slot(#1) is the same item as the one we have looped to
            if (turtle.compareTo(n) == true) then
         
                -- select the looped to slot
                turtle.select(n)
             
                -- Check if the selected slot DOESN'T have enough seeds to refill slot one
                if (refillCount > turtle.getItemCount(n)) then
             
                    -- transfer items(seeds) from the selected slot to slot one
                    turtle.transferTo(1, turtle.getItemCount(n))
                 
                    -- adjust refillCount to account for the transfer seeds
                    refillCount = 64 - turtle.getItemCount(1)
                 
                -- If the selected slot will fill fill slot#1 to 64 items(seeds)
                else
                    -- transfer just enough items to fill slot#1 with seeds
                    turtle.transferTo(1, refillCount)
                 
                    -- exit the loop
                    break
                end
             
                -- reset the selected slot to slot#1 for the next literation of the loop
                turtle.select(1)
            end
        end
    end
end


-- Retrives Bonemeal from an inventory below, waits if none is available
function getBoneMeal()
 
    -- Arguments: none
    -- Variables: none

    -- select slot #2(Bone meal Slot)
    turtle.select(2)
 
    -- check to see if the turtle has less than or equal to 8 items in slot#2
    if (turtle.getItemCount(2) <= 8) then
 
        -- If an attempt to suck items(bone meal) from an inventory below the turtle fails
        if (turtle.suckDown() == false) then
     
            -- print the turtle is out of bone meal
            print("Out of bonemeal, waiting...")
         
            -- wait 1 second
            repeat
                sleep(1)
             
            -- continue looping back until the turtle is able to suck in items from the inventory below
            until (turtle.suckDown() == true)
        end
     
        -- This section removes excess bonemeal from the turtles inventory
        -- Loop over slots 3 through 16
        for n = 3, 16, 1 do
         
            -- if the looped over slot has the same item as slot #2(bone meal) drop the items into a chest above
            if (turtle.compareTo(n) == true) then
                turtle.select(n)
                turtle.dropUp()
                turtle.select(2)
            end
        end
    end
end





-- On Start, prompt for seeds if needed
turtle.select(1)
if (turtle.getItemCount(1) == 0) then

    -- Tell the user the turtle needs seeds in the first slot
    print("Place seeds in first slot to start")
 
    -- loop + sleep until there are seeds in first slot
    repeat
        sleep(1)
    until (turtle.getItemCount(1) > 0)
end


-- Resets state for start up:
--     Clears soil
--     Empties Inventory
--     Refills Bone Meal
turtle.dig()
emptyInventory(forceEmpty)
turtle.select(2)
turtle.dropUp()
turtle.suckDown()


-- Processing Loop
while true do

    -- Empty Inventory & refill Bone Meal
    emptyInventory()
    getBoneMeal()
 
    -- Attempt to plant seeds until successful:
    turtle.select(1)
    if (turtle.place() == false) then
        print("Unable to place seeds, waiting...")
        repeat
            sleep(1)
        until (turtle.place() == true)
    end
 
    -- Attempt to bone meal until seccessful
    turtle.select(2)
    if (turtle.place() == false) then
        print("Unable to bone meal, waiting...")
        repeat
            sleep(1)
        until (turtle.place() == true)
    end

    -- Bone Meal nerf fix:
    --   Continue to loop over turtle.place() until it fails
    while (turtle.place() == true) do
    end
 
    -- Harvest Wheat
 
    -- Check to see if there is a block infront of the turtle
    -- and if so check if the turtle ISN'T able to dig/harvest it
    if (turtle.detect() == true and turtle.dig() == false) then
     
        -- Tell the user the turtle isn't able to dig()/harvest the block infront of the turtle
        print("Unable to break block, waiting...")
     
        -- Attempt to dig()/harvest every second until successful
        repeat
            sleep(1)
        until (turtle.detect() == false or turtle.dig() == true)
    end
end


I changed some of the code while commenting and haven't tested yet. The changes being while->sleep->loop to repeat->sleep->until.

So if it out and out fails while you test, those may be the issue(s)
 

casilleroatr

New Member
Jul 29, 2019
1,360
0
0
For the first part of your post, Its not that I don't have code that will work for before and after the bonemeal nerf, but rather me wondering if there is a way to optimize the code dependant on the MC version

as far as the 'seemingly forever' wait, its for if the turtle can't initially use bone meal (IE: no seeds are planted directly infront the of turtle). It will try then wait to bone meal until it succeeds in doing so


As far as the code is concerned:

<snip>


I changed some of the code while commenting and haven't tested yet. The changes being while->sleep->loop to repeat->sleep->until.

So if it out and out fails while you test, those may be the issue(s)
Its not really an optimization that needs to be made. If you want your code to work for both bonemeal mechanics it will involve a check somewhere that needs to be done. May as well be the check in the while loop. The only thing to be wary of is if the turtle.place() (bonemeal) loop is failing because it has no bonemeal, if that is the case the code will crash eventually. Therefore it is worth doing a getItemCount check on slot 2 to make sure that bonemeal is present. Something like this

Code:
while turtle.place() == true then
  if turtle.getItemCount(2) ==0 then
     break
  end
  sleep(0.05)
end

--//Or you can do a for loop like this

local bonemealAmount = turtle.getItemCount(2)

for i = 0, bonemealAmount do -- Makes sure you can never bonemeal without bonemeal
  if turtle.place() == false then --Once bonemealing stops working, the loop is terminated even if bonemeal remains
    break
  end
end

I am pretty sure that you don't need to run both the segment of code headed by the comment "--Attempt to bone meal until seccessful" and the one headed by "-- Bone Meal nerf fix:".

The reason why I am worried it will run seemingly forever as well is because I predict that turtle.place(2) will return false when attempting to bonemeal a fully grown plant. Given that once fully grown the state of the plant will not change, the turtle will be stuck in a loop of trying to bonemeal a plant that is mature and then going to sleep for a second. So the bonemeal nerf fix code should probably be ran exclusively no matter which version of minecraft. Unless if I am misunderstanding something I think that will be the most efficient way of doing things.
 

SReject

New Member
Jul 29, 2019
433
0
0
The first attempt to bonemeal serves two purposes: make sure the turtle CAN bonemeal and ofcourse, bonemealing. Without it, for example, if a mob fell on the plot and untilled it, this check would prevent the rest of the script from degrading. The nerf fix then follows to continue applying bonemeal as needed

As far as running out of bonemeal durring the nerf fix, I meant to call getBoneMeal() from within the nerf fix, but forgot to add it. It will refill the turtle with bone meal as needed, and/or wait until there is bone meal if the inventory below is out.

As far as your concerns of a fully grown plant, this won't be an issue as I'm using a T5 skeleton spawner+macarator to create bonemeal(Have filled up a 64k AE disk drive with it, and am voiding off excess). Though I do understand your concerns if the turtle has to wait a while for more bone meal and may implement something along the lines of if the turtle runs out of bonemeal, once more is retrieved (attempt to) break what ever is infront of it and start anew.


As far as my request being a minute optimization, A simple if check is far faster than even a single literation of a loop in Lua
 

casilleroatr

New Member
Jul 29, 2019
1,360
0
0
The first attempt to bonemeal serves two purposes: make sure the turtle CAN bonemeal and ofcourse, bonemealing. Without it, for example, if a mob fell on the plot and untilled it, this check would prevent the rest of the script from degrading. The nerf fix then follows to continue applying bonemeal as needed

As far as running out of bonemeal durring the nerf fix, I meant to call getBoneMeal() from within the nerf fix, but forgot to add it. It will refill the turtle with bone meal as needed, and/or wait until there is bone meal if the inventory below is out.

As far as your concerns of a fully grown plant, this won't be an issue as I'm using a T5 skeleton spawner+macarator to create bonemeal(Have filled up a 64k AE disk drive with it, and am voiding off excess). Though I do understand your concerns if the turtle has to wait a while for more bone meal and may implement something along the lines of if the turtle runs out of bonemeal, once more is retrieved (attempt to) break what ever is infront of it and start anew.


As far as my request being a minute optimization, A simple if check is far faster than even a single literation of a loop in Lua
Well, I'm not really sure how else I could help you. The only other way I can thing I can suggest is using arguments to instruct the program to use one method over another and the user configures the argument which is slightly more elegant than manually commenting out bits of the code.

If I were you I would just use the while loop though, or write two programs for the different versions, after all a turtle is only going to be using one instance of bonemeal mechanics at any one time per game.
 

wolfsilver00

New Member
Jul 29, 2019
752
0
0
Make the user imput the version in a variable and use that variable as a condition to whether or not use the new bonemealing?

But just saying, before the update you may have needed more than 1 bonemeal, so even before 1.5 you WOULD NEED a loop to be sure the bonemeal is correct. So doing a version checking is not needed nor it is to do different codes for 2 different versions..
 

SReject

New Member
Jul 29, 2019
433
0
0
Make the user imput the version in a variable and use that variable as a condition to whether or not use the new bonemealing?
Was thinking about this, but as casilleroatr stated, it'd probably be simpler to just make two versions of it, due to the fact the same turtle isn't going to be flip flopping between 1.4 and 1.5



But just saying, before the update you may have needed more than 1 bonemeal, so even before 1.5 you WOULD NEED a loop to be sure the bonemeal is correct. So doing a version checking is not needed nor it is to do different codes for 2 different versions..
For seeds, prior to 1.5, wheat took only a single bonemeal; with other plants such as trees and mushrooms there was a possibility of requiring more, but wheat was always a single bone meal.