Skip to content

Commit

Permalink
Merge pull request #60 from Grzybol/patch-4.3.0
Browse files Browse the repository at this point in the history
Patch 4.3.0
  • Loading branch information
Grzybol authored Apr 17, 2024
2 parents b8f9b0e + 40ae1e0 commit a178475
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 173 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>betterbox.mine.game</groupId>
<artifactId>BetterElo</artifactId>
<version>4.3.1-SNAPSHOT</version>
<version>4.3.8-SNAPSHOT</version>
<packaging>jar</packaging>

<name>BetterElo</name>
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/betterbox/mine/game/betterelo/BetterElo.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,11 @@ public void onEnable() {
customMobs.startSpawnerScheduler();

guiManager = new GuiManager(fileRewardManager, pluginLogger, this, dataManager, customMobsFileManager, customMobs, this);
getServer().getPluginManager().registerEvents(guiManager, this);
// Rejestracja komendy
betterRanksCheaters = new BetterRanksCheaters(this,pluginLogger);
CheaterCheckScheduler cheaterCheckScheduler = new CheaterCheckScheduler(this, betterRanksCheaters, getServer().getScheduler(), pluginLogger);
// Rejestracja listenera eventów
event = new Event(dataManager, pluginLogger,this,betterRanksCheaters,configManager,this,customMobs,fileRewardManager);
event = new Event(dataManager, pluginLogger,this,betterRanksCheaters,configManager,this,customMobs,fileRewardManager,guiManager);
getServer().getPluginManager().registerEvents(event, this);
getCommand("be").setExecutor(new BetterEloCommand(this, dataManager, guiManager, pluginLogger, this, configManager,event,PKDB, customMobs, customMobsFileManager));
pluginLogger.log(PluginLogger.LogLevel.DEBUG,"BetterElo: onEnable: Plugin BetterElo został włączony pomyślnie.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public static String dropAverageDamage() {
// Używamy funkcji wykładniczej do zmniejszenia prawdopodobieństwa wyższych wartości
double x = -Math.log(random.nextDouble()) / 10.0; // Dostosuj parametr 10.0, aby zmienić rozkład
//double y = random.
int bonus = (int) Math.round(x * 100); // Skalowanie wyniku
int bonus = (int) Math.round(x * 40); // Skalowanie wyniku

// Ograniczamy wartość bonusu do maksymalnie 60%
bonus = Math.min(bonus, 60);
Expand Down
187 changes: 183 additions & 4 deletions src/main/java/betterbox/mine/game/betterelo/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.inventory.EntityEquipment;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.FireworkMeta;
import org.bukkit.inventory.meta.ItemMeta;
Expand All @@ -53,11 +56,12 @@ public class Event implements Listener {
private HashMap<Player, Long> lastZephyrUsage = new HashMap<>();
private HashMap<Player, Long> lastFlameUsage = new HashMap<>();
private CustomMobs customMobs;
private GuiManager guiManager;
private FileRewardManager fileRewardManager;
private final Random random = new Random();
//public final long cooldownMillis = 1500; // 1.5s

public Event(DataManager dataManager, PluginLogger pluginLogger, JavaPlugin plugin, BetterRanksCheaters cheaters, ExtendedConfigManager configManager, BetterElo betterElo, CustomMobs customMobs, FileRewardManager fileRewardManager) {
public Event(DataManager dataManager, PluginLogger pluginLogger, JavaPlugin plugin, BetterRanksCheaters cheaters, ExtendedConfigManager configManager, BetterElo betterElo, CustomMobs customMobs, FileRewardManager fileRewardManager, GuiManager guiManager) {
this.dataManager = dataManager;
this.fileRewardManager = fileRewardManager;
this.pluginLogger = pluginLogger;
Expand All @@ -66,6 +70,7 @@ public Event(DataManager dataManager, PluginLogger pluginLogger, JavaPlugin plug
this.cheaters = cheaters;
this.configManager = configManager;
this.customMobs = customMobs;
this.guiManager = guiManager;
Bukkit.getPluginManager().registerEvents(this, plugin);
}

Expand Down Expand Up @@ -1049,18 +1054,23 @@ private ItemStack dropMobKillerSword(){
return null;
}
}
@EventHandler
@EventHandler(priority = EventPriority.HIGHEST)
public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
//pluginLogger.log(PluginLogger.LogLevel.KILL_EVENT, "Event.onEntityDamageByEntity onEntityDamageByEntity called");
if (event.getDamager() instanceof Player && event.getEntity() instanceof Player) {

if (event.getDamager() instanceof Player && event.getEntity() instanceof Player && !event.isCancelled()) {

Player damager = (Player) event.getDamager();
Player victim = (Player) event.getEntity();
if (damager.hasMetadata("handledDamage")) {
pluginLogger.log(PluginLogger.LogLevel.DEBUG, "Event.onEntityDamageByEntity event already handled!");
return;
}

ItemStack itemInHand = damager.getInventory().getItemInMainHand();
List<ItemStack> equippedItems = getPlayerEquippedItems(damager);
double averageDamageBonusPercent =0;
averageDamageBonusPercent = getTotalAvgDmgBonus(equippedItems);
averageDamageBonusPercent = getTotalAvgDmgBonus(equippedItems)/100;
double totalDamage = event.getDamage() + event.getDamage()*averageDamageBonusPercent;
pluginLogger.log(PluginLogger.LogLevel.DEBUG, "Event.onEntityDamageByEntity event.getDamage(): "+event.getDamage()+", averageDamageBonusPercent: "+averageDamageBonusPercent+", totalDamage: "+totalDamage);

Expand All @@ -1071,6 +1081,13 @@ public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
updateLastHitTime(damager);
//pluginLogger.log(PluginLogger.LogLevel.KILL_EVENT, "Event: onEntityDamageByEntity: calling updateLastHitTime(victim) "+victim);
updateLastHitTime(victim);
damager.setMetadata("handledDamage", new FixedMetadataValue(plugin, true));
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
@Override
public void run() {
damager.removeMetadata("handledDamage", plugin);
}
}, 1L);
}
LivingEntity entity = (LivingEntity) event.getEntity();
if (entity.hasMetadata("CustomMob")){
Expand Down Expand Up @@ -1223,5 +1240,167 @@ public boolean isValidAverageDamage (ArrayList<ItemStack> equippedItems){
pluginLogger.log(PluginLogger.LogLevel.CUSTOM_MOBS, "Event.TotalAvgDmgBonus total isValidAverageDamage: " + isValidAverageDamage);
return isValidAverageDamage;
}
@EventHandler(priority = EventPriority.LOW)
public void onInventoryClick(InventoryClickEvent event) {
if(event.getCurrentItem().getType() == Material.GRAY_STAINED_GLASS_PANE || event.getCurrentItem().getType() == Material.GREEN_WOOL){
pluginLogger.log(PluginLogger.LogLevel.DEBUG, "GuiManager.onInventoryClick green wool or blank pane clicked, cancelling..");
event.setCancelled(true);

}
Player player = (Player) event.getWhoClicked();
ItemStack currentItem = event.getCurrentItem();
Inventory playerInventory = player.getInventory();
ItemStack[] savedInventory = playerInventory.getContents();

String title = event.getView().getTitle();
pluginLogger.log(PluginLogger.LogLevel.DEBUG, "GuiManager.onInventoryClick called. title:"+title);
if (!Arrays.asList("Set Rewards", "Add Items", "Select Top", "AvgDmg bonus change").contains(title)) {
return;
}




if (currentItem == null || currentItem.getType() == Material.AIR) {
return;
}
Inventory inv;
//String rewardTypeTemp = currentItem.getItemMeta().getDisplayName();
switch (title) {
case "Set Rewards":
event.setCancelled(true);
guiManager.periodType = currentItem.getItemMeta().getDisplayName();
if(guiManager.periodType.equals("dropTable")){
List<ItemStack> currentRewards = fileRewardManager.loadRewards();
inv = Bukkit.createInventory(null, 36, "Add Items");
currentRewards.forEach(inv::addItem);
guiManager.createItem(inv, Material.GREEN_WOOL, 35, "Save", "Save drop table");
player.openInventory(inv);
break;
}
guiManager.openSubGui(player);
break;
case "Select Top":
event.setCancelled(true);
guiManager.rewardType = currentItem.getItemMeta().getDisplayName();
pluginLogger.log("GuiManager.onInventoryClick: rewardType:" + guiManager.rewardType + " periodType:" + guiManager.periodType);
fileRewardManager.setRewardType(guiManager.periodType, guiManager.rewardType);
List<ItemStack> currentRewards = fileRewardManager.loadRewards();
inv = Bukkit.createInventory(null, 36, "Add Items");
currentRewards.forEach(inv::addItem);
guiManager.createItem(inv, Material.GREEN_WOOL, 35, "Save", "Save rewards");
//createItem(inv, Material.ORANGE_WOOL, 34, "Reset", "Resetuj czas");
//createItem(inv, Material.YELLOW_WOOL, 33, "Reedem", "Rozdaj nagrody");
player.openInventory(inv);
break;
case "Add Items":
//save button check
pluginLogger.log(PluginLogger.LogLevel.DEBUG, "GuiManager.onInventoryClick Add Items");
if (currentItem.getType() == Material.GREEN_WOOL && (event.getSlot() == 35 || event.getSlot() == 53)) {
pluginLogger.log(PluginLogger.LogLevel.DEBUG, "GuiManager.onInventoryClick Add Items - save called.");
event.setCancelled(true);
Inventory inventory = event.getInventory();
List<ItemStack> itemsToSave = new ArrayList<>();
for (int i = 0; i < inventory.getSize(); i++) {
if (i == 35 || i == 53) { // Pomijamy slot przycisku "Save"
pluginLogger.log(PluginLogger.LogLevel.DEBUG, "GuiManager.onInventoryClick save button, skipping.");
continue;
}
ItemStack item = inventory.getItem(i);
pluginLogger.log(PluginLogger.LogLevel.DEBUG, "GuiManager.onInventoryClick save: item: "+item);
if (item != null && item.getType() != Material.AIR) {
pluginLogger.log(PluginLogger.LogLevel.DEBUG, "GuiManager.onInventoryClick no air save: item: "+item);
itemsToSave.add(item);
}

}

String fileName=guiManager.periodType+"_"+guiManager.rewardType;
pluginLogger.log(PluginLogger.LogLevel.DEBUG, "GuiManager.onInventoryClick calling fileRewardManager.saveRewards("+fileName+",itemsToSave)");
if(guiManager.periodType.equals("dropTable")){
fileName=guiManager.dropTable;
pluginLogger.log(PluginLogger.LogLevel.DEBUG, "GuiManager.onInventoryClick droptable: "+fileName);
fileRewardManager.saveCustomDrops(fileName, itemsToSave);
}else{
fileRewardManager.saveRewards(fileName, itemsToSave);
}

}
break;
case "AvgDmg bonus change":
pluginLogger.log(PluginLogger.LogLevel.DEBUG, "GuiManager.onInventoryClick Average Damage bonus re-roll");

if (currentItem.getType() == Material.GREEN_WOOL && event.getSlot() == 5){
playerInventory.setContents(savedInventory);
event.setCancelled(true);
pluginLogger.log(PluginLogger.LogLevel.DEBUG, "GuiManager.onInventoryClick Average Damage bonus re-roll clicked");
Inventory inventory = event.getInventory();
ItemStack item0 = inventory.getItem(3);
if (item0 != null && item0.hasItemMeta()) {
pluginLogger.log(PluginLogger.LogLevel.DEBUG, "GuiManager.onInventoryClick reroll, item0: "+item0+", item0.hasItemMeta(): "+item0.hasItemMeta());
ItemMeta meta0 = item0.getItemMeta();
boolean slot0Condition = meta0.getLore().stream().anyMatch(line -> line.contains("Average Damage"));
ItemMeta meta = item0.getItemMeta();
//List<String> lore = meta.getLore();
pluginLogger.log(PluginLogger.LogLevel.DEBUG, "GuiManager.onInventoryClick reroll, slot0Condition: "+slot0Condition);

if (slot0Condition) {
ItemStack result = item0.clone();
ItemMeta resultMeta = result.getItemMeta();
List<String> lore = new ArrayList<>(resultMeta.getLore());
for (int i = 0; i < lore.size(); i++) {
if (lore.get(i).contains("Average Damage")) {
pluginLogger.log(PluginLogger.LogLevel.DEBUG, "GuiManager.onInventoryClick reroll, Average Damage lore line found i: " + i);
if( guiManager.checkAndRemoveBetterCoins(player)) {
pluginLogger.log(PluginLogger.LogLevel.DEBUG, "GuiManager.onInventoryClick reroll, player paid, re-rolling..." );
lore.set(i, customMobs.dropAverageDamage());
break;
}
pluginLogger.log(PluginLogger.LogLevel.DEBUG, "GuiManager.onInventoryClick reroll, player has no money for the re-roll." );
}
}
resultMeta.setLore(lore);
result.setItemMeta(resultMeta);
inventory.setItem(3, result);
pluginLogger.log(PluginLogger.LogLevel.DEBUG, "GuiManager.onInventoryClick result placed back in slot 3");

}
}
}
break;
}

}
@EventHandler
public void onInventoryClose(InventoryCloseEvent event) {
Player player = (Player) event.getPlayer();
Inventory closedInventory = event.getInventory();

// Check if the closed inventory is the same one we're interested in
if (event.getView().getTitle().equalsIgnoreCase("AvgDmg bonus change")) {
pluginLogger.log(PluginLogger.LogLevel.DEBUG, "GuiManager.onInventoryClose: Checking items in closed GUI");

ItemStack itemInSlot0 = closedInventory.getItem(3);
if (itemInSlot0 != null && itemInSlot0.hasItemMeta()) {
ItemMeta meta = itemInSlot0.getItemMeta();
if (meta.getLore().stream().anyMatch(line -> line.contains("Average Damage"))) {
pluginLogger.log(PluginLogger.LogLevel.DEBUG, "GuiManager.onInventoryClose: Item with 'Average damage' found in slot 0");

// Optional: Directly give back the item to the player's inventory
if (player.getInventory().addItem(itemInSlot0).size() == 0) {
// Item successfully added to player's inventory
pluginLogger.log(PluginLogger.LogLevel.DEBUG, "GuiManager.onInventoryClose: Item returned to player inventory");
closedInventory.clear(3); // Clear the slot after returning item
} else {
// Inventory full, drop item at player's location
player.getWorld().dropItem(player.getLocation(), itemInSlot0);
pluginLogger.log(PluginLogger.LogLevel.DEBUG, "GuiManager.onInventoryClose: Inventory full, item dropped at player's location");
closedInventory.clear(3); // Clear the slot
}
}
}
}
}

}

Loading

0 comments on commit a178475

Please sign in to comment.