Add central group creator method

This commit is contained in:
Daniel Saukel 2021-02-27 22:37:39 +01:00
parent eae11e7b81
commit 5e7b28ea55
11 changed files with 58 additions and 109 deletions

View File

@ -40,6 +40,7 @@ public class GroupCreateEvent extends GroupEvent implements Cancellable {
*/
GROUP_ADAPTER,
GROUP_SIGN,
TUTORIAL,
/**
* When a group is created by an addon.
*/

View File

@ -29,6 +29,7 @@ import de.erethon.dungeonsxl.api.dungeon.Dungeon;
import de.erethon.dungeonsxl.api.dungeon.Game;
import de.erethon.dungeonsxl.api.dungeon.GameRule;
import de.erethon.dungeonsxl.api.dungeon.GameRuleContainer;
import de.erethon.dungeonsxl.api.event.group.GroupCreateEvent;
import de.erethon.dungeonsxl.api.mob.DungeonMob;
import de.erethon.dungeonsxl.api.mob.ExternalMobProvider;
import de.erethon.dungeonsxl.api.player.GroupAdapter;
@ -680,27 +681,31 @@ public class DungeonsXL extends DREPlugin implements DungeonsAPI {
/* Object initialization */
@Override
public PlayerGroup createGroup(Player leader) {
return new DGroup(this, leader);
return DGroup.create(this, GroupCreateEvent.Cause.CUSTOM, leader, null, null, null);
}
@Override
public PlayerGroup createGroup(Player leader, PlayerGroup.Color color) {
return new DGroup(this, leader, color);
return DGroup.create(this, GroupCreateEvent.Cause.CUSTOM, leader, null, color, null);
}
@Override
public PlayerGroup createGroup(Player leader, String name) {
return new DGroup(this, name, leader);
return DGroup.create(this, GroupCreateEvent.Cause.CUSTOM, leader, name, null, null);
}
@Override
public PlayerGroup createGroup(Player leader, Dungeon dungeon) {
return new DGroup(this, leader, dungeon);
return DGroup.create(this, GroupCreateEvent.Cause.CUSTOM, leader, null, null, dungeon);
}
@Override
public PlayerGroup createGroup(Player leader, Collection<Player> members, String name, Dungeon dungeon) {
return new DGroup(this, name, leader, members, dungeon);
PlayerGroup group = DGroup.create(this, GroupCreateEvent.Cause.CUSTOM, leader, name, null, dungeon);
if (members != null) {
members.forEach(group::addMember);
}
return group;
}
@Override

View File

@ -18,7 +18,7 @@ package de.erethon.dungeonsxl.announcer;
import de.erethon.dungeonsxl.DungeonsXL;
import de.erethon.dungeonsxl.api.dungeon.Dungeon;
import de.erethon.dungeonsxl.api.event.group.GroupCreateEvent;
import de.erethon.dungeonsxl.api.event.group.GroupCreateEvent.Cause;
import de.erethon.dungeonsxl.api.player.PlayerGroup.Color;
import de.erethon.dungeonsxl.config.DMessage;
import de.erethon.dungeonsxl.player.DGroup;
@ -33,7 +33,6 @@ import java.util.List;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
@ -368,10 +367,9 @@ public class Announcer {
}
} else if (dGroup == null && pGroup == null) {
GroupCreateEvent event = new GroupCreateEvent(dGroup, plugin.getPlayerCache().get(player), GroupCreateEvent.Cause.ANNOUNCER);
Bukkit.getPluginManager().callEvent(event);
if (!event.isCancelled()) {
dGroups.set(buttons.indexOf(button), new DGroup(plugin, player, color));
DGroup group = DGroup.create(plugin, Cause.ANNOUNCER, player, null, color, null);
if (group != null) {
dGroups.set(buttons.indexOf(button), group);
}
} else if (dGroup == null && pGroup != null) {

View File

@ -18,6 +18,7 @@ package de.erethon.dungeonsxl.command;
import de.erethon.dungeonsxl.DungeonsXL;
import de.erethon.dungeonsxl.api.dungeon.Game;
import de.erethon.dungeonsxl.api.event.group.GroupCreateEvent.Cause;
import de.erethon.dungeonsxl.api.player.PlayerGroup;
import de.erethon.dungeonsxl.config.DMessage;
import de.erethon.dungeonsxl.player.DGamePlayer;
@ -73,9 +74,11 @@ public class EnterCommand extends DCommand {
MessageUtil.sendMessage(sender, DMessage.ERROR_LEAVE_GAME.getMessage());
return;
}
if (joining == null) {
joining = new DGroup(plugin, captain, game.getDungeon());
joining = DGroup.create(plugin, Cause.COMMAND, captain, null, null, game.getDungeon());
}
if (joining == null) {
return;
}
if (joining.getLeader() != captain && !DPermission.hasPermission(sender, DPermission.BYPASS)) {

View File

@ -17,7 +17,7 @@
package de.erethon.dungeonsxl.command;
import de.erethon.dungeonsxl.DungeonsXL;
import de.erethon.dungeonsxl.api.event.group.GroupCreateEvent;
import de.erethon.dungeonsxl.api.event.group.GroupCreateEvent.Cause;
import de.erethon.dungeonsxl.api.event.group.GroupDisbandEvent;
import de.erethon.dungeonsxl.api.event.group.GroupPlayerKickEvent;
import de.erethon.dungeonsxl.config.DMessage;
@ -121,15 +121,8 @@ public class GroupCommand extends DCommand {
return;
}
DGroup dGroup = new DGroup(plugin, args[2], player);
GroupCreateEvent event = new GroupCreateEvent(dGroup, plugin.getPlayerCache().get(player), GroupCreateEvent.Cause.COMMAND);
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) {
dGroup.delete();
dGroup = null;
} else {
DGroup dGroup = DGroup.create(plugin, Cause.COMMAND, player, args[2], null, null);
if (dGroup != null) {
MessageUtil.sendMessage(sender, DMessage.GROUP_CREATED.getMessage(sender.getName(), args[2]));
}
}

View File

@ -29,7 +29,6 @@ import de.erethon.dungeonsxl.player.DGroup;
import de.erethon.dungeonsxl.player.DInstancePlayer;
import de.erethon.dungeonsxl.player.DPermission;
import de.erethon.dungeonsxl.util.commons.chat.MessageUtil;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@ -69,12 +68,9 @@ public class PlayCommand extends DCommand {
MessageUtil.sendMessage(player, DMessage.ERROR_LEAVE_GROUP.getMessage());
return;
} else if (group == null) {
group = new DGroup(plugin, player, dungeon);
GroupCreateEvent event = new GroupCreateEvent(group, dPlayer, GroupCreateEvent.Cause.COMMAND);
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) {
plugin.getGroupCache().remove(group);
group = null;
group = DGroup.create(plugin, GroupCreateEvent.Cause.COMMAND, player, null, null, dungeon);
if (group == null) {
return;
}
}
if (!group.getLeader().equals(player) && !DPermission.hasPermission(player, DPermission.BYPASS)) {

View File

@ -69,12 +69,9 @@ public class TestCommand extends DCommand {
MessageUtil.sendMessage(player, DMessage.ERROR_LEAVE_GROUP.getMessage());
return;
} else if (group == null) {
group = new DGroup(plugin, player, dungeon);
GroupCreateEvent event = new GroupCreateEvent(group, dPlayer, GroupCreateEvent.Cause.COMMAND);
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) {
plugin.getGroupCache().remove(group);
group = null;
group = DGroup.create(plugin, GroupCreateEvent.Cause.COMMAND, player, null, null, dungeon);
if (group == null) {
return;
}
}
if (!group.getLeader().equals(player) && !DPermission.hasPermission(player, DPermission.BYPASS)) {

View File

@ -23,7 +23,6 @@ import de.erethon.dungeonsxl.api.dungeon.Dungeon;
import de.erethon.dungeonsxl.api.dungeon.Game;
import de.erethon.dungeonsxl.api.player.PlayerGroup;
import de.erethon.dungeonsxl.api.world.GameWorld;
import de.erethon.dungeonsxl.api.world.ResourceWorld;
import de.erethon.dungeonsxl.config.DMessage;
import de.erethon.dungeonsxl.dungeon.DGame;
import de.erethon.dungeonsxl.player.DGamePlayer;

View File

@ -18,6 +18,7 @@ package de.erethon.dungeonsxl.global;
import de.erethon.caliburn.category.Category;
import de.erethon.dungeonsxl.DungeonsXL;
import de.erethon.dungeonsxl.api.event.group.GroupCreateEvent.Cause;
import de.erethon.dungeonsxl.config.DMessage;
import de.erethon.dungeonsxl.player.DGroup;
import de.erethon.dungeonsxl.util.commons.chat.MessageUtil;
@ -164,13 +165,11 @@ public class GroupSign extends JoinSign {
return;
}
if (groupName != null) {
group = new DGroup(plugin, groupName, player, dungeon);
group.setGroupSign(this);
} else {
group = new DGroup(plugin, player, dungeon);
group.setGroupSign(this);
group = DGroup.create(plugin, Cause.GROUP_SIGN, player, groupName, null, dungeon);
if (group == null) {
return;
}
group.setGroupSign(this);
update();
} else if (topSign.getLine(0).equals(DMessage.SIGN_GLOBAL_JOIN_GROUP.getMessage())) {

View File

@ -23,7 +23,7 @@ import de.erethon.dungeonsxl.api.dungeon.Dungeon;
import de.erethon.dungeonsxl.api.dungeon.Game;
import de.erethon.dungeonsxl.api.dungeon.GameRule;
import de.erethon.dungeonsxl.api.dungeon.GameRuleContainer;
import de.erethon.dungeonsxl.api.event.group.GroupCreateEvent;
import de.erethon.dungeonsxl.api.event.group.GroupCreateEvent.Cause;
import de.erethon.dungeonsxl.api.event.requirement.RequirementCheckEvent;
import de.erethon.dungeonsxl.api.player.GlobalPlayer;
import de.erethon.dungeonsxl.api.player.PlayerGroup;
@ -544,12 +544,8 @@ public class DGlobalPlayer implements GlobalPlayer {
}
}
DGroup dGroup = new DGroup(plugin, player, dungeon);
GroupCreateEvent createEvent = new GroupCreateEvent(dGroup, this, GroupCreateEvent.Cause.GROUP_SIGN);
Bukkit.getPluginManager().callEvent(createEvent);
if (createEvent.isCancelled()) {
dGroup = null;
DGroup dGroup = DGroup.create(plugin, Cause.TUTORIAL, player, null, null, dungeon);
if (dGroup == null) {
return;
}

View File

@ -23,6 +23,7 @@ import de.erethon.dungeonsxl.api.dungeon.Game;
import de.erethon.dungeonsxl.api.dungeon.GameRule;
import de.erethon.dungeonsxl.api.dungeon.GameRuleContainer;
import de.erethon.dungeonsxl.api.event.group.GroupCollectRewardEvent;
import de.erethon.dungeonsxl.api.event.group.GroupCreateEvent;
import de.erethon.dungeonsxl.api.event.group.GroupDisbandEvent;
import de.erethon.dungeonsxl.api.event.group.GroupFinishDungeonEvent;
import de.erethon.dungeonsxl.api.event.group.GroupFinishFloorEvent;
@ -45,7 +46,6 @@ import de.erethon.dungeonsxl.util.commons.chat.MessageUtil;
import de.erethon.dungeonsxl.util.commons.player.PlayerCollection;
import de.erethon.dungeonsxl.world.DResourceWorld;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
@ -83,72 +83,34 @@ public class DGroup implements PlayerGroup {
private int initialLives = -1;
private int lives = -1;
public DGroup(DungeonsXL plugin, Player player) {
this(plugin, "Group", player);
private DGroup() {
}
public DGroup(DungeonsXL plugin, Player player, Color color) {
this(plugin, color.toString(), player);
}
public static DGroup create(DungeonsXL plugin, GroupCreateEvent.Cause cause, Player leader, String name, Color color, Dungeon dungeon) {
if (name == null) {
name = color != null ? color.toString() : "Group";
}
public DGroup(DungeonsXL plugin, String name, Player player) {
this.plugin = plugin;
dPlayers = plugin.getPlayerCache();
DGroup group = new DGroup();
group.plugin = plugin;
group.dPlayers = plugin.getPlayerCache();
id = counter++;
untaggedName = name;
this.name = name + "#" + id;
plugin.getGroupCache().add(this.name, this);
group.id = counter++;
group.untaggedName = name;
group.name = name + "#" + group.id;
group.color = color;
group.dungeon = dungeon;
group.playing = false;
group.addMember(leader);
group.setLeader(leader);
GroupPlayerJoinEvent event = new GroupPlayerJoinEvent(this, dPlayers.get(player), true);
GroupCreateEvent event = new GroupCreateEvent(group, plugin.getPlayerCache().get(leader), cause);
Bukkit.getPluginManager().callEvent(event);
if (!event.isCancelled()) {
setLeader(player);
addMember(player);
} else {
plugin.getGroupCache().remove(this);
return;
plugin.getGroupCache().add(group.name, group);
return group;
}
playing = false;
}
public DGroup(DungeonsXL plugin, Player player, Dungeon dungeon) {
this(plugin, "Group", player, dungeon);
}
public DGroup(DungeonsXL plugin, String name, Player player, Dungeon dungeon) {
this(plugin, name, player, new ArrayList<Player>(), dungeon);
}
public DGroup(DungeonsXL plugin, String name, Player captain, Collection<Player> players, Dungeon dungeon) {
this.plugin = plugin;
dPlayers = plugin.getPlayerCache();
id = counter++;
untaggedName = name;
this.name = name + "#" + id;
plugin.getGroupCache().add(this.name, this);
GroupPlayerJoinEvent event = new GroupPlayerJoinEvent(this, dPlayers.get(captain), true);
Bukkit.getPluginManager().callEvent(event);
if (!event.isCancelled()) {
this.captain = captain;
this.players.add(captain);
}
for (Player player : players) {
if (!this.players.contains(player)) {
addMember(player);
}
}
if (getMembers().size() == 0) {
plugin.getGroupCache().remove(this);
return;
}
this.dungeon = dungeon;
playing = false;
return null;
}
// Getters and setters