package ru.will0376.api.gui;
import org.lwjgl.input.Keyboard;
import cpw.mods.fml.common.FMLLog;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiMainMenu;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.GuiSelectWorld;
import net.minecraft.client.gui.GuiTextField;
import net.minecraft.client.multiplayer.ServerData;
import net.minecraft.client.resources.I18n;
import ru.will0376.api.main;
import javazoom.jl.player.*;
import java.io.*;
import java.util.*;
import java.net.URL;
public class radiogui extends GuiScreen { // Пример брал с гуи добавления сервера в мультиплеер.
private final GuiScreen prgs;
private Boolean prpl;
private GuiTextField test;
private GuiTextField url;
static radioplay rplay = new radioplay();
private boolean alive = false;
public Thread th = new Thread(rplay);
public radiogui(custGuiMainMenu gs, GuiScreen gs2) {
if (gs == null)
this.prgs = gs;
else
this.prgs = gs2; //Просто ппц какой костыль.(но работает...лол)
//this.th ;
}
public void updateScreen()
{
this.url.updateCursorCounter();
}
public void initth() {
th.setName("radio");
th.setDaemon(true);
th.start();
rplay.init = true;
FMLLog.getLogger().info(main.Radio + " initTh. " + alive);
}
public void initGui()
{
Keyboard.enableRepeatEvents(true);
this.buttonList.clear();
this.buttonList.add(new GuiButton(0, this.width / 2 - 100, this.height / 4 + 96 + 18, I18n.format("radio.play", new Object[0])));
this.buttonList.add(new GuiButton(1, this.width / 2 - 100, this.height / 4 + 120 + 18, I18n.format("radio.back", new Object[0])));
this.buttonList.add(new GuiButton(2, this.width / 2 - 100, this.height / 4 + 72, I18n.format("radio.stop", new Object[0])));
this.buttonList.add(new GuiButton(2, this.width / 2 - 50, this.height / 4 + 0, I18n.format("test.button", new Object[0])));
/* this.test = new GuiTextField(this.fontRendererObj, this.width / 2 - 100, 66, 200, 20);
this.test.setFocused(true);
//this.test.setText(this.prsd.serverName);*/
this.url = new GuiTextField(this.fontRendererObj, this.width / 2 - 100, 106, 200, 20);
this.url.setMaxStringLength(128);
this.url.setText(radioplay.URL);
if (!rplay.enableplay) {
//((GuiButton)this.buttonList.get(2)).enabled = false; //на время тестов.
}
if (rplay.enableplay) {
((GuiButton)this.buttonList.get(0)).enabled = false;
}
// ((GuiButton)this.buttonList.get(0)).enabled = this.field_146308_f.getText().length() > 0 && this.field_146308_f.getText().split(":").length > 0 && this.field_146309_g.getText().length() > 0;
//Пример
}
public void onGuiClosed()
{
Keyboard.enableRepeatEvents(false);
}
public boolean enable;
protected void actionPerformed(GuiButton p_146284_1_)
{
if (p_146284_1_.enabled)
{
if (p_146284_1_.id == 2)
{
rplay.enableplay = false;
th.stop();
rplay.stop = true;
this.alive = false;
this.mc.displayGuiScreen(this);
}
else if (p_146284_1_.id == 1)
{
this.mc.displayGuiScreen(this.prgs);
//this.prgs.confirmClicked(false, 0); // не сработало... хотя хз, работает через костыль и норм.
}
else if (p_146284_1_.id == 0)
{
rplay.enableplay = true;
this.alive = th.isAlive();
if (!alive) {
initth();
}
//rplay.URL = "http://ep256.streamr.ru";// для теста
rplay.URL = this.url.getText();
// this.prgs.confirmClicked(true, 0);
this.mc.displayGuiScreen(this);
}
else if (p_146284_1_.id == 3) {
}
}
}
protected void keyTyped(char p_73869_1_, int p_73869_2_)
{
this.url.textboxKeyTyped(p_73869_1_, p_73869_2_);
if (p_73869_2_ == 15)
{
this.url.setFocused(!this.url.isFocused());
}
if (p_73869_2_ == 28 || p_73869_2_ == 156)
{
this.actionPerformed((GuiButton)this.buttonList.get(0));
}
// ((GuiButton)this.buttonList.get(0)).enabled = this.url.getText().length() > 0 && this.url.getText().split(":").length > 0 && this.url.getText().length() > 0;
}
protected void mouseClicked(int p_73864_1_, int p_73864_2_, int p_73864_3_)
{
super.mouseClicked(p_73864_1_, p_73864_2_, p_73864_3_);
this.url.mouseClicked(p_73864_1_, p_73864_2_, p_73864_3_);
}
public void drawScreen(int p_73863_1_, int p_73863_2_, float p_73863_3_)
{
this.drawWorldBackground(40);
// this.drawCenteredString(this.fontRendererObj, I18n.format("addServer.title", new Object[0]), this.width / 2, 17, 16777215);
// this.drawString(this.fontRendererObj, I18n.format("addServer.enterName", new Object[0]), this.width / 2 - 100, 53, 10526880);
this.drawString(this.fontRendererObj, I18n.format("radio.Url", new Object[0]), this.width / 2 - 100, 94, 10526880);
// this.test.drawTextBox();
this.url.drawTextBox();
super.drawScreen(p_73863_1_, p_73863_2_, p_73863_3_);
}
}
class radioplay implements Runnable
{
public static String URL = "";
public static boolean stop = false;
public static boolean enableplay = false;
public static boolean init = false;
private Player player;
private void running() {
FMLLog.getLogger().info(main.Radio + init + " , " + enableplay);
}
public void run()
{
FMLLog.getLogger().info(main.Radio + " URL: " + this.URL);
FMLLog.getLogger().info(main.Radio + init + " , " + enableplay);
try {
URL url1 = new URL(this.URL);
InputStream fin = url1.openStream();
InputStream is = new BufferedInputStream(fin);
player = new Player(is);
FMLLog.getLogger().info(main.Radio + " URL: " + this.URL);
if (stop) //убить поток. не работает.
{
FMLLog.getLogger().info(main.Radio + " killed!");
player.close();
return ;
}
if(enableplay)
player.play();
else
player.close();
}
catch (FileNotFoundException e)
{
FMLLog.getLogger().info("Url %s не найден:", this.URL);
}
catch (Exception e)
{
FMLLog.getLogger().info("При проигрывании с потока %s возникла следующая ошибка:", this.URL);
FMLLog.getLogger().info(e.toString());
}
}
}