Invitations as inner entities

This commit is contained in:
Magnus Ulf Jørgensen 2017-04-20 11:05:56 +02:00
parent f10da26159
commit 60c9d7fcac
11 changed files with 199 additions and 80 deletions

View File

@ -6,13 +6,14 @@ import com.massivecraft.factions.entity.Board;
import com.massivecraft.factions.entity.BoardColl;
import com.massivecraft.factions.entity.Faction;
import com.massivecraft.factions.entity.FactionColl;
import com.massivecraft.factions.entity.Invitation;
import com.massivecraft.factions.entity.MPlayer;
import com.massivecraft.factions.entity.MPlayerColl;
import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.ps.PS;
import com.massivecraft.massivecore.store.EntityInternalMap;
import com.massivecraft.massivecore.util.Txt;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map.Entry;
@ -70,11 +71,11 @@ public class CmdFactionsClean extends FactionsCommand
for (Faction faction : FactionColl.get().getAll())
{
Collection<String> invitedPlayerIds = faction.getInvitedPlayerIds();
if (invitedPlayerIds.isEmpty()) continue;
EntityInternalMap<Invitation> invitations = faction.getInvitations();
if (invitations.isEmpty()) continue;
ret += invitedPlayerIds.size();
invitedPlayerIds.clear();
ret += invitations.size();
invitations.clear();
faction.changed();
}

View File

@ -1,12 +1,14 @@
package com.massivecraft.factions.cmd;
import com.massivecraft.factions.cmd.type.TypeMPlayer;
import com.massivecraft.factions.entity.Invitation;
import com.massivecraft.factions.entity.MPerm;
import com.massivecraft.factions.entity.MPlayer;
import com.massivecraft.factions.event.EventFactionsInvitedChange;
import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.command.type.container.TypeSet;
import com.massivecraft.massivecore.mson.Mson;
import com.massivecraft.massivecore.util.IdUtil;
import com.massivecraft.massivecore.util.Txt;
import org.bukkit.ChatColor;
@ -34,6 +36,9 @@ public class CmdFactionsInviteAdd extends FactionsCommand
// Args
Collection<MPlayer> mplayers = this.readArg();
String senderId = IdUtil.getId(sender);
long creationMillis = System.currentTimeMillis();
// MPerm
if ( ! MPerm.getPermInvite().has(msender, msenderFaction, true)) return;
@ -62,7 +67,8 @@ public class CmdFactionsInviteAdd extends FactionsCommand
msenderFaction.msg("%s<i> invited %s<i> to your faction.", msender.describeTo(msenderFaction, true), mplayer.describeTo(msenderFaction));
// Apply
msenderFaction.setInvited(mplayer, true);
Invitation invitation = new Invitation(senderId, creationMillis);
msenderFaction.invite(mplayer.getId(), invitation);
msenderFaction.changed();
}
else

View File

@ -1,18 +1,26 @@
package com.massivecraft.factions.cmd;
import com.massivecraft.factions.Perm;
import com.massivecraft.factions.Rel;
import com.massivecraft.factions.cmd.type.TypeFaction;
import com.massivecraft.factions.entity.Faction;
import com.massivecraft.factions.entity.Invitation;
import com.massivecraft.factions.entity.MPerm;
import com.massivecraft.factions.entity.MPlayer;
import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.collections.MassiveList;
import com.massivecraft.massivecore.command.Parameter;
import com.massivecraft.massivecore.comparator.ComparatorSmart;
import com.massivecraft.massivecore.mixin.MixinDisplayName;
import com.massivecraft.massivecore.pager.Pager;
import com.massivecraft.massivecore.pager.Stringifier;
import com.massivecraft.massivecore.util.TimeDiffUtil;
import com.massivecraft.massivecore.util.TimeUnit;
import com.massivecraft.massivecore.util.Txt;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map.Entry;
public class CmdFactionsInviteList extends FactionsCommand
{
@ -45,25 +53,39 @@ public class CmdFactionsInviteList extends FactionsCommand
if ( ! MPerm.getPermInvite().has(msender, msenderFaction, true)) return;
// Pager Create
final List<MPlayer> mplayers = faction.getInvitedMPlayers();
final Pager<MPlayer> pager = new Pager<>(this, "Invited Players List", page, mplayers, new Stringifier<MPlayer>()
final List<Entry<String, Invitation>> invitations = new MassiveList<>(faction.getInvitations().entrySet());
Collections.sort(invitations, new Comparator<Entry<String, Invitation>>()
{
public String toString(MPlayer target, int index)
@Override
public int compare(Entry<String, Invitation> i1, Entry<String, Invitation> i2)
{
// TODO: Madus would like to implement this in MPlayer
String targetName = target.getDisplayName(msender);
String isAre = target == msender ? "are" : "is";
Rel targetRank = target.getRole();
Faction targetFaction = target.getFaction();
String theAan = targetRank == Rel.LEADER ? "the" : Txt.aan(targetRank.name());
String rankName = Txt.getNicedEnum(targetRank).toLowerCase();
String ofIn = targetRank == Rel.LEADER ? "of" : "in";
String factionName = targetFaction.describeTo(msender, true);
if (targetFaction == msenderFaction)
return ComparatorSmart.get().compare(i2.getValue().getCreationMillis(), i1.getValue().getCreationMillis());
}
});
final long now = System.currentTimeMillis();
final Pager<Entry<String, Invitation>> pager = new Pager<>(this, "Invited Players List", page, invitations, new Stringifier<Entry<String, Invitation>>()
{
public String toString(Entry<String, Invitation> entry, int index)
{
String inviteeId = entry.getKey();
String inviterId = entry.getValue().getInviterId();
String inviteeDisplayName = MixinDisplayName.get().getDisplayName(inviteeId, sender);
String inviterDisplayName = inviterId != null ? MixinDisplayName.get().getDisplayName(inviterId, sender) : Txt.parse("<silver>unknown");
String ageDesc = "";
if (entry.getValue().getCreationMillis() != null)
{
factionName = factionName.toLowerCase();
long millis = now - entry.getValue().getCreationMillis();
LinkedHashMap<TimeUnit, Long> ageUnitcounts = TimeDiffUtil.limit(TimeDiffUtil.unitcounts(millis, TimeUnit.getAllButMillis()), 2);
ageDesc = TimeDiffUtil.formatedMinimal(ageUnitcounts, "<i>");
ageDesc = " " + ageDesc + Txt.parse(" ago");
}
return Txt.parse("%s <i>%s %s <h>%s <i>%s %s<i>.", targetName, isAre, theAan, rankName, ofIn, factionName);
return Txt.parse("%s<i> was invited by %s<reset>%s<i>.", inviteeDisplayName, inviterDisplayName, ageDesc);
}
});

View File

@ -39,15 +39,18 @@ public class CmdFactionsInviteRemove extends FactionsCommand
// Args
if ("all".equalsIgnoreCase(this.argAt(0)))
{
List<MPlayer> invitedPlayers = msenderFaction.getInvitedMPlayers();
Set<String> ids = msenderFaction.getInvitations().keySet();
// Doesn't show up if list is empty. Test at home if it worked.
if (invitedPlayers == null || invitedPlayers.isEmpty())
if (ids == null || ids.isEmpty())
{
msg("<b>Your faction has not invited anyone.");
return;
throw new MassiveException().addMsg("<b>No one is invited to your faction.");
}
all = true;
mplayers.addAll(invitedPlayers);
for (String id : ids)
{
mplayers.add(MPlayer.get(id));
}
}
else
{
@ -98,7 +101,7 @@ public class CmdFactionsInviteRemove extends FactionsCommand
}
// Apply
msenderFaction.setInvited(mplayer, false);
msenderFaction.uninvite(msender);
// If all, we do this at last. So we only do it once.
if (! all) msenderFaction.changed();

View File

@ -117,7 +117,7 @@ public class CmdFactionsJoin extends FactionsCommand
mplayer.resetFactionData();
mplayer.setFaction(faction);
faction.setInvited(mplayer, false);
faction.uninvite(mplayer);
// Derplog
if (MConf.get().logFactionJoin)

View File

@ -92,7 +92,7 @@ public class CmdFactionsKick extends FactionsCommand
{
mplayerFaction.promoteNewLeader();
}
mplayerFaction.setInvited(mplayer, false);
mplayerFaction.uninvite(mplayer);
mplayer.resetFactionData();
}

View File

@ -281,7 +281,7 @@ public class CmdFactionsRank extends FactionsCommand
target.setFaction(endFaction);
// No longer invited.
endFaction.setInvited(target, false);
endFaction.uninvite(target);
// Create recipients
Set<MPlayer> recipients = new HashSet<>();

View File

@ -13,7 +13,6 @@ import com.massivecraft.massivecore.collections.MassiveList;
import com.massivecraft.massivecore.collections.MassiveMap;
import com.massivecraft.massivecore.collections.MassiveMapDef;
import com.massivecraft.massivecore.collections.MassiveSet;
import com.massivecraft.massivecore.collections.MassiveSetDef;
import com.massivecraft.massivecore.mixin.MixinMessage;
import com.massivecraft.massivecore.money.Money;
import com.massivecraft.massivecore.predicate.Predicate;
@ -21,6 +20,7 @@ import com.massivecraft.massivecore.predicate.PredicateAnd;
import com.massivecraft.massivecore.predicate.PredicateVisibleTo;
import com.massivecraft.massivecore.ps.PS;
import com.massivecraft.massivecore.store.Entity;
import com.massivecraft.massivecore.store.EntityInternalMap;
import com.massivecraft.massivecore.store.SenderColl;
import com.massivecraft.massivecore.util.IdUtil;
import com.massivecraft.massivecore.util.MUtil;
@ -70,7 +70,7 @@ public class Faction extends Entity<Faction> implements FactionsParticipator
this.setCreatedAtMillis(that.createdAtMillis);
this.setHome(that.home);
this.setPowerBoost(that.powerBoost);
this.setInvitedPlayerIds(that.invitedPlayerIds);
this.invitations.load(that.invitations);
this.setRelationWishes(that.relationWishes);
this.setFlagIds(that.flags);
this.setPermIds(that.perms);
@ -92,6 +92,12 @@ public class Faction extends Entity<Faction> implements FactionsParticipator
}
}
// -------------------------------------------- //
// VERSION
// -------------------------------------------- //
public int version = 1;
// -------------------------------------------- //
// FIELDS: RAW
// -------------------------------------------- //
@ -136,7 +142,7 @@ public class Faction extends Entity<Faction> implements FactionsParticipator
// This is the ids of the invited players.
// They are actually "senderIds" since you can invite "@console" to your faction.
// Null means no one is invited
private MassiveSetDef<String> invitedPlayerIds = new MassiveSetDef<>();
private EntityInternalMap<Invitation> invitations = new EntityInternalMap<>(this, Invitation.class);
// The keys in this map are factionIds.
// Null means no special relation whishes.
@ -436,31 +442,14 @@ public class Faction extends Entity<Faction> implements FactionsParticipator
// RAW
public Set<String> getInvitedPlayerIds()
{
return this.invitedPlayerIds;
}
public void setInvitedPlayerIds(Collection<String> invitedPlayerIds)
{
// Clean input
MassiveSetDef<String> target = new MassiveSetDef<>(invitedPlayerIds);
// Detect Nochange
if (MUtil.equals(this.invitedPlayerIds, target)) return;
// Apply
this.invitedPlayerIds = target;
// Mark as changed
this.changed();
}
public EntityInternalMap<Invitation> getInvitations() { return this.invitations; }
// FINER
public boolean isInvited(String playerId)
{
return this.getInvitedPlayerIds().contains(playerId);
return this.getInvitations().containsKey(playerId);
}
public boolean isInvited(MPlayer mplayer)
@ -468,38 +457,20 @@ public class Faction extends Entity<Faction> implements FactionsParticipator
return this.isInvited(mplayer.getId());
}
public boolean setInvited(String playerId, boolean invited)
public boolean uninvite(String playerId)
{
List<String> invitedPlayerIds = new MassiveList<>(this.getInvitedPlayerIds());
boolean ret;
if (invited)
{
ret = invitedPlayerIds.add(playerId);
}
else
{
ret = invitedPlayerIds.remove(playerId);
}
this.setInvitedPlayerIds(invitedPlayerIds);
return ret;
return this.getInvitations().remove(playerId) != null;
}
public void setInvited(MPlayer mplayer, boolean invited)
public boolean uninvite(MPlayer mplayer)
{
this.setInvited(mplayer.getId(), invited);
return uninvite(mplayer.getId());
}
public List<MPlayer> getInvitedMPlayers()
public void invite(String playerId, Invitation invitation)
{
List<MPlayer> mplayers = new MassiveList<>();
for (String id : this.getInvitedPlayerIds())
{
MPlayer mplayer = MPlayer.get(id);
mplayers.add(mplayer);
}
return mplayers;
uninvite(playerId);
this.invitations.attach(invitation, playerId);
}
// -------------------------------------------- //

View File

@ -0,0 +1,47 @@
package com.massivecraft.factions.entity;
import com.massivecraft.massivecore.store.EntityInternal;
public class Invitation extends EntityInternal<Invitation>
{
// -------------------------------------------- //
// OVERRIDE: ENTITY
// -------------------------------------------- //
@Override
public Invitation load(Invitation that)
{
this.inviterId = that.inviterId;
this.creationMillis = that.creationMillis;
return this;
}
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
private String inviterId;
public String getInviterId() { return inviterId; }
public void setInviterId(String inviterId) { this.inviterId = inviterId; }
private Long creationMillis;
public Long getCreationMillis() { return creationMillis; }
public void setCreationMillis(Long creationMillis) { this.creationMillis = creationMillis; }
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public Invitation()
{
this(null, null);
}
public Invitation(String inviterId, Long creationMillis)
{
this.inviterId = inviterId;
this.creationMillis = creationMillis;
}
}

View File

@ -0,0 +1,69 @@
package com.massivecraft.factions.entity.migrator;
import com.massivecraft.factions.entity.Faction;
import com.massivecraft.massivecore.store.migrator.MigratorFieldConvert;
import com.massivecraft.massivecore.store.migrator.MigratorFieldRename;
import com.massivecraft.massivecore.store.migrator.MigratorRoot;
import com.massivecraft.massivecore.xlib.gson.JsonElement;
import com.massivecraft.massivecore.xlib.gson.JsonObject;
public class MigratorFaction001Invitations extends MigratorRoot
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static com.massivecraft.factions.entity.migrator.MigratorFaction001Invitations i = new com.massivecraft.factions.entity.migrator.MigratorFaction001Invitations();
public static com.massivecraft.factions.entity.migrator.MigratorFaction001Invitations get() { return i; }
private MigratorFaction001Invitations()
{
super(Faction.class);
this.addInnerMigrator(MigratorFieldRename.get("invitedPlayerIds", "invitations"));
this.addInnerMigrator(new MigratorFaction001InvitationsField());
}
public class MigratorFaction001InvitationsField extends MigratorFieldConvert
{
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
private MigratorFaction001InvitationsField()
{
super("invitations");
}
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
public Object migrateInner(JsonElement idList)
{
JsonObject ret = new JsonObject();
//EntityInternalMap<Invitation> ret = new EntityInternalMap<>(null, Invitation.class);
// If non-null
if (!idList.isJsonNull())
{
// ... and proper type ...
if (!idList.isJsonArray()) throw new IllegalArgumentException(idList.toString());
// ... fill!
for (JsonElement playerId : idList.getAsJsonArray())
{
String id = playerId.getAsString();
// Create invitation
JsonObject invitation = new JsonObject();
// Attach
ret.add(id, invitation);
}
}
return ret;
}
}
}