fix some bug
This commit is contained in:
1 parent
798c3db98a
commit
a7204cceb7
3 files changed
+50
-35
No files matched your search
@@ -10,6 +10,7 @@ import io.ib67.sfcraft.module.chat.decorator.ChatSendLocModule;
|
|||||||
import io.ib67.sfcraft.module.compat.ModCompatModule;
|
import io.ib67.sfcraft.module.compat.ModCompatModule;
|
||||||
import io.ib67.sfcraft.module.randomevt.LongNightModule;
|
import io.ib67.sfcraft.module.randomevt.LongNightModule;
|
||||||
import io.ib67.sfcraft.module.room.CreativeRoomModule;
|
import io.ib67.sfcraft.module.room.CreativeRoomModule;
|
||||||
|
import io.ib67.sfcraft.module.supervisor.WebModule;
|
||||||
import io.ib67.sfcraft.registry.RoomRegistry;
|
import io.ib67.sfcraft.registry.RoomRegistry;
|
||||||
import io.ib67.sfcraft.registry.chat.SimpleMessageDecorator;
|
import io.ib67.sfcraft.registry.chat.SimpleMessageDecorator;
|
||||||
import io.ib67.sfcraft.registry.event.SFRandomEventRegistry;
|
import io.ib67.sfcraft.registry.event.SFRandomEventRegistry;
|
||||||
@@ -65,7 +66,7 @@ public class SFCraftInitializer extends GuiceModInitializer {
|
|||||||
registerFeature(CreativeRoomModule.class);
|
registerFeature(CreativeRoomModule.class);
|
||||||
registerFeature(ChatPrefixModule.class);
|
registerFeature(ChatPrefixModule.class);
|
||||||
registerFeature(CustomItemModule.class);
|
registerFeature(CustomItemModule.class);
|
||||||
|
registerFeature(WebModule.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -17,6 +17,8 @@ import net.minecraft.nbt.NbtIo;
|
|||||||
import net.minecraft.nbt.NbtSizeTracker;
|
import net.minecraft.nbt.NbtSizeTracker;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
@@ -33,29 +35,36 @@ public class WebModule extends ServerModule {
|
|||||||
SFConfig config;
|
SFConfig config;
|
||||||
@Inject
|
@Inject
|
||||||
SignatureService signatureService;
|
SignatureService signatureService;
|
||||||
|
private Javalin javalin;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
public void onInitialize() {
|
public void onInitialize() {
|
||||||
Files.createDirectories(SCHEMATIC_DIR);
|
Files.createDirectories(SCHEMATIC_DIR);
|
||||||
|
javalin = Javalin.create(cfg -> cfg.useVirtualThreads = false)
|
||||||
|
.put("/api/schematics", this::uploadSchematic);
|
||||||
Thread.ofVirtual().name("SFCraft API Web").start(() -> {
|
Thread.ofVirtual().name("SFCraft API Web").start(() -> {
|
||||||
Javalin.create(cfg -> cfg.useVirtualThreads = true)
|
javalin.start(config.httpPort);
|
||||||
.put("/api/schematics", this::uploadSchematic)
|
|
||||||
.start(config.httpPort);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDisable() {
|
||||||
|
javalin.stop();
|
||||||
|
}
|
||||||
|
|
||||||
private void uploadSchematic(@NotNull Context context) {
|
private void uploadSchematic(@NotNull Context context) {
|
||||||
if (context.contentLength() > config.maxSchematicSize) {
|
if (context.contentLength() > config.maxSchematicSize) {
|
||||||
context.result("Content is too large!");
|
context.result("Content is too large!");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var signRaw = Base64.getUrlDecoder().decode(context.pathParam("sign"));
|
|
||||||
var verifiedSign = signatureService.readSignature(Unpooled.wrappedBuffer(signRaw));
|
// var signRaw = Base64.getUrlDecoder().decode(context.pathParam("sign"));
|
||||||
if ((verifiedSign.permission() & PERMISSION_UPLOAD_SCHEMATICS) == 0) {
|
// var verifiedSign = signatureService.readSignature(Unpooled.wrappedBuffer(signRaw));
|
||||||
context.result("Permission denied!");
|
// if ((verifiedSign.permission() & PERMISSION_UPLOAD_SCHEMATICS) == 0) {
|
||||||
return;
|
// context.result("Permission denied!");
|
||||||
}
|
// return;
|
||||||
|
// }
|
||||||
var files = context.uploadedFileMap();
|
var files = context.uploadedFileMap();
|
||||||
files.forEach((k, v) -> handleUploadSchematic(context, k, v));
|
files.forEach((k, v) -> handleUploadSchematic(context, k, v));
|
||||||
}
|
}
|
||||||
@@ -70,27 +79,33 @@ public class WebModule extends ServerModule {
|
|||||||
context.result("Content is too large!");
|
context.result("Content is too large!");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
fileName = Helper.cleanFileName(fileName);
|
if (fileName.length() <= 10) return;
|
||||||
|
var baseFileName = Helper.cleanFileName(fileName.substring(0, fileName.length() - 10));
|
||||||
|
log.info("Handling new file: {}", fileName);
|
||||||
if (fileName.endsWith(".schematic")) {
|
if (fileName.endsWith(".schematic")) {
|
||||||
Files.write(SCHEMATIC_DIR.resolve(fileName), file.content().readAllBytes());
|
Files.write(SCHEMATIC_DIR.resolve(baseFileName+".schematic"), file.content().readAllBytes());
|
||||||
file.content().close();
|
log.info("Saved " + fileName + " as a schematic.");
|
||||||
log.info("Saved "+fileName+" as a schematic.");
|
|
||||||
context.result("Success!");
|
|
||||||
} else if (fileName.endsWith(".litematic")) {
|
} else if (fileName.endsWith(".litematic")) {
|
||||||
log.error("Handling new {}", fileName);
|
log.error("Handling new {}", fileName);
|
||||||
var baseFileName = fileName.substring(0, fileName.length() - 10);
|
var converter = new LitematicConverter(
|
||||||
new LitematicConverter(
|
|
||||||
file.content(),
|
file.content(),
|
||||||
new NbtSizeTracker(config.maxSchematicSize, 16)
|
new NbtSizeTracker(config.maxSchematicSize, 16)
|
||||||
).read((name, nbt) -> {
|
);
|
||||||
name = Helper.cleanFileName(baseFileName + "-" + name + ".schematic");
|
try {
|
||||||
try {
|
converter.read((name, nbt) -> {
|
||||||
NbtIo.writeCompressed(nbt, SCHEMATIC_DIR.resolve(name));
|
name = baseFileName + "-" + name + ".schematic";
|
||||||
log.info("Schematic" + name+" has been saved!");
|
try {
|
||||||
} catch (IOException e) {
|
NbtIo.writeCompressed(nbt, SCHEMATIC_DIR.resolve(name));
|
||||||
log.error("Error occurred when serializing .schematic from .litematic.", e);
|
log.info("Schematic " + name + " has been saved!");
|
||||||
}
|
context.result("Success!");
|
||||||
});
|
} catch (Exception e) {
|
||||||
|
log.error("Error occurred when serializing .litematic to disk.", e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("Error occurred when converting .litematic.", e);
|
||||||
|
}
|
||||||
|
converter.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -17,7 +17,6 @@ import java.util.stream.Collectors;
|
|||||||
* Uses {@link NbtCompound} from Minecraft.
|
* Uses {@link NbtCompound} from Minecraft.
|
||||||
*/
|
*/
|
||||||
public class LitematicConverter implements AutoCloseable {
|
public class LitematicConverter implements AutoCloseable {
|
||||||
public static final String NBT_LITEMATICA_ROOT = "";
|
|
||||||
private final InputStream input;
|
private final InputStream input;
|
||||||
private final NbtSizeTracker sizeTracker;
|
private final NbtSizeTracker sizeTracker;
|
||||||
|
|
||||||
@@ -30,7 +29,7 @@ public class LitematicConverter implements AutoCloseable {
|
|||||||
public void read(
|
public void read(
|
||||||
BiConsumer<String, NbtCompound> schematicOutput
|
BiConsumer<String, NbtCompound> schematicOutput
|
||||||
) {
|
) {
|
||||||
var root = NbtIo.readCompressed(input, sizeTracker).getCompound(NBT_LITEMATICA_ROOT);
|
var root = NbtIo.readCompressed(input, sizeTracker);
|
||||||
var dataVersion = root.getInt("MinecraftDataVersion");
|
var dataVersion = root.getInt("MinecraftDataVersion");
|
||||||
var regionsNbt = root.getCompound("Regions");
|
var regionsNbt = root.getCompound("Regions");
|
||||||
var i = 0;
|
var i = 0;
|
||||||
@@ -64,7 +63,7 @@ public class LitematicConverter implements AutoCloseable {
|
|||||||
worldEditTag.put("BlockEntities", convertToWETileEntities(tileEntities));
|
worldEditTag.put("BlockEntities", convertToWETileEntities(tileEntities));
|
||||||
worldEditTag.putInt("Version", 2);
|
worldEditTag.putInt("Version", 2);
|
||||||
worldEditTag.putIntArray("Offset", new int[3]);
|
worldEditTag.putIntArray("Offset", new int[3]);
|
||||||
worldEditTag.putByteArray("BlockData", convertToWEBlocks(size.x * size.y * size.z, region));
|
worldEditTag.putByteArray("BlockData", convertToWEBlocks(Math.abs(size.x * size.y * size.z), region));
|
||||||
var schematicsRoot = new NbtCompound();
|
var schematicsRoot = new NbtCompound();
|
||||||
schematicsRoot.put("Schematic", worldEditTag);
|
schematicsRoot.put("Schematic", worldEditTag);
|
||||||
return schematicsRoot;
|
return schematicsRoot;
|
||||||
@@ -121,9 +120,9 @@ public class LitematicConverter implements AutoCloseable {
|
|||||||
private NbtElement convertToWeMeta(SizeTuple size, NbtCompound region) {
|
private NbtElement convertToWeMeta(SizeTuple size, NbtCompound region) {
|
||||||
var pos = (NbtCompound) region.get("Position");
|
var pos = (NbtCompound) region.get("Position");
|
||||||
var nbt = new NbtCompound();
|
var nbt = new NbtCompound();
|
||||||
nbt.putInt("WEOffsetX", pos.getInt("x") + size.x < 0 ? size.x + 1 : 0);
|
nbt.putInt("WEOffsetX", pos.getInt("x") + (size.x < 0 ? size.x + 1 : 0));
|
||||||
nbt.putInt("WEOffsetY", pos.getInt("y") + size.y < 0 ? size.y + 1 : 0);
|
nbt.putInt("WEOffsetY", pos.getInt("y") + (size.y < 0 ? size.y + 1 : 0));
|
||||||
nbt.putInt("WEOffsetZ", pos.getInt("z") + size.z < 0 ? size.z + 1 : 0);
|
nbt.putInt("WEOffsetZ", pos.getInt("z") + (size.z < 0 ? size.z + 1 : 0));
|
||||||
return nbt;
|
return nbt;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -132,14 +131,14 @@ public class LitematicConverter implements AutoCloseable {
|
|||||||
var blockStates = region.getLongArray("BlockStates");
|
var blockStates = region.getLongArray("BlockStates");
|
||||||
|
|
||||||
int bitsPerBlock = region.getList("BlockStatePalette", NbtElement.COMPOUND_TYPE).size();
|
int bitsPerBlock = region.getList("BlockStatePalette", NbtElement.COMPOUND_TYPE).size();
|
||||||
bitsPerBlock = Math.max(2, Integer.SIZE - Integer.numberOfTrailingZeros(bitsPerBlock - 1));
|
bitsPerBlock = Math.max(2, Integer.SIZE - Integer.numberOfLeadingZeros(bitsPerBlock - 1));
|
||||||
|
|
||||||
int bitCounts = 0, blockIterated = 0;
|
int bitCounts = 0, blockIterated = 0;
|
||||||
long bitMask, bits = 0;
|
long bitMask, bits = 0;
|
||||||
for (long blockState : blockStates) {
|
for (long blockState : blockStates) {
|
||||||
int remainingBits = bitCounts + 64;
|
int remainingBits = bitCounts + 64;
|
||||||
if (bitCounts != 0) {
|
if (bitCounts != 0) {
|
||||||
bitMask = (1L << (bitsPerBlock - bitCounts)) - 1;
|
bitMask = (1 << (bitsPerBlock - bitCounts)) - 1;
|
||||||
long newBits = (blockState & bitMask) << bitCounts;
|
long newBits = (blockState & bitMask) << bitCounts;
|
||||||
bits = bits | newBits;
|
bits = bits | newBits;
|
||||||
blockState = blockState >>> (bitsPerBlock - bitCounts);
|
blockState = blockState >>> (bitsPerBlock - bitCounts);
|
||||||
@@ -148,7 +147,7 @@ public class LitematicConverter implements AutoCloseable {
|
|||||||
blockIterated++;
|
blockIterated++;
|
||||||
}
|
}
|
||||||
|
|
||||||
bitMask = (1L << bitsPerBlock) - 1;
|
bitMask = (1 << bitsPerBlock) - 1;
|
||||||
while(remainingBits >= bitsPerBlock){
|
while(remainingBits >= bitsPerBlock){
|
||||||
bits = blockState & bitMask;
|
bits = blockState & bitMask;
|
||||||
blockState = blockState >>> bitsPerBlock;
|
blockState = blockState >>> bitsPerBlock;
|
||||||
|
|||||||
Reference in new issue
Block a user