Skip to content

Commit

Permalink
Fix Problems with Event Concurrency (#38)
Browse files Browse the repository at this point in the history
## Additions
* Added an option in worldevent files to specify other events which cannot occur at the same time as the configured event. E.g. if SolarEclipse is listed in the SozinsComet blacklist, then whenever they would happen concurrently SozinsComet instead overpowers / cancels SolarEclipse.

## Misc. Changes
* Changed options which are two words to be separated by `-` rather than just combined together for easier readability.
  • Loading branch information
Simplicitee authored and ChristopherWMM committed Nov 11, 2019
1 parent 12c6452 commit 391d0b3
Show file tree
Hide file tree
Showing 14 changed files with 356 additions and 115 deletions.
39 changes: 32 additions & 7 deletions src/com/projectkorra/rpg/RPGListener.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.projectkorra.rpg;

import java.lang.reflect.Field;

import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.block.BlockFace;
Expand All @@ -13,20 +15,22 @@

import com.projectkorra.projectkorra.BendingPlayer;
import com.projectkorra.projectkorra.Element;
import com.projectkorra.projectkorra.Element.SubElement;
import com.projectkorra.projectkorra.ability.CoreAbility;
import com.projectkorra.projectkorra.ability.EarthAbility;
import com.projectkorra.projectkorra.attribute.Attribute;
import com.projectkorra.projectkorra.attribute.AttributeModifier;
import com.projectkorra.projectkorra.avatar.AvatarState;
import com.projectkorra.projectkorra.event.AbilityStartEvent;
import com.projectkorra.projectkorra.event.BendingPlayerCreationEvent;
import com.projectkorra.projectkorra.event.PlayerChangeElementEvent;
import com.projectkorra.projectkorra.event.PlayerChangeElementEvent.Result;
import com.projectkorra.rpg.configuration.ConfigManager;
import com.projectkorra.rpg.events.SunRiseEvent;
import com.projectkorra.rpg.events.SunSetEvent;
import com.projectkorra.rpg.events.WorldEventEndEvent;
import com.projectkorra.rpg.events.WorldEventStartEvent;
import com.projectkorra.rpg.worldevent.WorldEvent;
import com.projectkorra.rpg.worldevent.event.SunRiseEvent;
import com.projectkorra.rpg.worldevent.event.SunSetEvent;
import com.projectkorra.rpg.worldevent.event.WorldEventEndEvent;
import com.projectkorra.rpg.worldevent.event.WorldEventStartEvent;
import com.projectkorra.rpg.worldevent.util.Time;

public class RPGListener implements Listener {
Expand Down Expand Up @@ -89,12 +93,33 @@ public void onAbilityStart(AbilityStartEvent event) {
}

for (WorldEvent we : ProjectKorraRPG.getEventManager().getEventsHappening(world)) {
if (ability.getElement().equals(we.getElement())) {
Element e = ability.getElement();

if (e instanceof SubElement && !we.getElements().contains(e)) {
e = ((SubElement) e).getParentElement();
}

if (we.getElements().contains(e)) {
if (we.getModifier() <= 0) {
event.setCancelled(true);
} else {
for (String attribute : we.getAttributes()) {
String[] split = attribute.split("::");
boolean passed = false;

for (Field f : ability.getClass().getDeclaredFields()) {
if (f.isAnnotationPresent(Attribute.class)) {
if (f.getAnnotation(Attribute.class).value().equals(split[0])) {
passed = true;
break;
}
}
}

if (!passed) {
continue;
}

ability.addAttributeModifier(split[0], we.getModifier(), AttributeModifier.valueOf(split[1].toUpperCase()));
}
}
Expand Down Expand Up @@ -146,7 +171,7 @@ public void onSunRise(SunRiseEvent event) {
continue;
}

ProjectKorraRPG.getEventManager().startEvent(event.getWorld(), wEvent, true);
ProjectKorraRPG.getEventManager().startEvent(event.getWorld(), wEvent);
}
} else {
if (ProjectKorraRPG.getEventManager().isHappening(event.getWorld(), wEvent)) {
Expand Down Expand Up @@ -175,7 +200,7 @@ public void onSunSet(SunSetEvent event) {
continue;
}

ProjectKorraRPG.getEventManager().startEvent(event.getWorld(), wEvent, true);
ProjectKorraRPG.getEventManager().startEvent(event.getWorld(), wEvent);
}
} else if (wEvent.getTime() == Time.DAY) {
if (ProjectKorraRPG.getEventManager().isHappening(event.getWorld(), wEvent)) {
Expand Down
6 changes: 3 additions & 3 deletions src/com/projectkorra/rpg/commands/EventCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void execute(CommandSender sender, List<String> args) {
sender.sendMessage(ChatColor.YELLOW + "Current WorldEvents: ");

for (WorldEvent event : ProjectKorraRPG.getEventManager().getEventsHappening(world)) {
sender.sendMessage(event.getElement().getColor() + "- " + event.getName());
sender.sendMessage(event.getTextColor() + "- " + event.getName());
}
} else if (args.size() == 2) {
String name = args.get(1);
Expand All @@ -71,7 +71,7 @@ public void execute(CommandSender sender, List<String> args) {
sender.sendMessage(ChatColor.RED + "There is no WorldEvent to end at the moment.");
} else if (args.size() == 1) {
for (WorldEvent event : WorldEvent.getEvents()) {
ProjectKorraRPG.getEventManager().endEvent(world, event);
ProjectKorraRPG.getEventManager().endEvent(world, event, false);
}

sender.sendMessage(ChatColor.GOLD + "All WorldEvents have been ended!");
Expand All @@ -84,7 +84,7 @@ public void execute(CommandSender sender, List<String> args) {
} else if (!ProjectKorraRPG.getEventManager().isHappening(world, event)) {
sender.sendMessage(ChatColor.RED + "That event is currently not happening in this world!");
} else {
ProjectKorraRPG.getEventManager().endEvent(world, event);
ProjectKorraRPG.getEventManager().endEvent(world, event, false);
sender.sendMessage(ChatColor.GOLD + "You have ended the " + event.getName() + " event!");
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/com/projectkorra/rpg/commands/HelpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ public void execute(CommandSender sender, List<String> args) {
for (WorldEvent event : WorldEvent.getEvents()) {
if (args.get(0).equalsIgnoreCase(event.getName()) || event.getAliases().contains(args.get(0).toLowerCase())) {
sender.sendMessage(ChatColor.BOLD + event.getName());
sender.sendMessage(event.getElement().getColor() + event.getDescription());
sender.sendMessage(event.getTextColor() + event.getDescription());
sender.sendMessage(ChatColor.WHITE + "Affected Elements:");
for (Element e : event.getElements()) {
sender.sendMessage("- " + e.getColor() + e.getName());
}
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.projectkorra.rpg.worldevent.event;
package com.projectkorra.rpg.events;

import org.bukkit.World;
import org.bukkit.event.Event;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.projectkorra.rpg.worldevent.event;
package com.projectkorra.rpg.events;

import org.bukkit.World;
import org.bukkit.event.Event;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.projectkorra.rpg.worldevent.event;
package com.projectkorra.rpg.events;

import org.bukkit.World;
import org.bukkit.event.Cancellable;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.projectkorra.rpg.worldevent.event;
package com.projectkorra.rpg.events;

import org.bukkit.World;
import org.bukkit.event.Cancellable;
Expand Down
27 changes: 26 additions & 1 deletion src/com/projectkorra/rpg/worldevent/IWorldEvent.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.projectkorra.rpg.worldevent;

import java.util.List;
import java.util.Set;

import org.bukkit.ChatColor;
import org.bukkit.boss.BarColor;

import com.projectkorra.projectkorra.Element;
import com.projectkorra.rpg.worldevent.util.Time;
Expand Down Expand Up @@ -34,14 +38,21 @@ public interface IWorldEvent {
* @return list of attributes
*/
public List<String> getAttributes();

/**
* A list of worldevents which cannot concurrently occur with this worldevent
*
* @return blacklisted events
*/
public List<String> getBlacklistedEvents();

/**
* Which element the WorldEvent should affect. This will work with custom
* elements, but be sure the custom element does exist!
*
* @return element the WorldEvent affects
*/
public Element getElement();
public Set<Element> getElements();

/**
* Denotes when the WorldEvent should occur
Expand Down Expand Up @@ -99,4 +110,18 @@ public interface IWorldEvent {
* @return true if the WorldEvent will create fog in the world
*/
public boolean getCreateFog();

/**
* Gets the color of chat text for the event
*
* @return color of chat text
*/
public ChatColor getTextColor();

/**
* Gets the color of the bossbar for the event
*
* @return color of the bossbar
*/
public BarColor getBarColor();
}
62 changes: 34 additions & 28 deletions src/com/projectkorra/rpg/worldevent/WorldEvent.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.projectkorra.rpg.worldevent;

import java.util.Collection;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.bukkit.ChatColor;
import org.bukkit.boss.BarColor;

import com.projectkorra.projectkorra.Element;
import com.projectkorra.rpg.ProjectKorraRPG;
import com.projectkorra.rpg.worldevent.util.Time;
Expand All @@ -15,49 +18,44 @@
public class WorldEvent implements IWorldEvent {

protected static Map<String, WorldEvent> events = new HashMap<>();
protected static Map<Element, Set<WorldEvent>> eventsByElement = new HashMap<>();

static {
for (Element e : Element.getAllElements()) {
eventsByElement.put(e, new HashSet<>());
}
}

private String name;
private String description;
private List<String> aliases;
private List<String> attributes;
private Element element;
private Set<Element> elements;
private Time time;
private int frequency;
private double modifier;
private String startMessage;
private String endMessage;
private boolean darkenSky;
private boolean createFog;
private List<String> eventBlacklist;
private ChatColor text;
private BarColor bar;

public WorldEvent(WorldEventFile wFile) {
this(wFile.getName(), wFile.getDescription(), wFile.getAliases(), wFile.getAttributes(), Element.getElement(wFile.getElement()), Time.valueOf(wFile.getTime().toUpperCase()), wFile.getFrequency(), wFile.getModifier(), wFile.getStartMessage(), wFile.getEndMessage(), wFile.getDarkenSky(), wFile.getCreateFog());
this(wFile.getName(), wFile.getDescription(), wFile.getAliases(), wFile.getAttributes(), wFile.getElements(), wFile.getTime(), wFile.getFrequency(), wFile.getModifier(), wFile.getStartMessage(), wFile.getEndMessage(), wFile.getDarkenSky(), wFile.getCreateFog(), wFile.getEventBlacklist(), wFile.getTextColor(), wFile.getBarColor());
}

public WorldEvent(String name, String description, List<String> aliases, List<String> attributes, Element element, Time time, int frequency, double modifier, String startMessage, String endMessage, boolean darkenSky, boolean createFog) {
public WorldEvent(String name, String description, List<String> aliases, List<String> attributes, Element[] elements, Time time, int frequency, double modifier, String startMessage, String endMessage, boolean darkenSky, boolean createFog, List<String> eventBlacklist, ChatColor text, BarColor bar) {
this.name = name;
this.description = description;
this.aliases = aliases;
this.attributes = attributes;
this.element = element;
this.elements = new HashSet<>(Arrays.asList(elements));
this.time = time;
this.frequency = frequency;
this.modifier = modifier;
this.startMessage = startMessage;
this.endMessage = endMessage;
this.darkenSky = darkenSky;
this.createFog = createFog;
this.eventBlacklist = eventBlacklist;
this.text = text;
this.bar = bar;
events.put(name.toLowerCase(), this);
if (!eventsByElement.containsKey(element)) {
eventsByElement.put(element, new HashSet<>());
}
eventsByElement.get(element).add(this);
}

@Override
Expand All @@ -79,10 +77,15 @@ public List<String> getAliases() {
public List<String> getAttributes() {
return attributes;
}

@Override
public List<String> getBlacklistedEvents() {
return eventBlacklist;
}

@Override
public Element getElement() {
return element;
public Set<Element> getElements() {
return elements;
}

@Override
Expand Down Expand Up @@ -119,23 +122,26 @@ public boolean getDarkenSky() {
public boolean getCreateFog() {
return createFog;
}

@Override
public ChatColor getTextColor() {
return text;
}

@Override
public BarColor getBarColor() {
return bar;
}

public static WorldEvent get(String name) {
return events.containsKey(name.toLowerCase()) ? events.get(name.toLowerCase()) : (ProjectKorraRPG.getFileManager().loadFile(name.toLowerCase()) == null ? null : get(name));
}

public static Set<WorldEvent> getEventsByElement(Element e) {
if (!eventsByElement.containsKey(e)) {
eventsByElement.put(e, new HashSet<>());
}
return eventsByElement.get(e);
}

public static Set<String> getEventNames() {
return events.keySet();
return new HashSet<>(events.keySet());
}

public static Collection<WorldEvent> getEvents() {
return events.values();
public static Set<WorldEvent> getEvents() {
return new HashSet<>(events.values());
}
}
Loading

0 comments on commit 391d0b3

Please sign in to comment.