Chat channel improved, tried a bunch of ideas

This commit is contained in:
Norbi Peti 2017-04-30 00:35:42 +02:00
parent bcf8cc4e99
commit 308006d624
4 changed files with 159 additions and 56 deletions

View file

@ -1,22 +1,21 @@
package buttondevteam.core;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scoreboard.Scoreboard;
import com.palmergames.bukkit.towny.Towny;
import com.palmergames.bukkit.towny.object.Nation;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.TownyUniverse;
import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.chat.TBMCChatAPI;
import buttondevteam.lib.player.ChromaGamerBase;
import buttondevteam.lib.player.TBMCPlayerBase;
import net.milkbowl.vault.permission.Permission;
@ -27,7 +26,6 @@ public class MainPlugin extends JavaPlugin {
private PluginDescriptionFile pdfFile;
private Logger logger;
private int C = 0, keep = 0;
@Override
public void onEnable() {
@ -43,45 +41,6 @@ public class MainPlugin extends JavaPlugin {
TBMCCoreAPI.RegisterEventsForExceptions(new PlayerListener(), this);
TBMCCoreAPI.RegisterUserClass(TBMCPlayerBase.class);
logger.info(pdfFile.getName() + " has been Enabled (V." + pdfFile.getVersion() + ").");
Arrays.stream(new File(ChromaGamerBase.TBMC_PLAYERS_DIR).listFiles(f -> !f.isDirectory())).map(f -> {
YamlConfiguration yc = new YamlConfiguration();
try {
yc.load(f);
} catch (IOException | InvalidConfigurationException e) {
TBMCCoreAPI.SendException("Error while converting player data!", e);
}
f.delete();
return yc;
}).forEach(yc -> {
try {
int flairtime = yc.getInt("flairtime"), fcount = yc.getInt("fcount"), fdeaths = yc.getInt("fdeaths");
String flairstate = yc.getString("flairstate");
List<String> usernames = yc.getStringList("usernames");
boolean flaircheater = yc.getBoolean("flaircheater");
final String uuid = yc.getString("uuid");
C++;
if ((fcount == 0 || fdeaths == 0)
&& (flairstate == null || "NoComment".equals(flairstate) || flairtime <= 0))
return; // Those who received no Fs yet will also get their stats reset if no flair
final File file = new File(ChromaGamerBase.TBMC_PLAYERS_DIR + "minecraft", uuid + ".yml");
YamlConfiguration targetyc = YamlConfiguration.loadConfiguration(file);
targetyc.set("PlayerName", yc.getString("playername"));
targetyc.set("minecraft_id", uuid);
ConfigurationSection bc = targetyc.createSection("ButtonChat");
bc.set("FlairTime", "NoComment".equals(flairstate) ? -3 : flairtime); // FlairTimeNone: -3
bc.set("FCount", fcount);
bc.set("FDeaths", fdeaths);
bc.set("FlairState", flairstate);
bc.set("UserNames", usernames);
bc.set("FlairCheater", flaircheater);
targetyc.save(file);
keep++;
} catch (Exception e) {
TBMCCoreAPI.SendException("Error while converting player data!", e);
}
});
Bukkit.getScheduler().runTask(this, () -> logger.info("Converted " + keep + " player data from " + C));
//TODO: Remove once ran it at least once
}
@Override

View file

@ -2,26 +2,60 @@ package buttondevteam.lib.chat;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
import com.palmergames.bukkit.towny.object.Nation;
import com.palmergames.bukkit.towny.object.Resident;
import com.palmergames.bukkit.towny.object.Town;
import buttondevteam.core.MainPlugin;
public class Channel {
public final String DisplayName;
public final Color color;
public final String Command;
public final String ID;
/**
* Filters both the sender and the targets
*/
public final Function<CommandSender, RecipientTestResult> filteranderrormsg;
private static List<Channel> channels = new ArrayList<>();
public Channel(String displayname, Color color, String command) {
/**
* Creates a channel.
*
* @param displayname
* The name that should appear at the start of the message
* @param color
* The default color of the messages sent in the channel
* @param command
* The command to be used for the channel <i>without /</i>. For example "mod". It's also used for scoreboard objective names.
* @param filteranderrormsg
* Checks all senders against the criteria provided here and sends the message returned if it has an index - otherwise displays the error.<br>
* May be null to send to everyone.
*/
public Channel(String displayname, Color color, String command,
Function<CommandSender, RecipientTestResult> filteranderrormsg) {
DisplayName = displayname;
this.color = color;
Command = command;
ID = command;
this.filteranderrormsg = filteranderrormsg;
}
static {
channels.add(GlobalChat = new Channel("§fg§f", Color.White, "g"));
channels.add(TownChat = new Channel("§3TC§f", Color.DarkAqua, "tc"));
channels.add(NationChat = new Channel("§6NC§f", Color.Gold, "nc"));
channels.add(AdminChat = new Channel("§cADMIN§f", Color.Red, "a"));
channels.add(ModChat = new Channel("§9MOD§f", Color.Blue, "mod"));
RegisterChannel(GlobalChat = new Channel("§fg§f", Color.White, "g", null));
RegisterChannel(TownChat = new Channel("§3TC§f", Color.DarkAqua, "tc", s -> checkTownNationChat(s, false)));
RegisterChannel(NationChat = new Channel("§6NC§f", Color.Gold, "nc", s -> checkTownNationChat(s, true)));
RegisterChannel(AdminChat = new Channel("§cADMIN§f", Color.Red, "a", s -> s.isOp() ? new RecipientTestResult(0)
: new RecipientTestResult("You need to be an admin to use this channel.")));
RegisterChannel(ModChat = new Channel("§9MOD§f", Color.Blue, "mod",
s -> s.isOp() || (s instanceof Player && MainPlugin.permission.playerInGroup((Player) s, "mod"))
? new RecipientTestResult(0) //
: new RecipientTestResult("You need to be a mod to use this channel.")));
}
public static List<Channel> getChannels() {
@ -33,4 +67,77 @@ public class Channel {
public static Channel NationChat;
public static Channel AdminChat;
public static Channel ModChat;
static void RegisterChannel(Channel channel) {
channels.add(channel);
Bukkit.getPluginManager().callEvent(new ChatChannelRegisterEvent(channel));
}
/**
* Return the error message for the message sender if they can't send it and the score
*/
private static RecipientTestResult checkTownNationChat(CommandSender sender, boolean nationchat) {
if (!(sender instanceof Player))
return new RecipientTestResult("§cYou are not a player!");
try {
Resident resident = MainPlugin.TU.getResidentMap().get(sender.getName().toLowerCase());
if (resident != null && resident.getModes().contains("spy"))
return null;
/*
* p.sendMessage(String.format("[SPY-%s] - %s: %s", channel.DisplayName, ((Player) sender).getDisplayName(), message));
*/
Town town = null;
if (resident != null && resident.hasTown())
town = resident.getTown();
if (town == null)
return new RecipientTestResult("You aren't in a town.");
Nation nation = null;
int index = -1; // TODO: Move all this to the event in ButtonChat along with the channel definitions and make scores only for the sender...
if (nationchat) {
if (town.hasNation())
nation = town.getNation();
if (nation == null)
return new RecipientTestResult("Your town isn't in a nation.");
index = MainPlugin.Nations.indexOf(nation);
if (index < 0) {
MainPlugin.Nations.add(nation);
index = MainPlugin.Nations.size() - 1;
}
} else {
index = MainPlugin.Towns.indexOf(town);
if (index < 0) {
MainPlugin.Towns.add(town);
index = MainPlugin.Towns.size() - 1;
}
}
return new RecipientTestResult(index);
} catch (NotRegisteredException e) {
return new RecipientTestResult("You (probably) aren't knwon by Towny! (Not in a town)");
}
}
public static class RecipientTestResult {
public String errormessage;
public int score;
/**
* Creates a result that indicates an <b>error</b>
*
* @param errormessage
* The error message to show the sender if they don't meet the criteria.
*/
public RecipientTestResult(String errormessage) {
this.errormessage = errormessage;
}
/**
* Creates a result that indicates a <b>success</b>
*
* @param score
* The score that identifies the target group. For example, the index of the town or nation to send to.
*/
public RecipientTestResult(int score) {
this.score = score;
} // TODO: Set score of players, if they get one, otherwise (on error) set score to -1
}
}

View file

@ -0,0 +1,27 @@
package buttondevteam.lib.chat;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
public class ChatChannelRegisterEvent extends Event {
private static final HandlerList handlers = new HandlerList();
private Channel channel;
public ChatChannelRegisterEvent(Channel channel) {
this.channel = channel;
}
public Channel getChannel() {
return channel;
}
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
}

View file

@ -215,4 +215,14 @@ public class TBMCChatAPI {
Bukkit.getPluginManager().callEvent(event);
return event.isCancelled();
}
/**
* Register a chat channel. See {@link Channel#Channel(String, Color, String, java.util.function.Function)} for details.
*
* @param channel
* A new {@link Channel} to register
*/
public static void RegisterChatChannel(Channel channel) {
Channel.RegisterChannel(channel);
}
}