Created
January 11, 2013 01:43
-
-
Save MooseElkingtons/4507287 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package com.mutinycraft.jigsaw.FactionsExtra; | |
import java.io.*; | |
import java.util.*; | |
import java.util.logging.*; | |
import org.bukkit.configuration.file.FileConfiguration; | |
import org.bukkit.configuration.file.YamlConfiguration; | |
import org.bukkit.event.Listener; | |
import org.bukkit.plugin.java.JavaPlugin; | |
import com.massivecraft.factions.Factions; | |
import com.massivecraft.factions.P; | |
import com.mutinycraft.jigsaw.FactionsExtra.util.ValueComparator; | |
public class FactionsExtra extends JavaPlugin implements Listener { | |
Logger log; | |
File factionFile; | |
FileConfiguration factionData; | |
private FactionsExtraCommandExecutor cmdExecutor; | |
private static final String VERSION = " v1.0"; | |
/***************** Enable *****************/ | |
@Override | |
public void onEnable() { | |
log = this.getLogger(); | |
getServer().getPluginManager().registerEvents(this, this); | |
new FactionsExtraEventHandler(this); | |
try { | |
factionFile = new File(getDataFolder(), "FactionScores.yml"); | |
firstRun(); | |
// Get Factions | |
// getFactionsFromFile(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
factionData = new YamlConfiguration(); | |
loadYamls(); | |
cmdExecutor = new FactionsExtraCommandExecutor(this); | |
getCommand("factionscore").setExecutor(cmdExecutor); | |
log.info(this.getName() + VERSION + " enabled!"); | |
} | |
private void firstRun() throws Exception { | |
if (!factionFile.exists()) { | |
factionFile.getParentFile().mkdirs(); | |
copy(getResource("FactionScores.yml"), factionFile); | |
} | |
} | |
private void copy(InputStream in, File file) { | |
try { | |
if(!file.exists()) | |
file.createNewFile(); | |
OutputStream fout = new FileOutputStream(file); | |
byte[] buf = new byte[1024]; | |
int len; | |
while((len=in.read(buf))>0){ | |
fout.write(buf,0,len); | |
} | |
fout.close(); | |
in.close(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
try { | |
OutputStream fout = new FileOutputStream(file); | |
byte[] buf = new byte[1024]; | |
int len; | |
while ((len = in.read(buf)) > 0) { | |
fout.write(buf, 0, len); | |
} | |
fout.close(); | |
in.close(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
private void loadYamls() { | |
try { | |
factionData.load(factionFile); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
/***************** Config Handling *****************/ | |
public FileConfiguration getCustomConfig() { | |
if (factionData == null) { | |
this.reloadCustomConfig(); | |
} | |
return factionData; | |
} | |
public void reloadCustomConfig() { | |
if (factionFile == null) { | |
factionFile = new File(getDataFolder(), "customConfig.yml"); | |
} | |
factionData = YamlConfiguration.loadConfiguration(factionFile); | |
InputStream defConfigStream = this.getResource("customConfig.yml"); | |
if (defConfigStream != null) { | |
YamlConfiguration defConfig = YamlConfiguration | |
.loadConfiguration(defConfigStream); | |
factionData.setDefaults(defConfig); | |
} | |
} | |
/***************** Score Handling *****************/ | |
public List<Integer> getFactionData(String factionID) { | |
if (factionData.contains(factionID)) { | |
return factionData.getIntegerList(factionID); | |
} else { | |
addFaction(factionID); | |
return factionData.getIntegerList(factionID); | |
} | |
} | |
public int getFactionTier(String factionID) { | |
List<Integer> data = factionData.getIntegerList(factionID); | |
if (data != null && data.size() >= 2) { | |
return data.get(1); | |
} | |
return 0; | |
} | |
public int getFactionScore(String factionID) { | |
List<Integer> data = factionData.getIntegerList(factionID); | |
if (data != null && data.size() >= 2) { | |
return data.get(0); | |
} | |
return 0; | |
} | |
public List<String> getFactionTagList() { | |
List<String> list = new ArrayList<String>(); | |
Set<String> tags = P.p.getFactionTags(); | |
Iterator<String> i = tags.iterator(); | |
while(i.hasNext()) | |
list.add(i.next()); | |
return list; | |
} | |
public String[] getFactionList() { | |
Map<String, Integer> list = new HashMap<String,Integer>(); | |
TreeMap<String, Integer> sortedList = | |
new TreeMap<String, Integer>(new ValueComparator(list, false)); | |
Set<String> tags = P.p.getFactionTags(); | |
Iterator<String> i = tags.iterator(); | |
while(i.hasNext()) { | |
String tag = i.next(); | |
list.put(tag, getFactionScore(tag)); | |
} | |
sortedList.putAll(list); // Sorts the original list out. | |
return (String[]) sortedList.keySet().toArray(); | |
} | |
public void addFaction(String factionID){ | |
if (!factionData.contains(factionID)) { | |
List<Integer> data = new ArrayList<Integer>(1); | |
// Default score of 0 | |
data.add(0); | |
// Default tier of 1 | |
data.add(1); | |
updateFaction(factionID, data); | |
} | |
} | |
public void updateFaction(String factionID, List<Integer> data) { | |
getCustomConfig().set(factionID, data); | |
if (factionData == null || factionFile == null) { | |
return; | |
} | |
try { | |
getCustomConfig().save(factionFile); | |
} catch (IOException ex) { | |
this.getLogger().log(Level.SEVERE, | |
"Could not save data to " + factionFile, ex); | |
} | |
} | |
// This should only be called one time when the plugin is first ran. | |
public void getFactionsFromFile() { | |
Set<String> factions = P.p.getFactionTags(); | |
Iterator<String> iter = factions.iterator(); | |
while (iter.hasNext()) { | |
String factionName = iter.next(); | |
List<Integer> data = new ArrayList<Integer>(1); | |
// Default score of 0 | |
data.add(0); | |
// Default tier of 1 | |
data.add(1); | |
updateFaction(Factions.i.getByTag(factionName).getId(), data); | |
} | |
} | |
/***************** Disable *****************/ | |
public void onDisable() { | |
log.info(this.getName() + VERSION + " disabled!"); | |
} | |
} |
This file contains hidden or 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
package com.mutinycraft.jigsaw.FactionsExtra; | |
import org.bukkit.ChatColor; | |
import org.bukkit.command.Command; | |
import org.bukkit.command.CommandExecutor; | |
import org.bukkit.command.CommandSender; | |
import com.massivecraft.factions.Faction; | |
import com.massivecraft.factions.Factions; | |
public class FactionsExtraCommandExecutor implements CommandExecutor { | |
FactionsExtra plugin; | |
public FactionsExtraCommandExecutor(FactionsExtra pl) { | |
plugin = pl; | |
} | |
@Override | |
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { | |
if(cmd.getName().equalsIgnoreCase("ftop")) { | |
String[] list = plugin.getFactionList(); | |
boolean invalid = false; | |
int max = list.length / 10; | |
int page = 0; | |
try { | |
page = Integer.parseInt(args[1]); | |
} catch(NumberFormatException e) { | |
invalid = true; | |
} | |
invalid = page > max || page < 0; | |
if(invalid) { | |
sender.sendMessage("&cUsage: /ftop [page]"); | |
return true; | |
} | |
sender.sendMessage("&e===[ Faction Leaderboard &c| &3"+page+" &e/ &3"+max+" &e]==="); | |
for(int i = (page * 10); i < (page * 10) + 10; i++) { | |
String tag = list[i]; | |
sender.sendMessage("&b"+i+". "+tag+ " Score: &a"+plugin.getFactionScore(tag) + | |
"&b Tier: &a"+plugin.getFactionTier(tag)); | |
} | |
return true; | |
} | |
if (cmd.getName().equalsIgnoreCase("factionscore")) { | |
if (args.length == 1) { | |
int score = 0; | |
int tier = 0; | |
String fName = null; | |
try { | |
Faction f = Factions.i.getByTag(args[0]); | |
score = plugin.getFactionScore(f.getId()); | |
tier = plugin.getFactionTier(f.getId()); | |
fName = f.getTag(); | |
} catch (Exception e) { | |
score = 0; | |
tier = 0; | |
fName = null; | |
} | |
if (tier >= 1) { | |
sender.sendMessage(ChatColor.GREEN + fName + " has " | |
+ score + " points and is a tier " + tier | |
+ " faction."); | |
} else { | |
sender.sendMessage(ChatColor.GREEN | |
+ "The score of that faction is not recorded."); | |
} | |
return true; | |
} else { | |
sender.sendMessage(ChatColor.RED | |
+ "Usage: /factionscore [Faction Name]"); | |
return true; | |
} | |
} | |
return false; | |
} | |
} |
This file contains hidden or 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
package com.mutinycraft.jigsaw.FactionsExtra; | |
import java.io.BufferedWriter; | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
import java.io.PrintWriter; | |
import java.util.List; | |
import org.bukkit.ChatColor; | |
import org.bukkit.entity.Player; | |
import org.bukkit.event.EventHandler; | |
import org.bukkit.event.EventPriority; | |
import org.bukkit.event.Listener; | |
import org.bukkit.event.entity.PlayerDeathEvent; | |
import com.massivecraft.factions.Board; | |
import com.massivecraft.factions.FPlayer; | |
import com.massivecraft.factions.FPlayers; | |
import com.massivecraft.factions.Faction; | |
import com.massivecraft.factions.event.FactionCreateEvent; | |
import com.massivecraft.factions.event.LandClaimEvent; | |
public class FactionsExtraEventHandler implements Listener { | |
private FactionsExtra plugin; | |
private static final String FACTION_WORLD = "BG"; | |
private static final int POINT_PER_KILL_1_1 = 2; | |
private static final int POINT_PER_KILL_1_2 = 4; | |
private static final int POINT_PER_KILL_2_1 = 1; | |
private static final int POINT_PER_KILL_2_2 = 2; | |
private static final int POINT_PER_CLAIM_1_1 = 6; | |
private static final int POINT_PER_CLAIM_1_2 = 12; | |
private static final int POINT_PER_CLAIM_2_1 = 3; | |
private static final int POINT_PER_CLAIM_2_2 = 6; | |
public FactionsExtraEventHandler(FactionsExtra pl) { | |
this.plugin = pl; | |
plugin.getServer().getPluginManager().registerEvents(this, plugin); | |
} | |
// Events | |
@EventHandler(priority = EventPriority.LOW) | |
public void playerDeath(PlayerDeathEvent event) { | |
if (event.getEntity() instanceof Player) { | |
Player killed = (Player) event.getEntity(); | |
if (killed.getKiller() instanceof Player) { | |
Player killer = killed.getKiller(); | |
if (killer.getWorld().getName().equalsIgnoreCase(FACTION_WORLD)) { | |
factionKillHandling(killed, killer); | |
} | |
} | |
} | |
} | |
@EventHandler | |
public void landClaim(LandClaimEvent event) { | |
Faction claimedFrom = Board.getFactionAt(event.getLocation()); | |
FPlayer claimingFP = FPlayers.i.get(event.getPlayer()); | |
if (claimingFP.getFaction().getRelationTo(claimedFrom).isEnemy()) { | |
// Record data to file | |
if (getFactionTier(claimingFP.getFactionId()) == 1) { | |
if (getFactionTier(claimedFrom.getId()) == 1) { | |
addScore(claimingFP.getFactionId(), POINT_PER_CLAIM_1_1); | |
messageClaim(claimingFP.getPlayer(), POINT_PER_CLAIM_1_1); | |
} else if (getFactionTier(claimedFrom.getId()) == 2) { | |
addScore(claimingFP.getFactionId(), POINT_PER_CLAIM_1_2); | |
messageClaim(claimingFP.getPlayer(), POINT_PER_CLAIM_1_2); | |
} | |
} else if (getFactionTier(claimingFP.getFactionId()) == 2) { | |
if (getFactionTier(claimedFrom.getId()) == 1) { | |
addScore(claimingFP.getFactionId(), POINT_PER_CLAIM_2_1); | |
messageClaim(claimingFP.getPlayer(), POINT_PER_CLAIM_2_1); | |
} else if (getFactionTier(claimedFrom.getId()) == 2) { | |
addScore(claimingFP.getFactionId(), POINT_PER_CLAIM_2_2); | |
messageClaim(claimingFP.getPlayer(), POINT_PER_CLAIM_2_2); | |
} | |
} | |
recordDataInFile(claimingFP.getNameAndTag() + " claimed land from " | |
+ claimedFrom.getTag()); | |
} | |
} | |
@EventHandler | |
public void factionCreation(FactionCreateEvent event) { | |
String factionID = event.getFactionId(); | |
plugin.addFaction(factionID); | |
} | |
private void factionKillHandling(Player killed, Player killer) { | |
FPlayer killedFP = FPlayers.i.get(killed); | |
FPlayer killerFP = FPlayers.i.get(killer); | |
// Check to make sure this is not a Peaceful Faction | |
if (!killedFP.getFaction().isPeaceful() | |
&& !killerFP.getFaction().isPeaceful()) { | |
// Get Ally/Neutral/Enemy relationship | |
if (killedFP.getFaction().getRelationTo(killerFP.getFaction()) | |
.isEnemy()) { | |
// Record data to file | |
if (getFactionTier(killerFP.getFactionId()) == 1) { | |
if (getFactionTier(killedFP.getFactionId()) == 1) { | |
addScore(killerFP.getFactionId(), POINT_PER_KILL_1_1); | |
messageKill(killerFP.getPlayer(), POINT_PER_KILL_1_1); | |
} else if (getFactionTier(killedFP.getFactionId()) == 2) { | |
addScore(killerFP.getFactionId(), POINT_PER_KILL_1_2); | |
messageKill(killerFP.getPlayer(), POINT_PER_KILL_1_2); | |
} | |
} else if (getFactionTier(killerFP.getFactionId()) == 2) { | |
if (getFactionTier(killedFP.getFactionId()) == 1) { | |
addScore(killerFP.getFactionId(), POINT_PER_KILL_2_1); | |
messageKill(killerFP.getPlayer(), POINT_PER_KILL_2_1); | |
} else if (getFactionTier(killedFP.getFactionId()) == 2) { | |
addScore(killerFP.getFactionId(), POINT_PER_KILL_2_2); | |
messageKill(killerFP.getPlayer(), POINT_PER_KILL_2_2); | |
} | |
} | |
recordDataInFile(killedFP.getNameAndTag() + " was killed by " | |
+ killerFP.getNameAndTag()); | |
} | |
} | |
} | |
private void addScore(String factionID, int score) { | |
List<Integer> data = plugin.getFactionData(factionID); | |
if (data.size() >= 2) { | |
score = data.get(0) + score; | |
data.set(0, score); | |
} | |
plugin.updateFaction(factionID, data); | |
} | |
private int getFactionTier(String factionID) { | |
return plugin.getFactionTier(factionID); | |
} | |
private void recordDataInFile(String msg) { | |
try { | |
File dataFile = new File(plugin.getDataFolder(), "data.txt"); | |
PrintWriter out = new PrintWriter(new BufferedWriter( | |
new FileWriter(dataFile, true))); | |
out.println(msg); | |
out.close(); | |
} catch (FileNotFoundException e) { | |
plugin.log.severe("That file does not exist! Please create it."); | |
} catch (IOException e) { | |
plugin.log.severe("Error saving to file! Data is likely lost."); | |
} | |
} | |
private void messageKill(Player player, int points) { | |
player.sendMessage(ChatColor.GREEN | |
+ "You killed a member of an enemy faction and earned " | |
+ points + " points!"); | |
} | |
private void messageClaim(Player player, int points) { | |
player.sendMessage(ChatColor.GREEN | |
+ "You claimed land from an enemy faction and earned " + points | |
+ " points!"); | |
} | |
} |
This file contains hidden or 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
package com.mutinycraft.jigsaw.FactionsExtra.util; | |
import java.util.*; | |
/** | |
* Used to Compare and Sort values. | |
* | |
* @author MooseElkingtons | |
*/ | |
public class ValueComparator implements Comparator<String> { | |
private Map<String, Integer> map; | |
private boolean ascend; | |
public ValueComparator(Map<String, Integer> map, boolean ascend) { | |
this.ascend = ascend; | |
this.map = map; | |
} | |
@Override | |
public int compare(String value1, String value2) { | |
if(ascend) { | |
if(map.get(Integer.parseInt(value1)) | |
>= map.get(Integer.parseInt(value2))) | |
return -1; | |
else | |
return 1; | |
} else { | |
if(map.get(Integer.parseInt(value1)) | |
<= map.get(Integer.parseInt(value2))) | |
return -1; | |
else | |
return 1; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment