Skip to content

Commit

Permalink
+ Added a BattleState enum containing various states of the EnderDrag…
Browse files Browse the repository at this point in the history
…onBattle process maintained by DragonEggDrop

+ Added BattleStateChangeEvent to allow other developers to listen in state changes for the ender dragon battle

config.yml:
- Removed the "respawn" configuration option
+ Replaced it with two new configuration options, "respawn-on-join" and "respawn-on-death" to separate the two

plugin.yml:
* Updated the version number to prepare for a release

DragonLifeListener:
+ Called the new event 2 times in different states

RespawnListener:
* Modified the listeners to use the new configuration booleans rather than the single "respawn" configuration boolean

DragonDeathRunnable:
+ Called the new event 2 times in different states
+ Added an instance of the EnderDragon to the constructor to be stored in a field (used for event purposes)
+ Implemented support for the new "respawn-on-death" configuration option

RespawnRunnable:
+ Called the new event 2 times in different states

NMSAbstract:
+ Added a new #getEnderDragonFromBattle(Object) method to get an EnderDragon based on its associated EnderDragonBattle object
* Modified all NMSAbstract implementations to properly implement this new method as well
  • Loading branch information
2008Choco committed Feb 1, 2017
1 parent 4c54212 commit c8186cb
Show file tree
Hide file tree
Showing 13 changed files with 355 additions and 11 deletions.
7 changes: 5 additions & 2 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,12 @@ max-loot: 6
egg-name: ''
egg-lore: []

#Respawn dragon on death? (delay is in seconds)
respawn: false
#Respawn dragon on join? (delay is in seconds)
respawn-on-join: false
join-respawn-delay: 60

#Respawn dragon on death? (delay is in seconds)
respawn-on-death: true
death-respawn-delay: 300

#Announce respawn to end world players via the action bar.
Expand Down
2 changes: 1 addition & 1 deletion plugin.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: DragonEggDrop
main: com.ninjaguild.dragoneggdrop.DragonEggDrop
version: 1.2.2
version: 1.2.3
author: NinjaStix

commands:
Expand Down
67 changes: 67 additions & 0 deletions src/com/ninjaguild/dragoneggdrop/api/BattleState.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
DragonEggDrop
Copyright (C) 2016 NinjaStix
[email protected]
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.ninjaguild.dragoneggdrop.api;

/**
* Various states capable of being processed during an EnderDragonBattle
*
* @author Parker Hawke - 2008Choco
*/
public enum BattleState {

/**
* The dragon is dead, awaiting to be respawned
*/
DRAGON_DEAD,

/**
* The 4 crystals are starting to spawn on the portal
*/
CRYSTALS_SPAWNING,

/**
* The dragon has started its respawn process, including the spawning
* of the crystals on the pillars
*/
DRAGON_RESPAWNING,

/**
* The battle has initiated, and the dragon is free to roam
*/
BATTLE_COMMENCED,

/**
* The battle has ended and the dragon has been slain. Its death
* animation is playing
*/
BATTLE_END,

/**
* The particles have started descending to drop the loot (if any
* at all)
*/
PARTICLES_START,

/**
* The loot has been spawned on the portal
*/
LOOT_SPAWN;

}
101 changes: 101 additions & 0 deletions src/com/ninjaguild/dragoneggdrop/api/BattleStateChangeEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
DragonEggDrop
Copyright (C) 2016 NinjaStix
[email protected]
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.ninjaguild.dragoneggdrop.api;

import org.bukkit.entity.EnderDragon;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;

/**
* Called when the state of the EnderDragon battle changes
*
* @author Parker Hawke - 2008Choco
*/
public class BattleStateChangeEvent extends Event {

private static final HandlerList handlers = new HandlerList();

private final Object dragonBattle; // net.minecraft.server.EnderDragonBattle
private final EnderDragon dragon;

private final BattleState previousState, newState;

/**
* Construct a new BattleStateChangeEvent
*
* @param dragonBattle - An instance of the EnderDragonBattle
* @param dragon - An instance of the dragon
* @param previousState - The previous state of the battle
* @param newState - The new state of the battle
*/
public BattleStateChangeEvent(Object dragonBattle, EnderDragon dragon, BattleState previousState, BattleState newState) {
this.dragonBattle = dragonBattle;
this.dragon = dragon;
this.previousState = previousState;
this.newState = newState;
}

/**
* Get an instance of the EnderDragonBattle involved in
* this event
*
* @return the involved EnderDragonBattle
*/
public Object getDragonBattle() {
return dragonBattle;
}

/**
* Get an instance of the EnderDragon involved in this
* event
*
* @return the involved dragon
*/
public EnderDragon getDragon() {
return dragon;
}

/**
* Get the state that the battle was in prior to this change
*
* @return the previous battle state
*/
public BattleState getPreviousState() {
return previousState;
}

/**
* Get the new state of the battle
*
* @return the new battle state
*/
public BattleState getNewState() {
return newState;
}

@Override
public HandlerList getHandlers() {
return handlers;
}

public static HandlerList getHandlerList() {
return handlers;
}
}
15 changes: 14 additions & 1 deletion src/com/ninjaguild/dragoneggdrop/events/DragonLifeListeners.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@
import java.util.Random;

