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

Commit

Permalink
Feed and Heal Commands
Browse files Browse the repository at this point in the history
Also improved air command.
  • Loading branch information
Xenmai committed Apr 19, 2017
1 parent a16a574 commit 5d88b0b
Show file tree
Hide file tree
Showing 4 changed files with 246 additions and 22 deletions.
Expand Up @@ -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());
Expand All @@ -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());
Expand Down
Expand Up @@ -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 <entity> <duration>
// @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 <player> <player.max_air>
// - air <player> <player.max_air> --operation set
// -->

@Override
Expand All @@ -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 + "'!");
Expand All @@ -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!");
}
}
}
@@ -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 <entity> <amount>
// @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 <player> 4
// -->

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

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

@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 + "!");
}
}
}
@@ -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 <player> <amount>
// @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 <player> 20 --operation set
// -->

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

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

@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 + "!");
}
}
}

0 comments on commit 5d88b0b

Please sign in to comment.