initial commit

This commit is contained in:
Sen 2025-03-11 00:23:54 +01:00 committed by Sen
parent 3c9ee26b06
commit 22186c33b9
1458 changed files with 282792 additions and 0 deletions

View file

@ -0,0 +1,405 @@
package game.gui.world;
import java.io.File;
import java.io.IOException;
import java.util.Set;
import game.collect.Sets;
import game.color.TextColor;
import game.dimension.DimType;
import game.dimension.Dimension;
import game.dimension.Space;
import game.gui.GuiScreen;
import game.gui.element.Button;
import game.gui.element.TextField;
import game.init.UniverseRegistry;
import game.lib.input.Keyboard;
import game.network.NetHandlerPlayServer;
import game.renderer.FontRenderer;
import game.rng.Random;
import game.util.Predicate;
import game.world.Region;
public class GuiCreate extends GuiScreen
{
private static final String MBASE32_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ!?-_<3";
private static final String MBASE64A_CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-!";
private static final String MBASE64B_CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz<_";
private GuiScreen parentScreen;
private TextField worldNameField;
private TextField worldSeedField;
private TextField worldUserField;
// private Button modeButton;
private Button dimButton;
// private String descLine1;
// private String descLine2;
private String[] descLines;
private boolean alreadyGenerated;
private boolean fileExists;
private boolean fileInvalid;
private String seed;
private String decoded;
private int dimension;
private static long getSeed(String str) {
if(str == null || str.isEmpty())
return (new Random()).longv();
try {
return Long.parseLong(str);
}
catch(NumberFormatException e) {
}
long seed = 0L;
if(str.length() <= 9) {
for(int z = 0; z < str.length(); z++) {
seed <<= 7;
char c = str.charAt(z);
if(c >= 0x80) {
seed = -1L;
break;
}
seed |= (long)c;
}
}
else if(str.length() == 10) {
String ch = str.indexOf('<') != -1 || str.indexOf('_') != -1 ? MBASE64B_CHARS : MBASE64A_CHARS;
for(int z = 0; z < str.length(); z++) {
seed <<= 6;
int idx = ch.indexOf(str.charAt(z));
if(idx == -1) {
seed = -1L;
break;
}
seed |= (long)idx;
}
if(seed != -1L)
seed |= ch == MBASE64B_CHARS ? 0xb000000000000000L : 0xa000000000000000L;
}
else if(str.length() <= 12) {
for(int z = 0; z < str.length(); z++) {
seed <<= 5;
int idx = MBASE32_CHARS.indexOf(Character.toUpperCase(str.charAt(z)));
if(idx == -1) {
seed = -1L;
break;
}
seed |= (long)idx;
}
if(seed != -1L)
seed |= str.length() == 12 ? 0x9000000000000000L : 0x8000000000000000L;
}
else {
seed = -1L;
}
if(seed == -1L) {
seed = 0L;
for(int z = 0; z < str.length(); z++) {
seed <<= 7;
seed |= (long)((char)(str.charAt(z) & 0x7f));
}
seed = (seed ^ (((long)str.hashCode()) | (~((long)str.hashCode()) << 32))) | 0xc000000000000000L;
}
return seed;
}
private static String decodeSeed(long seed, String def) {
if(seed == 0L)
return (def == null ? ("" + seed) : def);
if((seed & 0x8000000000000000L) == 0L) {
String str = "";
long value = seed;
while(value != 0L) {
char c = (char)(value & 0x7fL);
if(c < 32 || c > 126)
return (def == null ? ("" + seed) : def);
str = c + str;
value >>= 7;
}
try {
Long.parseLong(str);
}
catch(NumberFormatException e) {
return str;
}
return (def == null ? ("" + seed) : def);
}
String chset;
int len;
int shift;
long mask;
long type = (seed >> 60) & 0xf;
switch((int)type) {
case 0x8:
chset = MBASE32_CHARS;
len = 11;
shift = 5;
mask = 0x1fL;
break;
case 0x9:
chset = MBASE32_CHARS;
len = 12;
shift = 5;
mask = 0x1fL;
break;
case 0xa:
chset = MBASE64A_CHARS;
len = 10;
shift = 6;
mask = 0x3fL;
break;
case 0xb:
chset = MBASE64B_CHARS;
len = 10;
shift = 6;
mask = 0x3fL;
break;
default:
return (def == null ? ("" + seed) : def);
}
String str = "";
for(int z = 0; z < len; z++) {
str = chset.charAt((int)(seed & mask)) + str;
seed >>= shift;
}
try {
Long.parseLong(str);
}
catch(NumberFormatException e) {
return str;
}
return (def == null ? ("" + seed) : def);
}
public GuiCreate(GuiScreen parent)
{
UniverseRegistry.clear();
this.parentScreen = parent;
// this.worldSeed = "";
// this.worldName = "Neue Welt";
}
public void updateScreen()
{
this.worldNameField.updateCursorCounter();
this.worldSeedField.updateCursorCounter();
this.worldUserField.updateCursorCounter();
String text = this.worldSeedField.getText();
if(text.isEmpty()) {
this.seed = "Zufällig";
this.decoded = "-";
}
else {
this.seed = "" + getSeed(text);
try {
this.decoded = decodeSeed(Long.parseLong(text), "-");
}
catch(NumberFormatException e) {
this.decoded = decodeSeed(getSeed(text), "-");
}
}
this.fileExists = false;
this.fileInvalid = false;
text = this.worldNameField.getText().trim();
// for(String n : GuiCreate.DISALLOWED_FILES) {
// if(text.equalsIgnoreCase(n)) {
// ((Button)this.buttonList.get(0)).enabled = false;
// this.fileInvalid = true;
// return;
// }
// }
if(this.fileInvalid = GuiCreate.DISALLOWED_FILES.contains(text.toUpperCase())) {
((Button)this.buttonList.get(0)).enabled = false;
return;
}
if(this.fileExists = (!text.isEmpty() && new File(Region.SAVE_DIR, text).exists()))
((Button)this.buttonList.get(0)).enabled = false;
}
public void initGui()
{
Keyboard.enableRepeatEvents(true);
this.buttonList.clear();
this.buttonList.add(new Button(0, this.width / 2 - 155, this.height - 28, 150, 20, "Neue Welt erstellen"));
this.buttonList.add(new Button(1, this.width / 2 + 5, this.height - 28, 150, 20, "Abbrechen"));
// this.buttonList.add(this.modeButton = new Button(2, this.width / 2 - 155, 175, 310, 20, ""));
this.buttonList.add(this.dimButton = new Button(2, this.width / 2 - 155, 220, 310, 20, ""));
this.worldNameField = new TextField(this.width / 2 - 100, 60, 200, 20);
this.worldNameField.setFocused(true);
this.worldNameField.setValidator(new Predicate<String>() {
public boolean apply(String name) {
for(int z = 0; z < name.length(); z++) {
if(DISALLOWED_CHARS.contains(name.charAt(z)))
return false;
}
return true;
}
});
// this.worldNameField.setText("Welt");
this.worldSeedField = new TextField(this.width / 2 - 100, 136, 200, 20);
// this.worldSeedField.setText(this.worldSeed);
this.worldUserField = new TextField(this.width / 2 - 100, 98, 200, 20);
this.worldUserField.setMaxStringLength(16);
this.worldUserField.setValidator(new Predicate<String>() {
public boolean apply(String name) {
return NetHandlerPlayServer.isValidUser(name);
}
});
// this.calcSaveDirName();
// this.setModeButton();
this.setDimButton();
((Button)this.buttonList.get(0)).enabled = false;
}
// private void setModeButton() {
// this.modeButton.display = "Kreativmodus: " + (this.noCreative ? "Aus" : "An");
// this.descLine1 = this.noCreative ? "Suche nach Ressourcen, baue Werkzeuge, Waffen und mehr"
// : "Unbegrenzte Rohstoffe, Flugmodus, Unverwundbarkeit, keine";
// this.descLine2 = this.noCreative ? "Gegenstände, sammle Erfahrung und erkunde die Welten"
// : "Energie oder Mana und sofortiges Zerstören von Blöcken";
// }
private void setDimButton() {
Dimension dim = this.dimension == Integer.MAX_VALUE ? Space.INSTANCE : UniverseRegistry.getBaseDimensions().get(this.dimension);
this.dimButton.display = (dim == Space.INSTANCE ? "" :
((dim.getDimensionId() >= UniverseRegistry.MORE_DIM_ID ?
"Vorlage" : (dim.getType() == DimType.PLANET ? "Heimplanet" : "Dimension")) + ": ")) +
(dim.getDimensionId() >= UniverseRegistry.MORE_DIM_ID ? dim.getCustomName() :
dim.getFormattedName(false));
this.descLines = dim.getFormattedName(true).split(" / ");
}
// private void calcSaveDirName()
// {
// this.saveDirName = this.worldNameField.getText().trim();
//
//// for (char c : DISALLOWED_CHARS)
//// {
//// this.saveDirName = this.saveDirName.replace(c, '_');
//// }
//
// if (!this.saveDirName.isEmpty())
//// {
//// this.saveDirName = "World";
//// }
//
// this.saveDirName = getUncollidingSaveDirName(Region.SAVE_DIR, this.saveDirName);
// }
public void onGuiClosed()
{
Keyboard.enableRepeatEvents(false);
}
protected void actionPerformed(Button button) throws IOException
{
if (button.enabled)
{
if (button.id == 1)
{
this.gm.displayGuiScreen(this.parentScreen);
}
else if (button.id == 0)
{
this.gm.displayGuiScreen(null);
if(this.alreadyGenerated)
return;
this.alreadyGenerated = true;
Dimension dim = this.dimension == Integer.MAX_VALUE ? Space.INSTANCE :
UniverseRegistry.getBaseDimensions().get(this.dimension);
dim.setSeed(getSeed(this.worldSeedField.getText()));
this.gm.launchIntegratedServer(this.worldNameField.getText().trim(), dim, this.worldUserField.getText());
}
// else if (button.id == 2)
// {
// this.noCreative ^= true;
// this.setModeButton();
// }
else if (button.id == 2)
{
// int index = Integer.MAX_VALUE;
// for(int z = 0; z < UniverseRegistry.getBasePlanets().size(); z++) {
// if(UniverseRegistry.getBasePlanets().get(z).getDimensionId() == this.dimension) {
// index = z;
// break;
// }
// }
if(this.dimension == Integer.MAX_VALUE) {
this.dimension = GuiScreen.isShiftKeyDown() ? UniverseRegistry.getBaseDimensions().size() - 1 : 0;
}
else {
if(GuiScreen.isShiftKeyDown()) {
if(--this.dimension < 0)
this.dimension = Integer.MAX_VALUE;
}
else {
if(++this.dimension >= UniverseRegistry.getBaseDimensions().size())
this.dimension = Integer.MAX_VALUE;
}
}
// this.dimension = index == Integer.MAX_VALUE ? Space.INSTANCE.getDimensionId() :
// UniverseRegistry.getBasePlanets().get(index).getDimensionId();
this.setDimButton();
}
}
}
protected void keyTyped(char typedChar, int keyCode) throws IOException
{
// if (this.worldNameField.isFocused())
// {
this.worldNameField.textboxKeyTyped(typedChar, keyCode);
// this.worldName = this.worldNameField.getText();
// }
// else if (this.worldSeedField.isFocused())
// {
this.worldSeedField.textboxKeyTyped(typedChar, keyCode);
// this.worldSeed = this.worldSeedField.getText();
// }
// else if (this.worldUserField.isFocused())
// {
this.worldUserField.textboxKeyTyped(typedChar, keyCode);
// }
if (keyCode == 28 || keyCode == 156)
{
this.actionPerformed((Button)this.buttonList.get(0));
}
((Button)this.buttonList.get(0)).enabled = this.worldNameField.getText().length() > 0 && this.worldUserField.getText().length() > 0;
// this.calcSaveDirName();
}
protected void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException
{
super.mouseClicked(mouseX, mouseY, mouseButton);
this.worldSeedField.mouseClicked(mouseX, mouseY, mouseButton);
this.worldNameField.mouseClicked(mouseX, mouseY, mouseButton);
this.worldUserField.mouseClicked(mouseX, mouseY, mouseButton);
}
public void drawScreen(int mouseX, int mouseY, float partialTicks)
{
this.drawBackground();
FontRenderer.drawCenteredString("Neue Welt erstellen", this.width / 2, 20, -1);
FontRenderer.drawStringWithShadow("Startwert [" + this.seed + "]", this.width / 2 - 100, 125, -6250336);
FontRenderer.drawStringWithShadow("Dekodiert: " + this.decoded, this.width / 2 - 100, 160, -6250336);
FontRenderer.drawStringWithShadow((this.worldNameField.getText().trim().isEmpty() || this.fileExists || this.fileInvalid ? TextColor.DARK_RED : "")
+ "Ordner der Welt" + (this.fileExists ? " - Existiert bereits" : ""), this.width / 2 - 100, 49, -6250336);
FontRenderer.drawStringWithShadow((this.worldUserField.getText().isEmpty() ? TextColor.DARK_RED : "") + "Spielername", this.width / 2 - 100, 87, -6250336);
this.worldSeedField.drawTextBox();
this.worldNameField.drawTextBox();
this.worldUserField.drawTextBox();
// FontRenderer.drawStringWithShadow(this.descLine1, this.width / 2 - 153, 197, -6250336);
// FontRenderer.drawStringWithShadow(this.descLine2, this.width / 2 - 153, 197 + FontRenderer.FONT_HEIGHT + 1, -6250336);
for(int z = 1; z < this.descLines.length; z++) {
FontRenderer.drawStringWithShadow(this.descLines[z], this.width / 2 - 153, 242 + (z - 1) * (FontRenderer.FONT_HEIGHT + 1), -6250336);
}
super.drawScreen(mouseX, mouseY, partialTicks);
}
}

