Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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?

  • 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...

  • 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.

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 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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

Important Information

By using this site, you agree to our Terms of Use.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.