Skip to content

Commit 4c506e0

Browse files
Added a few methods and Lore
To help with adventure support in State class.
1 parent 0a701ba commit 4c506e0

File tree

8 files changed

+749
-0
lines changed

8 files changed

+749
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<parent>
4+
<artifactId>core-project</artifactId>
5+
<groupId>me.flame.menus</groupId>
6+
<version>2.0.0</version>
7+
</parent>
8+
<modelVersion>4.0.0</modelVersion>
9+
<groupId>me.flame.menus.adventure</groupId>
10+
<artifactId>adventure</artifactId>
11+
<repositories>
12+
<repository>
13+
<id>paper-repo</id>
14+
<url>https://repo.papermc.io/repository/maven-public</url>
15+
</repository>
16+
</repositories>
17+
<dependencies>
18+
<dependency>
19+
<groupId>com.destroystokyo.paper</groupId>
20+
<artifactId>paper-api</artifactId>
21+
<version>1.16.5-R0.1-SNAPSHOT</version>
22+
<scope>provided</scope>
23+
</dependency>
24+
<dependency>
25+
<groupId>com.mojang</groupId>
26+
<artifactId>authlib</artifactId>
27+
<version>1.5.21</version>
28+
<scope>provided</scope>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.projectlombok</groupId>
32+
<artifactId>lombok</artifactId>
33+
<version>1.18.28</version>
34+
<scope>provided</scope>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.jetbrains</groupId>
38+
<artifactId>annotations</artifactId>
39+
<version>24.0.1</version>
40+
<scope>provided</scope>
41+
</dependency>
42+
<dependency>
43+
<groupId>com.google.errorprone</groupId>
44+
<artifactId>error_prone_annotations</artifactId>
45+
<version>2.18.0</version>
46+
<scope>provided</scope>
47+
</dependency>
48+
</dependencies>
49+
<properties>
50+
<maven.compiler.target>11</maven.compiler.target>
51+
<maven.compiler.source>11</maven.compiler.source>
52+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
53+
</properties>
54+
</project>

