SpawnCommand.java 6.14 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;
Melledy's avatar
Melledy committed
7
8
9
10
import emu.grasscutter.data.excels.AvatarData;
import emu.grasscutter.data.excels.GadgetData;
import emu.grasscutter.data.excels.ItemData;
import emu.grasscutter.data.excels.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
import static emu.grasscutter.Configuration.*;
24
25
import static emu.grasscutter.utils.Language.translate;

26
@Command(label = "spawn", usage = "spawn <entityId> [amount] [level(monster only)] [<x> <y> <z>(monster only, optional)]", aliases = {"drop"}, permission = "server.spawn", permissionTargeted = "server.spawn.others", description = "commands.spawn.description")
KingRainbow44's avatar
KingRainbow44 committed
27
28
29
public final class SpawnCommand implements CommandHandler {

    @Override
AnimeGitB's avatar
AnimeGitB committed
30
    public void execute(Player sender, Player targetPlayer, List<String> args) {
AnimeGitB's avatar
AnimeGitB committed
31
32
33
        int id = 0;  // This is just to shut up the linter, it's not a real default
        int amount = 1;
        int level = 1;
34
		float x = 0, y = 0, z = 0;
AnimeGitB's avatar
AnimeGitB committed
35
        switch (args.size()) {
36
37
38
39
40
41
42
43
            case 6:
                try {
                    x = Float.parseFloat(args.get(3));
                    y = Float.parseFloat(args.get(4));
                    z = Float.parseFloat(args.get(5));
                } catch (NumberFormatException ignored) {
                    CommandHandler.sendMessage(sender, translate(sender, "commands.execution.argument_error"));
                }  // Fallthrough
AnimeGitB's avatar
AnimeGitB committed
44
45
46
47
            case 3:
                try {
                    level = Integer.parseInt(args.get(2));
                } catch (NumberFormatException ignored) {
Secretboy's avatar
Secretboy committed
48
                    CommandHandler.sendMessage(sender, translate(sender, "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) {
Secretboy's avatar
Secretboy committed
54
                    CommandHandler.sendMessage(sender, translate(sender, "commands.generic.invalid.amount"));
AnimeGitB's avatar
AnimeGitB committed
55
56
57
58
59
                }  // Fallthrough
            case 1:
                try {
                    id = Integer.parseInt(args.get(0));
                } catch (NumberFormatException ignored) {
Secretboy's avatar
Secretboy committed
60
                    CommandHandler.sendMessage(sender, translate(sender, "commands.generic.invalid.entityId"));
AnimeGitB's avatar
AnimeGitB committed
61
62
63
                }
                break;
            default:
Secretboy's avatar
Secretboy committed
64
                CommandHandler.sendMessage(sender, translate(sender, "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) {
Secretboy's avatar
Secretboy committed
72
            CommandHandler.sendMessage(sender, translate(sender, "commands.generic.invalid.entityId"));
AnimeGitB's avatar
AnimeGitB committed
73
74
            return;
        }
75
       
AnimeGitB's avatar
AnimeGitB committed
76
        Scene scene = targetPlayer.getScene();
77
78
79
80
81
82
83
84
        
        if (scene.getEntities().size() + amount > GAME_OPTIONS.sceneEntityLimit) {
        	amount = Math.max(Math.min(GAME_OPTIONS.sceneEntityLimit - scene.getEntities().size(), amount), 0);
        	CommandHandler.sendMessage(sender, translate(sender, "commands.spawn.limit_reached", amount));
        	if (amount <= 0) {
        		return;
        	}
        }
KingRainbow44's avatar
KingRainbow44 committed
85

AnimeGitB's avatar
AnimeGitB committed
86
87
88
        double maxRadius = Math.sqrt(amount * 0.2 / Math.PI);
        for (int i = 0; i < amount; i++) {
            Position pos = GetRandomPositionInCircle(targetPlayer.getPos(), maxRadius).addY(3);
89
90
91
            if(x != 0 && y != 0 && z != 0) {
                pos = GetRandomPositionInCircle(new Position(x, y, z), maxRadius).addY(3);
            }
AnimeGitB's avatar
AnimeGitB committed
92
93
94
            GameEntity entity = null;
            if (itemData != null) {
                entity = new EntityItem(scene, null, itemData, pos, 1, true);
KingRainbow44's avatar
KingRainbow44 committed
95
            }
AnimeGitB's avatar
AnimeGitB committed
96
97
98
99
100
101
102
103
104
105
106
107
108
109
            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);
110
                    }
AnimeGitB's avatar
AnimeGitB committed
111
                    default -> {}
112
                }
KingRainbow44's avatar
KingRainbow44 committed
113
            }
AnimeGitB's avatar
AnimeGitB committed
114
115
116
117
118
            if (monsterData != null) {
                entity = new EntityMonster(scene, monsterData, pos, level);
            }

            scene.addEntity(entity);
KingRainbow44's avatar
KingRainbow44 committed
119
        }
Secretboy's avatar
Secretboy committed
120
        CommandHandler.sendMessage(sender, translate(sender, "commands.spawn.success", Integer.toString(amount), Integer.toString(id)));
KingRainbow44's avatar
KingRainbow44 committed
121
    }
122
123
124
125
126
127
128
129

    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
130
}