Skip to content

Instantly share code, notes, and snippets.

@Pikachu920
Created September 30, 2017 03:35
Show Gist options
  • Save Pikachu920/ae33e97cdcccc97203fd62f6ee543faa to your computer and use it in GitHub Desktop.
Save Pikachu920/ae33e97cdcccc97203fd62f6ee543faa to your computer and use it in GitHub Desktop.
options:
fontpath: font2.ttf
folder: sbgen
toppath: sbgen/top.png
bottompath: sbgen/bottom.png
on chat:
set chat-recipients to all players where [{chan::%player input's uuid%} is not set]
on script load:
import "java.io.File"
import "org.bukkit.ChatColor"
import "java.awt.Graphics"
import "java.text.AttributedString"
import "java.awt.Color"
import "java.lang.Float"
import "java.awt.font.TextAttribute"
import "javax.xml.bind.DatatypeConverter"
import "javax.imageio.ImageIO"
import "java.io.ByteArrayInputStream"
import "java.awt.FontMetrics"
import "java.awt.Font"
import "javax.swing.JComponent"
import "java.awt.image.BufferedImage"
import "java.awt.Rectangle"
import "java.awt.geom.Ellipse2D$Float" as {Ellipse2D.Float}
function getBukkitCode(i: int = 1) :: string:
return "&"
function getVanillaCode(i: int = 1) :: string:
return "§"
function getBottom(i: int = 1) :: string:
return "{@bottompath}"
function getTop(i: int = 1) :: string:
return "{@toppath}"
function getFolder(i: int = 1) :: string:
return "{@folder}"
function getFont(i: int = 1) :: object:
set {_args::*} to {sbgen::font}.getName(), 0 and 31
return new {Font}({_args::*})
function getRGB(color: object) :: numbers:
set {_name} to (check [{_color} is a text] ? {_color} : {_color}.name())
return {sbgen::colors::%{_name}%::*}
function getCode(code: string) :: object:
return {ChatColor}.getByChar(join split join split {_code} at getVanillaCode() with "" at getBukkitCode() with "")
function getImage(stream: object) :: object:
return {ImageIO}.read({_stream})
function disposeGraphic(g: object):
wait 1 minute
{_g}.dispose();
function getGraphics(image: object) :: object:
set {_g} to {_image}.createGraphics()
disposeGraphic({_g})
return {_g}
function getAbsolute(i: int = 1) :: string:
return new {File}("").getAbsolutePath()
function getSeparator(i: int = 1) :: string:
return "%{File}.separator!%"
function getPath(path: string) :: string:
return join split join split join split {_path} at "\" with "/" at "/" with getSeparator() at getSeparator() where [string input is not ""] with getSeparator()
function getTopSB(i: int = 1) :: string:
return "iVBORw0KGgoAAAANSUhEUgAAAWAAAAAkCAMAAACAARCwAAAAA1BMVEVNTl9KFg2nAAAAI0lEQVRo3u3BMQEAAADCoPVPbQhfoAAAAAAAAAAAAAAAAHgNMaQAAXRZkOEAAAAASUVORK5CYII="
function getBottomSB(i: int = 1) :: string:
return "iVBORw0KGgoAAAANSUhEUgAAAWAAAAAkCAMAAACAARCwAAAAA1BMVEVVVmhihcI5AAAAI0lEQVRo3u3BMQEAAADCoPVPbQhfoAAAAAAAAAAAAAAAAHgNMaQAAXRZkOEAAAAASUVORK5CYII="
function getRelative(path: string) :: string:
return getPath("%getAbsolute()%%getSeparator()%%getPath({_path}) ? """"%") # """
function rgbNum(num: number) :: object:
return new {Float}("%{_num} / 255%")
function getJavaColor(rgb: numbers) :: object:
if size of {_rgb::*} is 3:
return new {Color}(rgbNum({_rgb::1}), rgbNum({_rgb::2}) and rgbNum({_rgb::3}))
return false
function closeStream(steam: object):
wait 1 minute
{_stream}.close();
function getStreamFromBase64(base64: string) :: object:
set {_stream} to new {ByteArrayInputStream}({DatatypeConverter}.parseBase64Binary({_base64}))
closeStream({_stream})
return {_stream}
function fileExists(path: string) :: boolean:
return new {File}(getRelative({_path})).exists()
func "saveImage":
set {_image} to parameter 1
set {_path} to new {File}("%parameter 2%")
{ImageIO}.write({_image}, "png" and {_path});
return
on script load:
if (fileExists("{@folder}"), fileExists("{@toppath}") and fileExists("{@bottompath}")) where [boolean input is false] is set:
try new {File}(getRelative("{@folder}")).mkdir();
access "saveImage" from getImage(getStreamFromBase64(getTopSB())) and getTop()
wait 1 tick
access "saveImage" from getImage(getStreamFromBase64(getBottomSB())) and getBottom()
send "<bold><green>Scoreboard Generator files generated" to console
on script load:
if fileExists("{@fontpath}"):
set {sbgen::font} to {Font}.createFont({Font}.PLAIN! and new {File}(getRelative("{@fontpath}")))
else:
send "<bold><green>Scoreboard Generator font does not exist (""{@fontpath}"")" to console
function registerColor(color: string, rgb: string):
loop split {_rgb} at ",":
add loop-value parsed as a number to {_l::*}
set {sbgen::colors::%{_color}%::*} to {_l::*}
set {sbgen::colors::%{_color}%} to {_color} # because caps matter
evt "register colors":
registerColor("BLACK", "0,0,0")
registerColor("DARK_BLUE", "0,0,170")
registerColor("DARK_GREEN", "0,170,0")
registerColor("DARK_AQUA", "0,170,170")
registerColor("DARK_RED", "170,0,0")
registerColor("DARK_PURPLE", "170,0,170")
registerColor("GOLD", "255,170,0")
registerColor("GRAY", "170,170,170")
registerColor("DARK_GRAY", "85,85,85")
registerColor("BLUE", "85,85,255")
registerColor("GREEN", "85,255,85")
registerColor("AQUA", "85,255,255")
registerColor("RED", "255,85,85")
registerColor("LIGHT_PURPLE", "255,85,255")
registerColor("YELLOW", "255,255,85")
registerColor("WHITE", "255,255,255")
function rgbNum(num: number) :: object:
return new {Float}("%{_num} / 255%")
function getJavaColor(rgb: numbers) :: object:
if size of {_rgb::*} is 3:
return new {Color}(rgbNum({_rgb::1}), rgbNum({_rgb::2}) and rgbNum({_rgb::3}))
return false
on script load:
delete {sbgen::colors::*}
call custom event "register colors"
function isColor(code: object) :: boolean:
if {_code} is a text:
set {_color} to getCode({_code})
else:
set {_color} to {_code}
if {_color} is set:
return {_color}.isColor()
return false
function uncolour(string: string) :: string:
return join split {_string} at getVanillaCode() with getBukkitCode()
function getFormattedString(string: string) :: object:
set {_string} to new {AttributedString}({_string})
return {_string}.getIterator()
function getHeight(i: int = 1) :: number:
set {_image} to getImage(getStreamFromBase64(getBottomSB()))
return {_image}.getHeight()
function getWidth(i: int = 1) :: number:
set {_image} to getImage(getStreamFromBase64(getBottomSB()))
return {_image}.getWidth()
#thanks snow-pyon
func "drawCenteredString":
set {_image} to parameter 1
set {_input} to parameter 2
set {_font} to parameter 3 ? getFont()
set {_image} to {ImageIO}.read(new {File}({_image}))
set {_image-width} to {_image}.getWidth()
set {_image-height} to {_image}.getHeight()
set {_graphics} to {_image}.getGraphics()
set {_metrics} to {_graphics}.getFontMetrics({_font})
set {_args::*} to 0, 0, {_image-width} and {_image-height}
set {_rectangle} to new {Rectangle}({_args::*})
set {_x} to {_rectangle}.x!
set {_width} to {_rectangle}.width!
set {_stringWidth} to {_metrics}.stringWidth({_input})
set {_x} to {_x} + (({_width} - {_stringWidth}) / 2)
set {_y} to {_rectangle}.y!
set {_height} to {_rectangle}.height!
set {_stringHeight} to {_metrics}.getHeight()
set {_ascent} to {_metrics}.getAscent()
set {_y} to {_y} + (({_height} - {_stringHeight}) / 2) + {_ascent}
{_graphics}.setFont({_font});
remove 3 from {_y}
{_graphics}.drawString({_input}, {_x} and {_y});
set transient "image" to {_image}
$ access
set {_image} to transient "image"
return
func "combineImages":
set {_p} to 1
while parameter {_p} is set:
add parameter {_p} to {_p::*}
add 1 to {_p}
set {_h} to getHeight()
set {_w} to getWidth()
set {_args::*} to {_w}, ({_h} * size of {_p::*}) and {BufferedImage}.TYPE_INT_ARGB!
set {_b} to new {BufferedImage}({_args::*})
set {_g} to getGraphics({_b})
set {_y} to 0
loop {_p::*}:
set {_args::*} to loop-value, 10, {_y} and null ref
{_g}.drawImage({_args::*});
add {_h} to {_y}
set transient "combined" to {_b}
$ access
set {_combined} to transient "combined"
return
command /t2 <text>:
trigger:
set {_bool} to getTop()
loop split arg-1 at ",":
add 1 to {_s}
access "drawCenteredString" from {_bool} and arg-1
{ImageIO}.write({_image}, "png" and new {File}("new%{_s}%.png"));
set {_bool} to getBottom()
add {_image} to {_images::*}
wait 1 tick
access "combineImages" from {_images::*}
wait 1 tick
access "saveImage" from {_combined} and "combined.png"
broadcast "1"
on packet event play_server_update_health:
wait 1 tick
set player's tablist name to join (first element out of (split player's tablist name at " " where [string input is not ""])) and "<yellow>%floor(health of player * 10)%" with " "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment