Skip to content

Commit 57073d9

Browse files
committed
🧼 Cleanup
1 parent 1f2b514 commit 57073d9

File tree

6 files changed

+44
-36
lines changed

6 files changed

+44
-36
lines changed

‎src/main/java/com/james090500/client/ClientInput.java‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.james090500.client;
22

33
import com.james090500.BlockGame;
4-
import com.james090500.gui.DebugScreen;
54
import com.james090500.gui.Screen;
65
import com.james090500.gui.ScreenManager;
76
import lombok.Getter;

‎src/main/java/com/james090500/client/LocalPlayer.java‎

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.james090500.utils.SoundManager;
1313
import com.james090500.world.ChunkStatus;
1414
import lombok.Getter;
15+
import lombok.Setter;
1516
import org.joml.Vector3f;
1617
import org.joml.Vector3i;
1718

@@ -32,6 +33,7 @@ public class LocalPlayer {
3233
private boolean swimming = false;
3334

3435
Clock clock = new Clock();
36+
private final Camera camera;
3537
private final AABB aabb;
3638
private final float playerWidth = 0.4f;
3739
private final float playerHeight = 1.75f;
@@ -40,6 +42,10 @@ public class LocalPlayer {
4042
private double jumpStartTime = 0;
4143
private float stepCooldown;
4244
private String worldName;
45+
@Getter @Setter
46+
private int lastChunkX;
47+
@Getter @Setter
48+
private int lastChunkZ;
4349

4450
BlockOverlay blockOverlay = new BlockOverlay();
4551
ArmOverlay armOverlay = new ArmOverlay();
@@ -48,10 +54,10 @@ public class LocalPlayer {
4854
public LocalPlayer() {
4955
this.aabb = new AABB(playerWidth, playerHeight);
5056
this.worldName = BlockGame.getInstance().getWorld().getWorldName();
51-
Camera camera = BlockGame.getInstance().getCamera();
57+
camera = BlockGame.getInstance().getCamera();
5258

5359
if(BlockGame.getInstance().getWorld().isRemote()) {
54-
camera.setPosition(0, 100, 0);
60+
setPosition(0, 100, 0);
5561
} else {
5662
File playerPath = new File("worlds/" + worldName + "/players");
5763
File playerData = new File(playerPath + "/player.dat");
@@ -68,7 +74,7 @@ public LocalPlayer() {
6874
float pitch = raf.readFloat();
6975
float yaw = raf.readFloat();
7076

71-
camera.setPosition(x, y, z);
77+
setPosition(x, y, z);
7278
camera.setRotation(pitch, yaw);
7379

7480
} catch (IOException e) {
@@ -82,14 +88,13 @@ public void savePlayer() {
8288
if(!BlockGame.getInstance().getWorld().isRemote()) {
8389
File playerPath = new File("worlds/" + worldName + "/players");
8490
File playerData = new File(playerPath + "/player.dat");
85-
Camera camera = BlockGame.getInstance().getCamera();
8691

8792
// Write to file
8893
try (RandomAccessFile raf = new RandomAccessFile(playerData, "rw")) {
8994
raf.setLength(0);
90-
raf.writeFloat(camera.getPosition().x);
91-
raf.writeFloat(camera.getPosition().y);
92-
raf.writeFloat(camera.getPosition().z);
95+
raf.writeFloat(getPosition().x);
96+
raf.writeFloat(getPosition().y);
97+
raf.writeFloat(getPosition().z);
9398
raf.writeFloat(camera.pitch);
9499
raf.writeFloat(camera.yaw);
95100
} catch (IOException e) {
@@ -98,6 +103,14 @@ public void savePlayer() {
98103
}
99104
}
100105

106+
public Vector3f getPosition() {
107+
return camera.getPosition();
108+
}
109+
110+
public void setPosition(float x, float y, float z) {
111+
camera.setPosition(x, y, z);
112+
}
113+
101114
private void updateControls() {
102115
HashMap<Integer, Boolean> keys = BlockGame.getInstance().getClientWindow().getClientInput().getKeys();
103116

@@ -113,6 +126,8 @@ private void updateControls() {
113126
* @param {number} delta - Time since last frame.
114127
*/
115128
private void updateMovement(double delta) {
129+
if(!BlockGame.getInstance().getWorld().isChunkStatus(lastChunkX, lastChunkZ, ChunkStatus.FINISHED)) return;
130+
116131
float moveSpeed = 0.9f;
117132
if (this.noclip) {
118133
moveSpeed = 10f;
@@ -121,11 +136,10 @@ private void updateMovement(double delta) {
121136
}
122137

123138
// Get necessary references
124-
Camera camera = BlockGame.getInstance().getCamera();
139+
Vector3f playerPos = getPosition();
125140
HashMap<Integer, Boolean> keys = BlockGame.getInstance().getClientWindow().getClientInput().getKeys();
126141

127142
// Current Block the player is in
128-
Vector3f playerPos = new Vector3f(camera.getPosition());
129143
playerPos.y -= 1;
130144
Block currentBlock = BlockGame.getInstance().getWorld().getBlock(playerPos);
131145

@@ -137,8 +151,8 @@ private void updateMovement(double delta) {
137151
dir.cross(new Vector3f(0, 1, 0), right);
138152

139153
// Stop playing falling through the world
140-
if (camera.getPosition().y < -30) {
141-
camera.getPosition().y = 100;
154+
if (getPosition().y < -30) {
155+
getPosition().y = 100;
142156
}
143157

144158
// Dampen movement
@@ -245,7 +259,7 @@ private void updateMovement(double delta) {
245259
if (velocity.lengthSquared() > 0.0004f && !this.falling && !this.jumping) {
246260
stepCooldown -= delta; // deltaTime is the time since last frame
247261
if (stepCooldown <= 0f) {
248-
Block blockAtFeet = BlockGame.getInstance().getWorld().getBlock(camera.getPosition().sub(new Vector3f(0, 2, 0)));
262+
Block blockAtFeet = BlockGame.getInstance().getWorld().getBlock(getPosition().sub(new Vector3f(0, 2, 0)));
249263
if(blockAtFeet != null && blockAtFeet.getSound() != null) {
250264
SoundManager.play("assets/sound/block/" + blockAtFeet.getSound(), 4);
251265
stepCooldown = 0.5f; // play every half second while moving
@@ -261,8 +275,7 @@ private void updateMovement(double delta) {
261275
* Update interaction via mouse picking
262276
*/
263277
private void updateInteraction() {
264-
Camera camera = BlockGame.getInstance().getCamera();
265-
Vector3f origin = new Vector3f(camera.getPosition());
278+
Vector3f origin = new Vector3f(getPosition());
266279
Vector3f dir = new Vector3f(camera.getDirection()).normalize();
267280

268281
Vector3i[] raycast = Raycast.block(origin, dir, 5f);
@@ -312,11 +325,11 @@ public void render() {
312325
* @returns {boolean} True if the move was successful.
313326
*/
314327
public boolean tryMove(Vector3f velocity, int axis) {
315-
Vector3f pos = new Vector3f(BlockGame.getInstance().getCamera().getPosition());
328+
Vector3f pos = new Vector3f(getPosition());
316329
pos.setComponent(axis,pos.get(axis) + velocity.get(axis));
317330

318331
if (!aabb.isColliding(pos) || this.noclip) {
319-
BlockGame.getInstance().getCamera().setPosition(axis, pos.get(axis));
332+
camera.setPosition(axis, pos.get(axis));
320333
return true;
321334
}
322335
return false;

‎src/main/java/com/james090500/entity/model/PlayerModel.java‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,8 @@
88
import org.joml.Vector3f;
99
import org.lwjgl.system.MemoryUtil;
1010

11-
import static org.lwjgl.opengl.GL11.GL_FLOAT;
12-
import static org.lwjgl.opengl.GL11.GL_TRIANGLES;
13-
import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT;
14-
import static org.lwjgl.opengl.GL11.glDrawElements;
11+
import static org.lwjgl.opengl.GL11.*;
1512
import static org.lwjgl.opengl.GL15.*;
16-
import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW;
1713
import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray;
1814
import static org.lwjgl.opengl.GL20.glVertexAttribPointer;
1915
import static org.lwjgl.opengl.GL30.glBindVertexArray;

‎src/main/java/com/james090500/gui/DebugScreen.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ public DebugScreen() {
1515

1616
@Override
1717
public void render() {
18+
// Return if invalid
19+
if(
20+
BlockGame.getInstance().getWorld() == null ||
21+
BlockGame.getInstance().getLocalPlayer() == null ||
22+
BlockGame.getInstance().getCamera() == null
23+
) return;
24+
1825
Vector3f position = BlockGame.getInstance().getCamera().getPosition();
1926
String playerPos = String.format("Position: %.2f, %.2f, %.2f", position.x, position.y, position.z);
2027

‎src/main/java/com/james090500/network/NettyHandler.java‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@
1212
import io.netty.channel.nio.NioIoHandler;
1313
import io.netty.channel.socket.nio.NioSocketChannel;
1414

15-
import java.net.InetAddress;
16-
import java.net.InetSocketAddress;
17-
1815
public class NettyHandler {
1916

2017
private final String host;

‎src/main/java/com/james090500/world/World.java‎

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@
33
import com.james090500.BlockGame;
44
import com.james090500.blocks.Block;
55
import com.james090500.blocks.Blocks;
6+
import com.james090500.client.LocalPlayer;
67
import com.james090500.entity.Entity;
7-
import com.james090500.entity.PlayerEntity;
88
import com.james090500.network.packets.BlockUpdatePacket;
99
import com.james090500.network.packets.DisconnectPacket;
1010
import com.james090500.renderer.RenderManager;
1111
import com.james090500.utils.SoundManager;
1212
import com.james090500.utils.ThreadUtil;
1313
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
1414
import it.unimi.dsi.fastutil.objects.Object2ObjectMap;
15-
import it.unimi.dsi.fastutil.objects.Object2ObjectMaps;
1615
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
1716
import lombok.Getter;
1817
import lombok.Setter;
@@ -39,9 +38,6 @@ public record ChunkOffset(int dx, int dz, int distSq) {}
3938

4039
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
4140

42-
private int lastPlayerX;
43-
private int lastPlayerZ;
44-
4541
@Getter
4642
private int worldSeed;
4743

@@ -256,9 +252,9 @@ public void loadRemoteChunk(int chunkX, int chunkZ, byte[] chunkData) {
256252
* update the world. This also loads and remove chunks as needed
257253
*/
258254
public void update() {
259-
Vector3f playerPos = BlockGame.getInstance().getCamera().getPosition();
260-
int playerChunkX = (int) Math.floor(playerPos.x / 16);
261-
int playerChunkZ = (int) Math.floor(playerPos.z / 16);
255+
LocalPlayer player = BlockGame.getInstance().getLocalPlayer();
256+
int playerChunkX = (int) Math.floor(player.getPosition().x / 16);
257+
int playerChunkZ = (int) Math.floor(player.getPosition().z / 16);
262258

263259
// Load/generate nearby chunks in render distance
264260
List<ChunkOffset> offsets = new ArrayList<>();
@@ -283,7 +279,7 @@ public void update() {
283279
}
284280

285281
// No point looping if we aren't moving
286-
if(playerChunkX == lastPlayerX && playerChunkZ == lastPlayerZ && !this.forceUpdate) return;
282+
if(playerChunkX == player.getLastChunkX() && playerChunkZ == player.getLastChunkZ() && !this.forceUpdate) return;
287283

288284
// Render chunks from players pos.
289285
Set<ChunkPos> requiredChunks = new HashSet<>();
@@ -329,8 +325,8 @@ public void update() {
329325
return false;
330326
});
331327

332-
this.lastPlayerX = playerChunkX;
333-
this.lastPlayerZ = playerChunkZ;
328+
player.setLastChunkX(playerChunkX);
329+
player.setLastChunkZ(playerChunkZ);
334330
this.forceUpdate = false;
335331
}
336332

0 commit comments

Comments
 (0)