Let's try and load the jails from flatfile.

This commit is contained in:
graywolf336 2013-12-07 14:16:16 -06:00
parent 7d18cf9a96
commit 11f9e94f40
5 changed files with 517 additions and 450 deletions

View File

@ -2,11 +2,13 @@ package com.graywolf336.jail;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Set;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import com.graywolf336.jail.beans.Jail; import com.graywolf336.jail.beans.Jail;
import com.graywolf336.jail.beans.SimpleLocation;
public class JailIO { public class JailIO {
private JailMain pl; private JailMain pl;
@ -50,10 +52,22 @@ public class JailIO {
break; break;
default: default:
//load the jails from flatfile //load the jails from flatfile
if(flat.contains("jails")) if(flat.isConfigurationSection("jails")) {
pl.getLogger().info("Jails exists"); Set<String> jails = flat.getConfigurationSection("jails").getKeys(false);
if(!jails.isEmpty()) {
pl.getLogger().info("Jails configuration section exists and there are " + jails.size() + ".");
for(String name : jails) {
loadJail(name);
}
}else {
pl.getLogger().warning("Jails configuration section exists but no jails are there.");
}
}
break; break;
} }
int s = pl.getJailManager().getJails().size();
pl.getLogger().info("Loaded " + s + (s == 1 ? " jail." : " jails."));
} }
public void saveJail(Jail j) { public void saveJail(Jail j) {
@ -66,6 +80,7 @@ public class JailIO {
String node = "jails." + j.getName() + "."; String node = "jails." + j.getName() + ".";
//Corners //Corners
flat.set(node + "world", j.getWorldName());
flat.set(node + "top.x", j.getMaxPoint().getBlockX()); flat.set(node + "top.x", j.getMaxPoint().getBlockX());
flat.set(node + "top.y", j.getMaxPoint().getBlockY()); flat.set(node + "top.y", j.getMaxPoint().getBlockY());
flat.set(node + "top.z", j.getMaxPoint().getBlockZ()); flat.set(node + "top.z", j.getMaxPoint().getBlockZ());
@ -77,15 +92,16 @@ public class JailIO {
flat.set(node + "tps.in.x", j.getTeleportIn().getX()); flat.set(node + "tps.in.x", j.getTeleportIn().getX());
flat.set(node + "tps.in.y", j.getTeleportIn().getY()); flat.set(node + "tps.in.y", j.getTeleportIn().getY());
flat.set(node + "tps.in.z", j.getTeleportIn().getZ()); flat.set(node + "tps.in.z", j.getTeleportIn().getZ());
flat.set(node + "tps.in.pitch", j.getTeleportIn().getPitch());
flat.set(node + "tps.in.yaw", j.getTeleportIn().getYaw()); flat.set(node + "tps.in.yaw", j.getTeleportIn().getYaw());
flat.set(node + "tps.in.pitch", j.getTeleportIn().getPitch());
//Tele out //Tele out
flat.set(node + "tps.free.world", j.getTeleportFree().getWorld().getName());
flat.set(node + "tps.free.x", j.getTeleportFree().getX()); flat.set(node + "tps.free.x", j.getTeleportFree().getX());
flat.set(node + "tps.free.y", j.getTeleportFree().getY()); flat.set(node + "tps.free.y", j.getTeleportFree().getY());
flat.set(node + "tps.free.z", j.getTeleportFree().getZ()); flat.set(node + "tps.free.z", j.getTeleportFree().getZ());
flat.set(node + "tps.free.pitch", j.getTeleportFree().getPitch());
flat.set(node + "tps.free.yaw", j.getTeleportFree().getYaw()); flat.set(node + "tps.free.yaw", j.getTeleportFree().getYaw());
flat.set(node + "tps.free.pitch", j.getTeleportFree().getPitch());
try { try {
flat.save(new File(pl.getDataFolder(), "data.yml")); flat.save(new File(pl.getDataFolder(), "data.yml"));
@ -98,4 +114,37 @@ public class JailIO {
break; break;
} }
} }
private void loadJail(String name) {
switch(storage) {
case 1:
case 2:
break;
default:
String node = "jails." + name + ".";
Jail j = new Jail(pl, name);
j.setWorld(node + "world");
j.setMaxPoint(new int[] {flat.getInt(node + "top.x"), flat.getInt(node + "top.y"), flat.getInt(node + "top.z")});
j.setMinPoint(new int[] {flat.getInt(node + "bottom.x"), flat.getInt(node + "bottom.y"), flat.getInt(node + "bottom.z")});
j.setTeleportIn(new SimpleLocation(
flat.getString(node + "world"),
flat.getDouble(node + "tps.in.x"),
flat.getDouble(node + "tps.in.y"),
flat.getDouble(node + "tps.in.z"),
(float) flat.getDouble(node + "tps.in.yaw"),
(float) flat.getDouble(node + "tps.in.pitch")));
j.setTeleportFree(new SimpleLocation(
flat.getString(node + "tps.free.world"),
flat.getDouble(node + "tps.free.x"),
flat.getDouble(node + "tps.free.y"),
flat.getDouble(node + "tps.free.z"),
(float) flat.getDouble(node + "tps.free.yaw"),
(float) flat.getDouble(node + "tps.free.pitch")));
pl.getJailManager().addJail(j, false);
break;
}
}
} }

View File

@ -19,11 +19,11 @@ public class JailMain extends JavaPlugin {
public void onEnable() { public void onEnable() {
loadConfig(); loadConfig();
jm = new JailManager(this);
io = new JailIO(this); io = new JailIO(this);
io.prepareStorage(); io.prepareStorage();
io.loadJails(); io.loadJails();
jm = new JailManager(this);
cmdHand = new CommandHandler(this); cmdHand = new CommandHandler(this);
PluginManager pm = this.getServer().getPluginManager(); PluginManager pm = this.getServer().getPluginManager();

View File

@ -1,231 +1,247 @@
package com.graywolf336.jail.beans; package com.graywolf336.jail.beans;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.Player; import org.bukkit.World;
import org.bukkit.entity.Player;
import com.graywolf336.jail.JailMain;
import com.graywolf336.jail.JailMain;
/** Represents a Jail, contains the prisoners and the cells.
* /** Represents a Jail, contains the prisoners and the cells.
* @author graywolf336 *
* @since 3.0.0 * @author graywolf336
* @version 1.0.2 * @since 3.0.0
*/ * @version 1.0.3
public class Jail { */
private JailMain plugin; public class Jail {
private HashMap<String, Cell> cells; private JailMain plugin;
private HashSet<Prisoner> nocellPrisoners;//prisoners who aren't in a cell private HashMap<String, Cell> cells;
private String name = "", world = ""; private HashSet<Prisoner> nocellPrisoners;//prisoners who aren't in a cell
private int minX, minY, minZ, maxX, maxY, maxZ; private String name = "", world = "";
private SimpleLocation in, free; private int minX, minY, minZ, maxX, maxY, maxZ;
private SimpleLocation in, free;
public Jail(JailMain plugin, String name) {
this.plugin = plugin; public Jail(JailMain plugin, String name) {
this.name = name; this.plugin = plugin;
cells = new HashMap<String, Cell>(); this.name = name;
nocellPrisoners = new HashSet<Prisoner>(); cells = new HashMap<String, Cell>();
} nocellPrisoners = new HashSet<Prisoner>();
}
/** Gets the instance of the plugin's main class. */
public JailMain getPlugin() { /** Gets the instance of the plugin's main class. */
return this.plugin; public JailMain getPlugin() {
} return this.plugin;
}
/** Sets the name of the jail. */
public void setName(String name) { /** Sets the name of the jail. */
this.name = name; public void setName(String name) {
} this.name = name;
}
/** Gets the name of the jail. */
public String getName() { /** Gets the name of the jail. */
return this.name; public String getName() {
} return this.name;
}
/** Sets the location of the <b>minimum</b> point to the given location's coordinates. */
public void setMinPoint(Location location) { /** Sets the location of the <b>minimum</b> point to the given location's coordinates. */
if(this.world.isEmpty()) this.world = location.getWorld().getName(); public void setMinPoint(Location location) {
if(this.world.isEmpty()) this.world = location.getWorld().getName();
this.minX = location.getBlockX();
this.minY = location.getBlockY(); this.minX = location.getBlockX();
this.minZ = location.getBlockZ(); this.minY = location.getBlockY();
} this.minZ = location.getBlockZ();
}
/** Accepts an array of ints as the coord, where <strong>0 = x</strong>, <strong>1 = y</strong>, <strong>2 = z</strong>. */
public void setMinPoint(int[] coords) { /** Accepts an array of ints as the coord, where <strong>0 = x</strong>, <strong>1 = y</strong>, <strong>2 = z</strong>. */
if(coords.length != 3) return; public void setMinPoint(int[] coords) {
if(coords.length != 3) return;
this.minX = coords[0];
this.minY = coords[1]; this.minX = coords[0];
this.minZ = coords[2]; this.minY = coords[1];
} this.minZ = coords[2];
}
/** Gets the minimum point as a Bukkit Location class. */
public Location getMinPoint() { /** Gets the minimum point as a Bukkit Location class. */
return new Location(Bukkit.getServer().getWorld(world), minX, minY, minZ); public Location getMinPoint() {
} return new Location(Bukkit.getServer().getWorld(world), minX, minY, minZ);
}
/** Sets the location of the <b>maximum</b> point to the given location's coordinates. */
public void setMaxPoint(Location location) { /** Sets the location of the <b>maximum</b> point to the given location's coordinates. */
if(this.world.isEmpty()) this.world = location.getWorld().getName(); public void setMaxPoint(Location location) {
if(this.world.isEmpty()) this.world = location.getWorld().getName();
this.maxX = location.getBlockX();
this.maxY = location.getBlockY(); this.maxX = location.getBlockX();
this.maxZ = location.getBlockZ(); this.maxY = location.getBlockY();
} this.maxZ = location.getBlockZ();
}
/** Gets the minimum point as a Bukkit Location class. */
public Location getMaxPoint() { /** Gets the minimum point as a Bukkit Location class. */
return new Location(Bukkit.getServer().getWorld(world), maxX, maxY, maxZ); public Location getMaxPoint() {
} return new Location(Bukkit.getServer().getWorld(world), maxX, maxY, maxZ);
}
/** Accepts an array of ints as the coord, where <strong>0 = x</strong>, <strong>1 = y</strong>, <strong>2 = z</strong>. */
public void setMaxPoint(int[] coords) { /** Accepts an array of ints as the coord, where <strong>0 = x</strong>, <strong>1 = y</strong>, <strong>2 = z</strong>. */
if(coords.length != 3) return; public void setMaxPoint(int[] coords) {
if(coords.length != 3) return;
this.maxX = coords[0];
this.maxY = coords[1]; this.maxX = coords[0];
this.maxZ = coords[2]; this.maxY = coords[1];
} this.maxZ = coords[2];
}
/** Sets the {@link SimpleLocation location} of the teleport <strong>in</strong>. */
public void setTeleportIn(SimpleLocation location) { /** Sets the name of the world this Jail is in. */
if(this.world.isEmpty()) this.world = location.getWorldName(); public void setWorld(String name) {
this.world = name;
this.in = location; }
}
/** Gets the name of the world this Jail is in. */
/** Gets the {@link Location location} of the teleport in. */ public String getWorldName() {
public Location getTeleportIn() { return this.world;
return this.in.getLocation(); }
}
/** Gets the instance of the {@link World world} this Jail is in. */
/** Sets the {@link SimpleLocation location} of the teleport for the <strong>free</strong> spot. */ public World getWorld() {
public void setTeleportFree(SimpleLocation location) { return plugin.getServer().getWorld(world);
this.free = location; }
}
/** Sets the {@link SimpleLocation location} of the teleport <strong>in</strong>. */
/** Gets the {@link Location location} of the teleport free spot.*/ public void setTeleportIn(SimpleLocation location) {
public Location getTeleportFree() { if(this.world.isEmpty()) this.world = location.getWorldName();
return this.free.getLocation();
} this.in = location;
}
/** Adds a cell to the Jail. */
public void addCell(Cell cell) { /** Gets the {@link Location location} of the teleport in. */
this.cells.put(cell.getName(), cell); public Location getTeleportIn() {
} return this.in.getLocation();
}
/** Gets the cell with the given name. */
public Cell getCell(String name) { /** Sets the {@link SimpleLocation location} of the teleport for the <strong>free</strong> spot. */
return this.cells.get(name); public void setTeleportFree(SimpleLocation location) {
} this.free = location;
}
/** Returns the first empty cell, returns null if there aren't any cells or any free cells. */
public Cell getFirstEmptyCell() { /** Gets the {@link Location location} of the teleport free spot.*/
for(Cell c : getCells()) public Location getTeleportFree() {
if(c.hasPrisoner()) return this.free.getLocation();
continue; }
else
return c; /** Adds a cell to the Jail. */
public void addCell(Cell cell) {
return null; this.cells.put(cell.getName(), cell);
} }
/** Gets the amount of cells the jail. */ /** Gets the cell with the given name. */
public int getCellCount() { public Cell getCell(String name) {
return this.cells.size(); return this.cells.get(name);
} }
/** Gets all the cells in the jail. */ /** Returns the first empty cell, returns null if there aren't any cells or any free cells. */
public HashSet<Cell> getCells() { public Cell getFirstEmptyCell() {
return new HashSet<Cell>(this.cells.values()); for(Cell c : getCells())
} if(c.hasPrisoner())
continue;
/** Gets a HashSet of <b>all</b> the prisoners, the ones in cells and ones who aren't. */ else
public HashSet<Prisoner> getAllPrisoners() { return c;
HashSet<Prisoner> all = new HashSet<Prisoner>(nocellPrisoners); //initalize the temp one to return with the prisoners not in any cells
return null;
for(Cell c : cells.values()) }
if(c.hasPrisoner())
all.add(c.getPrisoner()); /** Gets the amount of cells the jail. */
public int getCellCount() {
return all; return this.cells.size();
} }
/** Gets a HashSet of the prisoners <b>in cells</b>. */ /** Gets all the cells in the jail. */
public HashSet<Prisoner> getPrisonersInCells() { public HashSet<Cell> getCells() {
HashSet<Prisoner> all = new HashSet<Prisoner>(); return new HashSet<Cell>(this.cells.values());
}
for(Cell c : getCells())
if(c.hasPrisoner()) /** Gets a HashSet of <b>all</b> the prisoners, the ones in cells and ones who aren't. */
all.add(c.getPrisoner()); public HashSet<Prisoner> getAllPrisoners() {
HashSet<Prisoner> all = new HashSet<Prisoner>(nocellPrisoners); //initalize the temp one to return with the prisoners not in any cells
return all;
} for(Cell c : cells.values())
if(c.hasPrisoner())
/** Gets a HashSet of the prisoners <b>not</b> in cells.*/ all.add(c.getPrisoner());
public HashSet<Prisoner> getPrisonersNotInCells() {
return this.nocellPrisoners; return all;
} }
/** /** Gets a HashSet of the prisoners <b>in cells</b>. */
* Returns whether the player is a prisoner in the system, whether in a cell or no cell. public HashSet<Prisoner> getPrisonersInCells() {
* HashSet<Prisoner> all = new HashSet<Prisoner>();
* @param player The {@link Player player instance} of the person we're checking.
* @return true if is jailed, false if not. for(Cell c : getCells())
*/ if(c.hasPrisoner())
public boolean isPlayerJailed(Player player) { all.add(c.getPrisoner());
return this.isPlayerAPrisoner(player.getName());
} return all;
}
/**
* Returns whether the name of a player is a prisoner in the system, whether in a cell or no cell. /** Gets a HashSet of the prisoners <b>not</b> in cells.*/
* public HashSet<Prisoner> getPrisonersNotInCells() {
* @param name The name of the person we're checking. return this.nocellPrisoners;
* @return true if is jailed, false if not. }
*/
public boolean isPlayerJailed(String name) { /**
return this.isPlayerAPrisoner(name); * Returns whether the player is a prisoner in the system, whether in a cell or no cell.
} *
* @param player The {@link Player player instance} of the person we're checking.
/** * @return true if is jailed, false if not.
* Returns whether the name of a player is a prisoner in the system, whether in a cell or no cell. */
* public boolean isPlayerJailed(Player player) {
* @param name The name of the person we're checking. return this.isPlayerAPrisoner(player.getName());
* @return true if is a prisoner, false if not. }
*/
public boolean isPlayerAPrisoner(String name) { /**
boolean is = false; * Returns whether the name of a player is a prisoner in the system, whether in a cell or no cell.
*
for(Prisoner p : this.getAllPrisoners()) { * @param name The name of the person we're checking.
if(p.getName().equalsIgnoreCase(name)) { * @return true if is jailed, false if not.
is = true; */
break; public boolean isPlayerJailed(String name) {
} return this.isPlayerAPrisoner(name);
} }
return is; /**
} * Returns whether the name of a player is a prisoner in the system, whether in a cell or no cell.
*
/** * @param name The name of the person we're checking.
* Gets the {@link Prisoner prisoner} instance for the given name. * @return true if is a prisoner, false if not.
* */
* @param name The name of the prisoner to get. public boolean isPlayerAPrisoner(String name) {
* @return the prisoner instance, can be null boolean is = false;
*/
public Prisoner getPrisoner(String name) { for(Prisoner p : this.getAllPrisoners()) {
Prisoner r = null; if(p.getName().equalsIgnoreCase(name)) {
is = true;
for(Prisoner p : this.getAllPrisoners()) { break;
if(p.getName().equalsIgnoreCase(name)) { }
r = p; }
break;
} return is;
} }
return r; /**
} * Gets the {@link Prisoner prisoner} instance for the given name.
} *
* @param name The name of the prisoner to get.
* @return the prisoner instance, can be null
*/
public Prisoner getPrisoner(String name) {
Prisoner r = null;
for(Prisoner p : this.getAllPrisoners()) {
if(p.getName().equalsIgnoreCase(name)) {
r = p;
break;
}
}
return r;
}
}

View File

@ -277,6 +277,7 @@ public class MockPlayerInventory implements PlayerInventory {
return false; return false;
} }
@SuppressWarnings("deprecation")
private static Map<String, Object> makeMap(ItemStack[] items) { private static Map<String, Object> makeMap(ItemStack[] items) {
Map<String, Object> contents = new LinkedHashMap<String, Object>( Map<String, Object> contents = new LinkedHashMap<String, Object>(
items.length); items.length);

View File

@ -1,214 +1,215 @@
package test.java.com.graywolf336.jail.util; package test.java.com.graywolf336.jail.util;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
import java.util.WeakHashMap; import java.util.WeakHashMap;
import org.bukkit.Difficulty; import org.bukkit.Difficulty;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.WorldType; import org.bukkit.WorldType;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.generator.ChunkGenerator; import org.bukkit.generator.ChunkGenerator;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer;
import org.mockito.stubbing.Answer;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.*;
public class MockWorldFactory {
public class MockWorldFactory {
private static final Map<String, World> createdWorlds = new HashMap<String, World>();
private static final Map<String, World> createdWorlds = new HashMap<String, World>(); private static final Map<UUID, World> worldUIDS = new HashMap<UUID, World>();
private static final Map<UUID, World> worldUIDS = new HashMap<UUID, World>();
private static final Map<World, Boolean> pvpStates = new WeakHashMap<World, Boolean>();
private static final Map<World, Boolean> pvpStates = new WeakHashMap<World, Boolean>(); private static final Map<World, Boolean> keepSpawnInMemoryStates = new WeakHashMap<World, Boolean>();
private static final Map<World, Boolean> keepSpawnInMemoryStates = new WeakHashMap<World, Boolean>(); private static final Map<World, Difficulty> difficultyStates = new WeakHashMap<World, Difficulty>();
private static final Map<World, Difficulty> difficultyStates = new WeakHashMap<World, Difficulty>();
private MockWorldFactory() {
private MockWorldFactory() { }
}
private static void registerWorld(World world) {
private static void registerWorld(World world) { createdWorlds.put(world.getName(), world);
createdWorlds.put(world.getName(), world); worldUIDS.put(world.getUID(), world);
worldUIDS.put(world.getUID(), world); new File(TestInstanceCreator.worldsDirectory, world.getName()).mkdir();
new File(TestInstanceCreator.worldsDirectory, world.getName()).mkdir(); }
}
private static World basics(String world, World.Environment env, WorldType type) {
private static World basics(String world, World.Environment env, WorldType type) { World mockWorld = mock(World.class);
World mockWorld = mock(World.class); when(mockWorld.getName()).thenReturn(world);
when(mockWorld.getName()).thenReturn(world); when(mockWorld.getPVP()).thenAnswer(new Answer<Boolean>() {
when(mockWorld.getPVP()).thenAnswer(new Answer<Boolean>() { public Boolean answer(InvocationOnMock invocation) throws Throwable {
public Boolean answer(InvocationOnMock invocation) throws Throwable { World w = (World) invocation.getMock();
World w = (World) invocation.getMock(); if (!pvpStates.containsKey(w))
if (!pvpStates.containsKey(w)) pvpStates.put(w, true); // default value
pvpStates.put(w, true); // default value return pvpStates.get(w);
return pvpStates.get(w); }
} });
}); doAnswer(new Answer<Void>() {
doAnswer(new Answer<Void>() { public Void answer(InvocationOnMock invocation) throws Throwable {
public Void answer(InvocationOnMock invocation) throws Throwable { pvpStates.put((World) invocation.getMock(), (Boolean) invocation.getArguments()[0]);
pvpStates.put((World) invocation.getMock(), (Boolean) invocation.getArguments()[0]); return null;
return null; }
} }).when(mockWorld).setPVP(anyBoolean());
}).when(mockWorld).setPVP(anyBoolean()); when(mockWorld.getKeepSpawnInMemory()).thenAnswer(new Answer<Boolean>() {
when(mockWorld.getKeepSpawnInMemory()).thenAnswer(new Answer<Boolean>() { public Boolean answer(InvocationOnMock invocation) throws Throwable {
public Boolean answer(InvocationOnMock invocation) throws Throwable { World w = (World) invocation.getMock();
World w = (World) invocation.getMock(); if (!keepSpawnInMemoryStates.containsKey(w))
if (!keepSpawnInMemoryStates.containsKey(w)) keepSpawnInMemoryStates.put(w, true); // default value
keepSpawnInMemoryStates.put(w, true); // default value return keepSpawnInMemoryStates.get(w);
return keepSpawnInMemoryStates.get(w); }
} });
}); doAnswer(new Answer<Void>() {
doAnswer(new Answer<Void>() { public Void answer(InvocationOnMock invocation) throws Throwable {
public Void answer(InvocationOnMock invocation) throws Throwable { keepSpawnInMemoryStates.put((World) invocation.getMock(), (Boolean) invocation.getArguments()[0]);
keepSpawnInMemoryStates.put((World) invocation.getMock(), (Boolean) invocation.getArguments()[0]); return null;
return null; }
} }).when(mockWorld).setKeepSpawnInMemory(anyBoolean());
}).when(mockWorld).setKeepSpawnInMemory(anyBoolean()); when(mockWorld.getDifficulty()).thenAnswer(new Answer<Difficulty>() {
when(mockWorld.getDifficulty()).thenAnswer(new Answer<Difficulty>() { public Difficulty answer(InvocationOnMock invocation) throws Throwable {
public Difficulty answer(InvocationOnMock invocation) throws Throwable { World w = (World) invocation.getMock();
World w = (World) invocation.getMock(); if (!difficultyStates.containsKey(w))
if (!difficultyStates.containsKey(w)) difficultyStates.put(w, Difficulty.NORMAL); // default value
difficultyStates.put(w, Difficulty.NORMAL); // default value return difficultyStates.get(w);
return difficultyStates.get(w); }
} });
}); doAnswer(new Answer<Void>() {
doAnswer(new Answer<Void>() { public Void answer(InvocationOnMock invocation) throws Throwable {
public Void answer(InvocationOnMock invocation) throws Throwable { difficultyStates.put((World) invocation.getMock(), (Difficulty) invocation.getArguments()[0]);
difficultyStates.put((World) invocation.getMock(), (Difficulty) invocation.getArguments()[0]); return null;
return null; }
} }).when(mockWorld).setDifficulty(any(Difficulty.class));
}).when(mockWorld).setDifficulty(any(Difficulty.class)); when(mockWorld.getEnvironment()).thenReturn(env);
when(mockWorld.getEnvironment()).thenReturn(env); when(mockWorld.getWorldType()).thenReturn(type);
when(mockWorld.getWorldType()).thenReturn(type); when(mockWorld.getSpawnLocation()).thenReturn(new Location(mockWorld, 0, 64, 0));
when(mockWorld.getSpawnLocation()).thenReturn(new Location(mockWorld, 0, 64, 0)); when(mockWorld.getWorldFolder()).thenAnswer(new Answer<File>() {
when(mockWorld.getWorldFolder()).thenAnswer(new Answer<File>() { public File answer(InvocationOnMock invocation) throws Throwable {
public File answer(InvocationOnMock invocation) throws Throwable { if (!(invocation.getMock() instanceof World))
if (!(invocation.getMock() instanceof World)) return null;
return null;
World thiss = (World) invocation.getMock();
World thiss = (World) invocation.getMock(); return new File(TestInstanceCreator.serverDirectory, thiss.getName());
return new File(TestInstanceCreator.serverDirectory, thiss.getName()); }
} });
}); when(mockWorld.getBlockAt(any(Location.class))).thenAnswer(new Answer<Block>() {
when(mockWorld.getBlockAt(any(Location.class))).thenAnswer(new Answer<Block>() { @SuppressWarnings("deprecation")
public Block answer(InvocationOnMock invocation) throws Throwable { public Block answer(InvocationOnMock invocation) throws Throwable {
Location loc; Location loc;
try { try {
loc = (Location) invocation.getArguments()[0]; loc = (Location) invocation.getArguments()[0];
} catch (Exception e) { } catch (Exception e) {
return null; return null;
} }
Material blockType = Material.AIR; Material blockType = Material.AIR;
Block mockBlock = mock(Block.class); Block mockBlock = mock(Block.class);
if (loc.getBlockY() < 64) { if (loc.getBlockY() < 64) {
blockType = Material.DIRT; blockType = Material.DIRT;
} }
when(mockBlock.getType()).thenReturn(blockType); when(mockBlock.getType()).thenReturn(blockType);
when(mockBlock.getTypeId()).thenReturn(blockType.getId()); when(mockBlock.getTypeId()).thenReturn(blockType.getId());
when(mockBlock.getWorld()).thenReturn(loc.getWorld()); when(mockBlock.getWorld()).thenReturn(loc.getWorld());
when(mockBlock.getX()).thenReturn(loc.getBlockX()); when(mockBlock.getX()).thenReturn(loc.getBlockX());
when(mockBlock.getY()).thenReturn(loc.getBlockY()); when(mockBlock.getY()).thenReturn(loc.getBlockY());
when(mockBlock.getZ()).thenReturn(loc.getBlockZ()); when(mockBlock.getZ()).thenReturn(loc.getBlockZ());
when(mockBlock.getLocation()).thenReturn(loc); when(mockBlock.getLocation()).thenReturn(loc);
when(mockBlock.isEmpty()).thenReturn(blockType == Material.AIR); when(mockBlock.isEmpty()).thenReturn(blockType == Material.AIR);
return mockBlock; return mockBlock;
} }
}); });
when(mockWorld.getUID()).thenReturn(UUID.randomUUID()); when(mockWorld.getUID()).thenReturn(UUID.randomUUID());
return mockWorld; return mockWorld;
} }
private static World nullWorld(String world, World.Environment env, WorldType type) { private static World nullWorld(String world, World.Environment env, WorldType type) {
World mockWorld = mock(World.class); World mockWorld = mock(World.class);
when(mockWorld.getName()).thenReturn(world); when(mockWorld.getName()).thenReturn(world);
when(mockWorld.getEnvironment()).thenReturn(env); when(mockWorld.getEnvironment()).thenReturn(env);
when(mockWorld.getWorldType()).thenReturn(type); when(mockWorld.getWorldType()).thenReturn(type);
when(mockWorld.getSpawnLocation()).thenReturn(new Location(mockWorld, 0, 64, 0)); when(mockWorld.getSpawnLocation()).thenReturn(new Location(mockWorld, 0, 64, 0));
when(mockWorld.getWorldFolder()).thenAnswer(new Answer<File>() { when(mockWorld.getWorldFolder()).thenAnswer(new Answer<File>() {
public File answer(InvocationOnMock invocation) throws Throwable { public File answer(InvocationOnMock invocation) throws Throwable {
if (!(invocation.getMock() instanceof World)) if (!(invocation.getMock() instanceof World))
return null; return null;
World thiss = (World) invocation.getMock(); World thiss = (World) invocation.getMock();
return new File(TestInstanceCreator.serverDirectory, thiss.getName()); return new File(TestInstanceCreator.serverDirectory, thiss.getName());
} }
}); });
when(mockWorld.getBlockAt(any(Location.class))).thenAnswer(new Answer<Block>() { when(mockWorld.getBlockAt(any(Location.class))).thenAnswer(new Answer<Block>() {
public Block answer(InvocationOnMock invocation) throws Throwable { @SuppressWarnings("deprecation")
Location loc; public Block answer(InvocationOnMock invocation) throws Throwable {
try { Location loc;
loc = (Location) invocation.getArguments()[0]; try {
} catch (Exception e) { loc = (Location) invocation.getArguments()[0];
return null; } catch (Exception e) {
} return null;
}
Block mockBlock = mock(Block.class);
Material blockType = Material.AIR; Block mockBlock = mock(Block.class);
Material blockType = Material.AIR;
when(mockBlock.getType()).thenReturn(blockType);
when(mockBlock.getTypeId()).thenReturn(blockType.getId()); when(mockBlock.getType()).thenReturn(blockType);
when(mockBlock.getWorld()).thenReturn(loc.getWorld()); when(mockBlock.getTypeId()).thenReturn(blockType.getId());
when(mockBlock.getX()).thenReturn(loc.getBlockX()); when(mockBlock.getWorld()).thenReturn(loc.getWorld());
when(mockBlock.getY()).thenReturn(loc.getBlockY()); when(mockBlock.getX()).thenReturn(loc.getBlockX());
when(mockBlock.getZ()).thenReturn(loc.getBlockZ()); when(mockBlock.getY()).thenReturn(loc.getBlockY());
when(mockBlock.getLocation()).thenReturn(loc); when(mockBlock.getZ()).thenReturn(loc.getBlockZ());
when(mockBlock.isEmpty()).thenReturn(blockType == Material.AIR); when(mockBlock.getLocation()).thenReturn(loc);
return mockBlock; when(mockBlock.isEmpty()).thenReturn(blockType == Material.AIR);
} return mockBlock;
}); }
return mockWorld; });
} return mockWorld;
}
public static World makeNewMockWorld(String world, World.Environment env, WorldType type) {
World w = basics(world, env, type); public static World makeNewMockWorld(String world, World.Environment env, WorldType type) {
registerWorld(w); World w = basics(world, env, type);
return w; registerWorld(w);
} return w;
}
public static World makeNewNullMockWorld(String world, World.Environment env, WorldType type) {
World w = nullWorld(world, env, type); public static World makeNewNullMockWorld(String world, World.Environment env, WorldType type) {
registerWorld(w); World w = nullWorld(world, env, type);
return w; registerWorld(w);
} return w;
}
public static World makeNewMockWorld(String world, World.Environment env, WorldType type, long seed,
ChunkGenerator generator) { public static World makeNewMockWorld(String world, World.Environment env, WorldType type, long seed,
World mockWorld = basics(world, env, type); ChunkGenerator generator) {
when(mockWorld.getGenerator()).thenReturn(generator); World mockWorld = basics(world, env, type);
when(mockWorld.getSeed()).thenReturn(seed); when(mockWorld.getGenerator()).thenReturn(generator);
registerWorld(mockWorld); when(mockWorld.getSeed()).thenReturn(seed);
return mockWorld; registerWorld(mockWorld);
} return mockWorld;
}
public static World getWorld(String name) {
return createdWorlds.get(name); public static World getWorld(String name) {
} return createdWorlds.get(name);
}
public static World getWorld(UUID worldUID) {
return worldUIDS.get(worldUID); public static World getWorld(UUID worldUID) {
} return worldUIDS.get(worldUID);
}
public static List<World> getWorlds() {
// we have to invert the order! public static List<World> getWorlds() {
ArrayList<World> myList = new ArrayList<World>(createdWorlds.values()); // we have to invert the order!
List<World> retList = new ArrayList<World>(); ArrayList<World> myList = new ArrayList<World>(createdWorlds.values());
for (int i = (myList.size() - 1); i >= 0; i--) { List<World> retList = new ArrayList<World>();
retList.add(myList.get(i)); for (int i = (myList.size() - 1); i >= 0; i--) {
} retList.add(myList.get(i));
return retList; }
} return retList;
}
public static void clearWorlds() {
for (String name : createdWorlds.keySet()) public static void clearWorlds() {
new File(TestInstanceCreator.worldsDirectory, name).delete(); for (String name : createdWorlds.keySet())
createdWorlds.clear(); new File(TestInstanceCreator.worldsDirectory, name).delete();
worldUIDS.clear(); createdWorlds.clear();
} worldUIDS.clear();
} }
}