Added balloons

This commit is contained in:
LCastr0 2016-12-26 18:10:42 -02:00
parent fb9720c926
commit 4ebc82bf1f
6 changed files with 305 additions and 73 deletions

View File

@ -57,7 +57,8 @@ import mineplex.core.gadget.gadgets.arrowtrail.shadow.ArrowTrailShadow;
import mineplex.core.gadget.gadgets.arrowtrail.titan.ArrowTrailTitan;
import mineplex.core.gadget.gadgets.arrowtrail.vampire.ArrowTrailBlood;
import mineplex.core.gadget.gadgets.arrowtrail.wisdom.ArrowTrailEnchant;
import mineplex.core.gadget.gadgets.balloons.BabyCowBalloon;
import mineplex.core.gadget.gadgets.balloons.BalloonItem;
import mineplex.core.gadget.gadgets.balloons.BalloonType;
import mineplex.core.gadget.gadgets.death.candycane.DeathCandyCane;
import mineplex.core.gadget.gadgets.death.christmas.DeathPresentDanger;
import mineplex.core.gadget.gadgets.death.cupidslove.DeathCupidsBrokenHeart;
@ -532,7 +533,10 @@ public class GadgetManager extends MiniPlugin
}*/
// Balloons
addGadget(new BabyCowBalloon(this));
for (BalloonType balloonType : BalloonType.values())
{
addGadget(new BalloonItem(this, balloonType));
}
// TAUNTS!!!
addGadget(new EternalTaunt(this));

View File

@ -1,52 +0,0 @@
package mineplex.core.gadget.gadgets.balloons;
import mineplex.core.common.util.UtilEnt;
import mineplex.core.gadget.GadgetManager;
import mineplex.core.gadget.types.BalloonGadget;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.*;
public class BabyCowBalloon extends BalloonGadget
{
private ArmorStand _entityStand, _playerStand;
private Entity _balloonEntity;
public BabyCowBalloon(GadgetManager manager)
{
super(manager, "Baby Cow Balloon", new String[]{"placeholder"}, 0, Material.MONSTER_EGG, UtilEnt.getEntityEggData(EntityType.COW));
}
@Override
public void enableCustom(Player player, boolean message)
{
if (!canSpawnBalloon(player))
{
// TODO MESSAGE
return;
}
addPlayerBalloon(player);
_entityStand = player.getWorld().spawn(player.getLocation(), ArmorStand.class);
_entityStand.setGravity(false);
_entityStand.setVisible(false);
Cow babyCow = player.getWorld().spawn(player.getLocation(), Cow.class);
babyCow.setBaby();
_balloonEntity = babyCow;
_entityStand.setPassenger(babyCow);
Location balloonLocation = player.getLocation().add(_random.nextDouble(), getNewHeight(player), _random.nextDouble());
_entityStand.teleport(balloonLocation);
babyCow.setLeashHolder(player);
// TODO UPDATE BALLOONS
}
@Override
public void disableCustom(Player player, boolean message)
{
_entityStand.remove();
_balloonEntity.remove();
removePlayerBalloon(player);
// TODO UPDATE PLAYER HEIGHT
}
}

View File

@ -0,0 +1,34 @@
package mineplex.core.gadget.gadgets.balloons;
import org.bukkit.Material;
import org.bukkit.entity.Ageable;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import mineplex.core.gadget.GadgetManager;
import mineplex.core.gadget.types.BalloonGadget;
public class BalloonItem extends BalloonGadget
{
private BalloonType _balloonType;
public BalloonItem(GadgetManager manager, BalloonType balloonType)
{
super(manager, balloonType.getName(), balloonType.getLore(), balloonType.getCost(), Material.GLASS, (byte) 0, balloonType.getEntityType());
// TODO ADD DISPLAY ITEM
_balloonType = balloonType;
}
@Override
public Entity spawnEntity(Player player)
{
Entity entity = player.getWorld().spawnEntity(player.getLocation(), _balloonType.getEntityType());
if (_balloonType.isBaby() && entity instanceof Ageable)
((Ageable) entity).setBaby();
((LivingEntity) entity).setLeashHolder(player);
return entity;
}
}

View File

@ -0,0 +1,61 @@
package mineplex.core.gadget.gadgets.balloons;
import org.bukkit.Material;
import org.bukkit.entity.EntityType;
import org.bukkit.inventory.ItemStack;
import mineplex.core.common.util.LineFormat;
import mineplex.core.common.util.UtilText;
public enum BalloonType
{
BABY_COW(EntityType.COW, true, "Baby Cow Balloon", UtilText.splitLinesToArray(new String[]{"Placeholder"}, LineFormat.LORE), 0, new ItemStack(Material.MONSTER_EGG, 1, EntityType.COW.getTypeId()));
private EntityType _entityType;
private boolean _isBaby;
private String _name;
private String[] _lore;
private int _cost;
private ItemStack _displayItem;
BalloonType(EntityType entityType, boolean isBaby, String name, String[] lore, int cost, ItemStack displayItem)
{
_entityType = entityType;
_isBaby = isBaby;
_name = name;
_lore = lore;
_cost = cost;
_displayItem = displayItem;
}
public EntityType getEntityType()
{
return _entityType;
}
public boolean isBaby()
{
return _isBaby;
}
public String getName()
{
return _name;
}
public String[] getLore()
{
return _lore;
}
public int getCost()
{
return _cost;
}
public ItemStack getDisplayItem()
{
return _displayItem;
}
}

View File

@ -1,29 +1,120 @@
package mineplex.core.gadget.types;
import java.util.*;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import mineplex.core.gadget.GadgetManager;
import org.bukkit.Material;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import mineplex.core.common.util.F;
import mineplex.core.common.util.UtilPlayer;
import mineplex.core.gadget.GadgetManager;
import mineplex.core.gadget.util.UtilBalloons;
import mineplex.core.updater.UpdateType;
import mineplex.core.updater.event.UpdateEvent;
public abstract class BalloonGadget extends Gadget
{
protected static final Map<UUID, List<BalloonGadget>> PLAYER_BALLOONS = new HashMap<>();
private static final Map<UUID, Map<EntityType, UtilBalloons>> PLAYER_BALLOONS = new HashMap<>();
protected final Random _random;
private EntityType _balloon;
public BalloonGadget(GadgetManager manager, String name, String[] desc, int cost, Material material, byte data, String... altNames)
public BalloonGadget(GadgetManager manager, String name, String[] desc, int cost, Material material, byte data, EntityType balloon, String... altNames)
{
super(manager, GadgetType.BALLOON, name, desc, cost, material, data, 1, altNames);
_random = new Random();
_balloon = balloon;
}
protected boolean canSpawnBalloon(Player player)
@Override
public void enableCustom(Player player, boolean message)
{
boolean add = addPlayerBalloon(player);
if (add)
{
_active.add(player);
if (message)
{
UtilPlayer.message(player, F.main("Gadget", "You spawned a " + F.elem(getName()) + "!"));
}
}
else
{
Manager.removeActive(player, this);
UtilPlayer.message(player, F.main("Gadget", "You cannot have more than " + F.count("10") + " balloons!"));
}
}
@Override
public void disableCustom(Player player, boolean message)
{
removePlayerBalloon(player);
UtilPlayer.message(player, F.main("Gadget", "You despawned a " + F.elem(getName())) + "!");
}
private boolean addPlayerBalloon(Player player)
{
if (!canSpawnBalloon(player))
return false;
PLAYER_BALLOONS.computeIfAbsent(player.getUniqueId(), map -> new HashMap<>());
PLAYER_BALLOONS.get(player.getUniqueId()).put(_balloon, new UtilBalloons(player, spawnEntity(player)));
return true;
}
private void removePlayerBalloon(Player player)
{
if (PLAYER_BALLOONS.containsKey(player.getUniqueId()))
{
List<BalloonGadget> balloonGadgets = PLAYER_BALLOONS.get(player.getUniqueId());
if (PLAYER_BALLOONS.get(player.getUniqueId()).containsKey(_balloon))
{
UtilBalloons utilBalloons = PLAYER_BALLOONS.get(player.getUniqueId()).get(_balloon);
Entity entity = utilBalloons.getBalloon();
entity.remove();
PLAYER_BALLOONS.get(player.getUniqueId()).remove(_balloon);
}
}
}
protected abstract Entity spawnEntity(Player player);
@EventHandler
public void onUpdate(UpdateEvent event)
{
if (event.getType() != UpdateType.TICK)
return;
for (Map.Entry<UUID, Map<EntityType, UtilBalloons>> entry : PLAYER_BALLOONS.entrySet())
{
for (UtilBalloons utilBalloons : entry.getValue().values())
{
utilBalloons.update();
}
}
}
private boolean canSpawnBalloon(Player player)
{
if (PLAYER_BALLOONS.containsKey(player.getUniqueId()))
{
Map<EntityType, UtilBalloons> balloonGadgets = PLAYER_BALLOONS.get(player.getUniqueId());
if (balloonGadgets.containsKey(_balloon))
return false;
return balloonGadgets.size() < 10;
}
return true;
}
/*protected boolean canSpawnBalloon(Player player)
{
if (PLAYER_BALLOONS.containsKey(player.getUniqueId()))
{
List<UtilBalloons> balloonGadgets = PLAYER_BALLOONS.get(player.getUniqueId());
return balloonGadgets.size() < 10;
}
return true;
@ -34,18 +125,18 @@ public abstract class BalloonGadget extends Gadget
if (canSpawnBalloon(player))
{
PLAYER_BALLOONS.computeIfAbsent(player.getUniqueId(), list -> new ArrayList<>());
List<BalloonGadget> balloonGadgets = PLAYER_BALLOONS.get(player.getUniqueId());
balloonGadgets.add(this);
List<UtilBalloons> balloonGadgets = PLAYER_BALLOONS.get(player.getUniqueId());
balloonGadgets.add(new UtilBalloons(player, _balloon));
PLAYER_BALLOONS.put(player.getUniqueId(), balloonGadgets);
}
}
protected void removePlayerBalloon(Player player)
{
List<BalloonGadget> balloonGadgets = PLAYER_BALLOONS.computeIfPresent(player.getUniqueId(), (uuid, list) -> list);
if (balloonGadgets.contains(this))
List<UtilBalloons> balloonGadgets = PLAYER_BALLOONS.computeIfPresent(player.getUniqueId(), (uuid, list) -> list);
if (balloonGadgets.contains(_balloon))
{
balloonGadgets.remove(this);
balloonGadgets.remove(_balloon);
}
if (balloonGadgets.size() >= 1)
PLAYER_BALLOONS.put(player.getUniqueId(), balloonGadgets);
@ -53,12 +144,15 @@ public abstract class BalloonGadget extends Gadget
PLAYER_BALLOONS.remove(player.getUniqueId());
}
protected double getNewHeight(Player player)
protected void spawnBalloon(Player player, Entity balloon)
{
List<BalloonGadget> balloonGadgets = PLAYER_BALLOONS.computeIfPresent(player.getUniqueId(), (uuid, list) -> list);
if (balloonGadgets != null)
return balloonGadgets.size() * _random.nextDouble() * (_random.nextInt(1) + 2);
return 3;
}
if (!canSpawnBalloon(player))
{
// TODO MESSAGE
return;
}
addPlayerBalloon(player, _balloon);
}*/
}

