Skip to content

Instantly share code, notes, and snippets.

@DarkSeraphim
Created January 24, 2013 01:40
Show Gist options
  • Save DarkSeraphim/4616864 to your computer and use it in GitHub Desktop.
Save DarkSeraphim/4616864 to your computer and use it in GitHub Desktop.
Artificial piston movement
public void powerPiston(Block b)
{
if(b == null)
{
return;
}
if(b.getType() != Material.PISTON_BASE && b.getType() != Material.PISTON_STICKY_BASE)
{
return;
}
BlockState state = b.getState();
PistonBaseMaterial pbm = (PistonBaseMaterial)state.getData();
BlockFace face = pbm.getFacing();
if(moveBlocks(b.getLocation(), face, 12))
{
pbm.setPowered(true);
state.setData(pbm);
state.update(true);
Block extention = b.getRelative(face, 1);
extention.setType(Material.PISTON_EXTENSION);
BlockState ext = extention.getState();
PistonExtensionMaterial extdat = (PistonExtensionMaterial)ext.getData();
extdat.setSticky(true);
extdat.setFacingDirection(face);
ext.setData(extdat);
ext.update(true);
}
}
private boolean moveBlocks(Location loc, BlockFace face, int distance)
{
BlockState[] newStates = new BlockState[distance];
Vector dir = new Vector(face.getModX(), face.getModY(), face.getModZ());
BlockIterator bi = new BlockIterator(loc.getWorld(), loc.toVector().add(dir), dir, 0, distance+1);
if(bi.hasNext())
{
Block current = bi.next();
int ip = 0;
boolean spacious = false;
while(bi.hasNext())
{
Block next = bi.next();
if(next.getTypeId() != 0 && ip == 12)
{
spacious = false;
break;
}
if(ip == 12)
{
break;
}
BlockState nextState = next.getState();
nextState.setType(current.getType());
nextState.setData(current.getState().getData());
newStates[ip] = nextState;
if(next.getTypeId() == 0)
{
spacious = true;
break;
}
current = next;
ip++;
}
if(spacious)
{
for(BlockState state : newStates)
{
if(state != null)
{
state.update(true);
}
}
return true;
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment