Skip to content

Commit

Permalink
1.2.3
Browse files Browse the repository at this point in the history
Bugfixes and hijack warning
  • Loading branch information
MarioFinale committed Aug 13, 2021
1 parent bef6787 commit daf8e8e
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 106 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ It preserves trades, homes, job location and updates reputation.

## Changelog

### 1.2.3
- Fixed logging (now using proper Bukkit logger, fixes #15).
- Fixed nag message about using stdout.
- Minor code changes to improve readability
- Change plugin messages to better communicate plugin status.
- Add a severe waring message about the plugin being hijacked and advising user to always download the plugin from a trusted source (GitHub). Fixes #16

### 1.2.2
- Updated to 1.17.1

Expand Down
6 changes: 3 additions & 3 deletions out/production/VillagerSaver/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
name: VillagerSaver
version: 1.2.1
version: 1.2.3
authors: [MarioFinale, Kasama]
main: cl.mariofinale.villagerSaver
api-version: 1.16
main: cl.mariofinale.VillagerSaver
api-version: 1.17
website: https://github.com/MarioFinale/VillagerSaver
commands:
villagersaver:
Expand Down
95 changes: 95 additions & 0 deletions src/cl/mariofinale/VillagerSaver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package cl.mariofinale;

import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;
import java.util.ArrayList;
import java.util.logging.Level;

/** @noinspection unused*/
public class VillagerSaver extends JavaPlugin {
static ArrayList<String> WorldBlackList = new ArrayList<>();
private static Plugin plugin;

@Override
public void onEnable() {
plugin = this;
LogError("WARNING, WARNING. PLEASE READ ME:");
LogError("IF YOU DOWNLOADED VILLAGERSAVER BETWEEN JULY 28 AND AUGUST 11 2021 YOU NEED TO DELETE IT IMMEDIATELY.");
LogWarn("PLEASE MAKE SURE TO ALWAYS DOWNLOAD THE PLUGIN FROM THE GITHUB RELEASES PAGE: https://github.com/MarioFinale/VillagerSaver/releases");
LogWarn("DO NOT DOWNLOAD THIS PLUGIN FROM ANY OTHER UNTRUSTED SOURCE!");
LogInfo("Registering listener.");
getServer().getPluginManager().registerEvents(new VillagerSaver_Listener(), this);
LogInfo("Listener registered.");
LogInfo("Loading World Blacklist.");
LoadWorldBlackList();
LogInfo("Registering commands.");
VillagerSaver_Commands villagerSaver_commands = new VillagerSaver_Commands();
this.getCommand("villagersaver").setExecutor(villagerSaver_commands);
LogInfo("Commands registered.");
LogInfo("VillagerSaver loaded!");
}

@Override
public void onDisable() {
LogInfo("Saving config.");
plugin.saveDefaultConfig();
SaveWorldBlackList();
LogInfo("VillagerSaver disabled!");
}

private void LogInfo(String line) {
plugin.getLogger().log(Level.INFO,line);
}
private void LogWarn(String line) {
plugin.getLogger().log(Level.WARNING,line);
}
private void LogError(String line) {
plugin.getLogger().log(Level.SEVERE,line);
}

private void SaveWorldBlackList() {
LogInfo("Saving blacklist.");
File WorldBlackListFile = new File(getDataFolder().getAbsolutePath(), "WorldBlackList.yml");
try {
WorldBlackListFile.delete();
WorldBlackListFile.createNewFile();
if (WorldBlackList == null) {
WorldBlackList = new ArrayList<>();
}
} catch (Exception ex) {
LogError("Error saving blacklist: " + ex.getMessage());
return;
}
FileConfiguration playerPointsConfig = YamlConfiguration.loadConfiguration(WorldBlackListFile);
playerPointsConfig.set("BlackList", WorldBlackList);

try {
playerPointsConfig.save(WorldBlackListFile);
} catch (Exception ex) {
LogError("Error saving blacklist: " + ex.getMessage());
return;
}
LogInfo("Blacklist saved.");
}

private void LoadWorldBlackList() {
File WorldBlackListFile = new File(getDataFolder().getAbsolutePath(), "WorldBlackList.yml");
try {
WorldBlackListFile.createNewFile();
FileConfiguration blacklistConfigFile = YamlConfiguration.loadConfiguration(WorldBlackListFile);
WorldBlackList = (ArrayList<String>) blacklistConfigFile.get("BlackList");
} catch (Exception ex) {
LogWarn("World blacklist file has not been loaded: " + ex.getMessage());
LogWarn("If this is the first time running the plugin the file will be created on server stop.");
return;
}
if (WorldBlackList == null) {
WorldBlackList = new ArrayList<>();
}
LogInfo("World Blacklist loaded.");
}
}
32 changes: 17 additions & 15 deletions src/cl/mariofinale/VillagerSaver_Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.bukkit.entity.Player;
import java.util.ArrayList;

/** @noinspection unused*/
public class VillagerSaver_Commands implements CommandExecutor {

@Override
Expand All @@ -15,7 +16,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
return true;
}
Player player = (Player) sender;
if (player == null) return true;
if (!player.isValid()) return true;
if ((args == null || args.length <= 1)) {
return false;
}
Expand All @@ -29,47 +30,48 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
}
}

