mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2025-07-01 23:47:21 +00:00
restructure LegacyTileData
This commit is contained in:
parent
fc793592f7
commit
dfe1cce6de
@ -1,8 +1,8 @@
|
||||
package com.volmit.iris.engine.object;
|
||||
|
||||
import com.volmit.iris.Iris;
|
||||
import com.volmit.iris.util.collection.KList;
|
||||
import com.volmit.iris.util.scheduling.J;
|
||||
import org.apache.commons.io.function.IOFunction;
|
||||
import org.bukkit.DyeColor;
|
||||
import org.bukkit.block.*;
|
||||
import org.bukkit.block.banner.Pattern;
|
||||
@ -12,127 +12,126 @@ import org.bukkit.entity.EntityType;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class LegacyTileData extends TileData {
|
||||
private static final Map<Short, Handler> legacy = Map.of(
|
||||
(short) 0, new SignHandler(),
|
||||
(short) 1, new SpawnerHandler(),
|
||||
(short) 2, new BannerHandler());
|
||||
private final short id;
|
||||
private final KList<Object> properties;
|
||||
private static final Map<Integer, IOFunction<DataInputStream, Handler>> legacy = Map.of(
|
||||
0, SignHandler::new,
|
||||
1, SpawnerHandler::new,
|
||||
2, BannerHandler::new);
|
||||
private final int id;
|
||||
private final Handler handler;
|
||||
|
||||
public LegacyTileData(DataInputStream in) throws IOException {
|
||||
id = in.readShort();
|
||||
var handler = legacy.get(id);
|
||||
if (handler == null)
|
||||
var factory = legacy.get(id);
|
||||
if (factory == null)
|
||||
throw new IOException("Unknown tile type: " + id);
|
||||
properties = handler.read(in);
|
||||
handler = factory.apply(in);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBukkit(Block block) {
|
||||
var handler = legacy.get(id);
|
||||
J.s(() -> handler.toBukkit(properties, block));
|
||||
J.s(() -> handler.toBukkit(block));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBinary(DataOutputStream out) throws IOException {
|
||||
out.writeShort(id);
|
||||
legacy.get(id).toBinary(properties, out);
|
||||
handler.toBinary(out);
|
||||
}
|
||||
|
||||
private interface Handler {
|
||||
KList<Object> read(DataInputStream in) throws IOException;
|
||||
void toBinary(KList<Object> list, DataOutputStream out) throws IOException;
|
||||
void toBukkit(KList<Object> list, Block block);
|
||||
void toBinary(DataOutputStream out) throws IOException;
|
||||
void toBukkit(Block block);
|
||||
}
|
||||
|
||||
private static class SignHandler implements Handler {
|
||||
@Override
|
||||
public KList<Object> read(DataInputStream in) throws IOException {
|
||||
return new KList<>()
|
||||
.qadd(in.readUTF())
|
||||
.qadd(in.readUTF())
|
||||
.qadd(in.readUTF())
|
||||
.qadd(in.readUTF())
|
||||
.qadd(DyeColor.values()[in.readByte()]);
|
||||
private final String line1;
|
||||
private final String line2;
|
||||
private final String line3;
|
||||
private final String line4;
|
||||
private final DyeColor dyeColor;
|
||||
|
||||
private SignHandler(DataInputStream in) throws IOException {
|
||||
line1 = in.readUTF();
|
||||
line2 = in.readUTF();
|
||||
line3 = in.readUTF();
|
||||
line4 = in.readUTF();
|
||||
dyeColor = DyeColor.values()[in.readByte()];
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBinary(KList<Object> list, DataOutputStream out) throws IOException {
|
||||
out.writeUTF((String) list.get(0));
|
||||
out.writeUTF((String) list.get(1));
|
||||
out.writeUTF((String) list.get(2));
|
||||
out.writeUTF((String) list.get(3));
|
||||
out.writeByte(((DyeColor) list.get(4)).ordinal());
|
||||
public void toBinary(DataOutputStream out) throws IOException {
|
||||
out.writeUTF(line1);
|
||||
out.writeUTF(line2);
|
||||
out.writeUTF(line3);
|
||||
out.writeUTF(line4);
|
||||
out.writeByte(dyeColor.ordinal());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBukkit(KList<Object> list, Block block) {
|
||||
public void toBukkit(Block block) {
|
||||
Sign sign = (Sign) block.getState();
|
||||
sign.setLine(0, (String) list.get(0));
|
||||
sign.setLine(1, (String) list.get(1));
|
||||
sign.setLine(2, (String) list.get(2));
|
||||
sign.setLine(3, (String) list.get(3));
|
||||
sign.setColor((DyeColor) list.get(4));
|
||||
sign.setLine(0, line1);
|
||||
sign.setLine(1, line2);
|
||||
sign.setLine(2, line3);
|
||||
sign.setLine(3, line4);
|
||||
sign.setColor(dyeColor);
|
||||
sign.update();
|
||||
}
|
||||
}
|
||||
|
||||
private static class SpawnerHandler implements Handler {
|
||||
@Override
|
||||
public KList<Object> read(DataInputStream in) throws IOException {
|
||||
return new KList<>().qadd(EntityType.values()[in.readShort()]);
|
||||
private final EntityType type;
|
||||
|
||||
private SpawnerHandler(DataInputStream in) throws IOException {
|
||||
type = EntityType.values()[in.readShort()];
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBinary(KList<Object> list, DataOutputStream out) throws IOException {
|
||||
out.writeShort(((EntityType) list.get(0)).ordinal());
|
||||
public void toBinary(DataOutputStream out) throws IOException {
|
||||
out.writeShort(type.ordinal());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBukkit(KList<Object> list, Block block) {
|
||||
public void toBukkit(Block block) {
|
||||
CreatureSpawner spawner = (CreatureSpawner) block.getState();
|
||||
spawner.setSpawnedType((EntityType) list.get(0));
|
||||
spawner.setSpawnedType(type);
|
||||
spawner.update();
|
||||
}
|
||||
}
|
||||
|
||||
private static class BannerHandler implements Handler {
|
||||
@Override
|
||||
public KList<Object> read(DataInputStream in) throws IOException {
|
||||
KList<Object> list = new KList<>();
|
||||
list.add(DyeColor.values()[in.readByte()]);
|
||||
int listSize = in.readByte();
|
||||
var patterns = new KList<>();
|
||||
private final KList<Pattern> patterns;
|
||||
private final DyeColor baseColor;
|
||||
|
||||
private BannerHandler(DataInputStream in) throws IOException {
|
||||
baseColor = DyeColor.values()[in.readByte()];
|
||||
patterns = new KList<>();
|
||||
int listSize = in.readByte();
|
||||
for (int i = 0; i < listSize; i++) {
|
||||
DyeColor color = DyeColor.values()[in.readByte()];
|
||||
PatternType type = PatternType.values()[in.readByte()];
|
||||
patterns.add(new Pattern(color, type));
|
||||
PatternType pattern = PatternType.values()[in.readByte()];
|
||||
patterns.add(new Pattern(color, pattern));
|
||||
}
|
||||
|
||||
list.add(patterns);
|
||||
return list;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBinary(KList<Object> list, DataOutputStream out) throws IOException {
|
||||
out.writeByte(((DyeColor) list.get(0)).ordinal());
|
||||
out.writeByte(((List<Pattern>) list.get(1)).size());
|
||||
for (Pattern i : (List<Pattern>) list.get(1)) {
|
||||
public void toBinary(DataOutputStream out) throws IOException {
|
||||
out.writeByte(baseColor.ordinal());
|
||||
out.writeByte(patterns.size());
|
||||
for (Pattern i : patterns) {
|
||||
out.writeByte(i.getColor().ordinal());
|
||||
out.writeByte(i.getPattern().ordinal());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBukkit(KList<Object> list, Block block) {
|
||||
public void toBukkit(Block block) {
|
||||
Banner banner = (Banner) block.getState();
|
||||
banner.setBaseColor((DyeColor) list.get(0));
|
||||
banner.setPatterns((List<Pattern>) list.get(1));
|
||||
banner.setBaseColor(baseColor);
|
||||
banner.setPatterns(patterns);
|
||||
banner.update();
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user