Saber-Factions/src/com/bukkit/mcteam/factions/listeners/FactionsPlayerListener.java

151 lines
4.6 KiB
Java
Raw Normal View History

package com.bukkit.mcteam.factions.listeners;
2011-02-06 13:36:11 +01:00
import java.util.*;
import java.util.logging.Logger;
2011-02-06 13:36:11 +01:00
import org.bukkit.entity.*;
import org.bukkit.event.player.PlayerChatEvent;
import org.bukkit.event.player.PlayerEvent;
import org.bukkit.event.player.PlayerListener;
import org.bukkit.event.player.PlayerMoveEvent;
import com.bukkit.mcteam.factions.Commands;
import com.bukkit.mcteam.factions.Factions;
2011-02-06 13:36:11 +01:00
import com.bukkit.mcteam.factions.entities.*;
import com.bukkit.mcteam.factions.util.TextUtil;
import com.bukkit.mcteam.factions.util.Log;
2011-02-06 13:36:11 +01:00
public class FactionsPlayerListener extends PlayerListener{
public Factions plugin;
public FactionsPlayerListener(Factions plugin) {
this.plugin = plugin;
}
/**
* If someone says something that starts with the factions base command
* we handle that command.
*/
@Override
2011-03-04 17:41:04 +01:00
public void onPlayerCommandPreprocess(PlayerChatEvent event) {
2011-02-06 13:36:11 +01:00
Player player = event.getPlayer();
String msg = event.getMessage();
if (handleCommandOrChat(player, msg)) {
event.setCancelled(true);
}
}
@Override
public void onPlayerChat(PlayerChatEvent event) {
if (event.isCancelled()) {
return; // Some other plugin ate this...
}
Player talkingPlayer = event.getPlayer();
2011-02-06 13:36:11 +01:00
String msg = event.getMessage();
// Is this a faction command?...
if ( handleCommandOrChat(talkingPlayer, msg) ) {
// ... Yes it was! We should choke the chat message.
event.setCancelled(true);
return;
}
// ... it was not a command. This means that it is a chat message!
Follower me = Follower.get(talkingPlayer);
// Is it a faction chat message?
if (me.isFactionChatting()) {
String message = String.format(Conf.factionChatFormat, me.getNameAndRelevant(me), msg);
me.getFaction().sendMessage(message, false);
Logger.getLogger("Minecraft").info("FactionChat "+me.getFaction().getTag()+": "+message);
event.setCancelled(true);
return;
}
// Are we to insert the Faction tag into the format?
// If we are not to insert it - we are done.
if ( ! Conf.chatTagEnabled) {
return;
}
String formatStart = event.getFormat().substring(0, Conf.chatTagInsertIndex);
String formatEnd = event.getFormat().substring(Conf.chatTagInsertIndex);
String nonColoredMsgFormat = formatStart + me.getChatTag() + formatEnd;
// Relation Colored?
if (Conf.chatTagRelationColored) {
// We must choke the standard message and send out individual messages to all players
// Why? Because the relations will differ.
event.setCancelled(true);
2011-02-13 17:04:06 +01:00
for (Player listeningPlayer : Factions.factions.getServer().getOnlinePlayers()) {
Follower you = Follower.get(listeningPlayer);
String yourFormat = formatStart + me.getChatTag(you) + formatEnd;
listeningPlayer.sendMessage(String.format(yourFormat, talkingPlayer.getDisplayName(), msg));
2011-02-06 13:36:11 +01:00
}
// Write to the log... We will write the non colored message.
String nonColoredMsg = String.format(nonColoredMsgFormat, talkingPlayer.getDisplayName(), msg);
Logger.getLogger("Minecraft").info(nonColoredMsg);
} else {
// No relation color.
event.setFormat(nonColoredMsgFormat);
2011-02-06 13:36:11 +01:00
}
}
public boolean handleCommandOrChat(Player player, String msg) {
ArrayList<String> tokens = TextUtil.split(msg.trim());
if (Conf.aliasBase.contains(tokens.get(0))) {
tokens.remove(0);
Follower follower = Follower.get(player);
Commands.base(follower, tokens);
return true;
}
return false;
}
@Override
public void onPlayerJoin(PlayerEvent event) {
//Follower.get(event.getPlayer()).sendJoinInfo();
}
@Override
public void onPlayerQuit(PlayerEvent event) {
Follower follower = Follower.get(event.getPlayer());
Log.debug("Saved follower on player quit: "+follower.getName());
2011-02-06 13:36:11 +01:00
follower.save(); // We save the followers on logout in order to save their non autosaved state like power.
}
@Override
public void onPlayerMove(PlayerMoveEvent event) {
2011-02-13 17:04:06 +01:00
Follower me = Follower.get(event.getPlayer());
2011-02-06 13:36:11 +01:00
// Did we change coord?
2011-02-13 17:04:06 +01:00
Coord coordFrom = me.lastStoodInCoord;
Coord coordTo = Coord.from(event.getTo());
2011-02-06 13:36:11 +01:00
if (coordFrom.equals(coordTo)) {
return;
}
// Yes we did change coord (:
2011-02-13 17:04:06 +01:00
me.lastStoodInCoord = coordTo;
2011-02-13 11:18:08 +01:00
Board board = Board.get(event.getPlayer().getWorld());
2011-02-06 13:36:11 +01:00
if (me.isMapAutoUpdating()) {
2011-02-13 11:18:08 +01:00
me.sendMessage(board.getMap(me.getFaction(), Coord.from(me), me.getPlayer().getLocation().getYaw()), false);
2011-02-06 13:36:11 +01:00
} else {
// Did we change "host"(faction)?
2011-02-13 11:18:08 +01:00
Faction factionFrom = board.getFactionAt(coordFrom);
Faction factionTo = board.getFactionAt(coordTo);
2011-02-06 13:36:11 +01:00
if ( factionFrom != factionTo) {
me.sendFactionHereMessage();
}
}
}
}