View file

@ -0,0 +1,140 @@
package game.gui.world;
import java.io.File;
import game.ActButton;
import game.Gui;
import game.Label;
import game.Textbox;
import game.Textbox.Action;
import game.ActButton.Mode;
import game.color.TextColor;
import game.network.NetHandlerPlayServer;
import game.world.Region;
public class GuiEdit extends Gui implements ActButton.Callback, Textbox.Callback {
public static interface Callback {
void confirm(String name);
}
private final String original;
private final boolean player;
private final String title;
private final String action;
private final String desc;
private final Callback callback;
private Textbox nameField;
private ActButton actionButton;
private ActButton cancelButton;
private Label actionLabel;
private boolean fileExists;
private boolean noChange;
public GuiEdit(String original, boolean player, String title, String action, String desc, Callback callback) {
this.original = original;
this.player = player;
this.title = title;
this.action = action;
this.desc = desc;
this.callback = callback;
}
public void updateScreen() {
this.actionButton.enabled = true;
this.fileExists = false;
this.noChange = false;
String text = this.nameField.getText().trim();
if(text.isEmpty() || (this.noChange = (text.equals(this.original) || (!this.player && !text.replaceAll("[\\./\"]", "_")
.equals(text))))) {
this.actionButton.enabled = false;
this.actionLabel.setText(this.getLabelDesc());
return;
}
if(this.player) {
this.actionLabel.setText(this.getLabelDesc());
return;
}
// {
// if(!StringValidator.isValidUser(text))
// this.actionButton.enabled = false;
// }
// else {
// for(String n : GuiCreate.DISALLOWED_FILES) {
// if(text.equalsIgnoreCase(n)) {
// this.actionButton.enabled = false;
// return;
// }
// }
if(this.fileExists = (!text.isEmpty() && new File(Region.SAVE_DIR, text).exists()))
this.actionButton.enabled = false;
this.actionLabel.setText(this.getLabelDesc());
// }
}
public void init(int width, int height) {
this.actionButton = this.add(new ActButton(width / 2 - 100, height / 4 + 96 + 12, 200, 20, (ActButton.Callback)this, this.action));
this.cancelButton = this.add(new ActButton(width / 2 - 100, height / 4 + 120 + 12, 200, 20, (ActButton.Callback)this, "Abbrechen"));
this.nameField = this.add(new Textbox(width / 2 - 100, 60, 200, 20, this.player ? NetHandlerPlayServer.MAX_USER_LENGTH : 256, true, this, this.player ? NetHandlerPlayServer.VALID_USER : GuiWorlds.VALID_FILE, this.original == null ? "" : this.original));
this.nameField.setSelected();
// if(this.player) {
// this.nameField.setMaxStringLength(16);
// this.nameField.setValidator(new Predicate<String>() {
// public boolean apply(String name) {
// return NetHandlerPlayServer.isValidUser(name);
// }
// });
// }
// else {
// this.nameField.setValidator(new Predicate<String>() {
// public boolean apply(String name) {
// for(int z = 0; z < name.length(); z++) {
// if(GuiCreate.DISALLOWED_CHARS.contains(name.charAt(z)))
// return false;
// }
// return true;
// }
// });
// }
this.actionLabel = this.add(new Label(width / 2 - 100, 20, 200, 24, this.getLabelDesc()));
// this.shift();
// this.nameField.setText();
this.actionButton.enabled = false;
}
public String getTitle() {
return "Welt bearbeiten";
}
public void use(ActButton btn, Mode mode) {
this.callback.confirm(btn == this.actionButton ? this.nameField.getText().trim() : null);
}
public void use(Textbox elem, Action action) {
if(action == Action.SEND)
this.use(this.actionButton, Mode.PRIMARY);
}
// protected void actionPerformed(Button button) throws IOException {
// if(button.enabled) {
// if(button.id == 1) {
// }
// else if(button.id == 0) {
// this.callback.confirm(this.nameField.getText().trim());
//// this.gm.displayGuiScreen(this.parent);
// }
// }
// }
// protected void keyTyped(char typedChar, int keyCode) throws IOException {
// ((Button)this.buttonList.get(0)).enabled = this.nameField.getText().trim().length() > 0;
////
//// if(keyCode == 28 || keyCode == 156) {
//// this.actionPerformed((Button)this.buttonList.get(0));
//// }
// }
private String getLabelDesc() {
return (this.actionButton.enabled ? "" : TextColor.DRED) + this.desc +
(this.fileExists ? " - Existiert bereits" : (this.noChange ? " - Nicht geändert" : ""));
}
}

View file

@ -0,0 +1,633 @@
package game.gui.world;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.Set;
import game.ActButton;
import game.CharValidator;
import game.ActButton.Mode;
import game.Drawing;
import game.FileCallback;
import game.FileUtils;
import game.GuiMenu;
import game.Log;
import game.WCF;
import game.collect.Sets;
import game.color.TextColor;
import game.dimension.Dimension;
import game.gui.GuiConfirm;
import game.gui.GuiList;
import game.gui.ListEntry;
import game.gui.world.GuiEdit.Callback;
import game.init.Config;
import game.init.UniverseRegistry;
import game.nbt.NBTLoader;
import game.nbt.NBTTagCompound;
import game.world.Converter;
import game.world.Converter.SaveVersion;
import game.world.Region;
import game.world.World;
import game.world.Region.FolderInfo;
public class GuiWorlds extends GuiList<GuiWorlds.SaveInfo> implements ActButton.Callback
{
protected class SaveInfo implements Comparable<SaveInfo>, ListEntry {
private final String file;
private final String name;
private final long seed;
private final FolderInfo info;
public SaveInfo(String file, String name, long seed, FolderInfo info) {
this.file = file;
this.name = name;
this.seed = seed;
this.info = info;
}
public String getFile() {
return this.file;
}
public String getName() {
return this.name == null ? "<?>" : this.name;
}
public String getUser() {
return this.info.user == null ? "<?>" : this.info.user;
}
public String getUsername() {
return this.info.user;
}
public boolean mustConvert() {
return this.info.legacy != null;
}
public String getVersion() {
return this.info.legacy == null ? (this.info.version == null ? "<Unbekannt>" : this.info.version) : this.info.legacy.toString();
}
public boolean isIncompatible() {
return this.info.legacy == SaveVersion.RELEASE_1_13;
}
public long getLastPlayed() {
return this.info.lastPlayed;
}
public long getSeed() {
return this.seed;
}
public int compareTo(SaveInfo comp) {
return this.info.lastPlayed < comp.info.lastPlayed ? 1 : (this.info.lastPlayed > comp.info.lastPlayed ? -1 : this.file.compareTo(comp.file));
}
public void select(boolean isDoubleClick, int mouseX, int mouseY)
{
boolean use = !this.isIncompatible();
boolean cur = use && !this.mustConvert();
GuiWorlds.this.selectButton.setText(!use || !cur ? "Konvertieren" : "Welt spielen");
GuiWorlds.this.selectButton.enabled = use;
GuiWorlds.this.deleteButton.enabled = true;
GuiWorlds.this.pruneButton.enabled = cur;
GuiWorlds.this.copyButton.enabled = use;
GuiWorlds.this.moveButton.enabled = use;
GuiWorlds.this.seedButton.enabled = cur;
GuiWorlds.this.userButton.enabled = cur;
GuiWorlds.this.dupeButton.enabled = cur;
if (isDoubleClick && use)
{
GuiWorlds.this.playWorld(null);
}
}
public void draw(int x, int y, int mouseXIn, int mouseYIn, boolean hover)
{
Drawing.drawText((this.isIncompatible() ? TextColor.DRED : "") + this.getFile() + (this.mustConvert() ? "" :
(TextColor.GRAY + " - " + TextColor.RESET + this.getUser())), x + 2, y, 0xffffffff);
Drawing.drawText((this.mustConvert() ? (this.isIncompatible() ? TextColor.CRIMSON : "") + this.getVersion() : this.getName())
, x + 2, y + 18, 0xff808080);
Drawing.drawText(this.mustConvert() ? (this.isIncompatible() ? TextColor.CRIMSON + "Kann nicht konvertiert werden!" :
"Muss konvertiert werden!") : ( // "Kreativmodus: " + (info.isNoCreative() ? "Aus" : "An") +
"Zuletzt gespielt: " + DATE_FORMAT.format(new Date(this.getLastPlayed()))) + " " + TextColor.LGRAY + this.getVersion(), x + 2, y + 18 + 16, 0xff808080);
}
}
public static final GuiWorlds INSTANCE = new GuiWorlds();
public static final Set<Character> DISALLOWED_CHARS = Sets.newHashSet('/', '\n', '\r', '\t', '\u0000', '\f', '`', '?', '*', '\\',
'<', '>', '|', '\"', ':');
public static final CharValidator VALID_FILE = new CharValidator() {
public boolean valid(char ch) {
return !DISALLOWED_CHARS.contains(ch);
}
};
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
private boolean starting;
private int warningTimer;
private String warningMessage;
private ActButton deleteButton;
private ActButton pruneButton;
private ActButton selectButton;
private ActButton copyButton;
private ActButton moveButton;
private ActButton seedButton;
private ActButton userButton;
private ActButton dupeButton;
private ActButton createButton;
private GuiWorlds()
{
}
public void init(int width, int height)
{
this.starting = false;
this.setDimensions(width, height, 48, height - 64);
boolean create = true;
this.elements.clear();
try
{
Region.SAVE_DIR.mkdirs();
if(!Region.SAVE_DIR.exists() || !Region.SAVE_DIR.isDirectory())
throw new RuntimeException("Kann den Speicherordner für Welten nicht lesen oder öffnen!");
// java.util.List<SaveInfo> list = Lists.<SaveInfo>newArrayList();
File[] files = Region.SAVE_DIR.listFiles();
if(files == null)
throw new RuntimeException("Kann den Speicherordner für Welten nicht lesen oder öffnen!");
for(File file : files) {
if(!file.isDirectory())
continue;
FolderInfo info = Region.loadWorldInfo(file);
if(info == null)
info = Converter.convertMapFormat(file, null);
if(info == null) {
this.elements.add(new SaveInfo(file.getName(), null,
0L, new FolderInfo(World.START_TIME, null, file.lastModified(), null, null)));
continue;
}
Dimension dim = info.legacy != null ? null : UniverseRegistry.getDimension(Config.spawnDim);
if(dim != null) {
File dat = new File(new File(new File(new File(Region.SAVE_DIR, file.getName()), "chunk"),
dim.getDimensionName()), "data.nbt");
try {
dim.fromNbt(NBTLoader.readGZip(dat).getCompoundTag("Generator"));
}
catch(Exception e) {
}
}
this.elements.add(new SaveInfo(file.getName(), dim == null ? null : TextColor.stripCodes(dim.getFormattedName(true)),
dim == null ? 0L : dim.getSeed(), info));
}
// this.saveList = list;
Collections.sort(this.elements);
}
catch (Exception e)
{
Log.IO.error("Konnte Weltliste nicht laden", e);
this.elements.clear();
create = false;
this.warningTimer = 120;
this.warningMessage = "Welten-Ordner nicht lesbar!";
}
this.add(this.selectButton = new ActButton(width / 2 - 153 - 39, height - 52, 72, 20, (ActButton.Callback)this, "Welt spielen"));
this.add(this.createButton = new ActButton(width / 2 + 81 + 72 + 6 - 39, height - 52, 72, 20, (ActButton.Callback)this,
(create ? "" : "" + TextColor.DRED) + (create ? "Neue Welt ..." : "Fehler!")));
this.add(this.deleteButton = new ActButton(width / 2 - 75 - 39, height - 28, 72, 20, (ActButton.Callback)this, "Löschen"));
this.add(this.pruneButton = new ActButton(width / 2 + 3 - 39, height - 28, 72, 20, (ActButton.Callback)this, "Leeren"));
this.add(this.copyButton = new ActButton(width / 2 - 153 - 39, height - 28, 72, 20, (ActButton.Callback)this, "Kopieren"));
this.add(this.moveButton = new ActButton(width / 2 + 81 - 39, height - 28, 72, 20, (ActButton.Callback)this, "Verschieben"));
this.add(this.seedButton = new ActButton(width / 2 + 3 - 39, height - 52, 72, 20, (ActButton.Callback)this, "Startwert"));
this.add(this.userButton = new ActButton(width / 2 - 75 - 39, height - 52, 72, 20, (ActButton.Callback)this, "Spieler"));
this.add(this.dupeButton = new ActButton(width / 2 + 81 - 39, height - 52, 72, 20, (ActButton.Callback)this, "Duplizieren"));
this.add(new ActButton(width / 2 + 81 + 72 + 6 - 39, height - 28, 72, 20, GuiMenu.INSTANCE, "Abbrechen"));
this.add(new ActButton(20, 20, 200, 24, new ActButton.Callback() {
public void use(ActButton elem, ActButton.Mode action) {
if(GuiWorlds.this.gm.theWorld != null)
return;
GuiWorlds.this.gm.showDirDialog("Welt öffnen", "saves", new FileCallback() {
public void selected(File file) {
if(GuiWorlds.this.gm.theWorld == null && GuiWorlds.this.gm.open instanceof GuiWorlds)
GuiWorlds.this.gm.startServer(file, "sen");
}
});
}
}, "Welt laden"));
this.selectButton.enabled = false;
this.deleteButton.enabled = false;
this.pruneButton.enabled = false;
this.copyButton.enabled = false;
this.moveButton.enabled = false;
this.seedButton.enabled = false;
this.userButton.enabled = false;
this.dupeButton.enabled = false;
this.createButton.enabled = create;
}
public String getTitle() {
return "Welt auswählen";
}
public int getListWidth()
{
return 660;
}
public int getSlotHeight()
{
return 56;
}
public void updateScreen() {
super.updateScreen();
// this.userField.updateCursorCounter();
if(this.warningTimer > 0) {
if(--this.warningTimer == 0) {
this.warningMessage = null;
}
}
}
// /**
// * Handles mouse input.
// */
// public void handleMouseInput() throws IOException
// {
// super.handleMouseInput();
// this.availableWorlds.handleMouseInput();
// }
// private void loadLevelList() {
//
// }
private String getSaveAt()
{
return this.getSelected().getFile();
}
// protected String getNameAt(int index)
// {
// String s = ((SaveInfo)this.saveList.get(index)).getName();
//
// if (s == null || s.isEmpty())
// {
// s = I18n.format("selectWorld.world") + " " + (index + 1);
// }
//
// return s;
// }
// public void addWorldSelectionButtons(boolean create)
// {
//// this.userField = new TextField(this.width / 2 + 4 + 2, 26, 146, 16);
//// this.userField.setMaxStringLength(16);
//// this.userField.setText(this.gm.localUser);
//// this.userField.setValidator(new Predicate<String>()
//// {
//// public boolean apply(String name)
//// {
//// return StringValidator.isValidUser(name);
//// }
//// });
// }
public void use(ActButton button, Mode mode)
{
// if (button.enabled)
// {
if (button == this.deleteButton)
{
String s = this.getSaveAt();
if (s != null)
{
GuiConfirm guiyesno = makeYesNo(this, s, false);
this.gm.displayGuiScreen(guiyesno);
}
}
else if (button == this.pruneButton)
{
String s = this.getSaveAt();
if (s != null)
{
GuiConfirm guiyesno = makeYesNo(this, s, true);
this.gm.displayGuiScreen(guiyesno);
}
}
else if (button == this.selectButton)
{
// if(isShiftKeyDown())
//
// else
this.playWorld(null);
}
else if (button == this.createButton)
{
if(GuiWorlds.this.gm.theWorld == null) {
if(this.gm.shift()) {
// this.gm.displayGuiScreen(null);
this.gm.startServer(null, "debug");
}
else {
// this.gm.displayGuiScreen(new GuiCreate(this)); //TODO: create
}
}
}
else if (button == this.userButton)
{
this.gm.displayGuiScreen(new GuiEdit(this.getSelected().getUsername(), true,
"Spieler für '" + this.getSaveAt() + "' ändern", "Ändern", "Spielername", new Callback() {
public void confirm(String name) {
if(name != null)
GuiWorlds.this.changeUser(name);
GuiWorlds.this.gm.displayGuiScreen(GuiWorlds.this);
}
}));
}
// else if (button.id == 0)
// {
// this.gm.displayGuiScreen(this.parentScreen);
// }
else if (button == this.dupeButton)
{
}
else if (button == this.copyButton)
{
this.gm.displayGuiScreen(new GuiEdit("", false,
"Welt '" + this.getSaveAt() + "' kopieren", "Kopieren", "Ordner der Welt", new Callback() {
public void confirm(String name) {
if(name != null)
GuiWorlds.this.copyWorld(name);
GuiWorlds.this.gm.displayGuiScreen(GuiWorlds.this);
}
}));
}
else if (button == this.moveButton)
{
this.gm.displayGuiScreen(new GuiEdit(this.getSaveAt(), false,
"Welt '" + this.getSaveAt() + "' verschieben", "Verschieben", "Ordner der Welt", new Callback() {
public void confirm(String name) {
if(name != null)
GuiWorlds.this.moveWorld(name);
GuiWorlds.this.gm.displayGuiScreen(GuiWorlds.this);
}
}));
}
else if (button == this.seedButton)
{
WCF.setClipboard("" + this.getSelected().getSeed());
this.warningTimer = 40;
this.warningMessage = TextColor.DGREEN + "Startwert wurde in die Zwischenablage kopiert";
}
// else
// {
// this.availableWorlds.actionPerformed(button);
// }
// }
}
private void copyWorld(String name) {
File oldDir = new File(Region.SAVE_DIR, this.getSaveAt());
// String name = this.getSaveAt(this.selectedIndex);
// while(new File(Region.SAVE_DIR, name).exists()) {
// name = name + "-";
// }
File newDir = new File(Region.SAVE_DIR, name);
Log.IO.info("Kopiere Welt " + oldDir + " nach " + newDir);
if(!copyFiles(oldDir, newDir, oldDir.listFiles())) {
this.warningTimer = 120;
this.warningMessage = "Fehler beim Kopieren der Welt, diese könnte unvollständig sein!";
}
// try
// {
// this.loadLevelList();
// }
// catch (Exception anvilconverterexception)
// {
// Log.error((String)"Konnte Weltliste nicht laden", (Throwable)anvilconverterexception);
// }
this.gm.displayGuiScreen(this);
}
private void moveWorld(String dest) {
File oldDir = new File(Region.SAVE_DIR, this.getSaveAt());
File newDir = new File(Region.SAVE_DIR, dest);
Log.IO.info("Kopiere Welt " + oldDir + " nach " + newDir);
if(!copyFiles(oldDir, newDir, oldDir.listFiles())) {
this.warningTimer = 120;
this.warningMessage = "Fehler beim Kopieren der Welt, diese könnte unvollständig sein!";
return;
}
if(!this.deleteWorld(false)) {
this.warningTimer = 120;
this.warningMessage = "Fehler beim Löschen der Welt!";
}
this.gm.displayGuiScreen(this);
}
private void changeUser(String user) {
File file = new File(new File(Region.SAVE_DIR, this.getSaveAt()), "level.nbt");
if(file.exists()) {
try {
NBTTagCompound tag = NBTLoader.readGZip(file);
tag.setString("Owner", user);
NBTLoader.writeGZip(tag, file);
}
catch(Exception e) {
Log.IO.error("Fehler beim Verarbeiten von " + file, e);
}
}
// this.gm.displayGuiScreen(this);
}
private void playWorld(String user)
{
if(GuiWorlds.this.gm.theWorld != null)
return;
// this.gm.displayGuiScreen(null);
if (!this.starting)
{
this.starting = true;
// int index = this.selectedIndex;
if(user == null)
user = this.getSelected().getUsername();
String dir = this.getSaveAt();
File folder = new File(Region.SAVE_DIR, dir);
if(folder.isDirectory()) {
if(user == null) {
this.gm.displayGuiScreen(new GuiEdit("", true,
"Spieler für '" + dir + "' festlegen", "Festlegen", "Spielername", new Callback() {
public void confirm(String name) {
if(name != null)
GuiWorlds.this.playWorld(name);
else
GuiWorlds.this.gm.displayGuiScreen(GuiWorlds.this);
}
}));
this.starting = false;
}
else {
if(this.getSelected().mustConvert()) {
this.gm.convert(folder, user);
this.gm.displayGuiScreen(this);
this.starting = false;
}
else {
this.gm.startServer(folder, user);
}
}
}
}
}
// public void confirmClicked(boolean result)
// {
// if (this.confirmingDelete >= 0 || this.confirmingPrune >= 0)
// {
// boolean prune = this.confirmingPrune >= 0;
// int id = prune ? this.confirmingPrune : this.confirmingDelete;
// this.confirmingDelete = -1;
// this.confirmingPrune = -1;
//
// if (result)
// {
//// Region.clearCache(true);
// if(!this.deleteWorld(prune)) {
// this.warningTimer = 120;
// this.warningMessage = "Fehler beim " + (prune ? "Leeren" : "Löschen") + " der Welt!";
// }
//
//// try
//// {
//// this.loadLevelList();
//// }
//// catch (Exception anvilconverterexception)
//// {
//// Log.error((String)"Konnte Weltliste nicht laden", (Throwable)anvilconverterexception);
//// }
// }
//
// this.gm.displayGuiScreen(this);
// }
// }
private boolean deleteWorld(final boolean pruneOnly)
{
File worldDir = new File(Region.SAVE_DIR, this.getSaveAt());
if (!worldDir.exists())
{
return true;
}
Log.IO.info((pruneOnly ? "Leere" : "Lösche") + " Welt " + worldDir);
boolean flag = false;
for (int i = 1; i <= 5; ++i)
{
Log.IO.info("Versuch " + i + "...");
if (FileUtils.deleteFiles(worldDir.listFiles(new FileFilter() {
public boolean accept(File file) {
return !pruneOnly || (file.isDirectory() ? file.getName().equals("chunk") : file.getName().equals("signs.nbt"));
}
})))
{
flag = true;
break;
}
Log.IO.warn("Konnte Inhalt nicht löschen.");
if (i < 5)
{
try
{
Thread.sleep(500L);
}
catch (InterruptedException e)
{
;
}
}
}
return pruneOnly ? flag : worldDir.delete();
}
private static boolean copyFiles(File source, File dest, File[] files)
{
dest.mkdirs();
if(files == null || !dest.isDirectory()) {
Log.IO.warn("Konnte Ordner " + source + " nicht nach " + dest + " kopieren");
return false;
}
boolean flag = true;
for (int i = 0; i < files.length; ++i)
{
File file = files[i];
File nfile = new File(dest, file.getName());
Log.IO.info("Kopiere " + file + " nach " + nfile);
if (file.isDirectory())
{
flag &= copyFiles(file, nfile, file.listFiles());
continue;
}
try {
Files.copy(file.toPath(), nfile.toPath(), StandardCopyOption.REPLACE_EXISTING);
// Files.copy(file, nfile);
}
catch(IOException e) {
Log.IO.error("Konnte Datei " + file + " nicht nach " + nfile + " kopieren", e);
flag = false;
}
}
return flag;
}
public void drawOverlays()
{
super.drawOverlays();
if(this.warningMessage != null) {
drawRect(this.gm.fb_x / 2 - 191, this.gm.fb_y - 82, this.gm.fb_x / 2 + 191, this.gm.fb_y - 66, 0xff808080);
drawRect(this.gm.fb_x / 2 - 190, this.gm.fb_y - 81, this.gm.fb_x / 2 + 190, this.gm.fb_y - 67, 0xff000000);
Drawing.drawText(this.warningMessage, this.gm.fb_x / 2, this.gm.fb_y - 78, 0xffff0000);
}
}
public static GuiConfirm makeYesNo(GuiWorlds selectWorld, String name, boolean prune)
{
String s = prune ? "Willst du diese Welt wirklich leeren?" : "Bist du sicher, dass du diese Welt löschen möchtest?";
String s1 = "\'" + name + "\' " + (prune ? "wird alle Daten außer dem Spieler und Einstellungen verlieren!"
: "wird für immer verloren sein! (Eine lange Zeit!)");
String s2 = prune ? "Leeren" : "Löschen";
String s3 = "Abbrechen";
GuiConfirm guiyesno = new GuiConfirm(new GuiConfirm.Callback() {
@Override
public void confirm(boolean confirmed) {
if(confirmed && !selectWorld.deleteWorld(prune)) {
selectWorld.warningTimer = 120;
selectWorld.warningMessage = "Fehler beim " + (prune ? "Leeren" : "Löschen") + " der Welt!";
}
selectWorld.gm.displayGuiScreen(selectWorld);
}
}, s, s1, s2, s3);
return guiyesno;
}
}