Skip to content

Commit b1bb5c1

Browse files
Clean up everything and support up to 1.17 with the Bukkit API (#21)
* Clean up everything and support up to 1.17 with the Bukkit API * Add Check for Updates config option * tweak * 1.8
1 parent 3f2cb06 commit b1bb5c1

File tree

13 files changed

+508
-341
lines changed

13 files changed

+508
-341
lines changed

README.md

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,34 @@ TitleAPI
44
[![Java CI with Maven](https://github.com/ConnorLinfoot/TitleAPI/actions/workflows/maven.yml/badge.svg)](https://github.com/ConnorLinfoot/TitleAPI/actions/workflows/maven.yml)
55
[![](https://jitpack.io/v/ConnorLinfoot/TitleAPI.svg)](https://jitpack.io/#ConnorLinfoot/TitleAPI)
66

7+
Simple to use library for sending Titles to players in Bukkit, supporting 1.8 - 1.17.
8+
9+
## Use Bukkit APIs
10+
11+
While this library does work with even the latest version of Bukkit, it is heavily recommended when possible to use the
12+
built-in APIs that have existed in Bukkit for over 5 years now.
13+
714
## Maven
815

9-
This library can be found on [JitPack](https://jitpack.io/#ConnorLinfoot/TitleAPI) for easy integration into maven
16+
This library can be found on [JitPack](https://jitpack.io/#ConnorLinfoot/TitleAPI) for easy integration into maven
1017
projects.
18+
1119
```xml
20+
1221
<repositories>
1322
<repository>
1423
<id>jitpack.io</id>
1524
<url>https://jitpack.io</url>
1625
</repository>
1726
</repositories>
18-
27+
1928
<dependencies>
20-
<dependency>
21-
<groupId>com.github.ConnorLinfoot</groupId>
22-
<artifactId>TitleAPI</artifactId>
23-
<version>1.7.6</version>
24-
</dependency>
25-
</dependencies>
29+
<dependency>
30+
<groupId>com.github.ConnorLinfoot</groupId>
31+
<artifactId>TitleAPI</artifactId>
32+
<version>1.8</version>
33+
</dependency>
34+
</dependencies>
2635
```
2736

2837
Spigot Page/More Info: http://www.spigotmc.org/resources/titleapi.1325/

pom.xml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.connorlinfoot</groupId>
88
<artifactId>TitleAPI</artifactId>
9-
<version>1.7.7</version>
9+
<version>1.8</version>
1010

1111
<properties>
1212
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
@@ -44,11 +44,6 @@
4444
</repository>
4545
</repositories>
4646
<dependencies>
47-
<dependency>
48-
<groupId>org.apache.commons</groupId>
49-
<artifactId>commons-io</artifactId>
50-
<version>1.3.2</version>
51-
</dependency>
5247
<dependency>
5348
<groupId>org.bukkit</groupId>
5449
<artifactId>bukkit</artifactId>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.connorlinfoot.titleapi;
2+
3+
import org.bukkit.entity.Player;
4+
5+
class BukkitTitleAPI implements InternalTitleAPI {
6+
7+
BukkitTitleAPI() {
8+
}
9+
10+
@Override
11+
public void sendTitle(Player player, String title, String subtitle, int fadeIn, int stay, int fadeOut) {
12+
player.sendTitle(title, subtitle, fadeIn, stay, fadeOut);
13+
}
14+
15+
@Override
16+
public void sendTabTitle(Player player, String header, String footer) {
17+
player.setPlayerListHeaderFooter(header, footer);
18+
}
19+
}

src/main/java/com/connorlinfoot/titleapi/CLUpdate.java

Lines changed: 113 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -17,126 +17,118 @@
1717
import java.net.HttpURLConnection;
1818
import java.net.URL;
1919

20-
public class CLUpdate implements Listener {
21-
private CLUpdate.UpdateResult result = CLUpdate.UpdateResult.DISABLED;
22-
private String version;
23-
private Plugin plugin;
24-
private String message = null;
25-
private String pluginMessage = null;
26-
private String updateMessage = null;
27-
private boolean updateAvailable = false;
28-
29-
public enum UpdateResult {
30-
NO_UPDATE,
31-
DISABLED,
32-
UPDATE_AVAILABLE
33-
}
34-
35-
public CLUpdate(JavaPlugin plugin) {
36-
this.plugin = plugin;
37-
Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() {
38-
@Override
39-
public void run() {
40-
doCheck();
41-
}
42-
});
43-
}
44-
45-
private void doCheck() {
46-
String data = null;
47-
String url = "http://api.connorlinfoot.com/v1/resource/release/" + plugin.getDescription().getName().toLowerCase() + "/";
48-
try {
49-
data = doCurl(url);
50-
} catch (IOException e) {
51-
e.printStackTrace();
52-
}
53-
JSONParser jsonParser = new JSONParser();
54-
try {
55-
JSONObject obj = (JSONObject) jsonParser.parse(data);
56-
if (obj.get("version") != null) {
57-
String newestVersion = (String) obj.get("version");
58-
String currentVersion = plugin.getDescription().getVersion().replaceAll("-SNAPSHOT-", "."); // Changes 4.0.0-SNAPSHOT-4 to 4.0.0.4
59-
if (Integer.parseInt(newestVersion.replace(".", "")) > Integer.parseInt(currentVersion.replace(".", ""))) {
60-
result = UpdateResult.UPDATE_AVAILABLE;
61-
version = (String) obj.get("version");
62-
} else {
63-
result = UpdateResult.NO_UPDATE;
64-
}
65-
if (obj.containsKey("message")) {
66-
this.message = ChatColor.translateAlternateColorCodes('&', (String) obj.get("message"));
67-
Bukkit.getConsoleSender().sendMessage(ChatColor.translateAlternateColorCodes('&', (String) obj.get("message")));
68-
}
69-
}
70-
} catch (ParseException e) {
71-
e.printStackTrace();
72-
}
73-
Bukkit.getScheduler().runTask(plugin, new Runnable() {
74-
@Override
75-
public void run() {
76-
handleResult();
77-
}
78-
});
79-
}
80-
81-
public String getVersion() {
82-
return version;
83-
}
84-
85-
public String doCurl(String urlString) throws IOException {
86-
URL url = new URL(urlString);
87-
HttpURLConnection con = (HttpURLConnection) url.openConnection();
88-
con.setRequestMethod("POST");
89-
con.setInstanceFollowRedirects(true);
90-
con.setDoOutput(true);
91-
con.setDoInput(true);
92-
DataOutputStream output = new DataOutputStream(con.getOutputStream());
93-
output.close();
94-
DataInputStream input = new DataInputStream(con.getInputStream());
95-
int c;
96-
StringBuilder resultBuf = new StringBuilder();
97-
while ((c = input.read()) != -1) {
98-
resultBuf.append((char) c);
99-
}
100-
input.close();
101-
return resultBuf.toString();
102-
}
103-
104-
public String getMessage() {
105-
return message;
106-
}
107-
108-
public void handleResult() {
109-
if (getMessage() != null) {
110-
pluginMessage = getMessage();
111-
}
112-
113-
switch (result) {
114-
default:
115-
case NO_UPDATE:
116-
updateAvailable = false;
117-
updateMessage = "No update was found, you are running the latest version.";
118-
break;
119-
case DISABLED:
120-
updateAvailable = false;
121-
updateMessage = "You currently have update checks disabled";
122-
break;
123-
case UPDATE_AVAILABLE:
124-
updateAvailable = true;
125-
updateMessage = "An update for " + plugin.getDescription().getName() + " is available, new version is " + getVersion() + ". Your installed version is " + plugin.getDescription().getVersion() + ".\nPlease update to the latest version :)";
126-
break;
127-
}
128-
129-
plugin.getLogger().info(updateMessage);
130-
}
131-
132-
@EventHandler
133-
public void onPlayerJoin(PlayerJoinEvent event) {
134-
if (updateAvailable && event.getPlayer().isOp()) {
135-
event.getPlayer().sendMessage(updateMessage);
136-
}
137-
if (pluginMessage != null && event.getPlayer().isOp()) {
138-
event.getPlayer().sendMessage(pluginMessage);
139-
}
140-
}
20+
class CLUpdate implements Listener {
21+
private CLUpdate.UpdateResult result = CLUpdate.UpdateResult.DISABLED;
22+
private String version;
23+
private Plugin plugin;
24+
private String message = null;
25+
private String pluginMessage = null;
26+
private String updateMessage = null;
27+
private boolean updateAvailable = false;
28+
29+
public enum UpdateResult {
30+
NO_UPDATE,
31+
DISABLED,
32+
UPDATE_AVAILABLE
33+
}
34+
35+
CLUpdate(JavaPlugin plugin) {
36+
this.plugin = plugin;
37+
Bukkit.getScheduler().runTaskAsynchronously(plugin, this::doCheck);
38+
}
39+
40+
private void doCheck() {
41+
String data;
42+
String url = "http://api.linfoot.dev/v1/resource/release/" + plugin.getDescription().getName().toLowerCase() + "/";
43+
try {
44+
data = doCurl(url);
45+
} catch (IOException e) {
46+
return;
47+
}
48+
49+
JSONParser jsonParser = new JSONParser();
50+
try {
51+
JSONObject obj = (JSONObject) jsonParser.parse(data);
52+
if (obj.get("version") != null) {
53+
String newestVersion = (String) obj.get("version");
54+
String currentVersion = plugin.getDescription().getVersion().replaceAll("-SNAPSHOT-", "."); // Changes 4.0.0-SNAPSHOT-4 to 4.0.0.4
55+
if (Integer.parseInt(newestVersion.replace(".", "")) > Integer.parseInt(currentVersion.replace(".", ""))) {
56+
result = UpdateResult.UPDATE_AVAILABLE;
57+
version = (String) obj.get("version");
58+
} else {
59+
result = UpdateResult.NO_UPDATE;
60+
}
61+
if (obj.containsKey("message")) {
62+
this.message = ChatColor.translateAlternateColorCodes('&', (String) obj.get("message"));
63+
Bukkit.getConsoleSender().sendMessage(ChatColor.translateAlternateColorCodes('&', (String) obj.get("message")));
64+
}
65+
}
66+
} catch (ParseException e) {
67+
return;
68+
}
69+
70+
Bukkit.getScheduler().runTask(plugin, this::handleResult);
71+
}
72+
73+
public String getVersion() {
74+
return version;
75+
}
76+
77+
public String doCurl(String urlString) throws IOException {
78+
URL url = new URL(urlString);
79+
HttpURLConnection con = (HttpURLConnection) url.openConnection();
80+
con.setRequestMethod("POST");
81+
con.setInstanceFollowRedirects(true);
82+
con.setDoOutput(true);
83+
con.setDoInput(true);
84+
DataOutputStream output = new DataOutputStream(con.getOutputStream());
85+
output.close();
86+
DataInputStream input = new DataInputStream(con.getInputStream());
87+
int c;
88+
StringBuilder resultBuf = new StringBuilder();
89+
while ((c = input.read()) != -1) {
90+
resultBuf.append((char) c);
91+
}
92+
input.close();
93+
return resultBuf.toString();
94+
}
95+
96+
public String getMessage() {
97+
return message;
98+
}
99+
100+
public void handleResult() {
101+
if (getMessage() != null) {
102+
pluginMessage = getMessage();
103+
}
104+
105+
switch (result) {
106+
default:
107+
case NO_UPDATE:
108+
updateAvailable = false;
109+
updateMessage = "No update was found, you are running the latest version.";
110+
break;
111+
case DISABLED:
112+
updateAvailable = false;
113+
updateMessage = "You currently have update checks disabled";
114+
break;
115+
case UPDATE_AVAILABLE:
116+
updateAvailable = true;
117+
updateMessage = "An update for " + plugin.getDescription().getName() + " is available, new version is " + getVersion() + ". Your installed version is " + plugin.getDescription().getVersion() + ".\nPlease update to the latest version :)";
118+
break;
119+
}
120+
121+
plugin.getLogger().info(updateMessage);
122+
}
123+
124+
@EventHandler
125+
public void onPlayerJoin(PlayerJoinEvent event) {
126+
if (updateAvailable && event.getPlayer().isOp()) {
127+
event.getPlayer().sendMessage(updateMessage);
128+
}
129+
if (pluginMessage != null && event.getPlayer().isOp()) {
130+
event.getPlayer().sendMessage(pluginMessage);
131+
}
132+
}
141133

142134
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.connorlinfoot.titleapi;
2+
3+
import java.util.logging.Level;
4+
5+
enum InternalAPIMapping {
6+
REFLECTION(ReflectionTitleAPI.class, MinecraftVersion.v1_12),
7+
BUKKIT(BukkitTitleAPI.class, MinecraftVersion.v1_17),
8+
;
9+
private final Class<? extends InternalTitleAPI> apiClass;
10+
private final MinecraftVersion maxVersion;
11+
12+
InternalAPIMapping(Class<? extends InternalTitleAPI> apiClass, MinecraftVersion maxVersion) {
13+
this.apiClass = apiClass;
14+
this.maxVersion = maxVersion;
15+
}
16+
17+
static InternalTitleAPI create() {
18+
MinecraftVersion version = MinecraftVersion.get();
19+
for (InternalAPIMapping thisOne : values()) {
20+
if (thisOne.maxVersion.isLessThanOrEqualTo(version)) {
21+
try {
22+
return thisOne.apiClass.newInstance();
23+
} catch (InstantiationException | IllegalAccessException e) {
24+
TitleAPI.instance.getLogger().log(Level.WARNING, "Failed to create InternalTitleAPI", e);
25+
}
26+
}
27+
}
28+
return null;
29+
}
30+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.connorlinfoot.titleapi;
2+
3+
import org.bukkit.entity.Player;
4+
5+
interface InternalTitleAPI {
6+
7+
void sendTitle(Player player, String title, String subtitle, int fadeIn, int stay, int fadeOut);
8+
9+
default void clearTitle(Player player) {
10+
sendTitle(player, "", "", 0, 0, 0);
11+
}
12+
13+
void sendTabTitle(Player player, String header, String footer);
14+
15+
}

0 commit comments

Comments
 (0)