Mineplex2018-withcommit/Plugins/Mineplex.Hub/src/mineplex/hub/commands/NewsCommand.java

216 lines
8.0 KiB
Java
Raw Normal View History

package mineplex.hub.commands;
import java.util.HashMap;
import java.util.Iterator;
import org.bukkit.entity.Player;
import mineplex.core.command.CommandBase;
import mineplex.core.common.Rank;
import mineplex.core.common.util.C;
import mineplex.core.common.util.Callback;
import mineplex.core.common.util.F;
import mineplex.core.common.util.UtilPlayer;
import mineplex.core.common.util.UtilServer;
import mineplex.hub.HubManager;
import mineplex.hub.modules.NewsManager;
public class NewsCommand extends CommandBase<HubManager>
{
public NewsCommand(HubManager plugin)
{
super(plugin, Rank.ADMIN, "news");
}
@Override
public void Execute(final Player caller, final String[] args)
{
final NewsManager newsMang = Plugin.GetNewsManager();
if (args == null || args.length == 0)
{
UtilPlayer.message(caller, F.main(Plugin.GetName(), C.cRed + "Your arguments are inappropriate for this command!"));
UtilPlayer.message(caller, F.main(Plugin.GetName(), C.cGray + "Available news arguments for this command:"));
UtilPlayer.message(caller, F.main(Plugin.GetName(), C.cWhite + "news list" + C.cGray + " - Lists (numbered) stored news messages from database."));
UtilPlayer.message(caller, F.main(Plugin.GetName(), C.cWhite + "news add <newsEntry>" + C.cGray + " - Adds specified news entry string to database at end of table."));
UtilPlayer.message(caller, F.main(Plugin.GetName(), C.cWhite + "news delete #" + C.cGray + " - Removes specified (numbered) news entry string from database."));
UtilPlayer.message(caller, F.main(Plugin.GetName(), C.cWhite + "news set # <newsEntry>" + C.cGray + " - Updates specified (numbered) news entry string in database."));
UtilPlayer.message(caller, F.main(Plugin.GetName(), C.cWhite + "*Please Note: " + C.cGray + "Updates to server news entries from the database are on a 4 minute refresh cycle!"));
return;
}
else if (args.length == 1 && args[0].equalsIgnoreCase("list"))
{
UtilPlayer.message(caller, F.main(Plugin.GetName(), C.cGray + "Current server news messages:"));
newsMang.RetriveNewsEntries(new Callback<HashMap<String, String>>()
{
public void run(final HashMap<String, String> newsEntries)
{
// Order newsEntries set or its output by newsPosition, not hash order...
newsMang.RetrieveMaxNewsPosition(new Callback<Integer>()
{
public void run(Integer maxPosition)
{
String[] newsStrings = new String[maxPosition];
for (Iterator<String> iterator = newsEntries.keySet().iterator(); iterator.hasNext();)
{
String newsPosition = iterator.next();
newsStrings[Integer.parseInt(newsPosition) - 1] = newsEntries.get(newsPosition);
}
for (int i = 0; i < newsStrings.length; i++)
{
UtilServer.getServer().dispatchCommand(UtilServer.getServer().getConsoleSender(), "tellraw " + caller.getName() + " {\"text\":\"" + Plugin.GetName() + "> \", color:blue, \"extra\":[{\"text\":\"[DELETE] \", color:red, \"clickEvent\":{\"action\":\"run_command\",\"value\":\"/news ¢¤₦₣¡₨₥ " + (i + 1) + "\"}, \"hoverEvent\":{\"action\":\"show_text\",\"value\":\"Deletes News Entry " + (i + 1) + " : " + newsStrings[i] + "\"}}, {\"text\":\"News " + (i + 1) + "\", color:gold}, {\"text\":\" : \", color:gray}, {\"text\":\"" + newsStrings[i] + "\", color:white}]}");
}
}
});
}
});
return;
}
else if (args.length >= 2 && args.length <= 128)
{
if (args[0].equals("¢¤₦₣¡₨₥") && args.length == 2)
{
int newsPosition;
try
{
newsPosition = Integer.parseInt(args[1]);
}
catch (Exception exception)
{
UtilPlayer.message(caller, F.main(Plugin.GetName(), C.cRed + "The specified news position is invalid!"));
return;
}
UtilServer.getServer().dispatchCommand(UtilServer.getServer().getConsoleSender(), "tellraw " + caller.getName() + " {\"text\":\"" + Plugin.GetName() + "> \", color:blue, \"extra\":[{\"text\":\"[CONFIRM] \", color:green, \"clickEvent\":{\"action\":\"run_command\",\"value\":\"/news delete " + newsPosition + "\"}, \"hoverEvent\":{\"action\":\"show_text\",\"value\":\"Are you absolutely sure???\"}}, {\"text\":\"News Entry " + newsPosition + "\", color:gold}, {\"text\":\" deletion?\", color:gray}]}");
return;
}
else if (args[0].equalsIgnoreCase("delete") && args.length == 2)
{
final int newsPosition;
try
{
newsPosition = Integer.parseInt(args[1]);
}
catch (Exception exception)
{
UtilPlayer.message(caller, F.main(Plugin.GetName(), C.cRed + "The specified news position is invalid!"));
return;
}
newsMang.DeleteNewsEntry(newsPosition, new Callback<Boolean>()
{
public void run(Boolean success)
{
if (success)
{
UtilPlayer.message(caller, F.main(Plugin.GetName(), C.cGray + "The news entry at position " + C.cGold + newsPosition + C.cGray + " has been deleted!"));
}
else
{
UtilPlayer.message(caller, F.main(Plugin.GetName(), C.cRed + "There was an error deleting the news entry; likely the specified news position was invalid!"));
}
}
});
return;
}
else if (args[0].equalsIgnoreCase("add"))
{
String newsEntry = "";
for (int i = 1; i < args.length; i++)
{
newsEntry += args[i] + " ";
}
newsEntry = newsEntry.substring(0, newsEntry.length() - 1);
// Check for 256 character length for MySQL!
if (newsEntry.length() > 256)
{
UtilPlayer.message(caller, F.main(Plugin.GetName(), C.cRed + "The specified news entry is too long [> 256 characters]!"));
return;
}
newsMang.AddNewsEntry(newsEntry, new Callback<Boolean>()
{
public void run(Boolean success)
{
if (success)
{
String newsEntry = "";
for (int i = 1; i < args.length; i++)
{
newsEntry += args[i] + " ";
}
newsEntry = newsEntry.substring(0, newsEntry.length() - 1);
UtilPlayer.message(caller, F.main(Plugin.GetName(), C.cGray + "The news entry: " + C.cGold + newsEntry + C.cGray + " has been added to the database!"));
}
else
{
UtilPlayer.message(caller, F.main(Plugin.GetName(), C.cRed + "There was an error adding the news entry to the database!"));
}
}
});
return;
}
else if (args[0].equalsIgnoreCase("set") && args.length >= 3)
{
final int newsPosition;
String newsEntry = "";
for (int i = 2; i < args.length; i++)
{
newsEntry += args[i] + " ";
}
newsEntry = newsEntry.substring(0, newsEntry.length() - 1);
// Check for 256 character length for MySQL!
if (newsEntry.length() > 256)
{
UtilPlayer.message(caller, F.main(Plugin.GetName(), C.cRed + "The specified news entry is too long [> 256 characters]!"));
return;
}
try
{
newsPosition = Integer.parseInt(args[1]);
}
catch (Exception exception)
{
UtilPlayer.message(caller, F.main(Plugin.GetName(), C.cRed + "The specified news position is invalid!"));
return;
}
newsMang.SetNewsEntry(newsEntry, newsPosition, new Callback<Boolean>()
{
public void run(Boolean success)
{
if (success)
{
String newsEntry = "";
for (int i = 2; i < args.length; i++)
{
newsEntry += args[i] + " ";
}
newsEntry = newsEntry.substring(0, newsEntry.length() - 1);
UtilPlayer.message(caller, F.main(Plugin.GetName(), C.cGray + "The news entry at position " + C.cGold + newsPosition + C.cGray + " has been updated to: " + C.cGold + newsEntry + C.cGray + "!"));
}
else
{
UtilPlayer.message(caller, F.main(Plugin.GetName(), C.cRed + "There was an error updating the news entry; likely the specified news position was invalid!"));
}
}
});
return;
}
UtilPlayer.message(caller, F.main(Plugin.GetName(), C.cRed + "Your arguments are inappropriate for this command!"));
return;
}
else
{
UtilPlayer.message(caller, F.main(Plugin.GetName(), C.cRed + "Your arguments are inappropriate for this command!"));
return;
}
}
}