Skip to content

Commit

Permalink
Ported block tracing code to WorldEdit.
Browse files Browse the repository at this point in the history
  • Loading branch information
sk89q committed Jan 19, 2011
1 parent 3e8b2ed commit 6f6a82d
Show file tree
Hide file tree
Showing 5 changed files with 222 additions and 680 deletions.
37 changes: 34 additions & 3 deletions src/com/sk89q/worldedit/LocalPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import com.sk89q.worldedit.bags.BlockBag;
import com.sk89q.worldedit.blocks.BlockType;
import com.sk89q.worldedit.util.TargetBlock;

/**
*
Expand Down Expand Up @@ -298,15 +299,21 @@ public WorldVector getBlockOn() {
* @param range
* @return point
*/
public abstract WorldVector getBlockTrace(int range);
public WorldVector getBlockTrace(int range) {
TargetBlock tb = new TargetBlock(this, range, 0.2);
return tb.getTargetBlock();
}

/**
* Get the point of the block being looked at. May return null.
*
* @param range
* @return point
*/
public abstract WorldVector getSolidBlockTrace(int range);
public WorldVector getSolidBlockTrace(int range) {
TargetBlock tb = new TargetBlock(this, range, 0.2);
return tb.getSolidTargetBlock();
}

/**
* Get the player's cardinal direction (N, W, NW, etc.). May return null.
Expand Down Expand Up @@ -418,7 +425,31 @@ private static LocalPlayer.DIRECTION getDirection(double rot) {
* @param range
* @return whether the player was pass through
*/
public abstract boolean passThroughForwardWall(int range);
public boolean passThroughForwardWall(int range) {
boolean foundNext = false;
int searchDist = 0;
TargetBlock hitBlox = new TargetBlock(this, range, 0.2);
LocalWorld world = getPosition().getWorld();
BlockWorldVector block;
while ((block = hitBlox.getNextBlock()) != null) {
searchDist++;
if (searchDist > 20) {
return false;
}
if (BlockType.canPassThrough(world.getBlockType(block))) {
if (foundNext) {
Vector v = new Vector(block.getX(), block.getY() - 1, block.getZ());
if (BlockType.canPassThrough(world.getBlockType(v))) {
setPosition(v.add(0.5, 0, 0.5));
return true;
}
}
} else {
foundNext = true;
}
}
return false;
}

/**
* Print a message.
Expand Down
14 changes: 14 additions & 0 deletions src/com/sk89q/worldedit/Vector.java
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,20 @@ public static Vector toBlockPoint(double x, double y, double z) {
(int)Math.floor(z));
}

/**
* Get a block point from a point.
*
* @param x
* @param y
* @param z
* @return point
*/
public BlockVector toBlockPoint() {
return new BlockVector((int)Math.floor(x),
(int)Math.floor(y),
(int)Math.floor(z));
}

/**
* Checks if another object is equivalent.
*
Expand Down
52 changes: 0 additions & 52 deletions src/com/sk89q/worldedit/bukkit/BukkitPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@
package com.sk89q.worldedit.bukkit;

import org.bukkit.*;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import com.sk89q.worldedit.*;
import com.sk89q.worldedit.bags.BlockBag;
import com.sk89q.worldedit.blocks.BlockType;

public class BukkitPlayer extends LocalPlayer {
private Player player;
Expand All @@ -37,29 +35,6 @@ public BukkitPlayer(WorldEditPlugin plugin, ServerInterface server, Player playe
this.player = player;
}

@Override
public WorldVector getBlockTrace(int range) {
TargetBlock tb = new TargetBlock(player, range, 0.2);
Block block = tb.getTargetBlock();
if (block == null) {
return null;
}
return new WorldVector(getWorld(), block.getX(), block.getY(), block.getZ());
}

@Override
public WorldVector getSolidBlockTrace(int range) {
TargetBlock tb = new TargetBlock(player, range, 0.2);
tb.reset();
while (tb.getNextBlock() != null
&& BlockType.canPassThrough(tb.getCurrentBlock().getTypeId()));
Block block = tb.getCurrentBlock();
if (block == null) {
return null;
}
return new WorldVector(getWorld(), block.getX(), block.getY(), block.getZ());
}

@Override
public int getItemInHand() {
ItemStack itemStack = player.getItemInHand();
Expand Down Expand Up @@ -94,33 +69,6 @@ public void giveItem(int type, int amt) {
// TODO: Make this actually give the item
}

@Override
public boolean passThroughForwardWall(int range) {
boolean foundNext = false;
int searchDist = 0;
TargetBlock hitBlox = new TargetBlock(player, range, 0.2);
LocalWorld world = getPosition().getWorld();
Block block;
while ((block = hitBlox.getNextBlock()) != null) {
searchDist++;
if (searchDist > 20) {
return false;
}
if (BlockType.canPassThrough(block.getTypeId())) {
if (foundNext) {
Vector v = new Vector(block.getX(), block.getY() - 1, block.getZ());
if (BlockType.canPassThrough(world.getBlockType(v))) {
setPosition(v.add(0.5, 0, 0.5));
return true;
}
}
} else {
foundNext = true;
}
}
return false;
}

@Override
public void printRaw(String msg) {
player.sendMessage(msg);
Expand Down
Loading

0 comments on commit 6f6a82d

Please sign in to comment.