This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Pikeman270's profile picture

Nickname Color Codes

Started by Pikeman270, 31 August 2016 - 04:28 AM
Pikeman270 #1
Posted 31 August 2016 - 06:28 AM
I just have a question. I want to make a program, whose function is to test your nickname using the normal minecraft color codes. For example, they would type in "&5Jack" I'm struggling with how to get it to recognize the &5, then set the color to whatever color that is, then write Jack in that color. And once I get that, maybe there is away to recognize multi-color. for example "&5J&6a&6c&k"

Regardless of the color codes, is there anyway I could accomplish this?

Update:
I decided to take a completely different approach. Here is my code

---------------------------------------
function writeText(xPos, yPos, b_color, t_color, text)
term.setCursorPos(xPos, yPos)
term.setBackgroundColor(b_color)
term.setTextColor(t_color)
term.write(text)
end
function writeLine(xPos, yPos, color)
  x, y = term.getSize()
  term.setCursorPos(xPos, yPos)
  term.setBackgroundColor(color)
  term.write(string.rep(" ", x))
end
function clear()
  term.setBackgroundColor(colors.white)
  term.clear()
end
function home()

clear()
x, y = term.getSize()
writeLine(1, 1, colors.purple)
writeLine(1, y, colors.purple)
writeText(1, 1, colors.purple, colors.white, "Jackson's Nickname Designer")
writeText(2, 2, colors.white, colors.black, "&0")
writeText(5, 2, colors.white, colors.blue, "&1")
writeText(8, 2, colors.white, colors.green, "&2")
writeText(11, 2, colors.white, colors.cyan, "&3")


end

home()
--------------------------------------
button, xPos, yPos = os.pullEvent("mouse_click")
if xPos == 2 and yPos == 2  then
term.setTextColor(colors.black)
term.setBackgroundColor(colors.white)
term.setCursorPos(1, 3)
wait = read()
else
write("you done didn't click right")--Troubleshooting


end

Anyways, it has the '&' codes on screen for the user to click on. I'm running into one issue. And although it's the simplest i'm sure, i'm stuck. For some reason, when I click 2 (x), 2 (y), it goes to the else. As if I didn't click in the right spot. And I have no clue why. It's my understanding x and y are as follow, (x, y) where x is across the top horizontally, while y runs down vertically. I know the code works if I do for example "if "mouse_click" then (code) and it works just fine. What am I overlooking?
Alas, another update:
Derp. For some reason, didn't like left click.
Edited on 31 August 2016 - 11:46 PM
Admicos #2
Posted 31 August 2016 - 06:40 AM
I just have a question. I want to make a program, whose function is to test your nickname using the normal minecraft color codes. For example, they would type in "&5Jack" I'm struggling with how to get it to recognize the &5, then set the color to whatever color that is, then write Jack in that color. And once I get that, maybe there is away to recognize multi-color. for example "&5J&6a&6c&k"

Regardless of the color codes, is there anyway I could accomplish this?

1. Please don't post multiple topics
2. I think you can use a string split function (search on google) to split in the "&" character and check the first character of the resulting strings (using loops and string.sub(str, 1, 1)) to find the color code and match it to this table.
Edited on 31 August 2016 - 04:41 AM
valithor #3
Posted 31 August 2016 - 07:02 AM
Here is a short example that I worked up using patterns and a for loop.


local str = "hello &5J&6a&7c&k" --# string to test with

local codes = { --# you will need to finish building this table
  1 = colors.blue,
  2 = colors.green,
  3 = colors.cyan
}

for code, word in str:gmatch("&?(%d*)([^&]*)") do --# for loop to loop through 
  if tonumber(code) and codes[tonumber(code)] then --# checking to see if code is a number and is in our table
    term.setTextColor(codes[tonumber(code)]) --# setting text color to the value
  else
    write("&"..code) --# it was not a recognized code, so write it with the rest of the string
  end
  term.write(word) --# writing everything between &'s
end
Pikeman270 #4
Posted 31 August 2016 - 07:52 AM
1. Please don't post multiple topics
My bad… I really don't understand forums. Do you mean I posted this topic twice? Or my the topic's i've posted previously. Like Disk Drive directories and Applied Energistics Peripheral Methods. I probably sound like a noob. Any help is greatly appreciated… and I will gladly delete any topics of mine if need be.
Bomb Bloke #5
Posted 31 August 2016 - 10:21 AM
You accidentally created two copies of the one topic. I've already cleared the duplicate. It's not something to be concerned about, as it's obvious it wasn't intentional - Admicos, just use the report button next time.