From bc62e1706881be6717a7984db91119994459806f Mon Sep 17 00:00:00 2001 From: Magnus Ulf Date: Sun, 21 Feb 2021 14:02:59 +0100 Subject: [PATCH] Allow naming of chunks --- plugin.yml | 3 + .../factions/TerritoryAccess.java | 28 ++++--- .../adapter/TerritoryAccessAdapter.java | 14 +++- .../factions/cmd/CmdFactions.java | 1 + .../cmd/CmdFactionsAccessAbstract.java | 5 +- .../factions/cmd/CmdFactionsChunkname.java | 81 +++++++++++++++++++ .../massivecraft/factions/util/AsciiMap.java | 53 +++++++++--- 7 files changed, 159 insertions(+), 26 deletions(-) create mode 100644 src/com/massivecraft/factions/cmd/CmdFactionsChunkname.java diff --git a/plugin.yml b/plugin.yml index e64af984..93553fb1 100644 --- a/plugin.yml +++ b/plugin.yml @@ -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 diff --git a/src/com/massivecraft/factions/TerritoryAccess.java b/src/com/massivecraft/factions/TerritoryAccess.java index 7a9ec9da..8fc7857f 100644 --- a/src/com/massivecraft/factions/TerritoryAccess.java +++ b/src/com/massivecraft/factions/TerritoryAccess.java @@ -29,6 +29,10 @@ public class TerritoryAccess private final Set grantedIds; public Set 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 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 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 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 grantedIds) + private TerritoryAccess(String hostFactionId, Boolean hostFactionAllowed, Collection 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 grantedIds) + public static TerritoryAccess valueOf(String hostFactionId, Boolean hostFactionAllowed, Collection 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; } // -------------------------------------------- // diff --git a/src/com/massivecraft/factions/adapter/TerritoryAccessAdapter.java b/src/com/massivecraft/factions/adapter/TerritoryAccessAdapter.java index 623a9e20..8e0e8378 100644 --- a/src/com/massivecraft/factions/adapter/TerritoryAccessAdapter.java +++ b/src/com/massivecraft/factions/adapter/TerritoryAccessAdapter.java @@ -26,6 +26,7 @@ public class TerritoryAccessAdapter implements JsonDeserializer 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>(){}.getType(); @@ -57,7 +58,8 @@ public class TerritoryAccessAdapter implements JsonDeserializer String hostFactionId = null; Boolean hostFactionAllowed = null; Set grantedIds = Collections.emptySet(); - + String chunkName = null; + // Read variables (test old values first) JsonElement element = null; @@ -69,8 +71,11 @@ public class TerritoryAccessAdapter implements JsonDeserializer element = obj.get(GRANTED_IDS); if (element != null) grantedIds = context.deserialize(element, SET_OF_STRING_TYPE); + + element = obj.get(CHUNK_NAME); + if (element != null) chunkName = element.getAsString(); - return TerritoryAccess.valueOf(hostFactionId, hostFactionAllowed, grantedIds); + return TerritoryAccess.valueOf(hostFactionId, hostFactionAllowed, grantedIds, chunkName); } @Override @@ -99,6 +104,11 @@ public class TerritoryAccessAdapter implements JsonDeserializer 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; diff --git a/src/com/massivecraft/factions/cmd/CmdFactions.java b/src/com/massivecraft/factions/cmd/CmdFactions.java index 94eb4bbb..8e39577a 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactions.java +++ b/src/com/massivecraft/factions/cmd/CmdFactions.java @@ -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"); diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsAccessAbstract.java b/src/com/massivecraft/factions/cmd/CmdFactionsAccessAbstract.java index a2a603bd..b116bb58 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactionsAccessAbstract.java +++ b/src/com/massivecraft/factions/cmd/CmdFactionsAccessAbstract.java @@ -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); diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsChunkname.java b/src/com/massivecraft/factions/cmd/CmdFactionsChunkname.java new file mode 100644 index 00000000..c863b20d --- /dev/null +++ b/src/com/massivecraft/factions/cmd/CmdFactionsChunkname.java @@ -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("This chunk has no name."); + } + else + { + msg("This chunk is called %s.", 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("This chunk already has no name."); + } + throw new MassiveException().addMsg("The name for this chunk is already %s.", old); + } + + ta = ta.withChunkName(target); + BoardColl.get().setTerritoryAccessAt(chunk, ta); + + String chunkDesc = AsciiMap.getChunkDesc(chunk); + msg("The chunk name%s is now %s.", chunkDesc, target); + } + +} diff --git a/src/com/massivecraft/factions/util/AsciiMap.java b/src/com/massivecraft/factions/util/AsciiMap.java index ba62ccac..f98f9011 100644 --- a/src/com/massivecraft/factions/util/AsciiMap.java +++ b/src/com/massivecraft/factions/util/AsciiMap.java @@ -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; @@ -112,7 +114,7 @@ public class AsciiMap ret.add(this.getTitle()); ret.addAll(this.getLines()); ret.add(this.getFactionLegend()); - + // Return return ret; } @@ -164,31 +166,42 @@ 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); } // Return return ret; } + + 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(int deltaZ, int deltaX) + 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 %s", name); + if (showChunkCoords(chunk)) + { + ret += Txt.parse(" (%s)", chunk.toString(PSFormatHumanSpace.get())); + } + return ret; + } + }