Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cancel block place/break event during preparation time #57

Merged
merged 2 commits into from
Oct 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ private void registerEvents() {
pm.registerEvents(new PlayerJoinListener(), this);
pm.registerEvents(new PlayerDeathListener(), this);
pm.registerEvents(new EntityDamageListener(), this);
pm.registerEvents(new BlockUpdateListener(), this);
pm.registerEvents(new PlayerRespawnListener(), this);
pm.registerEvents(new PlayerTeleportListener(), this);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package me.roinujnosde.titansbattle.listeners;

import me.roinujnosde.titansbattle.TitansBattle;
import me.roinujnosde.titansbattle.games.Game;
import me.roinujnosde.titansbattle.managers.DatabaseManager;
import me.roinujnosde.titansbattle.managers.GameManager;
import me.roinujnosde.titansbattle.types.Warrior;
import org.bukkit.entity.Player;
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.BlockPlaceEvent;

public class BlockUpdateListener implements Listener {

private final TitansBattle plugin = TitansBattle.getInstance();
private final GameManager gm;
private final DatabaseManager dm;

public BlockUpdateListener() {
gm = plugin.getGameManager();
dm = plugin.getDatabaseManager();
}

@EventHandler(priority = EventPriority.HIGHEST)
public void onBreak(BlockBreakEvent event) {
if (shouldCancel(event.getPlayer())) {
event.setCancelled(true);
}
}

@EventHandler(priority = EventPriority.HIGHEST)
public void onPlace(BlockPlaceEvent event) {
if (shouldCancel(event.getPlayer())) {
event.setCancelled(true);
}
}

private boolean shouldCancel(Player player) {
Game game = gm.getCurrentGame().orElse(null);
if (game == null) {
return false;
}

Warrior warrior = dm.getWarrior(player);
if (!game.isParticipant(warrior)) {
return false;
}

return !game.isInBattle(warrior);
}

}