View File

@ -0,0 +1,91 @@
package mineplex.core.gadget.util;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;
import mineplex.core.common.util.UtilAlg;
import mineplex.core.common.util.UtilMath;
public class UtilBalloons
{
/**
* Makes the Fkying pets fly around the player
* Copied from {@link mineplex.core.gadget.gadgets.particle.ParticleFairyData}
*/
private Player _player;
private Entity _balloon;
private Location _balloonLoc, _target;
private Vector _direction;
private double _speed;
private long _idleTime;
public UtilBalloons(Player player, Entity balloon)
{
_player = player;
_balloon = balloon;
_balloonLoc = player.getEyeLocation();
_target = getNewTarget();
_speed = 0.2;
_idleTime = 0;
_direction = new Vector(1, 0, 0);
}
public void update()
{
//Update Target
if (UtilMath.offset(_player.getEyeLocation(), _target) > 3 || UtilMath.offset(_balloonLoc, _target) < 1)
_target = getNewTarget();
//Pause?
if (Math.random() > 0.98)
_idleTime = System.currentTimeMillis() + (long)(Math.random() * 3000);
//Speed
if (UtilMath.offset(_player.getEyeLocation(), _balloonLoc) < 3)
{
if (_idleTime > System.currentTimeMillis())
{
_speed = Math.max(0, _speed - 0.005);
}
else
{
_speed = Math.min(0.15, _speed + 0.005);
}
}
else
{
_idleTime = 0;
_speed = Math.min(0.15 + UtilMath.offset(_player.getEyeLocation(), _balloonLoc) * 0.05, _speed + 0.02);
}
//Modify Direction
_direction.add(UtilAlg.getTrajectory(_balloonLoc, _target).multiply(0.15));
if (_direction.length() < 1)
_speed = _speed * _direction.length();
UtilAlg.Normalize(_direction);
//Move
if (UtilMath.offset(_balloonLoc, _target) > 0.1)
_balloonLoc.add(_direction.clone().multiply(_speed));
_balloon.teleport(_balloonLoc);
_balloon.setVelocity(new Vector(0, .25, 0));
}
private Location getNewTarget()
{
return _player.getEyeLocation().add(Math.random() * 6 - 3, Math.random() * 10, Math.random() * 6 - 3);
}
public Entity getBalloon()
{
return _balloon;
}
}