Skip to content

Commit

Permalink
Merge pull request #54 from Rincewind34/master
Browse files Browse the repository at this point in the history
Mark getTrait as deprecated and added getOrAddTrait and getTraitSafely
  • Loading branch information
fullwall committed Sep 14, 2020
2 parents 15c21ca + 8aa1a31 commit 45f8820
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package net.citizensnpcs.api.ai.speech;

import org.bukkit.Bukkit;

import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.ai.speech.event.NPCSpeechEvent;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.trait.trait.Speech;

import org.bukkit.Bukkit;

/**
* Simple implementation of {@link SpeechController} which allows a NPC to speak with any registered {@link VocalChord}.
*
Expand All @@ -20,7 +20,7 @@ public SimpleSpeechController(NPC npc) {

@Override
public void speak(SpeechContext context) {
speak(context, npc.getTrait(Speech.class).getDefaultVocalChord());
speak(context, npc.getOrAddTrait(Speech.class).getDefaultVocalChord());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void process(CommandSender sender, CommandContext context, Annotation ins
}

if (requirements.ownership() && npc != null && !sender.hasPermission("citizens.admin")
&& !npc.getTrait(Owner.class).isOwnedBy(sender)) {
&& !npc.getOrAddTrait(Owner.class).isOwnedBy(sender)) {
throw new RequirementMissingException(Messaging.tr(CommandMessages.MUST_BE_OWNER));
}

Expand All @@ -67,7 +67,7 @@ public void process(CommandSender sender, CommandContext context, Annotation ins
}
types.removeAll(Sets.newHashSet(requirements.excludedTypes()));

EntityType type = npc.getTrait(MobType.class).getType();
EntityType type = npc.getOrAddTrait(MobType.class).getType();
if (!types.contains(type)) {
throw new RequirementMissingException(Messaging.tr(CommandMessages.REQUIREMENTS_INVALID_MOB_TYPE,
type.name().toLowerCase().replace('_', ' ')));
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/net/citizensnpcs/api/npc/AbstractNPC.java
Original file line number Diff line number Diff line change
Expand Up @@ -241,14 +241,25 @@ public NPCRegistry getOwningRegistry() {
}

@Override
@Deprecated
public <T extends Trait> T getTrait(Class<T> clazz) {
return getOrAddTrait(clazz);
}

@Override
public <T extends Trait> T getOrAddTrait(Class<T> clazz) {
Trait trait = traits.get(clazz);
if (trait == null) {
trait = getTraitFor(clazz);
addTrait(trait);
}
return trait != null ? clazz.cast(trait) : null;
}

@Override
public <T extends Trait> T getTraitNullable(Class<T> clazz) {
return clazz.cast(traits.get(clazz)); // #cast allows null as value
}

protected Trait getTraitFor(Class<? extends Trait> clazz) {
return CitizensAPI.getTraitFactory().getTrait(clazz);
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/net/citizensnpcs/api/npc/NPC.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,31 @@ public interface NPC extends Agent, Cloneable {
* @param trait
* Trait to get
* @return Trait with the given name
*
* @deprecated for intransparent naming. Use {@link #getOrAddTrait(Class)} for the same behavior.
*/
@Deprecated
public <T extends Trait> T getTrait(Class<T> trait);

/**
* Gets a trait from the given class. If the NPC does not currently have the trait then it will be created and
* attached using {@link #addTrait(Class)} .
*
* @param trait
* Trait to get
* @return Trait with the given name
*/
public <T extends Trait> T getOrAddTrait(Class<T> trait);

/**
* Gets a trait from the given class. If the NPC does not currently have the trait,
* <code>null</code> will be returned.
*
* @param trait
* Trait to get
* @return Trait with the given name
*/
public <T extends Trait> T getTraitNullable(Class<T> trait);

/**
* Returns the currently attached {@link Trait}s
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/net/citizensnpcs/api/trait/trait/Inventory.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void inventoryCloseEvent(InventoryCloseEvent event) {
for (int i = 0; i < contents.length; i++) {
this.contents[i] = contents[i];
if (i == 0) {
npc.getTrait(Equipment.class).setItemInHand(contents[i]);
npc.getOrAddTrait(Equipment.class).setItemInHand(contents[i]);
}
}
if (npc.getEntity() instanceof InventoryHolder) {
Expand Down Expand Up @@ -164,7 +164,7 @@ private void saveContents(Entity entity) {
contents = ((InventoryHolder) entity).getInventory().getContents();
}
if (entity instanceof Player) {
npc.getTrait(Equipment.class).setItemInHand(contents[0]);
npc.getOrAddTrait(Equipment.class).setItemInHand(contents[0]);
}
}

Expand Down Expand Up @@ -230,7 +230,7 @@ public void setItem(int slot, ItemStack item) {
throw new IndexOutOfBoundsException();
}
if (slot == 0) {
npc.getTrait(Equipment.class).setItemInHand(item);
npc.getOrAddTrait(Equipment.class).setItemInHand(item);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/citizensnpcs/api/util/Placeholders.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static String replace(String text, CommandSender sender, NPC npc) {
if (npc == null) {
return text;
}
text = text.replace("<owner>", npc.getTrait(Owner.class).getOwner());
text = text.replace("<owner>", npc.getOrAddTrait(Owner.class).getOwner());
text = text.replace("<npc>", npc.getName());
text = text.replace("<id>", Integer.toString(npc.getId()));
return text;
Expand Down

0 comments on commit 45f8820

Please sign in to comment.