Skip to content

Commit

Permalink
Massive refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
kiip1 committed Feb 25, 2023
1 parent d9c5221 commit 402de76
Show file tree
Hide file tree
Showing 26 changed files with 100 additions and 180 deletions.
16 changes: 0 additions & 16 deletions src/main/java/nl/kiipdevelopment/minescreen/MineScreen.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
package nl.kiipdevelopment.minescreen;

import nl.kiipdevelopment.minescreen.map.Map;
import nl.kiipdevelopment.minescreen.map.graphics.MapGraphics;
import nl.kiipdevelopment.minescreen.screen.ScreenGui;
import nl.kiipdevelopment.minescreen.util.suppliers.MonoSupplier;
import nl.kiipdevelopment.minescreen.util.suppliers.TriSupplier;
import nl.kiipdevelopment.minescreen.world.GuiInstance;
import org.jetbrains.annotations.ApiStatus;

@ApiStatus.NonExtendable
Expand All @@ -17,14 +11,4 @@ static MineScreen instance() {

void init();

MonoSupplier<ScreenGui, GuiInstance> guiInstanceSupplier();

MonoSupplier<ScreenGui, MapGraphics> mapGraphicsSupplier();

MonoSupplier<ScreenGui, Runnable> refreshScreenSupplier();

TriSupplier<Short, Integer, Integer, Map> mapSupplier();

TriSupplier<Short, Integer, Integer, Integer> mapIdSupplier();

}
48 changes: 3 additions & 45 deletions src/main/java/nl/kiipdevelopment/minescreen/MineScreenImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,7 @@

import net.minestom.server.MinecraftServer;
import net.minestom.server.utils.validate.Check;
import nl.kiipdevelopment.minescreen.map.Map;
import nl.kiipdevelopment.minescreen.map.MapImpl;
import nl.kiipdevelopment.minescreen.map.graphics.MapGraphics;
import nl.kiipdevelopment.minescreen.map.graphics.MapGraphicsImpl;
import nl.kiipdevelopment.minescreen.screen.ScreenGui;
import nl.kiipdevelopment.minescreen.util.suppliers.MonoSupplier;
import nl.kiipdevelopment.minescreen.util.suppliers.TriSupplier;
import nl.kiipdevelopment.minescreen.world.GuiInstance;
import nl.kiipdevelopment.minescreen.world.GuiInstanceImpl;

final class MineScreenImpl implements MineScreen {
private static volatile MineScreen instance;
Expand All @@ -25,52 +17,18 @@ public static MineScreen instance() {
return instance;
}

// TODO This stuff is stupid
private final MonoSupplier<ScreenGui, GuiInstance> guiInstanceSupplier = GuiInstanceImpl::new;
private final MonoSupplier<ScreenGui, MapGraphics> mapGraphicsSupplier = MapGraphicsImpl::new;
private final MonoSupplier<ScreenGui, Runnable> refreshScreenSupplier = gui -> () -> {
gui.render(gui.mapGraphics());
gui.sendPacketUpdate();
};
private final TriSupplier<Short, Integer, Integer, Map> mapSupplier = MapImpl::new;
private final TriSupplier<Short, Integer, Integer, Integer> mapIdSupplier = (guiId, x, y) -> (guiId << 16) + (x << 8) + y;

private boolean initialized = false;
private volatile boolean initialized = false;

private MineScreenImpl() {}

@Override
public void init() {
Check.stateCondition(initialized, "Already initialized.");

MinecraftServer.getBiomeManager().addBiome(GuiInstanceImpl.BIOME);
MinecraftServer.getDimensionTypeManager().addDimension(GuiInstanceImpl.DIMENSION_TYPE);
MinecraftServer.getBiomeManager().addBiome(GuiInstance.BIOME);
MinecraftServer.getDimensionTypeManager().addDimension(GuiInstance.DIMENSION_TYPE);

initialized = true;
}

@Override
public MonoSupplier<ScreenGui, GuiInstance> guiInstanceSupplier() {
return guiInstanceSupplier;
}

@Override
public MonoSupplier<ScreenGui, MapGraphics> mapGraphicsSupplier() {
return mapGraphicsSupplier;
}

@Override
public MonoSupplier<ScreenGui, Runnable> refreshScreenSupplier() {
return refreshScreenSupplier;
}

@Override
public TriSupplier<Short, Integer, Integer, Map> mapSupplier() {
return mapSupplier;
}

@Override
public TriSupplier<Short, Integer, Integer, Integer> mapIdSupplier() {
return mapIdSupplier;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package nl.kiipdevelopment.minescreen.map.graphics;
package nl.kiipdevelopment.minescreen.graphics;

import net.minestom.server.map.MapColors;
import nl.kiipdevelopment.minescreen.screen.ScreenGui;

public interface MapGraphics {
static MapGraphics of(ScreenGui gui) {
return new MapGraphicsImpl(gui);
}

default void fill(MapColors.PreciseMapColor color) {
fill(color.getIndex());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nl.kiipdevelopment.minescreen.map.graphics;
package nl.kiipdevelopment.minescreen.graphics;

import net.minestom.server.utils.validate.Check;
import nl.kiipdevelopment.minescreen.map.Map;
Expand All @@ -7,7 +7,7 @@

import java.util.Arrays;

public class MapGraphicsImpl implements MapGraphics {
sealed class MapGraphicsImpl implements MapGraphics permits RelativeMapGraphicsImpl {
protected final ScreenGui gui;
protected final Map map;

Expand All @@ -29,12 +29,9 @@ public void drawRectangle(byte color, int startX, int startY, int endX, int endY
ensureWithinBounds(endX, endY, "Bottom right");

// TODO: Optimize this somehow

for (int x = startX; x < endX; x++) {
for (int y = startY; y < endY; y++) {
for (int x = startX; x < endX; x++)
for (int y = startY; y < endY; y++)
drawDirectDot(color, x, y);
}
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package nl.kiipdevelopment.minescreen.map.graphics;
package nl.kiipdevelopment.minescreen.graphics;

import nl.kiipdevelopment.minescreen.screen.ScreenGui;

public class RelativeMapGraphicsImpl extends MapGraphicsImpl {
final class RelativeMapGraphicsImpl extends MapGraphicsImpl {
private final int x, y;
private final int width, height;

Expand Down
5 changes: 5 additions & 0 deletions src/main/java/nl/kiipdevelopment/minescreen/map/Map.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package nl.kiipdevelopment.minescreen.map;

import net.minestom.server.entity.Player;
import nl.kiipdevelopment.minescreen.screen.ScreenGui;

import java.util.Collection;

public interface Map {
static Map of(ScreenGui gui, int x, int y) {
return new MapImpl(gui.guiId(), x, y);
}

int width();

int height();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import java.util.Collection;

public class MapImpl implements Map {
final class MapImpl implements Map {
private final SubMap[] subs;
private final int width, height;

Expand Down
17 changes: 8 additions & 9 deletions src/main/java/nl/kiipdevelopment/minescreen/map/SubMapImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,30 @@
import net.minestom.server.network.packet.server.SendablePacket;
import net.minestom.server.network.packet.server.play.MapDataPacket;
import net.minestom.server.utils.PacketUtils;
import nl.kiipdevelopment.minescreen.MineScreen;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

public class SubMapImpl implements SubMap {
final class SubMapImpl implements SubMap {
private static final MessageDigest DIGEST;

static {
MessageDigest digest = null;
try {
digest = MessageDigest.getInstance("SHA-256");
DIGEST = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
DIGEST = digest;
}

public final byte[] colors;
private final short guiId;
private final int mapX, mapY;
private final int width, height;
private final int mapX;
private final int mapY;
private final int width;
private final int height;

private byte[] prevHash;

Expand Down Expand Up @@ -65,7 +64,7 @@ public int height() {

@Override
public int id() {
return MineScreen.instance().mapIdSupplier().get(guiId, mapX, mapY);
return (guiId << 16) + (mapX << 8) + mapY;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
import net.minestom.server.event.player.PlayerHandAnimationEvent;
import nl.kiipdevelopment.minescreen.widget.Widget;
import nl.kiipdevelopment.minescreen.widget.Interactable;
import nl.kiipdevelopment.minescreen.map.graphics.MapGraphics;
import nl.kiipdevelopment.minescreen.graphics.MapGraphics;
import nl.kiipdevelopment.minescreen.util.Intersection3dUtils;
import nl.kiipdevelopment.minescreen.util.MathUtils;

import java.util.ArrayList;
import java.util.List;

public class InteractableScreenGui extends ScreenGui {
public final class InteractableScreenGui extends ScreenGui {
private final List<Mouse> mice = new ArrayList<>();
private final double[] plane;

Expand Down
29 changes: 17 additions & 12 deletions src/main/java/nl/kiipdevelopment/minescreen/screen/ScreenGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,32 @@
import net.minestom.server.entity.Player;
import net.minestom.server.instance.Instance;
import net.minestom.server.map.MapColors;
import net.minestom.server.timer.Task;
import net.minestom.server.timer.ExecutionType;
import net.minestom.server.utils.time.TimeUnit;
import net.minestom.server.utils.validate.Check;
import nl.kiipdevelopment.minescreen.MineScreen;
import nl.kiipdevelopment.minescreen.graphics.MapGraphics;
import nl.kiipdevelopment.minescreen.map.Map;
import nl.kiipdevelopment.minescreen.map.graphics.MapGraphics;
import nl.kiipdevelopment.minescreen.util.MathUtils;
import nl.kiipdevelopment.minescreen.widget.Widget;
import nl.kiipdevelopment.minescreen.world.GuiInstance;
import org.jetbrains.annotations.MustBeInvokedByOverriders;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

public class ScreenGui {
public sealed class ScreenGui permits InteractableScreenGui {
private static final Logger LOGGER = LoggerFactory.getLogger(ScreenGui.class);

private final short guiId;
private final int fps;
private final GuiInstance guiInstance;
private final Map map;
private final MapGraphics mapGraphics;
private final List<Target> targets = new ArrayList<>();
private final Task refreshScreenTask;

private MapColors background = MapColors.NONE;

Expand All @@ -48,15 +50,19 @@ public ScreenGui(short guiId, int width, int height, int fps) {
this.guiId = guiId;
this.fps = fps;

map = MineScreen.instance().mapSupplier().get(guiId, width, height);
guiInstance = MineScreen.instance().guiInstanceSupplier().get(this);
mapGraphics = MineScreen.instance().mapGraphicsSupplier().get(this);
map = Map.of(this, width, height);
guiInstance = GuiInstance.of(this);
mapGraphics = MapGraphics.of(this);

guiInstance.placeMaps();

refreshScreenTask = MinecraftServer.getSchedulerManager()
.buildTask(MineScreen.instance().refreshScreenSupplier().get(this))
MinecraftServer.getSchedulerManager()
.buildTask(() -> {
render(mapGraphics());
sendPacketUpdate();
})
.repeat(1000 / fps, TimeUnit.MILLISECOND)
.executionType(ExecutionType.ASYNC)
.schedule();
}

Expand Down Expand Up @@ -96,8 +102,7 @@ public void render(MapGraphics renderer) {
final double loopTime = MathUtils.round((System.nanoTime() - loopStartTime) / 1_000_000D, 2);
if (loopTime >= 1000d / fps) {
final double loopTimeTooMuch = MathUtils.round(loopTime - (1000d / fps), 2);

System.err.printf("Loop took %sms, that's %sms too much.\n", loopTime, loopTimeTooMuch);
LOGGER.warn("Loop took {}ms, that's {}ms too much.\n", loopTime, loopTimeTooMuch);
}
}

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package nl.kiipdevelopment.minescreen.widget;

import nl.kiipdevelopment.minescreen.map.graphics.MapGraphics;
import org.jetbrains.annotations.ApiStatus;

@ApiStatus.Experimental
Expand All @@ -21,8 +20,6 @@ public int height() {
return height;
}

public abstract void draw(MapGraphics renderer);

@Override
public String toString() {
return getClass().getSimpleName() + "(" + width + ", " + height + ")";
Expand Down
Loading

0 comments on commit 402de76

Please sign in to comment.