-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed search field in controls loosing the text when window is resized #44
Open
Klotzi111
wants to merge
13
commits into
Siphalor:1.17
Choose a base branch
from
Klotzi111:1.17
base: 1.17
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
37d87c4
Fixed search field in controls loosing the text when window is resized
5632f94
Improved code structure and readability
a497a0b
Added german (de_de) language
631c4a9
Base keybinding now always gets shown when at least one alternative of
dc983f6
Added keybindings for hotbar scroll to options gui (closes #35)
5706876
Added keybinding for 'drop stack' to options gui (closes #43)
3b15255
Moved keybindings for extra functionality into amecs
69bb021
Added changes corresponding to the change for hotbar scroll in amecs api
9bb7670
Update Amecs API to officially released version
Siphalor b967720
Fixed vanilla drop item logic for mc 1.14
7642056
Merge remote-tracking branch 'origin/1.17' into 1.17
54c5bbe
Calling mc 1.14 drop method via reflection
45e281d
searching drop method with deobfuscated and unified name
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Moved keybindings for extra functionality into amecs
- Loading branch information
There are no files selected for viewing
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
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
132 changes: 132 additions & 0 deletions
132
src/main/java/de/siphalor/amecs/VersionedLogicMethodHelper.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,132 @@ | ||
package de.siphalor.amecs; | ||
|
||
import java.lang.reflect.Field; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Modifier; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map.Entry; | ||
import java.util.TreeMap; | ||
|
||
import org.apache.logging.log4j.Level; | ||
|
||
import net.fabricmc.api.EnvType; | ||
import net.fabricmc.api.Environment; | ||
import net.fabricmc.loader.api.SemanticVersion; | ||
import net.fabricmc.loader.api.VersionParsingException; | ||
|
||
@Environment(EnvType.CLIENT) | ||
public class VersionedLogicMethodHelper { | ||
|
||
public static void initLogicMethodsForClasses(List<Class<?>> classes) { | ||
for (Class<?> clazz : classes) { | ||
List<MethodFieldAndName> fieldAndNames = getMethodFieldAndNamesForClass(clazz); | ||
for (MethodFieldAndName fn : fieldAndNames) { | ||
initLogicMethod(clazz, fn); | ||
} | ||
} | ||
} | ||
|
||
public static class MethodFieldAndName { | ||
public final Field methodField; | ||
public final String logicMethodNamePrefix; | ||
|
||
public MethodFieldAndName(Field methodField, String logicMethodNamePrefix) { | ||
this.methodField = methodField; | ||
this.logicMethodNamePrefix = logicMethodNamePrefix; | ||
} | ||
} | ||
|
||
public static final String LOGIC_METHOD_SEARCH_FIELD_NAME_PREFIX = "Method_"; | ||
public static final String LOGIC_METHOD_SEARCH_FIELD_TARGET_NAME_PREFIX_SUFFIX = "_PREFIX"; | ||
|
||
public static List<MethodFieldAndName> getMethodFieldAndNamesForClass(Class<?> clazz) { | ||
List<MethodFieldAndName> ret = new ArrayList<>(); | ||
for (Field f : clazz.getDeclaredFields()) { | ||
int modifiers = f.getModifiers(); | ||
if (!Modifier.isStatic(modifiers)) { | ||
continue; | ||
} | ||
String fName = f.getName(); | ||
if (!fName.startsWith(LOGIC_METHOD_SEARCH_FIELD_NAME_PREFIX)) { | ||
continue; | ||
} | ||
if (!ReflectionExceptionProxiedMethod.class.isAssignableFrom(f.getType())) { | ||
continue; | ||
} | ||
|
||
// found method field | ||
// now search for its prefix | ||
try { | ||
Field prefixField = clazz.getDeclaredField(fName + LOGIC_METHOD_SEARCH_FIELD_TARGET_NAME_PREFIX_SUFFIX); | ||
prefixField.setAccessible(true); | ||
String logicMethodPrefix = (String) prefixField.get(null); | ||
ret.add(new MethodFieldAndName(f, logicMethodPrefix)); | ||
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) { | ||
Amecs.log(Level.WARN, "Found logic method search method in class \"" + clazz.getName() + "\" but no associated logic method name prefix"); | ||
e.printStackTrace(); | ||
continue; | ||
} | ||
} | ||
return ret; | ||
} | ||
|
||
public static Method getLogicMethod(Class<?> clazz, String methodPrefix) { | ||
TreeMap<SemanticVersion, Method> methodAndVersions = new TreeMap<>(); | ||
for (Method m : clazz.getDeclaredMethods()) { | ||
String methodName = m.getName(); | ||
if (methodName.startsWith(methodPrefix)) { | ||
String versionString = methodName.substring(methodPrefix.length()).replace('_', '.'); | ||
try { | ||
SemanticVersion version = SemanticVersion.parse(versionString); | ||
methodAndVersions.put(version, m); | ||
} catch (VersionParsingException e) { | ||
Amecs.log(Level.ERROR, "Could not parse semantic version for logic method: " + methodName); | ||
} | ||
} | ||
} | ||
if (Amecs.SEMANTIC_MINECRAFT_VERSION == null) { | ||
return methodAndVersions.firstEntry().getValue(); | ||
} | ||
Entry<SemanticVersion, Method> suitable = methodAndVersions.floorEntry(Amecs.SEMANTIC_MINECRAFT_VERSION); | ||
if (suitable != null) { | ||
return suitable.getValue(); | ||
} | ||
return null; | ||
} | ||
|
||
public static void initLogicMethod(Class<?> clazz, MethodFieldAndName fieldAndName) { | ||
Method logicMethod = getLogicMethod(clazz, fieldAndName.logicMethodNamePrefix); | ||
if (logicMethod == null) { | ||
throw new IllegalStateException("No \"" + fieldAndName.logicMethodNamePrefix + "\" method available for minecraft Version: " + Amecs.SEMANTIC_MINECRAFT_VERSION.getFriendlyString()); | ||
} | ||
ReflectionExceptionProxiedMethod proxiedMethod = new ReflectionExceptionProxiedMethod(logicMethod); | ||
try { | ||
fieldAndName.methodField.setAccessible(true); | ||
fieldAndName.methodField.set(null, proxiedMethod); | ||
} catch (IllegalArgumentException | IllegalAccessException e) { | ||
throw new IllegalStateException("Failed to set the logic method for class: \"" + clazz.getName() + "\""); | ||
} | ||
} | ||
|
||
public static class ReflectionExceptionProxiedMethod { | ||
public final Method method; | ||
|
||
public ReflectionExceptionProxiedMethod(Method method) { | ||
method.setAccessible(true); | ||
this.method = method; | ||
} | ||
|
||
public Object invoke(Object instance, Object... args) { | ||
try { | ||
return method.invoke(instance, args); | ||
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { | ||
Amecs.log(Level.ERROR, "Error while executing: \"" + method.getName() + "\" in class: \"" + method.getDeclaringClass().getName() + "\""); | ||
e.printStackTrace(); | ||
} | ||
return null; | ||
} | ||
} | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/de/siphalor/amecs/keybinding/DropEntireStackKeyBinding.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,51 @@ | ||
package de.siphalor.amecs.keybinding; | ||
|
||
import de.siphalor.amecs.VersionedLogicMethodHelper.ReflectionExceptionProxiedMethod; | ||
import de.siphalor.amecs.api.AmecsKeyBinding; | ||
import de.siphalor.amecs.api.KeyModifiers; | ||
import net.fabricmc.api.EnvType; | ||
import net.fabricmc.api.Environment; | ||
import net.minecraft.client.MinecraftClient; | ||
import net.minecraft.client.network.ClientPlayerEntity; | ||
import net.minecraft.client.util.InputUtil; | ||
import net.minecraft.util.Hand; | ||
|
||
@Environment(EnvType.CLIENT) | ||
public class DropEntireStackKeyBinding extends AmecsKeyBinding implements DropItemStackTriggerListener { | ||
@SuppressWarnings("unused") // used via reflection | ||
private static final String Method_dropEntireStackLogic_PREFIX = "dropEntireStackLogic$"; | ||
private static ReflectionExceptionProxiedMethod Method_dropEntireStackLogic; | ||
|
||
public DropEntireStackKeyBinding(String id, InputUtil.Type type, int code, String category, KeyModifiers defaultModifiers) { | ||
super(id, type, code, category, defaultModifiers); | ||
} | ||
|
||
// TODO: check if it is really equal for all versions between 1.8 - 1.17.1 | ||
// from minecraft code: MinecraftClient with feedback addition | ||
@SuppressWarnings("unused") // used via reflection | ||
private boolean dropEntireStackLogic$1_8(MinecraftClient client) { | ||
ClientPlayerEntity player = client.player; | ||
|
||
// true to always drop an entire stack | ||
if (!player.isSpectator() && player.dropSelectedItem(true)) { | ||
player.swingHand(Hand.MAIN_HAND); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
// TODO: copy the byteCode from MinecraftClient in order to remove this version check | ||
private boolean dropEntireStackLogic_currentVersion(MinecraftClient client) { | ||
return (boolean) Method_dropEntireStackLogic.invoke(this, client); | ||
} | ||
|
||
// this method is NOT called by the InputHandlerManger (because we do not register it there) it is called from MixinMinecraftClient | ||
@Override | ||
public boolean handleDropItemStackEvent(MinecraftClient client) { | ||
boolean dropped = false; | ||
while (wasPressed()) { | ||
dropped |= dropEntireStackLogic_currentVersion(client); | ||
} | ||
return dropped; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/de/siphalor/amecs/keybinding/DropItemStackTriggerListener.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,12 @@ | ||
package de.siphalor.amecs.keybinding; | ||
|
||
import net.fabricmc.api.EnvType; | ||
import net.fabricmc.api.Environment; | ||
import net.minecraft.client.MinecraftClient; | ||
|
||
@Environment(EnvType.CLIENT) | ||
public interface DropItemStackTriggerListener { | ||
|
||
public boolean handleDropItemStackEvent(MinecraftClient client); | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this branch able to run on all the supported Minecraft versions (1.14-1.17)?
If not then this class should come in a separate pull request.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did not test it with versions other than 1.17.1. But I do not expect the logic for item dropping and mouse scrolling to change in versions down to 1.8. But that should be checked to be sure
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just discovered that drop logic changed a little bit from mc 1.14 to 1.15 and is now bytecode incompatible with the newer versions. But I will fix it. It make 1.17 the base/compile version and call the 1.14 version's variant via reflection
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added the 1.14 logic and call the method via reflection. But I did not test it with a mc 1.14 version yet. You could test it when backporting to 1.14