First test with custom skulls for settings ui.

This commit is contained in:
DidlDimpfe committed 2023-05-27 12:30:44 +02:00
1 parent 7b1598a23a
commit 1e580b2c92
5 files changed
+138 -67

No files matched your search

+1 -1
View File
@@ -66,7 +66,7 @@
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<artifactId>spigot</artifactId>
<version>1.19.4-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
@@ -1,17 +1,18 @@
package de.philw.textgenerator.command;
import de.philw.textgenerator.letters.big.LetterConverter;
import de.philw.textgenerator.letters.big.GenerateUtil;
import de.philw.textgenerator.letters.big.LetterConverter;
import de.philw.textgenerator.manager.ConfigManager;
import de.philw.textgenerator.ui.SettingsUI;
import de.philw.textgenerator.utils.Direction;
import de.philw.textgenerator.utils.TextInstance;
import de.philw.textgenerator.utils.Validator;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.block.data.BlockData;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -71,6 +72,9 @@ public class TextGeneratorCommand extends Command {
if (args.length == 2 && args[0].equalsIgnoreCase("setDirection")) {
setDirection(player, args[1]);
}
if (args.length == 1 && args[0].equalsIgnoreCase("settings")) {
new SettingsUI(player);
}
}
@@ -120,7 +124,7 @@ public class TextGeneratorCommand extends Command {
}
private void setStart(Player player, String x, String y, String z) {
if (isNotNumeric(x) || isNotNumeric(y) || isNotNumeric(z)) {
if (!Validator.isInteger(x) || !Validator.isInteger(y) || !Validator.isInteger(z)) {
player.sendMessage(ChatColor.RED + "The coordinates have to be integers.");
return;
}
@@ -155,15 +159,4 @@ public class TextGeneratorCommand extends Command {
lastChanges.remove(lastChanges.size()-1);
}
// UTILITY METHODS
private boolean isNotNumeric(String str) {
try {
Integer.parseInt(str);
return false;
} catch(NumberFormatException e){
return true;
}
}
}
@@ -1,5 +1,6 @@
package de.philw.textgenerator.letters.big;
import de.philw.textgenerator.manager.ConfigManager;
import de.philw.textgenerator.utils.Direction;
import de.philw.textgenerator.utils.TextInstance;
import org.bukkit.Bukkit;
@@ -16,62 +17,100 @@ import java.util.Objects;
public class GenerateUtil {
private static TextInstance textInstance;
private static boolean[][] blocks;
public static void setTextInstance(TextInstance textInstance) {
GenerateUtil.textInstance = textInstance;
}
public static void buildBlocks(BufferedImage textInPicture) {
boolean[][] blocks = getBlocks(textInPicture);
public static void setBlocks(boolean[][] blocks) {
GenerateUtil.blocks = blocks;
}
public static void buildBlocks() {
for (int heightIndex = 0; heightIndex < blocks.length; heightIndex++) {
for (int widthIndex = 0; widthIndex < blocks[0].length; widthIndex++) {
if (blocks[heightIndex][widthIndex]) {
Objects.requireNonNull(
textInstance.getStartLocation().getWorld()).getBlockAt(Objects.requireNonNull(editStartLocation(textInstance.getStartLocation(), widthIndex, heightIndex, textInstance.getDirection()))).
setBlockData(Bukkit.createBlockData(textInstance.getBlock().getNormalBlockData()));
}
try {
if (blocks[heightIndex][widthIndex]) {
Objects.requireNonNull(
textInstance.getStartLocation().getWorld()).getBlockAt(Objects.requireNonNull(editStartLocation(textInstance.getStartLocation(), widthIndex, heightIndex, textInstance.getDirection()))).
setBlockData(Bukkit.createBlockData(textInstance.getBlock().getNormalBlockData()));
}
} catch (IndexOutOfBoundsException ignored) {}
}
}
}
public static HashMap<Location, BlockData> getAffectedBlocks(BufferedImage textInPicture) {
public static HashMap<Location, BlockData> getAffectedBlocks() {
HashMap<Location, BlockData> affectedBlocks = new HashMap<>();
boolean[][] blocks = getBlocks(textInPicture);
for (int heightIndex = 0; heightIndex < blocks.length; heightIndex++) {
for (int widthIndex = 0; widthIndex < blocks[0].length; widthIndex++) {
Block block = Objects.requireNonNull(textInstance.getStartLocation().getWorld()).getBlockAt(
Objects.requireNonNull(editStartLocation(textInstance.getStartLocation(), widthIndex, heightIndex, textInstance.getDirection())));
affectedBlocks.put(block.getLocation(), block.getBlockData());
affectedBlocks.put(block.getLocation(), block.getBlockData());
}
}
return affectedBlocks;
}
private static boolean[][] getBlocks(BufferedImage textInPicture) {
boolean[][] blocks = new boolean[textInPicture.getHeight()][textInPicture.getWidth()];
for (int heightIndex = 0; heightIndex < textInPicture.getHeight(); heightIndex++) {
for (int widthIndex = 0; widthIndex < textInPicture.getWidth(); widthIndex++) {
int rgb = textInPicture.getRGB(widthIndex, heightIndex);
if (rgb == -1) {
blocks[heightIndex][widthIndex] = false;
} else if (rgb == -16777216){
blocks[heightIndex][widthIndex] = true;
public static boolean[][] getBlocks(ArrayList<BufferedImage> linesAsBufferedImages) {
ArrayList<boolean[][]> lines = new ArrayList<>();
// find out the biggest width
int width = 0;
for (BufferedImage bufferedImage: linesAsBufferedImages) {
if (bufferedImage.getWidth() > width) width = bufferedImage.getWidth();
}
boolean[][] biggest = null;
// loop through all linesAsBufferedImages, make 2D-Boolean arrays out of it (what block has to be placed and what not)
// and reduce them
for (BufferedImage bufferedImage: linesAsBufferedImages) {
boolean[][] tempLine = new boolean[bufferedImage.getHeight()][width];
for (int heightIndex = 0; heightIndex < bufferedImage.getHeight(); heightIndex++) {
for (int widthIndex = 0; widthIndex < bufferedImage.getWidth(); widthIndex++) {
int rgb = bufferedImage.getRGB(widthIndex, heightIndex);
tempLine[heightIndex][widthIndex] = rgb != -1;
}
}
int alreadyRemoved = 0;
for (int columnIndex: getToRemoveColumns(tempLine)) {
tempLine = removeColumn(tempLine, columnIndex - alreadyRemoved);
alreadyRemoved++;
}
alreadyRemoved = 0;
for (int rowIndex: getToRemoveRows(tempLine)) {
removeRow(tempLine, rowIndex - alreadyRemoved);
alreadyRemoved++;
}
if (bufferedImage.getWidth() == width) {
biggest = tempLine;
}
lines.add(tempLine);
}
int alreadyRemoved = 0;
for (int columnIndex: getToRemoveColumns(blocks)) {
blocks = removeColumn(blocks, columnIndex-alreadyRemoved);
alreadyRemoved++;
// make all 2D-Boolean arrays the same width
for (int arrayIndex = 0; arrayIndex < lines.size(); arrayIndex++) {
boolean[][] oldLine = lines.get(arrayIndex);
if (oldLine == biggest) continue;
boolean[][] doneLine = new boolean[oldLine.length][width];
for (int column = 0; column < oldLine.length; column++) {
System.arraycopy(oldLine[column], 0, doneLine[column], 0, oldLine[0].length);
}
lines.set(arrayIndex, doneLine);
}
alreadyRemoved = 0;
for (int rowIndex: getToRemoveRows(blocks)) {
removeRow(blocks, rowIndex - alreadyRemoved);
alreadyRemoved++;
// merge the 2D-Boolean arrays to a big 2D-Boolean considering the in the config given spaceBetweenEachLine
int height = 0;
int spaceBetweenEachLine = ConfigManager.getSpaceBetweenEachLine();
for (boolean[][] doneLine: lines) {
height += doneLine.length + spaceBetweenEachLine;
}
boolean[][] blocks = new boolean[height][width];
int currentLineIndexCount = 0;
for (boolean[][] doneLine: lines) {
for (boolean[] booleans : doneLine) {
blocks[currentLineIndexCount] = booleans;
currentLineIndexCount++;
}
currentLineIndexCount += spaceBetweenEachLine;
}
return blocks;
}
@@ -111,27 +150,6 @@ public class GenerateUtil {
start = false;
}
}
// Remove the spaces between each line
int spaceLineCount = 0;
for (int column = 0; column < blocks.length; column++) {
boolean reduce = true;
for (int row = 0; row < blocks[0].length; row++) {
if (blocks[column][row]) {
reduce = false;
spaceLineCount = 0;
break;
}
}
if (!reduce) continue;
// to here the column is empty
if (toRemoveColumns.contains(column)) continue;
// to here the column is not already removed
spaceLineCount++;
if (spaceLineCount > textInstance.getSpaceBetweenEachLine()) {
toRemoveColumns.add(column);
}
}
Collections.sort(toRemoveColumns);
return toRemoveColumns;
}
@@ -200,4 +218,4 @@ public class GenerateUtil {
return null;
}
}
}
@@ -0,0 +1,53 @@
package de.philw.textgenerator.ui;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property;
import de.philw.textgenerator.utils.SkullData;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.SkullMeta;
import java.lang.reflect.Field;
import java.util.UUID;
public class SettingsUI {
private Inventory inventory;
private Player player;
public SettingsUI(Player player) {
inventory = Bukkit.createInventory(null, 45);
this.player = player;
addSize();
player.openInventory(inventory);
}
public void addSize() {
inventory.addItem(getPlayerHeadByString(SkullData.fontSize));
}
public ItemStack getPlayerHeadByString(String string) {
ItemStack skullItemStack = new ItemStack(Material.PLAYER_HEAD);
SkullMeta skullItemMeta = (SkullMeta) skullItemStack.getItemMeta();
GameProfile profile = new GameProfile(UUID.randomUUID(), null);
profile.getProperties().put("textures", new Property("textures", string));
Field field;
try {
field = skullItemMeta.getClass().getDeclaredField("profile");
field.setAccessible(true);
field.set(skullItemMeta, profile);
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
return null;
}
skullItemStack.setItemMeta(skullItemMeta);
return skullItemStack;
}
}
@@ -0,0 +1,7 @@
package de.philw.textgenerator.utils;
public class SkullData {
public static final String fontSize = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvZmQxNjk0YzM4MDA5YWMxMWIwNmU3ZDg5ZTZmYjE5OGI2MTA3ODNhMzA4NjBmZDE2YTMyZGJlNmQ3OWExM2Q4In19fQ==";
}