Implement a terminal interface
This commit is contained in:
parent
a70425d418
commit
d28cd483f5
5 changed files with 185 additions and 2 deletions
31
src/main/java/dev/zontreck/ariaslib/events/CommandEvent.java
Normal file
31
src/main/java/dev/zontreck/ariaslib/events/CommandEvent.java
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
package dev.zontreck.ariaslib.events;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.google.common.collect.Lists;
|
||||||
|
|
||||||
|
public class CommandEvent extends Event
|
||||||
|
{
|
||||||
|
public String command;
|
||||||
|
public List<String> arguments = Lists.newArrayList();
|
||||||
|
public CommandEvent(String commandString)
|
||||||
|
{
|
||||||
|
String[] cmds = commandString.split(" ");
|
||||||
|
if(cmds.length > 0)
|
||||||
|
{
|
||||||
|
command = cmds[0];
|
||||||
|
if(cmds.length==1)return;
|
||||||
|
for(int i=1;i<cmds.length;i++)
|
||||||
|
{
|
||||||
|
arguments.add(cmds[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
}else throw new IllegalArgumentException("The command cannot be empty!");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isCancellable() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -15,11 +15,11 @@ public abstract class Event {
|
||||||
return isCancelled;
|
return isCancelled;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setCancelled(boolean cancel) throws Exception
|
public void setCancelled(boolean cancel)
|
||||||
{
|
{
|
||||||
if(!isCancellable())
|
if(!isCancellable())
|
||||||
{
|
{
|
||||||
throw new Exception("This event cannot be cancelled");
|
return;
|
||||||
}
|
}
|
||||||
isCancelled=cancel;
|
isCancelled=cancel;
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,26 @@
|
||||||
|
package dev.zontreck.ariaslib.terminal;
|
||||||
|
|
||||||
|
import java.io.Console;
|
||||||
|
|
||||||
|
import dev.zontreck.ariaslib.events.CommandEvent;
|
||||||
|
import dev.zontreck.ariaslib.events.EventBus;
|
||||||
|
import dev.zontreck.ariaslib.util.DelayedExecutorService;
|
||||||
|
|
||||||
|
public class ConsolePrompt implements Runnable
|
||||||
|
{
|
||||||
|
public static final Console console = System.console();
|
||||||
|
@Override
|
||||||
|
public void run()
|
||||||
|
{
|
||||||
|
// Print a prompt
|
||||||
|
console.printf("\n> ");
|
||||||
|
String commandInput = console.readLine();
|
||||||
|
|
||||||
|
CommandEvent event = new CommandEvent(commandInput);
|
||||||
|
if(!EventBus.BUS.post(event)){
|
||||||
|
DelayedExecutorService.getInstance().schedule(new ConsolePrompt(), 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
33
src/main/java/dev/zontreck/ariaslib/terminal/Terminal.java
Normal file
33
src/main/java/dev/zontreck/ariaslib/terminal/Terminal.java
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
package dev.zontreck.ariaslib.terminal;
|
||||||
|
|
||||||
|
import java.io.Console;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
|
import dev.zontreck.ariaslib.util.DelayedExecutorService;
|
||||||
|
|
||||||
|
public class Terminal {
|
||||||
|
private static final AtomicInteger ID = new AtomicInteger(0);
|
||||||
|
private static boolean running=true;
|
||||||
|
/**
|
||||||
|
* This starts a terminal instance
|
||||||
|
* @return The terminal ID
|
||||||
|
*/
|
||||||
|
public static int startTerminal()
|
||||||
|
{
|
||||||
|
running=true;
|
||||||
|
DelayedExecutorService.getInstance().schedule(new ConsolePrompt(), 1);
|
||||||
|
|
||||||
|
|
||||||
|
return ID.getAndIncrement();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isRunning()
|
||||||
|
{
|
||||||
|
return running;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setRunning(boolean running)
|
||||||
|
{
|
||||||
|
Terminal.running=running;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,93 @@
|
||||||
|
package dev.zontreck.ariaslib.util;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Timer;
|
||||||
|
import java.util.TimerTask;
|
||||||
|
|
||||||
|
import dev.zontreck.ariaslib.terminal.Terminal;
|
||||||
|
|
||||||
|
|
||||||
|
public class DelayedExecutorService {
|
||||||
|
private static int COUNT = 0;
|
||||||
|
private static final DelayedExecutorService inst;
|
||||||
|
private static final Timer repeater;
|
||||||
|
static{
|
||||||
|
inst=new DelayedExecutorService();
|
||||||
|
repeater=new Timer();
|
||||||
|
repeater.schedule(new TimerTask(){
|
||||||
|
@Override
|
||||||
|
public void run()
|
||||||
|
{
|
||||||
|
DelayedExecutorService.getInstance().onTick();
|
||||||
|
|
||||||
|
}
|
||||||
|
}, 1000L, 1000L);
|
||||||
|
}
|
||||||
|
private DelayedExecutorService(){}
|
||||||
|
|
||||||
|
public static DelayedExecutorService getInstance()
|
||||||
|
{
|
||||||
|
return inst;
|
||||||
|
}
|
||||||
|
public class DelayedExecution
|
||||||
|
{
|
||||||
|
public DelayedExecution(Runnable run, long unix) {
|
||||||
|
scheduled=run;
|
||||||
|
unix_time=unix;
|
||||||
|
}
|
||||||
|
public Runnable scheduled;
|
||||||
|
public long unix_time;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<DelayedExecution> EXECUTORS = new ArrayList<>();
|
||||||
|
|
||||||
|
public void schedule(final Runnable run, int seconds)
|
||||||
|
{
|
||||||
|
if(!Terminal.isRunning()){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
//long unix = Instant.now().getEpochSecond()+ (seconds);
|
||||||
|
TimerTask task = new TimerTask() {
|
||||||
|
@Override
|
||||||
|
public void run()
|
||||||
|
{
|
||||||
|
run.run();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
repeater.schedule(task, seconds*1000L);
|
||||||
|
//DelayedExecution exe = new DelayedExecution(run,unix);
|
||||||
|
//EXECUTORS.add(exe);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void stopRepeatingThread()
|
||||||
|
{
|
||||||
|
repeater.cancel();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onTick()
|
||||||
|
{
|
||||||
|
if(!Terminal.isRunning())
|
||||||
|
{
|
||||||
|
DelayedExecutorService.stopRepeatingThread();
|
||||||
|
}
|
||||||
|
/*Iterator<DelayedExecution> it = EXECUTORS.iterator();
|
||||||
|
while(it.hasNext())
|
||||||
|
{
|
||||||
|
DelayedExecution e = it.next();
|
||||||
|
if(e.unix_time < Instant.now().getEpochSecond())
|
||||||
|
{
|
||||||
|
it.remove();
|
||||||
|
Thread tx = new Thread(e.scheduled);
|
||||||
|
tx.setName("DelayedExecutorTask-"+String.valueOf(DelayedExecutorService.getNext()));
|
||||||
|
tx.start();
|
||||||
|
}
|
||||||
|
}*/
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int getNext()
|
||||||
|
{
|
||||||
|
COUNT++;
|
||||||
|
return COUNT;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue