Search in sources :

Example 1 with AttributeModifier

use of com.teamwizardry.wizardry.api.spell.attribute.AttributeModifier in project Wizardry by TeamWizardry.

the class SpellRing method processModifiers.

/**
 * Will process all modifiers and attributes set.
 * WILL RESET THE INFORMATION TAG.
 */
public void processModifiers() {
    informationTag = new NBTTagCompound();
    if (module != null) {
        module.getAttributeRanges().forEach((attribute, range) -> {
            informationTag.setDouble(attribute.getNbtName(), range.base);
        });
    }
    List<AttributeModifier> attributeModifiers = new ArrayList<>();
    if (module != null)
        attributeModifiers.addAll(module.getAttributes());
    for (ModuleModifier modifier : getModifiers().keySet()) {
        for (int i = 0; i < getModifiers().get(modifier); i++) attributeModifiers.addAll(modifier.getAttributes());
    }
    ArrayListMultimap<Operation, AttributeModifier> sortedMap = ArrayListMultimap.create();
    for (AttributeModifier modifier : attributeModifiers) sortedMap.put(modifier.getOperation(), modifier);
    for (Operation op : Operation.values()) {
        for (AttributeModifier modifier : sortedMap.get(op)) {
            double current = informationTag.getDouble(modifier.getAttribute().getNbtName());
            double newValue = modifier.apply(current);
            informationTag.setDouble(modifier.getAttribute().getNbtName(), newValue);
        // Wizardry.logger.info(module == null ? "<null module>" : module.getID() + ": Attribute: " + attribute + ": " + current + "-> " + newValue);
        }
    }
}
Also used : ModuleModifier(com.teamwizardry.wizardry.api.spell.module.ModuleModifier) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) Operation(com.teamwizardry.wizardry.api.spell.attribute.Operation) AttributeModifier(com.teamwizardry.wizardry.api.spell.attribute.AttributeModifier)

Example 2 with AttributeModifier

use of com.teamwizardry.wizardry.api.spell.attribute.AttributeModifier in project Wizardry by TeamWizardry.

the class ModuleRegistry method processModules.

