mirror of
https://github.com/CitizensDev/Citizens2.git
synced 2025-02-02 13:31:43 +01:00
Migrate glowing stuff to new scoreboard trait, add /npc scoreboard
This commit is contained in:
parent
f767c2bbd1
commit
673ee02542
@ -86,6 +86,7 @@ import net.citizensnpcs.trait.OcelotModifiers;
|
||||
import net.citizensnpcs.trait.Poses;
|
||||
import net.citizensnpcs.trait.Powered;
|
||||
import net.citizensnpcs.trait.RabbitType;
|
||||
import net.citizensnpcs.trait.ScoreboardTrait;
|
||||
import net.citizensnpcs.trait.ScriptTrait;
|
||||
import net.citizensnpcs.trait.SheepTrait;
|
||||
import net.citizensnpcs.trait.SkinLayers;
|
||||
@ -586,11 +587,7 @@ public class NPCCommands {
|
||||
ChatColor chatColor = Util.matchEnum(ChatColor.values(), args.getFlag("color"));
|
||||
if (!(npc.getEntity() instanceof Player))
|
||||
throw new CommandException();
|
||||
if (chatColor == null) {
|
||||
npc.data().remove(NPC.GLOWING_COLOR_METADATA);
|
||||
} else {
|
||||
npc.data().setPersistent(NPC.GLOWING_COLOR_METADATA, chatColor.name());
|
||||
}
|
||||
npc.getTrait(ScoreboardTrait.class).setColor(chatColor);
|
||||
Messaging.sendTr(sender, Messages.GLOWING_COLOR_SET, npc.getName(),
|
||||
chatColor == null ? ChatColor.WHITE + "white" : chatColor + Util.prettyEnum(chatColor));
|
||||
return;
|
||||
@ -1453,6 +1450,34 @@ public class NPCCommands {
|
||||
}
|
||||
}
|
||||
|
||||
@Command(
|
||||
aliases = { "npc" },
|
||||
usage = "scoreboard --addtag [tags] --removetag [tags]",
|
||||
desc = "Controls an NPC's scoreboard",
|
||||
modifiers = { "scoreboard" },
|
||||
min = 1,
|
||||
max = 1,
|
||||
permission = "citizens.npc.scoreboard")
|
||||
public void scoreboard(CommandContext args, CommandSender sender, NPC npc) {
|
||||
ScoreboardTrait trait = npc.getTrait(ScoreboardTrait.class);
|
||||
String output = "";
|
||||
if (args.hasValueFlag("addtag")) {
|
||||
for (String tag : args.getFlag("addtag").split(",")) {
|
||||
trait.addTag(tag);
|
||||
}
|
||||
output += " " + Messaging.tr(Messages.ADDED_SCOREBOARD_TAGS, args.getFlag("addtag"));
|
||||
}
|
||||
if (args.hasValueFlag("removetag")) {
|
||||
for (String tag : args.getFlag("removetag").split(",")) {
|
||||
trait.removeTag(tag);
|
||||
}
|
||||
output += " " + Messaging.tr(Messages.REMOVED_SCOREBOARD_TAGS, args.getFlag("removetag"));
|
||||
}
|
||||
if (!output.isEmpty()) {
|
||||
Messaging.send(sender, output.trim());
|
||||
}
|
||||
}
|
||||
|
||||
@Command(
|
||||
aliases = { "npc" },
|
||||
usage = "script --add [files] --remove [files]",
|
||||
|
@ -5,7 +5,6 @@ import java.util.Collection;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.entity.Entity;
|
||||
@ -15,8 +14,6 @@ import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.entity.CreatureSpawnEvent;
|
||||
import org.bukkit.metadata.FixedMetadataValue;
|
||||
import org.bukkit.scoreboard.Team;
|
||||
import org.bukkit.scoreboard.Team.Option;
|
||||
import org.bukkit.scoreboard.Team.OptionStatus;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.google.common.base.Throwables;
|
||||
@ -44,6 +41,7 @@ import net.citizensnpcs.api.util.Messaging;
|
||||
import net.citizensnpcs.npc.ai.CitizensNavigator;
|
||||
import net.citizensnpcs.npc.skin.SkinnableEntity;
|
||||
import net.citizensnpcs.trait.CurrentLocation;
|
||||
import net.citizensnpcs.trait.ScoreboardTrait;
|
||||
import net.citizensnpcs.util.ChunkCoord;
|
||||
import net.citizensnpcs.util.Messages;
|
||||
import net.citizensnpcs.util.NMS;
|
||||
@ -392,34 +390,7 @@ public class CitizensNPC extends AbstractNPC {
|
||||
return;
|
||||
}
|
||||
|
||||
if (SUPPORT_TEAM_SETOPTION) {
|
||||
try {
|
||||
team.setOption(Option.NAME_TAG_VISIBILITY, nameVisibility ? OptionStatus.ALWAYS : OptionStatus.NEVER);
|
||||
} catch (NoSuchMethodError e) {
|
||||
SUPPORT_TEAM_SETOPTION = false;
|
||||
} catch (NoClassDefFoundError e) {
|
||||
SUPPORT_TEAM_SETOPTION = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (data().has(NPC.GLOWING_COLOR_METADATA)) {
|
||||
if (SUPPORT_GLOWING_COLOR) {
|
||||
try {
|
||||
if (team.getColor() == null || (data().has("previous-glowing-color")
|
||||
&& !team.getColor().name().equals(data().get("previous-glowing-color")))) {
|
||||
team.setColor(ChatColor.valueOf(data().<String> get(NPC.GLOWING_COLOR_METADATA)));
|
||||
}
|
||||
} catch (NoSuchMethodError err) {
|
||||
SUPPORT_GLOWING_COLOR = false;
|
||||
}
|
||||
} else {
|
||||
if (team.getPrefix() == null || team.getPrefix().length() == 0 || (data().has("previous-glowing-color")
|
||||
&& !team.getPrefix().equals(data().get("previous-glowing-color")))) {
|
||||
team.setPrefix(ChatColor.valueOf(data().<String> get(NPC.GLOWING_COLOR_METADATA)).toString());
|
||||
data().set("previous-glowing-color", team.getPrefix());
|
||||
}
|
||||
}
|
||||
}
|
||||
getTrait(ScoreboardTrait.class).apply(team, nameVisibility);
|
||||
}
|
||||
|
||||
private void updateFlyableState() {
|
||||
|
@ -35,6 +35,7 @@ import net.citizensnpcs.trait.Poses;
|
||||
import net.citizensnpcs.trait.Powered;
|
||||
import net.citizensnpcs.trait.RabbitType;
|
||||
import net.citizensnpcs.trait.Saddle;
|
||||
import net.citizensnpcs.trait.ScoreboardTrait;
|
||||
import net.citizensnpcs.trait.ScriptTrait;
|
||||
import net.citizensnpcs.trait.SheepTrait;
|
||||
import net.citizensnpcs.trait.SkinLayers;
|
||||
@ -69,6 +70,7 @@ public class CitizensTraitFactory implements TraitFactory {
|
||||
registerTrait(TraitInfo.create(Powered.class));
|
||||
registerTrait(TraitInfo.create(RabbitType.class));
|
||||
registerTrait(TraitInfo.create(Saddle.class));
|
||||
registerTrait(TraitInfo.create(ScoreboardTrait.class));
|
||||
registerTrait(TraitInfo.create(ScriptTrait.class));
|
||||
registerTrait(TraitInfo.create(SheepTrait.class));
|
||||
registerTrait(TraitInfo.create(SkinLayers.class));
|
||||
|
@ -0,0 +1,86 @@
|
||||
package net.citizensnpcs.trait;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.scoreboard.Team;
|
||||
import org.bukkit.scoreboard.Team.Option;
|
||||
import org.bukkit.scoreboard.Team.OptionStatus;
|
||||
|
||||
import net.citizensnpcs.api.npc.NPC;
|
||||
import net.citizensnpcs.api.persistence.Persist;
|
||||
import net.citizensnpcs.api.trait.Trait;
|
||||
import net.citizensnpcs.api.trait.TraitName;
|
||||
|
||||
@TraitName("scoreboardtrait")
|
||||
public class ScoreboardTrait extends Trait {
|
||||
@Persist
|
||||
private ChatColor color;
|
||||
private ChatColor previousGlowingColor;
|
||||
@Persist
|
||||
private final Set<String> tags = new HashSet<String>();
|
||||
|
||||
public ScoreboardTrait() {
|
||||
super("scoreboardtrait");
|
||||
}
|
||||
|
||||
public void addTag(String tag) {
|
||||
tags.add(tag);
|
||||
}
|
||||
|
||||
public void apply(Team team, boolean nameVisibility) {
|
||||
Set<String> newTags = new HashSet<String>(tags);
|
||||
for (String oldTag : team.getEntries()) {
|
||||
if (!newTags.remove(oldTag)) {
|
||||
team.removeEntry(oldTag);
|
||||
}
|
||||
}
|
||||
for (String tag : newTags) {
|
||||
team.addEntry(tag);
|
||||
}
|
||||
|
||||
if (SUPPORT_TEAM_SETOPTION) {
|
||||
try {
|
||||
team.setOption(Option.NAME_TAG_VISIBILITY, nameVisibility ? OptionStatus.ALWAYS : OptionStatus.NEVER);
|
||||
} catch (NoSuchMethodError e) {
|
||||
SUPPORT_TEAM_SETOPTION = false;
|
||||
} catch (NoClassDefFoundError e) {
|
||||
SUPPORT_TEAM_SETOPTION = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (npc.data().has(NPC.GLOWING_COLOR_METADATA)) {
|
||||
color = ChatColor.valueOf(npc.data().get(NPC.GLOWING_COLOR_METADATA));
|
||||
npc.data().remove(NPC.GLOWING_COLOR_METADATA);
|
||||
}
|
||||
if (SUPPORT_GLOWING_COLOR && color != null) {
|
||||
try {
|
||||
if (team.getColor() == null || previousGlowingColor == null
|
||||
|| (previousGlowingColor != null && color != previousGlowingColor)) {
|
||||
team.setColor(color);
|
||||
previousGlowingColor = color;
|
||||
}
|
||||
} catch (NoSuchMethodError err) {
|
||||
SUPPORT_GLOWING_COLOR = false;
|
||||
}
|
||||
} else {
|
||||
if (team.getPrefix() == null || team.getPrefix().length() == 0 || previousGlowingColor == null
|
||||
|| (previousGlowingColor != null && !team.getPrefix().equals(previousGlowingColor.toString()))) {
|
||||
team.setPrefix(color.toString());
|
||||
previousGlowingColor = color;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void removeTag(String tag) {
|
||||
tags.remove(tag);
|
||||
}
|
||||
|
||||
public void setColor(ChatColor color) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
private static boolean SUPPORT_GLOWING_COLOR = true;
|
||||
private static boolean SUPPORT_TEAM_SETOPTION = true;
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package net.citizensnpcs.util;
|
||||
|
||||
public class Messages {
|
||||
public static final String ADDED_SCOREBOARD_TAGS = "citizens.commands.npc.scoreboard.added-tags";
|
||||
public static final String ADDED_TO_PLAYERLIST = "citizens.commands.npc.playerlist.added";
|
||||
public static final String AGE_LOCKED = "citizens.commands.npc.age.locked";
|
||||
public static final String AGE_SET_ADULT = "citizens.commands.npc.age.set-adult";
|
||||
@ -231,6 +232,7 @@ public class Messages {
|
||||
public static final String REMOVE_INCORRECT_SYNTAX = "citizens.commands.npc.remove.incorrect-syntax";
|
||||
public static final String REMOVED_ALL_NPCS = "citizens.commands.npc.remove.removed-all";
|
||||
public static final String REMOVED_FROM_PLAYERLIST = "citizens.commands.npc.playerlist.removed";
|
||||
public static final String REMOVED_SCOREBOARD_TAGS = "citizens.commands.npc.scoreboard.removed-tags";
|
||||
public static final String RESPAWN_DELAY_DESCRIBE = "citizens.commands.npc.respawn.describe";
|
||||
public static final String RESPAWN_DELAY_SET = "citizens.commands.npc.respawn.delay-set";
|
||||
public static final String SADDLED_SET = "citizens.editors.equipment.saddled-set";
|
||||
|
@ -145,6 +145,8 @@ citizens.commands.npc.respawn.delay-set=Respawn delay set to [[{0}]].
|
||||
citizens.commands.npc.respawn.describe=Respawn delay is currently [[{0}]].
|
||||
citizens.commands.npc.select.already-selected=You already have that NPC selected.
|
||||
citizens.commands.npc.script.invalid-file=Unknown or unavailable script ''[[{0}]]''.
|
||||
citizens.commands.npc.scoreboard.added-tags=Added these tags: [[{0}]].
|
||||
citizens.commands.npc.scoreboard.removed-tags=Removed these tags: [[{0}]].
|
||||
citizens.commands.npc.sheep.color-set=The sheep''s color was set to [[{0}]].
|
||||
citizens.commands.npc.sheep.invalid-color=Invalid sheep color given. Valid colors are: [[{0}]].
|
||||
citizens.commands.npc.script.current-scripts=[[{0}]]''s current scripts are [[{1}]].
|
||||
|
Loading…
Reference in New Issue
Block a user