-
Notifications
You must be signed in to change notification settings - Fork 8
Java Implementation
Here is a simple example on how to get started with creating your first app in Kotlin. We will be implementing the three main concepts previously discussed of Apps, the Environment, App, and Block and how to register your app with Apps.
The first class we'll create is our App class, you can copy and paste this code block into a new file in your project:
public class TutorialApp extends App {
private Player player;
public TutorialApp(Player player) {
super(player);
this.player = player;
}
@NotNull
@Override
public Block root() {
//TODO: Add later
}
@Override
public void onCreate(boolean b) {
}
}
You'll notice there's still a TODO in the root() method, we'll update this method after we create our root block. Also we're leaving the onCreate(child: Boolean) method empty as well. This method is called when a player opens your app, it is useful for initializing data or using Dependency Injection like Dagger.
Now that we have our App class created, we can create our Environment class. Here's the Environment code block you can copy and paste into your project:
public class TutorialEnvironment extends Environment<TutorialApp> {
@Override
public void build() {
//Do nothing
}
@NotNull
@Override
public TutorialApp getInstance(@NotNull Player player) {
return new TutorialApp(player);
}
@NotNull
@Override
public String name() {
return "Tutorial";
}
@NotNull
@Override
public String icon() {
return "http://textures.minecraft.net/texture/f9d8d552f88e0af9682c4f2eacc92e4b452796b8d61010dd3670046d13299ad4";
}
@Nullable
@Override
public String permission() {
return null;
}
@NotNull
@Override
public String summary() {
return "A tutorial app for the Apps Wiki";
}
}
Since the Environment class requires an App type parameter, it's typically easier to create your App class first and then your Environment. The Environment class also has several methods for you to implement, most are simple methods used by Apps to properly display your app on the Apps List screen and the Admin app, here's a brief description of what each method does:
This method is run once when you first register your Environment with Apps. It is meant for any initialization code like building a Dagger dependency tree for your app.
This method is called whenever a player opens your app. You should always create a new instance of your App class whenever this method is called as to not cause interference when two players are using your app at the same time.
This method should return the name of your app. Apps uses this value to display the name of your app on the Apps List page.
This method should return a url to your app's icon. App icons are player heads so this url should be a textures.minecraft.net url.
This method should return a permission node that will determine if a player is allowed to see your app on the Apps List page. Apps will check with the server's permissions plugin to see if the player currently using Apps has this permissions node. If they do (or you set the method to return null), your app will be displayed in the Apps List page, otherwise, it will be hidden.
This method should return a simple description of your app. Just one or two sentences is the expected length. This text will be displayed in areas in the Admin app when editing enabled apps and modifying your app's config.
Now that we have our Environment and App setup, now we need to create our first Block. This includes making a Block, ViewController, Presenter, and Interactor.
First we'll create a presenter for our block. This is the contract between our Interactor and ViewController and should extend the base Presenter class. Here's a code block you can paste into your project to get started:
public interface TutorialPresenter extends Presenter {
}
Now that we have our Presenter, we can create our ViewController. This class will implement our Present we just created which is why we created that first. Here's the code block for our ViewController:
public class TutorialViewController extends NavigationViewController {
public TutorialViewController(@NotNull Player player, @NotNull Location origin) {
super(player, origin);
}
@Override
public void createView() {
super.createView();
}
}
Now, we're actually extending the NavigationViewController class here and not ViewController. This is because the NavigationViewController automatically adds the navigation buttons (Back & Close) on the top of the screen. You can implement ViewController instead if you don't those buttons displayed but typically, you will want those buttons when creating a full screen block.
Next we'll add our Interactor with this code block:
public class TutorialInteractor extends Interactor {
public TutorialInteractor(@NotNull Presenter presenter) {
super(presenter);
}
@Override
public void onCreate() {
super.onCreate();
}
}
At this point, our Interactor does not do anything yet but notice that we're passing the TutorialPresenter as a parameter in our constructor. We'll be using the presenter later to set button listeners and update the screen.
The last class we'll be implementing is the Block class. This is our builder class, responsible for satisfying any dependencies for the Interactor and ViewController classes as well as creating our Interactor and ViewController instances. Here's the code block for the Block class:
public class TutorialBlock extends Block {
private final TutorialViewController view;
private final TutorialInteractor interactor;
public TutorialBlock(@NotNull Player player, @NotNull Location origin) {
super(player, origin);
view = new TutorialViewController(player, origin);
interactor = new TutorialInteractor(view);
}
@NotNull
@Override
public Interactor interactor() {
return interactor;
}
@NotNull
@Override
public ViewController view() {
return view;
}
}
Since neither of our Interactor or ViewController have any extra dependencies, the Block class is pretty simple.
Now that we have have a block to load when our app launches, we can update our App class to set it as the root block. In your TutorialApp class, replace your root() method with this code block:
@NotNull
@Override
public Block root() {
return new TutorialBlock(player, origin);
}
And that's all the code we need to create our first app! The last thing we need to do is register our app with Apps. This is very simple and only one line of code:
AppInjector.INSTANCE.register(TutorialEnvironment())
This line of code should be added you your JavaPlugin class in the onEnable() method to ensure your app is ready to use once players start joining the server.