Skip to content

Commit c8ce8f7

Browse files
committed
v0.2: rewrite AbilityAPI:
* Add User for simple, extendable Player data management, * Add SequenceBlueprint, replace return type in AbilityProvider#getSequence(), * Remove AbilityListeners, * Simplify Ability class, * Add Ability#isExecuting(), if returns false Ability will stop, * Simplify exposed API; make API return interfaces, * Rename default action names (Actions class) to be less confusing * Pass AbilityManager and the related Sequence instance to the AbilityProvider#createInstance(...) abstract method, * Add successful events array to Sequence to allow flexibility.
1 parent 8bbadfb commit c8ce8f7

30 files changed

Lines changed: 933 additions & 938 deletions

‎src/main/java/com/github/abilityapi/AbilityAPI.java‎

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,50 +11,60 @@
1111

1212
package com.github.abilityapi;
1313

14+
import com.github.abilityapi.ability.AbilityManager;
15+
import com.github.abilityapi.ability.AbilityRegistry;
1416
import com.github.abilityapi.metrics.MetricService;
17+
import com.github.abilityapi.sequence.SequenceInvoker;
18+
import com.github.abilityapi.sequence.SequenceManager;
19+
import com.github.abilityapi.services.AbilityService;
20+
import com.github.abilityapi.services.SequenceService;
1521
import com.github.abilityapi.test.TestAbilityProvider;
16-
import com.github.abilityapi.trigger.TriggerManager;
1722
import org.bukkit.plugin.java.JavaPlugin;
1823

24+
import java.util.ArrayList;
25+
import java.util.List;
26+
1927
public class AbilityAPI extends JavaPlugin {
2028

2129
private static AbilityAPI instance;
2230

23-
private final AbilityRegistry abilityRegistry = new AbilityRegistry();
2431
private final AbilityManager abilityManager = new AbilityManager(this);
25-
private final TriggerManager triggerManager = new TriggerManager(this, abilityRegistry, abilityManager);
32+
private final SequenceManager sequenceManager = new SequenceManager(abilityManager);
2633

27-
private final AbilityService abilityService = new AbilityService(this, abilityManager, triggerManager);
2834
private final MetricService metricService = new MetricService(this);
35+
private final AbilityService abilityService = new AbilityService(this, abilityManager);
36+
private final SequenceService sequenceService = new SequenceService(this, sequenceManager);
37+
38+
private final List<Service> services = new ArrayList<>();
2939

3040
public static AbilityAPI get() {
3141
return instance;
3242
}
3343

34-
public AbilityRegistry getRegistry() {
35-
return abilityRegistry;
36-
}
37-
38-
public AbilityManager getAbilityManager() {
39-
return abilityManager;
40-
}
41-
42-
public TriggerManager getTriggerManager() {
43-
return triggerManager;
44-
}
45-
4644
@Override
4745
public void onEnable() {
4846
instance = this;
49-
abilityService.start();
50-
metricService.start();
5147

52-
abilityRegistry.register(new TestAbilityProvider());
48+
services.add(metricService);
49+
services.add(abilityService);
50+
services.add(sequenceService);
51+
services.forEach(Service::start);
52+
53+
sequenceManager.register(new TestAbilityProvider());
5354
}
5455

5556
@Override
5657
public void onDisable() {
58+
services.forEach(Service::stop);
5759
instance = null;
5860
}
5961

62+
public AbilityRegistry getAbilityRegistry() {
63+
return sequenceManager;
64+
}
65+
66+
public SequenceInvoker getSequenceInvoker() {
67+
return sequenceManager;
68+
}
69+
6070
}

src/main/java/com/github/abilityapi/listener/ListenerRunnable.java renamed to src/main/java/com/github/abilityapi/ability/Ability.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,18 @@
99
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1010
*/
1111

12-
package com.github.abilityapi.listener;
12+
package com.github.abilityapi.ability;
1313

