more catapults!

This commit is contained in:
NewGarbo 2016-01-26 21:10:58 +00:00
parent 3d7a371d1d
commit 7f7e73a101
5 changed files with 43 additions and 11 deletions

View File

@ -162,4 +162,22 @@ public class UtilMath
{
return list.isEmpty() ? null : list.get(0);
}
public static <N extends Number> N closest(List<N> values, N value)
{
int closestIndex = -1;
int index = 0;
for (N number : values)
{
if (closestIndex == -1 || (Math.abs(value.doubleValue() - number.doubleValue()) < Math.abs(value.doubleValue() - values.get(closestIndex).doubleValue())))
{
closestIndex = index;
}
index++;
}
return values.get(closestIndex);
}
}

View File

@ -152,8 +152,6 @@ public class Clans extends JavaPlugin
new Updater(this);
MinecraftServer.getServer().getPropertyManager().setProperty("debug", true);
getServer().setWhitelist(false); // TODO: remove
}
public static String prettifyName(Material material)

View File

@ -111,7 +111,7 @@ public class SiegeManager extends MiniPlugin
// return false;
// }
spawnCannon(player, location);
spawnCatapult(player, location);
return true;
}

View File

@ -8,6 +8,7 @@ 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;
@ -17,7 +18,6 @@ import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.event.player.PlayerArmorStandManipulateEvent;
@ -66,6 +66,8 @@ public class Catapult implements Listener
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 List<Float> _rotationPoints;
private Location _location;
private CatapultState _state;
@ -87,8 +89,14 @@ public class Catapult implements Listener
_location = location;
_state = CatapultState.UNLOADED;
_rotationPoints = new ArrayList<>();
_entities = new ArrayList<>();
for (int i = 0; i < 8; i++)
{
_rotationPoints.add((360.f / 8.f) * i);
}
_inventory = UtilServer.getServer().createInventory(null, InventoryType.DISPENSER, C.cRedB + "Catapult");
_clan = clan;
@ -117,6 +125,8 @@ public class Catapult implements Listener
armorStand.setGravity(false);
armorStand.setHelmet(_state.toItemStack());
((CraftChicken) armorStand).getHandle().setSize(1.87F, 1.87F);
_entities.add(armorStand);
}
else
@ -132,7 +142,7 @@ public class Catapult implements Listener
private Location getArmorStandLocation()
{
return _location.clone().add(.5, -1.1, .5);
return _location.clone().add(.5, -.95, .5);
}
private void setState(CatapultState state)
@ -304,6 +314,11 @@ public class Catapult implements Listener
CatapultState newState = _inventory.getItem(COBBLE_SLOT) != null ? CatapultState.LOADED : CatapultState.UNLOADED;
if ((System.currentTimeMillis() - _lastFired) <= 3000)
{
newState = CatapultState.FIRED;
}
if (_state != newState)
{
setState(newState);
@ -346,7 +361,7 @@ public class Catapult implements Listener
}
else if (CHARGE_SLOTS.contains(slot))
{
if (item.getType() != Material.FIREWORK_CHARGE)
if (item.getType() != Material.FIREBALL)
{
human.getInventory().addItem(item);
_inventory.setItem(slot, null);
@ -355,8 +370,8 @@ public class Catapult implements Listener
{
if (item.getAmount() > MAX_CHARGES)
{
human.getInventory().addItem(new ItemStack(Material.FIREWORK_CHARGE, item.getAmount() - MAX_CHARGES));
_inventory.setItem(slot, new ItemStack(Material.FIREWORK_CHARGE, MAX_CHARGES));
human.getInventory().addItem(new ItemStack(Material.FIREBALL, item.getAmount() - MAX_CHARGES));
_inventory.setItem(slot, new ItemStack(Material.FIREBALL, MAX_CHARGES));
}
}
}
@ -409,7 +424,7 @@ public class Catapult implements Listener
ArmorStand armorStand = getArmorStand();
double standYaw = Math.toDegrees(armorStand.getHeadPose().getY()) % 360;
double riderYaw = _nextYaw;
double riderYaw = _nextYaw % 360;
// riderYaw = 350 and standYaw = 20
// dif should be -30 and not 330
@ -421,7 +436,7 @@ public class Catapult implements Listener
Location pos = armorStand.getLocation();
pos.setYaw((float) yaw);
pos.setYaw(UtilMath.closest(_rotationPoints, (float) yaw));
armorStand.teleport(pos);
armorStand.setHeadPose(new EulerAngle(0, Math.toRadians(yaw), 0));

View File

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