Reflection Question

Discussion in 'Plugin Development' started by ColonelHedgehog, Dec 25, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    ColonelHedgehog

    Hi,

    So I've been playing around with Reflection lately, trying to add dynamic class loading and such. But I've run into a few problems. For instance, how do I cast to a specific class? I looked it up and couldn't really find anything helpful (although I guess I may have missed something).

    For instance, take a look at this code:

    Code:
    ((CraftLivingEntity) horse)
    .getHandle()
    .getAttributeInstance(GenericAttributes.d)
    .setValue(GameState.getCurrentTrack().getTrackData().getDouble("NMS.MaxHorseSpeed"));
    You'll see that I had to cast "horse" to "CraftLivingEntity." CraftLivingEntity is an OBC (org.bukkit.craftbukkit) class that's likely to change with each Minecraft build. So how would I cast the class to a "variable class" (reflected class?)? Because obviously, if I try just using my Class<?>-returning method "getOBC" and then using "cast(horse)", it wouldn't return CraftLivingEntity in the IDE.
     
  2. Offline

    xTrollxDudex

    @ColonelHedgehog
    Not possible. The class would be typed <?> for reflection to be achieve version proofing, however using cast would result in Object being returned, not the actual class that it represents, albeit having the information about the class. You'd cast and use getMethod(...) to use getHandle and the rest of the methods.
     
  3. Offline

    ColonelHedgehog

    I feared as much. ;_;

    Is there some other way to achieve this? Or should I just stick with what I've got?
     
  4. Offline

    xTrollxDudex

    @ColonelHedgehog
    Unless you want to go the hard way and use getMethod(...) on everything, then yes, I would recommend you stay with what you have. You can look into an alternative version proofing method, such as maven abstraction.
     
  5. Offline

    ColonelHedgehog

    Ew, Maven...

    I'll pass. Thank you, though. :)
     
Thread Status:
Not open for further replies.

Share This Page