Improved Earth Dynamic Sourcing

This commit is contained in:
nathank33 2015-06-05 23:31:02 -07:00
parent c56bd64be4
commit c446f320f5

View file

@ -2,6 +2,7 @@ package com.projectkorra.ProjectKorra.Utilities;
import java.util.HashMap; import java.util.HashMap;
import org.bukkit.Location;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -12,21 +13,18 @@ import com.projectkorra.ProjectKorra.earthbending.EarthMethods;
import com.projectkorra.ProjectKorra.waterbending.WaterMethods; import com.projectkorra.ProjectKorra.waterbending.WaterMethods;
/** /**
* BlockSource is a class that handles water and earth bending sources. * BlockSource is a class that handles water and earth bending sources. When a Player left clicks or
* When a Player left clicks or presses shift the update method is called * presses shift the update method is called which attempts to update the player's sources.
* which attempts to update the player's sources.
* *
* In this class ClickType refers to the way in which the source was selected. * In this class ClickType refers to the way in which the source was selected. For example, Surge
* For example, Surge has two different ways to select a source, one involving shift and * has two different ways to select a source, one involving shift and another involving left clicks.
* another involving left clicks.
*/ */
public class BlockSource { public class BlockSource {
public static enum BlockSourceType { public static enum BlockSourceType {
WATER, ICE, PLANT, EARTH, METAL, LAVA WATER, ICE, PLANT, EARTH, METAL, LAVA
} }
private static HashMap<Player, HashMap<BlockSourceType, HashMap<ClickType, BlockSourceInformation>>> playerSources = private static HashMap<Player, HashMap<BlockSourceType, HashMap<ClickType, BlockSourceInformation>>> playerSources = new HashMap<Player, HashMap<BlockSourceType, HashMap<ClickType, BlockSourceInformation>>>();
new HashMap<Player, HashMap<BlockSourceType, HashMap<ClickType, BlockSourceInformation>>>();
private static FileConfiguration config = ProjectKorra.plugin.getConfig(); private static FileConfiguration config = ProjectKorra.plugin.getConfig();
// The player should never need to grab source blocks from farther than this. // The player should never need to grab source blocks from farther than this.
private static double MAX_RANGE = config.getDouble("Abilities.Water.WaterManipulation.Range"); private static double MAX_RANGE = config.getDouble("Abilities.Water.WaterManipulation.Range");
@ -65,10 +63,10 @@ public class BlockSource {
// We need to handle lava differently, since getEarthSourceBlock doesn't account for // We need to handle lava differently, since getEarthSourceBlock doesn't account for
// lava. We should only select the lava source if it is closer than the earth. // lava. We should only select the lava source if it is closer than the earth.
Block lavaBlock = EarthMethods.getLavaSourceBlock(player, MAX_RANGE); Block lavaBlock = EarthMethods.getLavaSourceBlock(player, MAX_RANGE);
double earthDist = earthBlock != null ? double earthDist = earthBlock != null ? earthBlock.getLocation().distanceSquared(player.getLocation())
earthBlock.getLocation().distanceSquared(player.getLocation()) : Double.MAX_VALUE; : Double.MAX_VALUE;
double lavaDist = lavaBlock != null ? double lavaDist = lavaBlock != null ? lavaBlock.getLocation().distanceSquared(player.getLocation())
lavaBlock.getLocation().distanceSquared(player.getLocation()) : Double.MAX_VALUE; : Double.MAX_VALUE;
if (lavaBlock != null && lavaDist <= earthDist) { if (lavaBlock != null && lavaDist <= earthDist) {
putSource(player, null, BlockSourceType.EARTH, clickType); putSource(player, null, BlockSourceType.EARTH, clickType);
putSource(player, lavaBlock, BlockSourceType.LAVA, clickType); putSource(player, lavaBlock, BlockSourceType.LAVA, clickType);
@ -96,16 +94,15 @@ public class BlockSource {
} }
/** /**
* Access a block source information depending on a range and click type. * Access a block source information depending on a source and click type.
* *
* @param player the player that is trying to bend. * @param player the player that is trying to bend.
* @param range the maximum range to access the block.
* @param clickType the action that was performed to access the source, either * @param clickType the action that was performed to access the source, either
* ClickType.SHIFT_DOWN or ClickType.LEFT_CLICK. * ClickType.SHIFT_DOWN or ClickType.LEFT_CLICK.
* @return a valid bendable block, or null if none was found. * @return a valid bendable block, or null if none was found.
*/ */
public static BlockSourceInformation getBlockSourceInformation(Player player, double range, public static BlockSourceInformation getBlockSourceInformation(Player player, BlockSourceType sourceType,
BlockSourceType sourceType, ClickType clickType) { ClickType clickType) {
if (!playerSources.containsKey(player)) { if (!playerSources.containsKey(player)) {
return null; return null;
} else if (!playerSources.get(player).containsKey(sourceType)) { } else if (!playerSources.get(player).containsKey(sourceType)) {
@ -113,7 +110,22 @@ public class BlockSource {
} else if (!playerSources.get(player).get(sourceType).containsKey(clickType)) { } else if (!playerSources.get(player).get(sourceType).containsKey(clickType)) {
return null; return null;
} }
BlockSourceInformation blockInfo = playerSources.get(player).get(sourceType).get(clickType); return playerSources.get(player).get(sourceType).get(clickType);
}
/**
* Access a block source information depending on a range, source, and click type.
*
* @param player the player that is trying to bend.
* @param range the maximum range to access the block.
* @param sourceType the elemental type of block to find.
* @param clickType the action that was performed to access the source, either
* ClickType.SHIFT_DOWN or ClickType.LEFT_CLICK.
* @return a valid bendable block, or null if none was found.
*/
public static BlockSourceInformation getValidBlockSourceInformation(Player player, double range, BlockSourceType sourceType,
ClickType clickType) {
BlockSourceInformation blockInfo = getBlockSourceInformation(player, sourceType, clickType);
return isStillAValidSource(blockInfo, range, clickType) ? blockInfo : null; return isStillAValidSource(blockInfo, range, clickType) ? blockInfo : null;
} }
@ -122,12 +134,13 @@ public class BlockSource {
* *
* @param player the player that is trying to bend. * @param player the player that is trying to bend.
* @param range the maximum range to access the block. * @param range the maximum range to access the block.
* @param sourceType the elemental type of block to find.
* @param clickType the action that was performed to access the source, either * @param clickType the action that was performed to access the source, either
* ClickType.SHIFT_DOWN or ClickType.LEFT_CLICK. * ClickType.SHIFT_DOWN or ClickType.LEFT_CLICK.
* @return a valid bendable block, or null if none was found. * @return a valid bendable block, or null if none was found.
*/ */
public static Block getSourceBlock(Player player, double range, BlockSourceType sourceType, ClickType clickType) { public static Block getSourceBlock(Player player, double range, BlockSourceType sourceType, ClickType clickType) {
BlockSourceInformation info = getBlockSourceInformation(player, range, sourceType, clickType); BlockSourceInformation info = getValidBlockSourceInformation(player, range, sourceType, clickType);
return info != null ? info.getBlock() : null; return info != null ? info.getBlock() : null;
} }
@ -201,15 +214,16 @@ public class BlockSource {
* @param allowWater true if water blocks are allowed. * @param allowWater true if water blocks are allowed.
* @param allowIce true if ice blocks are allowed. * @param allowIce true if ice blocks are allowed.
* @param allowPlant true if plant blocks are allowed. * @param allowPlant true if plant blocks are allowed.
* @param allowWaterBottles true if we should look for a close water block, that may have * @param allowWaterBottles true if we should look for a close water block, that may have been
* been created by a WaterBottle. * created by a WaterBottle.
* @return a valid Water bendable block, or null if none was found. * @return a valid Water bendable block, or null if none was found.
*/ */
public static Block getWaterSourceBlock(Player player, double range, ClickType clickType, boolean allowWater, public static Block getWaterSourceBlock(Player player, double range, ClickType clickType, boolean allowWater,
boolean allowIce, boolean allowPlant, boolean allowWaterBottles) { boolean allowIce, boolean allowPlant, boolean allowWaterBottles) {
Block sourceBlock = null; Block sourceBlock = null;
if (allowWaterBottles) { if (allowWaterBottles) {
// Check the block in front of the player's eyes, it may have been created by a WaterBottle. // Check the block in front of the player's eyes, it may have been created by a
// WaterBottle.
sourceBlock = WaterMethods.getWaterSourceBlock(player, range, allowPlant); sourceBlock = WaterMethods.getWaterSourceBlock(player, range, allowPlant);
if (sourceBlock == null || sourceBlock.getLocation().distance(player.getEyeLocation()) > 3) { if (sourceBlock == null || sourceBlock.getLocation().distance(player.getEyeLocation()) > 3) {
sourceBlock = null; sourceBlock = null;
@ -238,7 +252,45 @@ public class BlockSource {
* @return a valid Earth bendable block, or null if none was found. * @return a valid Earth bendable block, or null if none was found.
*/ */
public static Block getEarthSourceBlock(Player player, double range, ClickType clickType) { public static Block getEarthSourceBlock(Player player, double range, ClickType clickType) {
return getSourceBlock(player, range, BlockSourceType.EARTH, clickType); return getEarthSourceBlock(player, range, clickType, true);
}
/**
* Attempts to access a Earth bendable block that was recently shifted or clicked on by the
* player.
*
* @param player the player that is trying to bend.
* @param range the maximum range to access the block.
* @param clickType the action that was performed to access the source, either
* ClickType.SHIFT_DOWN or ClickType.LEFT_CLICK.
* @param allowNearbySubstitute if a valid earth source could not be found then this method will
* attempt to find a nearby valid earth block.
* @return a valid Earth bendable block, or null if none was found.
*/
public static Block getEarthSourceBlock(Player player, double range, ClickType clickType,
boolean allowNearbySubstitute) {
Block sourceBlock = getSourceBlock(player, range, BlockSourceType.EARTH, clickType);
if (sourceBlock == null && allowNearbySubstitute) {
BlockSourceInformation blockInfo = getBlockSourceInformation(player,
BlockSourceType.EARTH, clickType);
if (blockInfo == null) {
return null;
}
Block tempBlock = blockInfo.getBlock();
if (tempBlock == null) {
return null;
}
Location loc = tempBlock.getLocation();
sourceBlock = EarthMethods.getNearbyEarthBlock(loc, 3, 3);
if (sourceBlock == null
|| Math.abs(sourceBlock.getLocation().distance(player.getEyeLocation())) > range
|| !EarthMethods.isEarthbendable(player, sourceBlock)) {
return null;
}
}
return sourceBlock;
} }
/** /**
@ -256,8 +308,8 @@ public class BlockSource {
} }
/** /**
* Attempts to access a Lava bendable block or an Earth block that was recently shifted or clicked on by the * Attempts to access a Lava bendable block or an Earth block that was recently shifted or
* player. * clicked on by the player.
* *
* @param player the player that is trying to bend. * @param player the player that is trying to bend.
* @param range the maximum range to access the block. * @param range the maximum range to access the block.
@ -266,19 +318,18 @@ public class BlockSource {
* @return a valid Earth or Lava bendable block, or null if none was found. * @return a valid Earth or Lava bendable block, or null if none was found.
*/ */
public static Block getEarthOrLavaSourceBlock(Player player, double range, ClickType clickType) { public static Block getEarthOrLavaSourceBlock(Player player, double range, ClickType clickType) {
BlockSourceInformation earthBlockInfo = getBlockSourceInformation(player, range, BlockSourceType.EARTH, clickType); /*
BlockSourceInformation lavaBlockInfo = getBlockSourceInformation(player, range, BlockSourceType.LAVA, clickType); * When Lava is selected as a source it automatically overrides the previous Earth based source.
if (earthBlockInfo == null && lavaBlockInfo == null) { * Only one of these types can exist, so if Lava exists then we know Earth is null.
return null; */
} else if (earthBlockInfo == null && lavaBlockInfo != null) { Block earthBlock = getEarthSourceBlock(player, range, clickType);
return lavaBlockInfo.getBlock(); BlockSourceInformation lavaBlockInfo = getValidBlockSourceInformation(player, range, BlockSourceType.LAVA, clickType);
} else if (earthBlockInfo != null && lavaBlockInfo == null) { if (earthBlock != null) {
return earthBlockInfo.getBlock(); return earthBlock;
} else if (earthBlockInfo.getCreationTime() <= lavaBlockInfo.getCreationTime()) { } else if (lavaBlockInfo != null) {
return earthBlockInfo.getBlock();
} else {
return lavaBlockInfo.getBlock(); return lavaBlockInfo.getBlock();
} }
return null;
} }
/** /**