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 return x, y, z 115 } 116 117 // Spawn sets the spawn point to the given coordinates. 118 func (l *Level) Spawn(x, y, z int32) { 119 l.levelData.Set(nbt.NewTag("SpawnX", nbt.Int(x))) 120 l.levelData.Set(nbt.NewTag("SpawnY", nbt.Int(y))) 121 l.levelData.Set(nbt.NewTag("SpawnZ", nbt.Int(z))) 122 l.changed = true 123 } 124 125 //BorderCenter sets the position of the center of the World Border 126 func (l *Level) BorderCenter(x, z float64) { 127 l.levelData.Set(nbt.NewTag("BorderCenterX", nbt.Double(x))) 128 l.levelData.Set(nbt.NewTag("BorderCenterZ", nbt.Double(z))) 129 l.changed = true 130 } 131 132 //BorderSize sets the width of the border 133 func (l *Level) BorderSize(w float64) { 134 l.setOption("BorderSize", nbt.Double(w)) 135 } 136 137 // Raining sets the rain on or off 138 func (l *Level) Raining(raining bool) { 139 l.setOption("raining", boolToByte(raining)) 140 } 141 142 // RainTime sets the time until the rain state changes 143 func (l *Level) RainTime(time int32) { 144 l.setOption("rainTime", nbt.Int(time)) 145 } 146 147 // Thundering sets the lightning/thunder on or off 148 func (l *Level) Thundering(thundering bool) { 149 l.setOption("thundering", boolToByte(thundering)) 150 } 151 152 // ThunderTime sets the tune until the thunder state changes 153 func (l *Level) ThunderTime(time int32) { 154 l.setOption("thunderTime", nbt.Int(time)) 155 } 156 157 // CommandBlockOutput enables/disables chat echo for command blocks 158 func (l *Level) CommandBlockOutput(d bool) { 159 l.setGameRule("commandBlockOutput", d) 160 } 161 162 // DayLightCycle enables/disables the day/night cycle 163 func (l *Level) DayLightCycle(d bool) { 164 l.setGameRule("doDaylightCycle", d) 165 } 166 167 // FireTick enables/disables fire updates, such as spreading and extinguishing 168 func (l *Level) FireTick(d bool) { 169 l.setGameRule("doFireTick", d) 170 } 171 172 // MobLoot enables/disable mob loot drops 173 func (l *Level) MobLoot(d bool) { 174 l.setGameRule("doMobLoot", d) 175 } 176 177 // MobSpawning enables/disables mob spawning 178 func (l *Level) MobSpawning(d bool) { 179 l.setGameRule("doMobSpawning", d) 180 } 181 182 // TileDrops enables/disables the dropping of items upon block breakage 183 func (l *Level) TileDrops(d bool) { 184 l.setGameRule("doTileDrops", d) 185 } 186 187 // KeepInventory enables/disables the keeping of a players inventory upon death 188 func (l *Level) KeepInventory(d bool) { 189 l.setGameRule("keepInventory", d) 190 } 191 192 // LogAdminCommands enables/disables the logging of admin commmands to the log 193 func (l *Level) LogAdminCommands(d bool) { 194 l.setGameRule("logAdminCommands", d) 195 } 196 197 // MobGriefing enables/disables the ability of mobs to destroy blocks 198 func (l *Level) MobGriefing(d bool) { 199 l.setGameRule("mobGriefing", d) 200 } 201 202 // HealthRegeneration enables/disables the regeneration of the players health 203 // when their hunger is high enough 204 func (l *Level) HealthRegeneration(d bool) { 205 l.setGameRule("naturalRegeneration", d) 206 } 207 208 // CommandFeedback enables/disables the echo for player commands in the chat 209 func (l *Level) CommandFeedback(d bool) { 210 l.setGameRule("sendCommandFeedback", d) 211 } 212 213 // DeathMessages enables/disables the logging of player deaths to the chat 214 func (l *Level) DeathMessages(d bool) { 215 l.setGameRule("showDeathMessages", d) 216 } 217 218 func (l *Level) setOption(name string, data nbt.Data) { 219 l.levelData.Set(nbt.NewTag(name, data)) 220 l.changed = true 221 } 222 223 func (l *Level) setGameRule(name string, data bool) { 224 var grc nbt.Compound 225 gr := l.levelData.Get("GameRules") 226 if gr.TagID() != 0 { 227 grc = gr.Data().(nbt.Compound) 228 } 229 if grc.Type() == 0 { 230 l.levelData.Set(nbt.NewTag("GameRules", grc)) 231 } 232 var d nbt.String 233 if data { 234 d = nbt.String("True") 235 } else { 236 d = nbt.String("False") 237 } 238 grc.Set(nbt.NewTag(name, d)) 239 l.changed = true 240 } 241 242 func boolToByte(b bool) nbt.Byte { 243 if b { 244 return nbt.Byte(1) 245 } 246 return nbt.Byte(0) 247 } 248