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

Expose ManaItem on portable cells #52

Merged
merged 4 commits into from
Sep 27, 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
2 changes: 1 addition & 1 deletion common/src/main/java/appbot/AppliedBotanics.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ static ResourceLocation id(String path) {

@ExpectPlatform
static AppliedBotanics getInstance() {
throw new AssertionError();
return getInstance();
}

Lookup<IStorageMonitorableAccessor, Direction> meStorage(ServerLevel level, BlockPos pos);
Expand Down
103 changes: 103 additions & 0 deletions common/src/main/java/appbot/ae2/MEStorageManaItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package appbot.ae2;

import com.google.common.primitives.Ints;

import org.jetbrains.annotations.Nullable;

import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.entity.BlockEntity;

import vazkii.botania.api.mana.ManaItem;

import appeng.api.config.Actionable;
import appeng.api.networking.energy.IEnergySource;
import appeng.api.networking.security.IActionSource;
import appeng.api.storage.MEStorage;
import appeng.api.storage.StorageCells;
import appeng.api.storage.StorageHelper;
import appeng.items.tools.powered.AbstractPortableCell;

public class MEStorageManaItem implements ManaItem {

private final MEStorage storage;
private final IEnergySource energy;
private final IActionSource source;

public MEStorageManaItem(MEStorage storage, IEnergySource energy, IActionSource source) {
this.storage = storage;
this.energy = energy;
this.source = source;
}

@Nullable
public static ManaItem forItem(ItemStack stack) {
if (stack.getItem()instanceof AbstractPortableCell item) {
var storage = StorageCells.getCellInventory(stack, null);

if (storage == null) {
return null;
}

return new MEStorageManaItem(storage, (amount, mode, multiplier) -> {
amount = multiplier.multiply(amount);

if (mode == Actionable.SIMULATE) {
return multiplier.divide(Math.min(amount, item.getAECurrentPower(stack)));
}

return multiplier.divide(item.extractAEPower(stack, amount, Actionable.MODULATE));
}, IActionSource.empty());
}

// we could also add wireless terminal support, but no player
return null;
}

@Override
public int getMana() {
return (int) StorageHelper.poweredExtraction(energy, storage, ManaKey.KEY, Integer.MAX_VALUE, source,
Actionable.SIMULATE);
}

@Override
public int getMaxMana() {
return Ints.saturatedCast(StorageHelper.poweredExtraction(energy, storage, ManaKey.KEY, Integer.MAX_VALUE,
source, Actionable.SIMULATE)
+ StorageHelper.poweredInsert(energy, storage, ManaKey.KEY, Integer.MAX_VALUE, source,
Actionable.SIMULATE));
}

@Override
public void addMana(int mana) {
if (mana > 0) {
StorageHelper.poweredInsert(energy, storage, ManaKey.KEY, mana, source);
} else {
StorageHelper.poweredExtraction(energy, storage, ManaKey.KEY, -mana, source);
}
}

@Override
public boolean canReceiveManaFromPool(BlockEntity pool) {
return true;
}

@Override
public boolean canReceiveManaFromItem(ItemStack otherStack) {
return true;
}

@Override
public boolean canExportManaToPool(BlockEntity pool) {
return true;
}

@Override
public boolean canExportManaToItem(ItemStack otherStack) {
return true;
}

@Override
public boolean isNoExport() {
return false;
}
}
28 changes: 4 additions & 24 deletions common/src/main/java/appbot/item/cell/ManaCellInventory.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package appbot.item.cell;

import org.jetbrains.annotations.Nullable;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.Tag;
import net.minecraft.network.chat.Component;
import net.minecraft.world.item.ItemStack;

Expand All @@ -22,38 +23,17 @@ public class ManaCellInventory implements StorageCell {

private final IManaCellItem cellType;
private final ItemStack i;
@Nullable
private final ISaveProvider container;

private long storedMana;
private boolean isPersisted = true;

public ManaCellInventory(IManaCellItem cellType, ItemStack o, ISaveProvider container) {
public ManaCellInventory(IManaCellItem cellType, ItemStack o, @Nullable ISaveProvider container) {
this.cellType = cellType;
this.i = o;
this.container = container;

this.storedMana = getTag().getLong(AMOUNT);

// Only migration for <=1.19.2 releases
var ITEM_COUNT_TAG = "ic";
var STACK_KEYS = "keys";
var STACK_AMOUNTS = "amts";

if (getTag().contains(ITEM_COUNT_TAG)) {
var amounts = getTag().getLongArray(STACK_AMOUNTS);
var tags = getTag().getList(STACK_KEYS, Tag.TAG_COMPOUND);

for (var i = 0; i < amounts.length; i++) {
if (AEKey.fromTagGeneric(tags.getCompound(i)) == ManaKey.KEY) {
this.storedMana += amounts[i];
}
}

getTag().remove(ITEM_COUNT_TAG);
getTag().remove(STACK_KEYS);
getTag().remove(STACK_AMOUNTS);
saveChanges();
}
}

private CompoundTag getTag() {
Expand Down
6 changes: 2 additions & 4 deletions fabric/src/main/java/appbot/fabric/ABItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,8 @@ public void openChestGui(Player player, IChestOrDrive chest, ICellHandler cellHa

Upgrades.add(AEItems.ENERGY_CARD, portable, 2, GuiText.PortableCells.getTranslationKey());

String path1 = "block/drive/cells/" + Registry.ITEM.getKey(cell).getPath();
StorageCellModels.registerModel(cell, id(path1));
String path = "block/drive/cells/" + Registry.ITEM.getKey(cell).getPath();
StorageCellModels.registerModel(portable, id(path));
StorageCellModels.registerModel(cell, id("block/drive/cells/" + Registry.ITEM.getKey(cell).getPath()));
StorageCellModels.registerModel(portable, id("block/drive/cells/" + Registry.ITEM.getKey(cell).getPath()));
}
}

Expand Down
4 changes: 4 additions & 0 deletions fabric/src/main/java/appbot/fabric/AppliedBotanicsFabric.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,9 @@ static void initialize() {
});
}
});

BotaniaFabricCapabilities.MANA_ITEM.registerFallback((stack, context) -> {
return MEStorageManaItem.forItem(stack);
});
}
}
18 changes: 18 additions & 0 deletions forge/src/main/java/appbot/forge/AppliedBotanicsForge.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import net.minecraft.core.Direction;
import net.minecraft.core.Registry;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.common.MinecraftForge;
Expand Down Expand Up @@ -84,6 +85,23 @@ public <T> LazyOptional<T> getCapability(@NotNull Capability<T> capability, @Nul
}
});
});
MinecraftForge.EVENT_BUS.addGenericListener(ItemStack.class, (AttachCapabilitiesEvent<ItemStack> event) -> {
var item = MEStorageManaItem.forItem(event.getObject());

if (item != null) {
event.addCapability(AppliedBotanics.id("mana_item"), new ICapabilityProvider() {
@Override
public @NotNull <T> LazyOptional<T> getCapability(@NotNull Capability<T> capability,
@Nullable Direction arg) {
if (capability == BotaniaForgeCapabilities.MANA_ITEM) {
return LazyOptional.of(() -> item).cast();
}

return LazyOptional.empty();
}
});
}
});

StackWorldBehaviors.registerImportStrategy(ManaKeyType.TYPE, ManaStorageImportStrategy::new);
StackWorldBehaviors.registerExportStrategy(ManaKeyType.TYPE, ManaStorageExportStrategy::new);
Expand Down