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

refactor: bind energy storage to controller item #432

Merged
merged 1 commit into from
Aug 29, 2023
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Changed

- You can now recharge the Controller in item form.

### Removed

- The `useEnergy` config option for the Wireless Grid. If you do not wish to use energy, use the
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.refinedmods.refinedstorage2.platform.api.item;

import com.refinedmods.refinedstorage2.api.core.Action;
import com.refinedmods.refinedstorage2.platform.api.PlatformApi;

import java.util.List;
import java.util.function.Supplier;
import java.util.stream.Stream;
import javax.annotation.Nullable;

import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.Component;
import net.minecraft.util.Mth;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;

public abstract class AbstractEnergyBlockItem extends BlockItem implements EnergyItem {
protected AbstractEnergyBlockItem(final Block block, final Properties properties) {
super(block, properties);
}

@Override
public void appendHoverText(
final ItemStack stack,
@Nullable final Level level,
final List<Component> lines,
final TooltipFlag flag
) {
super.appendHoverText(stack, level, lines, flag);
PlatformApi.INSTANCE.getEnergyStorage(stack).ifPresent(energyStorage -> {
final long stored = energyStorage.getStored();
final long capacity = energyStorage.getCapacity();
final double pct = stored / (double) capacity;
lines.add(PlatformApi.INSTANCE.createStoredWithCapacityTranslation(stored, capacity, pct)
.withStyle(ChatFormatting.GRAY));
});
}

@Override
public boolean isBarVisible(final ItemStack stack) {
return PlatformApi.INSTANCE.getEnergyStorage(stack).isPresent();
}

@Override
public int getBarWidth(final ItemStack stack) {
return PlatformApi.INSTANCE.getEnergyStorage(stack).map(energyStorage -> (int) Math.round(
(energyStorage.getStored() / (double) energyStorage.getCapacity()) * 13D
)).orElse(0);
}

@Override
public int getBarColor(final ItemStack stack) {
return PlatformApi.INSTANCE.getEnergyStorage(stack).map(energyStorage -> Mth.hsvToRgb(
Math.max(0.0F, (float) energyStorage.getStored() / (float) energyStorage.getCapacity()) / 3.0F,
1.0F,
1.0F
)).orElse(0);
}

public ItemStack createAtEnergyCapacity() {
final ItemStack stack = new ItemStack(this);
PlatformApi.INSTANCE.getEnergyStorage(stack).ifPresent(energyStorage -> energyStorage.receive(
energyStorage.getCapacity(),
Action.EXECUTE
));
return stack;
}

public static Stream<ItemStack> createAllAtEnergyCapacity(final List<Supplier<BlockItem>> items) {
return items.stream().map(Supplier::get)
.filter(AbstractEnergyBlockItem.class::isInstance)
.map(AbstractEnergyBlockItem.class::cast)
.map(AbstractEnergyBlockItem::createAtEnergyCapacity);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{
"functions": [
{
"function": "refinedstorage2:energy"
}
],
"pools": [
{
"bonus_rolls": 0.0,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{
"functions": [
{
"function": "refinedstorage2:energy"
}
],
"pools": [
{
"bonus_rolls": 0.0,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{
"functions": [
{
"function": "refinedstorage2:energy"
}
],
"pools": [
{
"bonus_rolls": 0.0,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{
"functions": [
{
"function": "refinedstorage2:energy"
}
],
"pools": [
{
"bonus_rolls": 0.0,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{
"functions": [
{
"function": "refinedstorage2:energy"
}
],
"pools": [
{
"bonus_rolls": 0.0,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{
"functions": [
{
"function": "refinedstorage2:energy"
}
],
"pools": [
{
"bonus_rolls": 0.0,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{
"functions": [
{
"function": "refinedstorage2:energy"
}
],
"pools": [
{
"bonus_rolls": 0.0,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{
"functions": [
{
"function": "refinedstorage2:energy"
}
],
"pools": [
{
"bonus_rolls": 0.0,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{
"functions": [
{
"function": "refinedstorage2:energy"
}
],
"pools": [
{
"bonus_rolls": 0.0,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{
"functions": [
{
"function": "refinedstorage2:energy"
}
],
"pools": [
{
"bonus_rolls": 0.0,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{
"functions": [
{
"function": "refinedstorage2:energy"
}
],
"pools": [
{
"bonus_rolls": 0.0,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{
"functions": [
{
"function": "refinedstorage2:energy"
}
],
"pools": [
{
"bonus_rolls": 0.0,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{
"functions": [
{
"function": "refinedstorage2:energy"
}
],
"pools": [
{
"bonus_rolls": 0.0,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{
"functions": [
{
"function": "refinedstorage2:energy"
}
],
"pools": [
{
"bonus_rolls": 0.0,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{
"functions": [
{
"function": "refinedstorage2:energy"
}
],
"pools": [
{
"bonus_rolls": 0.0,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{
"functions": [
{
"function": "refinedstorage2:energy"
}
],
"pools": [
{
"bonus_rolls": 0.0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.refinedmods.refinedstorage2.api.network.impl.component.StorageNetworkComponentImpl;
import com.refinedmods.refinedstorage2.platform.api.PlatformApi;
import com.refinedmods.refinedstorage2.platform.api.PlatformApiProxy;
import com.refinedmods.refinedstorage2.platform.common.block.AbstractStorageBlock;
import com.refinedmods.refinedstorage2.platform.common.block.ControllerType;
import com.refinedmods.refinedstorage2.platform.common.block.DiskDriveBlock;
import com.refinedmods.refinedstorage2.platform.common.block.FluidStorageBlock;
Expand Down Expand Up @@ -84,6 +83,8 @@
import com.refinedmods.refinedstorage2.platform.common.item.block.FluidStorageBlockBlockItem;
import com.refinedmods.refinedstorage2.platform.common.item.block.ItemStorageBlockBlockItem;
import com.refinedmods.refinedstorage2.platform.common.item.block.SimpleBlockItem;
import com.refinedmods.refinedstorage2.platform.common.loot.EnergyLootItemFunctionSerializer;
import com.refinedmods.refinedstorage2.platform.common.loot.StorageBlockLootItemFunctionSerializer;
import com.refinedmods.refinedstorage2.platform.common.recipe.UpgradeWithEnchantedBookRecipeSerializer;

import java.util.Optional;
Expand Down Expand Up @@ -603,7 +604,11 @@ protected final void registerMenus(final RegistryCallback<MenuType<?>> callback,
protected final void registerLootFunctions(final RegistryCallback<LootItemFunctionType> callback) {
LootFunctions.INSTANCE.setStorageBlock(callback.register(
STORAGE_BLOCK,
() -> new LootItemFunctionType(new AbstractStorageBlock.StorageBlockLootItemFunctionSerializer())
() -> new LootItemFunctionType(new StorageBlockLootItemFunctionSerializer())
));
LootFunctions.INSTANCE.setEnergy(callback.register(
createIdentifier("energy"),
() -> new LootItemFunctionType(new EnergyLootItemFunctionSerializer())
));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,19 @@
package com.refinedmods.refinedstorage2.platform.common.block;

import com.refinedmods.refinedstorage2.platform.api.PlatformApi;
import com.refinedmods.refinedstorage2.platform.common.block.entity.storage.AbstractStorageBlockBlockEntity;
import com.refinedmods.refinedstorage2.platform.common.block.ticker.AbstractBlockEntityTicker;
import com.refinedmods.refinedstorage2.platform.common.content.LootFunctions;

import java.util.UUID;
import javax.annotation.Nullable;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.EntityBlock;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityTicker;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.world.level.storage.loot.Serializer;
import net.minecraft.world.level.storage.loot.functions.LootItemFunction;
import net.minecraft.world.level.storage.loot.functions.LootItemFunctionType;
import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class AbstractStorageBlock<T extends AbstractStorageBlockBlockEntity<?>>
extends AbstractBaseBlock
public abstract class AbstractStorageBlock<T extends AbstractStorageBlockBlockEntity<?>> extends AbstractBaseBlock
implements EntityBlock {
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractStorageBlock.class);

private final AbstractBlockEntityTicker<T> ticker;

protected AbstractStorageBlock(final Properties properties, final AbstractBlockEntityTicker<T> ticker) {
Expand All @@ -45,45 +28,4 @@ public <O extends BlockEntity> BlockEntityTicker<O> getTicker(final Level level,
final BlockEntityType<O> type) {
return ticker.get(level, type);
}

public static class StorageBlockLootItemFunction implements LootItemFunction {
@Override
public LootItemFunctionType getType() {
return LootFunctions.INSTANCE.getStorageBlock();
}

@Override
public ItemStack apply(final ItemStack stack, final LootContext lootContext) {
final BlockEntity blockEntity = lootContext.getParam(LootContextParams.BLOCK_ENTITY);
if (blockEntity instanceof AbstractStorageBlockBlockEntity<?> storageBlockEntity) {
apply(stack, storageBlockEntity);
}
return stack;
}

private void apply(final ItemStack stack, final AbstractStorageBlockBlockEntity<?> storageBlockEntity) {
final UUID storageId = storageBlockEntity.getStorageId();
if (storageId != null) {
LOGGER.debug("Transferred storage {} at {} to stack", storageId, storageBlockEntity.getBlockPos());
PlatformApi.INSTANCE.getStorageContainerItemHelper().setId(stack, storageId);
} else {
LOGGER.warn("Storage block {} has no associated storage ID!", storageBlockEntity.getBlockPos());
}
}
}

public static class StorageBlockLootItemFunctionSerializer implements Serializer<LootItemFunction> {
@Override
public void serialize(final JsonObject jsonObject,
final LootItemFunction lootItemFunction,
final JsonSerializationContext jsonSerializationContext) {
// nothing to do
}

@Override
public LootItemFunction deserialize(final JsonObject jsonObject,
final JsonDeserializationContext jsonDeserializationContext) {
return new StorageBlockLootItemFunction();
}
}
}
Loading