Skip to content

Commit

Permalink
Add get_player_input()
Browse files Browse the repository at this point in the history
  • Loading branch information
PseudoKnight committed Dec 26, 2024
1 parent 8d1b498 commit 11c33f8
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/laytonsmith/abstraction/MCPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,6 @@ public interface MCPlayer extends MCCommandSender, MCHumanEntity, MCOfflinePlaye
void sendEquipmentChange(MCLivingEntity entity, MCEquipmentSlot slot, MCItemStack item);

int getPing();

MCPlayerInput getCurrentInput();
}
11 changes: 11 additions & 0 deletions src/main/java/com/laytonsmith/abstraction/MCPlayerInput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.laytonsmith.abstraction;

public interface MCPlayerInput {
boolean forward();
boolean backward();
boolean left();
boolean right();
boolean jump();
boolean sneak();
boolean sprint();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.laytonsmith.abstraction.bukkit;

import com.laytonsmith.abstraction.MCPlayerInput;
import org.bukkit.Input;

public class BukkitMCPlayerInput implements MCPlayerInput {

private final Input input;

public BukkitMCPlayerInput(Input input) {
this.input = input;
}

@Override
public boolean forward() {
return this.input.isForward();
}

@Override
public boolean backward() {
return this.input.isBackward();
}

@Override
public boolean left() {
return this.input.isLeft();
}

@Override
public boolean right() {
return this.input.isRight();
}

@Override
public boolean jump() {
return this.input.isJump();
}

@Override
public boolean sneak() {
return this.input.isSneak();
}

@Override
public boolean sprint() {
return this.input.isSprint();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.laytonsmith.abstraction.MCNote;
import com.laytonsmith.abstraction.MCOfflinePlayer;
import com.laytonsmith.abstraction.MCPlayer;
import com.laytonsmith.abstraction.MCPlayerInput;
import com.laytonsmith.abstraction.MCPlayerInventory;
import com.laytonsmith.abstraction.MCScoreboard;
import com.laytonsmith.abstraction.MCWorldBorder;
Expand All @@ -19,6 +20,7 @@
import com.laytonsmith.abstraction.bukkit.BukkitConvertor;
import com.laytonsmith.abstraction.bukkit.BukkitMCItemStack;
import com.laytonsmith.abstraction.bukkit.BukkitMCLocation;
import com.laytonsmith.abstraction.bukkit.BukkitMCPlayerInput;
import com.laytonsmith.abstraction.bukkit.BukkitMCPlayerInventory;
import com.laytonsmith.abstraction.bukkit.BukkitMCScoreboard;
import com.laytonsmith.abstraction.bukkit.BukkitMCServer;
Expand Down Expand Up @@ -872,4 +874,9 @@ public void sendEquipmentChange(MCLivingEntity entity, MCEquipmentSlot slot, MCI
public int getPing() {
return p.getPing();
}

@Override
public MCPlayerInput getCurrentInput() {
return new BukkitMCPlayerInput(p.getCurrentInput());
}
}
55 changes: 55 additions & 0 deletions src/main/java/com/laytonsmith/core/functions/PlayerManagement.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.laytonsmith.abstraction.MCNamespacedKey;
import com.laytonsmith.abstraction.MCOfflinePlayer;
import com.laytonsmith.abstraction.MCPlayer;
import com.laytonsmith.abstraction.MCPlayerInput;
import com.laytonsmith.abstraction.MCServer;
import com.laytonsmith.abstraction.MCWorld;
import com.laytonsmith.abstraction.MCWorldBorder;
Expand Down Expand Up @@ -7252,4 +7253,58 @@ public Boolean runAsync() {
return false;
}
}

@api
public static class get_player_input extends AbstractFunction {

public String getName() {
return "get_player_input";
}

public Integer[] numArgs() {
return new Integer[]{0, 1};
}

public String docs() {
return "array {[player]} Returns a player's current input. (MC 1.21.3+)"
+ " This can be used to detect which movement keys the player is pressing."
+ " Array contains the following keys: forward, backward, left, right, jump, sneak, and sprint.";
}

public Construct exec(Target t, Environment env, Mixed... args) throws ConfigRuntimeException {
MCPlayer p;
if(args.length == 1) {
p = Static.GetPlayer(args[0].val(), t);
} else {
p = env.getEnv(CommandHelperEnvironment.class).GetPlayer();
Static.AssertPlayerNonNull(p, t);
}
CArray ret = CArray.GetAssociativeArray(t);
MCPlayerInput input = p.getCurrentInput();
ret.set("forward", CBoolean.get(input.forward()), t);
ret.set("backward", CBoolean.get(input.backward()), t);
ret.set("left", CBoolean.get(input.left()), t);
ret.set("right", CBoolean.get(input.right()), t);
ret.set("jump", CBoolean.get(input.jump()), t);
ret.set("sneak", CBoolean.get(input.sneak()), t);
ret.set("sprint", CBoolean.get(input.sprint()), t);
return ret;
}

public Class<? extends CREThrowable>[] thrown() {
return new Class[]{CREPlayerOfflineException.class, CRELengthException.class};
}

public Version since() {
return MSVersion.V3_3_5;
}

public boolean isRestricted() {
return true;
}

public Boolean runAsync() {
return false;
}
}
}
2 changes: 2 additions & 0 deletions src/test/java/com/laytonsmith/testing/StaticTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.laytonsmith.abstraction.MCNote;
import com.laytonsmith.abstraction.MCPattern;
import com.laytonsmith.abstraction.MCPlayer;
import com.laytonsmith.abstraction.MCPlayerInput;
import com.laytonsmith.abstraction.MCPlugin;
import com.laytonsmith.abstraction.MCPluginMeta;
import com.laytonsmith.abstraction.MCPotionData;
Expand Down Expand Up @@ -466,6 +467,7 @@ public static MCPlayer GetOnlinePlayer(String name, String worldName, MCServer s
when(p.isOnline()).thenReturn(true);
when(p.getName()).thenReturn(name);
when(p.getServer()).thenReturn(s);
when(p.getCurrentInput()).thenReturn(mock(MCPlayerInput.class));
when(p.isOp()).thenReturn(true);
if(s != null && s.getOnlinePlayers() != null) {
Collection<MCPlayer> online = s.getOnlinePlayers();
Expand Down

0 comments on commit 11c33f8

Please sign in to comment.