Skip to content

Commit

Permalink
Merge pull request #2 from huda0209/master
Browse files Browse the repository at this point in the history
[add]opがついた状態でswitchしても適切に権限が切り替わる機能を追加
  • Loading branch information
huda0209 authored Sep 9, 2021
2 parents 30a8199 + f2147e6 commit d503661
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 108 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import org.apache.tools.ant.filters.ReplaceTokens
apply plugin: 'java'

group = 'com.github.Mori01231'
version = '1.0.0'
version = '1.2.0'

sourceCompatibility = '1.8'
targetCompatibility = '1.8'
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/com/github/mori01231/aziswitch/AziSwitch.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,17 @@ public static AziSwitch getInstance() {
public void onEnable() {
// Plugin startup logic
getLogger().info("AziSwitch has been enabled.");
this.getCommand("switch").setExecutor(new SwitchCommandExecutor());
this.getCommand("newswitchgroup").setExecutor(new NewSwitchGroupCommandExecutor());

this.saveDefaultConfig();
configManager.loadConfig();

this.getCommand("switch").setExecutor(new SwitchCommandExecutor());
getServer().getPluginManager().registerEvents(new PlayerJoinEventListener(),this);
}



@Override
public void onDisable() {
// Plugin shutdown logic
this.saveConfig();
getLogger().info("AziSwitch has been disabled.");
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.github.mori01231.aziswitch;

import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;

import java.util.List;

public class PlayerJoinEventListener implements Listener {
private final String pluginName = AziSwitch.getInstance().getDescription().getPrefix();
private final String servername = configManager.getServerName();
private List<String> singleServerGroups;
private List<String> allServerGroups;
private List<String> OPs;

@EventHandler
public void PlayerJoinEventHandler(PlayerJoinEvent event){
OPs = configManager.getOPs();
Player player = event.getPlayer();
if(!OPs.contains(String.valueOf(player.getUniqueId())) ) return;

singleServerGroups = configManager.getSingleServerGroups();
allServerGroups = configManager.getAllServerGroups();

Boolean hasGroup = false;
for(String group : singleServerGroups) if(player.hasPermission("aziswitch.is" + group + " server=" + servername)) hasGroup = true;
for(String group : allServerGroups) if(player.hasPermission("aziswitch.is" + group)) hasGroup = true;
if(!hasGroup) return;

OPs.remove(String.valueOf(player.getUniqueId()));
configManager.setOPs(OPs);
player.setOp(true);
sendPlayerMessage(player,"&4OP&3を&a付与&3しました。");
}

public void sendPlayerMessage(Player player, String message){
player.sendMessage(ChatColor.translateAlternateColorCodes('&',"&7[&b"+pluginName+"&7]&r "+message ));
}
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
package com.github.mori01231.aziswitch;


import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;


import java.util.ArrayList;
import java.util.List;

import static org.bukkit.Bukkit.getServer;

public class SwitchCommandExecutor implements CommandExecutor {

private final String servername = AziSwitch.getInstance().getConfig().getString("lp-server-name");
private final List<String> singleServerGroups = AziSwitch.getInstance().getConfig().getStringList("SingleServerGroups");
private final List<String> allServerGroups = AziSwitch.getInstance().getConfig().getStringList("AllServerGroups");
private final String pluginName = AziSwitch.getInstance().getDescription().getPrefix();
private final String servername = configManager.getServerName();
private List<String> singleServerGroups;
private List<String> allServerGroups;
private List<String> OPs;

private Player player;

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {

singleServerGroups = configManager.getSingleServerGroups();
allServerGroups = configManager.getAllServerGroups();
OPs = configManager.getOPs();

// Check if the sender is a player
if (sender instanceof Player){
Expand All @@ -39,37 +41,37 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
allGroups.addAll(allServerGroups);
allGroups.addAll(singleServerGroups);
// Initialize boolean to check if a player has a switchable group
Boolean hasGroup = false;
Boolean hasSwitchGroup = false;
Boolean hasIsGroup = false;

// Build a list with all groups
for (String group : allGroups) {
if (sender.hasPermission("aziswitch.switch" + group)){
hasGroup = true;
hasSwitchGroup = true;
}
if (sender.hasPermission("aziswitch.is" + group)){
hasGroup = true;
hasIsGroup = true;
}
}

// Check if player has a switchable group
if(!hasGroup){
if(!hasSwitchGroup&&!hasIsGroup){
sender.sendMessage(ChatColor.translateAlternateColorCodes
('&',"&4権限不足です。\n&3Mori01231#9559のDMにこのメッセージのスクショをもって泣きつきましょう!" ));
return true; // Don't have the permissions? Sucks for you.
}

// Call all functions to switch groups
for (String group : singleServerGroups) {
SwitchFromMemberInServer(player, group);
}
for (String group : allServerGroups) {
SwitchFromMember(player, group);
}
for (String group : singleServerGroups) {
SwitchToMemberInServer(player, group);
if(hasSwitchGroup) {
// Call all functions to switch groups
for (String group : singleServerGroups) SwitchFromMemberInServer(player, group);
for (String group : allServerGroups) SwitchFromMember(player, group);
addOP(player);
}
for (String group : allServerGroups) {
SwitchToMember(player, group);
if(hasIsGroup){
removeOP(player);
// Call all functions to switch groups
for (String group : singleServerGroups) SwitchToMemberInServer(player, group);
for (String group : allServerGroups) SwitchToMember(player, group);
}

return true; // Correctly switched groups.
Expand All @@ -79,41 +81,58 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
public void SwitchFromMember(Player player, String group){
if (player.hasPermission("aziswitch.switch" + group) && !player.hasPermission("aziswitch.is" + group)){
sendCommand("lp u " + player.getName() + " parent add " + group);
sendPlayerMessage(player, "&3" + group + "モードになりました。");
sendPlayerMessage(player, "&c" + group + "モード&3になりました。");
sendCommand("lp u " + player.getName() + " parent remove switch" + group);
}
}

public void SwitchToMember(Player player, String group){
if (player.hasPermission("aziswitch.is" + group)){
sendCommand("lp u " + player.getName() + " parent add switch" + group);
sendPlayerMessage(player, "&3Memberモードになりました。");
sendPlayerMessage(player, "&7Memberモード&3になりました。");
sendCommand("lp u " + player.getName() + " parent remove " + group);
}
}

public void SwitchFromMemberInServer(Player player, String group){
if (player.hasPermission("aziswitch.switch" + group) && !player.hasPermission("aziswitch.is" + group + " server=" + servername)){
sendCommand("lp u " + player.getName() + " parent add " + group + " server=" + servername);
sendPlayerMessage(player, "&3" + group + "モードになりました。");
sendPlayerMessage(player, "&c" + group + "モード&3になりました。");
sendCommand("lp u " + player.getName() + " parent remove switch" + group + " server=" + servername);
}
}

public void SwitchToMemberInServer(Player player, String group){
if (player.hasPermission("aziswitch.is" + group)){
sendCommand("lp u " + player.getName() + " parent add switch" + group + " server=" + servername);
sendPlayerMessage(player, "&3Memberモードになりました。");
sendPlayerMessage(player, "&7Memberモード&3になりました。");
sendCommand("lp u " + player.getName() + " parent remove " + group + " server=" + servername);
}
}

private void addOP(Player player){
if(!OPs.contains(String.valueOf(player.getUniqueId())) ) return;
OPs.remove(String.valueOf(player.getUniqueId()));
configManager.setOPs(OPs);
player.setOp(true);
sendPlayerMessage(player,"&4OP&3を&a付与&3しました。");
}

private void removeOP(Player player){
if (!player.isOp()) return;
if(!OPs.contains(String.valueOf(player.getUniqueId())) ) {
OPs.add(String.valueOf(player.getUniqueId()));
configManager.setOPs(OPs);
}
player.setOp(false);
sendPlayerMessage(player,"&4OP&3を&c削除&3しました。");
}

public void sendCommand(String command){
getServer().dispatchCommand(getServer().getConsoleSender(), command);
}

public void sendPlayerMessage(Player player, String message){
player.sendMessage(ChatColor.translateAlternateColorCodes('&',message ));
player.sendMessage(ChatColor.translateAlternateColorCodes('&',"&7[&b"+pluginName+"&7]&r "+message ));
}
}
54 changes: 54 additions & 0 deletions src/main/java/com/github/mori01231/aziswitch/configManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.github.mori01231.aziswitch;

import org.bukkit.configuration.Configuration;


import java.util.List;

public class configManager {
private static AziSwitch plugin = AziSwitch.getInstance();
private static Configuration config = plugin.getConfig();
private static String servername;
private static List<String> singleServerGroups;
private static List<String> allServerGroups;
private static List<String> OPs;


public static void loadConfig(){
servername = config.getString("lp-server-name");
singleServerGroups = config.getStringList("SingleServerGroups");
allServerGroups = config.getStringList("AllServerGroups");
OPs = config.getStringList("OPs");
}

public static String getServerName(){
return servername;
}

public static List<String> getSingleServerGroups(){
return singleServerGroups;
}

public static List<String> getAllServerGroups(){
return allServerGroups;
}

public static List<String> getOPs(){
return OPs;
}

public static void setSingleServerGroups(List<String> Lists){
singleServerGroups = Lists;
config.set("SingleServerGroups", singleServerGroups);
}

public static void setAllServerGroups(List<String> Lists){
allServerGroups = Lists;
config.set("AllServerGroups", allServerGroups);
}

public static void setOPs(List<String> Lists){
OPs = Lists;
config.set("OPs", OPs);
}
}
3 changes: 3 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ AllServerGroups:
- owner
- developer
- alladmin

# op状態でswitchを実行した人を保存(触るな)
OPs:
3 changes: 0 additions & 3 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ commands:
switch:
description: 運営モードとサバ民モードを行き来する。
usage: エラー。/switchで運営モードを変更できます。
newswitchgroup:
description: Switch可能なグループを作成する。
usage: エラー。/newswitchgroup グループ名 でグループの作成が可能です。

permissions:
aziswitch.*:
Expand Down

0 comments on commit d503661

Please sign in to comment.