diff --git a/build.gradle b/build.gradle index 4594fb2..fc26a85 100644 --- a/build.gradle +++ b/build.gradle @@ -5,6 +5,8 @@ buildscript { name = "forge" url = "https://files.minecraftforge.net/maven" } + + flatDir dirs: 'libs' } dependencies { classpath "net.minecraftforge.gradle:ForgeGradle:2.1-SNAPSHOT" @@ -20,11 +22,6 @@ archivesBaseName = modBaseName sourceCompatibility = targetCompatibility = 1.8 compileJava.options.encoding = 'UTF-8' -configurations { - embed - compile.extendsFrom(embed) -} - minecraft { version = project.forgeVersion runDir = "run" @@ -32,8 +29,8 @@ minecraft { makeObfSourceJar = false } -repositories { - mavenCentral() +dependencies { + provided files("libs/modcore.jar") } processResources { @@ -54,3 +51,11 @@ processResources { exclude "mcmod.info" } } + +jar { + manifest { + attributes "FMLCorePlugin": "club.sk1er.modcore.FMLLoadingPlugin", + "ModSide": "CLIENT", + "FMLCorePluginContainsFMLMod": "Yes, yes it does" + } +} diff --git a/gradle.properties b/gradle.properties index a80e817..c15349c 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ modGroup=club.sk1er -modVersion=1.0 +modVersion=1.1 modBaseName=LobbySounds forgeVersion=1.8.9-11.15.1.2318-1.8.9 mcpVersion=stable_22 diff --git a/libs/modcore.jar b/libs/modcore.jar new file mode 100644 index 0000000..9d51af4 Binary files /dev/null and b/libs/modcore.jar differ diff --git a/src/main/java/club/sk1er/lobbysounds/LobbySounds.java b/src/main/java/club/sk1er/lobbysounds/LobbySounds.java index ace701a..edc34f2 100644 --- a/src/main/java/club/sk1er/lobbysounds/LobbySounds.java +++ b/src/main/java/club/sk1er/lobbysounds/LobbySounds.java @@ -2,6 +2,10 @@ import club.sk1er.lobbysounds.command.LobbySoundsCommand; import club.sk1er.lobbysounds.config.Sounds; +import club.sk1er.modcore.ModCoreInstaller; +import java.util.List; +import java.util.Timer; +import java.util.TimerTask; import net.minecraft.client.Minecraft; import net.minecraft.item.ItemStack; import net.minecraft.util.EnumChatFormatting; @@ -9,161 +13,161 @@ import net.minecraftforge.client.event.sound.PlaySoundEvent; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.fml.common.Mod.Instance; import net.minecraftforge.fml.common.event.FMLInitializationEvent; -import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; -import java.io.File; -import java.util.List; -import java.util.Timer; -import java.util.TimerTask; - @Mod(modid = "lobby_sounds", name = "Lobby Sounds", version = "1.0") public class LobbySounds { - private static String COMPASS_NAME = EnumChatFormatting.GREEN + "Game Menu " + EnumChatFormatting.GRAY + - "(Right Click)"; - private static String COMPASS_LORE = EnumChatFormatting.DARK_PURPLE.toString() + EnumChatFormatting.ITALIC.toString() + EnumChatFormatting.GRAY + - "Right Click to bring up the Game Menu!"; + private static final String COMPASS_NAME = + EnumChatFormatting.GREEN + "Game Menu " + EnumChatFormatting.GRAY + + "(Right Click)"; + private static final String COMPASS_LORE = + EnumChatFormatting.DARK_PURPLE.toString() + EnumChatFormatting.ITALIC.toString() + + EnumChatFormatting.GRAY + + "Right Click to bring up the Game Menu!"; - private boolean inHypixelLobby; - private boolean onHypixel; + private boolean inHypixelLobby; + private boolean onHypixel; - private File configFile; + private Sounds sounds; - @Mod.Instance("lobby_sounds") - public static LobbySounds instance; + @Instance("lobby_sounds") + public static LobbySounds instance; - @Mod.EventHandler - public void preinit(FMLPreInitializationEvent event) { - configFile = event.getSuggestedConfigurationFile(); - Sounds.loadConfig(); - Runtime.getRuntime().addShutdownHook(new Thread(Sounds::saveConfig)); - } + @Mod.EventHandler + public void init(FMLInitializationEvent event) { + ModCoreInstaller.initializeModCore(Minecraft.getMinecraft().mcDataDir); - @Mod.EventHandler - public void init(FMLInitializationEvent event) { - ClientCommandHandler.instance.registerCommand(new LobbySoundsCommand()); - MinecraftForge.EVENT_BUS.register(this); - runLobbyCheckerTimer(); - } + sounds = new Sounds(); + sounds.preload(); - @SubscribeEvent - public void soundPlay(PlaySoundEvent event) { - final String soundName = event.name; + ClientCommandHandler.instance.registerCommand(new LobbySoundsCommand()); + MinecraftForge.EVENT_BUS.register(this); + runLobbyCheckerTimer(); + } - if (onHypixel && inHypixelLobby) { - if (soundName.startsWith("mob.slime") && Sounds.DISABLE_SLIME_SOUNDS) { - setNullResult(event); - } + @SubscribeEvent + public void soundPlay(PlaySoundEvent event) { + String soundName = event.name; - if (soundName.startsWith("mob.enderdragon") && Sounds.DISABLE_DRAGON_SOUNDS) { - setNullResult(event); - } + if (onHypixel && inHypixelLobby) { + if (soundName.startsWith("mob.slime") && Sounds.DISABLE_SLIME_SOUNDS) { + setNullResult(event); + } - if ((soundName.startsWith("mob.wither") || soundName.startsWith("mob.skeleton")) && Sounds.DISABLE_WITHER_SOUNDS) { - setNullResult(event); - } + if (soundName.startsWith("mob.enderdragon") && Sounds.DISABLE_DRAGON_SOUNDS) { + setNullResult(event); + } - if (soundName.equals("random.orb") && Sounds.DISABLE_EXPERIENCE_SOUNDS) { - setNullResult(event); - } + if ((soundName.startsWith("mob.wither") || soundName.startsWith("mob.skeleton")) + && Sounds.DISABLE_WITHER_SOUNDS) { + setNullResult(event); + } - if (soundName.equals("random.pop") && Sounds.DISABLE_ITEM_PICKUP_SOUNDS) { - setNullResult(event); - } + if (soundName.equals("random.orb") && Sounds.DISABLE_EXPERIENCE_SOUNDS) { + setNullResult(event); + } - if (soundName.equals("game.tnt.primed") && Sounds.DISABLE_TNT_PRIME_SOUNDS) { - setNullResult(event); - } + if (soundName.equals("random.pop") && Sounds.DISABLE_ITEM_PICKUP_SOUNDS) { + setNullResult(event); + } - if (soundName.equals("random.explode") && Sounds.DISABLE_EXPLOSION_SOUNDS) { - setNullResult(event); - } + if (soundName.equals("game.tnt.primed") && Sounds.DISABLE_TNT_PRIME_SOUNDS) { + setNullResult(event); + } - if (soundName.equals("mob.chicken.plop") && Sounds.DISABLE_DELIVERY_MAN_SOUNDS) { - setNullResult(event); - } + if (soundName.equals("random.explode") && Sounds.DISABLE_EXPLOSION_SOUNDS) { + setNullResult(event); + } - if ((soundName.startsWith("note.") || soundName.equals("random.click") ) && Sounds.DISABLE_NOTE_SOUNDS) { - setNullResult(event); - } + if (soundName.equals("mob.chicken.plop") && Sounds.DISABLE_DELIVERY_MAN_SOUNDS) { + setNullResult(event); + } - if (soundName.startsWith("fireworks") && Sounds.DISABLE_FIREWORKS_SOUNDS) { - setNullResult(event); - } + if ((soundName.startsWith("note.") || soundName.equals("random.click")) + && Sounds.DISABLE_NOTE_SOUNDS) { + setNullResult(event); + } - if (soundName.equals("random.levelup") && Sounds.DISABLE_LEVELUP_SOUNDS) { - setNullResult(event); - } + if (soundName.startsWith("fireworks") && Sounds.DISABLE_FIREWORKS_SOUNDS) { + setNullResult(event); + } - if (soundName.startsWith("step.") && Sounds.DISABLE_STEP_SOUNDS) { - setNullResult(event); - } + if (soundName.equals("random.levelup") && Sounds.DISABLE_LEVELUP_SOUNDS) { + setNullResult(event); + } - if (soundName.startsWith("mob.bat") && Sounds.DISABLE_BAT_SOUNDS) { - setNullResult(event); - } + if (soundName.startsWith("step.") && Sounds.DISABLE_STEP_SOUNDS) { + setNullResult(event); + } - if (soundName.equals("fire.fire") && Sounds.DISABLE_FIRE_SOUNDS) { - setNullResult(event); - } + if (soundName.startsWith("mob.bat") && Sounds.DISABLE_BAT_SOUNDS) { + setNullResult(event); + } - if (soundName.startsWith("mob.endermen") && Sounds.DISABLE_ENDERMEN_SOUNDS) { - setNullResult(event); - } + if (soundName.equals("fire.fire") && Sounds.DISABLE_FIRE_SOUNDS) { + setNullResult(event); + } - if (event.result != null) { - System.out.println(event.name); - } - } - } + if (soundName.startsWith("mob.endermen") && Sounds.DISABLE_ENDERMEN_SOUNDS) { + setNullResult(event); + } - private void setNullResult(PlaySoundEvent event) { - event.result = null; + if (soundName.startsWith("random.bow") && Sounds.DISABLE_ARROW_SOUNDS) { + setNullResult(event); + } } - - // lobby checker taken from LobbyGlow by biscut - private void runLobbyCheckerTimer() { - Minecraft mc = Minecraft.getMinecraft(); - new Timer().scheduleAtFixedRate(new TimerTask() { - @Override - public void run() { - checkForItem(mc); - checkIp(mc); - } - }, 1000, 1000); + } + + private void setNullResult(PlaySoundEvent event) { + event.result = null; + } + + // lobby checker taken from LobbyGlow by biscut + private void runLobbyCheckerTimer() { + Minecraft mc = Minecraft.getMinecraft(); + new Timer().scheduleAtFixedRate(new TimerTask() { + @Override + public void run() { + checkForItem(mc); + checkIp(mc); + } + }, 1000, 1000); + } + + private void checkIp(Minecraft mc) { + if (mc.getCurrentServerData() != null) { + String ip = mc.getCurrentServerData().serverIP.toLowerCase(); + if (ip.equals("hypixel.net") || ip.endsWith(".hypixel.net") || ip.contains(".hypixel.net:") + || ip.startsWith("hypixel.net:")) { + onHypixel = true; + return; + } } - private void checkIp(Minecraft mc) { - if (mc.getCurrentServerData() != null) { - String ip = mc.getCurrentServerData().serverIP.toLowerCase(); - if (ip.equals("hypixel.net") || ip.endsWith(".hypixel.net") || ip.contains(".hypixel.net:") || ip.startsWith("hypixel.net:")) { - onHypixel = true; - return; - } - } + onHypixel = false; + } - onHypixel = false; - } + private void checkForItem(Minecraft mc) { + if (mc != null && mc.thePlayer != null && mc.thePlayer.inventory != null) { + ItemStack itemStack = mc.thePlayer.inventory.getStackInSlot(0); - private void checkForItem(Minecraft mc) { - if (mc != null && mc.thePlayer != null && mc.thePlayer.inventory != null) { - ItemStack itemStack = mc.thePlayer.inventory.getStackInSlot(0); - - if (itemStack != null && itemStack.hasDisplayName() && itemStack.getDisplayName().equals(COMPASS_NAME)) { - List tooltip = itemStack.getTooltip(mc.thePlayer, false); - if (tooltip.get(1).equals(COMPASS_LORE)) { - inHypixelLobby = true; - return; - } - } + if (itemStack != null && itemStack.hasDisplayName() && itemStack.getDisplayName() + .equals(COMPASS_NAME)) { + List tooltip = itemStack.getTooltip(mc.thePlayer, false); + if (tooltip.get(1).equals(COMPASS_LORE)) { + inHypixelLobby = true; + return; } - - inHypixelLobby = false; + } } - public File getConfigFile() { - return configFile; - } + inHypixelLobby = false; + } + + public Sounds getSounds() { + return sounds; + } } diff --git a/src/main/java/club/sk1er/lobbysounds/command/LobbySoundsCommand.java b/src/main/java/club/sk1er/lobbysounds/command/LobbySoundsCommand.java index 47820e2..7a397e7 100644 --- a/src/main/java/club/sk1er/lobbysounds/command/LobbySoundsCommand.java +++ b/src/main/java/club/sk1er/lobbysounds/command/LobbySoundsCommand.java @@ -1,37 +1,36 @@ package club.sk1er.lobbysounds.command; -import club.sk1er.lobbysounds.gui.LobbySoundsGui; -import club.sk1er.lobbysounds.util.TickScheduler; -import net.minecraft.client.Minecraft; -import net.minecraft.command.CommandBase; -import net.minecraft.command.ICommandSender; - +import club.sk1er.lobbysounds.LobbySounds; +import club.sk1er.mods.core.ModCore; import java.util.Collections; import java.util.List; +import net.minecraft.command.CommandBase; +import net.minecraft.command.ICommandSender; public class LobbySoundsCommand extends CommandBase { - @Override - public String getCommandName() { - return "lobbysounds"; - } - @Override - public String getCommandUsage(ICommandSender sender) { - return "/lobbysounds"; - } + @Override + public String getCommandName() { + return "lobbysounds"; + } + + @Override + public String getCommandUsage(ICommandSender sender) { + return "/lobbysounds"; + } - @Override - public void processCommand(ICommandSender sender, String[] args) { - TickScheduler.INSTANCE.schedule(0, () -> Minecraft.getMinecraft().displayGuiScreen(new LobbySoundsGui())); - } + @Override + public void processCommand(ICommandSender sender, String[] args) { + ModCore.getInstance().getGuiHandler().open(LobbySounds.instance.getSounds().gui()); + } - @Override - public int getRequiredPermissionLevel() { - return -1; - } + @Override + public int getRequiredPermissionLevel() { + return -1; + } - @Override - public List getCommandAliases() { - return Collections.singletonList("sounds"); - } + @Override + public List getCommandAliases() { + return Collections.singletonList("sounds"); + } } diff --git a/src/main/java/club/sk1er/lobbysounds/config/Sounds.java b/src/main/java/club/sk1er/lobbysounds/config/Sounds.java index 46aee86..e7d1c69 100644 --- a/src/main/java/club/sk1er/lobbysounds/config/Sounds.java +++ b/src/main/java/club/sk1er/lobbysounds/config/Sounds.java @@ -1,77 +1,157 @@ package club.sk1er.lobbysounds.config; -import club.sk1er.lobbysounds.LobbySounds; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; -import org.apache.commons.io.FileUtils; - -import java.io.IOException; - -public class Sounds { - - public static boolean DISABLE_SLIME_SOUNDS; - public static boolean DISABLE_DRAGON_SOUNDS; - public static boolean DISABLE_WITHER_SOUNDS; - public static boolean DISABLE_ITEM_PICKUP_SOUNDS; - public static boolean DISABLE_EXPERIENCE_SOUNDS; - public static boolean DISABLE_TNT_PRIME_SOUNDS; - public static boolean DISABLE_EXPLOSION_SOUNDS; - public static boolean DISABLE_DELIVERY_MAN_SOUNDS; - public static boolean DISABLE_NOTE_SOUNDS; - public static boolean DISABLE_FIREWORKS_SOUNDS; - public static boolean DISABLE_LEVELUP_SOUNDS; - public static boolean DISABLE_STEP_SOUNDS; - public static boolean DISABLE_ARROW_SOUNDS; - public static boolean DISABLE_BAT_SOUNDS; - public static boolean DISABLE_FIRE_SOUNDS; - public static boolean DISABLE_ENDERMEN_SOUNDS; - - public static void loadConfig() { - try { - JsonObject object = new JsonParser().parse(FileUtils.readFileToString(LobbySounds.instance.getConfigFile())).getAsJsonObject(); - if (object.has("disableSlimeSounds")) DISABLE_SLIME_SOUNDS = object.get("disableSlimeSounds").getAsBoolean(); - if (object.has("disableDragonSounds")) DISABLE_DRAGON_SOUNDS = object.get("disableDragonSounds").getAsBoolean(); - if (object.has("disableWitherSounds")) DISABLE_WITHER_SOUNDS = object.get("disableWitherSounds").getAsBoolean(); - if (object.has("disableItemPickupSounds")) DISABLE_ITEM_PICKUP_SOUNDS = object.get("disableItemPickupSounds").getAsBoolean(); - if (object.has("disableExperienceSounds")) DISABLE_EXPERIENCE_SOUNDS = object.get("disableExperienceSounds").getAsBoolean(); - if (object.has("disableTntPrimeSounds")) DISABLE_TNT_PRIME_SOUNDS = object.get("disableTntPrimeSounds").getAsBoolean(); - if (object.has("disableExplosionSounds")) DISABLE_EXPLOSION_SOUNDS = object.get("disableExplosionSounds").getAsBoolean(); - if (object.has("disableDeliveryManSounds")) DISABLE_DELIVERY_MAN_SOUNDS = object.get("disableDeliveryManSounds").getAsBoolean(); - if (object.has("disableNoteSounds")) DISABLE_NOTE_SOUNDS = object.get("disableNoteSounds").getAsBoolean(); - if (object.has("disableFireworksSounds")) DISABLE_FIREWORKS_SOUNDS = object.get("disableFireworksSounds").getAsBoolean(); - if (object.has("disableLevelupSounds")) DISABLE_LEVELUP_SOUNDS = object.get("disableLevelupSounds").getAsBoolean(); - if (object.has("disableStepSounds")) DISABLE_STEP_SOUNDS = object.get("disableStepSounds").getAsBoolean(); - if (object.has("disableArrowSounds")) DISABLE_ARROW_SOUNDS = object.get("disableArrowSounds").getAsBoolean(); - if (object.has("disableBatSounds")) DISABLE_BAT_SOUNDS = object.get("disableBatSounds").getAsBoolean(); - if (object.has("disableFireSounds")) DISABLE_FIRE_SOUNDS = object.get("disableFireSounds").getAsBoolean(); - if (object.has("disableEndermenSounds")) DISABLE_ENDERMEN_SOUNDS = object.get("disableEndermenSounds").getAsBoolean(); - } catch (IOException e) { - e.printStackTrace(); - } - } - - public static void saveConfig() { - try { - JsonObject object = new JsonObject(); - object.addProperty("disableSlimeSounds", DISABLE_SLIME_SOUNDS); - object.addProperty("disableDragonSounds", DISABLE_DRAGON_SOUNDS); - object.addProperty("disableWitherSounds", DISABLE_WITHER_SOUNDS); - object.addProperty("disableItemPickupSounds", DISABLE_ITEM_PICKUP_SOUNDS); - object.addProperty("disableExperienceSounds", DISABLE_EXPERIENCE_SOUNDS); - object.addProperty("disableTntPrimeSounds", DISABLE_TNT_PRIME_SOUNDS); - object.addProperty("disableExplosionSounds", DISABLE_EXPLOSION_SOUNDS); - object.addProperty("disableDeliveryManSounds", DISABLE_DELIVERY_MAN_SOUNDS); - object.addProperty("disableNoteSounds", DISABLE_NOTE_SOUNDS); - object.addProperty("disableFireworksSounds", DISABLE_FIREWORKS_SOUNDS); - object.addProperty("disableLevelupSounds", DISABLE_LEVELUP_SOUNDS); - object.addProperty("disableStepSounds", DISABLE_STEP_SOUNDS); - object.addProperty("disableArrowSounds", DISABLE_ARROW_SOUNDS); - object.addProperty("disableBatSounds", DISABLE_BAT_SOUNDS); - object.addProperty("disableFireSounds", DISABLE_FIRE_SOUNDS); - object.addProperty("disableEndermenSounds", DISABLE_ENDERMEN_SOUNDS); - FileUtils.writeStringToFile(LobbySounds.instance.getConfigFile(), object.toString()); - } catch (IOException e) { - e.printStackTrace(); - } - } +import club.sk1er.vigilance.Vigilant; +import club.sk1er.vigilance.data.Property; +import club.sk1er.vigilance.data.PropertyType; +import java.io.File; + +public class Sounds extends Vigilant { + + @Property( + type = PropertyType.SWITCH, + name = "Disable Slime Sounds", + description = "Remove sounds created by slimes.", + category = "Sounds", + subcategory = "Mobs" + ) + public static boolean DISABLE_SLIME_SOUNDS; + + @Property( + type = PropertyType.SWITCH, + name = "Disable Dragon Sounds", + description = "Remove sounds created by dragons.", + category = "Sounds", + subcategory = "Mobs" + ) + public static boolean DISABLE_DRAGON_SOUNDS; + + @Property( + type = PropertyType.SWITCH, + name = "Disable Wither Sounds", + description = "Remove sounds created by withers & wither skeletons.", + category = "Sounds", + subcategory = "Mobs" + ) + public static boolean DISABLE_WITHER_SOUNDS; + + @Property( + type = PropertyType.SWITCH, + name = "Disable Item Pickup Sounds", + description = "Remove sounds created by picking up an item.", + category = "Sounds", + subcategory = "Tile Entities" + ) + public static boolean DISABLE_ITEM_PICKUP_SOUNDS; + + @Property( + type = PropertyType.SWITCH, + name = "Disable Experience Orb Sounds", + description = "Remove sounds created by experience orbs.", + category = "Sounds", + subcategory = "Tile Entities" + ) + public static boolean DISABLE_EXPERIENCE_SOUNDS; + + @Property( + type = PropertyType.SWITCH, + name = "Disable Primed TNT Sounds", + description = "Remove sounds created by primed TNT.", + category = "Sounds", + subcategory = "Blocks" + ) + public static boolean DISABLE_TNT_PRIME_SOUNDS; + + @Property( + type = PropertyType.SWITCH, + name = "Disable Explosion Sounds", + description = "Remove sounds created by explosions.", + category = "Sounds", + subcategory = "Blocks" + ) + public static boolean DISABLE_EXPLOSION_SOUNDS; + + @Property( + type = PropertyType.SWITCH, + name = "Disable Delivery Man Sounds", + description = "Remove sounds created by delivery man events.", + category = "Sounds", + subcategory = "Players" + ) + public static boolean DISABLE_DELIVERY_MAN_SOUNDS; + + @Property( + type = PropertyType.SWITCH, + name = "Disable Mystery Box Sounds", + description = "Remove sounds created by mystery boxes.", + category = "Sounds", + subcategory = "Players" + ) + public static boolean DISABLE_NOTE_SOUNDS; + + @Property( + type = PropertyType.SWITCH, + name = "Disable Firework Sounds", + description = "Remove sounds created by fireworks.", + category = "Sounds", + subcategory = "Tile Entities" + ) + public static boolean DISABLE_FIREWORKS_SOUNDS; + + @Property( + type = PropertyType.SWITCH, + name = "Disable Levelup Sounds", + description = "Remove sounds created by someone leveling up.", + category = "Sounds", + subcategory = "Players" + ) + public static boolean DISABLE_LEVELUP_SOUNDS; + + @Property( + type = PropertyType.SWITCH, + name = "Disable Stepping Sounds", + description = "Remove sounds created by stepping.", + category = "Sounds", + subcategory = "Players" + ) + public static boolean DISABLE_STEP_SOUNDS; + + @Property( + type = PropertyType.SWITCH, + name = "Disable Arrow Sounds", + description = "Remove sounds created by arrows.", + category = "Sounds", + subcategory = "Tile Entities" + ) + public static boolean DISABLE_ARROW_SOUNDS; + + @Property( + type = PropertyType.SWITCH, + name = "Disable Bat Sounds", + description = "Remove sounds created by bats.", + category = "Sounds", + subcategory = "Mobs" + ) + public static boolean DISABLE_BAT_SOUNDS; + + @Property( + type = PropertyType.SWITCH, + name = "Disable Fire Sounds", + description = "Remove sounds created by fire.", + category = "Sounds", + subcategory = "Blocks" + ) + public static boolean DISABLE_FIRE_SOUNDS; + + @Property( + type = PropertyType.SWITCH, + name = "Disable Enderman Sounds", + description = "Remove sounds created by endermen.", + category = "Sounds", + subcategory = "Mobs" + ) + public static boolean DISABLE_ENDERMEN_SOUNDS; + + public Sounds() { + super(new File("./config/lobbysounds.toml")); + } } diff --git a/src/main/java/club/sk1er/lobbysounds/gui/LobbySoundsGui.java b/src/main/java/club/sk1er/lobbysounds/gui/LobbySoundsGui.java deleted file mode 100644 index 397c8d2..0000000 --- a/src/main/java/club/sk1er/lobbysounds/gui/LobbySoundsGui.java +++ /dev/null @@ -1,130 +0,0 @@ -package club.sk1er.lobbysounds.gui; - -import club.sk1er.lobbysounds.config.Sounds; -import club.sk1er.lobbysounds.gui.helper.AbstractScreen; -import net.minecraft.client.gui.GuiButton; -import net.minecraft.client.renderer.GlStateManager; -import net.minecraft.util.EnumChatFormatting; - -public class LobbySoundsGui extends AbstractScreen { - - @Override - public void initGui() { - createButton(0, 155, 0, getSuffix(Sounds.DISABLE_DRAGON_SOUNDS, "Dragon Sounds")); - createButton(1, 5, 0, getSuffix(Sounds.DISABLE_EXPERIENCE_SOUNDS, "Experience Sounds")); - createButton(2, 155, 1, getSuffix(Sounds.DISABLE_ITEM_PICKUP_SOUNDS, "Item Pickup Sounds")); - createButton(3, 5, 1, getSuffix(Sounds.DISABLE_SLIME_SOUNDS, "Slime Sounds")); - createButton(4, 155, 2, getSuffix(Sounds.DISABLE_WITHER_SOUNDS, "Wither Sounds")); - createButton(5, 5, 2, getSuffix(Sounds.DISABLE_TNT_PRIME_SOUNDS, "Tnt Prime Sounds")); - createButton(6, 155, 3, getSuffix(Sounds.DISABLE_EXPLOSION_SOUNDS, "Explosion Sounds")); - createButton(7, 5, 3, getSuffix(Sounds.DISABLE_DELIVERY_MAN_SOUNDS, "Delivery Man Sounds")); - createButton(8, 155, 4, getSuffix(Sounds.DISABLE_NOTE_SOUNDS, "Note Sounds")); - createButton(9, 5, 4, getSuffix(Sounds.DISABLE_FIREWORKS_SOUNDS, "Fireworks Sounds")); - createButton(10, 155, 5, getSuffix(Sounds.DISABLE_LEVELUP_SOUNDS, "Levelup Sounds")); - createButton(11, 5, 5, getSuffix(Sounds.DISABLE_STEP_SOUNDS, "Step Sounds")); - createButton(12, 155, 6, getSuffix(Sounds.DISABLE_ARROW_SOUNDS, "Arrow Sounds")); - createButton(13, 5, 6, getSuffix(Sounds.DISABLE_BAT_SOUNDS, "Bat Sounds")); - createButton(14, 155, 7, getSuffix(Sounds.DISABLE_FIRE_SOUNDS, "Fire Sounds")); - createButton(15, 5, 7, getSuffix(Sounds.DISABLE_ENDERMEN_SOUNDS, "Enderman Sounds")); - } - - @Override - protected void actionPerformed(GuiButton button) { - switch (button.id) { - case 0: - Sounds.DISABLE_DRAGON_SOUNDS = !Sounds.DISABLE_DRAGON_SOUNDS; - button.displayString = getSuffix(Sounds.DISABLE_DRAGON_SOUNDS, "Dragon Sounds"); - break; - case 1: - Sounds.DISABLE_EXPERIENCE_SOUNDS = !Sounds.DISABLE_EXPERIENCE_SOUNDS; - button.displayString = getSuffix(Sounds.DISABLE_EXPERIENCE_SOUNDS, "Experience Sounds"); - break; - case 2: - Sounds.DISABLE_ITEM_PICKUP_SOUNDS = !Sounds.DISABLE_ITEM_PICKUP_SOUNDS; - button.displayString = getSuffix(Sounds.DISABLE_ITEM_PICKUP_SOUNDS, "Item Pickup Sounds"); - break; - case 3: - Sounds.DISABLE_SLIME_SOUNDS = !Sounds.DISABLE_SLIME_SOUNDS; - button.displayString = getSuffix(Sounds.DISABLE_SLIME_SOUNDS, "Slime Sounds"); - break; - case 4: - Sounds.DISABLE_WITHER_SOUNDS = !Sounds.DISABLE_WITHER_SOUNDS; - button.displayString = getSuffix(Sounds.DISABLE_WITHER_SOUNDS, "Wither Sounds"); - break; - case 5: - Sounds.DISABLE_TNT_PRIME_SOUNDS = !Sounds.DISABLE_TNT_PRIME_SOUNDS; - button.displayString = getSuffix(Sounds.DISABLE_TNT_PRIME_SOUNDS, "Tnt Prime Sounds"); - break; - case 6: - Sounds.DISABLE_EXPLOSION_SOUNDS = !Sounds.DISABLE_EXPLOSION_SOUNDS; - button.displayString = getSuffix(Sounds.DISABLE_EXPLOSION_SOUNDS, "Explosion Sounds"); - break; - case 7: - Sounds.DISABLE_DELIVERY_MAN_SOUNDS = !Sounds.DISABLE_DELIVERY_MAN_SOUNDS; - button.displayString = getSuffix(Sounds.DISABLE_DELIVERY_MAN_SOUNDS, "Delivery Man Sounds"); - break; - case 8: - Sounds.DISABLE_NOTE_SOUNDS = !Sounds.DISABLE_NOTE_SOUNDS; - button.displayString = getSuffix(Sounds.DISABLE_NOTE_SOUNDS, "Note Sounds"); - break; - case 9: - Sounds.DISABLE_FIREWORKS_SOUNDS = !Sounds.DISABLE_FIREWORKS_SOUNDS; - button.displayString = getSuffix(Sounds.DISABLE_FIREWORKS_SOUNDS, "Fireworks Sounds"); - break; - case 10: - Sounds.DISABLE_LEVELUP_SOUNDS = !Sounds.DISABLE_LEVELUP_SOUNDS; - button.displayString = getSuffix(Sounds.DISABLE_LEVELUP_SOUNDS, "Levelup Sounds"); - break; - case 11: - Sounds.DISABLE_STEP_SOUNDS = !Sounds.DISABLE_STEP_SOUNDS; - button.displayString = getSuffix(Sounds.DISABLE_STEP_SOUNDS, "Step Sounds"); - break; - case 12: - Sounds.DISABLE_ARROW_SOUNDS = !Sounds.DISABLE_ARROW_SOUNDS; - button.displayString = getSuffix(Sounds.DISABLE_ARROW_SOUNDS, "Arrow Sounds"); - break; - case 13: - Sounds.DISABLE_BAT_SOUNDS = !Sounds.DISABLE_BAT_SOUNDS; - button.displayString = getSuffix(Sounds.DISABLE_BAT_SOUNDS, "Bat Sounds"); - break; - case 14: - Sounds.DISABLE_FIRE_SOUNDS = !Sounds.DISABLE_FIRE_SOUNDS; - button.displayString = getSuffix(Sounds.DISABLE_FIRE_SOUNDS, "Fire Sounds"); - break; - case 15: - Sounds.DISABLE_ENDERMEN_SOUNDS = !Sounds.DISABLE_ENDERMEN_SOUNDS; - button.displayString = getSuffix(Sounds.DISABLE_ENDERMEN_SOUNDS, "Endermen Sounds"); - break; - } - } - - @Override - public void drawScreen(int mouseX, int mouseY, float partialTicks) { - drawDefaultBackground(); - GlStateManager.pushMatrix(); - GlStateManager.scale(2, 2, 0); - drawCenteredString(mc.fontRendererObj, EnumChatFormatting.YELLOW + "Lobby Sounds", getCenter() / 2 + 5, getRowPos(-2), -1); - GlStateManager.popMatrix(); - drawCenteredString(mc.fontRendererObj, EnumChatFormatting.GREEN + "Green means the sound is enabled, " - + EnumChatFormatting.RED + "red means the sound is disabled.", width / 2, getRowPos(-1) + 8, -1); - super.drawScreen(mouseX, mouseY, partialTicks); - } - - private void createButton(int id, int width, int rowPos, String label) { - if (width == 155) { - buttonList.add(new GuiButton(id, getCenter() - width, getRowPos(rowPos), 150, 20, label)); - } else { - buttonList.add(new GuiButton(id, getCenter() + width, getRowPos(rowPos), 150, 20, label)); - } - } - - @Override - public boolean doesGuiPauseGame() { - return false; - } - - @Override - public void onGuiClosed() { - Sounds.saveConfig(); - } -} diff --git a/src/main/java/club/sk1er/lobbysounds/gui/helper/AbstractScreen.java b/src/main/java/club/sk1er/lobbysounds/gui/helper/AbstractScreen.java deleted file mode 100644 index ac93bbc..0000000 --- a/src/main/java/club/sk1er/lobbysounds/gui/helper/AbstractScreen.java +++ /dev/null @@ -1,9 +0,0 @@ -package club.sk1er.lobbysounds.gui.helper; - -import net.minecraft.client.gui.GuiScreen; - -public class AbstractScreen extends GuiScreen implements ScreenHelper { - protected int getCenter() { - return width / 2; - } -} diff --git a/src/main/java/club/sk1er/lobbysounds/gui/helper/ScreenHelper.java b/src/main/java/club/sk1er/lobbysounds/gui/helper/ScreenHelper.java deleted file mode 100644 index ec20994..0000000 --- a/src/main/java/club/sk1er/lobbysounds/gui/helper/ScreenHelper.java +++ /dev/null @@ -1,14 +0,0 @@ -package club.sk1er.lobbysounds.gui.helper; - -import net.minecraft.util.EnumChatFormatting; - -public interface ScreenHelper { - default String getSuffix(boolean option, String label) { - // if sound is disabled, show as red, otherwise show as green - return option ? EnumChatFormatting.RED + label : EnumChatFormatting.GREEN + label; - } - - default int getRowPos(int rowNumber) { - return 55 + rowNumber * 23; - } -} diff --git a/src/main/java/club/sk1er/lobbysounds/util/TickScheduler.java b/src/main/java/club/sk1er/lobbysounds/util/TickScheduler.java deleted file mode 100644 index 30e20a0..0000000 --- a/src/main/java/club/sk1er/lobbysounds/util/TickScheduler.java +++ /dev/null @@ -1,74 +0,0 @@ -package club.sk1er.lobbysounds.util; - -import net.minecraftforge.common.MinecraftForge; -import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; -import net.minecraftforge.fml.common.gameevent.TickEvent; - -import java.util.ArrayList; -import java.util.List; - -public class TickScheduler { - - /** - * Instance of this scheduler - */ - public static final TickScheduler INSTANCE = new TickScheduler(); - - /** - * List of tasks to execute - */ - private final List items = new ArrayList<>(); - - private TickScheduler() { - MinecraftForge.EVENT_BUS.register(this); - } - - /** - * Schedules a task that executes after a specific amount of ticks - * - * @param ticksToWait Ticks to wait - * @param runnable Task to execute - */ - public void schedule(int ticksToWait, Runnable runnable) { - items.add(new Task(ticksToWait, runnable)); - } - - @SubscribeEvent - public void onTickClientTick(TickEvent.ClientTickEvent event) { - items.removeIf(Task::execute); - } - - /** - * A simple task container - */ - private static class Task { - - /** - * Task to execute - */ - private final Runnable task; - /** - * Ticks left until the task executes - */ - private int ticksLeft; - - private Task(int ticksLeft, Runnable runnable) { - this.ticksLeft = ticksLeft; - this.task = runnable; - } - - /** - * Decrements the ticks left, and if it's 0 the task is ran and removed from the queue. - * - * @return True if the task should run and get removed - */ - boolean execute() { - if (ticksLeft <= 0) { - task.run(); - return true; - } - ticksLeft--; - return false; - } - } -} diff --git a/src/main/java/club/sk1er/modcore/FMLLoadingPlugin.java b/src/main/java/club/sk1er/modcore/FMLLoadingPlugin.java new file mode 100644 index 0000000..1948990 --- /dev/null +++ b/src/main/java/club/sk1er/modcore/FMLLoadingPlugin.java @@ -0,0 +1,81 @@ +package club.sk1er.modcore; + +import java.util.Map; +import net.minecraft.launchwrapper.Launch; +import net.minecraftforge.common.ForgeVersion; +import net.minecraftforge.fml.relauncher.IFMLLoadingPlugin; +import net.minecraftforge.fml.relauncher.IFMLLoadingPlugin.MCVersion; + +@MCVersion(ForgeVersion.mcVersion) +public class FMLLoadingPlugin implements IFMLLoadingPlugin { + + /** + * Return a list of classes that implements the IClassTransformer interface + * + * @return a list of classes that implements the IClassTransformer interface + */ + @Override + public String[] getASMTransformerClass() { + int initialize = ModCoreInstaller.initialize(Launch.minecraftHome, "1.8.9"); + + if (ModCoreInstaller.isErrored() || initialize != 0 && initialize != -1) { + // Technically wouldn't happen in simulated installed but is important for actual impl + System.out.println( + "Failed to load Sk1er Modcore - " + initialize + " - " + ModCoreInstaller.getError()); + } + + // If true the classes are loaded + if (ModCoreInstaller.isIsRunningModCore()) { + return new String[]{"club.sk1er.mods.core.forge.ClassTransformer"}; + } + + return new String[]{}; + } + + /** + * Return a class name that implements "ModContainer" for injection into the mod list The + * "getName" function should return a name that other mods can, if need be, depend on. Trivially, + * this modcontainer will be loaded before all regular mod containers, which means it will be + * forced to be "immutable" - not susceptible to normal sorting behaviour. All other mod + * behaviours are available however- this container can receive and handle normal loading events + */ + @Override + public String getModContainerClass() { + return null; + } + + /** + * Return the class name of an implementor of "IFMLCallHook", that will be run, in the main + * thread, to perform any additional setup this coremod may require. It will be run + * prior to Minecraft starting, so it CANNOT operate on minecraft itself. The + * game will deliberately crash if this code is detected to trigger a minecraft class loading + * (TODO: implement crash ;) ) + */ + @Override + public String getSetupClass() { + return null; + } + + /** + * Inject coremod data into this coremod This data includes: "mcLocation" : the location of the + * minecraft directory, "coremodList" : the list of coremods "coremodLocation" : the file this + * coremod loaded from, + * + * @param data + */ + @Override + public void injectData(Map data) { + + } + + /** + * Return an optional access transformer class for this coremod. It will be injected post-deobf so + * ensure your ATs conform to the new srgnames scheme. + * + * @return the name of an access transformer class or null if none is provided + */ + @Override + public String getAccessTransformerClass() { + return null; + } +} diff --git a/src/main/java/club/sk1er/modcore/ModCoreInstaller.java b/src/main/java/club/sk1er/modcore/ModCoreInstaller.java new file mode 100644 index 0000000..405acc9 --- /dev/null +++ b/src/main/java/club/sk1er/modcore/ModCoreInstaller.java @@ -0,0 +1,459 @@ +package club.sk1er.modcore; + +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import java.awt.Dimension; +import java.awt.Font; +import java.awt.GridLayout; +import java.awt.TextArea; +import java.awt.Toolkit; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.net.HttpURLConnection; +import java.net.URL; +import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Set; +import javax.swing.JFrame; +import javax.swing.JProgressBar; +import net.minecraft.launchwrapper.Launch; +import net.minecraft.launchwrapper.LaunchClassLoader; +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; + +/* + Created by Sk1er for use in all mods. Install under exact package name each time. + */ +public class ModCoreInstaller { + + + private static final String VERSION_URL = "https://api.sk1er.club/modcore_versions"; + private static final String className = "club.sk1er.mods.core.ModCore"; + private static boolean errored = false; + private static String error; + private static File dataDir = null; + private static boolean isRunningModCore = false; + + public static boolean isIsRunningModCore() { + return isRunningModCore; + } + + private static boolean isInitalized() { + try { + LinkedHashSet objects = new LinkedHashSet<>(); + objects.add(className); + Launch.classLoader.clearNegativeEntries(objects); + Field invalidClasses = LaunchClassLoader.class.getDeclaredField("invalidClasses"); + invalidClasses.setAccessible(true); + Object obj = invalidClasses.get(ModCoreInstaller.class.getClassLoader()); + ((Set) obj).remove(className); + return Class.forName("club.sk1er.mods.core.ModCore") != null; + } catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException ignored) { + ignored.printStackTrace(); + } + return false; + } + + public static boolean isErrored() { + return errored; + } + + public static String getError() { + return error; + } + + private static void bail(String error) { + errored = true; + ModCoreInstaller.error = error; + } + + private static JsonHolder readFile(File in) { + try { + return new JsonHolder(FileUtils.readFileToString(in)); + } catch (IOException ignored) { + + } + return new JsonHolder(); + } + + public static void initializeModCore(File gameDir) { + if (!isIsRunningModCore()) { + return; + } + try { + Class modCore = Class.forName(className); + Method instanceMethod = modCore.getMethod("getInstance"); + Method initialize = modCore.getMethod("initialize", File.class); + Object modCoreObject = instanceMethod.invoke(null); + initialize.invoke(modCoreObject, gameDir); + System.out.println("Loaded ModCore Successfully"); + return; + } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { + e.printStackTrace(); + } + System.out.println("Did NOT ModCore Successfully"); + } + + public static int initialize(File gameDir, String minecraftVersion) { + if (isInitalized()) return -1; + dataDir = new File(gameDir, "modcore"); + if (!dataDir.exists()) { + if (!dataDir.mkdirs()) { + bail("Unable to create necessary files"); + return 1; + } + } + JsonHolder jsonHolder = fetchJSON(VERSION_URL); + String latestRemote = jsonHolder.optString(minecraftVersion); + boolean failed = jsonHolder.getKeys().size() == 0 || (jsonHolder.has("success") && !jsonHolder.optBoolean("success")); + + File metadataFile = new File(dataDir, "metadata.json"); + JsonHolder localMetadata = readFile(metadataFile); + if (failed) latestRemote = localMetadata.optString(minecraftVersion); + File modcoreFile = new File(dataDir, "Sk1er Modcore-" + latestRemote + " (" + minecraftVersion + ").jar"); + + if (!modcoreFile.exists() || !localMetadata.optString(minecraftVersion).equalsIgnoreCase(latestRemote) && !failed) { + //File does not exist, or is out of date, download it + File old = new File(dataDir, "Sk1er Modcore-" + localMetadata.optString(minecraftVersion) + " (" + minecraftVersion + ").jar"); + if (old.exists()) old.delete(); + + if (!download("https://static.sk1er.club/repo/mods/modcore/" + latestRemote + "/" + minecraftVersion + "/ModCore-" + latestRemote + " (" + minecraftVersion + ").jar", latestRemote, modcoreFile, minecraftVersion, localMetadata)) { + bail("Unable to download"); + return 2; + } + + } + + addToClasspath(modcoreFile); + + if (!isInitalized()) { + bail("Something went wrong and it did not add the jar to the class path. Local file exists? " + modcoreFile.exists()); + return 3; + } + isRunningModCore = true; + return 0; + } + + + public static void addToClasspath(File file) { + try { + URL url = file.toURI().toURL(); + + ClassLoader classLoader = ModCoreInstaller.class.getClassLoader(); + Method method = classLoader.getClass().getDeclaredMethod("addURL", URL.class); + method.setAccessible(true); + method.invoke(classLoader, url); + } catch (Exception e) { + throw new RuntimeException("Unexpected exception", e); + } + } + + private static boolean download(String url, String version, File file, String mcver, JsonHolder versionData) { + url = url.replace(" ", "%20"); + System.out.println("Downloading ModCore " + " version " + version + " from: " + url); + JFrame frame = new JFrame("ModCore Initializer"); + JProgressBar bar = new JProgressBar(); + TextArea comp = new TextArea("", 1, 1, TextArea.SCROLLBARS_NONE); + frame.getContentPane().add(comp); + frame.getContentPane().add(bar); + GridLayout manager = new GridLayout(); + frame.setLayout(manager); + manager.setColumns(1); + manager.setRows(2); + comp.setText("Downloading Sk1er ModCore Library Version " + version + " for Minecraft " + mcver); + comp.setSize(399, 80); + comp.setEditable(false); + Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); + + + Dimension preferredSize = new Dimension(400, 225); + bar.setSize(preferredSize); + frame.setSize(preferredSize); + frame.setResizable(false); + bar.setBorderPainted(true); + bar.setMinimum(0); + bar.setStringPainted(true); + frame.setVisible(true); + frame.setLocation(dim.width / 2 - frame.getSize().width / 2, dim.height / 2 - frame.getSize().height / 2); + Font font = bar.getFont(); + bar.setFont(new Font(font.getName(), font.getStyle(), font.getSize() * 4)); + comp.setFont(new Font(font.getName(), font.getStyle(), font.getSize() * 2)); + + try { + + URL u = new URL(url); + HttpURLConnection connection = (HttpURLConnection) u.openConnection(); + connection.setRequestMethod("GET"); + connection.setUseCaches(true); + connection.addRequestProperty("User-Agent", "Mozilla/4.76 (Sk1er Modcore Initializer)"); + connection.setReadTimeout(15000); + connection.setConnectTimeout(15000); + connection.setDoOutput(true); + InputStream is = connection.getInputStream(); + int contentLength = connection.getContentLength(); + FileOutputStream outputStream = new FileOutputStream(file); + byte[] buffer = new byte[1024]; + System.out.println("MAX: " + contentLength); + bar.setMaximum(contentLength); + int read; + bar.setValue(0); + while ((read = is.read(buffer)) > 0) { + outputStream.write(buffer, 0, read); + bar.setValue(bar.getValue() + 1024); + } + outputStream.close(); + FileUtils.write(new File(dataDir, "metadata.json"), versionData.put(mcver, version).toString()); + } catch (Exception e) { + e.printStackTrace(); + frame.dispose(); + return false; + } + frame.dispose(); + return true; + } + + public static JsonHolder fetchJSON(String url) { + return new JsonHolder(fetchString(url)); + } + + public static String fetchString(String url) { + url = url.replace(" ", "%20"); + System.out.println("Fetching " + url); + try { + URL u = new URL(url); + HttpURLConnection connection = (HttpURLConnection) u.openConnection(); + connection.setRequestMethod("GET"); + connection.setUseCaches(true); + connection.addRequestProperty("User-Agent", "Mozilla/4.76 (Sk1er ModCore)"); + connection.setReadTimeout(15000); + connection.setConnectTimeout(15000); + connection.setDoOutput(true); + InputStream is = connection.getInputStream(); + return IOUtils.toString(is, Charset.defaultCharset()); + } catch (Exception e) { + e.printStackTrace(); + } + return "Failed to fetch"; + } + + + //Added because we need to use before ModCore is loaded + static class JsonHolder { + private JsonObject object; + + public JsonHolder(JsonObject object) { + this.object = object; + } + + public JsonHolder(String raw) { + if (raw == null) + object = new JsonObject(); + else + try { + this.object = new JsonParser().parse(raw).getAsJsonObject(); + } catch (Exception e) { + this.object = new JsonObject(); + e.printStackTrace(); + } + } + + public JsonHolder() { + this(new JsonObject()); + } + + @Override + public String toString() { + if (object != null) + return object.toString(); + return "{}"; + } + + public JsonHolder put(String key, boolean value) { + object.addProperty(key, value); + return this; + } + + public void mergeNotOverride(JsonHolder merge) { + merge(merge, false); + } + + public void mergeOverride(JsonHolder merge) { + merge(merge, true); + } + + public void merge(JsonHolder merge, boolean override) { + JsonObject object = merge.getObject(); + for (String s : merge.getKeys()) { + if (override || !this.has(s)) + put(s, object.get(s)); + } + } + + private void put(String s, JsonElement element) { + this.object.add(s, element); + } + + public JsonHolder put(String key, String value) { + object.addProperty(key, value); + return this; + } + + public JsonHolder put(String key, int value) { + object.addProperty(key, value); + return this; + } + + public JsonHolder put(String key, double value) { + object.addProperty(key, value); + return this; + } + + public JsonHolder put(String key, long value) { + object.addProperty(key, value); + return this; + } + + private JsonHolder defaultOptJSONObject(String key, JsonObject fallBack) { + try { + return new JsonHolder(object.get(key).getAsJsonObject()); + } catch (Exception e) { + return new JsonHolder(fallBack); + } + } + + public JsonArray defaultOptJSONArray(String key, JsonArray fallback) { + try { + return object.get(key).getAsJsonArray(); + } catch (Exception e) { + return fallback; + } + } + + public JsonArray optJSONArray(String key) { + return defaultOptJSONArray(key, new JsonArray()); + } + + + public boolean has(String key) { + return object.has(key); + } + + public long optLong(String key, long fallback) { + try { + return object.get(key).getAsLong(); + } catch (Exception e) { + return fallback; + } + } + + public long optLong(String key) { + return optLong(key, 0); + } + + public boolean optBoolean(String key, boolean fallback) { + try { + return object.get(key).getAsBoolean(); + } catch (Exception e) { + return fallback; + } + } + + public boolean optBoolean(String key) { + return optBoolean(key, false); + } + + public JsonObject optActualJSONObject(String key) { + try { + return object.get(key).getAsJsonObject(); + } catch (Exception e) { + return new JsonObject(); + } + } + + public JsonHolder optJSONObject(String key) { + return defaultOptJSONObject(key, new JsonObject()); + } + + + public int optInt(String key, int fallBack) { + try { + return object.get(key).getAsInt(); + } catch (Exception e) { + return fallBack; + } + } + + public int optInt(String key) { + return optInt(key, 0); + } + + + public String defaultOptString(String key, String fallBack) { + try { + return object.get(key).getAsString(); + } catch (Exception e) { + return fallBack; + } + } + + public String optString(String key) { + return defaultOptString(key, ""); + } + + + public double optDouble(String key, double fallBack) { + try { + return object.get(key).getAsDouble(); + } catch (Exception e) { + return fallBack; + } + } + + public List getKeys() { + List tmp = new ArrayList<>(); + object.entrySet().forEach(e -> tmp.add(e.getKey())); + return tmp; + } + + public double optDouble(String key) { + return optDouble(key, 0.0); + } + + + public JsonObject getObject() { + return object; + } + + public boolean isNull(String key) { + return object.has(key) && object.get(key).isJsonNull(); + } + + public JsonHolder put(String values, JsonHolder values1) { + return put(values, values1.getObject()); + } + + public JsonHolder put(String values, JsonObject object) { + this.object.add(values, object); + return this; + } + + public void put(String blacklisted, JsonArray jsonElements) { + this.object.add(blacklisted, jsonElements); + } + + public void remove(String header) { + object.remove(header); + } + } + + +} \ No newline at end of file