Skip to content

Commit ca5d574

Browse files
committed
Add per-user gadget setting
1 parent 3be6bfe commit ca5d574

File tree

6 files changed

+219
-146
lines changed

6 files changed

+219
-146
lines changed
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
package thorny.grasscutters.AttackModifier;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import emu.grasscutter.Grasscutter;
7+
import emu.grasscutter.command.CommandHandler;
8+
import emu.grasscutter.data.excels.AvatarSkillDepotData;
9+
import emu.grasscutter.game.avatar.Avatar;
10+
import emu.grasscutter.game.entity.EntityGadget;
11+
import emu.grasscutter.game.player.Player;
12+
import emu.grasscutter.game.world.Scene;
13+
import emu.grasscutter.net.proto.VisionTypeOuterClass.VisionType;
14+
import emu.grasscutter.server.game.GameSession;
15+
import emu.grasscutter.server.packet.send.PacketSceneEntityDisappearNotify;
16+
import emu.grasscutter.utils.Position;
17+
import thorny.grasscutters.AttackModifier.commands.AttackModifierCommand;
18+
import thorny.grasscutters.AttackModifier.utils.Config;
19+
import thorny.grasscutters.AttackModifier.utils.Config.characters;
20+
21+
public class AddAttack {
22+
23+
static ArrayList<Integer> blacklistUIDs = AttackModifier.getInstance().config.getBlacklist();
24+
static List<EntityGadget> activeGadgets = new ArrayList<>(); // Current gadgets
25+
static List<EntityGadget> removeGadgets = new ArrayList<>(); // To be removed gadgets
26+
27+
public static void addAttack(GameSession session, int skillId, int uid) {
28+
29+
if (!(blacklistUIDs.contains(uid))) {
30+
int fileUid = AttackModifier.getInstance().config.getGadgetConfigUid();
31+
32+
if (!(fileUid == uid)) {
33+
AttackModifier.getInstance().config.loadGadgetConfig(uid);
34+
}
35+
int addedAttack = 0; // Default of no gadget
36+
int usedAttack = -1; // Default of no attack
37+
38+
// Get avatar info
39+
Avatar avatar = session.getPlayer().getTeamManager().getCurrentAvatarEntity().getAvatar();
40+
AvatarSkillDepotData skillDepot = avatar.getSkillDepot();
41+
42+
// Check what skill type was used
43+
if (skillId == (skillDepot.getSkills().get(0))) {
44+
usedAttack = 0;
45+
}
46+
if (skillId == (skillDepot.getSkills().get(1))) {
47+
usedAttack = 1;
48+
}
49+
if (skillId == (skillDepot.getEnergySkill())) {
50+
usedAttack = 2;
51+
}
52+
53+
// Get current avatar name
54+
String curName = avatar.getAvatarData().getName().toLowerCase() + "Ids";
55+
characters currentAvatar = null;
56+
57+
// Get avatar from config
58+
try {
59+
currentAvatar = AddAttack.getCharacter.getCurrent(curName);
60+
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException
61+
| IllegalAccessException e) {
62+
// Should only be called when config is missing entry for the active character
63+
Grasscutter.getLogger().info("Invalid or missing config for: " + curName);
64+
e.printStackTrace();
65+
return;
66+
}
67+
68+
// Universal switch
69+
switch (usedAttack) {
70+
default -> addedAttack = 0;
71+
case 0 -> addedAttack = currentAvatar.skill.normalAtk; // Normal attack
72+
case 1 -> addedAttack = currentAvatar.skill.elementalSkill; // Elemental skill
73+
case 2 -> addedAttack = currentAvatar.skill.elementalBurst; // Burst
74+
}
75+
76+
// Get position
77+
var scene = session.getPlayer().getScene();
78+
Position pos = new Position(session.getPlayer().getPosition());
79+
Position rot = new Position(session.getPlayer().getRotation());
80+
var r = 3;
81+
82+
// Try to set position in front of player to not get hit
83+
double angle = rot.getY();
84+
Position target = new Position(pos);
85+
86+
// Only change gadget pos for basic attack
87+
if (usedAttack == 0) {
88+
target.addX((float) (r * Math.sin(Math.PI / 180 * angle)));
89+
target.addZ((float) (r * Math.cos(Math.PI / 180 * angle)));
90+
}
91+
92+
// Only spawn on match
93+
if (addedAttack != 0) {
94+
EntityGadget att = new EntityGadget(scene, addedAttack, target, rot);
95+
96+
// Silly way to track gadget alive time
97+
int currTime = (int) (System.currentTimeMillis() - 1665393100);
98+
att.setGroupId(currTime);
99+
100+
activeGadgets.add(att);
101+
102+
// Try to make it not hurt self
103+
scene.addEntity(att);
104+
// att.setFightProperty(2001, 0);
105+
// att.setFightProperty(1, 0);
106+
107+
}
108+
// Remove all gadgets when list not empty
109+
if (!activeGadgets.isEmpty()) {
110+
removeGadgets(scene);
111+
} // if
112+
} // if toAdd
113+
} // addAttack
114+
115+
public static void removeGadgets(Scene scene) {
116+
for (EntityGadget gadget : activeGadgets) {
117+
118+
// When gadgets have lived for 15 sec
119+
if (AttackModifierCommand.userCalled
120+
|| (int) (System.currentTimeMillis() - 1665393100) > (gadget.getGroupId() + 15000)) {
121+
// Add to removal list
122+
removeGadgets.add(gadget);
123+
124+
// Remove entity
125+
scene.removeEntity(gadget, VisionType.VISION_TYPE_REMOVE);
126+
scene.broadcastPacket(new PacketSceneEntityDisappearNotify(gadget, VisionType.VISION_TYPE_REMOVE));
127+
} // if
128+
} // for
129+
// Remove gadgets and clean list
130+
activeGadgets.removeAll(removeGadgets);
131+
removeGadgets.clear();
132+
AttackModifierCommand.userCalled = false;
133+
} // removeGadgets
134+
135+
public static class getCharacter {
136+
public static characters getCurrent(String curName)
137+
throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
138+
characters curr = new characters();
139+
var me = AttackModifier.getInstance().config.getGadgetConfig().getClass().getDeclaredField(curName);
140+
me.setAccessible(true);
141+
curr = (characters) me.get(AttackModifier.getInstance().config.getGadgetConfig());
142+
return curr;
143+
}
144+
}
145+
146+
public static void setGadget(Player targetPlayer, String avatarName, int uid, String attackType, int newGadget) {
147+
characters avatarToChange = null;
148+
Config gadgetConfig = AttackModifier.getInstance().config.getGadgetConfig();
149+
try {
150+
avatarToChange = AddAttack.getCharacter.getCurrent(avatarName);
151+
CommandHandler.sendMessage(targetPlayer, "Setting " + attackType + " to " + newGadget);
152+
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
153+
CommandHandler.sendMessage(targetPlayer, "Failed to set gadget! Change in plugins/AttackModifier/config.json");
154+
}
155+
switch (attackType) {
156+
default -> CommandHandler.sendMessage(targetPlayer, "/at set n|e|q [gadgetId]");
157+
case "n" -> avatarToChange.skill.normalAtk = newGadget; // Normal attack
158+
case "e" -> avatarToChange.skill.elementalSkill = newGadget; // Elemental skill
159+
case "q" -> avatarToChange.skill.elementalBurst = newGadget; // Burst
160+
}
161+
AttackModifier.getInstance().saveGadgetConfig(gadgetConfig, uid);
162+
}
163+
}

