Split PocketLib and workaround for EnchantingTable

This commit is contained in:
Runemoro 2018-01-20 18:30:53 -05:00
parent c9d9d1f076
commit 24393783da
22 changed files with 293 additions and 308 deletions

View file

@ -5,7 +5,7 @@ import net.minecraft.util.math.Vec3i;
import org.dimdev.annotatednbt.NBTSerializable; import org.dimdev.annotatednbt.NBTSerializable;
import org.dimdev.annotatednbt.Saved; import org.dimdev.annotatednbt.Saved;
import org.dimdev.ddutils.Location; import org.dimdev.ddutils.Location;
import org.dimdev.dimdoors.shared.pockets.Pocket; import org.dimdev.pocketlib.Pocket;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;

View file

@ -27,7 +27,8 @@ public class DimDoors {
public static final String MODID = "dimdoors"; public static final String MODID = "dimdoors";
public static final String MODNAME = "Dimensional Doors"; public static final String MODNAME = "Dimensional Doors";
public static final String VERSION = "${version}"; public static final String VERSION = "${version}";
public static final String DEPENDENCIES = "required-after:forge@[14.23.0.2517,)"; //TODO make the forge version here change depending on a field in build.gradle // TODO: make the forge version here change depending on a field in build.gradle
public static final String DEPENDENCIES = "required-after:forge@[14.23.0.2517,);before:csb_ench_table"; // before:csb_ench_table as a workaround for https://github.com/crazysnailboy/EnchantingTable/issues/7
@Mod.Instance(DimDoors.MODID) @Mod.Instance(DimDoors.MODID)
public static DimDoors instance; public static DimDoors instance;

View file

@ -9,7 +9,7 @@ import net.minecraftforge.event.entity.living.LivingHurtEvent;
import net.minecraftforge.fml.common.eventhandler.EventPriority; import net.minecraftforge.fml.common.eventhandler.EventPriority;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.PlayerEvent; import net.minecraftforge.fml.common.gameevent.PlayerEvent;
import org.dimdev.dimdoors.shared.pockets.PocketRegistry; import org.dimdev.pocketlib.PocketRegistry;
import org.dimdev.dimdoors.shared.rifts.registry.RiftRegistry; import org.dimdev.dimdoors.shared.rifts.registry.RiftRegistry;
import org.dimdev.dimdoors.shared.world.ModDimensions; import org.dimdev.dimdoors.shared.world.ModDimensions;
@ -35,7 +35,7 @@ public final class EventHandler {
if (!world.isRemote if (!world.isRemote
&& !player.isDead && !player.isDead
&& ModDimensions.isDimDoorsPocketDimension(world) && ModDimensions.isDimDoorsPocketDimension(world)
&& !PocketRegistry.instance(dim).isPlayerAllowedToBeHere(player, player.getPosition())) { && !PocketRegistry.instance(dim).isPlayerAllowedToBeAt(player, player.getPosition())) {
// TODO: make the world circular // TODO: make the world circular
} }
} }

View file

