Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finish Tab-Completion Handling #99

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions src/main/java/ce/ajneb97/listeners/PlayerEventsListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.event.player.*;
import org.bukkit.event.server.TabCompleteEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;

Expand Down Expand Up @@ -563,7 +564,6 @@ public void onHealthRegain(EntityRegainHealthEvent event){
).checkEvent();
}

/*
@EventHandler(priority = EventPriority.HIGHEST)
public void onTabComplete(TabCompleteEvent event){
if(!(event.getSender() instanceof Player)){
Expand All @@ -572,12 +572,18 @@ public void onTabComplete(TabCompleteEvent event){

Player player = (Player) event.getSender();
String command = event.getBuffer();
String[] args = command.split(" ");
ArrayList<StoredVariable> eventVariables = new ArrayList<StoredVariable>();
for(int i=1;i<args.length;i++) {
eventVariables.add(new StoredVariable("%arg_"+(i)+"%",args[i]));
}

new ConditionEvent(plugin, player, event, EventType.PLAYER_TAB_COMPLETE_EVENT, null)
.addVariables(
new StoredVariable("%command%",command)
).checkEvent();
ConditionEvent conditionEvent = new ConditionEvent(plugin, player, event, EventType.PLAYER_TAB_COMPLETE, null);
if(!conditionEvent.containsValidEvents()) return;
conditionEvent.addVariables(
new StoredVariable("%main_command%",args[0]),
new StoredVariable("%args_length%",(args.length-1)+"")
).addVariables(eventVariables)
.checkEvent();
}

*/
}
2 changes: 1 addition & 1 deletion src/main/java/ce/ajneb97/model/EventType.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public enum EventType {
PLAYER_REGAIN_HEALTH,
PLAYER_CHANGE_AIR, //1.10+
PLAYER_CHANGE_FOOD, //1.16+
PLAYER_TAB_COMPLETE_EVENT,
PLAYER_TAB_COMPLETE,
BLOCK_INTERACT,
BLOCK_BREAK,
BLOCK_PLACE,
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/ce/ajneb97/model/actions/ActionType.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,7 @@ public enum ActionType {
DISCORDSRV_EMBED,
CALL_EVENT,
EXECUTE_ACTION_GROUP,
TAB_COMPLETE,
API

}
2 changes: 2 additions & 0 deletions src/main/java/ce/ajneb97/model/internal/ExecutedEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,8 @@ private void executeAction(Player player,ActionType type,String apiType,String a
//case VECTOR:
// ActionUtils.vector(player, actionLine);
// return;
case TAB_COMPLETE:
ActionUtils.tabComplete(actionLine,minecraftEvent);
}
}

Expand Down
29 changes: 29 additions & 0 deletions src/main/java/ce/ajneb97/utils/ActionUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.*;
import org.bukkit.event.server.TabCompleteEvent;
import org.bukkit.inventory.EntityEquipment;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.FireworkMeta;
Expand All @@ -35,6 +36,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

public class ActionUtils {

Expand Down Expand Up @@ -415,6 +417,33 @@ public static void setItem(String actionLine,Event minecraftEvent){
}
}

public static void tabComplete(String actionLine,Event minecraftEvent){
// tab_complete: <mode>;<element1>,<element2>,<elementN>
String[] sep = actionLine.replace("tab_complete: ","").split(";");

String mode = sep[0];
List<String> completions = new ArrayList<>();
if(sep.length > 1){
completions = Arrays.asList(sep[1].split(","));
}

if(minecraftEvent instanceof TabCompleteEvent) {
TabCompleteEvent event = (TabCompleteEvent) minecraftEvent;
if (mode.equalsIgnoreCase("remove")) {
event.getCompletions().removeAll(completions);
} else if (mode.equalsIgnoreCase("clear")) {
event.getCompletions().clear();
} else if (mode.equalsIgnoreCase("set")) {
event.setCompletions(completions);
} else {
String[] buffer_words = event.getBuffer().split(" ", -1); // Don't trim empties
String partial_word = buffer_words[buffer_words.length - 1];
event.getCompletions().addAll(completions.stream().filter(w -> w.startsWith(partial_word)).collect(Collectors.toList()));
event.getCompletions().sort(Comparator.naturalOrder());
}
}
}

public static void setBlock(String actionLine){
// set_block: location:<x>,<y>,<z>,<world>;id:<id>
String[] sep = actionLine.replace("set_block: ","").split(";");
Expand Down