public static boolean AddWorldToBlackList(Player player, String worldName) {
/** @noinspection SameReturnValue*/
private static boolean AddWorldToBlackList(Player player, String worldName) {
ArrayList<String> worldList = new ArrayList<>();
for (World world : Bukkit.getWorlds()){
worldList.add(world.getName());
}
if (worldList.contains(worldName)){
if (!villagerSaver.WorldBlackList.contains(worldName)){
villagerSaver.WorldBlackList.add(worldName);
if (!VillagerSaver.WorldBlackList.contains(worldName)){
VillagerSaver.WorldBlackList.add(worldName);
SendMessageToPlayer(player, "The World '" + worldName + "' was added to the blacklist.");
}else{
SendMessageToPlayer(player, "The World '" + worldName + "' already is on the blacklist.");
}
}else {
SendMessageToPlayer(player, "The World '" + worldName + "' does not exist.");
String availableWorlds = "";
StringBuilder availableWorlds = new StringBuilder();
for (String wN : worldList){
availableWorlds = availableWorlds + wN + " - ";
availableWorlds.append(wN).append(" - ");
}
availableWorlds = availableWorlds.substring(0,availableWorlds.length() - 3).trim();
availableWorlds = new StringBuilder(availableWorlds.substring(0, availableWorlds.length() - 3).trim());
SendMessageToPlayer(player, "Available Worlds: " + availableWorlds + "");
}
return true;
}

public static boolean RemoveWorldFromTheBlackList(Player player, String worldName) {
if (villagerSaver.WorldBlackList.contains(worldName)){
villagerSaver.WorldBlackList.remove(worldName);
/** @noinspection SameReturnValue*/
private static boolean RemoveWorldFromTheBlackList(Player player, String worldName) {
if (VillagerSaver.WorldBlackList.contains(worldName)){
VillagerSaver.WorldBlackList.remove(worldName);
SendMessageToPlayer(player, "The World '" + worldName + "' was removed from the blacklist.");
}else {
SendMessageToPlayer(player, "The World '" + worldName + "' is not on the blacklist.");
}
return true;
}

public static boolean SendMessageToPlayer(Player player, String message) {
if (!player.isOnline()) return false;
if (player.isBanned()) return false;
if ((player == null)) return false;
private static void SendMessageToPlayer(Player player, String message) {
if (!player.isValid()) return;
if (player.isBanned()) return;
if (!player.isOnline()) return;
String resultingMessage = VillagerSaver_PluginVars.PluginPrefix + " " + message;
player.sendMessage(resultingMessage);
return true;
}


Expand Down
8 changes: 4 additions & 4 deletions src/cl/mariofinale/VillagerSaver_Listener.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import net.minecraft.world.item.trading.MerchantRecipeList;
import org.bukkit.craftbukkit.v1_17_R1.entity.*;
import org.bukkit.entity.*;
import org.bukkit.entity.Entity;
import org.bukkit.event.entity.*;
import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason;
import org.bukkit.event.entity.EntityTransformEvent.TransformReason;
Expand All @@ -16,22 +15,23 @@

public class VillagerSaver_Listener implements Listener{

/** @noinspection unused*/
@EventHandler
public void onEntityDamageByEntity(EntityDamageByEntityEvent event){
Entity damagedEntity = event.getEntity();
if(!(damagedEntity instanceof LivingEntity)) return; //Check if the damaged entity is a living entity
LivingEntity damagedVillager = (LivingEntity) damagedEntity;
Entity entityDamager = event.getDamager();
if (entityDamager == null) return; //Check if the damage was issued by an entity
if (!entityDamager.isValid()) return; //Check if the damage was issued by a valid entity
if (!(damagedVillager.getHealth() - event.getDamage() <= 0)) return; //Check if the damage is enough to kill the entity
if (!(damagedVillager.getType() == EntityType.VILLAGER)) return; //Check if the damaged entity is a Villager
if (!(VillagerSaver_PluginVars.ZombieTypes.contains(entityDamager.getType()))) return; //Check if the zombie types list contains the damager
if (villagerSaver.WorldBlackList.contains(damagedVillager.getWorld().getName())) return; //Check if the villager is not in a blacklisted World
if (VillagerSaver.WorldBlackList.contains(damagedVillager.getWorld().getName())) return; //Check if the villager is not in a blacklisted World
handleSpawnZombieVillager(damagedVillager);
event.setCancelled(true);
}

public void handleSpawnZombieVillager(LivingEntity livingEnt) {
private void handleSpawnZombieVillager(LivingEntity livingEnt) {
CraftVillager craftVillager = (CraftVillager) livingEnt;
Entity vehicle = craftVillager.getVehicle();

Expand Down
4 changes: 2 additions & 2 deletions src/cl/mariofinale/VillagerSaver_PluginVars.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import java.util.ArrayList;

public class VillagerSaver_PluginVars {
public static String PluginPrefix = ChatColor.BOLD + "" + ChatColor.GREEN + "[Villager Saver]:" + ChatColor.RESET;
public static ArrayList ZombieTypes = new ArrayList<EntityType>(){
public static final String PluginPrefix = ChatColor.BOLD + "" + ChatColor.GREEN + "[Villager Saver]:" + ChatColor.RESET;
public static final ArrayList ZombieTypes = new ArrayList<EntityType>(){
{
add(EntityType.ZOMBIE);
add(EntityType.ZOMBIE_VILLAGER);
Expand Down
80 changes: 0 additions & 80 deletions src/cl/mariofinale/villagerSaver.java

This file was deleted.

4 changes: 2 additions & 2 deletions src/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: VillagerSaver
version: 1.2.2
version: 1.2.3
authors: [MarioFinale, Kasama]
main: cl.mariofinale.villagerSaver
main: cl.mariofinale.VillagerSaver
api-version: 1.17
website: https://github.com/MarioFinale/VillagerSaver
commands:
Expand Down

0 comments on commit daf8e8e

Please sign in to comment.