public void processModules() {
    Wizardry.logger.info(" _______________________________________________________________________\\\\");
    Wizardry.logger.info(" | ");
    Wizardry.logger.error("| WARNING: YOU MAY WANT TO DELETE YOUR WHOLE WIZARDRY CONFIG FOLDER!!!! ======================= DO YOU SEE THIS MESSAGE YET?!?!?!?!? ======================= ");
    Wizardry.logger.error("| WARNING: YOU MAY WANT TO DELETE YOUR WHOLE WIZARDRY CONFIG FOLDER!!!! ======================= DO YOU SEE THIS MESSAGE YET?!?!?!?!? ======================= ");
    Wizardry.logger.error("| WARNING: YOU MAY WANT TO DELETE YOUR WHOLE WIZARDRY CONFIG FOLDER!!!! ======================= DO YOU SEE THIS MESSAGE YET?!?!?!?!? ======================= ");
    Wizardry.logger.error("| WARNING: YOU MAY WANT TO DELETE YOUR WHOLE WIZARDRY CONFIG FOLDER!!!! ======================= DO YOU SEE THIS MESSAGE YET?!?!?!?!? ======================= ");
    Wizardry.logger.error("| WARNING: YOU MAY WANT TO DELETE YOUR WHOLE WIZARDRY CONFIG FOLDER!!!! ======================= DO YOU SEE THIS MESSAGE YET?!?!?!?!? ======================= ");
    Wizardry.logger.error("| WARNING: YOU MAY WANT TO DELETE YOUR WHOLE WIZARDRY CONFIG FOLDER!!!! ======================= DO YOU SEE THIS MESSAGE YET?!?!?!?!? ======================= ");
    Wizardry.logger.error("| WARNING: YOU MAY WANT TO DELETE YOUR WHOLE WIZARDRY CONFIG FOLDER!!!! ======================= DO YOU SEE THIS MESSAGE YET?!?!?!?!? ======================= ");
    Wizardry.logger.error("| WARNING: YOU MAY WANT TO DELETE YOUR WHOLE WIZARDRY CONFIG FOLDER!!!! ======================= DO YOU SEE THIS MESSAGE YET?!?!?!?!? ======================= ");
    Wizardry.logger.error("| WARNING: YOU MAY WANT TO DELETE YOUR WHOLE WIZARDRY CONFIG FOLDER!!!! ======================= DO YOU SEE THIS MESSAGE YET?!?!?!?!? ======================= ");
    Wizardry.logger.error("| ================= THIS UPDATE BREAKS ALL OLD CONFIGS THIS IS WHY YOU SHOULD DELETE YOUR OLD ONES IF YOU HAVEN'T. OR RUN /wizardry reset IN GAME");
    Wizardry.logger.info(" | ");
    Wizardry.logger.info(" | ");
    Wizardry.logger.info(" | ");
    Wizardry.logger.info(" | ");
    Wizardry.logger.info(" | Starting module registration");
    HashSet<Module> processed = new HashSet<>();
    for (Module module : modules) {
        Wizardry.logger.info(" | |");
        Wizardry.logger.info(" | |_ Registering module " + module.getID());
        File file = new File(directory, module.getID() + ".json");
        if (!file.exists()) {
            Wizardry.logger.error("| | |_ SOMETHING WENT WRONG! " + file.getName() + " does NOT exist.");
            Wizardry.logger.error("| |___ Failed to register module " + module.getID());
            continue;
        }
        if (!file.canRead()) {
            Wizardry.logger.error("| | |_ SOMETHING WENT WRONG! Something is preventing me from reading " + file.getName());
            Wizardry.logger.error("| |___ Failed to register module " + module.getID());
        }
        JsonElement element;
        try {
            element = new JsonParser().parse(new FileReader(file));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            continue;
        }
        if (element == null) {
            Wizardry.logger.error("| | |_ SOMETHING WENT WRONG! Could not parse " + file.getName() + ". Invalid json.");
            Wizardry.logger.error("| |___ Failed to register module " + module.getID());
            continue;
        }
        if (!element.isJsonObject()) {
            Wizardry.logger.error("| | |_ SOMETHING WENT WRONG! " + file.getName() + "'s json is NOT a Json Object.");
            Wizardry.logger.error("| |___ Failed to register module " + module.getID());
            continue;
        }
        JsonObject moduleObject = element.getAsJsonObject();
        if (!moduleObject.has("item")) {
            Wizardry.logger.error("| | |_ SOMETHING WENT WRONG! No 'item' key found in " + file.getName() + ". Unknown item to use for element.");
            Wizardry.logger.error("| |___ Failed to register module " + module.getID());
            continue;
        }
        Color primaryColor = Color.WHITE;
        Color secondaryColor = Color.WHITE;
        int itemMeta = 0;
        DefaultHashMap<Attribute, AttributeRange> attributeRanges = new DefaultHashMap<>(AttributeRange.BACKUP);
        Item item = ForgeRegistries.ITEMS.getValue(new ResourceLocation(moduleObject.getAsJsonPrimitive("item").getAsString()));
        if (item == null || item.getRegistryName() == null) {
            Wizardry.logger.error("| | |_ SOMETHING WENT WRONG! Item for module " + module.getID() + " does not exist '" + moduleObject.getAsJsonPrimitive("item").getAsString() + "'");
            Wizardry.logger.error("| |___ Failed to register module " + module.getID());
            continue;
        } else {
            Wizardry.logger.info(" | | |_ Found Item " + item.getRegistryName().toString());
        }
        attributeRanges.put(AttributeRegistry.BURNOUT_MULTI, new AttributeRange(1, 0, Integer.MAX_VALUE));
        attributeRanges.put(AttributeRegistry.MANA_MULTI, new AttributeRange(1, 0, Integer.MAX_VALUE));
        attributeRanges.put(AttributeRegistry.POWER_MULTI, new AttributeRange(1, 0, Integer.MAX_VALUE));
        for (Entry<String, JsonElement> entry : moduleObject.entrySet()) {
            switch(entry.getKey()) {
                case "item_meta":
                    {
                        itemMeta = entry.getValue().getAsJsonPrimitive().getAsInt();
                        Wizardry.logger.info(" | | |_ Found Item Meta:          " + itemMeta);
                        break;
                    }
                case "primary_color":
                    {
                        primaryColor = new Color(Integer.parseInt(entry.getValue().getAsJsonPrimitive().getAsString(), 16));
                        Wizardry.logger.info(" | | |_ Found Primary Color:      " + primaryColor.getRGB());
                        break;
                    }
                case "secondary_color":
                    {
                        secondaryColor = new Color(Integer.parseInt(entry.getValue().getAsJsonPrimitive().getAsString(), 16));
                        Wizardry.logger.info(" | | |_ Found Secondary Color:    " + secondaryColor.getRGB());
                        break;
                    }
                default:
                    {
                        Attribute attribute = AttributeRegistry.getAttributeFromName(entry.getKey());
                        if (attribute != null) {
                            Wizardry.logger.info(" | | |_ Found base " + attribute.toString() + " values:");
                            JsonObject baseAttrib = entry.getValue().getAsJsonObject();
                            double min = 0;
                            if (baseAttrib.has("min") && baseAttrib.get("min").isJsonPrimitive()) {
                                min = baseAttrib.get("min").getAsInt();
                                if (min < 0) {
                                    Wizardry.logger.info(" | | | |_ Minimum value for " + attribute.toString() + " was " + min + ", must be a positive integer. Setting to 0");
                                    min = 0;
                                }
                                Wizardry.logger.info(" | | | |_ Minimum: " + min);
                            }
                            double max = Integer.MAX_VALUE;
                            if (baseAttrib.has("max") && baseAttrib.get("max").isJsonPrimitive()) {
                                max = baseAttrib.get("max").getAsDouble();
                                if (max < min) {
                                    Wizardry.logger.info(" | | | |_ Maximum value for " + attribute.toString() + " was " + max + ", must be greater than min. Setting to min, " + min);
                                    max = min;
                                }
                                if (max > Integer.MAX_VALUE) {
                                    Wizardry.logger.info(" | | | |_ Maximum maximum value is " + Integer.MAX_VALUE + ", max was " + max + ". Setting to " + Integer.MAX_VALUE);
                                    max = Integer.MAX_VALUE;
                                }
                                Wizardry.logger.info(" | | | |_ Maximum: " + max);
                            }
                            double base = min;
                            if (baseAttrib.has("base") && baseAttrib.get("base").isJsonPrimitive()) {
                                base = baseAttrib.get("base").getAsDouble();
                                if (base < min) {
                                    Wizardry.logger.info(" | | | |_ Base value for " + attribute.toString() + " was " + base + ", must be greater than min, " + min + ". Setting to " + min);
                                    base = min;
                                } else if (base > max) {
                                    Wizardry.logger.info(" | | | |_ Base value for " + attribute + " was " + base + ", must be less than max, " + max + ". Setting to " + max);
                                    base = max;
                                }
                                Wizardry.logger.info(" | | | |_ Base: " + base);
                            }
                            attributeRanges.put(attribute, new AttributeRange(base, min, max));
                        }
                    }
            }
        }
        module.init(new ItemStack(item, 1, itemMeta), primaryColor, secondaryColor, attributeRanges);
        if (moduleObject.has("modifiers") && moduleObject.get("modifiers").isJsonArray()) {
            Wizardry.logger.info(" | | |___ Found Modifiers. About to process them");
            JsonArray attributeModifiers = moduleObject.getAsJsonArray("modifiers");
            for (JsonElement attributeModifier : attributeModifiers) {
                if (attributeModifier.isJsonObject()) {
                    String attributeName = "NULL";
                    AttributeRegistry.Attribute attribute = null;
                    Operation operator = null;
                    double amount = 0;
                    JsonObject modifier = attributeModifier.getAsJsonObject();
                    if (modifier.has("attribute") && modifier.get("attribute").isJsonPrimitive() && modifier.getAsJsonPrimitive("attribute").isString()) {
                        attribute = AttributeRegistry.getAttributeFromName(modifier.getAsJsonPrimitive("attribute").getAsString());
                        if (attribute != null)
                            attributeName = attribute.getShortName();
                    }
                    if (modifier.has("operation") && modifier.get("operation").isJsonPrimitive() && modifier.getAsJsonPrimitive("operation").isString())
                        operator = Operation.valueOf(modifier.get("operation").getAsJsonPrimitive().getAsString().toUpperCase());
                    if (modifier.has("amount") && modifier.get("amount").isJsonPrimitive() && modifier.getAsJsonPrimitive("amount").isNumber())
                        amount = modifier.get("amount").getAsDouble();
                    Wizardry.logger.info(" | | | |_ Loading AttributeModifier for " + file.getName() + ": " + operator + " -> " + attributeName + ", " + amount);
                    if (attribute != null && operator != null) {
                        module.addAttribute(new AttributeModifier(attribute, amount, operator));
                        Wizardry.logger.info(" | | | | |_ AttributeModifier registered successfully");
                    } else {
                        Wizardry.logger.error("| | | | |_ Failed to register AttributeModifier!");
                    }
                }
            }
            Wizardry.logger.info(" | | |___ Modifiers Registered Successfully.");
        }
        processed.add(module);
        Wizardry.logger.info(" | |_ Module " + module.getID() + " registered successfully!");
    }
    primary: for (Module module1 : modules) {
        for (Module module2 : processed) if (module1.getID().equals(module2.getID()))
            continue primary;
        left.add(module1);
    }
    if (!left.isEmpty()) {
        Wizardry.logger.error("|");
        Wizardry.logger.error("|_ Missing or ignored modules detected in modules directory:");
        for (Module module : left) Wizardry.logger.error("| |_ " + module.getID());
    }
    left.clear();
    modules.clear();
    modules.addAll(processed);
    modules.sort(Comparator.comparing(Module::getID));
    Wizardry.logger.info(" |");
    Wizardry.logger.info(" | Module registration processing complete! (ᵔᴥᵔ)");
    Wizardry.logger.info(" |_______________________________________________________________________//");
}
Also used : DefaultHashMap(com.teamwizardry.wizardry.api.util.DefaultHashMap) Attribute(com.teamwizardry.wizardry.api.spell.attribute.AttributeRegistry.Attribute) JsonObject(com.google.gson.JsonObject) Operation(com.teamwizardry.wizardry.api.spell.attribute.Operation) Item(net.minecraft.item.Item) Attribute(com.teamwizardry.wizardry.api.spell.attribute.AttributeRegistry.Attribute) ResourceLocation(net.minecraft.util.ResourceLocation) JsonParser(com.google.gson.JsonParser) AttributeModifier(com.teamwizardry.wizardry.api.spell.attribute.AttributeModifier) JsonArray(com.google.gson.JsonArray) AttributeRegistry(com.teamwizardry.wizardry.api.spell.attribute.AttributeRegistry) JsonElement(com.google.gson.JsonElement) ItemStack(net.minecraft.item.ItemStack) AttributeRange(com.teamwizardry.wizardry.api.spell.attribute.AttributeRange)

