Skip to content

Instantly share code, notes, and snippets.

@tonybruess
Created June 29, 2014 18:33
Show Gist options
  • Save tonybruess/9485b569344f87777538 to your computer and use it in GitHub Desktop.
Save tonybruess/9485b569344f87777538 to your computer and use it in GitHub Desktop.
Bukkit's ChatColor implementation in ruby
class ChatColor
@@color_char = "\u00A7"
def initialize(code)
@code = code
end
def +(str)
to_str + str
end
def to_str
@@color_char + @code.to_s
end
def to_s
self.to_str
end
def ChatColor.color_char
@@color_char
end
def ChatColor.add_item(key,value)
@hash ||= {}
@hash[key] = value
end
def ChatColor.const_missing(key)
@hash[key]
end
def ChatColor.each
@hash.each {|key,value| yield(key, value)}
end
ChatColor.add_item :BLACK, ChatColor.new(0)
ChatColor.add_item :DARK_BLUE, ChatColor.new(1)
ChatColor.add_item :DARK_GREEN, ChatColor.new(2)
ChatColor.add_item :DARK_AQUA, ChatColor.new(3)
ChatColor.add_item :DARK_RED, ChatColor.new(4)
ChatColor.add_item :DARK_PURPLE, ChatColor.new(5)
ChatColor.add_item :GOLD, ChatColor.new(6)
ChatColor.add_item :GRAY, ChatColor.new(7)
ChatColor.add_item :DARK_GRAY, ChatColor.new(8)
ChatColor.add_item :BLUE, ChatColor.new(9)
ChatColor.add_item :GREEN, ChatColor.new("a")
ChatColor.add_item :AQUA, ChatColor.new("b")
ChatColor.add_item :RED, ChatColor.new("c")
ChatColor.add_item :LIGHT_PURPLE, ChatColor.new("d")
ChatColor.add_item :YELLOW, ChatColor.new("e")
ChatColor.add_item :WHITE, ChatColor.new("f")
ChatColor.add_item :MAGIC, ChatColor.new("k")
ChatColor.add_item :BOLD, ChatColor.new("l")
ChatColor.add_item :STRIKETHROUGH, ChatColor.new("m")
ChatColor.add_item :UNDERLINE, ChatColor.new("n")
ChatColor.add_item :ITALIC, ChatColor.new("o")
ChatColor.add_item :RESET, ChatColor.new("r")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment