Allow naming of chunks

This commit is contained in:
Magnus Ulf 2021-02-21 14:02:59 +01:00
parent bc01275933
commit bc62e17068
7 changed files with 159 additions and 26 deletions

View File

@ -26,6 +26,7 @@ permissions:
factions.access.view: {description: view access, default: false}
factions.override: {description: enable override mode, default: false}
factions.basecommand: {description: use factions base command, default: false}
factions.chunkname: {description: set chunk name, default: false}
factions.claim: {description: claim faction territory, default: false}
factions.claim.one: {description: claim a single chunk, default: false}
factions.claim.auto: {description: claim as you walk around, default: false}
@ -163,6 +164,7 @@ permissions:
factions.access.view: true
factions.override: true
factions.basecommand: true
factions.chunkname: true
factions.claim: true
factions.claim.one: true
factions.claim.auto: true
@ -344,6 +346,7 @@ permissions:
factions.access.inspect: true
factions.access.view: true
factions.basecommand: true
factions.chunkname: true
factions.claim: true
factions.claim.one: true
factions.claim.auto: true

View File

@ -29,6 +29,10 @@ public class TerritoryAccess
private final Set<String> grantedIds;
public Set<String> getGrantedIds() { return this.grantedIds; }
// default is null
private final String chunkName;
public String getChunkName() { return this.chunkName; }
// -------------------------------------------- //
// FIELDS: VERSION
// -------------------------------------------- //
@ -40,9 +44,10 @@ public class TerritoryAccess
// -------------------------------------------- //
// The simple ones
public TerritoryAccess withHostFactionId(String hostFactionId) { return valueOf(hostFactionId, hostFactionAllowed, grantedIds); }
public TerritoryAccess withHostFactionAllowed(Boolean hostFactionAllowed) { return valueOf(hostFactionId, hostFactionAllowed, grantedIds); }
public TerritoryAccess withGrantedIds(Collection<String> grantedIds) { return valueOf(hostFactionId, hostFactionAllowed, grantedIds); }
public TerritoryAccess withHostFactionId(String hostFactionId) { return valueOf(hostFactionId, hostFactionAllowed, grantedIds, chunkName); }
public TerritoryAccess withHostFactionAllowed(Boolean hostFactionAllowed) { return valueOf(hostFactionId, hostFactionAllowed, grantedIds, chunkName); }
public TerritoryAccess withGrantedIds(Collection<String> grantedIds) { return valueOf(hostFactionId, hostFactionAllowed, grantedIds, chunkName); }
public TerritoryAccess withChunkName(String chunkName) { return valueOf(hostFactionId, hostFactionAllowed, grantedIds, chunkName); }
// The intermediate ones
public TerritoryAccess withGranted(MPermable mpermable, boolean with)
@ -54,7 +59,7 @@ public class TerritoryAccess
{
if (this.getHostFactionId().equals(grantedId))
{
return valueOf(hostFactionId, with, grantedIds);
return valueOf(hostFactionId, with, grantedIds, chunkName);
}
Set<String> grantedIds = new MassiveSet<>(this.getGrantedIds());
@ -66,7 +71,7 @@ public class TerritoryAccess
{
grantedIds.remove(grantedId);
}
return valueOf(hostFactionId, hostFactionAllowed, grantedIds);
return valueOf(hostFactionId, hostFactionAllowed, grantedIds, chunkName);
}
// -------------------------------------------- //
@ -95,9 +100,10 @@ public class TerritoryAccess
this.hostFactionId = null;
this.hostFactionAllowed = true;
this.grantedIds = null;
this.chunkName = null;
}
private TerritoryAccess(String hostFactionId, Boolean hostFactionAllowed, Collection<String> grantedIds)
private TerritoryAccess(String hostFactionId, Boolean hostFactionAllowed, Collection<String> grantedIds, String chunkName)
{
if (hostFactionId == null) throw new NullPointerException("hostFactionId");
if (grantedIds == null) throw new NullPointerException("grantedIds");
@ -112,23 +118,25 @@ public class TerritoryAccess
this.grantedIds = Collections.unmodifiableSet(grantedIdsInner);
this.hostFactionAllowed = (hostFactionAllowed == null || hostFactionAllowed);
this.chunkName = chunkName;
}
// -------------------------------------------- //
// FACTORY: VALUE OF
// -------------------------------------------- //
public static TerritoryAccess valueOf(String hostFactionId, Boolean hostFactionAllowed, Collection<String> grantedIds)
public static TerritoryAccess valueOf(String hostFactionId, Boolean hostFactionAllowed, Collection<String> grantedIds, String chunkName)
{
if (hostFactionId == null) throw new NullPointerException("hostFactionId");
if (grantedIds == null) throw new NullPointerException("grantedIds");
return new TerritoryAccess(hostFactionId, hostFactionAllowed, grantedIds);
return new TerritoryAccess(hostFactionId, hostFactionAllowed, grantedIds, chunkName);
}
public static TerritoryAccess valueOf(String hostFactionId)
{
if (hostFactionId == null) throw new NullPointerException("hostFactionId");
return valueOf(hostFactionId, null, Collections.emptySet());
return valueOf(hostFactionId, null, Collections.emptySet(), null);
}
// -------------------------------------------- //
@ -150,7 +158,7 @@ public class TerritoryAccess
// The host faction is still allowed (default) and no faction or player has been granted explicit access (default).
public boolean isDefault()
{
return this.isHostFactionAllowed() && this.getGrantedIds().isEmpty();
return this.isHostFactionAllowed() && this.getGrantedIds().isEmpty() && this.getChunkName() == null;
}
// -------------------------------------------- //

View File

@ -26,6 +26,7 @@ public class TerritoryAccessAdapter implements JsonDeserializer<TerritoryAccess>
public static final String HOST_FACTION_ID = "hostFactionId";
public static final String HOST_FACTION_ALLOWED = "hostFactionAllowed";
public static final String GRANTED_IDS = "grantedIds";
public static final String CHUNK_NAME = "chunkName";
public static final Type SET_OF_STRING_TYPE = new TypeToken<Set<String>>(){}.getType();
@ -57,6 +58,7 @@ public class TerritoryAccessAdapter implements JsonDeserializer<TerritoryAccess>
String hostFactionId = null;
Boolean hostFactionAllowed = null;
Set<String> grantedIds = Collections.emptySet();
String chunkName = null;
// Read variables (test old values first)
JsonElement element = null;
@ -70,7 +72,10 @@ public class TerritoryAccessAdapter implements JsonDeserializer<TerritoryAccess>
element = obj.get(GRANTED_IDS);
if (element != null) grantedIds = context.deserialize(element, SET_OF_STRING_TYPE);
return TerritoryAccess.valueOf(hostFactionId, hostFactionAllowed, grantedIds);
element = obj.get(CHUNK_NAME);
if (element != null) chunkName = element.getAsString();
return TerritoryAccess.valueOf(hostFactionId, hostFactionAllowed, grantedIds, chunkName);
}
@Override
@ -99,6 +104,11 @@ public class TerritoryAccessAdapter implements JsonDeserializer<TerritoryAccess>
obj.add(GRANTED_IDS, context.serialize(src.getGrantedIds(), SET_OF_STRING_TYPE));
}
if (src.getChunkName() != null)
{
obj.addProperty(CHUNK_NAME, src.getChunkName());
}
obj.add(MigratorUtil.VERSION_FIELD_NAME, new JsonPrimitive(src.version));
return obj;

View File

