generated from ArtformGames/TemplateSinglePlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
162 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 0 additions & 6 deletions
6
src/main/java/com/artformgames/plugin/template/conf/PluginMessages.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/main/java/com/artformgames/plugin/togglepvp/command/TogglePVPCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.artformgames.plugin.togglepvp.command; | ||
|
||
import com.artformgames.plugin.togglepvp.handler.PVPHandler; | ||
import com.artformgames.plugin.togglepvp.conf.PluginMessages; | ||
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; | ||
|
||
public class TogglePVPCommand implements CommandExecutor { | ||
|
||
@Override | ||
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, | ||
@NotNull String label, @NotNull String[] args) { | ||
if (!(sender instanceof Player player)) { | ||
sender.sendMessage("You must be a player to use this command."); | ||
return true; | ||
} | ||
|
||
boolean current = PVPHandler.isPVPEnabled(player); | ||
PVPHandler.setPVPStatus(player, !current); | ||
if (!current) { | ||
PluginMessages.ENABLED.send(player); | ||
} else { | ||
PluginMessages.DISABLED.send(player); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/main/java/com/artformgames/plugin/togglepvp/conf/PluginMessages.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.artformgames.plugin.togglepvp.conf; | ||
|
||
import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredMessageList; | ||
import com.artformgames.core.conf.MessagesRoot; | ||
import net.md_5.bungee.api.chat.BaseComponent; | ||
|
||
public class PluginMessages extends MessagesRoot { | ||
|
||
public static final ConfiguredMessageList<BaseComponent[]> SELF_DISABLED = list() | ||
.defaults("&7 You have disabled PVP so you can't attack other players.") | ||
.build(); | ||
|
||
public static final ConfiguredMessageList<BaseComponent[]> TARGET_DISABLED = list() | ||
.defaults("&fPlayer &c%(player) &fPVP is disabled, so it can't be attacked.") | ||
.params("player") | ||
.build(); | ||
|
||
public static final ConfiguredMessageList<BaseComponent[]> ENABLED = list() | ||
.defaults("&fYou have &a&lenabled &fPVP, now you can fight with other players.") | ||
.build(); | ||
|
||
public static final ConfiguredMessageList<BaseComponent[]> DISABLED = list() | ||
.defaults("&fYou have &c&ldisabled &fPVP, now you can't attack other players, and you can't be attacked by other players.") | ||
.build(); | ||
|
||
} | ||
|
53 changes: 53 additions & 0 deletions
53
src/main/java/com/artformgames/plugin/togglepvp/handler/PVPHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.artformgames.plugin.togglepvp.handler; | ||
|
||
import com.artformgames.core.ArtCore; | ||
import com.artformgames.core.function.settings.SettingsType; | ||
import com.artformgames.core.function.settings.UserSettingsData; | ||
import com.artformgames.core.function.settings.values.BooleanSettingsType; | ||
import com.artformgames.plugin.togglepvp.conf.PluginConfig; | ||
import com.artformgames.plugin.togglepvp.conf.PluginMessages; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.entity.Projectile; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.Listener; | ||
import org.bukkit.event.entity.EntityDamageByEntityEvent; | ||
|
||
public class PVPHandler implements Listener { | ||
|
||
public static final SettingsType<Boolean> PVP = new BooleanSettingsType(10, PluginConfig.DEFAULTS.getNotNull()); | ||
|
||
|
||
@EventHandler | ||
public void onAttack(EntityDamageByEntityEvent e) { | ||
Player attacker = null; | ||
|
||
if (e.getDamager() instanceof Player player) { | ||
attacker = player; | ||
} else if (e.getDamager() instanceof Projectile proj && proj.getShooter() instanceof Player shooter) { | ||
attacker = shooter; | ||
} | ||
|
||
if (attacker == null) return; | ||
if (!(e.getEntity() instanceof Player player)) return; | ||
|
||
if (!isPVPEnabled(attacker)) { | ||
// 当玩家想要攻击别人时,先判断自己是否允许攻击 | ||
PluginMessages.SELF_DISABLED.send(attacker); | ||
e.setCancelled(true); | ||
} else if (!isPVPEnabled(player)) { | ||
//再判断被攻击者是否允许被攻击 | ||
PluginMessages.TARGET_DISABLED.send(attacker, player.getName()); | ||
e.setCancelled(true); | ||
} | ||
} | ||
|
||
public static boolean isPVPEnabled(Player player) { | ||
return ArtCore.getUserManager().get(player).getHandler(UserSettingsData.class).get(PVP); | ||
} | ||
|
||
public static void setPVPStatus(Player player, boolean status) { | ||
ArtCore.getUserManager().get(player).getHandler(UserSettingsData.class).set(PVP, status); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters