Skip to content

Commit 79d1c28

Browse files
committed
Save and restore Spawn Positions for schemas
1 parent 167014b commit 79d1c28

File tree

11 files changed

+81
-28
lines changed

11 files changed

+81
-28
lines changed

src/main/java/org/dave/compactmachines3/block/BlockMachine.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -332,10 +332,7 @@ public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, En
332332
0.5 + machine.getSize().getDimension() / 2
333333
};
334334

335-
double x = machine.coords * 1024 + 0.5 + machine.getSize().getDimension() / 2;
336-
double y = 42;
337-
double z = 0.5 + machine.getSize().getDimension() / 2;
338-
WorldSavedDataMachines.INSTANCE.addSpawnPoint(machine.coords, x, y, z);
335+
WorldSavedDataMachines.INSTANCE.addSpawnPoint(machine.coords, destination);
339336
}
340337

341338
WorldSavedDataMachines.INSTANCE.addMachinePosition(machine.coords, pos, world.provider.getDimension(), machine.getSize());

src/main/java/org/dave/compactmachines3/command/CommandSchemaLoad.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public void execute(MinecraftServer server, ICommandSender sender, String[] args
4545
Schema schema = SchemaRegistry.instance.getSchema(args[0]);
4646
int coords = StructureTools.getCoordsForPos(sender.getCommandSenderEntity().getPosition());
4747
EnumMachineSize machineSize = WorldSavedDataMachines.INSTANCE.machineSizes.get(coords);
48-
if(machineSize != schema.size) {
48+
if(machineSize != schema.getSize()) {
4949
throw this.getException(sender, "machine_size_does_not_match");
5050
}
5151

src/main/java/org/dave/compactmachines3/command/CommandSchemaSave.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import net.minecraft.entity.EntityLivingBase;
66
import net.minecraft.entity.player.EntityPlayer;
77
import net.minecraft.server.MinecraftServer;
8+
import net.minecraft.util.math.Vec3d;
89
import net.minecraft.util.text.TextComponentTranslation;
910
import org.dave.compactmachines3.misc.ConfigurationHandler;
1011
import org.dave.compactmachines3.schema.BlockInformation;
@@ -49,8 +50,12 @@ public void execute(MinecraftServer server, ICommandSender sender, String[] args
4950
List<BlockInformation> blockList = StructureTools.createNewSchema(coords);
5051

5152
if(blockList != null) {
52-
// TODO: Save and process the spawn point, including look direction this time!
53-
Schema schema = new Schema(args[0], blockList, WorldSavedDataMachines.INSTANCE.machineSizes.get(coords));
53+
Schema schema = new Schema(args[0]);
54+
schema.setBlocks(blockList);
55+
schema.setSize(WorldSavedDataMachines.INSTANCE.machineSizes.get(coords));
56+
57+
Vec3d pos = sender.getPositionVector();
58+
schema.setSpawnPosition(new double[] {pos.x % 1024, pos.y - 40, pos.z});
5459

5560
try {
5661
File schemaFile = new File(ConfigurationHandler.schemaDirectory, sane);

src/main/java/org/dave/compactmachines3/command/CommandSchemaSet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public void execute(MinecraftServer server, ICommandSender sender, String[] args
5858
}
5959

6060
Schema schema = SchemaRegistry.instance.getSchema(schemaName);
61-
if(machine.getSize() != schema.size) {
61+
if(machine.getSize() != schema.getSize()) {
6262
throw this.getException(sender, "machine_size_does_not_match");
6363
}
6464

src/main/java/org/dave/compactmachines3/schema/Schema.java

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,40 @@
55
import java.util.List;
66

77
public class Schema {
8-
public String name;
9-
public List<BlockInformation> blocks;
10-
public EnumMachineSize size;
8+
private String name;
9+
private List<BlockInformation> blocks;
10+
private EnumMachineSize size;
11+
private double[] spawnPosition;
1112

12-
public Schema(String name, List<BlockInformation> blocks, EnumMachineSize size) {
13+
public Schema(String name) {
1314
this.name = name;
15+
}
16+
17+
public String getName() {
18+
return name;
19+
}
20+
21+
public List<BlockInformation> getBlocks() {
22+
return blocks;
23+
}
24+
25+
public void setBlocks(List<BlockInformation> blocks) {
1426
this.blocks = blocks;
27+
}
28+
29+
public EnumMachineSize getSize() {
30+
return size;
31+
}
32+
33+
public void setSize(EnumMachineSize size) {
1534
this.size = size;
1635
}
36+
37+
public void setSpawnPosition(double[] spawnPosition) {
38+
this.spawnPosition = spawnPosition;
39+
}
40+
41+
public double[] getSpawnPosition() {
42+
return spawnPosition;
43+
}
1744
}

src/main/java/org/dave/compactmachines3/schema/SchemaRegistry.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ private void loadSchemas() {
4848
continue;
4949
}
5050

51-
Logz.info("Loaded schema: %s [size=%s, blocks=%d]", schema.name, schema.size, schema.blocks.size());
51+
Logz.info("Loaded schema: %s [size=%s, blocks=%d]", schema.getName(), schema.getSize(), schema.getBlocks().size());
5252
addSchema(schema);
5353
}
5454
}
5555

5656
public void addSchema(Schema schema) {
57-
schemas.put(schema.name, schema);
57+
schemas.put(schema.getName(), schema);
5858
}
5959

6060
public boolean hasSchema(String name) {

src/main/java/org/dave/compactmachines3/schema/SchemaSerializer.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,34 @@ public Schema deserialize(JsonElement json, Type typeOfT, JsonDeserializationCon
1818
JsonObject jsonRoot = json.getAsJsonObject();
1919
Type type = new TypeToken<List<BlockInformation>>() {}.getType();
2020
EnumMachineSize size = EnumMachineSize.getFromMeta(jsonRoot.get("size").getAsInt());
21-
return new Schema(jsonRoot.get("name").getAsString(), context.deserialize(jsonRoot.get("blocks").getAsJsonArray(), type), size);
21+
Schema result = new Schema(jsonRoot.get("name").getAsString());
22+
result.setBlocks(context.deserialize(jsonRoot.get("blocks").getAsJsonArray(), type));
23+
result.setSize(size);
24+
25+
JsonArray spawnPos = jsonRoot.getAsJsonArray("spawn");
26+
double[] spawnPosition = new double[] {
27+
spawnPos.get(0).getAsDouble(),
28+
spawnPos.get(1).getAsDouble(),
29+
spawnPos.get(2).getAsDouble()
30+
};
31+
result.setSpawnPosition(spawnPosition);
32+
33+
return result;
2234
}
2335

2436
@Override
2537
public JsonElement serialize(Schema src, Type typeOfSrc, JsonSerializationContext context) {
2638
JsonObject root = new JsonObject();
27-
root.addProperty("name", src.name);
28-
root.addProperty("size", src.size.getMeta());
29-
root.add("blocks", context.serialize(src.blocks));
39+
root.addProperty("name", src.getName());
40+
root.addProperty("size", src.getSize().getMeta());
41+
root.add("blocks", context.serialize(src.getBlocks()));
42+
43+
JsonArray spawnArray = new JsonArray();
44+
spawnArray.add(src.getSpawnPosition()[0]);
45+
spawnArray.add(src.getSpawnPosition()[1]);
46+
spawnArray.add(src.getSpawnPosition()[2]);
47+
48+
root.add("spawn", spawnArray);
3049
return root;
3150
}
3251
}

src/main/java/org/dave/compactmachines3/world/WorldSavedDataMachines.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public void addMachinePosition(int coord, BlockPos pos, int dimension, EnumMachi
7373
this.markDirty();
7474
}
7575

76-
private void addSpawnPoint(int coord, double[] destination) {
76+
public void addSpawnPoint(int coord, double[] destination) {
7777
if(destination.length != 3) {
7878
Logz.warn("Trying to set spawn point with invalid double[]=%s", destination);
7979
return;

src/main/java/org/dave/compactmachines3/world/tools/StructureTools.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public static List<BlockInformation> createNewSchema(TileEntityMachine machine)
187187
}
188188

189189
public static void restoreSchema(Schema schema, int coords) {
190-
List<BlockInformation> blockList = schema.blocks;
190+
List<BlockInformation> blockList = schema.getBlocks();
191191

192192
TileEntity te = WorldSavedDataMachines.INSTANCE.getMachinePosition(coords).getTileEntity();
193193

src/main/java/org/dave/compactmachines3/world/tools/TeleportationTools.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,25 @@ public static void teleportPlayerToMachine(EntityPlayerMP player, int coords, bo
6565
playerNBT.setTag("compactmachines3-coordHistory", coordHistory);
6666
}
6767

68-
double[] destination = WorldSavedDataMachines.INSTANCE.spawnPoints.get(coords);
69-
player.setPositionAndUpdate(destination[0], destination[1], destination[2]);
70-
7168
TileEntityMachine machine = WorldSavedDataMachines.INSTANCE.getMachine(coords);
7269
if(machine.hasNewSchema()) {
7370
Schema schema = SchemaRegistry.instance.getSchema(machine.getSchemaName());
7471
if(schema == null) {
7572
Logz.warn("Unknown schema used by Compact Machine @ %s", WorldSavedDataMachines.INSTANCE.getMachinePosition(coords));
76-
return;
73+
} else {
74+
StructureTools.restoreSchema(schema, coords);
75+
double[] adjustedSpawnPosition = schema.getSpawnPosition();
76+
adjustedSpawnPosition[0] += coords * 1024;
77+
adjustedSpawnPosition[1] += 40;
78+
WorldSavedDataMachines.INSTANCE.addSpawnPoint(machine.coords, adjustedSpawnPosition);
79+
machine.setSchema(null);
80+
machine.markDirty();
7781
}
78-
79-
StructureTools.restoreSchema(schema, coords);
80-
machine.setSchema(null);
81-
machine.markDirty();
8282
}
83+
84+
double[] destination = WorldSavedDataMachines.INSTANCE.spawnPoints.get(coords);
85+
player.setPositionAndUpdate(destination[0], destination[1], destination[2]);
86+
8387
}
8488

8589
public static void teleportPlayerOutOfMachineDimension(EntityPlayerMP player) {

0 commit comments

Comments
 (0)