Mineplex2018-withcommit/Plugins/Mineplex.Hub/src/mineplex/hub/modules/NewsManager.java

264 lines
6.9 KiB
Java
Raw Normal View History

package mineplex.hub.modules;
import java.util.HashMap;
import java.util.Iterator;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import mineplex.core.MiniPlugin;
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.UtilDisplay;
import mineplex.core.common.util.UtilPlayer;
import mineplex.core.common.util.UtilServer;
import mineplex.core.common.util.UtilTime;
import mineplex.core.mount.Mount;
2014-08-22 20:30:41 +02:00
import mineplex.core.mount.types.MountDragon;
import mineplex.core.updater.UpdateType;
import mineplex.core.updater.event.UpdateEvent;
import mineplex.hub.HubManager;
import mineplex.hub.HubRepository;
public class NewsManager extends MiniPlugin
{
public HubManager Manager;
private String[] _news;
private int _newsIndex = 0;
private long _newsTime = System.currentTimeMillis();
private int _mineplexIndex = 0;
private HubRepository _repository = new HubRepository();
public NewsManager(HubManager manager)
{
super("News Manager", manager.GetPlugin());
2013-12-18 11:22:40 +01:00
Manager = manager;
_repository.initialize(manager.GetPlugin().getConfig().getBoolean("serverstatus.us"));
_news = new String[]
{
2014-07-05 08:20:46 +02:00
"Champions: " + C.cYellow + C.Bold + "Team Deathmatch Beta",
"Survival: " + C.cGreen + C.Bold + "UHC Alpha",
};
RefreshNews();
}
private void RefreshNews()
{
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...
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);
}
_news = newsStrings;
}
});
}
});
}
public void RetriveNewsEntries(final Callback<HashMap<String, String>> callback)
{
if (callback == null)
return;
Bukkit.getScheduler().runTaskAsynchronously(GetPlugin(), new Runnable()
{
public void run()
{
final HashMap<String, String> newsEntries = _repository.retrieveNewsEntries();
Bukkit.getScheduler().runTask(GetPlugin(), new Runnable()
{
public void run()
{
callback.run(newsEntries);
}
});
}
});
}
public void SetNewsEntry(final String newsEntry, final int newsPosition, final Callback<Boolean> callback)
{
if (callback == null)
return;
Bukkit.getScheduler().runTaskAsynchronously(GetPlugin(), new Runnable()
{
public void run()
{
final Boolean success = _repository.setNewsEntry(newsEntry, newsPosition);
Bukkit.getScheduler().runTask(GetPlugin(), new Runnable()
{
public void run()
{
callback.run(success);
}
});
}
});
}
public void AddNewsEntry(final String newsEntry, final Callback<Boolean> callback)
{
if (callback == null)
return;
Bukkit.getScheduler().runTaskAsynchronously(GetPlugin(), new Runnable()
{
public void run()
{
final Boolean success = _repository.addNewsEntry(newsEntry);
Bukkit.getScheduler().runTask(GetPlugin(), new Runnable()
{
public void run()
{
callback.run(success);
}
});
}
});
}
public void DeleteNewsEntry(final int newsPosition, final Callback<Boolean> callback)
{
if (callback == null)
return;
Bukkit.getScheduler().runTaskAsynchronously(GetPlugin(), new Runnable()
{
public void run()
{
final Boolean success = _repository.deleteNewsEntry(newsPosition);
Bukkit.getScheduler().runTask(GetPlugin(), new Runnable()
{
public void run()
{
callback.run(success);
}
});
}
});
}
public void RetrieveMaxNewsPosition(final Callback<Integer> callback)
{
if (callback == null)
return;
Bukkit.getScheduler().runTaskAsynchronously(GetPlugin(), new Runnable()
{
public void run()
{
final Integer position = _repository.retrieveMaxNewsPosition();
Bukkit.getScheduler().runTask(GetPlugin(), new Runnable()
{
public void run()
{
callback.run(position);
}
});
}
});
}
public void Help(Player caller, String message)
{
UtilPlayer.message(caller, F.main(_moduleName, "Available news arguments for this command:"));
UtilPlayer.message(caller, F.help(C.cGold + "/news list", "Lists (numbered) stored news messages from database.", Rank.ADMIN));
UtilPlayer.message(caller, F.help(C.cGold + "/news add <newsEntry>", "Adds specified news entry string to database at end of table.", Rank.ADMIN));
UtilPlayer.message(caller, F.help(C.cGold + "/news delete #", "Removes specified (numbered) news entry string from database.", Rank.ADMIN));
UtilPlayer.message(caller, F.help(C.cGold + "/news set # <newsEntry>", "Updates specified (numbered) news entry string in database.", Rank.ADMIN));
UtilPlayer.message(caller, F.help("*Please Note:", "Updates to server news entries from the database are on a 4 minute refresh cycle!", Rank.ADMIN));
if (message != null)
UtilPlayer.message(caller, F.main(_moduleName, ChatColor.RED + message));
}
public void Help(Player caller)
{
Help(caller, null);
}
@EventHandler
public void NewsUpdate(UpdateEvent event)
{
if (event.getType() != UpdateType.MIN_04)
return;
RefreshNews();
}
@EventHandler
public void FlightUpdate(UpdateEvent event)
{
if (event.getType() != UpdateType.FASTEST)
return;
//Mineplex Color
ChatColor col = ChatColor.RED;
if (_mineplexIndex == 1) col = ChatColor.GOLD;
else if (_mineplexIndex == 2) col = ChatColor.YELLOW;
else if (_mineplexIndex == 3) col = ChatColor.GREEN;
else if (_mineplexIndex == 4) col = ChatColor.AQUA;
else if (_mineplexIndex == 5) col = ChatColor.LIGHT_PURPLE;
_mineplexIndex = (_mineplexIndex + 1)%6;
//News Change
if (UtilTime.elapsed(_newsTime, 4500))
{
_newsIndex = (_newsIndex + 1)%_news.length;
_newsTime = System.currentTimeMillis();
}
if (_newsIndex >= _news.length)
{
// Resets newsIndex if outside of bounds of news array after RefreshNews but before UtilTime.elapsed above
_newsIndex = 0;
}
//Text
String text = col + C.Bold + "MINEPLEX" + ChatColor.RESET + " - " + _news[_newsIndex];
if (text.length() > 64)
text = text.substring(0, 64);
double healthPercent = (double)_newsIndex/(double)(_news.length-1);
for (Player player : UtilServer.getPlayers())
UtilDisplay.displayTextBar(Manager.GetPlugin(), player, healthPercent, text);
for (Mount mount : Manager.GetMount().getMounts())
{
2014-08-22 20:30:41 +02:00
if (mount instanceof MountDragon)
{
2014-08-22 20:30:41 +02:00
((MountDragon)mount).SetName(text);
((MountDragon)mount).setHealthPercent(healthPercent);
}
}
}
}