Skip to content

Commit

Permalink
Dynmap compatability (ldtteam#8784)
Browse files Browse the repository at this point in the history
Ability to render borders of colonies on Dynmap using a new marker set "Colonies".
Renders the colony name, owner and population count on the description of the colony.
Automatically updates Dynmap when the following occur:
The colony name gets changed
The colony it's team color is changed
The colony borders change when a building is constructured/repaired/removed
The colony it's population count changes, either when colonists are born or die.
What is not added in the pull request:

Ability for Minecolonies specific blocks (primarly hut blocks) to render on Dynmap
Ability for DO blocks to render on Dynmap (this would have to be implemented from within DO, should not appear here)
  • Loading branch information
Thodor12 committed Dec 22, 2022
1 parent 5aac960 commit f0f45e7
Show file tree
Hide file tree
Showing 21 changed files with 855 additions and 29 deletions.
7 changes: 7 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ jmapApiVersion=1.18.1-1.8-SNAPSHOT
jmapMcVersion=1.18.1
jmapVersion=5.8.0beta11
tinkersConstructVersion=3.5.0.17
dynmapApiVersion=3.4
dynmapMcVersion=1.18.2
dynmapVersion=3.4
mantleVersion=1.9.20
dynamicTreesVersion=0.11.0-Beta4
# some mods include the MC version as part of their "real" version number, others
Expand All @@ -47,6 +50,10 @@ dynamicTreesVersion=0.11.0-Beta4
# then drop the regular jar into the "libs" dir and set this to true (editing version if needed)
runWithJourneymap=false

# Dynmap doesn't have a maven for its release artifacts so if you want to use it in dev
# then drop the Dynmap jar into the "libs" dir and set this to true (editing version if needed)
runWithDynmap=false

useShadowJar=true

githubUrl=https://github.com/ldtteam/Minecolonies
Expand Down
1 change: 1 addition & 0 deletions gradle/configuration.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ project.ext.customChangelogHeader = "\n" +
"- JEI: ${project.jei_version} (or above)\n" +
"- Dynamic Trees: ${project.dynamicTreesVersion} (or above)\n" +
"- Journeymap: ${project.jmapVersion} (or above)\n" +
"- Dynmap: ${project.dynmapVersion} (or above)\n" +
"\n"
11 changes: 11 additions & 0 deletions gradle/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ repositories {
name "JourneyMap"
url "https://jm.gserv.me/repository/maven-public/"
}
maven {
name "Dynmap"
url = "https://repo.mikeprimm.com/"
}
}

