Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.mrkirby153.quarxelnetwork.core.api.text;
- import me.mrkirby153.quarxelnetwork.core.api.Cuboid;
- import me.mrkirby153.quarxelnetwork.core.api.Direction;
- import org.bukkit.Location;
- import org.bukkit.Material;
- import org.bukkit.World;
- import org.bukkit.block.Block;
- import java.awt.*;
- import java.awt.font.FontRenderContext;
- import java.awt.geom.Rectangle2D;
- import java.awt.image.BufferedImage;
- public class BlockText {
- private final String text;
- private final Font font;
- private Cuboid cuboid;
- public BlockText(String text, Font font) {
- this.text = text;
- this.font = font;
- }
- public String getText() {
- return this.text;
- }
- public Font getFont() {
- return this.font;
- }
- public void generate(Direction generateDirection, Location location, Material material) {
- BufferedImage image = getImage();
- int color = 0;
- int startX = location.getBlockX();
- int startY = location.getBlockY();
- int startZ = location.getBlockZ();
- World w = location.getWorld();
- for (int y = 0; y < image.getHeight(); y++) {
- for (int x = 0; x < image.getWidth(); x++) {
- color = image.getRGB(x, y);
- if (Color.black.getRGB() != color)
- continue;
- // Actually draw the string
- Block b = null;
- switch (generateDirection) {
- case NORTH:
- b = w.getBlockAt(startX+x, startY+y, startZ);
- }
- if(b == null)
- continue;
- b.setType(material, false);
- }
- }
- }
- private BufferedImage getImage() {
- BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_4BYTE_ABGR);
- Graphics g = img.getGraphics();
- g.setFont(this.font);
- FontRenderContext frc = g.getFontMetrics().getFontRenderContext();
- Rectangle2D rect = this.font.getStringBounds(this.text, frc);
- g.dispose();
- img = new BufferedImage((int) Math.ceil(rect.getWidth()), (int) Math.ceil(rect.getHeight()), BufferedImage.TYPE_4BYTE_ABGR);
- g = img.getGraphics();
- g.setColor(Color.black);
- g.setFont(this.font);
- FontMetrics fm = g.getFontMetrics();
- int x = 0;
- int y = fm.getAscent();
- g.drawString(this.text, x, y);
- g.dispose();
- return img;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment