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

128 lines
4.4 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.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)
{
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."));
return;
}
else if (args.length == 1 && args[0].equalsIgnoreCase("list"))
{
UtilPlayer.message(caller, F.main(Plugin.GetName(), C.cGray + "Current server news messages will be listed here..."));
newsMang.RetriveNewsEntries(new Callback<HashMap<String, String>>()
{
public void run(HashMap<String, String> newsEntries)
{
for (Iterator<String> iterator = newsEntries.keySet().iterator(); iterator.hasNext();)
{
String newsPosition = iterator.next();
String newsEntry = newsEntries.get(newsPosition);
UtilPlayer.message(caller, F.main(Plugin.GetName(), C.cGold + "News " + newsPosition + C.cGray + " : " + newsEntry));
//iterator.remove();
}
}
});
return;
}
else if (args.length == 2)
{
if (args[0].equalsIgnoreCase("add"))
{
UtilPlayer.message(caller, F.main(Plugin.GetName(), C.cGray + "This is where future messages like, " + C.cGold + "'" + args[1] + "'" + C.cGray + " will be added to server news!"));
return;
}
else if (args[0].equalsIgnoreCase("delete"))
{
UtilPlayer.message(caller, F.main(Plugin.GetName(), C.cGray + "This is where future messages at positions like, " + C.cGold + "'News Position: " + C.cGold + args[1] + "'" + C.cGray + " will be removed from server news!"));
return;
}
}
else if (args.length >= 3 && args.length <= 128)
{
if (args[0].equalsIgnoreCase("set"))
{
final int newsPosition;
String newsEntry = "";
for (int i = 2; i < args.length; i++)
{
newsEntry += args[i] + " ";
}
newsEntry = newsEntry.substring(0, newsEntry.length() - 1);
// TODO: Check for 256 character length for MySQL!
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;
}
}
else
{
UtilPlayer.message(caller, F.main(Plugin.GetName(), C.cRed + "Your arguments are inappropriate for this command!"));
return;
}
}
}