diff --git a/src/main/java/ru/dmitriymx/plugin/BanHammerPlugin.java b/src/main/java/ru/dmitriymx/plugin/BanHammerPlugin.java index a26a339..f778063 100644 --- a/src/main/java/ru/dmitriymx/plugin/BanHammerPlugin.java +++ b/src/main/java/ru/dmitriymx/plugin/BanHammerPlugin.java @@ -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)); } diff --git a/src/main/java/ru/dmitriymx/plugin/PlayerListener.java b/src/main/java/ru/dmitriymx/plugin/PlayerListener.java new file mode 100644 index 0000000..04853c3 --- /dev/null +++ b/src/main/java/ru/dmitriymx/plugin/PlayerListener.java @@ -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!"); + } + } +} diff --git a/src/main/java/ru/dmitriymx/plugin/command/BanCommand.java b/src/main/java/ru/dmitriymx/plugin/command/BanCommand.java index b281a77..36d315a 100644 --- a/src/main/java/ru/dmitriymx/plugin/command/BanCommand.java +++ b/src/main/java/ru/dmitriymx/plugin/command/BanCommand.java @@ -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; } diff --git a/src/main/java/ru/dmitriymx/plugin/command/UnbanCommand.java b/src/main/java/ru/dmitriymx/plugin/command/UnbanCommand.java index e9c7bcd..2a169b1 100644 --- a/src/main/java/ru/dmitriymx/plugin/command/UnbanCommand.java +++ b/src/main/java/ru/dmitriymx/plugin/command/UnbanCommand.java @@ -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; } } diff --git a/src/main/java/ru/dmitriymx/plugin/config/SpringConfig.java b/src/main/java/ru/dmitriymx/plugin/config/SpringConfig.java index ca55a03..f0f0127 100644 --- a/src/main/java/ru/dmitriymx/plugin/config/SpringConfig.java +++ b/src/main/java/ru/dmitriymx/plugin/config/SpringConfig.java @@ -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"); diff --git a/src/main/java/ru/dmitriymx/plugin/entity/BannedUserEntity.java b/src/main/java/ru/dmitriymx/plugin/entity/BannedUserEntity.java index 3b59162..72efc9c 100644 --- a/src/main/java/ru/dmitriymx/plugin/entity/BannedUserEntity.java +++ b/src/main/java/ru/dmitriymx/plugin/entity/BannedUserEntity.java @@ -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( diff --git a/src/main/java/ru/dmitriymx/plugin/repository/BannedUserRepository.java b/src/main/java/ru/dmitriymx/plugin/repository/BannedUserRepository.java index a3c6880..6e69d6f 100644 --- a/src/main/java/ru/dmitriymx/plugin/repository/BannedUserRepository.java +++ b/src/main/java/ru/dmitriymx/plugin/repository/BannedUserRepository.java @@ -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 { +public interface BannedUserRepository extends JpaRepository { Optional findByPlayerName(String playerName); + + @Modifying + @Query("delete from #{#entityName} b where b.playerName = :playerName") + void deleteByPlayerName(@Param("playerName") String playerName); } diff --git a/src/main/java/ru/dmitriymx/plugin/service/BannedUserService.java b/src/main/java/ru/dmitriymx/plugin/service/BannedUserService.java index 5d0fe5c..cf357f5 100644 --- a/src/main/java/ru/dmitriymx/plugin/service/BannedUserService.java +++ b/src/main/java/ru/dmitriymx/plugin/service/BannedUserService.java @@ -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); + } } diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 581c141..69c517e 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -6,7 +6,7 @@ author: @BUKKIT_PLUGIN_AUTHOR@ commands: ban: description: Ban player - usage: / + usage: / PLAYER_NAME unban: description: Unban player - usage: / + usage: / PLAYER_NAME