T2C-AllayDuplicate/src/main/java/net/t2code/t2callayduplicate/event/Event.java

119 lines
4.9 KiB
Java

package net.t2code.t2callayduplicate.event;
import net.t2code.t2callayduplicate.Util;
import net.t2code.t2callayduplicate.config.ConfigFile;
import net.t2code.t2callayduplicate.system.Main;
import net.t2code.t2codelib.SPIGOT.api.eco.T2Ceco;
import net.t2code.t2codelib.SPIGOT.api.messages.T2Csend;
import net.t2code.t2codelib.SPIGOT.api.update.T2CupdateAPI;
import org.bukkit.*;
import org.bukkit.entity.Allay;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractAtEntityEvent;
import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.inventory.EquipmentSlot;
import java.time.Instant;
import java.util.HashMap;
import java.util.UUID;
public class Event implements Listener {
@EventHandler
public void onJoinEvent(PlayerLoginEvent event) {
T2CupdateAPI.join(Main.getPlugin(), Util.getPrefix(), "t2code.allayduplicate.admin", event.getPlayer(), Util.getSpigotID(), Util.getDiscord());
}
private static HashMap<UUID, Long> delay = new HashMap<>();
public static void clearCash() {
delay.clear();
}
public static void removePlayer(Player player) {
delay.remove(player.getUniqueId());
}
@EventHandler
public void onDuplicate(PlayerInteractAtEntityEvent e) {
Player player = e.getPlayer();
Location location = e.getRightClicked().getLocation();
if (!player.hasPermission("t2code.allayduplicate.use")) return;
if (!(e.getRightClicked() instanceof Allay)) return;
if (e.getHand() != EquipmentSlot.HAND) return;
if (ConfigFile.getSneakRequired() && !player.isSneaking()) return;
UUID uuid = player.getUniqueId();
if (ConfigFile.getItemEnable()) {
if (player.getItemInHand().getType() != Material.valueOf(ConfigFile.getItemMaterial())) return;
}
e.setCancelled(true);
if (!BuildCheck.canBuild(player, location)) {
T2Csend.player(player, ConfigFile.getErrorCanNotBuild());
return;
}
if (ConfigFile.getDelayEnable()) {
if (delay.containsKey(uuid) || delay.containsKey(e.getRightClicked().getUniqueId())) {
Long now = Instant.now().getEpochSecond();
long dl;
if (ConfigFile.getDelayProAllay()) {
dl = delay.get(e.getRightClicked().getUniqueId());
} else dl = delay.get(player.getUniqueId());
long diff = now - dl;
long bonustime_min = ConfigFile.getDelaySeconds();
long remainingMin = (int) ((bonustime_min - diff) / 60);
long remainingSec = (int) ((bonustime_min - diff) % 60);
T2Csend.player(player, ConfigFile.getErrorDelay().replace("&", "§").replace("[min]", String.valueOf(remainingMin))
.replace("[sec]", String.valueOf(remainingSec)));
return;
}
}
if (ConfigFile.getCostEnable()) {
if (!T2Ceco.moneyRemove(ConfigFile.getPrefix(), player, ConfigFile.getCostPrice())) {
T2Csend.player(player, ConfigFile.getErrorNoMoney().replace("[price]", String.valueOf(ConfigFile.getCostPrice())));
return;
}
}
if (!T2Ceco.itemRemove(player, ConfigFile.getItemMaterial(), ConfigFile.getItemAmount())) {
T2Csend.player(player, ConfigFile.getErrorIncorrectItem().replace("[amount]", String.valueOf(ConfigFile.getItemAmount()))
.replace("[item]", ConfigFile.getItemMaterial()));
return;
}
if (ConfigFile.getParticleEnable()) {
location.getWorld().spawnParticle(ConfigFile.getParticleParticle(), location, 3);
}
if (ConfigFile.getSoundEnable()) {
location.getWorld().playSound(location, ConfigFile.getSoundSound(), ConfigFile.getSoundVolume(), 0.0F);
}
UUID newAllayID = location.getWorld().spawnEntity(new Location(location.getWorld(), location.getX(), location.getY() + 1, location.getZ()), EntityType.ALLAY).getUniqueId();
if (ConfigFile.getDelayProAllay()) {
delay.put(newAllayID, Instant.now().getEpochSecond());
delay.put(e.getRightClicked().getUniqueId(), Instant.now().getEpochSecond());
} else delay.put(uuid, Instant.now().getEpochSecond());
T2Csend.player(player, ConfigFile.getDuplicate());
Bukkit.getScheduler().runTaskLaterAsynchronously(Main.getPlugin(), new Runnable() {
@Override
public void run() {
if (ConfigFile.getDelayProAllay()) {
delay.remove(newAllayID);
delay.remove (e.getRightClicked().getUniqueId());
} else delay.remove(player.getUniqueId());
}
}, ConfigFile.getDelaySeconds() * 20L);
}
}