2013-10-22 06:46:31 +02:00
|
|
|
package mineplex.hub.modules;
|
|
|
|
|
2014-07-10 02:38:11 +02:00
|
|
|
import java.util.HashMap;
|
2014-07-11 03:10:35 +02:00
|
|
|
import java.util.Iterator;
|
2014-07-10 02:38:11 +02:00
|
|
|
|
|
|
|
import org.bukkit.Bukkit;
|
2013-10-22 06:46:31 +02:00
|
|
|
import org.bukkit.ChatColor;
|
2014-12-17 06:31:51 +01:00
|
|
|
import org.bukkit.entity.Creature;
|
2013-10-22 06:46:31 +02:00
|
|
|
import org.bukkit.entity.Player;
|
2014-12-17 06:31:51 +01:00
|
|
|
import org.bukkit.entity.Wither;
|
2013-10-22 06:46:31 +02:00
|
|
|
import org.bukkit.event.EventHandler;
|
2014-09-14 06:10:40 +02:00
|
|
|
import org.bukkit.event.player.PlayerJoinEvent;
|
2013-10-22 06:46:31 +02:00
|
|
|
|
|
|
|
import mineplex.core.MiniPlugin;
|
2014-07-23 04:18:29 +02:00
|
|
|
import mineplex.core.common.Rank;
|
2013-10-22 06:46:31 +02:00
|
|
|
import mineplex.core.common.util.C;
|
2014-07-10 02:38:11 +02:00
|
|
|
import mineplex.core.common.util.Callback;
|
2014-07-11 03:10:35 +02:00
|
|
|
import mineplex.core.common.util.F;
|
|
|
|
import mineplex.core.common.util.UtilPlayer;
|
2013-10-22 06:46:31 +02:00
|
|
|
import mineplex.core.common.util.UtilServer;
|
2014-10-25 04:40:34 +02:00
|
|
|
import mineplex.core.common.util.UtilTextTop;
|
2013-10-22 06:46:31 +02:00
|
|
|
import mineplex.core.common.util.UtilTime;
|
2014-10-25 04:40:34 +02:00
|
|
|
import mineplex.core.common.util.UtilTextMiddle;
|
2014-12-16 06:18:09 +01:00
|
|
|
import mineplex.core.gadget.gadgets.MorphWither;
|
|
|
|
import mineplex.core.gadget.types.Gadget;
|
|
|
|
import mineplex.core.gadget.types.GadgetType;
|
2014-08-07 09:33:24 +02:00
|
|
|
import mineplex.core.mount.Mount;
|
2014-08-22 20:30:41 +02:00
|
|
|
import mineplex.core.mount.types.MountDragon;
|
2013-10-22 06:46:31 +02:00
|
|
|
import mineplex.core.updater.UpdateType;
|
|
|
|
import mineplex.core.updater.event.UpdateEvent;
|
|
|
|
import mineplex.hub.HubManager;
|
2014-07-09 04:55:51 +02:00
|
|
|
import mineplex.hub.HubRepository;
|
2013-10-22 06:46:31 +02:00
|
|
|
|
|
|
|
public class NewsManager extends MiniPlugin
|
|
|
|
{
|
|
|
|
public HubManager Manager;
|
|
|
|
|
|
|
|
private String[] _news;
|
|
|
|
private int _newsIndex = 0;
|
|
|
|
private long _newsTime = System.currentTimeMillis();
|
|
|
|
|
|
|
|
private int _mineplexIndex = 0;
|
2014-07-09 04:55:51 +02:00
|
|
|
|
|
|
|
private HubRepository _repository = new HubRepository();
|
2013-10-22 06:46:31 +02:00
|
|
|
|
|
|
|
public NewsManager(HubManager manager)
|
|
|
|
{
|
2015-02-26 06:07:07 +01:00
|
|
|
super("News Manager", manager.getPlugin());
|
2013-12-18 11:22:40 +01:00
|
|
|
|
2013-10-22 06:46:31 +02:00
|
|
|
Manager = manager;
|
|
|
|
|
2015-02-26 06:07:07 +01:00
|
|
|
_repository.initialize(manager.getPlugin().getConfig().getBoolean("serverstatus.us"));
|
2014-07-09 04:55:51 +02:00
|
|
|
|
2013-10-22 06:46:31 +02:00
|
|
|
_news = new String[]
|
|
|
|
{
|
2014-09-14 06:10:40 +02:00
|
|
|
"News Line 1",
|
|
|
|
"News Line 2",
|
|
|
|
"News Line 3",
|
|
|
|
"News Line 4",
|
2013-10-22 06:46:31 +02:00
|
|
|
};
|
2014-07-11 03:10:35 +02:00
|
|
|
|
2014-07-22 00:06:33 +02:00
|
|
|
RefreshNews();
|
2014-07-11 03:10:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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();)
|
|
|
|
{
|
2014-07-22 00:06:33 +02:00
|
|
|
String newsPosition = iterator.next();
|
2014-07-11 03:10:35 +02:00
|
|
|
newsStrings[Integer.parseInt(newsPosition) - 1] = newsEntries.get(newsPosition);
|
2014-07-22 00:06:33 +02:00
|
|
|
}
|
2014-07-11 03:10:35 +02:00
|
|
|
|
|
|
|
_news = newsStrings;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2013-10-22 06:46:31 +02:00
|
|
|
}
|
|
|
|
|
2014-07-10 02:38:11 +02:00
|
|
|
public void RetriveNewsEntries(final Callback<HashMap<String, String>> callback)
|
|
|
|
{
|
|
|
|
if (callback == null)
|
|
|
|
return;
|
|
|
|
|
2015-02-26 06:07:07 +01:00
|
|
|
Bukkit.getScheduler().runTaskAsynchronously(getPlugin(), new Runnable()
|
2014-07-10 02:38:11 +02:00
|
|
|
{
|
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
final HashMap<String, String> newsEntries = _repository.retrieveNewsEntries();
|
|
|
|
|
2015-02-26 06:07:07 +01:00
|
|
|
Bukkit.getScheduler().runTask(getPlugin(), new Runnable()
|
2014-07-10 02:38:11 +02:00
|
|
|
{
|
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
callback.run(newsEntries);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetNewsEntry(final String newsEntry, final int newsPosition, final Callback<Boolean> callback)
|
|
|
|
{
|
|
|
|
if (callback == null)
|
|
|
|
return;
|
|
|
|
|
2015-02-26 06:07:07 +01:00
|
|
|
Bukkit.getScheduler().runTaskAsynchronously(getPlugin(), new Runnable()
|
2014-07-10 02:38:11 +02:00
|
|
|
{
|
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
final Boolean success = _repository.setNewsEntry(newsEntry, newsPosition);
|
|
|
|
|
2015-02-26 06:07:07 +01:00
|
|
|
Bukkit.getScheduler().runTask(getPlugin(), new Runnable()
|
2014-07-10 02:38:11 +02:00
|
|
|
{
|
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
callback.run(success);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-07-11 03:10:35 +02:00
|
|
|
public void AddNewsEntry(final String newsEntry, final Callback<Boolean> callback)
|
|
|
|
{
|
|
|
|
if (callback == null)
|
|
|
|
return;
|
|
|
|
|
2015-02-26 06:07:07 +01:00
|
|
|
Bukkit.getScheduler().runTaskAsynchronously(getPlugin(), new Runnable()
|
2014-07-11 03:10:35 +02:00
|
|
|
{
|
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
final Boolean success = _repository.addNewsEntry(newsEntry);
|
|
|
|
|
2015-02-26 06:07:07 +01:00
|
|
|
Bukkit.getScheduler().runTask(getPlugin(), new Runnable()
|
2014-07-11 03:10:35 +02:00
|
|
|
{
|
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
callback.run(success);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public void DeleteNewsEntry(final int newsPosition, final Callback<Boolean> callback)
|
|
|
|
{
|
|
|
|
if (callback == null)
|
|
|
|
return;
|
|
|
|
|
2015-02-26 06:07:07 +01:00
|
|
|
Bukkit.getScheduler().runTaskAsynchronously(getPlugin(), new Runnable()
|
2014-07-11 03:10:35 +02:00
|
|
|
{
|
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
final Boolean success = _repository.deleteNewsEntry(newsPosition);
|
|
|
|
|
2015-02-26 06:07:07 +01:00
|
|
|
Bukkit.getScheduler().runTask(getPlugin(), new Runnable()
|
2014-07-11 03:10:35 +02:00
|
|
|
{
|
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
callback.run(success);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public void RetrieveMaxNewsPosition(final Callback<Integer> callback)
|
|
|
|
{
|
|
|
|
if (callback == null)
|
|
|
|
return;
|
|
|
|
|
2015-02-26 06:07:07 +01:00
|
|
|
Bukkit.getScheduler().runTaskAsynchronously(getPlugin(), new Runnable()
|
2014-07-11 03:10:35 +02:00
|
|
|
{
|
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
final Integer position = _repository.retrieveMaxNewsPosition();
|
|
|
|
|
2015-02-26 06:07:07 +01:00
|
|
|
Bukkit.getScheduler().runTask(getPlugin(), new Runnable()
|
2014-07-11 03:10:35 +02:00
|
|
|
{
|
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
callback.run(position);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-07-23 04:18:29 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-07-11 03:10:35 +02:00
|
|
|
@EventHandler
|
|
|
|
public void NewsUpdate(UpdateEvent event)
|
|
|
|
{
|
|
|
|
if (event.getType() != UpdateType.MIN_04)
|
|
|
|
return;
|
|
|
|
|
|
|
|
RefreshNews();
|
|
|
|
}
|
|
|
|
|
2013-10-22 06:46:31 +02:00
|
|
|
@EventHandler
|
2014-09-14 06:10:40 +02:00
|
|
|
public void DragonBarUpdate(UpdateEvent event)
|
2013-10-22 06:46:31 +02:00
|
|
|
{
|
|
|
|
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
|
2013-10-25 10:20:14 +02:00
|
|
|
if (UtilTime.elapsed(_newsTime, 4500))
|
2013-10-22 06:46:31 +02:00
|
|
|
{
|
|
|
|
_newsIndex = (_newsIndex + 1)%_news.length;
|
|
|
|
_newsTime = System.currentTimeMillis();
|
2014-10-15 18:11:32 +02:00
|
|
|
}
|
2014-07-22 00:06:33 +02:00
|
|
|
if (_newsIndex >= _news.length)
|
|
|
|
{
|
|
|
|
// Resets newsIndex if outside of bounds of news array after RefreshNews but before UtilTime.elapsed above
|
|
|
|
_newsIndex = 0;
|
2013-10-22 06:46:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//Text
|
|
|
|
String text = col + C.Bold + "MINEPLEX" + ChatColor.RESET + " - " + _news[_newsIndex];
|
|
|
|
if (text.length() > 64)
|
|
|
|
text = text.substring(0, 64);
|
2014-08-07 04:03:48 +02:00
|
|
|
|
|
|
|
double healthPercent = (double)_newsIndex/(double)(_news.length-1);
|
2014-10-25 04:40:34 +02:00
|
|
|
|
|
|
|
UtilTextTop.display(text, UtilServer.getPlayers());
|
2013-12-21 10:05:42 +01:00
|
|
|
|
2014-12-17 06:31:51 +01:00
|
|
|
for (Creature pet : Manager.getPetManager().getPets())
|
|
|
|
{
|
|
|
|
if (pet instanceof Wither)
|
|
|
|
{
|
|
|
|
pet.setCustomName(text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-21 10:05:42 +01:00
|
|
|
for (Mount mount : Manager.GetMount().getMounts())
|
|
|
|
{
|
2014-08-22 20:30:41 +02:00
|
|
|
if (mount instanceof MountDragon)
|
2013-12-21 10:05:42 +01:00
|
|
|
{
|
2014-08-22 20:30:41 +02:00
|
|
|
((MountDragon)mount).SetName(text);
|
2014-12-17 06:31:51 +01:00
|
|
|
//((MountDragon)mount).setHealthPercent(healthPercent);
|
2013-12-21 10:05:42 +01:00
|
|
|
}
|
|
|
|
}
|
2014-12-16 06:18:09 +01:00
|
|
|
|
|
|
|
for (Gadget gadget : Manager.GetGadget().getGadgets(GadgetType.Morph))
|
|
|
|
{
|
|
|
|
if (gadget instanceof MorphWither)
|
|
|
|
{
|
|
|
|
((MorphWither)gadget).setWitherData(text, healthPercent);
|
|
|
|
}
|
|
|
|
}
|
2013-10-22 06:46:31 +02:00
|
|
|
}
|
2014-09-14 06:10:40 +02:00
|
|
|
|
|
|
|
@EventHandler
|
|
|
|
public void joinNewsOverlay(final PlayerJoinEvent event)
|
|
|
|
{
|
|
|
|
for (int i=0 ; i<_news.length ; i++)
|
|
|
|
{
|
|
|
|
final int count = i;
|
|
|
|
|
2015-02-26 06:07:07 +01:00
|
|
|
UtilServer.getServer().getScheduler().runTaskLater(Manager.getPlugin(), new Runnable()
|
2014-09-14 06:10:40 +02:00
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void run()
|
|
|
|
{
|
2014-10-25 04:40:34 +02:00
|
|
|
UtilTextMiddle.display(C.cGold + C.Bold + "MINEPLEX" + ChatColor.RESET, _news[_news.length - 1 - count] + ChatColor.RESET, (count == 0) ? 20 : 0, 60, 20, event.getPlayer());
|
2014-09-14 06:10:40 +02:00
|
|
|
}
|
|
|
|
}, 60 * i + (i != 0 ? 20 : 0));
|
|
|
|
}
|
|
|
|
}
|
2013-10-22 06:46:31 +02:00
|
|
|
}
|