ban/unban process
This commit is contained in:
@@ -19,6 +19,8 @@ public class BanHammerPlugin extends JavaPlugin {
|
|||||||
|
|
||||||
ApplicationContext context = createSpringContext();
|
ApplicationContext context = createSpringContext();
|
||||||
|
|
||||||
|
this.getServer().getPluginManager().registerEvents(context.getBean(PlayerListener.class), this);
|
||||||
|
|
||||||
this.getCommand("ban").setExecutor(context.getBean(BanCommand.class));
|
this.getCommand("ban").setExecutor(context.getBean(BanCommand.class));
|
||||||
this.getCommand("unban").setExecutor(context.getBean(UnbanCommand.class));
|
this.getCommand("unban").setExecutor(context.getBean(UnbanCommand.class));
|
||||||
}
|
}
|
||||||
|
|||||||
27
src/main/java/ru/dmitriymx/plugin/PlayerListener.java
Normal file
27
src/main/java/ru/dmitriymx/plugin/PlayerListener.java
Normal 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!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,35 +4,32 @@ import org.bukkit.command.Command;
|
|||||||
import org.bukkit.command.CommandExecutor;
|
import org.bukkit.command.CommandExecutor;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Qualifier;
|
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import ru.dmitriymx.plugin.service.BannedUserService;
|
import ru.dmitriymx.plugin.service.BannedUserService;
|
||||||
|
|
||||||
import java.util.logging.Logger;
|
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class BanCommand implements CommandExecutor {
|
public class BanCommand implements CommandExecutor {
|
||||||
|
|
||||||
private final Logger logger;
|
|
||||||
private final BannedUserService service;
|
private final BannedUserService service;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
|
public BanCommand(BannedUserService service) {
|
||||||
public BanCommand(@Qualifier("bukkitLogger") Logger logger, BannedUserService service) {
|
|
||||||
this.logger = logger;
|
|
||||||
this.service = service;
|
this.service = service;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||||
logger.info("Call '" + label + "' command");
|
|
||||||
|
|
||||||
if (args.length == 0) {
|
if (args.length == 0) {
|
||||||
return true;
|
return false;
|
||||||
}
|
}
|
||||||
|
String playerName = args[0];
|
||||||
|
|
||||||
boolean result = service.inBanned(args[0]);
|
if (service.isBanned(playerName)) {
|
||||||
logger.info("Player '" + args[0] + "': " + (result ? "is banned" : "not banned"));
|
sender.sendMessage("Player '" + playerName + "' already banned");
|
||||||
|
} else {
|
||||||
|
service.ban(playerName);
|
||||||
|
sender.sendMessage("Player '" + playerName + "' now banned");
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,25 +4,33 @@ import org.bukkit.command.Command;
|
|||||||
import org.bukkit.command.CommandExecutor;
|
import org.bukkit.command.CommandExecutor;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Qualifier;
|
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
import ru.dmitriymx.plugin.service.BannedUserService;
|
||||||
import java.util.logging.Logger;
|
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class UnbanCommand implements CommandExecutor {
|
public class UnbanCommand implements CommandExecutor {
|
||||||
|
|
||||||
private final Logger logger;
|
private final BannedUserService service;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
|
public UnbanCommand(BannedUserService service) {
|
||||||
public UnbanCommand(@Qualifier("bukkitLogger") Logger logger) {
|
this.service = service;
|
||||||
this.logger = logger;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,14 +7,18 @@ import org.springframework.context.annotation.ComponentScan;
|
|||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||||
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
||||||
|
import org.springframework.orm.jpa.JpaTransactionManager;
|
||||||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||||
|
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||||
|
|
||||||
|
import javax.persistence.EntityManagerFactory;
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@ComponentScan(basePackages = "ru.dmitriymx.plugin")
|
@ComponentScan(basePackages = "ru.dmitriymx.plugin")
|
||||||
@EnableJpaRepositories(basePackages = "ru.dmitriymx.plugin.repository")
|
@EnableJpaRepositories(basePackages = "ru.dmitriymx.plugin.repository")
|
||||||
|
@EnableTransactionManagement
|
||||||
public class SpringConfig {
|
public class SpringConfig {
|
||||||
|
|
||||||
@Value("${database.url}")
|
@Value("${database.url}")
|
||||||
@@ -50,6 +54,14 @@ public class SpringConfig {
|
|||||||
return entityManagerFactoryBean;
|
return entityManagerFactoryBean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
|
||||||
|
JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
|
||||||
|
jpaTransactionManager.setEntityManagerFactory(entityManagerFactory);
|
||||||
|
|
||||||
|
return jpaTransactionManager;
|
||||||
|
}
|
||||||
|
|
||||||
private Properties hibernateJpaProperties() {
|
private Properties hibernateJpaProperties() {
|
||||||
Properties properties = new Properties();
|
Properties properties = new Properties();
|
||||||
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
|
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
|
||||||
|
|||||||
@@ -1,16 +1,15 @@
|
|||||||
package ru.dmitriymx.plugin.entity;
|
package ru.dmitriymx.plugin.entity;
|
||||||
|
|
||||||
import javax.persistence.Column;
|
import javax.persistence.*;
|
||||||
import javax.persistence.Entity;
|
|
||||||
import javax.persistence.Id;
|
|
||||||
import javax.persistence.Table;
|
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Table(name = "ban_users")
|
@Table(name = "ban_users")
|
||||||
|
@SuppressWarnings("unused")
|
||||||
public class BannedUserEntity {
|
public class BannedUserEntity {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
|
@GeneratedValue(strategy=GenerationType.IDENTITY)
|
||||||
private Integer id;
|
private Integer id;
|
||||||
|
|
||||||
@Column(
|
@Column(
|
||||||
|
|||||||
@@ -1,13 +1,20 @@
|
|||||||
package ru.dmitriymx.plugin.repository;
|
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 org.springframework.stereotype.Repository;
|
||||||
import ru.dmitriymx.plugin.entity.BannedUserEntity;
|
import ru.dmitriymx.plugin.entity.BannedUserEntity;
|
||||||
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public interface BannedUserRepository extends CrudRepository<BannedUserEntity, Integer> {
|
public interface BannedUserRepository extends JpaRepository<BannedUserEntity, Integer> {
|
||||||
|
|
||||||
Optional<BannedUserEntity> findByPlayerName(String playerName);
|
Optional<BannedUserEntity> findByPlayerName(String playerName);
|
||||||
|
|
||||||
|
@Modifying
|
||||||
|
@Query("delete from #{#entityName} b where b.playerName = :playerName")
|
||||||
|
void deleteByPlayerName(@Param("playerName") String playerName);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,9 +3,12 @@ package ru.dmitriymx.plugin.service;
|
|||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
import ru.dmitriymx.plugin.entity.BannedUserEntity;
|
||||||
import ru.dmitriymx.plugin.repository.BannedUserRepository;
|
import ru.dmitriymx.plugin.repository.BannedUserRepository;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
|
@Transactional
|
||||||
public class BannedUserService {
|
public class BannedUserService {
|
||||||
|
|
||||||
private final BannedUserRepository repository;
|
private final BannedUserRepository repository;
|
||||||
@@ -15,11 +18,21 @@ public class BannedUserService {
|
|||||||
this.repository = repository;
|
this.repository = repository;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean inBanned(Player player) {
|
public boolean isBanned(Player player) {
|
||||||
return inBanned(player.getName());
|
return isBanned(player.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean inBanned(String playerName) {
|
public boolean isBanned(String playerName) {
|
||||||
return repository.findByPlayerName(playerName.toLowerCase()).isPresent();
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ author: @BUKKIT_PLUGIN_AUTHOR@
|
|||||||
commands:
|
commands:
|
||||||
ban:
|
ban:
|
||||||
description: Ban player
|
description: Ban player
|
||||||
usage: /<command>
|
usage: /<command> PLAYER_NAME
|
||||||
unban:
|
unban:
|
||||||
description: Unban player
|
description: Unban player
|
||||||
usage: /<command>
|
usage: /<command> PLAYER_NAME
|
||||||
|
|||||||
Reference in New Issue
Block a user