Search in sources :

Example 1 with EnergyNetComponent

use of io.github.thebusybiscuit.slimefun4.core.attributes.EnergyNetComponent 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 EnergyNetComponent

use of io.github.thebusybiscuit.slimefun4.core.attributes.EnergyNetComponent 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 3 with EnergyNetComponent

use of io.github.thebusybiscuit.slimefun4.core.attributes.EnergyNetComponent in project FluffyMachines by NCBPFluffyBear.

the class FluffyWrench method onWrenchInteract.

@EventHandler
public void onWrenchInteract(PlayerInteractEvent e) {
    Player p = e.getPlayer();
    ItemStack wrenchItem = e.getItem();
    Long cooldown = cooldowns.get(p.getUniqueId());
    if (isItem(wrenchItem) && cooldown != null) {
        if ((System.currentTimeMillis() - cooldown) < WRENCH_DELAY) {
            return;
        }
    }
    cooldowns.put(p.getUniqueId(), System.currentTimeMillis());
    Block block = e.getClickedBlock();
    // Can't use offhand because a player can offhand the wrench to escape the event
    if (isItem(e.getItem()) && !isItem(p.getInventory().getItemInOffHand()) && e.getAction().toString().endsWith("_BLOCK") && Slimefun.getProtectionManager().hasPermission(e.getPlayer(), block.getLocation(), Interaction.BREAK_BLOCK)) {
        e.setCancelled(true);
        SlimefunItem slimefunBlock = BlockStorage.check(block);
        // Check if slimefunBlock is not a machine or a cargo component
        if (slimefunBlock == null || (!(slimefunBlock instanceof EnergyNetComponent) && !slimefunBlock.getId().startsWith("CARGO_NODE") && !slimefunBlock.getId().equals(SlimefunItems.CARGO_MANAGER.getItemId()) && !(slimefunBlock instanceof TrashCan))) {
            return;
        }
        if (!type.isElectric) {
            damageItem(p, wrenchItem);
            breakBlock(block, p);
        } else if (removeItemCharge(wrenchItem, 1)) {
            breakBlock(block, p);
        }
    }
}
Also used : Player(org.bukkit.entity.Player) EnergyNetComponent(io.github.thebusybiscuit.slimefun4.core.attributes.EnergyNetComponent) Block(org.bukkit.block.Block) SimpleSlimefunItem(io.github.thebusybiscuit.slimefun4.implementation.items.SimpleSlimefunItem) SlimefunItem(io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem) ItemStack(org.bukkit.inventory.ItemStack) SlimefunItemStack(io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack) TrashCan(io.github.thebusybiscuit.slimefun4.implementation.items.cargo.TrashCan) EventHandler(org.bukkit.event.EventHandler)

Example 4 with EnergyNetComponent

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

the class EnergyNet method tick.

public void tick(@Nonnull Block b) {
    AtomicLong timestamp = new AtomicLong(Slimefun.getProfiler().newEntry());
    if (!regulator.equals(b.getLocation())) {
        updateHologram(b, "&4Multiple Energy Regulators connected");
        Slimefun.getProfiler().closeEntry(b.getLocation(), SlimefunItems.ENERGY_REGULATOR.getItem(), timestamp.get());
        return;
    }
    super.tick();
    if (connectorNodes.isEmpty() && terminusNodes.isEmpty()) {
        updateHologram(b, "&4No Energy Network found");
    } else {
        int supply = tickAllGenerators(timestamp::getAndAdd) + tickAllCapacitors();
        int remainingEnergy = supply;
        int demand = 0;
        for (Map.Entry<Location, EnergyNetComponent> entry : consumers.entrySet()) {
            Location loc = entry.getKey();
            EnergyNetComponent component = entry.getValue();
            int capacity = component.getCapacity();
            int charge = component.getCharge(loc);
            if (charge < capacity) {
                int availableSpace = capacity - charge;
                demand += availableSpace;
                if (remainingEnergy > 0) {
                    if (remainingEnergy > availableSpace) {
                        component.setCharge(loc, capacity);
                        remainingEnergy -= availableSpace;
                    } else {
                        component.setCharge(loc, charge + remainingEnergy);
                        remainingEnergy = 0;
                    }
                }
            }
        }
        storeRemainingEnergy(remainingEnergy);
        updateHologram(b, supply, demand);
    }
    // We have subtracted the timings from Generators, so they do not show up twice.
    Slimefun.getProfiler().closeEntry(b.getLocation(), SlimefunItems.ENERGY_REGULATOR.getItem(), timestamp.get());
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) EnergyNetComponent(io.github.thebusybiscuit.slimefun4.core.attributes.EnergyNetComponent) HashMap(java.util.HashMap) Map(java.util.Map) Location(org.bukkit.Location)

Example 5 with EnergyNetComponent

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