import com.ninjaguild.dragoneggdrop.DragonEggDrop;
import com.ninjaguild.dragoneggdrop.api.BattleState;
import com.ninjaguild.dragoneggdrop.api.BattleStateChangeEvent;
import com.ninjaguild.dragoneggdrop.utils.runnables.DragonDeathRunnable;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.World;
import org.bukkit.entity.EnderDragon;
Expand All @@ -49,6 +52,7 @@ public void onCreatureSpawn(CreatureSpawnEvent event) {
if (!(event.getEntity() instanceof EnderDragon)) return;

EnderDragon dragon = (EnderDragon) event.getEntity();
Object dragonBattle = plugin.getNMSAbstract().getEnderDragonBattleFromDragon(dragon);
plugin.getDEDManager().setRespawnInProgress(false);

List<String> dragonNames = plugin.getDEDManager().getDragonNames();
Expand All @@ -58,24 +62,33 @@ public void onCreatureSpawn(CreatureSpawnEvent event) {

plugin.getDEDManager().setDragonBossBarTitle(name, plugin.getDEDManager().getEnderDragonBattleFromDragon(dragon));
}

BattleStateChangeEvent bscEventCrystals = new BattleStateChangeEvent(dragonBattle, dragon, BattleState.DRAGON_RESPAWNING, BattleState.BATTLE_COMMENCED);
Bukkit.getPluginManager().callEvent(bscEventCrystals);
}