dependencies {
Expand Down Expand Up @@ -57,6 +61,13 @@ dependencies {
if (project.hasProperty("runWithJourneymap") && project.runWithJourneymap.toBoolean()) {
runtimeOnly fg.deobf("flat:journeymap:${project.jmapMcVersion}-${project.jmapVersion}")
}

compileOnly fg.deobf("us.dynmap:DynmapCoreAPI:${project.dynmapApiVersion}")
apiCompileOnly fg.deobf("us.dynmap:DynmapCoreAPI:${project.dynmapApiVersion}")
if (project.hasProperty("runWithDynmap") && project.runWithDynmap.toBoolean()) {
runtimeOnly fg.deobf("flat:dynmap:${project.dynmapMcVersion}-${project.dynmapVersion}")
}

// replace "compileOnly" with "library" when you want to include it for testing
compileOnly fg.deobf("slimeknights.tconstruct:TConstruct:${project.exactMinecraftVersion}-${project.tinkersConstructVersion}")
compileOnly fg.deobf("slimeknights.mantle:Mantle:${project.exactMinecraftVersion}-${project.mantleVersion}")
Expand Down
99 changes: 86 additions & 13 deletions src/api/java/com/minecolonies/api/compatibility/Compatibility.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package com.minecolonies.api.compatibility;

import com.minecolonies.api.colony.IColony;
import com.minecolonies.api.compatibility.dynamictrees.DynamicTreeProxy;
import com.minecolonies.api.compatibility.dynmap.DynmapProxy;
import com.minecolonies.api.compatibility.resourcefulbees.IBeehiveCompat;
import com.minecolonies.api.compatibility.tinkers.*;
import com.minecolonies.api.compatibility.tinkers.SlimeTreeProxy;
import com.minecolonies.api.compatibility.tinkers.TinkersToolProxy;
import com.minecolonies.api.util.constant.IToolType;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.core.BlockPos;
import net.minecraft.core.NonNullList;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.core.NonNullList;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.fml.ModList;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -26,16 +29,17 @@
public final class Compatibility
{

public static IBeehiveCompat beeHiveCompat = new IBeehiveCompat() {};
public static SlimeTreeProxy tinkersSlimeCompat = new SlimeTreeProxy();
public static TinkersToolProxy tinkersCompat = new TinkersToolProxy();
public static DynamicTreeProxy dynamicTreesCompat = new DynamicTreeProxy();
public static DynmapProxy dynmapCompat = new DynmapProxy();

private Compatibility()
{
throw new IllegalAccessError("Utility class");
}

public static IBeehiveCompat beeHiveCompat = new IBeehiveCompat() {};
public static SlimeTreeProxy tinkersSlimeCompat = new SlimeTreeProxy();
public static TinkersToolProxy tinkersCompat = new TinkersToolProxy();
public static DynamicTreeProxy dynamicTreesCompat = new DynamicTreeProxy();

/**
* This method checks to see if STACK is able to mine anything. It goes through all compatibility checks.
*
Expand Down Expand Up @@ -116,11 +120,12 @@ public static boolean isTinkersWeapon(@NotNull final ItemStack stack)

/**
* Check if a certain item stack is a tinkers tool of the given tool type.
* @param stack the stack to check for.
*
* @param stack the stack to check for.
* @param toolType the tool type.
* @return true if so.
*/
public static boolean isTinkersTool(@Nullable final ItemStack stack, IToolType toolType) { return tinkersCompat.isTinkersTool(stack, toolType); }
public static boolean isTinkersTool(@Nullable final ItemStack stack, IToolType toolType) {return tinkersCompat.isTinkersTool(stack, toolType);}

/**
* Calculate the actual attack damage of the tinkers weapon.
Expand Down Expand Up @@ -296,4 +301,72 @@ public static List<ItemStack> getCombsFromHive(BlockPos pos, Level world, int am
{
return beeHiveCompat.getCombsFromHive(pos, world, amount);
}

/**
* Registers the Dynmap integration
*/
public static void registerDynmap()
{
dynmapCompat.registerIntegration();
}

/**
* Called whenever a colony it's name is changed.
*
* @param colony The colony.
*/
public static void updateColonyName(final IColony colony)
{
dynmapCompat.updateColonyName(colony);
}

/**
* Called whenever a colony it's team color is changed.
*
* @param colony The colony.
*/
public static void updateColonyTeamColor(final IColony colony)
{
dynmapCompat.updateColonyTeamColor(colony);
}

/**
* Called whenever a colony it's borders change (due to building (de)construction)
*
* @param colony The colony.
*/
public static void updateColonyBorders(final IColony colony)
{
dynmapCompat.updateColonyBorders(colony);
}

/**
* Called whenever a colony it's amount of citizens changes (includes citizen born, tavern hired, resurrected from death and death itself).
*
* @param colony The colony.
*/
public static void updateColonyCitizenCount(final IColony colony)
{
dynmapCompat.updateColonyCitizenCount(colony);
}

/**
* Called whenever a new colony is created.
*
* @param colony The colony.
*/
public static void colonyCreated(final IColony colony)
{
dynmapCompat.updateColonyCreated(colony);
}

/**
* Called whenever a colony is about to get deleted.
*
* @param colony The colony which is about to get deleted.
*/
public static void colonyDeleted(final IColony colony)
{
dynmapCompat.updateColonyDeleted(colony);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.minecolonies.api.MinecoloniesAPIProxy;
import com.minecolonies.api.colony.requestsystem.StandardFactoryController;
import com.minecolonies.api.compatibility.dynamictrees.DynamicTreeCompat;
import com.minecolonies.api.compatibility.dynmap.DynmapCompat;
import com.minecolonies.api.compatibility.resourcefulbees.ResourcefulBeesCompat;
import com.minecolonies.api.compatibility.tinkers.SlimeTreeCheck;
import com.minecolonies.api.compatibility.tinkers.TinkersToolHelper;
Expand Down Expand Up @@ -937,6 +938,11 @@ private void discoverModCompat()
{
Compatibility.dynamicTreesCompat = new DynamicTreeCompat();
}
if (ModList.get().isLoaded("dynmap"))
{
Compatibility.dynmapCompat = new DynmapCompat();
Compatibility.dynmapCompat.registerIntegration();
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.minecolonies.api.compatibility.dynmap;

import com.minecolonies.api.IMinecoloniesAPI;
import com.minecolonies.api.colony.IColony;
import com.minecolonies.api.colony.buildings.IBuilding;
import net.minecraft.core.BlockPos;
import net.minecraft.util.Tuple;
import net.minecraft.world.level.ChunkPos;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
* Utility class responsible for calculating all claims that are claimed by a colony.
*/
public class ColonyChunkClaimCalculator
{
private ColonyChunkClaimCalculator() {}

/**
* Fetches a collection of {@link ChunkPos}'s depending on what chunks a colony has claimed.
*
* @param colony The colony to calculate the claimed chunks for.
* @return A collection of claimed chunks.
*/
public static Collection<ChunkPos> getAllClaimedChunks(final IColony colony)
{
HashSet<ChunkPos> claimedChunks = new HashSet<>();

// Claim the protection area
getProtectionArea(colony, claimedChunks);

// Claim all the buildings
colony.getBuildingManager().getBuildings().values().forEach(f -> claimBuilding(f, claimedChunks));

return claimedChunks;
}

/**
* Calculate the claimed chunks regarding the protection area (the initial claim size after creating the colony)
*
* @param colony The colony to calculate the protection area for.
* @param claimedChunks The internal set to add the chunks to.
*/
private static void getProtectionArea(final IColony colony, Set<ChunkPos> claimedChunks)
{
ChunkPos protectionAreaCenterChunk = new ChunkPos(colony.getCenter());
Integer protectionAreaRadius = IMinecoloniesAPI.getInstance().getConfig().getServer().initialColonySize.get();
for (int i = -protectionAreaRadius; i <= protectionAreaRadius; i++)
{
for (int j = -protectionAreaRadius; j <= protectionAreaRadius; j++)
{
claimedChunks.add(new ChunkPos(protectionAreaCenterChunk.x + i, protectionAreaCenterChunk.z + j));
}
}
}

/**
* Calculate the claimed chunks regarding all buildings in the colony.
*
* @param building Which building in the colony to check for.
* @param claimedChunks The internal set to add the chunks to.
*/
private static void claimBuilding(final IBuilding building, Set<ChunkPos> claimedChunks)
{
// Add the building center to the claimed chunks
ChunkPos buildingCenterChunk = new ChunkPos(building.getPosition());
claimedChunks.add(buildingCenterChunk);

// Add the overlaying schematic to the claimed chunks
Tuple<BlockPos, BlockPos> corners = building.getCorners();
ChunkPos bottomLeft = new ChunkPos(corners.getA());
ChunkPos topRight = new ChunkPos(corners.getB());
if (!bottomLeft.equals(topRight))
{
// Chunks do not appear on the same XZ coordinate, so this means the building uses multiple chunks
int chunksHorizontal = ((topRight.getMinBlockX() - bottomLeft.getMinBlockX()) / 16) + 1;
int chunksVertical = ((topRight.getMinBlockZ() - bottomLeft.getMinBlockZ()) / 16) + 1;
for (int i = 0; i < chunksHorizontal; i++)
{
for (int j = 0; j < chunksVertical; j++)
{
claimedChunks.add(new ChunkPos(bottomLeft.x + i, bottomLeft.z + j));
}
}
}

// Add the claim radius to the claimed chunks
int radius = building.getClaimRadius(building.getBuildingLevel());
for (int i = -radius; i <= radius; i++)
{
for (int j = -radius; j <= radius; j++)
{
claimedChunks.add(new ChunkPos(buildingCenterChunk.x + i, buildingCenterChunk.z + j));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.minecolonies.api.compatibility.dynmap;

import com.minecolonies.api.util.Log;
import org.dynmap.DynmapCommonAPI;
import org.dynmap.DynmapCommonAPIListener;
import org.dynmap.markers.MarkerAPI;

import java.util.function.Consumer;

/**
* Dynmap integration class to connect Minecolonies to Dynmap it's API listening system.
* This way Dynmap can invoke {@link DynmapApiListener#apiEnabled} in order to
* start generating markers.
*/
public class DynmapApiListener extends DynmapCommonAPIListener
{
private final Consumer<DynmapIntegration> integrationConsumer;

/**
* Constructor containing a consumer callback which is invoked whenever Dynmap is ready
* to start creating markers.
*
* @param integrationConsumer The consumer instance.
*/
public DynmapApiListener(Consumer<DynmapIntegration> integrationConsumer)
{
this.integrationConsumer = integrationConsumer;
}

public void registerListener()
{
DynmapCommonAPIListener.register(this);
}

@Override
public void apiEnabled(final DynmapCommonAPI dynmapCommonAPI)
{
Log.getLogger().info("Dynmap API enabled, registering markers...");
MarkerAPI markerApi = dynmapCommonAPI.getMarkerAPI();

integrationConsumer.accept(new DynmapIntegration(markerApi));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.minecolonies.api.compatibility.dynmap;

/**
* Compat class for when Dynmap is detected and the integration should be enabled.
*/
public class DynmapCompat extends DynmapProxy
{
@Override
public boolean isDynmapPresent()
{
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.minecolonies.api.compatibility.dynmap;

/**
* A small set of constant declarations that the Dynmap plugin uses to handle content towards Dynmap it's map system.
*/
public final class DynmapConstants
{
public static final String DYNMAP_COLONY_MARKER_SET_ID = "minecolonies:colony_markers";
public static final String DYNMAP_COLONY_MARKER_SET_NAME = "Colonies";
public static final String DYNMAP_COLONY_MARKER_FORMAT = "minecolonies:colony:%d";
public static final String DYNMAP_COLONY_DESCRIPTION_FORMAT = "<h2>%s</h2><p>Mayor: %s</p><p>Population: %d</p>";

private DynmapConstants() {}
}
Loading

0 comments on commit f0f45e7

Please sign in to comment.