Skip to content

Commit

Permalink
Feature/world cap (ldtteam#2583)
Browse files Browse the repository at this point in the history
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
Raycoms authored Jul 3, 2018
1 parent 9067942 commit b801bda
Show file tree
Hide file tree
Showing 11 changed files with 454 additions and 144 deletions.
3 changes: 1 addition & 2 deletions build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
#
# Minecraft / Forge.
minecraft_version=1.12.2
forge_version=14.23.3.2655
forge_version=14.23.4.2705
mappings=snapshot_20180108
#
# Dependencies
guidebook_version=2.0.1
guidebook_core_version=0.6.4
Expand Down
143 changes: 143 additions & 0 deletions src/api/java/com/minecolonies/api/colony/IChunkmanagerCapability.java
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import static com.minecolonies.api.util.constant.NbtTagConstants.TAG_ID;

/**
* Not used for now, maybe in 1.13?
*
* Capability for the colony tag for chunks
*/
public interface IColonyTagCapability
Expand Down Expand Up @@ -79,6 +79,7 @@ public class Impl implements IColonyTagCapability
*/
private int owningColony = 0;

@NotNull
@Override
public IColonyTagCapability addColony(final int id)
{
Expand All @@ -96,6 +97,7 @@ public void reset()
owningColony = 0;
}

@NotNull
@Override
public IColonyTagCapability removeColony(final int id)
{
Expand All @@ -113,6 +115,7 @@ public IColonyTagCapability removeColony(final int id)
return this;
}

@NotNull
@Override
public IColonyTagCapability setOwningColony(final int id)
{
Expand Down Expand Up @@ -144,7 +147,7 @@ public NBTBase writeNBT(@NotNull final Capability<IColonyTagCapability> capabili
{
final NBTTagCompound compound = new NBTTagCompound();
compound.setInteger(TAG_ID, instance.getOwningColony());
compound.setTag(TAG_COLONIES, instance.getAllCloseColonies().stream().map(id -> Storage.write(id)).collect(NBTUtils.toNBTTagList()));
compound.setTag(TAG_COLONIES, instance.getAllCloseColonies().stream().map(Storage::write).collect(NBTUtils.toNBTTagList()));
return compound;
}

Expand All @@ -165,7 +168,7 @@ public void readNBT(@NotNull final Capability<IColonyTagCapability> capability,
* @param id the id.
* @return the compound of it.
*/
private static NBTTagCompound write(@NotNull final int id)
private static NBTTagCompound write(final int id)
{
final NBTTagCompound compound = new NBTTagCompound();
compound.setInteger(TAG_ID, id);
Expand Down
28 changes: 9 additions & 19 deletions src/api/java/com/minecolonies/api/util/ChunkLoadStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.util.stream.Collectors;

import static com.minecolonies.api.util.constant.NbtTagConstants.*;
import static com.minecolonies.api.util.constant.NbtTagConstants.TAG_WAYPOINT;

/**
* The chunkload storage used to load chunks with colony information.
Expand Down Expand Up @@ -115,24 +114,6 @@ private static NBTTagCompound getCompoundOfColonyId(final int id)
return compound;
}

/**
* Getter for the list of colonies to add.
* @return a copy of the list.
*/
public List<Integer> getColoniesToAdd()
{
return new ArrayList<>(coloniesToAdd);
}

/**
* Getter for the list of colonies to remove.
* @return a copy of the list.
*/
public List<Integer> getColoniesToRemove()
{
return new ArrayList<>(coloniesToRemove);
}

/**
* Getter for the colonyId.
* @return the id.
Expand All @@ -151,6 +132,15 @@ public int getDimension()
return dimension;
}

/**
* Get the x long.
* @return the long representing two integers.
*/
public long getXz()
{
return xz;
}

@Override
public boolean equals(final Object o)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,26 @@ public final class NbtTagConstants
*/
public static final String SECOND_POS_STRING = "pos2";

/**
* Tag for all chunk storages..
*/
public static final String TAG_CHUNK_STORAGE = "chunk";

/**
* Var for first pos string.
*/
public static final String TAG_X = "xPos";

/**
* Var for second pos string.
*/
public static final String TAG_Z = "zPos";

/**
* Tag for all chunk storages..
*/
public static final String TAG_ALL_CHUNK_STORAGES = "allchunk";

/**
* Private constructor to hide the implicit one.
*/
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/com/minecolonies/coremod/MineColonies.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.minecolonies.coremod;

import com.minecolonies.api.colony.IColonyTagCapability;
import com.minecolonies.api.colony.IChunkmanagerCapability;
import com.minecolonies.api.configuration.Configurations;
import com.minecolonies.api.util.constant.Constants;
import com.minecolonies.coremod.achievements.ModAchievements;
Expand Down Expand Up @@ -41,6 +42,9 @@ public class MineColonies
@CapabilityInject(IColonyTagCapability.class)
public static Capability<IColonyTagCapability> CLOSE_COLONY_CAP;

@CapabilityInject(IChunkmanagerCapability.class)
public static Capability<IChunkmanagerCapability> CHUNK_STORAGE_UPDATE_CAP;

private static final Logger logger = LogManager.getLogger(Constants.MOD_ID);
/**
* Forge created instance of the Mod.
Expand Down Expand Up @@ -102,7 +106,8 @@ public static Logger getLogger()
@Mod.EventHandler
public void preInit(@NotNull final FMLPreInitializationEvent event)
{
CapabilityManager.INSTANCE.register(IColonyTagCapability.class, new IColonyTagCapability.Storage(), IColonyTagCapability.Impl.class);
CapabilityManager.INSTANCE.register(IColonyTagCapability.class, new IColonyTagCapability.Storage(), IColonyTagCapability.Impl::new);
CapabilityManager.INSTANCE.register(IChunkmanagerCapability.class, new IChunkmanagerCapability.Storage(), IChunkmanagerCapability.Impl::new);

StandardFactoryControllerInitializer.onPreInit();
proxy.registerEntities();
Expand Down
Loading

0 comments on commit b801bda

Please sign in to comment.