Search in sources :

Example 1 with EnergyNetProvider

use of io.github.thebusybiscuit.slimefun4.core.attributes.EnergyNetProvider in project Slimefun4 by Slimefun.

the class EnergyNet method onClassificationChange.

@Override
public void onClassificationChange(Location l, NetworkComponent from, NetworkComponent to) {
    if (from == NetworkComponent.TERMINUS) {
        generators.remove(l);
        consumers.remove(l);
    }
    EnergyNetComponent component = getComponent(l);
    if (component != null) {
        switch(component.getEnergyComponentType()) {
            case CAPACITOR:
                capacitors.put(l, component);
                break;
            case CONSUMER:
                consumers.put(l, component);
                break;
            case GENERATOR:
                if (component instanceof EnergyNetProvider) {
                    generators.put(l, (EnergyNetProvider) component);
                } else if (component instanceof SlimefunItem) {
                    ((SlimefunItem) component).warn("This Item is marked as a GENERATOR but does not implement the interface EnergyNetProvider!");
                }
                break;
            default:
                break;
        }
    }
}
Also used : EnergyNetProvider(io.github.thebusybiscuit.slimefun4.core.attributes.EnergyNetProvider) EnergyNetComponent(io.github.thebusybiscuit.slimefun4.core.attributes.EnergyNetComponent) SlimefunItem(io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem)

Example 2 with EnergyNetProvider

use of io.github.thebusybiscuit.slimefun4.core.attributes.EnergyNetProvider in project Slimefun4 by Slimefun.

the class EnergyNet method tickAllGenerators.

private int tickAllGenerators(@Nonnull LongConsumer timings) {
    Set<Location> explodedBlocks = new HashSet<>();
    int supply = 0;
    for (Map.Entry<Location, EnergyNetProvider> entry : generators.entrySet()) {
        long timestamp = Slimefun.getProfiler().newEntry();
        Location loc = entry.getKey();
        EnergyNetProvider provider = entry.getValue();
        SlimefunItem item = (SlimefunItem) provider;
        try {
            Config data = BlockStorage.getLocationInfo(loc);
            int energy = provider.getGeneratedOutput(loc, data);
            if (provider.isChargeable()) {
                energy += provider.getCharge(loc, data);
            }
            if (provider.willExplode(loc, data)) {
                explodedBlocks.add(loc);
                BlockStorage.clearBlockInfo(loc);
                Slimefun.runSync(() -> {
                    loc.getBlock().setType(Material.LAVA);
                    loc.getWorld().createExplosion(loc, 0F, false);
                });
            } else {
                supply += energy;
            }
        } catch (Exception | LinkageError throwable) {
            explodedBlocks.add(loc);
            new ErrorReport<>(throwable, loc, item);
        }
        long time = Slimefun.getProfiler().closeEntry(loc, item, timestamp);
        timings.accept(time);
    }
    // Remove all generators which have exploded
    if (!explodedBlocks.isEmpty()) {
        generators.keySet().removeAll(explodedBlocks);
    }
    return supply;
}
Also used : EnergyNetProvider(io.github.thebusybiscuit.slimefun4.core.attributes.EnergyNetProvider) Config(me.mrCookieSlime.CSCoreLibPlugin.Configuration.Config) SlimefunItem(io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem) HashMap(java.util.HashMap) Map(java.util.Map) Location(org.bukkit.Location) HashSet(java.util.HashSet)

Example 3 with EnergyNetProvider

use of io.github.thebusybiscuit.slimefun4.core.attributes.EnergyNetProvider in project Slimefun4 by Slimefun.

the class EnergyNet method storeRemainingEnergy.

