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;
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.entity.ModBlockEntityTypes;
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");
private static MinecraftServer server;
public static MinecraftServer getServer() {
return server;
}
public static ServerWorld getWorld(RegistryKey<World> key) {
return getServer().getWorld(key);
}
@Override
public void onInitialize() {
ServerLifecycleEvents.SERVER_STARTED.register(DimensionalDoorsInitializer::setServer);
ModBlocks.init();
ModItems.init();
ModDimensions.init();
@ -43,18 +61,10 @@ public class DimensionalDoorsInitializer implements ModInitializer {
ModSoundEvents.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();
}
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 net.minecraft.block.*;
import org.dimdev.dimdoors.block.entity.DetachedRiftBlockEntity;
import org.dimdev.dimdoors.client.RiftParticle;
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.client.MinecraftClient;
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)
);*/
}
@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.world.BlockView;
import net.minecraft.world.World;
import org.dimdev.dimdoors.item.RiftSignatureItem;
public class DimensionalDoorBlock extends DoorBlock implements RiftProvider<EntranceRiftBlockEntity>, ConditionalBlockEntityProvider {
public DimensionalDoorBlock(Settings settings) {

View file

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

View file

@ -1,6 +1,7 @@
package org.dimdev.dimdoors.block.entity;
import java.util.Arrays;
import java.util.Objects;
import org.apache.logging.log4j.LogManager;
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.util.EntityUtils;
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 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 {
private static final Logger LOGGER = LogManager.getLogger();
/*@Saved*/ protected VirtualTarget destination; // How the rift acts as a source
@Saved
protected LinkProperties properties;
@Saved
protected boolean alwaysDelete;
@Saved
protected boolean forcedColor;
@Saved
protected float[] color = null;
protected RiftData data = new RiftData();
protected boolean riftStateChanged; // not saved
public RiftBlockEntity(BlockEntityType<? extends RiftBlockEntity> type) {
super(type);
alwaysDelete = false;
}
// NBT
@ -56,38 +51,36 @@ public abstract class RiftBlockEntity extends BlockEntity implements BlockEntity
if (this.world != null && !this.world.isClient()) {
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
public CompoundTag toTag(CompoundTag tag) {
if (this.world != null && !this.world.isClient()) {
this.sync();
}
return super.toTag(tag);
}
@Override
public CompoundTag toClientTag(CompoundTag tag) {
save(tag);
if (destination != null) {
tag.put("destination", destination.toTag(new CompoundTag()));
public void fromClientTag(CompoundTag tag) {
tag.put("data", NbtUtil.serialize(data, RiftData.CODEC));
}
@Override
public CompoundTag toClientTag(CompoundTag tag) {
tag.put("data", NbtUtil.serialize(data, RiftData.CODEC));
return tag;
}
public void setDestination(VirtualTarget destination) {
if (this.destination != null && isRegistered()) {
this.destination.unregister();
if (this.getDestination() != null && isRegistered()) {
this.getDestination().unregister();
}
this.destination = destination;
this.data.setDestination(destination);
if (destination != null) {
if (world != null && pos != null) {
destination.setLocation(new Location((ServerWorld) world, pos));
@ -99,14 +92,13 @@ public abstract class RiftBlockEntity extends BlockEntity implements BlockEntity
updateColor();
}
public void setColor(float[] color) {
forcedColor = color != null;
this.color = color;
public void setColor(RGBA color) {
data.setColor(color);
markDirty();
}
public void setProperties(LinkProperties properties) {
this.properties = properties;
data.setProperties(properties);
updateProperties();
markDirty();
}
@ -117,7 +109,7 @@ public abstract class RiftBlockEntity extends BlockEntity implements BlockEntity
}
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() {
@ -126,34 +118,34 @@ public abstract class RiftBlockEntity extends BlockEntity implements BlockEntity
}
Location loc = new Location((ServerWorld) world, pos);
RiftRegistry.instance(world).addRift(loc);
if (destination != null) destination.register();
RiftRegistry.instance().addRift(loc);
if (data.getDestination() != null) data.getDestination().register();
updateProperties();
updateColor();
}
public void updateProperties() {
if (isRegistered())
RiftRegistry.instance(world).setProperties(new Location((ServerWorld) world, pos), properties);
RiftRegistry.instance().setProperties(new Location((ServerWorld) world, pos), data.getProperties());
markDirty();
}
public void unregister() {
if (isRegistered()) {
RiftRegistry.instance(world).removeRift(new Location((ServerWorld) world, pos));
RiftRegistry.instance().removeRift(new Location((ServerWorld) world, pos));
}
}
public void updateType() {
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.markDirty();
}
public void handleTargetGone(Location location) {
if (destination.shouldInvalidate(location)) {
destination = null;
if (data.getDestination().shouldInvalidate(location)) {
data.setDestination(null);
markDirty();
}
@ -165,11 +157,11 @@ public abstract class RiftBlockEntity extends BlockEntity implements BlockEntity
}
public Target getTarget() {
if (destination == null) {
if (data.getDestination() == null) {
return new MessageTarget("rifts.unlinked");
} else {
destination.setLocation(new Location((ServerWorld) world, pos));
return destination;
data.getDestination().setLocation(new Location((ServerWorld) world, pos));
return data.getDestination();
}
}
@ -194,16 +186,16 @@ public abstract class RiftBlockEntity extends BlockEntity implements BlockEntity
}
public void updateColor() {
if (forcedColor) return;
if (data.isForcedColor()) return;
if (!isRegistered()) {
color = new float[]{0, 0, 0, 1};
} else if (destination == null) {
color = new float[]{0.7f, 0.7f, 0.7f, 1};
data.setColor(new RGBA(0, 0, 0, 1));
} else if (data.getDestination() == null) {
data.setColor(new RGBA(0.7f, 0.7f, 0.7f, 1));
} else {
destination.setLocation(new Location((ServerWorld) world, pos));
float[] newColor = destination.getColor();
if (color == null && newColor != null || !Arrays.equals(color, newColor)) {
color = newColor;
data.getDestination().setLocation(new Location((ServerWorld) world, pos));
RGBA newColor = data.getDestination().getColor();
if (data.getColor() == null && newColor != null || !Objects.equals(data.getColor(), newColor)) {
data.setColor(newColor);
markDirty();
}
}
@ -212,29 +204,30 @@ public abstract class RiftBlockEntity extends BlockEntity implements BlockEntity
protected abstract boolean isDetached();
public void copyFrom(DetachedRiftBlockEntity rift) {
destination = rift.destination;
properties = rift.properties;
alwaysDelete = rift.alwaysDelete;
forcedColor = rift.forcedColor;
data.setDestination(rift.data.getDestination());
data.setProperties(rift.data.getProperties());
data.setAlwaysDelete(rift.data.isAlwaysDelete());
data.setForcedColor(rift.data.isForcedColor());
}
public VirtualTarget getDestination() {
return destination;
return data.getDestination();
}
public LinkProperties getProperties() {
return properties;
return data.getProperties();
}
public boolean isAlwaysDelete() {
return alwaysDelete;
return data.isAlwaysDelete();
}
public boolean isForcedColor() {
return forcedColor;
return data.isForcedColor();
}
public float[] getColor() {
return color;
public RGBA getColor() {
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.Environment;
import org.dimdev.dimdoors.util.RGBA;
@Environment(EnvType.CLIENT)
public class DetachedRiftBlockEntityRenderer extends BlockEntityRenderer<DetachedRiftBlockEntity> {
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 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) {
double radian = nextAngle(rift, tickDelta) * TrigMath.DEG_TO_RAD;
float[] color = rift.getColor();
if (color == null) color = new float[]{1, 0.5f, 1, 1};
RGBA color = rift.getColor();
if (color == null) color = COLOR;
matrices.push();

View file

@ -5,6 +5,7 @@ import com.flowpowered.math.vector.Vector3f;
import com.flowpowered.math.vector.Vector4f;
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.sin;
@ -16,20 +17,20 @@ public class Plane {
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[1], radian), 0, 1, color);
drawVertex(vc, rotYW(vectors[2], radian), 1, 1, 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);
Vector3f scaled = vector.toVector3().mul(scalar);
vc.vertex(scaled.getX(), scaled.getY(), scaled.getZ())
.texture(u, v)
.color(color[0], color[1], color[2], color[3])
.color(color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha())
.next();
}

View file

@ -6,6 +6,7 @@ import net.minecraft.client.render.VertexConsumer;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import org.dimdev.dimdoors.util.RGBA;
public class Tesseract {
private final Plane[] planes = new Plane[24];
@ -181,7 +182,7 @@ public class Tesseract {
}
@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) {
plane.draw(vc, color, radian);
}

View file

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

View file

@ -27,7 +27,7 @@ public class SaveSchemCommand {
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)
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.Environment;
import org.dimdev.dimdoors.util.WorldUtil;
import org.jetbrains.annotations.NotNull;
public class RiftSignatureItem extends Item {
public static final String ID = "rift_signature";
@ -42,7 +44,7 @@ public class RiftSignatureItem extends Item {
}
@Override
public ActionResult useOnBlock(ItemUsageContext itemUsageContext) {
public ActionResult useOnBlock(@NotNull ItemUsageContext itemUsageContext) {
PlayerEntity player = itemUsageContext.getPlayer();
World world = itemUsageContext.getWorld();
BlockPos pos = itemUsageContext.getBlockPos();
@ -67,7 +69,7 @@ public class RiftSignatureItem extends Item {
if (target == null) {
// 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);
world.playSound(null, player.getBlockPos(), ModSoundEvents.RIFT_START, SoundCategory.BLOCKS, 0.6f, 1);
} 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!
return ActionResult.FAIL;
}
World sourceWorld = target.world;
World sourceWorld = WorldUtil.getWorld(target.world);
sourceWorld.setBlockState(target.getBlockPos(), ModBlocks.DETACHED_RIFT.getDefaultState());
DetachedRiftBlockEntity rift1 = (DetachedRiftBlockEntity) target.getBlockEntity();
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) {
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) {

View file

@ -25,6 +25,7 @@ import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.World;
import org.dimdev.dimdoors.util.WorldUtil;
public class StabilizedRiftSignatureItem extends Item { // TODO: common superclass with 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) {
// 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);
world.playSound(null, player.getBlockPos(), ModSoundEvents.RIFT_START, SoundCategory.BLOCKS, 0.6f, 1);
} else {
@ -74,7 +75,7 @@ public class StabilizedRiftSignatureItem extends Item { // TODO: common supercla
// Don't clear source, stabilized signatures always stay bound
return ActionResult.FAIL;
}
World targetWorld = target.world;
World targetWorld = WorldUtil.getWorld(target.world);
targetWorld.setBlockState(target.getBlockPos(), ModBlocks.DETACHED_RIFT.getDefaultState());
DetachedRiftBlockEntity rift1 = (DetachedRiftBlockEntity) target.getBlockEntity();
rift1.register();
@ -98,7 +99,7 @@ public class StabilizedRiftSignatureItem extends Item { // TODO: common supercla
public static void setSource(ItemStack itemStack, RotatedLocation destination) {
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) {
@ -109,8 +110,7 @@ public class StabilizedRiftSignatureItem extends Item { // TODO: common supercla
public static RotatedLocation getTarget(ItemStack itemStack) {
if (itemStack.hasTag() && itemStack.getTag().contains("destination")) {
RotatedLocation transform = RotatedLocation.deserialize(itemStack.getTag().getCompound("destination"));
return transform;
return RotatedLocation.deserialize(itemStack.getTag().getCompound("destination"));
} else {
return null;
}

View file

@ -4,9 +4,11 @@ import java.util.Random;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.dimdev.dimdoors.DimensionalDoorsInitializer;
import org.dimdev.dimdoors.ModConfig;
import org.dimdev.dimdoors.rift.registry.LinkProperties;
import org.dimdev.dimdoors.rift.targets.VirtualTarget;
import org.dimdev.dimdoors.util.WorldUtil;
import org.dimdev.dimdoors.world.ModDimensions;
import org.dimdev.dimdoors.world.pocket.Pocket;
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) {
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);
pocket.virtualLocation = virtualLocation;
return pocket;
@ -40,13 +42,13 @@ public final class PocketGenerator {
public static Pocket generatePrivatePocket(VirtualLocation virtualLocation) {
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
public static Pocket generatePublicPocket(VirtualLocation virtualLocation, VirtualTarget linkTo, LinkProperties linkProperties) {
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) {
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();
String group = random.nextFloat() < netherProbability ? "nether" : "ruins";
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.VirtualTarget;
import org.dimdev.dimdoors.util.Location;
import org.dimdev.dimdoors.util.WorldUtil;
import org.dimdev.dimdoors.util.math.MathUtil;
import org.dimdev.dimdoors.world.pocket.Pocket;
import org.dimdev.dimdoors.world.pocket.PocketRegistry;
@ -174,7 +175,7 @@ public class PocketTemplate {
public void place(Pocket pocket, boolean setup) {
pocket.setSize(size * 16, size * 16, size * 16);
int gridSize = PocketRegistry.instance(pocket.world).getGridSize();
ServerWorld world = pocket.world;
ServerWorld world = WorldUtil.getWorld(pocket.world);
int xBase = pocket.box.minX;
int yBase = pocket.box.minY;
int zBase = pocket.box.minZ;
@ -199,7 +200,7 @@ public class PocketTemplate {
public void setup(Pocket pocket, VirtualTarget linkTo, LinkProperties linkProperties) {
int gridSize = PocketRegistry.instance(pocket.world).getGridSize();
ServerWorld world = pocket.world;
ServerWorld world = WorldUtil.getWorld(pocket.world);
int xBase = pocket.box.minX;
int yBase = pocket.box.minY;
int zBase = pocket.box.minZ;
@ -267,10 +268,10 @@ public class PocketTemplate {
VirtualTarget dest = rift.getDestination();
if (dest instanceof PocketEntranceMarker) {
if (rift == selectedEntrance) {
PocketRegistry.instance(world).markDirty();
PocketRegistry.instance(world.getRegistryKey()).markDirty();
rift.setDestination(((PocketEntranceMarker) dest).getIfDestination());
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 {
rift.setDestination(((PocketEntranceMarker) dest).getOtherwiseDestination());
}

View file

@ -1,18 +1,44 @@
package org.dimdev.dimdoors.rift.registry;
import java.util.Objects;
import java.util.Set;
import java.util.*;
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.dimdoors.util.Codecs;
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
public float floatingWeight; // TODO: depend on rift properties (ex. size, stability, or maybe a getWeightFactor method) rather than rift type
@Saved
public float entranceWeight;
@Saved
public Set<Integer> groups;
public Set<Integer> groups = new HashSet<>();
@Saved
public int linksRemaining;
@Saved

View file

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

View file

@ -2,6 +2,8 @@ package org.dimdev.dimdoors.rift.registry;
import java.util.UUID;
import net.minecraft.util.registry.RegistryKey;
import net.minecraft.world.World;
import org.dimdev.annotatednbt.AnnotatedNbt;
import org.dimdev.annotatednbt.Saved;
@ -9,7 +11,7 @@ import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.world.ServerWorld;
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
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() {
((RiftBlockEntity) location.getBlockEntity()).updateColor();
for (Location location : RiftRegistry.instance(world).getSources(location)) {
RiftRegistry.instance(world).getRift(location).targetChanged(this);
for (Location location : RiftRegistry.instance().getSources(location)) {
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.ListTag;
import net.minecraft.nbt.Tag;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.PersistentState;
import net.minecraft.world.World;
import static net.minecraft.world.World.OVERWORLD;
import static org.dimdev.dimdoors.DimensionalDoorsInitializer.*;
public class RiftRegistry extends PersistentState {
private static final Logger LOGGER = LogManager.getLogger();
@ -46,12 +46,8 @@ public class RiftRegistry extends PersistentState {
this.overworld = overworld;
}
public static RiftRegistry instance(World world) {
return instance(world.getServer());
}
private static RiftRegistry instance(MinecraftServer server) {
return server.getWorld(OVERWORLD).getPersistentStateManager().getOrCreate(() -> new RiftRegistry(server.getWorld(OVERWORLD)), DATA_NAME);
public static RiftRegistry instance() {
return getWorld(OVERWORLD).getPersistentStateManager().getOrCreate(() -> new RiftRegistry(getWorld(OVERWORLD)), DATA_NAME);
}
@Override
@ -318,7 +314,7 @@ public class RiftRegistry extends PersistentState {
if (entrance != null) return entrance.location;
// 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) {

View file

@ -2,6 +2,8 @@ package org.dimdev.dimdoors.rift.targets;
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.rift.registry.RiftRegistry;
import org.dimdev.dimdoors.util.Location;
@ -10,30 +12,24 @@ import org.dimdev.dimdoors.world.ModDimensions;
import org.dimdev.dimdoors.world.pocket.VirtualLocation;
import net.minecraft.entity.Entity;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.TranslatableText;
import static org.dimdev.dimdoors.util.EntityUtils.chat;
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;
public EscapeTarget(boolean 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
public boolean receiveEntity(Entity entity, float yawOffset) {
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();
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) {
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 {
if (destLoc == null) {
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
}
}
@Override
public VirtualTargetType<? extends VirtualTarget> getType() {
return VirtualTargetType.ESCAPE;
}
}

View file

@ -1,33 +1,32 @@
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.Saved;
import org.dimdev.dimdoors.util.Location;
import net.minecraft.nbt.CompoundTag;
import java.util.function.Function;
public class GlobalReference extends RiftReference {
@Saved
public static Codec<GlobalReference> CODEC = Location.CODEC.fieldOf("location").xmap(GlobalReference::new, GlobalReference::getReferencedLocation).codec();
protected Location target;
public GlobalReference(Location 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
public Location getReferencedLocation() {
return target;
}
@Override
public VirtualTargetType<? extends VirtualTarget> getType() {
return VirtualTargetType.GLOBAL;
}
}

View file

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

View file

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

View file

@ -1,5 +1,7 @@
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.Saved;
import org.dimdev.dimdoors.util.EntityUtils;
@ -10,10 +12,17 @@ import net.minecraft.text.TranslatableText;
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;
/*@Saved*/ protected VirtualTarget ifDestination;
/*@Saved*/ protected VirtualTarget otherwiseDestination;
protected VirtualTarget ifDestination;
protected VirtualTarget otherwiseDestination;
public PocketEntranceMarker() {
}
@ -28,24 +37,6 @@ public class PocketEntranceMarker extends VirtualTarget implements EntityTarget
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
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."));
@ -72,6 +63,11 @@ public class PocketEntranceMarker extends VirtualTarget implements EntityTarget
return new PocketEntranceMarkerBuilder().weight(weight).ifDestination(ifDestination).otherwiseDestination(otherwiseDestination);
}
@Override
public VirtualTargetType<? extends VirtualTarget> getType() {
return VirtualTargetType.POCKET_ENTRANCE;
}
public static class PocketEntranceMarkerBuilder {
private float weight;
private VirtualTarget ifDestination;

View file

@ -1,5 +1,6 @@
package org.dimdev.dimdoors.rift.targets;
import com.mojang.serialization.Codec;
import org.dimdev.dimdoors.util.EntityUtils;
import net.minecraft.entity.Entity;
@ -7,23 +8,19 @@ import net.minecraft.nbt.CompoundTag;
import net.minecraft.text.TranslatableText;
public class PocketExitMarker extends VirtualTarget implements EntityTarget {
public static final Codec<PocketExitMarker> CODEC = Codec.unit(PocketExitMarker::new);
public PocketExitMarker() {
}
@Override
public void fromTag(CompoundTag nbt) {
super.fromTag(nbt);
}
@Override
public CompoundTag toTag(CompoundTag nbt) {
nbt = super.toTag(nbt);
return nbt;
}
@Override
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."));
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 com.mojang.serialization.Codec;
import org.dimdev.dimdoors.block.entity.RiftBlockEntity;
import org.dimdev.dimdoors.rift.registry.RiftRegistry;
import org.dimdev.dimdoors.util.EntityUtils;
import org.dimdev.dimdoors.util.Location;
import org.dimdev.dimdoors.util.RGBA;
import org.dimdev.dimdoors.world.ModDimensions;
import org.dimdev.dimdoors.world.pocket.Pocket;
import org.dimdev.dimdoors.world.pocket.PocketRegistry;
import org.dimdev.dimdoors.world.pocket.PrivatePocketData;
import net.minecraft.entity.Entity;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.text.TranslatableText;
//import net.fabricmc.fabric.api.dimension.v1.FabricDimensions;
public class PrivatePocketExitTarget extends VirtualTarget implements EntityTarget {
public static final Codec<PrivatePocketExitTarget> CODEC = Codec.unit(PrivatePocketExitTarget::new);
public static final RGBA COLOR = new RGBA(0, 1, 0, 1);
public PrivatePocketExitTarget() {
}
@Override
public void fromTag(CompoundTag nbt) {
super.fromTag(nbt);
}
@Override
public CompoundTag toTag(CompoundTag nbt) {
nbt = super.toTag(nbt);
return nbt;
}
@Override
public boolean receiveEntity(Entity entity, float yawOffset) {
Location destLoc;
// TODO: make this recursive
UUID uuid = EntityUtils.getOwner(entity).getUuid();
if (uuid != null) {
destLoc = RiftRegistry.instance(entity.world).getPrivatePocketExit(uuid);
Pocket pocket = PrivatePocketData.instance(entity.world).getPrivatePocket(uuid);
if (ModDimensions.isDimDoorsPocketDimension(location.world) && 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
destLoc = RiftRegistry.instance().getPrivatePocketExit(uuid);
Pocket pocket = PrivatePocketData.instance().getPrivatePocket(uuid);
if (ModDimensions.isDimDoorsPocketDimension(location.getWorld()) && pocket != null && PocketRegistry.instance(pocket.world).getPocketAt(location.pos).equals(pocket)) {
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) {
@ -65,11 +58,11 @@ public class PrivatePocketExitTarget extends VirtualTarget implements EntityTarg
super.register();
PocketRegistry privatePocketRegistry = PocketRegistry.instance(location.world);
Pocket pocket = privatePocketRegistry.getPocketAt(location.pos);
RiftRegistry.instance(location.world).addPocketEntrance(pocket, location);
RiftRegistry.instance().addPocketEntrance(pocket, location);
}
@Override
public float[] getColor() {
return new float[]{0, 1, 0, 1};
public VirtualTargetType<? extends VirtualTarget> getType() {
return VirtualTargetType.PRIVATE_POCKET_EXIT;
}
}

View file

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

View file

@ -1,5 +1,7 @@
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.rift.registry.RiftRegistry;
import org.dimdev.dimdoors.util.Location;
@ -9,18 +11,30 @@ import org.dimdev.dimdoors.world.pocket.VirtualLocation;
import net.minecraft.nbt.CompoundTag;
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() {
}
@Override
public void fromTag(CompoundTag nbt) {
super.fromTag(nbt);
protected VirtualTarget getTarget() {
return wrappedDestination;
}
@Override
public CompoundTag toTag(CompoundTag nbt) {
nbt = super.toTag(nbt);
return nbt;
protected void setTarget(VirtualTarget target) {
}
@Override
@ -31,6 +45,11 @@ public class PublicPocketTarget extends RestoringTarget {
newVirtualLocation = new VirtualLocation(riftVirtualLocation.world, riftVirtualLocation.x, riftVirtualLocation.z, depth);
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.Map;
import java.util.Set;
import java.util.stream.Collectors;
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.Saved;
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.RiftRegistry;
import org.dimdev.dimdoors.util.Location;
import org.dimdev.dimdoors.util.WorldUtil;
import org.dimdev.dimdoors.util.math.MathUtil;
import org.dimdev.dimdoors.world.pocket.Pocket;
import org.dimdev.dimdoors.world.pocket.VirtualLocation;
@ -26,6 +33,19 @@ import net.minecraft.world.Heightmap;
import net.minecraft.world.World;
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
protected float newRiftWeight;
@Saved
@ -58,19 +78,6 @@ public class RandomTarget extends VirtualTarget { // TODO: Split into DungeonTar
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
public Target receiveOther() { // TODO: Wrap rather than replace
VirtualLocation virtualLocationHere = VirtualLocation.fromLocation(location);
@ -78,7 +85,7 @@ public class RandomTarget extends VirtualTarget { // TODO: Split into DungeonTar
Map<Location, Float> riftWeights = new HashMap<>();
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);
if (otherRift.properties == null) continue;
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) {
// 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));
if (pos.getY() == -1) {
// 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
// Link the rift if necessary and teleport the entity
if (!noLink) linkRifts(location, RiftRegistry.instance(location.world).getPocketEntrance(pocket));
return (Target) RiftRegistry.instance(location.world).getPocketEntrance(pocket).getBlockEntity();
if (!noLink) linkRifts(location, RiftRegistry.instance().getPocketEntrance(pocket));
return (Target) RiftRegistry.instance().getPocketEntrance(pocket).getBlockEntity();
}
} else {
// An existing rift was selected
@ -239,6 +246,11 @@ public class RandomTarget extends VirtualTarget { // TODO: Split into DungeonTar
return this.noLinkBack;
}
@Override
public VirtualTargetType<? extends VirtualTarget> getType() {
return VirtualTargetType.AVAILABLE_LINK;
}
public static class RandomTargetBuilder {
private float newRiftWeight;
private double weightMaximum;

View file

@ -1,5 +1,7 @@
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.Saved;
import org.dimdev.dimdoors.util.Location;
@ -8,26 +10,15 @@ import net.minecraft.nbt.CompoundTag;
import net.minecraft.util.math.Vec3i;
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) {
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
public Location getReferencedLocation() {
return new Location(location.world, location.pos.add(offset));
@ -36,4 +27,9 @@ public class RelativeReference extends RiftReference {
public Vec3i getOffset() {
return offset;
}
@Override
public VirtualTargetType<? extends VirtualTarget> getType() {
return VirtualTargetType.RELATIVE;
}
}

View file

@ -1,45 +1,33 @@
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 net.minecraft.nbt.CompoundTag;
import org.dimdev.dimdoors.util.RGBA;
public abstract class RestoringTarget extends VirtualTarget {
private VirtualTarget wrappedDestination;
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
public Target receiveOther() {
if (wrappedDestination != null) {
wrappedDestination.location = location;
return wrappedDestination;
if (getTarget() != null) {
getTarget().location = location;
return getTarget();
}
Location linkTarget = makeLinkTarget();
if (linkTarget != null) {
wrappedDestination = RiftReference.tryMakeLocal(location, linkTarget);
wrappedDestination.setLocation(location);
wrappedDestination.register();
setTarget(RiftReference.tryMakeLocal(location, linkTarget));
getTarget().setLocation(location);
getTarget().register();
return wrappedDestination;
return getTarget();
} else {
return null;
}
@ -47,8 +35,8 @@ public abstract class RestoringTarget extends VirtualTarget {
@Override
public boolean shouldInvalidate(Location deletedRift) {
if (wrappedDestination.shouldInvalidate(deletedRift)) {
wrappedDestination.unregister();
if (getTarget().shouldInvalidate(deletedRift)) {
getTarget().unregister();
}
return false;
}
@ -56,28 +44,32 @@ public abstract class RestoringTarget extends VirtualTarget {
@Override
public void setLocation(Location location) {
super.setLocation(location);
if (wrappedDestination != null) {
wrappedDestination.setLocation(location);
if (getTarget() != null) {
getTarget().setLocation(location);
}
}
@Override
public void unregister() {
if (wrappedDestination != null) wrappedDestination.unregister();
if (getTarget() != null) getTarget().unregister();
}
protected abstract VirtualTarget getTarget();
protected abstract void setTarget(VirtualTarget target);
@Override
public float[] getColor() {
if (wrappedDestination != null) {
wrappedDestination.location = location;
return wrappedDestination.getColor();
public RGBA getColor() {
if (getTarget() != null) {
getTarget().location = location;
return getTarget().getColor();
} else {
return getUnlinkedColor(location);
}
}
protected float[] getUnlinkedColor(Location location) {
return new float[]{0, 1, 1, 1};
protected RGBA getUnlinkedColor(Location location) {
return new RGBA(0, 1, 1, 1);
}
public abstract Location makeLinkTarget();

View file

@ -6,6 +6,7 @@ import org.dimdev.dimdoors.rift.registry.RiftRegistry;
import org.dimdev.dimdoors.util.Location;
import net.minecraft.util.math.Vec3i;
import org.dimdev.dimdoors.util.RGBA;
/**
* Allows rifts and targets to reference another rift without having to
@ -47,12 +48,12 @@ public abstract class RiftReference extends VirtualTarget {
@Override
public void register() {
RiftRegistry.instance(location.world).addLink(location, getReferencedLocation());
RiftRegistry.instance().addLink(location, getReferencedLocation());
}
@Override
public void unregister() {
RiftRegistry.instance(location.world).removeLink(location, getReferencedLocation());
RiftRegistry.instance().removeLink(location, getReferencedLocation());
}
@Override
@ -67,14 +68,14 @@ public abstract class RiftReference extends VirtualTarget {
}
@Override
public float[] getColor() {
public RGBA getColor() {
Location target = getReferencedLocation();
if (target != null && RiftRegistry.instance(target.world).isRiftAt(target)) {
Set<Location> otherRiftTargets = RiftRegistry.instance(target.world).getTargets(target);
if (target != null && RiftRegistry.instance().isRiftAt(target)) {
Set<Location> otherRiftTargets = RiftRegistry.instance().getTargets(target);
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;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import org.dimdev.dimdoors.util.Location;
import com.mojang.datafixers.util.Pair;
import com.mojang.serialization.Codec;
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.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
* entity. Only virtual targets can be saved to NBT.
*/
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;
public static VirtualTarget readVirtualTargetNBT(CompoundTag nbt) {
String type = nbt.getString("type");
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;
return NbtUtil.deserialize(nbt, CODEC);
}
public void register() {
@ -44,12 +36,14 @@ public abstract class VirtualTarget implements Target {
public void unregister() {
}
public abstract VirtualTargetType<? extends VirtualTarget> getType();
public boolean shouldInvalidate(Location riftDeleted) {
return false;
}
public float[] getColor() {
return new float[]{1, 0, 0, 1};
public RGBA getColor() {
return getType().getColor();
}
public boolean equals(Object o) {
@ -69,4 +63,36 @@ public abstract class VirtualTarget implements Target {
public void setLocation(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;
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.Saved;
@ -10,14 +20,24 @@ import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.registry.RegistryKey;
import net.minecraft.world.World;
import org.dimdev.dimdoors.DimensionalDoorsInitializer;
import java.util.Optional;
import java.util.function.Function;
public class Location implements AutoSerializable {
@Saved
public final ServerWorld world;
@Saved
public static final Codec<Location> CODEC = RecordCodecBuilder.create(instance -> {
return instance.group(World.CODEC.fieldOf("world").forGetter(location -> {
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 Location(ServerWorld world, BlockPos pos) {
public Location(RegistryKey<World> world, BlockPos pos) {
this.world = world;
this.pos = pos;
}
@ -26,6 +46,10 @@ public class Location implements AutoSerializable {
this(world, new BlockPos(x, y, z));
}
public Location(ServerWorld world, BlockPos pos) {
this(world.getRegistryKey(), pos);
}
public int getX() {
return pos.getX();
}
@ -39,15 +63,15 @@ public class Location implements AutoSerializable {
}
public BlockState getBlockState() {
return world.getBlockState(pos);
return getWorld().getBlockState(pos);
}
public FluidState getFluidState() {
return world.getFluidState(pos);
return getWorld().getFluidState(pos);
}
public BlockEntity getBlockEntity() {
return world.getBlockEntity(pos);
return getWorld().getBlockEntity(pos);
}
public BlockPos getBlockPos() {
@ -67,6 +91,10 @@ public class Location implements AutoSerializable {
}
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;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import org.dimdev.dimdoors.rift.targets.EscapeTarget;
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 green;
float blue;

View file

@ -1,5 +1,10 @@
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.AutoSerializable;
import org.dimdev.annotatednbt.Saved;
@ -7,20 +12,34 @@ import org.dimdev.annotatednbt.Saved;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.world.ServerWorld;
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
public final float yaw;
@Saved
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);
this.yaw = yaw;
this.pitch = pitch;
}
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) {
teleport(player, location.world, location.pos, 0);
teleport(player, WorldUtil.getWorld(location.world), location.pos, 0);
}
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 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;
}

View file

@ -1,7 +1,12 @@
package org.dimdev.dimdoors.world.pocket;
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.dimdoors.util.Codecs;
import org.dimdev.dimdoors.util.EntityUtils;
import net.minecraft.entity.Entity;
@ -13,7 +18,18 @@ import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3i;
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;
@Saved
public final int id;
@Saved
@ -26,9 +42,19 @@ public final class Pocket {
public DyeColor nextDyeColor = null;
@Saved
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.world = world;
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.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.Saved;
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.world.ModDimensions;
@ -15,6 +20,7 @@ import net.minecraft.util.math.BlockPos;
import net.minecraft.world.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";
@ -40,16 +46,26 @@ public class PocketRegistry extends PersistentState {
@Override
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
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;
}
public static PocketRegistry instance(ServerWorld world) {
public static PocketRegistry instance(RegistryKey<World> key) {
ServerWorld world = WorldUtil.getWorld(key);
if (!(ModDimensions.isDimDoorsPocketDimension(world))) {
throw new UnsupportedOperationException("PocketRegistry is only available for pocket dimensions!");
}
@ -58,7 +74,7 @@ public class PocketRegistry extends PersistentState {
instance.world = world;
for (Pocket pocket : instance.pockets.values()) {
pocket.world = world;
pocket.world = key;
}
return instance;
@ -83,7 +99,7 @@ public class PocketRegistry extends PersistentState {
public Pocket newPocket(int id) {
if (pockets.get(id) != null) return null;
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);
if (id >= nextID) nextID = id + 1;
markDirty();

View file

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

View file

@ -1,9 +1,15 @@
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.Saved;
import org.dimdev.dimdoors.DimensionalDoorsInitializer;
import org.dimdev.dimdoors.ModConfig;
import org.dimdev.dimdoors.util.Location;
import org.dimdev.dimdoors.util.WorldUtil;
import org.dimdev.dimdoors.world.ModDimensions;
import net.minecraft.nbt.CompoundTag;
@ -14,8 +20,17 @@ import net.minecraft.world.Heightmap;
import static net.minecraft.world.World.OVERWORLD;
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
public final ServerWorld world;
public final RegistryKey<World> world;
@Saved
public final int x;
@Saved
@ -23,7 +38,7 @@ public class VirtualLocation {
@Saved
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.x = x;
this.z = z;
@ -49,19 +64,19 @@ public class VirtualLocation {
} else {
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);
} // TODO: nether coordinate transform
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;
}
public Location projectToWorld(boolean acceptLimbo) {
ServerWorld world = this.world;
ServerWorld world = DimensionalDoorsInitializer.getServer().getWorld(this.world);
if (!acceptLimbo && ModDimensions.isLimboDimension(world)) {
world = world.getServer().getWorld(OVERWORLD);