[Clans] World event state system

This commit is contained in:
Shaun Bennett 2015-05-21 15:33:02 -05:00
parent 4ac4e9d729
commit ff377a3e5c
15 changed files with 235 additions and 88 deletions

View File

@ -10,7 +10,4 @@
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" assert-keyword="true" jdk-15="true" project-jdk-name="1.7" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" assert-keyword="true" jdk-15="true" project-jdk-name="1.7" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" /> <output url="file://$PROJECT_DIR$/out" />
</component> </component>
<component name="TaskProjectConfiguration">
<server type="JIRA" url="http://184.154.0.242:8080" />
</component>
</project> </project>

View File

@ -1,69 +0,0 @@
package mineplex.game.clans.clans.worldevent;
import java.util.ArrayList;
import java.util.List;
public abstract class AbstractWorldEvent
{
private List<WorldEventListener> _listeners;
private boolean _running;
private int _ticks;
public AbstractWorldEvent()
{
_listeners = new ArrayList<WorldEventListener>();
}
public final void start()
{
_running = true;
customStart();
}
protected abstract void customStart();
public final void cancel()
{
_running = false;
customCancel();
}
protected abstract void customCancel();
public void tick()
{
_ticks++;
}
public boolean isRunning()
{
return _running;
}
public void addListener(WorldEventListener listener)
{
_listeners.add(listener);
}
public void removeListener(WorldEventListener listener)
{
_listeners.remove(listener);
}
protected List<WorldEventListener> getListeners()
{
return _listeners;
}
public void clearListeners()
{
_listeners.clear();
}
public int getTicks()
{
return _ticks;
}
}

View File

