Skip to content

Commit

Permalink
Change to use more modern APIs
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mastercoms committed Mar 1, 2016
1 parent 8bea108 commit aafa1f9
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 100 deletions.
2 changes: 1 addition & 1 deletion Glowkit
42 changes: 7 additions & 35 deletions src/main/java/net/glowstone/command/TitleCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
import org.bukkit.command.CommandSender;
import org.bukkit.command.defaults.BukkitCommand;
import org.bukkit.entity.Player;
import org.bukkit.title.Title;
import org.bukkit.title.TitleOptions;
import org.github.paperspigot.Title;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
Expand All @@ -18,7 +17,7 @@ public class TitleCommand extends BukkitCommand {
public TitleCommand() {
super("title");
this.description = "Sends a title to the specified player(s)";
this.usageMessage = "/title <player> <title|subtitle|clear|reset|times> ...";
this.usageMessage = "/title <player> <title|clear|reset> ...";
this.setAliases(Arrays.<String>asList());
this.setPermission("glowstone.command.title");
}
Expand Down Expand Up @@ -85,6 +84,7 @@ private static ChatColor toColor(String name) {
return null;
}

// TODO: rework this to support all supported options for titles
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
if (!testPermission(sender)) return true;
Expand All @@ -106,29 +106,7 @@ public boolean execute(CommandSender sender, String commandLabel, String[] args)
} else if (args[1].equalsIgnoreCase("reset")) {
player.resetTitle();
sender.sendMessage("Reset " + player.getName() + "'s title");
} else if (args[1].equalsIgnoreCase("times")) {
if (args.length < 5) {
sender.sendMessage(ChatColor.RED + "Usage: /title <player> times <fadeIn> <stay> <fadeOut>");
return false;
}

TitleOptions options = player.getTitleOptions();

int in = tryParseInt(sender, args[2], 0);
int stay = tryParseInt(sender, args[3], in);
int out = tryParseInt(sender, args[4], stay);

if (out == -1) {
return false;
}

options.setFadeInTime(in);
options.setFadeOutTime(out);
options.setVisibleTime(stay);
player.setTitleOptions(options);

sender.sendMessage("Updated " + player.getName() + "'s title times");
} else if (args[1].equalsIgnoreCase("title") || args[1].equalsIgnoreCase("subtitle")) {
} else if (args[1].equalsIgnoreCase("title")) {
if (args.length < 3) {
sender.sendMessage(ChatColor.RED + "Usage: /title <player> " + args[1] + " <raw json>");
return false;
Expand All @@ -149,17 +127,11 @@ public boolean execute(CommandSender sender, String commandLabel, String[] args)

String component = raw;
Object parsed = JSONValue.parse(raw);
if (parsed instanceof JSONObject)
if (parsed instanceof JSONObject) {
component = convertJson((JSONObject) parsed);


Title currentTitle = player.getTitle();
if (args[1].equalsIgnoreCase("title")) {
currentTitle.setHeading(component);
} else {
currentTitle.setSubtitle(component);
}
player.setTitle(currentTitle, args[1].equalsIgnoreCase("title"));

player.sendTitle(new Title(component));

sender.sendMessage("Updated " + player.getName() + "'s title");
} else {
Expand Down
85 changes: 29 additions & 56 deletions src/main/java/net/glowstone/entity/GlowPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import net.glowstone.net.message.login.LoginSuccessMessage;
import net.glowstone.net.message.play.entity.*;
import net.glowstone.net.message.play.game.*;
import net.glowstone.net.message.play.game.TitleMessage.Action;
import net.glowstone.net.message.play.inv.*;
import net.glowstone.net.message.play.player.PlayerAbilitiesMessage;
import net.glowstone.net.message.play.player.ResourcePackSendMessage;
Expand All @@ -37,7 +38,6 @@
import net.glowstone.util.TextMessage;
import net.glowstone.util.nbt.CompoundTag;
import net.md_5.bungee.api.chat.BaseComponent;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.bukkit.*;
import org.bukkit.World.Environment;
Expand All @@ -62,10 +62,9 @@
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.messaging.StandardMessenger;
import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.title.Title;
import org.bukkit.title.TitleOptions;
import org.bukkit.util.BlockVector;
import org.bukkit.util.Vector;
import org.github.paperspigot.Title;
import org.json.simple.JSONObject;

import java.net.InetSocketAddress;
Expand Down Expand Up @@ -269,11 +268,7 @@ public void playEffect(Location location, Effect effect, int id, int data, float
/**
* The player's current title, if any
*/
private Title currentTitle = new Title();
/**
* The player's current title options
*/
private TitleOptions titleOptions = new TitleOptions();
private Title currentTitle = new Title("");
/**
* The one block the player is currently digging.
*/
Expand Down Expand Up @@ -1734,7 +1729,7 @@ public void playNote(Location loc, byte instrument, byte note) {
@Override
public void playEffect(Location loc, Effect effect, int data) {
int id = effect.getId();
boolean ignoreDistance = effect.isDistanceIgnored();
boolean ignoreDistance = effect == Effect.WITHER_SPAWN || effect == Effect.ENDERDRAGON_DIE;
session.send(new PlayEffectMessage(id, loc.getBlockX(), loc.getBlockY(), loc.getBlockZ(), data, ignoreDistance));
}

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

@Override
public void sendMessage(BaseComponent component) {

throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public void sendMessage(BaseComponent... components) {

throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public void setPlayerListHeaderFooter(BaseComponent[] header, BaseComponent[] footer) {

throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public void setPlayerListHeaderFooter(BaseComponent header, BaseComponent footer) {

throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public void setTitleTimes(int fadeInTicks, int stayTicks, int fadeOutTicks) {

currentTitle = new Title(currentTitle.getTitle(), currentTitle.getSubtitle(), fadeInTicks, stayTicks, fadeOutTicks);
}

@Override
public void setSubtitle(BaseComponent[] subtitle) {

currentTitle = new Title(currentTitle.getTitle(), subtitle, currentTitle.getFadeIn(), currentTitle.getStay(), currentTitle.getFadeOut());
}

@Override
public void setSubtitle(BaseComponent subtitle) {

currentTitle = new Title(currentTitle.getTitle(), new BaseComponent[]{subtitle}, currentTitle.getFadeIn(), currentTitle.getStay(), currentTitle.getFadeOut());
}

@Override
public void showTitle(BaseComponent[] title) {

sendTitle(new Title(title));
}

@Override
public void showTitle(BaseComponent title) {

sendTitle(new Title(title));
}

@Override
public void showTitle(BaseComponent[] title, BaseComponent[] subtitle, int fadeInTicks, int stayTicks, int fadeOutTicks) {

sendTitle(new Title(title, subtitle, fadeInTicks, stayTicks, fadeOutTicks));
}

@Override
public void showTitle(BaseComponent title, BaseComponent subtitle, int fadeInTicks, int stayTicks, int fadeOutTicks) {
sendTitle(new Title(title, subtitle, fadeInTicks, stayTicks, fadeOutTicks));
}

@Override
public void sendTitle(Title title) {
session.sendAll(TitleMessage.fromTitle(title));
}

@Override
public void hideTitle() {
public void updateTitle(Title title) {
sendTitle(title); // TODO: update existing title instead of sending a new title
}

@Override
public void hideTitle() {
currentTitle = new Title("");
session.send(new TitleMessage(TitleMessage.Action.CLEAR));
}

////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -2371,48 +2377,15 @@ public Title getTitle() {
return currentTitle.clone();
}

@Override
public void setTitle(Title title) {
setTitle(title, false);
}

@Override
public TitleOptions getTitleOptions() {
return titleOptions.clone();
}

@Override
public void setTitleOptions(TitleOptions options) {
if (options == null) {
options = new TitleOptions();
}
titleOptions = options;
session.send(TitleMessage.fromOptions(titleOptions));
}

@Override
public void setTitle(Title title, boolean forceUpdate) {
Validate.notNull(title, "Title cannot be null");

String oldHeading = currentTitle.getHeading();
currentTitle = title;

if (forceUpdate || !StringUtils.equals(oldHeading, currentTitle.getHeading())) {
session.sendAll(TitleMessage.fromTitle(currentTitle));
}
}

@Override
public void clearTitle() {
currentTitle = new Title();
session.send(new TitleMessage(TitleMessage.Action.CLEAR));
session.send(new TitleMessage(Action.CLEAR));
}

@Override
public void resetTitle() {
currentTitle = new Title(currentTitle.getHeading());
titleOptions = new TitleOptions();
session.send(new TitleMessage(TitleMessage.Action.RESET));
currentTitle = new Title("");
session.send(new TitleMessage(Action.RESET));
}

public GlowBlock getDigging() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import com.flowpowered.networking.Message;
import lombok.Data;
import net.glowstone.util.TextMessage;
import org.bukkit.title.Title;
import org.bukkit.title.TitleOptions;
import net.md_5.bungee.api.chat.BaseComponent;
import org.github.paperspigot.Title;

@Data
public final class TitleMessage implements Message {
Expand Down Expand Up @@ -42,15 +42,12 @@ public TitleMessage(Action action) {

public static TitleMessage[] fromTitle(Title title) {
return new TitleMessage[]{
new TitleMessage(Action.TITLE, asTextMessage(title.getHeading())),
new TitleMessage(Action.SUBTITLE, asTextMessage(title.getSubtitle()))
new TitleMessage(Action.TITLE, asTextMessage(BaseComponent.toPlainText(title.getTitle()))),
new TitleMessage(Action.SUBTITLE, asTextMessage(BaseComponent.toPlainText(title.getSubtitle()))),
new TitleMessage(Action.TIMES, title.getFadeIn(), title.getStay(), title.getFadeOut())
};
}

public static TitleMessage fromOptions(TitleOptions options) {
return new TitleMessage(Action.TIMES, options.getFadeInTime(), options.getVisibleTime(), options.getFadeOutTime());
}

private static TextMessage asTextMessage(String rawString) {
if (rawString == null) rawString = "";
return new TextMessage(rawString);
Expand Down

0 comments on commit aafa1f9

Please sign in to comment.