Did a ton of DataFixerUpper stuff. ><

Rift signature technically works now.
This commit is contained in:
Waterpicker 2020-09-05 06:28:32 -05:00
parent 339c4d9b8c
commit c38b73a934
46 changed files with 690 additions and 383 deletions

View file

@ -1,5 +1,11 @@
package org.dimdev.dimdoors; package org.dimdev.dimdoors;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.registry.RegistryKey;
import net.minecraft.world.World;
import org.apache.logging.log4j.core.jmx.Server;
import org.dimdev.dimdoors.block.ModBlocks; import org.dimdev.dimdoors.block.ModBlocks;
import org.dimdev.dimdoors.block.entity.ModBlockEntityTypes; import org.dimdev.dimdoors.block.entity.ModBlockEntityTypes;
import org.dimdev.dimdoors.command.ModCommands; import org.dimdev.dimdoors.command.ModCommands;
@ -31,8 +37,20 @@ public class DimensionalDoorsInitializer implements ModInitializer {
public static final Identifier MONOLITH_PARTICLE_PACKET = new Identifier("dimdoors", "monolith_particle_packet"); public static final Identifier MONOLITH_PARTICLE_PACKET = new Identifier("dimdoors", "monolith_particle_packet");
private static MinecraftServer server;
public static MinecraftServer getServer() {
return server;
}
public static ServerWorld getWorld(RegistryKey<World> key) {
return getServer().getWorld(key);
}
@Override @Override
public void onInitialize() { public void onInitialize() {
ServerLifecycleEvents.SERVER_STARTED.register(DimensionalDoorsInitializer::setServer);
ModBlocks.init(); ModBlocks.init();
ModItems.init(); ModItems.init();
ModDimensions.init(); ModDimensions.init();
@ -43,18 +61,10 @@ public class DimensionalDoorsInitializer implements ModInitializer {
ModSoundEvents.init(); ModSoundEvents.init();
ModFeatures.init(); ModFeatures.init();
VirtualTarget.registry.put("available_link", RandomTarget.class);
VirtualTarget.registry.put("escape", EscapeTarget.class);
VirtualTarget.registry.put("global", GlobalReference.class);
VirtualTarget.registry.put("limbo", LimboTarget.class);
VirtualTarget.registry.put("local", LocalReference.class);
VirtualTarget.registry.put("public_pocket", PublicPocketTarget.class);
VirtualTarget.registry.put("pocket_entrance", PocketEntranceMarker.class);
VirtualTarget.registry.put("pocket_exit", PocketExitMarker.class);
VirtualTarget.registry.put("private", PrivatePocketTarget.class);
VirtualTarget.registry.put("private_pocket_exit", PrivatePocketExitTarget.class);
VirtualTarget.registry.put("relative", RelativeReference.class);
Targets.registerDefaultTargets(); Targets.registerDefaultTargets();
} }
private static void setServer(MinecraftServer server) {
DimensionalDoorsInitializer.server = server;
}
} }

View file

@ -2,14 +2,11 @@ package org.dimdev.dimdoors.block;
import java.util.Random; import java.util.Random;
import net.minecraft.block.*;
import org.dimdev.dimdoors.block.entity.DetachedRiftBlockEntity; import org.dimdev.dimdoors.block.entity.DetachedRiftBlockEntity;
import org.dimdev.dimdoors.client.RiftParticle; import org.dimdev.dimdoors.client.RiftParticle;
import org.dimdev.dimdoors.world.ModDimensions; import org.dimdev.dimdoors.world.ModDimensions;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.MaterialColor;
import net.minecraft.block.ShapeContext;
import net.minecraft.block.entity.BlockEntity; import net.minecraft.block.entity.BlockEntity;
import net.minecraft.client.MinecraftClient; import net.minecraft.client.MinecraftClient;
import net.minecraft.client.world.ClientWorld; import net.minecraft.client.world.ClientWorld;
@ -85,4 +82,9 @@ public class DetachedRiftBlock extends Block implements RiftProvider<DetachedRif
outsidePocket ? 0.0f : 0.7f, 0.55f, rift.stabilized ? 750 : 2000, rift.stabilized ? 750 : 2000) outsidePocket ? 0.0f : 0.7f, 0.55f, rift.stabilized ? 750 : 2000, rift.stabilized ? 750 : 2000)
);*/ );*/
} }
@Override
public BlockRenderType getRenderType(BlockState state) {
return BlockRenderType.ENTITYBLOCK_ANIMATED;
}
} }

View file

