Posted December 28, 201410 yr Trying reflection for the first time, and something which seemed very simple eludes me. if (rider != null && rider.ridingEntity != null) { if (rider.getHeldItem() != null && rider.getHeldItem().isItemEqual(new ItemStack(Items.carrot_on_a_stick))) { System.out.println("Rider is holding carrot on a stick."); Class clazz = EntityPig.class; Method method = null; try { method = clazz.getMethod("updateAITasks", (Class) null); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (method != null) { try { method.invoke(pig, (Class) null); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("Success!"); } } System.out.println("CheckRiderThread: ridingEntity set."); } } Here is the corresponding method I am trying to access from EntityPig: protected void updateAITasks() { super.updateAITasks(); } So, why is it throwing a NoSuchMethodException when I try to access the method?
December 28, 201410 yr Your class.getMethod() call has too many parameters. Remove "(Class) null" Maker of the Craft++ mod.
December 28, 201410 yr Author Still doesn't work. if (rider != null && rider.ridingEntity != null) { if (rider.getHeldItem() != null && rider.getHeldItem().isItemEqual(new ItemStack(Items.carrot_on_a_stick))) { System.out.println("Rider is holding carrot on a stick."); Class clazz = EntityPig.class; Method method = null; try { method = clazz.getMethod("updateAITasks"); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (method != null) { try { method.invoke(pig); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("Success!"); } } System.out.println("CheckRiderThread: ridingEntity set."); } } @diesieben: If you join the game on a mounted pig and are holding a carrot on a stick, you have to wait a significant amount of time before it registers and the pig starts walking. I'm attempting to eliminate the wait time.
December 28, 201410 yr Author How do I set it to accessible if I can't access it in the first place? Edit: Nevermind, figured it out. I got the method to fire but unfortunately it didn't do anything. Thanks anyway, though.
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.