adventure/pom.xml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>me.flame.menus</groupId>
8+
<artifactId>core-project</artifactId>
9+
<version>2.0.0</version>
10+
</parent>
11+
12+
<groupId>me.flame.menus.adventure</groupId>
13+
<artifactId>adventure</artifactId>
14+
15+
<properties>
16+
<maven.compiler.source>11</maven.compiler.source>
17+
<maven.compiler.target>11</maven.compiler.target>
18+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
19+
</properties>
20+
21+
<repositories>
22+
<!-- paper mc repo -->
23+
<repository>
24+
<id>paper-repo</id>
25+
<url>https://repo.papermc.io/repository/maven-public</url>
26+
</repository>
27+
</repositories>
28+
29+
<dependencies>
30+
<dependency>
31+
<groupId>com.destroystokyo.paper</groupId>
32+
<artifactId>paper-api</artifactId>
33+
<version>1.16.5-R0.1-SNAPSHOT</version>
34+
<scope>provided</scope>
35+
</dependency>
36+
<dependency>
37+
<groupId>net.kyori</groupId>
38+
<artifactId>adventure-text-serializer-legacy</artifactId>
39+
<version>4.14.0</version>
40+
<scope>compile</scope>
41+
</dependency>
42+
<dependency>
43+
<groupId>net.kyori</groupId>
44+
<artifactId>adventure-api</artifactId>
45+
<version>4.14.0</version>
46+
<scope>compile</scope>
47+
</dependency>
48+
</dependencies>
49+
</project>
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
package me.flame.menus.adventure;
2+
3+
import net.kyori.adventure.text.Component;
4+
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
5+
6+
import org.apache.commons.lang3.StringUtils;
7+
import org.bukkit.Bukkit;
8+
import org.bukkit.inventory.meta.ItemMeta;
9+
10+
import org.jetbrains.annotations.Contract;
11+
import org.jetbrains.annotations.NotNull;
12+
13+
import java.util.Objects;
14+
import java.util.regex.Matcher;
15+
import java.util.regex.Pattern;
16+
17+
/**
18+
* Wrapper of an Adventure {@link Component}.
19+
* @author stefvanschie at <a href="https://github.com/stefvanschie/IF">IF's github.</a>
20+
*/
21+
@SuppressWarnings("unused")
22+
public abstract class CompHolder extends TextHolder {
23+
private static final boolean nativeAdventureSupport = getCurrentVersion() == 1165 && isPaper();
24+
25+
private static int getCurrentVersion() {
26+
Matcher matcher = Pattern
27+
.compile("(\\d+\\.\\d+)(\\.\\d+)?")
28+
.matcher(Bukkit.getBukkitVersion());
29+
30+
if (matcher.find()) {
31+
String version = matcher.group(1).replace(".", "");
32+
String patch = matcher.group(2);
33+
patch = patch == null ? "0" : patch.replace(".", "");
34+
return Integer.parseInt(version + patch);
35+
}
36+
37+
throw new RuntimeException(
38+
"Could not retrieve server version! \nFix: Install the server properly or add a WORKING version/jar."
39+
);
40+
}
41+
42+
private static boolean isPaper() {
43+
try { // PaperConfig
44+
Class.forName("com.destroystokyo.paper.PaperConfig");
45+
return true;
46+
} catch (ClassNotFoundException e) {
47+
return false;
48+
}
49+
}
50+
51+
private static final LegacyComponentSerializer legacySerializer = LegacyComponentSerializer.builder()
52+
.character(LegacyComponentSerializer.SECTION_CHAR)
53+
.useUnusualXRepeatedCharacterHexFormat()
54+
.hexColors()
55+
.build();
56+
57+
/**
58+
* Wraps the specified Adventure component.
59+
*
60+
* @param value the value to wrap
61+
* @return an instance that wraps the specified value
62+
*/
63+
@NotNull
64+
@Contract(pure = true)
65+
public static CompHolder of(@NotNull Component value) {
66+
Objects.requireNonNull(value, "value mustn't be null");
67+
return isNativeAdventureSupport() ? new NativeCompHolder(value) : new ForeignCompHolder(value);
68+
}
69+
70+
/**
71+
* Gets whether the server platform natively supports Adventure.
72+
* Native Adventure support means that eg. {@link ItemMeta#displayName(Component)}
73+
* is a valid method.
74+
*
75+
* @return whether the server platform natively supports Adventure
76+
* @since 0.10.0
77+
*/
78+
static boolean isNativeAdventureSupport() {
79+
return nativeAdventureSupport;
80+
}
81+
82+
/**
83+
* Gets the serializer to use when converting wrapped values to legacy strings.
84+
* Main use case being the implementation of {@link #toString()}.
85+
*
86+
* @return a serializer for converting wrapped values to legacy strings
87+
* @since 0.10.0
88+
*/
89+
static LegacyComponentSerializer legacySerializer() {
90+
return legacySerializer;
91+
}
92+
93+
/**
94+
* The Adventure component this instance wraps.
95+
*/
96+
@NotNull
97+
protected final Component value;
98+
99+
/**
100+
* Creates and initializes a new instance.
101+
*
102+
* @param value the Adventure component this instance should wrap
103+
* @since 0.10.0
104+
*/
105+
CompHolder(@NotNull Component value) {
106+
this.value = value;
107+
}
108+
109+
/**
110+
* Gets the Adventure component this instance wraps.
111+
*
112+
* @return the contained Adventure component
113+
* @since 0.10.0
114+
*/
115+
@NotNull
116+
@Contract(pure = true)
117+
public Component component() {
118+
return value;
119+
}
120+
121+
@NotNull
122+
@Override
123+
@Contract(pure = true)
124+
public String toString() {
125+
return legacySerializer.serialize(value);
126+
}
127+
128+
@Override
129+
public int hashCode() {
130+
return value.hashCode();
131+
}
132+
133+
@Override
134+
public boolean equals(Object other) {
135+
return other != null && getClass() == other.getClass()
136+
&& Objects.equals(value, ((CompHolder) other).value);
137+
}
138+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package me.flame.menus.adventure;
2+
3+
import net.kyori.adventure.text.Component;
4+
import org.bukkit.event.inventory.InventoryType;
5+
import org.bukkit.inventory.Inventory;
6+
import org.bukkit.inventory.InventoryHolder;
7+
import org.bukkit.inventory.Merchant;
8+
import org.bukkit.inventory.meta.ItemMeta;
9+
import org.jetbrains.annotations.Contract;
10+
import org.jetbrains.annotations.NotNull;
11+
12+
/**
13+
* A {@link CompHolder} implementation for platforms where Adventure isn't natively supported.
14+
* Adventure components are converted to legacy Strings before passed to the Bukkit API.
15+
*
16+
* @see NativeCompHolder
17+
* @since 0.10.0
18+
* @author stefvanschie at <a href="https://github.com/stefvanschie/IF">IF's github.</a>
19+
*/
20+
class ForeignCompHolder extends CompHolder {
21+
@NotNull
22+
private final StringHolder legacy;
23+
24+
ForeignCompHolder(@NotNull Component value) {
25+
super(value);
26+
legacy = StringHolder.of(toString());
27+
}
28+
29+
@NotNull
30+
@Contract(pure = true)
31+
@Override
32+
public Inventory toInventory(InventoryHolder holder, InventoryType type) {
33+
return legacy.toInventory(holder, type);
34+
}
35+
36+
@NotNull
37+
@Contract(pure = true)
38+
@Override
39+
public Inventory toInventory(InventoryHolder holder, int size) {
40+
return legacy.toInventory(holder, size);
41+
}
42+
43+
@Override
44+
public void asItemDisplayName(ItemMeta meta) {
45+
legacy.asItemDisplayName(meta);
46+
}
47+
48+
@Override
49+
public void asItemLoreAtEnd(ItemMeta meta) {
50+
legacy.asItemLoreAtEnd(meta);
51+
}
52+
53+
@Override
54+
public void asItemLore(ItemMeta meta) {
55+
legacy.asItemLore(meta);
56+
}
57+
58+
@Override
59+
public boolean contains(@NotNull TextHolder string) {
60+
return this.legacy.contains(string);
61+
}
62+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package me.flame.menus.adventure;
2+
3+
import net.kyori.adventure.text.Component;
4+
5+
import org.apache.commons.lang.Validate;
6+
7+
import org.bukkit.inventory.ItemStack;
8+
import org.bukkit.inventory.meta.ItemMeta;
9+
10+
import org.jetbrains.annotations.NotNull;
11+
12+
import java.util.ArrayList;
13+
import java.util.Iterator;
14+
import java.util.List;
15+
import java.util.function.Consumer;
16+
17+
@SuppressWarnings({ "deprecation", "ForLoopReplaceableByForEach" })
18+
public class Lore implements Iterable<TextHolder> {
19+
private final ItemMeta meta;
20+
private final List<TextHolder> lore;
21+
22+
private static final Lore EMPTY = new Lore(null);
23+
private static final List<TextHolder> EMPTY_LORE = new ArrayList<>(0);
24+
25+
public Lore(ItemMeta meta) {
26+
this.meta = meta;
27+
this.lore = (CompHolder.isNativeAdventureSupport()) ? lore(meta) : getLore(meta);
28+
}
29+
30+
private static @NotNull List<TextHolder> lore(ItemMeta meta) {
31+
if (meta == null || !meta.hasLore()) return EMPTY_LORE;
32+
List<Component> components = meta.lore();
33+
Validate.notNull(components);
34+
35+
int size = components.size();
36+
List<TextHolder> lore = new ArrayList<>(size);
37+
for (int componentIndex = 0; componentIndex < size; componentIndex++) {
38+
lore.add(CompHolder.of(components.get(componentIndex)));
39+
}
40+
return lore;
41+
}
42+
43+
private static @NotNull List<TextHolder> getLore(ItemMeta meta) {
44+
if (meta == null || !meta.hasLore()) return EMPTY_LORE;
45+
List<String> components = meta.getLore();
46+
Validate.notNull(components);
47+
48+
int size = components.size();
49+
List<TextHolder> lore = new ArrayList<>(size);
50+
for (int componentIndex = 0; componentIndex < size; componentIndex++) {
51+
lore.add(CompHolder.of(components.get(componentIndex)));
52+
}
53+
return lore;
54+
}
55+
56+
public static Lore empty() {
57+
return EMPTY;
58+
}
59+
60+
public int size() {
61+
return lore.size();
62+
}
63+
64+
public TextHolder get(int stringIndex) {
65+
return lore.get(stringIndex);
66+
}
67+
68+
@NotNull
69+
@Override
70+
public Iterator<TextHolder> iterator() {
71+
return lore.listIterator();
72+
}
73+
74+
@Override
75+
public void forEach(Consumer<? super TextHolder> action) {
76+
lore.forEach(action);
77+
}
78+
79+
public void set(int stringIndex, TextHolder of) {
80+
lore.set(stringIndex, of);
81+
}
82+
83+
public void toItemLore(ItemStack itemStack) {
84+
for (TextHolder textHolder : lore) textHolder.asItemLoreAtEnd(meta);
85+
itemStack.setItemMeta(meta);
86+
}
87+
}

0 commit comments

Comments
 (0)