Yoted Codec stuff.

This commit is contained in:
Waterpicker 2021-01-07 06:40:21 -06:00
parent 2c84a4664f
commit 6cd6b08f2d
28 changed files with 558 additions and 141 deletions

View file

@ -10,9 +10,12 @@ import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.SpawnReason;
import net.minecraft.entity.mob.EndermanEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.Tickable;
import net.minecraft.util.registry.DynamicRegistryManager;
import net.minecraft.util.registry.Registry;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
@ -37,6 +40,7 @@ public class DetachedRiftBlockEntity extends RiftBlockEntity implements Tickable
@Override
public void tick() {
if (this.world == null) {
DynamicRegistryManager
return;
}
@ -77,7 +81,7 @@ public class DetachedRiftBlockEntity extends RiftBlockEntity implements Tickable
}
@Override
protected CompoundTag serialize(CompoundTag tag) {
public CompoundTag serialize(CompoundTag tag) {
super.serialize(tag);
tag.putBoolean("closing", this.closing);
tag.putBoolean("stablized", this.stabilized);
@ -87,7 +91,7 @@ public class DetachedRiftBlockEntity extends RiftBlockEntity implements Tickable
}
@Override
protected void deserialize(CompoundTag tag) {
public void deserialize(CompoundTag tag) {
super.deserialize(tag);
this.closing = tag.getBoolean("closing");
this.stabilized = tag.getBoolean("stablized");

View file

@ -1,15 +1,22 @@
package org.dimdev.dimdoors.block.entity;
import java.util.List;
import java.util.Objects;
import com.mojang.serialization.Codec;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import io.github.cottonmc.cotton.gui.widget.WWidget;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import org.dimdev.dimdoors.client.gui.RiftConfigurationToolGuiDescroption;
import org.dimdev.dimdoors.pockets.PocketTemplate;
import org.dimdev.dimdoors.rift.registry.LinkProperties;
import org.dimdev.dimdoors.rift.registry.Rift;
import org.dimdev.dimdoors.rift.registry.RiftRegistry;
import org.dimdev.dimdoors.rift.targets.EntityTarget;
import org.dimdev.dimdoors.rift.targets.ItemTarget;
import org.dimdev.dimdoors.rift.targets.MessageTarget;
import org.dimdev.dimdoors.rift.targets.Target;
import org.dimdev.dimdoors.rift.targets.Targets;
@ -23,19 +30,29 @@ import org.dimdev.dimdoors.world.pocket.VirtualLocation;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.block.entity.LootableContainerBlockEntity;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.screen.NamedScreenHandlerFactory;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.screen.ScreenHandlerContext;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.LiteralText;
import net.minecraft.text.Text;
import net.minecraft.text.TranslatableText;
import net.minecraft.world.World;
import net.fabricmc.fabric.api.block.entity.BlockEntityClientSerializable;
import org.jetbrains.annotations.NotNull;
public abstract class RiftBlockEntity extends BlockEntity implements BlockEntityClientSerializable, Target, EntityTarget {
private static final Codec<RiftData> CODEC = RiftData.CODEC;
private static final Logger LOGGER = LogManager.getLogger();
public static long showRiftCoreUntil = 0;
@NotNull
protected RiftData data = new RiftData();
protected boolean riftStateChanged; // not saved
@ -50,8 +67,8 @@ public abstract class RiftBlockEntity extends BlockEntity implements BlockEntity
this.deserialize(nbt);
}
protected void deserialize(CompoundTag nbt) {
this.data = NbtUtil.deserialize(nbt.get("data"), CODEC);
public void deserialize(CompoundTag nbt) {
this.data = RiftData.fromTag(nbt.getCompound("data"));
}
@Override
@ -61,8 +78,8 @@ public abstract class RiftBlockEntity extends BlockEntity implements BlockEntity
return super.toTag(tag);
}
protected CompoundTag serialize(CompoundTag tag) {
if (this.data != null) tag.put("data", NbtUtil.serialize(this.data, CODEC));
public CompoundTag serialize(CompoundTag tag) {
tag.put("data", RiftData.toTag(this.data));
return tag;
}
@ -121,7 +138,7 @@ public abstract class RiftBlockEntity extends BlockEntity implements BlockEntity
Location loc = new Location((ServerWorld) this.world, this.pos);
RiftRegistry.instance().addRift(loc);
if (this.data.getDestination() != VirtualTarget.NoneTarget.DUMMY) this.data.getDestination().register();
if (this.data.getDestination() != null) this.data.getDestination().register();
this.updateProperties();
this.updateColor();
}
@ -159,7 +176,7 @@ public abstract class RiftBlockEntity extends BlockEntity implements BlockEntity
}
public Target getTarget() {
if (this.data.getDestination() == VirtualTarget.NoneTarget.DUMMY) {
if (this.data.getDestination() == null) {
return new MessageTarget("rifts.unlinked1");
} else {
this.data.getDestination().setLocation(new Location((ServerWorld) this.world, this.pos));
@ -191,12 +208,12 @@ public abstract class RiftBlockEntity extends BlockEntity implements BlockEntity
if (this.data.isForcedColor()) return;
if (!this.isRegistered()) {
this.data.setColor(new RGBA(0, 0, 0, 1));
} else if (this.data.getDestination() == VirtualTarget.NoneTarget.DUMMY) {
} else if (this.data.getDestination() == null) {
this.data.setColor(new RGBA(0.7f, 0.7f, 0.7f, 1));
} else {
this.data.getDestination().setLocation(new Location((ServerWorld) this.world, this.pos));
RGBA newColor = this.data.getDestination().getColor();
if (this.data.getColor() == RGBA.NONE && newColor != RGBA.NONE || !Objects.equals(this.data.getColor(), newColor)) {
if (this.data.getColor() == null && newColor != null || !Objects.equals(this.data.getColor(), newColor)) {
this.data.setColor(newColor);
this.markDirty();
}

View file

@ -1,25 +1,25 @@
package org.dimdev.dimdoors.block.entity;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.text.LiteralText;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import io.github.cottonmc.cotton.gui.widget.WBox;
import io.github.cottonmc.cotton.gui.widget.WToggleButton;
import io.github.cottonmc.cotton.gui.widget.WWidget;
import io.github.cottonmc.cotton.gui.widget.data.Axis;
import org.dimdev.dimdoors.rift.registry.LinkProperties;
import org.dimdev.dimdoors.rift.targets.VirtualTarget;
import org.dimdev.dimdoors.util.RGBA;
public class RiftData {
public static Codec<RiftData> CODEC = RecordCodecBuilder.create(instance -> instance.group(
VirtualTarget.CODEC.optionalFieldOf("destination", VirtualTarget.NoneTarget.DUMMY).forGetter(RiftData::getDestination),
LinkProperties.CODEC.optionalFieldOf("properties", LinkProperties.NONE).forGetter(RiftData::getProperties),
Codec.BOOL.fieldOf("alwaysDelete").forGetter(RiftData::isAlwaysDelete),
Codec.BOOL.fieldOf("forcedColor").forGetter(RiftData::isForcedColor),
RGBA.CODEC.optionalFieldOf("color", RGBA.NONE).forGetter(RiftData::getColor)
).apply(instance, RiftData::new));
private VirtualTarget destination = VirtualTarget.NoneTarget.DUMMY; // How the rift acts as a source
private LinkProperties properties = LinkProperties.NONE;
private VirtualTarget destination = null; // How the rift acts as a source
private LinkProperties properties = null;
private boolean alwaysDelete;
private boolean forcedColor;
private RGBA color = RGBA.NONE;
private RGBA color = null;
public RiftData() {
}
@ -72,4 +72,37 @@ public class RiftData {
this.forcedColor = color != null;
this.color = color;
}
public static CompoundTag toTag(RiftData data) {
CompoundTag tag = new CompoundTag();
if(data.destination != null) tag.put("destination", VirtualTarget.toTag(data.destination));
if(data.properties != null) tag.put("properties", LinkProperties.toTag(data.properties));
if(data.color != null) tag.put("color", RGBA.toTag(data.color));
tag.putBoolean("alwaysDelete", data.alwaysDelete);
tag.putBoolean("forcedColor", data.forcedColor);
return tag;
}
public static RiftData fromTag(CompoundTag tag) {
RiftData data = new RiftData();
data.destination = tag.contains("destination") ? VirtualTarget.fromTag(tag.getCompound("destination")) : null;
data.properties = tag.contains("properties") ? LinkProperties.fromTag(tag.getCompound("properties")) : null;
data.alwaysDelete = tag.getBoolean("alwaysDelete");
data.forcedColor = tag.getBoolean("forcedColor");
data.color = tag.contains("color") ? RGBA.fromTag(tag.getCompound("color")) : null;
return data;
}
public WWidget widget() {
WBox box = new WBox(Axis.VERTICAL);
WToggleButton alwaysDelete = new WToggleButton().setLabel(new LiteralText("Always Delete")).setOnToggle(this::setAlwaysDelete);
alwaysDelete.setToggle(this.alwaysDelete);
WToggleButton forcedColor = new WToggleButton().setLabel(new LiteralText("Forced Color")).setOnToggle(this::setForcedColor);
forcedColor.setToggle(this.forcedColor);
box.add(alwaysDelete);
box.add(forcedColor);
return box;
}
}

View file

@ -1,11 +1,30 @@
package org.dimdev.dimdoors.rift.registry;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.commons.lang3.math.NumberUtils;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.text.Text;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import io.github.cottonmc.cotton.gui.widget.WBox;
import io.github.cottonmc.cotton.gui.widget.WLabel;
import io.github.cottonmc.cotton.gui.widget.WTextField;
import io.github.cottonmc.cotton.gui.widget.WToggleButton;
import io.github.cottonmc.cotton.gui.widget.WWidget;
import io.github.cottonmc.cotton.gui.widget.data.Axis;
import org.dimdev.dimdoors.util.Codecs;
public class LinkProperties {
@ -94,6 +113,26 @@ public class LinkProperties {
return new LinkPropertiesBuilder().floatingWeight(this.floatingWeight).entranceWeight(this.entranceWeight).groups(this.groups).linksRemaining(this.linksRemaining).oneWay(this.oneWay);
}
public static CompoundTag toTag(LinkProperties properties) {
net.minecraft.nbt.CompoundTag tag = new net.minecraft.nbt.CompoundTag();
tag.putFloat("floatingWeight", properties.floatingWeight);
tag.putFloat("entranceWeight", properties.entranceWeight);
tag.putIntArray("groups", new ArrayList<>(properties.groups));
tag.putInt("linksRemaining", properties.linksRemaining);
tag.putBoolean("oneWay", properties.oneWay);
return tag;
}
public static LinkProperties fromTag(CompoundTag tag) {
LinkProperties properties = new LinkProperties();
properties.floatingWeight = tag.getFloat("floatingWeight");
properties.entranceWeight = tag.getFloat("entranceWeight");
properties.groups = Arrays.stream(tag.getIntArray("groups")).boxed().collect(Collectors.toSet());
properties.linksRemaining = tag.getInt("linksRemaining");
properties.oneWay = tag.getBoolean("oneWay");
return properties;
}
public static class LinkPropertiesBuilder {
private float floatingWeight;
private float entranceWeight;
@ -137,4 +176,35 @@ public class LinkProperties {
return "LinkProperties.LinkPropertiesBuilder(floatingWeight=" + this.floatingWeight + ", entranceWeight=" + this.entranceWeight + ", groups=" + this.groups + ", linksRemaining=" + this.linksRemaining + ", oneWay=" + this.oneWay + ")";
}
}
public WWidget widget() {
WBox root = new WBox(Axis.VERTICAL);
root.add(new WLabel("Rift Data:"));
WBox tab = new WBox(Axis.HORIZONTAL);
tab.add(new WLabel(" "));
WBox main = new WBox(Axis.VERTICAL);
WBox box = new WBox(Axis.HORIZONTAL);
box.add(new WLabel("Floating Weight:"));
WTextField floatingWeightText = new WTextField().setChangedListener(a -> {
if(NumberUtils.isParsable(a)) floatingWeight = NumberUtils.toFloat(a);
});
floatingWeightText.setText(String.valueOf(floatingWeight));
box.add(floatingWeightText);
WToggleButton oneWayButton = new WToggleButton(Text.of("One Way:")).setOnToggle(oneWay -> this.oneWay = oneWay);
oneWayButton.setToggle(oneWay);
main.add(box);
main.add(oneWayButton);
tab.add(main);
root.add(tab);
return root;
}
}

View file

@ -5,6 +5,7 @@ import java.util.UUID;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.util.dynamic.DynamicSerializableUuid;
public class PlayerRiftPointer extends RegistryVertex {
@ -33,4 +34,17 @@ public class PlayerRiftPointer extends RegistryVertex {
public String toString() {
return "PlayerRiftPointer(player=" + this.player + ")";
}
public static CompoundTag toTag(PlayerRiftPointer vertex) {
CompoundTag tag = new CompoundTag();
tag.putUuid("id", vertex.id);
tag.putUuid("player", vertex.player);
return tag;
}
public static PlayerRiftPointer fromTag(CompoundTag tag) {
PlayerRiftPointer vertex = new PlayerRiftPointer(tag.getUuid("id"));
vertex.player = tag.getUuid("player");
return vertex;
}
}

View file

@ -3,7 +3,10 @@ package org.dimdev.dimdoors.rift.registry;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.util.Identifier;
import net.minecraft.util.dynamic.DynamicSerializableUuid;
import net.minecraft.util.registry.Registry;
import net.minecraft.util.registry.RegistryKey;
import net.minecraft.world.World;
@ -39,4 +42,18 @@ public class PocketEntrancePointer extends RegistryVertex { // TODO: PocketRiftP
public String toString() {
return "PocketEntrancePointer(pocketDim=" + this.pocketDim + ", pocketId=" + this.pocketId + ")";
}
public static CompoundTag toTag(PocketEntrancePointer vertex) {
CompoundTag tag = new CompoundTag();
tag.putUuid("id", vertex.id);
tag.putString("pocketDim", vertex.pocketDim.getValue().toString());
tag.putInt("pocketId", vertex.pocketId);
return tag;
}
public static PocketEntrancePointer fromTag(CompoundTag tag) {
PocketEntrancePointer pointer = new PocketEntrancePointer(RegistryKey.of(Registry.DIMENSION, new Identifier(tag.getString("pocketDim"))), tag.getInt("pocketId"));
pointer.id = tag.getUuid("id");
return pointer;
}
}

View file

@ -1,9 +1,12 @@
package org.dimdev.dimdoors.rift.registry;
import java.util.Objects;
import java.util.UUID;
import java.util.function.Function;
import com.mojang.serialization.Codec;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry;
import net.minecraft.util.registry.RegistryKey;
@ -11,14 +14,13 @@ import net.minecraft.world.World;
import net.fabricmc.fabric.api.event.registry.FabricRegistryBuilder;
import net.fabricmc.fabric.api.event.registry.RegistryAttribute;
import org.dimdev.dimdoors.rift.targets.VirtualTarget;
public abstract class RegistryVertex {
public static final Registry<RegistryVertexType> registry = FabricRegistryBuilder.createSimple(RegistryVertex.RegistryVertexType.class, new Identifier("dimdoors", "registry_vertex")).attribute(RegistryAttribute.MODDED).buildAndRegister();
public RegistryKey<World> world; // The dimension to store this object in. Links are stored in both registries.
public static final Codec<RegistryVertex> CODEC = registry.dispatch(RegistryVertex::getType, RegistryVertexType::codec);
public UUID id = UUID.randomUUID(); // Used to create pointers to registry vertices. Should not be used for anything other than saving.
public void sourceGone(RegistryVertex source) {
@ -39,16 +41,40 @@ public abstract class RegistryVertex {
return "RegistryVertex(dim=" + this.world + ", id=" + this.id + ")";
}
public static RegistryVertex fromTag(CompoundTag nbt) {
return Objects.requireNonNull(registry.get(new Identifier(nbt.getString("type")))).fromTag(nbt);
}
public static CompoundTag toTag(RegistryVertex registryVertex) {
String type = registry.getId(registryVertex.getType()).toString();
CompoundTag tag = registryVertex.getType().toTag(registryVertex);
tag.putString("type", type);
return tag;
}
public interface RegistryVertexType<T extends RegistryVertex> {
RegistryVertexType<PlayerRiftPointer> PLAYER = register("player", PlayerRiftPointer.CODEC);
RegistryVertexType<Rift> RIFT = register("rift", Rift.CODEC);
RegistryVertexType<PocketEntrancePointer> ENTRANCE = register("entrance", PocketEntrancePointer.CODEC);
RegistryVertexType<RiftPlaceholder> RIFT_PLACEHOLDER = register("rift_placeholder", RiftPlaceholder.CODEC);
RegistryVertexType<PlayerRiftPointer> PLAYER = register("player", PlayerRiftPointer::fromTag, PlayerRiftPointer::toTag);
RegistryVertexType<Rift> RIFT = register("rift", Rift::fromTag, Rift::toTag);
RegistryVertexType<PocketEntrancePointer> ENTRANCE = register("entrance", PocketEntrancePointer::fromTag, PocketEntrancePointer::toTag);
RegistryVertexType<RiftPlaceholder> RIFT_PLACEHOLDER = register("rift_placeholder", RiftPlaceholder::fromTag, RiftPlaceholder::toTag);
Codec<T> codec();
T fromTag(CompoundTag tag);
CompoundTag toTag(RegistryVertex virtualType);
static <T extends RegistryVertex> RegistryVertex.RegistryVertexType<T> register(String id, Codec<T> codec) {
return Registry.register(registry, id, () -> codec);
static <T extends RegistryVertex> RegistryVertex.RegistryVertexType<T> register(String id, Function<CompoundTag, T> fromTag, Function<T, CompoundTag> toTag) {
return Registry.register(registry, id, new RegistryVertexType<T>() {
@Override
public T fromTag(CompoundTag tag) {
return fromTag.apply(tag);
}
@Override
public CompoundTag toTag(RegistryVertex registryVertex) {
return toTag.apply((T) registryVertex);
}
});
}
}
}

View file

@ -7,20 +7,15 @@ import com.mojang.serialization.codecs.RecordCodecBuilder;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.dimdev.dimdoors.block.entity.RiftBlockEntity;
import org.dimdev.dimdoors.block.entity.RiftData;
import org.dimdev.dimdoors.rift.targets.VirtualTarget;
import org.dimdev.dimdoors.util.Location;
import org.dimdev.dimdoors.util.RGBA;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.util.dynamic.DynamicSerializableUuid;
public class Rift extends RegistryVertex {
public static final Codec<Rift> CODEC = RecordCodecBuilder.create(instance -> {
return instance.group(
DynamicSerializableUuid.CODEC.fieldOf("id").forGetter(a -> a.id),
Location.CODEC.fieldOf("location").forGetter(a -> a.location),
Codec.BOOL.fieldOf("isDetached").forGetter(a -> a.isDetached),
LinkProperties.CODEC.fieldOf("properties").forGetter(a -> a.properties)
).apply(instance, Rift::new);
});
private static final Logger LOGGER = LogManager.getLogger();
public Location location;
public boolean isDetached;
@ -81,4 +76,22 @@ public class Rift extends RegistryVertex {
public RegistryVertexType<? extends RegistryVertex> getType() {
return RegistryVertexType.RIFT;
}
public static CompoundTag toTag(Rift rift) {
CompoundTag tag = new CompoundTag();
tag.putUuid("id", rift.id);
tag.put("location", Location.toTag(rift.location));
tag.putBoolean("isDetached", rift.isDetached);
tag.put("properties", LinkProperties.toTag(rift.properties));
return tag;
}
public static Rift fromTag(CompoundTag tag) {
Rift rift = new Rift();
rift.id = tag.getUuid("id");
rift.location = Location.fromTag(tag.getCompound("location"));
rift.isDetached = tag.getBoolean("isDetached");
rift.properties = LinkProperties.fromTag(tag.getCompound("properties"));;
return rift;
}
}

View file

@ -4,6 +4,7 @@ import com.mojang.serialization.Codec;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.util.dynamic.DynamicSerializableUuid;
public class RiftPlaceholder extends Rift { // TODO: don't extend rift
@ -44,4 +45,16 @@ public class RiftPlaceholder extends Rift { // TODO: don't extend rift
public RegistryVertexType<? extends RegistryVertex> getType() {
return RegistryVertexType.RIFT_PLACEHOLDER;
}
public static CompoundTag toTag(RiftPlaceholder vertex) {
CompoundTag tag = new CompoundTag();
tag.putUuid("id", vertex.id);
return tag;
}
public static RiftPlaceholder fromTag(CompoundTag tag) {
RiftPlaceholder vertex = new RiftPlaceholder();
vertex.id = tag.getUuid("id");
return vertex;
}
}

View file

@ -10,6 +10,8 @@ import java.util.stream.Collectors;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import javafx.util.converter.DoubleStringConverter;
import org.dimdev.dimdoors.util.GraphUtils;
import org.dimdev.dimdoors.util.Location;
import org.dimdev.dimdoors.util.NbtUtil;
@ -57,10 +59,10 @@ public class RiftRegistry extends PersistentState {
ListTag riftsNBT = (ListTag) nbt.get("rifts");
for (Tag riftNBT : riftsNBT) {
Rift rift = NbtUtil.deserialize(riftNBT, Rift.CODEC);
this.graph.addVertex(rift);
this.uuidMap.put(rift.id, rift);
this.locationMap.put(rift.location, rift);
// Rift rift = NbtUtil.deserialize(riftNBT, Rift.CODEC);
// this.graph.addVertex(rift);
// this.uuidMap.put(rift.id, rift);
// this.locationMap.put(rift.location, rift);
}
ListTag pocketsNBT = (ListTag) nbt.get("pockets");
@ -95,7 +97,7 @@ public class RiftRegistry extends PersistentState {
ListTag riftsNBT = new ListTag();
ListTag pocketsNBT = new ListTag();
for (RegistryVertex vertex : this.graph.vertexSet()) {
CompoundTag vertexNBT = (CompoundTag) NbtUtil.serialize(vertex, RegistryVertex.CODEC);
CompoundTag vertexNBT = RegistryVertex.toTag(vertex);
if (vertex instanceof Rift) {
riftsNBT.add(vertexNBT);
} else if (vertex instanceof PocketEntrancePointer) {

View file

@ -1,5 +1,6 @@
package org.dimdev.dimdoors.rift.targets;
import java.util.Objects;
import java.util.UUID;
import com.mojang.serialization.Codec;
@ -12,8 +13,10 @@ import org.dimdev.dimdoors.world.ModDimensions;
import org.dimdev.dimdoors.world.pocket.VirtualLocation;
import net.minecraft.entity.Entity;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.TranslatableText;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import static org.dimdev.dimdoors.util.EntityUtils.chat;
@ -66,4 +69,14 @@ public class EscapeTarget extends VirtualTarget implements EntityTarget { // TOD
public VirtualTargetType<? extends VirtualTarget> getType() {
return VirtualTargetType.ESCAPE;
}
public static CompoundTag toTag(EscapeTarget virtualTarget) {
CompoundTag tag = new CompoundTag();
tag.putBoolean("canEscapeLimbo", virtualTarget.canEscapeLimbo);
return tag;
}
public static EscapeTarget fromTag(CompoundTag nbt) {
return new EscapeTarget(nbt.getBoolean("canEscapeLimbo"));
}
}

View file

@ -1,5 +1,7 @@
package org.dimdev.dimdoors.rift.targets;
import net.minecraft.nbt.CompoundTag;
import com.mojang.serialization.Codec;
import org.dimdev.dimdoors.util.Location;
@ -21,4 +23,14 @@ public class GlobalReference extends RiftReference {
public VirtualTargetType<? extends VirtualTarget> getType() {
return VirtualTargetType.GLOBAL;
}
public static CompoundTag toTag(GlobalReference virtualTarget) {
CompoundTag tag = new CompoundTag();
tag.put("target", Location.toTag(virtualTarget.location));
return tag;
}
public static GlobalReference fromTag(CompoundTag nbt) {
return new GlobalReference(Location.fromTag(nbt.getCompound("target")));
}
}

View file

@ -3,7 +3,11 @@ package org.dimdev.dimdoors.rift.targets;
import com.mojang.serialization.Codec;
import org.dimdev.dimdoors.util.Location;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.registry.Registry;
import net.minecraft.util.registry.RegistryKey;
public class LocalReference extends RiftReference {
public static final Codec<LocalReference> CODEC = BlockPos.CODEC.xmap(LocalReference::new, LocalReference::getTarget).fieldOf("target").codec();
@ -27,4 +31,17 @@ public class LocalReference extends RiftReference {
public VirtualTargetType<? extends VirtualTarget> getType() {
return VirtualTargetType.LOCAL;
}
public static CompoundTag toTag(LocalReference localReference) {
CompoundTag tag = new CompoundTag();
tag.putIntArray("target", new int[]{ localReference.target.getX(), localReference.target.getY(), localReference.target.getZ()});
return tag;
}
public static LocalReference fromTag(CompoundTag tag) {
int[] pos = tag.getIntArray("target");
return new LocalReference(
new BlockPos(pos[1], pos[2], pos[3])
);
}
}

View file

@ -5,17 +5,11 @@ import com.mojang.serialization.codecs.RecordCodecBuilder;
import org.dimdev.dimdoors.util.EntityUtils;
import net.minecraft.entity.Entity;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.text.TranslatableText;
public class PocketEntranceMarker extends VirtualTarget implements EntityTarget {
public static final Codec<PocketEntranceMarker> CODEC = RecordCodecBuilder.create(instance -> {
return instance.group(
Codec.FLOAT.fieldOf("weight").forGetter(target -> target.weight),
VirtualTarget.CODEC.fieldOf("ifDestination").forGetter(target -> target.ifDestination),
VirtualTarget.CODEC.fieldOf("otherwiseDestination").forGetter(target -> target.otherwiseDestination)
).apply(instance, PocketEntranceMarker::new);
});
protected float weight = 1;
protected VirtualTarget ifDestination;
@ -60,11 +54,28 @@ public class PocketEntranceMarker extends VirtualTarget implements EntityTarget
return new PocketEntranceMarkerBuilder().weight(this.weight).ifDestination(this.ifDestination).otherwiseDestination(this.otherwiseDestination);
}
@Override
public VirtualTargetType<? extends VirtualTarget> getType() {
return VirtualTargetType.POCKET_ENTRANCE;
}
public static CompoundTag toTag(PocketEntranceMarker target) {
CompoundTag tag = new CompoundTag();
tag.putFloat("weight", target.weight);
tag.put("ifDestination", VirtualTarget.toTag(target.ifDestination));
tag.put("otherwiseDestination", VirtualTarget.toTag(target.otherwiseDestination));
return tag;
}
public static PocketEntranceMarker fromTag(CompoundTag tag) {
PocketEntranceMarker target = new PocketEntranceMarker();
target.weight = tag.getFloat("weight");
target.ifDestination = VirtualTarget.fromTag(tag.getCompound("ifDestination"));
target.otherwiseDestination = VirtualTarget.fromTag(tag.getCompound("otherwiseDestination"));
return target;
}
public static class PocketEntranceMarkerBuilder {
private float weight;
private VirtualTarget ifDestination;

View file

@ -19,7 +19,6 @@ import net.minecraft.text.TranslatableText;
//import net.fabricmc.fabric.api.dimension.v1.FabricDimensions;
public class PrivatePocketExitTarget extends VirtualTarget implements EntityTarget {
public static final Codec<PrivatePocketExitTarget> CODEC = Codec.unit(PrivatePocketExitTarget::new);
public static final RGBA COLOR = new RGBA(0, 1, 0, 1);
public PrivatePocketExitTarget() {

View file

@ -19,12 +19,11 @@ import net.minecraft.entity.Entity;
import net.minecraft.entity.ItemEntity;
import net.minecraft.item.DyeItem;
import net.minecraft.item.Item;
import net.minecraft.nbt.CompoundTag;
public class PrivatePocketTarget extends VirtualTarget implements EntityTarget {
private static final Logger LOGGER = LogManager.getLogger();
public static final Codec<PrivatePocketTarget> CODEC = Codec.unit(PrivatePocketTarget::new);
public static final RGBA COLOR = new RGBA(0, 1, 0, 1);
public PrivatePocketTarget() {
@ -43,7 +42,6 @@ public class PrivatePocketTarget extends VirtualTarget implements EntityTarget {
PrivatePocketData.instance().setPrivatePocketID(uuid, pocket);
this.processEntity(pocket, RiftRegistry.instance().getPocketEntrance(pocket).getBlockEntity(), entity, uuid, yawOffset);
return true;
} else {
Location destLoc = RiftRegistry.instance().getPrivatePocketEntrance(uuid); // get the last used entrances
if (destLoc == null)
@ -57,8 +55,8 @@ public class PrivatePocketTarget extends VirtualTarget implements EntityTarget {
}
this.processEntity(pocket, destLoc.getBlockEntity(), entity, uuid, yawOffset);
return true;
}
return true;
} else {
return false;
}

View file

@ -1,5 +1,7 @@
package org.dimdev.dimdoors.rift.targets;
import net.minecraft.nbt.CompoundTag;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import org.dimdev.dimdoors.pockets.PocketGenerator;
@ -9,11 +11,8 @@ import org.dimdev.dimdoors.world.pocket.Pocket;
import org.dimdev.dimdoors.world.pocket.VirtualLocation;
public class PublicPocketTarget extends RestoringTarget {
public final static Codec<PublicPocketTarget> CODEC = RecordCodecBuilder.create(instance -> instance.group(
VirtualTarget.CODEC.optionalFieldOf("wrappedDestination", NoneTarget.DUMMY).forGetter(RestoringTarget::getTarget)
).apply(instance, PublicPocketTarget::new));
private VirtualTarget wrappedDestination;
private VirtualTarget wrappedDestination = null;
private PublicPocketTarget(VirtualTarget wrappedDestination) {
this.wrappedDestination = wrappedDestination;
@ -48,4 +47,16 @@ public class PublicPocketTarget extends RestoringTarget {
public VirtualTargetType<? extends VirtualTarget> getType() {
return VirtualTargetType.PUBLIC_POCKET;
}
public static CompoundTag toTag(PublicPocketTarget target) {
CompoundTag tag = new CompoundTag();
if(target.wrappedDestination != null) tag.put("wrappedDestination", VirtualTarget.toTag(target.wrappedDestination));
return tag;
}
public static PublicPocketTarget fromTag(CompoundTag tag) {
PublicPocketTarget target = new PublicPocketTarget();
if(tag.contains("wrappedDestination")) target.wrappedDestination = VirtualTarget.fromTag(tag.getCompound("wrappedDestination"));
return target;
}
}

View file

@ -1,5 +1,7 @@
package org.dimdev.dimdoors.rift.targets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
@ -22,6 +24,7 @@ import org.dimdev.dimdoors.util.math.MathUtil;
import org.dimdev.dimdoors.world.pocket.Pocket;
import org.dimdev.dimdoors.world.pocket.VirtualLocation;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.Heightmap;
@ -238,6 +241,33 @@ public class RandomTarget extends VirtualTarget { // TODO: Split into DungeonTar
return VirtualTargetType.AVAILABLE_LINK;
}
public static CompoundTag toTag(RandomTarget target) {
CompoundTag tag = new CompoundTag();
tag.putFloat("newRiftWeight", target.newRiftWeight);
tag.putDouble("weightMaximum", target.weightMaximum);
tag.putDouble("coordFactor", target.coordFactor);
tag.putDouble("positiveDepthFactor", target.positiveDepthFactor);
tag.putDouble("negativeDepthFactor", target.negativeDepthFactor);
tag.putIntArray("acceptedGroups", new ArrayList<>(target.acceptedGroups));
tag.putBoolean("noLink", target.noLink);
tag.putBoolean("noLinkBack", target.noLinkBack);
return tag;
}
public static RandomTarget fromTag(CompoundTag tag) {
return new RandomTarget(
tag.getFloat("newRiftWeight"),
tag.getDouble("weightMaximum"),
tag.getDouble("coordFactor"),
tag.getDouble("positiveDepthFactor"),
tag.getDouble("negativeDepthFactor"),
Arrays.stream(tag.getIntArray("acceptedGroups")).boxed().collect(Collectors.toSet()),
tag.getBoolean("noLink"),
tag.getBoolean("noLinkBack")
);
}
public static class RandomTargetBuilder {
private float newRiftWeight;
private double weightMaximum;

View file

@ -5,5 +5,7 @@ import net.minecraft.util.math.Direction;
public interface RedstoneTarget extends Target {
boolean addRedstonePower(Direction relativeFacing, int strength);
void subtractRedstonePower(Direction relativeFacing, int strength);
}

View file

@ -3,14 +3,13 @@ package org.dimdev.dimdoors.rift.targets;
import com.mojang.serialization.Codec;
import org.dimdev.dimdoors.util.Location;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.util.math.Vec3i;
public class RelativeReference extends RiftReference {
private final Vec3i offset;
public static Codec<RelativeReference> CODEC = Vec3i.CODEC.xmap(RelativeReference::new, RelativeReference::getOffset).fieldOf("offset").codec();
public RelativeReference(Vec3i offset) {
this.offset = offset;
}
@ -28,4 +27,15 @@ public class RelativeReference extends RiftReference {
public VirtualTargetType<? extends VirtualTarget> getType() {
return VirtualTargetType.RELATIVE;
}
public static CompoundTag toTag(RelativeReference target) {
CompoundTag tag = new CompoundTag();
tag.putIntArray("offset", new int[] { target.offset.getX(), target.offset.getY(), target.offset.getZ() });
return tag;
}
public static RelativeReference fromTag(CompoundTag tag) {
int[] offset = tag.getIntArray("offset");
return new RelativeReference(new Vec3i(offset[0], offset[1], offset[2]));
}
}

View file

@ -1,6 +1,7 @@
package org.dimdev.dimdoors.rift.targets;
import java.util.Objects;
import java.util.function.Function;
import com.mojang.serialization.Codec;
import com.mojang.serialization.Lifecycle;
@ -24,12 +25,19 @@ public abstract class VirtualTarget implements Target {
public static final Registry<VirtualTargetType<?>> REGISTRY = FabricRegistryBuilder.<VirtualTargetType<?>, SimpleRegistry<VirtualTargetType<?>>>from(new SimpleRegistry<>(RegistryKey.ofRegistry(new Identifier("dimdoors", "virtual_type")), Lifecycle.stable())).buildAndRegister();
public static final RGBA COLOR = new RGBA(1, 0, 0, 1);
public static Codec<VirtualTarget> CODEC = REGISTRY.dispatch(VirtualTarget::getType, VirtualTargetType::codec);
protected Location location;
public static VirtualTarget readVirtualTargetNBT(CompoundTag nbt) {
return NbtUtil.deserialize(nbt, CODEC);
public static VirtualTarget fromTag(CompoundTag nbt) {
return Objects.requireNonNull(REGISTRY.get(new Identifier(nbt.getString("type")))).fromTag(nbt);
}
public static CompoundTag toTag(VirtualTarget virtualTarget) {
String type = REGISTRY.getId(virtualTarget.getType()).toString();
CompoundTag tag = virtualTarget.getType().toTag(virtualTarget);
tag.putString("type", type);
return tag;
}
public void register() {
@ -70,28 +78,33 @@ public abstract class VirtualTarget implements Target {
}
public interface VirtualTargetType<T extends VirtualTarget> {
VirtualTargetType<RandomTarget> AVAILABLE_LINK = register("available_link", RandomTarget.CODEC, VirtualTarget.COLOR);
VirtualTargetType<EscapeTarget> ESCAPE = register("escape", EscapeTarget.CODEC, VirtualTarget.COLOR);
VirtualTargetType<GlobalReference> GLOBAL = register("global", GlobalReference.CODEC, VirtualTarget.COLOR);
VirtualTargetType<LimboTarget> LIMBO = register("limbo", LimboTarget.CODEC, VirtualTarget.COLOR);
VirtualTargetType<LocalReference> LOCAL = register("local", LocalReference.CODEC, VirtualTarget.COLOR);
VirtualTargetType<PublicPocketTarget> PUBLIC_POCKET = register("public_pocket", PublicPocketTarget.CODEC, VirtualTarget.COLOR);
VirtualTargetType<PocketEntranceMarker> POCKET_ENTRANCE = register("pocket_entrance", PocketEntranceMarker.CODEC, VirtualTarget.COLOR);
VirtualTargetType<PocketExitMarker> POCKET_EXIT = register("pocket_exit", PocketExitMarker.CODEC, VirtualTarget.COLOR);
VirtualTargetType<PrivatePocketTarget> PRIVATE = register("private", PrivatePocketTarget.CODEC, PrivatePocketExitTarget.COLOR);
VirtualTargetType<PrivatePocketExitTarget> PRIVATE_POCKET_EXIT = register("private_pocket_exit", PrivatePocketExitTarget.CODEC, PrivatePocketExitTarget.COLOR);
VirtualTargetType<RelativeReference> RELATIVE = register("relative", RelativeReference.CODEC, VirtualTarget.COLOR);
VirtualTargetType<NoneTarget> NONE = register("none", NoneTarget.CODEC, COLOR);
VirtualTargetType<RandomTarget> AVAILABLE_LINK = register("dimdoors:available_link", RandomTarget::fromTag, RandomTarget::toTag, VirtualTarget.COLOR);
VirtualTargetType<EscapeTarget> ESCAPE = register("dimdoors:escape", EscapeTarget::fromTag, EscapeTarget::toTag, VirtualTarget.COLOR);
VirtualTargetType<GlobalReference> GLOBAL = register("dimdoors:global", GlobalReference::fromTag, GlobalReference::toTag, VirtualTarget.COLOR);
VirtualTargetType<LimboTarget> LIMBO = register("dimdoors:limbo", a -> LimboTarget.INSTANCE, a -> new CompoundTag(), VirtualTarget.COLOR);
VirtualTargetType<LocalReference> LOCAL = register("dimdoors:local", LocalReference::fromTag, LocalReference::toTag, VirtualTarget.COLOR);
VirtualTargetType<PublicPocketTarget> PUBLIC_POCKET = register("dimdoors:public_pocket", PublicPocketTarget::fromTag, PublicPocketTarget::toTag, VirtualTarget.COLOR);
VirtualTargetType<PocketEntranceMarker> POCKET_ENTRANCE = register("dimdoors:pocket_entrance", PocketEntranceMarker::fromTag, PocketEntranceMarker::toTag, VirtualTarget.COLOR);
VirtualTargetType<PocketExitMarker> POCKET_EXIT = register("dimdoors:pocket_exit", a -> new PocketExitMarker(), a -> new CompoundTag(), VirtualTarget.COLOR);
VirtualTargetType<PrivatePocketTarget> PRIVATE = register("dimdoors:private", a -> new PrivatePocketTarget(), a -> new CompoundTag(), PrivatePocketExitTarget.COLOR);
VirtualTargetType<PrivatePocketExitTarget> PRIVATE_POCKET_EXIT = register("dimdoors:private_pocket_exit", a -> new PrivatePocketExitTarget(), a -> new CompoundTag(), PrivatePocketExitTarget.COLOR);
VirtualTargetType<RelativeReference> RELATIVE = register("dimdoors:relative", RelativeReference::fromTag, RelativeReference::toTag, VirtualTarget.COLOR);
Codec<T> codec();
T fromTag(CompoundTag tag);
CompoundTag toTag(VirtualTarget virtualType);
RGBA getColor();
static <T extends VirtualTarget> VirtualTargetType<T> register(String id, Codec<T> codec, RGBA color) {
static <T extends VirtualTarget> VirtualTargetType<T> register(String id, Function<CompoundTag, T> fromTag, Function<T, CompoundTag> toTag, RGBA color) {
return Registry.register(REGISTRY, (String) id, new VirtualTargetType<T>() {
@Override
public Codec<T> codec() {
return codec;
public T fromTag(CompoundTag tag) {
return fromTag.apply(tag);
}
@Override
public CompoundTag toTag(VirtualTarget virtualType) {
return toTag.apply((T) virtualType);
}
@Override
@ -102,14 +115,4 @@ public abstract class VirtualTarget implements Target {
}
}
public static class NoneTarget extends VirtualTarget {
public static NoneTarget DUMMY = new NoneTarget();
public static Codec<NoneTarget> CODEC = Codec.unit(DUMMY);
@Override
public VirtualTargetType<? extends VirtualTarget> getType() {
return VirtualTargetType.NONE;
}
}
}

View file

@ -1,5 +1,7 @@
package org.dimdev.dimdoors.util;
import java.util.stream.IntStream;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import org.dimdev.dimdoors.DimensionalDoorsInitializer;
@ -7,8 +9,12 @@ import org.dimdev.dimdoors.DimensionalDoorsInitializer;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.fluid.FluidState;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.Identifier;
import net.minecraft.util.Util;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.registry.Registry;
import net.minecraft.util.registry.RegistryKey;
import net.minecraft.world.World;
@ -84,4 +90,19 @@ public class Location {
public ServerWorld getWorld() {
return DimensionalDoorsInitializer.getServer().getWorld(this.world);
}
public static CompoundTag toTag(Location location) {
CompoundTag tag = new CompoundTag();
tag.putString("world", location.world.getValue().toString());
tag.putIntArray("pos", new int[]{ location.getX(), location.getY(), location.getZ()});
return tag;
}
public static Location fromTag(CompoundTag tag) {
int[] pos = tag.getIntArray("pos");
return new Location(
RegistryKey.of(Registry.DIMENSION, new Identifier(tag.getString("world"))),
new BlockPos(pos[1], pos[2], pos[3])
);
}
}

View file

@ -4,12 +4,14 @@ import java.util.Iterator;
import java.util.Objects;
import java.util.function.Consumer;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.Tag;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import org.jetbrains.annotations.NotNull;
public final class RGBA implements Cloneable, Comparable<RGBA>, Iterable<Float> {
public static final RGBA NONE = new RGBA(-1, -1, -1, -1);
public static Codec<RGBA> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codec.FLOAT.fieldOf("red").forGetter(RGBA::getRed),
Codec.FLOAT.fieldOf("green").forGetter(RGBA::getGreen),
@ -130,4 +132,22 @@ public final class RGBA implements Cloneable, Comparable<RGBA>, Iterable<Float>
Float.compare(this.green, o.green) +
Float.compare(this.blue, o.blue);
}
public static CompoundTag toTag(RGBA rgba) {
CompoundTag tag = new CompoundTag();
tag.putFloat("red", rgba.red);
tag.putFloat("green", rgba.green);
tag.putFloat("blue", rgba.blue);
tag.putFloat("alpha", rgba.alpha);
return tag;
}
public static RGBA fromTag(CompoundTag tag) {
return new RGBA(
tag.getFloat("red"),
tag.getFloat("green"),
tag.getFloat("blue"),
tag.getFloat("alpha")
);
}
}

View file

@ -5,7 +5,9 @@ import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.Tag;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.registry.Registry;
import net.minecraft.util.registry.RegistryKey;
import net.minecraft.world.World;
@ -28,11 +30,22 @@ public class RotatedLocation extends Location {
this.pitch = pitch;
}
public static RotatedLocation deserialize(CompoundTag nbt) {
return NbtUtil.deserialize(nbt, CODEC);
public static CompoundTag serialize(RotatedLocation location) {
CompoundTag tag = new CompoundTag();
tag.putString("world", location.world.getValue().toString());
tag.putIntArray("pos", new int[]{ location.getX(), location.getY(), location.getZ()});
tag.putFloat("yaw", location.pitch);
tag.putFloat("pitch", location.pitch);
return tag;
}
public static Tag serialize(RotatedLocation location) {
return NbtUtil.serialize(location, CODEC);
public static RotatedLocation deserialize(CompoundTag tag) {
int[] pos = tag.getIntArray("pos");
return new RotatedLocation(
RegistryKey.of(Registry.DIMENSION, new Identifier(tag.getString("world"))),
new BlockPos(pos[0], pos[1], pos[2]),
tag.getFloat("yaw"),
tag.getFloat("pitch")
);
}
}

View file

@ -1,31 +1,28 @@
package org.dimdev.dimdoors.world.pocket;
import java.util.stream.IntStream;
import com.flowpowered.math.vector.Vector3i;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import org.dimdev.dimdoors.rift.targets.VirtualTarget;
import org.dimdev.dimdoors.util.Codecs;
import org.dimdev.dimdoors.util.EntityUtils;
import net.minecraft.entity.Entity;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.Tag;
import net.minecraft.text.TranslatableText;
import net.minecraft.util.DyeColor;
import net.minecraft.util.math.BlockBox;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Box;
import net.minecraft.util.math.Vec3i;
import net.minecraft.util.registry.RegistryKey;
import net.minecraft.world.World;
public final class Pocket {
public static final Codec<Pocket> CODEC = RecordCodecBuilder.create(instance -> {
return instance.group(
Codec.INT.fieldOf("id").forGetter(a -> a.id),
Codecs.BLOCK_BOX.fieldOf("box").forGetter(a -> a.box),
VirtualLocation.CODEC.fieldOf("virtualLocation").forGetter(a -> a.virtualLocation),
PocketColor.CODEC.fieldOf("dyeColor").forGetter(a -> a.dyeColor),
PocketColor.CODEC.optionalFieldOf("nextDyeColor", PocketColor.NONE).forGetter(a -> a.nextDyeColor),
Codec.INT.fieldOf("count").forGetter(a -> a.count)
).apply(instance, Pocket::new);
});
private static final int BLOCKS_PAINTED_PER_DYE = 1106;
public final int id;
@ -130,6 +127,29 @@ public final class Pocket {
return new Vector3i(dimensions.getX(), dimensions.getY(), dimensions.getZ());
}
public CompoundTag toTag() {
CompoundTag tag = new CompoundTag();
tag.putInt("id", id);
tag.putIntArray("box", IntStream.of(box.minX, box.minY, box.minZ, box.maxX, box.maxY, box.maxZ).toArray());
tag.put("virtualLocation", VirtualLocation.toTag(virtualLocation));
tag.putInt("dyeColor", dyeColor.getId());
tag.putInt("nextDyeColor", nextDyeColor.getId());
tag.putInt("count", count);
return tag;
}
public static Pocket fromTag(CompoundTag tag) {
int[] box = tag.getIntArray("box");
return new Pocket(
tag.getInt("id"),
new BlockBox(box[0], box[1], box[2], box[3], box[4], box[5]),
VirtualLocation.fromTag(tag.getCompound("virtualLocation")),
PocketColor.from(tag.getInt("dyeColor")),
PocketColor.from(tag.getInt("nextDyeColor")),
tag.getInt("count")
);
}
public enum PocketColor {
WHITE(0, DyeColor.WHITE),
ORANGE(1, DyeColor.ORANGE),

View file

@ -2,6 +2,7 @@ package org.dimdev.dimdoors.world.pocket;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
import com.mojang.serialization.Codec;
import org.dimdev.dimdoors.DimensionalDoorsInitializer;
@ -18,8 +19,6 @@ import net.minecraft.world.PersistentState;
import net.minecraft.world.World;
public class PocketRegistry extends PersistentState {
private final Codec<Map<Integer, Pocket>> pocketsCodec = Codec.unboundedMap(Codec.INT, Pocket.CODEC);
private static final String DATA_NAME = "pocketlib_pockets";
/*package-private*/ int gridSize; // Determines how much pockets in their dimension are spaced
@ -47,7 +46,9 @@ public class PocketRegistry extends PersistentState {
this.gridSize = tag.getInt("gridSize");
this.privatePocketSize = tag.getInt("privatePocketSize");
this.publicPocketSize = tag.getInt("publicPocketSize");
this.pockets = NbtUtil.deserialize(tag.get("pockets"), this.pocketsCodec);
CompoundTag pocketsTag = tag.getCompound("pockets");
this.pockets = pocketsTag.getKeys().stream().collect(Collectors.toMap(Integer::parseInt, a -> Pocket.fromTag(pocketsTag.getCompound(a))));
this.nextID = tag.getInt("nextID");
}
@ -56,7 +57,10 @@ public class PocketRegistry extends PersistentState {
tag.putInt("gridSize", this.gridSize);
tag.putInt("privatePocketSize", this.privatePocketSize);
tag.putInt("publicPocketSize", this.publicPocketSize);
tag.put("pockets", NbtUtil.serialize(this.pockets, this.pocketsCodec));
CompoundTag pocketsTag = new CompoundTag();
this.pockets.forEach((key, value) -> pocketsTag.put(key.toString(), value.toTag()));
tag.put("pockets", pocketsTag);
tag.putInt("nextID", this.nextID);
return tag;
}

View file

@ -1,6 +1,8 @@
package org.dimdev.dimdoors.world.pocket;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
@ -10,7 +12,9 @@ import org.dimdev.dimdoors.DimensionalDoorsInitializer;
import org.dimdev.dimdoors.util.NbtUtil;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.util.Identifier;
import net.minecraft.util.dynamic.DynamicSerializableUuid;
import net.minecraft.util.registry.Registry;
import net.minecraft.util.registry.RegistryKey;
import net.minecraft.world.PersistentState;
import net.minecraft.world.World;
@ -19,13 +23,6 @@ import static net.minecraft.world.World.OVERWORLD;
public class PrivatePocketData extends PersistentState {
protected static class PocketInfo {
public static final Codec<PocketInfo> CODEC = RecordCodecBuilder.create(instance -> {
return instance.group(
World.CODEC.fieldOf("world").forGetter(a -> a.world),
Codec.INT.fieldOf("id").forGetter(a -> a.id)
).apply(instance, PocketInfo::new);
});
public final RegistryKey<World> world;
public final int id;
@ -33,9 +30,21 @@ public class PrivatePocketData extends PersistentState {
this.world = world;
this.id = id;
}
public static CompoundTag toTag(PocketInfo info) {
CompoundTag tag = new CompoundTag();
tag.putString("world", info.world.getValue().toString());
tag.putInt("id", info.id);
return tag;
}
public static final Codec<BiMap<UUID, PocketInfo>> CODEC = Codec.unboundedMap(DynamicSerializableUuid.CODEC, PocketInfo.CODEC).xmap(HashBiMap::create, a -> a);
public static PocketInfo fromTag(CompoundTag tag) {
return new PocketInfo(
RegistryKey.of(Registry.DIMENSION, new Identifier(tag.getString("world"))),
tag.getInt("id")
);
}
}
private static final String DATA_NAME = "dimdoors_private_pockets";
@ -55,12 +64,23 @@ public class PrivatePocketData extends PersistentState {
@Override
public void fromTag(CompoundTag nbt) {
this.privatePocketMap = NbtUtil.deserialize(nbt.get("privatePocketMap"), CODEC);
CompoundTag tag = nbt.getCompound("privatePocketMap");
HashBiMap<UUID, PocketInfo> bm = HashBiMap.create();
for (String t : tag.getKeys()) {
bm.put(UUID.fromString(t), PocketInfo.fromTag(tag.getCompound(t)));
}
this.privatePocketMap = bm;
}
@Override
public CompoundTag toTag(CompoundTag nbt) {
nbt.put("privatePocketMap", NbtUtil.serialize(this.privatePocketMap, CODEC));
CompoundTag tag = new CompoundTag();
for (Map.Entry<UUID, PocketInfo> entry : privatePocketMap.entrySet()) {
tag.put(entry.getKey().toString(), PocketInfo.toTag(entry.getValue()));
}
nbt.put("privatePocketMap", tag);
return nbt;
}

View file

@ -10,7 +10,9 @@ import org.dimdev.dimdoors.world.ModDimensions;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtOps;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.registry.Registry;
import net.minecraft.util.registry.RegistryKey;
import net.minecraft.world.Heightmap;
import net.minecraft.world.World;
@ -39,20 +41,22 @@ public class VirtualLocation {
this.depth = depth;
}
public void fromTag(CompoundTag tag) {
VirtualLocation location = CODEC.decode(NbtOps.INSTANCE, tag).getOrThrow(false, System.err::println).getFirst();
this.x = location.x;
this.z = location.z;
this.depth = location.depth;
this.world = location.world;
public static CompoundTag toTag(VirtualLocation virtualLocation) {
CompoundTag tag = new CompoundTag();
tag.putString("world", virtualLocation.world.getValue().toString());
tag.putInt("x", virtualLocation.x);
tag.putInt("z", virtualLocation.z);
tag.putInt("depth", virtualLocation.depth);
return tag;
}
public CompoundTag toTag(CompoundTag tag) {
CompoundTag encodedTag = (CompoundTag) CODEC.encodeStart(NbtOps.INSTANCE, this).getOrThrow(false, System.err::println);
for (String key : encodedTag.getKeys()) {
tag.put(key, encodedTag.get(key));
}
return tag;
public static VirtualLocation fromTag(CompoundTag tag) {
return new VirtualLocation(
RegistryKey.of(Registry.DIMENSION, new Identifier(tag.getString("world"))),
tag.getInt("x"),
tag.getInt("z"),
tag.getInt("depth")
);
}
public static VirtualLocation fromLocation(Location location) {