src/main/java/thorny/grasscutters/AttackModifier/AttackModifier.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ public void reloadConfig() {
5252
config.loadConfig();
5353
}
5454

55+
public void saveGadgetConfig(Config updated, int uid){
56+
config.saveGadgetList(updated, uid);
57+
config.loadGadgetConfig(uid);
58+
}
59+
5560
public void saveBlacklist(ArrayList<Integer> blacklistUIDs){
5661
config.saveBlacklist(blacklistUIDs);
5762
config.loadBlacklist();

src/main/java/thorny/grasscutters/AttackModifier/EventListener.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import emu.grasscutter.server.event.EventHandler;
88
import emu.grasscutter.server.event.HandlerPriority;
99
import emu.grasscutter.server.event.game.ReceivePacketEvent;
10-
import thorny.grasscutters.AttackModifier.commands.AttackModifierCommand;
1110

1211
/**
1312
* A class containing all event handlers.
@@ -36,7 +35,7 @@ public static void onPacket(ReceivePacketEvent event) {
3635
int uuid = session.getPlayer().getUid();
3736

3837
// Send to addAttack
39-
AttackModifierCommand.addAttack(session, skillId, uuid);
38+
AddAttack.addAttack(session, skillId, uuid);
4039
}
4140
}
4241
}
Lines changed: 6 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,22 @@
11
package thorny.grasscutters.AttackModifier.commands;
22

3-
import emu.grasscutter.Grasscutter;
43
import emu.grasscutter.command.Command;
54
import emu.grasscutter.command.CommandHandler;
6-
import emu.grasscutter.game.avatar.Avatar;
75
import emu.grasscutter.game.entity.EntityGadget;
86
import emu.grasscutter.game.player.Player;
9-
import emu.grasscutter.game.world.Scene;
10-
import emu.grasscutter.net.proto.VisionTypeOuterClass.VisionType;
11-
import emu.grasscutter.server.game.GameSession;
12-
import emu.grasscutter.server.packet.send.PacketSceneEntityDisappearNotify;
13-
import emu.grasscutter.utils.Position;
147
import emu.grasscutter.command.Command.TargetRequirement;
15-
import emu.grasscutter.data.excels.AvatarSkillDepotData;
8+
import thorny.grasscutters.AttackModifier.AddAttack;
169
import thorny.grasscutters.AttackModifier.AttackModifier;
1710
import thorny.grasscutters.AttackModifier.utils.*;
18-
import thorny.grasscutters.AttackModifier.utils.Config.characters;
1911

2012
import java.util.ArrayList;
2113
import java.util.List;
2214

2315
// Command usage
2416
@Command(label = "attack", aliases = "at", usage = "on|off|remove|reload \n set n|e|q [gadgetId]", targetRequirement = TargetRequirement.PLAYER)
2517
public class AttackModifierCommand implements CommandHandler {
26-
private static final Config config = AttackModifier.getInstance().config.getConfig();
2718
static ArrayList<Integer> blacklistUIDs = AttackModifier.getInstance().config.getBlacklist();
28-
29-
static List<EntityGadget> activeGadgets = new ArrayList<>(); // Current gadgets
30-
static List<EntityGadget> removeGadgets = new ArrayList<>(); // To be removed gadgets
19+
public static final Config gadgetConfig = AttackModifier.getInstance().config.getGadgetConfig();
3120

3221
public static boolean toAdd = true; // Default state to add attacks
3322
public static boolean userCalled = false; // Whether removeGadget was called by the user
@@ -46,6 +35,7 @@ public void execute(Player sender, Player targetPlayer, List<String> args) {
4635
var pos = targetPlayer.getPosition();
4736
var rot = targetPlayer.getRotation();
4837
int thing = 0;
38+
int newGadget = -1;
4939
String state;
5040
String avatarName = targetPlayer.getTeamManager().getCurrentAvatarEntity().getAvatar().getAvatarData().getName().toLowerCase() + "Ids";
5141
int uid = targetPlayer.getUid();
@@ -75,7 +65,7 @@ public void execute(Player sender, Player targetPlayer, List<String> args) {
7565

7666
if (state.equals("remove")) {
7767
userCalled = true;
78-
removeGadgets(scene);
68+
AddAttack.removeGadgets(scene);
7969
CommandHandler.sendMessage(targetPlayer, "Removed all active gadgets!");
8070
}
8171
if (state.equals("reload")) {
@@ -85,139 +75,13 @@ public void execute(Player sender, Player targetPlayer, List<String> args) {
8575
}
8676
if (state.equals("set")){
8777
var attackType = args.get(1).toLowerCase();
88-
int newGadget = Integer.parseInt(args.get(2));
89-
characters avatarToChange = null;
90-
try {
91-
avatarToChange = getCharacter.getCurrent(avatarName);
92-
CommandHandler.sendMessage(targetPlayer, "Setting " + attackType + " to " + newGadget);
93-
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
94-
CommandHandler.sendMessage(targetPlayer, "Failed to set gadget! Change in plugins/AttackModifier/config.json");
95-
}
96-
switch (attackType) {
97-
default -> CommandHandler.sendMessage(targetPlayer, "/at set n|e|q [gadgetId]");
98-
case "n" -> avatarToChange.skill.normalAtk = newGadget; // Normal attack
99-
case "e" -> avatarToChange.skill.elementalSkill = newGadget; // Elemental skill
100-
case "q" -> avatarToChange.skill.elementalBurst = newGadget; // Burst
101-
}
102-
AttackModifier.getInstance().reloadConfig();
78+
try{newGadget = Integer.parseInt(args.get(2));}catch(Exception e){sendUsageMessage(targetPlayer); return;}
79+
AddAttack.setGadget(targetPlayer, avatarName, uid, attackType, newGadget);
10380
CommandHandler.sendMessage(targetPlayer, "Set new gadget!");
10481
}
10582

10683
EntityGadget entity = new EntityGadget(scene, thing, pos, rot);
10784
scene.addEntity(entity);
10885

10986
}
110-
111-
public static void addAttack(GameSession session, int skillId, int uid) {
112-
113-
if (!(blacklistUIDs.contains(uid))) {
114-
115-
int addedAttack = 0; // Default of no gadget
116-
int usedAttack = -1; // Default of no attack
117-
118-
// Get avatar info
119-
Avatar avatar = session.getPlayer().getTeamManager().getCurrentAvatarEntity().getAvatar();
120-
AvatarSkillDepotData skillDepot = avatar.getSkillDepot();
121-
122-
// Check what skill type was used
123-
if (skillId == (skillDepot.getSkills().get(0))) {
124-
usedAttack = 0;
125-
}
126-
if (skillId == (skillDepot.getSkills().get(1))) {
127-
usedAttack = 1;
128-
}
129-
if (skillId == (skillDepot.getEnergySkill())) {
130-
usedAttack = 2;
131-
}
132-
133-
// Get current avatar name
134-
String curName = avatar.getAvatarData().getName().toLowerCase() + "Ids";
135-
characters currentAvatar = null;
136-
137-
// Get avatar from config
138-
try {
139-
currentAvatar = getCharacter.getCurrent(curName);
140-
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
141-
// Should only be called when config is missing entry for the active character
142-
Grasscutter.getLogger().info("Invalid or missing config for: " + curName);
143-
e.printStackTrace();
144-
return;
145-
}
146-
147-
// Universal switch
148-
switch (usedAttack) {
149-
default -> addedAttack = 0;
150-
case 0 -> addedAttack = currentAvatar.skill.normalAtk; // Normal attack
151-
case 1 -> addedAttack = currentAvatar.skill.elementalSkill; // Elemental skill
152-
case 2 -> addedAttack = currentAvatar.skill.elementalBurst; // Burst
153-
}
154-
155-
// Get position
156-
var scene = session.getPlayer().getScene();
157-
Position pos = new Position(session.getPlayer().getPosition());
158-
Position rot = new Position(session.getPlayer().getRotation());
159-
var r = 3;
160-
161-
// Try to set position in front of player to not get hit
162-
double angle = rot.getY();
163-
Position target = new Position(pos);
164-
165-
// Only change gadget pos for basic attack
166-
if (usedAttack == 0) {
167-
target.addX((float) (r * Math.sin(Math.PI / 180 * angle)));
168-
target.addZ((float) (r * Math.cos(Math.PI / 180 * angle)));
169-
}
170-
171-
// Only spawn on match
172-
if (addedAttack != 0) {
173-
EntityGadget att = new EntityGadget(scene, addedAttack, target, rot);
174-
175-
// Silly way to track gadget alive time
176-
int currTime = (int) (System.currentTimeMillis() - 1665393100);
177-
att.setGroupId(currTime);
178-
179-
activeGadgets.add(att);
180-
181-
// Try to make it not hurt self
182-
scene.addEntity(att);
183-
att.setFightProperty(2001, 0);
184-
att.setFightProperty(1, 0);
185-
186-
}
187-
// Remove all gadgets when list not empty
188-
if (!activeGadgets.isEmpty()) {
189-
removeGadgets(scene);
190-
} // if
191-
} // if toAdd
192-
} // addAttack
193-
194-
private static void removeGadgets(Scene scene) {
195-
for (EntityGadget gadget : activeGadgets) {
196-
197-
// When gadgets have lived for 15 sec
198-
if (userCalled || (int) (System.currentTimeMillis() - 1665393100) > (gadget.getGroupId() + 15000)) {
199-
// Add to removal list
200-
removeGadgets.add(gadget);
201-
202-
// Remove entity
203-
scene.removeEntity(gadget, VisionType.VISION_TYPE_REMOVE);
204-
scene.broadcastPacket(new PacketSceneEntityDisappearNotify(gadget, VisionType.VISION_TYPE_REMOVE));
205-
} // if
206-
} // for
207-
// Remove gadgets and clean list
208-
activeGadgets.removeAll(removeGadgets);
209-
removeGadgets.clear();
210-
userCalled = false;
211-
} // removeGadgets
212-
213-
public static class getCharacter {
214-
public static characters getCurrent(String curName)
215-
throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
216-
characters curr = new characters();
217-
var me = config.getClass().getDeclaredField(curName);
218-
me.setAccessible(true);
219-
curr = (characters) me.get(config);
220-
return curr;
221-
}
222-
}
22387
} // AttackModifierCommand

0 commit comments

Comments
 (0)