Add faction announcements to send to all players and show to players when they login if they were not online when it was first sent as requested in issue #82.

This commit is contained in:
drtshock 2014-10-18 03:54:45 -05:00
parent 047fe0937a
commit bed6fe0741
8 changed files with 93 additions and 6 deletions

View File

@ -563,7 +563,9 @@ public class FPlayer extends PlayerEntity implements EconomyParticipator {
/** /**
* Check if the scoreboard should be shown. Simple method to be used by above method. * Check if the scoreboard should be shown. Simple method to be used by above method.
*
* @param toShow Faction to be shown. * @param toShow Faction to be shown.
*
* @return true if should show, otherwise false. * @return true if should show, otherwise false.
*/ */
private boolean shouldShowScoreboard(Faction toShow) { private boolean shouldShowScoreboard(Faction toShow) {
@ -629,6 +631,7 @@ public class FPlayer extends PlayerEntity implements EconomyParticipator {
} }
} }
myFaction.removeAnnouncements(this);
this.resetFactionData(); this.resetFactionData();
if (myFaction.isNormal() && !perm && myFaction.getFPlayers().isEmpty()) { if (myFaction.isNormal() && !perm && myFaction.getFPlayers().isEmpty()) {

View File

@ -33,6 +33,37 @@ public class Faction extends Entity implements EconomyParticipator {
// speedy lookup of players in faction // speedy lookup of players in faction
private transient Set<FPlayer> fplayers = new HashSet<FPlayer>(); private transient Set<FPlayer> fplayers = new HashSet<FPlayer>();
private HashMap<String, List<String>> announcements;
public HashMap<String, List<String>> getAnnouncements() {
return this.announcements;
}
public void addAnnouncement(FPlayer fPlayer, String msg) {
List<String> list = announcements.containsKey(fPlayer.getId()) ? announcements.get(fPlayer.getId()) : new ArrayList<String>();
list.add(msg);
announcements.put(fPlayer.getId(), list);
}
public void sendUnreadAnnouncements(FPlayer fPlayer) {
if (!announcements.containsKey(fPlayer.getId())) {
return;
}
fPlayer.sendMessage(ChatColor.LIGHT_PURPLE + "--Unread Faction Announcements--");
for (String s : announcements.get(fPlayer.getPlayer().getUniqueId().toString())) {
fPlayer.sendMessage(s);
}
fPlayer.sendMessage(ChatColor.LIGHT_PURPLE + "--Unread Faction Announcements--");
announcements.remove(fPlayer.getId());
}
public void removeAnnouncements(FPlayer fPlayer) {
if (announcements.containsKey(fPlayer.getId())) {
announcements.remove(fPlayer.getId());
}
}
// FIELD: invites // FIELD: invites
private Set<String> invites; private Set<String> invites;
@ -236,6 +267,7 @@ public class Faction extends Entity implements EconomyParticipator {
this.permanent = false; this.permanent = false;
this.money = 0.0; this.money = 0.0;
this.powerBoost = 0.0; this.powerBoost = 0.0;
this.announcements = new HashMap<String, List<String>>();
} }
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -22,10 +22,7 @@ import org.bukkit.event.player.AsyncPlayerChatEvent;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.Arrays; import java.util.*;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level; import java.util.logging.Level;
@ -125,8 +122,7 @@ public class P extends MPlugin {
@Override @Override
public GsonBuilder getGsonBuilder() { public GsonBuilder getGsonBuilder() {
Type mapFLocToStringSetType = new TypeToken<Map<FLocation, Set<String>>>() { Type mapFLocToStringSetType = new TypeToken<Map<FLocation, Set<String>>>() {}.getType();
}.getType();
return new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().excludeFieldsWithModifiers(Modifier.TRANSIENT, Modifier.VOLATILE).registerTypeAdapter(LazyLocation.class, new MyLocationTypeAdapter()).registerTypeAdapter(mapFLocToStringSetType, new MapFLocToStringSetTypeAdapter()); return new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().excludeFieldsWithModifiers(Modifier.TRANSIENT, Modifier.VOLATILE).registerTypeAdapter(LazyLocation.class, new MyLocationTypeAdapter()).registerTypeAdapter(mapFLocToStringSetType, new MapFLocToStringSetTypeAdapter());
} }

View File

@ -0,0 +1,42 @@
package com.massivecraft.factions.cmd;
import com.massivecraft.factions.FPlayer;
import com.massivecraft.factions.struct.Permission;
import net.minecraft.util.org.apache.commons.lang3.StringUtils;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
public class CmdAnnounce extends FCommand {
public CmdAnnounce() {
super();
this.aliases.add("ann");
this.aliases.add("announce");
this.requiredArgs.add("message");
this.errorOnToManyArgs = false;
this.permission = Permission.ANNOUNCE.node;
this.disableOnLock = false;
senderMustBePlayer = true;
senderMustBeMember = true;
senderMustBeModerator = true;
}
@Override
public void perform() {
String prefix = ChatColor.GREEN + myFaction.getTag() + ChatColor.YELLOW + " [" + ChatColor.GRAY + me.getName() + ChatColor.YELLOW + "] " + ChatColor.RESET;
String message = StringUtils.join(args, " ");
for (Player player : myFaction.getOnlinePlayers()) {
player.sendMessage(prefix + message);
}
// Add for offline players.
for (FPlayer fp : myFaction.getFPlayersWhereOnline(false)) {
myFaction.addAnnouncement(fp, prefix + message);
}
}
}

View File

@ -44,6 +44,7 @@ public class CmdInvite extends FCommand {
} }
myFaction.invite(you); myFaction.invite(you);
if(!you.isOnline()) return;
// Tooltips, colors, and commands only apply to the string immediately before it. // Tooltips, colors, and commands only apply to the string immediately before it.
FancyMessage message = new FancyMessage(fme.describeTo(you, true)) FancyMessage message = new FancyMessage(fme.describeTo(you, true))

View File

@ -54,6 +54,7 @@ public class FCmdRoot extends FCommand {
public CmdWarunclaimall cmdWarunclaimall = new CmdWarunclaimall(); public CmdWarunclaimall cmdWarunclaimall = new CmdWarunclaimall();
public CmdSB cmdSB = new CmdSB(); public CmdSB cmdSB = new CmdSB();
public CmdShowInvites cmdShowInvites = new CmdShowInvites(); public CmdShowInvites cmdShowInvites = new CmdShowInvites();
public CmdAnnounce cmdAnnounce = new CmdAnnounce();
public FCmdRoot() { public FCmdRoot() {
super(); super();
@ -124,6 +125,7 @@ public class FCmdRoot extends FCommand {
this.addSubCommand(this.cmdWarunclaimall); this.addSubCommand(this.cmdWarunclaimall);
this.addSubCommand(this.cmdSB); this.addSubCommand(this.cmdSB);
this.addSubCommand(this.cmdShowInvites); this.addSubCommand(this.cmdShowInvites);
this.addSubCommand(this.cmdAnnounce);
} }
@Override @Override

View File

@ -45,6 +45,16 @@ public class FactionsPlayerListener implements Listener {
// Store player's current FLocation and notify them where they are // Store player's current FLocation and notify them where they are
me.setLastStoodAt(new FLocation(event.getPlayer().getLocation())); me.setLastStoodAt(new FLocation(event.getPlayer().getLocation()));
// Check for Faction announcements. Let's delay this so they actually see it.
Bukkit.getScheduler().runTaskLater(P.p, new Runnable() {
@Override
public void run() {
if (me.isOnline()) {
me.getFaction().sendUnreadAnnouncements(me);
}
}
}, 33L); // Don't ask me why.
if (P.p.getConfig().getBoolean("scoreboard.default-enabled", false) && P.p.cmdBase.cmdSB.showBoard(me)) { if (P.p.getConfig().getBoolean("scoreboard.default-enabled", false) && P.p.cmdBase.cmdSB.showBoard(me)) {
Bukkit.getScheduler().runTaskLater(P.p, new Runnable() { // I think we still have to delay this a few seconds. Bukkit.getScheduler().runTaskLater(P.p, new Runnable() { // I think we still have to delay this a few seconds.
@Override @Override

View File

@ -9,6 +9,7 @@ public enum Permission {
OWNERSHIP_BYPASS("ownershipbypass"), OWNERSHIP_BYPASS("ownershipbypass"),
ADMIN("admin"), ADMIN("admin"),
ADMIN_ANY("admin.any"), ADMIN_ANY("admin.any"),
ANNOUNCE("announce"),
AUTOCLAIM("autoclaim"), AUTOCLAIM("autoclaim"),
BYPASS("bypass"), BYPASS("bypass"),
CHAT("chat"), CHAT("chat"),