From 5d88b0b32dcd91e3652ef8c815f61d38f99565b1 Mon Sep 17 00:00:00 2001 From: Xenmai Date: Thu, 20 Apr 2017 01:07:22 +0200 Subject: [PATCH] Feed and Heal Commands Also improved air command. --- .../denizen2sponge/Denizen2Sponge.java | 2 + .../commands/entity/AirCommand.java | 58 ++++++---- .../commands/entity/HealCommand.java | 103 +++++++++++++++++ .../commands/player/FeedCommand.java | 105 ++++++++++++++++++ 4 files changed, 246 insertions(+), 22 deletions(-) create mode 100644 src/main/java/com/denizenscript/denizen2sponge/commands/entity/HealCommand.java create mode 100644 src/main/java/com/denizenscript/denizen2sponge/commands/player/FeedCommand.java diff --git a/src/main/java/com/denizenscript/denizen2sponge/Denizen2Sponge.java b/src/main/java/com/denizenscript/denizen2sponge/Denizen2Sponge.java index dd93fe4..6d03e62 100644 --- a/src/main/java/com/denizenscript/denizen2sponge/Denizen2Sponge.java +++ b/src/main/java/com/denizenscript/denizen2sponge/Denizen2Sponge.java @@ -124,6 +124,7 @@ public void onServerStart(GamePreInitializationEvent event) { Denizen2Core.register(new EditEntityCommand()); Denizen2Core.register(new EquipCommand()); Denizen2Core.register(new FlagCommand()); + Denizen2Core.register(new HealCommand()); Denizen2Core.register(new HurtCommand()); Denizen2Core.register(new MountCommand()); Denizen2Core.register(new RemoveCommand()); @@ -133,6 +134,7 @@ public void onServerStart(GamePreInitializationEvent event) { // Commands: Player Denizen2Core.register(new ActionBarCommand()); Denizen2Core.register(new BanCommand()); + Denizen2Core.register(new FeedCommand()); Denizen2Core.register(new GiveCommand()); Denizen2Core.register(new KickCommand()); Denizen2Core.register(new NarrateCommand()); diff --git a/src/main/java/com/denizenscript/denizen2sponge/commands/entity/AirCommand.java b/src/main/java/com/denizenscript/denizen2sponge/commands/entity/AirCommand.java index 254e725..fbf9f36 100644 --- a/src/main/java/com/denizenscript/denizen2sponge/commands/entity/AirCommand.java +++ b/src/main/java/com/denizenscript/denizen2sponge/commands/entity/AirCommand.java @@ -3,38 +3,31 @@ 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.tags.objects.DurationTag; -import com.denizenscript.denizen2core.tags.objects.NumberTag; import com.denizenscript.denizen2core.utilities.CoreUtilities; import com.denizenscript.denizen2core.utilities.debugging.ColorSet; -import com.denizenscript.denizen2sponge.Denizen2Sponge; import com.denizenscript.denizen2sponge.tags.objects.EntityTag; -import org.spongepowered.api.Sponge; import org.spongepowered.api.data.key.Keys; -import org.spongepowered.api.event.cause.entity.damage.DamageType; -import org.spongepowered.api.event.cause.entity.damage.DamageTypes; -import org.spongepowered.api.event.cause.entity.damage.source.DamageSource; - -import java.util.Optional; public class AirCommand extends AbstractCommand { // <--[command] // @Name air // @Arguments - // @Short sets the air level of the entity. - // @Updated 2017/04/17 + // @Short changes the air level of the entity. + // @Updated 2017/04/19 // @Group Entities // @Minimum 2 // @Maximum 2 // @Named type (TextTag) Sets of what type the air level will be. + // @Named operation (TextTag) Sets whether the command will add or set the value. // @Description - // Sets the air level of the entity. Optionally specify a type ('remaining' or 'maximum') - // to adjust the specified air level type. Defaults to 'remaining'. + // Changes the air level of the entity. Optionally specify a type ('remaining' or 'maximum') + // to adjust the specified air level type. Defaults to 'remaining'. Also specify whether + // the command will 'add' or 'set' the value. Defaults to 'add'. // @Example // # This example completely fills the air bar of the player - // - air + // - air --operation set // --> @Override @@ -60,20 +53,37 @@ public int getMaximumArguments() { @Override public void execute(CommandQueue queue, CommandEntry entry) { EntityTag ent = EntityTag.getFor(queue.error, entry.getArgumentObject(queue, 0)); - DurationTag dur = DurationTag.getFor(queue.error, entry.getArgumentObject(queue, 1)); + DurationTag dt = DurationTag.getFor(queue.error, entry.getArgumentObject(queue, 1)); if (!ent.getInternal().supports(Keys.MAX_AIR)) { queue.handleError(entry, "This entity does not support air levels!"); return; } + String operation; + if (entry.namedArgs.containsKey("operation")) { + operation = CoreUtilities.toLowerCase(entry.getNamedArgumentObject(queue, "operation").toString()); + if (!(operation.equals("add") || operation.equals("set"))) { + queue.handleError(entry, "Invalid operation: '" + operation + "'!"); + return; + } + } + else { + operation = "add"; + } String type; + int ticks; if (entry.namedArgs.containsKey("type")) { - type = entry.getNamedArgumentObject(queue, "type").toString(); + type = CoreUtilities.toLowerCase(entry.getNamedArgumentObject(queue, "type").toString()); switch (type) { case "remaining": - ent.getInternal().offer(Keys.REMAINING_AIR, (int) (dur.getInternal() * 20)); + ticks = operation.equals("add") ? (int) (dt.getInternal() * 20) + + ent.getInternal().get(Keys.REMAINING_AIR).orElseGet(() -> ent.getInternal().get(Keys.MAX_AIR).get()) : + (int) (dt.getInternal() * 20); + ent.getInternal().offer(Keys.REMAINING_AIR, ticks); break; case "maximum": - ent.getInternal().offer(Keys.MAX_AIR, (int) (dur.getInternal() * 20)); + ticks = operation.equals("add") ? (int) (dt.getInternal() * 20) + + ent.getInternal().get(Keys.MAX_AIR).get() : (int) (dt.getInternal() * 20); + ent.getInternal().offer(Keys.MAX_AIR, ticks); break; default: queue.handleError(entry, "Invalid air level type: '" + type + "'!"); @@ -82,12 +92,16 @@ public void execute(CommandQueue queue, CommandEntry entry) { } else { type = "remaining"; - ent.getInternal().offer(Keys.REMAINING_AIR, (int) (dur.getInternal() * 20)); + ticks = operation.equals("add") ? (int) (dt.getInternal() * 20) + + ent.getInternal().get(Keys.REMAINING_AIR).orElseGet(() -> ent.getInternal().get(Keys.MAX_AIR).get()) : + (int) (dt.getInternal() * 20); + ent.getInternal().offer(Keys.REMAINING_AIR, ticks); } if (queue.shouldShowGood()) { - queue.outGood("Setting the " + ColorSet.emphasis + type + ColorSet.good + " air level of " - + ColorSet.emphasis + ent.debug() + ColorSet.good + " to " - + ColorSet.emphasis + dur.debug() + ColorSet.good + " seconds!"); + queue.outGood(ColorSet.emphasis + (operation.equals("add") ? "Increasing" : "Setting") + + ColorSet.good + " the " + ColorSet.emphasis + type + ColorSet.good + " air level of " + + ColorSet.emphasis + ent.debug() + ColorSet.good + (operation.equals("add") ? " by " : " to ") + + ColorSet.emphasis + dt.debug() + ColorSet.good + " seconds!"); } } } diff --git a/src/main/java/com/denizenscript/denizen2sponge/commands/entity/HealCommand.java b/src/main/java/com/denizenscript/denizen2sponge/commands/entity/HealCommand.java new file mode 100644 index 0000000..4099a2a --- /dev/null +++ b/src/main/java/com/denizenscript/denizen2sponge/commands/entity/HealCommand.java @@ -0,0 +1,103 @@ +package com.denizenscript.denizen2sponge.commands.entity; + +import com.denizenscript.denizen2core.commands.AbstractCommand; +import com.denizenscript.denizen2core.commands.CommandEntry; +import com.denizenscript.denizen2core.commands.CommandQueue; +import com.denizenscript.denizen2core.tags.objects.NumberTag; +import com.denizenscript.denizen2core.utilities.CoreUtilities; +import com.denizenscript.denizen2core.utilities.debugging.ColorSet; +import com.denizenscript.denizen2sponge.tags.objects.EntityTag; +import org.spongepowered.api.data.key.Keys; +import org.spongepowered.api.entity.living.Living; + +public class HealCommand extends AbstractCommand { + + // <--[command] + // @Name heal + // @Arguments + // @Short changes the health of the entity. + // @Updated 2017/04/19 + // @Group Player + // @Minimum 2 + // @Maximum 2 + // @Named type (TextTag) Sets of what type the health will be. + // @Named operation (TextTag) Sets whether the command will add or set the value. + // @Description + // Changes the health of the entity. Optionally specify a type ('remaining' or 'maximum') + // to adjust the specified health type. Defaults to 'remaining'. Also specify whether + // the command will 'add' or 'set' the value. Defaults to 'add'. + // @Example + // # This example heals the player for 4 points (2 hearts) + // - heal 4 + // --> + + @Override + public String getName() { + return "heal"; + } + + @Override + public String getArguments() { + return " "; + } + + @Override + public int getMinimumArguments() { + return 2; + } + + @Override + public int getMaximumArguments() { + return 2; + } + + @Override + 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; + if (entry.namedArgs.containsKey("operation")) { + operation = CoreUtilities.toLowerCase(entry.getNamedArgumentObject(queue, "operation").toString()); + if (!(operation.equals("add") || operation.equals("set"))) { + queue.handleError(entry, "Invalid operation: '" + operation + "'!"); + return; + } + } + else { + operation = "add"; + } + 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); + break; + case "maximum": + value = ent.maxHealth().transform(operation.equals("add") ? + x -> x + amount.getInternal() : x -> amount.getInternal()).get(); + ent.offer(Keys.MAX_HEALTH, value); + break; + default: + queue.handleError(entry, "Invalid health type: '" + type + "'!"); + return; + } + } + else { + type = "remaining"; + value = ent.health().transform(operation.equals("add") ? + x -> x + amount.getInternal() : x -> amount.getInternal()).get(); + ent.offer(Keys.HEALTH, value); + } + if (queue.shouldShowGood()) { + queue.outGood(ColorSet.emphasis + (operation.equals("add") ? "Increasing" : "Setting") + + ColorSet.good + " the " + ColorSet.emphasis + type + ColorSet.good + " health of " + + ColorSet.emphasis + entityTag.debug() + ColorSet.good + (operation.equals("add") ? " by " : " to ") + + ColorSet.emphasis + amount.debug() + ColorSet.good + "!"); + } + } +} diff --git a/src/main/java/com/denizenscript/denizen2sponge/commands/player/FeedCommand.java b/src/main/java/com/denizenscript/denizen2sponge/commands/player/FeedCommand.java new file mode 100644 index 0000000..f3b7366 --- /dev/null +++ b/src/main/java/com/denizenscript/denizen2sponge/commands/player/FeedCommand.java @@ -0,0 +1,105 @@ +package com.denizenscript.denizen2sponge.commands.player; + +import com.denizenscript.denizen2core.commands.AbstractCommand; +import com.denizenscript.denizen2core.commands.CommandEntry; +import com.denizenscript.denizen2core.commands.CommandQueue; +import com.denizenscript.denizen2core.tags.objects.NumberTag; +import com.denizenscript.denizen2core.utilities.CoreUtilities; +import com.denizenscript.denizen2core.utilities.debugging.ColorSet; +import com.denizenscript.denizen2sponge.tags.objects.PlayerTag; +import org.spongepowered.api.data.key.Keys; + +public class FeedCommand extends AbstractCommand { + + // <--[command] + // @Name feed + // @Arguments + // @Short changes the food level of the player. + // @Updated 2017/04/19 + // @Group Player + // @Minimum 2 + // @Maximum 2 + // @Named type (TextTag) Sets of what type the food level will be. + // @Named operation (TextTag) Sets whether the command will add or set the value. + // @Description + // Changes the food level of the player. Optionally specify a type ('hunger', 'saturation' + // or 'exhaustion') to adjust the specified food level type. Defaults to 'hunger'. Also + // specify whether the command will 'add' or 'set' the value. Defaults to 'add'. + // @Example + // # This example completely fills the hunger bar of the player + // - feed 20 --operation set + // --> + + @Override + public String getName() { + return "feed"; + } + + @Override + public String getArguments() { + return " "; + } + + @Override + public int getMinimumArguments() { + return 2; + } + + @Override + public int getMaximumArguments() { + return 2; + } + + @Override + public void execute(CommandQueue queue, CommandEntry entry) { + PlayerTag player = PlayerTag.getFor(queue.error, entry.getArgumentObject(queue, 0)); + NumberTag amount = NumberTag.getFor(queue.error, entry.getArgumentObject(queue, 1)); + String operation; + if (entry.namedArgs.containsKey("operation")) { + operation = CoreUtilities.toLowerCase(entry.getNamedArgumentObject(queue, "operation").toString()); + if (!(operation.equals("add") || operation.equals("set"))) { + queue.handleError(entry, "Invalid operation: '" + operation + "'!"); + return; + } + } + else { + operation = "add"; + } + String type; + if (entry.namedArgs.containsKey("type")) { + type = CoreUtilities.toLowerCase(entry.getNamedArgumentObject(queue, "type").toString()); + switch (type) { + case "hunger": + int food_level = player.getInternal().foodLevel().transform(operation.equals("add") ? + x -> x + (int) amount.getInternal() : x -> (int) amount.getInternal()).get(); + player.getInternal().offer(Keys.FOOD_LEVEL, food_level); + break; + case "saturation": + double saturation = player.getInternal().saturation().transform(operation.equals("add") ? + x -> x + amount.getInternal() : x -> amount.getInternal()).get(); + player.getInternal().offer(Keys.SATURATION, saturation); + break; + case "exhaustion": + double exhaustion = player.getInternal().exhaustion().transform(operation.equals("add") ? + x -> x + amount.getInternal() : x -> amount.getInternal()).get(); + player.getInternal().offer(Keys.EXHAUSTION, exhaustion); + break; + default: + queue.handleError(entry, "Invalid food level type: '" + type + "'!"); + return; + } + } + else { + type = "hunger"; + int food_level = player.getInternal().foodLevel().transform(operation.equals("add") ? + x -> x + (int) amount.getInternal() : x -> (int) amount.getInternal()).get(); + player.getInternal().offer(Keys.FOOD_LEVEL, food_level); + } + if (queue.shouldShowGood()) { + queue.outGood(ColorSet.emphasis + (operation.equals("add") ? "Increasing" : "Setting") + + ColorSet.good + " the " + ColorSet.emphasis + type + ColorSet.good + " level of " + + ColorSet.emphasis + player.debug() + ColorSet.good + (operation.equals("add") ? " by " : " to ") + + ColorSet.emphasis + amount.debug() + ColorSet.good + "!"); + } + } +}