1 package minecraft 2 3 import "vimagination.zapto.org/minecraft/nbt" 4 5 // Option is a function used to set an option for a minecraft level struct. 6 type Option func(*Level) 7 8 // GetLevelName sets the given string to the name of the minecraft level. 9 func (l *Level) GetLevelName() string { 10 return string(l.levelData.Get("LevelName").Data().(nbt.String)) 11 } 12 13 // LevelName sets the name of the minecraft level. 14 func (l *Level) LevelName(name string) { 15 l.setOption("LevelName", nbt.String(name)) 16 } 17 18 // Default minecraft generators 19 const ( 20 DefaultGenerator = "default" 21 FlatGenerator = "flat" 22 LargeBiomeGenerator = "largeBiomes" 23 AmplifiedGenerator = "amplified" 24 CustomGenerator = "customized" 25 DebugGenerator = "debug_all_block_states" 26 ) 27 28 // Generator sets the generator type. 29 func (l *Level) Generator(generator string) { 30 l.setOption("generatorName", nbt.String(generator)) 31 } 32 33 // GeneratorOptions sets the generator options for a flat or custom generator. The syntax is not checked. 34 func (l *Level) GeneratorOptions(options string) { 35 l.setOption("generatorOptions", nbt.String(options)) 36 } 37 38 // Seed sets the random seed for the level. 39 func (l *Level) Seed(seed int64) { 40 l.setOption("RandomSeed", nbt.Long(seed)) 41 } 42 43 // MapFeatures enables/disables map feature generation (villages, strongholds, mineshafts, etc.). 44 func (l *Level) MapFeatures(mf bool) { 45 l.setOption("MapFeatures", boolToByte(mf)) 46 } 47 48 // AllowCommands enables/disables the cheat commands. 49 func (l *Level) AllowCommands(a bool) { 50 l.setOption("allowCommands", boolToByte(a)) 51 } 52 53 // Hardcore enables/disables hardcore mode. 54 func (l *Level) Hardcore(h bool) { 55 l.setOption("hardcore", boolToByte(h)) 56 } 57 58 // Game Modes Settings. 59 const ( 60 Survival int32 = iota 61 Creative 62 Adventure 63 Spectator 64 ) 65 66 // GameMode sets the game mode type. 67 func (l *Level) GameMode(gm int32) { 68 l.setOption("GameType", nbt.Int(gm)) 69 } 70 71 // Difficulty Settings. 72 const ( 73 Peaceful int8 = iota 74 Easy 75 Normal 76 Hard 77 ) 78 79 // Difficulty sets the level difficulty. 80 func (l *Level) Difficulty(d int8) { 81 l.setOption("Difficulty", nbt.Byte(d)) 82 } 83 84 // DifficultyLocked locks the difficulty in game. 85 func (l *Level) DifficultyLocked(dl bool) { 86 l.setOption("DifficultyLocked", boolToByte(dl)) 87 } 88 89 // TicksExisted sets how many ticks have passed in game. 90 func (l *Level) TicksExisted(t int64) { 91 l.setOption("Time", nbt.Long(t)) 92 } 93 94 // Time-of-day convenience constants. 95 const ( 96 SunRise = 0 97 Noon = 6000 98 SunSet = 12000 99 MidNight = 18000 100 Day = 24000 101 ) 102 103 // Time sets the in world time. 104 func (l *Level) Time(t int64) { 105 l.setOption("DayTime", nbt.Long(t)) 106 } 107 108 // GetSpawn sets the given x, y, z coordinates to the current spawn point. 109 func (l *Level) GetSpawn() (x int32, y int32, z int32) { 110 xTag, yTag, zTag := l.levelData.Get("SpawnX"), l.levelData.Get("SpawnY"), l.levelData.Get("SpawnZ") 111 x = int32(xTag.Data().(nbt.Int)) 112 y = int32(yTag.Data().(nbt.Int)) 113 z = int32(zTag.Data().(nbt.Int)) 114 115 return x, y, z 116 } 117 118 // Spawn sets the spawn point to the given coordinates.. 119 func (l *Level) Spawn(x, y, z int32) { 120 l.levelData.Set(nbt.NewTag("SpawnX", nbt.Int(x))) 121 l.levelData.Set(nbt.NewTag("SpawnY", nbt.Int(y))) 122 l.levelData.Set(nbt.NewTag("SpawnZ", nbt.Int(z))) 123 124 l.changed = true 125 } 126 127 // BorderCenter sets the position of the center of the World Border. 128 func (l *Level) BorderCenter(x, z float64) { 129 l.levelData.Set(nbt.NewTag("BorderCenterX", nbt.Double(x))) 130 l.levelData.Set(nbt.NewTag("BorderCenterZ", nbt.Double(z))) 131 132 l.changed = true 133 } 134 135 // BorderSize sets the width of the border. 136 func (l *Level) BorderSize(w float64) { 137 l.setOption("BorderSize", nbt.Double(w)) 138 } 139 140 // Raining sets the rain on or off. 141 func (l *Level) Raining(raining bool) { 142 l.setOption("raining", boolToByte(raining)) 143 } 144 145 // RainTime sets the time until the rain state changes. 146 func (l *Level) RainTime(time int32) { 147 l.setOption("rainTime", nbt.Int(time)) 148 } 149 150 // Thundering sets the lightning/thunder on or off. 151 func (l *Level) Thundering(thundering bool) { 152 l.setOption("thundering", boolToByte(thundering)) 153 } 154 155 // ThunderTime sets the tune until the thunder state changes. 156 func (l *Level) ThunderTime(time int32) { 157 l.setOption("thunderTime", nbt.Int(time)) 158 } 159 160 // CommandBlockOutput enables/disables chat echo for command blocks. 161 func (l *Level) CommandBlockOutput(d bool) { 162 l.setGameRule("commandBlockOutput", d) 163 } 164 165 // DayLightCycle enables/disables the day/night cycle. 166 func (l *Level) DayLightCycle(d bool) { 167 l.setGameRule("doDaylightCycle", d) 168 } 169 170 // FireTick enables/disables fire updates, such as spreading and extinguishing. 171 func (l *Level) FireTick(d bool) { 172 l.setGameRule("doFireTick", d) 173 } 174 175 // MobLoot enables/disable mob loot drops. 176 func (l *Level) MobLoot(d bool) { 177 l.setGameRule("doMobLoot", d) 178 } 179 180 // MobSpawning enables/disables mob spawning. 181 func (l *Level) MobSpawning(d bool) { 182 l.setGameRule("doMobSpawning", d) 183 } 184 185 // TileDrops enables/disables the dropping of items upon block breakage. 186 func (l *Level) TileDrops(d bool) { 187 l.setGameRule("doTileDrops", d) 188 } 189 190 // KeepInventory enables/disables the keeping of a players inventory upon death. 191 func (l *Level) KeepInventory(d bool) { 192 l.setGameRule("keepInventory", d) 193 } 194 195 // LogAdminCommands enables/disables the logging of admin commands to the log. 196 func (l *Level) LogAdminCommands(d bool) { 197 l.setGameRule("logAdminCommands", d) 198 } 199 200 // MobGriefing enables/disables the ability of mobs to destroy blocks. 201 func (l *Level) MobGriefing(d bool) { 202 l.setGameRule("mobGriefing", d) 203 } 204 205 // HealthRegeneration enables/disables the regeneration of the players health 206 // when their hunger is high enough. 207 func (l *Level) HealthRegeneration(d bool) { 208 l.setGameRule("naturalRegeneration", d) 209 } 210 211 // CommandFeedback enables/disables the echo for player commands in the chat. 212 func (l *Level) CommandFeedback(d bool) { 213 l.setGameRule("sendCommandFeedback", d) 214 } 215 216 // DeathMessages enables/disables the logging of player deaths to the chat. 217 func (l *Level) DeathMessages(d bool) { 218 l.setGameRule("showDeathMessages", d) 219 } 220 221 func (l *Level) setOption(name string, data nbt.Data) { 222 l.levelData.Set(nbt.NewTag(name, data)) 223 224 l.changed = true 225 } 226 227 func (l *Level) setGameRule(name string, data bool) { 228 var grc nbt.Compound 229 230 gr := l.levelData.Get("GameRules") 231 232 if gr.TagID() != 0 { 233 grc = gr.Data().(nbt.Compound) 234 } 235 236 if grc.Type() == 0 { 237 l.levelData.Set(nbt.NewTag("GameRules", grc)) 238 } 239 240 var d nbt.String 241 242 if data { 243 d = nbt.String("True") 244 } else { 245 d = nbt.String("False") 246 } 247 248 grc.Set(nbt.NewTag(name, d)) 249 250 l.changed = true 251 } 252 253 func boolToByte(b bool) nbt.Byte { 254 if b { 255 return nbt.Byte(1) 256 } 257 258 return nbt.Byte(0) 259 } 260