Skip to content

Commit

Permalink
added gamemode selection to compile args
Browse files Browse the repository at this point in the history
  • Loading branch information
Igrium committed Jun 30, 2021
1 parent a76f9e2 commit 82d1a8d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.apache.logging.log4j.LogManager;
import org.scaffoldeditor.scaffold.compile.Compiler.CompileProgressListener;
import org.scaffoldeditor.scaffold.level.Level;
import org.scaffoldeditor.scaffold.level.LevelData.GameType;
import org.scaffoldeditor.scaffold.level.entity.attribute.Attribute;
import org.scaffoldeditor.scaffold.level.entity.attribute.BooleanAttribute;

Expand All @@ -30,13 +31,19 @@ public boolean execute(Level level, Path target, Map<String, Attribute<?>> args,
if (args.get("cheats") instanceof BooleanAttribute) {
cheats = ((BooleanAttribute) args.get("cheats")).getValue();
}


GameType gameType = GameType.ADVENTURE;
Object gameTypeAtt = args.get("gameType").getValue();
if (gameTypeAtt instanceof GameType) {
gameType = (GameType) gameTypeAtt;
}

// Make world folder
target.toFile().mkdir();

// Compile levelData
try {
level.levelData().compileFile(target.resolve("level.dat").toFile(), cheats);
level.levelData().compileFile(target.resolve("level.dat").toFile(), cheats, gameType);
} catch (IOException e) {
e.printStackTrace();
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ public class LevelData {
private CompoundTag data = new CompoundTag();
private CompoundTag gamerules = new CompoundTag();

public enum GameType {
SURVIVAL,
CREATIVE,
ADVENTURE,
SPECTATOR
}

public LevelData(Level level) {
this.level = level;

Expand Down Expand Up @@ -105,15 +112,16 @@ public String getGamerule(String name) {
/**
* Compile this LevelData into a compound tag.
* @param cheats Should the compiled world have cheats on?
* @param gameType Initial gamemode for the player.
* @return Compiled NBT
*/
public CompoundTag compile(boolean cheats) throws IOException {
public CompoundTag compile(boolean cheats, GameType gameType) throws IOException {
AssetManager assetManager = level.getProject().assetManager();

NamedTag named = new NBTDeserializer(true).fromStream(assetManager.getAssetAsStream("defaults/default_level.dat"));
CompoundTag tag = (CompoundTag) named.getTag();
CompoundTag data = tag.getCompoundTag("Data");
NBTMerger.mergeCompound(data, data, true, ListMergeMode.REPLACE);
NBTMerger.mergeCompound(data, this.data, true, ListMergeMode.REPLACE);

// Identify player start
Entity start = null;
Expand All @@ -133,6 +141,7 @@ public CompoundTag compile(boolean cheats) throws IOException {

data.putBoolean("allowCommands", cheats);
data.putString("LevelName", level.getPrettyName());
data.putInt("GameType", gameType.ordinal());

return tag;
}
Expand All @@ -143,10 +152,11 @@ public CompoundTag compile(boolean cheats) throws IOException {
* Compile level data to nbt file.
* @param file File to save to.
* @param cheats Should cheats be enabled?
* @param gameType Initial gamemode for the player.
* @throws IOException if an I/O error occurs.
*/
public void compileFile(File file, boolean cheats) throws IOException {
CompoundTag compiled = compile(cheats);
public void compileFile(File file, boolean cheats, GameType gameType) throws IOException {
CompoundTag compiled = compile(cheats, gameType);
NBTUtil.write(new NamedTag("", compiled), file);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.HashMap;
import java.util.Map;

import org.scaffoldeditor.scaffold.level.LevelData;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

Expand Down Expand Up @@ -61,6 +62,7 @@ public EnumAttribute<?> deserialize(Element element) {

enumRegistry.put("placeholder", DefaultEnums.PlaceholderEnum.class);
enumRegistry.put("direction", DefaultEnums.Direction.class);
enumRegistry.put("game_type", LevelData.GameType.class);
}

private T value;
Expand Down

0 comments on commit 82d1a8d

Please sign in to comment.