Skip to content

Commit

Permalink
Add "Alchemical Mastery Card"
Browse files Browse the repository at this point in the history
  • Loading branch information
62832 committed May 6, 2024
1 parent 6856515 commit 172baa6
Show file tree
Hide file tree
Showing 16 changed files with 141 additions and 39 deletions.
7 changes: 7 additions & 0 deletions src/main/java/gripe/_90/appliede/AppliedE.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public final class AppliedE {
public static final RegistryObject<Item> EMC_IMPORT_BUS = ITEMS.register("emc_import_bus", () -> part(EMCImportBusPart.class, EMCImportBusPart::new));

public static final RegistryObject<Item> TRANSMUTATION_TERMINAL = ITEMS.register("transmutation_terminal", () -> part(TransmutationTerminalPart.class, TransmutationTerminalPart::new));
public static final RegistryObject<Item> LEARNING_CARD = ITEMS.register("learning_card", () -> Upgrades.createUpgradeCardItem(new Item.Properties()));

static {
ITEMS.register("dummy_emc_item", () -> new Item(new Item.Properties()));
Expand All @@ -145,6 +146,7 @@ public final class AppliedE {
output.accept(EMC_EXPORT_BUS.get());
output.accept(EMC_IMPORT_BUS.get());
output.accept(TRANSMUTATION_TERMINAL.get());
output.accept(LEARNING_CARD.get());
})
.build());
}
Expand Down Expand Up @@ -179,6 +181,11 @@ public AppliedE() {
Upgrades.add(AEItems.SPEED_CARD, EMC_IMPORT_BUS.get(), 4, busesGroup);
Upgrades.add(AEItems.INVERTER_CARD, EMC_IMPORT_BUS.get(), 1, busesGroup);

var emcInterfaceGroup = EMC_INTERFACE.get().getDescriptionId();
Upgrades.add(LEARNING_CARD.get(), EMC_INTERFACE.get(), 1, emcInterfaceGroup);
Upgrades.add(LEARNING_CARD.get(), CABLE_EMC_INTERFACE.get(), 1, emcInterfaceGroup);
Upgrades.add(LEARNING_CARD.get(), EMC_IMPORT_BUS.get(), 1);

registerEMC(AEItems.CERTUS_QUARTZ_CRYSTAL, 256);
registerEMC(AEBlocks.SKY_STONE_BLOCK, 256);
registerEMC(AEItems.CALCULATION_PROCESSOR_PRESS, 2048);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public EMCInterfaceBlockEntity(BlockEntityType<?> type, BlockPos pos, BlockState
}

protected EMCInterfaceLogic createLogic() {
return new EMCInterfaceLogic(getMainNode(), this);
return new EMCInterfaceLogic(getMainNode(), this, getItemFromBlockEntity());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.player.Inventory;

import appeng.client.gui.AEBaseScreen;
import appeng.client.gui.Icon;
import appeng.client.gui.implementations.UpgradeableScreen;
import appeng.client.gui.style.ScreenStyle;
import appeng.client.gui.widgets.IconButton;
import appeng.core.localization.ButtonToolTips;
import appeng.menu.SlotSemantics;

import gripe._90.appliede.menu.EMCInterfaceMenu;

public class EMCInterfaceScreen<M extends EMCInterfaceMenu> extends AEBaseScreen<M> {
public class EMCInterfaceScreen<M extends EMCInterfaceMenu> extends UpgradeableScreen<M> {
private final List<Button> amountButtons = new ArrayList<>();

public EMCInterfaceScreen(M menu, Inventory playerInventory, Component title, ScreenStyle style) {
Expand Down
50 changes: 38 additions & 12 deletions src/main/java/gripe/_90/appliede/me/misc/EMCInterfaceLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.jetbrains.annotations.Nullable;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ForgeCapabilities;
Expand All @@ -26,40 +27,50 @@
import appeng.api.stacks.GenericStack;
import appeng.api.storage.AEKeyFilter;
import appeng.api.storage.MEStorage;
import appeng.api.upgrades.IUpgradeInventory;
import appeng.api.upgrades.IUpgradeableObject;
import appeng.api.upgrades.UpgradeInventories;
import appeng.capabilities.Capabilities;
import appeng.helpers.externalstorage.GenericStackItemStorage;
import appeng.me.storage.DelegatingMEInventory;
import appeng.util.ConfigInventory;
import appeng.util.Platform;

import gripe._90.appliede.AppliedE;
import gripe._90.appliede.me.service.KnowledgeService;

public class EMCInterfaceLogic implements IActionHost, IGridTickable {
import moze_intel.projecte.api.proxy.IEMCProxy;

public class EMCInterfaceLogic implements IActionHost, IGridTickable, IUpgradeableObject {
protected final EMCInterfaceLogicHost host;
protected final IManagedGridNode mainNode;

private final ConfigInventory config;
private final ConfigInventory storage;
private final IUpgradeInventory upgrades;

private final MEStorage localInvHandler;
private final GenericStack[] plannedWork;
private final IActionSource source = IActionSource.ofMachine(this);

private final LazyOptional<IItemHandler> storageHolder;
private final LazyOptional<MEStorage> localInvHolder;

public EMCInterfaceLogic(IManagedGridNode node, EMCInterfaceLogicHost host) {
this(node, host, 9);
public EMCInterfaceLogic(IManagedGridNode node, EMCInterfaceLogicHost host, Item is) {
this(node, host, is, 9);
}

@SuppressWarnings("UnstableApiUsage")
public EMCInterfaceLogic(IManagedGridNode node, EMCInterfaceLogicHost host, int slots) {
public EMCInterfaceLogic(IManagedGridNode node, EMCInterfaceLogicHost host, Item is, int slots) {
this.host = host;
mainNode = node.setFlags(GridFlags.REQUIRE_CHANNEL)
.addService(IGridTickable.class, this)
.setIdlePowerUsage(10);

config = ConfigInventory.configStacks(AEItemKey.filter(), slots, this::onConfigRowChanged, false);
storage = ConfigInventory.storage(new StorageFilter(), slots, this::onStorageChanged);
upgrades = UpgradeInventories.forMachine(is, 1, this::onStorageChanged);

localInvHandler = new DelegatingMEInventory(storage);
plannedWork = new GenericStack[slots];

Expand All @@ -78,9 +89,15 @@ public ConfigInventory getStorage() {
return storage;
}

@Override
public IUpgradeInventory getUpgrades() {
return upgrades;
}

public void readFromNBT(CompoundTag tag) {
config.readFromChildTag(tag, "config");
storage.readFromChildTag(tag, "storage");
upgrades.readFromNBT(tag, "upgrades");

updatePlan();
notifyNeighbours();
Expand All @@ -89,6 +106,7 @@ public void readFromNBT(CompoundTag tag) {
public void writeToNBT(CompoundTag tag) {
config.writeToChildTag(tag, "config");
storage.writeToChildTag(tag, "storage");
upgrades.writeToNBT(tag, "upgrades");
}

@Nullable
Expand Down Expand Up @@ -189,12 +207,6 @@ private boolean tryUsePlan(int slot, AEKey what, int amount) {
return false;
}

var knowledge = grid.getService(KnowledgeService.class);

if (!knowledge.knowsItem(item)) {
return false;
}

if (amount < 0) {
amount = -amount;
var inSlot = storage.getStack(slot);
Expand All @@ -205,7 +217,12 @@ private boolean tryUsePlan(int slot, AEKey what, int amount) {

var depositedItems = grid.getService(KnowledgeService.class)
.getStorage()
.insertItem(item, amount, Actionable.MODULATE, source, false);
.insertItem(
item,
amount,
Actionable.MODULATE,
source,
upgrades.isInstalled(AppliedE.LEARNING_CARD.get()));

if (depositedItems > 0) {
storage.extract(slot, what, depositedItems, Actionable.MODULATE);
Expand Down Expand Up @@ -281,6 +298,12 @@ public void addDrops(List<ItemStack> drops) {
host.getBlockEntity().getBlockPos());
}
}

for (var is : this.upgrades) {
if (!is.isEmpty()) {
drops.add(is);
}
}
}

public <T> LazyOptional<T> getCapability(Capability<T> cap) {
Expand All @@ -306,7 +329,10 @@ public boolean matches(AEKey what) {
}

var grid = mainNode.getGrid();
return grid == null || grid.getService(KnowledgeService.class).knowsItem(item);
return grid == null
|| grid.getService(KnowledgeService.class).knowsItem(item)
|| (upgrades.isInstalled(AppliedE.LEARNING_CARD.get())
&& IEMCProxy.INSTANCE.hasValue(item.toStack()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import appeng.api.networking.IGridNode;
import appeng.api.networking.IGridNodeListener;
import appeng.api.storage.ISubMenuHost;
import appeng.api.upgrades.IUpgradeInventory;
import appeng.api.upgrades.IUpgradeableObject;
import appeng.helpers.IConfigInvHost;
import appeng.helpers.externalstorage.GenericStackInv;
import appeng.menu.ISubMenu;
Expand All @@ -14,7 +16,7 @@

import gripe._90.appliede.menu.EMCInterfaceMenu;

public interface EMCInterfaceLogicHost extends IConfigInvHost, ISubMenuHost {
public interface EMCInterfaceLogicHost extends IConfigInvHost, ISubMenuHost, IUpgradeableObject {
IGridNodeListener<EMCInterfaceLogicHost> NODE_LISTENER = new IGridNodeListener<>() {
@Override
public void onSaveChanges(EMCInterfaceLogicHost host, IGridNode node) {
Expand Down Expand Up @@ -49,6 +51,11 @@ default GenericStackInv getStorage() {
return getInterfaceLogic().getStorage();
}

@Override
default IUpgradeInventory getUpgrades() {
return getInterfaceLogic().getUpgrades();
}

default void openMenu(Player player, MenuLocator locator) {
MenuOpener.open(EMCInterfaceMenu.TYPE, player, locator);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class EMCTransferContext implements StackTransferContext {

private int operationsRemaining;
private boolean isInverted;
private boolean canLearn;

public EMCTransferContext(
EMCStorage emcStorage, IActionSource actionSource, IPartitionList filter, int operationsRemaining) {
Expand Down Expand Up @@ -97,6 +98,14 @@ public boolean isInverted() {
return isInverted;
}

public void setCanLearn(boolean learn) {
canLearn = learn;
}

public boolean canLearn() {
return canLearn;
}

@Override
public boolean canInsert(AEItemKey what, long amount) {
return true;
Expand Down
44 changes: 36 additions & 8 deletions src/main/java/gripe/_90/appliede/me/service/EMCStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Objects;

import com.google.common.primitives.Ints;

import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Player;

import appeng.api.config.Actionable;
import appeng.api.config.PowerMultiplier;
import appeng.api.features.IPlayerRegistry;
import appeng.api.networking.energy.IEnergySource;
import appeng.api.networking.security.IActionSource;
import appeng.api.stacks.AEItemKey;
Expand All @@ -23,6 +26,7 @@
import gripe._90.appliede.me.key.EMCKey;

import moze_intel.projecte.api.ItemInfo;
import moze_intel.projecte.api.capabilities.IKnowledgeProvider;
import moze_intel.projecte.api.proxy.IEMCProxy;

public class EMCStorage implements MEStorage {
Expand Down Expand Up @@ -146,9 +150,21 @@ public long insertItem(AEItemKey what, long amount, Actionable mode, IActionSour
}

var playerProvider = source.player().map(service::getProviderFor).orElse(null);
var machineOwnerProvider = source.machine()
.map(host -> {
var node = host.getActionableNode();
return node != null ? service.getProviderFor(node.getOwningPlayerProfileId()) : null;
})
.orElse(null);

if (mayLearn) {
if (source.player().isPresent() && playerProvider == null) {
return 0;
}

if (mayLearn && source.player().isPresent() && playerProvider == null) {
return 0;
if (source.machine().isPresent() && machineOwnerProvider == null) {
return 0;
}
}

var grid = service.getGrid();
Expand Down Expand Up @@ -185,12 +201,15 @@ public long insertItem(AEItemKey what, long amount, Actionable mode, IActionSour
if (mode == Actionable.MODULATE && mayLearn) {
source.player().ifPresent(player -> {
if (playerProvider != null) {
var stack = what.toStack();
playerProvider.get().addKnowledge(stack);

if (player instanceof ServerPlayer serverPlayer) {
playerProvider.get().syncKnowledgeChange(serverPlayer, ItemInfo.fromStack(stack), true);
}
addKnowledge(what, playerProvider.get(), player);
}
});
source.machine().ifPresent(host -> {
if (machineOwnerProvider != null) {
var node = Objects.requireNonNull(host.getActionableNode());
var id = node.getOwningPlayerId();
var player = IPlayerRegistry.getConnected(node.getLevel().getServer(), id);
addKnowledge(what, machineOwnerProvider.get(), player);
}
});
}
Expand Down Expand Up @@ -247,6 +266,15 @@ public long learnNewItem(AEItemKey what, ServerPlayer player) {
: 0;
}

private void addKnowledge(AEItemKey what, IKnowledgeProvider provider, Player player) {
var stack = what.toStack();
provider.addKnowledge(stack);

if (player instanceof ServerPlayer serverPlayer) {
provider.syncKnowledgeChange(serverPlayer, ItemInfo.fromStack(stack), true);
}
}

private long getAmountAfterPowerExpenditure(long maxAmount, IEnergySource source) {
var requiredPower = PowerMultiplier.CONFIG.multiply(maxAmount);
var availablePower = source.extractAEPower(requiredPower, Actionable.SIMULATE, PowerMultiplier.CONFIG);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,12 @@ Set<IKnowledgeProvider> getProviders() {
return providers.values().stream().map(Supplier::get).collect(Collectors.toUnmodifiableSet());
}

Supplier<IKnowledgeProvider> getProviderFor(UUID uuid) {
return providers.get(uuid);
}

Supplier<IKnowledgeProvider> getProviderFor(Player player) {
return providers.get(player.getUUID());
return getProviderFor(player.getUUID());
}

public EMCStorage getStorage() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public EMCItemImportStrategy(ServerLevel level, BlockPos fromPos, Direction from

@Override
public boolean transfer(StackTransferContext context) {
if (!(context instanceof EMCTransferContext emcContext)) {
if (!(context instanceof EMCTransferContext emc)) {
return false;
}

Expand All @@ -38,8 +38,7 @@ public boolean transfer(StackTransferContext context) {
}

var adjacentStorage = ExternalStorageFacade.of(itemHandler);
var remaining = emcContext.getOperationsRemaining();
var emc = emcContext.getEmcStorage();
var remaining = emc.getOperationsRemaining();

for (var i = 0; i < adjacentStorage.getSlots() && remaining > 0; i++) {
var resource = adjacentStorage.getStackInSlot(i);
Expand All @@ -55,7 +54,8 @@ public boolean transfer(StackTransferContext context) {
var amount = adjacentStorage.extract(item, remaining, Actionable.SIMULATE, context.getActionSource());

if (amount > 0) {
var inserted = emc.insertItem(item, amount, Actionable.MODULATE, context.getActionSource(), false);
var inserted = emc.getEmcStorage()
.insertItem(item, amount, Actionable.MODULATE, context.getActionSource(), emc.canLearn());
adjacentStorage.extract(item, inserted, Actionable.MODULATE, context.getActionSource());
context.reduceOperationsRemaining(inserted);
remaining -= (int) inserted;
Expand Down
Loading

0 comments on commit 172baa6

Please sign in to comment.