forked from ldtteam/minecolonies
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Increases forge version to latest recommended (Still works fine). Move chunk-storage to a world capability to avoid heavy IO. Preload 5 chunks into each direction after placing colony.
- Loading branch information
Showing
11 changed files
with
454 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
src/api/java/com/minecolonies/api/colony/IChunkmanagerCapability.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
package com.minecolonies.api.colony; | ||
|
||
import com.minecolonies.api.util.ChunkLoadStorage; | ||
import com.minecolonies.api.util.NBTUtils; | ||
import net.minecraft.nbt.NBTBase; | ||
import net.minecraft.nbt.NBTTagCompound; | ||
import net.minecraft.util.EnumFacing; | ||
import net.minecraft.util.Tuple; | ||
import net.minecraft.util.math.ChunkPos; | ||
import net.minecraftforge.common.capabilities.Capability; | ||
import net.minecraftforge.common.util.Constants; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import javax.annotation.Nullable; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static com.minecolonies.api.util.constant.NbtTagConstants.*; | ||
|
||
/** | ||
* | ||
* Capability for the colony tag for chunks | ||
*/ | ||
public interface IChunkmanagerCapability | ||
{ | ||
/** | ||
* Get the chunkStorage at a certain location. | ||
* @param chunkX the x chunk location. | ||
* @param chunkZ the z chunk location. | ||
* @return the storage or null. | ||
*/ | ||
@Nullable | ||
ChunkLoadStorage getChunkStorage(int chunkX, int chunkZ); | ||
|
||
/** | ||
* Add a new chunkStorage. | ||
* @param chunkX chunkX the x chunk location. | ||
* @param chunkZ chunkX the z chunk location. | ||
* @param storage the new to add or update. | ||
* @return true if override else false. | ||
*/ | ||
boolean addChunkStorage(int chunkX, int chunkZ, ChunkLoadStorage storage); | ||
|
||
/** | ||
* Get all chunk storages for serialization. | ||
* @return the storages. | ||
*/ | ||
Map<ChunkPos, ChunkLoadStorage> getAllChunkStorages(); | ||
|
||
/** | ||
* The implementation of the colonyTagCapability. | ||
*/ | ||
class Impl implements IChunkmanagerCapability | ||
{ | ||
/** | ||
* Map of chunkPos to chunkLoadStorage. | ||
*/ | ||
private final Map<ChunkPos, ChunkLoadStorage> chunkStorages = new HashMap<>(); | ||
|
||
@Nullable | ||
@Override | ||
public ChunkLoadStorage getChunkStorage(final int chunkX, final int chunkZ) | ||
{ | ||
return chunkStorages.remove(new ChunkPos(chunkX, chunkZ)); | ||
} | ||
|
||
@Override | ||
public boolean addChunkStorage(final int chunkX, final int chunkZ, final ChunkLoadStorage storage) | ||
{ | ||
final ChunkLoadStorage existingStorage = chunkStorages.get(new ChunkPos(chunkX, chunkZ)); | ||
if (existingStorage == null) | ||
{ | ||
chunkStorages.put(new ChunkPos(chunkX, chunkZ), storage); | ||
return false; | ||
} | ||
else | ||
{ | ||
existingStorage.merge(storage); | ||
return true; | ||
} | ||
} | ||
|
||
|
||
@Override | ||
public Map<ChunkPos, ChunkLoadStorage> getAllChunkStorages() | ||
{ | ||
return chunkStorages; | ||
} | ||
} | ||
|
||
/** | ||
* The storage class of the capability. | ||
*/ | ||
class Storage implements Capability.IStorage<IChunkmanagerCapability> | ||
{ | ||
@Override | ||
public NBTBase writeNBT(@NotNull final Capability<IChunkmanagerCapability> capability, @NotNull final IChunkmanagerCapability instance, @Nullable final EnumFacing side) | ||
{ | ||
final NBTTagCompound compound = new NBTTagCompound(); | ||
compound.setTag(TAG_ALL_CHUNK_STORAGES, instance.getAllChunkStorages().entrySet().stream().map(entry -> write(entry.getKey(), entry.getValue())).collect(NBTUtils.toNBTTagList())); | ||
return compound; | ||
} | ||
|
||
@Override | ||
public void readNBT(@NotNull final Capability<IChunkmanagerCapability> capability, @NotNull final IChunkmanagerCapability instance, | ||
@Nullable final EnumFacing side, @NotNull final NBTBase nbt) | ||
{ | ||
if(nbt instanceof NBTTagCompound && ((NBTTagCompound) nbt).hasKey(TAG_ID)) | ||
{ | ||
NBTUtils.streamCompound(((NBTTagCompound) nbt).getTagList(TAG_ALL_CHUNK_STORAGES, Constants.NBT.TAG_COMPOUND)) | ||
.map(Storage::read).forEach(key -> instance.addChunkStorage(key.getFirst().x, key.getFirst().z, key.getSecond())); | ||
} | ||
} | ||
|
||
/** | ||
* Write a single ChunkPos, ChunkLoadStorage pair to nbt. | ||
* @param key the key. | ||
* @param value the value | ||
* @return the resulting compound. | ||
*/ | ||
private static NBTTagCompound write(final ChunkPos key, final ChunkLoadStorage value) | ||
{ | ||
final NBTTagCompound compound = new NBTTagCompound(); | ||
compound.setTag(TAG_CHUNK_STORAGE, value.toNBT()); | ||
compound.setInteger(TAG_X, key.x); | ||
compound.setInteger(TAG_Z, key.z); | ||
return compound; | ||
} | ||
|
||
/** | ||
* Read a key value pair for the chunkloadstorages. | ||
* @param compound the compound to read it from. | ||
* @return a tuple for both. | ||
*/ | ||
private static Tuple<ChunkPos, ChunkLoadStorage> read(final NBTTagCompound compound) | ||
{ | ||
final ChunkLoadStorage storage = new ChunkLoadStorage(compound.getCompoundTag(TAG_CHUNK_STORAGE)); | ||
final int x = compound.getInteger(TAG_X); | ||
final int z = compound.getInteger(TAG_Z); | ||
return new Tuple<>(new ChunkPos(x,z), storage); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.