Skip to content

Commit

Permalink
Add 'set' to nameplate command. Will set the 'custom name' of a mob-t…
Browse files Browse the repository at this point in the history
…ype NPC. Player-type NPCs are currently unaffected!
  • Loading branch information
aufdemrand committed Mar 28, 2013
1 parent 1a2b739 commit 3112dc5
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 77 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -13,7 +13,7 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<craftbukkit.version>1.5.1-R0.1-SNAPSHOT</craftbukkit.version>
<craftbukkit.version>LATEST</craftbukkit.version>
<json.version>20090211</json.version>
<citizens.version>2.0.8-SNAPSHOT</citizens.version>
<build.number>Unknown</build.number>
Expand Down
74 changes: 50 additions & 24 deletions src/main/java/net/aufdemrand/denizen/npc/traits/NameplateTrait.java
Expand Up @@ -5,9 +5,12 @@
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.depends.Depends;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.util.DataKey;

Expand All @@ -34,20 +37,42 @@
*/
public class NameplateTrait extends Trait implements Listener {
private final static String DEFAULT_KEY = "_default_";

private Map<String, ChatColor> colors = new HashMap<String, ChatColor>();

@Persist("text")
private String text = null;

@Persist(value="colors", collectionType=ConcurrentHashMap.class)
private Map<String, ChatColor> colors = new ConcurrentHashMap<String, ChatColor>();

public NameplateTrait() {
super("nameplate");
}

@Override
public void onSpawn() {

if(text != null) {
Bukkit.getScheduler().scheduleSyncDelayedTask(DenizenAPI.getCurrentInstance(),
new Runnable() {
@Override
public void run() {
npc.getBukkitEntity().setCustomNameVisible(true);
npc.getBukkitEntity().setCustomName(text);
}
}, 2);
}

if(getColor() != null ) {
refreshTag( getNPC() );
}
}

@Override
public void onDespawn() {
if (npc.getBukkitEntity().getCustomName() != null)
text = npc.getBukkitEntity().getCustomName();
}

public void setColor(ChatColor color) {
this.setColor(color, DEFAULT_KEY);
}
Expand All @@ -70,7 +95,7 @@ public ChatColor getColor() {
* Returns the {@link ChatColor} prefixed to the nameplate for a specific
* player.
*
* @param The player name
* @param player The player name
* @return The stored {@link ChatColor} for the specific player
*/
public ChatColor getColor(String player) {
Expand All @@ -91,7 +116,7 @@ public boolean hasColor() {
/**
* Returns true if a color has been set for this player
*
* @param The player name to check
* @param player The player name to check
* @return True if set, otherwise false
*/
public boolean hasColor(String player) {
Expand All @@ -111,7 +136,7 @@ public String getTrimmedTag() {
* Retrieve the trimmed nameplate including the set color (max. 16 chars) for
* the specific player.
*
* @param The player name
* @param player The player name
* @return The trimmed nameplate including color
*/
public String getTrimmedTag(String player) {
Expand Down Expand Up @@ -165,24 +190,25 @@ private List<Player> getPlayersInRadius( NPC npc, int distance ) {
return players;
}

@Override
public void load(DataKey key) {
for(DataKey k : key.getSubKeys()) {
ChatColor c = null;

try {
c = ChatColor.valueOf(key.getString(k.name()));
} catch( Exception e) {}

colors.put(k.name(), c );
}
}

@Override
public void save(DataKey key) {
for(Entry<String, ChatColor> entry: colors.entrySet()) {
key.setString(entry.getKey(), entry.getValue().name());
}
}
// @Override
// public void load(DataKey key) {
// for(DataKey k : key.getSubKeys()) {
// ChatColor c = null;
//
// try {
// c = ChatColor.valueOf(key.getString(k.name()));
// } catch( Exception e) {}
//
// colors.put(k.name(), c );
// }
// }
//
// @Override
// public void save(DataKey key) {
// for(Entry<String, ChatColor> entry: colors.entrySet()) {
// key.setString(entry.getKey(), entry.getValue().name());
// }
// }


}
Expand Up @@ -146,7 +146,7 @@ public void registerCoreMembers() {
"MODIFYBLOCK", "modifyblock [location:x,y,z,world] [material:data] (radius:#) (height:#) (depth:#)", 2);

registerCoreMember(NameplateCommand.class,
"NAMEPLATE", "nameplate [chat_color] (target:player_name)", 1);
"NAMEPLATE", "nameplate [set:text|chat_color] (target:player_name)", 1);

registerCoreMember(NarrateCommand.class,
"NARRATE", "narrate [\"narration text\"] (player:name) (format:format)", 1);
Expand Down
Expand Up @@ -8,12 +8,15 @@
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.exceptions.ScriptEntryCreationException;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.ScriptRegistry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.scripts.containers.core.TaskScriptContainer;
import net.aufdemrand.denizen.utilities.arguments.aH;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.citizensnpcs.api.CitizensAPI;

import org.bukkit.ChatColor;
import org.bukkit.entity.Player;

/**
* Core dScript IF command.
Expand Down Expand Up @@ -495,6 +498,8 @@ private void doElse(ScriptEntry scriptEntry) {
}
else dB.echoDebug("Use '/denizen debug -s' for the nitty-gritty.");
}

}


}
Expand Up @@ -15,10 +15,11 @@
import net.aufdemrand.denizen.utilities.arguments.aH.ArgumentType;
import net.aufdemrand.denizen.utilities.debugging.dB;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;

/**
* Modifies the nameplate of the given NPC
*
*
* @author spaceemotion
*/
public class NameplateCommand extends AbstractCommand {
Expand All @@ -34,54 +35,68 @@ public class NameplateCommand extends AbstractCommand {
* (PLAYER) The player to apply the change to (can be per-player!).
*
*/

@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
dNPC npc = scriptEntry.getNPC();

if(npc.getCitizen().hasTrait(NameplateTrait.class)) {
ChatColor color = null;
String playerName = null;

List<String> args = scriptEntry.getArguments();

for(String arg : args) {
if(aH.matchesValueArg("COLOR", arg, ArgumentType.String)) {
String cString = aH.getStringFrom(arg).toUpperCase();

try {
color = ChatColor.valueOf(cString.toUpperCase());
dB.echoDebug("...COLOR set: '%s'", cString);
} catch( Exception e) {
dB.echoDebug("...COLOR could not be set: '%s' is an invalid color!", cString);
}
} else if(aH.matchesValueArg("PLAYER", arg, ArgumentType.String)) {
playerName = aH.getStringFrom(arg);
dB.echoDebug("...PLAYER set: '%s'", arg);
}
}

scriptEntry.addObject("color", color);
scriptEntry.addObject("player", playerName);
}
}

@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
dNPC npc = scriptEntry.getNPC();

ChatColor color = (ChatColor) scriptEntry.getObject("color");

if(color != null && npc.getCitizen().hasTrait(NameplateTrait.class)) {
NameplateTrait trait = npc.getCitizen().getTrait(NameplateTrait.class);
String playerName = (String) scriptEntry.getObject("player");

if(playerName != null) {
trait.setColor(color, playerName);
} else {
trait.setColor(color);
}
}
}


@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {

ChatColor color = null;
boolean player = false;
String text = null;

List<String> args = scriptEntry.getArguments();

for(String arg : args) {
if(aH.matchesValueArg("COLOR", arg, ArgumentType.String))
try { color = ChatColor.valueOf(aH.getStringFrom(arg).toUpperCase()); } catch( Exception e) {
dB.echoDebug("...COLOR could not be set: '%s' is an invalid color!", aH.getStringFrom(arg)); }

else if(aH.matchesValueArg("TARGET", arg, ArgumentType.Word)) {
player = true;
scriptEntry.setPlayer(aH.getPlayerFrom(arg));
}

else if (aH.matchesValueArg("SET", arg, ArgumentType.Custom))
text = aH.getStringFrom(arg);
}

scriptEntry.addObject("color", color)
.addObject("player", player)
.addObject("text", text);
}

@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {

Boolean player = (Boolean) scriptEntry.getObject("player");
ChatColor color = (ChatColor) scriptEntry.getObject("color");
String text = (String) scriptEntry.getObject("text");




if (text != null) {
if (text.equalsIgnoreCase("none")) {
scriptEntry.getNPC().getEntity().setCustomNameVisible(false);
dB.echoDebug("none");
} else {
scriptEntry.getNPC().getEntity().setCustomNameVisible(true);
scriptEntry.getNPC().getEntity().setCustomName(text);
dB.echoDebug(text);
}

if (scriptEntry.getNPC().getEntity() instanceof Player)
((Player) scriptEntry.getNPC().getEntity()).setDisplayName(text);
}

if(color != null) {
if (!scriptEntry.getNPC().getCitizen().hasTrait(NameplateTrait.class))
scriptEntry.getNPC().getCitizen().addTrait(NameplateTrait.class);
NameplateTrait trait = scriptEntry.getNPC().getCitizen().getTrait(NameplateTrait.class);

if (player) trait.setColor(color, scriptEntry.getPlayer().getName());
else trait.setColor(color);
}

}

}

0 comments on commit 3112dc5

Please sign in to comment.