Skip to content

Commit

Permalink
Lots of stuff.
Browse files Browse the repository at this point in the history
  • Loading branch information
torralbaalla committed Mar 8, 2021
1 parent 3c09b43 commit f739dca
Show file tree
Hide file tree
Showing 14 changed files with 503 additions and 151 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Builds
build/

# Player and world data
player.dat
world.dat
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
all:
mkdir -p ./build/mc4k/
mkdir -p ./build/res/
javac -d ./build/ ./mc4k/MCApplet.java
javac -d ./build/ ./mc4k/MCTerrainGenerator.java
javac -d ./build/ ./mc4k/MCPlayer.java
javac -d ./build/ ./mc4k/Minecraft4K.java
cp ./res/Manifest.txt ./build/
cp ./res/textures.dat ./build/res/
cp ./res/icon.png ./build/res/
cd build && jar cfm Minecraft4K.jar Manifest.txt mc4k/* res/*
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

Minecraft 4K, but fixed.

![screenshot](https://raw.githubusercontent.com/Alvarito050506/mc4k/master/screenshot.png)

## Requirements
To run this, you will need the Java Runtime Environment (JRE) `>= 8.0`, to build this you will need the Java Development Kit (JDK) `>= 8.0`. Both Oracle and OpenJDK versions of the programs were tested and work correctly.

## Building and usage
To build this, do:
```sh
Expand Down Expand Up @@ -31,8 +36,24 @@ java -jar ./build/Minecraft4K.jar
+ Save world: `'G'`
+ Load world: `'C'`
+ Quit: `Esc`
+ Screenshot: `F2`

## Changes
+ Ported the code from a Java Applet to a Java application.
+ Modified controls to not depend on the keypad.
+ Added a better world saving and loading system.
+ Added a player data saving and loading system.
+ Added a diamond counter, and removed it from the default "inventory".
+ Added world generation.
+ Packed the program in a single JAR.
+ Modified the code internally, to make it more friendly.
+ Made small changes and fixes to the way the GUI works and looks.
+ Added automatic world and player data saving on exit.
+ Added screenshot feature.

Both player and world data, and screenshots, are saved to the current directory.

## Credits and licensing
I don't even know from where the original code comes, but I downloaded it from a [post](https://www.minecraftforum.net/forums/mapping-and-modding-java-edition/minecraft-mods/1290821-minecraft-4k-improved-by-crunchycat-download-now?comment=60) in the MCForums. It looks like it was a modified version of the original reverse-engineered Minecraft 4K code.

All my modifications were made for the only purpose of making it more playable, feel free to modify and distribute them.
All my modifications were made for the only purpose of making it more enjoyable, feel free to modify and distribute them.
2 changes: 0 additions & 2 deletions build/Manifest.txt

This file was deleted.

Binary file removed build/Minecraft4K.jar
Binary file not shown.
Binary file removed build/mc4k/Minecraft4K$1.class
Binary file not shown.
Binary file removed build/mc4k/Minecraft4K.class
Binary file not shown.
Binary file removed build/res/textures.dat
Binary file not shown.
145 changes: 145 additions & 0 deletions mc4k/MCApplet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/*
* MCApplet.java - Helper class.
*
* Copyright 2021 Alvarito050506 <donfrutosgomez@gmail.com>
*
*/

package mc4k;

import java.awt.Panel;
import java.awt.Frame;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.awt.image.ImageObserver;

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 space = 0; // Space
public int mouseLocked = 1; // 0: Locked, 1: Game not started, 2: Focus lost
}

public class MCApplet extends Panel implements MouseMotionListener, MouseListener, KeyListener {
public MCEvents events = new MCEvents();
public static Frame frame = new Frame();

@Override
public void mouseDragged(MouseEvent paramEvent) {
if (events.mouseLocked != 0)
{
return;
}
events.mouseX = paramEvent.getX();
events.mouseY = paramEvent.getY();
}

@Override
public void mouseMoved(MouseEvent paramEvent) {
if (events.mouseLocked != 0)
{
return;
}
events.mouseX = paramEvent.getX();
events.mouseY = paramEvent.getY();
}

@Override
public void mousePressed(MouseEvent paramEvent) {
if (events.mouseLocked != 0)
{
return;
}
events.mouseX = paramEvent.getX();
events.mouseY = paramEvent.getY();
if ((paramEvent.getModifiers() & 4) <= 0)
events.buttons[0] = 1;
else
events.buttons[1] = 1;
}

private void setKeyEvent(KeyEvent paramEvent, int val) {
switch (paramEvent.getKeyCode()) {
case 27:
events.worldKeys[0] = val;
break;
case 67:
events.worldKeys[1] = val;
break;
case 71:
events.worldKeys[2] = val;
break;
case 113:
events.worldKeys[3] = val;
break;
case 37:
events.arrows[0] = val;
break;
case 39:
events.arrows[1] = val;
break;
case 87:
events.wasd[0] = val;
break;
case 65:
events.wasd[1] = val;
break;
case 83:
events.wasd[2] = val;
break;
case 68:
events.wasd[3] = val;
break;
case 32:
events.space = val;
break;
}
}

@Override
public void keyPressed(KeyEvent paramEvent) {
setKeyEvent(paramEvent, 1);
}

@Override
public void keyReleased(KeyEvent paramEvent) {
setKeyEvent(paramEvent, 0);
}

@Override
public void mouseExited(MouseEvent paramEvent) {
return;
}

@Override
public void mouseClicked(MouseEvent paramEvent) {
if (events.mouseLocked == 1) {
events.mouseLocked = 0;
}
return;
}

@Override
public void mouseEntered(MouseEvent paramEvent) {
return;
}

@Override
public void mouseReleased(MouseEvent paramEvent) {
return;
}

@Override
public void keyTyped(KeyEvent paramEvent) {
return;
}
}
23 changes: 23 additions & 0 deletions mc4k/MCPlayer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* MCPlayer.java - Player inventory and data.
*
* Copyright 2021 Alvarito050506 <donfrutosgomez@gmail.com>
*
*/

