Updated Multiverse-Core for Bukkit 1.13.2.

This commit is contained in:
Jeremy Wood 2019-01-23 23:50:47 -05:00
parent f7bbb5029b
commit 01f549f408
20 changed files with 472 additions and 1055 deletions

35
pom.xml
View File

@ -29,6 +29,10 @@
<id>vault-repo</id>
<url>http://nexus.hc.to/content/repositories/pub_releases</url>
</repository>
<repository>
<id>minebench-repo</id>
<url>https://repo.minebench.de/</url>
</repository>
<!-- Has a copy of metrics R8-SNAPSHOT !-->
<repository>
<id>elmakers-repo</id>
@ -250,6 +254,19 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>se.eris</groupId>
<artifactId>notnull-instrumenter-maven-plugin</artifactId>
<version>0.6.8</version>
<executions>
<execution>
<goals>
<goal>instrument</goal>
<goal>tests-instrument</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
@ -263,9 +280,9 @@
</dependency>
<!--Spigot API-->
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.12-R0.1-SNAPSHOT</version>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
<version>1.13.2-R0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<!--Spigot API-->
@ -355,6 +372,13 @@
<version>1.3.8</version>
</dependency>
<!-- End of JSON dependency -->
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>16.0.2</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<!-- Start of Test Dependencies -->
<dependency>
<groupId>junit</groupId>
@ -388,6 +412,11 @@
<version>3.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>de.themoep.idconverter</groupId>
<artifactId>mappings</artifactId>
<version>1.2-SNAPSHOT</version>
</dependency>
<!-- End of Test Dependencies -->
</dependencies>
<distributionManagement>

View File

