Skip to content

Commit 26b3035

Browse files
committed
Private pocket fixes
1 parent 4c7c10f commit 26b3035

3 files changed

Lines changed: 47 additions & 30 deletions

File tree

src/main/java/com/zixiken/dimdoors/shared/pockets/PocketRegistry.java

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import lombok.Getter;
1414
import net.minecraft.entity.player.EntityPlayer;
1515
import net.minecraft.entity.player.EntityPlayerMP;
16+
import net.minecraft.nbt.NBTBase;
1617
import net.minecraft.nbt.NBTTagCompound;
1718
import net.minecraft.nbt.NBTTagList;
1819
import net.minecraft.world.storage.MapStorage;
@@ -29,9 +30,9 @@ public class PocketRegistry extends WorldSavedData {
2930
@Getter private int publicPocketSize;
3031
private Map<String, Integer> privatePocketMap; // Player UUID -> Pocket ID, in pocket dim only
3132
@Getter private Map<Integer, Pocket> pockets; // TODO: remove getter?
33+
@Getter private int nextID;
3234

3335
@Getter private int dimID;
34-
@Getter private int nextFreeID;
3536

3637
public PocketRegistry() {
3738
super(DATA_NAME);
@@ -63,7 +64,7 @@ public void initNewRegistry() {
6364
privatePocketSize = DDConfig.getPrivatePocketSize();
6465
publicPocketSize = DDConfig.getPublicPocketSize();
6566

66-
nextFreeID = 0;
67+
nextID = 0;
6768
pockets = new HashMap<>();
6869
privatePocketMap = new HashMap<>();
6970
}
@@ -85,15 +86,13 @@ public void readFromNBT(NBTTagCompound nbt) {
8586
privatePocketSize = nbt.getInteger("privatePocketSize");
8687
publicPocketSize = nbt.getInteger("publicPocketSize");
8788
privatePocketMap = NBTUtils.readMapStringInteger(nbt.getCompoundTag("privatePocketMap"));
89+
nextID = nbt.getInteger("nextID");
8890

89-
NBTTagList pocketsTagList = (NBTTagList) nbt.getTag("pockets");
9091
pockets = new HashMap<>();
91-
for (int id = 0; id < pocketsTagList.tagCount(); id++) { // TODO: convert to map to be able to skip IDs efficiently
92-
NBTTagCompound pocketTag = pocketsTagList.getCompoundTagAt(id);
93-
if (!pocketTag.getBoolean("nullPocket")) {
94-
Pocket pocket = Pocket.readFromNBT(pocketTag);
95-
pockets.put(id, pocket);
96-
}
92+
NBTTagList pocketsNBT = (NBTTagList) nbt.getTag("pockets");
93+
for (NBTBase pocketNBT : pocketsNBT) { // TODO: convert to map to be able to skip IDs efficiently
94+
NBTTagCompound pocketNBTC = (NBTTagCompound) pocketNBT;
95+
pockets.put(pocketNBTC.getInteger("id"), Pocket.readFromNBT(pocketNBTC));
9796
}
9897

9998
for (Pocket pocket : pockets.values()) {
@@ -126,19 +125,16 @@ public NBTTagCompound writeToNBT(NBTTagCompound nbt) {
126125
nbt.setInteger("privatePocketSize", privatePocketSize);
127126
nbt.setInteger("publicPocketSize", publicPocketSize);
128127
nbt.setTag("privatePocketMap", NBTUtils.writeMapStringInteger(privatePocketMap));
129-
130-
NBTTagList pocketsTagList = new NBTTagList();
131-
for (int i = 0; i < nextFreeID; i++) {
132-
Pocket pocket = pockets.get(i);
133-
if (pocket == null) {
134-
NBTTagCompound nullPocket = new NBTTagCompound();
135-
nullPocket.setBoolean("nullPocket", true);
136-
pocketsTagList.appendTag(nullPocket);
137-
} else {
138-
pocketsTagList.appendTag(Pocket.writeToNBT(pocket));
139-
}
128+
nbt.setInteger("nextID", nextID);
129+
130+
NBTTagList pocketsNBT = new NBTTagList();
131+
for (Map.Entry<Integer, Pocket> pocketEntry : pockets.entrySet()) {
132+
if (pocketEntry.getValue() == null) continue;
133+
NBTTagCompound pocketNBT = (NBTTagCompound) Pocket.writeToNBT(pocketEntry.getValue());
134+
pocketNBT.setInteger("id", pocketEntry.getKey()); // TODO: store separately?
135+
pocketsNBT.appendTag(pocketNBT);
140136
}
141-
nbt.setTag("pockets", pocketsTagList);
137+
nbt.setTag("pockets", pocketsNBT);
142138

143139
return nbt;
144140
}
@@ -150,7 +146,7 @@ public NBTTagCompound writeToNBT(NBTTagCompound nbt) {
150146
*/
151147
public Pocket newPocket(int depth) {
152148
Pocket pocket = null;
153-
while(pocket == null) pocket = newPocket(nextFreeID++, depth); // TODO: config option to reuse IDs (start at 0 rather than nextFreePocket)
149+
while(pocket == null) pocket = newPocket(nextID++, depth); // TODO: config option to reuse IDs (start at 0 rather than nextFreePocket)
154150
return pocket;
155151
}
156152

@@ -165,7 +161,7 @@ public Pocket newPocket(int id, int depth) {
165161
GridUtils.GridPos pos = getGridPosFromID(id);
166162
Pocket pocket = new Pocket(id, dimID, pos.getX(), pos.getZ(), depth);
167163
pockets.put(id, pocket);
168-
if (id >= nextFreeID) nextFreeID = id + 1;
164+
if (id >= nextID) nextID = id + 1;
169165
markDirty();
170166
return pocket;
171167
}

src/main/java/com/zixiken/dimdoors/shared/rifts/TileEntityRift.java

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,18 @@
1515
import lombok.Getter;
1616
import net.minecraft.block.state.IBlockState;
1717
import net.minecraft.entity.Entity;
18+
import net.minecraft.entity.EntityLiving;
1819
import net.minecraft.entity.IEntityOwnable;
20+
import net.minecraft.entity.item.EntityItem;
1921
import net.minecraft.entity.player.EntityPlayer;
22+
import net.minecraft.entity.player.EntityPlayerMP;
23+
import net.minecraft.entity.projectile.*;
2024
import net.minecraft.nbt.NBTBase;
2125
import net.minecraft.nbt.NBTTagCompound;
2226
import net.minecraft.nbt.NBTTagList;
2327
import net.minecraft.network.NetworkManager;
2428
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
29+
import net.minecraft.server.management.PlayerList;
2530
import net.minecraft.tileentity.TileEntity;
2631
import net.minecraft.util.ITickable;
2732
import net.minecraft.util.math.BlockPos;
@@ -269,6 +274,7 @@ public boolean teleport(Entity entity) { try { // TODO: return failiure message
269274
RiftDestination dest = weightedDestination.getDestination();
270275
Location destLoc;
271276
GlobalDestination destHere = new GlobalDestination(new Location(world, pos)); // TODO: local if possible
277+
String uuid = getEntityOwnerUUID(entity);
272278
switch (dest.getType()) {
273279
case RELATIVE:
274280
case LOCAL:
@@ -283,9 +289,6 @@ public boolean teleport(Entity entity) { try { // TODO: return failiure message
283289
if (destLoc != null) makeDestinationPermanent(weightedDestination, destLoc);
284290
break;
285291
case PRIVATE: // TODO: move logic to PrivatePocketTeleportDestination
286-
String uuid = null;
287-
if (entity instanceof EntityPlayer) uuid = entity.getUniqueID().toString();
288-
if (entity instanceof IEntityOwnable && ((IEntityOwnable) entity).getOwnerId() != null) uuid = ((IEntityOwnable) entity).getOwnerId().toString();
289292
if (uuid != null) {
290293
PocketRegistry privatePocketRegistry = PocketRegistry.getForDim(DimDoorDimensions.getPrivateDimID());
291294
RiftRegistry privateRiftRegistry = RiftRegistry.getForDim(DimDoorDimensions.getPrivateDimID());
@@ -312,9 +315,6 @@ public boolean teleport(Entity entity) { try { // TODO: return failiure message
312315
break;
313316
case ESCAPE:
314317
case PRIVATE_POCKET_EXIT:
315-
/*String*/ uuid = null;
316-
if (entity instanceof EntityPlayer) uuid = entity.getUniqueID().toString();
317-
if (entity instanceof IEntityOwnable && ((IEntityOwnable) entity).getOwnerId() != null) uuid = ((IEntityOwnable) entity).getOwnerId().toString();
318318
if (uuid != null) {
319319
RiftRegistry privateRiftRegistry = RiftRegistry.getForDim(DimDoorDimensions.getPrivateDimID());
320320
destLoc = RiftRegistry.getEscapeRift(uuid);
@@ -390,7 +390,6 @@ public boolean teleport(Entity entity) { try { // TODO: return failiure message
390390
if (!(tileEntityAtLoc instanceof TileEntityRift)) throw new RuntimeException("The rift referenced by this rift does not exist, this is a bug.");
391391
TileEntityRift destRift = (TileEntityRift) tileEntityAtLoc;
392392
if (entity instanceof EntityPlayer && !DimDoorDimensions.isPocketDimension(WorldUtils.getDim(world))) { // TODO: What about player-owned entities? We should store their exit rift separately to avoid having problems if they enter different rifts
393-
String uuid = entity.getUniqueID().toString(); // TODO: More configuration on which worlds should be considered normal worlds. Other mods might add mostly void worlds, causing problems with random coordinates
394393
RiftRegistry.setEscapeRift(uuid, new Location(world, pos));
395394
}
396395
destRift.teleportTo(entity);
@@ -403,6 +402,27 @@ public boolean teleport(Entity entity) { try { // TODO: return failiure message
403402
}
404403
}
405404

405+
public String getEntityOwnerUUID(Entity entity) { // TODO: make this recursive, move to utils
406+
if (entity instanceof EntityThrowable) entity = ((EntityThrowable) entity).getThrower();
407+
if (entity instanceof EntityArrow) entity = ((EntityArrow) entity).shootingEntity;
408+
if (entity instanceof EntityFireball) entity = ((EntityFireball) entity).shootingEntity;
409+
if (entity instanceof EntityLlamaSpit) entity = ((EntityLlamaSpit) entity).owner; // Llamas are ownable
410+
if (entity.getControllingPassenger() != null && !(entity instanceof EntityPlayer)) entity = entity.getControllingPassenger();
411+
if (entity.getPassengers().size() > 0) entity.getPassengers().get(0);
412+
if (entity instanceof EntityFishHook) entity = ((EntityFishHook) entity).getAngler();
413+
if (entity instanceof EntityLiving && ((EntityLiving) entity).getLeashed()) entity = ((EntityLiving) entity).getLeashHolder();
414+
if (entity instanceof EntityItem) {
415+
String playerName = ((EntityItem) entity).getThrower();
416+
EntityPlayer player = null;
417+
if (playerName != null) player = entity.world.getPlayerEntityByName(((EntityItem) entity).getThrower());
418+
if (player != null) entity = player;
419+
}
420+
421+
if (entity instanceof IEntityOwnable && ((IEntityOwnable) entity).getOwnerId() != null) return ((IEntityOwnable) entity).getOwnerId().toString();
422+
if (entity instanceof EntityPlayer) return entity.getUniqueID().toString(); // ownable players shouldn't be a problem, but just in case we have a slave mod, check their owner's uuid first to send them to their owner's pocket :)
423+
return null;
424+
}
425+
406426
private void makeDestinationPermanent(WeightedRiftDestination weightedDestination, Location destLoc) {
407427
riftStateChanged = true;
408428
RiftDestination newDest;

src/main/java/com/zixiken/dimdoors/shared/world/limbodimension/LimboDecay.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public static IBlockState[] getBlocksImmuneToDecay() {
4949
blocksImmuneToDecay = new IBlockState[] {
5050
ModBlocks.FABRIC.getDefaultState().withProperty(BlockFabric.TYPE, BlockFabric.EnumType.UNRAVELED),
5151
ModBlocks.FABRIC.getDefaultState().withProperty(BlockFabric.TYPE, BlockFabric.EnumType.ETERNAL),
52+
ModBlocks.FABRIC.getDefaultState().withProperty(BlockFabric.TYPE, BlockFabric.EnumType.ANCIENT),
5253
ModBlocks.TRANSIENT_DIMENSIONAL_DOOR.getDefaultState(),
5354
ModBlocks.DIMENSIONAL_DOOR.getDefaultState(),
5455
ModBlocks.WARP_DIMENSIONAL_DOOR.getDefaultState(),

0 commit comments

Comments
 (0)