Skip to content

Commit fba94a7

Browse files
Justis RootJustis Root
authored andcommitted
Initial commit
0 parents  commit fba94a7

File tree

5 files changed

+343
-0
lines changed

5 files changed

+343
-0
lines changed

pom.xml

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0" 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+
7+
<groupId>com.builtbybit</groupId>
8+
<artifactId>demo-server</artifactId>
9+
<version>0.0.1</version>
10+
11+
<name>demo-server</name>
12+
13+
<repositories>
14+
15+
<repository>
16+
<id>spigot-repo</id>
17+
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
18+
</repository>
19+
</repositories>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.spigotmc</groupId>
24+
<artifactId>spigot-api</artifactId>
25+
<version>1.18.1-R0.1-SNAPSHOT</version>
26+
<scope>provided</scope>
27+
</dependency>
28+
<dependency>
29+
<groupId>com.googlecode.json-simple</groupId>
30+
<artifactId>json-simple</artifactId>
31+
<version>1.1</version>
32+
</dependency>
33+
</dependencies>
34+
35+
<build>
36+
37+
<resources>
38+
<resource>
39+
<directory>src/main/resources</directory>
40+
<filtering>true</filtering>
41+
</resource>
42+
</resources>
43+
44+
<plugins>
45+
46+
<plugin>
47+
<groupId>org.apache.maven.plugins</groupId>
48+
<artifactId>maven-surefire-plugin</artifactId>
49+
<version>3.0.0-M5</version>
50+
</plugin>
51+
52+
<plugin>
53+
<groupId>org.apache.maven.plugins</groupId>
54+
<artifactId>maven-compiler-plugin</artifactId>
55+
<version>3.2</version>
56+
<configuration>
57+
<source>12</source>
58+
<target>12</target>
59+
</configuration>
60+
</plugin>
61+
62+
</plugins>
63+
</build>
64+
</project>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2022 BuiltByBit
3+
* All rights reserved.
4+
*/
5+
package com.builtbybit;
6+
7+
import org.bukkit.Bukkit;
8+
import org.bukkit.ChatColor;
9+
import org.bukkit.event.Listener;
10+
import org.bukkit.plugin.java.JavaPlugin;
11+
import org.bukkit.scheduler.BukkitRunnable;
12+
13+
public class DemoServerPlugin extends JavaPlugin {
14+
15+
@Override
16+
public void onEnable() {
17+
this.scheduleAnnouncement();
18+
this.registerEvent(new PlayerListener());
19+
}
20+
21+
private final void scheduleAnnouncement() {
22+
new BukkitRunnable() {
23+
private int minutesRemaining = 30;
24+
private String prefix = ChatColor.AQUA + "[BUILTBYBIT] " + ChatColor.GRAY;
25+
public void run() {
26+
switch (--minutesRemaining) {
27+
case 20:
28+
case 10:
29+
case 5:
30+
case 2:
31+
Bukkit.getServer().broadcastMessage(prefix + "Test Server shutting down in " + minutesRemaining + " minutes!");
32+
break;
33+
case 0:
34+
Bukkit.getServer().broadcastMessage(prefix + "Test Server shutting down momentarily!");
35+
}
36+
}
37+
}.runTaskTimerAsynchronously(this, 20 * 60, 20 * 60);
38+
}
39+
40+
private final void registerEvent(Listener listener) {
41+
this.getServer().getPluginManager().registerEvents(listener, this);
42+
}
43+
}
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
/*
2+
* Copyright 2022 BuiltByBit
3+
* All rights reserved.
4+
*/
5+
package com.builtbybit;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
import java.util.Locale;
10+
11+
import org.bukkit.Bukkit;
12+
import org.bukkit.ChatColor;
13+
import org.bukkit.Server;
14+
import org.bukkit.entity.Player;
15+
import org.json.simple.JSONObject;
16+
17+
public class JsonMessage {
18+
19+
private String msg;
20+
21+
/**
22+
* Create a new json message!
23+
*/
24+
public JsonMessage() {
25+
msg = "[{\"text\":\"\",\"extra\":[{\"text\": \"\"}";
26+
}
27+
28+
/**
29+
* Send the json string to all players on the server.
30+
*/
31+
public void send() {
32+
List<Object> players = new ArrayList<>();
33+
for (Player p : Bukkit.getOnlinePlayers()) players.add(p);
34+
send(players.toArray(new Player[players.size()]));
35+
}
36+
37+
/**
38+
* Send the json string to specified player(s)
39+
* @param player to send the message to.
40+
*/
41+
public void send(Player... player) {
42+
sendRawJson(msg + "]}]", player);
43+
}
44+
45+
/**
46+
* Send a raw json string to specified players.
47+
* @param json string to send
48+
* @param player to send the message to.
49+
*/
50+
public static void sendRawJson(String json, Player... player) {
51+
Server server = Bukkit.getServer();
52+
for (Player p : player)
53+
server.dispatchCommand(server.getConsoleSender(), "tellraw " + p.getName() + " " + json);
54+
}
55+
56+
/**
57+
* Append text to the json message.
58+
* @param text to be appended
59+
* @return json string builder
60+
*/
61+
public JsonStringBuilder append(String text) {
62+
return new JsonStringBuilder(this, esc(text));
63+
}
64+
65+
private static String esc(String s) {
66+
return JSONObject.escape(s);
67+
}
68+
/**
69+
*
70+
* @author JustisR
71+
*
72+
*/
73+
public static class JsonStringBuilder {
74+
75+
private final JsonMessage message;
76+
private final String string = ",{\"text\":\"\",\"extra\":[";
77+
private final String[] strings;
78+
private String hover = "", click = "";
79+
80+
/**
81+
* Settings for the json message's text
82+
* @param jsonMessage the original message
83+
* @param text the text to be appended to the message.
84+
*/
85+
private JsonStringBuilder(JsonMessage jsonMessage, String text) {
86+
message = jsonMessage;
87+
strings = colorized(text);
88+
}
89+
90+
/**
91+
* Set the hover event's action as showing a tooltip with the given text
92+
* @param lore the text to be displayed in the tooltip
93+
* @return the json string builder to which you are applying settings
94+
*/
95+
public JsonStringBuilder setHoverAsTooltip(String... lore) {
96+
StringBuilder builder = new StringBuilder();
97+
for (int i = 0; i < lore.length; i++)
98+
if (i + 1 == lore.length) builder.append(lore[i]);
99+
else builder.append(lore[i] + "\n");
100+
hover = ",\"hoverEvent\":{\"action\":\"show_text\",\"value\":\"" + esc(builder.toString()) + "\"}";
101+
return this;
102+
}
103+
104+
/**
105+
* Set the click event's action as redirecting to a URL
106+
* @param link to redirect to
107+
* @return the json string builder to which you are applying settings.
108+
*/
109+
public JsonStringBuilder setClickAsURL(String link) {
110+
click = ",\"clickEvent\":{\"action\":\"open_url\",\"value\":\"" + esc(link) + "\"}";
111+
return this;
112+
}
113+
114+
/**
115+
* Set the click event's action as suggesting a command
116+
* @param cmd to suggest
117+
* @return the json string builder to which you are applying settings;
118+
*/
119+
public JsonStringBuilder setClickAsSuggestCmd(String cmd) {
120+
click = ",\"clickEvent\":{\"action\":\"suggest_command\",\"value\":\"" + esc(cmd) + "\"}";
121+
return this;
122+
}
123+
124+
/**
125+
* Set the click event's action as executing a command
126+
* @param cmd
127+
* @return
128+
*/
129+
public JsonStringBuilder setClickAsExecuteCmd(String cmd) {
130+
click = ",\"clickEvent\":{\"action\":\"run_command\",\"value\":\"" + esc(cmd) + "\"}";
131+
return this;
132+
}
133+
134+
/**
135+
* Finalize the appending of the text, with settings.
136+
* @return
137+
*/
138+
public JsonMessage save() {
139+
StringBuilder builder = new StringBuilder(message.msg + string);
140+
for (String string : strings) {
141+
builder.append(string);
142+
}
143+
builder.append("]" + hover + click + "}");
144+
message.msg = builder.toString();
145+
return message;
146+
}
147+
}
148+
149+
private static final String[] colorized(String text) {
150+
String[] colors = text.split(String.valueOf(ChatColor.COLOR_CHAR));
151+
boolean bold = false, italic = false, magic = false, underlined = false, strikethrough = false;
152+
ChatColor color = ChatColor.WHITE;
153+
for (int i = 0; i < colors.length; i++) {
154+
if (i == 0 && !text.startsWith(String.valueOf(ChatColor.COLOR_CHAR))) {
155+
colors[i] = "{\"text\":\"" + colors[i] + "\"}";
156+
} else if (colors[i].length() < 1) {
157+
continue;
158+
} else {
159+
ChatColor decoded = ChatColor.getByChar(colors[i].substring(0, 1));
160+
switch (decoded) {
161+
case RESET:
162+
bold = false;
163+
italic = false;
164+
magic = false;
165+
underlined = false;
166+
strikethrough = false;
167+
color = ChatColor.WHITE;
168+
break;
169+
case BOLD:
170+
bold = true;
171+
break;
172+
case ITALIC:
173+
italic = true;
174+
break;
175+
case MAGIC:
176+
magic = true;
177+
break;
178+
case UNDERLINE:
179+
underlined = true;
180+
break;
181+
case STRIKETHROUGH:
182+
strikethrough = true;
183+
break;
184+
default:
185+
color = decoded;
186+
}
187+
StringBuilder builder = new StringBuilder("{\"text\":\"" + colors[i].substring(1, colors[i].length()) + "\"");
188+
if (color != ChatColor.WHITE) builder.append(",\"color\":\"" + color.name().toLowerCase(Locale.US) + "\"");
189+
if (bold) builder.append(",\"bold\":\"" + bold + "\"");
190+
if (italic) builder.append(",\"italic\":\"" + italic + "\"");
191+
if (magic) builder.append(",\"obfuscated\":\"" + magic + "\"");
192+
if (underlined) builder.append(",\"underlined\":\"" + underlined + "\"");
193+
if (strikethrough) builder.append(",\"strikethrough\":\"" + strikethrough + "\"");
194+
colors[i] = builder.append("}").toString();
195+
}
196+
if (i + 1 < colors.length) colors[i] = colors[i] + ",";
197+
}
198+
return colors;
199+
}
200+
201+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2022 BuiltByBit
3+
* All rights reserved.
4+
*/
5+
package com.builtbybit;
6+
7+
import org.bukkit.ChatColor;
8+
import org.bukkit.event.EventHandler;
9+
import org.bukkit.event.Listener;
10+
import org.bukkit.event.player.PlayerJoinEvent;
11+
import org.bukkit.event.server.ServerListPingEvent;
12+
13+
public class PlayerListener implements Listener {
14+
15+
@EventHandler
16+
public void onPing(ServerListPingEvent e) {
17+
e.setMotd(ChatColor.AQUA + "BuiltByBit " + ChatColor.GRAY + "Resource Test Server \n" + ChatColor.GREEN + "Powered by PebbleHost");
18+
}
19+
20+
@EventHandler
21+
public void onJoin(PlayerJoinEvent e) {
22+
e.getPlayer().setOp(true);
23+
new JsonMessage().append(ChatColor.AQUA + "[BUILTBYBIT]").setClickAsURL("https://builtbybit.com").save()
24+
.append(ChatColor.GRAY + " This is a BuiltByBit resource test server.").save()
25+
.send(e.getPlayer());
26+
e.getPlayer().sendMessage(ChatColor.GRAY + "You've been granted operator priveleges.");
27+
new JsonMessage().append(ChatColor.GRAY + "Hosting is provided in proud partnership with ").save()
28+
.append(ChatColor.GREEN + "PebbleHost").setClickAsURL("https://pebblehost.com").save()
29+
.send(e.getPlayer());
30+
}
31+
}

src/main/resources/plugin.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
name: DemoServer
2+
main: com.builtbybit.DemoServerPlugin
3+
author: BuiltByBit
4+
version: 1.0

0 commit comments

Comments
 (0)