Added getInfo method and an event for it

This commit is contained in:
Norbi Peti 2016-11-01 21:13:12 +01:00
parent 076ee4ffd0
commit 347c6d40a4
2 changed files with 92 additions and 1 deletions

View file

@ -101,7 +101,8 @@ public class TBMCPlayer implements AutoCloseable {
String mname = st.getMethodName(); String mname = st.getMethodName();
if (!mname.startsWith("get")) if (!mname.startsWith("get"))
throw new UnsupportedOperationException("Can only use getEnumData from a getXYZ method"); throw new UnsupportedOperationException("Can only use getEnumData from a getXYZ method");
final String retstr = (String) getLoadedPlayers().get(uuid).data.get(mname.substring("get".length()).toLowerCase()); final String retstr = (String) getLoadedPlayers().get(uuid).data
.get(mname.substring("get".length()).toLowerCase());
if (retstr != null) if (retstr != null)
return Enum.valueOf(cl, retstr); return Enum.valueOf(cl, retstr);
else else
@ -444,4 +445,21 @@ public class TBMCPlayer implements AutoCloseable {
public static HashMap<UUID, TBMCPlayer> getLoadedPlayers() { public static HashMap<UUID, TBMCPlayer> getLoadedPlayers() {
return LoadedPlayers; return LoadedPlayers;
} }
/**
* Get player information. This method calls the {@link TBMCPlayerGetInfoEvent} to get all the player information across the TBMC plugins.
*
* @param target
* The {@link InfoTarget} to return the info for.
* @return The player information.
*/
public String getInfo(InfoTarget target) {
TBMCPlayerGetInfoEvent event = new TBMCPlayerGetInfoEvent(this, target);
Bukkit.getServer().getPluginManager().callEvent(event);
return event.getResult();
}
public enum InfoTarget {
MCHover, MCCommand, Discord
}
} }

View file

@ -0,0 +1,73 @@
package buttondevteam.lib;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import buttondevteam.lib.TBMCPlayer.InfoTarget;
/**
* <p>
* This event gets called when player information is requested. It can be used to give more per-plugin information about a player.
* </p>
*
* @author Norbi
*
*/
public class TBMCPlayerGetInfoEvent extends Event {
private static final HandlerList handlers = new HandlerList();
private TBMCPlayer player;
private List<String> infolines;
private TBMCPlayer.InfoTarget target;
TBMCPlayerGetInfoEvent(TBMCPlayer player, TBMCPlayer.InfoTarget target) {
this.player = player;
infolines = new ArrayList<>();
this.target = target;
}
/**
* Get the {@link TBMCPlayer} object
*
* @return A player object
*/
public TBMCPlayer getPlayer() {
return player;
}
/**
* Add a line to the information text. The line should be in the format of <i>key: value</i> .
*
* @param infoline
* The line to add
*/
public void addInfo(String infoline) {
infolines.add(infoline);
}
/**
* Get the {@link InfoTarget} we want the information for. Use this to format the returned string.
*
* @return The target of the information.
*/
public TBMCPlayer.InfoTarget getTarget() {
return target;
}
String getResult() {
return infolines.stream().collect(Collectors.joining("\n"));
}
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
}