fixed catapult state being mapped to incorrect blocks

This commit is contained in:
NewGarbo 2016-01-28 09:18:58 +00:00
parent 7f7e73a101
commit 682a243c31
8 changed files with 234 additions and 101 deletions

View File

@ -170,7 +170,7 @@ public class UtilMath
int index = 0;
for (N number : values)
{
if (closestIndex == -1 || (Math.abs(value.doubleValue() - number.doubleValue()) < Math.abs(value.doubleValue() - values.get(closestIndex).doubleValue())))
if (closestIndex == -1 || (Math.abs(number.doubleValue() - value.doubleValue()) < Math.abs(values.get(closestIndex).doubleValue() - value.doubleValue())))
{
closestIndex = index;
}

View File

@ -79,8 +79,6 @@ public class Cannon implements Listener
// Entity Information
private List<Entity> _entities;
private List<CannonCrater> _craters;
private TNTPrimed _projectile;
private Inventory _inventory;
@ -93,7 +91,6 @@ public class Cannon implements Listener
_state = CannonState.UNLOADED;
_entities = new ArrayList<>();
_craters = new ArrayList<>();
_inventory = UtilServer.getServer().createInventory(null, InventoryType.DISPENSER, C.cRedB + "Cannon");
@ -103,10 +100,8 @@ public class Cannon implements Listener
public void cleanup()
{
_entities.forEach(Entity::remove);
_craters.forEach(CannonCrater::cleanup);
_entities.clear();
_craters.clear();
HandlerList.unregisterAll(this);
}
@ -243,7 +238,7 @@ public class Cannon implements Listener
{
//Do Explosion;
_craters.add(new CannonCrater(this, UtilBlock.nearestFloor(_projectile.getLocation())));
new CannonCrater(this, UtilBlock.nearestFloor(_projectile.getLocation()));
UtilServer.getServer().getOnlinePlayers().forEach(player -> player.playSound(_projectile.getLocation(), Sound.EXPLODE, 1.f, 1.f));
@ -253,21 +248,6 @@ public class Cannon implements Listener
}
}
private List<Vector> circleAround(Vector origin, int points, double radius)
{
List<Vector> list = new LinkedList<>();
double slice = 2 * Math.PI / points;
for (int point = 0; point < points; point++)
{
double angle = slice * point;
list.add(new Vector(origin.getX() + radius * Math.cos(angle), 0, origin.getZ() + radius * Math.sin(angle)));
}
return list;
}
private void attemptFire()
{
if (canBeFired())
@ -532,11 +512,6 @@ public class Cannon implements Listener
double yaw = standYaw + Math.min(dif / 20.f, 5f);
Location pos = armorStand.getLocation();
pos.setYaw((float) yaw);
armorStand.teleport(pos);
armorStand.setHeadPose(new EulerAngle(0, Math.toRadians(yaw), 0));
}
}

View File

@ -81,6 +81,10 @@ public class CannonCrater implements Listener
UtilParticle.PlayParticleToAll(ParticleType.FLAME, block.Location.clone().add(.5, 1, .5), new Vector(0, 0, 0), .0f, 3, ViewDist.MAX);
UtilParticle.PlayParticleToAll(lifetime >= max / 1.4 ? ParticleType.SMOKE : ParticleType.LARGE_SMOKE, block.Location.clone().add(.5, .8, .5), new Vector(0, 0, 0), .0f, 1, ViewDist.MAX);
}
else
{
HandlerList.unregisterAll(this);
}
});
}
@ -155,9 +159,4 @@ public class CannonCrater implements Listener
_blocks.forEach(CraterBlock::set);
}
protected void cleanup()
{
HandlerList.unregisterAll(this);
}
}

View File

