mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2025-12-29 20:19:06 +00:00
implement dynamic tile entities
This commit is contained in:
@@ -117,10 +117,8 @@ public class CommandObject implements DecreeExecutor {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setTile(int xx, int yy, int zz, TileData<? extends TileState> tile) {
|
||||
BlockState state = world.getBlockAt(xx, yy, zz).getState();
|
||||
tile.toBukkitTry(state);
|
||||
state.update();
|
||||
public void setTile(int xx, int yy, int zz, TileData tile) {
|
||||
tile.toBukkitTry(world.getBlockAt(xx, yy, zz));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -28,10 +28,7 @@ import com.volmit.iris.util.math.Vector3d;
|
||||
import com.volmit.iris.util.nbt.mca.palette.MCABiomeContainer;
|
||||
import com.volmit.iris.util.nbt.mca.palette.MCAPaletteAccess;
|
||||
import com.volmit.iris.util.nbt.tag.CompoundTag;
|
||||
import org.bukkit.Chunk;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.WorldCreator;
|
||||
import org.bukkit.*;
|
||||
import org.bukkit.block.Biome;
|
||||
import org.bukkit.entity.Dolphin;
|
||||
import org.bukkit.entity.Entity;
|
||||
@@ -41,13 +38,16 @@ import org.bukkit.generator.ChunkGenerator;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.Color;
|
||||
|
||||
public interface INMSBinding {
|
||||
boolean hasTile(Material material);
|
||||
|
||||
boolean hasTile(Location l);
|
||||
|
||||
CompoundTag serializeTile(Location location);
|
||||
KMap<String, Object> serializeTile(Location location);
|
||||
|
||||
void deserializeTile(CompoundTag s, Location newPosition);
|
||||
void deserializeTile(KMap<String, Object> s, Location newPosition);
|
||||
|
||||
CompoundTag serializeEntity(Entity location);
|
||||
|
||||
|
||||
@@ -32,6 +32,7 @@ import com.volmit.iris.util.nbt.mca.palette.MCAPaletteAccess;
|
||||
import com.volmit.iris.util.nbt.tag.CompoundTag;
|
||||
import org.bukkit.Chunk;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.block.Biome;
|
||||
import org.bukkit.entity.Dolphin;
|
||||
@@ -61,16 +62,27 @@ public class NMSBinding1X implements INMSBinding {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasTile(Material material) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasTile(Location l) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompoundTag serializeTile(Location location) {
|
||||
public KMap<String, Object> serializeTile(Location location) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deserializeTile(KMap<String, Object> s, Location newPosition) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void injectBiomesFromMantle(Chunk e, Mantle mantle) {
|
||||
|
||||
@@ -105,11 +117,6 @@ public class NMSBinding1X implements INMSBinding {
|
||||
return Color.GREEN;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deserializeTile(CompoundTag s, Location newPosition) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompoundTag serializeEntity(Entity location) {
|
||||
return null;
|
||||
|
||||
@@ -183,7 +183,7 @@ public class TreeSVC implements IrisService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setTile(int xx, int yy, int zz, TileData<? extends TileState> tile) {
|
||||
public void setTile(int xx, int yy, int zz, TileData tile) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -59,7 +59,7 @@ import java.util.concurrent.CountDownLatch;
|
||||
public class WandSVC implements IrisService {
|
||||
private static final Particle CRIT_MAGIC = E.getOrDefault(Particle.class, "CRIT_MAGIC", "CRIT");
|
||||
private static final Particle REDSTONE = E.getOrDefault(Particle.class, "REDSTONE", "DUST");
|
||||
private static final int BLOCKS_PER_TICK = Integer.parseInt(System.getProperty("iris.blocks_per_tick", "100"));
|
||||
private static final int BLOCKS_PER_TICK = Integer.parseInt(System.getProperty("iris.blocks_per_tick", "1000"));
|
||||
|
||||
private static ItemStack dust;
|
||||
private static ItemStack wand;
|
||||
@@ -84,19 +84,6 @@ public class WandSVC implements IrisService {
|
||||
Cuboid c = new Cuboid(f[0], f[1]);
|
||||
IrisObject s = new IrisObject(c.getSizeX(), c.getSizeY(), c.getSizeZ());
|
||||
|
||||
if (Bukkit.isPrimaryThread()) {
|
||||
for (Block b : c) {
|
||||
if (b.getType().equals(Material.AIR)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
BlockVector bv = b.getLocation().subtract(c.getLowerNE().toVector()).toVector().toBlockVector();
|
||||
s.setUnsigned(bv.getBlockX(), bv.getBlockY(), bv.getBlockZ(), b);
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
var it = c.iterator();
|
||||
var latch = new CountDownLatch(1);
|
||||
new SR() {
|
||||
|
||||
@@ -53,7 +53,6 @@ import com.volmit.iris.util.math.Position2;
|
||||
import com.volmit.iris.util.math.RNG;
|
||||
import com.volmit.iris.util.matter.MatterCavern;
|
||||
import com.volmit.iris.util.matter.MatterUpdate;
|
||||
import com.volmit.iris.util.matter.TileWrapper;
|
||||
import com.volmit.iris.util.matter.slices.container.JigsawPieceContainer;
|
||||
import com.volmit.iris.util.parallel.BurstExecutor;
|
||||
import com.volmit.iris.util.parallel.MultiBurst;
|
||||
@@ -283,10 +282,10 @@ public interface Engine extends DataProvider, Fallible, LootProvider, BlockUpdat
|
||||
}
|
||||
|
||||
getMantle().getMantle().raiseFlag(c.getX(), c.getZ(), MantleFlag.TILE, () -> J.s(() -> {
|
||||
getMantle().getMantle().iterateChunk(c.getX(), c.getZ(), TileWrapper.class, (x, y, z, tile) -> {
|
||||
getMantle().getMantle().iterateChunk(c.getX(), c.getZ(), TileData.class, (x, y, z, tile) -> {
|
||||
int betterY = y + getWorld().minHeight();
|
||||
if (!TileData.setTileState(c.getBlock(x, betterY, z), tile.getData()))
|
||||
Iris.warn("Failed to set tile entity data at [%d %d %d | %s] for tile %s!", x, betterY, z, c.getBlock(x, betterY, z).getBlockData().getMaterial().getKey(), tile.getData().getTileId());
|
||||
if (!TileData.setTileState(c.getBlock(x, betterY, z), tile))
|
||||
Iris.warn("Failed to set tile entity data at [%d %d %d | %s] for tile %s!", x, betterY, z, c.getBlock(x, betterY, z).getBlockData().getMaterial().getKey(), tile.getMaterial().name());
|
||||
});
|
||||
}));
|
||||
getMantle().getMantle().raiseFlag(c.getX(), c.getZ(), MantleFlag.CUSTOM, () -> J.s(() -> {
|
||||
|
||||
@@ -79,7 +79,7 @@ public class HeightmapObjectPlacer implements IObjectPlacer {
|
||||
return oplacer.isDebugSmartBore();
|
||||
}
|
||||
|
||||
public void setTile(int param1Int1, int param1Int2, int param1Int3, TileData<? extends TileState> param1TileData) {
|
||||
public void setTile(int param1Int1, int param1Int2, int param1Int3, TileData param1TileData) {
|
||||
oplacer.setTile(param1Int1, param1Int2, param1Int3, param1TileData);
|
||||
}
|
||||
|
||||
|
||||
@@ -119,9 +119,7 @@ public class WorldObjectPlacer implements IObjectPlacer {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setTile(int xx, int yy, int zz, TileData<? extends TileState> tile) {
|
||||
BlockState state = world.getBlockAt(xx, yy + world.getMinHeight(), zz).getState();
|
||||
tile.toBukkitTry(state);
|
||||
state.update();
|
||||
public void setTile(int xx, int yy, int zz, TileData tile) {
|
||||
tile.toBukkitTry(world.getBlockAt(xx, yy + world.getMinHeight(), zz));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -107,8 +107,8 @@ public interface EngineMantle extends IObjectPlacer {
|
||||
}
|
||||
|
||||
@Override
|
||||
default void setTile(int x, int y, int z, TileData<? extends TileState> d) {
|
||||
getMantle().set(x, y, z, new TileWrapper(d));
|
||||
default void setTile(int x, int y, int z, TileData d) {
|
||||
getMantle().set(x, y, z, d);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -206,7 +206,7 @@ public class MantleWriter implements IObjectPlacer {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setTile(int xx, int yy, int zz, TileData<? extends TileState> tile) {
|
||||
public void setTile(int xx, int yy, int zz, TileData tile) {
|
||||
getEngineMantle().setTile(xx, yy, zz, tile);
|
||||
}
|
||||
|
||||
|
||||
@@ -44,7 +44,7 @@ public interface IObjectPlacer {
|
||||
|
||||
boolean isDebugSmartBore();
|
||||
|
||||
void setTile(int xx, int yy, int zz, TileData<? extends TileState> tile);
|
||||
void setTile(int xx, int yy, int zz, TileData tile);
|
||||
|
||||
Engine getEngine();
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ package com.volmit.iris.engine.object;
|
||||
import com.volmit.iris.Iris;
|
||||
import com.volmit.iris.core.loader.IrisData;
|
||||
import com.volmit.iris.core.loader.IrisRegistrant;
|
||||
import com.volmit.iris.core.nms.INMS;
|
||||
import com.volmit.iris.engine.data.cache.AtomicCache;
|
||||
import com.volmit.iris.engine.object.annotations.*;
|
||||
import com.volmit.iris.util.collection.KList;
|
||||
@@ -196,17 +197,12 @@ public class IrisBlockData extends IrisRegistrant {
|
||||
});
|
||||
}
|
||||
|
||||
public TileData<?> tryGetTile() {
|
||||
public TileData tryGetTile(IrisData data) {
|
||||
//TODO Do like a registry thing with the tile data registry. Also update the parsing of data to include **block** entities.
|
||||
if (data.containsKey("entitySpawn")) {
|
||||
TileSpawner spawner = new TileSpawner();
|
||||
String name = (String) data.get("entitySpawn");
|
||||
if (name.contains(":"))
|
||||
name = name.split(":")[1];
|
||||
spawner.setEntityType(EntityType.fromName(name));
|
||||
return spawner;
|
||||
}
|
||||
return null;
|
||||
var type = getBlockData(data).getMaterial();
|
||||
if (!INMS.get().hasTile(type))
|
||||
return null;
|
||||
return new TileData().setMaterial(type).setProperties(this.data);
|
||||
}
|
||||
|
||||
private String keyify(String dat) {
|
||||
|
||||
@@ -64,11 +64,11 @@ public class IrisMaterialPalette {
|
||||
return getLayerGenerator(rng, rdata).fit(getBlockData(rdata), x / zoom, y / zoom, z / zoom);
|
||||
}
|
||||
|
||||
public Optional<TileData<?>> getTile(RNG rng, double x, double y, double z, IrisData rdata) {
|
||||
public Optional<TileData> getTile(RNG rng, double x, double y, double z, IrisData rdata) {
|
||||
if (getBlockData(rdata).isEmpty())
|
||||
return Optional.empty();
|
||||
|
||||
TileData<?> tile = getBlockData(rdata).size() == 1 ? palette.get(0).tryGetTile() : palette.getRandom(rng).tryGetTile();
|
||||
TileData tile = getBlockData(rdata).size() == 1 ? palette.get(0).tryGetTile(rdata) : palette.getRandom(rng).tryGetTile(rdata);
|
||||
return tile != null ? Optional.of(tile) : Optional.empty();
|
||||
}
|
||||
|
||||
|
||||
@@ -84,7 +84,7 @@ public class IrisObject extends IrisRegistrant {
|
||||
@Setter
|
||||
protected transient AtomicCache<AxisAlignedBB> aabb = new AtomicCache<>();
|
||||
private KMap<BlockVector, BlockData> blocks;
|
||||
private KMap<BlockVector, TileData<? extends TileState>> states;
|
||||
private KMap<BlockVector, TileData> states;
|
||||
@Getter
|
||||
@Setter
|
||||
private int w;
|
||||
@@ -434,7 +434,7 @@ public class IrisObject extends IrisRegistrant {
|
||||
d.put(new BlockVector(i.getBlockX(), i.getBlockY(), i.getBlockZ()), Objects.requireNonNull(getBlocks().get(i)));
|
||||
}
|
||||
|
||||
KMap<BlockVector, TileData<? extends TileState>> dx = new KMap<>();
|
||||
KMap<BlockVector, TileData> dx = new KMap<>();
|
||||
|
||||
for (BlockVector i : getBlocks().keySet()) {
|
||||
d.put(new BlockVector(i.getBlockX(), i.getBlockY(), i.getBlockZ()), Objects.requireNonNull(getBlocks().get(i)));
|
||||
@@ -476,7 +476,7 @@ public class IrisObject extends IrisRegistrant {
|
||||
} else {
|
||||
BlockData data = block.getBlockData();
|
||||
getBlocks().put(v, data);
|
||||
TileData<? extends TileState> state = TileData.getTileState(block);
|
||||
TileData state = TileData.getTileState(block);
|
||||
if (state != null) {
|
||||
Iris.info("Saved State " + v);
|
||||
getStates().put(v, state);
|
||||
@@ -802,7 +802,7 @@ public class IrisObject extends IrisRegistrant {
|
||||
|
||||
for (BlockVector g : getBlocks().keySet()) {
|
||||
BlockData d;
|
||||
TileData<? extends TileState> tile = null;
|
||||
TileData tile = null;
|
||||
|
||||
try {
|
||||
d = getBlocks().get(g);
|
||||
@@ -842,11 +842,9 @@ public class IrisObject extends IrisRegistrant {
|
||||
else
|
||||
data = newData;
|
||||
|
||||
if (newData.getMaterial() == Material.SPAWNER) {
|
||||
Optional<TileData<?>> t = j.getReplace().getTile(rng, x, y, z, rdata);
|
||||
if (t.isPresent()) {
|
||||
tile = t.get();
|
||||
}
|
||||
Optional<TileData> t = j.getReplace().getTile(rng, x, y, z, rdata);
|
||||
if (t.isPresent()) {
|
||||
tile = t.get();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1044,7 +1042,7 @@ public class IrisObject extends IrisRegistrant {
|
||||
spinx, spiny, spinz));
|
||||
}
|
||||
|
||||
KMap<BlockVector, TileData<? extends TileState>> dx = new KMap<>();
|
||||
KMap<BlockVector, TileData> dx = new KMap<>();
|
||||
|
||||
for (BlockVector i : getStates().keySet()) {
|
||||
dx.put(r.rotate(i.clone(), spinx, spiny, spinz), getStates().get(i));
|
||||
@@ -1062,9 +1060,7 @@ public class IrisObject extends IrisRegistrant {
|
||||
|
||||
if (getStates().containsKey(i)) {
|
||||
Iris.info(Objects.requireNonNull(states.get(i)).toString());
|
||||
BlockState st = b.getState();
|
||||
Objects.requireNonNull(getStates().get(i)).toBukkitTry(st);
|
||||
st.update();
|
||||
Objects.requireNonNull(getStates().get(i)).toBukkitTry(b);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1075,7 +1071,7 @@ public class IrisObject extends IrisRegistrant {
|
||||
b.setBlockData(Objects.requireNonNull(getBlocks().get(i)), false);
|
||||
|
||||
if (getStates().containsKey(i)) {
|
||||
Objects.requireNonNull(getStates().get(i)).toBukkitTry(b.getState());
|
||||
Objects.requireNonNull(getStates().get(i)).toBukkitTry(b);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1084,7 +1080,7 @@ public class IrisObject extends IrisRegistrant {
|
||||
return blocks;
|
||||
}
|
||||
|
||||
public synchronized KMap<BlockVector, TileData<? extends TileState>> getStates() {
|
||||
public synchronized KMap<BlockVector, TileData> getStates() {
|
||||
return states;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,120 +0,0 @@
|
||||
/*
|
||||
* Iris is a World Generator for Minecraft Bukkit Servers
|
||||
* Copyright (c) 2022 Arcane Arts (Volmit Software)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.volmit.iris.engine.object;
|
||||
|
||||
import com.volmit.iris.util.nbt.tag.CompoundTag;
|
||||
import com.volmit.iris.util.nbt.tag.ListTag;
|
||||
import lombok.Data;
|
||||
import org.bukkit.DyeColor;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.block.Banner;
|
||||
import org.bukkit.block.banner.Pattern;
|
||||
import org.bukkit.block.banner.PatternType;
|
||||
import org.bukkit.block.data.BlockData;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class TileBanner implements TileData<Banner> {
|
||||
public static final int id = 2;
|
||||
|
||||
private List<Pattern> patterns = new ArrayList<>();
|
||||
private DyeColor baseColor;
|
||||
|
||||
@Override
|
||||
public String getTileId() {
|
||||
return "minecraft:banner";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isApplicable(BlockData data) {
|
||||
return isBanner(data.getMaterial());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBukkit(Banner banner) {
|
||||
banner.setPatterns(patterns);
|
||||
banner.setBaseColor(baseColor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromBukkit(Banner banner) {
|
||||
this.patterns = banner.getPatterns();
|
||||
this.baseColor = banner.getBaseColor();
|
||||
}
|
||||
|
||||
@SuppressWarnings("MethodDoesntCallSuperMethod")
|
||||
@Override
|
||||
public TileBanner clone() {
|
||||
TileBanner ts = new TileBanner();
|
||||
ts.setBaseColor(getBaseColor());
|
||||
ts.setPatterns(getPatterns());
|
||||
return ts;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBinary(DataOutputStream out) throws IOException {
|
||||
out.writeShort(id);
|
||||
out.writeByte(baseColor.ordinal());
|
||||
out.writeByte(patterns.size());
|
||||
for (Pattern p : patterns) {
|
||||
out.writeByte(p.getColor().ordinal());
|
||||
out.writeByte(p.getPattern().ordinal());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromBinary(DataInputStream in) throws IOException {
|
||||
baseColor = DyeColor.values()[in.readByte()];
|
||||
int listSize = in.readByte();
|
||||
patterns = new ArrayList<>();
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
public CompoundTag toNBT(CompoundTag tag) {
|
||||
@SuppressWarnings("unchecked") ListTag<CompoundTag> listTag = (ListTag<CompoundTag>) ListTag.createUnchecked(CompoundTag.class);
|
||||
for (Pattern p : patterns) {
|
||||
CompoundTag pattern = new CompoundTag();
|
||||
pattern.putString("Pattern", p.getPattern().getIdentifier());
|
||||
pattern.putByte("Color", p.getColor().getDyeData());
|
||||
listTag.add(pattern);
|
||||
}
|
||||
tag.put("Patterns", listTag);
|
||||
return tag;
|
||||
}
|
||||
|
||||
public boolean isBanner(Material material) {
|
||||
return switch (material) {
|
||||
case RED_BANNER, RED_WALL_BANNER, ORANGE_BANNER, ORANGE_WALL_BANNER, YELLOW_BANNER, YELLOW_WALL_BANNER, LIME_BANNER, LIME_WALL_BANNER, GREEN_BANNER, GREEN_WALL_BANNER, CYAN_BANNER, CYAN_WALL_BANNER, LIGHT_BLUE_BANNER, LIGHT_BLUE_WALL_BANNER, BLUE_BANNER, BLUE_WALL_BANNER, PURPLE_BANNER, PURPLE_WALL_BANNER, MAGENTA_BANNER, MAGENTA_WALL_BANNER, PINK_BANNER, PINK_WALL_BANNER, WHITE_BANNER, WHITE_WALL_BANNER, LIGHT_GRAY_BANNER, LIGHT_GRAY_WALL_BANNER, GRAY_BANNER, GRAY_WALL_BANNER, BLACK_BANNER, BLACK_WALL_BANNER, BROWN_BANNER, BROWN_WALL_BANNER ->
|
||||
true;
|
||||
default -> false;
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -18,9 +18,14 @@
|
||||
|
||||
package com.volmit.iris.engine.object;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.volmit.iris.Iris;
|
||||
import com.volmit.iris.util.collection.KList;
|
||||
import com.volmit.iris.util.nbt.tag.CompoundTag;
|
||||
import com.volmit.iris.core.nms.INMS;
|
||||
import com.volmit.iris.util.collection.KMap;
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.BlockState;
|
||||
import org.bukkit.block.TileState;
|
||||
@@ -29,73 +34,70 @@ import org.bukkit.block.data.BlockData;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
@Data
|
||||
@SuppressWarnings("ALL")
|
||||
public interface TileData<T extends TileState> extends Cloneable {
|
||||
@Accessors(chain = true)
|
||||
public class TileData implements Cloneable {
|
||||
private static final Gson gson = new GsonBuilder().disableHtmlEscaping().setLenient().create();
|
||||
|
||||
static final KList<TileData<? extends TileState>> registry = setup();
|
||||
private Material material = null;
|
||||
private KMap<String, Object> properties = new KMap<>();
|
||||
|
||||
static KList<TileData<? extends TileState>> setup() {
|
||||
KList<TileData<? extends TileState>> registry = new KList<>();
|
||||
|
||||
registry.add(new TileSign());
|
||||
registry.add(new TileSpawner());
|
||||
registry.add(new TileBanner());
|
||||
|
||||
return registry;
|
||||
}
|
||||
|
||||
static TileData<? extends TileState> read(DataInputStream s) throws IOException {
|
||||
try {
|
||||
int id = s.readShort();
|
||||
@SuppressWarnings("unchecked") TileData<? extends TileState> d = registry.get(id).getClass().getConstructor().newInstance();
|
||||
d.fromBinary(s);
|
||||
return d;
|
||||
} catch (InvocationTargetException | InstantiationException | IllegalAccessException |
|
||||
NoSuchMethodException e) {
|
||||
throw new IOException("Failed to create TileData instance due to missing type registrar!");
|
||||
}
|
||||
}
|
||||
|
||||
static boolean setTileState(Block block, TileData<? extends TileState> data) {
|
||||
public static boolean setTileState(Block block, TileData data) {
|
||||
if (block.getState() instanceof TileState && data.isApplicable(block.getBlockData()))
|
||||
return data.toBukkitTry(block.getState());
|
||||
return data.toBukkitTry(block);
|
||||
return false;
|
||||
}
|
||||
|
||||
static TileData<? extends TileState> getTileState(Block block) {
|
||||
for (TileData<? extends TileState> i : registry) {
|
||||
BlockData data = block.getBlockData();
|
||||
|
||||
if (i.isApplicable(data)) {
|
||||
try {
|
||||
@SuppressWarnings("unchecked") TileData<? extends TileState> s = i.getClass().getConstructor().newInstance();
|
||||
s.fromBukkitTry(block.getState());
|
||||
return s;
|
||||
} catch (Throwable e) {
|
||||
Iris.reportError(e);
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
public static TileData getTileState(Block block) {
|
||||
if (!INMS.get().hasTile(block.getType()))
|
||||
return null;
|
||||
return new TileData().fromBukkit(block);
|
||||
}
|
||||
|
||||
String getTileId();
|
||||
public static TileData read(DataInputStream in) throws IOException {
|
||||
TileData d = new TileData();
|
||||
d.material = Material.matchMaterial(in.readUTF());
|
||||
d.properties = gson.fromJson(in.readUTF(), KMap.class);
|
||||
return d;
|
||||
}
|
||||
|
||||
boolean isApplicable(BlockData data);
|
||||
public boolean isApplicable(BlockData data) {
|
||||
return material != null && data.getMaterial() == material;
|
||||
}
|
||||
|
||||
void toBukkit(T t);
|
||||
public void toBukkit(Block block) {
|
||||
if (material == null) throw new IllegalStateException("Material not set");
|
||||
if (block.getType() != material)
|
||||
throw new IllegalStateException("Material mismatch: " + block.getType() + " vs " + material);
|
||||
INMS.get().deserializeTile(properties, block.getLocation());
|
||||
}
|
||||
|
||||
void fromBukkit(T t);
|
||||
public TileData fromBukkit(Block block) {
|
||||
if (material != null && block.getType() != material)
|
||||
throw new IllegalStateException("Material mismatch: " + block.getType() + " vs " + material);
|
||||
if (material == null) material = block.getType();
|
||||
properties = INMS.get().serializeTile(block.getLocation());
|
||||
return this;
|
||||
}
|
||||
|
||||
default boolean toBukkitTry(BlockState t) {
|
||||
public boolean toBukkitTry(Block block) {
|
||||
try {
|
||||
//noinspection unchecked
|
||||
toBukkit((T) t);
|
||||
t.update();
|
||||
toBukkit(block);
|
||||
return true;
|
||||
} catch (Throwable e) {
|
||||
Iris.reportError(e);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean fromBukkitTry(Block block) {
|
||||
try {
|
||||
//noinspection unchecked
|
||||
fromBukkit(block);
|
||||
return true;
|
||||
} catch (Throwable e) {
|
||||
Iris.reportError(e);
|
||||
@@ -105,24 +107,16 @@ public interface TileData<T extends TileState> extends Cloneable {
|
||||
return false;
|
||||
}
|
||||
|
||||
default boolean fromBukkitTry(BlockState t) {
|
||||
try {
|
||||
//noinspection unchecked
|
||||
fromBukkit((T) t);
|
||||
return true;
|
||||
} catch (Throwable e) {
|
||||
Iris.reportError(e);
|
||||
|
||||
}
|
||||
|
||||
return false;
|
||||
public void toBinary(DataOutputStream out) throws IOException {
|
||||
out.writeUTF(material == null ? "" : material.getKey().toString());
|
||||
out.writeUTF(gson.toJson(properties));
|
||||
}
|
||||
|
||||
CompoundTag toNBT(CompoundTag parent);
|
||||
|
||||
void toBinary(DataOutputStream out) throws IOException;
|
||||
|
||||
void fromBinary(DataInputStream in) throws IOException;
|
||||
|
||||
TileData<T> clone();
|
||||
@Override
|
||||
public TileData clone() {
|
||||
var clone = new TileData();
|
||||
clone.material = material;
|
||||
clone.properties = properties.copy(); //TODO make a deep copy
|
||||
return clone;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,109 +0,0 @@
|
||||
/*
|
||||
* Iris is a World Generator for Minecraft Bukkit Servers
|
||||
* Copyright (c) 2022 Arcane Arts (Volmit Software)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.volmit.iris.engine.object;
|
||||
|
||||
import com.volmit.iris.util.nbt.tag.CompoundTag;
|
||||
import lombok.Data;
|
||||
import org.bukkit.DyeColor;
|
||||
import org.bukkit.block.Sign;
|
||||
import org.bukkit.block.data.BlockData;
|
||||
import org.bukkit.block.data.type.WallSign;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
@Data
|
||||
public class TileSign implements TileData<Sign> {
|
||||
public static final int id = 0;
|
||||
private String line1;
|
||||
private String line2;
|
||||
private String line3;
|
||||
private String line4;
|
||||
private DyeColor dyeColor;
|
||||
|
||||
@Override
|
||||
public String getTileId() {
|
||||
return "minecraft:sign";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isApplicable(BlockData data) {
|
||||
return data instanceof org.bukkit.block.data.type.Sign || data instanceof WallSign;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBukkit(Sign t) {
|
||||
t.setLine(0, line1);
|
||||
t.setLine(1, line2);
|
||||
t.setLine(2, line3);
|
||||
t.setLine(3, line4);
|
||||
t.setColor(dyeColor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromBukkit(Sign sign) {
|
||||
line1 = sign.getLine(0);
|
||||
line2 = sign.getLine(1);
|
||||
line3 = sign.getLine(2);
|
||||
line4 = sign.getLine(3);
|
||||
dyeColor = sign.getColor();
|
||||
}
|
||||
|
||||
@SuppressWarnings("MethodDoesntCallSuperMethod")
|
||||
@Override
|
||||
public TileSign clone() {
|
||||
TileSign ts = new TileSign();
|
||||
ts.setDyeColor(getDyeColor());
|
||||
ts.setLine1(getLine1());
|
||||
ts.setLine2(getLine2());
|
||||
ts.setLine3(getLine3());
|
||||
ts.setLine4(getLine4());
|
||||
return ts;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBinary(DataOutputStream out) throws IOException {
|
||||
out.writeShort(id);
|
||||
out.writeUTF(line1);
|
||||
out.writeUTF(line2);
|
||||
out.writeUTF(line3);
|
||||
out.writeUTF(line4);
|
||||
out.writeByte(dyeColor.ordinal());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromBinary(DataInputStream in) throws IOException {
|
||||
line1 = in.readUTF();
|
||||
line2 = in.readUTF();
|
||||
line3 = in.readUTF();
|
||||
line4 = in.readUTF();
|
||||
dyeColor = DyeColor.values()[in.readByte()];
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompoundTag toNBT(CompoundTag tag) {
|
||||
tag.putString("Text1", line1);
|
||||
tag.putString("Text2", line2);
|
||||
tag.putString("Text3", line3);
|
||||
tag.putString("Text4", line4);
|
||||
tag.putString("Color", dyeColor.name().toLowerCase());
|
||||
return tag;
|
||||
}
|
||||
}
|
||||
@@ -1,91 +0,0 @@
|
||||
/*
|
||||
* Iris is a World Generator for Minecraft Bukkit Servers
|
||||
* Copyright (c) 2022 Arcane Arts (Volmit Software)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.volmit.iris.engine.object;
|
||||
|
||||
import com.volmit.iris.util.nbt.tag.CompoundTag;
|
||||
import com.volmit.iris.util.nbt.tag.ListTag;
|
||||
import lombok.Data;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.block.CreatureSpawner;
|
||||
import org.bukkit.block.data.BlockData;
|
||||
import org.bukkit.entity.EntityType;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
@Data
|
||||
public class TileSpawner implements TileData<CreatureSpawner> {
|
||||
|
||||
public static final int id = 1;
|
||||
|
||||
private EntityType entityType;
|
||||
|
||||
@Override
|
||||
public String getTileId() {
|
||||
return "minecraft:mob_spawner";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isApplicable(BlockData data) {
|
||||
return data.getMaterial() == Material.SPAWNER;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBukkit(CreatureSpawner t) {
|
||||
t.setSpawnedType(entityType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromBukkit(CreatureSpawner sign) {
|
||||
entityType = sign.getSpawnedType();
|
||||
}
|
||||
|
||||
@SuppressWarnings("MethodDoesntCallSuperMethod")
|
||||
@Override
|
||||
public TileSpawner clone() {
|
||||
TileSpawner ts = new TileSpawner();
|
||||
ts.setEntityType(getEntityType());
|
||||
return ts;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBinary(DataOutputStream out) throws IOException {
|
||||
out.writeShort(id);
|
||||
out.writeShort(entityType.ordinal());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromBinary(DataInputStream in) throws IOException {
|
||||
entityType = EntityType.values()[in.readShort()];
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompoundTag toNBT(CompoundTag parent) {
|
||||
@SuppressWarnings("unchecked") ListTag<CompoundTag> potentials = (ListTag<CompoundTag>) ListTag.createUnchecked(CompoundTag.class);
|
||||
CompoundTag t = new CompoundTag();
|
||||
CompoundTag ent = new CompoundTag();
|
||||
ent.putString("id", entityType.getKey().toString());
|
||||
t.put("Entity", ent);
|
||||
t.putInt("Weight", 1);
|
||||
potentials.add(t);
|
||||
parent.put("SpawnPotentials", potentials);
|
||||
return parent;
|
||||
}
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
/*
|
||||
* Iris is a World Generator for Minecraft Bukkit Servers
|
||||
* Copyright (c) 2022 Arcane Arts (Volmit Software)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.volmit.iris.util.matter;
|
||||
|
||||
import com.volmit.iris.engine.object.TileData;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class TileWrapper {
|
||||
private final TileData<?> data;
|
||||
}
|
||||
@@ -19,6 +19,7 @@
|
||||
package com.volmit.iris.util.matter;
|
||||
|
||||
import com.volmit.iris.Iris;
|
||||
import com.volmit.iris.engine.object.TileData;
|
||||
import com.volmit.iris.util.data.Cuboid;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.block.data.BlockData;
|
||||
@@ -33,8 +34,8 @@ public class WorldMatter {
|
||||
matter.slice(MatterEntityGroup.class).writeInto(at);
|
||||
}
|
||||
|
||||
if (matter.hasSlice(TileWrapper.class)) {
|
||||
matter.slice(TileWrapper.class).writeInto(at);
|
||||
if (matter.hasSlice(TileData.class)) {
|
||||
matter.slice(TileData.class).writeInto(at);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,7 +46,7 @@ public class WorldMatter {
|
||||
s.getHeader().setAuthor(author);
|
||||
s.slice(BlockData.class).readFrom(c.getLowerNE());
|
||||
s.slice(MatterEntityGroup.class).readFrom(c.getLowerNE());
|
||||
s.slice(TileWrapper.class).readFrom(c.getLowerNE());
|
||||
s.slice(TileData.class).readFrom(c.getLowerNE());
|
||||
s.trimSlices();
|
||||
|
||||
return s;
|
||||
|
||||
@@ -18,13 +18,9 @@
|
||||
|
||||
package com.volmit.iris.util.matter.slices;
|
||||
|
||||
import com.volmit.iris.Iris;
|
||||
import com.volmit.iris.core.nms.INMS;
|
||||
import com.volmit.iris.engine.object.TileData;
|
||||
import com.volmit.iris.util.data.palette.Palette;
|
||||
import com.volmit.iris.util.matter.Sliced;
|
||||
import com.volmit.iris.util.matter.TileWrapper;
|
||||
import com.volmit.iris.util.nbt.tag.CompoundTag;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
|
||||
@@ -34,47 +30,28 @@ import java.io.IOException;
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
@Sliced
|
||||
public class TileMatter extends RawMatter<TileWrapper> {
|
||||
public class TileMatter extends RawMatter<TileData> {
|
||||
|
||||
public TileMatter() {
|
||||
this(1, 1, 1);
|
||||
}
|
||||
|
||||
public TileMatter(int width, int height, int depth) {
|
||||
super(width, height, depth, TileWrapper.class);
|
||||
registerWriter(World.class, (w, d, x, y, z) -> {
|
||||
CompoundTag tag = commonNbt(x, y, z, d.getData().getTileId());
|
||||
INMS.get().deserializeTile(d.getData().toNBT(d.getData().toNBT(tag)), new Location(w, x, y, z));
|
||||
Iris.warn("S: " + tag);
|
||||
});
|
||||
registerReader(World.class, (w, x, y, z) -> {
|
||||
TileData d = TileData.getTileState(w.getBlockAt(new Location(w, x, y, z)));
|
||||
if (d == null)
|
||||
return null;
|
||||
return new TileWrapper(d);
|
||||
});
|
||||
super(width, height, depth, TileData.class);
|
||||
registerWriter(World.class, (w, d, x, y, z) -> TileData.setTileState(w.getBlockAt(new Location(w, x, y, z)), d));
|
||||
registerReader(World.class, (w, x, y, z) -> TileData.getTileState(w.getBlockAt(new Location(w, x, y, z))));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Palette<TileWrapper> getGlobalPalette() {
|
||||
public Palette<TileData> getGlobalPalette() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public void writeNode(TileWrapper b, DataOutputStream dos) throws IOException {
|
||||
b.getData().toBinary(dos);
|
||||
public void writeNode(TileData b, DataOutputStream dos) throws IOException {
|
||||
b.toBinary(dos);
|
||||
}
|
||||
|
||||
public TileWrapper readNode(DataInputStream din) throws IOException {
|
||||
return new TileWrapper(TileData.read(din));
|
||||
}
|
||||
|
||||
private CompoundTag commonNbt(int x, int y, int z, String mobId) {
|
||||
CompoundTag tag = new CompoundTag();
|
||||
tag.putInt("x", x);
|
||||
tag.putInt("y", y);
|
||||
tag.putInt("z", z);
|
||||
tag.putBoolean("keepPacked", false);
|
||||
tag.putString("id", mobId);
|
||||
return tag;
|
||||
public TileData readNode(DataInputStream din) throws IOException {
|
||||
return TileData.read(din);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user