@ -6,6 +6,7 @@ import org.dimdev.dimdoors.shared.rifts.TileEntityRift;
import org.dimdev.ddutils.Location; import org.dimdev.ddutils.Location;
import org.dimdev.ddutils.TeleportUtils; import org.dimdev.ddutils.TeleportUtils;
import org.dimdev.ddutils.WorldUtils; import org.dimdev.ddutils.WorldUtils;
import org.dimdev.dimdoors.shared.rifts.registry.RiftRegistry;
import org.dimdev.dimdoors.shared.world.ModDimensions; import org.dimdev.dimdoors.shared.world.ModDimensions;
import net.minecraft.command.CommandBase; import net.minecraft.command.CommandBase;
import net.minecraft.command.CommandException; import net.minecraft.command.CommandException;
@ -14,6 +15,7 @@ import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.server.MinecraftServer; import net.minecraft.server.MinecraftServer;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.TextComponentString; import net.minecraft.util.text.TextComponentString;
import org.dimdev.pocketlib.Pocket;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.ArrayList; import java.util.ArrayList;
@ -88,12 +90,11 @@ public class CommandPocket extends CommandBase {
// Generate the schematic // Generate the schematic
PocketTemplate template = SchematicHandler.INSTANCE.getTemplate(group, name); PocketTemplate template = SchematicHandler.INSTANCE.getTemplate(group, name);
Pocket pocket = PocketGenerator.generatePocketFromTemplate(WorldUtils.getDim(player.world), template, null); Pocket pocket = PocketGenerator.generatePocketFromTemplate(WorldUtils.getDim(player.world), template, null, setup);
if (setup) pocket.setup();
// Teleport the player there // Teleport the player there
if (pocket.getEntrance() != null) { if (RiftRegistry.instance().getPocketEntrance(pocket) != null) {
TileEntityRift entrance = (TileEntityRift) player.world.getTileEntity(pocket.getEntrance().getPos()); TileEntityRift entrance = (TileEntityRift) player.world.getTileEntity(RiftRegistry.instance().getPocketEntrance(pocket).getPos());
entrance.teleportTo(player); entrance.teleportTo(player);
} else { } else {
int size = (pocket.getSize() + 1) * 16; int size = (pocket.getSize() + 1) * 16;

View file

@ -10,8 +10,8 @@ import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.TextComponentString; import net.minecraft.util.text.TextComponentString;
import org.dimdev.ddutils.schem.Schematic; import org.dimdev.ddutils.schem.Schematic;
import org.dimdev.dimdoors.DimDoors; import org.dimdev.dimdoors.DimDoors;
import org.dimdev.dimdoors.shared.pockets.Pocket; import org.dimdev.pocketlib.Pocket;
import org.dimdev.dimdoors.shared.pockets.PocketRegistry; import org.dimdev.pocketlib.PocketRegistry;
import org.dimdev.dimdoors.shared.pockets.SchematicHandler; import org.dimdev.dimdoors.shared.pockets.SchematicHandler;
import org.dimdev.dimdoors.shared.world.pocketdimension.WorldProviderPocket; import org.dimdev.dimdoors.shared.world.pocketdimension.WorldProviderPocket;

View file

@ -1,135 +0,0 @@
package org.dimdev.dimdoors.shared.pockets;
import lombok.Getter;
import lombok.Setter;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
import org.dimdev.annotatednbt.NBTSerializable;
import org.dimdev.annotatednbt.Saved;
import org.dimdev.ddutils.Location;
import org.dimdev.ddutils.math.MathUtils;
import org.dimdev.ddutils.nbt.INBTStorable;
import org.dimdev.ddutils.nbt.NBTUtils;
import org.dimdev.dimdoors.DimDoors;
import org.dimdev.dimdoors.shared.VirtualLocation;
import org.dimdev.dimdoors.shared.rifts.RiftDestination;
import org.dimdev.dimdoors.shared.rifts.TileEntityRift;
import org.dimdev.dimdoors.shared.rifts.destinations.PocketEntranceDestination;
import org.dimdev.dimdoors.shared.rifts.destinations.PocketExitDestination;
import org.dimdev.dimdoors.shared.rifts.registry.LinkProperties;
import org.dimdev.dimdoors.shared.tileentities.TileEntityEntranceRift;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@NBTSerializable public class Pocket implements INBTStorable { // TODO: better visibilities
@Saved @Getter protected int id;
@Saved @Getter protected int x; // Grid x TODO: rename to gridX and gridY, or just convert to non-grid dependant coordinates
@Saved @Getter protected int z; // Grid y
@Saved @Getter @Setter protected int size; // TODO: size = sizeInChunks - 1 ???!!! TODO: non chunk-based size, better bounds such as minX, minZ, maxX, maxZ, etc.
@Saved @Getter @Setter protected VirtualLocation virtualLocation;
@Saved @Getter @Setter protected Location entrance; // TODO: move this to the rift registry (pocketlib)
@Saved @Getter protected List<Location> riftLocations; // TODO: convert to a list of all tile entities (for chests, and to make it independant of pocketlib)
@Getter int dim; // Not saved
public Pocket() {}
public Pocket(int id, int dim, int x, int z) {
this.id = id;
this.dim = dim;
this.x = x;
this.z = z;
riftLocations = new ArrayList<>();
}
// TODO: make these static?
@Override public void readFromNBT(NBTTagCompound nbt) { NBTUtils.readFromNBT(this, nbt); }
@Override public NBTTagCompound writeToNBT(NBTTagCompound nbt) { return NBTUtils.writeToNBT(this, nbt); }
boolean isInBounds(BlockPos pos) {
// pocket bounds
int gridSize = PocketRegistry.instance(dim).getGridSize();
int minX = x * gridSize;
int minZ = z * gridSize;
int maxX = minX + (size + 1) * 16;
int maxZ = minX + (size + 1) * 16;
return minX <= pos.getX() && minZ <= pos.getZ() && pos.getX() < maxX && pos.getZ() < maxZ;
}
public List<TileEntityRift> getRifts() {
List<TileEntityRift> rifts = new ArrayList<>(riftLocations.size()); // TODO: make immutable
for (Location riftLocation : riftLocations) {
TileEntity tileEntity = riftLocation.getWorld().getTileEntity(riftLocation.getPos());
if (tileEntity instanceof TileEntityRift) {
rifts.add((TileEntityRift) tileEntity);
} else {
throw new RuntimeException("The rift locations array was wrong, check the schematic placing function to see why!");
}
}
return rifts;
}
public void setup() { // Always call after creating a pocket except when building the pocket
List<TileEntityRift> rifts = getRifts();
HashMap<TileEntityRift, Float> entranceWeights = new HashMap<>();
for (TileEntityRift rift : rifts) { // Find an entrance
if (rift.getDestination() instanceof PocketEntranceDestination) {
entranceWeights.put(rift, ((PocketEntranceDestination) rift.getDestination()).getWeight());
}
}
if (entranceWeights.size() == 0) {
DimDoors.log.warn("Pocket had no possible entrance in schematic!");
return;
}
TileEntityRift selectedEntrance = MathUtils.weightedRandom(entranceWeights);
// Replace entrances with appropriate destinations
for (TileEntityRift rift : rifts) {
RiftDestination dest = rift.getDestination();
if (dest instanceof PocketEntranceDestination) {
if (rift == selectedEntrance) {
entrance = new Location(rift.getWorld(), rift.getPos());
PocketRegistry.instance(dim).markDirty();
rift.setDestination(((PocketEntranceDestination) dest).getIfDestination());
} else {
rift.setDestination(((PocketEntranceDestination) dest).getOtherwiseDestination());
}
}
}
// register the rifts
for (TileEntityRift rift : rifts) {
rift.register();
}
}
public void linkPocketTo(RiftDestination linkTo, LinkProperties linkProperties) {
List<TileEntityRift> rifts = getRifts();
// Link pocket exits back
for (TileEntityRift rift : rifts) {
RiftDestination dest = rift.getDestination();
if (dest instanceof PocketExitDestination) {
if (linkProperties != null) rift.setProperties(linkProperties);
rift.setDestination(linkTo);
if (rift instanceof TileEntityEntranceRift && !rift.isAlwaysDelete()) {
((TileEntityEntranceRift) rift).setPlaceRiftOnBreak(true); // We modified the door's state
}
rift.markDirty();
}
}
}
public BlockPos getOrigin() {
int gridSize = PocketRegistry.instance(dim).getGridSize();
return new BlockPos(x * gridSize * 16, 0, z * gridSize * 16); // TODO: configurable yBase?
}
}

View file

@ -1,33 +1,40 @@
package org.dimdev.dimdoors.shared.pockets; package org.dimdev.dimdoors.shared.pockets;
import org.dimdev.dimdoors.DimDoors; import org.dimdev.dimdoors.DimDoors;
import org.dimdev.dimdoors.shared.VirtualLocation;
import org.dimdev.dimdoors.shared.*; import org.dimdev.dimdoors.shared.*;
import org.dimdev.dimdoors.shared.rifts.RiftDestination;
import org.dimdev.dimdoors.shared.rifts.registry.LinkProperties;
import org.dimdev.dimdoors.shared.world.ModDimensions; import org.dimdev.dimdoors.shared.world.ModDimensions;
import org.dimdev.pocketlib.Pocket;
import org.dimdev.pocketlib.PocketRegistry;
import org.dimdev.pocketlib.VirtualLocation;
import java.util.Random; import java.util.Random;
public final class PocketGenerator { public final class PocketGenerator {
public static Pocket generatePocketFromTemplate(int dim, PocketTemplate pocketTemplate, VirtualLocation virtualLocation) { public static Pocket generatePocketFromTemplate(int dim, PocketTemplate pocketTemplate, VirtualLocation virtualLocation, boolean setup) {
DimDoors.log.info("Generating pocket from template " + pocketTemplate.getId() + " at virtual location " + virtualLocation); DimDoors.log.info("Generating pocket from template " + pocketTemplate.getId() + " at virtual location " + virtualLocation);
PocketRegistry registry = PocketRegistry.instance(dim); PocketRegistry registry = PocketRegistry.instance(dim);
Pocket pocket = registry.newPocket(); Pocket pocket = registry.newPocket();
pocketTemplate.place(pocket); pocketTemplate.place(pocket);
pocket.setVirtualLocation(virtualLocation); pocket.setVirtualLocation(virtualLocation);
if (setup) pocketTemplate.setup(pocket, null, null);
return pocket; return pocket;
} }
public static Pocket generatePrivatePocket(VirtualLocation virtualLocation) { public static Pocket generatePrivatePocket(VirtualLocation virtualLocation) {
PocketTemplate pocketTemplate = SchematicHandler.INSTANCE.getPersonalPocketTemplate(); PocketTemplate pocketTemplate = SchematicHandler.INSTANCE.getPersonalPocketTemplate();
return generatePocketFromTemplate(ModDimensions.getPrivateDim(), pocketTemplate, virtualLocation); return generatePocketFromTemplate(ModDimensions.getPrivateDim(), pocketTemplate, virtualLocation, true);
} }
// TODO: size of public pocket should increase with depth // TODO: size of public pocket should increase with depth
public static Pocket generatePublicPocket(VirtualLocation virtualLocation) { public static Pocket generatePublicPocket(VirtualLocation virtualLocation, RiftDestination linkTo, LinkProperties linkProperties) {
PocketTemplate pocketTemplate = SchematicHandler.INSTANCE.getPublicPocketTemplate(); PocketTemplate pocketTemplate = SchematicHandler.INSTANCE.getPublicPocketTemplate();
return generatePocketFromTemplate(ModDimensions.getPublicDim(), pocketTemplate, virtualLocation); Pocket pocket = generatePocketFromTemplate(ModDimensions.getPublicDim(), pocketTemplate, virtualLocation, false);
pocketTemplate.setup(pocket, linkTo, linkProperties);
return pocket;
} }
/** /**
@ -36,13 +43,15 @@ public final class PocketGenerator {
* @param virtualLocation The virtual location of the pocket * @param virtualLocation The virtual location of the pocket
* @return The newly-generated dungeon pocket * @return The newly-generated dungeon pocket
*/ */
public static Pocket generateDungeonPocket(VirtualLocation virtualLocation) { public static Pocket generateDungeonPocket(VirtualLocation virtualLocation, RiftDestination linkTo, LinkProperties linkProperties) {
int depth = virtualLocation.getDepth(); int depth = virtualLocation.getDepth();
float netherProbability = virtualLocation.getDim() == -1 ? 1 : (float) depth / 200; // TODO: improve nether probability float netherProbability = virtualLocation.getDim() == -1 ? 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, Config.getMaxPocketSize(), false); PocketTemplate pocketTemplate = SchematicHandler.INSTANCE.getRandomTemplate(group, depth, Config.getMaxPocketSize(), false);
return generatePocketFromTemplate(ModDimensions.getDungeonDim(), pocketTemplate, virtualLocation); Pocket pocket = generatePocketFromTemplate(ModDimensions.getDungeonDim(), pocketTemplate, virtualLocation, false);
pocketTemplate.setup(pocket, linkTo, linkProperties);
return pocket;
} }
} }

View file

@ -1,13 +1,5 @@
package org.dimdev.dimdoors.shared.pockets; package org.dimdev.dimdoors.shared.pockets;
import org.dimdev.ddutils.WorldUtils;
import org.dimdev.dimdoors.shared.rifts.TileEntityRift;
import org.dimdev.ddutils.Location;
import org.dimdev.ddutils.schem.Schematic;
import org.dimdev.dimdoors.DimDoors;
import java.util.ArrayList;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@ -25,6 +17,24 @@ import net.minecraft.util.math.BlockPos;
import net.minecraft.world.WorldServer; import net.minecraft.world.WorldServer;
import net.minecraft.world.storage.loot.LootContext; import net.minecraft.world.storage.loot.LootContext;
import net.minecraft.world.storage.loot.LootTable; import net.minecraft.world.storage.loot.LootTable;
import org.dimdev.ddutils.Location;
import org.dimdev.ddutils.WorldUtils;
import org.dimdev.ddutils.math.MathUtils;
import org.dimdev.ddutils.schem.Schematic;
import org.dimdev.dimdoors.DimDoors;
import org.dimdev.dimdoors.shared.rifts.RiftDestination;
import org.dimdev.dimdoors.shared.rifts.TileEntityRift;
import org.dimdev.dimdoors.shared.rifts.destinations.PocketEntranceDestination;
import org.dimdev.dimdoors.shared.rifts.destinations.PocketExitDestination;
import org.dimdev.dimdoors.shared.rifts.registry.LinkProperties;
import org.dimdev.dimdoors.shared.rifts.registry.RiftRegistry;
import org.dimdev.dimdoors.shared.tileentities.TileEntityEntranceRift;
import org.dimdev.pocketlib.Pocket;
import org.dimdev.pocketlib.PocketRegistry;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/** /**
* @author Robijnvogel * @author Robijnvogel
@ -38,7 +48,7 @@ public class PocketTemplate {
@Getter private final String name; @Getter private final String name;
@Getter private final String author; @Getter private final String author;
@Getter @Setter private Schematic schematic; @Getter @Setter private Schematic schematic;
@Getter private final int size; // size in chunks (n*n chunks) @Getter private final int size; // number of chunks (16 blocks) on each side - 1
@Getter private final int baseWeight; @Getter private final int baseWeight;
public float getWeight(int depth) { public float getWeight(int depth) {
@ -52,27 +62,38 @@ public class PocketTemplate {
public void place(Pocket pocket) { public void place(Pocket pocket) {
pocket.setSize(size); pocket.setSize(size);
int gridSize = PocketRegistry.instance(pocket.dim).getGridSize(); int gridSize = PocketRegistry.instance(pocket.getDim()).getGridSize();
int dim = pocket.dim; int dim = pocket.getDim();
WorldServer world = WorldUtils.getWorld(dim);
int xBase = pocket.getX() * gridSize * 16; int xBase = pocket.getX() * gridSize * 16;
int yBase = 0; int yBase = 0;
int zBase = pocket.getZ() * gridSize * 16; int zBase = pocket.getZ() * gridSize * 16;
// Place the schematic
DimDoors.log.info("Placing new pocket using schematic " + schematic.name + " at x = " + xBase + ", z = " + zBase); DimDoors.log.info("Placing new pocket using schematic " + schematic.name + " at x = " + xBase + ", z = " + zBase);
WorldServer world = WorldUtils.getWorld(dim);
Schematic.place(schematic, world, xBase, yBase, zBase); Schematic.place(schematic, world, xBase, yBase, zBase);
}
public void setup(Pocket pocket, RiftDestination linkTo, LinkProperties linkProperties) {
int gridSize = PocketRegistry.instance(pocket.getDim()).getGridSize();
int dim = pocket.getDim();
WorldServer world = WorldUtils.getWorld(dim);
int xBase = pocket.getX() * gridSize * 16;
int yBase = 0;
int zBase = pocket.getZ() * gridSize * 16;
List<TileEntityRift> rifts = new ArrayList<>();
// Set pocket riftLocations
pocket.riftLocations = new ArrayList<>();
for (NBTTagCompound tileEntityNBT : schematic.tileEntities) { for (NBTTagCompound tileEntityNBT : schematic.tileEntities) {
BlockPos pos = new BlockPos( BlockPos pos = new BlockPos(
xBase + tileEntityNBT.getInteger("x"), xBase + tileEntityNBT.getInteger("x"),
yBase + tileEntityNBT.getInteger("y"), yBase + tileEntityNBT.getInteger("y"),
zBase + tileEntityNBT.getInteger("z")); zBase + tileEntityNBT.getInteger("z"));
TileEntity tile = world.getTileEntity(pos); TileEntity tile = world.getTileEntity(pos);
if (tile instanceof TileEntityRift) { if (tile instanceof TileEntityRift) {
DimDoors.log.info("Rift found in schematic at " + pos); DimDoors.log.info("Rift found in schematic at " + pos);
pocket.riftLocations.add(new Location(world, pos)); rifts.add((TileEntityRift) tile);
} else if (tile instanceof TileEntityChest) { } else if (tile instanceof TileEntityChest) {
DimDoors.log.info("Now populating chest."); DimDoors.log.info("Now populating chest.");
TileEntityChest chest = (TileEntityChest) tile; TileEntityChest chest = (TileEntityChest) tile;
@ -101,5 +122,56 @@ public class PocketTemplate {
DimDoors.log.info("Dispenser should be populated now. Dispenser is: " + (dispenser.isEmpty() ? "emtpy." : "filled.")); DimDoors.log.info("Dispenser should be populated now. Dispenser is: " + (dispenser.isEmpty() ? "emtpy." : "filled."));
} }
} }
// Find an entrance
HashMap<TileEntityRift, Float> entranceWeights = new HashMap<>();
for (TileEntityRift rift : rifts) { // Find an entrance
if (rift.getDestination() instanceof PocketEntranceDestination) {
entranceWeights.put(rift, ((PocketEntranceDestination) rift.getDestination()).getWeight());
}
}
if (entranceWeights.size() == 0) {
DimDoors.log.warn("Pocket had no possible entrance in schematic!");
return;
}
TileEntityRift selectedEntrance = MathUtils.weightedRandom(entranceWeights);
// Replace entrances with appropriate destinations
for (TileEntityRift rift : rifts) {
RiftDestination dest = rift.getDestination();
if (dest instanceof PocketEntranceDestination) {
if (rift == selectedEntrance) {
PocketRegistry.instance(dim).markDirty();
rift.setDestination(((PocketEntranceDestination) dest).getIfDestination());
rift.register();
RiftRegistry.instance().addPocketEntrance(pocket, new Location(rift.getWorld(), rift.getPos()));
} else {
rift.setDestination(((PocketEntranceDestination) dest).getOtherwiseDestination());
}
}
}
// Link the pocket back
// Link pocket exits back
for (TileEntityRift rift : rifts) {
RiftDestination dest = rift.getDestination();
if (dest instanceof PocketExitDestination) {
if (linkProperties != null) rift.setProperties(linkProperties);
rift.setDestination(rift.getProperties() == null || !rift.getProperties().oneWay ? linkTo : null);
if (rift instanceof TileEntityEntranceRift && !rift.isAlwaysDelete()) {
((TileEntityEntranceRift) rift).setPlaceRiftOnBreak(true); // We modified the door's state
}
}
}
// register the rifts
for (TileEntityRift rift : rifts) {
rift.register();
rift.markDirty();
}
} }
} }

View file

@ -28,11 +28,12 @@ import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.CompressedStreamTools; import net.minecraft.nbt.CompressedStreamTools;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.dimdev.dimdoors.shared.world.ModDimensions; import org.dimdev.dimdoors.shared.world.ModDimensions;
import org.dimdev.pocketlib.PocketRegistry;
/** /**
* @author Robijnvogel * @author Robijnvogel
*/ */
public class SchematicHandler { public class SchematicHandler { // TODO: parts of this should be moved to the org.dimdev.ddutils.schem package
private static final String SAVED_POCKETS_GROUP_NAME = "saved_pockets"; private static final String SAVED_POCKETS_GROUP_NAME = "saved_pockets";
public static final SchematicHandler INSTANCE = new SchematicHandler(); public static final SchematicHandler INSTANCE = new SchematicHandler();

View file

@ -18,7 +18,7 @@ import org.dimdev.annotatednbt.Saved;
import org.dimdev.ddutils.*; import org.dimdev.ddutils.*;
import org.dimdev.ddutils.nbt.NBTUtils; import org.dimdev.ddutils.nbt.NBTUtils;
import org.dimdev.dimdoors.DimDoors; import org.dimdev.dimdoors.DimDoors;
import org.dimdev.dimdoors.shared.VirtualLocation; import org.dimdev.pocketlib.VirtualLocation;
import org.dimdev.dimdoors.shared.blocks.BlockDimensionalDoor; import org.dimdev.dimdoors.shared.blocks.BlockDimensionalDoor;
import org.dimdev.dimdoors.shared.blocks.BlockFloatingRift; import org.dimdev.dimdoors.shared.blocks.BlockFloatingRift;
import org.dimdev.dimdoors.shared.rifts.registry.LinkProperties; import org.dimdev.dimdoors.shared.rifts.registry.LinkProperties;

View file

@ -16,9 +16,9 @@ import org.dimdev.ddutils.RotatedLocation;
import org.dimdev.ddutils.WorldUtils; import org.dimdev.ddutils.WorldUtils;
import org.dimdev.ddutils.math.MathUtils; import org.dimdev.ddutils.math.MathUtils;
import org.dimdev.ddutils.nbt.NBTUtils; import org.dimdev.ddutils.nbt.NBTUtils;
import org.dimdev.dimdoors.shared.VirtualLocation; import org.dimdev.pocketlib.VirtualLocation;
import org.dimdev.dimdoors.shared.blocks.ModBlocks; import org.dimdev.dimdoors.shared.blocks.ModBlocks;
import org.dimdev.dimdoors.shared.pockets.Pocket; import org.dimdev.pocketlib.Pocket;
import org.dimdev.dimdoors.shared.pockets.PocketGenerator; import org.dimdev.dimdoors.shared.pockets.PocketGenerator;
import org.dimdev.dimdoors.shared.rifts.*; import org.dimdev.dimdoors.shared.rifts.*;
import org.dimdev.dimdoors.shared.rifts.registry.LinkProperties; import org.dimdev.dimdoors.shared.rifts.registry.LinkProperties;
@ -146,18 +146,13 @@ import java.util.Set;
riftEntity.teleportTo(entity, thisRift.getYaw(), thisRift.getPitch()); riftEntity.teleportTo(entity, thisRift.getYaw(), thisRift.getPitch());
} else { } else {
// Make a new dungeon pocket // Make a new dungeon pocket
Pocket pocket = PocketGenerator.generateDungeonPocket(virtualLocation); // TODO make the generated dungeon of the same type, but in the overworld
pocket.setup();
// Link the pocket back
TileEntityRift thisRift = (TileEntityRift) location.getLocation().getTileEntity(); TileEntityRift thisRift = (TileEntityRift) location.getLocation().getTileEntity();
TileEntityRift riftEntity = (TileEntityRift) pocket.getEntrance().getTileEntity();
LinkProperties newLink = thisRift.getProperties() != null ? thisRift.getProperties().toBuilder().linksRemaining(0).build() : null; LinkProperties newLink = thisRift.getProperties() != null ? thisRift.getProperties().toBuilder().linksRemaining(0).build() : null;
pocket.linkPocketTo(new GlobalDestination(!noLinkBack && !riftEntity.getProperties().oneWay ? location.getLocation() : null), newLink); // TODO: linkId Pocket pocket = PocketGenerator.generateDungeonPocket(virtualLocation, new GlobalDestination(!noLinkBack ? location.getLocation() : 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.getLocation(), pocket.getEntrance()); if (!noLink) linkRifts(location.getLocation(), RiftRegistry.instance().getPocketEntrance(pocket));
((TileEntityRift) pocket.getEntrance().getTileEntity()).teleportTo(entity, location.getYaw(), location.getPitch()); ((TileEntityRift) RiftRegistry.instance().getPocketEntrance(pocket).getTileEntity()).teleportTo(entity, location.getYaw(), location.getPitch());
} }
} else { } else {
// An existing rift was selected // An existing rift was selected

View file

@ -37,7 +37,7 @@ import org.dimdev.dimdoors.shared.rifts.RiftDestination;
@Override @Override
public boolean teleport(RotatedLocation loc, Entity entity) { public boolean teleport(RotatedLocation loc, Entity entity) {
if (entity instanceof EntityPlayer) DimDoors.chat(entity, "The entrances of this dungeon has not been linked. Either this is a bug or you are in dungeon-building mode."); if (entity instanceof EntityPlayer) DimDoors.chat(entity, "The entrance of this dungeon has not been converted. If this is a normally generated pocket, please report this bug.");
return false; return false;
} }
} }

View file

@ -28,7 +28,7 @@ public class PocketExitDestination extends RiftDestination { // TODO: not exactl
@Override @Override
public boolean teleport(RotatedLocation loc, Entity entity) { public boolean teleport(RotatedLocation loc, Entity entity) {
if (entity instanceof EntityPlayer) DimDoors.chat(entity, "The exit of this dungeon has not been linked. Either this is a bug or you are in dungeon-building mode."); if (entity instanceof EntityPlayer) DimDoors.chat(entity, "The exit of this dungeon has not been linked. If this is a normally generated pocket, please report this bug.");
return false; return false;
} }
} }

