slight tweaks, start looking into a potential way to sync videos

This commit is contained in:
GiantLuigi4 2023-06-01 22:39:28 -04:00
parent 7c7d008589
commit 4ac3edb4af
5 changed files with 142 additions and 84 deletions

View File

@ -179,7 +179,7 @@ public class ClientProxy extends SharedProxy implements IDisplayHandler, IJSQuer
throw new RuntimeException("MCEF is missing"); throw new RuntimeException("MCEF is missing");
mcef.registerDisplayHandler(this); mcef.registerDisplayHandler(this);
//mcef.registerJSQueryHandler(this); //TODO why crashing on this method! mcef.registerJSQueryHandler(this);
findAdvancementToProgressField(); findAdvancementToProgressField();
} }
@ -698,13 +698,17 @@ public class ClientProxy extends SharedProxy implements IDisplayHandler, IJSQuer
if(t - lastPointPacket >= 100) { if(t - lastPointPacket >= 100) {
lastPointPacket = t; lastPointPacket = t;
WDNetworkRegistry.INSTANCE.sendToServer(C2SMessageScreenCtrl.laserMove(tes, side, hit)); if (Minecraft.getInstance().player.isShiftKeyDown()) {
WDNetworkRegistry.INSTANCE.sendToServer(C2SMessageScreenCtrl.laserDown(tes, side, hit));
} else {
WDNetworkRegistry.INSTANCE.sendToServer(C2SMessageScreenCtrl.laserMove(tes, side, hit));
}
} }
} else { } else {
deselectScreen(); deselectScreen();
pointedScreen = tes; pointedScreen = tes;
pointedScreenSide = side; pointedScreenSide = side;
WDNetworkRegistry.INSTANCE.sendToServer(C2SMessageScreenCtrl.laserDown(tes, side, hit)); // WDNetworkRegistry.INSTANCE.sendToServer(C2SMessageScreenCtrl.laserDown(tes, side, hit));
} }
} }

View File

