Player data fixes

This commit is contained in:
Norbi Peti 2017-01-29 01:48:53 +01:00
parent ef42d811b0
commit 11f13af8cc
4 changed files with 35 additions and 23 deletions

View file

@ -6,6 +6,7 @@ import java.util.HashMap;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.function.Consumer; import java.util.function.Consumer;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.TBMCCoreAPI;
@ -56,7 +57,7 @@ public abstract class ChromaGamerBase implements AutoCloseable {
return plugindata != null ? plugindata.getString("id") : null; return plugindata != null ? plugindata.getString("id") : null;
} }
protected static <T extends ChromaGamerBase> T getUser(String fname, Class<T> cl) { public static <T extends ChromaGamerBase> T getUser(String fname, Class<T> cl) {
try { try {
T obj = cl.newInstance(); T obj = cl.newInstance();
final File file = new File(TBMC_PLAYERS_DIR + getFolderForType(cl), fname + ".yml"); final File file = new File(TBMC_PLAYERS_DIR + getFolderForType(cl), fname + ".yml");
@ -126,6 +127,17 @@ public abstract class ChromaGamerBase implements AutoCloseable {
sync.accept(user.plugindata); sync.accept(user.plugindata);
} }
/**
* Retunrs the ID for the T typed player object connected with this one or null if no connection found.
*
* @param cl
* The player class to get the ID from
* @return The ID or null if not found
*/
public <T extends ChromaGamerBase> String getConnectedID(Class<T> cl) {
return plugindata.getString(getFolderForType(cl) + "_id");
}
/** /**
* Returns this player as a plugin player. This will return a new instance unless the player is online.<br> * Returns this player as a plugin player. This will return a new instance unless the player is online.<br>
* Make sure to close both the returned and this object. A try-with-resources block or two can help.<br> * Make sure to close both the returned and this object. A try-with-resources block or two can help.<br>
@ -146,4 +158,21 @@ public abstract class ChromaGamerBase implements AutoCloseable {
public String getFolder() { public String getFolder() {
return getFolderForType(getClass()); return getFolderForType(getClass());
} }
/**
* 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

@ -1,6 +1,6 @@
package buttondevteam.lib.player; package buttondevteam.lib.player;
@PlayerClass(pluginname = "ButtonCore") @PlayerClass(pluginname = "ButtonCore")
public class TBMCPlayer extends TBMCPlayerBase { public final class TBMCPlayer extends TBMCPlayerBase {
} }

View file

@ -189,21 +189,4 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase {
else else
return null; return null;
} }
/**
* 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

@ -7,7 +7,7 @@ import java.util.stream.Collectors;
import org.bukkit.event.Event; import org.bukkit.event.Event;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
import buttondevteam.lib.player.TBMCPlayerBase.InfoTarget; import buttondevteam.lib.player.ChromaGamerBase.InfoTarget;
/** /**
* <p> * <p>
@ -20,11 +20,11 @@ import buttondevteam.lib.player.TBMCPlayerBase.InfoTarget;
public class TBMCPlayerGetInfoEvent extends Event { public class TBMCPlayerGetInfoEvent extends Event {
private static final HandlerList handlers = new HandlerList(); private static final HandlerList handlers = new HandlerList();
private TBMCPlayerBase player; private ChromaGamerBase player;
private List<String> infolines; private List<String> infolines;
private InfoTarget target; private InfoTarget target;
TBMCPlayerGetInfoEvent(TBMCPlayerBase player, InfoTarget target) { TBMCPlayerGetInfoEvent(ChromaGamerBase player, InfoTarget target) {
this.player = player; this.player = player;
infolines = new ArrayList<>(); infolines = new ArrayList<>();
this.target = target; this.target = target;
@ -35,7 +35,7 @@ public class TBMCPlayerGetInfoEvent extends Event {
* *
* @return A player object * @return A player object
*/ */
public TBMCPlayerBase getPlayer() { public ChromaGamerBase getPlayer() {
return player; return player;
} }