some screenshare

This commit is contained in:
Chayapak 2023-09-19 21:04:14 +07:00
parent a8f2dd59f0
commit 11927ac373
3 changed files with 243 additions and 222 deletions

View file

@ -126,7 +126,7 @@ public class Bot {
this.tag = new TagPlugin(this);
this.world = new WorldPlugin(this);
this.auth = new AuthPlugin(this);
this.screenshare = new ScreensharePlugin(this);
// this.screenshare = new ScreensharePlugin(this);
for (Listener listener : listeners) listener.loadedPlugins();

View file

@ -1,76 +1,87 @@
package land.chipmunk.chayapak.chomens_bot.commands;
public class ScreenshareCommand { // extends Command {
// public ScreenshareCommand () {
// super(
// "screenshare",
// "Shares my screen",
// new String[] {
// "<start> <x> <y> <z>",
// "<stop>",
// "<setres> <width> <height>",
// "<setfps> <fps>"
// },
// new String[] {},
// TrustLevel.TRUSTED,
// false
// );
// }
import land.chipmunk.chayapak.chomens_bot.Bot;
import land.chipmunk.chayapak.chomens_bot.command.Command;
import land.chipmunk.chayapak.chomens_bot.command.CommandContext;
import land.chipmunk.chayapak.chomens_bot.command.CommandException;
import land.chipmunk.chayapak.chomens_bot.command.TrustLevel;
import land.chipmunk.chayapak.chomens_bot.util.ColorUtilities;
import net.kyori.adventure.text.Component;
import org.cloudburstmc.math.vector.Vector3i;
// @Override
// public Component execute(CommandContext context) throws CommandException {
// final Bot bot = context.bot;
//
// try {
// switch (args[0]) {
// case "start" -> {
// final int x = Integer.parseInt(args[1]);
// final int y = Integer.parseInt(args[2]);
// final int z = Integer.parseInt(args[3]);
//
// bot.screenshare.start(Vector3i.from(x, y, z));
//
// return Component
// .text("Started screen sharing")
// .color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor));
// }
// case "stop" -> {
// bot.screenshare.stop();
//
// return Component
// .text("Stopped screen sharing")
// .color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor));
// }
// case "setres" -> {
// final int width = Integer.parseInt(args[1]);
// final int height = Integer.parseInt(args[2]);
//
// bot.screenshare.width = width;
// bot.screenshare.height = height;
//
// bot.screenshare.screen.screen = new String[width][height];
//
// return Component
// .text("Set the resolution to ")
// .append(Component.text(width + "x" + height).color(ColorUtilities.getColorByString(bot.config.colorPalette.string)))
// .color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor));
// }
// case "setfps" -> {
// final int fps = Integer.parseInt(args[1]);
//
// bot.screenshare.fps = fps;
//
// return Component
// .text("Set the FPS to ")
// .append(Component.text(fps).color(ColorUtilities.getColorByString(bot.config.colorPalette.number)))
// .color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor));
// }
// default -> {
// throw new CommandException(Component.text("Invalid action"));
// }
// }
// } catch (NumberFormatException e) {
// throw new CommandException(Component.text("Invalid integer"));
// }
// }
public class ScreenshareCommand extends Command {
public ScreenshareCommand () {
super(
"screenshare",
"Shares my screen",
new String[] {
"<start> <x> <y> <z>",
"<stop>",
"<setres> <width> <height>",
"<setfps> <fps>"
},
new String[] {},
TrustLevel.TRUSTED,
false
);
}
@Override
public Component execute(CommandContext context) throws CommandException {
final Bot bot = context.bot;
final String action = context.getString(false, true);
try {
switch (action) {
case "start" -> {
final int x = context.getInteger(true);
final int y = context.getInteger(true);
final int z = context.getInteger(true);
bot.screenshare.start(Vector3i.from(x, y, z));
return Component
.text("Started screen sharing")
.color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor));
}
case "stop" -> {
bot.screenshare.stop();
return Component
.text("Stopped screen sharing")
.color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor));
}
case "setres" -> {
final int width = context.getInteger(true);
final int height = context.getInteger(true);
bot.screenshare.width = width;
bot.screenshare.height = height;
bot.screenshare.screen.screen = new String[width][height];
return Component
.text("Set the resolution to ")
.append(Component.text(width + "x" + height).color(ColorUtilities.getColorByString(bot.config.colorPalette.string)))
.color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor));
}
case "setfps" -> {
final int fps = context.getInteger(true);
bot.screenshare.fps = fps;
return Component
.text("Set the FPS to ")
.append(Component.text(fps).color(ColorUtilities.getColorByString(bot.config.colorPalette.number)))
.color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor));
}
default -> {
throw new CommandException(Component.text("Invalid action"));
}
}
} catch (NumberFormatException e) {
throw new CommandException(Component.text("Invalid integer"));
}
}
}

View file

@ -1,38 +1,48 @@
package land.chipmunk.chayapak.chomens_bot.plugins;
import land.chipmunk.chayapak.chomens_bot.Bot;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import org.cloudburstmc.math.vector.Vector3i;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
public class ScreensharePlugin {
private final Bot bot;
// private ScheduledFuture<?> future;
//
// public int fps = 15;
//
// public int width = 35;
// public int height = 18;
//
// public Screen screen;
//
// public Robot robot;
//
private ScheduledFuture<?> future;
public int fps = 15;
public int width = 35;
public int height = 18;
public Screen screen;
public Robot robot;
// public FFmpegFrameGrabber grabber;
public ScreensharePlugin (Bot bot) {
this.bot = bot;
// try {
// robot = new Robot();
// } catch (AWTException e) {
// e.printStackTrace();
// }
try {
robot = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
}
//
// public void start (Vector3i position) {
// screen = new Screen(bot, width, height, position);
//
// screen.update();
//
public void start (Vector3i position) {
screen = new Screen(bot, width, height, position);
screen.update();
// try (FFmpegFrameGrabber grabber = new FFmpegFrameGrabber("/tmp/rick.mp4")) {
// this.grabber = grabber;
// } catch (Exception e) {
@ -40,138 +50,138 @@ public class ScreensharePlugin {
// }
//
// try { grabber.start(); } catch (Exception ignored) {}
//
// future = bot.executor.scheduleAtFixedRate(this::drawScreen, 0, 1000 / fps, TimeUnit.MILLISECONDS); // frame. per. second.
// }
//
// public void stop () {
// future.cancel(false);
//
// screen.kill();
// }
//
// private void drawScreen () {
//// Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
//
//// BufferedImage capture = robot.createScreenCapture(screenRect);
//
future = bot.executor.scheduleAtFixedRate(this::drawScreen, 0, 1000 / fps, TimeUnit.MILLISECONDS); // frame. per. second.
}
public void stop () {
future.cancel(false);
screen.kill();
}
private void drawScreen () {
Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage capture = robot.createScreenCapture(screenRect);
// try (Java2DFrameConverter frameConverter = new Java2DFrameConverter()) {
// final Frame grabbed = grabber.grab();
//
// final BufferedImage capture = frameConverter.convert(grabbed);
//
// if (capture == null) return;
//
// BufferedImage resized = resize(capture, screen.width, screen.height);
//
// for (int y = 0; y < resized.getHeight(); y++) {
// for (int x = 0; x < resized.getWidth(); x++) {
// int rgba = resized.getRGB(x, y);
// int red = (rgba >> 16) & 255;
// int green = (rgba >> 8) & 255;
// int blue = rgba & 255;
//
// screen.screen[x][y] = String.format("#%02x%02x%02x", red, green, blue);
// }
// }
//
// screen.draw();
if (capture == null) return;
BufferedImage resized = resize(capture, screen.width, screen.height);
for (int y = 0; y < resized.getHeight(); y++) {
for (int x = 0; x < resized.getWidth(); x++) {
int rgba = resized.getRGB(x, y);
int red = (rgba >> 16) & 255;
int green = (rgba >> 8) & 255;
int blue = rgba & 255;
screen.screen[x][y] = String.format("#%02x%02x%02x", red, green, blue);
}
}
screen.draw();
// } catch (Exception e) {
// System.err.println("EXCEPTION ::::");
// e.printStackTrace();
// }
// }
//
// // move this to util?
// private BufferedImage resize(BufferedImage img, int newW, int newH) {
// Image tmp = img.getScaledInstance(newW, newH, Image.SCALE_SMOOTH);
// BufferedImage dimg = new BufferedImage(newW, newH, BufferedImage.TYPE_INT_ARGB);
//
// Graphics2D g2d = dimg.createGraphics();
// g2d.drawImage(tmp, 0, 0, null);
// g2d.dispose();
//
// return dimg;
// }
//
// public static class Screen {
// private final Bot bot;
//
// public String[][] screen;
// public int width;
// public int height;
// public Vector3i pos;
//
// public ArrayList<String> tags = new ArrayList<>();
//
// public Screen(Bot bot, int width, int height, Vector3i pos) {
// screen = new String[width][height];
//
// this.bot = bot;
//
// this.width = width;
// this.height = height;
//
// this.pos = pos;
// }
//
// public void draw () {
// final ArrayList<Component> names = new ArrayList<>();
//
// for (int y = 0; y < height; y++) {
// Component name = Component.empty();
//
// for (int x = 0; x < width; x++) {
// final Component pixel = Component.text("").color(TextColor.fromHexString(screen[x][y]));
//
// name = name.append(pixel);
// }
//
// names.add(name);
// }
//
// for (int i = 0; i < names.size(); i++) {
// bot.core.run("minecraft:data merge entity @e[tag=" + tags.get(i) + ",limit=1] {text:'" + GsonComponentSerializer.gson().serialize(names.get(i)) + "'}");
// }
// }
//
// public void kill () {
// for(String i : tags) {
// bot.core.run("minecraft:kill @e[tag=" + i + "]");
// }
//
// tags.clear();
// }
//
// public void update() {
// double startY = pos.getY();
//
// kill();
//
// for (int i = 0; i < this.height; i++) {
// final String actualTag = "chomens_bot_" + Math.random();
//
// tags.add(actualTag);
// startY -= 0.3f;
//
// bot.core.run(
// String.format(
// "minecraft:summon minecraft:text_display %s %s %s %s",
// pos.getX(),
// startY,
// pos.getZ(),
// "{Tags:[\"" + actualTag + "\"],text:'\"\"',line_width:32767}"
// )
// );
// }
// }
//
// public void setPixel(String hexColor, int x, int y) { screen[x][y] = hexColor; }
//
// public void setRow(String[] hexColor, int row) {
// for (int x = 0; x < width; x++) {
// screen[x][row] = hexColor[x];
// }
// }
// }
}
// move this to util?
private BufferedImage resize(BufferedImage img, int newW, int newH) {
Image tmp = img.getScaledInstance(newW, newH, Image.SCALE_SMOOTH);
BufferedImage dimg = new BufferedImage(newW, newH, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = dimg.createGraphics();
g2d.drawImage(tmp, 0, 0, null);
g2d.dispose();
return dimg;
}
public static class Screen {
private final Bot bot;
public String[][] screen;
public int width;
public int height;
public Vector3i pos;
public ArrayList<String> tags = new ArrayList<>();
public Screen(Bot bot, int width, int height, Vector3i pos) {
screen = new String[width][height];
this.bot = bot;
this.width = width;
this.height = height;
this.pos = pos;
}
public void draw () {
final ArrayList<Component> names = new ArrayList<>();
for (int y = 0; y < height; y++) {
Component name = Component.empty();
for (int x = 0; x < width; x++) {
final Component pixel = Component.text("").color(TextColor.fromHexString(screen[x][y]));
name = name.append(pixel);
}
names.add(name);
}
for (int i = 0; i < names.size(); i++) {
bot.core.run("minecraft:data merge entity @e[tag=" + tags.get(i) + ",limit=1] {text:'" + GsonComponentSerializer.gson().serialize(names.get(i)) + "'}");
}
}
public void kill () {
for(String i : tags) {
bot.core.run("minecraft:kill @e[tag=" + i + "]");
}
tags.clear();
}
public void update() {
double startY = pos.getY();
kill();
for (int i = 0; i < this.height; i++) {
final String actualTag = "chomens_bot_" + Math.random();
tags.add(actualTag);
startY -= 0.3f;
bot.core.run(
String.format(
"minecraft:summon minecraft:text_display %s %s %s %s",
pos.getX(),
startY,
pos.getZ(),
"{Tags:[\"" + actualTag + "\"],text:'\"\"',line_width:32767}"
)
);
}
}
public void setPixel(String hexColor, int x, int y) { screen[x][y] = hexColor; }
public void setRow(String[] hexColor, int row) {
for (int x = 0; x < width; x++) {
screen[x][row] = hexColor[x];
}
}
}
}