Java mixin #Redirect target attribute error - java

Whenever I try to use the target attribute of #At inside of #Redirect it throws an error.
My #At:
#Redirect(method = "drawBackground", at = #At("INVOKE", "Lnet/minecraft/client/gui/screen/ingame/InventoryScreen;drawBackground(Lnet/minecraft/client/util/math/MatrixStack;F;II)") )
Original method:
protected void drawBackground(MatrixStack matrices, float delta, int mouseX, int mouseY)

Related

How can I use glfwSetWindowUserPointer in LWJGL 3?

I'm trying to make a Window class to abstract all the GLFW stuff. The thing is that I don't know how to use glfwSetWindowUserPointer in LWJGL.
I've used the function before, but in C++. Now I'm moving to Java, using LWJGL.
In C++, I would do something like:
glfwSetWindowUserPointer(myWindow, &myData)
But in LWJGL the function takes 2 long, where the first argument is the window handle, but I don't know what to do with the second one.
How can I pass a pointer to my object containing all the data I need inside the callbacks?
Thanks in advance
To expand on #elect's comment about JNINativeInterface and memGlobalRefToObject:
import org.lwjgl.system.MemoryUtil;
import org.lwjgl.system.jni.JNINativeInterface;
class JavaObject {
String message;
JavaObject(String message) {
this.message = message
}
}
final long pointer = JNINativeInterface.NewGlobalRef(new JavaObject("Hello"));
JavaObject object = MemoryUtil.memGlobalRefToObject(pointer);
JNINativeInterface.DeleteGlobalRef(pointer);
System.out.println(object.message) // => "Hello"
// Already deleted the strong reference held by the native part of the application.
object = MemoryUtil.memGlobalRefToObject(pointer);
System.out.println(object) // => null
On a bit of advice: I'd only use the GLFW user pointer for the callbacks set with glfwSetMonitorCallback and glfwSetErrorCallback. You don't need it for the window callbacks, as you set one callback per window, so you already have a reference to each Java wrapper class.
class Window {
final long handle;
int width;
int height;
WindowObserver observer;
Window(final long handle, final int width, final int height) {
this.handle = handle;
this.width = width;
this.height = height;
glfwSetWindowSizeCallback(handle, (handle, w, h) -> {
if (observer != null) {
observer.windowDidResize(this, this.width, this.height, w, h);
}
this.width = w;
this.height = h;
});
}
}

Java: constructor argument (type double) always 0

