Add requirement event API

This commit is contained in:
Daniel Saukel 2020-04-13 20:51:15 +02:00
parent e80bb1f605
commit 89846991c1
12 changed files with 327 additions and 254 deletions

View File

@ -0,0 +1,42 @@
/*
* Copyright (C) 2014-2020 Daniel Saukel
*
* This library is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser 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 GNULesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.erethon.dungeonsxl.api.event.dungeon;
import de.erethon.dungeonsxl.api.dungeon.Dungeon;
import org.bukkit.event.Event;
/**
* Superclass for events involving {@link Dungeon}s.
*
* @author Daniel Saukel
*/
public abstract class DungeonEvent extends Event {
protected Dungeon dungeon;
protected DungeonEvent(Dungeon dungeon) {
this.dungeon = dungeon;
}
/**
* Returns the dungeon involved in this event.
*
* @return the dungeon involved in this event
*/
public Dungeon getDungeon() {
return dungeon;
}
}

View File

@ -17,16 +17,21 @@ package de.erethon.dungeonsxl.api.event.player;
import de.erethon.dungeonsxl.api.player.GamePlayer; import de.erethon.dungeonsxl.api.player.GamePlayer;
/** /**
* Superclass for events involving {@link de.erethon.dungeonsxl.api.player.GamePlayer GamePlayers}. * Superclass for events involving {@link GamePlayer}s.
* *
* @author Daniel Saukel * @author Daniel Saukel
*/ */
public abstract class GamePlayerEvent extends GlobalPlayerEvent { public abstract class GamePlayerEvent extends GlobalPlayerEvent {
public GamePlayerEvent(GamePlayer gamePlayer) { protected GamePlayerEvent(GamePlayer gamePlayer) {
super(gamePlayer); super(gamePlayer);
} }
/**
* Returns the GamePlayer involved in this event
*
* @return the GamePlayer involved in this event
*/
public GamePlayer getGamePlayer() { public GamePlayer getGamePlayer() {
return (GamePlayer) globalPlayer; return (GamePlayer) globalPlayer;
} }

View File

@ -27,7 +27,7 @@ public abstract class GlobalPlayerEvent extends Event {
protected GlobalPlayer globalPlayer; protected GlobalPlayer globalPlayer;
public GlobalPlayerEvent(GlobalPlayer globalPlayer) { protected GlobalPlayerEvent(GlobalPlayer globalPlayer) {
this.globalPlayer = globalPlayer; this.globalPlayer = globalPlayer;
} }

View File

@ -0,0 +1,114 @@
/*
* Copyright (C) 2014-2020 Daniel Saukel
*
* This library is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser 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 GNULesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.erethon.dungeonsxl.api.event.requirement;
import de.erethon.dungeonsxl.api.Requirement;
import de.erethon.dungeonsxl.api.dungeon.Dungeon;
import net.md_5.bungee.api.chat.BaseComponent;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
/**
* Fired when it is checked if a player fulfills a {@link Requirement} realized through the Requirement API.
* <p>
* Note that this is usually called twice per player: When he tries to enter a dungeon and when he tries to start a game.
*
* @author Daniel Saukel
*/
public class RequirementCheckEvent extends RequirementEvent implements Cancellable {
private static final HandlerList handlers = new HandlerList();
private boolean cancelled;
private Player player;
private boolean keepInventory;
private BaseComponent[] checkMessage;
public RequirementCheckEvent(Requirement requirement, Dungeon dungeon, Player player, boolean keepInventory) {
super(requirement, dungeon);
this.player = player;
this.keepInventory = keepInventory;
checkMessage = requirement.getCheckMessage(player);
}
/**
* Returns the checked player.
*
* @return the player
*/
public Player getPlayer() {
return player;
}
/**
* Sets the checked player.
*
* @param player the player
*/
public void setPlayer(Player player) {
this.player = player;
}
/**
* Returns the message that will be sent to the player to inform him what he needs in order to fulfill the requirement if there is a requirement that he
* does not fulfill.
*
* @return the message that will be sent to the player to inform him what he needs in order to fulfill the requirement if there is a requirement that he
* does not fulfill
*/
public BaseComponent[] getCheckMessage() {
return checkMessage;
}
/**
* Sets the message that will be sent to the player to inform him what he needs in order to fulfill the requirement if there is a a requirement that he does
* not fulfill.
*
* @param checkMessage the message component array
*/
public void setCheckMessage(BaseComponent[] checkMessage) {
this.checkMessage = checkMessage;
}
/**
* If the player's state - including his inventory, EXP etc. - is kept.
*
* @return if the player's state is kept
*/
public boolean isInventoryKept() {
return keepInventory;
}
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}
}

View File

@ -0,0 +1,90 @@
/*
* Copyright (C) 2014-2020 Daniel Saukel
*
* This library is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser 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 GNULesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.erethon.dungeonsxl.api.event.requirement;
import de.erethon.dungeonsxl.api.Requirement;
import de.erethon.dungeonsxl.api.dungeon.Dungeon;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
/**
* Fired when a {@link Requirement} is demanded from a player.
* <p>
* This is fired for all requirements, even for those that do not demand anything from the player.
*
* @author Daniel Saukel
*/
public class RequirementDemandEvent extends RequirementEvent implements Cancellable {
private static final HandlerList handlers = new HandlerList();
private boolean cancelled;
private Player player;
private boolean keepInventory;
public RequirementDemandEvent(Requirement requirement, Dungeon dungeon, Player player, boolean keepInventory) {
super(requirement, dungeon);
this.player = player;
this.keepInventory = keepInventory;
}
/**
* Returns the player who pays the requirement.
*
* @return the player
*/
public Player getPlayer() {
return player;
}
/**
* Sets the player who pays the requirement.
*
* @param player the player
*/
public void setPlayer(Player player) {
this.player = player;
}
/**
* If the player's state - including his inventory, EXP etc. - is kept.
*
* @return if the player's state is kept
*/
public boolean isInventoryKept() {
return keepInventory;
}
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}
}

View File

@ -0,0 +1,63 @@
/*
* Copyright (C) 2014-2020 Daniel Saukel
*
* This library is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser 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 GNULesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.erethon.dungeonsxl.api.event.requirement;
import de.erethon.dungeonsxl.api.Requirement;
import de.erethon.dungeonsxl.api.dungeon.Dungeon;
import org.bukkit.event.Event;
/**
* Superclass for events involving {@link Requirement}s.
*
* @author Daniel Saukel
*/
public abstract class RequirementEvent extends Event {
protected Requirement requirement;
protected Dungeon dungeon;
public RequirementEvent(Requirement requirement, Dungeon dungeon) {
this.requirement = requirement;
this.dungeon = dungeon;
}
/**
* Returns the dungeon involved in this event.
*
* @return the dungeon involved in this event
*/
public Dungeon getDungeon() {
return dungeon;
}
/**
* Returns the requirement involved in this event.
*
* @return the requirement involved in this event
*/
public Requirement getRequirement() {
return requirement;
}
/**
* Sets the requirement involved in this event.
*
* @param requirement the requirement
*/
public void setRequirement(Requirement requirement) {
this.requirement = requirement;
}
}

View File

@ -1,72 +0,0 @@
/*
* Copyright (C) 2012-2020 Frank Baumann
*
* 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 de.erethon.dungeonsxl.event.requirement;
import de.erethon.dungeonsxl.api.Requirement;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
/**
* @author Daniel Saukel
*/
public class RequirementCheckEvent extends RequirementEvent implements Cancellable {
private static final HandlerList handlers = new HandlerList();
private boolean cancelled;
private Player player;
public RequirementCheckEvent(Requirement requirement, Player player) {
super(requirement);
this.player = player;
}
/**
* @return the player
*/
public Player getPlayer() {
return player;
}
/**
* @param player the player to set
*/
public void setPlayer(Player player) {
this.player = player;
}
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}
}

View File

@ -1,72 +0,0 @@
/*
* Copyright (C) 2012-2020 Frank Baumann
*
* 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 de.erethon.dungeonsxl.event.requirement;
import de.erethon.dungeonsxl.api.Requirement;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
/**
* @author Daniel Saukel
*/
public class RequirementDemandEvent extends RequirementEvent implements Cancellable {
private static final HandlerList handlers = new HandlerList();
private boolean cancelled;
private Player player;
public RequirementDemandEvent(Requirement requirement, Player player) {
super(requirement);
this.player = player;
}
/**
* @return the player
*/
public Player getPlayer() {
return player;
}
/**
* @param player the player to set
*/
public void setPlayer(Player player) {
this.player = player;
}
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}
}

View File

@ -1,48 +0,0 @@
/*
* Copyright (C) 2012-2020 Frank Baumann
*
* 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 de.erethon.dungeonsxl.event.requirement;
import de.erethon.dungeonsxl.api.Requirement;
import org.bukkit.event.Event;
/**
*
* @author Daniel Saukel
*/
public abstract class RequirementEvent extends Event {
protected Requirement requirement;
public RequirementEvent(Requirement requirement) {
this.requirement = requirement;
}
/**
* @return the requirement
*/
public Requirement getRequirement() {
return requirement;
}
/**
* @param requirement the requirement to set
*/
public void setRequirement(Requirement requirement) {
this.requirement = requirement;
}
}

View File

@ -1,54 +0,0 @@
/*
* Copyright (C) 2012-2020 Frank Baumann
*
* 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 de.erethon.dungeonsxl.event.requirement;
import de.erethon.dungeonsxl.api.Requirement;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
/**
* @author Daniel Saukel
*/
public class RequirementRegistrationEvent extends RequirementEvent implements Cancellable {
private static final HandlerList handlers = new HandlerList();
private boolean cancelled;
public RequirementRegistrationEvent(Requirement requirement) {
super(requirement);
}
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}
}

View File

@ -25,19 +25,20 @@ import de.erethon.dungeonsxl.api.Reward;
import de.erethon.dungeonsxl.api.dungeon.Dungeon; import de.erethon.dungeonsxl.api.dungeon.Dungeon;
import de.erethon.dungeonsxl.api.dungeon.GameRule; import de.erethon.dungeonsxl.api.dungeon.GameRule;
import de.erethon.dungeonsxl.api.dungeon.GameRuleContainer; import de.erethon.dungeonsxl.api.dungeon.GameRuleContainer;
import de.erethon.dungeonsxl.api.event.requirement.RequirementCheckEvent;
import de.erethon.dungeonsxl.api.player.GlobalPlayer; import de.erethon.dungeonsxl.api.player.GlobalPlayer;
import de.erethon.dungeonsxl.api.player.PlayerGroup; import de.erethon.dungeonsxl.api.player.PlayerGroup;
import de.erethon.dungeonsxl.api.world.GameWorld; import de.erethon.dungeonsxl.api.world.GameWorld;
import de.erethon.dungeonsxl.config.DMessage; import de.erethon.dungeonsxl.config.DMessage;
import de.erethon.dungeonsxl.dungeon.DGame; import de.erethon.dungeonsxl.dungeon.DGame;
import de.erethon.dungeonsxl.event.dgroup.DGroupCreateEvent; import de.erethon.dungeonsxl.event.dgroup.DGroupCreateEvent;
import de.erethon.dungeonsxl.event.requirement.RequirementCheckEvent;
import de.erethon.dungeonsxl.global.DPortal; import de.erethon.dungeonsxl.global.DPortal;
import de.erethon.dungeonsxl.util.NBTUtil; import de.erethon.dungeonsxl.util.NBTUtil;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import net.md_5.bungee.api.chat.BaseComponent;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.attribute.Attribute; import org.bukkit.attribute.Attribute;
@ -259,6 +260,7 @@ public class DGlobalPlayer implements GlobalPlayer {
return DPermission.hasPermission(player, permission); return DPermission.hasPermission(player, permission);
} }
@Override
public boolean checkRequirements(Dungeon dungeon) { public boolean checkRequirements(Dungeon dungeon) {
boolean fulfilled = true; boolean fulfilled = true;
GameRuleContainer rules = dungeon.getRules(); GameRuleContainer rules = dungeon.getRules();
@ -279,13 +281,15 @@ public class DGlobalPlayer implements GlobalPlayer {
} }
boolean genericReqs = true; boolean genericReqs = true;
List<BaseComponent[]> msgs = new ArrayList<>(rules.getState(GameRule.REQUIREMENTS).size());
for (Requirement requirement : rules.getState(GameRule.REQUIREMENTS)) { for (Requirement requirement : rules.getState(GameRule.REQUIREMENTS)) {
RequirementCheckEvent event = new RequirementCheckEvent(requirement, player); RequirementCheckEvent event = new RequirementCheckEvent(requirement, dungeon, player, rules.getState(GameRule.KEEP_INVENTORY_ON_ENTER));
Bukkit.getPluginManager().callEvent(event); Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) { if (event.isCancelled()) {
continue; continue;
} }
msgs.add(event.getCheckMessage());
if (!requirement.check(player)) { if (!requirement.check(player)) {
fulfilled = false; fulfilled = false;
genericReqs = false; genericReqs = false;
@ -293,7 +297,7 @@ public class DGlobalPlayer implements GlobalPlayer {
} }
if (!genericReqs) { if (!genericReqs) {
MessageUtil.sendMessage(player, DMessage.ERROR_REQUIREMENTS.getMessage()); MessageUtil.sendMessage(player, DMessage.ERROR_REQUIREMENTS.getMessage());
rules.getState(GameRule.REQUIREMENTS).forEach(r -> MessageUtil.sendMessage(player, r.getCheckMessage(player))); msgs.forEach(msg -> MessageUtil.sendMessage(player, msg));
} }
if (rules.getState(GameRule.MUST_FINISH_ALL) != null) { if (rules.getState(GameRule.MUST_FINISH_ALL) != null) {

View File

@ -25,6 +25,7 @@ import de.erethon.dungeonsxl.api.dungeon.Dungeon;
import de.erethon.dungeonsxl.api.dungeon.Game; import de.erethon.dungeonsxl.api.dungeon.Game;
import de.erethon.dungeonsxl.api.dungeon.GameRule; import de.erethon.dungeonsxl.api.dungeon.GameRule;
import de.erethon.dungeonsxl.api.dungeon.GameRuleContainer; import de.erethon.dungeonsxl.api.dungeon.GameRuleContainer;
import de.erethon.dungeonsxl.api.event.requirement.RequirementDemandEvent;
import de.erethon.dungeonsxl.api.player.GlobalPlayer; import de.erethon.dungeonsxl.api.player.GlobalPlayer;
import de.erethon.dungeonsxl.api.player.InstancePlayer; import de.erethon.dungeonsxl.api.player.InstancePlayer;
import de.erethon.dungeonsxl.api.player.PlayerCache; import de.erethon.dungeonsxl.api.player.PlayerCache;
@ -41,7 +42,6 @@ import de.erethon.dungeonsxl.event.dgroup.DGroupFinishDungeonEvent;
import de.erethon.dungeonsxl.event.dgroup.DGroupFinishFloorEvent; import de.erethon.dungeonsxl.event.dgroup.DGroupFinishFloorEvent;
import de.erethon.dungeonsxl.event.dgroup.DGroupStartFloorEvent; import de.erethon.dungeonsxl.event.dgroup.DGroupStartFloorEvent;
import de.erethon.dungeonsxl.event.dplayer.DPlayerJoinDGroupEvent; import de.erethon.dungeonsxl.event.dplayer.DPlayerJoinDGroupEvent;
import de.erethon.dungeonsxl.event.requirement.RequirementDemandEvent;
import de.erethon.dungeonsxl.event.reward.RewardAdditionEvent; import de.erethon.dungeonsxl.event.reward.RewardAdditionEvent;
import de.erethon.dungeonsxl.world.DGameWorld; import de.erethon.dungeonsxl.world.DGameWorld;
import de.erethon.dungeonsxl.world.DResourceWorld; import de.erethon.dungeonsxl.world.DResourceWorld;
@ -759,7 +759,8 @@ public class DGroup implements PlayerGroup {
} }
for (Requirement requirement : rules.getState(GameRule.REQUIREMENTS)) { for (Requirement requirement : rules.getState(GameRule.REQUIREMENTS)) {
RequirementDemandEvent requirementDemandEvent = new RequirementDemandEvent(requirement, player); RequirementDemandEvent requirementDemandEvent
= new RequirementDemandEvent(requirement, dungeon, player, rules.getState(GameRule.KEEP_INVENTORY_ON_ENTER));
Bukkit.getPluginManager().callEvent(event); Bukkit.getPluginManager().callEvent(event);
if (requirementDemandEvent.isCancelled()) { if (requirementDemandEvent.isCancelled()) {
continue; continue;