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

252 lines
8.5 KiB
Java
Raw Normal View History

2017-01-08 23:21:15 +01:00
package de.Ste3et_C0st.Furniture.Objects.RPG;
2017-03-09 19:25:40 +01:00
import java.util.Arrays;
2017-01-08 23:21:15 +01:00
import java.util.HashMap;
2017-03-09 19:25:40 +01:00
import java.util.List;
import java.util.Random;
2017-01-08 23:21:15 +01:00
import org.bukkit.Bukkit;
2017-03-09 19:25:40 +01:00
import org.bukkit.DyeColor;
2017-01-08 23:21:15 +01:00
import org.bukkit.EntityEffect;
import org.bukkit.Location;
import org.bukkit.Material;
2017-03-09 19:25:40 +01:00
import org.bukkit.Sound;
2017-01-08 23:21:15 +01:00
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
2018-11-25 17:59:55 +01:00
import org.bukkit.block.data.BlockData;
2017-03-09 19:25:40 +01:00
import org.bukkit.entity.Creeper;
2017-01-08 23:21:15 +01:00
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.FallingBlock;
2017-03-09 19:25:40 +01:00
import org.bukkit.entity.LivingEntity;
2017-01-08 23:21:15 +01:00
import org.bukkit.entity.Player;
2017-03-09 19:25:40 +01:00
import org.bukkit.entity.Sheep;
2017-01-08 23:21:15 +01:00
import org.bukkit.entity.TNTPrimed;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
2017-03-09 19:25:40 +01:00
import org.bukkit.event.entity.EntityChangeBlockEvent;
2017-01-08 23:21:15 +01:00
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.inventory.ItemStack;
2017-03-09 19:25:40 +01:00
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
2017-01-08 23:21:15 +01:00
import org.bukkit.util.EulerAngle;
import org.bukkit.util.Vector;
import de.Ste3et_C0st.Furniture.Main.main;
2017-03-09 19:25:40 +01:00
import de.Ste3et_C0st.FurnitureLib.NBT.CraftItemStack;
import de.Ste3et_C0st.FurnitureLib.NBT.NBTTagCompound;
2017-01-09 23:09:37 +01:00
import de.Ste3et_C0st.FurnitureLib.ShematicLoader.Events.ProjectClickEvent;
2017-01-08 23:21:15 +01:00
import de.Ste3et_C0st.FurnitureLib.main.FurnitureHelper;
import de.Ste3et_C0st.FurnitureLib.main.FurnitureLib;
import de.Ste3et_C0st.FurnitureLib.main.ObjectID;
import de.Ste3et_C0st.FurnitureLib.main.Type.SQLAction;
import de.Ste3et_C0st.FurnitureLib.main.entity.fArmorStand;
import de.Ste3et_C0st.FurnitureLib.main.entity.fEntity;
public class Catapult extends FurnitureHelper implements Listener{
private fArmorStand stand1;
private HashMap<Entity, Player> fallingSandList = new HashMap<Entity, Player>();
2017-03-09 19:25:40 +01:00
private List<EntityType> entityList = Arrays.asList(EntityType.PIG, EntityType.CREEPER, EntityType.COW);
2017-01-08 23:21:15 +01:00
public Catapult(ObjectID id) {
super(id);
Bukkit.getPluginManager().registerEvents(this, main.instance);
for(fEntity stand : id.getPacketList()){
if(stand.getCustomName().toLowerCase().startsWith("#range")){
stand1 = (fArmorStand) stand;
stand1.setNameVasibility(false);
if(stand1.getCustomName().equalsIgnoreCase("#range")){
stand1.setName("#range:1");
}
}
}
}
@EventHandler
public void onPrimedTnt(EntityExplodeEvent e){
if(e.getEntity()==null) return;
if(e.getEntity() instanceof TNTPrimed){
if(fallingSandList.containsKey(e.getEntity())){
Entity entity = e.getEntity();
Player p = fallingSandList.get(e.getEntity());
fallingSandList.remove(e.getEntity());
Block b = entity.getLocation().getBlock().getRelative(BlockFace.DOWN);
if(!FurnitureLib.getInstance().getPermManager().canBuild(p, b.getLocation())){
e.setCancelled(true);
e.blockList().clear();
e.getEntity().remove();
}
}
}
}
2017-03-09 19:25:40 +01:00
@EventHandler
public void onFallingSand(EntityChangeBlockEvent e){
if(e.getEntity()==null) return;
if(e.getEntity() instanceof FallingBlock){
if(fallingSandList.containsKey(e.getEntity())){
Entity entity = e.getEntity();
Player p = fallingSandList.get(e.getEntity());
fallingSandList.remove(e.getEntity());
Block b = entity.getLocation().getBlock().getRelative(BlockFace.DOWN);
if(!FurnitureLib.getInstance().getPermManager().canBuild(p, b.getLocation())){
e.setCancelled(true);
e.getEntity().remove();
}
}
}
}
2017-01-08 23:21:15 +01:00
@EventHandler
2017-01-09 23:09:37 +01:00
public void FurnitureClickEvent(ProjectClickEvent e){
2017-01-08 23:21:15 +01:00
if(getObjID()==null) return;
if(e.getID()==null) return;
if(!e.getID().equals(getObjID())) return;
if(e.getID().getSQLAction().equals(SQLAction.REMOVE)) return;
Location loc = getRelative(getCenter(), getBlockFace(), -.8, -1.03).add(0, -.2, 0);
loc.setYaw(getYaw());
ItemStack stack = e.getPlayer().getInventory().getItemInMainHand();
if(stack.getType().equals(Material.TNT)){
TNTPrimed tnt = (TNTPrimed) e.getID().getWorld().spawnEntity(loc, EntityType.PRIMED_TNT);
if(tnt == null) return;
Vector v= getLaunchVector(getBlockFace());
if(v == null) return;
tnt.playEffect(EntityEffect.WITCH_MAGIC);
tnt.setVelocity(v.multiply(1));
fallingSandList.put(tnt, e.getPlayer());
}else if(stack.getType().equals(Material.AIR)){
2017-03-09 19:25:40 +01:00
setRange(e.getPlayer());
return;
2017-01-08 23:21:15 +01:00
}else{
2018-11-25 17:59:55 +01:00
// if(stack.getType().isBlock()){
// @SuppressWarnings("deprecation")
// FallingBlock block = getWorld().spawnFallingBlock(loc, stack.getType().getId(), (byte) stack.getDurability());
// if(block == null) return;
// Vector v= getLaunchVector(getBlockFace());
// if(v == null) return;
// block.setDropItem(false);
// block.setVelocity(v.multiply(1));
// fallingSandList.put(block, e.getPlayer());
// e.getPlayer().playSound(getLocation(), Sound.ENTITY_ARROW_SHOOT, 1, (float) getPitch());
// }else if(stack.getType().equals(Material.MONSTER_EGG)){
// try{
// NBTTagCompound nbtTag = new CraftItemStack().getNBTTag(stack);
// if(nbtTag.hasKey("tag")){
// NBTTagCompound tag = nbtTag.getCompound("tag");
// NBTTagCompound id = tag.getCompound("EntityTag");
// String str = id.getString("id").replace("minecraft:", "");
// @SuppressWarnings("deprecation")
// EntityType type = EntityType.fromName(str);
// if(!entityList.contains(type)){setRange(e.getPlayer());return;}
// Vector v= getLaunchVector(getBlockFace());
// Entity entity = getWorld().spawnEntity(loc, type);
// entity.setVelocity(v);
// LivingEntity entiLivingEntity = (LivingEntity) entity;
// entiLivingEntity.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, Integer.MAX_VALUE, 4), false);
// if(type.equals(EntityType.CREEPER)){
// if(randInt(0, 25)==7){
// Creeper creeper = (Creeper) entity;
// creeper.setPowered(true);
// }
// }else if(type.equals(EntityType.SHEEP)){
// Sheep mob = (Sheep) entity;
// int i = randInt(0, DyeColor.values().length);
// mob.setColor(DyeColor.values()[i]);
// if(randInt(0, 25)==7){
// mob.setCustomName("jeb_");
// mob.setCustomNameVisible(false);
// }
// }
// }
// e.getPlayer().playSound(getLocation(), Sound.ENTITY_ARROW_SHOOT, 1, (float) getPitch());
// return;
// }catch(Exception ex){
// ex.printStackTrace();
// }
// }else{
2017-03-09 19:25:40 +01:00
setRange(e.getPlayer());
return;
2018-11-25 17:59:55 +01:00
// }
2017-01-08 23:21:15 +01:00
}
2017-01-09 23:09:37 +01:00
consumeItem(e.getPlayer());
2017-01-08 23:21:15 +01:00
}
2017-03-09 19:25:40 +01:00
public void setRange(Player p){
if(stand1!=null){
int x = -59;
String name = "#range:1";
switch (stand1.getName().substring(stand1.getName().length()-1, stand1.getName().length())) {
case "1":
name = "#range:2";
x -= 4;
break;
case "2":
name = "#range:3";
x -= 8;
break;
case "3":
name = "#range:4";
x -= 12;
break;
case "4":
name = "#range:5";
x -= 16;
break;
default: break;
}
stand1.setName(name);
EulerAngle angle = stand1.getLeftArmPose();
angle = getLutil().Radtodegress(angle);
angle = angle.setX(x);
stand1.setLeftArmPose(getLutil().degresstoRad(angle));
update();
p.playSound(getLocation(), Sound.UI_BUTTON_CLICK, 1, (float) getPitch());
}
}
public static int randInt(int min, int max) {
Random rand = new Random();
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
public double getPitch(){
String str = "#range:1";
if(this.stand1!=null){str = stand1.getName().toLowerCase();}
str = str.replace("#range:", "");
if(str.equalsIgnoreCase("1")){
return .1;
}else if(str.equalsIgnoreCase("2")){
return .4;
}else if(str.equalsIgnoreCase("3")){
return .6;
}else if(str.equalsIgnoreCase("4")){
return .7;
}else if(str.equalsIgnoreCase("5")){
return .8;
}
return .1;
}
2017-01-08 23:21:15 +01:00
public Vector getLaunchVector(BlockFace face){
String str = "#range:1";
if(this.stand1!=null){str = stand1.getName().toLowerCase();}
str = str.replace("#range:", "");
String level = "Range" + str;
Vector v = new Vector(0, .5, 1.2);
if(main.catapultRange.containsKey(level)) v = main.catapultRange.get(level);
switch (face) {
case NORTH:v= new Vector(0, v.getY(), v.getZ());break;
case SOUTH:v= new Vector(0, v.getY(), -v.getZ());break;
case EAST: v= new Vector(-v.getZ(), v.getY(), 0);break;
case WEST: v= new Vector(v.getZ(), v.getY(), 0);break;
default:break;
}
return v;
}
}