@ -218,6 +218,8 @@ public final class JSQueryDispatcher {
} }
private void registerDefaults() { private void registerDefaults() {
VideoType.registerQueries(this);
register("GetSize", (cb, tes, side, args) -> { register("GetSize", (cb, tes, side, args) -> {
Vector2i size = tes.getScreen(side).size; Vector2i size = tes.getScreen(side).size;
cb.success("{\"x\":" + size.x + ",\"y\":" + size.y + "}"); cb.success("{\"x\":" + size.x + ",\"y\":" + size.y + "}");

View File

@ -1074,11 +1074,12 @@ public class TileEntityScreen extends BlockEntity {
if (scr != null) { if (scr != null) {
if (down) { if (down) {
//Try to acquire laser lock //Try to acquire laser lock
if (getLaserUser(scr) == null) { // if (getLaserUser(scr) == null) {
scr.laserUser = ply; scr.laserUser = ply;
WDNetworkRegistry.INSTANCE.send(PacketDistributor.NEAR.with(() -> point(level, getBlockPos())), S2CMessageScreenUpdate.click(this, side, S2CMessageScreenUpdate.MOUSE_DOWN, pos)); WDNetworkRegistry.INSTANCE.send(PacketDistributor.NEAR.with(() -> point(level, getBlockPos())), S2CMessageScreenUpdate.click(this, side, S2CMessageScreenUpdate.MOUSE_CLICK, pos));
} // }
} else if (getLaserUser(scr) == ply) } else
// if (getLaserUser(scr) == ply)
WDNetworkRegistry.INSTANCE.send(PacketDistributor.NEAR.with(() -> point(level, getBlockPos())), S2CMessageScreenUpdate.click(this, side, S2CMessageScreenUpdate.MOUSE_MOVE, pos)); WDNetworkRegistry.INSTANCE.send(PacketDistributor.NEAR.with(() -> point(level, getBlockPos())), S2CMessageScreenUpdate.click(this, side, S2CMessageScreenUpdate.MOUSE_MOVE, pos));
} }
} }

View File

@ -107,7 +107,7 @@ public class C2SMessageScreenCtrl extends Packet {
public static C2SMessageScreenCtrl laserDown(TileEntityScreen tes, BlockSide side, Vector2i vec) { public static C2SMessageScreenCtrl laserDown(TileEntityScreen tes, BlockSide side, Vector2i vec) {
C2SMessageScreenCtrl ret = base(tes, side); C2SMessageScreenCtrl ret = base(tes, side);
ret.control = new LaserControl(LaserControl.ControlType.MOVE, vec); ret.control = new LaserControl(LaserControl.ControlType.DOWN, vec);
return ret; return ret;
} }

View File

@ -4,87 +4,138 @@
package net.montoyo.wd.utilities; package net.montoyo.wd.utilities;
import net.montoyo.wd.client.JSQueryDispatcher;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
public enum VideoType { public enum VideoType {
YOUTUBE("document.getElementById(\"movie_player\").setVolume(", ")"), YOUTUBE(
YOUTUBE_EMBED("document.getElementsByClassName(\"html5-video-player\")[0].setVolume(", ")"); "document.getElementById(\"movie_player\").",
new Function("setVolume(", ")"),
private final String volumePrefix; new Function("getCurrentTime(", ")"),
private final String volumeSuffix; new Function("seekTo(", ")")
private final int volumeCap; ),
YOUTUBE_EMBED(
VideoType(String prefix, String suffix) { "document.getElementsByClassName(\"html5-video-player\")[0].",
volumePrefix = prefix; new Function("setVolume(", ")"),
volumeSuffix = suffix; new Function("getCurrentTime(", ")"),
volumeCap = prefix.length() + 5 + suffix.length(); new Function("seekTo(", ")")
);
private final String base;
private final Function volume;
private final Function getTime;
private final Function setTime;
private final int volumeCap;
VideoType(
String base,
Function volume,
Function getTime,
Function setTime
) {
this.base = base;
this.volume = volume;
this.getTime = getTime;
this.setTime = setTime;
// lol, what?
volumeCap = volume.prefix.length() + 5 + volume.suffix.length();
}
public static void registerQueries(JSQueryDispatcher jsQueryDispatcher) {
// TODO: register GetTime query
} }
@Nullable protected static class Function {
public static VideoType getTypeFromURL(@Nonnull URL url) { String prefix, suffix;
String loHost = url.getHost().toLowerCase();
if(loHost.equals("youtu.be")) public Function(String prefix, String suffix) {
return url.getPath().length() > 1 ? YOUTUBE : null; this.prefix = prefix;
else if(!loHost.equals("www.youtube.com") && !loHost.equals("youtube.com")) this.suffix = suffix;
return null; }
String loPath = url.getPath().toLowerCase(); public String apply() {
if(loPath.equals("/watch")) { return prefix + suffix;
if(url.getQuery() != null && (url.getQuery().startsWith("v=") || url.getQuery().contains("&v="))) }
return YOUTUBE;
} else if(loPath.startsWith("/embed/")) public String apply(String arg) {
return loPath.length() > 7 ? YOUTUBE_EMBED : null; return prefix + arg + suffix;
}
return null; }
}
@Nullable
@Nullable public static VideoType getTypeFromURL(@Nonnull URL url) {
public static VideoType getTypeFromURL(@Nonnull String url) { String loHost = url.getHost().toLowerCase();
try { if (loHost.equals("youtu.be"))
return getTypeFromURL(new URL(url)); return url.getPath().length() > 1 ? YOUTUBE : null;
} catch(MalformedURLException ex) { else if (!loHost.equals("www.youtube.com") && !loHost.equals("youtube.com"))
return null; return null;
}
} String loPath = url.getPath().toLowerCase();
if (loPath.equals("/watch")) {
@Nonnull if (url.getQuery() != null && (url.getQuery().startsWith("v=") || url.getQuery().contains("&v=")))
public String getVideoIDFromURL(@Nonnull URL url) { return YOUTUBE;
if(this == YOUTUBE) { } else if (loPath.startsWith("/embed/"))
if(url.getHost().equalsIgnoreCase("youtu.be")) return loPath.length() > 7 ? YOUTUBE_EMBED : null;
return url.getPath().substring(1);
return null;
String args[] = url.getQuery().split("&"); }
for(String arg : args) {
if(arg.startsWith("v=")) @Nullable
return arg.substring(2); public static VideoType getTypeFromURL(@Nonnull String url) {
} try {
} else if(this == YOUTUBE_EMBED) return getTypeFromURL(new URL(url));
return url.getPath().substring(7); } catch (MalformedURLException ex) {
return null;
return ""; }
} }
@Nonnull @Nonnull
public String getURLFromID(@Nonnull String vid, boolean autoplay) { public String getVideoIDFromURL(@Nonnull URL url) {
String format; if (this == YOUTUBE) {
if(this == YOUTUBE) if (url.getHost().equalsIgnoreCase("youtu.be"))
format = autoplay ? "https://www.youtube.com/watch?v=%s&autoplay=1" : "https://www.youtube.com/watch?v=%s"; return url.getPath().substring(1);
else if(this == YOUTUBE_EMBED)
format = autoplay ? "https://www.youtube.com/embed/%s?autoplay=1" : "https://www.youtube.com/embed/%s"; String args[] = url.getQuery().split("&");
else for (String arg : args) {
return ""; if (arg.startsWith("v="))
return arg.substring(2);
return String.format(format, vid); }
} } else if (this == YOUTUBE_EMBED)
return url.getPath().substring(7);
// TODO: timestamp stuff
@Nonnull return "";
public String getVolumeJSQuery(int volInt, int volFrac) { }
return (new StringBuilder(volumeCap)).append(volumePrefix).append(volInt).append('.').append(volFrac).append(volumeSuffix).toString();
} @Nonnull
public String getURLFromID(@Nonnull String vid, boolean autoplay) {
String format;
if (this == YOUTUBE)
format = autoplay ? "https://www.youtube.com/watch?v=%s&autoplay=1" : "https://www.youtube.com/watch?v=%s";
else if (this == YOUTUBE_EMBED)
format = autoplay ? "https://www.youtube.com/embed/%s?autoplay=1" : "https://www.youtube.com/embed/%s";
else
return "";
return String.format(format, vid);
}
// TODO: timestamp stuff
@Nonnull
public String getVolumeJSQuery(int volInt, int volFrac) {
return volume.apply(volInt + "." + volFrac);
}
public String getTimeStampQuery() {
return getTime.apply();
}
public String setTimeStampQuery(float ts) {
return setTime.apply(String.valueOf(ts));
}
} }