Skip to content

Commit

Permalink
Fixed issue #1009 and made it more configurable.
Browse files Browse the repository at this point in the history
It now uses the output from `/is info` to count items.
Might be memory intensive, so perhaps it should be disabled per default.
  • Loading branch information
rlf committed Jul 13, 2018
1 parent 4efa91e commit db0f6ee
Show file tree
Hide file tree
Showing 43 changed files with 919 additions and 105 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package us.talabrek.ultimateskyblock.async;
package us.talabrek.ultimateskyblock.api.async;

/**
* Runnable with state.
* @since v2.7.3
*/
public abstract class Callback<T> implements Runnable {
private volatile T state;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package us.talabrek.ultimateskyblock.api.event;

import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.player.PlayerEvent;

/**
* Common event for PlayerEvents that can be cancelled (async).
* @since 2.7.3
*/
public abstract class CancellableAsyncPlayerEvent extends Event implements Cancellable {
private boolean cancelled = false;
private Player player;

public CancellableAsyncPlayerEvent(Player player) {
super(true);
this.player = player;
}

public Player getPlayer() {
return player;
}

@Override
public boolean isCancelled() {
return cancelled;
}

@Override
public void setCancelled(boolean cancel) {
this.cancelled = cancel;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package us.talabrek.ultimateskyblock.api.event;

import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
import us.talabrek.ultimateskyblock.api.async.Callback;
import us.talabrek.ultimateskyblock.api.model.IslandScore;

/**
* @since v2.7.3
*/
public class IslandInfoEvent extends CancellableAsyncPlayerEvent {
private static final HandlerList handlers = new HandlerList();
private final Location islandLocation;
private final Callback<IslandScore> callback;

public IslandInfoEvent(Player who, Location islandLocation, Callback<IslandScore> callback) {
super(who);
this.islandLocation = islandLocation;
this.callback = callback;
}

public Location getIslandLocation() {
return islandLocation;
}

public Callback<IslandScore> getCallback() {
return callback;
}

@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package us.talabrek.ultimateskyblock.api.event;

import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
import us.talabrek.ultimateskyblock.api.model.IslandScore;
Expand All @@ -12,10 +13,12 @@ public class uSkyBlockScoreChangedEvent extends uSkyBlockEvent {
private static final HandlerList handlers = new HandlerList();

private final IslandScore score;
private final Location islandLocation;

public uSkyBlockScoreChangedEvent(Player player, uSkyBlockAPI api, IslandScore score) {
public uSkyBlockScoreChangedEvent(Player player, uSkyBlockAPI api, IslandScore score, Location islandLocation) {
super(player, api, Cause.SCORE_CHANGED);
this.score = score;
this.islandLocation = islandLocation;
}

/**
Expand All @@ -26,6 +29,10 @@ public IslandScore getScore() {
return score;
}

public Location getIslandLocation() {
return islandLocation;
}

@Override
public HandlerList getHandlers() {
return handlers;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package us.talabrek.ultimateskyblock.command.admin;

import dk.lockfuglsang.minecraft.command.AbstractCommand;
import dk.lockfuglsang.minecraft.po.I18nUtil;
import org.bukkit.command.CommandSender;
import org.bukkit.scheduler.BukkitRunnable;
import us.talabrek.ultimateskyblock.async.Callback;
import us.talabrek.ultimateskyblock.command.admin.task.PurgeScanTask;
import us.talabrek.ultimateskyblock.command.admin.task.PurgeTask;
import us.talabrek.ultimateskyblock.uSkyBlock;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.bukkit.block.Biome;
import org.bukkit.entity.Player;
import us.talabrek.ultimateskyblock.Settings;
import us.talabrek.ultimateskyblock.async.Callback;
import us.talabrek.ultimateskyblock.handler.WorldGuardHandler;
import us.talabrek.ultimateskyblock.island.IslandInfo;
import us.talabrek.ultimateskyblock.island.task.SetBiomeTask;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package us.talabrek.ultimateskyblock.command.island;

import org.bukkit.entity.Player;
import org.bukkit.metadata.FixedMetadataValue;
import us.talabrek.ultimateskyblock.Settings;
import us.talabrek.ultimateskyblock.api.async.Callback;
import us.talabrek.ultimateskyblock.api.model.BlockScore;
import us.talabrek.ultimateskyblock.async.Callback;
import us.talabrek.ultimateskyblock.handler.VaultHandler;
import us.talabrek.ultimateskyblock.island.IslandInfo;
import us.talabrek.ultimateskyblock.island.IslandScore;
import us.talabrek.ultimateskyblock.player.PatienceTester;
import us.talabrek.ultimateskyblock.player.PlayerInfo;
import us.talabrek.ultimateskyblock.uSkyBlock;
import us.talabrek.ultimateskyblock.util.LogUtil;
Expand All @@ -30,6 +29,9 @@ protected boolean doExecute(String alias, Player player, PlayerInfo pi, IslandIn
player.sendMessage(tr("\u00a74Island level has been disabled, contact an administrator."));
return true;
}
if (PatienceTester.isRunning(player, "usb.island.info.active")) {
return true;
}
if (player.hasMetadata("usb.island.info.active")) {
player.sendMessage(tr("\u00a74Hold your horses! \u00a7eYou have to be patient..."));
return true;
Expand Down Expand Up @@ -63,7 +65,7 @@ public boolean getIslandInfo(final Player player, final String islandPlayer, fin
return false;
}
final PlayerInfo playerInfo = islandPlayer.equals(player.getName()) ? plugin.getPlayerInfo(player) : plugin.getPlayerInfo(islandPlayer);
final Callback<IslandScore> showInfo = new Callback<IslandScore>() {
final Callback<us.talabrek.ultimateskyblock.api.model.IslandScore> showInfo = new Callback<us.talabrek.ultimateskyblock.api.model.IslandScore>() {
@Override
public void run() {
if (player.isOnline()) {
Expand All @@ -79,19 +81,19 @@ public void run() {
if (cmd.equalsIgnoreCase("info") && getState() != null) {
player.sendMessage(tr("Score Count Block"));
for (BlockScore score : getState().getTop((currentPage - 1) * 10, 10)) {
player.sendMessage(score.getState().getColor() + String.format("%05.2f %d %s",
player.sendMessage(score.getState().getColor() + tr("{0,number,#####.##} {1,number,#} {2}",
score.getScore(), score.getCount(),
VaultHandler.getItemName(score.getBlock())));
}
player.sendMessage(tr("\u00a7aIsland level is {0,number,###.##}", getState().getScore()));
}
}
player.removeMetadata("usb.island.info.active", plugin);
PatienceTester.stopRunning(player, "usb.island.info.active");
}
};
plugin.sync(() -> {
try {
player.setMetadata("usb.island.info.active", new FixedMetadataValue(plugin, Boolean.TRUE));
PatienceTester.startRunning(player, "usb.island.info.active");
plugin.calculateScoreAsync(player, playerInfo.locationForParty(), showInfo);
} catch (Exception e) {
LogUtil.log(Level.SEVERE, "Error while calculating Island Level", e);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package us.talabrek.ultimateskyblock.command.island;

import dk.lockfuglsang.minecraft.po.I18nUtil;
import org.bukkit.entity.Player;
import us.talabrek.ultimateskyblock.Settings;
import us.talabrek.ultimateskyblock.api.IslandRank;
import us.talabrek.ultimateskyblock.async.Callback;
import us.talabrek.ultimateskyblock.api.async.Callback;
import us.talabrek.ultimateskyblock.island.IslandInfo;
import us.talabrek.ultimateskyblock.island.IslandScore;
import us.talabrek.ultimateskyblock.island.level.IslandScore;
import us.talabrek.ultimateskyblock.player.PlayerInfo;
import us.talabrek.ultimateskyblock.uSkyBlock;

import java.util.Map;

import static dk.lockfuglsang.minecraft.perm.PermissionUtil.hasPermission;
import static dk.lockfuglsang.minecraft.po.I18nUtil.*;
import static dk.lockfuglsang.minecraft.po.I18nUtil.tr;

public class LevelCommand extends RequireIslandCommand {
Expand Down Expand Up @@ -81,7 +79,7 @@ public void run() {
plugin.getServer().getScheduler().runTaskLater(plugin, new Runnable() {
@Override
public void run() {
plugin.calculateScoreAsync(player, info.locationForParty(), new Callback<IslandScore>() {
plugin.calculateScoreAsync(player, info.locationForParty(), new Callback<us.talabrek.ultimateskyblock.api.model.IslandScore>() {
@Override
public void run() {
plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, showInfo, 10L);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import us.talabrek.ultimateskyblock.api.event.CreateIslandEvent;
import us.talabrek.ultimateskyblock.api.event.IslandInfoEvent;
import us.talabrek.ultimateskyblock.api.event.MemberJoinedEvent;
import us.talabrek.ultimateskyblock.api.event.MemberLeftEvent;
import us.talabrek.ultimateskyblock.api.event.RestartIslandEvent;
import us.talabrek.ultimateskyblock.api.event.uSkyBlockScoreChangedEvent;
import us.talabrek.ultimateskyblock.api.async.Callback;
import us.talabrek.ultimateskyblock.island.level.IslandScore;
import us.talabrek.ultimateskyblock.player.PlayerInfo;
import us.talabrek.ultimateskyblock.uSkyBlock;
import us.talabrek.ultimateskyblock.util.LocationUtil;

/**
* Main event-handler for internal uSkyBlock events
Expand Down Expand Up @@ -45,4 +50,14 @@ public void onMemberLeft(MemberLeftEvent e) {
PlayerInfo playerInfo = (PlayerInfo) e.getPlayerInfo();
playerInfo.execCommands(plugin.getConfig().getStringList("options.party.leave-commands"));
}

@EventHandler
public void onScoreChanged(uSkyBlockScoreChangedEvent e) {
plugin.getBlockLimitLogic().updateBlockCount(e.getIslandLocation(), (IslandScore) e.getScore());
}

@EventHandler
public void onInfoEvent(IslandInfoEvent e) {
plugin.calculateScoreAsync(e.getPlayer(), LocationUtil.getIslandName(e.getIslandLocation()), e.getCallback());
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package us.talabrek.ultimateskyblock.event;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.EnderPearl;
import org.bukkit.entity.Monster;
import org.bukkit.entity.Player;
Expand All @@ -25,8 +25,14 @@
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.projectiles.ProjectileSource;
import us.talabrek.ultimateskyblock.Settings;
import us.talabrek.ultimateskyblock.api.async.Callback;
import us.talabrek.ultimateskyblock.api.event.IslandInfoEvent;
import us.talabrek.ultimateskyblock.api.model.IslandScore;
import us.talabrek.ultimateskyblock.handler.VaultHandler;
import us.talabrek.ultimateskyblock.handler.WorldGuardHandler;
import us.talabrek.ultimateskyblock.island.BlockLimitLogic;
import us.talabrek.ultimateskyblock.island.IslandInfo;
import us.talabrek.ultimateskyblock.player.PatienceTester;
import us.talabrek.ultimateskyblock.player.Perk;
import us.talabrek.ultimateskyblock.player.PlayerInfo;
import us.talabrek.ultimateskyblock.uSkyBlock;
Expand All @@ -38,7 +44,6 @@
import java.util.Set;
import java.util.UUID;
import java.util.WeakHashMap;
import org.bukkit.ChatColor;

import static dk.lockfuglsang.minecraft.perm.PermissionUtil.hasPermission;
import static dk.lockfuglsang.minecraft.po.I18nUtil.tr;
Expand All @@ -56,15 +61,16 @@ public class PlayerEvents implements Listener {
private final boolean visitorMonsterProtected;
private final boolean protectLava;
private final Map<UUID, Long> obsidianClick = new WeakHashMap<>();
private final int hopperlimit;
private final boolean blockLimitsEnabled;

public PlayerEvents(uSkyBlock plugin) {
this.plugin = plugin;
visitorFallProtected = plugin.getConfig().getBoolean("options.protection.visitors.fall", true);
visitorFireProtected = plugin.getConfig().getBoolean("options.protection.visitors.fire-damage", true);
visitorMonsterProtected = plugin.getConfig().getBoolean("options.protection.visitors.monster-damage", false);
protectLava = plugin.getConfig().getBoolean("options.protection.protect-lava", true);
hopperlimit = plugin.getConfig().getInt("options.island.hopperlimit", 10);
FileConfiguration config = plugin.getConfig();
visitorFallProtected = config.getBoolean("options.protection.visitors.fall", true);
visitorFireProtected = config.getBoolean("options.protection.visitors.fire-damage", true);
visitorMonsterProtected = config.getBoolean("options.protection.visitors.monster-damage", false);
protectLava = config.getBoolean("options.protection.protect-lava", true);
blockLimitsEnabled = config.getBoolean("options.island.block-limits.enabled", false);
}

@EventHandler(priority = EventPriority.NORMAL)
Expand Down Expand Up @@ -264,28 +270,52 @@ public void onLeafBreak(BlockBreakEvent event) {
}

@EventHandler
public void onHopperPlace(BlockPlaceEvent event)
public void onBlockPlaceEvent(BlockPlaceEvent event)
{
if (Material.HOPPER.equals(event.getBlock().getType()))
{
IslandInfo isInfo = plugin.getIslandInfo(event.getBlock().getLocation());
if(isInfo.getHopperCount() > hopperlimit)
{
event.setCancelled(true);
event.getPlayer().sendMessage(tr("\u00a74You've hit the hopper limit! You can't have more hoppers!"));
}
else
{
isInfo.setHopperCount(isInfo.getHopperCount() + 1);
final Player player = event.getPlayer();
if (!blockLimitsEnabled || player == null || !plugin.isSkyWorld(player.getWorld()) || event.isCancelled()) {
return; // Skip
}

IslandInfo islandInfo = plugin.getIslandInfo(event.getBlock().getLocation());
if (islandInfo == null) {
return;
}
Material type = event.getBlock().getType();
BlockLimitLogic.CanPlace canPlace = plugin.getBlockLimitLogic().canPlace(type, islandInfo.getIslandLocation());
if (canPlace == BlockLimitLogic.CanPlace.UNCERTAIN) {
event.setCancelled(true);
final String key = "usb.block-limits";
if (!PatienceTester.isRunning(player, key)) {
PatienceTester.startRunning(player, key);
player.sendMessage(tr("\u00a74{0} is limited. \u00a7eScanning your island to see if you are allowed to place more, please be patient", VaultHandler.getItemName(new ItemStack(type))));
plugin.fireAsyncEvent(new IslandInfoEvent(player, islandInfo.getIslandLocation(), new Callback<IslandScore>() {
@Override
public void run() {
player.sendMessage(tr("\u00a7e... Scanning complete, you can try again"));
PatienceTester.stopRunning(player, key);
}
}));
}
return;
}
if (canPlace == BlockLimitLogic.CanPlace.NO) {
event.setCancelled(true);
player.sendMessage(tr("\u00a74You''ve hit the {0} limit!\u00a7e You can''t have more of that type on your island!\u00a79 Max: {1,number}", VaultHandler.getItemName(new ItemStack(type)), plugin.getBlockLimitLogic().getLimit(type)));
return;
}
plugin.getBlockLimitLogic().incBlockCount(islandInfo.getIslandLocation(), type);
}

@EventHandler
public void onHopperDestroy(BlockBreakEvent event){
if (Material.HOPPER.equals(event.getBlock().getType())){
IslandInfo isInfo = plugin.getIslandInfo(event.getBlock().getLocation());
isInfo.setHopperCount(isInfo.getHopperCount() - 1);
}
if (!blockLimitsEnabled || event.getPlayer() == null || !plugin.isSkyWorld(event.getPlayer().getWorld()) || event.isCancelled()) {
return; // Skip
}
IslandInfo islandInfo = plugin.getIslandInfo(event.getBlock().getLocation());
if (islandInfo == null) {
return;
}
plugin.getBlockLimitLogic().decBlockCount(islandInfo.getIslandLocation(), event.getBlock().getType());
}
}
Loading

0 comments on commit db0f6ee

Please sign in to comment.