private void storeRemainingEnergy(int remainingEnergy) {
    for (Map.Entry<Location, EnergyNetComponent> entry : capacitors.entrySet()) {
        Location loc = entry.getKey();
        EnergyNetComponent component = entry.getValue();
        if (remainingEnergy > 0) {
            int capacity = component.getCapacity();
            if (remainingEnergy > capacity) {
                component.setCharge(loc, capacity);
                remainingEnergy -= capacity;
            } else {
                component.setCharge(loc, remainingEnergy);
                remainingEnergy = 0;
            }
        } else {
            component.setCharge(loc, 0);
        }
    }
    for (Map.Entry<Location, EnergyNetProvider> entry : generators.entrySet()) {
        Location loc = entry.getKey();
        EnergyNetProvider component = entry.getValue();
        int capacity = component.getCapacity();
        if (remainingEnergy > 0) {
            if (remainingEnergy > capacity) {
                component.setCharge(loc, capacity);
                remainingEnergy -= capacity;
            } else {
                component.setCharge(loc, remainingEnergy);
                remainingEnergy = 0;
            }
        } else {
            component.setCharge(loc, 0);
        }
    }
}
Also used : EnergyNetProvider(io.github.thebusybiscuit.slimefun4.core.attributes.EnergyNetProvider) EnergyNetComponent(io.github.thebusybiscuit.slimefun4.core.attributes.EnergyNetComponent) HashMap(java.util.HashMap) Map(java.util.Map) Location(org.bukkit.Location)

Example 4 with EnergyNetProvider

use of io.github.thebusybiscuit.slimefun4.core.attributes.EnergyNetProvider in project SupplementalServiceableness by NCBPFluffyBear.

the class Voltmeter method elementStatistics.

private void elementStatistics(Player player, EnergyNet energyNet) {
    Map<Location, EnergyNetComponent> capacitors = null;
    Map<Location, EnergyNetComponent> consumers = null;
    Map<Location, EnergyNetProvider> generators = null;
    try {
        Field capacitorsField = energyNet.getClass().getDeclaredField("capacitors");
        Field consumersField = energyNet.getClass().getDeclaredField("consumers");
        Field generatorsField = energyNet.getClass().getDeclaredField("generators");
        capacitorsField.setAccessible(true);
        consumersField.setAccessible(true);
        generatorsField.setAccessible(true);
        capacitors = (Map<Location, EnergyNetComponent>) capacitorsField.get(energyNet);
        consumers = (Map<Location, EnergyNetComponent>) consumersField.get(energyNet);
        generators = (Map<Location, EnergyNetProvider>) generatorsField.get(energyNet);
    } catch (NoSuchFieldException | IllegalAccessException ex) {
        Bukkit.getLogger().log(Level.SEVERE, "An error has occurred while trying to access private fields: " + System.lineSeparator() + ex);
    }
    Map<String, Integer> consumersSorted = new HashMap<>();
    Map<String, Integer> capacitorsSorted = new HashMap<>();
    Map<String, Integer> generatorsSorted = new HashMap<>();
    int numberOfComponents = capacitors.size() + consumers.size() + generators.size();
    for (Location loc : capacitors.keySet()) {
        EnergyNetComponent capacitor = capacitors.get(loc);
        if (capacitorsSorted.containsKey(capacitor.getId())) {
            capacitorsSorted.replace(capacitor.getId(), capacitorsSorted.get(capacitor.getId()) + 1);
        } else {
            capacitorsSorted.put(capacitor.getId(), 1);
        }
    }
    for (Location loc : consumers.keySet()) {
        EnergyNetComponent consumer = consumers.get(loc);
        if (consumersSorted.containsKey(consumer.getId())) {
            consumersSorted.replace(consumer.getId(), consumersSorted.get(consumer.getId()) + 1);
        } else {
            consumersSorted.put(consumer.getId(), 1);
        }
    }
    for (Location loc : generators.keySet()) {
        EnergyNetProvider generator = generators.get(loc);
        if (generatorsSorted.containsKey(generator.getId())) {
            generatorsSorted.replace(generator.getId(), generatorsSorted.get(generator.getId()) + 1);
        } else {
            generatorsSorted.put(generator.getId(), 1);
        }
    }
    StringBuilder sb = new StringBuilder();
    for (String key : capacitorsSorted.keySet()) {
        sb.append(pretifyId(key));
        sb.append(" &ox");
        sb.append(capacitorsSorted.get(key));
        sb.append(", &r&7");
    }
    String capacitorsList = sb.toString();
    sb.setLength(0);
    for (String key : consumersSorted.keySet()) {
        sb.append(pretifyId(key));
        sb.append(" &ox");
        sb.append(consumersSorted.get(key));
        sb.append(", &r&7");
    }
    String consumersList = sb.toString();
    sb.setLength(0);
    for (String key : generatorsSorted.keySet()) {
        sb.append(pretifyId(key));
        sb.append(" &ox");
        sb.append(generatorsSorted.get(key));
        sb.append(", &r&7");
    }
    String generatorsList = sb.toString();
    Utils.sendChatMsg(player, "&7========== &5Components &7==========");
    Utils.sendChatMsg(player, "");
    Utils.sendChatMsg(player, "&5Number of components: &7" + numberOfComponents);
    Utils.sendChatMsg(player, "&5Consumers: &7" + consumersList.substring(0, consumersList.length() - 6));
    Utils.sendChatMsg(player, "&5Capacitors: &7" + capacitorsList.substring(0, capacitorsList.length() - 6));
    Utils.sendChatMsg(player, "&5Generators: &7" + generatorsList.substring(0, generatorsList.length() - 6));
    Utils.sendChatMsg(player, "");
    Utils.sendChatMsg(player, "&7============================");
}
Also used : EnergyNetProvider(io.github.thebusybiscuit.slimefun4.core.attributes.EnergyNetProvider) HashMap(java.util.HashMap) Field(java.lang.reflect.Field) EnergyNetComponent(io.github.thebusybiscuit.slimefun4.core.attributes.EnergyNetComponent) Location(org.bukkit.Location)

