From bed6fe07411e2b9b9dae77846d80c70decd6c618 Mon Sep 17 00:00:00 2001 From: drtshock Date: Sat, 18 Oct 2014 03:54:45 -0500 Subject: [PATCH] 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. --- .../com/massivecraft/factions/FPlayer.java | 3 ++ .../com/massivecraft/factions/Faction.java | 32 ++++++++++++++ .../java/com/massivecraft/factions/P.java | 8 +--- .../factions/cmd/CmdAnnounce.java | 42 +++++++++++++++++++ .../massivecraft/factions/cmd/CmdInvite.java | 1 + .../massivecraft/factions/cmd/FCmdRoot.java | 2 + .../listeners/FactionsPlayerListener.java | 10 +++++ .../factions/struct/Permission.java | 1 + 8 files changed, 93 insertions(+), 6 deletions(-) create mode 100644 src/main/java/com/massivecraft/factions/cmd/CmdAnnounce.java diff --git a/src/main/java/com/massivecraft/factions/FPlayer.java b/src/main/java/com/massivecraft/factions/FPlayer.java index a7790dc1..023afb8d 100644 --- a/src/main/java/com/massivecraft/factions/FPlayer.java +++ b/src/main/java/com/massivecraft/factions/FPlayer.java @@ -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. + * * @param toShow Faction to be shown. + * * @return true if should show, otherwise false. */ private boolean shouldShowScoreboard(Faction toShow) { @@ -629,6 +631,7 @@ public class FPlayer extends PlayerEntity implements EconomyParticipator { } } + myFaction.removeAnnouncements(this); this.resetFactionData(); if (myFaction.isNormal() && !perm && myFaction.getFPlayers().isEmpty()) { diff --git a/src/main/java/com/massivecraft/factions/Faction.java b/src/main/java/com/massivecraft/factions/Faction.java index 73e8e5d8..d4593d32 100644 --- a/src/main/java/com/massivecraft/factions/Faction.java +++ b/src/main/java/com/massivecraft/factions/Faction.java @@ -33,6 +33,37 @@ public class Faction extends Entity implements EconomyParticipator { // speedy lookup of players in faction private transient Set fplayers = new HashSet(); + private HashMap> announcements; + + public HashMap> getAnnouncements() { + return this.announcements; + } + + public void addAnnouncement(FPlayer fPlayer, String msg) { + List list = announcements.containsKey(fPlayer.getId()) ? announcements.get(fPlayer.getId()) : new ArrayList(); + 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 private Set invites; @@ -236,6 +267,7 @@ public class Faction extends Entity implements EconomyParticipator { this.permanent = false; this.money = 0.0; this.powerBoost = 0.0; + this.announcements = new HashMap>(); } // -------------------------------------------- // diff --git a/src/main/java/com/massivecraft/factions/P.java b/src/main/java/com/massivecraft/factions/P.java index cb8dbaf4..aca2beba 100644 --- a/src/main/java/com/massivecraft/factions/P.java +++ b/src/main/java/com/massivecraft/factions/P.java @@ -22,10 +22,7 @@ import org.bukkit.event.player.AsyncPlayerChatEvent; import java.lang.reflect.Modifier; import java.lang.reflect.Type; -import java.util.Arrays; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; +import java.util.*; import java.util.logging.Level; @@ -125,8 +122,7 @@ public class P extends MPlugin { @Override public GsonBuilder getGsonBuilder() { - Type mapFLocToStringSetType = new TypeToken>>() { - }.getType(); + Type mapFLocToStringSetType = new TypeToken>>() {}.getType(); return new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().excludeFieldsWithModifiers(Modifier.TRANSIENT, Modifier.VOLATILE).registerTypeAdapter(LazyLocation.class, new MyLocationTypeAdapter()).registerTypeAdapter(mapFLocToStringSetType, new MapFLocToStringSetTypeAdapter()); } diff --git a/src/main/java/com/massivecraft/factions/cmd/CmdAnnounce.java b/src/main/java/com/massivecraft/factions/cmd/CmdAnnounce.java new file mode 100644 index 00000000..c40f35fc --- /dev/null +++ b/src/main/java/com/massivecraft/factions/cmd/CmdAnnounce.java @@ -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); + } + } + +} diff --git a/src/main/java/com/massivecraft/factions/cmd/CmdInvite.java b/src/main/java/com/massivecraft/factions/cmd/CmdInvite.java index f6197d2a..61773574 100644 --- a/src/main/java/com/massivecraft/factions/cmd/CmdInvite.java +++ b/src/main/java/com/massivecraft/factions/cmd/CmdInvite.java @@ -44,6 +44,7 @@ public class CmdInvite extends FCommand { } myFaction.invite(you); + if(!you.isOnline()) return; // Tooltips, colors, and commands only apply to the string immediately before it. FancyMessage message = new FancyMessage(fme.describeTo(you, true)) diff --git a/src/main/java/com/massivecraft/factions/cmd/FCmdRoot.java b/src/main/java/com/massivecraft/factions/cmd/FCmdRoot.java index 15cb4e9a..7843722a 100644 --- a/src/main/java/com/massivecraft/factions/cmd/FCmdRoot.java +++ b/src/main/java/com/massivecraft/factions/cmd/FCmdRoot.java @@ -54,6 +54,7 @@ public class FCmdRoot extends FCommand { public CmdWarunclaimall cmdWarunclaimall = new CmdWarunclaimall(); public CmdSB cmdSB = new CmdSB(); public CmdShowInvites cmdShowInvites = new CmdShowInvites(); + public CmdAnnounce cmdAnnounce = new CmdAnnounce(); public FCmdRoot() { super(); @@ -124,6 +125,7 @@ public class FCmdRoot extends FCommand { this.addSubCommand(this.cmdWarunclaimall); this.addSubCommand(this.cmdSB); this.addSubCommand(this.cmdShowInvites); + this.addSubCommand(this.cmdAnnounce); } @Override diff --git a/src/main/java/com/massivecraft/factions/listeners/FactionsPlayerListener.java b/src/main/java/com/massivecraft/factions/listeners/FactionsPlayerListener.java index 936d55c6..2fa1f0fe 100644 --- a/src/main/java/com/massivecraft/factions/listeners/FactionsPlayerListener.java +++ b/src/main/java/com/massivecraft/factions/listeners/FactionsPlayerListener.java @@ -45,6 +45,16 @@ public class FactionsPlayerListener implements Listener { // Store player's current FLocation and notify them where they are 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)) { Bukkit.getScheduler().runTaskLater(P.p, new Runnable() { // I think we still have to delay this a few seconds. @Override diff --git a/src/main/java/com/massivecraft/factions/struct/Permission.java b/src/main/java/com/massivecraft/factions/struct/Permission.java index 90d9f0b5..b65baff9 100644 --- a/src/main/java/com/massivecraft/factions/struct/Permission.java +++ b/src/main/java/com/massivecraft/factions/struct/Permission.java @@ -9,6 +9,7 @@ public enum Permission { OWNERSHIP_BYPASS("ownershipbypass"), ADMIN("admin"), ADMIN_ANY("admin.any"), + ANNOUNCE("announce"), AUTOCLAIM("autoclaim"), BYPASS("bypass"), CHAT("chat"),