Skip to content

Commit

Permalink
Fixed inventory searcher-bar
Browse files Browse the repository at this point in the history
Changes:
* Now the error messages of the commands will be shown in the chat.
* Now when typing in the search bar, words will automatically be capitalized.
* Updated Spanish translation
  • Loading branch information
TheBigEye committed Aug 5, 2023
1 parent f1a75f2 commit 4f3f7b4
Show file tree
Hide file tree
Showing 6 changed files with 225 additions and 215 deletions.
6 changes: 1 addition & 5 deletions src/main/java/minicraft/screen/CommandsDisplay.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@

public class CommandsDisplay extends Display {

private static final int CHAT_BACKGOUND_COLOR = Color.BLACK;
private static final int CHAT_FOREGOUND_COLOR = Color.GRAY;
private static final float CHAT_OPACITY = 0.8F;

public static InputEntry command = new InputEntry("", ".*", 43, false);

public CommandsDisplay() {
Expand Down Expand Up @@ -103,7 +99,7 @@ public void render(Screen screen) {
int lineNumber = 0;
for (int j = Math.max(0, messages.size() - 7); j < messages.size(); j++) {
Font.draw("<Amy>", screen, 8, (lineNumber * 8) + Screen.h - 80, Color.DARK_GREEN);
Font.draw(messages.get(j), screen, 6 * 8 + 4, (lineNumber * 8) + Screen.h - 80, CHAT_FOREGOUND_COLOR);
Font.draw(messages.get(j), screen, 6 * 8 + 4, (lineNumber * 8) + Screen.h - 80, Color.GRAY);
lineNumber++;
}

Expand Down
20 changes: 19 additions & 1 deletion src/main/java/minicraft/screen/Menu.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,21 @@ public void tick(InputHandler input) {

if (searcherBarActive) {
String typingSearcher = input.addKeyTyped(this.typingSearcher, null);

if (typingSearcher.length() > 0) {
// Convert the first letter on uppercase
typingSearcher = typingSearcher.substring(0, 1).toUpperCase() + typingSearcher.substring(1);

// Iterate over the remaining characters to convert to lowercase until a space is found
for (int i = 1; i < typingSearcher.length(); i++) {
if (typingSearcher.charAt(i - 1) == ' ') {
typingSearcher = typingSearcher.substring(0, i) + typingSearcher.substring(i, i + 1).toUpperCase() + typingSearcher.substring(i + 1);
}
}
}



for (String pressedKey : input.getAllPressedKeys()) {
if (pressedKey.equals("ENTER")) {
continue;
Expand All @@ -240,14 +255,15 @@ public void tick(InputHandler input) {
// check if word was updated
if (typingSearcher.length() <= ((entryBounds.getWidth() / 8)) && typingSearcher.length() != this.typingSearcher.length()) {
this.typingSearcher = typingSearcher;

listSearcher.clear();
listPositionSearcher = 0;

Iterator<ListEntry> entryIt = entries.iterator();
boolean shouldSelect = true;
for (int i = 0; entryIt.hasNext(); i++) {
ListEntry entry = entryIt.next();

String stringEntry = entry.toString();
String typingString = typingSearcher;

Expand Down Expand Up @@ -326,6 +342,8 @@ private void doScroll() {
public void render(Screen screen) {
// render searcher bar
if (searcherBarActive && useSearcherBar) {


int leading = Font.textWidth(typingSearcher) * Font.textWidth(" ") / 15;
int xSearcherBar = titleLoc.x + title.length() * 8 / 2 - 16;

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/minicraft/screen/entry/InputEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ public InputEntry(String prompt, String regex, int maxLen, String initValue, boo
public void tick(InputHandler input) {
String prev = userInput;
userInput = input.addKeyTyped(userInput, regex);

if (!prev.equals(userInput) && listener != null)
listener.onChange(userInput);

if (maxLength > 0 && userInput.length() > maxLength)
userInput = userInput.substring(0, maxLength); // truncates extra
if (input.getKey("CTRL-V").clicked) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/minicraft/screen/entry/ItemEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void tick(InputHandler input) {
@Override
public void render(Screen screen, int x, int y, boolean isSelected) {
super.render(screen, x, y, true);
item.sprite.render(screen, x, y);
item.sprite.render(screen, x - 4, y);
}

// if you add to the length of the string, and therefore the width of the entry,
Expand Down
48 changes: 30 additions & 18 deletions src/main/java/minicraft/util/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ public Entity[] getEntitiesBySelector(String targetSelector) {
Entity[] entities = new Entity[0];

switch (targetSelector) {
case "@a": entities = currentLevel.getPlayers(); break; // all players
case "@e": entities = currentLevel.getEntities(); break; // all entities
case "@p": entities = new Entity[]{currentPlayer}; break; // nearest player
case "@s": entities = new Entity[]{currentPlayer}; break; // executing entity
case ALL_PLAYERS: entities = currentLevel.getPlayers(); break; // all players
case ALL_ENTITIES: entities = currentLevel.getEntities(); break; // all entities
case NEAR_PLAYER: entities = new Entity[]{currentPlayer}; break; // nearest player
case EXECUTING_ENTITY: entities = new Entity[]{currentPlayer}; break; // executing entity

default:
// parse target selector and return matching entities
Expand All @@ -38,7 +38,7 @@ public Entity[] getEntitiesBySelector(String targetSelector) {

public static void gamemodeCommand(String[] arguments) {
if (arguments.length < 2) {
Game.notifications.add("- missing gamemode -");
Game.player.sendMessage("{?} - Missing gamemode [creative, survival]");
return;
}

Expand All @@ -48,14 +48,14 @@ public static void gamemodeCommand(String[] arguments) {
} else if (mode.equals("survival") || mode.equals("s") || mode.equals("0")) {
Settings.set("mode", "survival");
} else {
Game.notifications.add("- unknown gamemode -");
Game.player.sendMessage("{?} - Unknown gamemode");
}
}

public static void timeCommand(String[] arguments) {

if (arguments.length < 2) {
Game.notifications.add("- missing time action -");
Game.player.sendMessage("{?} - Missing time action [add, set, get]");
return;
}

Expand All @@ -64,17 +64,18 @@ public static void timeCommand(String[] arguments) {
case "set":

if (arguments.length < 3) {
Game.notifications.add("- missing time of day -");
Game.player.sendMessage("{?} - Missing time of day [day, night, 12000]");
return;
}

String timeString = arguments[2]; // morning, night, etc
int timeValue = 0;

if (timeString.matches("\\d+")) {
try {
timeValue = Integer.parseInt(timeString);
} catch (NumberFormatException exception) {
Game.notifications.add("- Time value must be a numeric value -");
Game.player.sendMessage("{?} - Time value must be a numeric value");
return;
}
Updater.tickCount = timeValue;
Expand All @@ -85,22 +86,33 @@ public static void timeCommand(String[] arguments) {
case "evening": Updater.changeTimeOfDay(Updater.Time.Evening); break;
case "night": Updater.changeTimeOfDay(Updater.Time.Night); break;

default: Game.notifications.add("- Unknown time status '"+ timeString + "' -"); break;
default:
Game.player.sendMessage("{?} - Unknown time status '"+ timeString + "'");
break;
}
break;
}

case "add":

if (arguments.length < 3) {
Game.notifications.add("- missing time of day -");
Game.player.sendMessage("{?} - Missing time of day [1000, 6000, 32000]");
return;
}

int timeIndex = Integer.parseInt(arguments[2]); // 6000, 18000, 64000

int timeIndex = 0;

try {
timeIndex = Integer.parseInt(arguments[2]); // 6000, 18000, 64000
} catch (NumberFormatException exception) {
Game.player.sendMessage("{?} - Time value must be a numeric value");
}

Updater.tickCount += timeIndex; break;

default: Game.notifications.add("- Unknown time modifier'" + timeAction + "' -"); break;
default:
Game.player.sendMessage("{?} - Unknown time action '" + timeAction + "'");
break;
}
}

Expand All @@ -122,7 +134,7 @@ public static void killCommand(String[] arguments) {
for (Player player : players) {
player.die();
}
Game.notifications.add("Ouch! That looked like it hurt");
Game.player.sendMessage("{!} - All the players has died");
}
break;

Expand All @@ -131,18 +143,18 @@ public static void killCommand(String[] arguments) {
for (Entity entity : entities) {
entity.die();
}
Game.notifications.add("Ouch! That looked like it hurt");
Game.player.sendMessage("{!} - All the entities has died");
}
break;

case NEAR_PLAYER: // Get the closest player on the level and kill them
currentPlayer.die();
Game.notifications.add("Ouch! That looked like it hurt");
Game.player.sendMessage("{!} - A player has died");
break;

case EXECUTING_ENTITY: // Kills the entity that executes the command
executorEntity.die();
Game.notifications.add("Ouch! That looked like it hurt");
Game.player.sendMessage("{!} - A player has commit suicide");
break;

default:
Expand Down
Loading

0 comments on commit 4f3f7b4

Please sign in to comment.