mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2025-07-04 00:46:08 +00:00
add legacy tile data reader
This commit is contained in:
parent
623fd45ef4
commit
cacef8c8fc
@ -376,9 +376,11 @@ public class CommandObject implements DecreeExecutor {
|
|||||||
@Param(description = "The file to store it in, can use / for subfolders")
|
@Param(description = "The file to store it in, can use / for subfolders")
|
||||||
String name,
|
String name,
|
||||||
@Param(description = "Overwrite existing object files", defaultValue = "false", aliases = "force")
|
@Param(description = "Overwrite existing object files", defaultValue = "false", aliases = "force")
|
||||||
boolean overwrite
|
boolean overwrite,
|
||||||
|
@Param(description = "Use legacy TileState serialization if possible", defaultValue = "true")
|
||||||
|
boolean legacy
|
||||||
) {
|
) {
|
||||||
IrisObject o = WandSVC.createSchematic(player());
|
IrisObject o = WandSVC.createSchematic(player(), legacy);
|
||||||
|
|
||||||
if (o == null) {
|
if (o == null) {
|
||||||
sender().sendMessage(C.YELLOW + "You need to hold your wand!");
|
sender().sendMessage(C.YELLOW + "You need to hold your wand!");
|
||||||
|
@ -76,7 +76,7 @@ public class WandSVC implements IrisService {
|
|||||||
* @param p The wand player
|
* @param p The wand player
|
||||||
* @return The new object
|
* @return The new object
|
||||||
*/
|
*/
|
||||||
public static IrisObject createSchematic(Player p) {
|
public static IrisObject createSchematic(Player p, boolean legacy) {
|
||||||
if (!isHoldingWand(p)) {
|
if (!isHoldingWand(p)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -132,7 +132,7 @@ public class WandSVC implements IrisService {
|
|||||||
continue;
|
continue;
|
||||||
|
|
||||||
BlockVector bv = b.getLocation().subtract(c.getLowerNE().toVector()).toVector().toBlockVector();
|
BlockVector bv = b.getLocation().subtract(c.getLowerNE().toVector()).toVector().toBlockVector();
|
||||||
s.setUnsigned(bv.getBlockX(), bv.getBlockY(), bv.getBlockZ(), b);
|
s.setUnsigned(bv.getBlockX(), bv.getBlockY(), bv.getBlockZ(), b, legacy);
|
||||||
} finally {
|
} finally {
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
@ -562,7 +562,7 @@ public class IrisObject extends IrisRegistrant {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setUnsigned(int x, int y, int z, Block block) {
|
public void setUnsigned(int x, int y, int z, Block block, boolean legacy) {
|
||||||
BlockVector v = getSigned(x, y, z);
|
BlockVector v = getSigned(x, y, z);
|
||||||
|
|
||||||
if (block == null) {
|
if (block == null) {
|
||||||
@ -571,7 +571,7 @@ public class IrisObject extends IrisRegistrant {
|
|||||||
} else {
|
} else {
|
||||||
BlockData data = block.getBlockData();
|
BlockData data = block.getBlockData();
|
||||||
getBlocks().put(v, data);
|
getBlocks().put(v, data);
|
||||||
TileData state = TileData.getTileState(block);
|
TileData state = TileData.getTileState(block, legacy);
|
||||||
if (state != null) {
|
if (state != null) {
|
||||||
Iris.debug("Saved State " + v);
|
Iris.debug("Saved State " + v);
|
||||||
getStates().put(v, state);
|
getStates().put(v, state);
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
package com.volmit.iris.engine.object;
|
package com.volmit.iris.engine.object;
|
||||||
|
|
||||||
|
import com.volmit.iris.core.nms.container.Pair;
|
||||||
import com.volmit.iris.util.collection.KList;
|
import com.volmit.iris.util.collection.KList;
|
||||||
import com.volmit.iris.util.collection.KMap;
|
import com.volmit.iris.util.collection.KMap;
|
||||||
import com.volmit.iris.util.scheduling.J;
|
import com.volmit.iris.util.scheduling.J;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.EqualsAndHashCode;
|
import lombok.EqualsAndHashCode;
|
||||||
import lombok.NonNull;
|
import lombok.NonNull;
|
||||||
import lombok.ToString;
|
import lombok.ToString;
|
||||||
@ -15,6 +17,7 @@ import org.bukkit.block.banner.Pattern;
|
|||||||
import org.bukkit.block.banner.PatternType;
|
import org.bukkit.block.banner.PatternType;
|
||||||
import org.bukkit.block.data.BlockData;
|
import org.bukkit.block.data.BlockData;
|
||||||
import org.bukkit.entity.EntityType;
|
import org.bukkit.entity.EntityType;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.io.DataInputStream;
|
import java.io.DataInputStream;
|
||||||
import java.io.DataOutputStream;
|
import java.io.DataOutputStream;
|
||||||
@ -24,10 +27,10 @@ import java.util.Map;
|
|||||||
@ToString
|
@ToString
|
||||||
@EqualsAndHashCode(callSuper = false)
|
@EqualsAndHashCode(callSuper = false)
|
||||||
public class LegacyTileData extends TileData {
|
public class LegacyTileData extends TileData {
|
||||||
private static final Map<Integer, IOFunction<DataInputStream, Handler>> legacy = Map.of(
|
private static final Map<Integer, Pair<Builder, IOFunction<DataInputStream, Handler>>> legacy = Map.of(
|
||||||
0, SignHandler::new,
|
0, new Pair<>(SignHandler::fromBukkit, SignHandler::new),
|
||||||
1, SpawnerHandler::new,
|
1, new Pair<>(SpawnerHandler::fromBukkit, SpawnerHandler::new),
|
||||||
2, BannerHandler::new);
|
2, new Pair<>(BannerHandler::fromBukkit, BannerHandler::new));
|
||||||
private final int id;
|
private final int id;
|
||||||
private final Handler handler;
|
private final Handler handler;
|
||||||
|
|
||||||
@ -36,7 +39,24 @@ public class LegacyTileData extends TileData {
|
|||||||
var factory = legacy.get(id);
|
var factory = legacy.get(id);
|
||||||
if (factory == null)
|
if (factory == null)
|
||||||
throw new IOException("Unknown tile type: " + id);
|
throw new IOException("Unknown tile type: " + id);
|
||||||
handler = factory.apply(in);
|
handler = factory.getB().apply(in);
|
||||||
|
}
|
||||||
|
|
||||||
|
private LegacyTileData(int id, Handler handler) {
|
||||||
|
this.id = id;
|
||||||
|
this.handler = handler;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public static LegacyTileData fromBukkit(@NonNull BlockState tileState) {
|
||||||
|
var type = tileState.getType();
|
||||||
|
for (var id : legacy.keySet()) {
|
||||||
|
var factory = legacy.get(id);
|
||||||
|
var handler = factory.getA().apply(tileState, type);
|
||||||
|
if (handler != null)
|
||||||
|
return new LegacyTileData(id, handler);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -77,8 +97,14 @@ public class LegacyTileData extends TileData {
|
|||||||
void toBukkit(Block block);
|
void toBukkit(Block block);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
|
private interface Builder {
|
||||||
|
@Nullable Handler apply(@NonNull BlockState blockState, @NonNull Material type);
|
||||||
|
}
|
||||||
|
|
||||||
@ToString
|
@ToString
|
||||||
@EqualsAndHashCode
|
@EqualsAndHashCode
|
||||||
|
@AllArgsConstructor
|
||||||
private static class SignHandler implements Handler {
|
private static class SignHandler implements Handler {
|
||||||
private final String line1;
|
private final String line1;
|
||||||
private final String line2;
|
private final String line2;
|
||||||
@ -94,6 +120,13 @@ public class LegacyTileData extends TileData {
|
|||||||
dyeColor = DyeColor.values()[in.readByte()];
|
dyeColor = DyeColor.values()[in.readByte()];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("deprecation")
|
||||||
|
private static SignHandler fromBukkit(BlockState blockState, Material type) {
|
||||||
|
if (!Tag.ALL_SIGNS.isTagged(type) || !(blockState instanceof Sign sign))
|
||||||
|
return null;
|
||||||
|
return new SignHandler(sign.getLine(0), sign.getLine(1), sign.getLine(2), sign.getLine(3), sign.getColor());
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Material getMaterial() {
|
public Material getMaterial() {
|
||||||
return Material.OAK_SIGN;
|
return Material.OAK_SIGN;
|
||||||
@ -126,6 +159,7 @@ public class LegacyTileData extends TileData {
|
|||||||
}
|
}
|
||||||
@ToString
|
@ToString
|
||||||
@EqualsAndHashCode
|
@EqualsAndHashCode
|
||||||
|
@AllArgsConstructor
|
||||||
private static class SpawnerHandler implements Handler {
|
private static class SpawnerHandler implements Handler {
|
||||||
private final EntityType type;
|
private final EntityType type;
|
||||||
|
|
||||||
@ -133,6 +167,12 @@ public class LegacyTileData extends TileData {
|
|||||||
type = EntityType.values()[in.readShort()];
|
type = EntityType.values()[in.readShort()];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static SpawnerHandler fromBukkit(BlockState blockState, Material material) {
|
||||||
|
if (material != Material.SPAWNER || !(blockState instanceof CreatureSpawner spawner))
|
||||||
|
return null;
|
||||||
|
return new SpawnerHandler(spawner.getSpawnedType());
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Material getMaterial() {
|
public Material getMaterial() {
|
||||||
return Material.SPAWNER;
|
return Material.SPAWNER;
|
||||||
@ -157,6 +197,7 @@ public class LegacyTileData extends TileData {
|
|||||||
}
|
}
|
||||||
@ToString
|
@ToString
|
||||||
@EqualsAndHashCode
|
@EqualsAndHashCode
|
||||||
|
@AllArgsConstructor
|
||||||
private static class BannerHandler implements Handler {
|
private static class BannerHandler implements Handler {
|
||||||
private final KList<Pattern> patterns;
|
private final KList<Pattern> patterns;
|
||||||
private final DyeColor baseColor;
|
private final DyeColor baseColor;
|
||||||
@ -172,6 +213,12 @@ public class LegacyTileData extends TileData {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static BannerHandler fromBukkit(BlockState blockState, Material type) {
|
||||||
|
if (!Tag.BANNERS.isTagged(type) || !(blockState instanceof Banner banner))
|
||||||
|
return null;
|
||||||
|
return new BannerHandler(new KList<>(banner.getPatterns()), banner.getBaseColor());
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Material getMaterial() {
|
public Material getMaterial() {
|
||||||
return Material.WHITE_BANNER;
|
return Material.WHITE_BANNER;
|
||||||
|
@ -53,9 +53,15 @@ public class TileData implements Cloneable {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static TileData getTileState(Block block) {
|
public static TileData getTileState(Block block, boolean useLegacy) {
|
||||||
if (!INMS.get().hasTile(block.getType()))
|
if (!INMS.get().hasTile(block.getType()))
|
||||||
return null;
|
return null;
|
||||||
|
if (useLegacy) {
|
||||||
|
var legacy = LegacyTileData.fromBukkit(block.getState());
|
||||||
|
if (legacy != null)
|
||||||
|
return legacy;
|
||||||
|
}
|
||||||
|
|
||||||
return new TileData().fromBukkit(block);
|
return new TileData().fromBukkit(block);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -40,7 +40,7 @@ public class TileMatter extends RawMatter<TileWrapper> {
|
|||||||
public TileMatter(int width, int height, int depth) {
|
public TileMatter(int width, int height, int depth) {
|
||||||
super(width, height, depth, TileWrapper.class);
|
super(width, height, depth, TileWrapper.class);
|
||||||
registerWriter(World.class, (w, d, x, y, z) -> TileData.setTileState(w.getBlockAt(new Location(w, x, y, z)), d.getData()));
|
registerWriter(World.class, (w, d, x, y, z) -> TileData.setTileState(w.getBlockAt(new Location(w, x, y, z)), d.getData()));
|
||||||
registerReader(World.class, (w, x, y, z) -> new TileWrapper(TileData.getTileState(w.getBlockAt(new Location(w, x, y, z)))));
|
registerReader(World.class, (w, x, y, z) -> new TileWrapper(TileData.getTileState(w.getBlockAt(new Location(w, x, y, z)), false)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Loading…
x
Reference in New Issue
Block a user