@ -46,6 +46,7 @@ public class CmdFactions extends FactionsCommand
public CmdFactionsClaim cmdFactionsClaim = new CmdFactionsClaim();
public CmdFactionsUnclaim cmdFactionsUnclaim = new CmdFactionsUnclaim();
public CmdFactionsAccess cmdFactionsAccess = new CmdFactionsAccess();
public CmdFactionsChunkname cmdFactionsChunkname = new CmdFactionsChunkname();
public CmdFactionsRelation cmdFactionsRelation = new CmdFactionsRelation();
public CmdFactionsRelationOld cmdFactionsRelationOldAlly = new CmdFactionsRelationOld("ally");
public CmdFactionsRelationOld cmdFactionsRelationOldTruce = new CmdFactionsRelationOld("truce");

View File

@ -8,7 +8,6 @@ import com.massivecraft.factions.entity.MPerm.MPermable;
import com.massivecraft.factions.util.AsciiMap;
import com.massivecraft.massivecore.command.requirement.RequirementIsPlayer;
import com.massivecraft.massivecore.ps.PS;
import com.massivecraft.massivecore.ps.PSFormatHumanSpace;
import com.massivecraft.massivecore.util.Txt;
import java.util.Collection;
@ -59,7 +58,7 @@ public abstract class CmdFactionsAccessAbstract extends FactionsCommand
public void sendAccessInfo()
{
String chunkDesc = AsciiMap.showChunkCoords(chunk) ? " at " + chunk.toString(PSFormatHumanSpace.get()) : "";
String chunkDesc = AsciiMap.getChunkDescWithName(chunk, ta);
Object title = "Access" + chunkDesc;
title = Txt.titleize(title);
message(title);
@ -80,7 +79,7 @@ public abstract class CmdFactionsAccessAbstract extends FactionsCommand
Faction faction = ta.getHostFaction();
String chunkDesc = AsciiMap.showChunkCoords(chunk) ? " at " + chunk.toString(PSFormatHumanSpace.get()) : "";
String chunkDesc = AsciiMap.getChunkDescWithName(chunk, ta);
String grantedDenied = granted ? "granted" : "denied";
String mpermableDesc = mpermable.getDisplayName(msender);

View File

@ -0,0 +1,81 @@
package com.massivecraft.factions.cmd;
import com.massivecraft.factions.TerritoryAccess;
import com.massivecraft.factions.entity.BoardColl;
import com.massivecraft.factions.entity.MPerm;
import com.massivecraft.factions.util.AsciiMap;
import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.command.requirement.RequirementIsPlayer;
import com.massivecraft.massivecore.command.type.TypeNullable;
import com.massivecraft.massivecore.command.type.primitive.TypeString;
import com.massivecraft.massivecore.ps.PS;
import com.massivecraft.massivecore.util.MUtil;
import com.massivecraft.massivecore.util.Txt;
public class CmdFactionsChunkname extends FactionsCommand
{
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public CmdFactionsChunkname()
{
// Parameters
this.addParameter(TypeNullable.get(TypeString.get()), "name", "read");
this.addRequirements(RequirementIsPlayer.get());
}
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public void perform() throws MassiveException
{
PS chunk = PS.valueOf(me.getLocation()).getChunk(true);
TerritoryAccess ta = BoardColl.get().getTerritoryAccessAt(chunk);
if (!this.argIsSet(0))
{
String name = ta.getChunkName();
if (name == null)
{
msg("<i>This chunk has no name.");
}
else
{
msg("<i>This chunk is called <h>%s<i>.", name);
}
return;
}
// MPerm
if (!MPerm.getPermTerritory().has(msender, msenderFaction, true)) return;
// Args
String target = this.readArg();
target = target.trim();
target = Txt.parse(target);
String old = ta.getChunkName();
// NoChange
if (MUtil.equals(old, target))
{
if (old == null)
{
throw new MassiveException().addMsg("<b>This chunk already has no name.");
}
throw new MassiveException().addMsg("<b>The name for this chunk is already <h>%s<b>.", old);
}
ta = ta.withChunkName(target);
BoardColl.get().setTerritoryAccessAt(chunk, ta);
String chunkDesc = AsciiMap.getChunkDesc(chunk);
msg("<i>The chunk name%s<i> is now %s.", chunkDesc, target);
}
}

View File

@ -1,12 +1,14 @@
package com.massivecraft.factions.util;
import com.massivecraft.factions.RelationParticipator;
import com.massivecraft.factions.TerritoryAccess;
import com.massivecraft.factions.entity.Board;
import com.massivecraft.factions.entity.BoardColl;
import com.massivecraft.factions.entity.Faction;
import com.massivecraft.massivecore.collections.MassiveList;
import com.massivecraft.massivecore.mson.Mson;
import com.massivecraft.massivecore.ps.PS;
import com.massivecraft.massivecore.ps.PSFormatHumanSpace;
import com.massivecraft.massivecore.util.Txt;
import org.bukkit.ChatColor;
import org.bukkit.GameRule;
@ -164,7 +166,7 @@ public class AsciiMap
for (int deltaX = startX; deltaX < WIDTH; deltaX++)
{
boolean isMiddle = deltaX == WIDTH_HALF && deltaZ == this.getHeightHalf();
factionChar = isMiddle ? KEY_MIDDLE : this.getCharFaction(deltaZ, deltaX);
factionChar = isMiddle ? KEY_MIDDLE : this.getCharChunk(deltaZ, deltaX);
ret = ret.add(factionChar);
}
@ -172,23 +174,34 @@ public class AsciiMap
return ret;
}
private Mson getCharFaction(int deltaZ, int deltaX)
private Mson getCharChunk(int deltaZ, int deltaX)
{
PS herePs = this.getTopLeft().plusChunkCoords(deltaX, deltaZ);
Faction hereFaction = this.getBoard().getFactionAt(herePs);
String chunkName = this.getBoard().getTerritoryAccessAt(herePs).getChunkName();
Mson charFaction = getCharFaction(hereFaction);
String tooltip = charFaction.getTooltip();
if (chunkName != null) tooltip += "\n" + ChatColor.WHITE + chunkName;
Mson charChunk = charFaction.tooltip(tooltip);
return charChunk;
}
private Mson getCharFaction(Faction faction)
{
// Calculate overflow
int index = this.getFactionChars().size();
if (!this.isOverflown() && index >= KEY_SIZE) this.setOverflown(true);
PS herePs = this.getTopLeft().plusChunkCoords(deltaX, deltaZ);
Faction hereFaction = this.getBoard().getFactionAt(herePs);
Mson factionChar = this.getFactionChars().get(hereFaction);
Mson factionChar = this.getFactionChars().get(faction);
// Is Wilderness or known?
if (hereFaction.isNone()) return KEY_WILDERNESS;
if (faction.isNone()) return KEY_WILDERNESS;
if (factionChar != null) return factionChar;
// Create descriptions
ChatColor color = hereFaction.getColorTo(this.getRelationParticipator());
String name = hereFaction.getName(this.getRelationParticipator());
ChatColor color = faction.getColorTo(this.getRelationParticipator());
String name = faction.getName(this.getRelationParticipator());
String tooltip = color.toString() + name;
// Is overflown?
@ -199,7 +212,7 @@ public class AsciiMap
factionChar = factionChar.tooltip(tooltip);
// Store for later use
this.getFactionChars().put(hereFaction, factionChar);
this.getFactionChars().put(faction, factionChar);
// Return
return factionChar;
@ -237,4 +250,22 @@ public class AsciiMap
return ! w.getGameRuleValue(GameRule.REDUCED_DEBUG_INFO);
}
public static String getChunkDesc(PS chunk)
{
return showChunkCoords(chunk) ? " at " + chunk.toString(PSFormatHumanSpace.get()) : "";
}
public static String getChunkDescWithName(PS chunk, TerritoryAccess ta)
{
String name = ta.getChunkName();
if (name == null) return getChunkDesc(chunk);
String ret = Txt.parse(" at <h>%s", name);
if (showChunkCoords(chunk))
{
ret += Txt.parse(" <i>(%s<i>)", chunk.toString(PSFormatHumanSpace.get()));
}
return ret;
}
}