Skip to content

Commit 8b2bcfb

Browse files
committed
Fix NeoForge & Sponge commands
1 parent fc9567d commit 8b2bcfb

2 files changed

Lines changed: 76 additions & 64 deletions

File tree

‎versions/neoforge/java/pl/olafcio/avoid_impl/mixin/CommandsMixin.fp-java‎

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,48 @@ import com.mojang.brigadier.context.CommandContext;
22
import java.lang.reflect.Field;
33

44
{
5-
var ictx = (ICommandContext<CommandSourceStack>) ctx;
6-
7-
// == replacement == //
8-
9-
Field F_arguments;
10-
11-
try {
12-
F_arguments = CommandContext.class.getDeclaredField("arguments");
13-
F_arguments.setAccessible(true);
14-
} catch (NoSuchFieldException e) {
15-
throw new RuntimeException("AvoidLib failed to reflectively retrieve CommandContext#arguments", e);
16-
}
5+
public class CommandsMixin {
6+
public class CommandsMixin {
7+
private static Field _getArgField() {
8+
Field F_arguments;
9+
10+
try {
11+
F_arguments = CommandContext.class.getDeclaredField("arguments");
12+
F_arguments.setAccessible(true);
13+
} catch (NoSuchFieldException e) {
14+
throw new RuntimeException("AvoidLib failed to reflectively retrieve CommandContext#arguments", e);
15+
}
16+
17+
return F_arguments;
18+
}
19+
20+
private static Map<String, ParsedArgument<CommandSourceStack, ?>> _getArgs(CommandContext<CommandSourceStack> ctx) {
21+
Map<String, ParsedArgument<CommandSourceStack, ?>> argsraw;
22+
23+
try {
24+
argsraw = (Map<String, ParsedArgument<CommandSourceStack, ?>>) _getArgField().get(ctx);
25+
} catch (IllegalAccessException e) {
26+
throw new RuntimeException("AvoidLib failed to reflectively get CommandContext#arguments", e);
27+
}
28+
29+
return argsraw;
30+
}
31+
32+
private static void _setArgs(CommandContext<CommandSourceStack> ctx, Map<String, ParsedArgument<CommandSourceStack, ?>> rekeyed) {
33+
try {
34+
_getArgField().set(ctx, rekeyed);
35+
} catch (IllegalAccessException ex) {
36+
throw new RuntimeException("AvoidLib failed to reflectively set CommandContext#arguments", ex);
37+
}
38+
}
1739
}
1840

1941
{
20-
var argsraw = ictx.avoid$arguments();
21-
22-
// == replacement == //
23-
24-
Map<String, ParsedArgument<CommandSourceStack, ?>> argsraw;
25-
26-
try {
27-
argsraw = (Map<String, ParsedArgument<CommandSourceStack, ?>>) F_arguments.get(ctx);
28-
} catch (IllegalAccessException e) {
29-
throw new RuntimeException("AvoidLib failed to reflectively get CommandContext#arguments", e);
30-
}
42+
((ICommandContext<CommandSourceStack>) ctx).avoid$arguments()
43+
_getArgs(ctx)
3144
}
3245

3346
{
34-
ictx.avoid$arguments(rekeyed);
35-
36-
// == replacement == //
37-
38-
try {
39-
F_arguments.set(ctx, rekeyed);
40-
} catch (IllegalAccessException ex) {
41-
throw new RuntimeException("AvoidLib failed to reflectively set CommandContext#arguments", ex);
42-
}
47+
((ICommandContext<CommandSourceStack>) ctx).avoid$arguments(
48+
_setArgs(ctx,
4349
}

‎versions/sponge/java/pl/olafcio/avoid_impl/mixin/CommandsMixin.fp-java‎

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,48 @@ import java.lang.reflect.Field;
77
}
88

99
{
10-
var ictx = (ICommandContext<CommandSourceStack>) ctx;
11-
12-
// == replacement == //
13-
14-
Field F_arguments;
15-
16-
try {
17-
F_arguments = CommandContext.class.getDeclaredField("arguments");
18-
F_arguments.setAccessible(true);
19-
} catch (NoSuchFieldException e) {
20-
throw new RuntimeException("AvoidLib failed to reflectively retrieve CommandContext#arguments", e);
21-
}
10+
public class CommandsMixin {
11+
public class CommandsMixin {
12+
private static Field _getArgField() {
13+
Field F_arguments;
14+
15+
try {
16+
F_arguments = CommandContext.class.getDeclaredField("arguments");
17+
F_arguments.setAccessible(true);
18+
} catch (NoSuchFieldException e) {
19+
throw new RuntimeException("AvoidLib failed to reflectively retrieve CommandContext#arguments", e);
20+
}
21+
22+
return F_arguments;
23+
}
24+
25+
private static Map<String, ParsedArgument<CommandSourceStack, ?>> _getArgs(CommandContext<CommandSourceStack> ctx) {
26+
Map<String, ParsedArgument<CommandSourceStack, ?>> argsraw;
27+
28+
try {
29+
argsraw = (Map<String, ParsedArgument<CommandSourceStack, ?>>) _getArgField().get(ctx);
30+
} catch (IllegalAccessException e) {
31+
throw new RuntimeException("AvoidLib failed to reflectively get CommandContext#arguments", e);
32+
}
33+
34+
return argsraw;
35+
}
36+
37+
private static void _setArgs(CommandContext<CommandSourceStack> ctx, Map<String, ParsedArgument<CommandSourceStack, ?>> rekeyed) {
38+
try {
39+
_getArgField().set(ctx, rekeyed);
40+
} catch (IllegalAccessException ex) {
41+
throw new RuntimeException("AvoidLib failed to reflectively set CommandContext#arguments", ex);
42+
}
43+
}
2244
}
2345

2446
{
25-
var argsraw = ictx.avoid$arguments();
26-
27-
// == replacement == //
28-
29-
Map<String, ParsedArgument<CommandSourceStack, ?>> argsraw;
30-
31-
try {
32-
argsraw = (Map<String, ParsedArgument<CommandSourceStack, ?>>) F_arguments.get(ctx);
33-
} catch (IllegalAccessException e) {
34-
throw new RuntimeException("AvoidLib failed to reflectively get CommandContext#arguments", e);
35-
}
47+
((ICommandContext<CommandSourceStack>) ctx).avoid$arguments()
48+
_getArgs(ctx)
3649
}
3750

3851
{
39-
ictx.avoid$arguments(rekeyed);
40-
41-
// == replacement == //
42-
43-
try {
44-
F_arguments.set(ctx, rekeyed);
45-
} catch (IllegalAccessException ex) {
46-
throw new RuntimeException("AvoidLib failed to reflectively set CommandContext#arguments", ex);
47-
}
52+
((ICommandContext<CommandSourceStack>) ctx).avoid$arguments(
53+
_setArgs(ctx,
4854
}

0 commit comments

Comments
 (0)