Use a different method for targeting blocks.

This commit is contained in:
William Burns 2016-02-25 16:09:00 +00:00
parent a97409b64a
commit dead7a0850
3 changed files with 54 additions and 6 deletions

View File

@ -15,6 +15,7 @@ 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;
import org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory;
@ -30,6 +31,8 @@ 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();
@ -755,4 +758,46 @@ public class UtilPlayer
looking.multiply(distance);
return player.getEyeLocation().clone().add(looking);
}
public static Block getTargetBlock(List<Material> ignore, Entity entity, int minDistance, int maxDistance)
{
Location location = offset(entity, minDistance, false);
Validate.isTrue(maxDistance > minDistance, "Max distance must be larger than min distance.");
for (int i = 0; i < maxDistance - minDistance; i++)
{
Material type = location.getBlock().getType();
if (i + maxDistance >= maxDistance)
{
break;
}
if (ignore != null && !ignore.contains(type) || ignore == null && !type.equals(Material.AIR))
{
break;
}
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);
}
}

View File

@ -587,7 +587,7 @@ public class Draw extends SoloGame
continue;
}
Block block = p.getTargetBlock(UtilBlock.blockPassSet, 400);
Block block = UtilPlayer.getTargetBlock(Arrays.asList(Material.BARRIER, Material.AIR), p, 50, 400);
if (block == null || !_canvas.contains(block))
continue;
@ -646,7 +646,7 @@ public class Draw extends SoloGame
continue;
}
Block block = player.getTargetBlock(UtilBlock.blockPassSet, 400);
Block block = UtilPlayer.getTargetBlock(Arrays.asList(Material.BARRIER, Material.AIR), player, 50, 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 = p.getTargetBlock(UtilBlock.blockPassSet, 400);
Block target = UtilPlayer.getTargetBlock(Arrays.asList(Material.BARRIER, Material.AIR), p, 50, 400);
if (target == null || !_canvas.contains(target))
{
@ -843,7 +843,7 @@ public class Draw extends SoloGame
if (!_drawers.HasPlayer(player))
return;
Block block = player.getTargetBlock(UtilBlock.blockPassSet, 400);
Block block = UtilPlayer.getTargetBlock(Arrays.asList(Material.BARRIER, Material.AIR), player, 50, 400);
List<Block> possibleBlocks = UtilBlock.getInBoundingBox(WorldData.GetDataLocs("GREEN").get(0),
WorldData.GetDataLocs("GREEN").get(1));

View File

@ -1,12 +1,14 @@
package nautilus.game.arcade.game.games.draw.tools;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import mineplex.core.common.util.UtilBlock;
import mineplex.core.common.util.UtilEvent;
import mineplex.core.common.util.UtilGear;
import mineplex.core.common.util.UtilPlayer;
import mineplex.core.common.util.UtilServer;
import mineplex.core.common.util.UtilEvent.ActionType;
import nautilus.game.arcade.game.games.draw.BlockInfo;
@ -40,7 +42,8 @@ public abstract class Tool
{
if (!UtilEvent.isAction(event, ActionType.R))
return;
Block block = event.getPlayer().getTargetBlock(UtilBlock.blockPassSet, 400);
Block block = UtilPlayer.getTargetBlock(Arrays.asList(Material.BARRIER, Material.AIR), event.getPlayer(), 50, 400);
if (block == null)
return;
@ -73,7 +76,7 @@ public abstract class Tool
_new = new HashMap<Block, BlockInfo>();
//Calculate New
Block end = _drawer.getTargetBlock(UtilBlock.blockPassSet, 400);
Block end = UtilPlayer.getTargetBlock(Arrays.asList(Material.BARRIER, Material.AIR), _drawer, 50, 400);
if (end != null && Host.getCanvas().contains(end))
{
customDraw(end);