Skip to content
This repository has been archived by the owner on Aug 5, 2024. It is now read-only.

Commit

Permalink
Multiple world support.
Browse files Browse the repository at this point in the history
  • Loading branch information
torralbaa committed Mar 11, 2021
1 parent 6d87fcc commit 682bbd7
Show file tree
Hide file tree
Showing 6 changed files with 296 additions and 63 deletions.
12 changes: 5 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
VERSION:=v0.3.0
VERSION:=v0.4.0

all: config
javac -d ./build/ ./mc4k/MCApplet.java
javac -d ./build/ ./mc4k/MCTerrainGenerator.java
javac -d ./build/ ./mc4k/MCPlayer.java
javac -d ./build/ ./mc4k/Minecraft4K.java
javac -Xlint:deprecation -d ./build/ ./mc4k/*.java
cd build && jar cfm Minecraft4K.jar manifest_minecraft4k.txt mc4k/* res/*

config:
mkdir -p ./build/mc4k/
mkdir -p ./build/res/
cp ./res/manifest_minecraft4k.txt ./build/
cp ./res/textures.dat ./build/res/
cp ./res/icon.png ./build/res/
cp ./res/*.png ./build/res/
cp ./res/*.dat ./build/res/

25 changes: 15 additions & 10 deletions mc4k/MCApplet.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
package mc4k;

import java.awt.Panel;
import java.awt.Frame;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
Expand All @@ -17,18 +16,16 @@
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelListener;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.awt.image.ImageObserver;
import javax.swing.JFrame;

import mc4k.MCFrame;

class MCEvents {
public int mouseX = 0;
public int mouseY = 0;
public int[] wasd = new int[4]; // WASD
public int[] buttons = new int[2]; // Left, Right
public int[] arrows = new int[2]; // Left, Right
public int[] worldKeys = new int[4]; // Esc, C, G, F2
public int[] worldKeys = new int[5]; // Esc, C, G, F2, F1
public int space = 0; // Space
public int mouseLocked = 1; // 0: Locked, 1: Game not started, 2: Focus lost
public int mouseWheel = 0;
Expand All @@ -37,15 +34,18 @@ class MCEvents {
public class MCApplet extends Panel implements MouseMotionListener, MouseListener, KeyListener, MouseWheelListener {
public MCEvents events = new MCEvents();
public static Robot robot;
public static JFrame frame = new JFrame();
public static MCFrame frame;

int imageWidth = 214 * 2;
int imageHeight = 120 * 2;
int screenWidth = 856 * 1;
int screenHeigth = 480 * 1;
int screenWidth = 856;
int screenHeight = 480;
float scale = 1F;

int centerX = screenWidth / 2;
int centerY = screenHeigth / 2;
int centerY = screenHeight / 2;

int initialized = 1;

@Override
public void mouseDragged(MouseEvent paramEvent) {
Expand Down Expand Up @@ -92,6 +92,11 @@ private void setKeyEvent(KeyEvent paramEvent, int val) {
case 113:
events.worldKeys[3] = val;
break;
case 112:
if (val == 1) {
events.worldKeys[4] = 1;
}
break;
case 37:
events.arrows[0] = val;
break;
Expand Down
36 changes: 36 additions & 0 deletions mc4k/MCFrame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* MCFrame.java - Helper class.
*
* Copyright 2021 Alvarito050506 <[email protected]>
*
*/

package mc4k;

import java.awt.Panel;
import java.net.URL;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.ImageIcon;

public class MCFrame extends JFrame {
public static MCFrame other;
public static Panel panel;

public MCFrame(int width, int height, Panel tmpPanel) {
panel = tmpPanel;

add(panel);
pack();

setSize(width, height);
setResizable(false);
setTitle("mc4k - Minecraft 4k");

URL iconURL = MCFrame.class.getResource("/res/icon.png");
if (iconURL != null) {
ImageIcon icon = new ImageIcon(iconURL);
setIconImage(icon.getImage());
}
}
}
159 changes: 159 additions & 0 deletions mc4k/MCSettings.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/*
* MCSettings.java - Main settings logic/interface.
*
* Copyright 2021 Alvarito050506 <[email protected]>
*
*/

package mc4k;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.awt.image.ImageObserver;
import java.awt.image.BufferStrategy;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import java.io.File;
import javax.swing.JFrame;
import javax.swing.BoxLayout;
import javax.imageio.ImageIO;

import mc4k.MCFrame;

public class MCSettings extends Panel implements Runnable, KeyListener {
public static MCFrame frame;

public boolean runMC = false;
public String worldName = null;
public String worldsDirectoryName = System.getProperty("user.home") + "/.mc4k";
public String worldDirectoryName = null;
public String worldFileName = null;
public String playerFileName = null;

String titleString = "mc4k - Minecraft 4k";
String worldNameLabelString = "World name: ";
int titleWidth = 0;
int worldNameWidth = 0;
int worldNameLabelWidth = 0;
int width = 0;
int height = 0;
int i = 1;
BufferedImage backgroundImage;
BufferedImage screenshotImage;
Graphics graphics;
Graphics panelGraphics;
StringBuffer inputBuffer = new StringBuffer("world");
Font titleFont = new Font("", Font.BOLD, 26);
Font inputFont = new Font("", Font.PLAIN, 18);
BufferStrategy strategy;

public void start() {
mkdirp(worldsDirectoryName);
panelGraphics = getGraphics();
try {
screenshotImage = ImageIO.read(MCSettings.class.getResourceAsStream("/res/screenshot.png"));
} catch (Exception err) {
err.printStackTrace();
System.exit(-1);
}

width = frame.getWidth();
height = frame.getHeight();

backgroundImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
graphics = backgroundImage.createGraphics();

graphics.setFont(titleFont);
titleWidth = graphics.getFontMetrics().stringWidth(titleString);
graphics.setFont(inputFont);
worldNameLabelWidth = graphics.getFontMetrics().stringWidth(worldNameLabelString);
(new Thread(this)).start();
addKeyListener(this);
}

public void run() {
while (true) {
graphics.setColor(Color.WHITE);
graphics.drawImage(screenshotImage, 0, 0, width, height, (ImageObserver)null);
graphics.setFont(titleFont);
graphics.drawString(titleString, width / 2 - titleWidth / 2, 32);
graphics.setFont(inputFont);
graphics.drawString(worldNameLabelString, width / 2 - worldNameLabelWidth, 110);
graphics.drawString(inputBuffer.toString(), width / 2, 110);
worldNameWidth = graphics.getFontMetrics().stringWidth(inputBuffer.toString());
if (i % 25 != 0) {
graphics.drawRect(width / 2 + worldNameWidth + 2, 95, 6, 15);
i++;
} else {
if (i > 1000) {
i = 0;
}
i++;
}
panelGraphics.drawImage(backgroundImage, 0, 0, width, height, (ImageObserver)null);

try {
Thread.sleep(1000/30);
} catch (InterruptedException err) {
err.printStackTrace();
}
}
}

public void mkdirp(String directoryName) {
File directory = new File(directoryName);
if (directory.exists() && !directory.isDirectory()) {
System.out.println(directory + " exists but isn't a directory. Aborting.");
System.out.println(-1);
} else if (!directory.exists()) {
directory.mkdir();
}
}

@Override
public void keyPressed(KeyEvent paramEvent) {
int keyCode = paramEvent.getKeyCode();
if (keyCode == 0x0a) { // Enter
if (inputBuffer.length() <= 0) {
worldName = "world";
} else {
worldName = inputBuffer.toString();
}
worldDirectoryName = worldsDirectoryName + "/" + worldName;
mkdirp(worldDirectoryName);
runMC = true;
frame.setFocusable(false);
graphics.dispose();
frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
} else if (keyCode >= 0x30 && (keyCode <= 0x39 || (keyCode >= 0x61 && keyCode <= 0x7a))) { // [0-9a-z]
if (inputBuffer.length() <= 12) {
inputBuffer.append((char)keyCode);
}
} else if (keyCode >= 0x41 && keyCode <= 0x5a) { // [A-Z]
if (inputBuffer.length() <= 12) {
inputBuffer.append((char)(keyCode + 0x20));
}
} else if (keyCode == 0x08) { // Back/delete
if (inputBuffer.length() > 0) {
inputBuffer.deleteCharAt(inputBuffer.length() - 1);
graphics.clearRect(frame.getWidth() / 2, 75, frame.getWidth() / 2 + worldNameWidth * 2, 125);
}
} else if (keyCode == 0x1b) { // Esc
graphics.dispose();
frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
}
return;
}

@Override
public void keyReleased(KeyEvent paramEvent) {
return;
}

@Override
public void keyTyped(KeyEvent paramEvent) {
return;
}
}
Loading

0 comments on commit 682bbd7

Please sign in to comment.