i have a class with an construktor with two arguments of type double.
public class NetcdfHandler {
double x;
double y;
public NetcdfHandler (double x, double y){
x = this.x;
y = this.y;
}
}
When i call this in the doGet Method of my Servlet, the given double values somehow are not "received" by the constructor. That means after creating an instance of NetcdfHandler, x and y both have the value 0.0 (but not "null") although "Lat" and "Lng" are set up correctly:
public class Test extends HttpServlet {
private static final long serialVersionUID = 1L;
double Lat;
double Lng;
public Test() {
super();
}
protected void doGet(HttpServletRequest req, HttpServletResponse rsp) throws ServletException, IOException {
Lat = Double.parseDouble(req.getParameter("lat"));
Lng = Double.parseDouble(req.getParameter("lng"));
NetcdfHandler nc = new NetcdfHandler(Lat, Lng);
}
I guess it's quite a beginners mistake, but i couldn't figure out whats the problem here. Maybe someone can help?
Your constructor is incorrect. Write this way:
public NetcdfHandler (double x, double y){
this.x = x;
this.y = y;
}
Currently you're doing the opposite thing: put the default field values (which are 0.0) into parameters. So fields are not changed and parameters are forgotten upon constructor exit.
If you are not going to change fields x and y after construction, it's better to declare them final:
final double x;
final double y;
This way it's easier to prevent some programming mistakes. In particular your code would result in compilation error like "final field is not initialized in constructor".

Java: Misplaced candidate list when using Chinese (Traditional) - New Phonetic keyboard

I'm attempting to use the Chinese (Traditional, Taiwan), Chinese (Traditional) - New Phonetic keyboard on an English (US) Windows 7. When I type into a Java Swing-based text area, the candidate list is showing up on the bottom-right of my screen, regardless of where the text area is positioned on the screen. When I'm not using a Java program, the candidate list shows up in the correct place, directly under the text I'm typing.
Has anybody else run into this behavior and found a workaround for it? I haven't found other reports of this behavior online.
Thanks in advance for any help!
System Details:
Microsoft New Phonetic IME 10.1 (10.1.7601.0)
Chinese input mode
Either half or full shape (doesn't matter)
Standard keyboard layout
Windows 7, 64-bit (same happens on 32-bit)
Affects Java 6, 7, and 8
Affects Swing and JavaFX
I did eventually find similar problems reported, but most of them were related to Japanese IMEs and have already been fixed in the JDK. I didn't find any reports specific to this Chinese IME, but I did find a workaround in case it's useful for others.
The brief summary is that I listen for the WM_IME_STARTCOMPOSITION Windows message. When I see that, I locate the IME candidate window, move it to the location I want, and override its WindowProc to prevent further moves. During composition I also listen for WM_KEYDOWN events because I no longer received any WM_IME messages while the user was composing, even though the candidate window closes and gets recreated several times throughout composition. When I receive the WM_IME_ENDCOMPOSITON message, I stop listening for WM_KEYDOWN messages.
As an alternative approach, I tried sending a WM_IME_CONTROL message with the IMC_SETCANDIDATEPOS command to move the candidate window, but this particular IME seems to ignore it.
I used JNA (https://github.com/twall/jna) to override the WindowProc on both the window containing my text area as well as the IME candidate window.
The code snippet below is an example of the workaround.
hwndMain = WIN_INSTANCE.FindWindow(null, "Main Window");
// Note the existing WindowProc so we can restore it later.
prevWndProc = new BaseTSD.LONG_PTR((long) WIN_INSTANCE.GetWindowLong(hwndMain, WinUser.GWL_WNDPROC));
// Register a new WindowProc that we will use to intercept IME messages.
mainListener = new WindowsCallbackListener() {
#Override
public int callback(int hWnd, int uMsg, int uParam, int lParam) {
if (uMsg == WM_IME_STARTCOMPOSITION || (imeComposing && uMsg == WM_KEYDOWN)) {
imeComposing = true;
final WinDef.HWND hwndIme = WIN_INSTANCE.FindWindow("SYSIME7_READING_UI", null);
if (hwndIme != null && !hwndIme.equals(imeWindow)) {
// We found an IME window that is not the same as the last one. We assume the last one was
// closed. We need to register our callback with the new window.
imeWindow = hwndIme;
final Point imeWindowLocation = getImeWindowLocation();
WIN_INSTANCE.MoveWindow(hwndIme, imeWindowLocation.x, imeWindowLocation.y, 0, 0, true);
final BaseTSD.LONG_PTR prevWndProcIme =
new BaseTSD.LONG_PTR((long) WIN_INSTANCE.GetWindowLong(hwndIme, WinUser.GWL_WNDPROC));
imeListener = new WindowsCallbackListener() {
#Override
public int callback(int hWnd, int uMsg, int uParam, int lParam) {
if (uMsg == WM_WINDOWPOSCHANGING) {
final WindowPosition pos = new WindowPosition(new Pointer((long)lParam));
pos.read();
pos.flags |= SWP_NOMOVE;
pos.write();
}
// Call the window's actual WndProc so the events get processed.
return WIN_INSTANCE.CallWindowProc(prevWndProcIme, hWnd, uMsg, uParam, lParam);
}
};
// Set the WndProc function to use our callback listener instead of the window's one.
WIN_INSTANCE.SetWindowLong(hwndIme, WinUser.GWL_WNDPROC, imeListener);
}
}
else if (uMsg == WM_IME_ENDCOMPOSITION) {
// We can discard the IME listener since its window is closed. If another one gets opened, we'll
// create a new listener.
imeListener = null;
imeComposing = false;
}
// Call the window's previous WindowProc so the event continues to get processed.
return WIN_INSTANCE.CallWindowProc(prevWndProc, hWnd, uMsg, uParam, lParam);
}
};
// Set the WindowProc function to use our WindowProc so the event continues to get processed.
WIN_INSTANCE.SetWindowLong(hwndMain, WinUser.GWL_WNDPROC, mainListener);
The code above assumes the following definitions:
private static final MyUser32 WIN_INSTANCE = MyUser32.INSTANCE;
private static final int SWP_NOMOVE = 2;
private static final int WM_KEYDOWN = 256;
private static final int WM_WINDOWPOSCHANGING = 70;
private static final int WM_IME_ENDCOMPOSITION = 270;
private static final int WM_IME_STARTCOMPOSITION = 269;
private WinDef.HWND hwndMain;
private BaseTSD.LONG_PTR prevWndProc;
// Keep references to these listeners so they don't get garbage-collected.
private WindowsCallbackListener mainListener;
private WindowsCallbackListener imeListener;
private boolean imeComposing;
private WinDef.HWND imeWindow;
public static class WindowPosition extends Structure {
public WinDef.HWND hwnd;
public WinDef.HWND hwndInsertAfter;
public int x;
public int y;
public int cx;
public int cy;
public int flags;
public WindowPosition(Pointer p) {
super(p);
}
#Override
protected List getFieldOrder() {
return Arrays.asList("hwnd", "hwndInsertAfter", "x", "y", "cx", "cy", "flags");
}
}
private interface MyUser32 extends User32 {
MyUser32 INSTANCE = (MyUser32) Native.loadLibrary("user32", MyUser32.class, W32APIOptions.DEFAULT_OPTIONS);
int CallWindowProc(BaseTSD.LONG_PTR prevWndProc, int hWnd, int uMsg, int uParam, int lParam);
int SetWindowLong(HWND hwnd, int nIndex, BaseTSD.LONG_PTR listener) throws LastErrorException;
int SetWindowLong(HWND hwnd, int nIndex, WindowsCallbackListener listener) throws LastErrorException;
}
private interface WindowsCallbackListener extends Callback, StdCall {
int callback(int hWnd, int Msg, int wParam, int lParam);
}

Missing Mesh class on RenderScript support library

I'm following this sample in order to learn about RenderScript. I added the following at project.properties:
renderscript.target=19
renderscript.support.mode=true
sdk.buildtools=23.0.2
I wrote my .rs file, the ScriptC_myfilename was generated at gen folder but the android.support.v8.renderscript could not be resolved to a type, so I added the renderscript-v8.jar located at sdk/build-tools/android-4.4W/renderscript/lib as a library (configure Build Path >> Libraries >> add External JARs) and the problem was fixed.
After codding, I couldn't compile the code due to this error, found on ScriptC_filename.java at gen folder:
Mesh cannot be resolved to a Type
I search about the issue, trying to find the missing class and, I don't know, maybe manually implement this class as a part of my project, so Eclipse would be allowed to import it and fix the error, but I'm a bit confused since the Mesh class isn't cited even on android docs.
I also tried to import the renderscript-v8.jar at sdk\build-tools\android-4.4.2\renderscript\lib, as well as add import android.support.v8.renderscript.Mesh but with no success.
I don't know if this would help, but this is my ScriptC_Snow.java file (everything here was generated, I didn't edit it), The comments is the error showed on Eclipse
public class ScriptC_Snow extends ScriptC {
private static final String __rs_resource_name = "snow";
public ScriptC_Snow(RenderScript rs) {
this(rs,
rs.getApplicationContext().getResources(),
rs.getApplicationContext().getResources().getIdentifier(
__rs_resource_name, "raw",
rs.getApplicationContext().getPackageName()));
}
public ScriptC_Snow(RenderScript rs, Resources resources, int id) {
super(rs, resources, id);
__MESH = Element.MESH(rs); //the method MESH(RenderScript) is undefined for the type Element
__F32_2 = Element.F32_2(rs);
}
private Element __F32_2;
private Element __MESH;
private FieldPacker __rs_fp_F32_2;
private FieldPacker __rs_fp_MESH;
private final static int mExportVarIdx_snowMesh = 0;
private Mesh mExportVar_snowMesh; // Mesh cannot be resolved to a type
public synchronized void set_snowMesh(Mesh v) { // Mesh cannot be resolved to a type
setVar(mExportVarIdx_snowMesh, v);
mExportVar_snowMesh = v; // Mesh cannot be resolved to a type
}
public Mesh get_snowMesh() { // Mesh cannot be resolved to a type
return mExportVar_snowMesh; // Mesh cannot be resolved to a type
}
public Script.FieldID getFieldID_snowMesh() {
return createFieldID(mExportVarIdx_snowMesh, null);
}
private final static int mExportVarIdx_snow = 1;
private ScriptField_Snow mExportVar_snow;
public void bind_snow(ScriptField_Snow v) {
mExportVar_snow = v;
if (v == null) bindAllocation(null, mExportVarIdx_snow);
else bindAllocation(v.getAllocation(), mExportVarIdx_snow);
}
public ScriptField_Snow get_snow() {
return mExportVar_snow;
}
private final static int mExportVarIdx_wind = 2;
private Float2 mExportVar_wind;
public synchronized void set_wind(Float2 v) {
mExportVar_wind = v;
FieldPacker fp = new FieldPacker(8);
fp.addF32(v);
int []__dimArr = new int[1];
__dimArr[0] = 4;
setVar(mExportVarIdx_wind, fp, __F32_2, __dimArr);
}
public Float2 get_wind() {
return mExportVar_wind;
}
public Script.FieldID getFieldID_wind() {
return createFieldID(mExportVarIdx_wind, null);
}
private final static int mExportVarIdx_grav = 3;
private Float2 mExportVar_grav;
public synchronized void set_grav(Float2 v) {
mExportVar_grav = v;
FieldPacker fp = new FieldPacker(8);
fp.addF32(v);
int []__dimArr = new int[1];
__dimArr[0] = 4;
setVar(mExportVarIdx_grav, fp, __F32_2, __dimArr);
}
public Float2 get_grav() {
return mExportVar_grav;
}
public Script.FieldID getFieldID_grav() {
return createFieldID(mExportVarIdx_grav, null);
}
private final static int mExportFuncIdx_initSnow = 0;
public void invoke_initSnow() {
invoke(mExportFuncIdx_initSnow);
}
}
This is my renderscript code (.rs file):
#pragma version(1)
#pragma rs java_package_name(com.mypackage.script)
#include "rs_graphics.rsh"
rs_mesh snowMesh;
typedef struct __attribute__((packed, aligned(4))) Snow {
enter code here
float2 velocity;
float2 position;
uchar4 color;
enter code here
} Snow_t;
Snow_t *snow;
float2 wind;
float2 grav;
int root() {
rsgClearColor(0.0f, 0.0f, 0.0f, 0.0f);
rsgDrawMesh(snowMesh);
return 0;
}
void init() {
grav.x = 0;
grav.y = 18;
wind.x = rsRand(50)+20;
wind.y = rsRand(4) - 2;
}
void initSnow() {
enter code here
const float w = rsgGetWidth();
const float h = rsgGetHeight();
int snowCount = rsAllocationGetDimX(rsGetAllocation(snow));
Snow_t *pSnow = snow;
for (int i=0; i < snowCount; i++) {
pSnow->position.x = rsRand(w);
pSnow->position.y = rsRand(h);
pSnow->velocity.y = rsRand(60);
pSnow->velocity.x = rsRand(100);
pSnow->velocity.x -= 50;
uchar4 c = rsPackColorTo8888(255, 255, 255);
pSnow->color = c;
pSnow++;
}
}
I only used the SDK Manager to obtain the files, is there anything I'm missing? Anyone can give me a link to donwload the latest version of renderscript-v8.jar? Is there any link that I could use to see the missing class, in order to implement it in my project, so Eclipse'd be allowed to import and use it?
Thanks in advance.
You can't use the support library with rs_graphics.rsh. It doesn't support objects like rs_mesh, rs_font, or rs_program*. The support library is only for accessing RenderScript compute.

PropertyAccess: Is there a way to create access functions dynamically?

Here is the thing.. I want to draw some lines into some charts for a website.
These charts sometimes will have sometimes one or two lines.. but sometimes a few more where each is defined by an arbitrary mathematical function.
As for now, I only know this possibility to draw e.g. three lines:
public class ArbitraryFunctionData {
private double x1;
private double x2;
private double x3;
private double y;
public ArbitraryFunctionData(double x1, doouble x2, double x3, double y) {
this.x1 = x1;
this.x2 = x2;
this.x3 = x3;
this.y = y;
}
public double getX1() {
return x1;
}
public double getX2() {
return x2;
}
public double getX3() {
return x3;
}
public double getY() {
return y;
}
public void setX1(double x1) {
this.x1 = x1;
}
public void setX2(double x2) {
this.x2 = x2;
}
public void setX3(double x3) {
this.x3 = x3;
}
public void setY(double y) {
this.y = y;
}
}
Somewhere there is a need to define this interface:
public interface ArbitraryFunctionAccess extends
PropertyAccess<ArbitraryFunctionData> {
ValueProvider<ArbitraryFunctionData, Double> x1();
ValueProvider<ArbitraryFunctionData, Double> x2();
ValueProvider<ArbitraryFunctionData, Double> x3();
#Path("x")
ModelKeyProvider<ArbitraryFunctionData> xKey();
}
So I can add these access function as fields to the chart:
horizontalAxis.addField(arbFuncAccess.x1());
horizontalAxis.addField(arbFuncAccess.x2());
horizontalAxis.addField(arbFuncAccess.x3());
This is a very uncomfortable way to create a chart. Is there any better way to do this?
ArbitraryFunctionData needs to be pre defined and one needs to add every single access method by hand. I'd rather do something like that:
ArbitraryFunction f1 = new ArbitraryFunction(0, 5, 0, 5) {
#Override
public double f(double x) {
return x+1;
}
};
ArbitraryFunction f2 = new ArbitraryFunction(0, 5, 0, 5) {
#Override
public double f(double x) {
return x+2;
}
};
store.add(0, f1.getData()); // a line
store.add(1, f2.getData()); // another line
where ArbitraryFunctions function public double f(double x) needs to be overwritten and ArbitraryFunction.getData() is defined as public List<ArbitraryFunctionData> getData(). This would add more dynamic to the whole thing but the problem is, that I can not add the fields to the chart since they need to be pre-defined in public interface ArbitraryFunctionAccess.
I hope I described my need properly. Does anyone know a possible solution to this?
PropertyAccess isn't the point here - ValueProvider is. The PropertyAccess type is just a nice way to auto-generate a bunch of really boring ValueProvider/ModelKeyProvider/LabelProvider types that you could easily do by hand. Your time is valuable, so we don't make you do it by hand.
This means that you can make a hand-made implementation of ValueProvider that does whatever you want it to. I'm not really clear what 0,5,0,5 means or what f1.getData() is intended to return/do, but you could define f1 as a ValueProvider type that can find objects in that list.
public class AribtraryDataVP implements
ValueProvider<ArbitraryFunctionData, Double>() {
private final int index;
public AribtraryDataVP(int index) {
this.index = index;
}
public Double getValue(ArbitraryFunctionData object) {
return object.getData().get(index);
}
public void setValue(ArbtraryFunctionData object, Double value) {
object.getData().set(index, object);
}
public String getPath() {
return "data[" + index + "]";
}
}
The purpose of the ValueProvider type is to allow access to reading and writing properties of objects. In your case, you probably don't care about setValue, but it is usually good to either implement it or at least throw an exception so you know that someone tried to call it and it didn't work. Likewise, the getPath method is used to tell the difference between properties, sometimes for debugging purposes, and sometimes for sorting or filtering on the server, which needs the name of the property it is supposed to sort/filter. Again, probably not needed in your case, but it is usually good to get right.
I think this then will do what you were trying to do, adding those ValueProviders (i.e. properties, now mapped to list items) to the chart.
//f1, f2
axis.addField(new AribtraryDataVP(1));
axis.addField(new AribtraryDataVP(2));

Categories