This commit is contained in:
Christoph J. Scherr 2023-11-12 16:59:25 +01:00
parent ecb2cdcd85
commit e5ec40b8c2
7 changed files with 132 additions and 55 deletions

View File

@ -1,41 +1,21 @@
package de.cscherr.mcpht; package de.cscherr.mcpht;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper; import de.cscherr.mcpht.util.Hack;
import net.minecraft.block.Block; import de.cscherr.mcpht.util.MaybeKey;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.client.option.KeyBinding; import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
import org.lwjgl.glfw.GLFW;
import java.util.ArrayList; public class Fly extends Hack {
import java.util.List; public Fly() {
super(new MaybeKey());
public class Fly {
public boolean enable = true;
private static KeyBinding toggleKey;
public void init() {
toggleKey = KeyBindingHelper.registerKeyBinding(new KeyBinding(
"key.mcpht.toggleXray", // Translation of name
InputUtil.Type.KEYSYM, // Type: MOUSE or KESYM (Keyboard)
GLFW.GLFW_KEY_Y, // The keycode of the key (default?)
"category.mcpht.rendering" // Translation key for the keybinding category
));
ClientTickEvents.END_CLIENT_TICK.register(client -> {
// NOTE: IDK why we need a while here
while (toggleKey.wasPressed()) {
if (MCPHTClient.XRAY.getEnable()) {
MCPHTClient.LOGGER.info(String.format("toggle XRay: %s", MCPHTClient.XRAY.enable));
MCPHTClient.XRAY.enable = !MCPHTClient.XRAY.enable;
MCPHTClient.MC.worldRenderer.reload();
}
}
});
} }
@Override
public void onKeyPress() {
}
@Override
public boolean getEnable() { public boolean getEnable() {
return enable && MCPHTClient.CONFIG.fly.enable; return enable && MCPHTClient.CONFIG.fly.enable;
} }

View File

@ -1,19 +1,17 @@
package de.cscherr.mcpht; package de.cscherr.mcpht;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; import de.cscherr.mcpht.util.Hack;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper; import de.cscherr.mcpht.util.MaybeKey;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks; import net.minecraft.block.Blocks;
import net.minecraft.client.option.KeyBinding; import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
import org.lwjgl.glfw.GLFW; import org.lwjgl.glfw.GLFW;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class XRay { public class XRay extends Hack {
public boolean enable = true;
public ArrayList<Block> whitelistBlocks = new ArrayList<>( public ArrayList<Block> whitelistBlocks = new ArrayList<>(
List.of( List.of(
// Ores // Ores
@ -80,31 +78,24 @@ public class XRay {
Blocks.RED_SHULKER_BOX Blocks.RED_SHULKER_BOX
) )
); );
private static KeyBinding toggleKey;
public void init() { public XRay() {
toggleKey = KeyBindingHelper.registerKeyBinding(new KeyBinding( super(new MaybeKey("key.mcpht.toggleXray",
"key.mcpht.toggleXray", // Translation of name "category.mcpht.rendering",
InputUtil.Type.KEYSYM, // Type: MOUSE or KESYM (Keyboard) GLFW.GLFW_KEY_X));
GLFW.GLFW_KEY_X, // The keycode of the key (default?) }
"category.mcpht.rendering" // Translation key for the keybinding category
)); @Override
ClientTickEvents.END_CLIENT_TICK.register(client -> { public void onKeyPress() {
// NOTE: IDK why we need a while here MCPHTClient.XRAY.enable = !MCPHTClient.XRAY.enable;
while (toggleKey.wasPressed()) { MCPHTClient.MC.worldRenderer.reload();
if (MCPHTClient.XRAY.getEnable()) {
MCPHTClient.LOGGER.info(String.format("toggle XRay: %s", MCPHTClient.XRAY.enable));
MCPHTClient.XRAY.enable = !MCPHTClient.XRAY.enable;
MCPHTClient.MC.worldRenderer.reload();
}
}
});
} }
public boolean showRenderBlock(BlockState state) { public boolean showRenderBlock(BlockState state) {
return whitelistBlocks.contains(state.getBlock()); return whitelistBlocks.contains(state.getBlock());
} }
@Override
public boolean getEnable() { public boolean getEnable() {
return enable && MCPHTClient.CONFIG.xRay.enable; return enable && MCPHTClient.CONFIG.xRay.enable;
} }

View File

@ -0,0 +1,39 @@
package de.cscherr.mcpht.util;
import de.cscherr.mcpht.MCPHTClient;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
import org.lwjgl.glfw.GLFW;
import java.util.Optional;
public abstract class Hack {
public boolean enable = false;
public MaybeKey toggleKey;
public Hack(MaybeKey toggleKey) {
toggleKey = toggleKey;
}
public void init() {
if (toggleKey.hasKey) {
ClientTickEvents.END_CLIENT_TICK.register(client -> {
// NOTE: IDK why we need a while here
while (toggleKey.key.wasPressed()) {
if (this.getEnable()) {
MCPHTClient.LOGGER.info(String.format("toggle %s: %s",
this.getClass().getSimpleName(),
this.getEnable()));
onKeyPress();
}
}
});
}
}
public abstract void onKeyPress();
public abstract boolean getEnable();
}

View File

@ -0,0 +1,27 @@
package de.cscherr.mcpht.util;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
public class MaybeKey {
public KeyBinding key;
public String translationKey;
public String translationCategory;
public int keyCode;
public boolean hasKey;
public MaybeKey() {
hasKey = false;
}
public MaybeKey(String translationKey, String translationCategory, int keyCode) {
key = KeyBindingHelper.registerKeyBinding(new KeyBinding(
translationKey, // Translation of name
InputUtil.Type.KEYSYM, // Type: MOUSE or KESYM (Keyboard)
keyCode, // The keycode of the key (default?)
translationCategory // Translation key for the keybinding category
));
hasKey = true;
}
}

View File

@ -0,0 +1,9 @@
package de.cscherr.mcpht.util.event;
import java.util.ArrayList;
public abstract class Event<T extends Listener> {
public abstract void fire(ArrayList<T> listeners);
public abstract Class<T> getListenerType();
}

View File

@ -0,0 +1,6 @@
package de.cscherr.mcpht.util.event;
import java.util.EventListener;
public interface Listener extends EventListener {
}

View File

@ -0,0 +1,25 @@
package de.cscherr.mcpht.util.event;
import java.util.ArrayList;
import java.util.EventListener;
public interface UpdateListener extends Listener {
public void onUpdate();
public static class UpdateEvent extends Event<UpdateListener>
{
public static final UpdateEvent INSTANCE = new UpdateEvent();
@Override
public void fire(ArrayList<UpdateListener> listeners)
{
for(UpdateListener listener : listeners)
listener.onUpdate();
}
@Override
public Class<UpdateListener> getListenerType()
{
return UpdateListener.class;
}
}
}