-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e258118
commit b60eced
Showing
4 changed files
with
54 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
specter-debug/src/main/java/dev/spiritstudios/specter/impl/debug/command/HealCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package dev.spiritstudios.specter.impl.debug.command; | ||
|
||
import com.mojang.brigadier.Command; | ||
import com.mojang.brigadier.CommandDispatcher; | ||
import com.mojang.brigadier.arguments.FloatArgumentType; | ||
import com.mojang.brigadier.exceptions.CommandSyntaxException; | ||
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType; | ||
import net.minecraft.command.argument.EntityArgumentType; | ||
import net.minecraft.entity.Entity; | ||
import net.minecraft.entity.LivingEntity; | ||
import net.minecraft.server.command.CommandManager; | ||
import net.minecraft.server.command.ServerCommandSource; | ||
import net.minecraft.text.Text; | ||
|
||
public class HealCommand { | ||
private static final SimpleCommandExceptionType NOT_LIVING_EXCEPTION = new SimpleCommandExceptionType(Text.translatable("commands.heal.not_living")); | ||
|
||
public static void register(CommandDispatcher<ServerCommandSource> dispatcher) { | ||
dispatcher.register(CommandManager.literal("heal") | ||
.requires(source -> source.hasPermissionLevel(2)) | ||
.executes(context -> fullHeal(context.getSource().getEntityOrThrow(), context.getSource())) | ||
.then(CommandManager.argument("target", EntityArgumentType.entity()) | ||
.executes(context -> fullHeal(EntityArgumentType.getEntity(context, "target"), context.getSource())) | ||
.then(CommandManager.argument("amount", FloatArgumentType.floatArg(0)) | ||
.executes(context -> heal(EntityArgumentType.getEntity(context, "target"), FloatArgumentType.getFloat(context, "amount"), context.getSource())) | ||
) | ||
) | ||
); | ||
} | ||
|
||
private static int fullHeal(Entity entity, ServerCommandSource source) throws CommandSyntaxException { | ||
if (!(entity instanceof LivingEntity livingEntity)) | ||
throw NOT_LIVING_EXCEPTION.create(); | ||
|
||
return heal(entity, livingEntity.getMaxHealth(), source); | ||
} | ||
|
||
private static int heal(Entity entity, float amount, ServerCommandSource source) throws CommandSyntaxException { | ||
if (!(entity instanceof LivingEntity livingEntity)) | ||
throw NOT_LIVING_EXCEPTION.create(); | ||
|
||
float oldHealth = livingEntity.getHealth(); | ||
livingEntity.heal(amount); | ||
|
||
source.sendFeedback(() -> Text.translatable("commands.heal.success", livingEntity.getDisplayName(), livingEntity.getHealth() - oldHealth), true); | ||
|
||
return Command.SINGLE_SUCCESS; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters