Skip to content

Commit

Permalink
feat: add /kill command
Browse files Browse the repository at this point in the history
  • Loading branch information
smartcmd committed Oct 16, 2024
1 parent 216e93a commit 70677f1
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.allaymc.api.entity.component.attribute;

import org.allaymc.api.entity.component.EntityBaseComponent;
import org.allaymc.api.entity.component.EntityComponent;
import org.cloudburstmc.nbt.NbtMap;

Expand Down Expand Up @@ -134,6 +135,9 @@ default void resetHealth() {

/**
* Kill the entity.
* <p>
* Compared to {@link EntityBaseComponent#despawn()} method, this method will set the health
* of this entity to zero, rather than remove the entity directly.
*/
default void kill() {
setHealth(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.allaymc.api.item.type.ItemType;
import org.allaymc.api.item.type.ItemTypes;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

Expand All @@ -36,8 +35,13 @@ public void prepareCommandTree(CommandTree tree) {
.intNum("maxCount", -1)
.optional()
.exec((context, sender) -> {
var targets = new ArrayList<EntityPlayer>();
targets.addAll(context.getResult(0) != null ? context.getResult(0) : List.of(sender));
List<EntityPlayer> targets = context.getResult(0);
if (targets != null && targets.isEmpty()) {
context.addNoTargetMatchError();
return context.fail();
} else if (targets == null) {
targets = List.of(sender);
}

ItemType<?> itemType = context.getResult(1);
int data = context.getResult(2);
Expand All @@ -46,6 +50,8 @@ public void prepareCommandTree(CommandTree tree) {
maxCount = Integer.MAX_VALUE;
}

boolean success = true;
int status = 0;
for (var target : targets) {
var containers = Stream.of(FullContainerType.PLAYER_INVENTORY, FullContainerType.OFFHAND, FullContainerType.ARMOR).map(target::getContainer).toList();
if (maxCount == 0) {
Expand All @@ -58,7 +64,7 @@ public void prepareCommandTree(CommandTree tree) {
.sum())
.sum();
context.addOutput(TrKeys.M_COMMANDS_CLEAR_TESTING, target.getOriginName(), count);
return context.success(count);
status = count;
} else {
int c = maxCount;
for (var container : containers) {
Expand All @@ -81,13 +87,14 @@ public void prepareCommandTree(CommandTree tree) {
if (maxCount != c) {
context.addOutput(TrKeys.M_COMMANDS_CLEAR_SUCCESS, target.getOriginName(), maxCount - c);
} else {
context.addOutput(TrKeys.M_COMMANDS_CLEAR_FAILURE_NO_ITEMS, target.getOriginName());
context.addError("%" + TrKeys.M_COMMANDS_CLEAR_FAILURE_NO_ITEMS, target.getOriginName());
success = false;
}
}
}
return context.success();
return success ? context.success(status) : context.fail();
}, SenderType.PLAYER);
}


}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package org.allaymc.server.command.defaults;

import org.allaymc.api.command.SenderType;
import org.allaymc.api.command.SimpleCommand;
import org.allaymc.api.command.tree.CommandTree;
import org.allaymc.api.entity.Entity;
import org.allaymc.api.entity.component.attribute.EntityAttributeComponent;
import org.allaymc.api.entity.interfaces.EntityPlayer;
import org.allaymc.api.i18n.TrKeys;
import org.cloudburstmc.protocol.bedrock.data.GameType;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author daoge_cmd
*/
public class KillCommand extends SimpleCommand {

public KillCommand() {
super("kill", TrKeys.M_COMMANDS_KILL_DESCRIPTION);
}

@Override
public void prepareCommandTree(CommandTree tree) {
tree.getRoot()
// Set default value to null to
// mark that the sender want to suicide
.target("targets", null)
.optional()
.exec((context, sender) -> {
List<Entity> targets = context.getResult(0);
if (targets == null) {
targets = List.of(sender);
}

if (targets.stream().allMatch(target -> target instanceof EntityPlayer player && player.getGameType() == GameType.CREATIVE)) {
context.addError("%" + TrKeys.M_COMMANDS_KILL_ATTEMPTKILLPLAYERCREATIVE);
return context.fail();
}

Map<String, Integer> killedEntities = new HashMap<>();
for (var target : targets) {
if (target instanceof EntityPlayer player && (player.getGameType() == GameType.CREATIVE || player.getGameType() == GameType.SPECTATOR)) {
continue;
}
if (target instanceof EntityAttributeComponent damageComponent) {
damageComponent.kill();
} else {
target.despawn();
}
killedEntities.compute(target.getCommandSenderName(), (k ,v) -> v != null ? ++v : 1);
}
context.addOutput(TrKeys.M_COMMANDS_KILL_SUCCESSFUL, killedEntities.entrySet().stream().map(entry -> entry.getKey() + " * " + entry.getValue()).collect(Collectors.joining(", ")));
return context.success();
}, SenderType.ENTITY);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ protected void tickEffects() {
}

protected void checkDead() {
// TODO: move these code to EntityAttributeComponentImpl
if (attributeComponent == null || !attributeComponent.supportHealth()) return;
if (attributeComponent.getHealth() == 0 && !dead) {
onDie();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -640,11 +640,6 @@ public void sendTr(String key, boolean forceTranslatedByClient, String... args)
} else sendText(I18n.get().tr(thisPlayer.getLangCode(), key, args));
}

@Override
public String getCommandSenderName() {
return thisPlayer.getDisplayName();
}

@Override
public void applyEntityEvent(EntityEventType event, int data) {
var packet = new EntityEventPacket();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ private void registerDefaultCommands() {
register(new SetBlockCommand());
register(new WeatherCommand());
register(new ClearCommand());
register(new KillCommand());
}

@Override
Expand Down

0 comments on commit 70677f1

Please sign in to comment.