Skip to content

Commit

Permalink
add HEX support
Browse files Browse the repository at this point in the history
  • Loading branch information
zitreF committed Aug 4, 2023
1 parent df6ba54 commit 32410e1
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 3 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = 'me.cocos'
version = '1.1'
version = '1.2'

compileJava.options.encoding = 'UTF-8'

Expand Down Expand Up @@ -71,7 +71,7 @@ publishing {
maven(MavenPublication) {
groupId = 'me.cocos'
artifactId = 'cocosgui'
version = '1.1'
version = '1.2'

from components.java
}
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/me/cocos/gui/gui/Gui.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ public void addItems(GuiItem... guiItems) {
}
}

public void addItem(GuiItem item, int times) {
for (int i = 0; i < times; i++) {
this.setItem(this.inventory.firstEmpty(), item);
}
}

public GuiItem getGuiItem(int slot) {
return this.actions.get(slot);
}
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/me/cocos/gui/helper/ChatHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
import org.bukkit.ChatColor;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public final class ChatHelper {

private static final Pattern HEX_PATTERN = Pattern.compile("&#(\\w{5}[0-9a-f])");


private ChatHelper() {
throw new UnsupportedOperationException("You cannot do this!");
}
Expand All @@ -14,7 +19,24 @@ public static String colored(String text) {
return ChatColor.translateAlternateColorCodes('&', text);
}

public static String coloredHex(String text) {
Matcher matcher = HEX_PATTERN.matcher(text);

StringBuilder builder = new StringBuilder();

while (matcher.find()) {
matcher.appendReplacement(builder, net.md_5.bungee.api.ChatColor.of("#" + matcher.group(1)).toString());
}

return ChatColor.translateAlternateColorCodes('&', matcher.appendTail(builder).toString());

}

public static List<String> colored(List<String> lines) {
return lines.stream().map(ChatHelper::colored).toList();
}

public static List<String> coloredHex(List<String> lines) {
return lines.stream().map(ChatHelper::coloredHex).toList();
}
}
4 changes: 3 additions & 1 deletion src/test/java/command/TestCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ public class TestCommand implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
Player player = (Player) sender;
testMenu.openSecondGui(player);
testMenu.openThirdGui(player);
return false;
}


}
15 changes: 15 additions & 0 deletions src/test/java/menu/TestMenu.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package menu;

import me.cocos.gui.builder.gui.GuiBuilder;
import me.cocos.gui.builder.item.impl.ItemBuilder;
import me.cocos.gui.data.GuiItem;
import me.cocos.gui.structure.Structure;
import me.cocos.gui.gui.Gui;
Expand Down Expand Up @@ -50,4 +51,18 @@ public void openSecondGui(Player player) {
.build();
gui.open(player);
}

public void openThirdGui(Player player) {
List<GuiItem> randomList = Arrays.stream(Material.values())
.filter(material -> material != Material.AIR)
.limit(10)
.map(GuiItem::of)
.toList();
Gui gui = GuiBuilder.normal("&7Test 2", 3)
.onClick((event, p) -> event.setCancelled(true))
.disposable(true)
.build();
gui.setItem(0, ItemBuilder.from(Material.TERRACOTTA).asGuiItem());
gui.open(player);
}
}

0 comments on commit 32410e1

Please sign in to comment.