Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added DenyActionForName, and denying ATTACK for some creatures
- Loading branch information
Showing
3 changed files
with
68 additions
and
3 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
cardshifter-core/src/main/java/net/zomis/cardshifter/ecs/usage/DenyActionForNames.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package net.zomis.cardshifter.ecs.usage; | ||
|
||
import com.cardshifter.modapi.actions.ActionAllowedCheckEvent; | ||
import com.cardshifter.modapi.attributes.AttributeRetriever; | ||
import com.cardshifter.modapi.attributes.Attributes; | ||
import com.cardshifter.modapi.base.ECSGame; | ||
import com.cardshifter.modapi.base.ECSSystem; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
/** | ||
* A system that denies a specific action on all entities with a specific name | ||
*/ | ||
public class DenyActionForNames implements ECSSystem { | ||
|
||
private final HashSet<String> names; | ||
private final String action; | ||
|
||
private AttributeRetriever name = AttributeRetriever.forAttribute(Attributes.NAME); | ||
|
||
public DenyActionForNames(String action, Set<String> deniedNames) { | ||
this.names = new HashSet<>(deniedNames); | ||
this.action = action; | ||
} | ||
|
||
@Override | ||
public void startGame(ECSGame game) { | ||
game.getEvents().registerHandlerAfter(this, ActionAllowedCheckEvent.class, this::allowCheck); | ||
} | ||
|
||
private void allowCheck(ActionAllowedCheckEvent event) { | ||
if (!action.equals(event.getAction().getName())) { | ||
return; | ||
} | ||
if (names.contains(name.getOrDefault(event.getEntity(), ""))) { | ||
event.setAllowed(false); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters