Skip to content

[C] Refactor large method getEntity to reduce complexity and size.#17

Open
Athear wants to merge 1 commit intoNicMcPhee:masterfrom
Athear:getEntityReflection
Open

[C] Refactor large method getEntity to reduce complexity and size.#17
Athear wants to merge 1 commit intoNicMcPhee:masterfrom
Athear:getEntityReflection

Conversation

@Athear
Copy link
Copy Markdown

@Athear Athear commented May 2, 2014

The Issue:
The getEntity method in CraftEntity is very large and complex, involving around 120 lines of code and a long, nested if-else chain. This can lead to issues when new Minecraft entities are added, requiring precise placement within the chain.

Justification for this PR:
This PR proposes to reduce both length and complexity with reflection. The proposed getEntity method will be less visually intimidating to editors and require less editing in general.

PR Breakdown:
As most Craftbukkit entity names reflect the naming scheme of Minecraft entities, reflection can be used to quickly convert from a Minecraft entity to a Craftbukkit entity without having to traverse a nested if chain. The new getEntity method extracts the Mincraft entity's name and converts it to the Craftbukkit name, then finds and instantiates the desired Craftbukkit entity.
Special Craftbukkit entity names, which do not precisely match their Minecraft counterpart, are taken from a static map. This map is only used for special names to reduce complexity and increase easy of converting new additions from Minecraft updates.
External behavior of getEntity should remain unchanged.

Test Results and Materials:
Test file: Included in PR.
Tests were added to test the translation of Minecraft entities to Craftbukkit entities. The test instantiates as many Minecraft entities as we were able and attempts to generate Craftbukkit entities from them using the getEntity method. Certain entities required more information than we were able to provide for instantiation, and were excluded from the test suite. These have been noted at the beginning of the file.
The tests were written and confirmed against the original getEntity method before work began on the new getEntity method.

This commit replaces the 120-line method that handled transitioning from
Minecraft entities to Craftbukkit entites with a 40 line reflective
method. The new method should allow simpler handling of new Minecraft
entity classes.
Additionally, this commit adds tests for entity translation.
@schil227
Copy link
Copy Markdown

schil227 commented May 8, 2014

This is an absolutely excellent pull request write-up; it has the perfect amount of detail split up into easily digestible parts. The problem is well defined, as is the justification. The 'PR Breakdown' section allowed me to understand what was being modified without actually looking at the code, and to understand that they're using reflection to solve the problem. Also, adding their own tests for the code is super awesome!

@alexemmons
Copy link
Copy Markdown

Non-code:
Great job, I feel that the pull request would be likely to be accepted based on the CraftBukkit guidelines. The English is very helpful and describes the situation and solution very well. The commit message was also good and it was nice that it was for a specific issue. I was worried that I wouldn't understand what was going on but the English made it very clear. The testing situation is also covered thoroughly.

Code:
The pull request represents a single change, which is cleaning up an if-else chain using reflection. This is definitely the kind of change that would be likely to be accepted by the CraftBukkit team, in my opinion. This is probably one of the more "interesting" refactorings that could help this situation. Based on the group's testing I would conclude that the changes are correct. It would be nice if the fringe cases that they mentioned could be tested by someone before the code went live.

@NicMcPhee
Copy link
Copy Markdown
Owner

Really nice job with a tricky pull request, both to document and to implement. Well done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants