Added simple panel

This commit is contained in:
tastybento 2019-02-07 15:17:55 -08:00
parent eaa1d7b133
commit dfd2d03d36
6 changed files with 308 additions and 4 deletions

View File

@ -43,7 +43,7 @@ public class Greenhouses extends Addon {
recipes = new RecipeManager(this);
// Load manager
manager = new GreenhouseManager(this);
// Register commands for AcidIsland and BSkyBlock
// Register commands for
getPlugin().getAddonsManager().getGameModeAddons().stream()
.filter(gm -> settings.getGameModes().contains(gm.getDescription().getName()))
.forEach(gm -> {

View File

@ -0,0 +1,52 @@
package world.bentobox.greenhouses.ui.panel;
import java.util.ArrayList;
import java.util.List;
import world.bentobox.bentobox.api.panels.builders.PanelBuilder;
import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.util.Util;
import world.bentobox.greenhouses.Greenhouses;
import world.bentobox.greenhouses.greenhouse.BiomeRecipe;
public class Panel {
private Greenhouses addon;
public Panel(Greenhouses addon) {
super();
this.addon = addon;
}
public void ShowPanel(User user) {
PanelBuilder pb = new PanelBuilder().name("Greenhouses");
for (BiomeRecipe br : addon.getRecipes().getBiomeRecipes()) {
if (user.hasPermission(br.getPermission())) {
pb.item(new PanelItemBuilder()
.name(br.getFriendlyName()).icon(br.getIcon())
.description(getDescription(br))
.clickHandler(new PanelClick(addon, br)).build());
}
}
pb.user(user).build();
}
private List<String> getDescription(BiomeRecipe br) {
List<String> d = new ArrayList<>();
// Make description
d.add("Make a " + Util.prettifyText(br.getBiome().toString()));
d.add("Greenhouse. Requires:");
d.addAll(br.getRecipeBlocks());
if (br.getWaterCoverage() > 0) {
d.add("Water coverage = " + br.getWaterCoverage() + "%");
}
if (br.getLavaCoverage() > 0) {
d.add("Lava coverage = " + br.getLavaCoverage() + "%");
}
if (br.getIceCoverage() > 0) {
d.add("Ice coverage = " + br.getIceCoverage() + "%");
}
return d;
}
}

View File

@ -0,0 +1,73 @@
/**
*
*/
package world.bentobox.greenhouses.ui.panel;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.util.Vector;
import world.bentobox.bentobox.api.panels.Panel;
import world.bentobox.bentobox.api.panels.PanelItem.ClickHandler;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.greenhouses.Greenhouses;
import world.bentobox.greenhouses.greenhouse.BiomeRecipe;
import world.bentobox.greenhouses.managers.GreenhouseManager.GhResult;
import world.bentobox.greenhouses.managers.GreenhouseManager.GreenhouseResult;
/**
* @author tastybento
*
*/
public class PanelClick implements ClickHandler {
private Greenhouses addon;
private BiomeRecipe br;
public PanelClick(Greenhouses addon, BiomeRecipe br) {
this.addon = addon;
this.br = br;
}
/* (non-Javadoc)
* @see world.bentobox.bentobox.api.panels.PanelItem.ClickHandler#onClick(world.bentobox.bentobox.api.panels.Panel, world.bentobox.bentobox.api.user.User, org.bukkit.event.inventory.ClickType, int)
*/
@Override
public boolean onClick(Panel panel, User user, ClickType clickType, int slot) {
if (user.hasPermission(br.getPermission())) {
makeGreenhouse(user, br);
}
return true;
}
private boolean makeGreenhouse(User user, BiomeRecipe br) {
// Check flag
if (!addon.getIslands().getIslandAt(user.getLocation()).map(i -> i.isAllowed(user, Greenhouses.GREENHOUSES)).orElse(false)) {
user.sendMessage("greenhouses.errors.no-rank");
return false;
}
// Find the physical the greenhouse
Location location = user.getLocation().add(new Vector(0,1,0));
// Check if there's a gh here already
if (addon.getManager().getMap().getGreenhouse(location).isPresent()) {
user.sendMessage("greenhouses.commands.user.make.error.already");
return false;
}
GhResult result = addon.getManager().tryToMakeGreenhouse(location, br);
if (result.getResults().contains(GreenhouseResult.SUCCESS)) {
// Success
user.sendMessage("greenhouses.commands.user.make.success", "[biome]", result.getFinder().getGh().getBiomeRecipe().getFriendlyName());
return true;
}
result.getResults().forEach(r -> user.sendMessage("greenhouses.commands.user.make.error." + r.name()));
if (!result.getFinder().getRedGlass().isEmpty()) {
// Show red glass
result.getFinder().getRedGlass().forEach(rg -> user.getPlayer().sendBlockChange(rg, Material.RED_STAINED_GLASS.createBlockData()));
Bukkit.getScheduler().runTaskLater(addon.getPlugin(), () -> result.getFinder().getRedGlass().forEach(rg -> user.getPlayer().sendBlockChange(rg, rg.getBlock().getBlockData())), 120L);
}
return true;
}
}

View File

@ -10,8 +10,10 @@ import org.bukkit.util.Vector;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.greenhouses.Greenhouses;
import world.bentobox.greenhouses.greenhouse.BiomeRecipe;
import world.bentobox.greenhouses.managers.GreenhouseManager.GhResult;
import world.bentobox.greenhouses.managers.GreenhouseManager.GreenhouseResult;
import world.bentobox.greenhouses.ui.panel.Panel;
/**
* Command to try to make a greenhouse
@ -43,6 +45,14 @@ class MakeCommand extends CompositeCommand {
*/
@Override
public boolean execute(User user, String label, List<String> args) {
if (args.isEmpty()) {
new Panel((Greenhouses)this.getAddon()).ShowPanel(user);
return true;
}
return makeGreenhouse(user, null);
}
private boolean makeGreenhouse(User user, BiomeRecipe br) {
// Check flag
if (!getIslands().getIslandAt(user.getLocation()).map(i -> i.isAllowed(user, Greenhouses.GREENHOUSES)).orElse(false)) {
user.sendMessage("greenhouses.errors.no-rank");
@ -55,7 +65,7 @@ class MakeCommand extends CompositeCommand {
user.sendMessage("greenhouses.commands.user.make.error.already");
return false;
}
GhResult result = ((Greenhouses)this.getAddon()).getManager().tryToMakeGreenhouse(location, null);
GhResult result = ((Greenhouses)this.getAddon()).getManager().tryToMakeGreenhouse(location, br);
if (result.getResults().contains(GreenhouseResult.SUCCESS)) {
// Success
@ -70,4 +80,5 @@ class MakeCommand extends CompositeCommand {
}
return true;
}
}

View File

@ -12,6 +12,8 @@ import world.bentobox.greenhouses.Greenhouses;
*/
public class UserCommand extends CompositeCommand {
private MakeCommand makeCommand;
/**
* @param gh - addon
* @param parent - parent command
@ -32,7 +34,7 @@ public class UserCommand extends CompositeCommand {
//new InfoCommand(this);
//new ListCommand(this);
new MakeCommand(this);
makeCommand = new MakeCommand(this);
//new RecipeCommand(this);
new RemoveCommand(this);
}
@ -46,4 +48,11 @@ public class UserCommand extends CompositeCommand {
return true;
}
/**
* @return the makeCommand
*/
public MakeCommand getMakeCommand() {
return makeCommand;
}
}

View File

@ -0,0 +1,159 @@
/**
*
*/
package world.bentobox.greenhouses.greenhouse;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
/**
* @author tastybento
*
*/
public class RoofTest {
private Roof roof;
private Block block;
private Location location;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
location = mock(Location.class);
World world = mock(World.class);
when(world.getMaxHeight()).thenReturn(255);
block = mock(Block.class);
when(block.getType()).thenReturn(Material.GLASS);
when(world.getBlockAt(Mockito.anyInt(), Mockito.anyInt(), Mockito.anyInt())).thenReturn(block);
when(location.getWorld()).thenReturn(world);
when(location.getBlockX()).thenReturn(10);
when(location.getBlockY()).thenReturn(10);
when(location.getBlockZ()).thenReturn(10);
}
/**
* Test method for {@link world.bentobox.greenhouses.greenhouse.Roof#Roof(org.bukkit.Location)}.
*/
@Test
public void testRoof() {
//roof = new Roof(location);
}
/**
* Test method for {@link world.bentobox.greenhouses.greenhouse.Roof#getMinX()}.
*/
@Test
public void testGetMinX() {
fail("Not yet implemented"); // TODO
}
/**
* Test method for {@link world.bentobox.greenhouses.greenhouse.Roof#setMinX(int)}.
*/
@Test
public void testSetMinX() {
fail("Not yet implemented"); // TODO
}
/**
* Test method for {@link world.bentobox.greenhouses.greenhouse.Roof#getMaxX()}.
*/
@Test
public void testGetMaxX() {
fail("Not yet implemented"); // TODO
}
/**
* Test method for {@link world.bentobox.greenhouses.greenhouse.Roof#setMaxX(int)}.
*/
@Test
public void testSetMaxX() {
fail("Not yet implemented"); // TODO
}
/**
* Test method for {@link world.bentobox.greenhouses.greenhouse.Roof#getMinZ()}.
*/
@Test
public void testGetMinZ() {
fail("Not yet implemented"); // TODO
}
/**
* Test method for {@link world.bentobox.greenhouses.greenhouse.Roof#setMinZ(int)}.
*/
@Test
public void testSetMinZ() {
fail("Not yet implemented"); // TODO
}
/**
* Test method for {@link world.bentobox.greenhouses.greenhouse.Roof#getMaxZ()}.
*/
@Test
public void testGetMaxZ() {
fail("Not yet implemented"); // TODO
}
/**
* Test method for {@link world.bentobox.greenhouses.greenhouse.Roof#setMaxZ(int)}.
*/
@Test
public void testSetMaxZ() {
fail("Not yet implemented"); // TODO
}
/**
* Test method for {@link world.bentobox.greenhouses.greenhouse.Roof#getArea()}.
*/
@Test
public void testGetArea() {
fail("Not yet implemented"); // TODO
}
/**
* Test method for {@link world.bentobox.greenhouses.greenhouse.Roof#isRoofFound()}.
*/
@Test
public void testIsRoofFound() {
fail("Not yet implemented"); // TODO
}
/**
* Test method for {@link world.bentobox.greenhouses.greenhouse.Roof#getHeight()}.
*/
@Test
public void testGetHeight() {
fail("Not yet implemented"); // TODO
}
/**
* Test method for {@link world.bentobox.greenhouses.greenhouse.Roof#getLocation()}.
*/
@Test
public void testGetLocation() {
fail("Not yet implemented"); // TODO
}
/**
* Test method for {@link world.bentobox.greenhouses.greenhouse.Roof#toString()}.
*/
@Test
public void testToString() {
fail("Not yet implemented"); // TODO
}
}