Skip to content

Commit

Permalink
v1.2 - Big update with some bugfixes
Browse files Browse the repository at this point in the history
Added expression to get image from locale file
Added effect to draw line
Added effect to draw rounded rectangle with custom arcs
Added DrawString effect to make the text center
Added image rounded expression

Fixed version configuration in the build.gradle
  • Loading branch information
BuildTools committed Jan 7, 2021
1 parent c979681 commit 521857d
Show file tree
Hide file tree
Showing 9 changed files with 322 additions and 5 deletions.
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ plugins {
}

group 'info.itsthesky'
version '1.1'
version '1.2'

def version = '1.2'

repositories {
mavenCentral()
Expand All @@ -28,6 +30,10 @@ processResources {
filter ReplaceTokens, tokens: [VERSION: version]
}

jar {
archiveName 'SkImage.jar'
}

dependencies {
implementation 'com.github.SkriptLang:Skript:2.5'
implementation 'org.spigotmc:spigot-api:1.16.4-R0.1-SNAPSHOT'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package info.itsthesky.SkImage.skript.effects;

import ch.njol.skript.Skript;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.lang.Effect;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser;
import ch.njol.util.Kleenean;
import org.bukkit.event.Event;

import java.awt.*;
import java.awt.image.BufferedImage;

@Name("Draw Line on Image")
@Description("Draw a line on a specific image (with color, location and size)")
@Examples("draw line with size 5 from 50, 30 to 60, 40 with color from rgb 150, 30, 233 on {_image}")
@Since("1.0")
public class EffDrawLine extends Effect {

static {
Skript.registerEffect(EffDrawLine.class,
"[skimage] draw line with [the] (size|width) %integer% from %integer%[ ][,][ ]%integer% to %integer%[ ][,][ ]%integer% with [(color|colored)] %imagecolor% on [the] [image] %image%");
}

private Expression<Integer> exprSize;
private Expression<Integer> exprFromX, exprFromY;
private Expression<Integer> exprToX, exprToY;
private Expression<Color> exprColor;
private Expression<BufferedImage> exprImage;

@SuppressWarnings("unchecked")
@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) {
exprSize = (Expression<Integer>) exprs[0];
exprFromX = (Expression<Integer>) exprs[1];
exprFromY = (Expression<Integer>) exprs[2];
exprToX = (Expression<Integer>) exprs[3];
exprToY = (Expression<Integer>) exprs[4];
exprColor = (Expression<Color>) exprs[5];
exprImage = (Expression<BufferedImage>) exprs[6];
return true;
}

@Override
protected void execute(Event e) {
Integer fromX = exprFromX.getSingle(e);
Integer fromY = exprFromY.getSingle(e);
Integer toX = exprToX.getSingle(e);
Integer toY = exprToY.getSingle(e);
Integer size = exprSize.getSingle(e);
Color color = exprColor.getSingle(e);
BufferedImage image = exprImage.getSingle(e);
if (color == null || image == null || size == null || fromX == null || fromY == null || toX == null || toY == null) return;
Graphics2D g2d = image.createGraphics();
g2d.setColor(color);
g2d.setStroke(new BasicStroke(size));
g2d.drawLine(fromX, fromY, toX, toY);
g2d.dispose();
}

@Override
public String toString(Event e, boolean debug) {
return "";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package info.itsthesky.SkImage.skript.effects;

import ch.njol.skript.Skript;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.lang.Effect;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser;
import ch.njol.util.Kleenean;
import org.bukkit.event.Event;

import java.awt.*;
import java.awt.image.BufferedImage;

@Name("Draw Rounded Rectangle on Image")
@Description("Draw a rectangle with rounded arc on a specific image (with color, location and size)")
@Examples("draw rounded rect at 0, 0 with size 50, 50 with color from rgb 50, 30, 60 with arc size 50, 50 on {_image}")
@Since("1.0")
public class EffDrawRoundRect extends Effect {

static {
Skript.registerEffect(EffDrawRoundRect.class,
"[skimage] draw round[ed] rect[angle] at [the [pixel] location] %integer%[ ][,][ ]%integer% with [the] size %integer%[ ][,][ ]%integer% with [(color|colored)] %imagecolor% with arc (size|pixel) %integer%[ ][,][ ]%integer% on [the] [image] %image%");
}

private Expression<Integer> exprX, exprY;
private Expression<Integer> exprSizeX, exprSizeY;
private Expression<Integer> exprArcX, exprArcY;
private Expression<Color> exprColor;
private Expression<BufferedImage> exprImage;

@SuppressWarnings("unchecked")
@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) {
exprX = (Expression<Integer>) exprs[0];
exprY = (Expression<Integer>) exprs[1];
exprSizeX = (Expression<Integer>) exprs[2];
exprSizeY = (Expression<Integer>) exprs[3];
exprColor = (Expression<Color>) exprs[4];
exprArcX = (Expression<Integer>) exprs[5];
exprArcY = (Expression<Integer>) exprs[6];
exprImage = (Expression<BufferedImage>) exprs[7];
return true;
}

@Override
protected void execute(Event e) {
Integer x = exprX.getSingle(e);
Integer y = exprY.getSingle(e);
Integer sizeX = exprSizeX.getSingle(e);
Integer sizeY = exprSizeY.getSingle(e);
Integer arcX = exprArcX.getSingle(e);
Integer arcY = exprArcY.getSingle(e);
Color color = exprColor.getSingle(e);
BufferedImage image = exprImage.getSingle(e);
if (x == null || y == null || sizeX == null || sizeY == null || color == null || image == null || arcX == null || arcY == null) return;
Graphics2D g2d = image.createGraphics();
g2d.setColor(color);
g2d.fillRoundRect(x, y, sizeX, sizeY, arcX, arcY);
g2d.dispose();
}

@Override
public String toString(Event e, boolean debug) {
return "draw rounded rectangle on " + exprImage.toString(e, debug) + " at " + exprX.toString(e, debug) + ", " + exprY.toString(e, debug) + " with size " + exprSizeX.toString(e, debug) + ", " + exprSizeY.toString(e, debug);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,27 @@
import org.bukkit.event.Event;

import java.awt.*;
import java.awt.font.TextLayout;
import java.awt.image.BufferedImage;

@Name("Draw Text on Image")
@Description("Draw text at specific pixel location and with specific color on image")
@Description("Draw text at specific pixel location and with specific color on image. Use `with align center` at the end to make the text center.")
@Examples("draw \"Hello World :D\" with font style {_font} at 0, 50 with color from rgb 50, 32, 12 on {_image}")
@Since("1.0")
public class EffDrawText extends Effect {

static {
Skript.registerEffect(EffDrawText.class,
"[skimage] draw [text] %string% with [the] font [style] %font% at [x] %integer%[ ](,|and)[ ][y] %integer% with [color] %imagecolor% on [the] [image] %image%");
"[skimage] draw [text] %string% with [the] font [style] %font% at [x] %integer%[ ](,|and)[ ][y] %integer% with [color] %imagecolor% on [the] [image] %image%",
"[skimage] draw [text] %string% with [the] font [style] %font% at [x] %integer%[ ](,|and)[ ][y] %integer% with [color] %imagecolor% on [the] [image] %image% with align center");
}

private Expression<String> exprText;
private Expression<Font> exprFont;
private Expression<Integer> exprX, exprY;
private Expression<Color> exprColor;
private Expression<BufferedImage> exprImage;
private Integer isCenter;

@SuppressWarnings("unchecked")
@Override
Expand All @@ -40,6 +43,7 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
exprY = (Expression<Integer>) exprs[3];
exprColor = (Expression<Color>) exprs[4];
exprImage = (Expression<BufferedImage>) exprs[5];
isCenter = matchedPattern;
return true;
}

Expand All @@ -55,7 +59,16 @@ protected void execute(Event e) {
Graphics2D g2d = image.createGraphics();
g2d.setFont(font);
g2d.setColor(color);
g2d.drawString(text, x, y);
if (isCenter == 0) {
g2d.drawString(text, x, y);
} else {
TextLayout textLayout = new TextLayout(text, g2d.getFont(),
g2d.getFontRenderContext());
double textHeight = textLayout.getBounds().getHeight();
double textWidth = textLayout.getBounds().getWidth();
g2d.drawString(text, (x / 2 - (int) textWidth / 2),
(y / 2 + (int) textHeight / 2));
}
g2d.dispose();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package info.itsthesky.SkImage.skript.expressions;

import ch.njol.skript.Skript;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.SkriptParser;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.util.Kleenean;
import org.bukkit.event.Event;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Path;
import java.nio.file.Paths;

@Name("Image from File")
@Description("Get and return an image from locale File")
@Examples("set {_image2} to image from file \"plugins/temp/image.png\"")
@Since("1.0")
public class ExprImageFromFile extends SimpleExpression<BufferedImage> {

static {
Skript.registerExpression(ExprImageFromFile.class, BufferedImage.class, ExpressionType.SIMPLE,
"[skimage] [the] image from [the] [file] %string%");
}

private Expression<String> exprFile;

@SuppressWarnings("unchecked")
@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) {
exprFile = (Expression<String>) exprs[0];
return true;
}

@Override
protected BufferedImage[] get(Event e) {
String file = exprFile.getSingle(e);
if (file == null) return new BufferedImage[0];

File image = new File(file);
if (!image.exists()) {
return new BufferedImage[0];
}
Path path = Paths.get(file);
try {
return new BufferedImage[] {ImageIO.read(path.toFile())};
} catch (IOException ioException) {
ioException.printStackTrace();
}
return new BufferedImage[0];
}

@Override
public boolean isSingle() {
return true;
}

@Override
public Class<? extends BufferedImage> getReturnType() {
return BufferedImage.class;
}

@Override
public String toString(Event e, boolean debug) {
return "image from the file " + exprFile.toString(e, debug);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package info.itsthesky.SkImage.skript.expressions;

import ch.njol.skript.Skript;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.SkriptParser;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.util.Kleenean;
import org.bukkit.event.Event;

import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;

@Name("Rounded Image")
@Description("Make an return the rounded version of the input Image")
@Examples("set {_image} to image rounded {_image}")
@Since("1.2")
public class ExprImageRounded extends SimpleExpression<BufferedImage> {

static {
Skript.registerExpression(ExprImageRounded.class, BufferedImage.class, ExpressionType.SIMPLE,
"[skimage] image (oval|roud[ed]) [of] %image%",
"[skimage] rounded image %image%");
}

private Expression<BufferedImage> exprImage;

@SuppressWarnings("unchecked")
@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) {
exprImage = (Expression<BufferedImage>) exprs[0];
return true;
}

@Override
protected BufferedImage[] get(Event e) {
BufferedImage image = exprImage.getSingle(e);
if (image == null) return new BufferedImage[0];
int w = image.getWidth();
int h = image.getHeight();
BufferedImage output = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);

Graphics2D g2 = output.createGraphics();

g2.setComposite(AlphaComposite.Src);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(Color.WHITE);
g2.fill(new RoundRectangle2D.Float(0, 0, w, h, Math.round(image.getWidth()), Math.round(image.getHeight())));
g2.setComposite(AlphaComposite.SrcAtop);
g2.drawImage(image, 0, 0, null);
g2.dispose();

return new BufferedImage[] {output};
}

@Override
public boolean isSingle() {
return true;
}

@Override
public Class<? extends BufferedImage> getReturnType() {
return BufferedImage.class;
}

@Override
public String toString(Event e, boolean debug) {
return "rounded version of image " + exprImage.toString(e, debug);
}

}
Loading

0 comments on commit 521857d

Please sign in to comment.