furniture/src/de/Ste3et_C0st/Furniture/Objects/RPG/Crossbow.java

135 lines
3.7 KiB
Java
Raw Normal View History

package de.Ste3et_C0st.Furniture.Objects.RPG;
2017-03-09 19:25:40 +01:00
import java.util.Random;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Sound;
2017-03-09 19:25:40 +01:00
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Arrow;
2017-03-09 19:25:40 +01:00
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.Vector;
2017-03-09 19:25:40 +01:00
import de.Ste3et_C0st.FurnitureLib.main.FurnitureHelper;
import de.Ste3et_C0st.FurnitureLib.main.ObjectID;
import de.Ste3et_C0st.FurnitureLib.main.Type.SQLAction;
2016-06-21 19:17:24 +02:00
import de.Ste3et_C0st.FurnitureLib.main.entity.fEntity;
public class Crossbow extends FurnitureHelper {
2015-11-18 18:04:31 +01:00
public Crossbow(ObjectID id){
super(id);
}
@Override
public void onClick(Player player){
if(getObjID() == null) return;
if(getObjID().getSQLAction().equals(SQLAction.REMOVE)) return;
if(player == null) return;
if(canBuild(player)) {
fEntity stand = getArmorStand();
if(stand==null){return;}
ItemStack is = player.getInventory().getItemInMainHand();
if(is!=null&& (is.getType().equals(Material.ARROW) || is.getType().equals(Material.SPECTRAL_ARROW) || is.getType().equals(Material.TIPPED_ARROW)) ){
if(!hasArrow()){
fEntity entity = getArmorStand();
entity.setItemInMainHand(is.clone());
update();
consumeItem(player);
return;
}
}
if(hasArrow()){
spawnArrow(getArrow().getType(), player);
2017-03-09 19:25:40 +01:00
}
}
}
2017-03-09 19:25:40 +01:00
private void spawnArrow(Material mat, Player p){
Location loc = getRelative(getCenter(), getBlockFace(), 0,18);
loc.setYaw(getYaw());
2017-03-09 19:25:40 +01:00
Vector v= getLaunchVector(getBlockFace());
2017-01-08 23:21:15 +01:00
if(v == null) return;
getWorld().playSound(getLocation(), Sound.ENTITY_ARROW_SHOOT, 1, 1);
Location start = getRelative(getCenter(), getBlockFace(), 0,0);
start.setYaw(getYaw());
start = start.add(0, 1.8, 0);
2017-03-09 19:25:40 +01:00
Arrow a = null;
if(mat.equals(Material.ARROW)){
a = (Arrow) getWorld().spawnEntity(start, EntityType.ARROW);
}
a.setCritical(true);
2017-03-09 19:25:40 +01:00
a.setVelocity(v);
a.setShooter(p);
fEntity entity = getArmorStand();
entity.setItemInMainHand(null);
update();
}
2017-03-09 19:25:40 +01:00
public Vector getLaunchVector(BlockFace face){
2017-10-28 18:54:21 +02:00
int l =random(300, 150);
double h = ((double) l / 100);
2017-03-09 19:25:40 +01:00
2017-10-28 18:54:21 +02:00
Vector v = new Vector(0, 0, h);
2017-03-09 19:25:40 +01:00
switch (face) {
case SOUTH:v= new Vector(-v.getY(), v.getY(), -v.getZ());break;
case EAST: v= new Vector(-v.getZ(), v.getY(), -v.getY());break;
case WEST: v= new Vector(v.getZ(), v.getY(), v.getY());break;
default:break;
}
2017-03-09 19:25:40 +01:00
return v;
}
2017-03-09 19:25:40 +01:00
public int random(int max, int min){
Random r = new Random();
int randInt = r.nextInt(max-min) + min;
return randInt;
}
2016-06-21 19:17:24 +02:00
private fEntity getArmorStand(){
for(fEntity stand : getfAsList()){
if(stand.getName().equalsIgnoreCase("#ARROW#")){
return stand;
}
}
return null;
}
private ItemStack getArrow(){
2016-06-21 19:17:24 +02:00
for(fEntity stand : getfAsList()){
if(stand.getName().equalsIgnoreCase("#ARROW#")){
if(!(stand.getItemInMainHand()==null||stand.getItemInMainHand().getType()==null||stand.getItemInMainHand().getType().equals(Material.AIR))){
return stand.getItemInMainHand();
}
}
}
return null;
}
private boolean hasArrow(){
2016-06-21 19:17:24 +02:00
for(fEntity stand : getfAsList()){
if(stand.getName().equalsIgnoreCase("#ARROW#")){
if(stand.getItemInMainHand()==null||stand.getItemInMainHand().getType()==null||stand.getItemInMainHand().getType().equals(Material.AIR)){
return false;
}else{
return true;
}
}
}
return false;
}
@Override
public void onBreak(Player player) {
if(getObjID() == null) return;
if(getObjID().getSQLAction().equals(SQLAction.REMOVE)) return;
if(player == null) return;
if(canBuild(player)) {
this.destroy(player);
}
}
}