Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds a Debug Panel #504

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/client/java/minicraft/core/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ protected Game() {} // Can't instantiate the Game class.
public static List<String> notifications = new ArrayList<>();

public static int MAX_FPS;
public static boolean debug = false;

// DISPLAY
static Display currentDisplay = null;
Expand Down
2 changes: 2 additions & 0 deletions src/client/java/minicraft/core/Initializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ static void parseArgs(String[] args) {
saveDir = args[i];
} else if (args[i].equalsIgnoreCase("--fullscreen")) {
Updater.FULLSCREEN = true;
} else if (args[i].equalsIgnoreCase("--debug")) { // Basic debugging flag
debug = true;
} else if (args[i].equalsIgnoreCase("--debug-log-time")) {
Logging.logTime = true;
} else if (args[i].equalsIgnoreCase("--debug-log-thread")) {
Expand Down
77 changes: 4 additions & 73 deletions src/client/java/minicraft/core/Updater.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import minicraft.level.tile.Tile;
import minicraft.level.tile.Tiles;
import minicraft.saveload.Save;
import minicraft.screen.DebugPanelDisplay;
import minicraft.screen.Display;
import minicraft.screen.EndGameDisplay;
import minicraft.screen.LevelTransitionDisplay;
Expand Down Expand Up @@ -90,15 +91,6 @@ static void updateFullscreen() {
// VERY IMPORTANT METHOD!! Makes everything keep happening.
// In the end, calls menu.tick() if there's a menu, or level.tick() if no menu.
public static void tick() {

// Quick Level change: move the player for -1, or 1 levels
if (isMode("minicraft.settings.mode.creative") && input.getKey("SHIFT-S").clicked ) {
Game.setDisplay(new LevelTransitionDisplay(-1));

} else if (isMode("minicraft.settings.mode.creative") && input.getKey("SHIFT-W").clicked ){
Game.setDisplay(new LevelTransitionDisplay(1));
}

if (input.getKey("FULLSCREEN").clicked) {
Updater.FULLSCREEN = !Updater.FULLSCREEN;
Updater.updateFullscreen();
Expand Down Expand Up @@ -220,70 +212,9 @@ public static void tick() {
}

// For debugging only
{
if (input.getKey("F3-L").clicked) {
// Print all players on all levels, and their coordinates.
Logging.WORLD.info("Printing players on all levels.");
for (Level value : levels) {
if (value == null) continue;
value.printEntityLocs(Player.class);
}
}

// Host-only cheats.
if (input.getKey("F3-T-1").clicked) changeTimeOfDay(Time.Morning);
if (input.getKey("F3-T-2").clicked) changeTimeOfDay(Time.Day);
if (input.getKey("F3-T-3").clicked) changeTimeOfDay(Time.Evening);
if (input.getKey("F3-T-4").clicked) changeTimeOfDay(Time.Night);

String prevMode = (String)Settings.get("mode");
if (input.getKey("F3-F4-2").clicked) {
Settings.set("mode", "minicraft.settings.mode.creative");
Logging.WORLDNAMED.trace("Game mode changed from {} into {}.", prevMode, "minicraft.settings.mode.creative");
}
if (input.getKey("F3-F4-1").clicked) {
Settings.set("mode", "minicraft.settings.mode.survival");
Logging.WORLDNAMED.trace("Game mode changed from {} into {}.", prevMode, "minicraft.settings.mode.survival");
}
if (input.getKey("F3-F4-3").clicked) {
Settings.set("mode", "minicraft.settings.mode.score");
Logging.WORLDNAMED.trace("Game mode changed from {} into {}.", prevMode, "minicraft.settings.mode.score");
}

if (isMode("minicraft.settings.mode.score") && input.getKey("F3-SHIFT-T").clicked) {
scoreTime = normSpeed * 5; // 5 seconds
}

float prevSpeed = gamespeed;
if (input.getKey("F3-S-0").clicked) {
gamespeed = 1;
Logging.WORLDNAMED.trace("Tick speed reset from {} into 1.", prevSpeed);
}
if (input.getKey("F3-S-equals").clicked) {
if (gamespeed < 1) gamespeed *= 2;
else if (normSpeed*gamespeed < 2000) gamespeed++;
Logging.WORLDNAMED.trace("Tick speed increased from {} into {}.", prevSpeed, gamespeed);
}
if (input.getKey("F3-S-minus").clicked) {
if (gamespeed > 1) gamespeed--;
else if (normSpeed*gamespeed > 5) gamespeed /= 2;
Logging.WORLDNAMED.trace("Tick speed decreased from {} into {}.", prevSpeed, gamespeed);
}

if (input.getKey("F3-h").clicked) player.health--;
if (input.getKey("F3-b").clicked) player.hunger--;

if (input.getKey("F3-M-0").clicked) player.moveSpeed = 1;
if (input.getKey("F3-M-equals").clicked) player.moveSpeed++;
if (input.getKey("F3-M-minus").clicked && player.moveSpeed > 1) player.moveSpeed--; // -= 0.5D;

if (input.getKey("F3-u").clicked) {
levels[currentLevel].setTile(player.x>>4, player.y>>4, Tiles.get("Stairs Up"));
}
if (input.getKey("F3-d").clicked) {
levels[currentLevel].setTile(player.x>>4, player.y>>4, Tiles.get("Stairs Down"));
}
} // End debug only cond.
if (debug && currentDisplay == null && input.getKey("F4").clicked) {
Game.setDisplay(new DebugPanelDisplay());
}
} // End "menu-null" conditional
} // End hasfocus conditional
} // End tick()
Expand Down
3 changes: 2 additions & 1 deletion src/client/java/minicraft/core/io/Localization.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package minicraft.core.io;

import minicraft.core.Game;
import minicraft.util.Logging;
import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;
Expand Down Expand Up @@ -43,7 +44,7 @@ public static String getLocalized(String key, Object... arguments) {

String localString = localization.get(key);

if (localString == null) {
if (localString == null && Game.debug) {
if (!knownUnlocalizedStrings.containsKey(selectedLocale)) knownUnlocalizedStrings.put(selectedLocale, new HashSet<>());
if (!knownUnlocalizedStrings.get(selectedLocale).contains(key)) {
Logger.tag("LOC").trace(unlocalizedStringTracing ? new Throwable("Tracing") : null, "{}: '{}' is unlocalized.", selectedLocale.toLanguageTag(), key);
Expand Down
13 changes: 0 additions & 13 deletions src/client/java/minicraft/entity/mob/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -267,13 +267,6 @@ public void addPotionEffect(PotionType type) {
public void tick() {
if (level == null || isRemoved()) return;
if (Game.getDisplay() != null) return; // Don't tick player when menu is open
if (input.getKey("F3-Y").clicked) {
World.scheduleLevelChange(1);
return;
} else if (input.getKey("F3-H").clicked) {
World.scheduleLevelChange(-1);
return;
}

super.tick(); // Ticks Mob.java

Expand Down Expand Up @@ -534,12 +527,6 @@ public void tick() {
LoadingDisplay.setPercentage(0);
new Save(WorldSelectDisplay.getWorldName());
}
//debug feature:
if (input.inputDown("F3-p")) { // Remove all potion effects
for (PotionType potionType : potioneffects.keySet()) {
PotionItem.applyPotion(this, potionType, false);
}
}

if (input.inputPressed("pickup") && (activeItem == null || !activeItem.used_pending)) {
if (!(activeItem instanceof PowerGloveItem)) { // If you are not already holding a power glove (aka in the middle of a separate interaction)...
Expand Down
2 changes: 1 addition & 1 deletion src/client/java/minicraft/level/tile/StairsTile.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public boolean mayPass(Level level, int x, int y, Entity e) {
public boolean interact(Level level, int xt, int yt, Player player, Item item, Direction attackDir) {
super.interact(level, xt, yt, player, item, attackDir);

// Makes it so you can remove the stairs if you are in creative and debug mode.
// Makes it so you can remove the stairs if you are in creative mode.
if (item instanceof PowerGloveItem && Game.isMode("minicraft.settings.mode.creative")) {
BenCheung0422 marked this conversation as resolved.
Show resolved Hide resolved
int data = level.getData(xt, yt);
level.setTile(xt, yt, Tiles.get("Grass"));
Expand Down
2 changes: 2 additions & 0 deletions src/client/java/minicraft/network/Analytics.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import kong.unirest.HttpResponse;
import kong.unirest.Unirest;
import kong.unirest.UnirestException;
import minicraft.core.Game;
import org.jetbrains.annotations.Nullable;
import org.tinylog.Logger;

Expand Down Expand Up @@ -52,6 +53,7 @@ public enum Analytics {

@Nullable public Future<HttpResponse<Empty>> ping() { return ping(1); }
@Nullable public Future<HttpResponse<Empty>> ping(int value) {
if (Game.debug) return null;
final String url = "https://pingdat.io?t="+token+"&v="+value;

return Unirest.get(url).asEmptyAsync(new Callback<Empty>() {
Makkkkus marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
155 changes: 155 additions & 0 deletions src/client/java/minicraft/screen/DebugPanelDisplay.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package minicraft.screen;

import minicraft.core.Game;
import minicraft.core.Updater;
import minicraft.core.World;
import minicraft.core.io.InputHandler;
import minicraft.core.io.Settings;
import minicraft.entity.mob.Player;
import minicraft.gfx.Point;
import minicraft.item.PotionItem;
import minicraft.item.PotionType;
import minicraft.level.Level;
import minicraft.level.tile.Tiles;
import minicraft.screen.entry.ListEntry;
import minicraft.screen.entry.SelectEntry;
import minicraft.util.Logging;

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

public class DebugPanelDisplay extends Display {
public DebugPanelDisplay() {
super(new Menu.Builder(true, 0, RelPos.LEFT, getEntries())
.setPositioning(new Point(9, 9), RelPos.BOTTOM_RIGHT)
.setDisplayLength(6)
.setSelectable(true)
.setScrollPolicies(1, false)
.setSearcherBar(true)
.setTitle("minicraft.display.debug_panel")
.createMenu());
}

private static List<ListEntry> getEntries() {
ArrayList<ListEntry> entries = new ArrayList<>();

entries.add(new SelectEntry("Print all players", () -> {
// Print all players on all levels, and their coordinates.
Logging.WORLD.info("Printing players on all levels.");
for (Level value : Game.levels) {
if (value == null) continue;
value.printEntityLocs(Player.class);
}
Game.exitDisplay();
}, false));
entries.add(new SelectEntry("Time set morning", () -> {
Updater.changeTimeOfDay(Updater.Time.Morning);
Game.exitDisplay();
}, false));
entries.add(new SelectEntry("Time set day", () -> {
Updater.changeTimeOfDay(Updater.Time.Day);
Game.exitDisplay();
}, false));
entries.add(new SelectEntry("Time set evening", () -> {
Updater.changeTimeOfDay(Updater.Time.Evening);
Game.exitDisplay();
}, false));
entries.add(new SelectEntry("Time set night", () -> {
Updater.changeTimeOfDay(Updater.Time.Night);
Game.exitDisplay();
}, false));
entries.add(new SelectEntry("Gamemode creative", () -> {
Settings.set("mode", "minicraft.settings.mode.creative");
Logging.WORLDNAMED.trace("Game mode changed from {} into {}.", Settings.get("mode"), "minicraft.settings.mode.creative");
Game.exitDisplay();
}, false));
entries.add(new SelectEntry("Gamemode survival", () -> {
Settings.set("mode", "minicraft.settings.mode.survival");
Logging.WORLDNAMED.trace("Game mode changed from {} into {}.", Settings.get("mode"), "minicraft.settings.mode.survival");
Game.exitDisplay();
}, false));
entries.add(new SelectEntry("Gamemode score", () -> {
Settings.set("mode", "minicraft.settings.mode.score");
Logging.WORLDNAMED.trace("Game mode changed from {} into {}.", Settings.get("mode"), "minicraft.settings.mode.score");
Game.exitDisplay();
}, false));
entries.add(new SelectEntry("Reset score time as 5 seconds", () -> {
if (Game.isMode("minicraft.settings.mode.score")) {
Updater.scoreTime = Updater.normSpeed * 5; // 5 seconds
}
Game.exitDisplay();
}, false));
entries.add(new SelectEntry("Reset tick speed (TPS)", () -> {
float prevSpeed = Updater.gamespeed;
Updater.gamespeed = 1;
Logging.WORLDNAMED.trace("Tick speed reset from {} into 1.", prevSpeed);
Game.exitDisplay();
}, false));
entries.add(new SelectEntry("Increase tick speed (TPS)", () -> {
float prevSpeed = Updater.gamespeed;
if (Updater.gamespeed < 1) Updater.gamespeed *= 2;
else if (Updater.normSpeed * Updater.gamespeed < 2000) Updater.gamespeed++;
Logging.WORLDNAMED.trace("Tick speed increased from {} into {}.", prevSpeed, Updater.gamespeed);
Game.exitDisplay();
}, false));
entries.add(new SelectEntry("Decrease tick speed (TPS)", () -> {
float prevSpeed = Updater.gamespeed;
if (Updater.gamespeed > 1) Updater.gamespeed--;
else if (Updater.normSpeed * Updater.gamespeed > 5) Updater.gamespeed /= 2;
Logging.WORLDNAMED.trace("Tick speed decreased from {} into {}.", prevSpeed, Updater.gamespeed);
Game.exitDisplay();
}, false));
entries.add(new SelectEntry("Reduce health point", () -> {
Game.player.health--;
Game.exitDisplay();
}, false));
entries.add(new SelectEntry("Reduce hunger point", () -> {
Game.player.hunger--;
Game.exitDisplay();
}, false));
entries.add(new SelectEntry("Reset moving speed", () -> {
Game.player.moveSpeed = 1;
Game.exitDisplay();
}, false));
entries.add(new SelectEntry("Increase moving speed", () -> {
Game.player.moveSpeed++;
Game.exitDisplay();
}, false));
entries.add(new SelectEntry("Decrease moving speed", () -> {
if (Game.player.moveSpeed > 1) Game.player.moveSpeed--; // -= 0.5D;
Game.exitDisplay();
}, false));
entries.add(new SelectEntry("Set tile stairs up", () -> {
Game.levels[Game.currentLevel].setTile(Game.player.x>>4, Game.player.y>>4, Tiles.get("Stairs Up"));
Game.exitDisplay();
}, false));
entries.add(new SelectEntry("Set tile stairs down", () -> {
Game.levels[Game.currentLevel].setTile(Game.player.x>>4, Game.player.y>>4, Tiles.get("Stairs Down"));
Game.exitDisplay();
}, false));
entries.add(new SelectEntry("Change level down (instant)", () -> {
Game.exitDisplay();
Game.setDisplay(new LevelTransitionDisplay(-1));
}, false));
entries.add(new SelectEntry("Change level up (instant)", () -> {
Game.exitDisplay();
Game.setDisplay(new LevelTransitionDisplay(1));
}, false));
entries.add(new SelectEntry("Change level up", () -> {
World.scheduleLevelChange(1);
Game.exitDisplay();
}, false));
entries.add(new SelectEntry("Change level down", () -> {
World.scheduleLevelChange(-1);
Game.exitDisplay();
}, false));
entries.add(new SelectEntry("Remove all potion effects", () -> {
for (PotionType potionType : Game.player.potioneffects.keySet()) {
PotionItem.applyPotion(Game.player, potionType, false);
}
Game.exitDisplay();
}, false));

return entries;
}
}
2 changes: 1 addition & 1 deletion src/client/java/minicraft/screen/TitleDisplay.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ private void checkVersion() {

@Override
public void tick(InputHandler input) {
if (input.getKey("F3-r").clicked) rand = random.nextInt(splashes.length - 3) + 3;
if (input.getKey("F3-r").clicked && Game.debug) rand = random.nextInt(splashes.length - 3) + 3;

super.tick(input);
}
Expand Down
4 changes: 3 additions & 1 deletion src/client/java/minicraft/util/TinylogLoggingProvider.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package minicraft.util;

import minicraft.core.Game;
import minicraft.util.TinylogLoggingConfiguration.WriterConfig;
import org.tinylog.Level;
import org.tinylog.core.ConfigurationParser;
Expand Down Expand Up @@ -178,7 +179,8 @@ private void output(final StackTraceElement stackTraceElement, final String tag,

Consumer<Writer> addToThread = writer -> {
WriterConfig cfg = writers.get(writer);
if (cfg.levels.contains(level) && cfg.tags.contains(tag))
if ((level.ordinal() > Level.DEBUG.ordinal() || Game.debug) &&
cfg.levels.contains(level) && cfg.tags.contains(tag))
writingThread.add(writer, logEntry);
};

Expand Down