Finish the XRay interface

This commit is contained in:
20kdc 2020-02-17 01:06:13 +00:00
parent 78e0bd57fa
commit 9c774ffc93
2 changed files with 98 additions and 4 deletions

View File

@ -0,0 +1,65 @@
package me.zeroeightsix.kami.command.commands;
import me.zeroeightsix.kami.command.Command;
import me.zeroeightsix.kami.command.syntax.ChunkBuilder;
import me.zeroeightsix.kami.module.ModuleManager;
import me.zeroeightsix.kami.module.modules.render.XRay;
import net.minecraft.block.Block;
/**
* Created by 20kdc on 17/02/2020.
*/
public class XRayCommand extends Command {
public XRayCommand() {
super("xray", new ChunkBuilder().append("subcommand").build());
setDescription("Has a set of sub-commands to control the XRay module.");
}
@Override
public void call(String[] args) {
XRay xr = (XRay) ModuleManager.getModuleByName("XRay");
if (xr == null) {
Command.sendChatMessage("&cThe XRay module is not available for some reason.");
return;
}
if (!xr.isEnabled()) {
Command.sendChatMessage("&cWarning: The XRay module is not enabled.");
Command.sendChatMessage("&cThese commands will still have effects, but will not visibly do anything.");
}
for (String s : args) {
if (s == null)
continue;
if (s.equalsIgnoreCase("help")) {
Command.sendChatMessage("The XRay module has a list of blocks.");
Command.sendChatMessage("Normally, the XRay module hides these blocks.");
Command.sendChatMessage("When the Invert setting is on, the XRay only shows these blocks.");
Command.sendChatMessage("This command is a convenient way to quickly edit the list.");
Command.sendChatMessage("XRay Subcommands:");
Command.sendChatMessage(".xray help : Shows this text.");
Command.sendChatMessage(".xray clear : Removes all blocks from the XRay block list.");
Command.sendChatMessage(".xray show : Shows the list.");
Command.sendChatMessage(".xray +<block> : Adds a block to the list.");
Command.sendChatMessage(".xray -<block> : Removes a block from the list.");
} else if (s.equalsIgnoreCase("clear")) {
xr.extClear();
Command.sendChatMessage("Cleared the XRay block list.");
} else if (s.equalsIgnoreCase("show")) {
Command.sendChatMessage(xr.extGet());
} else if (s.startsWith("+") || s.startsWith("-")) {
String name = s.substring(1);
Block b = Block.getBlockFromName(name);
if (b == null) {
Command.sendChatMessage("&cInvalid block name " + name + ".");
} else {
if (s.startsWith("+")) {
xr.extAdd(name);
} else {
xr.extRemove(name);
}
}
} else {
Command.sendChatMessage("&cInvalid subcommand " + s + ".");
}
}
}
}

View File

@ -34,21 +34,38 @@ import me.zeroeightsix.kami.KamiMod;
public class XRay extends Module {
// A default reasonable configuration for the XRay. Most people will want to use it like this.
public static final String DEFAULT_XRAY_CONFIG = "minecraft:grass,minecraft:dirt,minecraft:netherrack,minecraft:gravel,minecraft:sand,minecraft:stone";
private static final String DEFAULT_XRAY_CONFIG = "minecraft:grass,minecraft:dirt,minecraft:netherrack,minecraft:gravel,minecraft:sand,minecraft:stone";
// Split by ',' & each element trimmed (this is a bit weird but it works for now?)
private Setting<String> hiddenBlockNames = register(Settings.stringBuilder("HiddenBlocks").withValue(DEFAULT_XRAY_CONFIG).withConsumer((old, value) -> {
refreshHiddenBlocksSet(value);
if (isEnabled())
mc.renderGlobal.loadRenderers();
}));
private Setting<Boolean> invert = register(Settings.booleanBuilder("Invert").withValue(false).withConsumer((old, value) -> {
invertStatic = value;
if (isEnabled())
mc.renderGlobal.loadRenderers();
}));
private Setting<Boolean> outlines = register(Settings.booleanBuilder("Outlines").withValue(true).withConsumer((old, value) -> {
outlinesStatic = value;
if (isEnabled())
mc.renderGlobal.loadRenderers();
}));
// A static mirror of the state.
private static Set<Block> hiddenBlocks = Collections.synchronizedSet(new HashSet<>());
private static boolean invertStatic, outlinesStatic = true;
// This is the state used for hidden blocks.
private static IBlockState transparentState;
// This is used as part of a mechanism to make the Minecraft renderer play along with the XRay.
// Essentially, the XRay primitive is just a block state transformer.
// Then this implements a custom block that the block state transformer can use for hidden blocks.
public static Block transparentBlock;
public XRay() {
invertStatic = invert.getValue();
outlinesStatic = outlines.getValue();
refreshHiddenBlocksSet(hiddenBlockNames.getValue());
}
@ -62,7 +79,11 @@ public class XRay extends Module {
}
// Remove entry by arbitrary user-provided string
public void extRemove(String s) {
hiddenBlockNames.setValue(extGetInternal(Block.getBlockFromName(s)) + ", " + s);
hiddenBlockNames.setValue(extGetInternal(Block.getBlockFromName(s)));
}
// Clears the list.
public void extClear() {
hiddenBlockNames.setValue("");
}
private String extGetInternal(Block filter) {
@ -126,8 +147,16 @@ public class XRay extends Module {
}
public static IBlockState transform(IBlockState input) {
if (hiddenBlocks.contains(input.getBlock()))
return transparentState != null ? transparentState : Blocks.AIR.getDefaultState();
Block b = input.getBlock();
boolean hide = hiddenBlocks.contains(b);
if (invertStatic)
hide = !hide;
if (hide) {
IBlockState target = Blocks.AIR.getDefaultState();
if (outlinesStatic && (transparentState != null))
target = transparentState;
return target;
}
return input;
}