SpawnCommand.java 5.19 KB
Newer Older
KingRainbow44's avatar
KingRainbow44 committed
1
2
package emu.grasscutter.command.commands;

方块君's avatar
方块君 committed
3
import emu.grasscutter.Grasscutter;
KingRainbow44's avatar
KingRainbow44 committed
4
5
import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
6
import emu.grasscutter.data.GameData;
7
8
9
import emu.grasscutter.data.def.AvatarData;
import emu.grasscutter.data.def.GadgetData;
import emu.grasscutter.data.def.ItemData;
KingRainbow44's avatar
KingRainbow44 committed
10
import emu.grasscutter.data.def.MonsterData;
11
12
import emu.grasscutter.game.avatar.Avatar;
import emu.grasscutter.game.entity.*;
Melledy's avatar
Melledy committed
13
import emu.grasscutter.game.player.Player;
14
15
import emu.grasscutter.game.props.EntityType;
import emu.grasscutter.game.props.FightProperty;
KingRainbow44's avatar
KingRainbow44 committed
16
import emu.grasscutter.utils.Position;
AnimeGitB's avatar
AnimeGitB committed
17
import emu.grasscutter.game.world.Scene;
KingRainbow44's avatar
KingRainbow44 committed
18

19
import javax.swing.text.html.parser.Entity;
KingRainbow44's avatar
KingRainbow44 committed
20
import java.util.List;
21
import java.util.Random;
KingRainbow44's avatar
KingRainbow44 committed
22

23
24
import static emu.grasscutter.utils.Language.translate;

25
@Command(label = "spawn", usage = "spawn <entityId> [amount] [level(monster only)]", permission = "server.spawn")
KingRainbow44's avatar
KingRainbow44 committed
26
27
public final class SpawnCommand implements CommandHandler {

28
29
30
31
32
    @Override
    public String description() {
        return translate("commands.spawn.description");
    }

KingRainbow44's avatar
KingRainbow44 committed
33
    @Override
AnimeGitB's avatar
AnimeGitB committed
34
    public void execute(Player sender, Player targetPlayer, List<String> args) {
AnimeGitB's avatar
AnimeGitB committed
35
        if (targetPlayer == null) {
36
            CommandHandler.sendMessage(sender, translate("commands.execution.need_target"));
KingRainbow44's avatar
KingRainbow44 committed
37
38
39
            return;
        }

AnimeGitB's avatar
AnimeGitB committed
40
41
42
43
44
45
46
47
        int id = 0;  // This is just to shut up the linter, it's not a real default
        int amount = 1;
        int level = 1;
        switch (args.size()) {
            case 3:
                try {
                    level = Integer.parseInt(args.get(2));
                } catch (NumberFormatException ignored) {
48
                    CommandHandler.sendMessage(sender, translate("commands.execution.argument_error"));
AnimeGitB's avatar
AnimeGitB committed
49
50
51
52
53
                }  // Fallthrough
            case 2:
                try {
                    amount = Integer.parseInt(args.get(1));
                } catch (NumberFormatException ignored) {
54
                    CommandHandler.sendMessage(sender, translate("commands.generic.error.amount"));
AnimeGitB's avatar
AnimeGitB committed
55
56
57
58
59
                }  // Fallthrough
            case 1:
                try {
                    id = Integer.parseInt(args.get(0));
                } catch (NumberFormatException ignored) {
60
                    CommandHandler.sendMessage(sender, translate("commands.generic.error.entityId"));
AnimeGitB's avatar
AnimeGitB committed
61
62
63
                }
                break;
            default:
64
                CommandHandler.sendMessage(sender, translate("commands.spawn.usage"));
AnimeGitB's avatar
AnimeGitB committed
65
                return;
KingRainbow44's avatar
KingRainbow44 committed
66
67
        }

AnimeGitB's avatar
AnimeGitB committed
68
69
70
71
        MonsterData monsterData = GameData.getMonsterDataMap().get(id);
        GadgetData gadgetData = GameData.getGadgetDataMap().get(id);
        ItemData itemData = GameData.getItemDataMap().get(id);
        if (monsterData == null && gadgetData == null && itemData == null) {
72
            CommandHandler.sendMessage(sender, translate("commands.generic.error.entityId"));
AnimeGitB's avatar
AnimeGitB committed
73
74
75
            return;
        }
        Scene scene = targetPlayer.getScene();
KingRainbow44's avatar
KingRainbow44 committed
76

AnimeGitB's avatar
AnimeGitB committed
77
78
79
80
81
82
        double maxRadius = Math.sqrt(amount * 0.2 / Math.PI);
        for (int i = 0; i < amount; i++) {
            Position pos = GetRandomPositionInCircle(targetPlayer.getPos(), maxRadius).addY(3);
            GameEntity entity = null;
            if (itemData != null) {
                entity = new EntityItem(scene, null, itemData, pos, 1, true);
KingRainbow44's avatar
KingRainbow44 committed
83
            }
AnimeGitB's avatar
AnimeGitB committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
            if (gadgetData != null) {
                entity = new EntityVehicle(scene, targetPlayer.getSession().getPlayer(), gadgetData.getId(), 0, pos, targetPlayer.getRotation());  // TODO: does targetPlayer.getSession().getPlayer() have some meaning?
                int gadgetId = gadgetData.getId();
                switch (gadgetId) {
                    // TODO: Not hardcode this. Waverider (skiff)
                    case 45001001, 45001002 -> {
                        entity.addFightProperty(FightProperty.FIGHT_PROP_BASE_HP, 10000);
                        entity.addFightProperty(FightProperty.FIGHT_PROP_BASE_ATTACK, 100);
                        entity.addFightProperty(FightProperty.FIGHT_PROP_CUR_ATTACK, 100);
                        entity.addFightProperty(FightProperty.FIGHT_PROP_CUR_HP, 10000);
                        entity.addFightProperty(FightProperty.FIGHT_PROP_CUR_DEFENSE, 0);
                        entity.addFightProperty(FightProperty.FIGHT_PROP_CUR_SPEED, 0);
                        entity.addFightProperty(FightProperty.FIGHT_PROP_CHARGE_EFFICIENCY, 0);
                        entity.addFightProperty(FightProperty.FIGHT_PROP_MAX_HP, 10000);
98
                    }
AnimeGitB's avatar
AnimeGitB committed
99
                    default -> {}
100
                }
KingRainbow44's avatar
KingRainbow44 committed
101
            }
AnimeGitB's avatar
AnimeGitB committed
102
103
104
105
106
            if (monsterData != null) {
                entity = new EntityMonster(scene, monsterData, pos, level);
            }

            scene.addEntity(entity);
KingRainbow44's avatar
KingRainbow44 committed
107
        }
108
        CommandHandler.sendMessage(sender, translate("commands.spawn.success", Integer.toString(amount), Integer.toString(id)));
KingRainbow44's avatar
KingRainbow44 committed
109
    }
110
111
112
113
114
115
116
117

    private Position GetRandomPositionInCircle(Position origin, double radius){
        Position target = origin.clone();
        double angle = Math.random() * 360;
        double r = Math.sqrt(Math.random() * radius * radius);
        target.addX((float) (r * Math.cos(angle))).addZ((float) (r * Math.sin(angle)));
        return target;
    }
KingRainbow44's avatar
KingRainbow44 committed
118
}