Skip to content

Commit

Permalink
Basic implementation of AntiGriefing features
Browse files Browse the repository at this point in the history
  • Loading branch information
Rhydium committed Nov 7, 2024
1 parent f25aabe commit ea03b6f
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/main/java/com/mythicamc/KitPvP.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.mythicamc;

import com.mythicamc.commands.BuildModeCommand;
import com.mythicamc.listeners.PlayerQuitListener;
import com.mythicamc.listeners.PlayerJoinListener;
import com.mythicamc.listeners.PvPListener;
import com.mythicamc.managers.CombatManager;
import com.mythicamc.managers.DatabaseManager;
import com.mythicamc.managers.PlayerScoreboardManager;
import com.mythicamc.managers.PlayerStatsManager;
import com.mythicamc.utils.AntiGriefUtility;
import com.mythicamc.utils.KitSelectorGUI;
import org.bukkit.plugin.java.JavaPlugin;

public final class KitPvP extends JavaPlugin {
Expand Down Expand Up @@ -53,13 +56,16 @@ public void onDisable() {
}

private void registerCommands() {
// SOON
// Register command
getCommand("kitpvpbuild").setExecutor(new BuildModeCommand());
}

private void registerListeners() {
getServer().getPluginManager().registerEvents(new PlayerJoinListener(this), this);
getServer().getPluginManager().registerEvents(new PvPListener(this), this);
getServer().getPluginManager().registerEvents(new PlayerQuitListener(this), this);
getServer().getPluginManager().registerEvents(new KitSelectorGUI(this), this);
getServer().getPluginManager().registerEvents(new AntiGriefUtility(), this);
}

public CombatManager getCombatManager() {
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/com/mythicamc/commands/BuildModeCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.mythicamc.commands;

import com.mythicamc.utils.AntiGriefUtility;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

import java.util.UUID;

public class BuildModeCommand implements CommandExecutor {
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;

// Check if the player has the necessary permission
if (player.hasPermission("kitpvp.admin.build")) {
UUID playerId = player.getUniqueId();

// Toggle build mode for the player
if (AntiGriefUtility.buildModePlayers.contains(playerId)) {
AntiGriefUtility.buildModePlayers.remove(playerId);
player.sendMessage("Build mode disabled.");
} else {
AntiGriefUtility.buildModePlayers.add(playerId);
player.sendMessage("Build mode enabled.");
}

return true;
} else {
player.sendMessage("You do not have permission to use this command.");
}
} else {
sender.sendMessage("Only players can use this command.");
}
return false;
}
}
57 changes: 57 additions & 0 deletions src/main/java/com/mythicamc/utils/AntiGriefUtility.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.mythicamc.utils;

import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerInteractEvent;

import java.util.HashSet;
import java.util.UUID;

public class AntiGriefUtility implements Listener {

public static final HashSet<UUID> buildModePlayers = new HashSet<>();

@EventHandler
public void onBlockBreak(BlockBreakEvent event) {
Player player = event.getPlayer();
if (!buildModePlayers.contains(player.getUniqueId())) {
event.setCancelled(true);
}
}

@EventHandler
public void onBlockPlace(BlockPlaceEvent event) {
Player player = event.getPlayer();
if (!buildModePlayers.contains(player.getUniqueId())) {
event.setCancelled(true);
}
}

@EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
Player player = event.getPlayer();
if ((event.getAction() == Action.RIGHT_CLICK_BLOCK || event.getAction() == Action.LEFT_CLICK_BLOCK)
&& !buildModePlayers.contains(player.getUniqueId())) {
event.setCancelled(true);
}
}

@EventHandler
public void onEntityDamage(EntityDamageEvent event) {
// Check if the entity is a player and the damage cause is falling
if (event.getEntity() instanceof Player && event.getCause() == EntityDamageEvent.DamageCause.FALL) {
event.setCancelled(true); // Cancel the fall damage
}
}

@EventHandler
public void onPlayerDropItem(PlayerDropItemEvent event) {
event.setCancelled(true);
}
}
4 changes: 4 additions & 0 deletions src/main/java/com/mythicamc/utils/KitSelectorGUI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.mythicamc.utils;

public class KitSelectorGUI {
}
7 changes: 6 additions & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@ api-version: '1.21'
authors: [ Rhydium ]
description: KitPvP custom built for MythicaMC
website: https://mythicamc.com
softdepend: ["PlaceholderAPI"]
softdepend: ["PlaceholderAPI"]
commands:
kitpvpbuild:
description: Toggles build mode for admins.
usage: /kitpvp build
permission: kitpvp.admin.build

0 comments on commit ea03b6f

Please sign in to comment.