Skip to content

Commit

Permalink
Bats effect
Browse files Browse the repository at this point in the history
  • Loading branch information
mbax committed Feb 15, 2016
1 parent b877bfb commit 25307bc
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/main/java/org/kitteh/vanish/Vanish.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@
public class Vanish {
public static final String PERMISSION_VANISH = "vanish.vanish";

public static final String PERMISSION_EFFECTS_BATS = "vanish.effects.bats";

@Inject
private Game game;

@Listener
public void onGameInit(GameInitializationEvent event) {
CommandSpec vanishCommandSpec = CommandSpec.builder().permission(PERMISSION_VANISH).executor(new VanishCommand()).build();
CommandSpec vanishCommandSpec = CommandSpec.builder().permission(PERMISSION_VANISH).executor(new VanishCommand(this)).build();
this.game.getCommandManager().register(this, vanishCommandSpec, "vanish", "v");
}
}
38 changes: 38 additions & 0 deletions src/main/java/org/kitteh/vanish/VanishCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,40 @@
*/
package org.kitteh.vanish;

import org.spongepowered.api.Sponge;
import org.spongepowered.api.command.CommandException;
import org.spongepowered.api.command.CommandResult;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.command.args.CommandContext;
import org.spongepowered.api.command.spec.CommandExecutor;
import org.spongepowered.api.data.key.Keys;
import org.spongepowered.api.effect.particle.ParticleEffect;
import org.spongepowered.api.effect.particle.ParticleTypes;
import org.spongepowered.api.entity.Entity;
import org.spongepowered.api.entity.EntityTypes;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.event.cause.Cause;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.format.TextColors;
import org.spongepowered.api.world.Location;

import javax.annotation.Nonnull;
import java.util.HashSet;
import java.util.Set;

/**
* Vanish!
*/
class VanishCommand implements CommandExecutor {
private final Vanish plugin;
private final ParticleEffect effect;
private static final int LIFE_TICKS = 3 * 20;

VanishCommand(Vanish plugin) {
this.plugin = plugin;
this.effect = ParticleEffect.builder().type(ParticleTypes.SMOKE_LARGE).count(1).build();
}

@Nonnull
@Override
public CommandResult execute(@Nonnull CommandSource commandSource, @Nonnull CommandContext commandContext) throws CommandException {
Expand All @@ -51,6 +69,26 @@ public CommandResult execute(@Nonnull CommandSource commandSource, @Nonnull Comm
player.offer(Keys.INVISIBLE, !wasVisible);
player.offer(Keys.INVISIBILITY_IGNORES_COLLISION, !wasVisible);
player.offer(Keys.INVISIBILITY_PREVENTS_TARGETING, !wasVisible);
if (player.hasPermission(Vanish.PERMISSION_EFFECTS_BATS)) {
Set<Entity> bats = new HashSet<>();
Location location = player.getLocation();
for (int i = 0; i < 10; i++) {
location.getExtent().createEntity(EntityTypes.BAT, location.getPosition()).ifPresent(bat -> {
bats.add(bat);
location.getExtent().spawnEntity(bat, Cause.of(bat)); // TODO actual causes once SpongeDocs match SpongeReality
bat.offer(Keys.INVULNERABILITY, LIFE_TICKS);
});
}
// TODO remove on shutdown too!
Sponge.getScheduler().createTaskBuilder().delayTicks(LIFE_TICKS).execute(() -> {
bats.forEach(bat -> {
if (bat.isLoaded()) {
bat.getWorld().spawnParticles(this.effect, bat.getLocation().getPosition());
bat.remove();
}
});
}).submit(this.plugin);
}
player.sendMessage(Text.of(TextColors.AQUA, "You are now " + (wasVisible ? "" : "in") + "visible"));
return CommandResult.success();
}
Expand Down

0 comments on commit 25307bc

Please sign in to comment.