Skip to content

Commit aafa1f9

Browse files
committed
Change to use more modern APIs
The new Title API is now implemented, however work needs to be done to stop using TextMessage, and instead use BaseComponent The Particle API no longer uses distance ignored, and instead checks for the relevant effects that should have distance ignored. This should be updated to be customizable to set a limit on the distance of the distance ignored effects, like Spigot
1 parent 8bea108 commit aafa1f9

File tree

4 files changed

+42
-100
lines changed

4 files changed

+42
-100
lines changed

Glowkit

src/main/java/net/glowstone/command/TitleCommand.java

+7-35
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
import org.bukkit.command.CommandSender;
66
import org.bukkit.command.defaults.BukkitCommand;
77
import org.bukkit.entity.Player;
8-
import org.bukkit.title.Title;
9-
import org.bukkit.title.TitleOptions;
8+
import org.github.paperspigot.Title;
109
import org.json.simple.JSONArray;
1110
import org.json.simple.JSONObject;
1211
import org.json.simple.JSONValue;
@@ -18,7 +17,7 @@ public class TitleCommand extends BukkitCommand {
1817
public TitleCommand() {
1918
super("title");
2019
this.description = "Sends a title to the specified player(s)";
21-
this.usageMessage = "/title <player> <title|subtitle|clear|reset|times> ...";
20+
this.usageMessage = "/title <player> <title|clear|reset> ...";
2221
this.setAliases(Arrays.<String>asList());
2322
this.setPermission("glowstone.command.title");
2423
}
@@ -85,6 +84,7 @@ private static ChatColor toColor(String name) {
8584
return null;
8685
}
8786

87+
// TODO: rework this to support all supported options for titles
8888
@Override
8989
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
9090
if (!testPermission(sender)) return true;
@@ -106,29 +106,7 @@ public boolean execute(CommandSender sender, String commandLabel, String[] args)
106106
} else if (args[1].equalsIgnoreCase("reset")) {
107107
player.resetTitle();
108108
sender.sendMessage("Reset " + player.getName() + "'s title");
109-
} else if (args[1].equalsIgnoreCase("times")) {
110-
if (args.length < 5) {
111-
sender.sendMessage(ChatColor.RED + "Usage: /title <player> times <fadeIn> <stay> <fadeOut>");
112-
return false;
113-
}
114-
115-
TitleOptions options = player.getTitleOptions();
116-
117-
int in = tryParseInt(sender, args[2], 0);
118-
int stay = tryParseInt(sender, args[3], in);
119-
int out = tryParseInt(sender, args[4], stay);
120-
121-
if (out == -1) {
122-
return false;
123-
}
124-
125-
options.setFadeInTime(in);
126-
options.setFadeOutTime(out);
127-
options.setVisibleTime(stay);
128-
player.setTitleOptions(options);
129-
130-
sender.sendMessage("Updated " + player.getName() + "'s title times");
131-
} else if (args[1].equalsIgnoreCase("title") || args[1].equalsIgnoreCase("subtitle")) {
109+
} else if (args[1].equalsIgnoreCase("title")) {
132110
if (args.length < 3) {
133111
sender.sendMessage(ChatColor.RED + "Usage: /title <player> " + args[1] + " <raw json>");
134112
return false;
@@ -149,17 +127,11 @@ public boolean execute(CommandSender sender, String commandLabel, String[] args)
149127

150128
String component = raw;
151129
Object parsed = JSONValue.parse(raw);
152-
if (parsed instanceof JSONObject)
130+
if (parsed instanceof JSONObject) {
153131
component = convertJson((JSONObject) parsed);
154-
155-
156-
Title currentTitle = player.getTitle();
157-
if (args[1].equalsIgnoreCase("title")) {
158-
currentTitle.setHeading(component);
159-
} else {
160-
currentTitle.setSubtitle(component);
161132
}
162-
player.setTitle(currentTitle, args[1].equalsIgnoreCase("title"));
133+
134+
player.sendTitle(new Title(component));
163135

164136
sender.sendMessage("Updated " + player.getName() + "'s title");
165137
} else {

src/main/java/net/glowstone/entity/GlowPlayer.java

+29-56
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import net.glowstone.net.message.login.LoginSuccessMessage;
2727
import net.glowstone.net.message.play.entity.*;
2828
import net.glowstone.net.message.play.game.*;
29+
import net.glowstone.net.message.play.game.TitleMessage.Action;
2930
import net.glowstone.net.message.play.inv.*;
3031
import net.glowstone.net.message.play.player.PlayerAbilitiesMessage;
3132
import net.glowstone.net.message.play.player.ResourcePackSendMessage;
@@ -37,7 +38,6 @@
3738
import net.glowstone.util.TextMessage;
3839
import net.glowstone.util.nbt.CompoundTag;
3940
import net.md_5.bungee.api.chat.BaseComponent;
40-
import org.apache.commons.lang3.StringUtils;
4141
import org.apache.commons.lang3.Validate;
4242
import org.bukkit.*;
4343
import org.bukkit.World.Environment;
@@ -62,10 +62,9 @@
6262
import org.bukkit.plugin.Plugin;
6363
import org.bukkit.plugin.messaging.StandardMessenger;
6464
import org.bukkit.scoreboard.Scoreboard;
65-
import org.bukkit.title.Title;
66-
import org.bukkit.title.TitleOptions;
6765
import org.bukkit.util.BlockVector;
6866
import org.bukkit.util.Vector;
67+
import org.github.paperspigot.Title;
6968
import org.json.simple.JSONObject;
7069

7170
import java.net.InetSocketAddress;
@@ -269,11 +268,7 @@ public void playEffect(Location location, Effect effect, int id, int data, float
269268
/**
270269
* The player's current title, if any
271270
*/
272-
private Title currentTitle = new Title();
273-
/**
274-
* The player's current title options
275-
*/
276-
private TitleOptions titleOptions = new TitleOptions();
271+
private Title currentTitle = new Title("");
277272
/**
278273
* The one block the player is currently digging.
279274
*/
@@ -1734,7 +1729,7 @@ public void playNote(Location loc, byte instrument, byte note) {
17341729
@Override
17351730
public void playEffect(Location loc, Effect effect, int data) {
17361731
int id = effect.getId();
1737-
boolean ignoreDistance = effect.isDistanceIgnored();
1732+
boolean ignoreDistance = effect == Effect.WITHER_SPAWN || effect == Effect.ENDERDRAGON_DIE;
17381733
session.send(new PlayEffectMessage(id, loc.getBlockX(), loc.getBlockY(), loc.getBlockZ(), data, ignoreDistance));
17391734
}
17401735

@@ -1852,62 +1847,73 @@ public void sendMap(MapView map) {
18521847

18531848
@Override
18541849
public void sendMessage(BaseComponent component) {
1855-
1850+
throw new UnsupportedOperationException("Not supported yet.");
18561851
}
18571852

18581853
@Override
18591854
public void sendMessage(BaseComponent... components) {
1860-
1855+
throw new UnsupportedOperationException("Not supported yet.");
18611856
}
18621857

18631858
@Override
18641859
public void setPlayerListHeaderFooter(BaseComponent[] header, BaseComponent[] footer) {
1865-
1860+
throw new UnsupportedOperationException("Not supported yet.");
18661861
}
18671862

18681863
@Override
18691864
public void setPlayerListHeaderFooter(BaseComponent header, BaseComponent footer) {
1870-
1865+
throw new UnsupportedOperationException("Not supported yet.");
18711866
}
18721867

18731868
@Override
18741869
public void setTitleTimes(int fadeInTicks, int stayTicks, int fadeOutTicks) {
1875-
1870+
currentTitle = new Title(currentTitle.getTitle(), currentTitle.getSubtitle(), fadeInTicks, stayTicks, fadeOutTicks);
18761871
}
18771872

18781873
@Override
18791874
public void setSubtitle(BaseComponent[] subtitle) {
1880-
1875+
currentTitle = new Title(currentTitle.getTitle(), subtitle, currentTitle.getFadeIn(), currentTitle.getStay(), currentTitle.getFadeOut());
18811876
}
18821877

18831878
@Override
18841879
public void setSubtitle(BaseComponent subtitle) {
1885-
1880+
currentTitle = new Title(currentTitle.getTitle(), new BaseComponent[]{subtitle}, currentTitle.getFadeIn(), currentTitle.getStay(), currentTitle.getFadeOut());
18861881
}
18871882

18881883
@Override
18891884
public void showTitle(BaseComponent[] title) {
1890-
1885+
sendTitle(new Title(title));
18911886
}
18921887

18931888
@Override
18941889
public void showTitle(BaseComponent title) {
1895-
1890+
sendTitle(new Title(title));
18961891
}
18971892

18981893
@Override
18991894
public void showTitle(BaseComponent[] title, BaseComponent[] subtitle, int fadeInTicks, int stayTicks, int fadeOutTicks) {
1900-
1895+
sendTitle(new Title(title, subtitle, fadeInTicks, stayTicks, fadeOutTicks));
19011896
}
19021897

19031898
@Override
19041899
public void showTitle(BaseComponent title, BaseComponent subtitle, int fadeInTicks, int stayTicks, int fadeOutTicks) {
1900+
sendTitle(new Title(title, subtitle, fadeInTicks, stayTicks, fadeOutTicks));
1901+
}
19051902

1903+
@Override
1904+
public void sendTitle(Title title) {
1905+
session.sendAll(TitleMessage.fromTitle(title));
19061906
}
19071907

19081908
@Override
1909-
public void hideTitle() {
1909+
public void updateTitle(Title title) {
1910+
sendTitle(title); // TODO: update existing title instead of sending a new title
1911+
}
19101912

1913+
@Override
1914+
public void hideTitle() {
1915+
currentTitle = new Title("");
1916+
session.send(new TitleMessage(TitleMessage.Action.CLEAR));
19111917
}
19121918

19131919
////////////////////////////////////////////////////////////////////////////
@@ -2371,48 +2377,15 @@ public Title getTitle() {
23712377
return currentTitle.clone();
23722378
}
23732379

2374-
@Override
2375-
public void setTitle(Title title) {
2376-
setTitle(title, false);
2377-
}
2378-
2379-
@Override
2380-
public TitleOptions getTitleOptions() {
2381-
return titleOptions.clone();
2382-
}
2383-
2384-
@Override
2385-
public void setTitleOptions(TitleOptions options) {
2386-
if (options == null) {
2387-
options = new TitleOptions();
2388-
}
2389-
titleOptions = options;
2390-
session.send(TitleMessage.fromOptions(titleOptions));
2391-
}
2392-
2393-
@Override
2394-
public void setTitle(Title title, boolean forceUpdate) {
2395-
Validate.notNull(title, "Title cannot be null");
2396-
2397-
String oldHeading = currentTitle.getHeading();
2398-
currentTitle = title;
2399-
2400-
if (forceUpdate || !StringUtils.equals(oldHeading, currentTitle.getHeading())) {
2401-
session.sendAll(TitleMessage.fromTitle(currentTitle));
2402-
}
2403-
}
2404-
24052380
@Override
24062381
public void clearTitle() {
2407-
currentTitle = new Title();
2408-
session.send(new TitleMessage(TitleMessage.Action.CLEAR));
2382+
session.send(new TitleMessage(Action.CLEAR));
24092383
}
24102384

24112385
@Override
24122386
public void resetTitle() {
2413-
currentTitle = new Title(currentTitle.getHeading());
2414-
titleOptions = new TitleOptions();
2415-
session.send(new TitleMessage(TitleMessage.Action.RESET));
2387+
currentTitle = new Title("");
2388+
session.send(new TitleMessage(Action.RESET));
24162389
}
24172390

24182391
public GlowBlock getDigging() {

src/main/java/net/glowstone/net/message/play/game/TitleMessage.java

+5-8
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import com.flowpowered.networking.Message;
44
import lombok.Data;
55
import net.glowstone.util.TextMessage;
6-
import org.bukkit.title.Title;
7-
import org.bukkit.title.TitleOptions;
6+
import net.md_5.bungee.api.chat.BaseComponent;
7+
import org.github.paperspigot.Title;
88

99
@Data
1010
public final class TitleMessage implements Message {
@@ -42,15 +42,12 @@ public TitleMessage(Action action) {
4242

4343
public static TitleMessage[] fromTitle(Title title) {
4444
return new TitleMessage[]{
45-
new TitleMessage(Action.TITLE, asTextMessage(title.getHeading())),
46-
new TitleMessage(Action.SUBTITLE, asTextMessage(title.getSubtitle()))
45+
new TitleMessage(Action.TITLE, asTextMessage(BaseComponent.toPlainText(title.getTitle()))),
46+
new TitleMessage(Action.SUBTITLE, asTextMessage(BaseComponent.toPlainText(title.getSubtitle()))),
47+
new TitleMessage(Action.TIMES, title.getFadeIn(), title.getStay(), title.getFadeOut())
4748
};
4849
}
4950

50-
public static TitleMessage fromOptions(TitleOptions options) {
51-
return new TitleMessage(Action.TIMES, options.getFadeInTime(), options.getVisibleTime(), options.getFadeOutTime());
52-
}
53-
5451
private static TextMessage asTextMessage(String rawString) {
5552
if (rawString == null) rawString = "";
5653
return new TextMessage(rawString);

0 commit comments

Comments
 (0)