Skip to content

Commit c1e3766

Browse files
committed
Add NBT filtering to /crender
1 parent 741a835 commit c1e3766

File tree

4 files changed

+53
-28
lines changed

4 files changed

+53
-28
lines changed

src/main/java/net/earthcomputer/clientcommands/command/CommandRender.java

+18-11
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
import net.minecraft.command.WrongUsageException;
1212
import net.minecraft.entity.Entity;
1313
import net.minecraft.entity.EntityList;
14+
import net.minecraft.nbt.JsonToNBT;
15+
import net.minecraft.nbt.NBTException;
16+
import net.minecraft.nbt.NBTTagCompound;
1417
import net.minecraft.server.MinecraftServer;
1518
import net.minecraft.util.ResourceLocation;
1619
import net.minecraft.util.math.BlockPos;
@@ -57,33 +60,37 @@ public void execute(MinecraftServer server, ICommandSender sender, String[] args
5760

5861
private void toggleEntities(ICommandSender sender, String[] args, boolean enable) throws CommandException {
5962
Set<ResourceLocation> types;
63+
NBTTagCompound nbt = new NBTTagCompound();
6064
if (args.length < 3) {
6165
types = EntityList.getEntityNameList();
6266
} else {
6367
types = new HashSet<>();
64-
for (int i = 2; i < args.length; i++) {
68+
int i;
69+
for (i = 2; i < args.length; i++) {
70+
if (args[i].startsWith("{"))
71+
break;
6572
ResourceLocation type = new ResourceLocation(args[i]);
6673
if (!EntityList.isRegistered(type)) {
6774
throw new CommandException("commands.crender.entities.unknown", type);
6875
}
6976
types.add(type);
7077
}
78+
if (i != args.length) {
79+
try {
80+
nbt = JsonToNBT.getTagFromJson(buildString(args, i));
81+
} catch (NBTException e) {
82+
throw new CommandException("commands.scoreboard.players.set.tagError", e.getMessage());
83+
}
84+
}
7185
}
72-
int count = 0;
7386
for (ResourceLocation type : types) {
7487
Class<? extends Entity> clazz = EntityList.getClass(type);
75-
if (RenderSettings.isEntityRenderingDisabled(clazz) == enable) {
76-
count++;
77-
if (enable)
78-
RenderSettings.enableEntityRendering(clazz);
79-
else
80-
RenderSettings.disableEntityRendering(clazz);
81-
}
88+
RenderSettings.addRenderingFilter(clazz, nbt, enable);
8289
}
8390
if (enable)
84-
sender.sendMessage(new TextComponentTranslation("commands.crender.entities.enable.success", count));
91+
sender.sendMessage(new TextComponentTranslation("commands.crender.entities.enable.success", types.size()));
8592
else
86-
sender.sendMessage(new TextComponentTranslation("commands.crender.entities.disable.success", count));
93+
sender.sendMessage(new TextComponentTranslation("commands.crender.entities.disable.success", types.size()));
8794
}
8895

8996
@Override

src/main/java/net/earthcomputer/clientcommands/core/RenderManagerTransformer.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,17 @@ private static void transformShouldRender(MethodNode method) {
6262
/*
6363
* Add:
6464
*
65-
* if (RenderSettings.isEntityRenderingDisabled(entityIn.getClass()))
65+
* if (!RenderSettings.shouldRender(entityIn))
6666
* return false;
6767
*/
6868
// @formatter:on
6969

7070
InsnList insns = new InsnList();
7171
insns.add(new VarInsnNode(Opcodes.ALOAD, 1));
72-
insns.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;",
73-
false));
7472
insns.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "net/earthcomputer/clientcommands/render/RenderSettings",
75-
"isEntityRenderingDisabled", "(Ljava/lang/Class;)Z", false));
73+
"shouldRender", "(Lnet/minecraft/entity/Entity;)Z", false));
7674
LabelNode label = new LabelNode();
77-
insns.add(new JumpInsnNode(Opcodes.IFEQ, label));
75+
insns.add(new JumpInsnNode(Opcodes.IFNE, label));
7876
insns.add(new InsnNode(Opcodes.ICONST_0));
7977
insns.add(new InsnNode(Opcodes.IRETURN));
8078
insns.add(label);
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,52 @@
11
package net.earthcomputer.clientcommands.render;
22

3-
import java.util.Set;
3+
import java.util.*;
44

5-
import com.google.common.collect.Sets;
5+
import com.google.common.collect.Maps;
66

77
import net.earthcomputer.clientcommands.EventManager;
8+
import net.minecraft.command.CommandBase;
89
import net.minecraft.entity.Entity;
10+
import net.minecraft.nbt.NBTTagCompound;
11+
import net.minecraft.nbt.NBTUtil;
12+
import org.apache.commons.lang3.tuple.Pair;
913

1014
public class RenderSettings {
1115

1216
public static void registerEvents() {
1317
EventManager.addDisconnectExceptRelogListener(e -> {
14-
entitiesDisabled.clear();
18+
filters.clear();
1519
});
1620
}
1721

18-
private static Set<Class<? extends Entity>> entitiesDisabled = Sets.newIdentityHashSet();
22+
private static Map<Class<? extends Entity>, List<Pair<NBTTagCompound, Boolean>>> filters = Maps.newHashMap();
1923

20-
public static boolean isEntityRenderingDisabled(Class<? extends Entity> clazz) {
21-
return entitiesDisabled.contains(clazz);
22-
}
24+
public static boolean shouldRender(Entity entity) {
25+
List<Pair<NBTTagCompound, Boolean>> filters = RenderSettings.filters.get(entity.getClass());
26+
if (filters == null)
27+
return true;
28+
29+
NBTTagCompound nbt = CommandBase.entityToNBT(entity);
30+
boolean shouldRender = true;
2331

24-
public static void enableEntityRendering(Class<? extends Entity> clazz) {
25-
entitiesDisabled.remove(clazz);
32+
for (Pair<NBTTagCompound, Boolean> filter : filters) {
33+
if (NBTUtil.areNBTEquals(filter.getLeft(), nbt, true)) {
34+
shouldRender = filter.getRight();
35+
}
36+
}
37+
38+
return shouldRender;
2639
}
2740

28-
public static void disableEntityRendering(Class<? extends Entity> clazz) {
29-
entitiesDisabled.add(clazz);
41+
public static void addRenderingFilter(Class<? extends Entity> clazz, NBTTagCompound filter, boolean shouldRender) {
42+
if (filter.hasNoTags() && shouldRender) {
43+
RenderSettings.filters.remove(clazz);
44+
return;
45+
}
46+
47+
List<Pair<NBTTagCompound, Boolean>> filters = RenderSettings.filters.computeIfAbsent(clazz, k -> new ArrayList<>());
48+
filters.removeIf(existingFilter -> NBTUtil.areNBTEquals(filter, existingFilter.getLeft(), true));
49+
filters.add(Pair.of(filter, shouldRender));
3050
}
3151

3252
}

src/main/resources/assets/clientcommands/lang/en_us.lang

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ commands.cnote.usage=/cnote <message>
152152

153153
commands.crelog.usage=/crelog
154154

155-
commands.crender.usage=/crender <enable|disable> <entities> [type...]
155+
commands.crender.usage=/crender <enable|disable> <entities> [type...] [nbt]
156156
commands.crender.entities.unknown=Unknown entity %s
157157
commands.crender.entities.enable.success=Enabled rendering for %s entities
158158
commands.crender.entities.disable.success=Disabled rendering for %s entities

0 commit comments

Comments
 (0)