Abstract Data storage method for future implementations. Thanks to Ryan from Reactive MC

Also included:
-Heavily optimized loading process
-Optimizations for various commands.
This commit is contained in:
t00thpick1
2014-10-19 01:37:25 -04:00
parent 48e43ceba0
commit ee52016a87
65 changed files with 3229 additions and 2577 deletions

View File

@@ -53,7 +53,7 @@ public class CmdAdmin extends FCommand {
// only perform a FPlayerJoinEvent when newLeader isn't actually in the faction
if (fyou.getFaction() != targetFaction) {
FPlayerJoinEvent event = new FPlayerJoinEvent(FPlayers.i.get(me), targetFaction, FPlayerJoinEvent.PlayerJoinReason.LEADER);
FPlayerJoinEvent event = new FPlayerJoinEvent(FPlayers.getInstance().getByPlayer(me), targetFaction, FPlayerJoinEvent.PlayerJoinReason.LEADER);
Bukkit.getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) {
return;
@@ -78,7 +78,7 @@ public class CmdAdmin extends FCommand {
msg("<i>You have promoted %s<i> to the position of faction admin.", fyou.describeTo(fme, true));
// Inform all players
for (FPlayer fplayer : FPlayers.i.getOnline()) {
for (FPlayer fplayer : FPlayers.getInstance().getOnlinePlayers()) {
fplayer.msg("%s<i> gave %s<i> the leadership of %s<i>.", senderIsConsole ? "A server admin" : fme.describeTo(fplayer, true), fyou.describeTo(fplayer), targetFaction.describeTo(fplayer));
}
}

View File

@@ -0,0 +1,39 @@
package com.massivecraft.factions.cmd;
import org.bukkit.command.ConsoleCommandSender;
import com.massivecraft.factions.Conf;
import com.massivecraft.factions.Conf.Backend;
import com.massivecraft.factions.zcore.persist.json.FactionsJSON;
public class CmdConvert extends FCommand {
public CmdConvert() {
this.aliases.add("convert");
this.requiredArgs.add("[MYSQL|JSON]");
}
@Override
public void perform() {
if (!(this.sender instanceof ConsoleCommandSender)) {
this.sender.sendMessage("Console only command");
}
Backend nb = Backend.valueOf(this.argAsString(0).toUpperCase());
if (nb == Conf.backEnd) {
this.sender.sendMessage("Already running that backend");
return;
}
switch (nb) {
case JSON:
FactionsJSON.convertTo();
break;
default:
this.sender.sendMessage("Invalid backend");
return;
}
Conf.backEnd = nb;
}
}

View File

@@ -5,6 +5,8 @@ import com.massivecraft.factions.event.FPlayerJoinEvent;
import com.massivecraft.factions.event.FactionCreateEvent;
import com.massivecraft.factions.struct.Permission;
import com.massivecraft.factions.struct.Role;
import com.massivecraft.factions.util.MiscUtil;
import org.bukkit.Bukkit;
import java.util.ArrayList;
@@ -37,12 +39,12 @@ public class CmdCreate extends FCommand {
return;
}
if (Factions.i.isTagTaken(tag)) {
if (Factions.getInstance().isTagTaken(tag)) {
msg("<b>That tag is already in use.");
return;
}
ArrayList<String> tagValidationErrors = Factions.validateTag(tag);
ArrayList<String> tagValidationErrors = MiscUtil.validateTag(tag);
if (tagValidationErrors.size() > 0) {
sendMessage(tagValidationErrors);
return;
@@ -65,7 +67,7 @@ public class CmdCreate extends FCommand {
return;
}
Faction faction = Factions.i.create();
Faction faction = Factions.getInstance().createFaction();
// TODO: Why would this even happen??? Auto increment clash??
if (faction == null) {
@@ -77,7 +79,7 @@ public class CmdCreate extends FCommand {
faction.setTag(tag);
// trigger the faction join event for the creator
FPlayerJoinEvent joinEvent = new FPlayerJoinEvent(FPlayers.i.get(me), faction, FPlayerJoinEvent.PlayerJoinReason.CREATE);
FPlayerJoinEvent joinEvent = new FPlayerJoinEvent(FPlayers.getInstance().getByPlayer(me), faction, FPlayerJoinEvent.PlayerJoinReason.CREATE);
Bukkit.getServer().getPluginManager().callEvent(joinEvent);
// join event cannot be cancelled or you'll have an empty faction
@@ -85,7 +87,7 @@ public class CmdCreate extends FCommand {
fme.setRole(Role.ADMIN);
fme.setFaction(faction);
for (FPlayer follower : FPlayers.i.getOnline()) {
for (FPlayer follower : FPlayers.getInstance().getOnlinePlayers()) {
follower.msg("%s<i> created a new faction %s", fme.describeTo(follower, true), faction.getTag(follower));
}

View File

@@ -41,7 +41,7 @@ public class CmdDescription extends FCommand {
}
// Broadcast the description to everyone
for (FPlayer fplayer : FPlayers.i.getOnline()) {
for (FPlayer fplayer : FPlayers.getInstance().getOnlinePlayers()) {
fplayer.msg("<i>The faction %s<i> changed their description to:", myFaction.describeTo(fplayer));
fplayer.sendMessage(myFaction.getDescription()); // players can inject "&" or "`" or "<i>" or whatever in their description; &k is particularly interesting looking
}

View File

@@ -68,7 +68,7 @@ public class CmdDisband extends FCommand {
}
// Inform all players
for (FPlayer fplayer : FPlayers.i.getOnline()) {
for (FPlayer fplayer : FPlayers.getInstance().getOnlinePlayers()) {
String who = senderIsConsole ? "A server admin" : fme.describeTo(fplayer);
if (fplayer.getFaction() == faction) {
fplayer.msg("<h>%s<i> disbanded your faction.", who);
@@ -92,6 +92,6 @@ public class CmdDisband extends FCommand {
}
}
faction.detach();
Factions.getInstance().removeFaction(faction.getId());
}
}

View File

@@ -61,7 +61,7 @@ public class CmdHome extends FCommand {
return;
}
Faction faction = Board.getFactionAt(new FLocation(me.getLocation()));
Faction faction = Board.getInstance().getFactionAt(new FLocation(me.getLocation()));
Location loc = me.getLocation().clone();
// if player is not in a safe zone or their own faction territory, only allow teleport if no enemies are nearby
@@ -78,7 +78,7 @@ public class CmdHome extends FCommand {
continue;
}
FPlayer fp = FPlayers.i.get(p);
FPlayer fp = FPlayers.getInstance().getByPlayer(p);
if (fme.getRelationTo(fp) != Relation.ENEMY) {
continue;
}

View File

@@ -77,7 +77,7 @@ public class CmdJoin extends FCommand {
}
// trigger the join event (cancellable)
FPlayerJoinEvent joinEvent = new FPlayerJoinEvent(FPlayers.i.get(me), faction, FPlayerJoinEvent.PlayerJoinReason.COMMAND);
FPlayerJoinEvent joinEvent = new FPlayerJoinEvent(FPlayers.getInstance().getByPlayer(me), faction, FPlayerJoinEvent.PlayerJoinReason.COMMAND);
Bukkit.getServer().getPluginManager().callEvent(joinEvent);
if (joinEvent.isCancelled()) {
return;

View File

@@ -36,10 +36,10 @@ public class CmdList extends FCommand {
return;
}
ArrayList<Faction> factionList = new ArrayList<Faction>(Factions.i.get());
factionList.remove(Factions.i.getNone());
factionList.remove(Factions.i.getSafeZone());
factionList.remove(Factions.i.getWarZone());
ArrayList<Faction> factionList = Factions.getInstance().getAllFactions();
factionList.remove(Factions.getInstance().getNone());
factionList.remove(Factions.getInstance().getSafeZone());
factionList.remove(Factions.getInstance().getWarZone());
// Sort by total followers first
Collections.sort(factionList, new Comparator<Faction>() {
@@ -90,7 +90,7 @@ public class CmdList extends FCommand {
sendMessage(p.txt.getPage(lines, this.argAsInt(0, 1), "Faction List"));
*/
factionList.add(0, Factions.i.getNone());
factionList.add(0, Factions.getInstance().getNone());
final int pageheight = 9;
int pagenumber = this.argAsInt(0, 1);
@@ -110,7 +110,7 @@ public class CmdList extends FCommand {
for (Faction faction : factionList.subList(start, end)) {
if (faction.isNone()) {
lines.add(p.txt.parse("<i>Factionless<i> %d online", Factions.i.getNone().getFPlayersWhereOnline(true).size()));
lines.add(p.txt.parse("<i>Factionless<i> %d online", Factions.getInstance().getNone().getFPlayersWhereOnline(true).size()));
continue;
}
lines.add(p.txt.parse("%s<i> %d/%d online, %d/%d/%d", faction.getTag(fme), faction.getFPlayersWhereOnline(true).size(), faction.getFPlayers().size(), faction.getLandRounded(), faction.getPowerRounded(), faction.getPowerMaxRounded()));

View File

@@ -56,7 +56,7 @@ public class CmdMap extends FCommand {
}
public void showMap() {
sendMessage(Board.getMap(myFaction, new FLocation(fme), fme.getPlayer().getLocation().getYaw()));
sendMessage(Board.getInstance().getMap(myFaction, new FLocation(fme), fme.getPlayer().getLocation().getYaw()));
}
}

View File

@@ -1,8 +1,8 @@
package com.massivecraft.factions.cmd;
import com.massivecraft.factions.Conf;
import com.massivecraft.factions.Faction;
import com.massivecraft.factions.Factions;
import com.massivecraft.factions.FPlayer;
import com.massivecraft.factions.FPlayers;
import com.massivecraft.factions.struct.Permission;
public class CmdOpen extends FCommand {
@@ -35,12 +35,12 @@ public class CmdOpen extends FCommand {
String open = myFaction.getOpen() ? "open" : "closed";
// Inform
myFaction.msg("%s<i> changed the faction to <h>%s<i>.", fme.describeTo(myFaction, true), open);
for (Faction faction : Factions.i.get()) {
if (faction == myFaction) {
for (FPlayer fplayer : FPlayers.getInstance().getOnlinePlayers()) {
if (fplayer.getFactionId() == myFaction.getId()) {
fplayer.msg("%s<i> changed the faction to <h>%s<i>.", open);
continue;
}
faction.msg("<i>The faction %s<i> is now %s", myFaction.getTag(faction), open);
fplayer.msg("<i>The faction %s<i> is now %s", myFaction.getTag(fplayer.getFaction()), open);
}
}

View File

@@ -49,7 +49,7 @@ public class CmdOwner extends FCommand {
FLocation flocation = new FLocation(fme);
Faction factionHere = Board.getFactionAt(flocation);
Faction factionHere = Board.getInstance().getFactionAt(flocation);
if (factionHere != myFaction) {
if (!hasBypass) {
fme.msg("<b>This land is not claimed by your faction, so you can't set ownership of it.");

View File

@@ -39,13 +39,13 @@ public class CmdOwnerList extends FCommand {
FLocation flocation = new FLocation(fme);
if (Board.getFactionAt(flocation) != myFaction) {
if (Board.getInstance().getFactionAt(flocation) != myFaction) {
if (!hasBypass) {
fme.msg("<b>This land is not claimed by your faction.");
return;
}
myFaction = Board.getFactionAt(flocation);
myFaction = Board.getInstance().getFactionAt(flocation);
if (!myFaction.isNormal()) {
fme.msg("<i>This land is not claimed by any faction, thus no owners.");
return;

View File

@@ -40,7 +40,7 @@ public class CmdPeaceful extends FCommand {
}
// Inform all players
for (FPlayer fplayer : FPlayers.i.getOnline()) {
for (FPlayer fplayer : FPlayers.getInstance().getOnlinePlayers()) {
if (fplayer.getFaction() == faction) {
fplayer.msg((fme == null ? "A server admin" : fme.describeTo(fplayer, true)) + "<i> has " + change + " your faction.");
} else {

View File

@@ -44,7 +44,7 @@ public class CmdPermanent extends FCommand {
P.p.log((fme == null ? "A server admin" : fme.getName()) + " " + change + " the faction \"" + faction.getTag() + "\".");
// Inform all players
for (FPlayer fplayer : FPlayers.i.getOnline()) {
for (FPlayer fplayer : FPlayers.getInstance().getOnlinePlayers()) {
if (fplayer.getFaction() == faction) {
fplayer.msg((fme == null ? "A server admin" : fme.describeTo(fplayer, true)) + "<i> " + change + " your faction.");
} else {

View File

@@ -28,7 +28,7 @@ public class CmdSafeunclaimall extends FCommand {
@Override
public void perform() {
Board.unclaimAll(Factions.i.getSafeZone().getId());
Board.getInstance().unclaimAll(Factions.getInstance().getSafeZone().getId());
msg("<i>You unclaimed ALL safe zone land.");
if (Conf.logLandUnclaims) {

View File

@@ -27,9 +27,9 @@ public class CmdSaveAll extends FCommand {
@Override
public void perform() {
FPlayers.i.saveToDisc();
Factions.i.saveToDisc();
Board.save();
FPlayers.getInstance().forceSave();
Factions.getInstance().forceSave();
Board.getInstance().forceSave();
Conf.save();
msg("<i>Factions saved to disk!");
}

View File

@@ -50,7 +50,7 @@ public class CmdSethome extends FCommand {
// Can the player set the faction home HERE?
if (!Permission.BYPASS.has(me) &&
Conf.homesMustBeInClaimedTerritory &&
Board.getFactionAt(new FLocation(me)) != faction) {
Board.getInstance().getFactionAt(new FLocation(me)) != faction) {
fme.msg("<b>Sorry, your faction home can only be set inside your own claimed territory.");
return;
}

View File

@@ -90,7 +90,7 @@ public class CmdShow extends FCommand {
// List relation
String allyList = p.txt.parse("<a>Allies: ");
String enemyList = p.txt.parse("<a>Enemies: ");
for (Faction otherFaction : Factions.i.get()) {
for (Faction otherFaction : Factions.getInstance().getAllFactions()) {
if (otherFaction == faction) {
continue;
}

View File

@@ -19,7 +19,7 @@ public class CmdShowInvites extends FCommand {
public void perform() {
StringBuilder sb = new StringBuilder();
for (String id : myFaction.getInvites()) {
FPlayer fp = FPlayers.i.get(id);
FPlayer fp = FPlayers.getInstance().getById(id);
sb.append(fp != null ? fp.getName() : id).append(" ");
}
msg("<a>Players with pending invites: <i> %s", sb.toString().trim());

View File

@@ -1,12 +1,15 @@
package com.massivecraft.factions.cmd;
import com.massivecraft.factions.Conf;
import com.massivecraft.factions.FPlayer;
import com.massivecraft.factions.FPlayers;
import com.massivecraft.factions.Faction;
import com.massivecraft.factions.Factions;
import com.massivecraft.factions.event.FactionRenameEvent;
import com.massivecraft.factions.scoreboards.FTeamWrapper;
import com.massivecraft.factions.struct.Permission;
import com.massivecraft.factions.util.MiscUtil;
import org.bukkit.Bukkit;
import java.util.ArrayList;
@@ -33,13 +36,12 @@ public class CmdTag extends FCommand {
String tag = this.argAsString(0);
// TODO does not first test cover selfcase?
if (Factions.i.isTagTaken(tag) && !MiscUtil.getComparisonString(tag).equals(myFaction.getComparisonTag())) {
if (Factions.getInstance().isTagTaken(tag) && !MiscUtil.getComparisonString(tag).equals(myFaction.getComparisonTag())) {
msg("<b>That tag is already taken");
return;
}
ArrayList<String> errors = new ArrayList<String>();
errors.addAll(Factions.validateTag(tag));
ArrayList<String> errors = MiscUtil.validateTag(tag);
if (errors.size() > 0) {
sendMessage(errors);
return;
@@ -66,12 +68,13 @@ public class CmdTag extends FCommand {
myFaction.setTag(tag);
// Inform
myFaction.msg("%s<i> changed your faction tag to %s", fme.describeTo(myFaction, true), myFaction.getTag(myFaction));
for (Faction faction : Factions.i.get()) {
if (faction == myFaction) {
for (FPlayer fplayer : FPlayers.getInstance().getOnlinePlayers()) {
if (fplayer.getFactionId() == myFaction.getId()) {
fplayer.msg("%s<i> changed your faction tag to %s", fme.describeTo(myFaction, true), myFaction.getTag(myFaction));
continue;
}
faction.msg("<i>The faction %s<i> changed their name to %s.", fme.getColorTo(faction) + oldtag, myFaction.getTag(faction));
Faction faction = fplayer.getFaction();
fplayer.msg("<i>The faction %s<i> changed their name to %s.", fme.getColorTo(faction) + oldtag, myFaction.getTag(faction));
}
FTeamWrapper.updatePrefixes(myFaction);

View File

@@ -28,11 +28,11 @@ public class CmdUnclaim extends FCommand {
@Override
public void perform() {
FLocation flocation = new FLocation(fme);
Faction otherFaction = Board.getFactionAt(flocation);
Faction otherFaction = Board.getInstance().getFactionAt(flocation);
if (otherFaction.isSafeZone()) {
if (Permission.MANAGE_SAFE_ZONE.has(sender)) {
Board.removeAt(flocation);
Board.getInstance().removeAt(flocation);
msg("<i>Safe zone was unclaimed.");
if (Conf.logLandUnclaims) {
@@ -44,7 +44,7 @@ public class CmdUnclaim extends FCommand {
return;
} else if (otherFaction.isWarZone()) {
if (Permission.MANAGE_WAR_ZONE.has(sender)) {
Board.removeAt(flocation);
Board.getInstance().removeAt(flocation);
msg("<i>War zone was unclaimed.");
if (Conf.logLandUnclaims) {
@@ -57,7 +57,7 @@ public class CmdUnclaim extends FCommand {
}
if (fme.isAdminBypassing()) {
Board.removeAt(flocation);
Board.getInstance().removeAt(flocation);
otherFaction.msg("%s<i> unclaimed some of your land.", fme.describeTo(otherFaction, true));
msg("<i>You unclaimed this land.");
@@ -103,7 +103,7 @@ public class CmdUnclaim extends FCommand {
}
}
Board.removeAt(flocation);
Board.getInstance().removeAt(flocation);
myFaction.msg("%s<i> unclaimed some land.", fme.describeTo(myFaction, true));
if (Conf.logLandUnclaims) {

View File

@@ -45,7 +45,7 @@ public class CmdUnclaimall extends FCommand {
Bukkit.getServer().getPluginManager().callEvent(unclaimAllEvent);
// this event cannot be cancelled
Board.unclaimAll(myFaction.getId());
Board.getInstance().unclaimAll(myFaction.getId());
myFaction.msg("%s<i> unclaimed ALL of your faction's land.", fme.describeTo(myFaction, true));
if (Conf.logLandUnclaims) {

View File

@@ -28,7 +28,7 @@ public class CmdWarunclaimall extends FCommand {
@Override
public void perform() {
Board.unclaimAll(Factions.i.getWarZone().getId());
Board.getInstance().unclaimAll(Factions.getInstance().getWarZone().getId());
msg("<i>You unclaimed ALL war zone land.");
if (Conf.logLandUnclaims) {

View File

@@ -56,6 +56,7 @@ public class FCmdRoot extends FCommand {
public CmdShowInvites cmdShowInvites = new CmdShowInvites();
public CmdAnnounce cmdAnnounce = new CmdAnnounce();
public CmdSeeChunk cmdSeeChunk = new CmdSeeChunk();
public CmdConvert cmdConvert = new CmdConvert();
public FCmdRoot() {
super();
@@ -128,6 +129,7 @@ public class FCmdRoot extends FCommand {
this.addSubCommand(this.cmdShowInvites);
this.addSubCommand(this.cmdAnnounce);
this.addSubCommand(this.cmdSeeChunk);
this.addSubCommand(this.cmdConvert);
}
@Override

View File

@@ -41,7 +41,7 @@ public abstract class FCommand extends MCommand<P> {
@Override
public void execute(CommandSender sender, List<String> args, List<MCommand<?>> commandChain) {
if (sender instanceof Player) {
this.fme = FPlayers.i.get((Player) sender);
this.fme = FPlayers.getInstance().getByPlayer((Player) sender);
this.myFaction = this.fme.getFaction();
} else {
this.fme = null;
@@ -85,19 +85,17 @@ public abstract class FCommand extends MCommand<P> {
return false;
}
FPlayer fplayer = FPlayers.i.get((Player) sender);
if (!fplayer.hasFaction()) {
if (!fme.hasFaction()) {
sender.sendMessage(p.txt.parse("<b>You are not member of any faction."));
return false;
}
if (this.senderMustBeModerator && !fplayer.getRole().isAtLeast(Role.MODERATOR)) {
if (this.senderMustBeModerator && !fme.getRole().isAtLeast(Role.MODERATOR)) {
sender.sendMessage(p.txt.parse("<b>Only faction moderators can %s.", this.getHelpShort()));
return false;
}
if (this.senderMustBeAdmin && !fplayer.getRole().isAtLeast(Role.ADMIN)) {
if (this.senderMustBeAdmin && !fme.getRole().isAtLeast(Role.ADMIN)) {
sender.sendMessage(p.txt.parse("<b>Only faction admins can %s.", this.getHelpShort()));
return false;
}
@@ -143,7 +141,7 @@ public abstract class FCommand extends MCommand<P> {
if (name != null) {
OfflinePlayer player = Bukkit.getOfflinePlayer(name);
FPlayer fplayer = FPlayers.i.get(player);
FPlayer fplayer = FPlayers.getInstance().getByOfflinePlayer(player);
if (fplayer != null) {
ret = fplayer;
}
@@ -194,18 +192,18 @@ public abstract class FCommand extends MCommand<P> {
// First we try an exact match
if (faction == null) {
faction = Factions.i.getByTag(name); // Checks for faction name match.
faction = Factions.getInstance().getByTag(name); // Checks for faction name match.
}
// Next we match faction tags
if (faction == null) {
faction = Factions.i.getBestTagMatch(name);
faction = Factions.getInstance().getBestTagMatch(name);
}
// Next we match player names
if (faction == null) {
OfflinePlayer player = Bukkit.getOfflinePlayer(name);
FPlayer fplayer = FPlayers.i.get(player);
FPlayer fplayer = FPlayers.getInstance().getByOfflinePlayer(player);
if (fplayer != null) {
faction = fplayer.getFaction();
}