@ -8,7 +8,6 @@ import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftArmorStand;
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftChicken;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
@ -64,13 +63,14 @@ public class Catapult implements Listener
public static final List<Integer> EMPTY_SLOTS = Arrays.asList(0, 2, 6, 8);
private static final double[] VERT_POWER = { 0.9, 1.25631, 1.5382 };
private static final double[] HORIZ_POWER = { 0.8, 1.1241, 1.34231 };
private static final double[] HORIZ_POWER = { 1.2, 1.6241, 1.9231 };
private List<Float> _rotationPoints;
private Location _location;
private CatapultState _state;
private float _curYaw;
private float _nextYaw;
// Entity Information
@ -92,9 +92,9 @@ public class Catapult implements Listener
_rotationPoints = new ArrayList<>();
_entities = new ArrayList<>();
for (int i = 0; i < 8; i++)
for (int i = 0; i < 20; i++)
{
_rotationPoints.add((360.f / 8.f) * i);
_rotationPoints.add((360.f / 20.f) * i);
}
_inventory = UtilServer.getServer().createInventory(null, InventoryType.DISPENSER, C.cRedB + "Catapult");
@ -125,8 +125,6 @@ public class Catapult implements Listener
armorStand.setGravity(false);
armorStand.setHelmet(_state.toItemStack());
((CraftChicken) armorStand).getHandle().setSize(1.87F, 1.87F);
_entities.add(armorStand);
}
else
@ -142,7 +140,7 @@ public class Catapult implements Listener
private Location getArmorStandLocation()
{
return _location.clone().add(.5, -.95, .5);
return _location.clone().add(.5, -.5, .5);
}
private void setState(CatapultState state)
@ -153,7 +151,7 @@ public class Catapult implements Listener
public void fire()
{
CatapultProjectile projectile = new CatapultProjectile(_location.clone().add(.5, .3, .5), Material.COBBLESTONE);
CatapultProjectile projectile = new CatapultProjectile(this, _location.clone().add(.5, .3, .5), Material.COBBLESTONE, (float) Math.toDegrees(getArmorStand().getHeadPose().getY()));
_projectile = projectile;
@ -165,7 +163,7 @@ public class Catapult implements Listener
UtilAlg.moveForward(
getArmorStand().getLocation(),
2.,
(float) Math.toDegrees(getArmorStand().getHeadPose().getY()), false))
(float) Math.toDegrees(getArmorStand().getHeadPose().getY()), true))
.multiply(hMulti)
.setY(vVel);
@ -204,16 +202,13 @@ public class Catapult implements Listener
{
if (_projectile != null)
{
UtilParticle.PlayParticleToAll(ParticleType.LARGE_SMOKE, _projectile.getLocation().add(0, .5, 0), new Vector(0, 0, 0), .1f, 3, ViewDist.MAX);
if ((Math.abs(_projectile.getVelocity().getX()) < 0.01 || Math.abs(_projectile.getVelocity().getZ()) < 0.01) && UtilBlock.getInRadius(_projectile.getLocation(), 2).keySet().stream().filter(block -> !UtilItem.isBoundless(block.getType())).iterator().hasNext() && _projectile.getTicksLived() >= 10)
if (_projectile.dead())
{
//Do Explosion;
UtilServer.getServer().getOnlinePlayers().forEach(player -> player.playSound(_projectile.getLocation(), Sound.EXPLODE, 1.f, 1.f));
_projectile.remove();
_projectile = null;
return;
}
UtilParticle.PlayParticleToAll(ParticleType.LARGE_SMOKE, _projectile.getLocation().add(0, .5, 0), new Vector(0, 0, 0), .1f, 3, ViewDist.MAX);
}
}
@ -293,20 +288,6 @@ public class Catapult implements Listener
@EventHandler
public void updateState(UpdateEvent event)
{
// if (event.getType() == UpdateType.FASTEST)
// {
// if (_state == CannonState.LOADED)
// {
// double yaw = getArmorStand().getHeadPose().getY() - 7.75d;
//
// Location loc = new Location(_location.getWorld(), getArmorStand().getLocation().getX() + (0.5 * Math.cos(yaw)), _location.getY() + 1.12, getArmorStand().getLocation().getZ() + (0.5 * Math.sin(yaw)));
//
// loc = UtilAlg.moveForward(loc, 0.1, (float) Math.toDegrees(yaw + 7.75), false);
//
// UtilParticle.PlayParticleToAll(ParticleType.FLAME, loc, new Vector(0, 0, 0), 0.f, 1, ViewDist.MAX);
// }
// }
if (event.getType() != UpdateType.TICK)
{
return;
@ -423,7 +404,7 @@ public class Catapult implements Listener
}
ArmorStand armorStand = getArmorStand();
double standYaw = Math.toDegrees(armorStand.getHeadPose().getY()) % 360;
double standYaw = _curYaw % 360;
double riderYaw = _nextYaw % 360;
// riderYaw = 350 and standYaw = 20
@ -432,14 +413,9 @@ public class Catapult implements Listener
if (dif > 180) dif -= 360;
if (dif < -180) dif += 360;
double yaw = standYaw + Math.min(dif / 20.f, 5f);
_curYaw = (float) (standYaw + dif / 10.f);
Location pos = armorStand.getLocation();
pos.setYaw(UtilMath.closest(_rotationPoints, (float) yaw));
armorStand.teleport(pos);
armorStand.setHeadPose(new EulerAngle(0, Math.toRadians(yaw), 0));
armorStand.setHeadPose(new EulerAngle(0, Math.toRadians(UtilMath.closest(_rotationPoints, _curYaw)), 0));
}
@EventHandler

