Skip to content

Commit

Permalink
trying to fix the placement bug xD
Browse files Browse the repository at this point in the history
  • Loading branch information
sakura-ryoko committed Oct 25, 2024
1 parent 64c8e91 commit b7b7b5e
Show file tree
Hide file tree
Showing 18 changed files with 248 additions and 79 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod_name = Syncmatica
mod_file_name = syncmatica-fabric

# Mod Version
mod_version = 0.3.12-sakura.8
mod_version = 0.3.12-sakura.9

# Dependencies (malilib, litematica)
#malilib_fileid=4946328
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/ch/endte/syncmatica/Feature.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ public enum Feature {
MESSAGE, // ability to send messages to display from server to client
QUOTA, // quota on client uploads to the server
DEBUG, // ability to configure debugging
CORE_EX; // extended basic features - such as who owns a placement and subregion sharing
CORE_EX, // extended basic features - such as who owns a placement and subregion sharing
VERSION; // extended version metadata

public static Feature fromString(final String s) {
for (final Feature f : Feature.values()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void putMetaData(final ServerPlacement metaData, final PacketByteBuf buf,
{
buf.writeUuid(metaData.getId());

buf.writeString(SyncmaticaUtil.sanitizeFileName(metaData.getName()));
buf.writeString(SyncmaticaUtil.sanitizeFileName(metaData.getFileName()));
buf.writeUuid(metaData.getHash());

if (exchangeTarget.getFeatureSet().hasFeature(Feature.CORE_EX))
Expand All @@ -98,6 +98,10 @@ public void putMetaData(final ServerPlacement metaData, final PacketByteBuf buf,
buf.writeUuid(metaData.getLastModifiedBy().uuid);
buf.writeString(metaData.getLastModifiedBy().getName());
}
if (exchangeTarget.getFeatureSet().hasFeature(Feature.VERSION)) {
buf.writeVarInt(metaData.getLitematicVersion());
buf.writeVarInt(metaData.getDataVersion());
}

putPositionData(metaData, buf, exchangeTarget);
}
Expand Down Expand Up @@ -158,7 +162,18 @@ public ServerPlacement receiveMetaData(final PacketByteBuf buf, final ExchangeTa
);
}

final ServerPlacement placement = new ServerPlacement(id, fileName, hash, owner);
ServerPlacement placement;
int litematicVersion;
int dataVersion;

if (exchangeTarget.getFeatureSet().hasFeature(Feature.VERSION)) {
litematicVersion = buf.readVarInt();
dataVersion = buf.readVarInt();
placement = new ServerPlacement(id, fileName, hash, owner, litematicVersion, dataVersion);
} else {
placement = new ServerPlacement(id, fileName, hash, owner);
}

placement.setLastModifiedBy(lastModifiedBy);

receivePositionData(placement, buf, exchangeTarget);
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/ch/endte/syncmatica/data/FileStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ private File getSchematicPath(final ServerPlacement placement) {
if (context.isServer()) {
return new File(litematicPath, placement.getHash().toString() + ".litematic");
}
return new File(litematicPath, placement.getName() + ".litematic");
if (placement.getFileName().contains(".litematic")) {
return new File(litematicPath, placement.getFileName());
} else {
return new File(litematicPath, placement.getFileName() + ".litematic");
}
}
}
64 changes: 58 additions & 6 deletions src/main/java/ch/endte/syncmatica/data/ServerPlacement.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,44 @@ public class ServerPlacement {

private SubRegionData subRegionData = new SubRegionData();

// Feature.VERSION
private int dataVersion;
private int litematicVersion;

private SyncmaticaMaterialList matList;

public ServerPlacement(final UUID id, final String fileName, final UUID hashValue, final PlayerIdentifier owner) {
public ServerPlacement(final UUID id, final String fileName, final UUID hashValue, final PlayerIdentifier owner, int litematicVersion, int dataVersion) {
this.id = id;
this.fileName = fileName;
this.hashValue = hashValue;
this.owner = owner;
lastModifiedBy = owner;
this.lastModifiedBy = owner;
this.litematicVersion = litematicVersion;
this.dataVersion = dataVersion;
}

public ServerPlacement(final UUID id, final File file, final PlayerIdentifier owner) {
this(id, removeExtension(file), generateHash(file), owner);
this(id, removeExtension(file), generateHash(file), owner, -1, -1);
}

public ServerPlacement(UUID id, String fileName, UUID hash, PlayerIdentifier owner) {
this(id, fileName, hash, owner, -1, -1);
}

public UUID getId() {
return id;
}

public String getName() {
return fileName;
if (this.getFileName().contains(".")) {
return removeExtension(this.getFileName());
}

return this.getFileName();
}

public String getFileName() {
return this.fileName;
}

public UUID getHash() {
Expand All @@ -78,6 +96,17 @@ public BlockMirror getMirror() {
return mirror;
}

// Feature.VERSION
public int getLitematicVersion() { return litematicVersion; }

public int getDataVersion() { return dataVersion; }

public ServerPlacement setVersion(final int litematicVersion, final int dataVersion) {
this.litematicVersion = litematicVersion;
this.dataVersion = dataVersion;
return this;
}

public ServerPlacement move(final String dimensionId, final BlockPos origin, final BlockRotation rotation, final BlockMirror mirror) {
move(new ServerPosition(origin, dimensionId), rotation, mirror);
return this;
Expand Down Expand Up @@ -121,13 +150,19 @@ public ServerPlacement setMaterialList(final SyncmaticaMaterialList matList) {
return this;
}

private static String removeExtension(final File file) {
public static String removeExtension(final File file) {
// source stackoverflow
final String fileName = file.getName();
final int pos = fileName.lastIndexOf(".");
return fileName.substring(0, pos);
}

public static String removeExtension(final String fileName) {
// source stackoverflow
final int pos = fileName.lastIndexOf(".");
return fileName.substring(0, pos);
}

private static UUID generateHash(final File file) {
UUID hash = null;
try {
Expand Down Expand Up @@ -157,6 +192,13 @@ public JsonObject toJson() {
if (subRegionData.isModified()) {
obj.add("subregionData", subRegionData.toJson());
}
// Feature.VERSION
if (litematicVersion > -1) {
obj.add("litematicVersion", new JsonPrimitive(litematicVersion));
}
if (dataVersion > -1) {
obj.add("dataVersion", new JsonPrimitive(dataVersion));
}

return obj;
}
Expand All @@ -171,13 +213,23 @@ public static ServerPlacement fromJson(final JsonObject obj, final Context conte
final UUID id = UUID.fromString(obj.get("id").getAsString());
final String name = obj.get("file_name").getAsString();
final UUID hashValue = UUID.fromString(obj.get("hash").getAsString());
int version = -1;
int dataVersion = -1;

PlayerIdentifier owner = PlayerIdentifier.MISSING_PLAYER;
if (obj.has("owner")) {
owner = context.getPlayerIdentifierProvider().fromJson(obj.get("owner").getAsJsonObject());
}

final ServerPlacement newPlacement = new ServerPlacement(id, name, hashValue, owner);
// Feature.VERSION
if (obj.has("litematicVersion")) {
version = obj.get("litematicVersion").getAsInt();
}
if (obj.has("dataVersion")) {
dataVersion = obj.get("dataVersion").getAsInt();
}

final ServerPlacement newPlacement = new ServerPlacement(id, name, hashValue, owner, version, dataVersion);

final ServerPosition pos = ServerPosition.fromJson(obj.get("origin").getAsJsonObject());
if (pos == null) {
Expand Down
66 changes: 61 additions & 5 deletions src/main/java/ch/endte/syncmatica/litematica/LitematicManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import fi.dy.masa.litematica.data.DataManager;
import fi.dy.masa.litematica.data.SchematicHolder;
import fi.dy.masa.litematica.schematic.LitematicaSchematic;
import fi.dy.masa.litematica.schematic.SchematicMetadata;
import fi.dy.masa.litematica.schematic.SchematicSchema;
import fi.dy.masa.litematica.schematic.placement.SchematicPlacement;
import fi.dy.masa.litematica.schematic.placement.SchematicPlacementManager;
import fi.dy.masa.litematica.schematic.placement.SubRegionPlacement;
Expand All @@ -22,7 +24,9 @@
import net.minecraft.util.BlockMirror;
import net.minecraft.util.BlockRotation;
import net.minecraft.util.math.BlockPos;
import org.apache.commons.lang3.tuple.Pair;

import javax.annotation.Nullable;
import java.io.File;
import java.util.*;

Expand Down Expand Up @@ -91,8 +95,11 @@ public void renderSyncmatic(final ServerPlacement placement) {

final BlockPos origin = placement.getPosition();

final SchematicPlacement litematicaPlacement = SchematicPlacement.createFor(schematic, origin, file.getName(), true, true);
rendering.put(placement, litematicaPlacement);
final SchematicPlacement litematicaPlacement = SchematicPlacement.createFor(schematic, origin, ServerPlacement.removeExtension(file.getName()), true, true);
// Feature.VERSION
final ServerPlacement adjusted = readVersionInfo(placement, litematicaPlacement);
rendering.put(Objects.requireNonNullElse(adjusted, placement), litematicaPlacement);

((IIDContainer) litematicaPlacement).syncmatica$setServerId(placement.getId());
if (litematicaPlacement.isLocked()) {
litematicaPlacement.toggleLocked();
Expand All @@ -103,6 +110,11 @@ public void renderSyncmatic(final ServerPlacement placement) {
litematicaPlacement.toggleLocked();

DataManager.getSchematicPlacementManager().addSchematicPlacement(litematicaPlacement, true);

// Mark as selected if none
if (DataManager.getSchematicPlacementManager().getSelectedSchematicPlacement() == null) {
DataManager.getSchematicPlacementManager().setSelectedSchematicPlacement(litematicaPlacement);
}
context.getSyncmaticManager().updateServerPlacement(placement);
}

Expand All @@ -122,6 +134,7 @@ public ServerPlacement syncmaticFromSchematic(final SchematicPlacement schem) {
}
try {
final File placementFile = schem.getSchematicFile();
if (placementFile == null) { return null; }
final FileType fileType = FileType.fromFile(placementFile);
if (fileType == FileType.VANILLA_STRUCTURE || fileType == FileType.SCHEMATICA_SCHEMATIC) {
ScreenHelper.ifPresent(s -> s.addMessage(Message.MessageType.ERROR, "syncmatica.error.share_incompatible_schematic"));
Expand All @@ -141,7 +154,10 @@ public ServerPlacement syncmaticFromSchematic(final SchematicPlacement schem) {
final String dimension = MinecraftClient.getInstance().getCameraEntity().getEntityWorld().getRegistryKey().getValue().toString();
placement.move(dimension, schem.getOrigin(), schem.getRotation(), schem.getMirror());
transferSubregionDataToServerPlacement(schem, placement);
return placement;

// Feature.VERSION
final ServerPlacement adjusted = readVersionInfo(placement, schem);
return Objects.requireNonNullElse(adjusted, placement);
} catch (final Exception e) {
ScreenHelper.ifPresent(s -> s.addMessage(Message.MessageType.ERROR, "syncmatica.error.create_from_schematic", e.getMessage()));
}
Expand Down Expand Up @@ -209,7 +225,14 @@ public void renderSyncmatic(final ServerPlacement placement, final SchematicPlac
if (modPlacement.syncmatica$getServerId() != null && !modPlacement.syncmatica$getServerId().equals(placement.getId())) {
return;
}
rendering.put(placement, litematicaPlacement);
// Feature.VERSION
final ServerPlacement adjusted = readVersionInfo(placement, litematicaPlacement);
if (adjusted != null) {
rendering.put(adjusted, litematicaPlacement);
}
else {
rendering.put(placement, litematicaPlacement);
}
modPlacement.syncmatica$setServerId(placement.getId());

if (litematicaPlacement.isLocked()) {
Expand All @@ -223,6 +246,11 @@ public void renderSyncmatic(final ServerPlacement placement, final SchematicPlac
context.getSyncmaticManager().updateServerPlacement(placement);
if (addToRendering) {
DataManager.getSchematicPlacementManager().addSchematicPlacement(litematicaPlacement, false);

// Set as selected if none are
if (DataManager.getSchematicPlacementManager().getSelectedSchematicPlacement() == null) {
DataManager.getSchematicPlacementManager().setSelectedSchematicPlacement(litematicaPlacement);
}
}
}

Expand Down Expand Up @@ -291,14 +319,42 @@ public void preLoad(final SchematicPlacement schem) {
final UUID id = ((IIDContainer) schem).syncmatica$getServerId();
final ServerPlacement p = context.getSyncmaticManager().getPlacement(id);
if (isRendered(p)) {
rendering.put(p, schem);
final ServerPlacement adjusted = readVersionInfo(p, schem);
if (adjusted != null) {
rendering.put(adjusted, schem);
}
else {
rendering.put(p, schem);
}
DataManager.getSchematicPlacementManager().addSchematicPlacement(schem, false);
}
} else if (preLoadList != null) {
preLoadList.add(schem);
}
}

@Nullable
private ServerPlacement readVersionInfo(ServerPlacement p, SchematicPlacement s) {
try {
final File file = s.getSchematicFile();
if (file != null) {
final File dir = new File(file.getParent());

if (file.getName().endsWith(LitematicaSchematic.FILE_EXTENSION)) {
final Pair<SchematicSchema, SchematicMetadata> pair = LitematicaSchematic.readMetadataAndVersionFromFile(dir, file.getName());

if (pair != null) {
final SchematicSchema schema = pair.getLeft();
return p.setVersion(schema.litematicVersion(), schema.minecraftDataVersion());
}
}
}
}
catch (Exception ignored) {}

return null;
}

public void commitLoad() {
final SyncmaticManager man = context.getSyncmaticManager();
for (final SchematicPlacement schem : preLoadList) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ch.endte.syncmatica.litematica.gui;

import fi.dy.masa.malilib.gui.button.ButtonBase;

import java.util.List;

public interface IGuiBase {
List<ButtonBase> getButtons();
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import ch.endte.syncmatica.litematica.ScreenHelper;
import ch.endte.syncmatica.util.SyncmaticaUtil;
import fi.dy.masa.litematica.gui.Icons;
import fi.dy.masa.litematica.schematic.SchematicSchema;
import fi.dy.masa.litematica.util.DataFixerMode;
import fi.dy.masa.malilib.gui.GuiBase;
import fi.dy.masa.malilib.gui.LeftRight;
import fi.dy.masa.malilib.gui.interfaces.ISelectionListener;
Expand Down Expand Up @@ -116,6 +118,24 @@ private void drawPlacementInfo(final ServerPlacement placement, final DrawContex
drawString(drawContext, str, x, y, textColor);
y += 12;
drawString(drawContext, placement.getLastModifiedBy().getName(), x + 4, y, valueColor);
y += 12;

// Feature.VERSION
final int litematic = placement.getLitematicVersion();
final int dataVersion = placement.getDataVersion();

if (litematic > -1 && dataVersion > -1) {
final SchematicSchema version = new SchematicSchema(litematic, dataVersion);
final DataFixerMode.Schema schema = DataFixerMode.getSchemaByVersion(dataVersion);

str = StringUtils.translate("syncmatica.gui.label.placement_info.version", version.litematicVersion());
drawString(drawContext, str, x, y, textColor);
y += 12;

str = StringUtils.translate("syncmatica.gui.label.placement_info.schema", schema.getString(), version.minecraftDataVersion());
drawString(drawContext, str, x, y, textColor);
y += 12;
}
}

@Override
Expand Down
Loading

0 comments on commit b7b7b5e

Please sign in to comment.