Skip to content

Commit

Permalink
EliteMobs 7.3.12-SNAPSHOT-6
Browse files Browse the repository at this point in the history
- [New] Added the exports folder
    - [New] Exports folder now packages resource packs for admins. This hooks up to Model Engine to get the files, and aggregates the resource packs in the exports folder in a non-destructive way, meaning you can safely update existing resource packs if you place an existing one in the exports folder
    - [New] Added SHA1 texture pack calculation and optional server.properties overriding, updates the server-side data for you. All that is left to be done to apply Custom Models via resource packs is uploading the resource pack at a location of your choice.
    - [New] Added the command /em generateresourcepack for generating the resource pack
    - [New] Added the command /em updateresourcepack for automatically updating the SHA1 code of the texture pack based on the zipped resource pack in the exports folder, still requires a restart for it to be applied
    - [New] Importing dungeons with custom models will now autosuggest generating the resource pack to any online players with elitemobs.*
- [Fix] Reloading elitemobs now correctly unregisters timed and action events
- [Fix] Fixed error associated to the knockback invulnerability power
- [Fix] Fixed a database error which would trigger when a new player logged in and out extremely quickly
- [Refactor] Minor Sonarlint improvements

Signed-off-by: MagmaGuy <tiagoarnaut@gmail.com>
  • Loading branch information
MagmaGuy committed Jan 1, 2022
1 parent 4231110 commit 1d1e20f
Show file tree
Hide file tree
Showing 20 changed files with 543 additions and 139 deletions.
6 changes: 5 additions & 1 deletion src/main/java/com/magmaguy/elitemobs/EliteMobs.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.magmaguy.elitemobs.commands.guild.AdventurersGuildCommand;
import com.magmaguy.elitemobs.config.*;
import com.magmaguy.elitemobs.config.commands.CommandsConfig;
import com.magmaguy.elitemobs.config.configurationimporter.ConfigurationImporter;
import com.magmaguy.elitemobs.config.custombosses.CustomBossesConfig;
import com.magmaguy.elitemobs.config.custombosses.CustomBossesConfigFields;
import com.magmaguy.elitemobs.config.customevents.CustomEventsConfig;
Expand Down Expand Up @@ -234,6 +233,7 @@ public void onEnable() {

//Imports custom configurations and mindungeons from the import folder
ConfigurationImporter.initializeConfigs();
ConfigurationExporter.initializeConfigs();

//Import custom items after potentially importing new items
new CustomItemsConfig();
Expand Down Expand Up @@ -327,6 +327,10 @@ public void onDisable() {
new InfoMessage("Wiping Elite entities clean...");
EntityTracker.wipeShutdown();

new InfoMessage("Clearing events...");
TimedEvent.shutdown();
ActionEvent.shutdown();

new InfoMessage("Clearing valid worlds...");
validWorldList.clear();
new InfoMessage("Clearing zone based worlds...");
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/com/magmaguy/elitemobs/commands/AdminCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import cloud.commandframework.types.tuples.Triplet;
import com.magmaguy.elitemobs.ChatColorConverter;
import com.magmaguy.elitemobs.commands.admin.*;
import com.magmaguy.elitemobs.config.ConfigurationExporter;
import com.magmaguy.elitemobs.config.DefaultConfig;
import com.magmaguy.elitemobs.config.custombosses.CustomBossesConfig;
import com.magmaguy.elitemobs.config.custombosses.CustomBossesConfigFields;
Expand Down Expand Up @@ -705,6 +706,21 @@ public AdminCommands(PaperCommandManager<CommandSender> manager, Command.Builder
}
}));

// /em generateresourcepack
manager.command(builder.literal("generateresourcepack")
.senderType(CommandSender.class)
.permission("elitemobs.*")
.meta(CommandMeta.DESCRIPTION, "Generates a resource pack for EliteMobs using ModelEngine")
.handler(commandContext -> ConfigurationExporter.createResourcePack(commandContext.getSender())));

// /em updateresourcepack
manager.command(builder.literal("updateresourcepack")
.senderType(CommandSender.class)
.permission("elitemobs.*")
.meta(CommandMeta.DESCRIPTION, "Updates the SHA1 texture pack code based on the zipped resource pack in the exports.")
.handler(commandContext -> ConfigurationExporter.overwriteSHA1(commandContext.getSender())));


}

