From 5def9344e8c07689b15fcbb669014d944fa6d781 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Thu, 25 Oct 2018 23:50:17 +0200 Subject: [PATCH] Quick and dirty fix and another fix --- .../lib/player/ChannelPlayerData.java | 25 +++++++++++++++ .../lib/player/ChromaGamerBase.java | 32 +++++++++++++------ 2 files changed, 47 insertions(+), 10 deletions(-) create mode 100644 ButtonCore/src/main/java/buttondevteam/lib/player/ChannelPlayerData.java diff --git a/ButtonCore/src/main/java/buttondevteam/lib/player/ChannelPlayerData.java b/ButtonCore/src/main/java/buttondevteam/lib/player/ChannelPlayerData.java new file mode 100644 index 0000000..ee62bd7 --- /dev/null +++ b/ButtonCore/src/main/java/buttondevteam/lib/player/ChannelPlayerData.java @@ -0,0 +1,25 @@ +package buttondevteam.lib.player; + +import buttondevteam.lib.chat.Channel; +import org.bukkit.configuration.file.YamlConfiguration; + +public class ChannelPlayerData { //I just want this to work + private final PlayerData data; + private final Channel def; + + public ChannelPlayerData(String name, YamlConfiguration yaml, Channel def) { + data = new PlayerData<>(name, yaml, ""); + this.def = def; + } + + public Channel get() { + String str = data.get(); + if (str.isEmpty()) + return def; + return Channel.getChannels().stream().filter(c -> str.equals(c.ID)).findAny().orElse(def); + } + + public void set(Channel value) { + data.set(value.toString()); + } +} diff --git a/ButtonCore/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java b/ButtonCore/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java index 571e0fc..3ab07b6 100755 --- a/ButtonCore/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java +++ b/ButtonCore/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java @@ -109,18 +109,16 @@ public abstract class ChromaGamerBase implements AutoCloseable { } /** - * Get from the given sender. May be null,.but shouldn't be. + * Get from the given sender. the object's type will depend on the sender's type. May be null, but shouldn't be. * * @param sender The sender to use - * @param cl The type of the requested user object - subclasses of {@link TBMCPlayerBase} don't work, use {@link TBMCPlayer} * @return A user as returned by a converter or null if none can supply it */ - @SuppressWarnings("unchecked") - public static T getFromSender(CommandSender sender, Class cl) { + public static ChromaGamerBase getFromSender(CommandSender sender) { for (val converter : senderConverters) { - val ocg = converter.apply(sender).filter(cg -> cl.isAssignableFrom(cg.getClass())); + val ocg = converter.apply(sender); if (ocg.isPresent()) - return (T) ocg.get(); + return ocg.get(); } return null; } @@ -260,7 +258,8 @@ public abstract class ChromaGamerBase implements AutoCloseable { } @SuppressWarnings("rawtypes") - private final HashMap dataenummap = new HashMap<>(); + private final HashMap dataenummap = new HashMap<>(); + private ChannelPlayerData datachannel; /** * Use from a data() method, which is in a method with the name of the key. For example, use flair() for the enclosing method of the outer data() to save to and load from "flair" @@ -278,7 +277,7 @@ public abstract class ChromaGamerBase implements AutoCloseable { /** * Use from a method with the name of the key. For example, use flair() for the enclosing method to save to and load from "flair" - * + * * @return A data object with methods to get and set */ @SuppressWarnings("unchecked") @@ -290,6 +289,19 @@ public abstract class ChromaGamerBase implements AutoCloseable { return dataenummap.get(mname); } + /** + * Channel + * + * @return A data object with methods to get and set + */ + @SuppressWarnings("unchecked") + protected ChannelPlayerData dataChannel(Channel def) { //TODO: Make interface with fromString() method and require use of that for player data types + ThrowIfNoUser(); + if (datachannel == null) + datachannel = new ChannelPlayerData("channel", plugindata, def); + return datachannel; + } + /** * Get player information. This method calls the {@link TBMCPlayerGetInfoEvent} to get all the player information across the TBMC plugins. * @@ -309,7 +321,7 @@ public abstract class ChromaGamerBase implements AutoCloseable { //----------------------------------------------------------------- - public PlayerData channel() { - return data(Channel.GlobalChat); + public ChannelPlayerData channel() { + return dataChannel(Channel.GlobalChat); } }