forked from Rashnain/SaveMod
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNameSaveScreen.java
More file actions
79 lines (63 loc) · 2.85 KB
/
NameSaveScreen.java
File metadata and controls
79 lines (63 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.github.rashnain.savemod.gui;
import net.minecraft.client.gui.GuiGraphicsExtractor;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.EditBox;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.input.KeyEvent;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;
import java.util.function.Consumer;
public class NameSaveScreen extends Screen {
private final Screen parent;
private final String previousName;
private final String worldName;
private final Consumer<String> consumer;
private EditBox nameBox;
public NameSaveScreen(Screen parent, String previousName, String worldName, Consumer<String> consumer) {
super(Component.empty());
this.parent = parent;
this.previousName = previousName;
this.worldName = worldName;
this.consumer = consumer;
}
@Override
public boolean keyPressed(KeyEvent input) {
if (super.keyPressed(input))
return true;
if (getFocused() == nameBox && input.input() == 257 || input.input() == 335) {
consumer.accept(nameBox.getValue());
return true;
}
return false;
}
@Override
protected void init() {
nameBox = new EditBox(font, width / 2 - 100, height / 2 - 10, 200, 20, null, Component.empty());
addRenderableWidget(nameBox);
if (previousName != null && !previousName.equals(worldName))
nameBox.setValue(previousName);
Component message;
if (previousName == null || previousName.isEmpty())
message = Component.translatable("savemod.name.create");
else
message = Component.translatable("savemod.name.rename");
addRenderableWidget(Button.builder(message, button -> consumer.accept(nameBox.getValue())
).bounds(width / 2 - 150 - 5, height / 2 + 25, 150, 20).build());
addRenderableWidget(Button.builder(CommonComponents.GUI_CANCEL, button -> onClose()
).bounds(width / 2 + 5, height / 2 + 25, 150, 20).build());
setInitialFocus(nameBox);
}
@Override
public void extractRenderState(GuiGraphicsExtractor context, int mouseX, int mouseY, float delta) {
super.extractRenderState(context, mouseX, mouseY, delta);
if (previousName == null || previousName.isEmpty())
context.centeredText(font, Component.translatable("savemod.name.title.new"), width / 2, height / 2 - 45, -1);
else
context.centeredText(font, Component.translatable("savemod.name.title.rename"), width / 2, height / 2 - 45, -1);
context.centeredText(font, Component.translatable("savemod.name.hint", worldName), width / 2, height / 2 - 30, -0x808080);
}
@Override
public void onClose() {
minecraft.setScreen(parent);
}
}