Add url based macro utility command. Also tweak TimeUnit a bit.

This commit is contained in:
Olof Larsson 2014-10-06 13:06:01 +02:00
parent 04f03eb14a
commit c11e2ba234
9 changed files with 230 additions and 12 deletions

View File

@ -20,6 +20,7 @@ import com.massivecraft.massivecore.adapter.PlayerInventoryAdapter;
import com.massivecraft.massivecore.adapter.UUIDAdapter;
import com.massivecraft.massivecore.cmd.massivecore.CmdMassiveCore;
import com.massivecraft.massivecore.cmd.massivecore.CmdMassiveCoreBuffer;
import com.massivecraft.massivecore.cmd.massivecore.CmdMassiveCoreCmdurl;
import com.massivecraft.massivecore.cmd.massivecore.CmdMassiveCoreStore;
import com.massivecraft.massivecore.cmd.massivecore.CmdMassiveCoreUsys;
import com.massivecraft.massivecore.event.EventMassiveCoreUuidUpdate;
@ -105,6 +106,9 @@ public class MassiveCore extends MassivePlugin
private CmdMassiveCoreBuffer outerCmdMassiveCoreBuffer;
public CmdMassiveCoreBuffer getOuterCmdMassiveCoreBuffer() { return this.outerCmdMassiveCoreBuffer; }
private CmdMassiveCoreCmdurl outerCmdMassiveCoreCmdurl;
public CmdMassiveCoreCmdurl getOuterCmdMassiveCoreCmdurl() { return this.outerCmdMassiveCoreCmdurl; }
// Runnables
// TODO: Make this one a singleton
private Runnable collTickTask = new Runnable()
@ -186,6 +190,9 @@ public class MassiveCore extends MassivePlugin
this.outerCmdMassiveCoreBuffer = new CmdMassiveCoreBuffer() { public List<String> getAliases() { return MassiveCoreMConf.get().aliasesOuterMassiveCoreBuffer; } };
this.outerCmdMassiveCoreBuffer.register();
this.outerCmdMassiveCoreCmdurl = new CmdMassiveCoreCmdurl() { public List<String> getAliases() { return MassiveCoreMConf.get().aliasesOuterMassiveCoreCmdurl; } };
this.outerCmdMassiveCoreCmdurl.register();
// Integration
this.integrate(
IntegrationVault.get()

View File

@ -32,6 +32,8 @@ public class MassiveCoreMConf extends Entity<MassiveCoreMConf>
public List<String> aliasesOuterMassiveCoreBuffer = MUtil.list("buffer");
public List<String> aliasesOuterMassiveCoreCmdurl = MUtil.list("cmdurl");
public boolean usingRecipientChatEvent = true;
public Map<String, String> permissionDeniedFormats = MUtil.map(

View File

@ -40,6 +40,7 @@ public enum MassiveCorePerm
BUFFER_SET("buffer.set"),
BUFFER_ADD("buffer.add"),
BUFFER_WHITESPACE("buffer.whitespace"),
CMDURL("cmdurl"),
NOTPDELAY("notpdelay"),
VARIABLE_BOOK("variable.book"),
VARIABLE_BUFFER("variable.buffer"),

View File

@ -18,6 +18,7 @@ public class CmdMassiveCore extends MassiveCommand
public CmdMassiveCoreTest cmdMassiveCoreTest = new CmdMassiveCoreTest();
public CmdMassiveCoreHearsound cmdMassiveCoreHearsound = new CmdMassiveCoreHearsound();
public CmdMassiveCoreBuffer cmdMassiveCoreBuffer = new CmdMassiveCoreBuffer();
public CmdMassiveCoreCmdurl cmdMassiveCoreCmdurl = new CmdMassiveCoreCmdurl();
public VersionCommand cmdMassiveCoreVersion = new VersionCommand(MassiveCore.get(), MassiveCorePerm.VERSION.node, "v", "version");
// -------------------------------------------- //
@ -33,6 +34,7 @@ public class CmdMassiveCore extends MassiveCommand
this.addSubCommand(this.cmdMassiveCoreTest);
this.addSubCommand(this.cmdMassiveCoreHearsound);
this.addSubCommand(this.cmdMassiveCoreBuffer);
this.addSubCommand(this.cmdMassiveCoreCmdurl);
this.addSubCommand(this.cmdMassiveCoreVersion);
// Requirements

View File

@ -0,0 +1,126 @@
package com.massivecraft.massivecore.cmd.massivecore;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import com.massivecraft.massivecore.MassiveCore;
import com.massivecraft.massivecore.MassiveCorePerm;
import com.massivecraft.massivecore.cmd.MassiveCommand;
import com.massivecraft.massivecore.cmd.req.ReqHasPerm;
import com.massivecraft.massivecore.cmd.req.ReqIsPlayer;
import com.massivecraft.massivecore.mixin.Mixin;
import com.massivecraft.massivecore.util.WebUtil;
public class CmdMassiveCoreCmdurl extends MassiveCommand
{
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public CmdMassiveCoreCmdurl()
{
// Aliases
this.addAliases("cmdurl");
// Args
this.addRequiredArg("url");
// Requirements
this.addRequirements(ReqHasPerm.get(MassiveCorePerm.CMDURL.node));
this.addRequirements(ReqIsPlayer.get());
}
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public void perform()
{
// Args
String urlString = this.arg(0);
final URL url;
try
{
url = new URL(urlString);
}
catch (MalformedURLException e)
{
msg("<b>Malformed URL: %s", e.getMessage());
return;
}
// Apply
final Player commander = me;
msg("<i>Loading <aqua>%s <i>...", urlString);
async(new Runnable()
{
@Override
public void run()
{
try
{
final List<String> lines = WebUtil.getUrlLines(url, 5000);
sync(new Runnable()
{
@Override
public void run()
{
Mixin.msgOne(commander, "<i>... <h>%d <i>lines loaded. Now executing ...", lines.size());
for (int i = 0; i <= lines.size() - 1; i++)
{
String line = lines.get(i);
line = line.trim();
if (line.length() == 0 || line.startsWith("#"))
{
Mixin.msgOne(commander, "<b>#%d: <i>%s", i, line);
// Ignore the line
}
else
{
Mixin.msgOne(commander, "<g>#%d: <i>%s", i, line);
// Run the line
commander.chat(line);
}
}
}
});
return;
}
catch (final Exception e)
{
sync(new Runnable()
{
@Override
public void run()
{
Mixin.msgOne(commander, "<b>%s: %s", e.getClass().getSimpleName(), e.getMessage());
}
});
return;
}
}
});
}
// -------------------------------------------- //
// ASYNC/SYNC SHORTHANDS
// -------------------------------------------- //
public static void sync(Runnable runnable)
{
Bukkit.getScheduler().runTask(MassiveCore.get(), runnable);
}
public static void async(Runnable runnable)
{
Bukkit.getScheduler().runTaskAsynchronously(MassiveCore.get(), runnable);
}
}

View File

@ -93,6 +93,21 @@ public abstract class SenderEntity<E extends SenderEntity<E>> extends Entity<E>
return Mixin.isOffline(this.getId());
}
public Long getLastPlayed()
{
return Mixin.getLastPlayed(this.getId());
}
public Long getFirstPlayed()
{
return Mixin.getFirstPlayed(this.getId());
}
public boolean hasPlayedBefore()
{
return Mixin.hasPlayedBefore(this.getId());
}
// DISPLAY NAME
@Deprecated

View File

@ -36,25 +36,35 @@ public class TimeUnit implements Comparable<TimeUnit>
private static final TreeSet<TimeUnit> all = new TreeSet<TimeUnit>();
public static TreeSet<TimeUnit> getAll() { return new TreeSet<TimeUnit>(all); }
public static TreeSet<TimeUnit> getAllBut(TimeUnit... timeUnits)
{
TreeSet<TimeUnit> ret = new TreeSet<TimeUnit>(all);
for (TimeUnit timeUnit : timeUnits)
{
ret.remove(timeUnit);
}
return ret;
}
public static TreeSet<TimeUnit> getAllButMillis()
{
TreeSet<TimeUnit> ret = new TreeSet<TimeUnit>(all);
ret.remove(MILLISECOND);
return ret;
return getAllBut(MILLISECOND);
}
public static TreeSet<TimeUnit> getAllButMillisAndSeconds()
{
TreeSet<TimeUnit> ret = new TreeSet<TimeUnit>(all);
ret.remove(MILLISECOND);
ret.remove(SECOND);
return ret;
return getAllBut(MILLISECOND, SECOND);
}
public static TreeSet<TimeUnit> getAllButMillisSecondsAndMinutes()
{
TreeSet<TimeUnit> ret = new TreeSet<TimeUnit>(all);
ret.remove(MILLISECOND);
ret.remove(SECOND);
ret.remove(MINUTE);
return getAllBut(MILLISECOND, SECOND, MINUTE);
}
public static TreeSet<TimeUnit> getSpecific(TimeUnit... timeUnits)
{
TreeSet<TimeUnit> ret = new TreeSet<TimeUnit>(Arrays.asList(timeUnits));
return ret;
}
@ -62,7 +72,7 @@ public class TimeUnit implements Comparable<TimeUnit>
{
if (timeUnitString == null) return null;
String timeUnitStringLowerCase = timeUnitString.toLowerCase();
for(TimeUnit timeUnit : all)
for (TimeUnit timeUnit : all)
{
for (String alias : timeUnit.aliases)
{

View File

@ -1,14 +1,23 @@
package com.massivecraft.massivecore.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class WebUtil
{
// -------------------------------------------- //
// ASYNC GET TOUCH
// -------------------------------------------- //
private static final ExecutorService executor = Executors.newSingleThreadExecutor();
public static void asyncGetTouch(String url)
@ -44,4 +53,48 @@ public class WebUtil
}
});
}
// -------------------------------------------- //
// GET URL LINES
// -------------------------------------------- //
// This one should be run async since it's very slow.
public static List<String> getUrlLines(URL url, int timeoutMillis) throws IOException
{
HttpURLConnection uc = null;
InputStream is = null;
InputStreamReader isr = null;
BufferedReader br = null;
try
{
uc = (HttpURLConnection) url.openConnection();
uc.setConnectTimeout(timeoutMillis);
uc.setRequestMethod("GET");
uc.addRequestProperty("User-Agent", "Mozilla/4.76");
uc.connect();
is = uc.getInputStream();
isr = new InputStreamReader(is, "UTF-8");
br = new BufferedReader(isr);
List<String> lines = new ArrayList<String>();
String line = null;
while ( (line = br.readLine()) != null )
{
lines.add(line);
}
return lines;
}
finally
{
// Closing BufferedReader does close InputStreamReader does close InputStream.
try { br.close(); } catch (Exception ignored) {}
// However documentation suggested we must disconnect the HttpURLConnection manually.
try { uc.disconnect(); } catch (Exception ignored) {}
}
}
}

View File

@ -40,6 +40,7 @@ permissions:
massivecore.buffer.set: {description: set buffer, default: false}
massivecore.buffer.add: {description: add to buffer, default: false}
massivecore.buffer.whitespace: {description: add whitespace to buffer, default: false}
massivecore.cmdurl: {description: run all lines of url content, default: false}
# misc
massivecore.notpdelay: {description: teleport without delay, default: false}
massivecore.variable.book: {description: replace ***book*** with content of book in your hand, default: false}
@ -80,6 +81,7 @@ permissions:
massivecore.buffer.set: true
massivecore.buffer.add: true
massivecore.buffer.whitespace: true
massivecore.cmdurl: true
massivecore.notpdelay: true
massivecore.variable.book: true
massivecore.variable.buffer: true