Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First attempt at allowing dynamic portals #118

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

rourke750
Copy link
Contributor

Allow other plugins to create portals through the api.

Allow other plugins to create portals through the api.
@rourke750
Copy link
Contributor Author

@Maxopoly @ProgrammerDan Thoughts on how to make this better?

Copy link
Contributor

@Maxopoly Maxopoly left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm barely understanding what you are even trying to do here. How does this allow dynamic portals? What's the supposed use case of this?

* portal type.
* @param plugin Your plugin's name.
*/
public void addPortalType(int id, String plugin) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those ids should be managed in BetterShards, what you are doing here will require all possible users of this API to coordinate with each other to avoid overlap, which is exactly the opposite of what you want in an API.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No they won't because when they get the portal id it will be specific in that specific server set up.
So better shards can create 3 portals and they will be given id 0,1,2.
Then some other plugin can create a portal and it will be given the id of 3.

return null;
}
return event.getPortal();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure you want the API to offer changing the portal being loaded? It means that if I request a portal with name "XYZ" from the database, I might get anything else, because of listeners interferring. You should be careful to separate database storage and gameplay mechanics here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other plugin listeners should only modify portals they care about. If a plugin is doing something they shouldn't there are additional problems anways.

@@ -221,6 +227,11 @@ public Boolean call() {
+ "inv_id INT NOT NULL,"
+ "last_upd TIMESTAMP NOT NULL DEFAULT NOW(),"
+ "PRIMARY KEY (uuid, inv_id));");
this.db.registerMigration(3, false, "CREATE TABLE IF NOT EXISTS portalVersion("
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is redundant with the portal_type column in the table "createPortalDataTable". It also doesnt convert over any preexisting portals

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It isn't. portal_type doesn't offer any unique way for other plugins to know what ids are taken by who.
In portals manager it registers the portals there.

@@ -489,4 +493,31 @@ public void blockPlaceEvent(BlockPlaceEvent event) {
event.setCancelled(true);
}
}

@EventHandler(priority = EventPriority.LOWEST)
public void portalLoadEvent(PortalLoadEvent event) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Listening to your own events, especially when that event is only created in one place, is bad practice

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it's only bad practice when you don't mention where to find that specific code which in fairness I didn't. I was also not sure how I should do this. I was thinking I could keep the code in the load method but then not call the event for BetterShards portals but then there is a state where the event is only called for other plugins making it inconsistent

wb.setServerName(event.getServerName());
event.setPortal(wb);
}
else if (db.getPortalID(BetterShardsPlugin.getInstance().getName(), 2) == specialId) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There has to be an easier way than doing 3 separate sync db queries here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea I can. Will do.

@rourke750
Copy link
Contributor Author

Also self note need to work on making the create portal command compatible with these changes.

Plugins register their portals with the BetterShardsAPI class.
BetterShards builds the class and loads all the generic portal
attributes, inside each sub Portal class there is a method where each
plugin can specify how they want extra attributes loaded.
PreparedStatement addPortalLoc = connection.prepareStatement(DatabaseManager.addPortalType)){
addPortalLoc.setString(1, plugin);
addPortalLoc.setInt(2, id);
addPortalLoc.execute();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's worth noting that inserts / updates / deletes can silently fail. Always use executeUpdate() and check the value returned. If != to the number of rows you thought you'd be modifying, you've got a failure-state.

* @param name The name of the portalType that will show up to players.
*/
public static <E extends Portal> void registerPortal(int plugin_id, String plugin, Class<E> portal, String name) {
BetterShardsPlugin.getDatabaseManager().addPortalType(plugin_id, plugin);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that this is called from startup and repeatedly elsewhere, shouldn't you be checking if this type exists before adding it? Even though your uniqueness pkey guarantee will probably prevent a new row from being generated, relying on an error is risky... better to explicitly check imho.

}

public boolean isOnCurrentServer() {
return isOnCurrentServer;
}

public Portal setIsOnCurrentServer(boolean value) {
isOnCurrentServer = value;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does dirty need to be set here?

}

public Portal setFirstLocation(LocationWrapper loc) {
first = loc;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does dirty need to be set here? I'd imagine so.

}

public Portal setSecondLocation(LocationWrapper loc) {
second = loc;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same -- I'd assume setting dirty is indicated.

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

Successfully merging this pull request may close these issues.

3 participants