blob: a491c0ffcc9a4e50f49070f2335e04f0bf02e4a5 [file] [log] [blame] [raw]
/* Copyright 2015-2021 Rivoreo
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package org.rivoreo.minecraft.givecooldown;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.plugin.PluginManager;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.entity.Player;
import java.util.HashMap;
import java.util.Calendar;
public final class BukkitPlugin extends JavaPlugin implements Listener {
public BukkitPlugin() {
pm = getServer().getPluginManager();
player_map = new HashMap<Player, Calendar>();
}
private PluginManager pm;
private HashMap<Player, Calendar> player_map;
@EventHandler
public void on_player_command_preprocess(PlayerCommandPreprocessEvent event) {
String line = event.getMessage();
if(!line.startsWith("/give ") && !line.startsWith("/egive ")) return;
String a[] = line.split("[ ]");
if(a.length < 3) return;
int item_count = 64;
if(a.length > 3) try {
item_count = Integer.parseInt(a[3]);
if(item_count < 1) return;
} catch(NumberFormatException e) {
e.printStackTrace();
}
Player player = event.getPlayer();
Calendar cal = Calendar.getInstance();
Calendar blocked_until = player_map.get(player);
if(blocked_until != null && cal.before(blocked_until)) {
//System.err.println(blocked_until.get(Calendar.SECOND));
//System.err.println(cal.get(Calendar.SECOND));
//System.err.println(blocked_until.getTimeInMillis());
//System.err.println(cal.getTimeInMillis());
int time_left = (int)(blocked_until.getTimeInMillis() - cal.getTimeInMillis()) / 1000 + 1;
getLogger().info(String.format("%s is currently blocked from using 'give' command, %s second%s left", player.getDisplayName(), time_left, time_left == 1 ? "" : "s"));
if(time_left < 1) time_left = 1;
player.sendMessage(String.format("Please wait for %s second%s before issuing another 'give' command", time_left, time_left == 1 ? "" : "s"));
event.setCancelled(true);
} else {
//cal.add(Calendar.SECOND, item_count * 2);
cal.add(Calendar.SECOND, (int)(item_count * 1.5));
player_map.put(player, cal);
}
}
/*
@Override
public void onLoad() {
}
*/
@Override
public void onDisable() {
HandlerList.unregisterAll((Listener)this);
player_map.clear();
}
@Override
public void onEnable() {
pm.registerEvents(this, this);
}
}