Added F (fortune) trigger; resolves #105

This commit is contained in:
Daniel Saukel 2016-07-05 20:05:36 +02:00
parent cd0fe34bf4
commit 2389d54490
4 changed files with 90 additions and 0 deletions

View File

@ -0,0 +1,78 @@
/*
* 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.commons.util.NumberUtil;
import io.github.dre2n.dungeonsxl.event.trigger.TriggerActionEvent;
import static io.github.dre2n.dungeonsxl.trigger.Trigger.plugin;
import io.github.dre2n.dungeonsxl.world.DGameWorld;
/**
* @author Frank Baumann, Daniel Saukel
*/
public class FortuneTrigger extends Trigger {
private TriggerType type = TriggerTypeDefault.FORTUNE;
private double chance = 0;
public FortuneTrigger(double chance) {
this.chance = chance;
}
/* Getters and setters */
public double getChance() {
return chance;
}
/**
* @param chance
* the chance to set
*/
public void setChance(double chance) {
this.chance = chance;
}
@Override
public TriggerType getType() {
return type;
}
/* Actions */
public void onTrigger() {
int random = NumberUtil.generateRandomInt(0, 100);
if (chance * 100 < random) {
return;
}
TriggerActionEvent event = new TriggerActionEvent(this);
plugin.getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) {
return;
}
setTriggered(true);
updateDSigns();
}
/* Statics */
public static FortuneTrigger getOrCreate(String chance, DGameWorld gameWorld) {
return new FortuneTrigger(NumberUtil.parseDouble(chance));
}
}

View File

@ -138,6 +138,12 @@ public abstract class Trigger {
trigger = new DistanceTrigger(dSign.getSign().getLocation());
}
} else if (type == TriggerTypeDefault.FORTUNE) {
if (value != null) {
trigger = new FortuneTrigger(NumberUtil.parseDouble(value));
}
} else if (type == TriggerTypeDefault.SIGN) {
if (value != null) {

View File

@ -22,6 +22,7 @@ package io.github.dre2n.dungeonsxl.trigger;
public enum TriggerTypeDefault implements TriggerType {
DISTANCE("D", DistanceTrigger.class),
FORTUNE("F", FortuneTrigger.class),
INTERACT("I", InteractTrigger.class),
MOB("M", MobTrigger.class),
PROGRESS("P", ProgressTrigger.class),

View File

@ -30,6 +30,7 @@ import io.github.dre2n.dungeonsxl.sign.DSignType;
import io.github.dre2n.dungeonsxl.sign.DSignTypeDefault;
import io.github.dre2n.dungeonsxl.sign.MobSign;
import io.github.dre2n.dungeonsxl.sign.StartSign;
import io.github.dre2n.dungeonsxl.trigger.FortuneTrigger;
import io.github.dre2n.dungeonsxl.trigger.ProgressTrigger;
import io.github.dre2n.dungeonsxl.trigger.RedstoneTrigger;
import io.github.dre2n.dungeonsxl.trigger.Trigger;
@ -375,6 +376,10 @@ public class DGameWorld extends DInstanceWorld {
((RedstoneTrigger) trigger).onTrigger();
}
for (Trigger trigger : getTriggers(TriggerTypeDefault.FORTUNE)) {
((FortuneTrigger) trigger).onTrigger();
}
for (DSign dSign : dSigns) {
if (dSign != null) {
if (!dSign.hasTriggers()) {