From 63ba61a13817a5faeeb056b684f09af99201ec23 Mon Sep 17 00:00:00 2001 From: Nicolas BARBOTIN Date: Tue, 30 Jan 2018 22:07:27 +0100 Subject: [PATCH] * Movies volume/distance rework --- README.md | 1 - src/main/java/net/montoyo/wd/WebDisplays.java | 26 +++++------ .../net/montoyo/wd/client/ClientProxy.java | 19 ++++++-- .../montoyo/wd/entity/TileEntityScreen.java | 44 ++++++++++--------- .../net/montoyo/wd/utilities/VideoType.java | 12 ++++- 5 files changed, 63 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index 4c02918..312bdfd 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,6 @@ This is the unfinished port of the WebDisplays mod for Minecraft 1.12.2. The tex * Read config (see "Config elements" below) ### TODO -* VideoType doesn't seem to be used... * Achievements (minePad 2 and all that stuff) * Top/bottom screen orientation * GuiSetURL2 missing buttons diff --git a/src/main/java/net/montoyo/wd/WebDisplays.java b/src/main/java/net/montoyo/wd/WebDisplays.java index 466f563..1c73eb6 100644 --- a/src/main/java/net/montoyo/wd/WebDisplays.java +++ b/src/main/java/net/montoyo/wd/WebDisplays.java @@ -142,19 +142,10 @@ public class WebDisplays { @SubscribeEvent public void onRegisterSounds(RegistryEvent.Register ev) { - soundTyping = new SoundEvent(new ResourceLocation("webdisplays", "keyboardType")); - soundTyping.setRegistryName(soundTyping.getSoundName()); - - soundUpgradeAdd = new SoundEvent(new ResourceLocation("webdisplays", "upgradeAdd")); - soundUpgradeAdd.setRegistryName(soundUpgradeAdd.getSoundName()); - - soundUpgradeDel = new SoundEvent(new ResourceLocation("webdisplays", "upgradeDel")); - soundUpgradeDel.setRegistryName(soundUpgradeDel.getSoundName()); - - soundScreenCfg = new SoundEvent(new ResourceLocation("webdisplays", "screencfgOpen")); - soundScreenCfg.setRegistryName(soundScreenCfg.getSoundName()); - - ev.getRegistry().registerAll(soundTyping, soundUpgradeAdd, soundUpgradeDel, soundScreenCfg); + soundTyping = registerSound(ev, "keyboardType"); + soundUpgradeAdd = registerSound(ev, "upgradeAdd"); + soundUpgradeDel = registerSound(ev, "upgradeDel"); + soundScreenCfg = registerSound(ev, "screencfgOpen"); } @SubscribeEvent @@ -204,4 +195,13 @@ public class WebDisplays { return INSTANCE.lastPadId++; } + private static SoundEvent registerSound(RegistryEvent.Register ev, String resName) { + ResourceLocation resLoc = new ResourceLocation("webdisplays", resName); + SoundEvent ret = new SoundEvent(resLoc); + ret.setRegistryName(resLoc); + + ev.getRegistry().register(ret); + return ret; + } + } diff --git a/src/main/java/net/montoyo/wd/client/ClientProxy.java b/src/main/java/net/montoyo/wd/client/ClientProxy.java index 5706893..5535f3c 100644 --- a/src/main/java/net/montoyo/wd/client/ClientProxy.java +++ b/src/main/java/net/montoyo/wd/client/ClientProxy.java @@ -13,7 +13,6 @@ import net.minecraft.client.renderer.texture.TextureMap; import net.minecraft.client.resources.IResourceManager; import net.minecraft.client.resources.IResourceManagerReloadListener; import net.minecraft.client.resources.SimpleReloadableResourceManager; -import net.minecraft.client.settings.KeyBinding; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; @@ -28,6 +27,7 @@ import net.minecraft.world.World; import net.minecraftforge.client.event.*; import net.minecraftforge.client.model.ModelLoader; import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.event.world.WorldEvent; import net.minecraftforge.fml.client.registry.ClientRegistry; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import net.minecraftforge.fml.common.gameevent.TickEvent; @@ -49,7 +49,6 @@ import net.montoyo.wd.entity.TileEntityScreen; import net.montoyo.wd.net.SMessagePadCtrl; import net.montoyo.wd.net.SMessageScreenCtrl; import net.montoyo.wd.utilities.*; -import org.lwjgl.Sys; import java.util.ArrayList; import java.util.HashMap; @@ -215,13 +214,16 @@ public class ClientProxy extends SharedProxy implements IResourceManagerReloadLi if(browser != null) { long t = System.currentTimeMillis(); - for(PadData pd : padList) { + for(PadData pd: padList) { if(pd.view == browser && t - pd.lastURLSent >= 1000) { pd.lastURLSent = t; //Avoid spamming the server with porn URLs WebDisplays.NET_HANDLER.sendToServer(new SMessagePadCtrl(pd.id, url)); break; } } + + for(TileEntityScreen tes: screenTracking) + tes.updateClientSideURL(browser, url); } } @@ -389,6 +391,17 @@ public class ClientProxy extends SharedProxy implements IResourceManagerReloadLi ev.setCanceled(true); } + @SubscribeEvent + public void onWorldUnload(WorldEvent.Unload ev) { + Log.info("World unloaded; killing screens..."); + int dim = ev.getWorld().provider.getDimension(); + + for(int i = screenTracking.size() - 1; i >= 0; i--) { + if(screenTracking.get(i).getWorld().provider.getDimension() == dim) //Could be world == ev.getWorld() + screenTracking.remove(i).unload(); + } + } + /**************************************** OTHER METHODS ****************************************/ private void laserClick(TileEntityScreen tes, BlockSide side, TileEntityScreen.Screen scr, Vector2i hit) { diff --git a/src/main/java/net/montoyo/wd/entity/TileEntityScreen.java b/src/main/java/net/montoyo/wd/entity/TileEntityScreen.java index b36ba8c..9a5867e 100644 --- a/src/main/java/net/montoyo/wd/entity/TileEntityScreen.java +++ b/src/main/java/net/montoyo/wd/entity/TileEntityScreen.java @@ -40,14 +40,11 @@ public class TileEntityScreen extends TileEntity { public static class Screen { - private static final String YT_REGEX1 = "^https?\\://(?:www\\.)?youtube\\.com/watch.+$"; //TODO: Fix embedded videos sound/distance - private static final String YT_REGEX2 = "^https?\\://(?:www\\.)?youtu\\.be/[a-zA-Z0-9_\\-]+.*$"; - public BlockSide side; public Vector2i size; public Vector2i resolution; public String url; - public boolean isYouTube = false; + private VideoType videoType; public NameUUIDPair owner; public ArrayList friends; public int friendRights; @@ -59,17 +56,13 @@ public class TileEntityScreen extends TileEntity { public EntityPlayer laserUser; public final Vector2i lastMousePos = new Vector2i(); - public static boolean isYouTubeURL(String url) { - return url.matches(YT_REGEX1) || url.matches(YT_REGEX2); - } - public static Screen deserialize(NBTTagCompound tag) { Screen ret = new Screen(); ret.side = BlockSide.values()[tag.getByte("Side")]; ret.size = new Vector2i(tag.getInteger("Width"), tag.getInteger("Height")); ret.resolution = new Vector2i(tag.getInteger("ResolutionX"), tag.getInteger("ResolutionY")); ret.url = tag.getString("URL"); - ret.isYouTube = isYouTubeURL(ret.url); + ret.videoType = VideoType.getTypeFromURL(ret.url); if(ret.resolution.x <= 0 || ret.resolution.y <= 0) { float psx = ((float) ret.size.x) * 16.f - 4.f; @@ -165,7 +158,7 @@ public class TileEntityScreen extends TileEntity { private ArrayList screens = new ArrayList<>(); private AxisAlignedBB renderBB = new AxisAlignedBB(0.0, 0.0, 0.0, 1.0, 1.0, 1.0); private boolean loaded = true; - public float ytVolume = 100.0f; + public float ytVolume = Float.POSITIVE_INFINITY; public boolean isLoaded() { return loaded; @@ -292,7 +285,7 @@ public class TileEntityScreen extends TileEntity { } scr.url = url; - scr.isYouTube = Screen.isYouTubeURL(url); + scr.videoType = VideoType.getTypeFromURL(url); if(world.isRemote) { if(scr.browser != null) @@ -472,13 +465,15 @@ public class TileEntityScreen extends TileEntity { public void updateTrackDistance(double d) { boolean needsComputation = true; - float vol; - String jsCode = null; + int intPart = 0; //Need to initialize those because the compiler is stupid + int fracPart = 0; for(Screen scr: screens) { - if(scr.isYouTube && scr.browser != null && !scr.browser.isPageLoading()) { + if(scr.videoType != null && scr.browser != null && !scr.browser.isPageLoading()) { if(needsComputation) { float dist = (float) Math.sqrt(d); + float vol; + if(dist <= 10.f) vol = 100.f; else if(dist >= 30.f) @@ -490,16 +485,23 @@ public class TileEntityScreen extends TileEntity { return; //Delta is too small ytVolume = vol; - int intPart = (int) vol; //Manually convert to string, probably faster in that case... - int fracPart = ((int) (vol * 100.f)) - intPart * 100; - //jsCode = "yt.player.getPlayerByElement(document.getElementById(\"movie_player\")).setVolume(" + intPart + '.' + fracPart + ')'; - //jsCode = "console.log(document.getElementById(\"movie_player\"))"; - jsCode = "document.getElementById(\"movie_player\").setVolume(" + intPart + '.' + fracPart + ')'; - //Log.info(jsCode); + intPart = (int) vol; //Manually convert to string, probably faster in that case... + fracPart = ((int) (vol * 100.f)) - intPart * 100; needsComputation = false; } - scr.browser.runJS(jsCode, ""); + scr.browser.runJS(scr.videoType.getVolumeJSQuery(intPart, fracPart), ""); + } + } + } + + public void updateClientSideURL(IBrowser target, String url) { + for(Screen scr: screens) { + if(scr.browser == target) { + scr.url = url; //FIXME: This is an invalid fix for something that CANNOT be fixed + scr.videoType = VideoType.getTypeFromURL(url); + ytVolume = Float.POSITIVE_INFINITY; //Force volume update + break; } } } diff --git a/src/main/java/net/montoyo/wd/utilities/VideoType.java b/src/main/java/net/montoyo/wd/utilities/VideoType.java index e4233a4..28176c5 100644 --- a/src/main/java/net/montoyo/wd/utilities/VideoType.java +++ b/src/main/java/net/montoyo/wd/utilities/VideoType.java @@ -6,12 +6,13 @@ package net.montoyo.wd.utilities; import javax.annotation.Nonnull; import javax.annotation.Nullable; +import java.net.MalformedURLException; import java.net.URL; public enum VideoType { YOUTUBE("document.getElementById(\"movie_player\").setVolume(", ")"), - YOUTUBE_EMBED("", ""); + YOUTUBE_EMBED("document.getElementsByClassName(\"html5-video-player\")[0].setVolume(", ")"); private final String volumePrefix; private final String volumeSuffix; @@ -41,6 +42,15 @@ public enum VideoType { return null; } + @Nullable + public static VideoType getTypeFromURL(@Nonnull String url) { + try { + return getTypeFromURL(new URL(url)); + } catch(MalformedURLException ex) { + return null; + } + } + @Nonnull public String getVideoIDFromURL(@Nonnull URL url) { if(this == YOUTUBE) {