Implemented the ability to toggle pvp (#97)

* Implemented the ability to toggle pvp

* Changed listener to  PrePlayerAttackEntityEvent

* ... Updated the formatting so I don't get screamed at !

* _
This commit is contained in:
james 2024-06-04 16:30:34 +01:00 committed by GitHub
parent 9e974ae737
commit e066d9b3a1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 90 additions and 6 deletions

View File

@ -32,6 +32,7 @@ public class ToggleCMD extends PlexCommand
sender.sendMessage(PlexUtils.mmDeserialize("<gray> - Fluidspread" + status("fluidspread"))); sender.sendMessage(PlexUtils.mmDeserialize("<gray> - Fluidspread" + status("fluidspread")));
sender.sendMessage(PlexUtils.mmDeserialize("<gray> - Drops" + status("drops"))); sender.sendMessage(PlexUtils.mmDeserialize("<gray> - Drops" + status("drops")));
sender.sendMessage(PlexUtils.mmDeserialize("<gray> - Redstone" + status("redstone"))); sender.sendMessage(PlexUtils.mmDeserialize("<gray> - Redstone" + status("redstone")));
sender.sendMessage(PlexUtils.mmDeserialize("<gray> - PVP" + status("pvp")));
sender.sendMessage(PlexUtils.mmDeserialize("<gray> - Chat" + status("chat"))); sender.sendMessage(PlexUtils.mmDeserialize("<gray> - Chat" + status("chat")));
return null; return null;
} }
@ -53,6 +54,10 @@ public class ToggleCMD extends PlexCommand
{ {
return toggle("redstone"); return toggle("redstone");
} }
case "pvp" ->
{
return toggle("pvp");
}
case "chat" -> case "chat" ->
{ {
PlexUtils.broadcast(PlexUtils.messageComponent("chatToggled", sender.getName(), plugin.toggles.getBoolean("chat") ? "off" : "on")); PlexUtils.broadcast(PlexUtils.messageComponent("chatToggled", sender.getName(), plugin.toggles.getBoolean("chat") ? "off" : "on"));

View File

@ -4,23 +4,26 @@ import dev.plex.Plex;
import dev.plex.listener.PlexListener; import dev.plex.listener.PlexListener;
import dev.plex.util.PlexUtils; import dev.plex.util.PlexUtils;
import io.papermc.paper.event.player.AsyncChatEvent; import io.papermc.paper.event.player.AsyncChatEvent;
import io.papermc.paper.event.player.PrePlayerAttackEntityEvent;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.entity.ThrownPotion;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.block.BlockExplodeEvent; import org.bukkit.event.block.BlockExplodeEvent;
import org.bukkit.event.block.BlockFromToEvent; import org.bukkit.event.block.BlockFromToEvent;
import org.bukkit.event.block.BlockRedstoneEvent; import org.bukkit.event.block.BlockRedstoneEvent;
import org.bukkit.event.entity.EntityExplodeEvent; import org.bukkit.event.entity.*;
import org.bukkit.event.entity.ExplosionPrimeEvent;
import org.bukkit.event.player.PlayerDropItemEvent; import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerCommandPreprocessEvent; import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.projectiles.ProjectileSource;
import java.util.List; import java.util.List;
public class TogglesListener extends PlexListener public class TogglesListener extends PlexListener
{ {
List<String> commands = plugin.commands.getStringList("block_on_mute"); List<String> commands = plugin.commands.getStringList("block_on_mute");
@EventHandler @EventHandler
public void onExplosionPrime(ExplosionPrimeEvent event) public void onExplosionPrime(ExplosionPrimeEvent event)
{ {
@ -32,7 +35,8 @@ public class TogglesListener extends PlexListener
} }
@EventHandler @EventHandler
public void onBlockExplode(BlockExplodeEvent event) { public void onBlockExplode(BlockExplodeEvent event)
{
if (!plugin.toggles.getBoolean("explosions")) if (!plugin.toggles.getBoolean("explosions"))
{ {
event.getBlock().breakNaturally(); event.getBlock().breakNaturally();
@ -97,10 +101,12 @@ public class TogglesListener extends PlexListener
for (String command : commands) for (String command : commands)
{ {
Command cmd = Bukkit.getCommandMap().getCommand(command); Command cmd = Bukkit.getCommandMap().getCommand(command);
if (cmd == null) { if (cmd == null)
{
return; return;
} }
if (cmd.getAliases().contains(message.toLowerCase())) { if (cmd.getAliases().contains(message.toLowerCase()))
{
event.getPlayer().sendMessage(PlexUtils.messageComponent("chatIsOff")); event.getPlayer().sendMessage(PlexUtils.messageComponent("chatIsOff"));
event.setCancelled(true); event.setCancelled(true);
return; return;
@ -110,6 +116,56 @@ public class TogglesListener extends PlexListener
} }
@EventHandler
public void onPlayerAttack(PrePlayerAttackEntityEvent event)
{
if (!plugin.toggles.getBoolean("pvp"))
{
if (event.getAttacked() instanceof Player)
{
event.setCancelled(true);
event.getPlayer().sendMessage(PlexUtils.messageComponent("pvpDisabled"));
}
}
}
@EventHandler
public void onPotionSplash(PotionSplashEvent event)
{
if (!plugin.toggles.getBoolean("pvp"))
{
ProjectileSource shooter = event.getEntity().getShooter();
if (shooter instanceof Player)
{
ThrownPotion potion = event.getPotion();
if (potion.getEffects().stream().anyMatch(effect -> effect.getType().getName().startsWith("HARM") ||
effect.getType().getName().startsWith("POISON")) &&
event.getAffectedEntities().stream().anyMatch(entity -> entity instanceof Player))
{
event.setCancelled(true);
((Player) shooter).sendMessage(PlexUtils.messageComponent("pvpDisabled"));
}
}
}
}
@EventHandler
public void onProjectileHit(ProjectileHitEvent event)
{
if (!plugin.toggles.getBoolean("pvp"))
{
ProjectileSource shooter = event.getEntity().getShooter();
if (shooter instanceof Player && event.getHitEntity() instanceof Player)
{
event.setCancelled(true);
((Player) shooter).sendMessage(PlexUtils.messageComponent("pvpDisabled"));
}
}
}
/* I have no idea if this is the best way to do this /* I have no idea if this is the best way to do this
There is a very weird bug where if you try to create a loop using two repeaters and a lever, after disabling There is a very weird bug where if you try to create a loop using two repeaters and a lever, after disabling
and re-enabling redstone, you are unable to recreate the loop with a lever. Using a redstone torch works fine. and re-enabling redstone, you are unable to recreate the loop with a lever. Using a redstone torch works fine.

View File

@ -7,6 +7,7 @@ import org.bukkit.Material;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryView; import org.bukkit.inventory.InventoryView;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
@ -24,6 +25,7 @@ public class ToggleMenu extends AbstractMenu
resetFluidspreadItem(this.inventory()); resetFluidspreadItem(this.inventory());
resetDropsItem(this.inventory()); resetDropsItem(this.inventory());
resetRedstoneItem(this.inventory()); resetRedstoneItem(this.inventory());
resetPVPItem(this.inventory());
resetChatItem(this.inventory()); resetChatItem(this.inventory());
} }
@ -67,6 +69,17 @@ public class ToggleMenu extends AbstractMenu
inventory.setItem(3, redstone); inventory.setItem(3, redstone);
} }
private void resetPVPItem(Inventory inventory)
{
ItemStack pvp = new ItemStack(Material.IRON_SWORD);
ItemMeta pvpItemMeta = pvp.getItemMeta();
pvpItemMeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
pvpItemMeta.displayName(PlexUtils.mmDeserialize("<!italic><light_purple>PVP"));
pvpItemMeta.lore(List.of(PlexUtils.mmDeserialize("<!italic><yellow>PVP is " + (plugin.toggles.getBoolean("pvp") ? "<green>enabled" : "<red>disabled"))));
pvp.setItemMeta(pvpItemMeta);
inventory.setItem(4, pvp);
}
private void resetChatItem(Inventory inventory) private void resetChatItem(Inventory inventory)
{ {
ItemStack chat = new ItemStack(Material.OAK_SIGN); ItemStack chat = new ItemStack(Material.OAK_SIGN);
@ -74,7 +87,7 @@ public class ToggleMenu extends AbstractMenu
chatItemMeta.displayName(PlexUtils.mmDeserialize("<!italic><light_purple>Toggle chat")); chatItemMeta.displayName(PlexUtils.mmDeserialize("<!italic><light_purple>Toggle chat"));
chatItemMeta.lore(List.of(PlexUtils.mmDeserialize("<!italic><yellow>Chat is currently " + (plugin.toggles.getBoolean("chat") ? "<green>on" : "<red>off")))); chatItemMeta.lore(List.of(PlexUtils.mmDeserialize("<!italic><yellow>Chat is currently " + (plugin.toggles.getBoolean("chat") ? "<green>on" : "<red>off"))));
chat.setItemMeta(chatItemMeta); chat.setItemMeta(chatItemMeta);
inventory.setItem(4, chat); inventory.setItem(5, chat);
} }
@Override @Override
@ -104,6 +117,12 @@ public class ToggleMenu extends AbstractMenu
resetRedstoneItem(inventory); resetRedstoneItem(inventory);
player.sendMessage(PlexUtils.mmDeserialize("<gray>Toggled redstone.")); player.sendMessage(PlexUtils.mmDeserialize("<gray>Toggled redstone."));
} }
if (clicked.getType() == Material.IRON_SWORD)
{
plugin.toggles.set("pvp", !plugin.toggles.getBoolean("pvp"));
resetPVPItem(inventory);
player.sendMessage(PlexUtils.mmDeserialize("<gray>Toggled PVP"));
}
if (clicked.getType() == Material.OAK_SIGN) if (clicked.getType() == Material.OAK_SIGN)
{ {
plugin.toggles.set("chat", !plugin.toggles.getBoolean("chat")); plugin.toggles.set("chat", !plugin.toggles.getBoolean("chat"));

View File

@ -119,6 +119,7 @@ playerFrozen: "<red>That player is already frozen!"
playerMuted: "<red>That player is already muted!" playerMuted: "<red>That player is already muted!"
playerLockedUp: "<red>That player is already locked up!" playerLockedUp: "<red>That player is already locked up!"
muted: "<red>You are currently muted - STFU!" muted: "<red>You are currently muted - STFU!"
pvpDisabled: "<red>PVP has been disabled!"
chatIsOff: "<red>Chat is currently toggled off!" chatIsOff: "<red>Chat is currently toggled off!"
# 0 - The command sender # 0 - The command sender
# 1 - The set value of the chat toggle # 1 - The set value of the chat toggle

View File

@ -14,3 +14,6 @@ redstone: true
# Is chat enabled? # Is chat enabled?
chat: true chat: true
# Is PVP enabled?
pvp: true