Follow along with the video below to see how to install our site as a web app on your home screen.
Примечание: This feature may not be available in some browsers.
package ru.nelegal.custommenumod.handlermenu;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import net.minecraft.client.gui.GuiMainMenu;
import net.minecraftforge.client.event.GuiOpenEvent;
public class EventHandlerMenu {
@SubscribeEvent
public void onGuiOpen(GuiOpenEvent event) {
if (event.gui instanceof GuiMainMenu) {
event.gui = new ru.nelegal.custommenumod.CustomMainMenu();
}
}
}
package ru.nelegal.custommenumod;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.*;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.event.GuiOpenEvent;
import net.minecraftforge.common.MinecraftForge;
import org.lwjgl.opengl.Display;
import ru.nelegal.custommenumod.handlermenu.EventHandlerMenu;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
@Mod(modid = "custommenumod", name = "CustomMenu", version = "1.0")
public class CustomMainMenu extends GuiScreen {
private static final ResourceLocation BACKGROUND_TEXTURE = new ResourceLocation("custommenumod", "textures/gui/background.png");
@Mod.EventHandler
public void init(FMLInitializationEvent event) {
Display.setTitle("SCN");
setCustomIcon();
MinecraftForge.EVENT_BUS.register(new EventHandlerMenu());
}
private void setCustomIcon() {
try {
InputStream icon16 = Minecraft.getMinecraft().getResourceManager().getResource(
new ResourceLocation("custommenumod", "icons/icon_16x16.png")
).getInputStream();
InputStream icon32 = Minecraft.getMinecraft().getResourceManager().getResource(
new ResourceLocation("custommenumod", "icons/icon_32x32.png")
).getInputStream();
List<ByteBuffer> iconBuffers = new ArrayList<ByteBuffer>();
iconBuffers.add(convertToByteBuffer(ImageIO.read(icon16)));
iconBuffers.add(convertToByteBuffer(ImageIO.read(icon32)));
Display.setIcon(iconBuffers.toArray(new ByteBuffer[0]));
} catch (IOException e) {
e.printStackTrace();
}
}
private ByteBuffer convertToByteBuffer(BufferedImage image) {
int[] pixels = image.getRGB(0, 0, image.getWidth(), image.getHeight(), null, 0, image.getWidth());
ByteBuffer buffer = ByteBuffer.allocate(4 * pixels.length);
for (int pixel : pixels) {
buffer.put((byte) ((pixel >> 16) & 0xFF)); // Красный
buffer.put((byte) ((pixel >> 8) & 0xFF)); // Зеленый
buffer.put((byte) (pixel & 0xFF)); // Синий
buffer.put((byte) ((pixel >> 24) & 0xFF)); // Альфа
}
buffer.flip();
return buffer;
}
@Override
public void drawScreen(int mouseX, int mouseY, float partialTicks) {
this.mc.getTextureManager().bindTexture(BACKGROUND_TEXTURE);
Tessellator tessellator = Tessellator.instance;
tessellator.startDrawingQuads();
tessellator.addVertexWithUV(0, this.height, 0, 0, 1);
tessellator.addVertexWithUV(this.width, this.height, 0, 1, 1);
tessellator.addVertexWithUV(this.width, 0, 0, 1, 0);
tessellator.addVertexWithUV(0, 0, 0, 0, 0);
tessellator.draw();
super.drawScreen(mouseX, mouseY, partialTicks);
}
@Override
public void initGui() {
super.initGui();
this.buttonList.clear();
int centerX = this.width / 2;
int centerY = this.height / 2;
this.buttonList.add(new CustomButton(
0,
centerX + 250,
centerY - 245,
200,
35,
"",
new ResourceLocation("custommenumod", "textures/gui/play.png"),
new ResourceLocation("custommenumod", "textures/gui/playh.png")
));
this.buttonList.add(new CustomButton(
1,
centerX + 290,
centerY - 200,
160,
35,
"",
new ResourceLocation("custommenumod", "textures/gui/sett.png"),
new ResourceLocation("custommenumod", "textures/gui/setth.png")
));
this.buttonList.add(new CustomButton(
2,
centerX + 380,
centerY - 155,
70,
35,
"",
new ResourceLocation("custommenumod", "textures/gui/site.png"),
new ResourceLocation("custommenumod", "textures/gui/siteh.png")
));
this.buttonList.add(new CustomButton(
3,
centerX + 350,
centerY - 110,
100,
35,
"",
new ResourceLocation("custommenumod", "textures/gui/exit.png"),
new ResourceLocation("custommenumod", "textures/gui/exith.png")
));
}
protected void actionPerformed(GuiButton button) {
switch (button.id) {
case 0:
this.mc.displayGuiScreen(new GuiSelectWorld(this));
break;
case 1:
this.mc.displayGuiScreen(new GuiOptions(this, this.mc.gameSettings));
break;
case 2:
break;
case 3:
this.mc.shutdown();
break;
}
}
@SubscribeEvent
public void onGuiOpen(GuiOpenEvent event) {
if (event.gui instanceof GuiMainMenu) {
event.gui = new CustomMainMenu();
}
}
}
package ru.nelegal.custommenumod;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.util.ResourceLocation;
import org.lwjgl.opengl.GL11;
public class CustomButton extends GuiButton {
private final ResourceLocation textureNormal;
private final ResourceLocation textureHover;
public CustomButton(int buttonId, int x, int y, int width, int height, String buttonText,
ResourceLocation textureNormal, ResourceLocation textureHover) {
super(buttonId, x, y, width, height, buttonText);
this.textureNormal = textureNormal;
this.textureHover = textureHover;
}
@Override
public void drawButton(Minecraft mc, int mouseX, int mouseY) {
if (this.visible) {
boolean isHovered = mouseX >= this.xPosition && mouseY >= this.yPosition &&
mouseX < this.xPosition + this.width && mouseY < this.yPosition + this.height;
ResourceLocation textureToRender = isHovered ? textureHover : textureNormal;
mc.getTextureManager().bindTexture(textureToRender);
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
Tessellator tessellator = Tessellator.instance;
tessellator.startDrawingQuads();
tessellator.addVertexWithUV(this.xPosition, this.yPosition + this.height, 0, 0, 1);
tessellator.addVertexWithUV(this.xPosition + this.width, this.yPosition + this.height, 0, 1, 1);
tessellator.addVertexWithUV(this.xPosition + this.width, this.yPosition, 0, 1, 0);
tessellator.addVertexWithUV(this.xPosition, this.yPosition, 0, 0, 0);
tessellator.draw();
this.drawCenteredString(mc.fontRenderer, this.displayString,
this.xPosition + this.width / 2,
this.yPosition + (this.height - 8) / 2,
0xFFFFFF);
}
}
}