Example 3 with AttributeModifier

use of com.teamwizardry.wizardry.api.spell.attribute.AttributeModifier in project Wizardry by TeamWizardry.

the class CommandWizardry method execute.

@Override
public void execute(@Nonnull MinecraftServer server, @Nonnull ICommandSender sender, @Nonnull String[] args) throws CommandException {
    if (args.length < 1)
        throw new WrongUsageException(getUsage(sender));
    if (args[0].equalsIgnoreCase("reload")) {
        ModuleRegistry.INSTANCE.loadUnprocessedModules();
        ModuleRegistry.INSTANCE.copyMissingModulesFromResources(CommonProxy.directory);
        ModuleRegistry.INSTANCE.processModules();
        if (server.isDedicatedServer()) {
            PacketHandler.NETWORK.sendToAll(new PacketSyncModules(ModuleRegistry.INSTANCE.modules));
        }
        notifyCommandListener(sender, this, "wizardry.command.success");
    } else if (args[0].equalsIgnoreCase("reset")) {
        notifyCommandListener(sender, this, "wizardry.command.reset");
        File moduleDirectory = new File(CommonProxy.directory, "modules");
        if (moduleDirectory.exists()) {
            File[] files = moduleDirectory.listFiles();
            if (files != null)
                for (File file : files) {
                    String name = file.getName();
                    if (!file.delete()) {
                        throw new CommandException("wizardry.command.fail", name);
                    } else {
                        notifyCommandListener(sender, this, "wizardry.command.success_delete", name);
                    }
                }
            if (!moduleDirectory.delete()) {
                throw new CommandException("wizardry.command.fail_dir_delete");
            }
        }
        if (!moduleDirectory.exists())
            if (!moduleDirectory.mkdirs()) {
                throw new CommandException("wizardry.command.fail_dir_create");
            }
        ModuleRegistry.INSTANCE.setDirectory(moduleDirectory);
        ModuleRegistry.INSTANCE.loadUnprocessedModules();
        ModuleRegistry.INSTANCE.copyMissingModulesFromResources(CommonProxy.directory);
        ModuleRegistry.INSTANCE.processModules();
        if (server.isDedicatedServer()) {
            PacketHandler.NETWORK.sendToAll(new PacketSyncModules(ModuleRegistry.INSTANCE.modules));
        }
        notifyCommandListener(sender, this, "wizardry.command.success");
    } else if (args[0].equalsIgnoreCase("listModules")) {
        notifyCommandListener(sender, this, TextFormatting.YELLOW + " ________________________________________________\\\\");
        notifyCommandListener(sender, this, TextFormatting.YELLOW + " | " + TextFormatting.GRAY + "Module List");
        for (Module module : ModuleRegistry.INSTANCE.modules) {
            notifyCommandListener(sender, this, TextFormatting.YELLOW + " | |_ " + TextFormatting.GREEN + module.getID() + TextFormatting.RESET + ": " + TextFormatting.GRAY + module.getReadableName());
        }
        notifyCommandListener(sender, this, TextFormatting.YELLOW + " |________________________________________________//");
    } else if (args[0].equalsIgnoreCase("debug")) {
        if (args.length < 2)
            throw new WrongUsageException(getUsage(sender));
        Module module = ModuleRegistry.INSTANCE.getModule(args[1]);
        if (module == null) {
            notifyCommandListener(sender, this, "Module not found.");
            return;
        }
        notifyCommandListener(sender, this, TextFormatting.YELLOW + " ________________________________________________\\\\");
        notifyCommandListener(sender, this, TextFormatting.YELLOW + " | " + TextFormatting.GRAY + "Module " + module.getReadableName() + ":");
        notifyCommandListener(sender, this, TextFormatting.YELLOW + " |  |_ " + TextFormatting.GREEN + "Description           " + TextFormatting.GRAY + " | " + TextFormatting.GRAY + module.getDescription());
        notifyCommandListener(sender, this, TextFormatting.YELLOW + " |  |_ " + TextFormatting.GREEN + "Item Stack            " + TextFormatting.GRAY + " | " + TextFormatting.GRAY + module.getItemStack().getDisplayName());
        notifyCommandListener(sender, this, TextFormatting.YELLOW + " |  |_ " + TextFormatting.GREEN + "Burnout Fill          " + TextFormatting.GRAY + " | " + TextFormatting.GRAY + module.getBurnoutFill());
        notifyCommandListener(sender, this, TextFormatting.YELLOW + " |  |  |_ " + TextFormatting.DARK_GREEN + "Burnout Multiplier" + TextFormatting.GRAY + " | " + TextFormatting.GRAY + module.getBurnoutMultiplier());
        notifyCommandListener(sender, this, TextFormatting.YELLOW + " |  |_ " + TextFormatting.GREEN + "Mana Drain           " + TextFormatting.GRAY + " | " + TextFormatting.GRAY + module.getManaDrain());
        notifyCommandListener(sender, this, TextFormatting.YELLOW + " |  |  |_" + TextFormatting.DARK_GREEN + "Mana Multiplier     " + TextFormatting.GRAY + " | " + TextFormatting.GRAY + module.getManaMultiplier());
        notifyCommandListener(sender, this, TextFormatting.YELLOW + " |  |_ " + TextFormatting.GREEN + "Power Multiplier     " + TextFormatting.GRAY + " | " + TextFormatting.GRAY + module.getPowerMultiplier());
        notifyCommandListener(sender, this, TextFormatting.YELLOW + " |  |_ " + TextFormatting.GREEN + "Charge Up Time      " + TextFormatting.GRAY + " | " + TextFormatting.GRAY + module.getChargeupTime());
        notifyCommandListener(sender, this, TextFormatting.YELLOW + " |  |_ " + TextFormatting.GREEN + "Cooldown Time        " + TextFormatting.GRAY + " | " + TextFormatting.GRAY + module.getCooldownTime());
        notifyCommandListener(sender, this, TextFormatting.YELLOW + " |  |_ " + TextFormatting.GREEN + "Primary Color        " + TextFormatting.GRAY + " | " + TextFormatting.RED + module.getPrimaryColor().getRed() + TextFormatting.GRAY + ", " + TextFormatting.GREEN + module.getPrimaryColor().getGreen() + TextFormatting.GRAY + ", " + TextFormatting.BLUE + module.getPrimaryColor().getBlue());
        notifyCommandListener(sender, this, TextFormatting.YELLOW + " |  |_ " + TextFormatting.GREEN + "Secondary Color    " + TextFormatting.GRAY + " | " + TextFormatting.RED + module.getSecondaryColor().getRed() + TextFormatting.GRAY + ", " + TextFormatting.GREEN + module.getSecondaryColor().getGreen() + TextFormatting.GRAY + ", " + TextFormatting.BLUE + module.getSecondaryColor().getBlue());
        if (!module.getAttributes().isEmpty())
            notifyCommandListener(sender, this, TextFormatting.YELLOW + " |  |_ " + TextFormatting.GREEN + "Default AttributeRegistry");
        for (AttributeModifier attributeModifier : module.getAttributes()) notifyCommandListener(sender, this, TextFormatting.YELLOW + " |  |  |_ " + TextFormatting.GRAY + attributeModifier.toString());
        ModuleModifier[] modifierList = module.applicableModifiers();
        if (modifierList != null) {
            notifyCommandListener(sender, this, TextFormatting.YELLOW + " |  |_ " + TextFormatting.GREEN + "Applicable Modifiers ");
            for (ModuleModifier modifier : modifierList) notifyCommandListener(sender, this, TextFormatting.YELLOW + " |     |_ " + TextFormatting.DARK_GREEN + modifier.getID());
        }
        notifyCommandListener(sender, this, TextFormatting.YELLOW + " |________________________________________________//");
    } else {
        throw new WrongUsageException(getUsage(sender));
    }
}
Also used : WrongUsageException(net.minecraft.command.WrongUsageException) PacketSyncModules(com.teamwizardry.wizardry.common.network.PacketSyncModules) ModuleModifier(com.teamwizardry.wizardry.api.spell.module.ModuleModifier) CommandException(net.minecraft.command.CommandException) Module(com.teamwizardry.wizardry.api.spell.module.Module) AttributeModifier(com.teamwizardry.wizardry.api.spell.attribute.AttributeModifier) File(java.io.File)

Aggregations

AttributeModifier (com.teamwizardry.wizardry.api.spell.attribute.AttributeModifier)3 Operation (com.teamwizardry.wizardry.api.spell.attribute.Operation)2 ModuleModifier (com.teamwizardry.wizardry.api.spell.module.ModuleModifier)2 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 JsonParser (com.google.gson.JsonParser)1 AttributeRange (com.teamwizardry.wizardry.api.spell.attribute.AttributeRange)1 AttributeRegistry (com.teamwizardry.wizardry.api.spell.attribute.AttributeRegistry)1 Attribute (com.teamwizardry.wizardry.api.spell.attribute.AttributeRegistry.Attribute)1 Module (com.teamwizardry.wizardry.api.spell.module.Module)1 DefaultHashMap (com.teamwizardry.wizardry.api.util.DefaultHashMap)1 PacketSyncModules (com.teamwizardry.wizardry.common.network.PacketSyncModules)1 File (java.io.File)1 CommandException (net.minecraft.command.CommandException)1 WrongUsageException (net.minecraft.command.WrongUsageException)1 Item (net.minecraft.item.Item)1 ItemStack (net.minecraft.item.ItemStack)1 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)1 ResourceLocation (net.minecraft.util.ResourceLocation)1