@ -2,14 +2,16 @@ package mineplex.game.clans.clans.worldevent;
import org.bukkit.Location; import org.bukkit.Location;
import mineplex.game.clans.clans.worldevent.boss.GiantBoss; import mineplex.game.clans.clans.worldevent.event.AbstractWorldEvent;
import mineplex.game.clans.clans.worldevent.event.WorldEventType;
import mineplex.game.clans.clans.worldevent.event.boss.slime.SlimeBoss;
public class ConcreteWorldEventFactory implements WorldEventFactory public class ConcreteWorldEventFactory implements WorldEventFactory
{ {
@Override @Override
public AbstractWorldEvent fromName(Location location, String name) public AbstractWorldEvent fromName(Location location, String name)
{ {
return new GiantBoss(location); return new SlimeBoss(location);
} }
@Override @Override

View File

@ -2,6 +2,9 @@ package mineplex.game.clans.clans.worldevent;
import org.bukkit.Location; import org.bukkit.Location;
import mineplex.game.clans.clans.worldevent.event.AbstractWorldEvent;
import mineplex.game.clans.clans.worldevent.event.WorldEventType;
public interface WorldEventFactory public interface WorldEventFactory
{ {
public AbstractWorldEvent fromName(Location location, String name); public AbstractWorldEvent fromName(Location location, String name);

View File

@ -1,5 +1,7 @@
package mineplex.game.clans.clans.worldevent; package mineplex.game.clans.clans.worldevent;
import mineplex.game.clans.clans.worldevent.event.AbstractWorldEvent;
public interface WorldEventListener public interface WorldEventListener
{ {
public void onComplete(AbstractWorldEvent event); public void onComplete(AbstractWorldEvent event);

View File

@ -12,6 +12,7 @@ import mineplex.core.MiniPlugin;
import mineplex.core.updater.UpdateType; import mineplex.core.updater.UpdateType;
import mineplex.core.updater.event.UpdateEvent; import mineplex.core.updater.event.UpdateEvent;
import mineplex.game.clans.clans.worldevent.command.WorldEventCommand; import mineplex.game.clans.clans.worldevent.command.WorldEventCommand;
import mineplex.game.clans.clans.worldevent.event.AbstractWorldEvent;
public class WorldEventManager extends MiniPlugin implements WorldEventListener public class WorldEventManager extends MiniPlugin implements WorldEventListener
{ {

View File

@ -1,6 +0,0 @@
package mineplex.game.clans.clans.worldevent;
public enum WorldEventType
{
BOSS_FIGHT;
}

View File

@ -6,7 +6,7 @@ import mineplex.core.command.CommandBase;
import mineplex.core.common.Rank; import mineplex.core.common.Rank;
import mineplex.core.common.util.F; import mineplex.core.common.util.F;
import mineplex.core.common.util.UtilPlayer; import mineplex.core.common.util.UtilPlayer;
import mineplex.game.clans.clans.worldevent.AbstractWorldEvent; import mineplex.game.clans.clans.worldevent.event.AbstractWorldEvent;
import mineplex.game.clans.clans.worldevent.WorldEventManager; import mineplex.game.clans.clans.worldevent.WorldEventManager;
public class StartCommand extends CommandBase<WorldEventManager> public class StartCommand extends CommandBase<WorldEventManager>

View File

@ -0,0 +1,120 @@
package mineplex.game.clans.clans.worldevent.event;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.bukkit.event.HandlerList;
import org.bukkit.plugin.java.JavaPlugin;
import mineplex.game.clans.clans.worldevent.WorldEventListener;
import mineplex.game.clans.clans.worldevent.WorldEventManager;
import mineplex.game.clans.clans.worldevent.event.state.EventState;
public abstract class AbstractWorldEvent
{
private WorldEventManager _eventManager;
private List<WorldEventListener> _listeners;
private boolean _running;
private int _ticks;
private Random _random;
// Event States
private List<EventState> _states;
private int _maxTicksPerState;
private int _minTicksPerState;
private EventState _currentState;
public AbstractWorldEvent(WorldEventManager eventManager)
{
_eventManager = eventManager;
_listeners = new ArrayList<WorldEventListener>();
_states = new ArrayList<EventState>();
_random = new Random();
}
public final void start()
{
_running = true;
customStart();
}
protected abstract void customStart();
public final void cancel()
{
_running = false;
customCancel();
}
protected abstract void customCancel();
public void tick()
{
_ticks++;
if (_currentState != null) _currentState.tick();
}
public boolean isRunning()
{
return _running;
}
public void addListener(WorldEventListener listener)
{
_listeners.add(listener);
}
public void removeListener(WorldEventListener listener)
{
_listeners.remove(listener);
}
protected List<WorldEventListener> getListeners()
{
return _listeners;
}
public void clearListeners()
{
_listeners.clear();
}
public int getTicks()
{
return _ticks;
}
public WorldEventManager getEventManager()
{
return _eventManager;
}
public EventState getRandomState()
{
return _states.get(_random.nextInt(_states.size()));
}
public void setState(EventState state)
{
JavaPlugin plugin = _eventManager.getPlugin();
// Unregister old state listener
HandlerList.unregisterAll(_currentState);
_currentState.onStateStop();
// Register new state listener
plugin.getServer().getPluginManager().registerEvents(state, plugin);
state.onStateStart();
_currentState = state;
}
public void addState(EventState state)
{
_states.add(state);
}
}

View File

@ -0,0 +1,6 @@
package mineplex.game.clans.clans.worldevent.event;
public enum WorldEventType
{
BOSS_FIGHT;
}

View File

@ -1,5 +1,6 @@
package mineplex.game.clans.clans.worldevent.boss; package mineplex.game.clans.clans.worldevent.event.boss;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -7,8 +8,10 @@ import org.bukkit.Location;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import mineplex.core.common.util.UtilTextBottom; import mineplex.core.common.util.UtilTextBottom;
import mineplex.game.clans.clans.worldevent.AbstractWorldEvent; import mineplex.game.clans.clans.worldevent.WorldEventManager;
import mineplex.game.clans.clans.worldevent.event.AbstractWorldEvent;
import mineplex.game.clans.clans.worldevent.WorldEventListener; import mineplex.game.clans.clans.worldevent.WorldEventListener;
import mineplex.game.clans.clans.worldevent.event.state.EventState;
public abstract class AbstractBoss extends AbstractWorldEvent public abstract class AbstractBoss extends AbstractWorldEvent
{ {
@ -21,9 +24,9 @@ public abstract class AbstractBoss extends AbstractWorldEvent
private Location _center; private Location _center;
private float _radiusSquared; private float _radiusSquared;
public AbstractBoss(WorldEventManager eventManager, String name, Location center, float radius, double maxHealth)
public AbstractBoss(String name, Location center, float radius, double maxHealth)
{ {
super(eventManager);
_center = center; _center = center;
_name = name; _name = name;
_health = maxHealth; _health = maxHealth;

View File

@ -1,11 +1,13 @@
package mineplex.game.clans.clans.worldevent.boss; package mineplex.game.clans.clans.worldevent.event.boss.slime;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
public class GiantBoss extends AbstractBoss import mineplex.game.clans.clans.worldevent.event.boss.AbstractBoss;
public class SlimeBoss extends AbstractBoss
{ {
public GiantBoss(Location center) public SlimeBoss(Location center)
{ {
super("Giant", center, 20, 300); super("Giant", center, 20, 300);
} }

View File

@ -0,0 +1,20 @@
package mineplex.game.clans.clans.worldevent.event.boss.state;
import mineplex.game.clans.clans.worldevent.event.boss.AbstractBoss;
import mineplex.game.clans.clans.worldevent.event.state.EventState;
public abstract class BossState extends EventState
{
private AbstractBoss _boss;
public BossState(AbstractBoss boss)
{
super(boss);
_boss = boss;
}
public AbstractBoss getBoss()
{
return _boss;
}
}

View File

@ -0,0 +1,27 @@
package mineplex.game.clans.clans.worldevent.event.boss.state;
import mineplex.game.clans.clans.worldevent.event.boss.AbstractBoss;
public class SlamState extends BossState
{
public SlamState(AbstractBoss boss)
{
super(boss);
}
@Override
public void onTick()
{
}
@Override
public void onStateStart()
{
}
@Override
public void onStateStop()
{
}
}

View File

@ -0,0 +1,39 @@
package mineplex.game.clans.clans.worldevent.event.state;
import org.bukkit.event.Listener;
import mineplex.game.clans.clans.worldevent.event.AbstractWorldEvent;
public abstract class EventState implements Listener
{
private AbstractWorldEvent _event;
private int _ticks;
public EventState(AbstractWorldEvent event)
{
_event = event;
_ticks = 0;
}
public AbstractWorldEvent getEvent()
{
return _event;
}
public final void tick()
{
_ticks++;
onTick();
}
public int getTicks()
{
return _ticks;
}
public abstract void onTick();
public abstract void onStateStart();
public abstract void onStateStop();
}