Created
May 11, 2014 04:37
-
-
Save macalinao/c5ee6e0d98b7ea61412a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package net.og_mc.mattlog; | |
import com.massivecraft.factions.FPlayer; | |
import com.massivecraft.factions.FPlayers; | |
import com.massivecraft.factions.struct.Relation; | |
import org.bukkit.Bukkit; | |
import org.bukkit.ChatColor; | |
import org.bukkit.Location; | |
import org.bukkit.Sound; | |
import org.bukkit.entity.Player; | |
import org.bukkit.scheduler.BukkitRunnable; | |
import org.bukkit.scoreboard.DisplaySlot; | |
import org.bukkit.scoreboard.Objective; | |
import org.bukkit.scoreboard.Scoreboard; | |
import org.bukkit.scoreboard.ScoreboardManager; | |
/** | |
* | |
* @author ian | |
*/ | |
public class Tag { | |
public String player; | |
public String enemy; | |
public long time; | |
public boolean delete = false; | |
public boolean timeSafeLast = false; | |
public boolean distSafeLast = false; | |
public TagScoreboardRemoveTask task; | |
public Tag(String player, String enemy) { | |
this.player = player; | |
this.enemy = enemy; | |
refresh(); | |
} | |
public void refresh() { | |
time = System.currentTimeMillis(); | |
} | |
public Player getPlayer() { | |
return Bukkit.getPlayerExact(player); | |
} | |
public Player getEnemy() { | |
return Bukkit.getPlayerExact(enemy); | |
} | |
public boolean canLogSafely() { | |
return delete || (isTimeExpired() && isSafeDistance()); | |
} | |
public boolean isSafeDistance() { | |
return getDistance() >= Config.i.combatRange; | |
} | |
public boolean isTimeExpired() { | |
return getTime() == 0; | |
} | |
/** | |
* Gets the time until the tag is expired. | |
* | |
* @return | |
*/ | |
public int getTime() { | |
int t = Config.i.combatDuration - ((int) (System.currentTimeMillis() - time) / 1000); | |
if (t < 0) { | |
t = 0; | |
} | |
return t; | |
} | |
public int getDistance() { | |
Player p = getPlayer(); | |
FPlayer fp = FPlayers.i.get(p); | |
Location l = p.getLocation(); | |
int lowest = Integer.MAX_VALUE; | |
for (Player pl : Bukkit.getOnlinePlayers()) { | |
if (pl.hasPermission("mattlog.ignore")) { | |
continue; | |
} | |
if (pl.equals(p)) { | |
continue; | |
} | |
FPlayer fpl = FPlayers.i.get(pl); | |
Relation r = fp.getRelationTo(fpl); | |
if (r.isEnemy() || r.isNeutral()) { | |
Location el = pl.getLocation(); | |
if (!el.getWorld().equals(l.getWorld())) { | |
continue; | |
} | |
int distSq = (int) l.distanceSquared(el); | |
if (distSq < lowest) { | |
lowest = distSq; | |
} | |
} | |
} | |
int dist = (int) Math.sqrt(lowest); | |
if (dist > Config.i.maxRange) { | |
dist = Config.i.maxRange + 1; | |
} | |
return dist; | |
} | |
public void update() { | |
Player p = getPlayer(); | |
// Player combat logged. Mark for deletion. | |
if (p == null) { | |
delete(); | |
return; | |
} | |
if (isTimeExpired() && isSafeDistance()) { | |
p.playSound(p.getLocation(), Sound.NOTE_PIANO, 2f, 1f); | |
// Fuck this shit. Why does this have to be so complicated? | |
final String name = p.getName(); | |
(new BukkitRunnable() { | |
@Override | |
public void run() { | |
Player p = Bukkit.getPlayerExact(name); | |
if (p == null) { | |
return; | |
} | |
p.playSound(p.getLocation(), Sound.NOTE_PIANO, 2f, 1.5f); | |
} | |
}).runTaskLater(MattLog.i, 10L); | |
// We are no longer tagged. | |
delete(); | |
return; | |
} else { | |
if (isTimeExpired()) { | |
if (!timeSafeLast) { | |
p.playSound(p.getLocation(), Sound.NOTE_PIANO, 2f, 1f); | |
timeSafeLast = true; | |
} | |
} else { | |
if (timeSafeLast) { | |
timeSafeLast = false; | |
} | |
} | |
if (isSafeDistance()) { | |
if (!distSafeLast) { | |
p.playSound(p.getLocation(), Sound.NOTE_PIANO, 2f, 1f); | |
distSafeLast = true; | |
} | |
} else { | |
if (distSafeLast) { | |
distSafeLast = false; | |
} | |
} | |
} | |
ScoreboardManager sm = Bukkit.getScoreboardManager(); | |
Scoreboard s = p.getScoreboard(); | |
if (s.getObjective("mattlog") == null) { | |
s = sm.getNewScoreboard(); | |
p.setScoreboard(s); | |
} | |
Objective o = s.getObjective("mattlog"); | |
if (o == null) { | |
o = s.registerNewObjective("mattlog", "dummy"); | |
o.setDisplaySlot(DisplaySlot.SIDEBAR); | |
o.setDisplayName(ChatColor.DARK_RED.toString() + ChatColor.BOLD + "Logger"); | |
} | |
// Again, fuck this shit | |
if (isTimeExpired()) { | |
s.resetScores(Bukkit.getOfflinePlayer(ChatColor.RED + "Time:")); | |
o.getScore(Bukkit.getOfflinePlayer(ChatColor.GREEN + "Time:")).setScore(1); | |
o.getScore(Bukkit.getOfflinePlayer(ChatColor.GREEN + "Time:")).setScore(0); | |
} else { | |
s.resetScores(Bukkit.getOfflinePlayer(ChatColor.GREEN + "Time:")); | |
o.getScore(Bukkit.getOfflinePlayer(ChatColor.RED + "Time:")).setScore(getTime()); | |
} | |
if (isSafeDistance()) { | |
s.resetScores(Bukkit.getOfflinePlayer(ChatColor.RED + "Distance:")); | |
o.getScore(Bukkit.getOfflinePlayer(ChatColor.GREEN + "Distance:")).setScore(getDistance()); | |
} else { | |
s.resetScores(Bukkit.getOfflinePlayer(ChatColor.GREEN + "Distance:")); | |
o.getScore(Bukkit.getOfflinePlayer(ChatColor.RED + "Distance:")).setScore(getDistance()); | |
} | |
} | |
public void delete() { | |
if (delete) { | |
return; | |
} | |
delete = true; | |
Player p = getPlayer(); | |
if (p != null) { | |
p.sendMessage(ChatColor.YELLOW + "You are now out of combat."); | |
} | |
task = new TagScoreboardRemoveTask(this); | |
task.runTaskLater(MattLog.i, 20L * 2); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment