func (r RPC) createMap(data data.DefaultMap, generatorSettings string) error { if data.Seed == 0 { data.Seed = rand.Int63() } m := r.c.NewMap() if m == nil { return errors.New("failed to create map") } m.Lock() defer m.Unlock() p, err := minecraft.NewFilePath(m.Path) if err != nil { r.c.RemoveMap(m.ID) return err } l, err := minecraft.NewLevel(p) if err != nil { r.c.RemoveMap(m.ID) return err } l.GameMode(data.GameMode) l.LevelName(data.Name) m.Name = data.Name switch data.Mode { case 0: l.Generator(minecraft.DefaultGenerator) case 1: l.Generator(minecraft.FlatGenerator) case 2: l.Generator(minecraft.LargeBiomeGenerator) case 3: l.Generator(minecraft.AmplifiedGenerator) case 4: l.Generator(minecraft.CustomGenerator) } l.Seed(data.Seed) l.AllowCommands(data.Cheats) l.MapFeatures(data.Structures) if generatorSettings != "" { l.GeneratorOptions(generatorSettings) } l.Save() f, err := os.Create(path.Join(m.Path, "properties.map")) if err != nil { r.c.RemoveMap(m.ID) return err } defer f.Close() ms := DefaultMapSettings() ms["gamemode"] = strconv.Itoa(int(data.GameMode)) if !data.Structures { ms["generate-structures"] = "false" } if data.GameMode == 3 { ms["hardcore"] = "true" } if generatorSettings != "" { ms["generator-settings"] = generatorSettings } ms["level-seed"] = strconv.FormatInt(data.Seed, 10) ms["motd"] = data.Name switch data.Mode { case 0: ms["level-type"] = minecraft.DefaultGenerator case 1: ms["level-type"] = minecraft.FlatGenerator case 2: ms["level-type"] = minecraft.LargeBiomeGenerator case 3: ms["level-type"] = minecraft.AmplifiedGenerator case 4: ms["level-type"] = minecraft.CustomGenerator case 5: ms["level-type"] = minecraft.DebugGenerator } if err := ms.WriteTo(f); err != nil { return err } go r.c.Save() return nil }
func createMap(o *overlay.Overlay) func(dom.Element) { return func(c dom.Element) { name := xform.InputText("name", "") name.Required = true gmOpts := make([]xform.Option, len(gameModes)) for i, m := range gameModes { gmOpts[i] = xform.Option{ Label: m, Value: strconv.Itoa(i), } } gameMode := xform.SelectBox("gamemode", gmOpts...) seed := xform.InputText("seed", "") structures := xform.InputCheckbox("structures", true) cheats := xform.InputCheckbox("cheats", false) fs := xdom.Fieldset() fs.AppendChild(xjs.SetInnerText(xdom.Legend(), "Create Map")) c.AppendChild(xjs.AppendChildren(xdom.Form(), fs)) dataParser := func(mode int) func() (data.DefaultMap, error) { return func() (data.DefaultMap, error) { data := data.DefaultMap{ Mode: mode, } var err error data.Name = name.Value si := gameMode.SelectedIndex if si < 0 || si >= len(gameModes) { return data, errors.New("invalid gamemode") } if seed.Value == "" { seed.Value = "0" } data.Seed, err = strconv.ParseInt(seed.Value, 10, 64) if err != nil { return data, err } data.Structures = structures.Checked data.Cheats = cheats.Checked return data, nil } } xjs.AppendChildren(fs, xform.Label("Name", "name"), name, xdom.Br(), xform.Label("Game Mode", "gamemode"), gameMode, xdom.Br(), xform.Label("Level Seed", "seed"), seed, xdom.Br(), xform.Label("Structures", "structures"), structures, xdom.Br(), xform.Label("Cheats", "cheats"), cheats, xdom.Br(), tabs.New([]tabs.Tab{ {"Default", createMapMode(0, o, dataParser(0))}, {"Super Flat", createSuperFlatMap(o, dataParser(1))}, {"Large Biomes", createMapMode(2, o, dataParser(2))}, {"Amplified", createMapMode(3, o, dataParser(3))}, {"Customised", createCustomisedMap(o, dataParser(4))}, }), ) } }