@ -24,6 +24,7 @@ import org.bukkit.ChatColor;
import org.bukkit.Difficulty;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.World.Environment;
import org.bukkit.WorldType;
@ -33,6 +34,7 @@ import org.bukkit.entity.Player;
import org.bukkit.permissions.Permission;
import org.bukkit.permissions.PermissionDefault;
import org.bukkit.util.Vector;
import org.jetbrains.annotations.Nullable;
import org.json.simple.JSONObject;
import java.util.Collections;
@ -943,7 +945,7 @@ public class MVWorld implements MultiverseWorld {
* {@inheritDoc}
*/
@Override
public int getCurrency() {
public Material getCurrency() {
return this.props.getCurrency();
}
@ -951,7 +953,7 @@ public class MVWorld implements MultiverseWorld {
* {@inheritDoc}
*/
@Override
public void setCurrency(int currency) {
public void setCurrency(@Nullable Material currency) {
this.props.setCurrency(currency);
}

View File

@ -84,12 +84,14 @@ import com.onarandombox.MultiverseCore.utils.UnsafeCallWrapper;
import com.onarandombox.MultiverseCore.utils.VaultHandler;
import com.onarandombox.MultiverseCore.utils.WorldManager;
import com.pneumaticraft.commandhandler.CommandHandler;
import de.themoep.idconverter.IdMappings;
import me.main__.util.SerializationConfig.NoSuchPropertyException;
import me.main__.util.SerializationConfig.SerializationConfig;
import org.bukkit.ChatColor;
import org.bukkit.Difficulty;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Server;
import org.bukkit.World.Environment;
import org.bukkit.command.Command;
@ -638,8 +640,15 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core {
// migrate entryfee
if (section.isConfigurationSection("entryfee")) {
ConfigurationSection feeSection = section.getConfigurationSection("entryfee");
if (feeSection.isInt("currency"))
world.setCurrency(feeSection.getInt("currency"));
if (feeSection.isInt("currency")) {
int oldCurrencyItemId = feeSection.getInt("currency", -1);
if (oldCurrencyItemId >= 0) {
String flatteningType = IdMappings.getById(Integer.toString(oldCurrencyItemId))
.getFlatteningType();
world.setCurrency(Material.matchMaterial(flatteningType));
}
world.setCurrency(null);
}
if (feeSection.isDouble("amount"))
world.setPrice(feeSection.getDouble("amount"));
@ -885,17 +894,6 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core {
}
}
/**
* {@inheritDoc}
*
* @deprecated This is deprecated.
*/
@Override
@Deprecated
public com.onarandombox.MultiverseCore.utils.SafeTTeleporter getTeleporter() {
return new com.onarandombox.MultiverseCore.utils.SafeTTeleporter(this);
}
/**
* {@inheritDoc}
*/

View File

@ -8,6 +8,7 @@ import com.onarandombox.MultiverseCore.configuration.WorldPropertyValidator;
import com.onarandombox.MultiverseCore.enums.AllowedPortalType;
import com.onarandombox.MultiverseCore.enums.EnglishChatColor;
import com.onarandombox.MultiverseCore.enums.EnglishChatStyle;
import de.themoep.idconverter.IdMappings;
import me.main__.util.SerializationConfig.IllegalPropertyValueException;
import me.main__.util.SerializationConfig.Property;
import me.main__.util.SerializationConfig.SerializationConfig;
@ -18,8 +19,10 @@ import org.bukkit.ChatColor;
import org.bukkit.Difficulty;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World.Environment;
import org.bukkit.configuration.serialization.SerializableAs;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Collections;
@ -261,7 +264,7 @@ public class WorldProperties extends SerializationConfig {
@Property(description = "Sorry, 'animals' must either be: true or false.")
private volatile SpawnSettings spawning;
@Property
private volatile EntryFee entryfee;
volatile EntryFee entryfee;
@Property(description = "Sorry, 'hunger' must either be: true or false.")
private volatile boolean hunger;
@Property(description = "Sorry, 'autoheal' must either be: true or false.")
@ -496,11 +499,11 @@ public class WorldProperties extends SerializationConfig {
return this.setPropertyValueUnchecked("respawnWorld", respawnToWorld);
}
public int getCurrency() {
public Material getCurrency() {
return this.entryfee.getCurrency();
}
public void setCurrency(int currency) {
public void setCurrency(@Nullable Material currency) {
this.setPropertyValueUnchecked("entryfee.currency", currency);
}

View File

@ -70,17 +70,6 @@ public interface Core {
*/
MVPlayerSession getPlayerSession(Player player);
/**
* Gets the instantiated Safe-T-Teleporter for performing
* safe teleports.
*
* @return A non-null {@link SafeTTeleporter}.
*
* @deprecated Use {@link #getSafeTTeleporter()} instead.
*/
@Deprecated
com.onarandombox.MultiverseCore.utils.SafeTTeleporter getTeleporter();
/**
* Multiverse uses an advanced permissions setup, this object
* simplifies getting/setting permissions.

View File

@ -13,10 +13,12 @@ import org.bukkit.ChatColor;
import org.bukkit.Difficulty;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.WorldType;
import org.bukkit.command.CommandSender;
import org.bukkit.permissions.Permission;
import org.jetbrains.annotations.Nullable;
import java.util.List;
@ -511,19 +513,21 @@ public interface MultiverseWorld {
void setPrice(double price);
/**
* Gets the Type of currency that will be used when users enter this world.
* Gets the type of currency that will be used when users enter this world. A value of null indicates a non-item
* based currency is used.
*
* @return The Type of currency that will be used when users enter this world.
* @return The type of currency that will be used when users enter this world.
*/
int getCurrency();
@Nullable
Material getCurrency();
/**
* Sets the type of item that will be required given the price is not 0.
* Use -1 to use an AllPay economy, or any valid itemid
* Use a value of null to specify a non-item based currency.
*
* @param item The Type of currency that will be used when users enter this world.
*/
void setCurrency(int item);
void setCurrency(@Nullable Material item);
/**
* Gets the world players will respawn in if they die in this one.

View File

@ -62,7 +62,7 @@ public class CoordCommand extends MultiverseCommand {
df.setMaximumFractionDigits(2);
p.sendMessage(ChatColor.AQUA + "Coordinates: " + ChatColor.WHITE + plugin.getLocationManipulation().strCoords(p.getLocation()));
p.sendMessage(ChatColor.AQUA + "Direction: " + ChatColor.WHITE + plugin.getLocationManipulation().getDirection(p.getLocation()));
p.sendMessage(ChatColor.AQUA + "Block: " + ChatColor.WHITE + Material.getMaterial(world.getBlockTypeIdAt(p.getLocation())));
p.sendMessage(ChatColor.AQUA + "Block: " + ChatColor.WHITE + world.getBlockAt(p.getLocation()).getType());
} else {
sender.sendMessage("This command needs to be used from a Player.");
}

View File

@ -2,10 +2,15 @@ package com.onarandombox.MultiverseCore.configuration;
import java.util.Map;
import de.themoep.idconverter.IdMappings;
import me.main__.util.SerializationConfig.IllegalPropertyValueException;
import me.main__.util.SerializationConfig.Property;
import me.main__.util.SerializationConfig.SerializationConfig;
import me.main__.util.SerializationConfig.Serializor;
import org.bukkit.Material;
import org.bukkit.configuration.serialization.SerializableAs;
import org.jetbrains.annotations.Nullable;
/**
* Entryfee-settings.
@ -14,8 +19,9 @@ import org.bukkit.configuration.serialization.SerializableAs;
public class EntryFee extends SerializationConfig {
@Property
private double amount;
@Property
private int currency;
@Property(serializor = EntryFeeCurrencySerializor.class)
@Nullable
private Material currency;
public EntryFee() {
super();
@ -31,7 +37,7 @@ public class EntryFee extends SerializationConfig {
@Override
protected void setDefaults() {
amount = 0D;
currency = -1;
currency = null;
}
/**
@ -44,7 +50,8 @@ public class EntryFee extends SerializationConfig {
/**
* @return the currency
*/
public int getCurrency() {
@Nullable
public Material getCurrency() {
return currency;
}
@ -60,7 +67,23 @@ public class EntryFee extends SerializationConfig {
* Sets the currency.
* @param currency The new value.
*/
public void setCurrency(int currency) {
public void setCurrency(@Nullable Material currency) {
this.currency = currency;
}
public static final class EntryFeeCurrencySerializor implements Serializor<Material, Object> {
@Override
public String serialize(Material material) {
return material.toString();
}
@Override
public Material deserialize(Object o, Class<Material> aClass) throws IllegalPropertyValueException {
IdMappings.Mapping mapping = IdMappings.getById(o.toString());
if (mapping != null) {
return Material.matchMaterial(mapping.getFlatteningType());
}
return Material.matchMaterial(o.toString());
}
}
}

View File

@ -29,7 +29,7 @@ public class MVMapListener implements Listener {
public void mapInitialize(final MapInitializeEvent event) {
for (final Player player : Bukkit.getOnlinePlayers()) {
if ((player.getItemInHand().getType() == Material.MAP
|| player.getItemInHand().getType() == Material.EMPTY_MAP)
|| player.getItemInHand().getType() == Material.FILLED_MAP)
&& player.getItemInHand().getDurability() == event.getMap().getId()) {
final Location playerLoc = player.getLocation();
final MapView map = event.getMap();

View File

@ -252,7 +252,7 @@ public class MVPlayerListener implements Listener {
// REMEMBER! getTo MAY be NULL HERE!!!
// If the player was actually outside of the portal, adjust the from location
if (event.getFrom().getWorld().getBlockAt(event.getFrom()).getType() != Material.PORTAL) {
if (event.getFrom().getWorld().getBlockAt(event.getFrom()).getType() != Material.NETHER_PORTAL) {
Location newloc = this.plugin.getSafeTTeleporter().findPortalBlockNextTo(event.getFrom());
// TODO: Fix this. Currently, we only check for PORTAL blocks. I'll have to figure out what
// TODO: we want to do here.

View File

@ -71,10 +71,10 @@ public class MVPortalListener implements Listener {
if (event.getAction() != Action.RIGHT_CLICK_BLOCK) {
return;
}
if (event.getClickedBlock().getType() != Material.ENDER_PORTAL_FRAME) {
if (event.getClickedBlock().getType() != Material.END_PORTAL_FRAME) {
return;
}
if (event.getItem() == null || event.getItem().getType() != Material.EYE_OF_ENDER) {
if (event.getItem() == null || event.getItem().getType() != Material.ENDER_EYE) {
return;
}
MultiverseWorld world = this.plugin.getMVWorldManager().getMVWorld(event.getPlayer().getWorld());

View File

@ -1,281 +0,0 @@
/******************************************************************************
* Multiverse 2 Copyright (c) the Multiverse Team 2011. *
* Multiverse 2 is licensed under the BSD License. *
* For more information please check the README.md file included *
* with this project. *
******************************************************************************/
package com.onarandombox.MultiverseCore.utils;
import com.dumptruckman.minecraft.util.Logging;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.entity.Minecart;
import org.bukkit.entity.Vehicle;
/**
* Used to determine block/location-related facts.
*
* @deprecated Use instead: {@link com.onarandombox.MultiverseCore.api.BlockSafety} and {@link SimpleBlockSafety}.
*/
@Deprecated
public class BlockSafety {
/**
* This function checks whether the block at the given coordinates are above air or not.
* @param l The {@link Location} of the block.
* @return True if the block at that {@link Location} is above air.
*/
public boolean isBlockAboveAir(Location l) {
Location downOne = l.clone();
downOne.setY(downOne.getY() - 1);
return (downOne.getBlock().getType() == Material.AIR);
}
// TODO maybe remove this?
private boolean blockIsNotSafe(World world, double x, double y, double z) {
return !playerCanSpawnHereSafely(world, x, y, z);
}
/**
* Checks if a player can spawn safely at the given coordinates.
* @param world The {@link World}.
* @param x The x-coordinate.
* @param y The y-coordinate.
* @param z The z-coordinate.
* @return True if a player can spawn safely at the given coordinates.
*/
public boolean playerCanSpawnHereSafely(World world, double x, double y, double z) {
Location l = new Location(world, x, y, z);
return playerCanSpawnHereSafely(l);
}
/**
* This function checks whether the block at the coordinates given is safe or not by checking for Lava/Fire/Air
* etc. This also ensures there is enough space for a player to spawn!
*
* @param l The {@link Location}
* @return Whether the player can spawn safely at the given {@link Location}
*/
public boolean playerCanSpawnHereSafely(Location l) {
if (l == null) {
// Can't safely spawn at a null location!
return false;
}
World world = l.getWorld();
Location actual = l.clone();
Location upOne = l.clone();
Location downOne = l.clone();
upOne.setY(upOne.getY() + 1);
downOne.setY(downOne.getY() - 1);
if (this.isSolidBlock(world.getBlockAt(actual).getType())
|| this.isSolidBlock(upOne.getBlock().getType())) {
Logging.finest("Error Here (Actual)? (%s)[%s]", actual.getBlock().getType(),
this.isSolidBlock(actual.getBlock().getType()));
Logging.finest("Error Here (upOne)? (%s)[%s]", upOne.getBlock().getType(),
this.isSolidBlock(upOne.getBlock().getType()));
return false;
}
if (downOne.getBlock().getType() == Material.LAVA || downOne.getBlock().getType() == Material.STATIONARY_LAVA) {
Logging.finest("Error Here (downOne)? (%s)[%s]", downOne.getBlock().getType(),
this.isSolidBlock(downOne.getBlock().getType()));
return false;
}
if (downOne.getBlock().getType() == Material.FIRE) {
Logging.finest("There's fire below! (%s)[%s]", actual.getBlock().getType(),
this.isSolidBlock(actual.getBlock().getType()));
return false;
}
if (isBlockAboveAir(actual)) {
Logging.finest("Is block above air [%s]", isBlockAboveAir(actual));
Logging.finest("Has 2 blocks of water below [%s]", this.hasTwoBlocksofWaterBelow(actual));
return this.hasTwoBlocksofWaterBelow(actual);
}
return true;
}
/**
* Gets the location of the top block at the specified {@link Location}.
* @param l Any {@link Location}.
* @return The {@link Location} of the top-block.
*/
public Location getTopBlock(Location l) {
Location check = l.clone();
check.setY(127); // SUPPRESS CHECKSTYLE: MagicNumberCheck
while (check.getY() > 0) {
if (this.playerCanSpawnHereSafely(check)) {
return check;
}
check.setY(check.getY() - 1);
}
return null;
}
/**
* Gets the location of the top block at the specified {@link Location}.
* @param l Any {@link Location}.
* @return The {@link Location} of the top-block.
*/
public Location getBottomBlock(Location l) {
Location check = l.clone();
check.setY(0);
while (check.getY() < 127) { // SUPPRESS CHECKSTYLE: MagicNumberCheck
if (this.playerCanSpawnHereSafely(check)) {
return check;
}
check.setY(check.getY() + 1);
}
return null;
}
/*
* If someone has a better way of this... Please either tell us, or submit a pull request!
*/
private boolean isSolidBlock(Material type) {
switch (type) {
case AIR:
return false;
case SNOW:
return false;
case TRAP_DOOR:
return false;
case TORCH:
return false;
case YELLOW_FLOWER:
return false;
case RED_ROSE:
return false;
case RED_MUSHROOM:
return false;
case BROWN_MUSHROOM:
return false;
case REDSTONE:
return false;
case REDSTONE_WIRE:
return false;
case RAILS:
return false;
case POWERED_RAIL:
return false;
case REDSTONE_TORCH_ON:
return false;
case REDSTONE_TORCH_OFF:
return false;
case DEAD_BUSH:
return false;
case SAPLING:
return false;
case STONE_BUTTON:
return false;
case LEVER:
return false;
case LONG_GRASS:
return false;
case PORTAL:
return false;
case STONE_PLATE:
return false;
case WOOD_PLATE:
return false;
case SEEDS:
return false;
case SUGAR_CANE_BLOCK:
return false;
case WALL_SIGN:
return false;
case SIGN_POST:
return false;
case WOODEN_DOOR:
return false;
case STATIONARY_WATER:
return false;
case WATER:
return false;
default:
return true;
}
}
/**
* Checks if an entity would be on track at the specified {@link Location}.
* @param l The {@link Location}.
* @return True if an entity would be on tracks at the specified {@link Location}.
*/
public boolean isEntitiyOnTrack(Location l) {
Material currentBlock = l.getBlock().getType();
return (currentBlock == Material.POWERED_RAIL || currentBlock == Material.DETECTOR_RAIL || currentBlock == Material.RAILS);
}
// TODO maybe remove this?
private void showDangers(Location l) {
Location actual = new Location(l.getWorld(), l.getX(), l.getY(), l.getZ());
Location upOne = new Location(l.getWorld(), l.getX(), l.getY(), l.getZ());
Location downOne = new Location(l.getWorld(), l.getX(), l.getY(), l.getZ());
upOne.setY(upOne.getY() + 1);
downOne.setY(downOne.getY() - 1);
System.out.print("Location Up: " + upOne.getBlock().getType());
System.out.print(" " + upOne);
System.out.print("Location: " + actual.getBlock().getType());
System.out.print(" " + actual);
System.out.print("Location Down: " + downOne.getBlock().getType());
System.out.print(" " + downOne);
}
/**
* Checks recursively below a {@link Location} for 2 blocks of water.
*
* @param l The {@link Location}
* @return Whether there are 2 blocks of water
*/
private boolean hasTwoBlocksofWaterBelow(Location l) {
if (l.getBlockY() < 0) {
return false;
}
Location oneBelow = l.clone();
oneBelow.subtract(0, 1, 0);
if (oneBelow.getBlock().getType() == Material.WATER || oneBelow.getBlock().getType() == Material.STATIONARY_WATER) {
Location twoBelow = oneBelow.clone();
twoBelow.subtract(0, 1, 0);
return (oneBelow.getBlock().getType() == Material.WATER || oneBelow.getBlock().getType() == Material.STATIONARY_WATER);
}
if (oneBelow.getBlock().getType() != Material.AIR) {
return false;
}
return hasTwoBlocksofWaterBelow(oneBelow);
}
/**
* Checks if the specified {@link Minecart} can spawn safely.
* @param cart The {@link Minecart}.
* @return True if the minecart can spawn safely.
*/
public boolean canSpawnCartSafely(Minecart cart) {
if (this.isBlockAboveAir(cart.getLocation())) {
return true;
}
if (this.isEntitiyOnTrack(LocationManipulation.getNextBlock(cart))) {
return true;
}
return false;
}
/**
* Checks if the specified {@link Vehicle} can spawn safely.
* @param vehicle The {@link Vehicle}.
* @return True if the vehicle can spawn safely.
*/
public boolean canSpawnVehicleSafely(Vehicle vehicle) {
if (this.isBlockAboveAir(vehicle.getLocation())) {
return true;
}
return false;
}
}

View File

@ -1,284 +1,285 @@
package com.onarandombox.MultiverseCore.utils;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
import java.util.HashMap;
/**
* Multiverse's Friendly Economist. This is used to deal with external economies and also item costs for stuff in MV.
*/
public class MVEconomist {
private final VaultHandler vaultHandler;
public MVEconomist(Plugin plugin) {
vaultHandler = new VaultHandler(plugin);
}
private boolean isUsingVault(int currency) {
return !isItemCurrency(currency) && getVaultHandler().hasEconomy();
}
/**
* Checks if an economy plugin is in use.
*
* @return true if an economy plugin is detected by Vault.
*/
public boolean isUsingEconomyPlugin() {
return getVaultHandler().hasEconomy();
}
/**
* Formats the amount to a human readable currency string.
*
* @param amount the amount of currency.
* @param currency the type of currency. A value greater than -1 indicates the material type used for currency.
* @return the human readable currency string.
*/
public String formatPrice(double amount, int currency) {
if (isUsingVault(currency)) {
return getVaultHandler().getEconomy().format(amount);
} else {
return ItemEconomy.getFormattedPrice(amount, currency);
}
}
/**
* Returns the name of the economy in use.
*
* @return the name of the economy in use.
*/
public String getEconomyName() {
if (getVaultHandler().hasEconomy()) {
return getVaultHandler().getEconomy().getName();
} else {
return ItemEconomy.getName();
}
}
/**
* Determines if a player has enough of a given currency.
* @param player the player to check for currency.
* @param amount the amount of currency.
* @param currency the type of currency. A value greater than -1 indicates the material type used for currency.
* @return true if the player has enough of the given currency or the amount is 0 or less.
*/
public boolean isPlayerWealthyEnough(Player player, double amount, int currency) {
if (amount <= 0D) {
return true;
} else if (isUsingVault(currency)) {
return getVaultHandler().getEconomy().has(player, amount);
} else {
return ItemEconomy.hasEnough(player, amount, currency);
}
}
/**
* Formats a message for a player indicating they don't have enough currency.
*
* @param currency the type of currency. A value greater than -1 indicates the material type used for currency.
* @param message The more specific message to append to the generic message of not having enough.
* @return the formatted insufficient funds message.
*/
public String getNSFMessage(int currency, String message) {
return "Sorry, you don't have enough " + (isItemCurrency(currency) ? "items" : "funds") + ". " + message;
}
/**
* Deposits a given amount of currency either into the player's economy account or inventory if the currency
* represents an item.
*
* @param player the player to give currency to.
* @param amount the amount to give.
* @param currency the type of currency. A value greater than -1 indicates the material type used for currency.
*/
public void deposit(Player player, double amount, int currency) {
if (isUsingVault(currency)) {
getVaultHandler().getEconomy().depositPlayer(player, amount);
} else {
ItemEconomy.deposit(player, amount, currency);
}
}
/**
* Withdraws a given amount of currency either from the player's economy account or inventory if the currency
* represents an item.
*
* @param player the player to take currency from.
* @param amount the amount to take.
* @param currency the type of currency. A value greater than -1 indicates the material type used for currency.
*/
public void withdraw(Player player, double amount, int currency) {
if (isUsingVault(currency)) {
getVaultHandler().getEconomy().withdrawPlayer(player, amount);
} else {
ItemEconomy.withdraw(player, amount, currency);
}
}
/**
* Returns the economy balance of the given player.
*
* @param player the player to get the balance for.
* @return the economy balance of the given player.
* @throws IllegalStateException thrown if this is used when no economy plugin is available.
*/
public double getBalance(Player player) throws IllegalStateException {
return getBalance(player, null);
}
/**
* Returns the economy balance of the given player in the given world. If the economy plugin does not have world
* specific balances then the global balance will be returned.
*
* @param player the player to get the balance for.
* @param world the world to get the balance for.
* @return the economy balance of the given player in the given world.
* @throws IllegalStateException thrown if this is used when no economy plugin is available.
*/
public double getBalance(Player player, World world) throws IllegalStateException {
if (!isUsingEconomyPlugin()) {
throw new IllegalStateException("getBalance is only available when using an economy plugin with Vault");
}
if (world != null) {
return getVaultHandler().getEconomy().getBalance(player, world.getName());
} else {
return getVaultHandler().getEconomy().getBalance(player);
}
}
/**
* Sets the economy balance for the given player.
*
* @param player the player to set the balance for.
* @param amount the amount to set the player's balance to.
* @throws IllegalStateException thrown if this is used when no economy plugin is available.
*/
public void setBalance(Player player, double amount) throws IllegalStateException {
setBalance(player, null, amount);
}
/**
* Sets the economy balance for the given player in the given world. If the economy plugin does not have world
* specific balances then the global balance will be set.
*
* @param player the player to set the balance for.
* @param world the world to get the balance for.
* @param amount the amount to set the player's balance to.
* @throws IllegalStateException thrown if this is used when no economy plugin is available.
*/
public void setBalance(Player player, World world, double amount) throws IllegalStateException {
if (!isUsingEconomyPlugin()) {
throw new IllegalStateException("getBalance is only available when using an economy plugin with Vault");
}
if (world != null) {
getVaultHandler().getEconomy().withdrawPlayer(player, world.getName(), getBalance(player, world));
getVaultHandler().getEconomy().depositPlayer(player, world.getName(), amount);
} else {
getVaultHandler().getEconomy().withdrawPlayer(player, getBalance(player));
getVaultHandler().getEconomy().depositPlayer(player, amount);
}
}
/**
* This method is public for backwards compatibility.
*
* @return the old VaultHandler.
* @deprecated just use the other methods in this class for economy stuff.
*/
// TODO make private
@Deprecated
public VaultHandler getVaultHandler() {
return vaultHandler;
}
/**
* Determines if the currency type given represents an item currency.
*
* @param currency the type of currency. A value greater than -1 indicates the material type used for currency.
* @return true if currency is greater than -1.
*/
public static boolean isItemCurrency(int currency) {
return currency >= 0;
}
private static class ItemEconomy {
private static final String ECONOMY_NAME = "Simple Item Economy";
private static String getFormattedPrice(double amount, int currency) {
if (isItemCurrency(currency)) {
Material m = Material.getMaterial(currency);
return m != null ? amount + " " + m.toString() : "NO ITEM FOUND";
} else {
return "";
}
}
private static String getName() {
return ECONOMY_NAME;
}
private static boolean hasEnough(Player player, double amount, int currency) {
if (isItemCurrency(currency)) {
return player.getInventory().contains(currency, (int) amount);
} else {
return true;
}
}
private static void deposit(Player player, double amount, int currency) {
if (isItemCurrency(currency)) {
giveItem(player, amount, currency);
}
}
private static void withdraw(Player player, double amount, int currency) {
if (isItemCurrency(currency)) {
takeItem(player, amount, currency);
}
}
private static void giveItem(Player player, double amount, int type) {
ItemStack item = new ItemStack(type, (int) amount);
player.getInventory().addItem(item);
showReceipt(player, (amount * -1), type);
}
private static void takeItem(Player player, double amount, int type) {
int removed = 0;
HashMap<Integer, ItemStack> items = (HashMap<Integer, ItemStack>) player.getInventory().all(type);
for (int i : items.keySet()) {
if (removed >= amount) {
break;
}
int diff = (int) (amount - removed);
int amt = player.getInventory().getItem(i).getAmount();
if (amt - diff > 0) {
player.getInventory().getItem(i).setAmount(amt - diff);
break;
} else {
removed += amt;
player.getInventory().clear(i);
}
}
showReceipt(player, amount, type);
}
private static void showReceipt(Player player, double price, int item) {
if (price > 0D) {
player.sendMessage(String.format("%s%s%s %s",
ChatColor.WHITE, "You have been charged", ChatColor.GREEN, getFormattedPrice(price, item)));
} else if (price < 0D) {
player.sendMessage(String.format("%s%s%s %s",
ChatColor.DARK_GREEN, getFormattedPrice((price * -1), item),
ChatColor.WHITE, "has been added to your inventory."));
}
}
}
}
package com.onarandombox.MultiverseCore.utils;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.Nullable;
import java.util.HashMap;
/**
* Multiverse's Friendly Economist. This is used to deal with external economies and also item costs for stuff in MV.
*/
public class MVEconomist {
private final VaultHandler vaultHandler;
public MVEconomist(Plugin plugin) {
vaultHandler = new VaultHandler(plugin);
}
private boolean isUsingVault(Material currency) {
return !isItemCurrency(currency) && getVaultHandler().hasEconomy();
}
/**
* Checks if an economy plugin is in use.
*
* @return true if an economy plugin is detected by Vault.
*/
public boolean isUsingEconomyPlugin() {
return getVaultHandler().hasEconomy();
}
/**
* Formats the amount to a human readable currency string.
*
* @param amount the amount of currency.
* @param currency the type of currency. Null indicates a non-item currency is used.
* @return the human readable currency string.
*/
public String formatPrice(double amount, @Nullable Material currency) {
if (isUsingVault(currency)) {
return getVaultHandler().getEconomy().format(amount);
} else {
return ItemEconomy.getFormattedPrice(amount, currency);
}
}
/**
* Returns the name of the economy in use.
*
* @return the name of the economy in use.
*/
public String getEconomyName() {
if (getVaultHandler().hasEconomy()) {
return getVaultHandler().getEconomy().getName();
} else {
return ItemEconomy.getName();
}
}
/**
* Determines if a player has enough of a given currency.
*
* @param player the player to check for currency.
* @param amount the amount of currency.
* @param currency the type of currency. Null indicates non-item currency is used.
* @return true if the player has enough of the given currency or the amount is 0 or less.
*/
public boolean isPlayerWealthyEnough(Player player, double amount, Material currency) {
if (amount <= 0D) {
return true;
} else if (isUsingVault(currency)) {
return getVaultHandler().getEconomy().has(player, amount);
} else {
return ItemEconomy.hasEnough(player, amount, currency);
}
}
/**
* Formats a message for a player indicating they don't have enough currency.
*
* @param currency the type of currency. Null indicates a non-item currency is used.
* @param message The more specific message to append to the generic message of not having enough.
* @return the formatted insufficient funds message.
*/
public String getNSFMessage(Material currency, String message) {
return "Sorry, you don't have enough " + (isItemCurrency(currency) ? "items" : "funds") + ". " + message;
}
/**
* Deposits a given amount of currency either into the player's economy account or inventory if the currency
* is not null.
*
* @param player the player to give currency to.
* @param amount the amount to give.
* @param currency the type of currency.
*/
public void deposit(Player player, double amount, @Nullable Material currency) {
if (isUsingVault(currency)) {
getVaultHandler().getEconomy().depositPlayer(player, amount);
} else {
ItemEconomy.deposit(player, amount, currency);
}
}
/**
* Withdraws a given amount of currency either from the player's economy account or inventory if the currency
* is not null.
*
* @param player the player to take currency from.
* @param amount the amount to take.
* @param currency the type of currency.
*/
public void withdraw(Player player, double amount, @Nullable Material currency) {
if (isUsingVault(currency)) {
getVaultHandler().getEconomy().withdrawPlayer(player, amount);
} else {
ItemEconomy.withdraw(player, amount, currency);
}
}
/**
* Returns the economy balance of the given player.
*
* @param player the player to get the balance for.
* @return the economy balance of the given player.
* @throws IllegalStateException thrown if this is used when no economy plugin is available.
*/
public double getBalance(Player player) throws IllegalStateException {
return getBalance(player, null);
}
/**
* Returns the economy balance of the given player in the given world. If the economy plugin does not have world
* specific balances then the global balance will be returned.
*
* @param player the player to get the balance for.
* @param world the world to get the balance for.
* @return the economy balance of the given player in the given world.
* @throws IllegalStateException thrown if this is used when no economy plugin is available.
*/
public double getBalance(Player player, World world) throws IllegalStateException {
if (!isUsingEconomyPlugin()) {
throw new IllegalStateException("getBalance is only available when using an economy plugin with Vault");
}
if (world != null) {
return getVaultHandler().getEconomy().getBalance(player, world.getName());
} else {
return getVaultHandler().getEconomy().getBalance(player);
}
}
/**
* Sets the economy balance for the given player.
*
* @param player the player to set the balance for.
* @param amount the amount to set the player's balance to.
* @throws IllegalStateException thrown if this is used when no economy plugin is available.
*/
public void setBalance(Player player, double amount) throws IllegalStateException {
setBalance(player, null, amount);
}
/**
* Sets the economy balance for the given player in the given world. If the economy plugin does not have world
* specific balances then the global balance will be set.
*
* @param player the player to set the balance for.
* @param world the world to get the balance for.
* @param amount the amount to set the player's balance to.
* @throws IllegalStateException thrown if this is used when no economy plugin is available.
*/
public void setBalance(Player player, World world, double amount) throws IllegalStateException {
if (!isUsingEconomyPlugin()) {
throw new IllegalStateException("getBalance is only available when using an economy plugin with Vault");
}
if (world != null) {
getVaultHandler().getEconomy().withdrawPlayer(player, world.getName(), getBalance(player, world));
getVaultHandler().getEconomy().depositPlayer(player, world.getName(), amount);
} else {
getVaultHandler().getEconomy().withdrawPlayer(player, getBalance(player));
getVaultHandler().getEconomy().depositPlayer(player, amount);
}
}
/**
* This method is public for backwards compatibility.
*
* @return the old VaultHandler.
* @deprecated just use the other methods in this class for economy stuff.
*/
// TODO make private
@Deprecated
public VaultHandler getVaultHandler() {
return vaultHandler;
}
/**
* Determines if the currency type string given represents an item currency.
*
* @param currency the type of currency.
* @return true if currency string matches a valid material.
*/
public static boolean isItemCurrency(Material currency) {
return currency != null;
}
private static class ItemEconomy {
private static final String ECONOMY_NAME = "Simple Item Economy";
private static String getFormattedPrice(double amount, Material currency) {
if (isItemCurrency(currency)) {
return amount + " " + currency.toString();
} else {
return "";
}
}
private static String getName() {
return ECONOMY_NAME;
}
private static boolean hasEnough(Player player, double amount, Material currency) {
if (currency != null) {
return player.getInventory().contains(currency, (int) amount);
} else {
return true;
}
}
private static void deposit(Player player, double amount, Material currency) {
if (isItemCurrency(currency)) {
giveItem(player, amount, currency);
}
}
private static void withdraw(Player player, double amount, Material currency) {
if (isItemCurrency(currency)) {
takeItem(player, amount, currency);
}
}
private static void giveItem(Player player, double amount, Material type) {
ItemStack item = new ItemStack(type, (int) amount);
player.getInventory().addItem(item);
showReceipt(player, (amount * -1), type);
}
private static void takeItem(Player player, double amount, Material type) {
int removed = 0;
HashMap<Integer, ItemStack> items = (HashMap<Integer, ItemStack>) player.getInventory().all(type);
for (int i : items.keySet()) {
if (removed >= amount) {
break;
}
int diff = (int) (amount - removed);
int amt = player.getInventory().getItem(i).getAmount();
if (amt - diff > 0) {
player.getInventory().getItem(i).setAmount(amt - diff);
break;
} else {
removed += amt;
player.getInventory().clear(i);
}
}
showReceipt(player, amount, type);
}
private static void showReceipt(Player player, double price, Material item) {
if (price > 0D) {
player.sendMessage(String.format("%s%s%s %s",
ChatColor.WHITE, "You have been charged", ChatColor.GREEN, getFormattedPrice(price, item)));
} else if (price < 0D) {
player.sendMessage(String.format("%s%s%s %s",
ChatColor.DARK_GREEN, getFormattedPrice((price * -1), item),
ChatColor.WHITE, "has been added to your inventory."));
}
}
}
}

View File

@ -9,6 +9,7 @@ package com.onarandombox.MultiverseCore.utils;
import com.onarandombox.MultiverseCore.MultiverseCore;
import com.onarandombox.MultiverseCore.api.MultiverseWorld;
import org.bukkit.Material;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;
@ -145,7 +146,7 @@ public class PermissionTools {
}
final MVEconomist economist = plugin.getEconomist();
final int currency = toWorld.getCurrency();
final Material currency = toWorld.getCurrency();
final String formattedAmount = economist.formatPrice(price, currency);
if (economist.isPlayerWealthyEnough(teleporterPlayer, price, currency)) {
@ -171,7 +172,7 @@ public class PermissionTools {
return true;
}
private void sendTeleportPaymentMessage (MVEconomist economist, Player teleporterPlayer, Player teleportee, String toWorld, double price, int currency) {
private void sendTeleportPaymentMessage (MVEconomist economist, Player teleporterPlayer, Player teleportee, String toWorld, double price, Material currency) {
price = Math.abs(price);
if (teleporterPlayer.equals(teleportee)) {
teleporterPlayer.sendMessage("You were " + (price > 0D ? "charged " : "given ") + economist.formatPrice(price, currency) + " for teleporting to " + toWorld);

View File

@ -1,358 +0,0 @@
/******************************************************************************
* Multiverse 2 Copyright (c) the Multiverse Team 2011. *
* Multiverse 2 is licensed under the BSD License. *
* For more information please check the README.md file included *
* with this project. *
******************************************************************************/
package com.onarandombox.MultiverseCore.utils;
import com.onarandombox.MultiverseCore.MultiverseCore;
import com.onarandombox.MultiverseCore.api.BlockSafety;
import com.onarandombox.MultiverseCore.api.MVDestination;
import com.onarandombox.MultiverseCore.destination.InvalidDestination;
import com.onarandombox.MultiverseCore.enums.TeleportResult;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Minecart;
import org.bukkit.entity.Player;
import org.bukkit.entity.Vehicle;
import org.bukkit.util.Vector;
import java.util.logging.Level;
/**
* The {@link SafeTTeleporter}.
*
* @deprecated Use instead: {@link com.onarandombox.MultiverseCore.api.SafeTTeleporter} and {@link SimpleSafeTTeleporter}.
*/
@Deprecated
public class SafeTTeleporter {
private MultiverseCore plugin;
private BlockSafety bs;
public SafeTTeleporter(MultiverseCore plugin) {
this.plugin = plugin;
this.bs = plugin.getBlockSafety();
}
private static final int DEFAULT_TOLERANCE = 6;
private static final int DEFAULT_RADIUS = 9;
/**
* Gets the next safe location around the given location.
* @param l A {@link Location}.
* @return A safe {@link Location}.
*/
public Location getSafeLocation(Location l) {
return this.getSafeLocation(l, DEFAULT_TOLERANCE, DEFAULT_RADIUS);
}
/**
* Gets the next safe location around the given location.
* @param l A {@link Location}.
* @param tolerance The tolerance.
* @param radius The radius.
* @return A safe {@link Location}.
*/
public Location getSafeLocation(Location l, int tolerance, int radius) {
// Check around the player first in a configurable radius:
Location safe = checkAboveAndBelowLocation(l, tolerance, radius);
if (safe != null) {
safe.setX(safe.getBlockX() + .5); // SUPPRESS CHECKSTYLE: MagicNumberCheck
safe.setZ(safe.getBlockZ() + .5); // SUPPRESS CHECKSTYLE: MagicNumberCheck
this.plugin.log(Level.FINE, "Hey! I found one: " + plugin.getLocationManipulation().strCoordsRaw(safe));
} else {
this.plugin.log(Level.FINE, "Uh oh! No safe place found!");
}
return safe;
}
private Location checkAboveAndBelowLocation(Location l, int tolerance, int radius) {
// Tolerance must be an even number:
if (tolerance % 2 != 0) {
tolerance += 1;
}
// We want half of it, so we can go up and down
tolerance /= 2;
this.plugin.log(Level.FINER, "Given Location of: " + plugin.getLocationManipulation().strCoordsRaw(l));
this.plugin.log(Level.FINER, "Checking +-" + tolerance + " with a radius of " + radius);
// For now this will just do a straight up block.
Location locToCheck = l.clone();
// Check the main level
Location safe = this.checkAroundLocation(locToCheck, radius);
if (safe != null) {
return safe;
}
// We've already checked zero right above this.
int currentLevel = 1;
while (currentLevel <= tolerance) {
// Check above
locToCheck = l.clone();
locToCheck.add(0, currentLevel, 0);
safe = this.checkAroundLocation(locToCheck, radius);
if (safe != null) {
return safe;
}
// Check below
locToCheck = l.clone();
locToCheck.subtract(0, currentLevel, 0);
safe = this.checkAroundLocation(locToCheck, radius);
if (safe != null) {
return safe;
}
currentLevel++;
}
return null;
}
/*
* For my crappy algorithm, radius MUST be odd.
*/
private Location checkAroundLocation(Location l, int diameter) {
if (diameter % 2 == 0) {
diameter += 1;
}
Location checkLoc = l.clone();
// Start at 3, the min diameter around a block
int loopcounter = 3;
while (loopcounter <= diameter) {
boolean foundSafeArea = checkAroundSpecificDiameter(checkLoc, loopcounter);
// If a safe area was found:
if (foundSafeArea) {
// Return the checkLoc, it is the safe location.
return checkLoc;
}
// Otherwise, let's reset our location
checkLoc = l.clone();
// And increment the radius
loopcounter += 2;
}
return null;
}
private boolean checkAroundSpecificDiameter(Location checkLoc, int circle) {
// Adjust the circle to get how many blocks to step out.
// A radius of 3 makes the block step 1
// A radius of 5 makes the block step 2
// A radius of 7 makes the block step 3
// ...
int adjustedCircle = ((circle - 1) / 2);
checkLoc.add(adjustedCircle, 0, 0);
if (this.bs.playerCanSpawnHereSafely(checkLoc)) {
return true;
}
// Now we go to the right that adjustedCircle many
for (int i = 0; i < adjustedCircle; i++) {
checkLoc.add(0, 0, 1);
if (this.bs.playerCanSpawnHereSafely(checkLoc)) {
return true;
}
}
// Then down adjustedCircle *2
for (int i = 0; i < adjustedCircle * 2; i++) {
checkLoc.add(-1, 0, 0);
if (this.bs.playerCanSpawnHereSafely(checkLoc)) {
return true;
}
}
// Then left adjustedCircle *2
for (int i = 0; i < adjustedCircle * 2; i++) {
checkLoc.add(0, 0, -1);
if (this.bs.playerCanSpawnHereSafely(checkLoc)) {
return true;
}
}
// Then up Then left adjustedCircle *2
for (int i = 0; i < adjustedCircle * 2; i++) {
checkLoc.add(1, 0, 0);
if (this.bs.playerCanSpawnHereSafely(checkLoc)) {
return true;
}
}
// Then finish up by doing adjustedCircle - 1
for (int i = 0; i < adjustedCircle - 1; i++) {
checkLoc.add(0, 0, 1);
if (this.bs.playerCanSpawnHereSafely(checkLoc)) {
return true;
}
}
return false;
}
/**
* Safely teleport the entity to the MVDestination. This will perform checks to see if the place is safe, and if
* it's not, will adjust the final destination accordingly.
*
* @param teleporter Person who performed the teleport command.
* @param teleportee Entity to teleport
* @param d Destination to teleport them to
* @return true for success, false for failure
*/
public TeleportResult safelyTeleport(CommandSender teleporter, Entity teleportee, MVDestination d) {
if (d instanceof InvalidDestination) {
this.plugin.log(Level.FINER, "Entity tried to teleport to an invalid destination");
return TeleportResult.FAIL_INVALID;
}
Player teleporteePlayer = null;
if (teleportee instanceof Player) {
teleporteePlayer = ((Player) teleportee);
} else if (teleportee.getPassenger() instanceof Player) {
teleporteePlayer = ((Player) teleportee.getPassenger());
}
if (teleporteePlayer == null) {
return TeleportResult.FAIL_INVALID;
}
MultiverseCore.addPlayerToTeleportQueue(teleporter.getName(), teleporteePlayer.getName());
Location safeLoc = d.getLocation(teleportee);
if (d.useSafeTeleporter()) {
safeLoc = this.getSafeLocation(teleportee, d);
}
if (safeLoc != null) {
if (teleportee.teleport(safeLoc)) {
if (!d.getVelocity().equals(new Vector(0, 0, 0))) {
teleportee.setVelocity(d.getVelocity());
}
return TeleportResult.SUCCESS;
}
return TeleportResult.FAIL_OTHER;
}
return TeleportResult.FAIL_UNSAFE;
}
/**
* Safely teleport the entity to the Location. This may perform checks to
* see if the place is safe, and if
* it's not, will adjust the final destination accordingly.
*
* @param teleporter Person who issued the teleport command.
* @param teleportee Entity to teleport.
* @param location Location to teleport them to.
* @param safely Should the destination be checked for safety before teleport?
* @return true for success, false for failure.
*/
public TeleportResult safelyTeleport(CommandSender teleporter, Entity teleportee, Location location, boolean safely) {
if (safely) {
location = this.getSafeLocation(location);
}
if (location != null) {
if (teleportee.teleport(location)) {
return TeleportResult.SUCCESS;
}
return TeleportResult.FAIL_OTHER;
}
return TeleportResult.FAIL_UNSAFE;
}
/**
* Returns a safe location for the entity to spawn at.
*
* @param e The entity to spawn
* @param d The MVDestination to take the entity to.
* @return A new location to spawn the entity at.
*/
public Location getSafeLocation(Entity e, MVDestination d) {
Location l = d.getLocation(e);
if (this.bs.playerCanSpawnHereSafely(l)) {
plugin.log(Level.FINE, "The first location you gave me was safe.");
return l;
}
if (e instanceof Minecart) {
Minecart m = (Minecart) e;
if (!this.bs.canSpawnCartSafely(m)) {
return null;
}
} else if (e instanceof Vehicle) {
Vehicle v = (Vehicle) e;
if (!this.bs.canSpawnVehicleSafely(v)) {
return null;
}
}
Location safeLocation = this.getSafeLocation(l);
if (safeLocation != null) {
// Add offset to account for a vehicle on dry land!
if (e instanceof Minecart && !this.bs.isEntitiyOnTrack(safeLocation)) {
safeLocation.setY(safeLocation.getBlockY() + .5);
this.plugin.log(Level.FINER, "Player was inside a minecart. Offsetting Y location.");
}
this.plugin.log(Level.FINE, "Had to look for a bit, but I found a safe place for ya!");
return safeLocation;
}
if (e instanceof Player) {
Player p = (Player) e;
this.plugin.getMessaging().sendMessage(p, "No safe locations found!", false);
this.plugin.log(Level.FINER, "No safe location found for " + p.getName());
} else if (e.getPassenger() instanceof Player) {
Player p = (Player) e.getPassenger();
this.plugin.getMessaging().sendMessage(p, "No safe locations found!", false);
this.plugin.log(Level.FINER, "No safe location found for " + p.getName());
}
this.plugin.log(Level.FINE, "Sorry champ, you're basically trying to teleport into a minefield. I should just kill you now.");
return null;
}
/**
* Finds a portal-block next to the specified {@link Location}.
* @param l The {@link Location}
* @return The next portal-block's {@link Location}.
*/
public static Location findPortalBlockNextTo(Location l) {
Block b = l.getWorld().getBlockAt(l);
Location foundLocation = null;
if (b.getType() == Material.PORTAL) {
return l;
}
if (b.getRelative(BlockFace.NORTH).getType() == Material.PORTAL) {
foundLocation = getCloserBlock(l, b.getRelative(BlockFace.NORTH).getLocation(), foundLocation);
}
if (b.getRelative(BlockFace.SOUTH).getType() == Material.PORTAL) {
foundLocation = getCloserBlock(l, b.getRelative(BlockFace.SOUTH).getLocation(), foundLocation);
}
if (b.getRelative(BlockFace.EAST).getType() == Material.PORTAL) {
foundLocation = getCloserBlock(l, b.getRelative(BlockFace.EAST).getLocation(), foundLocation);
}
if (b.getRelative(BlockFace.WEST).getType() == Material.PORTAL) {
foundLocation = getCloserBlock(l, b.getRelative(BlockFace.WEST).getLocation(), foundLocation);
}
return foundLocation;
}
private static Location getCloserBlock(Location source, Location blockA, Location blockB) {
// If B wasn't given, return a.
if (blockB == null) {
return blockA;
}
// Center our calculations
blockA.add(.5, 0, .5);
blockB.add(.5, 0, .5);
// Retrieve the distance to the normalized blocks
double testA = source.distance(blockA);
double testB = source.distance(blockB);
// Compare and return
if (testA <= testB) {
return blockA;
}
return blockB;
}
}

View File

@ -14,9 +14,10 @@ import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.BlockFace;
import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.type.Bed;
import org.bukkit.entity.Minecart;
import org.bukkit.entity.Vehicle;
import org.bukkit.material.Bed;
import java.util.EnumSet;
import java.util.Iterator;
@ -89,7 +90,7 @@ public class SimpleBlockSafety implements BlockSafety {
return false;
}
if (downOne.getBlock().getType() == Material.LAVA || downOne.getBlock().getType() == Material.STATIONARY_LAVA) {
if (downOne.getBlock().getType() == Material.LAVA) {
Logging.finer("Error Here (downOne)? (%s)[%s]", downOne.getBlock().getType(), isSolidBlock(downOne.getBlock().getType()));
return false;
}
@ -151,12 +152,13 @@ public class SimpleBlockSafety implements BlockSafety {
* @return The location of the other bed piece, or null if it was a jacked up bed.
*/
private Location findOtherBedPiece(Location checkLoc) {
if (checkLoc.getBlock().getType() != Material.BED_BLOCK) {
BlockData data = checkLoc.getBlock().getBlockData();
if (!(data instanceof Bed)) {
return null;
}
// Construct a bed object at this location
final Bed b = new Bed(Material.BED_BLOCK, checkLoc.getBlock().getData());
if (b.isHeadOfBed()) {
Bed b = (Bed) data;
if (b.getPart() == Bed.Part.HEAD) {
return checkLoc.getBlock().getRelative(b.getFacing().getOppositeFace()).getLocation();
}
// We shouldn't ever be looking at the foot, but here's the code for it.
@ -199,69 +201,8 @@ public class SimpleBlockSafety implements BlockSafety {
/*
* If someone has a better way of this... Please either tell us, or submit a pull request!
*/
private static boolean isSolidBlock(Material type) {
switch (type) {
case AIR:
return false;
case SNOW:
return false;
case TRAP_DOOR:
return false;
case TORCH:
return false;
case YELLOW_FLOWER:
return false;
case RED_ROSE:
return false;
case RED_MUSHROOM:
return false;
case BROWN_MUSHROOM:
return false;
case REDSTONE:
return false;
case REDSTONE_WIRE:
return false;
case RAILS:
return false;
case POWERED_RAIL:
return false;
case REDSTONE_TORCH_ON:
return false;
case REDSTONE_TORCH_OFF:
return false;
case DEAD_BUSH:
return false;
case SAPLING:
return false;
case STONE_BUTTON:
return false;
case LEVER:
return false;
case LONG_GRASS:
return false;
case PORTAL:
return false;
case STONE_PLATE:
return false;
case WOOD_PLATE:
return false;
case SEEDS:
return false;
case SUGAR_CANE_BLOCK:
return false;
case WALL_SIGN:
return false;
case SIGN_POST:
return false;
case WOODEN_DOOR:
return false;
case STATIONARY_WATER:
return false;
case WATER:
return false;
default:
return true;
}
public static boolean isSolidBlock(Material type) {
return type.isSolid();
}
/**
@ -270,7 +211,10 @@ public class SimpleBlockSafety implements BlockSafety {
@Override
public boolean isEntitiyOnTrack(Location l) {
Material currentBlock = l.getBlock().getType();
return (currentBlock == Material.POWERED_RAIL || currentBlock == Material.DETECTOR_RAIL || currentBlock == Material.RAILS);
return (currentBlock == Material.POWERED_RAIL
|| currentBlock == Material.DETECTOR_RAIL
|| currentBlock == Material.RAIL
|| currentBlock == Material.ACTIVATOR_RAIL);
}
/**
@ -285,10 +229,10 @@ public class SimpleBlockSafety implements BlockSafety {
}
Location oneBelow = l.clone();
oneBelow.subtract(0, 1, 0);
if (oneBelow.getBlock().getType() == Material.WATER || oneBelow.getBlock().getType() == Material.STATIONARY_WATER) {
if (oneBelow.getBlock().getType() == Material.WATER) {
Location twoBelow = oneBelow.clone();
twoBelow.subtract(0, 1, 0);
return (oneBelow.getBlock().getType() == Material.WATER || oneBelow.getBlock().getType() == Material.STATIONARY_WATER);
return (oneBelow.getBlock().getType() == Material.WATER);
}
if (oneBelow.getBlock().getType() != Material.AIR) {
return false;

View File

@ -292,19 +292,19 @@ public class SimpleSafeTTeleporter implements SafeTTeleporter {
public Location findPortalBlockNextTo(Location l) {
Block b = l.getWorld().getBlockAt(l);
Location foundLocation = null;
if (b.getType() == Material.PORTAL) {
if (b.getType() == Material.NETHER_PORTAL) {
return l;
}
if (b.getRelative(BlockFace.NORTH).getType() == Material.PORTAL) {
if (b.getRelative(BlockFace.NORTH).getType() == Material.NETHER_PORTAL) {
foundLocation = getCloserBlock(l, b.getRelative(BlockFace.NORTH).getLocation(), foundLocation);
}
if (b.getRelative(BlockFace.SOUTH).getType() == Material.PORTAL) {
if (b.getRelative(BlockFace.SOUTH).getType() == Material.NETHER_PORTAL) {
foundLocation = getCloserBlock(l, b.getRelative(BlockFace.SOUTH).getLocation(), foundLocation);
}
if (b.getRelative(BlockFace.EAST).getType() == Material.PORTAL) {
if (b.getRelative(BlockFace.EAST).getType() == Material.NETHER_PORTAL) {
foundLocation = getCloserBlock(l, b.getRelative(BlockFace.EAST).getLocation(), foundLocation);
}
if (b.getRelative(BlockFace.WEST).getType() == Material.PORTAL) {
if (b.getRelative(BlockFace.WEST).getType() == Material.NETHER_PORTAL) {
foundLocation = getCloserBlock(l, b.getRelative(BlockFace.WEST).getLocation(), foundLocation);
}
return foundLocation;

View File

@ -0,0 +1,63 @@
package com.onarandombox.MultiverseCore;
import com.onarandombox.MultiverseCore.utils.TestInstanceCreator;
import org.bukkit.Material;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.java.JavaPluginLoader;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.*;
@RunWith(PowerMockRunner.class)
@PrepareForTest({ MultiverseCore.class, PluginDescriptionFile.class, JavaPluginLoader.class})
public class TestEntryFeeConversion {
private TestInstanceCreator creator;
private MultiverseCore core;
Map<String, Object> config;
Map<String, Object> entryFee;
@Before
public void setUp() {
creator = new TestInstanceCreator();
assertTrue(creator.setUp());
core = creator.getCore();
config = new HashMap<>();
entryFee = new HashMap<>();
config.put("entryfee", entryFee);
entryFee.put("==", "MVEntryFee");
}
@After
public void tearDown() {
creator.tearDown();
}
@Test
public void testConvertIntegerCurrencyToMaterialCurrency() {
entryFee.put("currency", -1);
WorldProperties props = new WorldProperties(config);
assertNull(props.entryfee.getCurrency());
entryFee.put("currency", 1);
props = new WorldProperties(config);
assertEquals(Material.STONE, props.entryfee.getCurrency());
entryFee.put("currency", "1");
props = new WorldProperties(config);
assertEquals(Material.STONE, props.entryfee.getCurrency());
entryFee.put("currency", "stone");
props = new WorldProperties(config);
assertEquals(Material.STONE, props.entryfee.getCurrency());
}
}

View File

@ -18,6 +18,7 @@ import org.bukkit.ChatColor;
import org.bukkit.Difficulty;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.WorldType;
import org.bukkit.command.Command;
@ -151,7 +152,7 @@ public class TestWorldProperties {
assertEquals(Difficulty.NORMAL, mvWorld.getDifficulty());
assertTrue(mvWorld.canAnimalsSpawn());
assertTrue(mvWorld.canMonstersSpawn());
assertEquals(-1, mvWorld.getCurrency());
assertNull(mvWorld.getCurrency());
assertEquals(0, mvWorld.getPrice(), 0);
assertTrue(mvWorld.getHunger());
assertTrue(mvWorld.getAutoHeal());
@ -233,8 +234,8 @@ public class TestWorldProperties {
assertEquals(false, mvWorld.canAnimalsSpawn());
mvWorld.setAllowMonsterSpawn(false);
assertEquals(false, mvWorld.canMonstersSpawn());
mvWorld.setCurrency(1);
assertEquals(1, mvWorld.getCurrency());
mvWorld.setCurrency(Material.STONE);
assertEquals(Material.STONE, mvWorld.getCurrency());
mvWorld.setPrice(1D);
assertEquals(1D, mvWorld.getPrice(), 0);
mvWorld.setHunger(false);
@ -332,7 +333,7 @@ public class TestWorldProperties {
assertEquals(Difficulty.PEACEFUL, mvWorld.getDifficulty());
assertEquals(false, mvWorld.canAnimalsSpawn());
assertEquals(false, mvWorld.canMonstersSpawn());
assertEquals(1, mvWorld.getCurrency());
assertEquals(Material.STONE, mvWorld.getCurrency());
assertEquals(1D, mvWorld.getPrice(), 0);
assertEquals(false, mvWorld.getHunger());
assertEquals(false, mvWorld.getAutoHeal());

View File

@ -126,7 +126,6 @@ public class MockWorldFactory {
}
when(mockBlock.getType()).thenReturn(blockType);
when(mockBlock.getTypeId()).thenReturn(blockType.getId());
when(mockBlock.getWorld()).thenReturn(loc.getWorld());
when(mockBlock.getX()).thenReturn(loc.getBlockX());
when(mockBlock.getY()).thenReturn(loc.getBlockY());
@ -170,7 +169,6 @@ public class MockWorldFactory {
Material blockType = Material.AIR;
when(mockBlock.getType()).thenReturn(blockType);
when(mockBlock.getTypeId()).thenReturn(blockType.getId());
when(mockBlock.getWorld()).thenReturn(loc.getWorld());
when(mockBlock.getX()).thenReturn(loc.getBlockX());
when(mockBlock.getY()).thenReturn(loc.getBlockY());