Skip to content
This repository has been archived by the owner on Apr 12, 2022. It is now read-only.

Commit

Permalink
Strike Command
Browse files Browse the repository at this point in the history
Also fixed and improved the Heal command
  • Loading branch information
Xenmai committed Nov 10, 2017
1 parent 2569225 commit 7504cc3
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 20 deletions.
Expand Up @@ -168,6 +168,7 @@ public void onServerStart(GamePreInitializationEvent event) {
Denizen2Core.register(new RemoveGameRuleCommand());
Denizen2Core.register(new SetBlockCommand());
Denizen2Core.register(new SetGameRuleCommand());
Denizen2Core.register(new StrikeCommand());
Denizen2Core.register(new UnloadWorldCommand());
Denizen2Core.register(new WeatherCommand());
// Events: Entity
Expand Down
Expand Up @@ -57,31 +57,52 @@ public void execute(CommandQueue queue, CommandEntry entry) {
EntityTag entityTag = EntityTag.getFor(queue.error, entry.getArgumentObject(queue, 0));
Living ent = (Living) entityTag.getInternal();
NumberTag amount = NumberTag.getFor(queue.error, entry.getArgumentObject(queue, 1));
String operation;
Boolean set;
if (entry.namedArgs.containsKey("operation")) {
operation = CoreUtilities.toLowerCase(entry.getNamedArgumentObject(queue, "operation").toString());
if (!(operation.equals("add") || operation.equals("set"))) {
String operation = CoreUtilities.toLowerCase(entry.getNamedArgumentObject(queue, "operation").toString());
if (operation.equals("add")) {
set = false;
}
else if (operation.equals("set")) {
set = true;
}
else {
queue.handleError(entry, "Invalid operation: '" + operation + "'!");
return;
}
}
else {
operation = "add";
set = false;
}
String type;
double value;
if (entry.namedArgs.containsKey("type")) {
type = CoreUtilities.toLowerCase(entry.getNamedArgumentObject(queue, "type").toString());
switch (type) {
case "remaining":
value = ent.health().transform(operation.equals("add") ?
x -> x + amount.getInternal() : x -> amount.getInternal()).get();
ent.offer(Keys.HEALTH, value);
if (set) {
if (amount.getInternal() < 0) {
queue.handleError(entry, "You can't set health to negative values!");
return;
}
ent.offer(Keys.HEALTH, Math.min(amount.getInternal(), ent.maxHealth().get()));
}
else {
ent.offer(Keys.HEALTH, ent.health().transform(
x -> Math.max(Math.min(x + amount.getInternal(), ent.maxHealth().get()), 0)).get());
}
break;
case "maximum":
value = ent.maxHealth().transform(operation.equals("add") ?
x -> x + amount.getInternal() : x -> amount.getInternal()).get();
ent.offer(Keys.MAX_HEALTH, value);
if (set) {
if (amount.getInternal() < 0) {
queue.handleError(entry, "You can't set max health to negative values!");
return;
}
ent.offer(Keys.MAX_HEALTH, amount.getInternal());
}
else {
ent.offer(Keys.MAX_HEALTH, ent.maxHealth().transform(
x -> Math.max(x + amount.getInternal(), 0)).get());
}
break;
default:
queue.handleError(entry, "Invalid health type: '" + type + "'!");
Expand All @@ -90,14 +111,22 @@ public void execute(CommandQueue queue, CommandEntry entry) {
}
else {
type = "remaining";
value = ent.health().transform(operation.equals("add") ?
x -> x + amount.getInternal() : x -> amount.getInternal()).get();
ent.offer(Keys.HEALTH, value);
if (set) {
if (amount.getInternal() < 0) {
queue.handleError(entry, "You can't set health to negative values!");
return;
}
ent.offer(Keys.HEALTH, Math.min(amount.getInternal(), ent.maxHealth().get()));
}
else {
ent.offer(Keys.HEALTH, ent.health().transform(
x -> Math.max(Math.min(x + amount.getInternal(), ent.maxHealth().get()), 0)).get());
}
}
if (queue.shouldShowGood()) {
queue.outGood(ColorSet.emphasis + (operation.equals("add") ? "Increasing" : "Setting") +
queue.outGood(ColorSet.emphasis + (set ? "Setting" : "Increasing") +
ColorSet.good + " the " + ColorSet.emphasis + type + ColorSet.good + " health of " +
ColorSet.emphasis + entityTag.debug() + ColorSet.good + (operation.equals("add") ? " by " : " to ") +
ColorSet.emphasis + entityTag.debug() + ColorSet.good + (set ? " to " : " by ") +
ColorSet.emphasis + amount.debug() + ColorSet.good + "!");
}
}
Expand Down
Expand Up @@ -25,7 +25,7 @@ public class BanCommand extends AbstractCommand {
// <--[command]
// @Since 0.3.0
// @Name ban
// @Arguments <PlayerTag>/<OfflinePlayerTag>/<IP> [duration] [reason]
// @Arguments <PlayerTag>/<IP> [duration] [reason]
// @Short bans a player.
// @Updated 2017/04/08
// @Group Player
Expand All @@ -48,7 +48,7 @@ public String getName() {

@Override
public String getArguments() {
return "<PlayerTag>/<OfflinePlayerTag>/<IP> [duration] [reason]";
return "<PlayerTag>/<IP> [duration] [reason]";
}

@Override
Expand Down
Expand Up @@ -18,7 +18,7 @@ public class PardonCommand extends AbstractCommand {
// <--[command]
// @Since 0.3.0
// @Name pardon
// @Arguments <OfflinePlayerTag>/<IP>
// @Arguments <PlayerTag>/<IP>
// @Short pardons a player.
// @Updated 2017/04/08
// @Group Player
Expand All @@ -38,7 +38,7 @@ public String getName() {

@Override
public String getArguments() {
return "<OfflinePlayerTag>/<IP>";
return "<PlayerTag>/<IP>";
}

@Override
Expand Down
@@ -0,0 +1,68 @@
package com.denizenscript.denizen2sponge.commands.world;

import com.denizenscript.denizen2core.commands.AbstractCommand;
import com.denizenscript.denizen2core.commands.CommandEntry;
import com.denizenscript.denizen2core.commands.CommandQueue;
import com.denizenscript.denizen2core.tags.objects.BooleanTag;
import com.denizenscript.denizen2core.utilities.debugging.ColorSet;
import com.denizenscript.denizen2sponge.tags.objects.LocationTag;
import org.spongepowered.api.entity.EntityTypes;
import org.spongepowered.api.entity.weather.WeatherEffect;

public class StrikeCommand extends AbstractCommand {

// <--[command]
// @Since 0.4.0
// @Name strike
// @Arguments <location>
// @Short strikes lightning.
// @Updated 2017/11/10
// @Group World
// @Minimum 1
// @Maximum 1
// @Named ambient (BooleanTag) Sets whether the lightning will affect entities.
// @Description
// Strikes lightning upon a location. Optionally specify whether it will affect entities
// or not. Damaging lightning will also charge creepers and turn pigs into pig zombies.
// @Example
// # This example strikes a damaging lightning upon the player.
// - strike <player.location> --ambient false
// -->

@Override
public String getName() {
return "strike";
}

@Override
public String getArguments() {
return "<location>";
}

@Override
public int getMinimumArguments() {
return 1;
}

@Override
public int getMaximumArguments() {
return 1;
}

@Override
public void execute(CommandQueue queue, CommandEntry entry) {
LocationTag loc = LocationTag.getFor(queue.error, entry.getArgumentObject(queue, 0));
WeatherEffect ent = (WeatherEffect) loc.getInternal().world.createEntity(EntityTypes.LIGHTNING,
loc.getInternal().toVector3d());
if (entry.namedArgs.containsKey("ambient")) {
BooleanTag ambient = BooleanTag.getFor(queue.error, entry.getNamedArgumentObject(queue, "ambient"));
ent.setEffect(ambient.getInternal());
}
loc.getInternal().world.spawnEntity(ent);
if (queue.shouldShowGood()) {
queue.outGood("Successfully struck " + ColorSet.emphasis
+ (ent.isEffect() ? "ambient" : "damaging") + ColorSet.good
+ " lightning at location " + ColorSet.emphasis + loc.debug() + ColorSet.good + "!");
}
}
}

0 comments on commit 7504cc3

Please sign in to comment.