View File

@ -0,0 +1,164 @@
package mineplex.game.clans.clans.siege.catapult;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.event.EventHandler;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.util.Vector;
import mineplex.core.common.util.UtilBlock;
import mineplex.core.common.util.UtilItem;
import mineplex.core.common.util.UtilMath;
import mineplex.core.common.util.UtilParticle;
import mineplex.core.common.util.UtilParticle.ParticleType;
import mineplex.core.common.util.UtilParticle.ViewDist;
import mineplex.core.common.util.UtilServer;
import mineplex.core.updater.UpdateType;
import mineplex.core.updater.event.UpdateEvent;
import mineplex.game.clans.clans.ClansManager;
import mineplex.game.clans.clans.siege.cannon.CraterBlock;
public class CatapultCrater implements Listener
{
private static final double EXPL_RADIUS = 2;
public static final Material CHARRED_TYPE = Material.OBSIDIAN;
private Catapult _host;
private Location _origin;
private long _birthTime;
private List<CraterBlock> _blocks;
public CatapultCrater(Catapult cannon, Location origin)
{
_host = cannon;
_origin = origin;
_birthTime = System.currentTimeMillis();
_blocks = new ArrayList<>();
UtilServer.getServer().getPluginManager().registerEvents(this, ClansManager.getInstance().getPlugin());
createExplosion();
}
@EventHandler
public void updateEffects(UpdateEvent event)
{
if (event.getType() != UpdateType.TICK)
{
return;
}
_blocks.stream().filter(block -> block.Type == CHARRED_TYPE).forEach(block -> {
long lifetime = System.currentTimeMillis() - _birthTime;
long max = 80000;
if (block.Location.getBlock().getType() != CHARRED_TYPE)
{
return;
}
// Water touching the charred blocks
if (UtilBlock.getInRadius(block.Location, 1.6).keySet().stream().filter(water -> water.getType() == Material.WATER).iterator().hasNext())
{
return;
}
if (lifetime < max)
{
UtilParticle.PlayParticleToAll(ParticleType.FLAME, block.Location.clone().add(.5, 1, .5), new Vector(0, 0, 0), .0f, 3, ViewDist.MAX);
UtilParticle.PlayParticleToAll(lifetime >= max / 1.4 ? ParticleType.SMOKE : ParticleType.LARGE_SMOKE, block.Location.clone().add(.5, .8, .5), new Vector(0, 0, 0), .0f, 1, ViewDist.MAX);
}
else
{
HandlerList.unregisterAll(this);
}
});
}
private void createExplosion()
{
_blocks.add(new CraterBlock(_origin, 0, Material.AIR));
HashMap<Block, Double> blockList = new HashMap<Block, Double>();
int iR = (int) EXPL_RADIUS + 1;
for (int x = -iR; x <= iR; x++)
{
for (int z = -iR; z <= iR; z++)
{
for (int y = -iR; y <= iR; y++)
{
Block curBlock = _origin.getBlock().getRelative(x, y, z);
double offset = UtilMath.offset(_origin, curBlock.getLocation());
if (offset <= EXPL_RADIUS)
{
blockList.put(curBlock, offset);
}
}
}
}
for (Entry<Block, Double> entry : blockList.entrySet())
{
Block block = entry.getKey();
double distance = entry.getValue();
boolean air = distance <= 2 || (Math.random() > (distance) / 3.65d);
if (block.getType() == Material.AIR) continue;
if (air)
{
_blocks.add(new CraterBlock(block.getLocation(), distance, Material.AIR));
Block above = block;
while (!UtilItem.isBoundless((above = above.getRelative(BlockFace.UP)).getType()))
{
_blocks.add(new CraterBlock(above.getLocation(), distance, Material.AIR));
}
}
}
_blocks.forEach(CraterBlock::set);
for (Entry<Block, Double> entry : blockList.entrySet())
{
Block block = entry.getKey();
double distance = entry.getValue();
if (block.getType() == Material.AIR) continue;
if (
distance > 2 &&
Math.random() > .85 &&
UtilItem.isBoundless(block.getRelative(BlockFace.UP).getType()) &&
!UtilItem.isBoundless(block.getRelative(BlockFace.DOWN).getType()) &&
!block.getRelative(BlockFace.UP).getType().equals(CHARRED_TYPE) &&
!block.getRelative(BlockFace.DOWN).getType().equals(CHARRED_TYPE))
{
_blocks.add(new CraterBlock(block.getLocation(), distance, CHARRED_TYPE));
_blocks.add(new CraterBlock(block.getLocation().clone().add(0, 1, 0), distance, Material.FIRE));
}
}
_blocks.forEach(CraterBlock::set);
}
}

