Added ProgressTrigger

This commit is contained in:
Daniel Saukel 2016-04-26 22:20:24 +02:00
parent 53ef4f2280
commit df806d1520
5 changed files with 217 additions and 1 deletions

View File

@ -19,9 +19,11 @@ package io.github.dre2n.dungeonsxl.game;
import io.github.dre2n.commons.util.playerutil.PlayerUtil;
import io.github.dre2n.dungeonsxl.DungeonsXL;
import io.github.dre2n.dungeonsxl.config.MessageConfig;
import io.github.dre2n.dungeonsxl.dungeon.Dungeon;
import io.github.dre2n.dungeonsxl.player.DGroup;
import io.github.dre2n.dungeonsxl.sign.DSign;
import io.github.dre2n.dungeonsxl.sign.MobSign;
import io.github.dre2n.dungeonsxl.trigger.ProgressTrigger;
import io.github.dre2n.dungeonsxl.world.GameWorld;
import java.util.ArrayList;
import java.util.Arrays;
@ -136,6 +138,36 @@ public class Game {
this.world = world;
}
/**
* Refers to the DGroup with the best progress.
*
* @return the unplayed floors
*/
public List<String> getUnplayedFloors() {
List<String> unplayedFloors = new ArrayList<>();
for (DGroup dGroup : dGroups) {
if (dGroup.getUnplayedFloors().size() < unplayedFloors.size()) {
unplayedFloors = dGroup.getUnplayedFloors();
}
}
return unplayedFloors;
}
/**
* Refers to the DGroup with the best progress.
*
* @return the floorCount
*/
public int getFloorCount() {
int floorCount = 0;
for (DGroup dGroup : dGroups) {
if (dGroup.getFloorCount() > floorCount) {
floorCount = dGroup.getFloorCount();
}
}
return floorCount;
}
/**
* @return the waveCount
*/
@ -192,6 +224,15 @@ public class Game {
waveKills.clear();
}
/**
* Refers to a DGroup.
*
* @return the dungeon
*/
public Dungeon getDungeon() {
return dGroups.get(0).getDungeon();
}
/**
* @return the players in all dGroups
*/
@ -221,6 +262,13 @@ public class Game {
waveCount++;
resetWaveKills();
Set<ProgressTrigger> triggers = ProgressTrigger.getByGameWorld(world);
for (ProgressTrigger trigger : triggers) {
if (getWaveCount() >= trigger.getWaveCount() & getFloorCount() >= trigger.getFloorCount() - 1 || !getUnplayedFloors().contains(trigger.getFloor()) & trigger.getFloor() != null) {
trigger.onTrigger();
}
}
int delay = world.getConfig().getTimeToNextWave();
sendMessage(plugin.getMessageConfig().getMessage(MessageConfig.Messages.GROUP_WAVE_FINISHED, String.valueOf(waveCount), String.valueOf(delay)));

View File

@ -425,7 +425,7 @@ public class DGroup {
public boolean isEmpty() {
return players.isEmpty();
}
/* Actions */
/**
* Remove the group from the List

View File

@ -0,0 +1,154 @@
/*
* Copyright (C) 2012-2016 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 io.github.dre2n.dungeonsxl.trigger;
import io.github.dre2n.dungeonsxl.event.trigger.TriggerActionEvent;
import io.github.dre2n.dungeonsxl.world.GameWorld;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
* @author Frank Baumann, Daniel Saukel
*/
public class ProgressTrigger extends Trigger {
private static Map<GameWorld, ArrayList<ProgressTrigger>> triggers = new HashMap<>();
private TriggerType type = TriggerTypeDefault.PROGRESS;
private String floor;
private int floorCount;
private int waveCount;
public ProgressTrigger(int floorCount, int waveCount) {
this.floorCount = floorCount;
this.waveCount = waveCount;
}
public ProgressTrigger(String floor) {
this.floor = floor;
}
/* Getters and setters */
/**
* @return the specific floor that must be finished
*/
public String getFloor() {
return floor;
}
/**
* @param floor
* the specific floor to set
*/
public void setFloor(String floor) {
this.floor = floor;
}
/**
* @return the floor count to trigger
*/
public int getFloorCount() {
return floorCount;
}
/**
* @param floorCount
* the floor count to set
*/
public void setFloorCount(int floorCount) {
this.floorCount = floorCount;
}
/**
* @return the wave count to trigger
*/
public int getWaveCount() {
return waveCount;
}
/**
* @param waveCount
* the wave count to set
*/
public void setWaveCount(int waveCount) {
this.waveCount = waveCount;
}
/* Actions */
public void onTrigger() {
TriggerActionEvent event = new TriggerActionEvent(this);
if (event.isCancelled()) {
return;
}
setTriggered(true);
updateDSigns();
}
@Override
public void register(GameWorld gameWorld) {
if (!hasTriggers(gameWorld)) {
ArrayList<ProgressTrigger> list = new ArrayList<>();
list.add(this);
triggers.put(gameWorld, list);
} else {
triggers.get(gameWorld).add(this);
}
}
@Override
public void unregister(GameWorld gameWorld) {
if (hasTriggers(gameWorld)) {
triggers.get(gameWorld).remove(this);
}
}
@Override
public TriggerType getType() {
return type;
}
public static ProgressTrigger getOrCreate(int floorCount, int waveCount, GameWorld gameWorld) {
if (floorCount == 0 & waveCount == 0 || floorCount < 0 || waveCount < 0) {
return null;
}
return new ProgressTrigger(floorCount, waveCount);
}
public static ProgressTrigger getOrCreate(String floor, GameWorld gameWorld) {
return new ProgressTrigger(floor);
}
public static Set<ProgressTrigger> getByGameWorld(GameWorld gameWorld) {
Set<ProgressTrigger> toReturn = new HashSet<>();
for (ProgressTrigger trigger : triggers.get(gameWorld)) {
toReturn.add(trigger);
}
return toReturn;
}
public static boolean hasTriggers(GameWorld gameWorld) {
return !triggers.isEmpty() && triggers.containsKey(gameWorld);
}
}

View File

@ -146,6 +146,19 @@ public abstract class Trigger {
return MobTrigger.getOrCreate(value, dSign.getGameWorld());
}
} else if (type == TriggerTypeDefault.PROGRESS) {
if (value != null) {
if (value.matches("[0-99]/[0-999]")) {
int floorCount = NumberUtil.parseInt(value.split("/")[0]);
int waveCount = NumberUtil.parseInt(value.split("/")[1]);
return ProgressTrigger.getOrCreate(floorCount, waveCount, dSign.getGameWorld());
} else {
return ProgressTrigger.getOrCreate(value, dSign.getGameWorld());
}
}
} else if (type == TriggerTypeDefault.USE_ITEM) {
if (value != null) {

View File

@ -24,6 +24,7 @@ public enum TriggerTypeDefault implements TriggerType {
DISTANCE("D", DistanceTrigger.class),
INTERACT("I", InteractTrigger.class),
MOB("M", MobTrigger.class),
PROGRESS("P", ProgressTrigger.class),
REDSTONE("R", RedstoneTrigger.class),
SIGN("T", SignTrigger.class),
USE_ITEM("U", UseItemTrigger.class),