@ -19,6 +19,7 @@ import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.world.BlockView; import net.minecraft.world.BlockView;
import net.minecraft.world.World; import net.minecraft.world.World;
import org.dimdev.dimdoors.item.RiftSignatureItem;
public class DimensionalDoorBlock extends DoorBlock implements RiftProvider<EntranceRiftBlockEntity>, ConditionalBlockEntityProvider { public class DimensionalDoorBlock extends DoorBlock implements RiftProvider<EntranceRiftBlockEntity>, ConditionalBlockEntityProvider {
public DimensionalDoorBlock(Settings settings) { public DimensionalDoorBlock(Settings settings) {

View file

@ -39,7 +39,7 @@ public class EntranceRiftBlockEntity extends RiftBlockEntity {
public boolean teleport(Entity entity) { public boolean teleport(Entity entity) {
boolean status = super.teleport(entity); boolean status = super.teleport(entity);
if (riftStateChanged && !alwaysDelete) { if (riftStateChanged && !data.isAlwaysDelete()) {
markDirty(); markDirty();
} }

View file

@ -1,6 +1,7 @@
package org.dimdev.dimdoors.block.entity; package org.dimdev.dimdoors.block.entity;
import java.util.Arrays; import java.util.Arrays;
import java.util.Objects;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
@ -18,6 +19,8 @@ import org.dimdev.dimdoors.rift.targets.Targets;
import org.dimdev.dimdoors.rift.targets.VirtualTarget; import org.dimdev.dimdoors.rift.targets.VirtualTarget;
import org.dimdev.dimdoors.util.EntityUtils; import org.dimdev.dimdoors.util.EntityUtils;
import org.dimdev.dimdoors.util.Location; import org.dimdev.dimdoors.util.Location;
import org.dimdev.dimdoors.util.NbtUtil;
import org.dimdev.dimdoors.util.RGBA;
import org.dimdev.dimdoors.world.pocket.VirtualLocation; import org.dimdev.dimdoors.world.pocket.VirtualLocation;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
@ -32,21 +35,13 @@ import net.fabricmc.fabric.api.block.entity.BlockEntityClientSerializable;
public abstract class RiftBlockEntity extends BlockEntity implements BlockEntityClientSerializable, Target, EntityTarget, AutoSerializable { public abstract class RiftBlockEntity extends BlockEntity implements BlockEntityClientSerializable, Target, EntityTarget, AutoSerializable {
private static final Logger LOGGER = LogManager.getLogger(); private static final Logger LOGGER = LogManager.getLogger();
/*@Saved*/ protected VirtualTarget destination; // How the rift acts as a source
@Saved protected RiftData data = new RiftData();
protected LinkProperties properties;
@Saved
protected boolean alwaysDelete;
@Saved
protected boolean forcedColor;
@Saved
protected float[] color = null;
protected boolean riftStateChanged; // not saved protected boolean riftStateChanged; // not saved
public RiftBlockEntity(BlockEntityType<? extends RiftBlockEntity> type) { public RiftBlockEntity(BlockEntityType<? extends RiftBlockEntity> type) {
super(type); super(type);
alwaysDelete = false;
} }
// NBT // NBT
@ -56,38 +51,36 @@ public abstract class RiftBlockEntity extends BlockEntity implements BlockEntity
if (this.world != null && !this.world.isClient()) { if (this.world != null && !this.world.isClient()) {
this.sync(); this.sync();
} }
AnnotatedNbt.load(this, nbt);
this.destination = nbt.contains("destination") ? VirtualTarget.readVirtualTargetNBT(nbt.getCompound("destination")) : null; this.data = NbtUtil.deserialize(nbt.get("data"), RiftData.CODEC);
} }
@Override
public void fromClientTag(CompoundTag tag) {
fromTag(this.world.getBlockState(this.pos), tag);
}
@Override @Override
public CompoundTag toTag(CompoundTag tag) { public CompoundTag toTag(CompoundTag tag) {
if (this.world != null && !this.world.isClient()) { if (this.world != null && !this.world.isClient()) {
this.sync(); this.sync();
} }
return super.toTag(tag); return super.toTag(tag);
} }
@Override
public void fromClientTag(CompoundTag tag) {
tag.put("data", NbtUtil.serialize(data, RiftData.CODEC));
}
@Override @Override
public CompoundTag toClientTag(CompoundTag tag) { public CompoundTag toClientTag(CompoundTag tag) {
save(tag); tag.put("data", NbtUtil.serialize(data, RiftData.CODEC));
if (destination != null) {
tag.put("destination", destination.toTag(new CompoundTag()));
}
return tag; return tag;
} }
public void setDestination(VirtualTarget destination) { public void setDestination(VirtualTarget destination) {
if (this.destination != null && isRegistered()) { if (this.getDestination() != null && isRegistered()) {
this.destination.unregister(); this.getDestination().unregister();
} }
this.destination = destination; this.data.setDestination(destination);
if (destination != null) { if (destination != null) {
if (world != null && pos != null) { if (world != null && pos != null) {
destination.setLocation(new Location((ServerWorld) world, pos)); destination.setLocation(new Location((ServerWorld) world, pos));
@ -99,14 +92,13 @@ public abstract class RiftBlockEntity extends BlockEntity implements BlockEntity
updateColor(); updateColor();
} }
public void setColor(float[] color) { public void setColor(RGBA color) {
forcedColor = color != null; data.setColor(color);
this.color = color;
markDirty(); markDirty();
} }
public void setProperties(LinkProperties properties) { public void setProperties(LinkProperties properties) {
this.properties = properties; data.setProperties(properties);
updateProperties(); updateProperties();
markDirty(); markDirty();
} }
@ -117,7 +109,7 @@ public abstract class RiftBlockEntity extends BlockEntity implements BlockEntity
} }
public boolean isRegistered() { public boolean isRegistered() {
return !PocketTemplate.isReplacingPlaceholders() && RiftRegistry.instance(world).isRiftAt(new Location((ServerWorld) world, pos)); return !PocketTemplate.isReplacingPlaceholders() && RiftRegistry.instance().isRiftAt(new Location((ServerWorld) world, pos));
} }
public void register() { public void register() {
@ -126,34 +118,34 @@ public abstract class RiftBlockEntity extends BlockEntity implements BlockEntity
} }
Location loc = new Location((ServerWorld) world, pos); Location loc = new Location((ServerWorld) world, pos);
RiftRegistry.instance(world).addRift(loc); RiftRegistry.instance().addRift(loc);
if (destination != null) destination.register(); if (data.getDestination() != null) data.getDestination().register();
updateProperties(); updateProperties();
updateColor(); updateColor();
} }
public void updateProperties() { public void updateProperties() {
if (isRegistered()) if (isRegistered())
RiftRegistry.instance(world).setProperties(new Location((ServerWorld) world, pos), properties); RiftRegistry.instance().setProperties(new Location((ServerWorld) world, pos), data.getProperties());
markDirty(); markDirty();
} }
public void unregister() { public void unregister() {
if (isRegistered()) { if (isRegistered()) {
RiftRegistry.instance(world).removeRift(new Location((ServerWorld) world, pos)); RiftRegistry.instance().removeRift(new Location((ServerWorld) world, pos));
} }
} }
public void updateType() { public void updateType() {
if (!isRegistered()) return; if (!isRegistered()) return;
Rift rift = RiftRegistry.instance(world).getRift(new Location((ServerWorld) world, pos)); Rift rift = RiftRegistry.instance().getRift(new Location((ServerWorld) world, pos));
rift.isDetached = isDetached(); rift.isDetached = isDetached();
rift.markDirty(); rift.markDirty();
} }
public void handleTargetGone(Location location) { public void handleTargetGone(Location location) {
if (destination.shouldInvalidate(location)) { if (data.getDestination().shouldInvalidate(location)) {
destination = null; data.setDestination(null);
markDirty(); markDirty();
} }
@ -165,11 +157,11 @@ public abstract class RiftBlockEntity extends BlockEntity implements BlockEntity
} }
public Target getTarget() { public Target getTarget() {
if (destination == null) { if (data.getDestination() == null) {
return new MessageTarget("rifts.unlinked"); return new MessageTarget("rifts.unlinked");
} else { } else {
destination.setLocation(new Location((ServerWorld) world, pos)); data.getDestination().setLocation(new Location((ServerWorld) world, pos));
return destination; return data.getDestination();
} }
} }
@ -194,16 +186,16 @@ public abstract class RiftBlockEntity extends BlockEntity implements BlockEntity
} }
public void updateColor() { public void updateColor() {
if (forcedColor) return; if (data.isForcedColor()) return;
if (!isRegistered()) { if (!isRegistered()) {
color = new float[]{0, 0, 0, 1}; data.setColor(new RGBA(0, 0, 0, 1));
} else if (destination == null) { } else if (data.getDestination() == null) {
color = new float[]{0.7f, 0.7f, 0.7f, 1}; data.setColor(new RGBA(0.7f, 0.7f, 0.7f, 1));
} else { } else {
destination.setLocation(new Location((ServerWorld) world, pos)); data.getDestination().setLocation(new Location((ServerWorld) world, pos));
float[] newColor = destination.getColor(); RGBA newColor = data.getDestination().getColor();
if (color == null && newColor != null || !Arrays.equals(color, newColor)) { if (data.getColor() == null && newColor != null || !Objects.equals(data.getColor(), newColor)) {
color = newColor; data.setColor(newColor);
markDirty(); markDirty();
} }
} }
@ -212,29 +204,30 @@ public abstract class RiftBlockEntity extends BlockEntity implements BlockEntity
protected abstract boolean isDetached(); protected abstract boolean isDetached();
public void copyFrom(DetachedRiftBlockEntity rift) { public void copyFrom(DetachedRiftBlockEntity rift) {
destination = rift.destination;
properties = rift.properties; data.setDestination(rift.data.getDestination());
alwaysDelete = rift.alwaysDelete; data.setProperties(rift.data.getProperties());
forcedColor = rift.forcedColor; data.setAlwaysDelete(rift.data.isAlwaysDelete());
data.setForcedColor(rift.data.isForcedColor());
} }
public VirtualTarget getDestination() { public VirtualTarget getDestination() {
return destination; return data.getDestination();
} }
public LinkProperties getProperties() { public LinkProperties getProperties() {
return properties; return data.getProperties();
} }
public boolean isAlwaysDelete() { public boolean isAlwaysDelete() {
return alwaysDelete; return data.isAlwaysDelete();
} }
public boolean isForcedColor() { public boolean isForcedColor() {
return forcedColor; return data.isForcedColor();
} }
public float[] getColor() { public RGBA getColor() {
return color; return data.getColor();
} }
} }

View file

@ -0,0 +1,77 @@
package org.dimdev.dimdoors.block.entity;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import org.dimdev.annotatednbt.Saved;
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 -> {
return instance.group(
VirtualTarget.CODEC.optionalFieldOf("destination", null).forGetter(RiftData::getDestination),
LinkProperties.CODEC.optionalFieldOf("properties", null).forGetter(RiftData::getProperties),
Codec.BOOL.fieldOf("alwaysDelete").forGetter(RiftData::isAlwaysDelete),
Codec.BOOL.fieldOf("forcedColor").forGetter(RiftData::isForcedColor),
RGBA.CODEC.optionalFieldOf("color", null).forGetter(RiftData::getColor)
).apply(instance, RiftData::new);
});
private VirtualTarget destination; // How the rift acts as a source
private LinkProperties properties;
private boolean alwaysDelete;
private boolean forcedColor;
private RGBA color = null;
public RiftData() {}
private RiftData(VirtualTarget destination, LinkProperties properties, boolean alwaysDelete, boolean forcedColor, RGBA color) {
this.destination = destination;
this.properties = properties;
this.alwaysDelete = alwaysDelete;
this.forcedColor = forcedColor;
this.color = color;
}
public VirtualTarget getDestination() {
return destination;
}
public void setDestination(VirtualTarget destination) {
this.destination = destination;
}
public LinkProperties getProperties() {
return properties;
}
public void setProperties(LinkProperties properties) {
this.properties = properties;
}
public boolean isAlwaysDelete() {
return alwaysDelete;
}
public void setAlwaysDelete(boolean alwaysDelete) {
this.alwaysDelete = alwaysDelete;
}
public boolean isForcedColor() {
return forcedColor;
}
public void setForcedColor(boolean forcedColor) {
this.forcedColor = forcedColor;
}
public RGBA getColor() {
return color;
}
public void setColor(RGBA color) {
forcedColor = color != null;
this.color = color;
}
}

View file

@ -15,10 +15,12 @@ import net.minecraft.util.Identifier;
import net.fabricmc.api.EnvType; import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment; import net.fabricmc.api.Environment;
import org.dimdev.dimdoors.util.RGBA;
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public class DetachedRiftBlockEntityRenderer extends BlockEntityRenderer<DetachedRiftBlockEntity> { public class DetachedRiftBlockEntityRenderer extends BlockEntityRenderer<DetachedRiftBlockEntity> {
public static final Identifier TESSERACT_PATH = new Identifier("dimdoors:textures/other/tesseract.png"); public static final Identifier TESSERACT_PATH = new Identifier("dimdoors:textures/other/tesseract.png");
private RGBA COLOR = new RGBA(1, 0.5f, 1, 1);
private static final Tesseract TESSERACT = new Tesseract(); private static final Tesseract TESSERACT = new Tesseract();
private static final RiftCurves.PolygonInfo CURVE = RiftCurves.CURVES.get(0); private static final RiftCurves.PolygonInfo CURVE = RiftCurves.CURVES.get(0);
@ -51,8 +53,8 @@ public class DetachedRiftBlockEntityRenderer extends BlockEntityRenderer<Detache
private void renderTesseract(VertexConsumer vc, DetachedRiftBlockEntity rift, MatrixStack matrices, float tickDelta) { private void renderTesseract(VertexConsumer vc, DetachedRiftBlockEntity rift, MatrixStack matrices, float tickDelta) {
double radian = nextAngle(rift, tickDelta) * TrigMath.DEG_TO_RAD; double radian = nextAngle(rift, tickDelta) * TrigMath.DEG_TO_RAD;
float[] color = rift.getColor(); RGBA color = rift.getColor();
if (color == null) color = new float[]{1, 0.5f, 1, 1}; if (color == null) color = COLOR;
matrices.push(); matrices.push();

View file

@ -5,6 +5,7 @@ import com.flowpowered.math.vector.Vector3f;
import com.flowpowered.math.vector.Vector4f; import com.flowpowered.math.vector.Vector4f;
import net.minecraft.client.render.VertexConsumer; import net.minecraft.client.render.VertexConsumer;
import org.dimdev.dimdoors.util.RGBA;
import static com.flowpowered.math.TrigMath.cos; import static com.flowpowered.math.TrigMath.cos;
import static com.flowpowered.math.TrigMath.sin; import static com.flowpowered.math.TrigMath.sin;
@ -16,20 +17,20 @@ public class Plane {
vectors = new Vector4f[]{vec1, vec2, vec3, vec4}; vectors = new Vector4f[]{vec1, vec2, vec3, vec4};
} }
public void draw(VertexConsumer vc, float[] color, double radian) { public void draw(VertexConsumer vc, RGBA color, double radian) {
drawVertex(vc, rotYW(vectors[0], radian), 0, 0, color); drawVertex(vc, rotYW(vectors[0], radian), 0, 0, color);
drawVertex(vc, rotYW(vectors[1], radian), 0, 1, color); drawVertex(vc, rotYW(vectors[1], radian), 0, 1, color);
drawVertex(vc, rotYW(vectors[2], radian), 1, 1, color); drawVertex(vc, rotYW(vectors[2], radian), 1, 1, color);
drawVertex(vc, rotYW(vectors[3], radian), 1, 0, color); drawVertex(vc, rotYW(vectors[3], radian), 1, 0, color);
} }
private static void drawVertex(VertexConsumer vc, Vector4f vector, int u, int v, float[] color) { private static void drawVertex(VertexConsumer vc, Vector4f vector, int u, int v, RGBA color) {
double scalar = 1d / (vector.getW() + 1); double scalar = 1d / (vector.getW() + 1);
Vector3f scaled = vector.toVector3().mul(scalar); Vector3f scaled = vector.toVector3().mul(scalar);
vc.vertex(scaled.getX(), scaled.getY(), scaled.getZ()) vc.vertex(scaled.getX(), scaled.getY(), scaled.getZ())
.texture(u, v) .texture(u, v)
.color(color[0], color[1], color[2], color[3]) .color(color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha())
.next(); .next();
} }

View file

@ -6,6 +6,7 @@ import net.minecraft.client.render.VertexConsumer;
import net.fabricmc.api.EnvType; import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment; import net.fabricmc.api.Environment;
import org.dimdev.dimdoors.util.RGBA;
public class Tesseract { public class Tesseract {
private final Plane[] planes = new Plane[24]; private final Plane[] planes = new Plane[24];
@ -181,7 +182,7 @@ public class Tesseract {
} }
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public void draw(VertexConsumer vc, float[] color, double radian) { public void draw(VertexConsumer vc, RGBA color, double radian) {
for (Plane plane : planes) { for (Plane plane : planes) {
plane.draw(vc, color, radian); plane.draw(vc, color, radian);
} }

View file

@ -61,8 +61,8 @@ public class PocketCommand {
Pocket pocket = PocketGenerator.generatePocketFromTemplate(player.getServerWorld(), template, null, setup); Pocket pocket = PocketGenerator.generatePocketFromTemplate(player.getServerWorld(), template, null, setup);
// Teleport the player there // Teleport the player there
if (RiftRegistry.instance(player.world).getPocketEntrance(pocket) != null) { if (RiftRegistry.instance().getPocketEntrance(pocket) != null) {
EntityTarget entrance = (EntityTarget) player.world.getBlockEntity(RiftRegistry.instance(player.world).getPocketEntrance(pocket).pos); EntityTarget entrance = (EntityTarget) player.world.getBlockEntity(RiftRegistry.instance().getPocketEntrance(pocket).pos);
if (entrance != null) { if (entrance != null) {
entrance.receiveEntity(player, 0); entrance.receiveEntity(player, 0);
} }

View file

@ -27,7 +27,7 @@ public class SaveSchemCommand {
throw new CommandException(new TranslatableText("commands.generic.dimdoors.not_in_pocket")); throw new CommandException(new TranslatableText("commands.generic.dimdoors.not_in_pocket"));
} }
Pocket pocket = PocketRegistry.instance(player.getServerWorld()).getPocketAt(player.getBlockPos()); Pocket pocket = PocketRegistry.instance(player.getServerWorld().getRegistryKey()).getPocketAt(player.getBlockPos());
if (pocket == null) if (pocket == null)
throw new CommandException(new TranslatableText("commands.generic.dimdoors.not_in_pocket")); throw new CommandException(new TranslatableText("commands.generic.dimdoors.not_in_pocket"));

View file

@ -28,6 +28,8 @@ import net.minecraft.world.World;
import net.fabricmc.api.EnvType; import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment; import net.fabricmc.api.Environment;
import org.dimdev.dimdoors.util.WorldUtil;
import org.jetbrains.annotations.NotNull;
public class RiftSignatureItem extends Item { public class RiftSignatureItem extends Item {
public static final String ID = "rift_signature"; public static final String ID = "rift_signature";
@ -42,7 +44,7 @@ public class RiftSignatureItem extends Item {
} }
@Override @Override
public ActionResult useOnBlock(ItemUsageContext itemUsageContext) { public ActionResult useOnBlock(@NotNull ItemUsageContext itemUsageContext) {
PlayerEntity player = itemUsageContext.getPlayer(); PlayerEntity player = itemUsageContext.getPlayer();
World world = itemUsageContext.getWorld(); World world = itemUsageContext.getWorld();
BlockPos pos = itemUsageContext.getBlockPos(); BlockPos pos = itemUsageContext.getBlockPos();
@ -67,7 +69,7 @@ public class RiftSignatureItem extends Item {
if (target == null) { if (target == null) {
// The link signature has not been used. Store its current target as the first location. // The link signature has not been used. Store its current target as the first location.
setSource(stack, new RotatedLocation((ServerWorld) world, pos, player.yaw, 0)); setSource(stack, new RotatedLocation(world.getRegistryKey(), pos, player.yaw, 0));
player.sendMessage(new TranslatableText(getTranslationKey() + ".stored"), true); player.sendMessage(new TranslatableText(getTranslationKey() + ".stored"), true);
world.playSound(null, player.getBlockPos(), ModSoundEvents.RIFT_START, SoundCategory.BLOCKS, 0.6f, 1); world.playSound(null, player.getBlockPos(), ModSoundEvents.RIFT_START, SoundCategory.BLOCKS, 0.6f, 1);
} else { } else {
@ -78,7 +80,7 @@ public class RiftSignatureItem extends Item {
clearSource(stack); // TODO: But is this fair? It's a rather hidden way of unbinding your signature! clearSource(stack); // TODO: But is this fair? It's a rather hidden way of unbinding your signature!
return ActionResult.FAIL; return ActionResult.FAIL;
} }
World sourceWorld = target.world; World sourceWorld = WorldUtil.getWorld(target.world);
sourceWorld.setBlockState(target.getBlockPos(), ModBlocks.DETACHED_RIFT.getDefaultState()); sourceWorld.setBlockState(target.getBlockPos(), ModBlocks.DETACHED_RIFT.getDefaultState());
DetachedRiftBlockEntity rift1 = (DetachedRiftBlockEntity) target.getBlockEntity(); DetachedRiftBlockEntity rift1 = (DetachedRiftBlockEntity) target.getBlockEntity();
rift1.setDestination(RiftReference.tryMakeRelative(target, new Location((ServerWorld) world, pos))); rift1.setDestination(RiftReference.tryMakeRelative(target, new Location((ServerWorld) world, pos)));
@ -105,7 +107,7 @@ public class RiftSignatureItem extends Item {
public static void setSource(ItemStack itemStack, RotatedLocation destination) { public static void setSource(ItemStack itemStack, RotatedLocation destination) {
if (!itemStack.hasTag()) itemStack.setTag(new CompoundTag()); if (!itemStack.hasTag()) itemStack.setTag(new CompoundTag());
itemStack.getTag().put("destination", destination.serialize()); itemStack.getTag().put("destination", RotatedLocation.serialize(destination));
} }
public static void clearSource(ItemStack itemStack) { public static void clearSource(ItemStack itemStack) {

View file

@ -25,6 +25,7 @@ import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction; import net.minecraft.util.math.Direction;
import net.minecraft.world.World; import net.minecraft.world.World;
import org.dimdev.dimdoors.util.WorldUtil;
public class StabilizedRiftSignatureItem extends Item { // TODO: common superclass with rift signature public class StabilizedRiftSignatureItem extends Item { // TODO: common superclass with rift signature
public static final String ID = "stabilized_rift_signature"; public static final String ID = "stabilized_rift_signature";
@ -63,7 +64,7 @@ public class StabilizedRiftSignatureItem extends Item { // TODO: common supercla
if (target == null) { if (target == null) {
// The link signature has not been used. Store its current target as the first location. // The link signature has not been used. Store its current target as the first location.
setSource(stack, new RotatedLocation((ServerWorld) world, pos, player.yaw, 0)); setSource(stack, new RotatedLocation(world.getRegistryKey(), pos, player.yaw, 0));
player.sendMessage(new TranslatableText(getTranslationKey() + ".stored"), true); player.sendMessage(new TranslatableText(getTranslationKey() + ".stored"), true);
world.playSound(null, player.getBlockPos(), ModSoundEvents.RIFT_START, SoundCategory.BLOCKS, 0.6f, 1); world.playSound(null, player.getBlockPos(), ModSoundEvents.RIFT_START, SoundCategory.BLOCKS, 0.6f, 1);
} else { } else {
@ -74,7 +75,7 @@ public class StabilizedRiftSignatureItem extends Item { // TODO: common supercla
// Don't clear source, stabilized signatures always stay bound // Don't clear source, stabilized signatures always stay bound
return ActionResult.FAIL; return ActionResult.FAIL;
} }
World targetWorld = target.world; World targetWorld = WorldUtil.getWorld(target.world);
targetWorld.setBlockState(target.getBlockPos(), ModBlocks.DETACHED_RIFT.getDefaultState()); targetWorld.setBlockState(target.getBlockPos(), ModBlocks.DETACHED_RIFT.getDefaultState());
DetachedRiftBlockEntity rift1 = (DetachedRiftBlockEntity) target.getBlockEntity(); DetachedRiftBlockEntity rift1 = (DetachedRiftBlockEntity) target.getBlockEntity();
rift1.register(); rift1.register();
@ -98,7 +99,7 @@ public class StabilizedRiftSignatureItem extends Item { // TODO: common supercla
public static void setSource(ItemStack itemStack, RotatedLocation destination) { public static void setSource(ItemStack itemStack, RotatedLocation destination) {
if (!itemStack.hasTag()) itemStack.setTag(new CompoundTag()); if (!itemStack.hasTag()) itemStack.setTag(new CompoundTag());
itemStack.getTag().put("destination", destination.serialize()); itemStack.getTag().put("destination", RotatedLocation.serialize(destination));
} }
public static void clearSource(ItemStack itemStack) { public static void clearSource(ItemStack itemStack) {
@ -109,8 +110,7 @@ public class StabilizedRiftSignatureItem extends Item { // TODO: common supercla
public static RotatedLocation getTarget(ItemStack itemStack) { public static RotatedLocation getTarget(ItemStack itemStack) {
if (itemStack.hasTag() && itemStack.getTag().contains("destination")) { if (itemStack.hasTag() && itemStack.getTag().contains("destination")) {
RotatedLocation transform = RotatedLocation.deserialize(itemStack.getTag().getCompound("destination")); return RotatedLocation.deserialize(itemStack.getTag().getCompound("destination"));
return transform;
} else { } else {
return null; return null;
} }

View file

@ -4,9 +4,11 @@ import java.util.Random;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.dimdev.dimdoors.DimensionalDoorsInitializer;
import org.dimdev.dimdoors.ModConfig; import org.dimdev.dimdoors.ModConfig;
import org.dimdev.dimdoors.rift.registry.LinkProperties; import org.dimdev.dimdoors.rift.registry.LinkProperties;
import org.dimdev.dimdoors.rift.targets.VirtualTarget; import org.dimdev.dimdoors.rift.targets.VirtualTarget;
import org.dimdev.dimdoors.util.WorldUtil;
import org.dimdev.dimdoors.world.ModDimensions; import org.dimdev.dimdoors.world.ModDimensions;
import org.dimdev.dimdoors.world.pocket.Pocket; import org.dimdev.dimdoors.world.pocket.Pocket;
import org.dimdev.dimdoors.world.pocket.PocketRegistry; import org.dimdev.dimdoors.world.pocket.PocketRegistry;
@ -20,7 +22,7 @@ public final class PocketGenerator {
private static Pocket prepareAndPlacePocket(ServerWorld world, PocketTemplate pocketTemplate, VirtualLocation virtualLocation, boolean setup) { private static Pocket prepareAndPlacePocket(ServerWorld world, PocketTemplate pocketTemplate, VirtualLocation virtualLocation, boolean setup) {
LOGGER.info("Generating pocket from template " + pocketTemplate.getId() + " at virtual location " + virtualLocation); LOGGER.info("Generating pocket from template " + pocketTemplate.getId() + " at virtual location " + virtualLocation);
Pocket pocket = PocketRegistry.instance(world).newPocket(); Pocket pocket = PocketRegistry.instance(world.getRegistryKey()).newPocket();
pocketTemplate.place(pocket, setup); pocketTemplate.place(pocket, setup);
pocket.virtualLocation = virtualLocation; pocket.virtualLocation = virtualLocation;
return pocket; return pocket;
@ -40,13 +42,13 @@ public final class PocketGenerator {
public static Pocket generatePrivatePocket(VirtualLocation virtualLocation) { public static Pocket generatePrivatePocket(VirtualLocation virtualLocation) {
PocketTemplate pocketTemplate = SchematicHandler.INSTANCE.getPersonalPocketTemplate(); PocketTemplate pocketTemplate = SchematicHandler.INSTANCE.getPersonalPocketTemplate();
return generatePocketFromTemplate(virtualLocation.world.getServer().getWorld(ModDimensions.PERSONAL), pocketTemplate, virtualLocation, true); return generatePocketFromTemplate(WorldUtil.getWorld(ModDimensions.PERSONAL), pocketTemplate, virtualLocation, true);
} }
// TODO: size of public pockets should increase with depth // TODO: size of public pockets should increase with depth
public static Pocket generatePublicPocket(VirtualLocation virtualLocation, VirtualTarget linkTo, LinkProperties linkProperties) { public static Pocket generatePublicPocket(VirtualLocation virtualLocation, VirtualTarget linkTo, LinkProperties linkProperties) {
PocketTemplate pocketTemplate = SchematicHandler.INSTANCE.getPublicPocketTemplate(); PocketTemplate pocketTemplate = SchematicHandler.INSTANCE.getPublicPocketTemplate();
return generatePocketFromTemplate(virtualLocation.world.getServer().getWorld(ModDimensions.PUBLIC), pocketTemplate, virtualLocation, linkTo, linkProperties); return generatePocketFromTemplate(WorldUtil.getWorld(ModDimensions.PUBLIC), pocketTemplate, virtualLocation, linkTo, linkProperties);
} }
/** /**
@ -57,11 +59,11 @@ public final class PocketGenerator {
*/ */
public static Pocket generateDungeonPocket(VirtualLocation virtualLocation, VirtualTarget linkTo, LinkProperties linkProperties) { public static Pocket generateDungeonPocket(VirtualLocation virtualLocation, VirtualTarget linkTo, LinkProperties linkProperties) {
int depth = virtualLocation.depth; int depth = virtualLocation.depth;
float netherProbability = virtualLocation.world.getDimension().isUltrawarm() ? 1 : (float) depth / 200; // TODO: improve nether probability float netherProbability = WorldUtil.getWorld(virtualLocation.world).getDimension().isUltrawarm() ? 1 : (float) depth / 200; // TODO: improve nether probability
Random random = new Random(); Random random = new Random();
String group = random.nextFloat() < netherProbability ? "nether" : "ruins"; String group = random.nextFloat() < netherProbability ? "nether" : "ruins";
PocketTemplate pocketTemplate = SchematicHandler.INSTANCE.getRandomTemplate(group, depth, ModConfig.POCKETS.maxPocketSize, false); PocketTemplate pocketTemplate = SchematicHandler.INSTANCE.getRandomTemplate(group, depth, ModConfig.POCKETS.maxPocketSize, false);
return generatePocketFromTemplate(virtualLocation.world.getServer().getWorld(ModDimensions.DUNGEON), pocketTemplate, virtualLocation, linkTo, linkProperties); return generatePocketFromTemplate(WorldUtil.getWorld(ModDimensions.DUNGEON), pocketTemplate, virtualLocation, linkTo, linkProperties);
} }
} }

View file

@ -18,6 +18,7 @@ import org.dimdev.dimdoors.rift.targets.PocketEntranceMarker;
import org.dimdev.dimdoors.rift.targets.PocketExitMarker; import org.dimdev.dimdoors.rift.targets.PocketExitMarker;
import org.dimdev.dimdoors.rift.targets.VirtualTarget; import org.dimdev.dimdoors.rift.targets.VirtualTarget;
import org.dimdev.dimdoors.util.Location; import org.dimdev.dimdoors.util.Location;
import org.dimdev.dimdoors.util.WorldUtil;
import org.dimdev.dimdoors.util.math.MathUtil; import org.dimdev.dimdoors.util.math.MathUtil;
import org.dimdev.dimdoors.world.pocket.Pocket; import org.dimdev.dimdoors.world.pocket.Pocket;
import org.dimdev.dimdoors.world.pocket.PocketRegistry; import org.dimdev.dimdoors.world.pocket.PocketRegistry;
@ -174,7 +175,7 @@ public class PocketTemplate {
public void place(Pocket pocket, boolean setup) { public void place(Pocket pocket, boolean setup) {
pocket.setSize(size * 16, size * 16, size * 16); pocket.setSize(size * 16, size * 16, size * 16);
int gridSize = PocketRegistry.instance(pocket.world).getGridSize(); int gridSize = PocketRegistry.instance(pocket.world).getGridSize();
ServerWorld world = pocket.world; ServerWorld world = WorldUtil.getWorld(pocket.world);
int xBase = pocket.box.minX; int xBase = pocket.box.minX;
int yBase = pocket.box.minY; int yBase = pocket.box.minY;
int zBase = pocket.box.minZ; int zBase = pocket.box.minZ;
@ -199,7 +200,7 @@ public class PocketTemplate {
public void setup(Pocket pocket, VirtualTarget linkTo, LinkProperties linkProperties) { public void setup(Pocket pocket, VirtualTarget linkTo, LinkProperties linkProperties) {
int gridSize = PocketRegistry.instance(pocket.world).getGridSize(); int gridSize = PocketRegistry.instance(pocket.world).getGridSize();
ServerWorld world = pocket.world; ServerWorld world = WorldUtil.getWorld(pocket.world);
int xBase = pocket.box.minX; int xBase = pocket.box.minX;
int yBase = pocket.box.minY; int yBase = pocket.box.minY;
int zBase = pocket.box.minZ; int zBase = pocket.box.minZ;
@ -267,10 +268,10 @@ public class PocketTemplate {
VirtualTarget dest = rift.getDestination(); VirtualTarget dest = rift.getDestination();
if (dest instanceof PocketEntranceMarker) { if (dest instanceof PocketEntranceMarker) {
if (rift == selectedEntrance) { if (rift == selectedEntrance) {
PocketRegistry.instance(world).markDirty(); PocketRegistry.instance(world.getRegistryKey()).markDirty();
rift.setDestination(((PocketEntranceMarker) dest).getIfDestination()); rift.setDestination(((PocketEntranceMarker) dest).getIfDestination());
rift.register(); rift.register();
RiftRegistry.instance(world).addPocketEntrance(pocket, new Location((ServerWorld) rift.getWorld(), rift.getPos())); RiftRegistry.instance().addPocketEntrance(pocket, new Location((ServerWorld) rift.getWorld(), rift.getPos()));
} else { } else {
rift.setDestination(((PocketEntranceMarker) dest).getOtherwiseDestination()); rift.setDestination(((PocketEntranceMarker) dest).getOtherwiseDestination());
} }

View file

@ -1,18 +1,44 @@
package org.dimdev.dimdoors.rift.registry; package org.dimdev.dimdoors.rift.registry;
import java.util.Objects; import java.util.*;
import java.util.Set;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.util.dynamic.DynamicSerializableUuid;
import org.dimdev.annotatednbt.Saved; import org.dimdev.annotatednbt.Saved;
import org.dimdev.dimdoors.util.Codecs;
public class LinkProperties { public class LinkProperties {
public static Codec<LinkProperties> CODEC = RecordCodecBuilder.create(instance -> {
return instance.group(
Codec.FLOAT
.fieldOf("floatingWeight")
.forGetter(
properties ->
properties.
floatingWeight),
Codec.FLOAT
.fieldOf("entranceWeight")
.forGetter(properties -> properties.entranceWeight),
Codecs.INT_SET
.fieldOf("groups")
.forGetter(properties -> properties.groups),
Codec.INT
.fieldOf("linksRemaining")
.forGetter(properties -> properties.linksRemaining),
Codec.BOOL
.fieldOf("oneWay")
.forGetter(properties -> properties.oneWay)
).apply(instance, LinkProperties::new);
});
@Saved @Saved
public float floatingWeight; // TODO: depend on rift properties (ex. size, stability, or maybe a getWeightFactor method) rather than rift type public float floatingWeight; // TODO: depend on rift properties (ex. size, stability, or maybe a getWeightFactor method) rather than rift type
@Saved @Saved
public float entranceWeight; public float entranceWeight;
@Saved @Saved
public Set<Integer> groups; public Set<Integer> groups = new HashSet<>();
@Saved @Saved
public int linksRemaining; public int linksRemaining;
@Saved @Saved

View file

@ -1,5 +1,6 @@
package org.dimdev.dimdoors.rift.registry; package org.dimdev.dimdoors.rift.registry;
import net.minecraft.util.registry.RegistryKey;
import org.dimdev.annotatednbt.AnnotatedNbt; import org.dimdev.annotatednbt.AnnotatedNbt;
import org.dimdev.annotatednbt.Saved; import org.dimdev.annotatednbt.Saved;
@ -8,11 +9,11 @@ import net.minecraft.world.World;
public class PocketEntrancePointer extends RegistryVertex { // TODO: PocketRiftPointer superclass? public class PocketEntrancePointer extends RegistryVertex { // TODO: PocketRiftPointer superclass?
@Saved @Saved
public World pocketDim; public RegistryKey<World> pocketDim;
@Saved @Saved
public int pocketId; public int pocketId;
public PocketEntrancePointer(World pocketDim, int pocketId) { public PocketEntrancePointer(RegistryKey<World> pocketDim, int pocketId) {
this.pocketDim = pocketDim; this.pocketDim = pocketDim;
this.pocketId = pocketId; this.pocketId = pocketId;
} }

View file

@ -2,6 +2,8 @@ package org.dimdev.dimdoors.rift.registry;
import java.util.UUID; import java.util.UUID;
import net.minecraft.util.registry.RegistryKey;
import net.minecraft.world.World;
import org.dimdev.annotatednbt.AnnotatedNbt; import org.dimdev.annotatednbt.AnnotatedNbt;
import org.dimdev.annotatednbt.Saved; import org.dimdev.annotatednbt.Saved;
@ -9,7 +11,7 @@ import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.world.ServerWorld; import net.minecraft.server.world.ServerWorld;
public abstract class RegistryVertex { public abstract class RegistryVertex {
public ServerWorld world; // The dimension to store this object in. Links are stored in both registries. public RegistryKey<World> world; // The dimension to store this object in. Links are stored in both registries.
@Saved @Saved
public UUID id = UUID.randomUUID(); // Used to create pointers to registry vertices. Should not be used for anything other than saving. public UUID id = UUID.randomUUID(); // Used to create pointers to registry vertices. Should not be used for anything other than saving.

View file

@ -57,8 +57,8 @@ public class Rift extends RegistryVertex {
public void markDirty() { public void markDirty() {
((RiftBlockEntity) location.getBlockEntity()).updateColor(); ((RiftBlockEntity) location.getBlockEntity()).updateColor();
for (Location location : RiftRegistry.instance(world).getSources(location)) { for (Location location : RiftRegistry.instance().getSources(location)) {
RiftRegistry.instance(world).getRift(location).targetChanged(this); RiftRegistry.instance().getRift(location).targetChanged(this);
} }
} }

View file

@ -21,11 +21,11 @@ import org.jgrapht.graph.DefaultEdge;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag; import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.Tag; import net.minecraft.nbt.Tag;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.PersistentState; import net.minecraft.world.PersistentState;
import net.minecraft.world.World; import net.minecraft.world.World;
import static net.minecraft.world.World.OVERWORLD; import static net.minecraft.world.World.OVERWORLD;
import static org.dimdev.dimdoors.DimensionalDoorsInitializer.*;
public class RiftRegistry extends PersistentState { public class RiftRegistry extends PersistentState {
private static final Logger LOGGER = LogManager.getLogger(); private static final Logger LOGGER = LogManager.getLogger();
@ -46,12 +46,8 @@ public class RiftRegistry extends PersistentState {
this.overworld = overworld; this.overworld = overworld;
} }
public static RiftRegistry instance(World world) { public static RiftRegistry instance() {
return instance(world.getServer()); return getWorld(OVERWORLD).getPersistentStateManager().getOrCreate(() -> new RiftRegistry(getWorld(OVERWORLD)), DATA_NAME);
}
private static RiftRegistry instance(MinecraftServer server) {
return server.getWorld(OVERWORLD).getPersistentStateManager().getOrCreate(() -> new RiftRegistry(server.getWorld(OVERWORLD)), DATA_NAME);
} }
@Override @Override
@ -318,7 +314,7 @@ public class RiftRegistry extends PersistentState {
if (entrance != null) return entrance.location; if (entrance != null) return entrance.location;
// If there was no last used private entrance, get the first player's private pocket entrance // If there was no last used private entrance, get the first player's private pocket entrance
return getPocketEntrance(PrivatePocketData.instance(overworld).getPrivatePocket(playerUUID)); return getPocketEntrance(PrivatePocketData.instance().getPrivatePocket(playerUUID));
} }
private void setPlayerRiftPointer(UUID playerUUID, Location rift, Map<UUID, PlayerRiftPointer> map) { private void setPlayerRiftPointer(UUID playerUUID, Location rift, Map<UUID, PlayerRiftPointer> map) {

View file

@ -2,6 +2,8 @@ package org.dimdev.dimdoors.rift.targets;
import java.util.UUID; import java.util.UUID;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import org.dimdev.dimdoors.block.entity.RiftBlockEntity; import org.dimdev.dimdoors.block.entity.RiftBlockEntity;
import org.dimdev.dimdoors.rift.registry.RiftRegistry; import org.dimdev.dimdoors.rift.registry.RiftRegistry;
import org.dimdev.dimdoors.util.Location; import org.dimdev.dimdoors.util.Location;
@ -10,30 +12,24 @@ import org.dimdev.dimdoors.world.ModDimensions;
import org.dimdev.dimdoors.world.pocket.VirtualLocation; import org.dimdev.dimdoors.world.pocket.VirtualLocation;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.world.ServerWorld; import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.TranslatableText; import net.minecraft.text.TranslatableText;
import static org.dimdev.dimdoors.util.EntityUtils.chat; import static org.dimdev.dimdoors.util.EntityUtils.chat;
public class EscapeTarget extends VirtualTarget implements EntityTarget { // TODO: createRift option public class EscapeTarget extends VirtualTarget implements EntityTarget { // TODO: createRift option
public static final Codec<EscapeTarget> CODEC = RecordCodecBuilder.create(instance -> {
return instance.group(
Codec.BOOL.fieldOf("canEscapeLimbo").forGetter(target -> target.canEscapeLimbo)
).apply(instance, EscapeTarget::new);
});
protected boolean canEscapeLimbo = false; protected boolean canEscapeLimbo = false;
public EscapeTarget(boolean canEscapeLimbo) { public EscapeTarget(boolean canEscapeLimbo) {
this.canEscapeLimbo = canEscapeLimbo; this.canEscapeLimbo = canEscapeLimbo;
} }
@Override
public void fromTag(CompoundTag nbt) {
super.fromTag(nbt);
}
@Override
public CompoundTag toTag(CompoundTag nbt) {
nbt = super.toTag(nbt);
return nbt;
}
@Override @Override
public boolean receiveEntity(Entity entity, float yawOffset) { public boolean receiveEntity(Entity entity, float yawOffset) {
if (!ModDimensions.isDimDoorsPocketDimension(entity.world) && !(ModDimensions.isLimboDimension(entity.world))) { if (!ModDimensions.isDimDoorsPocketDimension(entity.world) && !(ModDimensions.isLimboDimension(entity.world))) {
@ -47,10 +43,10 @@ public class EscapeTarget extends VirtualTarget implements EntityTarget { // TOD
UUID uuid = entity.getUuid(); UUID uuid = entity.getUuid();
if (uuid != null) { if (uuid != null) {
Location destLoc = RiftRegistry.instance(entity.world).getOverworldRift(uuid); Location destLoc = RiftRegistry.instance().getOverworldRift(uuid);
if (destLoc != null && destLoc.getBlockEntity() instanceof RiftBlockEntity || canEscapeLimbo) { if (destLoc != null && destLoc.getBlockEntity() instanceof RiftBlockEntity || canEscapeLimbo) {
Location location = VirtualLocation.fromLocation(new Location((ServerWorld) entity.world, entity.getBlockPos())).projectToWorld(false); Location location = VirtualLocation.fromLocation(new Location((ServerWorld) entity.world, entity.getBlockPos())).projectToWorld(false);
TeleportUtil.teleport(entity, location.world, location.pos, 0); TeleportUtil.teleport(entity, location.getWorld(), location.getBlockPos(), 0);
} else { } else {
if (destLoc == null) { if (destLoc == null) {
chat(entity, new TranslatableText("rifts.destinations.escape.did_not_use_rift")); chat(entity, new TranslatableText("rifts.destinations.escape.did_not_use_rift"));
@ -69,4 +65,9 @@ public class EscapeTarget extends VirtualTarget implements EntityTarget { // TOD
return false; // No escape info for that entity return false; // No escape info for that entity
} }
} }
@Override
public VirtualTargetType<? extends VirtualTarget> getType() {
return VirtualTargetType.ESCAPE;
}
} }

View file

@ -1,33 +1,32 @@
package org.dimdev.dimdoors.rift.targets; package org.dimdev.dimdoors.rift.targets;
import com.mojang.datafixers.util.Pair;
import com.mojang.serialization.Codec;
import net.minecraft.nbt.NbtOps;
import org.dimdev.annotatednbt.AnnotatedNbt; import org.dimdev.annotatednbt.AnnotatedNbt;
import org.dimdev.annotatednbt.Saved; import org.dimdev.annotatednbt.Saved;
import org.dimdev.dimdoors.util.Location; import org.dimdev.dimdoors.util.Location;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import java.util.function.Function;
public class GlobalReference extends RiftReference { public class GlobalReference extends RiftReference {
@Saved public static Codec<GlobalReference> CODEC = Location.CODEC.fieldOf("location").xmap(GlobalReference::new, GlobalReference::getReferencedLocation).codec();
protected Location target; protected Location target;
public GlobalReference(Location target) { public GlobalReference(Location target) {
this.target = target; this.target = target;
} }
@Override
public void fromTag(CompoundTag nbt) {
super.fromTag(nbt);
AnnotatedNbt.save(this, nbt);
}
@Override
public CompoundTag toTag(CompoundTag nbt) {
nbt = super.toTag(nbt);
return AnnotatedNbt.serialize(this);
}
@Override @Override
public Location getReferencedLocation() { public Location getReferencedLocation() {
return target; return target;
} }
@Override
public VirtualTargetType<? extends VirtualTarget> getType() {
return VirtualTargetType.GLOBAL;
}
} }

View file

@ -1,26 +1,24 @@
package org.dimdev.dimdoors.rift.targets; package org.dimdev.dimdoors.rift.targets;
import com.mojang.serialization.Codec;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import org.dimdev.dimdoors.util.TeleportUtil;
public class LimboTarget extends VirtualTarget implements EntityTarget { public class LimboTarget extends VirtualTarget implements EntityTarget {
public static final Codec<LimboTarget> CODEC = Codec.unit(LimboTarget::new);
public LimboTarget() { public LimboTarget() {
} }
@Override
public void fromTag(CompoundTag nbt) {
super.fromTag(nbt);
}
@Override
public CompoundTag toTag(CompoundTag nbt) {
nbt = super.toTag(nbt);
return nbt;
}
@Override @Override
public boolean receiveEntity(Entity entity, float yawOffset) { public boolean receiveEntity(Entity entity, float yawOffset) {
//FabricDimensions.teleport(entity, entity.getServer().getWorld(LIMBO)); //FabricDimensions.teleport(entity, entity.getServer().getWorld(LIMBO));
return true; return true;
} }
@Override
public VirtualTargetType<? extends VirtualTarget> getType() {
return VirtualTargetType.LIMBO;
}
} }

View file

@ -1,5 +1,6 @@
package org.dimdev.dimdoors.rift.targets; package org.dimdev.dimdoors.rift.targets;
import com.mojang.serialization.Codec;
import org.dimdev.annotatednbt.AnnotatedNbt; import org.dimdev.annotatednbt.AnnotatedNbt;
import org.dimdev.annotatednbt.Saved; import org.dimdev.annotatednbt.Saved;
import org.dimdev.dimdoors.util.Location; import org.dimdev.dimdoors.util.Location;
@ -8,6 +9,8 @@ import net.minecraft.nbt.CompoundTag;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
public class LocalReference extends RiftReference { public class LocalReference extends RiftReference {
public static final Codec<LocalReference> CODEC = BlockPos.field_25064.xmap(LocalReference::new, LocalReference::getTarget).fieldOf("target").codec();
@Saved @Saved
protected BlockPos target; protected BlockPos target;
@ -15,18 +18,6 @@ public class LocalReference extends RiftReference {
this.target = target; this.target = target;
} }
@Override
public void fromTag(CompoundTag nbt) {
super.fromTag(nbt);
AnnotatedNbt.load(this, nbt);
}
@Override
public CompoundTag toTag(CompoundTag nbt) {
nbt = super.toTag(nbt);
return AnnotatedNbt.serialize(this);
}
@Override @Override
public Location getReferencedLocation() { public Location getReferencedLocation() {
return new Location(location.world, target); return new Location(location.world, target);
@ -35,4 +26,9 @@ public class LocalReference extends RiftReference {
public BlockPos getTarget() { public BlockPos getTarget() {
return target; return target;
} }
@Override
public VirtualTargetType<? extends VirtualTarget> getType() {
return VirtualTargetType.LOCAL;
}
} }

View file

@ -1,5 +1,7 @@
package org.dimdev.dimdoors.rift.targets; package org.dimdev.dimdoors.rift.targets;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import org.dimdev.annotatednbt.AnnotatedNbt; import org.dimdev.annotatednbt.AnnotatedNbt;
import org.dimdev.annotatednbt.Saved; import org.dimdev.annotatednbt.Saved;
import org.dimdev.dimdoors.util.EntityUtils; import org.dimdev.dimdoors.util.EntityUtils;
@ -10,10 +12,17 @@ import net.minecraft.text.TranslatableText;
public class PocketEntranceMarker extends VirtualTarget implements EntityTarget { public class PocketEntranceMarker extends VirtualTarget implements EntityTarget {
@Saved 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 float weight = 1;
/*@Saved*/ protected VirtualTarget ifDestination; protected VirtualTarget ifDestination;
/*@Saved*/ protected VirtualTarget otherwiseDestination; protected VirtualTarget otherwiseDestination;
public PocketEntranceMarker() { public PocketEntranceMarker() {
} }
@ -28,24 +37,6 @@ public class PocketEntranceMarker extends VirtualTarget implements EntityTarget
return new PocketEntranceMarkerBuilder(); return new PocketEntranceMarkerBuilder();
} }
@Override
public void fromTag(CompoundTag nbt) {
super.fromTag(nbt);
ifDestination = nbt.contains("ifDestination") ? VirtualTarget.readVirtualTargetNBT(nbt.getCompound("ifDestination")) : null;
otherwiseDestination = nbt.contains("otherwiseDestination") ? VirtualTarget.readVirtualTargetNBT(nbt.getCompound("otherwiseDestination")) : null;
AnnotatedNbt.load(this, nbt);
}
@Override
public CompoundTag toTag(CompoundTag nbt) {
nbt = super.toTag(nbt);
if (ifDestination != null) nbt.put("ifDestination", ifDestination.toTag(new CompoundTag()));
if (otherwiseDestination != null)
nbt.put("otherwiseDestination", otherwiseDestination.toTag(new CompoundTag()));
AnnotatedNbt.save(this, nbt);
return nbt;
}
@Override @Override
public boolean receiveEntity(Entity entity, float yawOffset) { public boolean receiveEntity(Entity entity, float yawOffset) {
EntityUtils.chat(entity, new TranslatableText("The entrance of this dungeon has not been converted. If this is a normally generated pocket, please report this bug.")); EntityUtils.chat(entity, new TranslatableText("The entrance of this dungeon has not been converted. If this is a normally generated pocket, please report this bug."));
@ -72,6 +63,11 @@ public class PocketEntranceMarker extends VirtualTarget implements EntityTarget
return new PocketEntranceMarkerBuilder().weight(weight).ifDestination(ifDestination).otherwiseDestination(otherwiseDestination); return new PocketEntranceMarkerBuilder().weight(weight).ifDestination(ifDestination).otherwiseDestination(otherwiseDestination);
} }
@Override
public VirtualTargetType<? extends VirtualTarget> getType() {
return VirtualTargetType.POCKET_ENTRANCE;
}
public static class PocketEntranceMarkerBuilder { public static class PocketEntranceMarkerBuilder {
private float weight; private float weight;
private VirtualTarget ifDestination; private VirtualTarget ifDestination;

View file

@ -1,5 +1,6 @@
package org.dimdev.dimdoors.rift.targets; package org.dimdev.dimdoors.rift.targets;
import com.mojang.serialization.Codec;
import org.dimdev.dimdoors.util.EntityUtils; import org.dimdev.dimdoors.util.EntityUtils;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
@ -7,23 +8,19 @@ import net.minecraft.nbt.CompoundTag;
import net.minecraft.text.TranslatableText; import net.minecraft.text.TranslatableText;
public class PocketExitMarker extends VirtualTarget implements EntityTarget { public class PocketExitMarker extends VirtualTarget implements EntityTarget {
public static final Codec<PocketExitMarker> CODEC = Codec.unit(PocketExitMarker::new);
public PocketExitMarker() { public PocketExitMarker() {
} }
@Override
public void fromTag(CompoundTag nbt) {
super.fromTag(nbt);
}
@Override
public CompoundTag toTag(CompoundTag nbt) {
nbt = super.toTag(nbt);
return nbt;
}
@Override @Override
public boolean receiveEntity(Entity entity, float yawOffset) { public boolean receiveEntity(Entity entity, float yawOffset) {
EntityUtils.chat(entity, new TranslatableText("The exit of this dungeon has not been linked. If this is a normally generated pocket, please report this bug.")); EntityUtils.chat(entity, new TranslatableText("The exit of this dungeon has not been linked. If this is a normally generated pocket, please report this bug."));
return false; return false;
} }
@Override
public VirtualTargetType<? extends VirtualTarget> getType() {
return VirtualTargetType.POCKET_EXIT;
}
} }

View file

@ -2,46 +2,39 @@ package org.dimdev.dimdoors.rift.targets;
import java.util.UUID; import java.util.UUID;
import com.mojang.serialization.Codec;
import org.dimdev.dimdoors.block.entity.RiftBlockEntity; import org.dimdev.dimdoors.block.entity.RiftBlockEntity;
import org.dimdev.dimdoors.rift.registry.RiftRegistry; import org.dimdev.dimdoors.rift.registry.RiftRegistry;
import org.dimdev.dimdoors.util.EntityUtils; import org.dimdev.dimdoors.util.EntityUtils;
import org.dimdev.dimdoors.util.Location; import org.dimdev.dimdoors.util.Location;
import org.dimdev.dimdoors.util.RGBA;
import org.dimdev.dimdoors.world.ModDimensions; import org.dimdev.dimdoors.world.ModDimensions;
import org.dimdev.dimdoors.world.pocket.Pocket; import org.dimdev.dimdoors.world.pocket.Pocket;
import org.dimdev.dimdoors.world.pocket.PocketRegistry; import org.dimdev.dimdoors.world.pocket.PocketRegistry;
import org.dimdev.dimdoors.world.pocket.PrivatePocketData; import org.dimdev.dimdoors.world.pocket.PrivatePocketData;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.text.TranslatableText; import net.minecraft.text.TranslatableText;
//import net.fabricmc.fabric.api.dimension.v1.FabricDimensions; //import net.fabricmc.fabric.api.dimension.v1.FabricDimensions;
public class PrivatePocketExitTarget extends VirtualTarget implements EntityTarget { 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() { public PrivatePocketExitTarget() {
} }
@Override
public void fromTag(CompoundTag nbt) {
super.fromTag(nbt);
}
@Override
public CompoundTag toTag(CompoundTag nbt) {
nbt = super.toTag(nbt);
return nbt;
}
@Override @Override
public boolean receiveEntity(Entity entity, float yawOffset) { public boolean receiveEntity(Entity entity, float yawOffset) {
Location destLoc; Location destLoc;
// TODO: make this recursive // TODO: make this recursive
UUID uuid = EntityUtils.getOwner(entity).getUuid(); UUID uuid = EntityUtils.getOwner(entity).getUuid();
if (uuid != null) { if (uuid != null) {
destLoc = RiftRegistry.instance(entity.world).getPrivatePocketExit(uuid); destLoc = RiftRegistry.instance().getPrivatePocketExit(uuid);
Pocket pocket = PrivatePocketData.instance(entity.world).getPrivatePocket(uuid); Pocket pocket = PrivatePocketData.instance().getPrivatePocket(uuid);
if (ModDimensions.isDimDoorsPocketDimension(location.world) && pocket != null && PocketRegistry.instance(pocket.world).getPocketAt(location.pos).equals(pocket)) { if (ModDimensions.isDimDoorsPocketDimension(location.getWorld()) && pocket != null && PocketRegistry.instance(pocket.world).getPocketAt(location.pos).equals(pocket)) {
RiftRegistry.instance(entity.world).setLastPrivatePocketEntrance(uuid, location); // Remember which exit was used for next time the pocket is entered RiftRegistry.instance().setLastPrivatePocketEntrance(uuid, location); // Remember which exit was used for next time the pocket is entered
} }
if (destLoc == null || !(destLoc.getBlockEntity() instanceof RiftBlockEntity)) { if (destLoc == null || !(destLoc.getBlockEntity() instanceof RiftBlockEntity)) {
if (destLoc == null) { if (destLoc == null) {
@ -65,11 +58,11 @@ public class PrivatePocketExitTarget extends VirtualTarget implements EntityTarg
super.register(); super.register();
PocketRegistry privatePocketRegistry = PocketRegistry.instance(location.world); PocketRegistry privatePocketRegistry = PocketRegistry.instance(location.world);
Pocket pocket = privatePocketRegistry.getPocketAt(location.pos); Pocket pocket = privatePocketRegistry.getPocketAt(location.pos);
RiftRegistry.instance(location.world).addPocketEntrance(pocket, location); RiftRegistry.instance().addPocketEntrance(pocket, location);
} }
@Override @Override
public float[] getColor() { public VirtualTargetType<? extends VirtualTarget> getType() {
return new float[]{0, 1, 0, 1}; return VirtualTargetType.PRIVATE_POCKET_EXIT;
} }
} }

View file

@ -2,12 +2,14 @@ package org.dimdev.dimdoors.rift.targets;
import java.util.UUID; import java.util.UUID;
import com.mojang.serialization.Codec;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.dimdev.dimdoors.pockets.PocketGenerator; import org.dimdev.dimdoors.pockets.PocketGenerator;
import org.dimdev.dimdoors.rift.registry.RiftRegistry; import org.dimdev.dimdoors.rift.registry.RiftRegistry;
import org.dimdev.dimdoors.util.EntityUtils; import org.dimdev.dimdoors.util.EntityUtils;
import org.dimdev.dimdoors.util.Location; import org.dimdev.dimdoors.util.Location;
import org.dimdev.dimdoors.util.RGBA;
import org.dimdev.dimdoors.world.pocket.Pocket; import org.dimdev.dimdoors.world.pocket.Pocket;
import org.dimdev.dimdoors.world.pocket.PrivatePocketData; import org.dimdev.dimdoors.world.pocket.PrivatePocketData;
import org.dimdev.dimdoors.world.pocket.VirtualLocation; import org.dimdev.dimdoors.world.pocket.VirtualLocation;
@ -17,49 +19,41 @@ import net.minecraft.entity.Entity;
import net.minecraft.entity.ItemEntity; import net.minecraft.entity.ItemEntity;
import net.minecraft.item.DyeItem; import net.minecraft.item.DyeItem;
import net.minecraft.item.Item; import net.minecraft.item.Item;
import net.minecraft.nbt.CompoundTag;
public class PrivatePocketTarget extends VirtualTarget implements EntityTarget { public class PrivatePocketTarget extends VirtualTarget implements EntityTarget {
private static final Logger LOGGER = LogManager.getLogger(); 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() { public PrivatePocketTarget() {
} }
@Override
public void fromTag(CompoundTag nbt) {
super.fromTag(nbt);
}
@Override
public CompoundTag toTag(CompoundTag nbt) {
nbt = super.toTag(nbt);
return nbt;
}
@Override @Override
public boolean receiveEntity(Entity entity, float yawOffset) { public boolean receiveEntity(Entity entity, float yawOffset) {
// TODO: make this recursive // TODO: make this recursive
UUID uuid = EntityUtils.getOwner(entity).getUuid(); UUID uuid = EntityUtils.getOwner(entity).getUuid();
VirtualLocation virtualLocation = VirtualLocation.fromLocation(location); VirtualLocation virtualLocation = VirtualLocation.fromLocation(location);
if (uuid != null) { if (uuid != null) {
Pocket pocket = PrivatePocketData.instance(entity.world).getPrivatePocket(uuid); Pocket pocket = PrivatePocketData.instance().getPrivatePocket(uuid);
if (pocket == null) { // generate the private pocket and get its entrances if (pocket == null) { // generate the private pocket and get its entrances
// set to where the pocket was first created // set to where the pocket was first created
pocket = PocketGenerator.generatePrivatePocket(new VirtualLocation(virtualLocation.world, virtualLocation.x, virtualLocation.z, -1)); pocket = PocketGenerator.generatePrivatePocket(new VirtualLocation(virtualLocation.world, virtualLocation.x, virtualLocation.z, -1));
PrivatePocketData.instance(entity.world).setPrivatePocketID(uuid, pocket); PrivatePocketData.instance().setPrivatePocketID(uuid, pocket);
processEntity(pocket, RiftRegistry.instance(entity.world).getPocketEntrance(pocket).getBlockEntity(), entity, uuid, yawOffset); processEntity(pocket, RiftRegistry.instance().getPocketEntrance(pocket).getBlockEntity(), entity, uuid, yawOffset);
return true; return true;
} else { } else {
Location destLoc = RiftRegistry.instance(entity.world).getPrivatePocketEntrance(uuid); // get the last used entrances Location destLoc = RiftRegistry.instance().getPrivatePocketEntrance(uuid); // get the last used entrances
if (destLoc == null) if (destLoc == null)
destLoc = RiftRegistry.instance(entity.world).getPocketEntrance(pocket); // if there's none, then set the target to the main entrances destLoc = RiftRegistry.instance().getPocketEntrance(pocket); // if there's none, then set the target to the main entrances
if (destLoc == null) { // if the pocket entrances is gone, then create a new private pocket if (destLoc == null) { // if the pocket entrances is gone, then create a new private pocket
LOGGER.info("All entrances are gone, creating a new private pocket!"); LOGGER.info("All entrances are gone, creating a new private pocket!");
pocket = PocketGenerator.generatePrivatePocket(new VirtualLocation(virtualLocation.world, virtualLocation.x, virtualLocation.z, -1)); pocket = PocketGenerator.generatePrivatePocket(new VirtualLocation(virtualLocation.world, virtualLocation.x, virtualLocation.z, -1));
PrivatePocketData.instance(entity.world).setPrivatePocketID(uuid, pocket); PrivatePocketData.instance().setPrivatePocketID(uuid, pocket);
destLoc = RiftRegistry.instance(entity.world).getPocketEntrance(pocket); destLoc = RiftRegistry.instance().getPocketEntrance(pocket);
} }
processEntity(pocket, destLoc.getBlockEntity(), entity, uuid, yawOffset); processEntity(pocket, destLoc.getBlockEntity(), entity, uuid, yawOffset);
@ -82,12 +76,12 @@ public class PrivatePocketTarget extends VirtualTarget implements EntityTarget {
} }
} else { } else {
((EntityTarget) BlockEntity).receiveEntity(entity, relativeYaw); ((EntityTarget) BlockEntity).receiveEntity(entity, relativeYaw);
RiftRegistry.instance(entity.world).setLastPrivatePocketExit(uuid, location); RiftRegistry.instance().setLastPrivatePocketExit(uuid, location);
} }
} }
@Override @Override
public float[] getColor() { public VirtualTargetType<? extends VirtualTarget> getType() {
return new float[]{0, 1, 0, 1}; return VirtualTargetType.PRIVATE;
} }
} }

View file

@ -1,5 +1,7 @@
package org.dimdev.dimdoors.rift.targets; package org.dimdev.dimdoors.rift.targets;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import org.dimdev.dimdoors.pockets.PocketGenerator; import org.dimdev.dimdoors.pockets.PocketGenerator;
import org.dimdev.dimdoors.rift.registry.RiftRegistry; import org.dimdev.dimdoors.rift.registry.RiftRegistry;
import org.dimdev.dimdoors.util.Location; import org.dimdev.dimdoors.util.Location;
@ -9,18 +11,30 @@ import org.dimdev.dimdoors.world.pocket.VirtualLocation;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
public class PublicPocketTarget extends RestoringTarget { public class PublicPocketTarget extends RestoringTarget {
public final static Codec<PublicPocketTarget> CODEC = RecordCodecBuilder.create(instance -> {
return instance.group(
VirtualTarget.CODEC.optionalFieldOf("wrappedDestination", null).forGetter(RestoringTarget::getTarget)
).apply(instance, PublicPocketTarget::new);
});
private VirtualTarget wrappedDestination;
private PublicPocketTarget(VirtualTarget wrappedDestination) {
this.wrappedDestination = wrappedDestination;
}
public PublicPocketTarget() { public PublicPocketTarget() {
} }
@Override @Override
public void fromTag(CompoundTag nbt) { protected VirtualTarget getTarget() {
super.fromTag(nbt); return wrappedDestination;
} }
@Override @Override
public CompoundTag toTag(CompoundTag nbt) { protected void setTarget(VirtualTarget target) {
nbt = super.toTag(nbt);
return nbt;
} }
@Override @Override
@ -31,6 +45,11 @@ public class PublicPocketTarget extends RestoringTarget {
newVirtualLocation = new VirtualLocation(riftVirtualLocation.world, riftVirtualLocation.x, riftVirtualLocation.z, depth); newVirtualLocation = new VirtualLocation(riftVirtualLocation.world, riftVirtualLocation.x, riftVirtualLocation.z, depth);
Pocket pocket = PocketGenerator.generatePublicPocket(newVirtualLocation, new GlobalReference(location), null); Pocket pocket = PocketGenerator.generatePublicPocket(newVirtualLocation, new GlobalReference(location), null);
return RiftRegistry.instance(location.world).getPocketEntrance(pocket); return RiftRegistry.instance().getPocketEntrance(pocket);
}
@Override
public VirtualTargetType<? extends VirtualTarget> getType() {
return VirtualTargetType.PUBLIC_POCKET;
} }
} }

View file

@ -3,8 +3,14 @@ package org.dimdev.dimdoors.rift.targets;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.nbt.NbtOps;
import net.minecraft.nbt.Tag;
import org.dimdev.annotatednbt.AnnotatedNbt; import org.dimdev.annotatednbt.AnnotatedNbt;
import org.dimdev.annotatednbt.Saved; import org.dimdev.annotatednbt.Saved;
import org.dimdev.dimdoors.block.ModBlocks; import org.dimdev.dimdoors.block.ModBlocks;
@ -15,6 +21,7 @@ import org.dimdev.dimdoors.rift.registry.LinkProperties;
import org.dimdev.dimdoors.rift.registry.Rift; import org.dimdev.dimdoors.rift.registry.Rift;
import org.dimdev.dimdoors.rift.registry.RiftRegistry; import org.dimdev.dimdoors.rift.registry.RiftRegistry;
import org.dimdev.dimdoors.util.Location; import org.dimdev.dimdoors.util.Location;
import org.dimdev.dimdoors.util.WorldUtil;
import org.dimdev.dimdoors.util.math.MathUtil; import org.dimdev.dimdoors.util.math.MathUtil;
import org.dimdev.dimdoors.world.pocket.Pocket; import org.dimdev.dimdoors.world.pocket.Pocket;
import org.dimdev.dimdoors.world.pocket.VirtualLocation; import org.dimdev.dimdoors.world.pocket.VirtualLocation;
@ -26,6 +33,19 @@ import net.minecraft.world.Heightmap;
import net.minecraft.world.World; import net.minecraft.world.World;
public class RandomTarget extends VirtualTarget { // TODO: Split into DungeonTarget subclass public class RandomTarget extends VirtualTarget { // TODO: Split into DungeonTarget subclass
public static final Codec<RandomTarget> CODEC = RecordCodecBuilder.create(instance -> {
return instance.group(
Codec.FLOAT.fieldOf("newRiftWeight").forGetter(target -> target.newRiftWeight),
Codec.DOUBLE.fieldOf("weightMaximum").forGetter(location -> location.weightMaximum),
Codec.DOUBLE.fieldOf("coordFactor").forGetter(location -> location.coordFactor),
Codec.DOUBLE.fieldOf("positiveDepthFactor").forGetter(location -> location.positiveDepthFactor),
Codec.DOUBLE.fieldOf("negativeDepthFactor").forGetter(location -> location.negativeDepthFactor),
Codec.INT_STREAM.<Set<Integer>>comapFlatMap(a -> DataResult.success(a.boxed().collect(Collectors.toSet())), a -> a.stream().mapToInt(Integer::intValue)).fieldOf("acceptedGroups").forGetter(target -> target.acceptedGroups),
Codec.BOOL.fieldOf("noLink").forGetter(target -> target.noLink),
Codec.BOOL.fieldOf("noLinkBack").forGetter(target -> target.noLinkBack)
).apply(instance, RandomTarget::new);
});
@Saved @Saved
protected float newRiftWeight; protected float newRiftWeight;
@Saved @Saved
@ -58,19 +78,6 @@ public class RandomTarget extends VirtualTarget { // TODO: Split into DungeonTar
return new RandomTargetBuilder(); return new RandomTargetBuilder();
} }
@Override
public void fromTag(CompoundTag nbt) {
super.fromTag(nbt);
AnnotatedNbt.load(this, nbt);
}
@Override
public CompoundTag toTag(CompoundTag nbt) {
nbt = super.toTag(nbt);
AnnotatedNbt.save(this, nbt);
return nbt;
}
@Override @Override
public Target receiveOther() { // TODO: Wrap rather than replace public Target receiveOther() { // TODO: Wrap rather than replace
VirtualLocation virtualLocationHere = VirtualLocation.fromLocation(location); VirtualLocation virtualLocationHere = VirtualLocation.fromLocation(location);
@ -78,7 +85,7 @@ public class RandomTarget extends VirtualTarget { // TODO: Split into DungeonTar
Map<Location, Float> riftWeights = new HashMap<>(); Map<Location, Float> riftWeights = new HashMap<>();
if (newRiftWeight > 0) riftWeights.put(null, newRiftWeight); if (newRiftWeight > 0) riftWeights.put(null, newRiftWeight);
for (Rift otherRift : RiftRegistry.instance(location.world).getRifts()) { for (Rift otherRift : RiftRegistry.instance().getRifts()) {
VirtualLocation otherVirtualLocation = VirtualLocation.fromLocation(otherRift.location); VirtualLocation otherVirtualLocation = VirtualLocation.fromLocation(otherRift.location);
if (otherRift.properties == null) continue; if (otherRift.properties == null) continue;
double otherWeight = otherRift.isDetached ? otherRift.properties.floatingWeight : otherRift.properties.entranceWeight; double otherWeight = otherRift.isDetached ? otherRift.properties.floatingWeight : otherRift.properties.entranceWeight;
@ -153,7 +160,7 @@ public class RandomTarget extends VirtualTarget { // TODO: Split into DungeonTar
if (virtualLocation.depth <= 0) { if (virtualLocation.depth <= 0) {
// This will lead to the overworld // This will lead to the overworld
World world = virtualLocation.world; World world = WorldUtil.getWorld(virtualLocation.world);
BlockPos pos = world.getTopPosition(Heightmap.Type.MOTION_BLOCKING_NO_LEAVES, new BlockPos(virtualLocation.x, 0, virtualLocation.z)); BlockPos pos = world.getTopPosition(Heightmap.Type.MOTION_BLOCKING_NO_LEAVES, new BlockPos(virtualLocation.x, 0, virtualLocation.z));
if (pos.getY() == -1) { if (pos.getY() == -1) {
// No blocks at that XZ (hole in bedrock) // No blocks at that XZ (hole in bedrock)
@ -177,8 +184,8 @@ public class RandomTarget extends VirtualTarget { // TODO: Split into DungeonTar
Pocket pocket = PocketGenerator.generateDungeonPocket(virtualLocation, new GlobalReference(!noLinkBack ? location : null), newLink); // TODO make the generated dungeon of the same type, but in the overworld Pocket pocket = PocketGenerator.generateDungeonPocket(virtualLocation, new GlobalReference(!noLinkBack ? location : null), newLink); // TODO make the generated dungeon of the same type, but in the overworld
// Link the rift if necessary and teleport the entity // Link the rift if necessary and teleport the entity
if (!noLink) linkRifts(location, RiftRegistry.instance(location.world).getPocketEntrance(pocket)); if (!noLink) linkRifts(location, RiftRegistry.instance().getPocketEntrance(pocket));
return (Target) RiftRegistry.instance(location.world).getPocketEntrance(pocket).getBlockEntity(); return (Target) RiftRegistry.instance().getPocketEntrance(pocket).getBlockEntity();
} }
} else { } else {
// An existing rift was selected // An existing rift was selected
@ -239,6 +246,11 @@ public class RandomTarget extends VirtualTarget { // TODO: Split into DungeonTar
return this.noLinkBack; return this.noLinkBack;
} }
@Override
public VirtualTargetType<? extends VirtualTarget> getType() {
return VirtualTargetType.AVAILABLE_LINK;
}
public static class RandomTargetBuilder { public static class RandomTargetBuilder {
private float newRiftWeight; private float newRiftWeight;
private double weightMaximum; private double weightMaximum;

View file

@ -1,5 +1,7 @@
package org.dimdev.dimdoors.rift.targets; package org.dimdev.dimdoors.rift.targets;
import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
import org.dimdev.annotatednbt.AnnotatedNbt; import org.dimdev.annotatednbt.AnnotatedNbt;
import org.dimdev.annotatednbt.Saved; import org.dimdev.annotatednbt.Saved;
import org.dimdev.dimdoors.util.Location; import org.dimdev.dimdoors.util.Location;
@ -8,26 +10,15 @@ import net.minecraft.nbt.CompoundTag;
import net.minecraft.util.math.Vec3i; import net.minecraft.util.math.Vec3i;
public class RelativeReference extends RiftReference { public class RelativeReference extends RiftReference {
@Saved
protected Vec3i offset; private Vec3i offset;
public static Codec<RelativeReference> CODEC = Vec3i.field_25123.xmap(RelativeReference::new, RelativeReference::getOffset).fieldOf("offset").codec();
public RelativeReference(Vec3i offset) { public RelativeReference(Vec3i offset) {
this.offset = offset; this.offset = offset;
} }
@Override
public void fromTag(CompoundTag nbt) {
super.fromTag(nbt);
AnnotatedNbt.load(this, nbt);
}
@Override
public CompoundTag toTag(CompoundTag tag) {
tag = super.toTag(tag);
AnnotatedNbt.save(this, tag);
return tag;
}
@Override @Override
public Location getReferencedLocation() { public Location getReferencedLocation() {
return new Location(location.world, location.pos.add(offset)); return new Location(location.world, location.pos.add(offset));
@ -36,4 +27,9 @@ public class RelativeReference extends RiftReference {
public Vec3i getOffset() { public Vec3i getOffset() {
return offset; return offset;
} }
@Override
public VirtualTargetType<? extends VirtualTarget> getType() {
return VirtualTargetType.RELATIVE;
}
} }

View file

@ -1,45 +1,33 @@
package org.dimdev.dimdoors.rift.targets; package org.dimdev.dimdoors.rift.targets;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import org.dimdev.dimdoors.util.Location; import org.dimdev.dimdoors.util.Location;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import org.dimdev.dimdoors.util.RGBA;
public abstract class RestoringTarget extends VirtualTarget { public abstract class RestoringTarget extends VirtualTarget {
private VirtualTarget wrappedDestination;
public RestoringTarget() { public RestoringTarget() {
} }
;
@Override
public void fromTag(CompoundTag nbt) {
super.fromTag(nbt);
wrappedDestination = nbt.contains("wrappedDestination") ? VirtualTarget.readVirtualTargetNBT(nbt.getCompound("wrappedDestination")) : null;
}
@Override
public CompoundTag toTag(CompoundTag nbt) {
nbt = super.toTag(nbt);
if (wrappedDestination != null) nbt.put("wrappedDestination", wrappedDestination.toTag(new CompoundTag()));
return nbt;
}
@Override @Override
public Target receiveOther() { public Target receiveOther() {
if (wrappedDestination != null) { if (getTarget() != null) {
wrappedDestination.location = location; getTarget().location = location;
return wrappedDestination; return getTarget();
} }
Location linkTarget = makeLinkTarget(); Location linkTarget = makeLinkTarget();
if (linkTarget != null) { if (linkTarget != null) {
wrappedDestination = RiftReference.tryMakeLocal(location, linkTarget); setTarget(RiftReference.tryMakeLocal(location, linkTarget));
wrappedDestination.setLocation(location); getTarget().setLocation(location);
wrappedDestination.register(); getTarget().register();
return wrappedDestination; return getTarget();
} else { } else {
return null; return null;
} }
@ -47,8 +35,8 @@ public abstract class RestoringTarget extends VirtualTarget {
@Override @Override
public boolean shouldInvalidate(Location deletedRift) { public boolean shouldInvalidate(Location deletedRift) {
if (wrappedDestination.shouldInvalidate(deletedRift)) { if (getTarget().shouldInvalidate(deletedRift)) {
wrappedDestination.unregister(); getTarget().unregister();
} }
return false; return false;
} }
@ -56,28 +44,32 @@ public abstract class RestoringTarget extends VirtualTarget {
@Override @Override
public void setLocation(Location location) { public void setLocation(Location location) {
super.setLocation(location); super.setLocation(location);
if (wrappedDestination != null) { if (getTarget() != null) {
wrappedDestination.setLocation(location); getTarget().setLocation(location);
} }
} }
@Override @Override
public void unregister() { public void unregister() {
if (wrappedDestination != null) wrappedDestination.unregister(); if (getTarget() != null) getTarget().unregister();
} }
protected abstract VirtualTarget getTarget();
protected abstract void setTarget(VirtualTarget target);
@Override @Override
public float[] getColor() { public RGBA getColor() {
if (wrappedDestination != null) { if (getTarget() != null) {
wrappedDestination.location = location; getTarget().location = location;
return wrappedDestination.getColor(); return getTarget().getColor();
} else { } else {
return getUnlinkedColor(location); return getUnlinkedColor(location);
} }
} }
protected float[] getUnlinkedColor(Location location) { protected RGBA getUnlinkedColor(Location location) {
return new float[]{0, 1, 1, 1}; return new RGBA(0, 1, 1, 1);
} }
public abstract Location makeLinkTarget(); public abstract Location makeLinkTarget();

View file

@ -6,6 +6,7 @@ import org.dimdev.dimdoors.rift.registry.RiftRegistry;
import org.dimdev.dimdoors.util.Location; import org.dimdev.dimdoors.util.Location;
import net.minecraft.util.math.Vec3i; import net.minecraft.util.math.Vec3i;
import org.dimdev.dimdoors.util.RGBA;
/** /**
* Allows rifts and targets to reference another rift without having to * Allows rifts and targets to reference another rift without having to
@ -47,12 +48,12 @@ public abstract class RiftReference extends VirtualTarget {
@Override @Override
public void register() { public void register() {
RiftRegistry.instance(location.world).addLink(location, getReferencedLocation()); RiftRegistry.instance().addLink(location, getReferencedLocation());
} }
@Override @Override
public void unregister() { public void unregister() {
RiftRegistry.instance(location.world).removeLink(location, getReferencedLocation()); RiftRegistry.instance().removeLink(location, getReferencedLocation());
} }
@Override @Override
@ -67,14 +68,14 @@ public abstract class RiftReference extends VirtualTarget {
} }
@Override @Override
public float[] getColor() { public RGBA getColor() {
Location target = getReferencedLocation(); Location target = getReferencedLocation();
if (target != null && RiftRegistry.instance(target.world).isRiftAt(target)) { if (target != null && RiftRegistry.instance().isRiftAt(target)) {
Set<Location> otherRiftTargets = RiftRegistry.instance(target.world).getTargets(target); Set<Location> otherRiftTargets = RiftRegistry.instance().getTargets(target);
if (otherRiftTargets.size() == 1 && otherRiftTargets.contains(location)) { if (otherRiftTargets.size() == 1 && otherRiftTargets.contains(location)) {
return new float[]{0, 1, 0, 1}; return new RGBA(0, 1, 0, 1);
} }
} }
return new float[]{1, 0, 0, 1}; return new RGBA(1, 0, 0, 1);
} }
} }

View file

@ -1,41 +1,33 @@
package org.dimdev.dimdoors.rift.targets; package org.dimdev.dimdoors.rift.targets;
import com.google.common.collect.BiMap; import com.mojang.datafixers.util.Pair;
import com.google.common.collect.HashBiMap; import com.mojang.serialization.Codec;
import org.dimdev.dimdoors.util.Location; import net.fabricmc.fabric.api.event.registry.FabricRegistryBuilder;
import net.fabricmc.fabric.api.event.registry.RegistryAttribute;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtOps;
import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry;
import org.dimdev.dimdoors.util.Location;
import org.dimdev.dimdoors.util.NbtUtil;
import org.dimdev.dimdoors.util.RGBA;
import java.util.function.Function;
/** /**
* A target that is not an actual object in the game such as a block or a tile * A target that is not an actual object in the game such as a block or a tile
* entity. Only virtual targets can be saved to NBT. * entity. Only virtual targets can be saved to NBT.
*/ */
public abstract class VirtualTarget implements Target { public abstract class VirtualTarget implements Target {
public static final Registry<VirtualTargetType> registry = FabricRegistryBuilder.createSimple(VirtualTargetType.class, new Identifier("dimdoors", "virtual_type")).attribute(RegistryAttribute.MODDED).buildAndRegister();
public static final RGBA COLOR = new RGBA(1, 0, 0, 1);
public static Codec<VirtualTarget> CODEC = registry.dispatch(VirtualTarget::getType, VirtualTargetType::codec);
public static final BiMap<String, Class<? extends VirtualTarget>> registry = HashBiMap.create();
protected Location location; protected Location location;
public static VirtualTarget readVirtualTargetNBT(CompoundTag nbt) { public static VirtualTarget readVirtualTargetNBT(CompoundTag nbt) {
String type = nbt.getString("type"); return NbtUtil.deserialize(nbt, CODEC);
Class<? extends VirtualTarget> destinationClass = registry.get(type);
if (destinationClass == null) throw new RuntimeException("Unknown type '" + type + "'.");
try {
VirtualTarget destination = destinationClass.newInstance();
destination.fromTag(nbt);
return destination;
} catch (IllegalAccessException | InstantiationException e) {
throw new RuntimeException("The class registered for virtual target " + type + " must have a public no-args constructor and must not be abstract", e);
}
}
public void fromTag(CompoundTag nbt) {
}
public CompoundTag toTag(CompoundTag nbt) {
String type = registry.inverse().get(getClass());
if (type == null) throw new RuntimeException("No type has been registered for class" + getClass().getName());
nbt.putString("type", type);
return nbt;
} }
public void register() { public void register() {
@ -44,12 +36,14 @@ public abstract class VirtualTarget implements Target {
public void unregister() { public void unregister() {
} }
public abstract VirtualTargetType<? extends VirtualTarget> getType();
public boolean shouldInvalidate(Location riftDeleted) { public boolean shouldInvalidate(Location riftDeleted) {
return false; return false;
} }
public float[] getColor() { public RGBA getColor() {
return new float[]{1, 0, 0, 1}; return getType().getColor();
} }
public boolean equals(Object o) { public boolean equals(Object o) {
@ -69,4 +63,36 @@ public abstract class VirtualTarget implements Target {
public void setLocation(Location location) { public void setLocation(Location location) {
this.location = location; this.location = location;
} }
public interface VirtualTargetType<T extends VirtualTarget> {
public VirtualTargetType<RandomTarget> AVAILABLE_LINK = register("available_link", RandomTarget.CODEC, VirtualTarget.COLOR);
public VirtualTargetType<EscapeTarget> ESCAPE = register("escape", EscapeTarget.CODEC, VirtualTarget.COLOR);
public VirtualTargetType<GlobalReference> GLOBAL = register("global", GlobalReference.CODEC, VirtualTarget.COLOR);
public VirtualTargetType<LimboTarget> LIMBO = register("limbo", LimboTarget.CODEC, VirtualTarget.COLOR);
public VirtualTargetType<LocalReference> LOCAL = register("local", LocalReference.CODEC, VirtualTarget.COLOR);
public VirtualTargetType<PublicPocketTarget> PUBLIC_POCKET = register("public_pocket", PublicPocketTarget.CODEC, VirtualTarget.COLOR);
public VirtualTargetType<PocketEntranceMarker> POCKET_ENTRANCE = register("pocket_entrance", PocketEntranceMarker.CODEC, VirtualTarget.COLOR);
public VirtualTargetType<PocketExitMarker> POCKET_EXIT = register("pocket_exit", PocketExitMarker.CODEC, VirtualTarget.COLOR);
public VirtualTargetType<PrivatePocketTarget> PRIVATE = register("private", PrivatePocketTarget.CODEC, PrivatePocketExitTarget.COLOR);
public VirtualTargetType<PrivatePocketExitTarget> PRIVATE_POCKET_EXIT = register("private_pocket_exit", PrivatePocketExitTarget.CODEC, PrivatePocketExitTarget.COLOR);
public VirtualTargetType<RelativeReference> RELATIVE = register("relative", RelativeReference.CODEC, VirtualTarget.COLOR);
Codec<T> codec();
RGBA getColor();
static <T extends VirtualTarget> VirtualTargetType<T> register(String id, Codec<T> codec, RGBA color) {
return Registry.register(registry, (String) id, new VirtualTargetType<T>() {
@Override
public Codec<T> codec() {
return codec;
}
@Override
public RGBA getColor() {
return color;
}
});
}
}
} }

View file

@ -0,0 +1,26 @@
package org.dimdev.dimdoors.util;
import com.mojang.datafixers.util.Pair;
import com.mojang.serialization.*;
import com.mojang.serialization.codecs.PrimitiveCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.util.DyeColor;
import net.minecraft.util.math.BlockBox;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3i;
import net.minecraft.world.World;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.*;
public class Codecs {
public static Codec<Set<Integer>> INT_SET = Codec.INT_STREAM.<Set<Integer>>comapFlatMap(a -> DataResult.success(a.boxed().collect(Collectors.toSet())), a -> a.stream().mapToInt(Integer::intValue));
public static Codec<BlockBox> BLOCK_BOX = Codec.INT_STREAM.<BlockBox>comapFlatMap(a -> DataResult.success(new BlockBox(a.toArray())), a -> IntStream.of(a.minX, a.minY, a.minZ, a.maxX, a.maxY, a.maxZ));
public static Codec<DyeColor> DYE_COLOR = Codec.INT.xmap(DyeColor::byId, DyeColor::getId);
}

View file

@ -1,5 +1,15 @@
package org.dimdev.dimdoors.util; package org.dimdev.dimdoors.util;
import com.mojang.datafixers.kinds.App;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtOps;
import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry;
import net.minecraft.world.gen.placer.ColumnPlacer;
import org.dimdev.annotatednbt.AnnotatedNbt;
import org.dimdev.annotatednbt.AutoSerializable; import org.dimdev.annotatednbt.AutoSerializable;
import org.dimdev.annotatednbt.Saved; import org.dimdev.annotatednbt.Saved;
@ -10,14 +20,24 @@ import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.registry.RegistryKey; import net.minecraft.util.registry.RegistryKey;
import net.minecraft.world.World; import net.minecraft.world.World;
import org.dimdev.dimdoors.DimensionalDoorsInitializer;
import java.util.Optional;
import java.util.function.Function;
public class Location implements AutoSerializable { public class Location implements AutoSerializable {
@Saved public static final Codec<Location> CODEC = RecordCodecBuilder.create(instance -> {
public final ServerWorld world; return instance.group(World.CODEC.fieldOf("world").forGetter(location -> {
@Saved return location.world;
}), BlockPos.field_25064.fieldOf("pos").forGetter(location -> {
return location.pos;
})).apply(instance, Location::new);
});
public final RegistryKey<World> world;
public final BlockPos pos; public final BlockPos pos;
public Location(ServerWorld world, BlockPos pos) { public Location(RegistryKey<World> world, BlockPos pos) {
this.world = world; this.world = world;
this.pos = pos; this.pos = pos;
} }
@ -26,6 +46,10 @@ public class Location implements AutoSerializable {
this(world, new BlockPos(x, y, z)); this(world, new BlockPos(x, y, z));
} }
public Location(ServerWorld world, BlockPos pos) {
this(world.getRegistryKey(), pos);
}
public int getX() { public int getX() {
return pos.getX(); return pos.getX();
} }
@ -39,15 +63,15 @@ public class Location implements AutoSerializable {
} }
public BlockState getBlockState() { public BlockState getBlockState() {
return world.getBlockState(pos); return getWorld().getBlockState(pos);
} }
public FluidState getFluidState() { public FluidState getFluidState() {
return world.getFluidState(pos); return getWorld().getFluidState(pos);
} }
public BlockEntity getBlockEntity() { public BlockEntity getBlockEntity() {
return world.getBlockEntity(pos); return getWorld().getBlockEntity(pos);
} }
public BlockPos getBlockPos() { public BlockPos getBlockPos() {
@ -67,6 +91,10 @@ public class Location implements AutoSerializable {
} }
public RegistryKey<World> getWorldId() { public RegistryKey<World> getWorldId() {
return world.getRegistryKey(); return world;
}
public ServerWorld getWorld() {
return DimensionalDoorsInitializer.getServer().getWorld(world);
} }
} }

View file

@ -0,0 +1,15 @@
package org.dimdev.dimdoors.util;
import com.mojang.serialization.Codec;
import net.minecraft.nbt.NbtOps;
import net.minecraft.nbt.Tag;
public class NbtUtil {
public static <T> T deserialize(Tag data, Codec<T> codec) {
return NbtOps.INSTANCE.withParser(codec).apply(data).getOrThrow(true, a -> {});
}
public static <T> Tag serialize(T data, Codec<T> codec) {
return NbtOps.INSTANCE.withEncoder(codec).apply(data).getOrThrow(true, a -> {});
}
}

View file

@ -1,6 +1,19 @@
package org.dimdev.dimdoors.util; package org.dimdev.dimdoors.util;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import org.dimdev.dimdoors.rift.targets.EscapeTarget;
public class RGBA implements Cloneable { public class RGBA implements Cloneable {
public static Codec<RGBA> CODEC = RecordCodecBuilder.create(instance -> {
return instance.group(
Codec.FLOAT.fieldOf("red").forGetter(RGBA::getRed),
Codec.FLOAT.fieldOf("green").forGetter(RGBA::getGreen),
Codec.FLOAT.fieldOf("blue").forGetter(RGBA::getBlue),
Codec.FLOAT.fieldOf("alpha").forGetter(RGBA::getAlpha)
).apply(instance, RGBA::new);
});
float red; float red;
float green; float green;
float blue; float blue;

View file

@ -1,5 +1,10 @@
package org.dimdev.dimdoors.util; package org.dimdev.dimdoors.util;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.nbt.Tag;
import net.minecraft.util.registry.RegistryKey;
import net.minecraft.world.World;
import org.dimdev.annotatednbt.AnnotatedNbt; import org.dimdev.annotatednbt.AnnotatedNbt;
import org.dimdev.annotatednbt.AutoSerializable; import org.dimdev.annotatednbt.AutoSerializable;
import org.dimdev.annotatednbt.Saved; import org.dimdev.annotatednbt.Saved;
@ -7,20 +12,34 @@ import org.dimdev.annotatednbt.Saved;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.world.ServerWorld; import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import org.dimdev.dimdoors.ModConfig;
public class RotatedLocation extends Location {
static Codec<RotatedLocation> CODEC = RecordCodecBuilder.create(instance -> {
return instance.group(
World.CODEC.fieldOf("world").forGetter(location -> location.world),
BlockPos.field_25064.fieldOf("pos").forGetter(location -> location.pos),
Codec.FLOAT.fieldOf("yaw").forGetter(a -> a.yaw),
Codec.FLOAT.fieldOf("pitch").forGetter(a -> a.pitch)
).apply(instance, RotatedLocation::new);
});
public class RotatedLocation extends Location implements AutoSerializable {
@Saved @Saved
public final float yaw; public final float yaw;
@Saved @Saved
public final float pitch; public final float pitch;
public RotatedLocation(ServerWorld world, BlockPos pos, float yaw, float pitch) { public RotatedLocation(RegistryKey<World> world, BlockPos pos, float yaw, float pitch) {
super(world, pos); super(world, pos);
this.yaw = yaw; this.yaw = yaw;
this.pitch = pitch; this.pitch = pitch;
} }
public static RotatedLocation deserialize(CompoundTag nbt) { public static RotatedLocation deserialize(CompoundTag nbt) {
return AnnotatedNbt.deserialize(RotatedLocation.class, nbt); return NbtUtil.deserialize(nbt, CODEC);
}
public static Tag serialize(RotatedLocation location) {
return NbtUtil.serialize(location, CODEC);
} }
} }

View file

@ -29,10 +29,10 @@ public final class TeleportUtil {
} }
public static void teleport(ServerPlayerEntity player, Location location) { public static void teleport(ServerPlayerEntity player, Location location) {
teleport(player, location.world, location.pos, 0); teleport(player, WorldUtil.getWorld(location.world), location.pos, 0);
} }
public static void teleport(ServerPlayerEntity player, RotatedLocation location) { public static void teleport(ServerPlayerEntity player, RotatedLocation location) {
teleport(player, location.world, location.pos, (int) location.yaw); teleport(player, WorldUtil.getWorld(location.world), location.pos, (int) location.yaw);
} }
} }

View file

@ -0,0 +1,13 @@
package org.dimdev.dimdoors.util;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.registry.RegistryKey;
import net.minecraft.world.World;
import org.dimdev.dimdoors.DimensionalDoorsInitializer;
import org.dimdev.dimdoors.ModConfig;
public class WorldUtil {
public static ServerWorld getWorld(RegistryKey<World> key) {
return DimensionalDoorsInitializer.getWorld(key);
}
}

View file

@ -35,7 +35,10 @@ public final class ModDimensions {
public static ServerWorld dungeonPocketDimension; public static ServerWorld dungeonPocketDimension;
public static boolean isDimDoorsPocketDimension(World world) { public static boolean isDimDoorsPocketDimension(World world) {
RegistryKey<World> type = world.getRegistryKey(); return isDimDoorsPocketDimension(world.getRegistryKey());
}
public static boolean isDimDoorsPocketDimension(RegistryKey<World> type) {
return type == PERSONAL || type == PUBLIC || type == DUNGEON; return type == PERSONAL || type == PUBLIC || type == DUNGEON;
} }

View file

@ -1,7 +1,12 @@
package org.dimdev.dimdoors.world.pocket; package org.dimdev.dimdoors.world.pocket;
import com.flowpowered.math.vector.Vector3i; import com.flowpowered.math.vector.Vector3i;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.util.registry.RegistryKey;
import net.minecraft.world.World;
import org.dimdev.annotatednbt.Saved; import org.dimdev.annotatednbt.Saved;
import org.dimdev.dimdoors.util.Codecs;
import org.dimdev.dimdoors.util.EntityUtils; import org.dimdev.dimdoors.util.EntityUtils;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
@ -13,7 +18,18 @@ import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3i; import net.minecraft.util.math.Vec3i;
public final class Pocket { 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),
Codecs.DYE_COLOR.fieldOf("dyeColor").forGetter(a -> a.dyeColor),
Codecs.DYE_COLOR.optionalFieldOf("nextDyeColor", null).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; private static final int BLOCKS_PAINTED_PER_DYE = 1106;
@Saved @Saved
public final int id; public final int id;
@Saved @Saved
@ -26,9 +42,19 @@ public final class Pocket {
public DyeColor nextDyeColor = null; public DyeColor nextDyeColor = null;
@Saved @Saved
public int count = 0; public int count = 0;
public ServerWorld world;
public Pocket(int id, ServerWorld world, int x, int z) { public RegistryKey<World> world;
private Pocket(int id, BlockBox box, VirtualLocation virtualLocation, DyeColor dyeColor, DyeColor nextDyeColor, int count) {
this.id = id;
this.box = box;
this.virtualLocation = virtualLocation;
this.dyeColor = dyeColor;
this.nextDyeColor = nextDyeColor;
this.count = count;
}
public Pocket(int id, RegistryKey<World> world, int x, int z) {
this.id = id; this.id = id;
this.world = world; this.world = world;
box = new BlockBox(x * 16, 0, z * 16, (x + 1) * 16, 0, (z + 1) * 16); box = new BlockBox(x * 16, 0, z * 16, (x + 1) * 16, 0, (z + 1) * 16);

View file

@ -3,9 +3,14 @@ package org.dimdev.dimdoors.world.pocket;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import com.mojang.serialization.Codec;
import net.minecraft.util.registry.RegistryKey;
import net.minecraft.world.World;
import org.dimdev.annotatednbt.AnnotatedNbt; import org.dimdev.annotatednbt.AnnotatedNbt;
import org.dimdev.annotatednbt.Saved; import org.dimdev.annotatednbt.Saved;
import org.dimdev.dimdoors.ModConfig; import org.dimdev.dimdoors.ModConfig;
import org.dimdev.dimdoors.util.NbtUtil;
import org.dimdev.dimdoors.util.WorldUtil;
import org.dimdev.dimdoors.util.math.GridUtil; import org.dimdev.dimdoors.util.math.GridUtil;
import org.dimdev.dimdoors.world.ModDimensions; import org.dimdev.dimdoors.world.ModDimensions;
@ -15,6 +20,7 @@ import net.minecraft.util.math.BlockPos;
import net.minecraft.world.PersistentState; import net.minecraft.world.PersistentState;
public class PocketRegistry extends PersistentState { public class PocketRegistry extends PersistentState {
private Codec<Map<Integer, Pocket>> pocketsCodec = Codec.unboundedMap(Codec.INT, Pocket.CODEC);
private static final String DATA_NAME = "pocketlib_pockets"; private static final String DATA_NAME = "pocketlib_pockets";
@ -40,16 +46,26 @@ public class PocketRegistry extends PersistentState {
@Override @Override
public void fromTag(CompoundTag tag) { public void fromTag(CompoundTag tag) {
AnnotatedNbt.load(this, tag); gridSize = tag.getInt("gridSize");
privatePocketSize = tag.getInt("privatePocketSize");
publicPocketSize = tag.getInt("publicPocketSize");
pockets = NbtUtil.deserialize(tag.get("pockets"), pocketsCodec);
nextID = tag.getInt("nextID");
} }
@Override @Override
public CompoundTag toTag(CompoundTag tag) { public CompoundTag toTag(CompoundTag tag) {
AnnotatedNbt.save(this, tag); tag.putInt("gridSize", gridSize);
tag.putInt("privatePocketSize", privatePocketSize);
tag.putInt("publicPocketSize", publicPocketSize);
tag.put("pockets", NbtUtil.serialize(pockets, pocketsCodec));
tag.putInt("nextID", nextID);
return tag; return tag;
} }
public static PocketRegistry instance(ServerWorld world) { public static PocketRegistry instance(RegistryKey<World> key) {
ServerWorld world = WorldUtil.getWorld(key);
if (!(ModDimensions.isDimDoorsPocketDimension(world))) { if (!(ModDimensions.isDimDoorsPocketDimension(world))) {
throw new UnsupportedOperationException("PocketRegistry is only available for pocket dimensions!"); throw new UnsupportedOperationException("PocketRegistry is only available for pocket dimensions!");
} }
@ -58,7 +74,7 @@ public class PocketRegistry extends PersistentState {
instance.world = world; instance.world = world;
for (Pocket pocket : instance.pockets.values()) { for (Pocket pocket : instance.pockets.values()) {
pocket.world = world; pocket.world = key;
} }
return instance; return instance;
@ -83,7 +99,7 @@ public class PocketRegistry extends PersistentState {
public Pocket newPocket(int id) { public Pocket newPocket(int id) {
if (pockets.get(id) != null) return null; if (pockets.get(id) != null) return null;
GridUtil.GridPos pos = idToGridPos(id); GridUtil.GridPos pos = idToGridPos(id);
Pocket pocket = new Pocket(id, world, pos.x, pos.z); Pocket pocket = new Pocket(id, world.getRegistryKey(), pos.x, pos.z);
pockets.put(id, pocket); pockets.put(id, pocket);
if (id >= nextID) nextID = id + 1; if (id >= nextID) nextID = id + 1;
markDirty(); markDirty();

View file

@ -4,25 +4,25 @@ import java.util.UUID;
import com.google.common.collect.BiMap; import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap; import com.google.common.collect.HashBiMap;
import net.minecraft.util.registry.RegistryKey;
import org.dimdev.annotatednbt.AnnotatedNbt; import org.dimdev.annotatednbt.AnnotatedNbt;
import org.dimdev.annotatednbt.Saved; import org.dimdev.annotatednbt.Saved;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.world.PersistentState; import net.minecraft.world.PersistentState;
import net.minecraft.world.World; import net.minecraft.world.World;
import org.dimdev.dimdoors.util.WorldUtil;
import static net.minecraft.world.World.OVERWORLD; import static net.minecraft.world.World.OVERWORLD;
public class PrivatePocketData extends PersistentState { public class PrivatePocketData extends PersistentState {
protected static class PocketInfo { protected static class PocketInfo {
@Saved @Saved
public final ServerWorld world; public final RegistryKey<World> world;
@Saved @Saved
public final int id; public final int id;
public PocketInfo(ServerWorld world, int id) { public PocketInfo(RegistryKey<World> world, int id) {
this.world = world; this.world = world;
this.id = id; this.id = id;
} }
@ -40,12 +40,8 @@ public class PrivatePocketData extends PersistentState {
super(DATA_NAME); super(DATA_NAME);
} }
public static PrivatePocketData instance(World world) { public static PrivatePocketData instance() {
return instance(world.getServer()); return WorldUtil.getWorld(OVERWORLD).getPersistentStateManager().getOrCreate(PrivatePocketData::new, DATA_NAME);
}
private static PrivatePocketData instance(MinecraftServer server) {
return server.getWorld(OVERWORLD).getPersistentStateManager().getOrCreate(PrivatePocketData::new, DATA_NAME);
} }
@Override @Override

View file

@ -1,9 +1,15 @@
package org.dimdev.dimdoors.world.pocket; package org.dimdev.dimdoors.world.pocket;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.util.registry.RegistryKey;
import net.minecraft.world.World;
import org.dimdev.annotatednbt.AnnotatedNbt; import org.dimdev.annotatednbt.AnnotatedNbt;
import org.dimdev.annotatednbt.Saved; import org.dimdev.annotatednbt.Saved;
import org.dimdev.dimdoors.DimensionalDoorsInitializer;
import org.dimdev.dimdoors.ModConfig; import org.dimdev.dimdoors.ModConfig;
import org.dimdev.dimdoors.util.Location; import org.dimdev.dimdoors.util.Location;
import org.dimdev.dimdoors.util.WorldUtil;
import org.dimdev.dimdoors.world.ModDimensions; import org.dimdev.dimdoors.world.ModDimensions;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
@ -14,8 +20,17 @@ import net.minecraft.world.Heightmap;
import static net.minecraft.world.World.OVERWORLD; import static net.minecraft.world.World.OVERWORLD;
public class VirtualLocation { public class VirtualLocation {
public static Codec<VirtualLocation> CODEC = RecordCodecBuilder.create(instance ->
instance.group(
World.CODEC.fieldOf("world").forGetter(virtualLocation -> virtualLocation.world),
Codec.INT.fieldOf("x").forGetter(virtualLocation -> virtualLocation.x),
Codec.INT.fieldOf("z").forGetter(virtualLocation -> virtualLocation.z),
Codec.INT.fieldOf("depth").forGetter(virtualLocation -> virtualLocation.depth)
).apply(instance, VirtualLocation::new)
);
@Saved @Saved
public final ServerWorld world; public final RegistryKey<World> world;
@Saved @Saved
public final int x; public final int x;
@Saved @Saved
@ -23,7 +38,7 @@ public class VirtualLocation {
@Saved @Saved
public final int depth; public final int depth;
public VirtualLocation(ServerWorld world, int x, int z, int depth) { public VirtualLocation(RegistryKey<World> world, int x, int z, int depth) {
this.world = world; this.world = world;
this.x = x; this.x = x;
this.z = z; this.z = z;
@ -49,19 +64,19 @@ public class VirtualLocation {
} else { } else {
virtualLocation = null; // TODO: door was placed in a pockets dim but outside of a pockets... virtualLocation = null; // TODO: door was placed in a pockets dim but outside of a pockets...
} }
} else if (ModDimensions.isLimboDimension(location.world)) { // TODO: convert to interface on worldprovider } else if (ModDimensions.isLimboDimension(location.getWorld())) { // TODO: convert to interface on worldprovider
virtualLocation = new VirtualLocation(location.world, location.getX(), location.getZ(), ModConfig.DUNGEONS.maxDungeonDepth); virtualLocation = new VirtualLocation(location.world, location.getX(), location.getZ(), ModConfig.DUNGEONS.maxDungeonDepth);
} // TODO: nether coordinate transform } // TODO: nether coordinate transform
if (virtualLocation == null) { if (virtualLocation == null) {
return new VirtualLocation(location.world.getServer().getWorld(OVERWORLD), location.getX(), location.getZ(), 5); return new VirtualLocation(OVERWORLD, location.getX(), location.getZ(), 5);
} }
return virtualLocation; return virtualLocation;
} }
public Location projectToWorld(boolean acceptLimbo) { public Location projectToWorld(boolean acceptLimbo) {
ServerWorld world = this.world; ServerWorld world = DimensionalDoorsInitializer.getServer().getWorld(this.world);
if (!acceptLimbo && ModDimensions.isLimboDimension(world)) { if (!acceptLimbo && ModDimensions.isLimboDimension(world)) {
world = world.getServer().getWorld(OVERWORLD); world = world.getServer().getWorld(OVERWORLD);