0

ban/unban process

This commit is contained in:
2021-05-18 20:29:54 +03:00
parent 4a37364497
commit edd21ef315
9 changed files with 96 additions and 31 deletions

View File

@@ -19,6 +19,8 @@ public class BanHammerPlugin extends JavaPlugin {
ApplicationContext context = createSpringContext();
this.getServer().getPluginManager().registerEvents(context.getBean(PlayerListener.class), this);
this.getCommand("ban").setExecutor(context.getBean(BanCommand.class));
this.getCommand("unban").setExecutor(context.getBean(UnbanCommand.class));
}

View File

@@ -0,0 +1,27 @@
package ru.dmitriymx.plugin;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerLoginEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import ru.dmitriymx.plugin.service.BannedUserService;
@Component
public class PlayerListener implements Listener {
private final BannedUserService service;
@Autowired
public PlayerListener(BannedUserService service) {
this.service = service;
}
@EventHandler
@SuppressWarnings("unused")
public void onLogin(PlayerLoginEvent event) {
if (service.isBanned(event.getPlayer())) {
event.disallow(PlayerLoginEvent.Result.KICK_BANNED, "You banned!");
}
}
}

View File

@@ -4,35 +4,32 @@ import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import ru.dmitriymx.plugin.service.BannedUserService;
import java.util.logging.Logger;
@Component
public class BanCommand implements CommandExecutor {
private final Logger logger;
private final BannedUserService service;
@Autowired
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
public BanCommand(@Qualifier("bukkitLogger") Logger logger, BannedUserService service) {
this.logger = logger;
public BanCommand(BannedUserService service) {
this.service = service;
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
logger.info("Call '" + label + "' command");
if (args.length == 0) {
return true;
return false;
}
String playerName = args[0];
boolean result = service.inBanned(args[0]);
logger.info("Player '" + args[0] + "': " + (result ? "is banned" : "not banned"));
if (service.isBanned(playerName)) {
sender.sendMessage("Player '" + playerName + "' already banned");
} else {
service.ban(playerName);
sender.sendMessage("Player '" + playerName + "' now banned");
}
return true;
}

View File

@@ -4,25 +4,33 @@ import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import java.util.logging.Logger;
import ru.dmitriymx.plugin.service.BannedUserService;
@Component
public class UnbanCommand implements CommandExecutor {
private final Logger logger;
private final BannedUserService service;
@Autowired
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
public UnbanCommand(@Qualifier("bukkitLogger") Logger logger) {
this.logger = logger;
public UnbanCommand(BannedUserService service) {
this.service = service;
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
logger.info("Call '" + label + "' command");
if (args.length == 0) {
return false;
}
String playerName = args[0];
if (service.isBanned(playerName)) {
service.unban(playerName);
sender.sendMessage("Player '" + playerName + "' now not banned");
} else {
sender.sendMessage("Player '" + playerName + "' not banned");
}
return true;
}
}

View File

@@ -7,14 +7,18 @@ import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import java.util.Properties;
@Configuration
@ComponentScan(basePackages = "ru.dmitriymx.plugin")
@EnableJpaRepositories(basePackages = "ru.dmitriymx.plugin.repository")
@EnableTransactionManagement
public class SpringConfig {
@Value("${database.url}")
@@ -50,6 +54,14 @@ public class SpringConfig {
return entityManagerFactoryBean;
}
@Bean
public JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
jpaTransactionManager.setEntityManagerFactory(entityManagerFactory);
return jpaTransactionManager;
}
private Properties hibernateJpaProperties() {
Properties properties = new Properties();
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");

View File

@@ -1,16 +1,15 @@
package ru.dmitriymx.plugin.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.*;
import java.util.Objects;
@Entity
@Table(name = "ban_users")
@SuppressWarnings("unused")
public class BannedUserEntity {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
@Column(

View File

@@ -1,13 +1,20 @@
package ru.dmitriymx.plugin.repository;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import ru.dmitriymx.plugin.entity.BannedUserEntity;
import java.util.Optional;
@Repository
public interface BannedUserRepository extends CrudRepository<BannedUserEntity, Integer> {
public interface BannedUserRepository extends JpaRepository<BannedUserEntity, Integer> {
Optional<BannedUserEntity> findByPlayerName(String playerName);
@Modifying
@Query("delete from #{#entityName} b where b.playerName = :playerName")
void deleteByPlayerName(@Param("playerName") String playerName);
}

View File

@@ -3,9 +3,12 @@ package ru.dmitriymx.plugin.service;
import org.bukkit.entity.Player;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ru.dmitriymx.plugin.entity.BannedUserEntity;
import ru.dmitriymx.plugin.repository.BannedUserRepository;
@Service
@Transactional
public class BannedUserService {
private final BannedUserRepository repository;
@@ -15,11 +18,21 @@ public class BannedUserService {
this.repository = repository;
}
public boolean inBanned(Player player) {
return inBanned(player.getName());
public boolean isBanned(Player player) {
return isBanned(player.getName());
}
public boolean inBanned(String playerName) {
public boolean isBanned(String playerName) {
return repository.findByPlayerName(playerName.toLowerCase()).isPresent();
}
public void ban(String playerName) {
BannedUserEntity entity = new BannedUserEntity();
entity.setPlayerName(playerName);
repository.save(entity);
}
public void unban(String playerName) {
repository.deleteByPlayerName(playerName);
}
}

View File

@@ -6,7 +6,7 @@ author: @BUKKIT_PLUGIN_AUTHOR@
commands:
ban:
description: Ban player
usage: /<command>
usage: /<command> PLAYER_NAME
unban:
description: Unban player
usage: /<command>
usage: /<command> PLAYER_NAME