Skip to content

Commit

Permalink
feat: heal command
Browse files Browse the repository at this point in the history
  • Loading branch information
CallMeEchoCodes committed Oct 2, 2024
1 parent e258118 commit b60eced
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
1 change: 0 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ subprojects {
}
}


val remapMavenJar by tasks.registering(RemapJarTask::class) {
inputFile.set(tasks.jar.flatMap { it.archiveFile })
archiveFileName.set("${project.properties["archivesBaseName"]}-${project.version}-maven.jar")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.spiritstudios.specter.impl.debug;

import dev.spiritstudios.specter.impl.debug.command.HealCommand;
import dev.spiritstudios.specter.impl.debug.command.MetatagCommand;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
Expand All @@ -9,6 +10,7 @@ public class SpecterDebug implements ModInitializer {
public void onInitialize() {
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> {
MetatagCommand.register(dispatcher);
HealCommand.register(dispatcher);
});
}
}
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
"item.specter-debug.loot_loader.invalid_loot_table": "Loot Table \"%s\" either does not exist or is invalid.",
"item.specter-debug.loot_loader.loaded_loot_table": "Loot Table \"%s\" loaded successfully.",
"item_group.specter.debug": "Specter Debug",
"item.specter-debug.loot_loader": "Loot Loader"
"item.specter-debug.loot_loader": "Loot Loader",
"commands.heal.not_living": "Cannot heal non-living entity",
"commands.heal.success": "Healed %s for %s health"
}

0 comments on commit b60eced

Please sign in to comment.