From a09ee5f1f45dcbf37814ff6818b905d4c194835a Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Thu, 2 Oct 2014 16:12:16 +0200 Subject: [PATCH] MOTD --- .../java/com/massivecraft/factions/Lang.java | 1 + .../java/com/massivecraft/factions/Perm.java | 1 + .../factions/cmd/CmdFactions.java | 2 + .../factions/cmd/CmdFactionsMotd.java | 91 +++++++++++++++++++ .../massivecraft/factions/entity/Faction.java | 56 ++++++++++++ .../massivecraft/factions/entity/MConf.java | 7 ++ .../listeners/FactionsListenerMain.java | 85 +++++++++++++++++ src/main/resources/plugin.yml | 3 + 8 files changed, 246 insertions(+) create mode 100644 src/main/java/com/massivecraft/factions/cmd/CmdFactionsMotd.java diff --git a/src/main/java/com/massivecraft/factions/Lang.java b/src/main/java/com/massivecraft/factions/Lang.java index f052393f..bd513278 100644 --- a/src/main/java/com/massivecraft/factions/Lang.java +++ b/src/main/java/com/massivecraft/factions/Lang.java @@ -5,5 +5,6 @@ import com.massivecraft.massivecore.util.Txt; public class Lang { public static final String FACTION_NODESCRIPTION = Txt.parse("no description set"); + public static final String FACTION_NOMOTD = Txt.parse("no message of the day set"); public static final String PLAYER_NOTITLE = Txt.parse("no title set"); } diff --git a/src/main/java/com/massivecraft/factions/Perm.java b/src/main/java/com/massivecraft/factions/Perm.java index 21055147..6f17200e 100644 --- a/src/main/java/com/massivecraft/factions/Perm.java +++ b/src/main/java/com/massivecraft/factions/Perm.java @@ -44,6 +44,7 @@ public enum Perm MONEY_F2P("money.f2p"), MONEY_P2F("money.p2f"), MONEY_WITHDRAW("money.withdraw"), + MOTD("motd"), OFFICER("officer"), OFFICER_ANY("officer.any"), OPEN("open"), diff --git a/src/main/java/com/massivecraft/factions/cmd/CmdFactions.java b/src/main/java/com/massivecraft/factions/cmd/CmdFactions.java index ccfc835f..4b9ebdca 100644 --- a/src/main/java/com/massivecraft/factions/cmd/CmdFactions.java +++ b/src/main/java/com/massivecraft/factions/cmd/CmdFactions.java @@ -24,6 +24,7 @@ public class CmdFactions extends FactionsCommand public CmdFactionsCreate cmdFactionsCreate = new CmdFactionsCreate(); public CmdFactionsName cmdFactionsName = new CmdFactionsName(); public CmdFactionsDescription cmdFactionsDescription = new CmdFactionsDescription(); + public CmdFactionsMotd cmdFactionsMotd = new CmdFactionsMotd(); public CmdFactionsSethome cmdFactionsSethome = new CmdFactionsSethome(); public CmdFactionsUnsethome cmdFactionsUnsethome = new CmdFactionsUnsethome(); public CmdFactionsInvite cmdFactionsInvite = new CmdFactionsInvite(); @@ -69,6 +70,7 @@ public class CmdFactions extends FactionsCommand this.addSubCommand(this.cmdFactionsCreate); this.addSubCommand(this.cmdFactionsName); this.addSubCommand(this.cmdFactionsDescription); + this.addSubCommand(this.cmdFactionsMotd); this.addSubCommand(this.cmdFactionsSethome); this.addSubCommand(this.cmdFactionsUnsethome); this.addSubCommand(this.cmdFactionsInvite); diff --git a/src/main/java/com/massivecraft/factions/cmd/CmdFactionsMotd.java b/src/main/java/com/massivecraft/factions/cmd/CmdFactionsMotd.java new file mode 100644 index 00000000..0611f317 --- /dev/null +++ b/src/main/java/com/massivecraft/factions/cmd/CmdFactionsMotd.java @@ -0,0 +1,91 @@ +package com.massivecraft.factions.cmd; + +import com.massivecraft.factions.Perm; +import com.massivecraft.factions.Rel; +import com.massivecraft.factions.cmd.req.ReqRoleIsAtLeast; +import com.massivecraft.factions.entity.MPlayer; +import com.massivecraft.massivecore.cmd.req.ReqHasPerm; +import com.massivecraft.massivecore.mixin.Mixin; +import com.massivecraft.massivecore.util.MUtil; +import com.massivecraft.massivecore.util.Txt; + +public class CmdFactionsMotd extends FactionsCommand +{ + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + public CmdFactionsMotd() + { + // Aliases + this.addAliases("motd"); + + // Args + this.addOptionalArg("new", "read"); + this.setErrorOnToManyArgs(false); + + // Requirements + this.addRequirements(ReqHasPerm.get(Perm.MOTD.node)); + } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public void perform() + { + // Read + if ( ! this.argIsSet(0)) + { + sendMessage(msenderFaction.getMotdMessages()); + return; + } + + // Check Role + if ( ! ReqRoleIsAtLeast.get(Rel.OFFICER).apply(sender, this)) + { + sendMessage(ReqRoleIsAtLeast.get(Rel.OFFICER).createErrorMessage(sender, this)); + return; + } + + // Args + String target = this.argConcatFrom(0); + target = target.trim(); + target = Txt.parse(target); + + // Removal + if (target != null && MUtil.set("", "r", "remove", "d", "delete", "del", "e", "erase", "none", "null", "nothing").contains(target)) + { + target = null; + } + + // Get Old + String old = null; + if (msenderFaction.hasMotd()) + { + old = msenderFaction.getMotd(); + } + + // Target Desc + String targetDesc = target; + if (targetDesc == null) targetDesc = Txt.parse("nothing"); + + // NoChange + if (MUtil.equals(old, target)) + { + msg("The motd for %s is already: %s", msenderFaction.describeTo(msender, true), target); + return; + } + + // Apply + msenderFaction.setMotd(target); + + // Inform + for (MPlayer follower : msenderFaction.getMPlayers()) + { + follower.msg("%s set your faction motd to:\n%s", Mixin.getDisplayName(sender, follower), msenderFaction.getMotd()); + } + } + +} diff --git a/src/main/java/com/massivecraft/factions/entity/Faction.java b/src/main/java/com/massivecraft/factions/entity/Faction.java index dda96830..4d78662f 100644 --- a/src/main/java/com/massivecraft/factions/entity/Faction.java +++ b/src/main/java/com/massivecraft/factions/entity/Faction.java @@ -43,6 +43,7 @@ public class Faction extends Entity implements EconomyParticipator { this.setName(that.name); this.setDescription(that.description); + this.setMotd(that.motd); this.setCreatedAtMillis(that.createdAtMillis); this.setHome(that.home); this.setPowerBoost(that.powerBoost); @@ -87,6 +88,11 @@ public class Faction extends Entity implements EconomyParticipator // Null means the faction has no description. private String description = null; + // Factions can optionally set a message of the day. + // This message will be shown when logging on to the server. + // Null means the faction has no motd + private String motd = null; + // We store the creation date for the faction. // It can be displayed on info pages etc. private long createdAtMillis = System.currentTimeMillis(); @@ -232,6 +238,56 @@ public class Faction extends Entity implements EconomyParticipator this.changed(); } + // -------------------------------------------- // + // FIELD: motd + // -------------------------------------------- // + + // RAW + + public boolean hasMotd() + { + return this.motd != null; + } + + public String getMotd() + { + if (this.hasMotd()) return Txt.parse(this.motd); + return Lang.FACTION_NOMOTD; + } + + public void setMotd(String motd) + { + // Clean input + String target = motd; + if (target != null) + { + target = target.trim(); + if (target.length() == 0) + { + target = null; + } + } + + // Detect Nochange + if (MUtil.equals(this.motd, target)) return; + + // Apply + this.motd = target; + + // Mark as changed + this.changed(); + } + + // FINER + + public List getMotdMessages() + { + final String title = Txt.titleize(this.getName() + " - Message of the Day"); + final String motd = "" + this.getMotd(); + final List messages = Txt.parse(MUtil.list(title, motd)); + return messages; + } + // -------------------------------------------- // // FIELD: createdAtMillis // -------------------------------------------- // diff --git a/src/main/java/com/massivecraft/factions/entity/MConf.java b/src/main/java/com/massivecraft/factions/entity/MConf.java index e994b0d1..dfac68c2 100644 --- a/src/main/java/com/massivecraft/factions/entity/MConf.java +++ b/src/main/java/com/massivecraft/factions/entity/MConf.java @@ -102,6 +102,13 @@ public class MConf extends Entity public String defaultPlayerFactionId = Factions.ID_NONE; public Rel defaultPlayerRole = Rel.RECRUIT; public double defaultPlayerPower = 0.0; + + // -------------------------------------------- // + // MOTD + // -------------------------------------------- // + + public EventPriority motdPriority = EventPriority.NORMAL; + public int motdDelayTicks = -1; // -------------------------------------------- // // POWER diff --git a/src/main/java/com/massivecraft/factions/listeners/FactionsListenerMain.java b/src/main/java/com/massivecraft/factions/listeners/FactionsListenerMain.java index 9af6ff69..87858345 100644 --- a/src/main/java/com/massivecraft/factions/listeners/FactionsListenerMain.java +++ b/src/main/java/com/massivecraft/factions/listeners/FactionsListenerMain.java @@ -49,6 +49,7 @@ import org.bukkit.event.player.PlayerBucketEmptyEvent; import org.bukkit.event.player.PlayerBucketFillEvent; import org.bukkit.event.player.PlayerCommandPreprocessEvent; import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerKickEvent; import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.event.player.PlayerRespawnEvent; @@ -68,6 +69,7 @@ import com.massivecraft.factions.event.EventFactionsPvpDisallowed; import com.massivecraft.factions.event.EventFactionsPowerChange; import com.massivecraft.factions.event.EventFactionsPowerChange.PowerChangeReason; import com.massivecraft.factions.util.VisualizeUtil; +import com.massivecraft.massivecore.mixin.Mixin; import com.massivecraft.massivecore.ps.PS; import com.massivecraft.massivecore.util.MUtil; import com.massivecraft.massivecore.util.PlayerUtil; @@ -92,6 +94,89 @@ public class FactionsListenerMain implements Listener Bukkit.getPluginManager().registerEvents(this, Factions.get()); } + // -------------------------------------------- // + // MOTD + // -------------------------------------------- // + + public static void motd(PlayerJoinEvent event, EventPriority currentPriority) + { + // Gather info ... + final Player player = event.getPlayer(); + final MPlayer mplayer = MPlayer.get(player); + final Faction faction = mplayer.getFaction(); + + // ... if there is a motd ... + if ( ! faction.hasMotd()) return; + + // ... and this is the priority we are supposed to act on ... + if (currentPriority != MConf.get().motdPriority) return; + + // ... and this is an actual join ... + if (!Mixin.isActualJoin(event)) return; + + // ... then prepare the messages ... + final List messages = faction.getMotdMessages(); + + // ... and send to the player. + if (MConf.get().motdDelayTicks < 0) + { + Mixin.messageOne(player, messages); + } + else + { + Bukkit.getScheduler().scheduleSyncDelayedTask(Factions.get(), new Runnable() + { + @Override + public void run() + { + Mixin.messageOne(player, messages); + } + }, MConf.get().motdDelayTicks); + } + } + + // Can't be cancelled + @EventHandler(priority = EventPriority.LOWEST) + public void motdLowest(PlayerJoinEvent event) + { + motd(event, EventPriority.LOWEST); + } + + // Can't be cancelled + @EventHandler(priority = EventPriority.LOW) + public void motdLow(PlayerJoinEvent event) + { + motd(event, EventPriority.LOW); + } + + // Can't be cancelled + @EventHandler(priority = EventPriority.NORMAL) + public void motdNormal(PlayerJoinEvent event) + { + motd(event, EventPriority.NORMAL); + } + + // Can't be cancelled + @EventHandler(priority = EventPriority.HIGH) + public void motdHigh(PlayerJoinEvent event) + { + motd(event, EventPriority.HIGH); + } + + // Can't be cancelled + @EventHandler(priority = EventPriority.HIGHEST) + public void motdHighest(PlayerJoinEvent event) + { + motd(event, EventPriority.HIGHEST); + } + + // Can't be cancelled + @EventHandler(priority = EventPriority.MONITOR) + public void motdMonitor(PlayerJoinEvent event) + { + motd(event, EventPriority.MONITOR); + } + // -------------------------------------------- // // CHUNK CHANGE: DETECT // -------------------------------------------- // diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index a868487c..5fc4dd39 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -44,6 +44,7 @@ permissions: factions.money.f2p: {description: transfer f --> p, default: false} factions.money.p2f: {description: transfer p --> f, default: false} factions.money.withdraw: {description: withdraw from faction, default: false} + factions.motd: {description: faction motd, default: false} factions.officer: {description: make player officer, default: false} factions.officer.any: {description: set officers for another faction, default: false} factions.open: {description: set if invitation is required to join, default: false} @@ -101,6 +102,7 @@ permissions: factions.money.f2p: true factions.money.p2f: true factions.money.withdraw: true + factions.motd: true factions.officer: true factions.officer.any: true factions.open: true @@ -179,6 +181,7 @@ permissions: factions.money.f2p: true factions.money.p2f: true factions.money.withdraw: true + factions.motd: true factions.officer: true factions.open: true factions.perm: true