This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
TheWhoAreYouPerson's profile picture

Allow the same logic to be used for ITurtleUpgrade and IPeripheral

Started by TheWhoAreYouPerson, 19 May 2015 - 07:38 PM
TheWhoAreYouPerson #1
Posted 19 May 2015 - 09:38 PM
I think it would be a great addition/change to the ComputerCraft API would be to allow turtle upgrades (ITurtleUpgrade) and in-world peripherals (IPeripheral) to share the same logic. This would be most easily solved, I think, if the getType method for one of the interfaces was renamed, removing the conflict of different arguments, but same name and arguments. This would allow addon makers to easily change the logic for blocks that are both block-peripherals, and turtle-peripherals at the same time.
I'm sure some of you are thinking that a class can't be both for a turtle, and a tileEntity. You are right. I am talking about setting default methods on interfaces this allows to essentially implement the peripheral on any relevant class. Iam up for discussion on how this would be a great or terrible idea/why it would be very bad practice. Thanks for the time.
SquidDev #2
Posted 19 May 2015 - 10:02 PM
ITurtleUpgrade has a IPeripheral createPeripheral(ITurtleAccess, TurtleSide) method which allows you to reuse your peripheral. If you want to reuse the same code for both TEs and turtles, put most of your logic in some abstract peripheral class and subclass for turtle/TE specific methods (like here).

Sadly default methods are Java 8 only so we cannot use those yet.