Skip to content

Commit 11c33f8

Browse files
committed
Add get_player_input()
1 parent 8d1b498 commit 11c33f8

File tree

6 files changed

+125
-0
lines changed

6 files changed

+125
-0
lines changed

src/main/java/com/laytonsmith/abstraction/MCPlayer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,6 @@ public interface MCPlayer extends MCCommandSender, MCHumanEntity, MCOfflinePlaye
204204
void sendEquipmentChange(MCLivingEntity entity, MCEquipmentSlot slot, MCItemStack item);
205205

206206
int getPing();
207+
208+
MCPlayerInput getCurrentInput();
207209
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.laytonsmith.abstraction;
2+
3+
public interface MCPlayerInput {
4+
boolean forward();
5+
boolean backward();
6+
boolean left();
7+
boolean right();
8+
boolean jump();
9+
boolean sneak();
10+
boolean sprint();
11+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.laytonsmith.abstraction.bukkit;
2+
3+
import com.laytonsmith.abstraction.MCPlayerInput;
4+
import org.bukkit.Input;
5+
6+
public class BukkitMCPlayerInput implements MCPlayerInput {
7+
8+
private final Input input;
9+
10+
public BukkitMCPlayerInput(Input input) {
11+
this.input = input;
12+
}
13+
14+
@Override
15+
public boolean forward() {
16+
return this.input.isForward();
17+
}
18+
19+
@Override
20+
public boolean backward() {
21+
return this.input.isBackward();
22+
}
23+
24+
@Override
25+
public boolean left() {
26+
return this.input.isLeft();
27+
}
28+
29+
@Override
30+
public boolean right() {
31+
return this.input.isRight();
32+
}
33+
34+
@Override
35+
public boolean jump() {
36+
return this.input.isJump();
37+
}
38+
39+
@Override
40+
public boolean sneak() {
41+
return this.input.isSneak();
42+
}
43+
44+
@Override
45+
public boolean sprint() {
46+
return this.input.isSprint();
47+
}
48+
}

src/main/java/com/laytonsmith/abstraction/bukkit/entities/BukkitMCPlayer.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.laytonsmith.abstraction.MCNote;
1010
import com.laytonsmith.abstraction.MCOfflinePlayer;
1111
import com.laytonsmith.abstraction.MCPlayer;
12+
import com.laytonsmith.abstraction.MCPlayerInput;
1213
import com.laytonsmith.abstraction.MCPlayerInventory;
1314
import com.laytonsmith.abstraction.MCScoreboard;
1415
import com.laytonsmith.abstraction.MCWorldBorder;
@@ -19,6 +20,7 @@
1920
import com.laytonsmith.abstraction.bukkit.BukkitConvertor;
2021
import com.laytonsmith.abstraction.bukkit.BukkitMCItemStack;
2122
import com.laytonsmith.abstraction.bukkit.BukkitMCLocation;
23+
import com.laytonsmith.abstraction.bukkit.BukkitMCPlayerInput;
2224
import com.laytonsmith.abstraction.bukkit.BukkitMCPlayerInventory;
2325
import com.laytonsmith.abstraction.bukkit.BukkitMCScoreboard;
2426
import com.laytonsmith.abstraction.bukkit.BukkitMCServer;
@@ -872,4 +874,9 @@ public void sendEquipmentChange(MCLivingEntity entity, MCEquipmentSlot slot, MCI
872874
public int getPing() {
873875
return p.getPing();
874876
}
877+
878+
@Override
879+
public MCPlayerInput getCurrentInput() {
880+
return new BukkitMCPlayerInput(p.getCurrentInput());
881+
}
875882
}

src/main/java/com/laytonsmith/core/functions/PlayerManagement.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import com.laytonsmith.abstraction.MCNamespacedKey;
1515
import com.laytonsmith.abstraction.MCOfflinePlayer;
1616
import com.laytonsmith.abstraction.MCPlayer;
17+
import com.laytonsmith.abstraction.MCPlayerInput;
1718
import com.laytonsmith.abstraction.MCServer;
1819
import com.laytonsmith.abstraction.MCWorld;
1920
import com.laytonsmith.abstraction.MCWorldBorder;
@@ -7252,4 +7253,58 @@ public Boolean runAsync() {
72527253
return false;
72537254
}
72547255
}
7256+
7257+
@api
7258+
public static class get_player_input extends AbstractFunction {
7259+
7260+
public String getName() {
7261+
return "get_player_input";
7262+
}
7263+
7264+
public Integer[] numArgs() {
7265+
return new Integer[]{0, 1};
7266+
}
7267+
7268+
public String docs() {
7269+
return "array {[player]} Returns a player's current input. (MC 1.21.3+)"
7270+
+ " This can be used to detect which movement keys the player is pressing."
7271+
+ " Array contains the following keys: forward, backward, left, right, jump, sneak, and sprint.";
7272+
}
7273+
7274+
public Construct exec(Target t, Environment env, Mixed... args) throws ConfigRuntimeException {
7275+
MCPlayer p;
7276+
if(args.length == 1) {
7277+
p = Static.GetPlayer(args[0].val(), t);
7278+
} else {
7279+
p = env.getEnv(CommandHelperEnvironment.class).GetPlayer();
7280+
Static.AssertPlayerNonNull(p, t);
7281+
}
7282+
CArray ret = CArray.GetAssociativeArray(t);
7283+
MCPlayerInput input = p.getCurrentInput();
7284+
ret.set("forward", CBoolean.get(input.forward()), t);
7285+
ret.set("backward", CBoolean.get(input.backward()), t);
7286+
ret.set("left", CBoolean.get(input.left()), t);
7287+
ret.set("right", CBoolean.get(input.right()), t);
7288+
ret.set("jump", CBoolean.get(input.jump()), t);
7289+
ret.set("sneak", CBoolean.get(input.sneak()), t);
7290+
ret.set("sprint", CBoolean.get(input.sprint()), t);
7291+
return ret;
7292+
}
7293+
7294+
public Class<? extends CREThrowable>[] thrown() {
7295+
return new Class[]{CREPlayerOfflineException.class, CRELengthException.class};
7296+
}
7297+
7298+
public Version since() {
7299+
return MSVersion.V3_3_5;
7300+
}
7301+
7302+
public boolean isRestricted() {
7303+
return true;
7304+
}
7305+
7306+
public Boolean runAsync() {
7307+
return false;
7308+
}
7309+
}
72557310
}

src/test/java/com/laytonsmith/testing/StaticTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.laytonsmith.abstraction.MCNote;
2525
import com.laytonsmith.abstraction.MCPattern;
2626
import com.laytonsmith.abstraction.MCPlayer;
27+
import com.laytonsmith.abstraction.MCPlayerInput;
2728
import com.laytonsmith.abstraction.MCPlugin;
2829
import com.laytonsmith.abstraction.MCPluginMeta;
2930
import com.laytonsmith.abstraction.MCPotionData;
@@ -466,6 +467,7 @@ public static MCPlayer GetOnlinePlayer(String name, String worldName, MCServer s
466467
when(p.isOnline()).thenReturn(true);
467468
when(p.getName()).thenReturn(name);
468469
when(p.getServer()).thenReturn(s);
470+
when(p.getCurrentInput()).thenReturn(mock(MCPlayerInput.class));
469471
when(p.isOp()).thenReturn(true);
470472
if(s != null && s.getOnlinePlayers() != null) {
471473
Collection<MCPlayer> online = s.getOnlinePlayers();

0 commit comments

Comments
 (0)