Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
boonsboos authored Aug 5, 2021
1 parent 46d4842 commit 8694296
Show file tree
Hide file tree
Showing 2 changed files with 195 additions and 0 deletions.
93 changes: 93 additions & 0 deletions src/main/java/online/monkegame/monkeonetap/Listeners.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package online.monkegame.monkeonetap;


import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.TextDecoration;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.Statistic;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.scheduler.BukkitScheduler;

import java.util.logging.Logger;

public class Listeners implements Listener {

public Main plugin;
public Logger log;
public String tappingItem;
public String tappingItemName;
public long updateRate;

public Listeners(Main main, Logger logger, String tappingItem, String tappingItemName, long dbUpdate) {
this.plugin = main;
this.log = logger;
this.tappingItem = tappingItem;
this.tappingItemName = tappingItemName;
this.updateRate = dbUpdate;
}

//event handlers
@EventHandler
public void onJoin(PlayerJoinEvent evt) {
final Player p = evt.getPlayer();
evt.joinMessage(Component.text(p.getName() + " has joined the battle!"));
PotionEffect speed = new PotionEffect(PotionEffectType.SPEED, (int) updateRate, 3, false, false);
PotionEffect saturation = new PotionEffect(PotionEffectType.SATURATION, (int) updateRate, 0, false, false);
p.addPotionEffect(speed);
p.addPotionEffect(saturation);
BukkitScheduler scheduler = Bukkit.getScheduler();
scheduler.runTaskLaterAsynchronously(plugin, () -> {
PlayerInventory inv = p.getInventory();
ItemStack tapper = new ItemStack(Material.valueOf(tappingItem));
ItemMeta meta = tapper.getItemMeta();
assert tappingItemName != null;
meta.displayName(Component.text(tappingItemName).decoration(TextDecoration.ITALIC, false));
meta.addEnchant(Enchantment.DAMAGE_ALL, 1000, true);
tapper.setItemMeta(meta);
inv.setItem(0, tapper);
}, 10L);
}

@EventHandler
public void onQuit(PlayerQuitEvent qevt) {
final Player p = qevt.getPlayer();
qevt.quitMessage(Component.text(p.getName() + " took the coward's way out..."));
PlayerInventory inv = p.getInventory();
inv.clear();
}

@EventHandler
public void onDrop(PlayerDropItemEvent dievt) {
dievt.setCancelled(true);
}

@EventHandler
public void onPlayerRespawn(PlayerRespawnEvent revt) {
PotionEffect speed = new PotionEffect(PotionEffectType.SPEED, (int) updateRate, 3, false, false);
PotionEffect saturation = new PotionEffect(PotionEffectType.SATURATION, (int) updateRate, 0, false, false);
revt.getPlayer().addPotionEffect(speed);
revt.getPlayer().addPotionEffect(saturation);
}

@EventHandler
public void setExp(PlayerDeathEvent devt) {
Player epic = devt.getEntity().getKiller();
float playerkills = epic.getStatistic(Statistic.PLAYER_KILLS);
epic.setExp(playerkills);
}

}
102 changes: 102 additions & 0 deletions src/main/java/online/monkegame/monkeonetap/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package online.monkegame.monkeonetap;

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.format.TextColor;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.Statistic;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitScheduler;

import java.sql.*;
import java.util.UUID;

/*
* made by monkegame team
* Mrs_Herobrine_
* contact here: mrsherobrinenaomi@gmail.com
* discord: mrsherobrine (naomi)#6263
* play.monkegame.online
*/

public final class Main extends JavaPlugin {

public TextComponent updateMessage = Component.text("[monkebot] ",TextColor.color(0x4bcc7f))
.append(Component.text("UPDATING THE DATABASE! EXPECT SOME LAG!")).color(TextColor.color(0xa61111));
public TextComponent updatedMessage = Component.text("[monkebot] ", TextColor.color(0x4bcc7f))
.append(Component.text("Database updated!")).color(TextColor.color(0xbaffe6));
public long updateRate;
public String databaseLocation;
public String databaseTable;
public String tappingItem;
public String tappingItemName;

@Override
public void onEnable() {

this.saveDefaultConfig();

updateRate = (this.getConfig().getLong("db.dbupdaterate") * 20);
databaseLocation = this.getConfig().getString("db.dblocation");
databaseTable = this.getConfig().getString("db.dbtable");
tappingItem = this.getConfig().getString("onetapper.item");
tappingItemName = (this.getConfig().getString("onetapper.name"));

if (databaseLocation == null) {
getLogger().info("You haven't linked your database! Please do so!");
getServer().getPluginManager().disablePlugin(this);
} else if (databaseTable == null) {
getLogger().info("No table found! Have you checked your database?");
getServer().getPluginManager().disablePlugin(this);
} else {
getServer().getPluginManager().registerEvents(new Listeners(this, getLogger(), tappingItem, tappingItemName, updateRate), this);
main();
getLogger().info("Database found! Updates will now be made every " + updateRate/20 + " seconds.");
}
}

public String playerName = "";
public int playerKills = 0;
public UUID playerUuid;

public void main() {
String sql = "INSERT INTO " + databaseTable + " (uuid, username, killcount)\n"
+ "VALUES ('" + playerUuid + "','" + playerName + "','" + playerKills + "')"
+ "ON CONFLICT(uuid) DO UPDATE SET "
+ "username=excluded.username, "
+ "killcount=excluded.killcount;";
String url = "jdbc:sqlite:" + databaseLocation;

BukkitScheduler scheduler = getServer().getScheduler();
scheduler.scheduleSyncRepeatingTask(this, () -> {
Bukkit.broadcast(updateMessage, "*.*");
long startTime = System.currentTimeMillis();

for (OfflinePlayer player : Bukkit.getServer().getOfflinePlayers()) {
playerKills = player.getStatistic(Statistic.PLAYER_KILLS);
playerName = player.getName();
playerUuid = player.getUniqueId();
scheduler.runTaskAsynchronously(this, ()-> {
try (Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement()) {
stmt.execute(sql);
} catch (SQLException e) {
getLogger().severe("Encountered an error: " + e.getMessage());
getLogger().info("Are you sure you configured your database correctly?");
}
});
}

long endTime = System.currentTimeMillis();
getLogger().info("DB update took " + (endTime - startTime) + " ms");
Bukkit.broadcast(updatedMessage, "*.*");
}, 0L, updateRate);
}

@Override
public void onDisable() {
getLogger().info("database will no longer update periodically");
}

}

0 comments on commit 8694296

Please sign in to comment.