Skip to content

Commit

Permalink
Add some javadoc
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed May 16, 2019
1 parent 65cbb88 commit 8c30a78
Show file tree
Hide file tree
Showing 36 changed files with 524 additions and 153 deletions.
11 changes: 11 additions & 0 deletions main/src/main/java/net/citizensnpcs/trait/Age.java
Expand Up @@ -10,6 +10,11 @@
import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.util.Messages;

/**
* Persists the Minecraft age variable for {@link Ageable}s. Will also set baby for {@link Zombie}s.
*
* @see Ageable
*/
@TraitName("age")
public class Age extends Trait implements Toggleable {
@Persist
Expand All @@ -22,6 +27,9 @@ public Age() {
super("age");
}

/**
* Send a brief description of the current state of the trait to the supplied {@link CommandSender}.
*/
public void describe(CommandSender sender) {
Messaging.sendTr(sender, Messages.AGE_TRAIT_DESCRIPTION, npc.getName(), age, locked);
}
Expand Down Expand Up @@ -61,6 +69,9 @@ public void setAge(int age) {
}
}

/**
* Toggles the age lock variable and returns whether the age is currently locked.
*/
@Override
public boolean toggle() {
locked = !locked;
Expand Down
11 changes: 8 additions & 3 deletions main/src/main/java/net/citizensnpcs/trait/Anchors.java
Expand Up @@ -16,6 +16,9 @@
import net.citizensnpcs.util.Anchor;
import net.citizensnpcs.util.Messages;

/**
* Persists a list of {@link Anchor}s.
*/
@TraitName("anchors")
public class Anchors extends Trait {
private final List<Anchor> anchors = new ArrayList<Anchor>();
Expand All @@ -33,10 +36,12 @@ public boolean addAnchor(String name, Location location) {
}

@EventHandler
public void checkWorld(WorldLoadEvent event) {
for (Anchor anchor : anchors)
if (!anchor.isLoaded())
private void checkWorld(WorldLoadEvent event) {
for (Anchor anchor : anchors) {
if (!anchor.isLoaded()) {
anchor.load();
}
}
}

public Anchor getAnchor(String name) {
Expand Down
21 changes: 21 additions & 0 deletions main/src/main/java/net/citizensnpcs/trait/ArmorStandTrait.java
Expand Up @@ -7,6 +7,9 @@
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.TraitName;

/**
* Persists data related to {@link ArmorStand} NPCs.
*/
@TraitName("armorstandtrait")
public class ArmorStandTrait extends Trait {
@Persist
Expand Down Expand Up @@ -82,26 +85,44 @@ public void run() {
}
}

/**
* @see ArmorStand#setGravity(boolean)
*/
public void setGravity(boolean gravity) {
this.gravity = gravity;
}

/**
* @see ArmorStand#setArms(boolean)
*/
public void setHasArms(boolean arms) {
this.hasarms = arms;
}

/**
* @see ArmorStand#setBasePlate(boolean)
*/
public void setHasBaseplate(boolean baseplate) {
this.hasbaseplate = baseplate;
}

/**
* @see ArmorStand#setMarker(boolean)
*/
public void setMarker(boolean marker) {
this.marker = marker;
}

/**
* @see ArmorStand#setSmall(boolean)
*/
public void setSmall(boolean small) {
this.small = small;
}

/**
* @see ArmorStand#setVisible(boolean)
*/
public void setVisible(boolean visible) {
this.visible = visible;
}
Expand Down
98 changes: 76 additions & 22 deletions main/src/main/java/net/citizensnpcs/trait/Controllable.java
Expand Up @@ -31,6 +31,12 @@
import net.citizensnpcs.util.NMS;
import net.citizensnpcs.util.Util;

/**
* Persists the controllable status for /npc controllable
*
* A controllable {@link NPC} can be mounted by a {@link Player} using right click or /npc mount and moved around using
* e.g. arrow keys.
*/
@TraitName("controllable")
public class Controllable extends Trait implements Toggleable, CommandConfigurable {
private MovementController controller = new GroundController();
Expand All @@ -49,6 +55,11 @@ public Controllable(boolean enabled) {
this.enabled = enabled;
}

/**
* Configures the explicit type parameter.
*
* @see #setExplicitType(EntityType)
*/
@Override
public void configure(CommandContext args) {
if (args.hasFlag('f')) {
Expand Down Expand Up @@ -103,22 +114,14 @@ private void loadController() {
controller = new LookAirController();
return;
}
Class<? extends MovementController> clazz = controllerTypes.get(type);
if (clazz == null) {
Constructor<? extends MovementController> innerConstructor = CONTROLLER_TYPES.get(type);
if (innerConstructor == null) {
controller = new GroundController();
return;
}
Constructor<? extends MovementController> innerConstructor = null;
try {
innerConstructor = clazz.getConstructor(Controllable.class);
innerConstructor.setAccessible(true);
} catch (Exception e) {
e.printStackTrace();
}

try {
if (innerConstructor == null) {
controller = clazz.newInstance();
if (innerConstructor.getParameterCount() == 0) {
controller = innerConstructor.newInstance();
} else {
controller = innerConstructor.newInstance(this);
}
Expand All @@ -128,6 +131,13 @@ private void loadController() {
}
}

/**
* Attempts to mount the {@link NPC} onto the supplied {@link Player}.
*
* @param toMount
* the player to mount
* @return whether the mount was successful
*/
public boolean mount(Player toMount) {
boolean found = NMS.getPassengers(npc.getEntity()).size() == 0;
for (Entity passenger : NMS.getPassengers(npc.getEntity())) {
Expand All @@ -143,7 +153,7 @@ public boolean mount(Player toMount) {
}

@EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR)
public void onPlayerInteract(PlayerInteractEvent event) {
private void onPlayerInteract(PlayerInteractEvent event) {
if (!npc.isSpawned() || !enabled)
return;
Action performed = event.getAction();
Expand All @@ -164,7 +174,7 @@ public void onPlayerInteract(PlayerInteractEvent event) {
}

@EventHandler
public void onRightClick(NPCRightClickEvent event) {
private void onRightClick(NPCRightClickEvent event) {
if (!enabled || !npc.isSpawned() || !event.getNPC().equals(npc))
return;
controller.rightClickEntity(event);
Expand Down Expand Up @@ -199,6 +209,16 @@ public boolean setEnabled(boolean enabled) {
return enabled;
}

/**
* Configures the explicit typei.e. whether the NPC should be controlled as if it was a certain {@link EntityType}.
*
* @param type
* the explicit type
*/
public void setExplicitType(EntityType type) {
this.explicitType = type;
}

private void setMountedYaw(Entity entity) {
if (entity instanceof EnderDragon || !Setting.USE_BOAT_CONTROLS.asBoolean())
return; // EnderDragon handles this separately
Expand All @@ -218,6 +238,12 @@ private void setMountedYaw(Entity entity) {
NMS.look(entity, loc.getYaw(), loc.getPitch());
}

/**
* Sets whether the {@link Player} attempting to mount the {@link NPC} must actually own the {@link NPC} to mount
* it.
*
* @see Owner#isOwnedBy(org.bukkit.command.CommandSender)
*/
public void setOwnerRequired(boolean ownerRequired) {
this.ownerRequired = ownerRequired;
}
Expand Down Expand Up @@ -378,19 +404,47 @@ public void run(Player rider) {
}
}

/**
* Register a movement controller for a certain {@link EntityType} to be used for {@link NPC}s with that type.
*
* Default controllers are registered for BAT, BLAZE, ENDER_DRAGON, GHAST, WITHER and PARROT using
* {@link PlayerInputAirController}.
*
* @param type
* the entity type
* @param clazz
* the controller class
*/
public static void registerControllerType(EntityType type, Class<? extends MovementController> clazz) {
controllerTypes.put(type, clazz);
try {
Constructor<? extends MovementController> constructor = clazz.getConstructor(Controllable.class);
constructor.setAccessible(true);
CONTROLLER_TYPES.put(type, constructor);
return;
} catch (Exception e) {
try {
Constructor<? extends MovementController> constructor = clazz.getConstructor();
constructor.setAccessible(true);
CONTROLLER_TYPES.put(type, constructor);
} catch (Exception e2) {
throw new RuntimeException(e2);
}
}
}

private static final Map<EntityType, Class<? extends MovementController>> controllerTypes = Maps
private static final Map<EntityType, Constructor<? extends MovementController>> CONTROLLER_TYPES = Maps
.newEnumMap(EntityType.class);

static {
controllerTypes.put(EntityType.BAT, PlayerInputAirController.class);
controllerTypes.put(EntityType.BLAZE, PlayerInputAirController.class);
controllerTypes.put(EntityType.ENDER_DRAGON, PlayerInputAirController.class);
controllerTypes.put(EntityType.GHAST, PlayerInputAirController.class);
controllerTypes.put(EntityType.WITHER, PlayerInputAirController.class);
controllerTypes.put(EntityType.UNKNOWN, LookAirController.class);
registerControllerType(EntityType.BAT, PlayerInputAirController.class);
registerControllerType(EntityType.BLAZE, PlayerInputAirController.class);
registerControllerType(EntityType.ENDER_DRAGON, PlayerInputAirController.class);
registerControllerType(EntityType.GHAST, PlayerInputAirController.class);
registerControllerType(EntityType.WITHER, PlayerInputAirController.class);
try {
registerControllerType(EntityType.valueOf("PARROT"), PlayerInputAirController.class);
} catch (IllegalArgumentException ex) {
}
registerControllerType(EntityType.UNKNOWN, LookAirController.class);
}
}
Expand Up @@ -6,6 +6,9 @@
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.TraitName;

/**
* Persists the current {@link Location} of the {@link NPC}. Will cache last known location if despawned.
*/
@TraitName("location")
public class CurrentLocation extends Trait {
@Persist(value = "", required = true)
Expand Down
39 changes: 30 additions & 9 deletions main/src/main/java/net/citizensnpcs/trait/FollowTrait.java
Expand Up @@ -12,10 +12,13 @@
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.TraitName;

/**
* Persists a {@link Player} to follow while spawned. Optionally allows protecting of the player as well.
*/
@TraitName("followtrait")
public class FollowTrait extends Trait {
@Persist
private boolean active = false;
@Persist("active")
private boolean enabled = false;
@Persist
private UUID followingUUID;
private Player player;
Expand All @@ -26,12 +29,19 @@ public FollowTrait() {
super("followtrait");
}

private boolean isActive() {
return active && npc.isSpawned() && player != null && npc.getEntity().getWorld().equals(player.getWorld());
/**
* Returns whether the trait is actively following a {@link Player}.
*/
public boolean isActive() {
return enabled && npc.isSpawned() && player != null && npc.getEntity().getWorld().equals(player.getWorld());
}

public boolean isEnabled() {
return enabled;
}

@EventHandler
public void onEntityDamage(EntityDamageByEntityEvent event) {
private void onEntityDamage(EntityDamageByEntityEvent event) {
if (isActive() && event.getEntity().equals(player)) {
npc.getNavigator().setTarget(event.getDamager(), true);
}
Expand All @@ -55,18 +65,29 @@ public void run() {
}
}

/**
* Toggles and/or sets the {@link OfflinePlayer} to follow and whether to protect them (similar to wolves in
* Minecraft, attack whoever attacks the player).
*
* Will toggle if the {@link OfflinePlayer} is the player currently being followed.
*
* @param player
* the player to follow
* @param protect
* whether to protect the player
* @return whether the trait is enabled
*/
public boolean toggle(OfflinePlayer player, boolean protect) {
this.protect = protect;
if (player.getUniqueId().equals(this.followingUUID) || this.followingUUID == null) {
this.active = !active;
this.enabled = !enabled;
}
this.followingUUID = player.getUniqueId();
if (npc.getNavigator().isNavigating() && this.player != null
&& npc.getNavigator().getEntityTarget() != null
if (npc.getNavigator().isNavigating() && this.player != null && npc.getNavigator().getEntityTarget() != null
&& this.player == npc.getNavigator().getEntityTarget().getTarget()) {
npc.getNavigator().cancelNavigation();
}
this.player = null;
return this.active;
return this.enabled;
}
}
5 changes: 5 additions & 0 deletions main/src/main/java/net/citizensnpcs/trait/GameModeTrait.java
Expand Up @@ -7,6 +7,11 @@
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.TraitName;

/**
* Persists the {@link GameMode} of a {@link Player} NPC.
*
* @see Player#setGameMode(GameMode)
*/
@TraitName("gamemodetrait")
public class GameModeTrait extends Trait {
@Persist
Expand Down

0 comments on commit 8c30a78

Please sign in to comment.