WIP - updated to use latest Panel API

This commit is contained in:
Tastybento 2018-03-11 17:36:06 -07:00
parent a11e670b0d
commit fd3e7a928f
25 changed files with 254 additions and 34 deletions

21
pom.xml
View File

@ -5,9 +5,9 @@
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<powermock.version>1.7.1</powermock.version>
</properties>
<build>
<sourceDirectory>src</sourceDirectory>
<defaultGoal>clean package install</defaultGoal>
<resources>
<resource>
@ -58,6 +58,25 @@
<version>0.0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>
<repository>

View File

@ -1,11 +1,34 @@
package bskyblock.addon.challenges;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.potion.PotionData;
import org.bukkit.potion.PotionType;
import com.google.gson.Gson;
import bskyblock.addon.challenges.commands.ChallengesCommand;
import bskyblock.addon.challenges.commands.admin.ChallengesAdminCommand;
import bskyblock.addon.challenges.config.PluginConfig;
import bskyblock.addon.challenges.database.object.Challenges;
import bskyblock.addon.challenges.database.object.Challenges.ChallengeType;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.addons.Addon;
import us.tastybento.bskyblock.database.DatabaseConnectionSettingsImpl;
import us.tastybento.bskyblock.database.mysql.MySQLDatabaseConnecter;
/**
* Add-on to BSkyBlock that enables challenges
@ -26,13 +49,77 @@ public class ChallengesAddon extends Addon {
this.setEnabled(false);
return;
}
// Challenges Manager
challengesManager = new ChallengesManager(this);
// Register commands
new ChallengesCommand(this);
new ChallengesAdminCommand(this);
// Done
Gson gson = new Gson();
Challenges challenges = new Challenges();
challenges.setChallengeType(ChallengeType.SURROUNDING);
Map<Material, Integer> map = new HashMap<>();
map.put(Material.DIRT, 5);
map.put(Material.ACACIA_FENCE_GATE, 3);
challenges.setRequiredBlocks(map);
challenges.setIcon(new ItemStack(Material.ACACIA_FENCE_GATE));
List<ItemStack> requiredItems = new ArrayList<>();
ItemStack result = new ItemStack(Material.POTION, 55);
ItemStack result2 = new ItemStack(Material.SPLASH_POTION, 22);
ItemStack result3 = new ItemStack(Material.LINGERING_POTION, 11);
PotionMeta potionMeta = (PotionMeta) result.getItemMeta();
PotionData potionData = new PotionData(PotionType.FIRE_RESISTANCE, true, false);
potionMeta.setBasePotionData(potionData);
result.setItemMeta(potionMeta);
PotionMeta potionMeta2 = (PotionMeta) result2.getItemMeta();
PotionData potionData2 = new PotionData(PotionType.SPEED, true, false);
potionMeta2.setBasePotionData(potionData2);
potionMeta2.addEnchant(Enchantment.BINDING_CURSE, 1, true);
result2.setItemMeta(potionMeta2);
requiredItems.add(result);
requiredItems.add(result2);
requiredItems.add(result3);
challenges.setRequiredItems(requiredItems);
challenges.setUniqueId(UUID.randomUUID().toString());
String json = gson.toJson(challenges);
Logger.getAnonymousLogger().info(json);
BSkyBlock plugin = BSkyBlock.getInstance();
MySQLDatabaseConnecter conn = new MySQLDatabaseConnecter(new DatabaseConnectionSettingsImpl(
plugin.getSettings().getDbHost(),
plugin.getSettings().getDbPort(),
plugin.getSettings().getDbName(),
plugin.getSettings().getDbUsername(),
plugin.getSettings().getDbPassword()
));
try (Connection connection = conn.createConnection()) {
StringBuilder sql = new StringBuilder();
sql.append("create table if not exists test (json JSON, uniqueId VARCHAR(255) GENERATED ALWAYS AS (json->\"$.uniqueId\"), INDEX i (uniqueId) );");
// Prepare and execute the database statements
try (PreparedStatement pstmt = connection.prepareStatement(sql.toString())) {
pstmt.executeUpdate();
} catch (SQLException e) {
plugin.getLogger().severe(() -> "Problem trying to create schema for data object ");
}
sql = new StringBuilder();
sql.append("INSERT INTO `TEST` (`json`) VALUES (?)");
try (PreparedStatement pstmt = connection.prepareStatement(sql.toString())) {
pstmt.setString(1, json);
pstmt.executeUpdate();
} catch (SQLException e) {
plugin.getLogger().severe(() -> "Problem trying to create data object ");
e.printStackTrace();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override

View File

@ -19,8 +19,8 @@ import bskyblock.addon.challenges.database.object.Challenges;
import bskyblock.addon.challenges.database.object.Challenges.ChallengeType;
import bskyblock.addon.challenges.panel.ChallengesPanels;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.commands.User;
import us.tastybento.bskyblock.api.configuration.BSBConfig;
import us.tastybento.bskyblock.api.user.User;
public class ChallengesManager {

View File

@ -5,7 +5,7 @@ import java.util.List;
import bskyblock.addon.challenges.ChallengesAddon;
import us.tastybento.bskyblock.Constants;
import us.tastybento.bskyblock.api.commands.CompositeCommand;
import us.tastybento.bskyblock.api.commands.User;
import us.tastybento.bskyblock.api.user.User;
public class ChallengesCommand extends CompositeCommand {
private static final String CHALLENGE_COMMAND = "challenges";

View File

@ -5,7 +5,7 @@ import java.util.List;
import bskyblock.addon.challenges.ChallengesAddon;
import us.tastybento.bskyblock.Constants;
import us.tastybento.bskyblock.api.commands.CompositeCommand;
import us.tastybento.bskyblock.api.commands.User;
import us.tastybento.bskyblock.api.user.User;
public class ChallengesAdminCommand extends CompositeCommand {
private static final String CHALLENGE_ADMIN_COMMAND = "cadmin";

View File

@ -6,8 +6,8 @@ import bskyblock.addon.challenges.ChallengesAddon;
import bskyblock.addon.challenges.panel.CreateChallengeListener;
import us.tastybento.bskyblock.Constants;
import us.tastybento.bskyblock.api.commands.CompositeCommand;
import us.tastybento.bskyblock.api.commands.User;
import us.tastybento.bskyblock.api.panels.builders.PanelBuilder;
import us.tastybento.bskyblock.api.user.User;
public class CreateChallenge extends CompositeCommand {
@ -41,10 +41,10 @@ public class CreateChallenge extends CompositeCommand {
return false;
}
new PanelBuilder()
.setName(args.get(0))
.setSize(49)
.setListener(new CreateChallengeListener(addon, user))
.setUser(user)
.name(args.get(0))
.size(49)
.listener(new CreateChallengeListener(addon, user))
.user(user)
.build();
return true;
}

View File

@ -19,7 +19,7 @@ import org.bukkit.event.player.PlayerQuitEvent;
import bskyblock.addon.challenges.ChallengesAddon;
import us.tastybento.bskyblock.Constants;
import us.tastybento.bskyblock.api.commands.CompositeCommand;
import us.tastybento.bskyblock.api.commands.User;
import us.tastybento.bskyblock.api.user.User;
import us.tastybento.bskyblock.util.Util;
/**

View File

@ -7,7 +7,7 @@ import java.util.List;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.commands.CompositeCommand;
import us.tastybento.bskyblock.api.commands.User;
import us.tastybento.bskyblock.api.user.User;
/**
* @author tastybento

View File

@ -7,7 +7,7 @@ import java.util.List;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.commands.CompositeCommand;
import us.tastybento.bskyblock.api.commands.User;
import us.tastybento.bskyblock.api.user.User;
/**
* @author tastybento

View File

@ -7,7 +7,7 @@ import java.util.List;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.commands.CompositeCommand;
import us.tastybento.bskyblock.api.commands.User;
import us.tastybento.bskyblock.api.user.User;
/**
* @author tastybento

View File

@ -7,7 +7,7 @@ import java.util.List;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.commands.CompositeCommand;
import us.tastybento.bskyblock.api.commands.User;
import us.tastybento.bskyblock.api.user.User;
/**
* @author tastybento

View File

@ -7,7 +7,7 @@ import java.util.List;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.commands.CompositeCommand;
import us.tastybento.bskyblock.api.commands.User;
import us.tastybento.bskyblock.api.user.User;
/**
* @author tastybento

View File

@ -7,7 +7,7 @@ import java.util.List;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.commands.CompositeCommand;
import us.tastybento.bskyblock.api.commands.User;
import us.tastybento.bskyblock.api.user.User;
/**
* @author tastybento

View File

@ -7,7 +7,7 @@ import java.util.List;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.commands.CompositeCommand;
import us.tastybento.bskyblock.api.commands.User;
import us.tastybento.bskyblock.api.user.User;
/**
* @author tastybento

View File

@ -7,7 +7,7 @@ import java.util.List;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.commands.CompositeCommand;
import us.tastybento.bskyblock.api.commands.User;
import us.tastybento.bskyblock.api.user.User;
/**
* @author tastybento

View File

@ -7,7 +7,7 @@ import java.util.List;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.commands.CompositeCommand;
import us.tastybento.bskyblock.api.commands.User;
import us.tastybento.bskyblock.api.user.User;
/**
* @author tastybento

View File

@ -7,7 +7,7 @@ import java.util.List;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.commands.CompositeCommand;
import us.tastybento.bskyblock.api.commands.User;
import us.tastybento.bskyblock.api.user.User;
/**
* @author tastybento

View File

@ -7,7 +7,7 @@ import java.util.List;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.commands.CompositeCommand;
import us.tastybento.bskyblock.api.commands.User;
import us.tastybento.bskyblock.api.user.User;
/**
* @author tastybento

View File

@ -7,7 +7,7 @@ import org.bukkit.Material;
import org.bukkit.entity.EntityType;
import bskyblock.addon.challenges.ChallengesAddon;
import us.tastybento.bskyblock.api.commands.User;
import us.tastybento.bskyblock.api.user.User;
/**
* Enables the state of a Surrounding Challenge to be stored as it is built

View File

@ -7,7 +7,7 @@ import java.util.List;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.commands.CompositeCommand;
import us.tastybento.bskyblock.api.commands.User;
import us.tastybento.bskyblock.api.user.User;
/**
* @author tastybento

View File

@ -12,12 +12,12 @@ import bskyblock.addon.challenges.ChallengesManager;
import bskyblock.addon.challenges.LevelStatus;
import bskyblock.addon.challenges.database.object.Challenges;
import bskyblock.addon.challenges.database.object.Challenges.ChallengeType;
import us.tastybento.bskyblock.api.commands.User;
import us.tastybento.bskyblock.api.panels.ClickType;
import us.tastybento.bskyblock.api.panels.Panel;
import us.tastybento.bskyblock.api.panels.PanelItem;
import us.tastybento.bskyblock.api.panels.builders.PanelBuilder;
import us.tastybento.bskyblock.api.panels.builders.PanelItemBuilder;
import us.tastybento.bskyblock.api.user.User;
public class ChallengesPanels {
@ -45,7 +45,7 @@ public class ChallengesPanels {
public void getChallenges(User user, String level) {
addon.getLogger().info("DEBUG: level requested = " + level);
PanelBuilder panelBuilder = new PanelBuilder()
.setName(user.getTranslation("challenges.guiTitle"));
.name(user.getTranslation("challenges.guiTitle"));
addChallengeItems(panelBuilder, user, level);
addFreeChallanges(panelBuilder);
@ -106,9 +106,9 @@ public class ChallengesPanels {
.build();
addon.getLogger().info("requested slot" + challenge.getSlot());
if (challenge.getSlot() >= 0) {
panelBuilder.addItem(challenge.getSlot(),item);
panelBuilder.item(challenge.getSlot(),item);
} else {
panelBuilder.addItem(item);
panelBuilder.item(item);
}
}
@ -136,7 +136,7 @@ public class ChallengesPanels {
})
//.setCommand(CHALLENGE_COMMAND + " c " + status.getLevel().getUniqueId())
.build();
panelBuilder.addItem(item);
panelBuilder.item(item);
} else {
// Clicking on this icon will do nothing because the challenge is not unlocked yet
String previousLevelName = ChatColor.GOLD + (status.getPreviousLevel().getFriendlyName().isEmpty() ? status.getPreviousLevel().getUniqueId() : status.getPreviousLevel().getFriendlyName());
@ -145,7 +145,7 @@ public class ChallengesPanels {
.name(name)
.description(Arrays.asList(user.getTranslation("challenges.toComplete", "[challengesToDo]",String.valueOf(status.getNumberOfChallengesStillToDo()), "[thisLevel]", previousLevelName)))
.build();
panelBuilder.addItem(item);
panelBuilder.item(item);
}
}
}

View File

@ -4,8 +4,8 @@ import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import bskyblock.addon.challenges.ChallengesAddon;
import us.tastybento.bskyblock.api.commands.User;
import us.tastybento.bskyblock.api.panels.PanelListener;
import us.tastybento.bskyblock.api.user.User;
public class CreateChallengeListener implements PanelListener {

View File

@ -1,13 +1,13 @@
package bskyblock.addon.challenges.panel;
import bskyblock.addon.challenges.ChallengesAddon;
import us.tastybento.bskyblock.api.commands.User;
import us.tastybento.bskyblock.api.panels.builders.PanelBuilder;
import us.tastybento.bskyblock.api.user.User;
public class CreateChallengePanel {
public CreateChallengePanel(ChallengesAddon addon, User user) {
new PanelBuilder().setSize(49).setListener(new CreateChallengeListener(addon, user)).setUser(user).build();
new PanelBuilder().size(49).listener(new CreateChallengeListener(addon, user)).user(user).build();
}
}

View File

@ -20,7 +20,7 @@ import bskyblock.addon.challenges.database.object.Challenges;
import bskyblock.addon.challenges.database.object.Challenges.ChallengeType;
import bskyblock.addon.level.Level;
import us.tastybento.bskyblock.Constants;
import us.tastybento.bskyblock.api.commands.User;
import us.tastybento.bskyblock.api.user.User;
import us.tastybento.bskyblock.util.Util;
/**

View File

@ -0,0 +1,114 @@
/**
*
*/
package bskyblock.addon.challenges;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemFactory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.plugin.PluginManager;
import org.bukkit.potion.PotionData;
import org.bukkit.potion.PotionType;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.Mockito;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import bskyblock.addon.challenges.database.object.Challenges;
import bskyblock.addon.challenges.database.object.Challenges.ChallengeType;
/**
* @author tastybento
*
*/
public class ChallengesAddonTest {
/**
* @throws java.lang.Exception
*/
@BeforeClass
public static void setUpBeforeClass() throws Exception {
Server server = mock(Server.class);
World world = mock(World.class);
world = mock(World.class);
Mockito.when(server.getLogger()).thenReturn(Logger.getAnonymousLogger());
Mockito.when(server.getWorld("world")).thenReturn(world);
Mockito.when(server.getVersion()).thenReturn("BSB_Mocking");
PluginManager pluginManager = mock(PluginManager.class);
when(server.getPluginManager()).thenReturn(pluginManager);
ItemFactory itemFactory = mock(ItemFactory.class);
when(server.getItemFactory()).thenReturn(itemFactory);
Bukkit.setServer(server);
PotionMeta potionMeta = mock(PotionMeta.class);
when(itemFactory.getItemMeta(any())).thenReturn(potionMeta);
OfflinePlayer offlinePlayer = mock(OfflinePlayer.class);
when(Bukkit.getOfflinePlayer(any(UUID.class))).thenReturn(offlinePlayer);
when(offlinePlayer.getName()).thenReturn("tastybento");
when(Bukkit.getItemFactory()).thenReturn(itemFactory);
when(Bukkit.getLogger()).thenReturn(Logger.getAnonymousLogger());
}
@Test
public void test() {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
Challenges challenges = new Challenges();
challenges.setChallengeType(ChallengeType.SURROUNDING);
Map<Material, Integer> map = new HashMap<>();
map.put(Material.DIRT, 5);
map.put(Material.ACACIA_FENCE_GATE, 3);
challenges.setRequiredBlocks(map);
challenges.setIcon(new ItemStack(Material.ACACIA_FENCE_GATE));
List<ItemStack> requiredItems = new ArrayList<>();
ItemStack result = new ItemStack(Material.POTION, 55);
ItemStack result2 = new ItemStack(Material.SPLASH_POTION, 22);
ItemStack result3 = new ItemStack(Material.LINGERING_POTION, 11);
PotionMeta potionMeta = (PotionMeta) result.getItemMeta();
PotionData potionData = new PotionData(PotionType.FIRE_RESISTANCE, true, false);
potionMeta.setBasePotionData(potionData);
result.setItemMeta(potionMeta);
PotionMeta potionMeta2 = (PotionMeta) result2.getItemMeta();
PotionData potionData2 = new PotionData(PotionType.SPEED, true, false);
potionMeta2.setBasePotionData(potionData2);
potionMeta2.addEnchant(Enchantment.BINDING_CURSE, 1, true);
result2.setItemMeta(potionMeta2);
requiredItems.add(result);
requiredItems.add(result2);
requiredItems.add(result3);
challenges.setRequiredItems(requiredItems);
String json = gson.toJson(challenges);
Logger.getAnonymousLogger().info(json);
}
}