mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2025-07-19 02:36:59 +00:00
Data formats Matter
This commit is contained in:
parent
65fdd6e25f
commit
294a5e39a6
@ -24,10 +24,13 @@ import com.volmit.iris.engine.object.objects.IrisObject;
|
||||
import com.volmit.iris.util.collection.KList;
|
||||
import com.volmit.iris.util.data.Cuboid;
|
||||
import com.volmit.iris.util.format.C;
|
||||
import com.volmit.iris.util.hunk.storage.MappedHunk;
|
||||
import com.volmit.iris.util.math.M;
|
||||
import com.volmit.iris.util.matter.IrisMatter;
|
||||
import com.volmit.iris.util.plugin.VolmitSender;
|
||||
import org.bukkit.*;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.data.BlockData;
|
||||
import org.bukkit.enchantments.Enchantment;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
@ -224,6 +227,40 @@ public class WandManager implements Listener {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an Iris Object from the 2 coordinates selected with a wand
|
||||
*
|
||||
* @param wand The wand itemstack
|
||||
* @return The new object
|
||||
*/
|
||||
public static IrisMatter createSchematic(Player p, ItemStack wand) {
|
||||
if (!isWand(wand)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
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());
|
||||
Iris.info("Entries " + s.getSlice(BlockData.class).getCount());
|
||||
|
||||
return s;
|
||||
} catch (Throwable e) {
|
||||
e.printStackTrace();
|
||||
Iris.reportError(e);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a user friendly location string to an actual Location
|
||||
*
|
||||
|
@ -32,6 +32,12 @@ public class CommandIrisObject extends MortarCommand {
|
||||
@Command
|
||||
private CommandIrisObjectDust dust;
|
||||
|
||||
@Command
|
||||
private CommandIrisObjectPasteMatter mpaste;
|
||||
|
||||
@Command
|
||||
private CommandIrisObjectSaveMatter msave;
|
||||
|
||||
@Command
|
||||
private CommandIrisObjectXPY xpy;
|
||||
|
||||
|
@ -0,0 +1,174 @@
|
||||
/*
|
||||
* 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.core.command.object;
|
||||
|
||||
import com.volmit.iris.Iris;
|
||||
import com.volmit.iris.core.IrisSettings;
|
||||
import com.volmit.iris.core.ProjectManager;
|
||||
import com.volmit.iris.core.WandManager;
|
||||
import com.volmit.iris.core.project.loader.IrisData;
|
||||
import com.volmit.iris.core.tools.IrisWorlds;
|
||||
import com.volmit.iris.engine.object.common.IObjectPlacer;
|
||||
import com.volmit.iris.engine.object.objects.IrisObject;
|
||||
import com.volmit.iris.engine.object.objects.IrisObjectPlacement;
|
||||
import com.volmit.iris.engine.object.objects.IrisObjectPlacementScaleInterpolator;
|
||||
import com.volmit.iris.engine.object.objects.IrisObjectRotation;
|
||||
import com.volmit.iris.engine.object.tile.TileData;
|
||||
import com.volmit.iris.util.collection.KList;
|
||||
import com.volmit.iris.util.math.RNG;
|
||||
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.VolmitSender;
|
||||
import org.bukkit.*;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.BlockState;
|
||||
import org.bukkit.block.TileState;
|
||||
import org.bukkit.block.data.BlockData;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
public class CommandIrisObjectPasteMatter extends MortarCommand {
|
||||
|
||||
public CommandIrisObjectPasteMatter() {
|
||||
super("mpaste");
|
||||
requiresPermission(Iris.perm);
|
||||
setCategory("Object");
|
||||
setDescription("Paste an object");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addTabOptions(VolmitSender sender, String[] args, KList<String> list) {
|
||||
if ((args.length == 0 || args.length == 1) && sender.isPlayer() && IrisWorlds.isIrisWorld(sender.player().getWorld())) {
|
||||
IrisData data = IrisWorlds.access(sender.player().getWorld()).getData();
|
||||
if (data == null) {
|
||||
sender.sendMessage("Tab complete options only work for objects while in an Iris world.");
|
||||
} else if (args.length == 0) {
|
||||
list.add(data.getObjectLoader().getPossibleKeys());
|
||||
} else {
|
||||
list.add(data.getObjectLoader().getPossibleKeys(args[0]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean handle(VolmitSender sender, String[] args) {
|
||||
if (!IrisSettings.get().isStudio()) {
|
||||
sender.sendMessage("To use Iris Studio Objects, please enable studio in Iris/settings.json");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!sender.isPlayer()) {
|
||||
sender.sendMessage("Only players can spawn objects with this command");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args.length == 0) {
|
||||
sender.sendMessage("Please specify the name of of the object want to paste");
|
||||
return true;
|
||||
}
|
||||
|
||||
Player p = sender.player();
|
||||
File f = new File(args[0]);
|
||||
try {
|
||||
Matter matter = Matter.read(f);
|
||||
matter.slice(BlockData.class)
|
||||
.writeInto(p.getWorld(), p.getLocation().getBlockX(), p.getLocation().getBlockY(), p.getLocation().getBlockZ());
|
||||
} catch (Throwable e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getArgsUsage() {
|
||||
return "[name] [-edit] [-rotate [angle]] [-scale [num] [method]]";
|
||||
}
|
||||
|
||||
public static IObjectPlacer createPlacer(Player player, World world, Map<Block, BlockData> futureBlockChanges) {
|
||||
|
||||
return new IObjectPlacer() {
|
||||
@Override
|
||||
public int getHighest(int x, int z, IrisData data) {
|
||||
return world.getHighestBlockYAt(x, z);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getHighest(int x, int z, IrisData data, boolean ignoreFluid) {
|
||||
return world.getHighestBlockYAt(x, z, ignoreFluid ? HeightMap.OCEAN_FLOOR : HeightMap.MOTION_BLOCKING);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(int x, int y, int z, BlockData d) {
|
||||
Block block = world.getBlockAt(x, y, z);
|
||||
|
||||
//Prevent blocks being set in or bellow bedrock
|
||||
if (y <= world.getMinHeight() || block.getType() == Material.BEDROCK) return;
|
||||
|
||||
futureBlockChanges.put(block, block.getBlockData());
|
||||
|
||||
block.setBlockData(d);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BlockData get(int x, int y, int z) {
|
||||
return world.getBlockAt(x, y, z).getBlockData();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPreventingDecay() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSolid(int x, int y, int z) {
|
||||
return world.getBlockAt(x, y, z).getType().isSolid();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isUnderwater(int x, int z) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getFluidHeight() {
|
||||
return 63;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDebugSmartBore() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@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();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
@ -0,0 +1,108 @@
|
||||
/*
|
||||
* 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.core.command.object;
|
||||
|
||||
import com.volmit.iris.Iris;
|
||||
import com.volmit.iris.core.IrisSettings;
|
||||
import com.volmit.iris.core.WandManager;
|
||||
import com.volmit.iris.engine.object.objects.IrisObject;
|
||||
import com.volmit.iris.util.collection.KList;
|
||||
import com.volmit.iris.util.format.Form;
|
||||
import com.volmit.iris.util.matter.IrisMatter;
|
||||
import com.volmit.iris.util.plugin.MortarCommand;
|
||||
import com.volmit.iris.util.plugin.VolmitSender;
|
||||
import com.volmit.iris.util.scheduling.PrecisionStopwatch;
|
||||
import org.bukkit.Sound;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class CommandIrisObjectSaveMatter extends MortarCommand {
|
||||
public CommandIrisObjectSaveMatter() {
|
||||
super("msave");
|
||||
requiresPermission(Iris.perm);
|
||||
setCategory("Object");
|
||||
setDescription("Save an object");
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void addTabOptions(VolmitSender sender, String[] args, KList<String> list) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean handle(VolmitSender sender, String[] args) {
|
||||
if (!IrisSettings.get().isStudio()) {
|
||||
sender.sendMessage("To use Iris Studio Objects, please enable studio in Iris/settings.json");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!sender.isPlayer()) {
|
||||
sender.sendMessage("You don't have a wand");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args.length < 2) {
|
||||
sender.sendMessage("/iris o save <project> <object>");
|
||||
sender.sendMessage("I.e. /iris o save overworld some-tree/tree1");
|
||||
return true;
|
||||
}
|
||||
|
||||
try {
|
||||
boolean overwrite = false;
|
||||
|
||||
for (String i : args) {
|
||||
if (i.equals("-o")) {
|
||||
overwrite = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Player p = sender.player();
|
||||
ItemStack wand = p.getInventory().getItemInMainHand();
|
||||
IrisMatter o = WandManager.createSchematic(p, wand);
|
||||
File file = Iris.proj.getWorkspaceFile(args[0], "objects", args[1] + ".iob");
|
||||
|
||||
if (file.exists()) {
|
||||
if (!overwrite) {
|
||||
sender.sendMessage("File Exists. Overwrite by adding -o");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
o.write(file);
|
||||
sender.sendMessage("Saved " + args[1]);
|
||||
p.getWorld().playSound(p.getLocation(), Sound.BLOCK_ENCHANTMENT_TABLE_USE, 1f, 1.5f);
|
||||
} catch (Throwable e) {
|
||||
Iris.reportError(e);
|
||||
sender.sendMessage("Failed to save " + args[1] + ". Are you holding your wand?");
|
||||
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getArgsUsage() {
|
||||
return "[project] [name]";
|
||||
}
|
||||
}
|
@ -18,6 +18,7 @@
|
||||
|
||||
package com.volmit.iris.engine.jigsaw;
|
||||
|
||||
import com.volmit.iris.Iris;
|
||||
import com.volmit.iris.core.project.loader.IrisData;
|
||||
import com.volmit.iris.core.tools.IrisWorlds;
|
||||
import com.volmit.iris.engine.framework.Engine;
|
||||
@ -30,6 +31,7 @@ import com.volmit.iris.engine.object.loot.IrisLootTable;
|
||||
import com.volmit.iris.engine.object.meta.InventorySlotType;
|
||||
import com.volmit.iris.engine.object.objects.IrisObject;
|
||||
import com.volmit.iris.engine.object.objects.IrisObjectRotation;
|
||||
import com.volmit.iris.engine.object.objects.IrisObjectTranslate;
|
||||
import com.volmit.iris.engine.object.tile.TileData;
|
||||
import com.volmit.iris.util.collection.KList;
|
||||
import com.volmit.iris.util.math.AxisAlignedBB;
|
||||
@ -159,6 +161,7 @@ public class PlannedPiece {
|
||||
minY--; //If the dimension has no bedrock, allow it to go a block lower
|
||||
}
|
||||
|
||||
getPiece().getPlacementOptions().setTranslate(new IrisObjectTranslate());
|
||||
getPiece().getPlacementOptions().setRotation(rotation);
|
||||
int finalMinY = minY;
|
||||
RNG rng = getStructure().getRng().nextParallelRNG(37555);
|
||||
|
@ -74,6 +74,11 @@ public class PlannedStructure {
|
||||
}
|
||||
|
||||
generateTerminators();
|
||||
|
||||
for(PlannedPiece i : pieces)
|
||||
{
|
||||
Iris.debug("Place: " + i.getObject().getLoadKey() + " at @ relative " + i.getPosition().toString());
|
||||
}
|
||||
}
|
||||
|
||||
public KList<Runnable> place(IObjectPlacer placer, EngineParallaxManager e) {
|
||||
|
@ -18,6 +18,7 @@
|
||||
|
||||
package com.volmit.iris.util.hunk;
|
||||
|
||||
import com.volmit.iris.Iris;
|
||||
import com.volmit.iris.util.collection.KList;
|
||||
import com.volmit.iris.util.function.*;
|
||||
import com.volmit.iris.util.hunk.io.HunkIOAdapter;
|
||||
|
@ -18,6 +18,7 @@
|
||||
|
||||
package com.volmit.iris.util.hunk.storage;
|
||||
|
||||
import com.volmit.iris.Iris;
|
||||
import com.volmit.iris.util.collection.KMap;
|
||||
import com.volmit.iris.util.function.Consumer4;
|
||||
import com.volmit.iris.util.hunk.Hunk;
|
||||
@ -37,6 +38,10 @@ public class MappedHunk<T> extends StorageHunk<T> implements Hunk<T> {
|
||||
data = new KMap<>();
|
||||
}
|
||||
|
||||
public int getEntryCount() {
|
||||
return data.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRaw(int x, int y, int z, T t) {
|
||||
if (t == null) {
|
||||
|
@ -22,6 +22,8 @@ import com.volmit.iris.Iris;
|
||||
import com.volmit.iris.util.collection.KMap;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
public class IrisMatter implements Matter {
|
||||
private static final KMap<Class<?>, MatterSlice<?>> slicers = buildSlicers();
|
||||
|
||||
@ -45,6 +47,7 @@ public class IrisMatter implements Matter {
|
||||
this.height = height;
|
||||
this.depth = depth;
|
||||
this.header = new MatterHeader();
|
||||
this.sliceMap = new KMap<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -55,7 +58,13 @@ public class IrisMatter implements Matter {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (MatterSlice<T>) slice;
|
||||
try {
|
||||
return slice.getClass().getConstructor(int.class, int.class, int.class).newInstance(getWidth(), getHeight(), getDepth());
|
||||
} catch (Throwable e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static KMap<Class<?>, MatterSlice<?>> buildSlicers() {
|
||||
|
@ -25,6 +25,8 @@ import java.io.*;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
import java.util.zip.GZIPInputStream;
|
||||
import java.util.zip.GZIPOutputStream;
|
||||
|
||||
/**
|
||||
* When Red Matter isn't enough
|
||||
@ -208,6 +210,14 @@ public interface Matter {
|
||||
*/
|
||||
Map<Class<?>, MatterSlice<?>> getSliceMap();
|
||||
|
||||
default void write(File f) throws IOException
|
||||
{
|
||||
FileOutputStream out = new FileOutputStream(f);
|
||||
GZIPOutputStream gzo = new GZIPOutputStream(out);
|
||||
write(gzo);
|
||||
gzo.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
@ -231,6 +241,20 @@ public interface Matter {
|
||||
dos.flush();
|
||||
}
|
||||
|
||||
static Matter read(File f) throws IOException, ClassNotFoundException
|
||||
{
|
||||
FileInputStream in = new FileInputStream(f);
|
||||
GZIPInputStream gzi = new GZIPInputStream(in);
|
||||
Matter m = read(gzi);
|
||||
gzi.close();
|
||||
return m;
|
||||
}
|
||||
|
||||
static Matter read(InputStream in) throws IOException, ClassNotFoundException
|
||||
{
|
||||
return read(in, (b) -> new IrisMatter(b.getX(), b.getY(), b.getZ()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the input stream into a matter object using a matter factory.
|
||||
* Does not close the input stream. Be a man, close it yourself.
|
||||
|
@ -1,82 +0,0 @@
|
||||
/*
|
||||
* 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.util.collection.KMap;
|
||||
import com.volmit.iris.util.function.Consumer4;
|
||||
import com.volmit.iris.util.hunk.Hunk;
|
||||
import com.volmit.iris.util.hunk.storage.StorageHunk;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@SuppressWarnings({"DefaultAnnotationParam", "Lombok"})
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
public class MatterHunk<T> extends StorageHunk<T> implements Hunk<T> {
|
||||
private final Map<Integer, T> data;
|
||||
|
||||
public MatterHunk(int w, int h, int d) {
|
||||
super(w, h, d);
|
||||
data = new KMap<>();
|
||||
}
|
||||
|
||||
public int getCount() {
|
||||
return data.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRaw(int x, int y, int z, T t) {
|
||||
if (t == null) {
|
||||
data.remove(index(x, y, z));
|
||||
return;
|
||||
}
|
||||
|
||||
data.put(index(x, y, z), t);
|
||||
}
|
||||
|
||||
private Integer index(int x, int y, int z) {
|
||||
return (z * getWidth() * getHeight()) + (y * getWidth()) + x;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized Hunk<T> iterateSync(Consumer4<Integer, Integer, Integer, T> c) {
|
||||
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
|
||||
public void empty(T b) {
|
||||
data.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public T getRaw(int x, int y, int z) {
|
||||
return data.get(index(x, y, z));
|
||||
}
|
||||
}
|
24
src/main/java/com/volmit/iris/util/matter/MatterReader.java
Normal file
24
src/main/java/com/volmit/iris/util/matter/MatterReader.java
Normal file
@ -0,0 +1,24 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface MatterReader<W, T> {
|
||||
T readMatter(W w, int x, int y, int z);
|
||||
}
|
@ -18,9 +18,13 @@
|
||||
|
||||
package com.volmit.iris.util.matter;
|
||||
|
||||
import com.volmit.iris.Iris;
|
||||
import com.volmit.iris.engine.data.cache.Cache;
|
||||
import com.volmit.iris.util.data.Varint;
|
||||
import com.volmit.iris.util.hunk.Hunk;
|
||||
import com.volmit.iris.util.hunk.storage.MappedHunk;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.block.data.BlockData;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
@ -33,6 +37,90 @@ public interface MatterSlice<T> extends Hunk<T> {
|
||||
|
||||
T readNode(DataInputStream din) throws IOException;
|
||||
|
||||
<W> MatterWriter<W, T> writeInto(Class<W> mediumType);
|
||||
|
||||
<W> MatterReader<W, T> readFrom(Class<W> mediumType);
|
||||
|
||||
default Class<?> getClass(Object w)
|
||||
{
|
||||
Class<?> c = w.getClass();
|
||||
|
||||
if(w instanceof World)
|
||||
{
|
||||
c = World.class;
|
||||
}else if(w instanceof BlockData)
|
||||
{
|
||||
c = BlockData.class;
|
||||
}
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
default <W> boolean writeInto(W w, int x, int y, int z)
|
||||
{
|
||||
MatterWriter<W, T> injector = (MatterWriter<W, T>) writeInto(getClass(w));
|
||||
|
||||
if(injector == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
for(int i = x; i < x + getWidth(); i++)
|
||||
{
|
||||
for(int j = y; j < y + getHeight(); j++)
|
||||
{
|
||||
for(int k = z; k < z + getDepth(); k++)
|
||||
{
|
||||
injector.writeMatter(w, get(i - x, j - y, k - z), i, j, k);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
default <W> boolean readFrom(W w, int x, int y, int z)
|
||||
{
|
||||
MatterReader<W, T> ejector = (MatterReader<W, T>) readFrom(getClass(w));
|
||||
|
||||
if(ejector == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
for(int i = x; i < x + getWidth(); i++)
|
||||
{
|
||||
for(int j = y; j < y + getHeight(); j++)
|
||||
{
|
||||
for(int k = z; k < z + getDepth(); k++)
|
||||
{
|
||||
set(i - x, j - y, k - z, ejector.readMatter(w, i, j, k));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// BlockMatter<T>
|
||||
// RawMatter<T> ex MappedHunk<T>
|
||||
// IMatterSlice<T> ex Hunk<T>
|
||||
|
||||
default int getCount()
|
||||
{
|
||||
return ((MappedHunk<?>)this).getEntryCount();
|
||||
}
|
||||
|
||||
default boolean canWrite(Class<?> mediumType)
|
||||
{
|
||||
return writeInto(mediumType) != null;
|
||||
}
|
||||
|
||||
default boolean canRead(Class<?> mediumType)
|
||||
{
|
||||
return readFrom(mediumType) != null;
|
||||
}
|
||||
|
||||
default void write(DataOutputStream dos) throws IOException {
|
||||
int w = getWidth();
|
||||
int h = getHeight();
|
||||
@ -40,9 +128,9 @@ public interface MatterSlice<T> extends Hunk<T> {
|
||||
MatterPalette<T> palette = new MatterPalette<T>(this);
|
||||
iterateSync((x, y, z, b) -> palette.assign(b));
|
||||
palette.writePalette(dos);
|
||||
Varint.writeUnsignedVarInt(((MatterHunk<?>) this).getCount(), dos);
|
||||
Varint.writeUnsignedVarInt(getCount(), dos);
|
||||
iterateSyncIO((x, y, z, b) -> {
|
||||
Varint.writeUnsignedVarInt((z * w * h) + (y * w) + x, dos);
|
||||
Varint.writeUnsignedVarInt(Cache.to1D(x, y, z, w, h), dos);
|
||||
palette.writeNode(b, dos);
|
||||
});
|
||||
}
|
||||
@ -51,7 +139,6 @@ public interface MatterSlice<T> extends Hunk<T> {
|
||||
int w = getWidth();
|
||||
int h = getHeight();
|
||||
|
||||
// canonical is read in parent
|
||||
MatterPalette<T> palette = new MatterPalette<T>(this, din);
|
||||
int nodes = Varint.readUnsignedVarInt(din);
|
||||
int[] pos;
|
||||
|
24
src/main/java/com/volmit/iris/util/matter/MatterWriter.java
Normal file
24
src/main/java/com/volmit/iris/util/matter/MatterWriter.java
Normal file
@ -0,0 +1,24 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface MatterWriter<W, T> {
|
||||
void writeMatter(W w, T d, int x, int y, int z);
|
||||
}
|
@ -18,10 +18,14 @@
|
||||
|
||||
package com.volmit.iris.util.matter.slices;
|
||||
|
||||
import com.volmit.iris.engine.parallax.ParallaxAccess;
|
||||
import com.volmit.iris.engine.parallax.ParallaxWorld;
|
||||
import com.volmit.iris.util.data.B;
|
||||
import com.volmit.iris.util.matter.Sliced;
|
||||
import com.volmit.iris.util.nbt.io.NBTUtil;
|
||||
import com.volmit.iris.util.nbt.mca.NBTWorld;
|
||||
import com.volmit.iris.util.nbt.tag.CompoundTag;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.block.data.BlockData;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
@ -36,15 +40,19 @@ public class BlockMatter extends RawMatter<BlockData> {
|
||||
|
||||
public BlockMatter(int width, int height, int depth) {
|
||||
super(width, height, depth, BlockData.class);
|
||||
registerWriter(World.class, ((w, d, x, y, z) -> w.getBlockAt(x,y,z).setBlockData(d)));
|
||||
registerWriter(ParallaxWorld.class, (w, d, x, y, z) -> w.setBlock(x,y,z,d));
|
||||
registerReader(World.class, (w, x, y, z) -> w.getBlockAt(x,y,z).getBlockData());
|
||||
registerReader(ParallaxWorld.class, ParallaxAccess::getBlock);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeNode(BlockData b, DataOutputStream dos) throws IOException {
|
||||
NBTUtil.write(NBTWorld.getCompound(b), dos, false);
|
||||
dos.writeUTF(b.getAsString(true));
|
||||
}
|
||||
|
||||
@Override
|
||||
public BlockData readNode(DataInputStream din) throws IOException {
|
||||
return NBTWorld.getBlockData((CompoundTag) NBTUtil.read(din, false).getTag());
|
||||
return B.get(din.readUTF());
|
||||
}
|
||||
}
|
||||
|
@ -18,7 +18,10 @@
|
||||
|
||||
package com.volmit.iris.util.matter.slices;
|
||||
|
||||
import com.volmit.iris.util.matter.MatterHunk;
|
||||
import com.volmit.iris.util.collection.KMap;
|
||||
import com.volmit.iris.util.hunk.storage.MappedHunk;
|
||||
import com.volmit.iris.util.matter.MatterReader;
|
||||
import com.volmit.iris.util.matter.MatterWriter;
|
||||
import com.volmit.iris.util.matter.MatterSlice;
|
||||
import lombok.Getter;
|
||||
|
||||
@ -26,23 +29,39 @@ import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
public abstract class RawMatter<T> extends MatterHunk<T> implements MatterSlice<T> {
|
||||
public abstract class RawMatter<T> extends MappedHunk<T> implements MatterSlice<T> {
|
||||
@Getter
|
||||
private final Class<T> type;
|
||||
private final KMap<Class<?>, MatterWriter<?, T>> injectors;
|
||||
private final KMap<Class<?>, MatterReader<?, T>> ejectors;
|
||||
|
||||
public RawMatter(int width, int height, int depth, Class<T> type) {
|
||||
super(width, height, depth);
|
||||
injectors = new KMap<>();
|
||||
ejectors = new KMap<>();
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRaw(int x, int y, int z, T t) {
|
||||
protected <W> void registerWriter(Class<W> mediumType, MatterWriter<W, T> injector)
|
||||
{
|
||||
injectors.put(mediumType, injector);
|
||||
}
|
||||
|
||||
protected <W> void registerReader(Class<W> mediumType, MatterReader<W, T> injector)
|
||||
{
|
||||
ejectors.put(mediumType, injector);
|
||||
}
|
||||
|
||||
@Override
|
||||
public T getRaw(int x, int y, int z) {
|
||||
return null;
|
||||
public <W> MatterWriter<W, T> writeInto(Class<W> mediumType)
|
||||
{
|
||||
return (MatterWriter<W, T>) injectors.get(mediumType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <W> MatterReader<W, T> readFrom(Class<W> mediumType)
|
||||
{
|
||||
return (MatterReader<W, T>) ejectors.get(mediumType);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Loading…
x
Reference in New Issue
Block a user