Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Made Location less annoying to use. Too bad Location can't simply inh…
…erit Vector at this point without breaking things.
- Loading branch information
Showing
1 changed file
with
161 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2f06bec
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Too bad Location and Vector are not interfaces. I'd say go ahead and break them if you also changed them to interfaces.
2f06bec
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I vote for breaking as well. A BlockLocation that somehow extends or implements Location for working with integer locations would also help a lot.
2f06bec
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, I was thinking of writing a class that both implemented Vector and Location simultanouesly. If they were interfaces, I could.
2f06bec
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And what should BlockLocation do? A Location but instead of doubles with integers? And isn't it possible to create a subclass of Vector with the same functionally as Location does?
Fabian
2f06bec
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fabian
Sure, I could create a subclass of a vector that acted exactly like a location. But say I wanted to pass that to a 2nd plugin, that did not have my class in it. It'd treat it like a vector.
With an interface, I could use them interchangably, and design it however I decided. Polymorphism. Interfaces > Classes.
2f06bec
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, esiental's idea of an integer only Location or Vector is not a bad one. Integer math is many orders of magnitude faster than floating point, and if you are not interested in the extra percision (e.g only looking at integer coords anyway), the speed up is considerable. Also, sqrt and power functions are MUCH MUCH faster with integers (if you design them yourself, at any rate)
2f06bec
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding BlockLocation would have the same rationale as BlockVector does. As Afforess said, a standard way to pass block coordinates. The problem is that the Location interface would anyway need method declarations that use doubles and return doubles so I'm not sure how to prevent this conversion.
Also, creating a new object instance by using Location.toVector() whenever I want to make a vector calculation of a Location object coordinates is very wasteful. I use static methods for that, modifying existing objects instead.
2f06bec
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I see that's the purpose of this commit (not making new instances). Thanks a lot :)
2f06bec
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Location and Vector should disappear … or just become 'static' classes (like java.util.Math, etc) with a bunch of utility methods for doing Location/Vector calculations. Block and Entity should implement BlockLocation and EntityLocation instead of having to have separate object instances for these values. Something like an AnyLocation could exist as a value holder apart from a specific Block or Entity — or Vector could be kept around for that purpose but it really is a poor naming choice.
2f06bec
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice idea, but for example the spawn... methods in the world interface. What location type do they accept? Both? Then you need for many methods which uses locations two methods. Or could you convert from a block location to an entity location? But then you have to create two objects every time, which reduces the advantages by using integers. Or could the block location act like an entity location? Then you have to separate the modifier methods from the getter methods. Meaning: Both location types return the same values (both int and doubles) but support only one modifier type (a integer location couldn't be modified with a double location.
And where is the advantage of supporting a integer and double constructor? Isn't the integer constructor covered by the double constructor?
I uploaded a gist example which also supports a fixed location (allows final location variables). At the moment there is no world and yaw/pitch support. This is only a idea how to accomplish this. The spawn…-methods then don't accept a Location or something like this instead they accept the “LocationGetter” variable (so allows block based, entity based and fixed locations).
Okay based on the gist I uploaded already some files. It is mostly done, and in CraftBukkit and Bukkit are only warnings about this. The only problem could be other plugins using the classes
Location
,Vector
andBlockVector
. Please report any problems/ideas/feature request to me.I also added changes for CraftBukkit, which uses the new location types. On the new-locations branch it don't break any API. On the branch new-locations-break I included the locations types fully. But this will break the API as some return types changes.
On CraftBukkit: new-locations and new-locations-break
Fabian
2f06bec
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice job Fabian… I like it. The only (very minor) suggestion I would have would be some name changes:
2f06bec
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks ;) but I have some questions:
World w.getBlockAt(LocationGetter)
) I used LocationGetter, as this is the lowest class. This doesn't need a world/directional definition. And it isn't relevant if it is mutable. Also have everything that will change the location also a setter (as you could react only on a setter and not “location change”).FixedIntLocation
shouldn't store the location as double, as we want to use faster integer calculations.Also I will rename the class files in a more consistent way:
Also these classes are optimized on speed and not on the dry principle. It is possible to create a abstract class, which contain the implementations of the methods and using the getter (getX, getY, …) and not the variables directly. This is maybe slower (don't know if the JVM is optimizing anything there) and couldn't differ between integer and double implementation. So it couldn't profit from the faster integer calculations.
Fabian