Skip to content

Commit 256d26e

Browse files
committed
Add compatibility with EntityCulling
1 parent cc74aa4 commit 256d26e

File tree

10 files changed

+175
-28
lines changed

10 files changed

+175
-28
lines changed

build.gradle

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ repositories {
2222
name = "Terraformers"
2323
url = "https://maven.terraformersmc.com/"
2424
}
25+
exclusiveContent {
26+
forRepository {
27+
maven {
28+
name = "Modrinth"
29+
url = "https://api.modrinth.com/maven"
30+
}
31+
}
32+
filter {
33+
includeGroup "maven.modrinth"
34+
}
35+
}
2536
}
2637

2738
loom {
@@ -55,6 +66,10 @@ dependencies {
5566
modImplementation("com.terraformersmc:modmenu:${project.modmenu_version}") {
5667
exclude(group: "net.fabricm.fabric-api")
5768
}
69+
70+
// Entity Culling
71+
// implementation 'com.github.tr7zw:EntityCulling:1.6.6'
72+
modImplementation "maven.modrinth:entityculling:1.6.6"
5873
}
5974

6075
processResources {

src/client/java/io/github/discusser/toomanyentities/TooManyEntitiesClient.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
package io.github.discusser.toomanyentities;
22

3-
import com.sun.jna.Native;
43
import io.github.discusser.toomanyentities.config.MapGuiProvider;
54
import io.github.discusser.toomanyentities.config.TooManyEntitiesConfig;
65
import me.shedaniel.autoconfig.AutoConfig;
76
import me.shedaniel.autoconfig.gui.registry.GuiRegistry;
87
import me.shedaniel.autoconfig.serializer.GsonConfigSerializer;
98
import net.fabricmc.api.ClientModInitializer;
10-
import net.minecraft.entity.EntityType;
9+
import net.fabricmc.loader.api.FabricLoader;
10+
import org.apache.logging.log4j.util.Lazy;
1111
import org.slf4j.Logger;
1212
import org.slf4j.LoggerFactory;
1313

14+
import java.util.HashMap;
1415
import java.util.Map;
1516

1617
public class TooManyEntitiesClient implements ClientModInitializer {
1718
public static final Logger LOGGER = LoggerFactory.getLogger("too-many-entities");
1819
public static final String MODID = "too-many-entities";
20+
public static Lazy<Boolean> entityCullingLoaded = Lazy.lazy(() -> FabricLoader.getInstance().isModLoaded("entityculling"));
21+
public static final HashMap<String, Integer> entityCounts = new HashMap<>();
1922

2023
@Override
2124
public void onInitializeClient() {

src/client/java/io/github/discusser/toomanyentities/config/TooManyEntitiesConfig.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ public class TooManyEntitiesConfig implements ConfigData {
1818
@ConfigEntry.Category(value = "general")
1919
@ConfigEntry.Gui.Tooltip()
2020
public Boolean applyMaxEntityCountGlobally = false;
21+
@ConfigEntry.Category(value = "general")
22+
@ConfigEntry.Gui.Tooltip()
23+
public Boolean useEntityCulling = false;
2124

2225
@ConfigEntry.Category(value = "entities")
2326
public TreeMap<String, Integer> entityMaxCounts = new TreeMap<>(Comparator.naturalOrder());
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package io.github.discusser.toomanyentities.mixin.client;
2+
3+
import com.llamalad7.mixinextras.sugar.Local;
4+
import io.github.discusser.toomanyentities.TooManyEntitiesClient;
5+
import io.github.discusser.toomanyentities.config.TooManyEntitiesConfig;
6+
import net.minecraft.client.render.WorldRenderer;
7+
import net.minecraft.entity.Entity;
8+
import org.spongepowered.asm.mixin.Mixin;
9+
import org.spongepowered.asm.mixin.injection.At;
10+
import org.spongepowered.asm.mixin.injection.Inject;
11+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
12+
13+
@Mixin(WorldRenderer.class)
14+
public class CommonWorldRendererMixin {
15+
@Inject(method = "renderEntity", at = @At(value = "HEAD"), cancellable = true)
16+
private void beforeEntityRender(CallbackInfo info, @Local(argsOnly = true) Entity entity) {
17+
String key = entity.getType().getTranslationKey();
18+
int maxEntityCount = TooManyEntitiesConfig.instance.applyMaxEntityCountGlobally
19+
? TooManyEntitiesConfig.instance.maxEntityCount
20+
: TooManyEntitiesConfig.instance.entityMaxCounts.get(key);
21+
if (maxEntityCount > 0 && TooManyEntitiesClient.entityCounts.getOrDefault(key, 0) > maxEntityCount) {
22+
info.cancel();
23+
}
24+
}
25+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package io.github.discusser.toomanyentities.mixin.client;
2+
3+
import io.github.discusser.toomanyentities.TooManyEntitiesClient;
4+
import net.minecraft.client.gui.hud.DebugHud;
5+
import org.spongepowered.asm.mixin.Mixin;
6+
import org.spongepowered.asm.mixin.injection.At;
7+
import org.spongepowered.asm.mixin.injection.Inject;
8+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
9+
10+
import java.util.List;
11+
12+
@Mixin(DebugHud.class)
13+
public class DebugHudMixin {
14+
@Inject(method = "getLeftText", at = @At(value = "TAIL"))
15+
public void getLeftText(CallbackInfoReturnable<List<String>> info) {
16+
TooManyEntitiesClient.entityCounts.clear();
17+
}
18+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package io.github.discusser.toomanyentities.mixin.client;
2+
3+
import com.google.common.collect.ImmutableMap;
4+
import io.github.discusser.toomanyentities.TooManyEntitiesClient;
5+
import net.fabricmc.loader.api.FabricLoader;
6+
import org.objectweb.asm.tree.ClassNode;
7+
import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin;
8+
import org.spongepowered.asm.mixin.extensibility.IMixinInfo;
9+
10+
import java.util.List;
11+
import java.util.Map;
12+
import java.util.Set;
13+
import java.util.function.Supplier;
14+
15+
public class EntityCullingMixinPlugin implements IMixinConfigPlugin {
16+
private static final Supplier<Boolean> entityCullingLoaded = () -> FabricLoader.getInstance().isModLoaded("entityculling");
17+
private static final Supplier<Boolean> entityCullingNotLoaded = () -> !FabricLoader.getInstance().isModLoaded("entityculling");
18+
19+
private static final Map<String, Supplier<Boolean>> CONDITIONS = ImmutableMap.of(
20+
"io.github.discusser.toomanyentities.mixin.client.DebugHudMixin", entityCullingLoaded,
21+
"io.github.discusser.toomanyentities.mixin.client.EntityCullingWorldRendererMixin", entityCullingLoaded,
22+
"io.github.discusser.toomanyentities.mixin.client.WorldRendererMixin", entityCullingNotLoaded
23+
);
24+
25+
@Override
26+
public void onLoad(String mixinPackage) {
27+
28+
}
29+
30+
@Override
31+
public String getRefMapperConfig() {
32+
return null;
33+
}
34+
35+
@Override
36+
public boolean shouldApplyMixin(String targetClassName, String mixinClassName) {
37+
return CONDITIONS.getOrDefault(mixinClassName, () -> true).get();
38+
}
39+
40+
@Override
41+
public void acceptTargets(Set<String> myTargets, Set<String> otherTargets) {
42+
43+
}
44+
45+
@Override
46+
public List<String> getMixins() {
47+
return null;
48+
}
49+
50+
@Override
51+
public void preApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo) {
52+
53+
}
54+
55+
@Override
56+
public void postApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo) {
57+
58+
}
59+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package io.github.discusser.toomanyentities.mixin.client;
2+
3+
import com.llamalad7.mixinextras.sugar.Local;
4+
import dev.tr7zw.entityculling.EntityCullingModBase;
5+
import io.github.discusser.toomanyentities.TooManyEntitiesClient;
6+
import io.github.discusser.toomanyentities.config.TooManyEntitiesConfig;
7+
import net.minecraft.client.render.WorldRenderer;
8+
import net.minecraft.entity.Entity;
9+
import org.spongepowered.asm.mixin.Mixin;
10+
import org.spongepowered.asm.mixin.Unique;
11+
import org.spongepowered.asm.mixin.injection.At;
12+
import org.spongepowered.asm.mixin.injection.Inject;
13+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
14+
15+
@Mixin(WorldRenderer.class)
16+
public class EntityCullingWorldRendererMixin {
17+
@Unique
18+
private int previousRenderedEntities = 0;
19+
20+
@Inject(method = "render", at = @At(value = "FIELD", target = "Lnet/minecraft/client/render/WorldRenderer;regularEntityCount:I", ordinal = 1, shift = At.Shift.AFTER))
21+
private void afterEntityCountIncrement(CallbackInfo info, @Local Entity entity) {
22+
String key = entity.getType().getTranslationKey();
23+
if (TooManyEntitiesConfig.instance.useEntityCulling) {
24+
int previous = previousRenderedEntities;
25+
int current = EntityCullingModBase.instance.renderedEntities;
26+
if (current - previous > 0) {
27+
TooManyEntitiesClient.entityCounts.put(key, TooManyEntitiesClient.entityCounts.getOrDefault(key, 0) + current - previous);
28+
}
29+
previousRenderedEntities = current;
30+
} else {
31+
TooManyEntitiesClient.entityCounts.put(key, TooManyEntitiesClient.entityCounts.getOrDefault(key, 0) + 1);
32+
}
33+
}
34+
35+
@Inject(method = "render", at = @At(value = "FIELD", target = "Lnet/minecraft/client/render/WorldRenderer;regularEntityCount:I", ordinal = 0))
36+
private void afterEntityCountReset(CallbackInfo info) {
37+
TooManyEntitiesClient.entityCounts.clear();
38+
}
39+
}
Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,19 @@
11
package io.github.discusser.toomanyentities.mixin.client;
22

33
import com.llamalad7.mixinextras.sugar.Local;
4-
import io.github.discusser.toomanyentities.config.TooManyEntitiesConfig;
4+
import io.github.discusser.toomanyentities.TooManyEntitiesClient;
55
import net.minecraft.client.render.WorldRenderer;
66
import net.minecraft.entity.Entity;
77
import org.spongepowered.asm.mixin.Mixin;
8-
import org.spongepowered.asm.mixin.Unique;
98
import org.spongepowered.asm.mixin.injection.At;
109
import org.spongepowered.asm.mixin.injection.Inject;
1110
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
1211

13-
import java.util.HashMap;
14-
1512
@Mixin(WorldRenderer.class)
1613
public class WorldRendererMixin {
17-
@Unique
18-
private final HashMap<String, Integer> entityCounts = new HashMap<>();
19-
20-
@Inject(method = "renderEntity", at = @At(value = "HEAD"), cancellable = true)
21-
private void beforeEntityRender(CallbackInfo info, @Local(argsOnly = true) Entity entity) {
22-
String key = entity.getType().getTranslationKey();
23-
int maxEntityCount = TooManyEntitiesConfig.instance.applyMaxEntityCountGlobally
24-
? TooManyEntitiesConfig.instance.maxEntityCount
25-
: TooManyEntitiesConfig.instance.entityMaxCounts.get(key);
26-
if (maxEntityCount > 0 && entityCounts.getOrDefault(key, 0) > maxEntityCount) {
27-
info.cancel();
28-
}
29-
}
30-
3114
@Inject(method = "render", at = @At(value = "FIELD", target = "Lnet/minecraft/client/render/WorldRenderer;regularEntityCount:I", ordinal = 1, shift = At.Shift.AFTER))
3215
private void afterEntityCountIncrement(CallbackInfo info, @Local Entity entity) {
3316
String key = entity.getType().getTranslationKey();
34-
entityCounts.put(key, entityCounts.getOrDefault(key, 0) + 1);
35-
}
36-
37-
38-
@Inject(method = "render", at = @At(value = "FIELD", target = "Lnet/minecraft/client/render/WorldRenderer;regularEntityCount:I", ordinal = 0))
39-
private void afterEntityCountReset(CallbackInfo info) {
40-
entityCounts.clear();
17+
TooManyEntitiesClient.entityCounts.put(key, TooManyEntitiesClient.entityCounts.getOrDefault(key, 0) + 1);
4118
}
4219
}

src/client/resources/too-many-entities.client.mixins.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
"required": true,
33
"package": "io.github.discusser.toomanyentities.mixin.client",
44
"compatibilityLevel": "JAVA_21",
5+
"plugin": "io.github.discusser.toomanyentities.mixin.client.EntityCullingMixinPlugin",
56
"client": [
7+
"CommonWorldRendererMixin",
8+
"DebugHudMixin",
9+
"EntityCullingWorldRendererMixin",
610
"WorldRendererMixin"
711
],
812
"injectors": {

src/main/resources/assets/too-many-entities/lang/en_us.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22
"text.autoconfig.too-many-entities.title": "Too Many Entities",
33
"text.autoconfig.too-many-entities.category.general": "General",
44
"text.autoconfig.too-many-entities.category.entities": "Entities",
5+
"text.autoconfig.too-many-entities.option.enabled": "Enabled",
6+
"text.autoconfig.too-many-entities.option.enabled.@Tooltip": "Whether or not the mod is enabled",
57
"text.autoconfig.too-many-entities.option.maxEntityCount": "Max Entity Count",
68
"text.autoconfig.too-many-entities.option.maxEntityCount.@Tooltip": "The maximum number of entities of a specific type to render. If this is set to 0, the limit is removed",
79
"text.autoconfig.too-many-entities.option.applyMaxEntityCountGlobally": "Apply Max Entity Count Globally",
8-
"text.autoconfig.too-many-entities.option.applyMaxEntityCountGlobally.@Tooltip": "If this option is enabled, the maximum entity count defined above will be used for every single entity type. If this option is disabled, each entity type will use its own maximum entity count"
10+
"text.autoconfig.too-many-entities.option.applyMaxEntityCountGlobally.@Tooltip": "If this option is enabled, the maximum entity count defined above will be used for every single entity type. If this option is disabled, each entity type will use its own maximum entity count",
11+
"text.autoconfig.too-many-entities.option.useEntityCulling": "Use Entity Culling",
12+
"text.autoconfig.too-many-entities.option.useEntityCulling.@Tooltip": "If this option is enabled, and the mod Entity Culling is installed, the number of entities rendered will be taken from the entity culling mod. This allows you to have more visible entities without necessarily reducing performance. There can be flickering when using this option, so only enable it if you want to."
913
}

0 commit comments

Comments
 (0)