This commit is contained in:
Daniel Mills 2021-08-05 21:24:52 -04:00
parent e636a6ad58
commit f091256edb
11 changed files with 191 additions and 59 deletions

View File

@ -26,6 +26,8 @@ import com.volmit.iris.util.data.Cuboid;
import com.volmit.iris.util.format.C; import com.volmit.iris.util.format.C;
import com.volmit.iris.util.math.M; import com.volmit.iris.util.math.M;
import com.volmit.iris.util.matter.IrisMatter; import com.volmit.iris.util.matter.IrisMatter;
import com.volmit.iris.util.matter.Matter;
import com.volmit.iris.util.matter.WorldMatter;
import com.volmit.iris.util.plugin.VolmitSender; import com.volmit.iris.util.plugin.VolmitSender;
import org.bukkit.*; import org.bukkit.*;
import org.bukkit.block.Block; import org.bukkit.block.Block;
@ -232,26 +234,15 @@ public class WandManager implements Listener {
* @param wand The wand itemstack * @param wand The wand itemstack
* @return The new object * @return The new object
*/ */
public static IrisMatter createSchematic(Player p, ItemStack wand) { public static Matter createMatterSchem(Player p, ItemStack wand) {
if (!isWand(wand)) { if (!isWand(wand)) {
return null; return null;
} }
try { try {
Location[] f = getCuboid(wand); Location[] f = getCuboid(wand);
Cuboid c = new Cuboid(f[0], f[1]);
IrisMatter s = new IrisMatter(c.getSizeX(), c.getSizeY(), c.getSizeZ());
Iris.info(s.getWidth() + " " + s.getHeight() + " " + s.getDepth());
s.getHeader().setAuthor(p.getName());
s.slice(BlockData.class)
.readFrom(c.getWorld(),
c.getLowerNE().getBlockX(),
c.getLowerNE().getBlockY(), c.getLowerNE().getBlockZ());
Iris.info("Slices: " + s.getSliceMap().size()); return WorldMatter.createMatter(p.getName(), f[0], f[1]);
Iris.info("Entries " + s.getSlice(BlockData.class).getCount());
return s;
} catch (Throwable e) { } catch (Throwable e) {
e.printStackTrace(); e.printStackTrace();
Iris.reportError(e); Iris.reportError(e);

View File

@ -26,6 +26,7 @@ import com.volmit.iris.engine.object.common.IObjectPlacer;
import com.volmit.iris.engine.object.tile.TileData; import com.volmit.iris.engine.object.tile.TileData;
import com.volmit.iris.util.collection.KList; import com.volmit.iris.util.collection.KList;
import com.volmit.iris.util.matter.Matter; import com.volmit.iris.util.matter.Matter;
import com.volmit.iris.util.matter.WorldMatter;
import com.volmit.iris.util.plugin.MortarCommand; import com.volmit.iris.util.plugin.MortarCommand;
import com.volmit.iris.util.plugin.VolmitSender; import com.volmit.iris.util.plugin.VolmitSender;
import org.bukkit.HeightMap; import org.bukkit.HeightMap;
@ -84,8 +85,7 @@ public class CommandIrisObjectPasteMatter extends MortarCommand {
File f = new File(args[0]); File f = new File(args[0]);
try { try {
Matter matter = Matter.read(f); Matter matter = Matter.read(f);
matter.slice(BlockData.class) WorldMatter.placeMatter(matter, p.getLocation());
.writeInto(p.getWorld(), p.getLocation().getBlockX(), p.getLocation().getBlockY(), p.getLocation().getBlockZ());
} catch (Throwable e) { } catch (Throwable e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@ -23,6 +23,7 @@ import com.volmit.iris.core.IrisSettings;
import com.volmit.iris.core.WandManager; import com.volmit.iris.core.WandManager;
import com.volmit.iris.util.collection.KList; import com.volmit.iris.util.collection.KList;
import com.volmit.iris.util.matter.IrisMatter; import com.volmit.iris.util.matter.IrisMatter;
import com.volmit.iris.util.matter.Matter;
import com.volmit.iris.util.plugin.MortarCommand; import com.volmit.iris.util.plugin.MortarCommand;
import com.volmit.iris.util.plugin.VolmitSender; import com.volmit.iris.util.plugin.VolmitSender;
import org.bukkit.Sound; import org.bukkit.Sound;
@ -75,7 +76,7 @@ public class CommandIrisObjectSaveMatter extends MortarCommand {
Player p = sender.player(); Player p = sender.player();
ItemStack wand = p.getInventory().getItemInMainHand(); ItemStack wand = p.getInventory().getItemInMainHand();
IrisMatter o = WandManager.createSchematic(p, wand); Matter o = WandManager.createMatterSchem(p, wand);
File file = Iris.proj.getWorkspaceFile(args[0], "objects", args[1] + ".iob"); File file = Iris.proj.getWorkspaceFile(args[0], "objects", args[1] + ".iob");
if (file.exists()) { if (file.exists()) {

View File

@ -19,9 +19,11 @@
package com.volmit.iris.core.nms.v1X; package com.volmit.iris.core.nms.v1X;
import com.volmit.iris.core.nms.INMSBinding; import com.volmit.iris.core.nms.INMSBinding;
import com.volmit.iris.util.nbt.tag.CompoundTag;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.block.Biome; import org.bukkit.block.Biome;
import org.bukkit.entity.Entity;
import org.bukkit.generator.ChunkGenerator; import org.bukkit.generator.ChunkGenerator;
public class NMSBinding1X implements INMSBinding { public class NMSBinding1X implements INMSBinding {
@ -42,6 +44,31 @@ public class NMSBinding1X implements INMSBinding {
return false; return false;
} }
@Override
public boolean hasTile(Location l) {
return false;
}
@Override
public CompoundTag serializeTile(Location location) {
return null;
}
@Override
public void deserializeTile(CompoundTag s, Location newPosition) {
}
@Override
public CompoundTag serializeEntity(Entity location) {
return null;
}
@Override
public Entity deserializeEntity(CompoundTag s, Location newPosition) {
return null;
}
@Override @Override
public boolean supportsCustomHeight() { public boolean supportsCustomHeight() {
return supportsCustomHeight; return supportsCustomHeight;

View File

@ -20,10 +20,12 @@ package com.volmit.iris.util.hunk.storage;
import com.volmit.iris.util.collection.KMap; import com.volmit.iris.util.collection.KMap;
import com.volmit.iris.util.function.Consumer4; import com.volmit.iris.util.function.Consumer4;
import com.volmit.iris.util.function.Consumer4IO;
import com.volmit.iris.util.hunk.Hunk; import com.volmit.iris.util.hunk.Hunk;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import java.io.IOException;
import java.util.Map; import java.util.Map;
@SuppressWarnings({"DefaultAnnotationParam", "Lombok"}) @SuppressWarnings({"DefaultAnnotationParam", "Lombok"})
@ -69,6 +71,20 @@ public class MappedHunk<T> extends StorageHunk<T> implements Hunk<T> {
return this; return this;
} }
@Override
public synchronized Hunk<T> iterateSyncIO(Consumer4IO<Integer, Integer, Integer, T> c) throws IOException {
int idx, z;
for (Map.Entry<Integer, T> g : data.entrySet()) {
idx = g.getKey();
z = idx / (getWidth() * getHeight());
idx -= (z * getWidth() * getHeight());
c.accept(idx % getWidth(), idx / getWidth(), z, g.getValue());
}
return this;
}
@Override @Override
public void empty(T b) { public void empty(T b) {
data.clear(); data.clear();

View File

@ -19,6 +19,7 @@
package com.volmit.iris.util.matter; package com.volmit.iris.util.matter;
import com.volmit.iris.engine.object.basic.IrisPosition; import com.volmit.iris.engine.object.basic.IrisPosition;
import com.volmit.iris.util.collection.KSet;
import com.volmit.iris.util.data.Varint; import com.volmit.iris.util.data.Varint;
import com.volmit.iris.util.hunk.Hunk; import com.volmit.iris.util.hunk.Hunk;
import com.volmit.iris.util.math.BlockPosition; import com.volmit.iris.util.math.BlockPosition;
@ -240,6 +241,35 @@ public interface Matter {
gzo.close(); gzo.close();
} }
/**
* Remove any slices that are empty
*/
default void trimSlices()
{
Set<Class<?>> drop = null;
for(Class<?> i : getSliceTypes())
{
if(getSlice(i).getCount() == 0)
{
if(drop == null)
{
drop = new KSet<>();
}
drop.add(i);
}
}
if(drop != null)
{
for(Class<?> i : drop)
{
deleteSlice(i);
}
}
}
/** /**
* Writes the data to the output stream. The data will be flushed to the provided output * Writes the data to the output stream. The data will be flushed to the provided output
* stream however the provided stream will NOT BE CLOSED, so be sure to actually close it * stream however the provided stream will NOT BE CLOSED, so be sure to actually close it
@ -248,12 +278,12 @@ public interface Matter {
* @throws IOException shit happens yo * @throws IOException shit happens yo
*/ */
default void write(OutputStream out) throws IOException { default void write(OutputStream out) throws IOException {
trimSlices();
DataOutputStream dos = new DataOutputStream(out); DataOutputStream dos = new DataOutputStream(out);
// Write size
Varint.writeUnsignedVarInt(getWidth(), dos); Varint.writeUnsignedVarInt(getWidth(), dos);
Varint.writeUnsignedVarInt(getHeight(), dos); Varint.writeUnsignedVarInt(getHeight(), dos);
Varint.writeUnsignedVarInt(getDepth(), dos); Varint.writeUnsignedVarInt(getDepth(), dos);
dos.writeByte(getSliceTypes().size() + Byte.MIN_VALUE); dos.writeByte(getSliceTypes().size());
getHeader().write(dos); getHeader().write(dos);
for (Class<?> i : getSliceTypes()) { for (Class<?> i : getSliceTypes()) {
@ -286,19 +316,28 @@ public interface Matter {
*/ */
static Matter read(InputStream in, Function<BlockPosition, Matter> matterFactory) throws IOException, ClassNotFoundException { static Matter read(InputStream in, Function<BlockPosition, Matter> matterFactory) throws IOException, ClassNotFoundException {
DataInputStream din = new DataInputStream(in); DataInputStream din = new DataInputStream(in);
// Read size into new matter object
Matter matter = matterFactory.apply(new BlockPosition( Matter matter = matterFactory.apply(new BlockPosition(
Varint.readUnsignedVarInt(din), Varint.readUnsignedVarInt(din),
Varint.readUnsignedVarInt(din), Varint.readUnsignedVarInt(din),
Varint.readUnsignedVarInt(din))); Varint.readUnsignedVarInt(din)));
int sliceCount = din.readByte() - Byte.MIN_VALUE; int sliceCount = din.readByte();
matter.getHeader().read(din); matter.getHeader().read(din);
while (sliceCount-- > 0) { while (sliceCount-- > 0) {
Class<?> type = Class.forName(din.readUTF()); String cn = din.readUTF();
MatterSlice<?> slice = matter.createSlice(type, matter); try
slice.read(din); {
matter.putSlice(type, slice); Class<?> type = Class.forName(cn);
MatterSlice<?> slice = matter.createSlice(type, matter);
slice.read(din);
matter.putSlice(type, slice);
}
catch(Throwable e)
{
e.printStackTrace();
throw new IOException("Can't read class '" + cn + "' (slice count reverse at " + sliceCount + ")");
}
} }
return matter; return matter;

View File

@ -18,10 +18,12 @@
package com.volmit.iris.util.matter; package com.volmit.iris.util.matter;
import com.volmit.iris.Iris;
import com.volmit.iris.engine.data.cache.Cache; import com.volmit.iris.engine.data.cache.Cache;
import com.volmit.iris.util.data.Varint; import com.volmit.iris.util.data.Varint;
import com.volmit.iris.util.hunk.Hunk; import com.volmit.iris.util.hunk.Hunk;
import com.volmit.iris.util.hunk.storage.MappedHunk; import com.volmit.iris.util.hunk.storage.MappedHunk;
import org.bukkit.Location;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.block.data.BlockData; import org.bukkit.block.data.BlockData;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
@ -55,6 +57,10 @@ public interface MatterSlice<T> extends Hunk<T> {
return c; return c;
} }
default boolean writeInto(Location location) {
return writeInto(location.getWorld(), location.getBlockX(), location.getBlockY(), location.getBlockZ());
}
default <W> boolean writeInto(W w, int x, int y, int z) { default <W> boolean writeInto(W w, int x, int y, int z) {
MatterWriter<W, T> injector = (MatterWriter<W, T>) writeInto(getClass(w)); MatterWriter<W, T> injector = (MatterWriter<W, T>) writeInto(getClass(w));
@ -62,22 +68,15 @@ public interface MatterSlice<T> extends Hunk<T> {
return false; return false;
} }
for (int i = x; i < x + getWidth(); i++) { iterateSync((a,b,c,t) -> injector.writeMatter(w, t, a+x, b+y, c+z));
for (int j = y; j < y + getHeight(); j++) {
for (int k = z; k < z + getDepth(); k++) {
T g = get(i - x, j - y, k - z);
if(g != null)
{
injector.writeMatter(w, g, i, j, k);
}
}
}
}
return true; return true;
} }
default boolean readFrom(Location location) {
return readFrom(location.getWorld(), location.getBlockX(), location.getBlockY(), location.getBlockZ());
}
default <W> boolean readFrom(W w, int x, int y, int z) { default <W> boolean readFrom(W w, int x, int y, int z) {
MatterReader<W, T> ejector = (MatterReader<W, T>) readFrom(getClass(w)); MatterReader<W, T> ejector = (MatterReader<W, T>) readFrom(getClass(w));

View File

@ -0,0 +1,58 @@
/*
* Iris is a World Generator for Minecraft Bukkit Servers
* Copyright (c) 2021 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.Iris;
import com.volmit.iris.util.data.Cuboid;
import org.bukkit.Location;
import org.bukkit.block.data.BlockData;
public class WorldMatter {
public static void placeMatter(Matter matter, Location at)
{
if(matter.hasSlice(BlockData.class))
{
matter.slice(BlockData.class).writeInto(at);
}
if(matter.hasSlice(MatterEntityGroup.class))
{
matter.slice(MatterEntityGroup.class).writeInto(at);
}
if(matter.hasSlice(MatterTile.class))
{
matter.slice(MatterTile.class).writeInto(at);
}
}
public static Matter createMatter(String author, Location a, Location b)
{
Cuboid c = new Cuboid(a, b);
Matter s = new IrisMatter(c.getSizeX(), c.getSizeY(), c.getSizeZ());
Iris.info(s.getWidth() + " " + s.getHeight() + " " + s.getDepth());
s.getHeader().setAuthor(author);
s.slice(BlockData.class).readFrom(c.getLowerNE());
s.slice(MatterEntityGroup.class).readFrom(c.getLowerNE());
s.slice(MatterTile.class).readFrom(c.getLowerNE());
s.trimSlices();
return s;
}
}

View File

@ -18,6 +18,7 @@
package com.volmit.iris.util.matter.slices; package com.volmit.iris.util.matter.slices;
import com.volmit.iris.Iris;
import com.volmit.iris.core.nms.INMS; import com.volmit.iris.core.nms.INMS;
import com.volmit.iris.engine.object.basic.IrisPosition; import com.volmit.iris.engine.object.basic.IrisPosition;
import com.volmit.iris.util.collection.KList; import com.volmit.iris.util.collection.KList;
@ -41,6 +42,10 @@ public class EntityMatter extends RawMatter<MatterEntityGroup> {
public EntityMatter() { public EntityMatter() {
this(1, 1, 1); this(1, 1, 1);
}
public EntityMatter(int width, int height, int depth) {
super(width, height, depth, MatterEntityGroup.class);
registerWriter(World.class, ((w, d, x, y, z) -> { registerWriter(World.class, ((w, d, x, y, z) -> {
for(MatterEntity i : d.getEntities()) for(MatterEntity i : d.getEntities())
{ {
@ -57,10 +62,10 @@ public class EntityMatter extends RawMatter<MatterEntityGroup> {
for(Entity i : entities) for(Entity i : entities)
{ {
g.getEntities().add(new MatterEntity( g.getEntities().add(new MatterEntity(
Math.abs(i.getLocation().getX()) - Math.abs(i.getLocation().getBlockX()), Math.abs(i.getLocation().getX()) - Math.abs(i.getLocation().getBlockX()),
Math.abs(i.getLocation().getY()) - Math.abs(i.getLocation().getBlockY()), Math.abs(i.getLocation().getY()) - Math.abs(i.getLocation().getBlockY()),
Math.abs(i.getLocation().getZ()) - Math.abs(i.getLocation().getBlockZ()), Math.abs(i.getLocation().getZ()) - Math.abs(i.getLocation().getBlockZ()),
INMS.get().serializeEntity(i) INMS.get().serializeEntity(i)
)); ));
} }
@ -71,10 +76,6 @@ public class EntityMatter extends RawMatter<MatterEntityGroup> {
}); });
} }
public EntityMatter(int width, int height, int depth) {
super(width, height, depth, MatterEntityGroup.class);
}
/** /**
* The readFrom is overridden only if W is a Bukkit World, instead of looping * The readFrom is overridden only if W is a Bukkit World, instead of looping
* across every block position, we simply use getNearbyEntities and cache each * across every block position, we simply use getNearbyEntities and cache each
@ -94,9 +95,9 @@ public class EntityMatter extends RawMatter<MatterEntityGroup> {
return super.readFrom(w, x, y, z); return super.readFrom(w, x, y, z);
} }
MatterReader<W, MatterEntityGroup> ejector = (MatterReader<W, MatterEntityGroup>) readFrom(getClass(w)); MatterReader<W, MatterEntityGroup> reader = (MatterReader<W, MatterEntityGroup>) readFrom(World.class);
if (ejector == null) { if (reader == null) {
return false; return false;
} }
@ -110,7 +111,7 @@ public class EntityMatter extends RawMatter<MatterEntityGroup> {
for(IrisPosition i : entityCache.keySet()) for(IrisPosition i : entityCache.keySet())
{ {
MatterEntityGroup g = ejector.readMatter(w, i.getX(), i.getY(), i.getZ()); MatterEntityGroup g = reader.readMatter(w, i.getX(), i.getY(), i.getZ());
if(g != null) if(g != null)
{ {

View File

@ -32,32 +32,32 @@ import java.io.IOException;
public abstract class RawMatter<T> extends MappedHunk<T> implements MatterSlice<T> { public abstract class RawMatter<T> extends MappedHunk<T> implements MatterSlice<T> {
@Getter @Getter
private final Class<T> type; private final Class<T> type;
private final KMap<Class<?>, MatterWriter<?, T>> injectors; protected final KMap<Class<?>, MatterWriter<?, T>> writers;
private final KMap<Class<?>, MatterReader<?, T>> ejectors; protected final KMap<Class<?>, MatterReader<?, T>> readers;
public RawMatter(int width, int height, int depth, Class<T> type) { public RawMatter(int width, int height, int depth, Class<T> type) {
super(width, height, depth); super(width, height, depth);
injectors = new KMap<>(); writers = new KMap<>();
ejectors = new KMap<>(); readers = new KMap<>();
this.type = type; this.type = type;
} }
protected <W> void registerWriter(Class<W> mediumType, MatterWriter<W, T> injector) { protected <W> void registerWriter(Class<W> mediumType, MatterWriter<W, T> injector) {
injectors.put(mediumType, injector); writers.put(mediumType, injector);
} }
protected <W> void registerReader(Class<W> mediumType, MatterReader<W, T> injector) { protected <W> void registerReader(Class<W> mediumType, MatterReader<W, T> injector) {
ejectors.put(mediumType, injector); readers.put(mediumType, injector);
} }
@Override @Override
public <W> MatterWriter<W, T> writeInto(Class<W> mediumType) { public <W> MatterWriter<W, T> writeInto(Class<W> mediumType) {
return (MatterWriter<W, T>) injectors.get(mediumType); return (MatterWriter<W, T>) writers.get(mediumType);
} }
@Override @Override
public <W> MatterReader<W, T> readFrom(Class<W> mediumType) { public <W> MatterReader<W, T> readFrom(Class<W> mediumType) {
return (MatterReader<W, T>) ejectors.get(mediumType); return (MatterReader<W, T>) readers.get(mediumType);
} }
@Override @Override

View File

@ -36,6 +36,10 @@ import java.io.IOException;
public class TileMatter extends RawMatter<MatterTile> { public class TileMatter extends RawMatter<MatterTile> {
public TileMatter() { public TileMatter() {
this(1, 1, 1); this(1, 1, 1);
}
public TileMatter(int width, int height, int depth) {
super(width, height, depth, MatterTile.class);
registerWriter(World.class, ((w, d, x, y, z) -> INMS.get().deserializeTile(d.getTileData(), new Location(w, x, y, z)))); registerWriter(World.class, ((w, d, x, y, z) -> INMS.get().deserializeTile(d.getTileData(), new Location(w, x, y, z))));
registerReader(World.class, (w, x, y, z) -> { registerReader(World.class, (w, x, y, z) -> {
Location l = new Location(w, x, y, z); Location l = new Location(w, x, y, z);
@ -53,10 +57,6 @@ public class TileMatter extends RawMatter<MatterTile> {
}); });
} }
public TileMatter(int width, int height, int depth) {
super(width, height, depth, MatterTile.class);
}
@Override @Override
public void writeNode(MatterTile b, DataOutputStream dos) throws IOException { public void writeNode(MatterTile b, DataOutputStream dos) throws IOException {
NBTUtil.write(b.getTileData(), dos, false); NBTUtil.write(b.getTileData(), dos, false);