-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
934 additions
and
0 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
src/main/java/com/github/hf02/scrollForWorldEdit/ScrollForWorldEdit.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.github.hf02.scrollForWorldEdit; | ||
|
||
import net.fabricmc.api.ModInitializer; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class ScrollForWorldEdit implements ModInitializer { | ||
|
||
// This logger is used to write text to the console and the log file. | ||
// It is considered best practice to use your mod id as the logger's name. | ||
// That way, it's clear which mod wrote info, warnings, and errors. | ||
public static final Logger LOGGER = LoggerFactory.getLogger( | ||
"scroll-for-worldedit" | ||
); | ||
|
||
@Override | ||
public void onInitialize() { | ||
// This code runs as soon as Minecraft is in a mod-load-ready state. | ||
// However, some things (like resources) may still be uninitialized. | ||
// Proceed with mild caution. | ||
|
||
LOGGER.info("Scroll for WorldEdit :)"); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/com/github/hf02/scrollForWorldEdit/client/Key.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.github.hf02.scrollForWorldEdit.client; | ||
|
||
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper; | ||
import net.minecraft.client.option.KeyBinding; | ||
|
||
public class Key { | ||
|
||
public final String code; | ||
public final String name; | ||
public final String command; | ||
public final KeyBinding keybinding; | ||
public final KeyManager keyManager; | ||
private final ScrollForWorldEditClient scrollClient; | ||
public final KeyHandler handler; | ||
|
||
public Key( | ||
KeyManager keyManager, | ||
String code, | ||
String command, | ||
String name, | ||
String keybindingName, | ||
net.minecraft.client.util.InputUtil.Type type, | ||
int defaultCode, | ||
String category, | ||
KeyHandler handler | ||
) { | ||
this.keyManager = keyManager; | ||
this.code = code; | ||
this.command = command; | ||
this.name = name; | ||
this.scrollClient = keyManager.scrollClient; | ||
this.handler = handler; | ||
this.keybinding = | ||
KeyBindingHelper.registerKeyBinding( | ||
new KeyBinding(keybindingName, type, defaultCode, category) | ||
); | ||
} | ||
|
||
public void run(TakeScroll scroll) { | ||
boolean isActive = keyManager.isKeyActive(this.code); | ||
if (isActive) { | ||
scrollClient.mouseHandler.capturingScroll = true; | ||
this.handler.callback(this, scroll); | ||
} | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/java/com/github/hf02/scrollForWorldEdit/client/KeyHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.github.hf02.scrollForWorldEdit.client; | ||
|
||
public interface KeyHandler { | ||
void callback(Key key, TakeScroll scroll); | ||
} |
257 changes: 257 additions & 0 deletions
257
src/main/java/com/github/hf02/scrollForWorldEdit/client/KeyManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,257 @@ | ||
package com.github.hf02.scrollForWorldEdit.client; | ||
|
||
import com.github.hf02.scrollForWorldEdit.ScrollForWorldEdit; | ||
import java.util.Optional; | ||
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; | ||
|
||
public class KeyManager { | ||
|
||
private Key[] keys; | ||
|
||
public final int count; | ||
|
||
public final KeyBinding modeKey = KeyBindingHelper.registerKeyBinding( | ||
new KeyBinding( | ||
"key.scroll-for-worldedit.mode", | ||
InputUtil.Type.KEYSYM, | ||
GLFW.GLFW_KEY_LEFT_CONTROL, | ||
"key.scroll-for-worldedit.main" | ||
) | ||
); | ||
private boolean modeKeyToggle = false; | ||
|
||
public final KeyBinding useKey = KeyBindingHelper.registerKeyBinding( | ||
new KeyBinding( | ||
"key.scroll-for-worldedit.use", | ||
InputUtil.Type.KEYSYM, | ||
GLFW.GLFW_KEY_LEFT_ALT, | ||
"key.scroll-for-worldedit.main" | ||
) | ||
); | ||
private boolean useKeyToggle = false; | ||
|
||
public final KeyBinding modifierKey = KeyBindingHelper.registerKeyBinding( | ||
new KeyBinding( | ||
"key.scroll-for-worldedit.modifier", | ||
InputUtil.Type.KEYSYM, | ||
GLFW.GLFW_KEY_Z, | ||
"key.scroll-for-worldedit.main" | ||
) | ||
); | ||
|
||
private Optional<Key> getKey(String code) { | ||
for (Key key : keys) { | ||
if (key.code == code) { | ||
return Optional.of(key); | ||
} | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
public boolean isUseKeyActive() { | ||
return ScrollForWorldEditClient.config.useKeyToggles | ||
? useKeyToggle | ||
: useKey.isPressed(); | ||
} | ||
|
||
public boolean isModeKeyActive() { | ||
boolean modeKeyAllowed = ScrollForWorldEditClient.config.mustHoldUseKeyForModeKey | ||
? isUseKeyActive() | ||
: true; | ||
boolean modeKeyPressed = ScrollForWorldEditClient.config.modeKeyToggles | ||
? modeKeyToggle | ||
: modeKey.isPressed(); | ||
return modeKeyAllowed && modeKeyPressed; | ||
} | ||
|
||
private int activeKeyIndex = 0; | ||
private Key activeKey; | ||
|
||
public void setActiveKey(int index) { | ||
activeKeyIndex = index % count; | ||
if (activeKeyIndex < 0) { | ||
activeKeyIndex = count + activeKeyIndex; | ||
} | ||
activeKey = keys[activeKeyIndex]; | ||
} | ||
|
||
public Key getActiveKey() { | ||
return activeKey; | ||
} | ||
|
||
public int getActiveKeyIndex() { | ||
return activeKeyIndex; | ||
} | ||
|
||
public final ScrollForWorldEditClient scrollClient; | ||
|
||
public KeyManager(ScrollForWorldEditClient scrollForWorldEditClient) { | ||
this.scrollClient = scrollForWorldEditClient; | ||
|
||
ClientTickEvents.END_CLIENT_TICK.register(client -> { | ||
while (useKey.wasPressed()) { | ||
useKeyToggle = | ||
ScrollForWorldEditClient.config.useKeyToggles | ||
? !useKeyToggle | ||
: false; | ||
} | ||
while (modeKey.wasPressed()) { | ||
modeKeyToggle = | ||
ScrollForWorldEditClient.config.modeKeyToggles | ||
? !modeKeyToggle | ||
: false; | ||
} | ||
}); | ||
|
||
keys = | ||
new Key[] { | ||
new Key( | ||
this, | ||
"move", | ||
"/move %s %s -s", | ||
"scroll-for-worldedit.mode.move", | ||
"key.scroll-for-worldedit.move", | ||
InputUtil.Type.KEYSYM, | ||
GLFW.GLFW_KEY_UNKNOWN, | ||
"key.scroll-for-worldedit.main", | ||
this::runNonNegativeMove | ||
), | ||
new Key( | ||
this, | ||
"expand", | ||
"/expand %s %s", | ||
"scroll-for-worldedit.mode.expand", | ||
"key.scroll-for-worldedit.expand", | ||
InputUtil.Type.KEYSYM, | ||
GLFW.GLFW_KEY_UNKNOWN, | ||
"key.scroll-for-worldedit.main", | ||
this::runMove | ||
), | ||
new Key( | ||
this, | ||
"contract", | ||
"/contract %s %s", | ||
"scroll-for-worldedit.mode.contract", | ||
"key.scroll-for-worldedit.contract", | ||
InputUtil.Type.KEYSYM, | ||
GLFW.GLFW_KEY_UNKNOWN, | ||
"key.scroll-for-worldedit.main", | ||
this::runMove | ||
), | ||
new Key( | ||
this, | ||
"shift", | ||
"/shift %s %s", | ||
"scroll-for-worldedit.mode.shift", | ||
"key.scroll-for-worldedit.shift", | ||
InputUtil.Type.KEYSYM, | ||
GLFW.GLFW_KEY_UNKNOWN, | ||
"key.scroll-for-worldedit.main", | ||
this::runMove | ||
), | ||
new Key( | ||
this, | ||
"stack", | ||
"/stack %s %s -s", | ||
"scroll-for-worldedit.mode.stack", | ||
"key.scroll-for-worldedit.stack", | ||
InputUtil.Type.KEYSYM, | ||
GLFW.GLFW_KEY_UNKNOWN, | ||
"key.scroll-for-worldedit.main", | ||
this::runNonNegativeMove | ||
), | ||
}; | ||
count = keys.length; | ||
setActiveKey(0); | ||
} | ||
|
||
public boolean isKeyActive(String keyName) { | ||
Optional<Key> key = getKey(keyName); | ||
if (!key.isPresent()) { | ||
ScrollForWorldEdit.LOGGER.warn( | ||
String.format("isKeyActive(): Unknown key %s", keyName) | ||
); | ||
return false; | ||
} | ||
if (isUseKeyActive() && activeKey == key.get()) { | ||
return true; | ||
} | ||
return key.get().keybinding.isPressed(); | ||
} | ||
|
||
public void processKeys(TakeScroll scroll) { | ||
for (Key key : keys) { | ||
key.run(scroll); | ||
} | ||
} | ||
|
||
private void sendCommand(String command) { | ||
scrollClient.client.player.networkHandler.sendChatCommand(command); | ||
} | ||
|
||
// methods to handle Keys | ||
|
||
private void runNonNegativeMove(Key key, TakeScroll scroll) { | ||
if (scroll.scrollY > 0) { | ||
sendCommand( | ||
String.format( | ||
key.command, | ||
scroll.scrollY, | ||
scrollClient.primaryTextDirection() | ||
) | ||
); | ||
} else if (scroll.scrollY < 0) { | ||
sendCommand( | ||
String.format( | ||
key.command, | ||
-scroll.scrollY, | ||
scrollClient.primaryTextDirection(true) | ||
) | ||
); | ||
} | ||
|
||
if (scroll.scrollX > 0) { | ||
sendCommand( | ||
String.format( | ||
key.command, | ||
scroll.scrollX, | ||
scrollClient.secondaryTextDirection() | ||
) | ||
); | ||
} else if (scroll.scrollX < 0) { | ||
sendCommand( | ||
String.format( | ||
key.command, | ||
-scroll.scrollX, | ||
scrollClient.secondaryTextDirection(true) | ||
) | ||
); | ||
} | ||
} | ||
|
||
private void runMove(Key key, TakeScroll scroll) { | ||
if (scroll.scrollY != 0) { | ||
sendCommand( | ||
String.format( | ||
key.command, | ||
scroll.scrollY, | ||
scrollClient.primaryTextDirection() | ||
) | ||
); | ||
} | ||
|
||
if (scroll.scrollX != 0) { | ||
sendCommand( | ||
String.format( | ||
key.command, | ||
scroll.scrollX, | ||
scrollClient.secondaryTextDirection() | ||
) | ||
); | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/com/github/hf02/scrollForWorldEdit/client/MouseScrollCallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.github.hf02.scrollForWorldEdit.client; | ||
|
||
import net.fabricmc.fabric.api.event.Event; | ||
import net.fabricmc.fabric.api.event.EventFactory; | ||
import net.minecraft.util.ActionResult; | ||
|
||
/** | ||
* callback for the mouse scrolling. | ||
* returning SUCCESS will cancel minecraft's handling of the mouse | ||
* wheel. | ||
*/ | ||
public interface MouseScrollCallback { | ||
Event<MouseScrollCallback> EVENT = EventFactory.createArrayBacked( | ||
MouseScrollCallback.class, | ||
listeners -> | ||
(horizontal, vertical) -> { | ||
for (MouseScrollCallback listener : listeners) { | ||
ActionResult result = listener.interact( | ||
horizontal, | ||
vertical | ||
); | ||
|
||
if (result != ActionResult.PASS) { | ||
return result; | ||
} | ||
} | ||
return ActionResult.PASS; | ||
} | ||
); | ||
|
||
ActionResult interact(double horizontal, double vertical); | ||
} |
Oops, something went wrong.