Skip to content

Commit

Permalink
1.1.0
Browse files Browse the repository at this point in the history
Added json encode expression and more synonyms for encode expressions
  • Loading branch information
btk5h committed Dec 4, 2016
1 parent 198f377 commit c1820a1
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 4 deletions.
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,22 @@ into a URL (e.g. when using a search api).

#### Syntax

`(http|ur(i|l)) (safe|encoded) %input%`
`(http|ur(i|l)) (safe|encoded|escaped) %input%`

#### Parameters

- `input` (type `texts`) - One or more input texts to encode.

---

### Expression `JSON Safe Text` => `text`

Converts a text into a text safe for usage in JSON strings. This can be useful for injecting user
input into a JSON payload (e.g. when using a search api).

#### Syntax

`json (safe|encoded|escaped) %input%`

#### Parameters

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/

group 'com.w00tmast3r'
version '1.0.0'
version '1.1.0'

apply plugin: 'java'

Expand Down
121 changes: 121 additions & 0 deletions src/main/java/com/w00tmast3r/reqn/skript/ExprJSONEncode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* MIT License
*
* Copyright (c) 2016 Bryan Terce
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

package com.w00tmast3r.reqn.skript;

import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Arrays;

import ch.njol.skript.Skript;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.SkriptParser;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.util.Kleenean;

public class ExprJSONEncode extends SimpleExpression<String> {

static {
Skript.registerExpression(ExprJSONEncode.class, String.class, ExpressionType.SIMPLE,
"json (safe|encoded|escaped) %strings%");
}

private Expression<String> str;

private static String encode(String s) {
StringBuilder sb = new StringBuilder(s.length());
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
switch (c) {
case '\\':
case '"':
sb.append('\\');
sb.append(c);
break;
case '\b':
sb.append("\\b");
break;
case '\t':
sb.append("\\t");
break;
case '\n':
sb.append("\\n");
break;
case '\f':
sb.append("\\f");
break;
case '\r':
sb.append("\\r");
break;
default:
if (c < ' ') {
String t = "000" + Integer.toHexString(c);
sb.append("\\u");
sb.append(t.substring(t.length() - 4));
} else {
sb.append(c);
}
break;
}
}
return sb.toString();
}

@Override
protected String[] get(Event e) {
return Arrays.stream(str.getAll(e))
.map(ExprJSONEncode::encode)
.toArray(String[]::new);
}

@Override
public boolean isSingle() {
return str.isSingle();
}

@Override
public Class<? extends String> getReturnType() {
return String.class;
}

@Override
public String toString(@Nullable Event e, boolean debug) {
return "url encoded text";
}

@SuppressWarnings("unchecked")
@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed,
SkriptParser.ParseResult parseResult) {
str = (Expression<String>) exprs[0];
return true;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class ExprURLEncode extends SimpleExpression<String> {

static {
Skript.registerExpression(ExprURLEncode.class, String.class, ExpressionType.SIMPLE,
"(http|ur(i|l)) (safe|encoded) %strings%");
"(http|ur(i|l)) (safe|encoded|escaped) %strings%");
}

private Expression<String> str;
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Reqn
version: 1.0.0
version: 1.1.0
main: com.w00tmast3r.reqn.Reqn
depend: [Skript]

0 comments on commit c1820a1

Please sign in to comment.