Skip to content

Commit

Permalink
feat: add vault economy example incl. tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Silthus committed Nov 11, 2021
1 parent 1a71da6 commit 331e961
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 1 deletion.
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ spigot {
apiVersion = project.property("apiVersion")
load = STARTUP
// depends = ['']
// softDepends = []
softDepends = ['Vault']
}

compileJava {
Expand Down Expand Up @@ -69,6 +69,8 @@ dependencies {

// Annotation Command Framework: https://github.com/aikar/commands
implementation "co.aikar:acf-paper:0.5.0-SNAPSHOT"
// Vault (https://github.com/MilkBowl/VaultAPI) for economy, permissions and chat API
implementation "com.github.MilkBowl:VaultAPI:1.7.1"

// Test dependencies
testImplementation 'org.junit.jupiter:junit-jupiter:5.8.1'
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/net/silthus/template/TemplatePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@

import co.aikar.commands.PaperCommandManager;
import kr.entree.spigradle.annotations.PluginMain;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import net.milkbowl.vault.chat.Chat;
import net.milkbowl.vault.economy.Economy;
import net.silthus.template.commands.TemplateCommands;
import net.silthus.template.integrations.vault.VaultProvider;
import org.bukkit.Bukkit;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
Expand All @@ -16,13 +22,17 @@
import java.io.File;
import java.io.IOException;
import java.util.Locale;
import java.util.Objects;

@PluginMain
public class TemplatePlugin extends JavaPlugin implements Listener {

@Getter
@Accessors(fluent = true)
private static TemplatePlugin instance;
@Getter
@Setter(AccessLevel.PACKAGE)
private VaultProvider vault;
private PaperCommandManager commandManager;

public TemplatePlugin() {
Expand All @@ -39,6 +49,7 @@ public TemplatePlugin(
public void onEnable() {
saveDefaultConfig();

setupVaultIntegration();
setupCommands();

getServer().getPluginManager().registerEvents(this, this);
Expand All @@ -49,6 +60,14 @@ public void onPlayerJoin(PlayerJoinEvent event) {
getLogger().info("Player joined.");
}

private void setupVaultIntegration() {
if (Bukkit.getPluginManager().isPluginEnabled("Vault")) {
vault = new VaultProvider(Objects.requireNonNull(getServer().getServicesManager().getRegistration(Economy.class)).getProvider());
} else {
vault = new VaultProvider();
}
}

private void setupCommands() {
commandManager = new PaperCommandManager(this);
commandManager.enableUnstableAPI("help");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* sChat, a Supercharged Minecraft Chat Plugin
* Copyright (C) Silthus <https://www.github.com/silthus>
* Copyright (C) sChat team and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package net.silthus.template.integrations.vault;

import net.milkbowl.vault.economy.Economy;
import org.bukkit.OfflinePlayer;

public class VaultProvider {

private final Economy economy;

public VaultProvider(Economy economy) {
this.economy = economy;
}

public VaultProvider() {
this.economy = null;
}

public double getBalance(OfflinePlayer player) {
if (economy == null) return 0d;
return economy.getBalance(player);
}
}
11 changes: 11 additions & 0 deletions src/test/java/net/silthus/template/TestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,29 @@

import be.seeseemelk.mockbukkit.MockBukkit;
import be.seeseemelk.mockbukkit.ServerMock;
import net.milkbowl.vault.economy.Economy;
import net.silthus.template.integrations.vault.VaultProvider;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;

import static org.mockito.Mockito.mock;

public abstract class TestBase {

protected ServerMock server;
protected TemplatePlugin plugin;
protected Economy economy;

@BeforeEach
public void setUp() {
server = MockBukkit.mock();
plugin = MockBukkit.load(TemplatePlugin.class);
mockVaultEconomy();
}

private void mockVaultEconomy() {
economy = mock(Economy.class);
plugin.setVault(new VaultProvider(economy));
}

@AfterEach
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package net.silthus.template.integrations.vault;

import be.seeseemelk.mockbukkit.entity.PlayerMock;
import net.silthus.template.TestBase;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;

class VaultProviderTest extends TestBase {

private PlayerMock player;

@Override
@BeforeEach
public void setUp() {
super.setUp();

player = server.addPlayer();
}

@Test
void getPlayerBalance_returnsVaultBalance() {
when(economy.getBalance(player)).thenReturn(100D);

assertThat(plugin.getVault().getBalance(player)).isEqualTo(100D);
}
}

0 comments on commit 331e961

Please sign in to comment.