* Movies volume/distance rework

This commit is contained in:
Nicolas BARBOTIN 2018-01-30 22:07:27 +01:00
parent 8dc5a0e5d6
commit 63ba61a138
5 changed files with 63 additions and 39 deletions

View File

@ -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

View File

@ -142,19 +142,10 @@ public class WebDisplays {
@SubscribeEvent
public void onRegisterSounds(RegistryEvent.Register<SoundEvent> 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<SoundEvent> ev, String resName) {
ResourceLocation resLoc = new ResourceLocation("webdisplays", resName);
SoundEvent ret = new SoundEvent(resLoc);
ret.setRegistryName(resLoc);
ev.getRegistry().register(ret);
return ret;
}
}

View File

@ -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) {

View File

@ -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<NameUUIDPair> 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<Screen> 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;
}
}
}

View File

@ -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) {