Skip to content

Commit

Permalink
Added /damage
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedro270707 committed Jan 21, 2024
1 parent dc5b4cb commit 2df03bb
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public void init() {
SayCommand.register(DISPATCHER);
TellRawCommand.register(DISPATCHER);
TestForCommand.register(DISPATCHER);
DamageCommand.register(DISPATCHER);

if (this.isServer) {
StopCommand.register(DISPATCHER);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package net.pedroricardo.commander.content.arguments;

import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import net.minecraft.core.lang.I18n;
import net.minecraft.core.util.helper.DamageType;
import net.pedroricardo.commander.CommanderHelper;

import java.util.Arrays;
import java.util.Collection;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

public class DamageTypeArgumentType implements ArgumentType<DamageType> {
private static final Collection<String> EXAMPLES = Arrays.asList("damagetype.combat", "damagetype.blast");

public static DamageTypeArgumentType damageType() {
return new DamageTypeArgumentType();
}

@Override
public DamageType parse(StringReader reader) throws CommandSyntaxException {
final String string = reader.readString();

for (DamageType damageType : DamageType.values()) {
if (CommanderHelper.matchesKeyString(damageType.getLanguageKey(), string)) {
return damageType;
}
}
throw new CommandSyntaxException(CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownArgument(), () -> I18n.getInstance().translateKey("argument_types.commander.damage_type.invalid_damage_type"));
}

@Override
public <S> CompletableFuture<Suggestions> listSuggestions(final CommandContext<S> context, final SuggestionsBuilder builder) {
String remaining = builder.getRemainingLowerCase();
for (DamageType damageType : DamageType.values()) {
Optional<String> optional = CommanderHelper.getStringToSuggest(damageType.getLanguageKey(), remaining);
optional.ifPresent(builder::suggest);
}
return builder.buildFuture();
}

@Override
public Collection<String> getExamples() {
return EXAMPLES;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package net.pedroricardo.commander.content.commands;

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import net.minecraft.core.entity.Entity;
import net.minecraft.core.util.helper.DamageType;
import net.pedroricardo.commander.content.CommanderCommandSource;
import net.pedroricardo.commander.content.arguments.DamageTypeArgumentType;
import net.pedroricardo.commander.content.arguments.EntityArgumentType;
import net.pedroricardo.commander.content.helpers.EntitySelector;

import java.util.List;

public class DamageCommand {
public static void register(CommandDispatcher<CommanderCommandSource> dispatcher) {
dispatcher.register(LiteralArgumentBuilder.<CommanderCommandSource>literal("damage")
.then(RequiredArgumentBuilder.<CommanderCommandSource, EntitySelector>argument("entities", EntityArgumentType.entities())
.then(RequiredArgumentBuilder.<CommanderCommandSource, DamageType>argument("type", DamageTypeArgumentType.damageType())
.then(RequiredArgumentBuilder.<CommanderCommandSource, Integer>argument("amount", IntegerArgumentType.integer(0, 32768))
.executes(c -> {
List<? extends Entity> entities = c.getArgument("entities", EntitySelector.class).get(c.getSource());
DamageType type = c.getArgument("type", DamageType.class);
int amount = c.getArgument("amount", Integer.class);
int entitiesAffected = 0;
for (Entity entity : entities) {
if (entity.hurt(null, amount, type)) ++entitiesAffected;
}
return entitiesAffected;
})))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static void register(CommandDispatcher<CommanderCommandSource> dispatcher
CommanderCommandSource source = c.getSource();
String message = StringArgumentType.getString(c, "message");
String senderName = source.getName();
source.sendMessageToAllPlayers("[" + senderName + "§r]" + message);
source.sendMessageToAllPlayers("[" + senderName + "§r] " + message);
return Command.SINGLE_SUCCESS;
})));
}
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/lang/commander/en_US.lang
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ argument_types.commander.block.invalid_block=Invalid block ID
argument_types.commander.item.invalid_item=Invalid item ID
argument_types.commander.dimension.invalid_dimension=Invalid dimension ID
argument_types.commander.biome.invalid_biome=Invalid biome ID
argument_types.commander.damage_type.invalid_damage_type=Invalid damage type ID
argument_types.commander.gamerule.invalid_value=Invalid game rule value
argument_types.commander.entity.invalid_selector.single_player_only=Selector includes more than one entity, but command only accepts a single player as an argument
argument_types.commander.entity.invalid_selector.single_entity_only=Selector includes more than one entity, but command only accepts a single entity as an argument
Expand Down

0 comments on commit 2df03bb

Please sign in to comment.