/*
* Copyright (C) 2016 eccentric_nz
*
* 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 me.eccentric_nz.TARDIS.artron;
import java.util.HashMap;
import me.eccentric_nz.TARDIS.TARDIS;
import me.eccentric_nz.TARDIS.database.QueryFactory;
import me.eccentric_nz.TARDIS.database.ResultSetTardisArtron;
import me.eccentric_nz.TARDIS.utility.TARDISMessage;
import org.bukkit.entity.Player;
/**
* Artron energy is used as a source of energy in the TARDIS. Visually, artron
* energy resembles a sort of blue electricity. Within the TARDIS' generator
* room is an Artron Energy Capacitor. Artron energy can be absorbed by
* travelling through time, such as by travellers in a TARDIS.
*
* @author eccentric_nz
*/
public class TARDISArtronLevels {
private final TARDIS plugin;
public TARDISArtronLevels(TARDIS plugin) {
this.plugin = plugin;
}
/**
* Starts a repeating task to recharge the TARDIS. The task is started each
* time the player exits the TARDIS after travelling. If the TARDIS moves
* away from the recharge location the task is cancelled.
*
* @param id the unique TARDIS database key
*/
public void recharge(int id) {
QueryFactory qf = new QueryFactory(plugin);
HashMap<String, Object> set = new HashMap<String, Object>();
set.put("recharging", 1);
HashMap<String, Object> where = new HashMap<String, Object>();
where.put("tardis_id", id);
qf.doUpdate("tardis", set, where);
TARDISArtronRunnable runnable = new TARDISArtronRunnable(plugin, id);
int taskID = plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, runnable, 480L, 480L);
runnable.setTask(taskID);
}
/**
* Checks whether the TARDIS has sufficient Artron Energy levels. If the
* energy level will drop below 100, then the player is warned.
*
* @param id the unique TARDIS database key
* @param required
* @param p the player to message
* @return a boolean - true if the TARDIS has sufficient energy
*/
public boolean checkLevel(int id, int required, Player p) {
ResultSetTardisArtron rs = new ResultSetTardisArtron(plugin);
if (!rs.fromID(id)) {
return false;
}
int level = rs.getArtron_level();
if (level - required <= 100) {
TARDISMessage.send(p, "ENERGY_LOW");
}
return (level > required);
}
}