package mc4k;

import java.util.Random;
import java.time.Instant;
import java.lang.Math;
import java.io.Serializable;

public class MCPlayer implements Serializable {
private static final long serialVersionUID = 1L;

public int blockInHand = 1;
public int diamondCounter = 0;
public float posX = 96.5F;
public float posY = 95.0F;
public float posZ = 96.5F;
}
140 changes: 140 additions & 0 deletions mc4k/MCTerrainGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
* MCTerrainGenerator.java - Terrain generator.
*
* Copyright 2021 Alvarito050506 <donfrutosgomez@gmail.com>
* Copyright 2020 @saca44 at the MCForums
*
*/

package mc4k;

import java.util.Random;
import java.time.Instant;
import java.lang.Math;

public class MCTerrainGenerator {
Random random = new Random();
int seed = 0;
int worldSize = 64 * 64 * 64;
int[] worldBlocks = new int[worldSize];
double[][] heightMap = new double[64][64];

public MCTerrainGenerator() {
int i = 0;
int tbsi;
int randomMax = (int)Math.pow(64, 4);

float fx = 0;
float fy = 0;

random.setSeed(Instant.now().getEpochSecond());
for (int y = 0; y < 64; y++) {
fx = 0;
for (int x = 0; x < 64; x++) {
heightMap[x][y] = 32 + (Math.cos(fx) + Math.sin(fy)) * 2;
fx += 0.2f;
}
fy += 0.2f;
}

for (int y = 0; y < 64; y++) {
for (int x = 0; x < 64; x++) {
for (int z = 0; z < 64; z++) {
int lim = i / 64 % 64;
double mapVal = heightMap[y][z];
if (heightMap[y][z] > lim) {
worldBlocks[i] = 0;
} else if (mapVal > lim - 1) {
worldBlocks[i] = 1;
} else if (mapVal > lim - 4 + random.nextFloat()) {
worldBlocks[i] = 2;
} else {
if (random.nextInt(randomMax) >= randomMax - randomMax / 128) {
worldBlocks[i] = 3;
} else {
worldBlocks[i] = 4;
}
}
i++;
}
}
}

for (int x = 0; x < 64; x++) {
for (int z = 0; z < 64; z++) {
tbsi = vecToInt(x, (int)(heightMap[x][z]), z);
if (isTreeAreaBlank(x, (int)(heightMap[x][z]), z) > 0) {
if (random.nextInt(96) == 0) {
tree(x, (int)(heightMap[x][z]), z);
}
}
}
}
}

public int vecToInt(int x, int y, int z) {
return z + y * 64 + x * 4096;
}

public void setBlock(int block, int x, int y, int z) {
int coords = vecToInt(x, y, z);
if (coords > 0 && coords < worldSize) {
worldBlocks[coords] = block;
}
}

public void fillBlocks(int block, int x, int y, int z, int w, int h, int d) {
w += x;
h += y;
d += z;
for (int xx = x; xx < w; xx++) {
for (int yy = y; yy < h; yy++) {
for (int zz = z; zz < d; zz++) {
setBlock(block, xx, yy, zz);
}
}
}
}

public int isTreeAreaBlank(int x, int y, int z) {
int coords;

try {
coords = vecToInt(x, (int)(heightMap[x][z]), z);
if (worldBlocks[coords] != 0)
{
return 0;
}
coords = vecToInt(x + 1, (int)(heightMap[x][z]), z);
if (worldBlocks[coords] != 0)
{
return 0;
}
coords = vecToInt(x + 1, (int)(heightMap[x][z]), z + 1);
if (worldBlocks[coords] != 0)
{
return 0;
}
coords = vecToInt(x, (int)(heightMap[x][z]), z + 1);
if (worldBlocks[coords] != 0)
{
return 0;
}
} catch (Exception err) {
return 0;
}
return 1;
}

public void tree(int x, int y, int z) {
fillBlocks(8, x - 2, y - 4, z - 2, 5, 2, 5);
fillBlocks(8, x - 1, y - 6, z - 1, 3, 2, 3);
for (int i = 0; i < 5; i++) {
setBlock(7, x, y - i, z);
}
}

public int[] getBlocks() {
return worldBlocks;
}
}

0 comments on commit f739dca

Please sign in to comment.