@EventHandler
public void onDragonDeath(EntityDeathEvent event) {
if (!(event.getEntity() instanceof EnderDragon)) return;

EnderDragon dragon = (EnderDragon) event.getEntity();
Object dragonBattle = plugin.getNMSAbstract().getEnderDragonBattleFromDragon(dragon);
boolean prevKilled = this.plugin.getNMSAbstract().hasBeenPreviouslyKilled(dragon); // PreviouslyKilled
World world = event.getEntity().getWorld();

BattleStateChangeEvent bscEventCrystals = new BattleStateChangeEvent(dragonBattle, dragon, BattleState.BATTLE_COMMENCED, BattleState.BATTLE_END);
Bukkit.getPluginManager().callEvent(bscEventCrystals);

new BukkitRunnable() {
@Override
public void run() {
if (plugin.getNMSAbstract().getEnderDragonDeathAnimationTime(dragon)>= 185) { // Dragon is dead at 200
this.cancel();
new DragonDeathRunnable(plugin, world, prevKilled);
new DragonDeathRunnable(plugin, world, dragon, prevKilled);
}
}
}.runTaskTimer(plugin, 0L, 1L);


}
}
12 changes: 7 additions & 5 deletions src/com/ninjaguild/dragoneggdrop/events/RespawnListeners.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public RespawnListeners(DragonEggDrop plugin) {

@EventHandler
public void onPlayerSwitchWorlds(PlayerChangedWorldEvent event) {
if (!plugin.getConfig().getBoolean("respawn", false)) return;
if (!plugin.getConfig().getBoolean("respawn-on-join", false)
&& !plugin.getConfig().getBoolean("respawn-on-death", true)) return;

World fromWorld = event.getFrom(), toWorld = event.getPlayer().getWorld();

Expand All @@ -57,8 +58,8 @@ public void onPlayerSwitchWorlds(PlayerChangedWorldEvent event) {
}
}

if (toWorld.getEnvironment() == Environment.THE_END) {
if (toWorld.getPlayers().size() == 1) {
if (plugin.getConfig().getBoolean("respawn-on-join", false)) {
if (toWorld.getEnvironment() == Environment.THE_END && toWorld.getPlayers().size() == 1) {
// Schedule respawn, if not dragon exists, or in progress
for (int y = toWorld.getMaxHeight(); y > 0; y--) {
Block block = toWorld.getBlockAt(0, y, 0);
Expand All @@ -80,7 +81,7 @@ public void onPlayerJoin(PlayerJoinEvent event) {
}

// Dragon respawn logic
if (!plugin.getConfig().getBoolean("respawn", false)) return;
if (!plugin.getConfig().getBoolean("respawn-on-join", false)) return;

World world = player.getWorld();
if (world.getEnvironment() != Environment.THE_END || !world.getPlayers().isEmpty()) return;
Expand All @@ -96,7 +97,8 @@ public void onPlayerJoin(PlayerJoinEvent event) {

@EventHandler
public void onPlayerLeave(PlayerQuitEvent event) {
if (!plugin.getConfig().getBoolean("respawn", false)) return;
if (!plugin.getConfig().getBoolean("respawn-on-join", false)
&& !plugin.getConfig().getBoolean("respawn-on-death", true)) return;

World world = event.getPlayer().getWorld();
if (world.getEnvironment() != Environment.THE_END || world.getPlayers().size() != 1) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,18 @@
import java.util.concurrent.ThreadLocalRandom;

import com.ninjaguild.dragoneggdrop.DragonEggDrop;
import com.ninjaguild.dragoneggdrop.api.BattleState;
import com.ninjaguild.dragoneggdrop.api.BattleStateChangeEvent;
import com.ninjaguild.dragoneggdrop.utils.ParticleShapeDefinition;
import com.ninjaguild.dragoneggdrop.utils.manager.DEDManager.RespawnType;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Particle;
import org.bukkit.World;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.EnderDragon;
import org.bukkit.scheduler.BukkitRunnable;

/**
Expand All @@ -54,6 +58,7 @@ public class DragonDeathRunnable extends BukkitRunnable {
private long particleInterval = 0L;
private int lightningAmount;

private EnderDragon dragon;
private boolean respawnDragon = false;
private String rewardType;

Expand All @@ -69,9 +74,10 @@ public class DragonDeathRunnable extends BukkitRunnable {
* @param world - The world in which the dragon death is taking place
* @param prevKilled - Whether the dragon was previously killed or not
*/
public DragonDeathRunnable(final DragonEggDrop plugin, final World world, boolean prevKilled) {
public DragonDeathRunnable(final DragonEggDrop plugin, final World world, EnderDragon dragon, boolean prevKilled) {
this.plugin = plugin;
this.world = world;
this.dragon = dragon;
this.placeEgg = prevKilled;

FileConfiguration config = plugin.getConfig();
Expand Down Expand Up @@ -114,6 +120,10 @@ else if (shape.equalsIgnoreCase("OPEN_END_HELIX")) {

this.respawnDragon = config.getBoolean("respawn", false);
this.runTaskTimer(plugin, 0, this.particleInterval);

Object dragonBattle = plugin.getNMSAbstract().getEnderDragonBattleFromDragon(dragon);
BattleStateChangeEvent bscEventCrystals = new BattleStateChangeEvent(dragonBattle, dragon, BattleState.BATTLE_END, BattleState.PARTICLES_START);
Bukkit.getPluginManager().callEvent(bscEventCrystals);
}

@Override
Expand Down Expand Up @@ -164,10 +174,14 @@ else if (rewardType.equalsIgnoreCase("ALL")) {
}
}

if (respawnDragon && world.getPlayers().size() > 0) {
if (respawnDragon && world.getPlayers().size() > 0 && plugin.getConfig().getBoolean("respawn-on-death", true)) {
plugin.getDEDManager().startRespawn(location, RespawnType.DEATH);
}

Object dragonBattle = plugin.getNMSAbstract().getEnderDragonBattleFromDragon(dragon);
BattleStateChangeEvent bscEventCrystals = new BattleStateChangeEvent(dragonBattle, dragon, BattleState.PARTICLES_START, BattleState.LOOT_SPAWN);
Bukkit.getPluginManager().callEvent(bscEventCrystals);

this.cancel();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@
package com.ninjaguild.dragoneggdrop.utils.runnables;

import com.ninjaguild.dragoneggdrop.DragonEggDrop;
import com.ninjaguild.dragoneggdrop.api.BattleState;
import com.ninjaguild.dragoneggdrop.api.BattleStateChangeEvent;

import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.Particle;
import org.bukkit.entity.EnderCrystal;
import org.bukkit.entity.EnderDragon;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.scheduler.BukkitRunnable;
Expand Down Expand Up @@ -60,12 +64,16 @@ public void run() {
};

Object dragonBattle = plugin.getDEDManager().getEnderDragonBattleFromWorld(eggLocation.getWorld());
EnderDragon dragon = plugin.getNMSAbstract().getEnderDragonFromBattle(dragonBattle);

for (int i = 0; i < crystalLocs.length; i++) {
Location cLoc = crystalLocs[i];
new BukkitRunnable() {
@Override
public void run() {
BattleStateChangeEvent bscEventCrystals = new BattleStateChangeEvent(dragonBattle, dragon, BattleState.DRAGON_DEAD, BattleState.CRYSTALS_SPAWNING);
Bukkit.getPluginManager().callEvent(bscEventCrystals);

Chunk crystalChunk = eggLocation.getWorld().getChunkAt(cLoc);
if (!crystalChunk.isLoaded()) {
crystalChunk.load();
Expand All @@ -87,6 +95,9 @@ public void run() {
if (cLoc.equals(crystalLocs[crystalLocs.length - 1])) {
plugin.getNMSAbstract().respawnEnderDragon(dragonBattle);
plugin.getDEDManager().setRespawnInProgress(true);

BattleStateChangeEvent bscEventRespawning = new BattleStateChangeEvent(dragonBattle, dragon, BattleState.CRYSTALS_SPAWNING, BattleState.DRAGON_RESPAWNING);
Bukkit.getPluginManager().callEvent(bscEventRespawning);
}
}

Expand Down
Loading

0 comments on commit c8186cb

Please sign in to comment.