Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Multimeter Functionality on Generators #3300

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* It must be implemented on any Generator or {@link Reactor}.
*
* @author TheBusyBiscuit
* @author md5sha256
*
* @see EnergyNet
* @see EnergyNetComponent
Expand Down Expand Up @@ -48,6 +49,19 @@ default EnergyNetComponentType getEnergyComponentType() {
*/
int getGeneratedOutput(@Nonnull Location l, @Nonnull Config data);

/**
* The method return how much this {@link EnergyNetProvider} is expected to provide to the {@link EnergyNet}
* and is not to be confused with {@link #getGeneratedOutput(Location, Config)}. This method will not
* consume any fuel or modify the provider in any way.
Comment on lines +53 to +55
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

English here can be improved.

Suggested change
* The method return how much this {@link EnergyNetProvider} is expected to provide to the {@link EnergyNet}
* and is not to be confused with {@link #getGeneratedOutput(Location, Config)}. This method will not
* consume any fuel or modify the provider in any way.
* This method will return how much this {@link EnergyNetProvider} is expected to provide to the {@link EnergyNet}.
* It is not to be confused with {@link #getGeneratedOutput(Location, Config)}.
* This method <b>should not</b> consume any fuel or modify the provider in any way.

* @param l
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new line

Suggested change
* @param l
*
* @param l

* The {@link Location} of this {@link EnergyNetProvider}
* @param data
* The stored block data
*
* @return The expected generated output energy of this {@link EnergyNetProvider}.
*/
int peekGeneratedOutput(@Nonnull Location l, @Nonnull Config data);

/**
* This method returns whether the given {@link Location} is going to explode on the
* next tick.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package io.github.thebusybiscuit.slimefun4.implementation.items.electric.gadgets;

import java.util.Optional;
import java.util.function.Function;

import javax.annotation.ParametersAreNonnullByDefault;

import io.github.thebusybiscuit.slimefun4.core.attributes.EnergyNetProvider;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Import belongs with the others

import me.mrCookieSlime.CSCoreLibPlugin.Configuration.Config;
import me.mrCookieSlime.Slimefun.api.BlockStorage;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
Expand All @@ -23,6 +27,7 @@
* The {@link Multimeter} is used to measure charge and capacity of any {@link EnergyNetComponent}.
*
* @author TheBusyBiscuit
* @author md5sha256
*
* @see EnergyNet
* @see EnergyNetComponent
Expand Down Expand Up @@ -55,7 +60,15 @@ public ItemUseHandler getItemHandler() {

Player p = e.getPlayer();
p.sendMessage("");
Slimefun.getLocalization().sendMessage(p, "messages.multimeter", false, str -> str.replace("%stored%", stored).replace("%capacity%", capacity));

if (component instanceof EnergyNetProvider) {
EnergyNetProvider provider = (EnergyNetProvider) component;
Config data = BlockStorage.getLocationInfo(l);
String generating = NumberUtils.getCompactDouble(provider.peekGeneratedOutput(l, data)) + " J/t";
Slimefun.getLocalization().sendMessages(p, "messages.multimeter-generating", false, str -> str.replace("%stored%", stored).replace("%capacity%", capacity).replace("%generating%", generating));
} else {
Slimefun.getLocalization().sendMessage(p, "messages.multimeter", false, str -> str.replace("%stored%", stored).replace("%capacity%", capacity));
Comment on lines +68 to +70
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer moving multimeter out of messages now that it has multiple messages.

}
p.sendMessage("");
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.thebusybiscuit.slimefun4.implementation.items.electric.generators;

import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;

import org.bukkit.Location;
Expand All @@ -26,6 +27,7 @@
* {@link #getNightEnergy()}.
*
* @author TheBusyBiscuit
* @author md5sha256
*
* @see EnergyNet
* @see EnergyNetProvider
Expand Down Expand Up @@ -96,6 +98,12 @@ public int getGeneratedOutput(Location l, Config data) {
}
}

@Override
public int peekGeneratedOutput(@Nonnull Location l, @Nonnull Config data) {
// Solar panels will not change when generating energy
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Solar panels will not change when generating energy
// Solar panels do not consume fuel, we can fallback to the original method

return getGeneratedOutput(l, data);
}

/**
* This method returns whether a given {@link World} has daytime.
* It will also return false if a thunderstorm is active in this world.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
* @author John000708
* @author AlexLander123
* @author TheBusyBiscuit
* @author md5sha256
*
* @see AGenerator
* @see NuclearReactor
Expand Down Expand Up @@ -299,6 +300,43 @@ public int getGeneratedOutput(Location l, Config data) {
}
}

@Override
public int peekGeneratedOutput(@Nonnull Location l, @Nonnull Config data) {
BlockMenu inv = BlockStorage.getInventory(l);
BlockMenu accessPort = getAccessPort(l);
FuelOperation operation = processor.getOperation(l);

if (operation != null) {
return peekGenerateEnergy(l, data, inv, accessPort, operation);
} else {
return 0;
}
}

private int peekGenerateEnergy(@Nonnull Location l, @Nonnull Config data, @Nonnull BlockMenu inv, @Nullable BlockMenu accessPort, FuelOperation operation) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we extract some of the dupe code here? There's no reason to do a bunch of this in both peekGenerateEnergy and generateEnergy

int produced = getEnergyProduction();
String energyData = data.getString("energy-charge");
int charge = 0;

if (energyData != null) {
charge = Integer.parseInt(energyData);
}

int space = getCapacity() - charge;

if (space >= produced || getReactorMode(l) != ReactorMode.GENERATOR) {
if (needsCooling() && !hasEnoughCoolant(l, inv, accessPort, operation)) {
return 0;
}
}

if (space >= produced) {
return getEnergyProduction();
} else {
return 0;
}
}

private int generateEnergy(@Nonnull Location l, @Nonnull Config data, @Nonnull BlockMenu inv, @Nullable BlockMenu accessPort, @Nonnull FuelOperation operation) {
int produced = getEnergyProduction();
String energyData = data.getString("energy-charge");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,28 @@ public int getGeneratedOutput(Location l, Config data) {
}
}

@Override
public int peekGeneratedOutput(@Nonnull Location l, @Nonnull Config data) {
FuelOperation operation = processor.getOperation(l);
if (operation != null) {
if (!operation.isFinished()) {
if (isChargeable()) {
int charge = getCharge(l, data);
if (getCapacity() - charge >= getEnergyProduction()) {
return getEnergyProduction();
}
return 0;
} else {
return getEnergyProduction();
}
} else {
return 0;
}
} else {
return 0;
}
}
Comment on lines +205 to +222
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is overly long. Can we clean this up a bit?


private boolean isBucket(@Nullable ItemStack item) {
if (item == null) {
return false;
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/languages/en/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ messages:
disabled-item: '&4&lThis Item has been disabled! How did you even get that?'
no-tome-yourself: '&cYou cannot use the &4Tome of Knowledge &con yourself...'
multimeter: '&bStored Energy: &3%stored% &b/ &3%capacity%'
multimeter-generating:
- '&bStored Energy: &3%stored% &b/ &3%capacity%'
- '&bGenerating: &3%generating%'
piglin-barter: '&4You cannot barter with piglins using Slimefun items'
bee-suit-slow-fall: '&eYour Bee Wings will help you to get back to the ground safe and slow'

Expand Down