// ==UserScript==
// @namespace runonstof
// @name ScriptManager
// @version 1.0.1
// @description Manage and download CustomNPCs scripts ingame!
// @author Runonstof
// @license MIT
// @minecraft 1.20.1
// @match https://customnpcs.com
// ==/UserScript==
// var JavaRunnable = Java.type('java.lang.Runnable');
var JavaThread = Java.type('java.lang.Thread');
var API = Java.type('noppes.npcs.api.NpcAPI').Instance();
var world = API.getIWorld('minecraft:overworld');
var HEX_COLORS = {
RED: 0xFF0000FF,
GREEN: 0x00FF00FF,
BLUE: 0x0000FFFF,
YELLOW: 0xFFFF00FF,
PURPLE: 0xFF00FFFF,
ORANGE: 0xFFA500FF,
PINK: 0xFFC0CBFF,
GRAY: 0x808080FF,
}
var Base64 = Java.type('java.util.Base64');
var JavaString = Java.type('java.lang.String');
function atob(str) {
return new JavaString(Base64.getDecoder().decode(new JavaString(str)));
}
function btoa(str) {
return new JavaString(Base64.getEncoder().encodeToString(new JavaString(str).getBytes()));
}
function hash(data) {
return btoa(JSON.stringify(data));
}
function dd(msg) {
world.broadcast(msg);
}
function chat(e) {
// Check if the player is in creative mode
if (e.player.gamemode !== 1) return;
if (e.message !== '!scripts') return;
e.setCanceled(true);
var scriptManager = new ScriptManager(e.player);
doAsync(function() {
fetchCategories();
dd('done!');
scriptManager.updateCategories();
});
}
var _GUI_IDS = {};
var items = [
'Item #1',
'Item #2',
'Item #3',
'Item #4',
'Item #5',
'Item #6',
'Item #7',
'Item #8',
'Item #9',
'Item #10',
'Item #11',
'Item #12',
'Item #13',
'Item #14',
'Item #15',
'Item #16',
'Item #17',
'Item #18',
'Item #19',
'Item #20',
'Item #21',
'Item #22',
'Item #23',
'Item #24',
'Item #25',
'Item #26',
'Item #27',
'Item #28',
'Item #29',
'Item #30',
];
var STATE = {
categories: null,
};
world.tempdata.put('STATE', STATE);
function fetchData(url, requestBody) {
try {
var URL = Java.type("java.net.URL");
var HttpURLConnection = Java.type("java.net.HttpURLConnection")
var OutputStreamWriter = Java.type("java.io.OutputStreamWriter")
var BufferedReader = Java.type("java.io.BufferedReader")
var InputStreamReader = Java.type("java.io.InputStreamReader")
var url = new URL(url)
var connection = url.openConnection()
connection.setRequestMethod("GET")
// connection.setRequestProperty("Content-Type", "application/json")
connection.setDoOutput(false)
if (requestBody) {
var writer = new OutputStreamWriter(connection.getOutputStream(), "UTF-8")
writer.write(JSON.stringify(requestBody))
writer.flush()
writer.close()
}
var responseCode = connection.getResponseCode()
world.tempdata.put('res', connection);
if (responseCode >= 200 && responseCode < 300) {
var reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))
var response = ""
var line;
while ((line = reader.readLine()) != null) {
response += line
}
reader.close()
var jsonResponse = JSON.parse(response)
return jsonResponse;
} else {
dd("§cError! Response code: " + responseCode)
}
connection.disconnect()
} catch (e) {
dd("§cError: " + e.message)
}
}
function doAsync(callback) {
var thread = new JavaThread(function () {
var result;
try {
result = callback();
} catch (e) {
dd("§cError: " + e.message)
}
return result;
});
thread.start();
return thread;
}
function fetchCategories() {
STATE.categories = fetchData('https://api.greasyfork.org/en/scripts/by-site/customnpcs.com.json');
}
function id(name) {
// generate random name if name is not provided
if (!name) {
name = Math.random().toString(36).substring(2, 15);
}
return _GUI_IDS[name] = _GUI_IDS[name] || Object.keys(_GUI_IDS).length + 1;
}
function Group(callback, x, y, width, height) {
typeof x === 'undefined' ? x = 0 : x;
typeof y === 'undefined' ? y = 0 : y;
typeof width === 'undefined' ? width = 0 : width;
typeof height === 'undefined' ? height = 0 : height;
var sumWith = function (base) {
typeof base === 'undefined' ? base = 0 : base;
return function (value) {
typeof value === 'undefined' ? value = 0 : value;
return base + value;
};
};
return callback(sumWith(x), sumWith(y), sumWith(width), sumWith(height));
}
function ScriptManager(player) {
this.gui = API.createCustomGui(1, 384, 256, false, player);
this.build = function () {
Group(function (x, y, width, height) {
this.gui.addLabel(id('lbl_title'), '§eScriptManager', x(), y(-15), x(this.gui.getWidth()), y(16))
.setCentered(false)
.setScale(2);
Group(function (x, y, width, height) {
this.gui.addColoredLine(id('ln_top'), x(0), y(0), width(), y(0), HEX_COLORS.PURPLE, 2);
this.gui.addColoredLine(id('ln_right'), width(), y(0), width(), y() + height(), HEX_COLORS.RED, 2);
this.gui.addColoredLine(id('ln_bottom'), x(0), y() + height(), width(), y() + height(), HEX_COLORS.GREEN, 2);
this.gui.addColoredLine(id('ln_left'), x(0), y(0), x(0), y() + height(), HEX_COLORS.BLUE, 2);
var scroll = this.gui.addScroll(id('scrl_categories'), x(0), y(0), x(64), height(), []);
scroll.setHasSearch(false)
.setVisible(STATE.categories !== null)
// scroll.setList(STATE.categories);
}.bind(this), x(), y(32), width(), height(-32));
}.bind(this), 0, 0, this.gui.getWidth(), this.gui.getHeight());
};
this.updateCategories = function () {
var scroll = this.gui.getComponent(id('scrl_categories'));
if (!STATE.categories) return;
var categories = STATE.categories.map(function (category) {
return category.name;
});
scroll.setList(categories);
scroll.setVisible(true);
this.gui.update(scroll);
this.gui.update();
}
this.build();
player.showCustomGui(this.gui);
}
function customGuiScroll(e) {
dd("customGuiScroll")
// if (e.doubleClick) {
// items.splice(e.scrollIndex, 1);
// } else {
// items.push('Item #' + (items.length + 1));
// }
// e.scroll.setList(items);
// e.gui.update(e.scroll);
}