Implement a better section system

This commit is contained in:
Sam 2017-11-08 18:59:19 +00:00 committed by Alexander Meech
parent a40625825d
commit eda592a35f
8 changed files with 542 additions and 6 deletions

View File

@ -122,6 +122,8 @@ public class ChristmasCommon extends SoloGame
HungerSet = 20;
PrepareFreeze = false;
manager.GetCreature().SetDisableCustomDrops(true);
registerChatStats(
DamageDealt,
DamageTaken

View File

@ -0,0 +1,96 @@
package nautilus.game.arcade.game.games.christmasnew.present;
import net.minecraft.server.v1_8_R3.Entity;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftEntity;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import mineplex.core.common.skin.SkinData;
import mineplex.core.common.util.C;
import mineplex.core.common.util.MapUtil;
import mineplex.core.common.util.UtilEnt;
import mineplex.core.common.util.UtilMath;
import mineplex.core.lifetimes.Component;
public class Present implements Component
{
private static final ItemStack PRESENT = SkinData.PRESENT.getSkull();
private static final int ROTATION_ITERATIONS = 20;
private static final int ROTATION_DELTA_Y = 1 / ROTATION_ITERATIONS;
private final Location _location;
private final ArmorStand _stand;
private int _iterations;
private boolean _down;
private boolean _collected;
public Present(Location location)
{
_location = location.clone();
_location.setYaw(UtilMath.r(360));
_stand = _location.getWorld().spawn(_location, ArmorStand.class);
_stand.setVisible(false);
_stand.setGravity(false);
_stand.setHelmet(PRESENT);
_stand.setCustomName(C.cGreenB + "Present");
_stand.setRemoveWhenFarAway(false);
UtilEnt.ghost(_stand, true, false);
MapUtil.QuickChangeBlockAt(_location, Material.STONE);
}
@Override
public void activate()
{
_stand.setCustomNameVisible(true);
MapUtil.QuickChangeBlockAt(_location, Material.STAINED_GLASS, (byte) (Math.random() < 0.5 ? 14 : 5));
}
@Override
public void deactivate()
{
if (!_collected)
{
_stand.remove();
MapUtil.QuickChangeBlockAt(_location.clone().subtract(0, 1, 0), Material.COAL_BLOCK);
}
_collected = true;
}
public void updateRotation()
{
Entity entity = ((CraftEntity) _stand).getHandle();
double newY = entity.locY + (_down ? -ROTATION_DELTA_Y : ROTATION_DELTA_Y);
if (++_iterations == 20)
{
_iterations = 0;
_down = !_down;
}
entity.setPosition(entity.locX, newY, entity.locZ);
}
public Location getLocation()
{
return _location;
}
public boolean isColliding(Player player)
{
return UtilMath.offsetSquared(player, _stand) < 4;
}
public boolean isCollected()
{
return _collected;
}
}

View File

@ -1,23 +1,108 @@
package nautilus.game.arcade.game.games.christmasnew.section;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.bukkit.Color;
import org.bukkit.FireworkEffect;
import org.bukkit.FireworkEffect.Type;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import nautilus.game.arcade.game.games.christmasnew.ChristmasNew;
import mineplex.core.common.util.UtilFirework;
import mineplex.core.lifetimes.SimpleLifetime;
public abstract class Section implements Listener
import nautilus.game.arcade.game.games.christmasnew.ChristmasNew;
import nautilus.game.arcade.game.games.christmasnew.present.Present;
import nautilus.game.arcade.world.WorldData;
public abstract class Section extends SimpleLifetime implements Listener, SectionRegister
{
private final ChristmasNew _host;
private static final FireworkEffect FIREWORK_EFFECT = FireworkEffect.builder()
.with(Type.STAR)
.withColor(Color.RED, Color.LIME)
.withFade(Color.WHITE)
.withFlicker()
.withTrail()
.build();
protected final ChristmasNew _host;
protected final WorldData _worldData;
private final List<SectionChallenge> _challenges;
public Section(ChristmasNew host)
{
_host = host;
_worldData = host.WorldData;
_challenges = new ArrayList<>(2);
_challenges.forEach(this::register);
}
public abstract void onRegister();
@Override
public void start() throws IllegalStateException
{
super.start();
onRegister();
_challenges.forEach(challenge ->
{
challenge.onRegister();
challenge.activate();
}); }
public abstract void onUnregister();
@Override
public void end() throws IllegalStateException
{
super.end();
onUnregister();
_challenges.forEach(challenge ->
{
challenge.onUnregister();
challenge.deactivate();
});
}
public abstract boolean isComplete();
protected void registerChallenges(SectionChallenge... challenges)
{
List<SectionChallenge> challengesList = Arrays.asList(challenges);
_challenges.addAll(challengesList);
challengesList.forEach(this::register);
}
public boolean isComplete()
{
for (SectionChallenge challenge : _challenges)
{
if (challenge.getPresent().isCollected())
{
return false;
}
}
return true;
}
public void onPresentCollect(Player player, Present present)
{
int left = 0;
for (SectionChallenge challenge : _challenges)
{
Present otherPresent =challenge.getPresent();
if (present.equals(otherPresent))
{
challenge.onPresentCollect();
}
else if (!otherPresent.isCollected())
{
left++;
}
}
_host.sendSantaMessage("Well done " + player.getName() + " you found a present!" + (left > 0 ? " Only " + left + " to go!" : ""), null);
UtilFirework.launchFirework(present.getLocation(), FIREWORK_EFFECT, null, 2);
}
}

View File

@ -0,0 +1,97 @@
package nautilus.game.arcade.game.games.christmasnew.section;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.event.EventHandler;
import mineplex.core.lifetimes.ListenerComponent;
import mineplex.core.updater.UpdateType;
import mineplex.core.updater.event.UpdateEvent;
import nautilus.game.arcade.game.games.christmasnew.ChristmasNew;
import nautilus.game.arcade.game.games.christmasnew.present.Present;
import nautilus.game.arcade.world.WorldData;
public abstract class SectionChallenge extends ListenerComponent implements SectionRegister
{
protected final ChristmasNew _host;
protected final WorldData _worldData;
private final Present _present;
private final Section _section;
protected final List<Entity> _entities;
protected int _mobsPerSecond;
protected int _maxMobs;
public SectionChallenge(ChristmasNew host, Location present, Section section)
{
_host = host;
_worldData = host.WorldData;
_present = new Present(present);
_section = section;
_entities = new ArrayList<>();
}
public abstract void onPresentCollect();
@Override
public void deactivate()
{
super.deactivate();
_entities.forEach(Entity::remove);
_entities.clear();
}
public <T extends Entity> T spawn(Location location, Class<T> classOfT)
{
_host.CreatureAllowOverride = true;
T entity = location.getWorld().spawn(location, classOfT);
_entities.add(entity);
_host.CreatureAllowOverride = false;
return entity;
}
@EventHandler
public void updateDeadMobs(UpdateEvent event)
{
if (event.getType() != UpdateType.SLOW)
{
return;
}
_entities.removeIf(entity -> entity.isDead() || !entity.isValid());
}
@EventHandler
public void updatePresents(UpdateEvent event)
{
if (event.getType() == UpdateType.TICK)
{
_present.updateRotation();
}
else if (event.getType() == UpdateType.FAST && !_present.isCollected())
{
_host.GetPlayers(true).forEach(player ->
{
if (!_present.isColliding(player))
{
_section.onPresentCollect(player, _present);
_present.deactivate();
}
});
}
}
public Present getPresent()
{
return _present;
}
}

View File

@ -0,0 +1,10 @@
package nautilus.game.arcade.game.games.christmasnew.section;
public interface SectionRegister
{
void onRegister();
void onUnregister();
}

View File

@ -0,0 +1,97 @@
package nautilus.game.arcade.game.games.christmasnew.section.one;
import java.util.List;
import org.bukkit.Effect;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Skeleton;
import org.bukkit.event.EventHandler;
import org.bukkit.inventory.ItemStack;
import mineplex.core.common.util.MapUtil;
import mineplex.core.common.util.UtilAlg;
import mineplex.core.common.util.UtilMath;
import mineplex.core.updater.UpdateType;
import mineplex.core.updater.event.UpdateEvent;
import nautilus.game.arcade.game.games.christmasnew.ChristmasNew;
import nautilus.game.arcade.game.games.christmasnew.section.Section;
import nautilus.game.arcade.game.games.christmasnew.section.SectionChallenge;
public class CaveMaze extends SectionChallenge
{
private static final int MAX_MOBS = 25;
private static final ItemStack[] IN_HAND =
{
new ItemStack(Material.WOOD_SWORD),
new ItemStack(Material.STONE_SWORD),
new ItemStack(Material.STONE_PICKAXE),
new ItemStack(Material.IRON_PICKAXE)
};
private final List<Location> _mobSpawns;
private final List<Location> _quickOutWood;
private final List<Location> _quickOutAir;
CaveMaze(ChristmasNew host, Location present, Section section)
{
super(host, present, section);
_mobSpawns = _worldData.GetDataLocs("BROWN");
_quickOutWood = _worldData.GetCustomLocs(String.valueOf(Material.NETHERRACK.getId()));
_quickOutAir = _worldData.GetCustomLocs(String.valueOf(Material.SOUL_SAND.getId()));
_quickOutWood.forEach(location -> MapUtil.QuickChangeBlockAt(location, Material.AIR));
_quickOutAir.forEach(location -> MapUtil.QuickChangeBlockAt(location, Material.IRON_FENCE));
}
@Override
public void onPresentCollect()
{
_quickOutWood.forEach(location ->
{
if (Math.random() > 0.95)
{
location.getWorld().playEffect(location, Effect.STEP_SOUND, Material.WOOD_STEP);
}
MapUtil.QuickChangeBlockAt(location, Material.WOOD_STEP);
});
_quickOutAir.forEach(location ->
{
if (Math.random() > 0.95)
{
location.getWorld().playEffect(location, Effect.STEP_SOUND, Material.IRON_FENCE);
}
MapUtil.QuickChangeBlockAt(location, Material.AIR);
});
}
@Override
public void onRegister()
{
}
@Override
public void onUnregister()
{
}
@EventHandler
public void updateMobSpawn(UpdateEvent event)
{
if (event.getType() != UpdateType.SEC || _entities.size() >= MAX_MOBS)
{
return;
}
Skeleton skeleton = spawn(UtilAlg.Random(_mobSpawns), Skeleton.class);
skeleton.getEquipment().setItemInHand(UtilMath.randomElement(IN_HAND));
}
}

View File

@ -0,0 +1,32 @@
package nautilus.game.arcade.game.games.christmasnew.section.one;
import org.bukkit.Location;
import nautilus.game.arcade.game.games.christmasnew.ChristmasNew;
import nautilus.game.arcade.game.games.christmasnew.section.Section;
public class Section1 extends Section
{
public Section1(ChristmasNew host, Location... presents)
{
super(host);
registerChallenges(
new TreeParkour(host, presents[0], this),
new CaveMaze(host, presents[1], this)
);
}
@Override
public void onRegister()
{
}
@Override
public void onUnregister()
{
}
}

View File

@ -0,0 +1,117 @@
package nautilus.game.arcade.game.games.christmasnew.section.one;
import java.util.List;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.scheduler.BukkitRunnable;
import mineplex.core.common.util.C;
import mineplex.core.common.util.MapUtil;
import mineplex.core.common.util.UtilPlayer;
import mineplex.core.hologram.Hologram;
import nautilus.game.arcade.game.games.christmasnew.ChristmasNew;
import nautilus.game.arcade.game.games.christmasnew.section.Section;
import nautilus.game.arcade.game.games.christmasnew.section.SectionChallenge;
class TreeParkour extends SectionChallenge
{
private final Location _checkpointTrigger;
private final Hologram _checkpointHologram;
private final List<Location> _checkpoint;
TreeParkour(ChristmasNew host, Location present, Section section)
{
super(host, present, section);
_checkpointTrigger = _worldData.GetCustomLocs(String.valueOf(Material.LAPIS_BLOCK.getId())).get(0).getBlock().getLocation();
_checkpointHologram = new Hologram(host.getArcadeManager().getHologramManager(), _checkpointTrigger, C.cGoldB + "Checkpoint")
.setInteraction((player, clickType) -> activateCheckpoint());
_checkpoint = _worldData.GetCustomLocs(String.valueOf(Material.LAPIS_ORE.getId()));
MapUtil.QuickChangeBlockAt(_checkpointTrigger, Material.LEVER);
}
@Override
public void onPresentCollect()
{
}
@Override
public void onRegister()
{
_checkpointHologram.start();
}
@Override
public void onUnregister()
{
_checkpointHologram.stop();
}
@EventHandler
public void checkpointInteract(PlayerInteractEvent event)
{
Player player = event.getPlayer();
if (UtilPlayer.isSpectator(player))
{
return;
}
Block block = event.getClickedBlock();
if (block == null || block.getType() != Material.LEVER || !_checkpointTrigger.equals(block))
{
return;
}
activateCheckpoint();
}
private void activateCheckpoint()
{
_checkpointTrigger.getWorld().playSound(_checkpointTrigger, Sound.LEVEL_UP, 1, 0.5F);
MapUtil.QuickChangeBlockAt(_checkpointTrigger, Material.AIR);
_checkpointHologram.stop();
_host.getArcadeManager().runSyncTimer(new BukkitRunnable()
{
int y = _checkpointTrigger.getBlockY();
@Override
public void run()
{
_checkpoint.removeIf(location ->
{
if (location.getY() > y)
{
location.getWorld().playSound(location, Sound.PISTON_RETRACT, 1, 0.6F);
MapUtil.QuickChangeBlockAt(location, Material.WOOD_STAIRS, (byte) 1);
return true;
}
return false;
});
if (_checkpoint.isEmpty())
{
cancel();
}
else
{
y--;
}
}
}, 10, 10);
}
}