Skip to content

Commit

Permalink
Broadcaster: Some more formatting / renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
Frank Baumann committed Oct 22, 2013
1 parent 0b489da commit 42c9025
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 76 deletions.
4 changes: 3 additions & 1 deletion src/com/dre/managerxl/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;

import com.dre.managerxl.broadcaster.Broadcaster;

public class Config {

// File
Expand Down Expand Up @@ -45,7 +47,7 @@ private void loadBroadcasterConfigSection(FileConfiguration ymlFile) {
this.broadcasterConfigSection = ymlFile.getConfigurationSection("Broadcaster");

if(broadcasterConfigSection == null){
Broadcast.saveDefaultConfig(this);
Broadcaster.saveDefaultConfig(this);
this.broadcasterConfigSection = ymlFile.getConfigurationSection("Broadcaster");
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/com/dre/managerxl/MPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;

import com.dre.managerxl.broadcaster.BroadcastPlayerMsg;
import com.dre.managerxl.broadcaster.BroadcasterPlayerMsg;

public class MPlayer {
private static Set<MPlayer> mPlayers = new HashSet<MPlayer>();
Expand All @@ -30,13 +30,13 @@ public class MPlayer {
private long lastTeleport;

/* BroadcasterPlayer */
public HashMap<Integer,BroadcastPlayerMsg> playerMsgs = new HashMap<Integer,BroadcastPlayerMsg>();
public HashMap<Integer,BroadcasterPlayerMsg> playerMsgs = new HashMap<Integer,BroadcasterPlayerMsg>();

public BroadcastPlayerMsg getBMsg(int id) {
BroadcastPlayerMsg msg = playerMsgs.get(id);
public BroadcasterPlayerMsg getBMsg(int id) {
BroadcasterPlayerMsg msg = playerMsgs.get(id);

if(msg == null){
msg = new BroadcastPlayerMsg(id, this);
msg = new BroadcasterPlayerMsg(id, this);
}

return msg;
Expand Down
16 changes: 7 additions & 9 deletions src/com/dre/managerxl/P.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.bukkit.plugin.java.JavaPlugin;
import org.dynmap.DynmapCommonAPI;

import com.dre.managerxl.broadcaster.Broadcaster;
import com.dre.managerxl.commands.MCommand;
import com.dre.managerxl.listeners.PlayerListener;
import com.dre.managerxl.listeners.ServerListener;
Expand All @@ -35,10 +36,10 @@ public LanguageReader getLanguageReader() {
}

/* Broadcaster */
private Broadcast broadcast;
private Broadcaster broadcaster;

public Broadcast getBroadcast(){
return broadcast;
public Broadcaster getBroadcast(){
return broadcaster;
}

@Override
Expand All @@ -52,7 +53,7 @@ public void onEnable() {
this.languageReader = new LanguageReader(new File(p.getDataFolder(), "languages/default.yml"));

// Load Broadcaster
this.broadcast= new Broadcast();
this.broadcaster= new Broadcaster();

// Setup Permissions
setupPermissions();
Expand All @@ -79,10 +80,6 @@ public void onEnable() {

@Override
public void onDisable() {

// Save Broadcaster
broadcast.save();

// Save
SaveAll();
}
Expand All @@ -91,7 +88,6 @@ public void onDisable() {
public void initSchedulers() {
p.getServer().getScheduler().scheduleSyncRepeatingTask(p, new Runnable() {
public void run() {
broadcast.save();
SaveAll();
}
}, 0L, 18000L);
Expand All @@ -104,6 +100,8 @@ public void SaveAll() {
} else {
P.p.log(Level.WARNING, getLanguageReader().get("Log_Error_PlayersSaved"));
}

broadcaster.save();
}

public void LoadAll() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.dre.managerxl;
package com.dre.managerxl.broadcaster;

import java.io.File;
import java.io.IOException;
Expand All @@ -11,10 +11,11 @@
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;

import com.dre.managerxl.broadcaster.BroadcastMsg;
import com.dre.managerxl.broadcaster.BroadcastPlayerMsg;
import com.dre.managerxl.Config;
import com.dre.managerxl.MPlayer;
import com.dre.managerxl.P;

public class Broadcast {
public class Broadcaster {

public static int maxLevel;// = 10;
public static int sendsPerLevel;// = 20;
Expand All @@ -36,7 +37,7 @@ public class Broadcast {
private File broadcastDataFile;
private File broadcastMsgFile;

public Broadcast(){
public Broadcaster(){
loadConfig();

initializeBroadcaster();
Expand Down Expand Up @@ -79,8 +80,8 @@ private void initBroadcastSchedulers() {
public void run() {
Player[] players = P.p.getServer().getOnlinePlayers();

for(Integer msgId: BroadcastMsg.messages.keySet()){
BroadcastMsg msg = BroadcastMsg.messages.get(msgId);
for(Integer msgId: BroadcasterMsg.messages.keySet()){
BroadcasterMsg msg = BroadcasterMsg.messages.get(msgId);
boolean endThisMsg = false;
if(msg.getEndTime()<System.currentTimeMillis()){
endThisMsg = true;
Expand All @@ -105,8 +106,8 @@ public void run() {
}

private void deleteOldMessages() {
for (Iterator<Map.Entry<Integer, BroadcastMsg>> it = BroadcastMsg.messages.entrySet().iterator(); it.hasNext();) {
Map.Entry<Integer, BroadcastMsg> entry = it.next();
for (Iterator<Map.Entry<Integer, BroadcasterMsg>> it = BroadcasterMsg.messages.entrySet().iterator(); it.hasNext();) {
Map.Entry<Integer, BroadcasterMsg> entry = it.next();
if (entry.getValue().isDelete()) {

it.remove();
Expand All @@ -115,9 +116,9 @@ private void deleteOldMessages() {
}

private void deleteOldPlayerData() {
for(MPlayer bPlayer : MPlayer.get()){
for (Iterator<Map.Entry<Integer,BroadcastPlayerMsg>> it = bPlayer.playerMsgs.entrySet().iterator(); it.hasNext();) {
Map.Entry<Integer,BroadcastPlayerMsg> entry = it.next();
for(MPlayer mPlayer : MPlayer.get()){
for (Iterator<Map.Entry<Integer,BroadcasterPlayerMsg>> it = mPlayer.playerMsgs.entrySet().iterator(); it.hasNext();) {
Map.Entry<Integer,BroadcasterPlayerMsg> entry = it.next();
if (entry.getValue().getBroadcastMsg() == null) {
it.remove();
break;
Expand All @@ -127,10 +128,10 @@ private void deleteOldPlayerData() {
}


public static void broadcastMsg(Player player, BroadcastMsg msg){
public static void broadcastMsg(Player player, BroadcasterMsg msg){

MPlayer bPlayer = MPlayer.getOrCreate(player.getName());
BroadcastPlayerMsg bMsg = bPlayer.getBMsg(msg.getId());
MPlayer mPlayer = MPlayer.getOrCreate(player.getName());
BroadcasterPlayerMsg bMsg = mPlayer.getBMsg(msg.getId());

bMsg.setSendCount(bMsg.getSendCount()+1);

Expand All @@ -151,9 +152,9 @@ public static void broadcastMsg(Player player, BroadcastMsg msg){

}

public static long getNextSendTime(Player player, BroadcastMsg msg){
MPlayer bPlayer = MPlayer.getOrCreate(player.getName());
BroadcastPlayerMsg bMsg = bPlayer.getBMsg(msg.getId());
public static long getNextSendTime(Player player, BroadcasterMsg msg){
MPlayer mPlayer = MPlayer.getOrCreate(player.getName());
BroadcasterPlayerMsg bMsg = mPlayer.getBMsg(msg.getId());
long lastSend = bMsg.getLastSend();
int playerLevel = bMsg.getPlayerLevel();
int timeDiff = maxTimeInMinutes-minTimeInMinutes;
Expand Down Expand Up @@ -184,11 +185,11 @@ public void load(){
public void saveMessages(){
FileConfiguration file = new YamlConfiguration();

file.set("idCounterBroadcasterVar",BroadcastMsg.idCounter);
file.set("idCounterBroadcasterVar",BroadcasterMsg.idCounter);


for(Integer id:BroadcastMsg.messages.keySet()){
BroadcastMsg message = BroadcastMsg.messages.get(id);
for(Integer id:BroadcasterMsg.messages.keySet()){
BroadcasterMsg message = BroadcasterMsg.messages.get(id);
file.set(id+".type", message.getType());
file.set(id+".msg", message.getMsg());
file.set(id+".endTime", message.getEndTime());
Expand All @@ -206,20 +207,20 @@ public void saveMessages(){
public void saveData(){
FileConfiguration file = new YamlConfiguration();

for(MPlayer bPlayer: MPlayer.get()){
for(Integer id: bPlayer.playerMsgs.keySet()){
BroadcastPlayerMsg bpMsg = bPlayer.playerMsgs.get(id);
file.set(bPlayer.getPlayer()+"."+id+".playerLevel", bpMsg.getPlayerLevel());
file.set(bPlayer.getPlayer()+"."+id+".sendCount", bpMsg.getSendCount());
file.set(bPlayer.getPlayer()+"."+id+".lastSend", bpMsg.getLastSend());
for(MPlayer mPlayer: MPlayer.get()){
for(Integer id: mPlayer.playerMsgs.keySet()){
BroadcasterPlayerMsg bpMsg = mPlayer.playerMsgs.get(id);
file.set(mPlayer.getPlayer()+"."+id+".playerLevel", bpMsg.getPlayerLevel());
file.set(mPlayer.getPlayer()+"."+id+".sendCount", bpMsg.getSendCount());
file.set(mPlayer.getPlayer()+"."+id+".lastSend", bpMsg.getLastSend());
}
}
}

public void loadMessages(){
FileConfiguration file = YamlConfiguration.loadConfiguration(broadcastMsgFile);

BroadcastMsg.idCounter = file.getInt("idCounterBroadcasterVar");
BroadcasterMsg.idCounter = file.getInt("idCounterBroadcasterVar");

String type;
String msg;
Expand All @@ -232,26 +233,26 @@ public void loadMessages(){
msg = file.getString(id+".msg");
endTime = file.getLong(id+".endTime");
startTime = file.getLong(id+".startTime");
new BroadcastMsg(Integer.parseInt(id), type, msg, endTime, startTime);
new BroadcasterMsg(Integer.parseInt(id), type, msg, endTime, startTime);
}
}
}

public void loadData(){
FileConfiguration file = YamlConfiguration.loadConfiguration(broadcastDataFile);
MPlayer bPlayer;
MPlayer mPlayer;
int playerLevel;
int sendCount;
long lastSend;

for(String name: file.getKeys(false)){
bPlayer = MPlayer.getOrCreate(name);
mPlayer = MPlayer.getOrCreate(name);
ConfigurationSection section = file.getConfigurationSection(name);
for(String id: section.getKeys(false)){
playerLevel = file.getInt(name+"."+id+".playerLevel");
sendCount = file.getInt(name+"."+id+".sendCount");
lastSend = file.getInt(name+"."+id+".lastSend");
new BroadcastPlayerMsg(Integer.parseInt(id), bPlayer, playerLevel, sendCount, lastSend);
new BroadcasterPlayerMsg(Integer.parseInt(id), mPlayer, playerLevel, sendCount, lastSend);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@

import org.apache.commons.lang.time.DateFormatUtils;

import com.dre.managerxl.Broadcast;

public class BroadcastMsg {
public class BroadcasterMsg {

public static HashMap<Integer, BroadcastMsg> messages = new HashMap<Integer, BroadcastMsg>();
public static HashMap<Integer, BroadcasterMsg> messages = new HashMap<Integer, BroadcasterMsg>();

private int id;

Expand All @@ -23,7 +21,7 @@ public class BroadcastMsg {

private boolean delete = false;

public BroadcastMsg(int id, String type, String msg, long endTime, long startTime){
public BroadcasterMsg(int id, String type, String msg, long endTime, long startTime){
this.id=id;
this.type=type;
this.msg = msg;
Expand All @@ -33,7 +31,7 @@ public BroadcastMsg(int id, String type, String msg, long endTime, long startTim
calculateTimeLevel();
}

public BroadcastMsg(String type, String msg, long endTime){
public BroadcasterMsg(String type, String msg, long endTime){
this.id=idCounter;
idCounter++;
this.type=type;
Expand Down Expand Up @@ -69,28 +67,28 @@ public int getId(){
}

public String getMsgAsBroadcast() {
String beginColor = Broadcast.broadcastColor;
String text = beginColor + "["+Broadcast.broadcastText+"]:&f" + msg;
String beginColor = Broadcaster.broadcastColor;
String text = beginColor + "["+Broadcaster.broadcastText+"]:&f" + msg;
return text;
}

public String getMsgAsNews() {
String beginColor = Broadcast.timeColors.get(timeLevel);
String text = beginColor + "["+Broadcast.newsText+"]:&f" + msg;
String beginColor = Broadcaster.timeColors.get(timeLevel);
String text = beginColor + "["+Broadcaster.newsText+"]:&f" + msg;
return text;
}

public String getMsgAsDate() {
String beginColor = Broadcast.timeColors.get(Broadcast.timeColors.size()-1-timeLevel);
String text = beginColor + "["+Broadcast.dateText+" "+DateFormatUtils.format(endTime, "dd MM yy HH:mm")+"]:&f" + msg;
String beginColor = Broadcaster.timeColors.get(Broadcaster.timeColors.size()-1-timeLevel);
String text = beginColor + "["+Broadcaster.dateText+" "+DateFormatUtils.format(endTime, "dd MM yy HH:mm")+"]:&f" + msg;
return text;
}

public void calculateTimeLevel(){
long diff = endTime-startTime;
if(diff == 0){timeLevel = 0; return;}
timeLevel = (int)((System.currentTimeMillis() - diff) /
(diff / Broadcast.timeColors.size()));
(diff / Broadcaster.timeColors.size()));
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,32 @@

import com.dre.managerxl.MPlayer;

public class BroadcastPlayerMsg {
public class BroadcasterPlayerMsg {

BroadcastMsg msg;
BroadcasterMsg msg;

MPlayer bPlayer;
MPlayer mPlayer;

private int playerLevel;

private int sendCount;

private long lastSend;

public BroadcastPlayerMsg(int id,MPlayer bPlayer, int playerLevel, int sendCount, long lastSend){
this.msg = BroadcastMsg.messages.get(id);
public BroadcasterPlayerMsg(int id, MPlayer mPlayer, int playerLevel, int sendCount, long lastSend){
this.msg = BroadcasterMsg.messages.get(id);
this.playerLevel = playerLevel;
this.lastSend = lastSend;
this.sendCount = sendCount;
this.bPlayer = bPlayer;
bPlayer.playerMsgs.put(id, this);
this.mPlayer = mPlayer;
this.mPlayer.playerMsgs.put(id, this);
}

public BroadcastPlayerMsg(int id,MPlayer bPlayer){
public BroadcasterPlayerMsg(int id,MPlayer bPlayer){
this(id, bPlayer, 1, 0, 0L);
}

public BroadcastMsg getBroadcastMsg(){
public BroadcasterMsg getBroadcastMsg(){
return msg;
}

Expand Down
4 changes: 2 additions & 2 deletions src/com/dre/managerxl/commands/managing/AddBroadcast.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.bukkit.command.CommandSender;

import com.dre.managerxl.P;
import com.dre.managerxl.broadcaster.BroadcastMsg;
import com.dre.managerxl.broadcaster.BroadcasterMsg;
import com.dre.managerxl.commands.MCommand;
import com.dre.managerxl.util.MUtility;

Expand All @@ -27,7 +27,7 @@ public void onExecute(String[] args, CommandSender sender) {
String msg = MUtility.parseMessage(args, 0);
long endtime = Long.MAX_VALUE;

new BroadcastMsg(type, msg, endtime);
new BroadcasterMsg(type, msg, endtime);
}else{
sender.sendMessage(help);
}
Expand Down
Loading

0 comments on commit 42c9025

Please sign in to comment.