1
0
mirror of https://github.com/Zrips/Jobs.git synced 2025-01-16 21:21:20 +01:00

Players can have multiple campfires

This commit is contained in:
montlikadani 2021-05-16 13:57:16 +02:00
parent ae1b8b5d3c
commit 0e2621e42a
4 changed files with 89 additions and 49 deletions

View File

@ -129,20 +129,18 @@ public class PlayerManager {
public void addPlayerToCache(JobsPlayer jPlayer) {
String jName = jPlayer.getName().toLowerCase();
if (!playersCache.containsKey(jName))
playersCache.put(jName, jPlayer);
playersCache.put(jName, jPlayer);
if (jPlayer.getUniqueId() != null && !playersUUIDCache.containsKey(jPlayer.getUniqueId()))
if (jPlayer.getUniqueId() != null)
playersUUIDCache.put(jPlayer.getUniqueId(), jPlayer);
}
public void addPlayer(JobsPlayer jPlayer) {
String jName = jPlayer.getName().toLowerCase();
if (!players.containsKey(jName))
players.put(jName, jPlayer);
players.put(jName, jPlayer);
if (jPlayer.getUniqueId() != null && !playersUUID.containsKey(jPlayer.getUniqueId()))
if (jPlayer.getUniqueId() != null)
playersUUID.put(jPlayer.getUniqueId(), jPlayer);
}

View File

@ -1,48 +1,89 @@
package com.gamingmesh.jobs.listeners;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import org.bukkit.block.Campfire;
import org.bukkit.entity.Player;
import org.bukkit.Material;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockCookEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import com.gamingmesh.jobs.Jobs;
import com.gamingmesh.jobs.actions.ItemActionInfo;
import com.gamingmesh.jobs.container.ActionType;
import com.gamingmesh.jobs.container.JobsPlayer;
import com.gamingmesh.jobs.container.PlayerCamp;
public class JobsPayment14Listener implements Listener {
public final class JobsPayment14Listener implements Listener {
private final Map<PlayerCamp, Player> campPlayers = new HashMap<>();
// BlockCookEvent does not have "cooking owner"
private final Map<UUID, List<PlayerCamp>> campPlayers = new HashMap<>();
@EventHandler(priority = EventPriority.LOW)
public void onCook(BlockCookEvent event) {
if (event.isCancelled())
if (event.isCancelled() || !(event.getBlock().getType() != Material.CAMPFIRE) || campPlayers.isEmpty())
return;
if (!Jobs.getGCManager().canPerformActionInWorld(event.getBlock().getWorld()) || !(event.getBlock().getState() instanceof Campfire))
if (!Jobs.getGCManager().canPerformActionInWorld(event.getBlock().getWorld()))
return;
for (Iterator<Entry<PlayerCamp, Player>> it = campPlayers.entrySet().iterator(); it.hasNext();) {
Entry<PlayerCamp, Player> camps = it.next();
if (!camps.getKey().getBlock().getLocation().equals(event.getBlock().getLocation()))
for (Map.Entry<UUID, List<PlayerCamp>> map : campPlayers.entrySet()) {
List<PlayerCamp> camps = map.getValue();
if (camps.isEmpty()) {
campPlayers.remove(map.getKey());
continue;
}
JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayer(camps.getValue());
if (jPlayer == null)
for (PlayerCamp camp : new ArrayList<>(camps)) {
if (camp.getBlock().getLocation().equals(event.getBlock().getLocation())) {
if (camp.getItem().equals(event.getSource())) {
camps.remove(camp);
if (camps.isEmpty()) {
campPlayers.remove(map.getKey());
} else {
campPlayers.replace(map.getKey(), camps);
}
}
Jobs.action(Jobs.getPlayerManager().getJobsPlayer(map.getKey()), new ItemActionInfo(event.getSource(), ActionType.BAKE));
break;
}
}
}
}
@EventHandler(ignoreCancelled = true)
public void onBlockBreak(BlockBreakEvent event) {
if (event.getBlock().getType() != Material.CAMPFIRE || campPlayers.isEmpty())
return;
List<PlayerCamp> camps = campPlayers.get(event.getPlayer().getUniqueId());
if (camps != null) {
if (camps.isEmpty()) {
campPlayers.remove(event.getPlayer().getUniqueId());
return;
}
Jobs.action(jPlayer, new ItemActionInfo(event.getSource(), ActionType.BAKE));
if (camps.getKey().getItem().equals(event.getSource())) {
it.remove();
for (PlayerCamp camp : new ArrayList<>(camps)) {
if (camp.getBlock().getLocation().equals(event.getBlock().getLocation())) {
camps.remove(camp);
if (camps.isEmpty()) {
campPlayers.remove(event.getPlayer().getUniqueId());
} else {
campPlayers.replace(event.getPlayer().getUniqueId(), camps);
}
break;
}
}
}
}
@ -50,15 +91,16 @@ public class JobsPayment14Listener implements Listener {
@EventHandler(priority = EventPriority.LOWEST)
public void onCampPlace(PlayerInteractEvent ev) {
org.bukkit.block.Block click = ev.getClickedBlock();
if (click == null || click.getType() != org.bukkit.Material.CAMPFIRE)
if (click == null || click.getType() != Material.CAMPFIRE || !ev.hasItem())
return;
if (!Jobs.getGCManager().canPerformActionInWorld(click.getWorld()) || !(click.getState() instanceof Campfire))
if (!Jobs.getGCManager().canPerformActionInWorld(click.getWorld()) || !JobsPaymentListener.payIfCreative(ev.getPlayer()))
return;
if (!ev.hasItem() || !JobsPaymentListener.payIfCreative(ev.getPlayer()))
return;
List<PlayerCamp> camps = campPlayers.getOrDefault(ev.getPlayer().getUniqueId(), new ArrayList<>());
camps.add(new PlayerCamp(ev.getItem(), click));
campPlayers.put(new PlayerCamp(ev.getItem(), click), ev.getPlayer());
campPlayers.put(ev.getPlayer().getUniqueId(), camps);
}
}

View File

@ -987,10 +987,9 @@ public class JobsPaymentListener implements Listener {
if (!Jobs.getGCManager().PreventHopperFillUps || event.getItem().getType() == Material.AIR)
return;
String type = event.getDestination().getType().toString();
Block block = null;
switch (type.toLowerCase()) {
switch (event.getDestination().getType().toString().toLowerCase()) {
case "furnace":
block = ((Furnace) event.getDestination().getHolder()).getBlock();
break;
@ -1068,11 +1067,12 @@ public class JobsPaymentListener implements Listener {
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onEntityDamageByPlayer(EntityDamageEvent event) {
if (!Jobs.getGCManager().MonsterDamageUse || !Jobs.getGCManager().canPerformActionInWorld(event.getEntity().getWorld()))
if (!Jobs.getGCManager().MonsterDamageUse || !(event instanceof EntityDamageByEntityEvent)
|| !Jobs.getGCManager().canPerformActionInWorld(event.getEntity().getWorld()))
return;
Entity ent = event.getEntity();
if (ent instanceof Player || !(ent instanceof Damageable) || !(event instanceof EntityDamageByEntityEvent))
if (ent instanceof Player || !(ent instanceof Damageable))
return;
if (!(((EntityDamageByEntityEvent) event).getDamager() instanceof Player))
@ -1125,8 +1125,8 @@ public class JobsPaymentListener implements Listener {
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onEntityDeath(EntityDeathEvent event) {
if (!Jobs.getGCManager().canPerformActionInWorld(event.getEntity().getWorld())
|| !(event.getEntity().getLastDamageCause() instanceof EntityDamageByEntityEvent))
if (!(event.getEntity().getLastDamageCause() instanceof EntityDamageByEntityEvent) ||
!Jobs.getGCManager().canPerformActionInWorld(event.getEntity().getWorld()))
return;
EntityDamageByEntityEvent e = (EntityDamageByEntityEvent) event.getEntity().getLastDamageCause();
@ -1458,12 +1458,14 @@ public class JobsPaymentListener implements Listener {
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerEat(FoodLevelChangeEvent event) {
if (!(event.getEntity() instanceof Player) || !Jobs.getGCManager().canPerformActionInWorld(event.getEntity().getWorld())
|| event.getEntity().hasMetadata("NPC"))
HumanEntity human = event.getEntity();
if (event.getFoodLevel() <= human.getFoodLevel() || !(human instanceof Player)
|| !Jobs.getGCManager().canPerformActionInWorld(human.getWorld()) || human.hasMetadata("NPC"))
return;
Player player = (Player) event.getEntity();
if (!player.isOnline() || event.getFoodLevel() <= player.getFoodLevel())
Player player = (Player) human;
if (!player.isOnline())
return;
// check if in creative
@ -1643,14 +1645,11 @@ public class JobsPaymentListener implements Listener {
@EventHandler
public void onExplore(JobsChunkChangeEvent event) {
if (event.isCancelled())
if (event.isCancelled() || !Jobs.getExplore().isExploreEnabled())
return;
Player player = event.getPlayer();
if (player == null || !player.isOnline() || !Jobs.getGCManager().canPerformActionInWorld(player.getWorld()))
return;
if (!Jobs.getExplore().isExploreEnabled())
if (player == null || !player.isOnline())
return;
// check if in spectator, #330
@ -1668,11 +1667,13 @@ public class JobsPaymentListener implements Listener {
&& !Jobs.getGCManager().payExploringWhenGliding && player.isGliding())
return;
org.bukkit.World playerWorld = player.getWorld();
// check if in creative
if (!payIfCreative(player))
if (!Jobs.getGCManager().canPerformActionInWorld(playerWorld) || !payIfCreative(player))
return;
if (!Jobs.getPermissionHandler().hasWorldPermission(player, player.getLocation().getWorld().getName()))
if (!Jobs.getPermissionHandler().hasWorldPermission(player, playerWorld.getName()))
return;
JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayer(player);
@ -1692,8 +1693,7 @@ public class JobsPaymentListener implements Listener {
if (Version.isCurrentEqualOrHigher(Version.v1_13_R1) && entity.isPersistent())
break;
if (entity.hasMetadata(Jobs.getPlayerManager().getMobSpawnerMetadata()))
entity.removeMetadata(Jobs.getPlayerManager().getMobSpawnerMetadata(), plugin);
entity.removeMetadata(Jobs.getPlayerManager().getMobSpawnerMetadata(), plugin);
}
}

View File

@ -41,8 +41,8 @@ public class BufferedPaymentTask implements Runnable {
double money = payment.get(CurrencyType.MONEY);
if (money > 0) {
if (Jobs.getGCManager().isEconomyAsync()) {
org.bukkit.Bukkit.getScheduler().runTaskLater(bufferedEconomy.getPlugin(), () ->
economy.depositPlayer(payment.getOfflinePlayer(), money), 1L);
org.bukkit.Bukkit.getScheduler().scheduleSyncDelayedTask(bufferedEconomy.getPlugin(), () ->
economy.depositPlayer(payment.getOfflinePlayer(), money));
} else {
economy.depositPlayer(payment.getOfflinePlayer(), money);
}