Skip to content

Commit dbf82bf

Browse files
Add files via upload
1 parent 094c895 commit dbf82bf

File tree

19 files changed

+624
-0
lines changed

19 files changed

+624
-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: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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+
*/
20+
@SuppressWarnings("unused")
21+
public abstract class CompHolder extends TextHolder {
22+
private static final boolean nativeAdventureSupport = getCurrentVersion() == 1165 && isPaper();
23+
24+
private static int getCurrentVersion() {
25+
Matcher matcher = Pattern
26+
.compile("(\\d+\\.\\d+)(\\.\\d+)?")
27+
.matcher(Bukkit.getBukkitVersion());
28+
29+
if (matcher.find()) {
30+
String version = matcher.group(1).replace(".", "");
31+
String patch = matcher.group(2);
32+
patch = patch == null ? "0" : patch.replace(".", "");
33+
return Integer.parseInt(version + patch);
34+
}
35+
36+
throw new RuntimeException(
37+
"Could not retrieve server version! \nFix: Install the server properly or add a WORKING version/jar."
38+
);
39+
}
40+
41+
private static boolean isPaper() {
42+
try { // PaperConfig
43+
Class.forName("com.destroystokyo.paper.PaperConfig");
44+
return true;
45+
} catch (ClassNotFoundException e) {
46+
return false;
47+
}
48+
}
49+
50+
private static final LegacyComponentSerializer legacySerializer = LegacyComponentSerializer.builder()
51+
.character(LegacyComponentSerializer.SECTION_CHAR)
52+
.useUnusualXRepeatedCharacterHexFormat()
53+
.hexColors()
54+
.build();
55+
56+
/**
57+
* Wraps the specified Adventure component.
58+
*
59+
* @param value the value to wrap
60+
* @return an instance that wraps the specified value
61+
*/
62+
@NotNull
63+
@Contract(pure = true)
64+
public static CompHolder of(@NotNull Component value) {
65+
Objects.requireNonNull(value, "value mustn't be null");
66+
return isNativeAdventureSupport() ? new NativeCompHolder(value) : new ForeignCompHolder(value);
67+
}
68+
69+
/**
70+
* Gets whether the server platform natively supports Adventure.
71+
* Native Adventure support means that eg. {@link ItemMeta#displayName(Component)}
72+
* is a valid method.
73+
*
74+
* @return whether the server platform natively supports Adventure
75+
* @since 0.10.0
76+
*/
77+
private static boolean isNativeAdventureSupport() {
78+
return nativeAdventureSupport;
79+
}
80+
81+
/**
82+
* Gets the serializer to use when converting wrapped values to legacy strings.
83+
* Main use case being the implementation of {@link #asLegacyString()}.
84+
*
85+
* @return a serializer for converting wrapped values to legacy strings
86+
* @since 0.10.0
87+
*/
88+
private static LegacyComponentSerializer legacySerializer() {
89+
return legacySerializer;
90+
}
91+
92+
/**
93+
* The Adventure component this instance wraps.
94+
*/
95+
@NotNull
96+
protected final Component value;
97+
98+
/**
99+
* Creates and initializes a new instance.
100+
*
101+
* @param value the Adventure component this instance should wrap
102+
* @since 0.10.0
103+
*/
104+
CompHolder(@NotNull Component value) {
105+
this.value = value;
106+
}
107+
108+
/**
109+
* Gets the Adventure component this instance wraps.
110+
*
111+
* @return the contained Adventure component
112+
* @since 0.10.0
113+
*/
114+
@NotNull
115+
@Contract(pure = true)
116+
public Component component() {
117+
return value;
118+
}
119+
120+
@NotNull
121+
@Override
122+
@Contract(pure = true)
123+
public String toString() {
124+
return legacySerializer.serialize(value);
125+
}
126+
127+
@Override
128+
public int hashCode() {
129+
return value.hashCode();
130+
}
131+
132+
@Override
133+
public boolean equals(Object other) {
134+
return other != null && getClass() == other.getClass()
135+
&& Objects.equals(value, ((CompHolder) other).value);
136+
}
137+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
*/
19+
class ForeignCompHolder extends CompHolder {
20+
@NotNull
21+
private final StringHolder legacy;
22+
23+
ForeignCompHolder(@NotNull Component value) {
24+
super(value);
25+
legacy = StringHolder.of(toString());
26+
}
27+
28+
@NotNull
29+
@Contract(pure = true)
30+
@Override
31+
public Inventory toInventory(InventoryHolder holder, InventoryType type) {
32+
return legacy.toInventory(holder, type);
33+
}
34+
35+
@NotNull
36+
@Contract(pure = true)
37+
@Override
38+
public Inventory toInventory(InventoryHolder holder, int size) {
39+
return legacy.toInventory(holder, size);
40+
}
41+
42+
@Override
43+
public void asItemDisplayName(ItemMeta meta) {
44+
legacy.asItemDisplayName(meta);
45+
}
46+
47+
@Override
48+
public void asItemLoreAtEnd(ItemMeta meta) {
49+
legacy.asItemLoreAtEnd(meta);
50+
}
51+
52+
@Override
53+
public void asItemLore(ItemMeta meta) {
54+
legacy.asItemLore(meta);
55+
}
56+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package me.flame.menus.adventure;
2+
3+
import net.kyori.adventure.text.Component;
4+
import org.bukkit.Bukkit;
5+
import org.bukkit.event.inventory.InventoryType;
6+
import org.bukkit.inventory.Inventory;
7+
import org.bukkit.inventory.InventoryHolder;
8+
import org.bukkit.inventory.Merchant;
9+
import org.bukkit.inventory.meta.ItemMeta;
10+
import org.jetbrains.annotations.Contract;
11+
import org.jetbrains.annotations.NotNull;
12+
13+
import java.util.ArrayList;
14+
import java.util.List;
15+
import java.util.Objects;
16+
17+
/**
18+
* A {@link CompHolder} implementation for platforms where Adventure is natively supported.
19+
* Adventure components are directly passed to the Bukkit (Paper) API.
20+
*
21+
* @see ForeignCompHolder
22+
* @since 0.10.0
23+
*/
24+
class NativeCompHolder extends CompHolder {
25+
26+
/**
27+
* Creates and initializes a new instance.
28+
*
29+
* @param value the Adventure component this instance should wrap
30+
* @since 0.10.0
31+
*/
32+
NativeCompHolder(@NotNull Component value) {
33+
super(value);
34+
}
35+
36+
@NotNull
37+
@Contract(pure = true)
38+
@Override
39+
public Inventory toInventory(InventoryHolder holder, InventoryType type) {
40+
return Bukkit.createInventory(holder, type, value);
41+
}
42+
43+
@NotNull
44+
@Contract(pure = true)
45+
@Override
46+
public Inventory toInventory(InventoryHolder holder, int size) {
47+
return Bukkit.createInventory(holder, size, value);
48+
}
49+
50+
@Override
51+
public void asItemDisplayName(ItemMeta meta) {
52+
meta.displayName(value);
53+
}
54+
55+
@Override
56+
public void asItemLoreAtEnd(ItemMeta meta) {
57+
List<Component> lore = meta.hasLore()
58+
? Objects.requireNonNull(meta.lore())
59+
: new ArrayList<>();
60+
lore.add(value);
61+
meta.lore(lore);
62+
}
63+
64+
@Override
65+
public void asItemLore(@NotNull ItemMeta meta) {
66+
List<Component> lore = new ArrayList<>();
67+
lore.add(value);
68+
meta.lore(lore);
69+
}
70+
}

0 commit comments

Comments
 (0)