private void testFireball(Player player) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public void onInventoryInteraction(InventoryClickEvent event) {
if (!setupMenu.adventurersGuildIsDownloaded) {
player.closeInventory();
player.sendMessage("----------------------------------------------------");
player.sendMessage(ChatColorConverter.convert("&8[EliteMobs] &2Adventurer's Guild Hub download link: &9&nhttps://magmaguy.com/downloads/em_adventurers_guild.zip"));
player.sendMessage(ChatColorConverter.convert("&8[EliteMobs] &2Adventurer's Guild Hub download link: https://magmaguy.itch.io/"));
player.sendMessage(ChatColorConverter.convert("&8[EliteMobs] &aOnce downloaded, follow this setup guide: &9&nhttps://youtu.be/boRg2X4qhw4"));
player.sendMessage(ChatColorConverter.convert("&8[EliteMobs] &2Need help? " + DiscordLinks.mainLink));
player.sendMessage("----------------------------------------------------");
Expand Down
209 changes: 209 additions & 0 deletions src/main/java/com/magmaguy/elitemobs/config/ConfigurationExporter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
package com.magmaguy.elitemobs.config;

import com.magmaguy.elitemobs.ChatColorConverter;
import com.magmaguy.elitemobs.MetadataHandler;
import com.magmaguy.elitemobs.utils.InfoMessage;
import com.magmaguy.elitemobs.utils.SpigotMessage;
import com.magmaguy.elitemobs.utils.WarningMessage;
import com.magmaguy.elitemobs.utils.ZipFile;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Properties;

public class ConfigurationExporter {
private ConfigurationExporter() {
}

public static void initializeConfigs() {
Path configurationsPath = Paths.get(MetadataHandler.PLUGIN.getDataFolder().getAbsolutePath());
if (!Files.isDirectory(Paths.get(configurationsPath.normalize() + "" + File.separatorChar + "exports"))) {
try {
Files.createDirectory(Paths.get(configurationsPath.normalize() + "" + File.separatorChar + "exports"));
} catch (Exception exception) {
new WarningMessage("Failed to create exports directory! Tell the dev!");
exception.printStackTrace();
}
return;
}

try {
new File(Paths.get(MetadataHandler.PLUGIN.getDataFolder().getCanonicalPath() + File.separatorChar + "exports").toString());
} catch (Exception ex) {
new WarningMessage("Failed to get imports folder! Report this to the dev!");
}

}

public static void createResourcePack(CommandSender commandSender) {
if (!Bukkit.getPluginManager().isPluginEnabled("ModelEngine")) {
commandSender.sendMessage(ChatColorConverter.convert("&8[EliteMobs]&c Could not generate resource pack because ModelEngine is not installed! Install ModelEngine to use this feature."));
return;
}

File modelEngineResourcePackFile = new File(MetadataHandler.PLUGIN.getDataFolder().getParentFile().toString() + File.separatorChar + "ModelEngine" + File.separatorChar + "resource pack");
if (!modelEngineResourcePackFile.exists()) {
commandSender.sendMessage(ChatColorConverter.convert("&8[EliteMobs]&c Could not generate resource pack because ModelEngine is not installed! Install ModelEngine to use this feature."));
return;
}


try {
if (!Paths.get(MetadataHandler.PLUGIN.getDataFolder() + "" + File.separatorChar + "exports" + File.separatorChar + "elitemobs_resource_pack").toFile().exists())
Files.createDirectory(Paths.get(MetadataHandler.PLUGIN.getDataFolder() + "" + File.separatorChar + "exports" + File.separatorChar + "elitemobs_resource_pack"));
} catch (Exception ex) {
commandSender.sendMessage(ChatColorConverter.convert("&8[EliteMobs]&c Failed to generate target directory plugins/EliteMobs/exports/elitemobs_resource_pack required for storing the resource pack! Report this to the dev!"));
return;
}

copyDirectory(modelEngineResourcePackFile, Paths.get(MetadataHandler.PLUGIN.getDataFolder() + "" + File.separatorChar + "exports" + File.separatorChar + "elitemobs_resource_pack"));
commandSender.sendMessage(ChatColorConverter.convert("&8[EliteMobs]&f Copied all files from Model Engine to " + MetadataHandler.PLUGIN.getDataFolder() + "" + File.separatorChar + "exports" + File.separatorChar + "elitemobs_resource_pack"));

if (ZipFile.zip(new File(MetadataHandler.PLUGIN.getDataFolder() + "" + File.separatorChar + "exports" + File.separatorChar + "elitemobs_resource_pack"),
MetadataHandler.PLUGIN.getDataFolder() + "" + File.separatorChar + "exports" + File.separatorChar + "elitemobs_resource_pack.zip"))
commandSender.sendMessage(ChatColorConverter.convert("&8[EliteMobs]&f Packaged texture pack into " +
MetadataHandler.PLUGIN.getDataFolder() + "" + File.separatorChar + "exports" + File.separatorChar + "elitemobs_resource_pack.zip") +
" , ready to distribute!");
else {
commandSender.sendMessage(ChatColorConverter.convert("&8[EliteMobs]&c Failed to package the resource pack into a zipped file! Report this to the dev!"));
return;
}

if (commandSender instanceof Player) {
((Player) commandSender).spigot().sendMessage(SpigotMessage.commandHoverMessage(ChatColorConverter.convert("Done! &2You can click here to update your server.properties with the new SHA1 value of this texture pack!"),
"Click runs the /em updateresourcepack command!", "/em updateresourcepack"));
commandSender.sendMessage("If you want to do it manually, your SHA1 code is " + generateResourcePackSHA1(commandSender));
} else {
commandSender.sendMessage("Done! You can run the command /em updateresourcepack in order to put the right SHA1 value into server.properties. Don't forget to upload the texture place to some place where players can get it!");
commandSender.sendMessage("If you want to do it manually, your SHA1 code is " + generateResourcePackSHA1(commandSender));
}

}

private static String generateResourcePackSHA1(CommandSender commandSender) {
File zippedResourcePack = Paths.get(MetadataHandler.PLUGIN.getDataFolder() + "" + File.separatorChar + "exports" + File.separatorChar + "elitemobs_resource_pack.zip").toFile();
if (!zippedResourcePack.exists()) {
commandSender.sendMessage("[EliteMobs] Failed to generate SHA-1 code, no zipped resource pack found!");
return null;
}
String sha1 = null;
try {
sha1 = sha1Code(zippedResourcePack);
} catch (Exception ex) {
commandSender.sendMessage("[EliteMobs] Failed to generate SHA-1 code! Report this to the dev!");
ex.printStackTrace();
return null;
}
if (sha1 == null) {
commandSender.sendMessage("[EliteMobs] SHA-1 code is null! Report this to the dev!");
return null;
}
return sha1;
}

public static String sha1Code(File file) throws IOException, NoSuchAlgorithmException {
FileInputStream fileInputStream = new FileInputStream(file);
MessageDigest digest = MessageDigest.getInstance("SHA-1");
DigestInputStream digestInputStream = new DigestInputStream(fileInputStream, digest);
byte[] bytes = new byte[1024];
// read all file content
while (digestInputStream.read(bytes) > 0)
digest = digestInputStream.getMessageDigest();
byte[] resultByteArry = digest.digest();
return bytesToHexString(resultByteArry);
}

public static String bytesToHexString(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (byte b : bytes) {
int value = b & 0xFF;
if (value < 16) {
// if value less than 16, then it's hex String will be only
// one character, so we need to append a character of '0'
sb.append("0");
}
sb.append(Integer.toHexString(value).toUpperCase());
}
return sb.toString();
}

private static void copyDirectory(File directoryToClone, Path targetPath) {
for (File file : directoryToClone.listFiles())
try {
new InfoMessage("Adding " + file.getCanonicalPath());
copyFile(file, targetPath);
} catch (Exception exception) {
new WarningMessage("Failed to move directories for " + file.getName() + "! Tell the dev!");
exception.printStackTrace();
}
}

private static void copyFile(File file, Path targetPath) {
try {
if (file.isDirectory()) {
if (!Paths.get(targetPath + "" + File.separatorChar + file.getName()).toFile().exists())
Files.createDirectory(Paths.get(targetPath + "" + File.separatorChar + file.getName()));
for (File iteratedFile : file.listFiles())
copyFile(iteratedFile, Paths.get(targetPath + "" + File.separatorChar + file.getName()));
} else {
if (!Paths.get(targetPath + "" + File.separatorChar + file.getName()).toFile().exists() ||
!targetPath.toString().contains("pack.png") && !targetPath.toString().contains("pack.mcmeta"))
Files.copy(file.toPath(), Paths.get(targetPath + "" + File.separatorChar + file.getName()), StandardCopyOption.REPLACE_EXISTING);
else
new InfoMessage("File " + targetPath + "" + File.separatorChar + file.getName() + " already existed and should not be overwritten, skipping!");
}
} catch (Exception exception) {
new WarningMessage("Failed to copy directories for " + file.getName() + "! Tell the dev!");
exception.printStackTrace();
}
}

public static void overwriteSHA1(CommandSender commandSender) {
File serverProperties = null;
try {
serverProperties = new File(Paths.get(MetadataHandler.PLUGIN.getDataFolder().getParentFile().getCanonicalFile().getParentFile().toString() + File.separatorChar + "server.properties").toString());
if (!serverProperties.exists()) {
commandSender.sendMessage("[EliteMobs] Could not find server.properties file correctly! You will have to set the SHA-1 value manually!");
return;
}
} catch (Exception exception) {
new WarningMessage("[EliteMobs] Could not find server.properties file correctly! You will have to set the SHA-1 value manually.");
exception.printStackTrace();
return;
}

String sha1 = generateResourcePackSHA1(commandSender);
if (sha1 == null) return;
try {
FileInputStream in = new FileInputStream(serverProperties);
Properties props = new Properties();
props.load(in);
in.close();

FileOutputStream out = new FileOutputStream(serverProperties);
props.setProperty("resource-pack-sha1", sha1);
props.store(out, null);
out.close();
} catch (Exception ex) {
commandSender.sendMessage("[EliteMobs] Failed to save SHA1 value " + sha1 + " ! You will have to do this manually.");
}
commandSender.sendMessage(ChatColor.GREEN + "[EliteMobs] Successfully set the value resource-pack-sha1=" + sha1 + " in server.properties!");
commandSender.sendMessage(ChatColor.RED + "[EliteMobs] Don't forget to update the downloadable resource pack at your online location of choice!" +
" If you don't update the version people download things won't work correctly!");
commandSender.sendMessage(ChatColor.GREEN + "[EliteMobs] The server.properties modification will work starting with the next restart!");

}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.magmaguy.elitemobs.config.configurationimporter;
package com.magmaguy.elitemobs.config;

import com.magmaguy.elitemobs.ChatColorConverter;
import com.magmaguy.elitemobs.MetadataHandler;
import com.magmaguy.elitemobs.thirdparty.modelengine.CustomModel;
import com.magmaguy.elitemobs.utils.InfoMessage;
import com.magmaguy.elitemobs.utils.UnzipFile;
import com.magmaguy.elitemobs.utils.SpigotMessage;
import com.magmaguy.elitemobs.utils.WarningMessage;
import com.magmaguy.elitemobs.utils.ZipFile;
import org.apache.commons.io.FileUtils;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import java.io.File;
import java.nio.file.Files;
Expand All @@ -15,6 +18,8 @@
import java.nio.file.StandardCopyOption;

public class ConfigurationImporter {
private ConfigurationImporter() {
}

public static void initializeConfigs() {
Path configurationsPath = Paths.get(MetadataHandler.PLUGIN.getDataFolder().getAbsolutePath());
Expand Down Expand Up @@ -44,7 +49,7 @@ public static void initializeConfigs() {
File unzippedFile;
try {
if (zippedFile.getName().contains(".zip"))
unzippedFile = UnzipFile.run(zippedFile.getName());
unzippedFile = ZipFile.unzip(zippedFile.getName());
else unzippedFile = zippedFile;
} catch (Exception e) {
new WarningMessage("Failed to unzip config file " + zippedFile.getName() + " ! Tell the dev!");
Expand Down Expand Up @@ -115,6 +120,13 @@ public static void initializeConfigs() {

if (importedModels) {
CustomModel.reloadModels();
for (Player player : Bukkit.getOnlinePlayers())
if (player.hasPermission("elitemobs.*"))
player.spigot().sendMessage(SpigotMessage.commandHoverMessage(
ChatColorConverter.convert("&8[EliteMobs] &fEliteMobs just detected that recently imported files had Custom Models in them! " +
"&2Click here to generate the EliteMobs resource pack for those models!"),
"Clicking will run the command /em generateresourcepack",
"/em generateresourcepack"));
}

}
Expand Down
Loading

0 comments on commit 1d1e20f

Please sign in to comment.