View File

@ -2,33 +2,33 @@ package mineplex.game.clans.clans.siege.catapult;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.ArmorStand;
import org.bukkit.Sound;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.FallingBlock;
import org.bukkit.event.EventHandler;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.EulerAngle;
import org.bukkit.event.entity.EntityChangeBlockEvent;
import org.bukkit.util.Vector;
import mineplex.core.common.util.UtilEnt;
import mineplex.core.common.util.UtilBlock;
import mineplex.core.common.util.UtilMath;
import mineplex.core.common.util.UtilServer;
import mineplex.core.updater.UpdateType;
import mineplex.core.updater.event.UpdateEvent;
import mineplex.game.clans.clans.ClansManager;
public class CatapultProjectile implements Listener
{
private ArmorStand _parent;
private FallingBlock _sand;
public CatapultProjectile(Location location, Material material)
private Catapult _host;
public CatapultProjectile(Catapult host, Location location, Material material, float yaw)
{
_parent = location.getWorld().spawn(location, ArmorStand.class);
UtilEnt.setFakeHead(_parent, true);
_parent.teleport(location);
_parent.setVisible(false);
_parent.setGravity(false);
_parent.setHelmet(new ItemStack(material));
_host = host;
_sand = location.getWorld().spawnFallingBlock(location, material, (byte) 0);
_sand.setFireTicks(20000);
_sand.setDropItem(false);
UtilServer.getServer().getPluginManager().registerEvents(this, ClansManager.getInstance().getPlugin());
}
@ -36,44 +36,58 @@ public class CatapultProjectile implements Listener
@EventHandler
public void update(UpdateEvent event)
{
if (event.getType() != UpdateType.TICK)
if (Math.abs(_sand.getVelocity().getX()) < 0.05 || Math.abs(_sand.getVelocity().getZ()) < 0.05)
{
return;
remove();
}
if (_parent == null || _parent.isDead())
}
@EventHandler
public void onBlockFall(EntityChangeBlockEvent event)
{
if (event.getEntity().equals(_sand))
{
return;
event.setCancelled(true);
remove();
}
_parent.setHeadPose(new EulerAngle(_parent.getHeadPose().getX() + 2.75, _parent.getHeadPose().getY(), _parent.getHeadPose().getZ()));
}
public void remove()
{
HandlerList.unregisterAll(this);
_parent.remove();
_parent = null;
HandlerList.unregisterAll(this);
UtilServer.getServer().getOnlinePlayers().forEach(player -> player.playSound(getLocation(), Sound.EXPLODE, 1.f, 1.f));
new CatapultCrater(_host, UtilBlock.nearestFloor(_sand.getLocation()));
_sand.remove();
_sand = null;
}
public boolean dead()
{
return _sand == null;
}
public Location getLocation()
{
return _parent.getLocation();
return _sand.getLocation();
}
public Vector getVelocity()
{
return _parent.getVelocity();
return _sand.getVelocity();
}
public int getTicksLived()
{
return _parent.getTicksLived();
return _sand.getTicksLived();
}
public void setVelocity(Vector velocity)
{
_parent.setVelocity(velocity);
_sand.setVelocity(velocity);
}
}

View File

@ -5,9 +5,9 @@ import org.bukkit.inventory.ItemStack;
public enum CatapultState
{
UNLOADED(Material.PACKED_ICE, (byte) 0 /* packed ice */),
LOADED(Material.ENDER_PORTAL_FRAME, (byte) 0 /* lapis block */),
FIRED(Material.LAPIS_BLOCK, (byte) 0);
UNLOADED(Material.PACKED_ICE, (byte) 0),
LOADED(Material.ENDER_PORTAL_FRAME, (byte) 0),
FIRED(Material.LAPIS_BLOCK, (byte) 1);
private Material _material;
private byte _data;

View File

@ -513,6 +513,11 @@ public class Gameplay extends MiniPlugin
{
event.setCancelled(true);
}
if (event.getItem().getType() == Material.SAPLING)
{
event.setCancelled(true);
}
}
// @EventHandler(priority = EventPriority.LOWEST)