Skip to content

Commit 1b97b5e

Browse files
Merge pull request #35 from XenithMusic/1.19.4
Get requests (Unstable)
2 parents 73fd92a + 0412bca commit 1b97b5e

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

src/main/java/io/github/techstreet/dfscript/script/action/ScriptActionType.java

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
import net.minecraft.util.Identifier;
4343
import net.minecraft.world.GameMode;
4444

45-
import java.io.IOException;
45+
import java.io.*;
46+
import java.net.*;
4647
import java.nio.file.Files;
4748
import java.nio.file.Path;
4849
import java.text.SimpleDateFormat;
@@ -119,6 +120,31 @@ public enum ScriptActionType {
119120
ctx.value("Value")
120121
))),
121122

123+
GET_REQUEST(builder -> builder.name("Get Webrequest")
124+
.description("Makes a get request to the internet.")
125+
.icon(Items.GRASS_BLOCK)
126+
.arg("Result", ScriptActionArgumentType.VARIABLE)
127+
.arg("Url", ScriptActionArgumentType.TEXT)
128+
.category(ScriptActionCategory.VARIABLES)
129+
.action(ctx -> {
130+
try{
131+
StringBuilder result = new StringBuilder();
132+
URL url = new URL(ctx.value("Url").asText());
133+
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
134+
conn.setRequestMethod("GET");
135+
try (BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()))) {
136+
for (String line; (line = reader.readLine()) != null; ) {
137+
result.append(line);
138+
}
139+
}
140+
ctx.task().context().setVariable(ctx.variable("Result").name(), new ScriptTextValue(result.toString()));
141+
}catch(MalformedURLException ex){
142+
143+
}catch(IOException ex){
144+
145+
}
146+
})),
147+
122148
INCREMENT(builder -> builder.name("Increment")
123149
.description("Increments a variable by a value.")
124150
.icon(Items.GLOWSTONE_DUST)
@@ -911,7 +937,7 @@ public enum ScriptActionType {
911937
String text = ctx.value("Text").asText();
912938
ctx.task().context().setVariable(ctx.variable("Result").name(), new ScriptNumberValue(text.length()));
913939
})),
914-
940+
915941
READ_FILE(builder -> builder.name("Read File")
916942
.description("Reads a file from the scripts folder.")
917943
.icon(Items.WRITTEN_BOOK)
@@ -1231,7 +1257,7 @@ public enum ScriptActionType {
12311257
OverlayManager.getInstance().add("Unable to set text field value! (Unknown menu type)");
12321258
}
12331259
})),
1234-
1260+
12351261
RANDOM_INT(builder -> builder.name("Random Int")
12361262
.description("Generates a random whole number between two other numbers.")
12371263
.icon(Items.HOPPER)
@@ -1266,7 +1292,7 @@ public enum ScriptActionType {
12661292
new ScriptNumberValue(result)
12671293
);
12681294
})),
1269-
1295+
12701296
RANDOM_NUMBER(builder -> builder.name("Random Number")
12711297
.description("Generates a random number between two other numbers.")
12721298
.icon(Items.HOPPER)

0 commit comments

Comments
 (0)