Skip to content

Commit

Permalink
Add NBT filtering to /crender
Browse files Browse the repository at this point in the history
  • Loading branch information
Earthcomputer committed Apr 8, 2019
1 parent 741a835 commit c1e3766
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
import net.minecraft.command.WrongUsageException;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityList;
import net.minecraft.nbt.JsonToNBT;
import net.minecraft.nbt.NBTException;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
Expand Down Expand Up @@ -57,33 +60,37 @@ public void execute(MinecraftServer server, ICommandSender sender, String[] args

private void toggleEntities(ICommandSender sender, String[] args, boolean enable) throws CommandException {
Set<ResourceLocation> types;
NBTTagCompound nbt = new NBTTagCompound();
if (args.length < 3) {
types = EntityList.getEntityNameList();
} else {
types = new HashSet<>();
for (int i = 2; i < args.length; i++) {
int i;
for (i = 2; i < args.length; i++) {
if (args[i].startsWith("{"))
break;
ResourceLocation type = new ResourceLocation(args[i]);
if (!EntityList.isRegistered(type)) {
throw new CommandException("commands.crender.entities.unknown", type);
}
types.add(type);
}
if (i != args.length) {
try {
nbt = JsonToNBT.getTagFromJson(buildString(args, i));
} catch (NBTException e) {
throw new CommandException("commands.scoreboard.players.set.tagError", e.getMessage());
}
}
}
int count = 0;
for (ResourceLocation type : types) {
Class<? extends Entity> clazz = EntityList.getClass(type);
if (RenderSettings.isEntityRenderingDisabled(clazz) == enable) {
count++;
if (enable)
RenderSettings.enableEntityRendering(clazz);
else
RenderSettings.disableEntityRendering(clazz);
}
RenderSettings.addRenderingFilter(clazz, nbt, enable);
}
if (enable)
sender.sendMessage(new TextComponentTranslation("commands.crender.entities.enable.success", count));
sender.sendMessage(new TextComponentTranslation("commands.crender.entities.enable.success", types.size()));
else
sender.sendMessage(new TextComponentTranslation("commands.crender.entities.disable.success", count));
sender.sendMessage(new TextComponentTranslation("commands.crender.entities.disable.success", types.size()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,17 @@ private static void transformShouldRender(MethodNode method) {
/*
* Add:
*
* if (RenderSettings.isEntityRenderingDisabled(entityIn.getClass()))
* if (!RenderSettings.shouldRender(entityIn))
* return false;
*/
// @formatter:on

InsnList insns = new InsnList();
insns.add(new VarInsnNode(Opcodes.ALOAD, 1));
insns.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;",
false));
insns.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "net/earthcomputer/clientcommands/render/RenderSettings",
"isEntityRenderingDisabled", "(Ljava/lang/Class;)Z", false));
"shouldRender", "(Lnet/minecraft/entity/Entity;)Z", false));
LabelNode label = new LabelNode();
insns.add(new JumpInsnNode(Opcodes.IFEQ, label));
insns.add(new JumpInsnNode(Opcodes.IFNE, label));
insns.add(new InsnNode(Opcodes.ICONST_0));
insns.add(new InsnNode(Opcodes.IRETURN));
insns.add(label);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,52 @@
package net.earthcomputer.clientcommands.render;

import java.util.Set;
import java.util.*;

import com.google.common.collect.Sets;
import com.google.common.collect.Maps;

import net.earthcomputer.clientcommands.EventManager;
import net.minecraft.command.CommandBase;
import net.minecraft.entity.Entity;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTUtil;
import org.apache.commons.lang3.tuple.Pair;

public class RenderSettings {

public static void registerEvents() {
EventManager.addDisconnectExceptRelogListener(e -> {
entitiesDisabled.clear();
filters.clear();
});
}

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

public static boolean isEntityRenderingDisabled(Class<? extends Entity> clazz) {
return entitiesDisabled.contains(clazz);
}
public static boolean shouldRender(Entity entity) {
List<Pair<NBTTagCompound, Boolean>> filters = RenderSettings.filters.get(entity.getClass());
if (filters == null)
return true;

NBTTagCompound nbt = CommandBase.entityToNBT(entity);
boolean shouldRender = true;

public static void enableEntityRendering(Class<? extends Entity> clazz) {
entitiesDisabled.remove(clazz);
for (Pair<NBTTagCompound, Boolean> filter : filters) {
if (NBTUtil.areNBTEquals(filter.getLeft(), nbt, true)) {
shouldRender = filter.getRight();
}
}

return shouldRender;
}

public static void disableEntityRendering(Class<? extends Entity> clazz) {
entitiesDisabled.add(clazz);
public static void addRenderingFilter(Class<? extends Entity> clazz, NBTTagCompound filter, boolean shouldRender) {
if (filter.hasNoTags() && shouldRender) {
RenderSettings.filters.remove(clazz);
return;
}

List<Pair<NBTTagCompound, Boolean>> filters = RenderSettings.filters.computeIfAbsent(clazz, k -> new ArrayList<>());
filters.removeIf(existingFilter -> NBTUtil.areNBTEquals(filter, existingFilter.getLeft(), true));
filters.add(Pair.of(filter, shouldRender));
}

}
2 changes: 1 addition & 1 deletion src/main/resources/assets/clientcommands/lang/en_us.lang
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ commands.cnote.usage=/cnote <message>

commands.crelog.usage=/crelog

commands.crender.usage=/crender <enable|disable> <entities> [type...]
commands.crender.usage=/crender <enable|disable> <entities> [type...] [nbt]
commands.crender.entities.unknown=Unknown entity %s
commands.crender.entities.enable.success=Enabled rendering for %s entities
commands.crender.entities.disable.success=Disabled rendering for %s entities
Expand Down

0 comments on commit c1e3766

Please sign in to comment.