Example 5 with EnergyNetProvider

use of io.github.thebusybiscuit.slimefun4.core.attributes.EnergyNetProvider in project SupplementalServiceableness by NCBPFluffyBear.

the class Voltmeter method chargeStatistics.

private void chargeStatistics(Player player, EnergyNet energyNet) {
    Map<Location, EnergyNetComponent> capacitors = null;
    Map<Location, EnergyNetComponent> consumers = null;
    Map<Location, EnergyNetProvider> generators = null;
    try {
        Field capacitorsField = energyNet.getClass().getDeclaredField("capacitors");
        Field consumersField = energyNet.getClass().getDeclaredField("consumers");
        Field generatorsField = energyNet.getClass().getDeclaredField("generators");
        capacitorsField.setAccessible(true);
        consumersField.setAccessible(true);
        generatorsField.setAccessible(true);
        capacitors = (Map<Location, EnergyNetComponent>) capacitorsField.get(energyNet);
        consumers = (Map<Location, EnergyNetComponent>) consumersField.get(energyNet);
        generators = (Map<Location, EnergyNetProvider>) generatorsField.get(energyNet);
    } catch (NoSuchFieldException | IllegalAccessException ex) {
        Bukkit.getLogger().log(Level.SEVERE, "An error has occurred while trying to access private fields: " + System.lineSeparator() + ex);
    }
    int numberOfComponents = capacitors.size() + consumers.size() + generators.size();
    int capacity = 0;
    int charge = 0;
    int minCharge = Integer.MAX_VALUE;
    int maxCharge = 0;
    EnergyNetComponent smallestChargeComponent = null;
    EnergyNetComponent largestChargeComponent = null;
    for (Location loc : capacitors.keySet()) {
        Capacitor capacitor = (Capacitor) capacitors.get(loc);
        int currCharge = capacitor.getCharge(loc);
        capacity += capacitor.getCapacity();
        charge += currCharge;
        if (currCharge < minCharge) {
            minCharge = currCharge;
            smallestChargeComponent = capacitor;
        }
        if (currCharge > maxCharge) {
            maxCharge = currCharge;
            largestChargeComponent = capacitor;
        }
    }
    for (Location loc : consumers.keySet()) {
        EnergyNetComponent consumer = consumers.get(loc);
        int currCharge = consumer.getCharge(loc);
        capacity += consumer.getCapacity();
        charge += currCharge;
        if (currCharge < minCharge) {
            minCharge = currCharge;
            smallestChargeComponent = consumer;
        }
        if (currCharge > maxCharge) {
            maxCharge = currCharge;
            largestChargeComponent = consumer;
        }
    }
    for (Location loc : generators.keySet()) {
        EnergyNetProvider generator = generators.get(loc);
        int currCharge = generator.getCharge(loc);
        capacity += generator.getCapacity();
        charge += currCharge;
        if (currCharge < minCharge) {
            minCharge = currCharge;
            smallestChargeComponent = generator;
        }
        if (currCharge > maxCharge) {
            maxCharge = currCharge;
            largestChargeComponent = generator;
        }
    }
    Utils.sendChatMsg(player, "&7========== &5Charge &7==========");
    Utils.sendChatMsg(player, "");
    Utils.sendChatMsg(player, "&5Number of components: &7" + numberOfComponents);
    Utils.sendChatMsg(player, "&5Capacity: &7" + capacity + " J");
    Utils.sendChatMsg(player, "&5Current charge: &7" + charge + " J &o(" + ((double) Math.round(((double) charge / capacity * 100) * 100) / 100) + "%)");
    Utils.sendChatMsg(player, "&5Smallest charge: &7" + (Objects.isNull(smallestChargeComponent) ? "None" : pretifyId(smallestChargeComponent.getId())) + " &5with &7" + (Objects.isNull(smallestChargeComponent) ? "0" : minCharge) + " J");
    Utils.sendChatMsg(player, "&5Largest charge: &7" + (Objects.isNull(largestChargeComponent) ? "None" : pretifyId(largestChargeComponent.getId())) + " &5with &7" + maxCharge + " J");
    Utils.sendChatMsg(player, "");
    Utils.sendChatMsg(player, "&7============================");
}
Also used : Field(java.lang.reflect.Field) EnergyNetProvider(io.github.thebusybiscuit.slimefun4.core.attributes.EnergyNetProvider) EnergyNetComponent(io.github.thebusybiscuit.slimefun4.core.attributes.EnergyNetComponent) Capacitor(io.github.thebusybiscuit.slimefun4.implementation.items.electric.Capacitor) Location(org.bukkit.Location)

Aggregations

EnergyNetProvider (io.github.thebusybiscuit.slimefun4.core.attributes.EnergyNetProvider)7 EnergyNetComponent (io.github.thebusybiscuit.slimefun4.core.attributes.EnergyNetComponent)5 Location (org.bukkit.Location)5 SlimefunItem (io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem)3 Field (java.lang.reflect.Field)3 HashMap (java.util.HashMap)3 Map (java.util.Map)2 AbstractEnergyProvider (io.github.thebusybiscuit.slimefun4.implementation.items.electric.AbstractEnergyProvider)1 Capacitor (io.github.thebusybiscuit.slimefun4.implementation.items.electric.Capacitor)1 SolarGenerator (io.github.thebusybiscuit.slimefun4.implementation.items.electric.generators.SolarGenerator)1 HashSet (java.util.HashSet)1 ParametersAreNonnullByDefault (javax.annotation.ParametersAreNonnullByDefault)1 Config (me.mrCookieSlime.CSCoreLibPlugin.Configuration.Config)1 Skull (org.bukkit.block.Skull)1 Directional (org.bukkit.block.data.Directional)1 Rotatable (org.bukkit.block.data.Rotatable)1