Fix colour targeting FINALLY!

This commit is contained in:
William Burns 2016-02-25 19:48:15 +00:00
parent dead7a0850
commit e4ba5d9723
3 changed files with 45 additions and 42 deletions

View File

@ -1,9 +1,9 @@
package mineplex.core.common.util;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
@ -14,7 +14,6 @@ import org.bukkit.ChatColor;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.block.Block;
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftEntity;
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
@ -25,14 +24,13 @@ import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryView;
import org.bukkit.potion.PotionEffect;
import org.bukkit.util.BlockIterator;
import org.bukkit.util.Vector;
import net.minecraft.server.v1_8_R3.EntityPlayer;
import net.minecraft.server.v1_8_R3.Packet;
import net.minecraft.server.v1_8_R3.PlayerConnection;
import org.apache.commons.lang3.Validate;
public class UtilPlayer
{
private static Random RANDOM = new Random();
@ -759,45 +757,31 @@ public class UtilPlayer
return player.getEyeLocation().clone().add(looking);
}
public static Block getTargetBlock(List<Material> ignore, Entity entity, int minDistance, int maxDistance)
public static Block getTarget(LivingEntity entity, HashSet<Byte> ignore, int maxDistance)
{
Location location = offset(entity, minDistance, false);
Iterator<Block> itr = new BlockIterator(entity, maxDistance);
Validate.isTrue(maxDistance > minDistance, "Max distance must be larger than min distance.");
for (int i = 0; i < maxDistance - minDistance; i++)
while (itr.hasNext())
{
Material type = location.getBlock().getType();
Block block = itr.next();
int id = block.getTypeId();
if (i + maxDistance >= maxDistance)
if (ignore == null)
{
break;
if (id != 0)
{
return block;
}
}
if (ignore != null && !ignore.contains(type) || ignore == null && !type.equals(Material.AIR))
else
{
break;
if (!ignore.contains((byte)id))
{
return block;
}
}
location = offset(entity, 1, false);
}
return location.getBlock();
}
private static Location offset(Entity entity, double strength, boolean reverse)
{
double x = entity.getLocation().getBlockX();
double y = entity.getLocation().getBlockY();
double z = entity.getLocation().getBlockZ();
double yaw = Math.toRadians(entity.getLocation().getYaw());
double pitch = Math.toRadians(entity.getLocation().getPitch());
x = reverse ? (x + strength * Math.sin(yaw)) : (x - strength * Math.sin(yaw));
y = reverse ? (y + strength * Math.sin(pitch)) : (y - strength * Math.sin(pitch));
z = reverse ? (z - strength * Math.cos(yaw)) : (z + strength * Math.cos(yaw));
return new Location(entity.getWorld(), x, y, z);
return null;
}
}

View File

@ -106,7 +106,7 @@ public class Draw extends SoloGame
new String[]
{
"Take turns to draw something",
"Right-Click with Swords to draw",
"Right-Click with items to draw",
"Hints are given at top of screen",
});
@ -587,7 +587,7 @@ public class Draw extends SoloGame
continue;
}
Block block = UtilPlayer.getTargetBlock(Arrays.asList(Material.BARRIER, Material.AIR), p, 50, 400);
Block block = UtilPlayer.getTarget(p, UtilBlock.blockPassSet, 400);
if (block == null || !_canvas.contains(block))
continue;
@ -646,7 +646,7 @@ public class Draw extends SoloGame
continue;
}
Block block = UtilPlayer.getTargetBlock(Arrays.asList(Material.BARRIER, Material.AIR), player, 50, 400);
Block block = UtilPlayer.getTarget(player, UtilBlock.blockPassSet, 400);
if (block == null || !_canvas.contains(block))
continue;
@ -760,7 +760,7 @@ public class Draw extends SoloGame
}
// Get the target block that the player clicks on.
Block target = UtilPlayer.getTargetBlock(Arrays.asList(Material.BARRIER, Material.AIR), p, 50, 400);
Block target = UtilPlayer.getTarget(p, UtilBlock.blockPassSet, 400);
if (target == null || !_canvas.contains(target))
{
@ -843,11 +843,30 @@ public class Draw extends SoloGame
if (!_drawers.HasPlayer(player))
return;
Block block = UtilPlayer.getTargetBlock(Arrays.asList(Material.BARRIER, Material.AIR), player, 50, 400);
Location loc = UtilPlayer.getTarget(player, UtilBlock.blockPassSet, 400).getLocation();
List<Block> possibleBlocks = UtilBlock.getInBoundingBox(WorldData.GetDataLocs("GREEN").get(0),
WorldData.GetDataLocs("GREEN").get(1));
Block block = player.getWorld().getBlockAt(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
if (block == null)
{
return;
}
for (Block other : possibleBlocks)
{
if (
other.getX() == block.getX()
&& other.getY() == block.getY()
&& other.getZ() == block.getZ()
)
{
block = other;
}
}
if (block == null || !possibleBlocks.contains(block))
return;

View File

@ -43,7 +43,7 @@ public abstract class Tool
if (!UtilEvent.isAction(event, ActionType.R))
return;
Block block = UtilPlayer.getTargetBlock(Arrays.asList(Material.BARRIER, Material.AIR), event.getPlayer(), 50, 400);
Block block = UtilPlayer.getTarget(event.getPlayer(), UtilBlock.blockPassSet, 400);
if (block == null)
return;
@ -76,7 +76,7 @@ public abstract class Tool
_new = new HashMap<Block, BlockInfo>();
//Calculate New
Block end = UtilPlayer.getTargetBlock(Arrays.asList(Material.BARRIER, Material.AIR), _drawer, 50, 400);
Block end = UtilPlayer.getTarget(_drawer, UtilBlock.blockPassSet, 400);
if (end != null && Host.getCanvas().contains(end))
{
customDraw(end);