14-
public interface ListenerRunnable {
14+
import org.bukkit.event.Listener;
1515

16-
/**
17-
* @return true if the surrounding AbilityListener should be cancelled.
18-
* @see AbilityListener
19-
*/
20-
boolean run();
16+
public interface Ability extends Listener {
17+
18+
void start();
19+
20+
void update();
21+
22+
void stop();
23+
24+
boolean isExecuting();
2125

2226
}

src/main/java/com/github/abilityapi/AbilityManager.java renamed to src/main/java/com/github/abilityapi/ability/AbilityManager.java

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,62 +9,55 @@
99
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1010
*/
1111

12-
package com.github.abilityapi;
12+
package com.github.abilityapi.ability;
1313

14+
import com.github.abilityapi.sequence.Sequence;
15+
import com.github.abilityapi.user.User;
1416
import org.bukkit.Bukkit;
15-
import org.bukkit.entity.Player;
1617
import org.bukkit.event.HandlerList;
17-
import org.bukkit.event.Listener;
1818
import org.bukkit.plugin.java.JavaPlugin;
1919

20-
import java.util.HashMap;
21-
import java.util.Iterator;
22-
import java.util.Map;
20+
import java.util.List;
21+
import java.util.concurrent.CopyOnWriteArrayList;
2322

2423
public class AbilityManager {
2524

25+
private final List<Ability> abilities = new CopyOnWriteArrayList<>();
2626
private final JavaPlugin plugin;
27-
private final Map<Player, Ability> executing = new HashMap<>();
2827

2928
public AbilityManager(JavaPlugin plugin) {
3029
this.plugin = plugin;
3130
}
3231

33-
public void execute(Player player, AbilityProvider provider) {
34-
Ability ability = provider.createInstance(player);
35-
ability.start();
36-
37-
if (ability instanceof Listener) {
38-
Bukkit.getPluginManager().registerEvents((Listener) ability, plugin);
39-
}
32+
public void execute(User user, Sequence sequence, AbilityProvider provider) {
33+
Ability ability = provider.createInstance(this, sequence, user);
34+
abilities.add(ability);
4035

41-
executing.put(player, ability);
36+
Bukkit.getPluginManager().registerEvents(ability, plugin);
37+
ability.start();
4238
}
4339

44-
public void checkExpire() {
45-
Iterator<Ability> it = executing.values().iterator();
46-
while (it.hasNext()) {
47-
Ability ability = it.next();
48-
if (ability.hasExpired()) {
49-
ability.stop();
50-
51-
if (ability instanceof Listener) {
52-
HandlerList.unregisterAll((Listener) ability);
53-
}
40+
public void updateAll() {
41+
abilities.forEach(Ability::update);
42+
}
5443

55-
it.remove();
44+
public void cleanup() {
45+
abilities.removeIf(ability -> {
46+
if (ability.isExecuting()) {
47+
return false;
5648
}
57-
}
58-
}
5949

60-
public void updateAll() {
61-
for (Ability ability : executing.values()) {
62-
ability.update();
63-
}
50+
HandlerList.unregisterAll(ability);
51+
ability.stop();
52+
return true;
53+
});
6454
}
6555

66-
public Map<Player, Ability> getExecuting() {
67-
return executing;
56+
public void stop(Ability ability) {
57+
HandlerList.unregisterAll(ability);
58+
ability.stop();
59+
60+
abilities.remove(ability);
6861
}
6962

7063
}

src/main/java/com/github/abilityapi/AbilityProvider.java renamed to src/main/java/com/github/abilityapi/ability/AbilityProvider.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@
99
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1010
*/
1111

12-
package com.github.abilityapi;
12+
package com.github.abilityapi.ability;
1313

14-
import com.github.abilityapi.trigger.Trigger;
15-
import org.bukkit.entity.Player;
14+
import com.github.abilityapi.sequence.Sequence;
15+
import com.github.abilityapi.sequence.SequenceBlueprint;
16+
import com.github.abilityapi.user.User;
1617

17-
public interface AbilityProvider<T extends Ability> {
18+
public interface AbilityProvider {
1819

19-
Trigger getTrigger();
20+
SequenceBlueprint getSequence();
2021

21-
T createInstance(Player player);
22+
Ability createInstance(AbilityManager manager, Sequence sequence, User user);
2223

2324
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* The MIT License (MIT)
3+
* Copyright (c) 2016 Chris Martin (OmniCypher-), Dylan Curzon (curz46), Connor Hartley (connorhartley)
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
*
7+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
*
9+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10+
*/
11+
12+
package com.github.abilityapi.ability;
13+
14+
public interface AbilityRegistry {
15+
16+
void register(AbilityProvider provider);
17+
18+
}

‎src/main/java/com/github/abilityapi/listener/AbilityListener.java‎

Lines changed: 0 additions & 102 deletions
This file was deleted.

0 commit comments

Comments
 (0)