Posted March 12, 20223 yr I've successfully implemented a series of classes that extend vanilla mobs: public class SkeletonUnit extends Skeleton implements Unit public class ZombieUnit extends Zombie implements Unit public class CreeperUnit extends Creeper implements Unit The 'Unit' part of the name denotes that it is a vanilla mob class that has modified AI, fields and methods to allow for RTS-like top-down controls. However, all of these classes' tick() methods are almost identical and need the use of many of the same private helper functions and fields. Just having an interface implemented hardly helps with this as it only enforces that these methods exist and not what kind of code they run or the private fields they rely on. Ideally I would have wanted to have it inherit from both my own original class 'Unit' (where I can define a tick() and series of helper functions that all three of these classes use) as well as the vanilla mob (so that I don't have to recreate all of the vanilla mob's features such as sounds, model and basic AI) but Java does not allow for multiple inheritance. Is there a way that I can have the best of both worlds here without having to duplicate my 'Unit' code across all of these classes?
March 12, 20223 yr Author That does sound like the way to go, however, many of these helper functions would need to directly modify a lot of new fields specific to 'Units' I've added - I have about 15 of these. I would like to pass in the mob object itself to the functions so that I don't have to keep passing these fields in, but then what comes up is the issue of parameter typing - I want the helper functions to accept any of my three extended mob classes but of course, Java is hard-typed so it's not gonna accept any old generic variable. I could make the parameter something more generic like Monster or even just a plain Object, but then of course we lose the ability to reference my Unit fields. Afaik, interfaces don't let you define non-static fields either, that would have been really helpful for me...
March 12, 20223 yr Author Ah of course, I got used to just accessing directly since they were only ever used privately. Well I suppose this helps out the core issue, would've liked to be able to extend from two classes but I guess this is the best I've got. Thanks for the help.
March 12, 20223 yr Instead of using an interface and extending the vanilla entity classes, could you use a capability? You could have a base class with the shared logic and then entity-specific implementations attached to different entity classes. If you need to do stuff every tick, you'd need to use LivingUpdateEvent. Edited March 12, 20223 yr by Choonster Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.
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.