View file

@ -3,14 +3,13 @@ package org.dimdev.dimdoors.shared.rifts.destinations;
import org.dimdev.ddutils.RGBA; import org.dimdev.ddutils.RGBA;
import org.dimdev.ddutils.RotatedLocation; import org.dimdev.ddutils.RotatedLocation;
import org.dimdev.dimdoors.DimDoors; import org.dimdev.dimdoors.DimDoors;
import org.dimdev.dimdoors.shared.VirtualLocation; import org.dimdev.pocketlib.PrivatePocketData;
import org.dimdev.dimdoors.shared.pockets.Pocket; import org.dimdev.pocketlib.VirtualLocation;
import org.dimdev.pocketlib.Pocket;
import org.dimdev.dimdoors.shared.pockets.PocketGenerator; import org.dimdev.dimdoors.shared.pockets.PocketGenerator;
import org.dimdev.dimdoors.shared.pockets.PocketRegistry;
import org.dimdev.dimdoors.shared.rifts.RiftDestination; import org.dimdev.dimdoors.shared.rifts.RiftDestination;
import org.dimdev.dimdoors.shared.rifts.registry.RiftRegistry; import org.dimdev.dimdoors.shared.rifts.registry.RiftRegistry;
import org.dimdev.dimdoors.shared.rifts.TileEntityRift; import org.dimdev.dimdoors.shared.rifts.TileEntityRift;
import org.dimdev.dimdoors.shared.world.ModDimensions;
import org.dimdev.ddutils.EntityUtils; import org.dimdev.ddutils.EntityUtils;
import org.dimdev.ddutils.Location; import org.dimdev.ddutils.Location;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
@ -34,25 +33,22 @@ public class PrivateDestination extends RiftDestination {
UUID uuid = EntityUtils.getEntityOwnerUUID(entity); UUID uuid = EntityUtils.getEntityOwnerUUID(entity);
VirtualLocation virtualLocation = VirtualLocation.fromLocation(loc.getLocation()); VirtualLocation virtualLocation = VirtualLocation.fromLocation(loc.getLocation());
if (uuid != null) { if (uuid != null) {
PocketRegistry privatePocketRegistry = PocketRegistry.instance(ModDimensions.getPrivateDim()); Pocket pocket = PrivatePocketData.instance().getPrivatePocket(uuid);
Pocket pocket = privatePocketRegistry.getPocket(privatePocketRegistry.getPrivatePocketID(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(virtualLocation != null ? virtualLocation.toBuilder().depth(-1).build() : null); pocket = PocketGenerator.generatePrivatePocket(virtualLocation != null ? virtualLocation.toBuilder().depth(-1).build() : null);
pocket.setup(); PrivatePocketData.instance().setPrivatePocketID(uuid, pocket);
privatePocketRegistry.setPrivatePocketID(uuid, pocket.getId()); ((TileEntityRift) RiftRegistry.instance().getPocketEntrance(pocket).getTileEntity()).teleportTo(entity, loc.getYaw(), loc.getPitch());
((TileEntityRift) pocket.getEntrance().getTileEntity()).teleportTo(entity, loc.getYaw(), loc.getPitch());
RiftRegistry.instance().setLastPrivatePocketExit(uuid, loc.getLocation()); RiftRegistry.instance().setLastPrivatePocketExit(uuid, loc.getLocation());
return true; return true;
} else { } else {
Location destLoc = RiftRegistry.instance().getPrivatePocketEntrance(uuid); // get the last used entrances Location destLoc = RiftRegistry.instance().getPrivatePocketEntrance(uuid); // get the last used entrances
if (destLoc == null) destLoc = pocket.getEntrance(); // if there's none, then set the target to the main entrances if (destLoc == null) 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
DimDoors.log.info("All entrances are gone, creating a new private pocket!"); DimDoors.log.info("All entrances are gone, creating a new private pocket!");
pocket = PocketGenerator.generatePrivatePocket(virtualLocation != null ? virtualLocation.toBuilder().depth(-1).build() : null); pocket = PocketGenerator.generatePrivatePocket(virtualLocation != null ? virtualLocation.toBuilder().depth(-1).build() : null);
pocket.setup(); PrivatePocketData.instance().setPrivatePocketID(uuid, pocket);
privatePocketRegistry.setPrivatePocketID(uuid, pocket.getId()); destLoc = RiftRegistry.instance().getPocketEntrance(pocket);
destLoc = pocket.getEntrance();
} }
((TileEntityRift) destLoc.getTileEntity()).teleportTo(entity, loc.getYaw(), loc.getPitch()); ((TileEntityRift) destLoc.getTileEntity()).teleportTo(entity, loc.getYaw(), loc.getPitch());
RiftRegistry.instance().setLastPrivatePocketExit(uuid, loc.getLocation()); RiftRegistry.instance().setLastPrivatePocketExit(uuid, loc.getLocation());

View file

@ -8,12 +8,12 @@ import net.minecraft.entity.Entity;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import org.dimdev.ddutils.*; import org.dimdev.ddutils.*;
import org.dimdev.dimdoors.DimDoors; import org.dimdev.dimdoors.DimDoors;
import org.dimdev.dimdoors.shared.pockets.Pocket; import org.dimdev.pocketlib.PrivatePocketData;
import org.dimdev.dimdoors.shared.pockets.PocketRegistry; import org.dimdev.pocketlib.Pocket;
import org.dimdev.pocketlib.PocketRegistry;
import org.dimdev.dimdoors.shared.rifts.RiftDestination; import org.dimdev.dimdoors.shared.rifts.RiftDestination;
import org.dimdev.dimdoors.shared.rifts.TileEntityRift; import org.dimdev.dimdoors.shared.rifts.TileEntityRift;
import org.dimdev.dimdoors.shared.rifts.registry.RiftRegistry; import org.dimdev.dimdoors.shared.rifts.registry.RiftRegistry;
import org.dimdev.dimdoors.shared.world.ModDimensions;
import org.dimdev.dimdoors.shared.world.limbo.WorldProviderLimbo; import org.dimdev.dimdoors.shared.world.limbo.WorldProviderLimbo;
import org.dimdev.dimdoors.shared.world.pocketdimension.WorldProviderPersonalPocket; import org.dimdev.dimdoors.shared.world.pocketdimension.WorldProviderPersonalPocket;
@ -39,9 +39,9 @@ public class PrivatePocketExitDestination extends RiftDestination {
Location destLoc; Location destLoc;
UUID uuid = EntityUtils.getEntityOwnerUUID(entity); UUID uuid = EntityUtils.getEntityOwnerUUID(entity);
if (uuid != null) { if (uuid != null) {
PocketRegistry privatePocketRegistry = PocketRegistry.instance(ModDimensions.getPrivateDim());
destLoc = RiftRegistry.instance().getPrivatePocketExit(uuid); destLoc = RiftRegistry.instance().getPrivatePocketExit(uuid);
if (loc.getLocation().getWorld().provider instanceof WorldProviderPersonalPocket && privatePocketRegistry.getPrivatePocketID(uuid) == privatePocketRegistry.posToID(loc.getLocation().getPos())) { Pocket pocket = PrivatePocketData.instance().getPrivatePocket(uuid);
if (loc.getLocation().getWorld().provider instanceof WorldProviderPersonalPocket && pocket != null && PocketRegistry.instance(pocket.getDim()).getPocketAt(loc.getLocation().getPos()).equals(pocket)) {
RiftRegistry.instance().setLastPrivatePocketEntrance(uuid, loc.getLocation()); // Remember which exit was used for next time the pocket is entered RiftRegistry.instance().setLastPrivatePocketEntrance(uuid, loc.getLocation()); // Remember which exit was used for next time the pocket is entered
} }
if (destLoc == null || !(destLoc.getTileEntity() instanceof TileEntityRift)) { if (destLoc == null || !(destLoc.getTileEntity() instanceof TileEntityRift)) {

View file

@ -5,8 +5,9 @@ import net.minecraft.entity.Entity;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import org.dimdev.ddutils.Location; import org.dimdev.ddutils.Location;
import org.dimdev.ddutils.RotatedLocation; import org.dimdev.ddutils.RotatedLocation;
import org.dimdev.dimdoors.shared.VirtualLocation; import org.dimdev.dimdoors.shared.rifts.registry.RiftRegistry;
import org.dimdev.dimdoors.shared.pockets.Pocket; import org.dimdev.pocketlib.VirtualLocation;
import org.dimdev.pocketlib.Pocket;
import org.dimdev.dimdoors.shared.pockets.PocketGenerator; import org.dimdev.dimdoors.shared.pockets.PocketGenerator;
@Getter @AllArgsConstructor @Builder(toBuilder = true) @ToString @Getter @AllArgsConstructor @Builder(toBuilder = true) @ToString
@ -24,10 +25,8 @@ public class PublicPocketDestination extends RestoringDestination {
int depth = Math.max(riftVirtualLocation.getDepth(), 1); int depth = Math.max(riftVirtualLocation.getDepth(), 1);
newVirtualLocation = riftVirtualLocation.toBuilder().depth(depth).build(); newVirtualLocation = riftVirtualLocation.toBuilder().depth(depth).build();
} }
Pocket pocket = PocketGenerator.generatePublicPocket(newVirtualLocation); Pocket pocket = PocketGenerator.generatePublicPocket(newVirtualLocation, new GlobalDestination(loc.getLocation()), null);
pocket.setup();
pocket.linkPocketTo(new GlobalDestination(loc.getLocation()), null);
return pocket.getEntrance(); return RiftRegistry.instance().getPocketEntrance(pocket);
} }
} }

View file

@ -3,9 +3,7 @@ package org.dimdev.dimdoors.shared.rifts.registry;
import lombok.ToString; import lombok.ToString;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import org.dimdev.annotatednbt.NBTSerializable; import org.dimdev.annotatednbt.NBTSerializable;
import org.dimdev.ddutils.Location;
import org.dimdev.dimdoors.DimDoors; import org.dimdev.dimdoors.DimDoors;
import org.dimdev.dimdoors.shared.rifts.TileEntityRift;
@ToString @ToString
@NBTSerializable public class RiftPlaceholder extends Rift { // TODO: don't extend rift @NBTSerializable public class RiftPlaceholder extends Rift { // TODO: don't extend rift

View file

@ -10,9 +10,9 @@ import org.dimdev.ddutils.Location;
import org.dimdev.ddutils.WorldUtils; import org.dimdev.ddutils.WorldUtils;
import org.dimdev.dimdoors.DimDoors; import org.dimdev.dimdoors.DimDoors;
import org.dimdev.ddutils.GraphUtils; import org.dimdev.ddutils.GraphUtils;
import org.dimdev.dimdoors.shared.pockets.Pocket; import org.dimdev.pocketlib.PrivatePocketData;
import org.dimdev.dimdoors.shared.pockets.PocketRegistry; import org.dimdev.pocketlib.Pocket;
import org.dimdev.dimdoors.shared.world.ModDimensions; import org.dimdev.pocketlib.PocketRegistry;
import org.jgrapht.graph.DefaultDirectedGraph; import org.jgrapht.graph.DefaultDirectedGraph;
import org.jgrapht.graph.DefaultEdge; import org.jgrapht.graph.DefaultEdge;
@ -374,6 +374,10 @@ public class RiftRegistry extends WorldSavedData {
} }
} }
public Location getPocketEntrance(Pocket pocket) {
return getPocketEntrances(pocket).stream().findFirst().orElse(null);
}
public void addPocketEntrance(Pocket pocket, Location location) { public void addPocketEntrance(Pocket pocket, Location location) {
DimDoors.log.info("Adding pocket entrance for pocket " + pocket.getId() + " in dimension " + pocket.getDim() + " at " + location); DimDoors.log.info("Adding pocket entrance for pocket " + pocket.getId() + " in dimension " + pocket.getDim() + " at " + location);
PocketEntrancePointer pointer = pocketEntranceMap.get(pocket); PocketEntrancePointer pointer = pocketEntranceMap.get(pocket);
@ -394,10 +398,8 @@ public class RiftRegistry extends WorldSavedData {
Rift entrance = (Rift) GraphUtils.followPointer(graph, entrancePointer); Rift entrance = (Rift) GraphUtils.followPointer(graph, entrancePointer);
if (entrance != null) return entrance.location; if (entrance != null) return entrance.location;
// If there was no last used private entrance, get one of the player's private pocket entrances // If there was no last used private entrance, get the first player's private pocket entrance
PocketRegistry privatePocketRegistry = PocketRegistry.instance(ModDimensions.getPrivateDim()); return getPocketEntrance(PrivatePocketData.instance().getPrivatePocket(playerUUID));
Pocket pocket = privatePocketRegistry.getPocket(privatePocketRegistry.getPrivatePocketID(playerUUID));
return getPocketEntrances(pocket).stream().findFirst().orElse(null);
} }
private void setPlayerRiftPointer(UUID playerUUID, Location rift, Map<UUID, PlayerRiftPointer> map) { private void setPlayerRiftPointer(UUID playerUUID, Location rift, Map<UUID, PlayerRiftPointer> map) {

View file

@ -0,0 +1,50 @@
package org.dimdev.pocketlib;
import lombok.Getter;
import lombok.Setter;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.math.BlockPos;
import org.dimdev.annotatednbt.NBTSerializable;
import org.dimdev.annotatednbt.Saved;
import org.dimdev.ddutils.nbt.INBTStorable;
import org.dimdev.ddutils.nbt.NBTUtils;
@NBTSerializable public class Pocket implements INBTStorable {
@Saved @Getter protected int id;
@Saved @Getter protected int x; // Grid x TODO: rename to gridX and gridY, or just convert to non-grid dependant coordinates
@Saved @Getter protected int z; // Grid y
@Saved @Getter @Setter protected int size; // TODO: size = sizeInChunks - 1 ???!!! TODO: non chunk-based size, better bounds such as minX, minZ, maxX, maxZ, etc.
@Saved @Getter @Setter protected VirtualLocation virtualLocation;
@Getter int dim; // Not saved
public Pocket() {}
public Pocket(int id, int dim, int x, int z) {
this.id = id;
this.dim = dim;
this.x = x;
this.z = z;
}
@Override public void readFromNBT(NBTTagCompound nbt) { NBTUtils.readFromNBT(this, nbt); }
@Override public NBTTagCompound writeToNBT(NBTTagCompound nbt) { return NBTUtils.writeToNBT(this, nbt); }
boolean isInBounds(BlockPos pos) {
// pocket bounds
int gridSize = PocketRegistry.instance(dim).getGridSize();
int minX = x * gridSize;
int minZ = z * gridSize;
int maxX = minX + (size + 1) * 16;
int maxZ = minX + (size + 1) * 16;
return minX <= pos.getX() && minZ <= pos.getZ() && pos.getX() < maxX && pos.getZ() < maxZ;
}
public BlockPos getOrigin() {
int gridSize = PocketRegistry.instance(dim).getGridSize();
return new BlockPos(x * gridSize * 16, 0, z * gridSize * 16);
}
// TODO: more pocket methods
}

View file

@ -1,19 +1,15 @@
package org.dimdev.dimdoors.shared.pockets; package org.dimdev.pocketlib;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import net.minecraft.world.World; import net.minecraft.world.World;
import org.dimdev.annotatednbt.Saved; import org.dimdev.annotatednbt.Saved;
import org.dimdev.annotatednbt.NBTSerializable; import org.dimdev.annotatednbt.NBTSerializable;
import org.dimdev.dimdoors.shared.Config; import org.dimdev.dimdoors.shared.Config;
import org.dimdev.ddutils.math.GridUtils; import org.dimdev.ddutils.math.GridUtils;
import org.dimdev.dimdoors.DimDoors;
import org.dimdev.ddutils.nbt.NBTUtils; import org.dimdev.ddutils.nbt.NBTUtils;
import org.dimdev.ddutils.WorldUtils; import org.dimdev.ddutils.WorldUtils;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.UUID;
import lombok.Getter; import lombok.Getter;
import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.entity.player.EntityPlayerMP;
@ -23,18 +19,14 @@ import net.minecraft.world.storage.MapStorage;
import net.minecraft.world.storage.WorldSavedData; import net.minecraft.world.storage.WorldSavedData;
import org.dimdev.dimdoors.shared.world.pocketdimension.WorldProviderPocket; import org.dimdev.dimdoors.shared.world.pocketdimension.WorldProviderPocket;
@NBTSerializable public class @NBTSerializable public class PocketRegistry extends WorldSavedData {
PocketRegistry extends WorldSavedData { // TODO: unregister pocket entrances, private pocket entrances/exits
private static final String DATA_NAME = DimDoors.MODID + "_pockets"; private static final String DATA_NAME = "pocketlib_pockets";
@Getter private static final int DATA_VERSION = 0; // IMPORTANT: Update this and upgradeRegistry when making changes.
@Saved @Getter /*package-private*/ int gridSize; // Determines how much pockets in their dimension are spaced @Saved @Getter /*package-private*/ int gridSize; // Determines how much pockets in their dimension are spaced
@Saved @Getter /*package-private*/ int maxPocketSize;
@Saved @Getter /*package-private*/ int privatePocketSize; @Saved @Getter /*package-private*/ int privatePocketSize;
@Saved @Getter /*package-private*/ int publicPocketSize; @Saved @Getter /*package-private*/ int publicPocketSize;
@Saved /*package-private*/ BiMap<String, Integer> privatePocketMap; // Player UUID -> Pocket ID, in pocket dim only TODO: move this out of pocketlib @Saved @Getter /*package-private*/ Map<Integer, Pocket> pockets;
@Saved @Getter /*package-private*/ Map<Integer, Pocket> pockets; // TODO: remove getter?
@Saved @Getter /*package-private*/ int nextID; @Saved @Getter /*package-private*/ int nextID;
@Getter private int dim; @Getter private int dim;
@ -73,51 +65,15 @@ PocketRegistry extends WorldSavedData { // TODO: unregister pocket entrances, pr
public void initNewRegistry() { public void initNewRegistry() {
gridSize = Config.getPocketGridSize(); gridSize = Config.getPocketGridSize();
maxPocketSize = Config.getMaxPocketSize();
privatePocketSize = Config.getPrivatePocketSize(); privatePocketSize = Config.getPrivatePocketSize();
publicPocketSize = Config.getPublicPocketSize(); publicPocketSize = Config.getPublicPocketSize();
nextID = 0; nextID = 0;
pockets = new HashMap<>(); pockets = new HashMap<>();
privatePocketMap = HashBiMap.create();
} }
@Override @Override public void readFromNBT(NBTTagCompound nbt) { NBTUtils.readFromNBT(this, nbt); }
public void readFromNBT(NBTTagCompound nbt) { @Override public NBTTagCompound writeToNBT(NBTTagCompound nbt) { return NBTUtils.writeToNBT(this, nbt); }
Integer version = nbt.getInteger("version");
if (version == null || version != DATA_VERSION) {
if (upgradeRegistry(nbt, version == null ? -1 : version)) {
markDirty();
} else {
DimDoors.log.fatal("Failed to upgrade the pocket registry, you'll have to recreate your world!");
throw new RuntimeException("Couldn't upgrade registry"); // TODO: better exceptions
}
}
NBTUtils.readFromNBT(this, nbt);
}
@SuppressWarnings("unused")
private static boolean upgradeRegistry(NBTTagCompound nbt, int oldVersion) {
if (oldVersion > DATA_VERSION) throw new RuntimeException("Upgrade the mod!"); // TODO: better exceptions
switch (oldVersion) {
case -1: // No version tag
return false;
case 0:
// Upgrade to 1 or return false
case 1:
// Upgrade to 2 or return false
case 2:
// Upgrade to 3 or return false
// ...
}
return true;
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound nbt) {
nbt.setInteger("version", DATA_VERSION);
return NBTUtils.writeToNBT(this, nbt);
}
/** /**
* Create a new blank pocket. * Create a new blank pocket.
@ -126,7 +82,7 @@ PocketRegistry extends WorldSavedData { // TODO: unregister pocket entrances, pr
*/ */
public Pocket newPocket() { public Pocket newPocket() {
Pocket pocket = null; Pocket pocket = null;
while(pocket == null) pocket = newPocket(nextID++); // TODO: config option to reuse IDs (start at 0 rather than nextFreePocket) while(pocket == null) pocket = newPocket(nextID++);
return pocket; return pocket;
} }
@ -150,8 +106,6 @@ PocketRegistry extends WorldSavedData { // TODO: unregister pocket entrances, pr
markDirty(); markDirty();
} }
// TODO: Lookup functions such as getPocketByType/Depth/etc.
/** /**
* Gets the pocket with a certain ID, or null if there is no pocket with that ID. * Gets the pocket with a certain ID, or null if there is no pocket with that ID.
* *
@ -161,22 +115,6 @@ PocketRegistry extends WorldSavedData { // TODO: unregister pocket entrances, pr
return pockets.get(id); return pockets.get(id);
} }
// TODO: these should be per-map rather than per-world
public int getPrivatePocketID(UUID playerUUID) {
Integer id = privatePocketMap.get(playerUUID.toString());
if (id == null) return -1;
return id;
}
public UUID getPrivatePocketOwner(int id) {
return UUID.fromString(privatePocketMap.inverse().get(id));
}
public void setPrivatePocketID(UUID playerUUID, int id) {
privatePocketMap.put(playerUUID.toString(), id);
markDirty();
}
public GridUtils.GridPos idToGridPos(int id) { public GridUtils.GridPos idToGridPos(int id) {
return GridUtils.numToPos(id); return GridUtils.numToPos(id);
} }
@ -211,7 +149,7 @@ PocketRegistry extends WorldSavedData { // TODO: unregister pocket entrances, pr
return getPocket(posToID(pos)); return getPocket(posToID(pos));
} }
public boolean isPlayerAllowedToBeHere(EntityPlayerMP player, BlockPos pos) { public boolean isPlayerAllowedToBeAt(EntityPlayerMP player, BlockPos pos) {
Pocket pocket = getPocketAt(pos); Pocket pocket = getPocketAt(pos);
return pocket != null && pocket.isInBounds(pos); return pocket != null && pocket.isInBounds(pos);
} }

View file

@ -0,0 +1,72 @@
package org.dimdev.pocketlib;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.storage.MapStorage;
import net.minecraft.world.storage.WorldSavedData;
import org.dimdev.annotatednbt.NBTSerializable;
import org.dimdev.annotatednbt.Saved;
import org.dimdev.ddutils.WorldUtils;
import org.dimdev.ddutils.nbt.INBTStorable;
import org.dimdev.ddutils.nbt.NBTUtils;
import org.dimdev.dimdoors.DimDoors;
import java.util.UUID;
@NBTSerializable public class PrivatePocketData extends WorldSavedData {
@AllArgsConstructor @NoArgsConstructor @EqualsAndHashCode @ToString
@NBTSerializable protected static class PocketInfo implements INBTStorable {
@Saved protected int dim;
@Saved protected int id;
@Override public void readFromNBT(NBTTagCompound nbt) { NBTUtils.readFromNBT(this, nbt); }
@Override public NBTTagCompound writeToNBT(NBTTagCompound nbt) { return NBTUtils.writeToNBT(this, nbt); }
}
private static final String DATA_NAME = DimDoors.MODID + "_private_pockets";
@Saved protected BiMap<String, PocketInfo> privatePocketMap = HashBiMap.create(); // Player UUID -> Pocket Info TODO: fix AnnotatedNBT and use UUID rather than String
public PrivatePocketData(String name) {
super(name);
}
public PrivatePocketData() {
super(DATA_NAME);
}
public static PrivatePocketData instance() {
MapStorage storage = WorldUtils.getWorld(0).getMapStorage();
PrivatePocketData instance = (PrivatePocketData) storage.getOrLoadData(PrivatePocketData.class, DATA_NAME);
if (instance == null) {
instance = new PrivatePocketData();
storage.setData(DATA_NAME, instance);
}
return instance;
}
@Override public void readFromNBT(NBTTagCompound nbt) { NBTUtils.readFromNBT(this, nbt); }
@Override public NBTTagCompound writeToNBT(NBTTagCompound nbt) { return NBTUtils.writeToNBT(this, nbt); }
public Pocket getPrivatePocket(UUID playerUUID) {
PocketInfo pocket = privatePocketMap.get(playerUUID.toString());
if (pocket == null) return null;
return PocketRegistry.instance(pocket.dim).getPocket(pocket.id);
}
public void setPrivatePocketID(UUID playerUUID, Pocket pocket) {
privatePocketMap.put(playerUUID.toString(), new PocketInfo(pocket.getDim(), pocket.getId()));
markDirty();
}
public UUID getPrivatePocketOwner(Pocket pocket) {
return UUID.fromString(privatePocketMap.inverse().get(new PocketInfo(pocket.getDim(), pocket.getId())));
}
}

View file

@ -1,56 +1,42 @@
package org.dimdev.dimdoors.shared; package org.dimdev.pocketlib;
import lombok.*; import lombok.*;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import org.dimdev.annotatednbt.NBTSerializable; import org.dimdev.annotatednbt.NBTSerializable;
import org.dimdev.annotatednbt.Saved; import org.dimdev.annotatednbt.Saved;
import org.dimdev.ddutils.Location;
import org.dimdev.ddutils.nbt.INBTStorable; import org.dimdev.ddutils.nbt.INBTStorable;
import org.dimdev.ddutils.nbt.NBTUtils; import org.dimdev.ddutils.nbt.NBTUtils;
import org.dimdev.dimdoors.DimDoors; import org.dimdev.dimdoors.shared.Config;
import org.dimdev.dimdoors.shared.pockets.Pocket;
import org.dimdev.dimdoors.shared.pockets.PocketRegistry;
import org.dimdev.ddutils.Location;
import org.dimdev.dimdoors.shared.world.ModDimensions;
import org.dimdev.dimdoors.shared.world.limbo.WorldProviderLimbo; import org.dimdev.dimdoors.shared.world.limbo.WorldProviderLimbo;
import org.dimdev.dimdoors.shared.world.pocketdimension.WorldProviderPocket;
/*@Value*/ @ToString @AllArgsConstructor @NoArgsConstructor @Builder(toBuilder = true) /*@Value*/ @ToString @AllArgsConstructor @NoArgsConstructor @Builder(toBuilder = true)
@NBTSerializable public class VirtualLocation implements INBTStorable { // TODO: fix AnnotatedNBT and rename this class back to VirtualLocation @NBTSerializable public class VirtualLocation implements INBTStorable {
@Saved @Getter protected int dim; @Saved @Getter protected int dim;
@Saved @Getter protected int x; @Saved @Getter protected int x;
@Saved @Getter protected int z; @Saved @Getter protected int z;
@Saved @Getter protected int depth; // TODO: convert to doubles @Saved @Getter protected int depth; // TODO: convert to doubles
@Override public void readFromNBT(NBTTagCompound nbt) { NBTUtils.readFromNBT(this, nbt); } @Override public void readFromNBT(NBTTagCompound nbt) { NBTUtils.readFromNBT(this, nbt); }
@Override public NBTTagCompound writeToNBT(NBTTagCompound nbt) { return NBTUtils.writeToNBT(this, nbt); } @Override public NBTTagCompound writeToNBT(NBTTagCompound nbt) { return NBTUtils.writeToNBT(this, nbt); }
public static VirtualLocation fromLocation(Location location) { public static VirtualLocation fromLocation(Location location) {
VirtualLocation virtualLocation = null; VirtualLocation virtualLocation = null;
if (ModDimensions.isDimDoorsPocketDimension(location.getDim())) { if (location.getWorld().provider instanceof WorldProviderPocket) {
Pocket pocket = PocketRegistry.instance(location.getDim()).getPocketAt(location.getPos()); Pocket pocket = PocketRegistry.instance(location.getDim()).getPocketAt(location.getPos());
if (pocket != null) { if (pocket != null) {
virtualLocation = pocket.getVirtualLocation(); // TODO: pocket-relative coordinates virtualLocation = pocket.getVirtualLocation(); // TODO: pocket-relative coordinates
} else { } else {
DimDoors.log.warn("Tried to get VirtualLocation at " + location + " which is inside a pocket dimension but outside of a pocket");
virtualLocation = null; // TODO: door was placed in a pocket dim but outside of a pocket... virtualLocation = null; // TODO: door was placed in a pocket dim but outside of a pocket...
} }
} else if (location.getWorld().provider instanceof WorldProviderLimbo) { } else if (location.getWorld().provider instanceof WorldProviderLimbo) { // TODO: convert to interface on worldprovider
virtualLocation = new VirtualLocation(location.getDim(), location.getX(), location.getZ(), Config.getMaxDungeonDepth()); virtualLocation = new VirtualLocation(location.getDim(), location.getX(), location.getZ(), Config.getMaxDungeonDepth());
} } // TODO: nether coordinate transform
if (virtualLocation == null) { if (virtualLocation == null) {
virtualLocation = new VirtualLocation(0, location.getX(), location.getZ(), 5); // TODO virtualLocation = new VirtualLocation(0, location.getX(), location.getZ(), 5); // TODO
DimDoors.log.warn("VirtualLocation was null at " + location);
} }
return virtualLocation; return virtualLocation;
} }
/*// TODO: world-seed based transformations and pocket selections
public VirtualLocation transformDepth(int depth) { // TODO: Config option for block ratio between depths (see video of removed features)
Random random = new Random();
int depthDiff = Math.abs(this.depth - depth);
int base = Config.getOwCoordinateOffsetBase();
double power = Config.getOwCoordinateOffsetPower();
int xOffset = random.nextInt((int) Math.pow(base * (depthDiff + 1), power)) * (random.nextBoolean() ? 1 : -1);
int zOffset = random.nextInt((int) Math.pow(base * (depthDiff + 1), power)) * (random.nextBoolean() ? 1 : -1);
return new VirtualLocation(getDim(), getPos().offset(EnumFacing.EAST, xOffset).offset(EnumFacing.SOUTH, zOffset), depth);
}*/
} }