the class DebugFishListener method sendInfo.

@ParametersAreNonnullByDefault
private void sendInfo(Player p, Block b) {
    SlimefunItem item = BlockStorage.check(b);
    p.sendMessage(" ");
    p.sendMessage(ChatColors.color("&d" + b.getType() + " &e@ X: " + b.getX() + " Y: " + b.getY() + " Z: " + b.getZ()));
    p.sendMessage(ChatColors.color("&dId: " + "&e" + item.getId()));
    p.sendMessage(ChatColors.color("&dPlugin: " + "&e" + item.getAddon().getName()));
    if (b.getState() instanceof Skull) {
        p.sendMessage(ChatColors.color("&dSkull: " + greenCheckmark));
        // Check if the skull is a wall skull, and if so use Directional instead of Rotatable.
        if (b.getType() == Material.PLAYER_WALL_HEAD) {
            p.sendMessage(ChatColors.color("  &dFacing: &e" + ((Directional) b.getBlockData()).getFacing().toString()));
        } else {
            p.sendMessage(ChatColors.color("  &dRotation: &e" + ((Rotatable) b.getBlockData()).getRotation().toString()));
        }
    }
    if (BlockStorage.getStorage(b.getWorld()).hasInventory(b.getLocation())) {
        p.sendMessage(ChatColors.color("&dInventory: " + greenCheckmark));
    } else {
        p.sendMessage(ChatColors.color("&dInventory: " + redCross));
    }
    if (item.isTicking()) {
        p.sendMessage(ChatColors.color("&dTicking: " + greenCheckmark));
        p.sendMessage(ChatColors.color("  &dAsync: &e" + (item.getBlockTicker().isSynchronized() ? redCross : greenCheckmark)));
    } else if (item instanceof EnergyNetProvider) {
        p.sendMessage(ChatColors.color("&dTicking: &3Indirect (Generator)"));
    } else {
        p.sendMessage(ChatColors.color("&dTicking: " + redCross));
    }
    if (Slimefun.getProfiler().hasTimings(b)) {
        p.sendMessage(ChatColors.color("  &dTimings: &e" + Slimefun.getProfiler().getTime(b)));
        p.sendMessage(ChatColors.color("  &dTotal Timings: &e" + Slimefun.getProfiler().getTime(item)));
        p.sendMessage(ChatColors.color("  &dChunk Timings: &e" + Slimefun.getProfiler().getTime(b.getChunk())));
    }
    if (item instanceof EnergyNetComponent) {
        EnergyNetComponent component = (EnergyNetComponent) item;
        p.sendMessage(ChatColors.color("&dEnergyNet Component"));
        p.sendMessage(ChatColors.color("  &dType: &e" + component.getEnergyComponentType()));
        if (component.isChargeable()) {
            p.sendMessage(ChatColors.color("  &dChargeable: " + greenCheckmark));
            p.sendMessage(ChatColors.color("  &dEnergy: &e" + component.getCharge(b.getLocation()) + " / " + component.getCapacity()));
        } else {
            p.sendMessage(ChatColors.color("&dChargeable: " + redCross));
        }
    }
    p.sendMessage(ChatColors.color("&6" + BlockStorage.getBlockInfoAsJson(b)));
    p.sendMessage(" ");
}
Also used : Rotatable(org.bukkit.block.data.Rotatable) EnergyNetProvider(io.github.thebusybiscuit.slimefun4.core.attributes.EnergyNetProvider) EnergyNetComponent(io.github.thebusybiscuit.slimefun4.core.attributes.EnergyNetComponent) Skull(org.bukkit.block.Skull) SlimefunItem(io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem) Directional(org.bukkit.block.data.Directional) ParametersAreNonnullByDefault(javax.annotation.ParametersAreNonnullByDefault)

Aggregations

EnergyNetComponent (io.github.thebusybiscuit.slimefun4.core.attributes.EnergyNetComponent)5 SlimefunItem (io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem)3 EnergyNetProvider (io.github.thebusybiscuit.slimefun4.core.attributes.EnergyNetProvider)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Location (org.bukkit.Location)2 SlimefunItemStack (io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack)1 SimpleSlimefunItem (io.github.thebusybiscuit.slimefun4.implementation.items.SimpleSlimefunItem)1 TrashCan (io.github.thebusybiscuit.slimefun4.implementation.items.cargo.TrashCan)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 ParametersAreNonnullByDefault (javax.annotation.ParametersAreNonnullByDefault)1 Block (org.bukkit.block.Block)1 Skull (org.bukkit.block.Skull)1 Directional (org.bukkit.block.data.Directional)1 Rotatable (org.bukkit.block.data.Rotatable)1 Player (org.bukkit.entity.Player)1 EventHandler (org.bukkit.event.EventHandler)1 ItemStack (org.bukkit.inventory.ItemStack)1