Skip to content

Commit

Permalink
Allow players to directly extract EMC-able items via terminals
Browse files Browse the repository at this point in the history
  • Loading branch information
62832 committed Apr 29, 2024
1 parent 5c444a8 commit e3beaaa
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 33 deletions.
4 changes: 4 additions & 0 deletions src/main/java/gripe/_90/appliede/AppliedE.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ public static ResourceLocation id(String path) {
return new ResourceLocation(MODID, path);
}

public static long clampedLong(BigInteger toClamp) {
return toClamp.min(BigInteger.valueOf(Long.MAX_VALUE)).longValue();
}

private static <P extends IPart> Item part(Class<P> partClass, Function<IPartItem<P>, P> factory) {
PartModels.registerModels(PartModelsHelper.createModels(partClass));
return new PartItem<>(new Item.Properties(), partClass, factory);
Expand Down
34 changes: 4 additions & 30 deletions src/main/java/gripe/_90/appliede/iface/EMCInterfaceLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -265,39 +265,13 @@ private boolean tryUsePlan(int slot, AEKey what, int amount) {
}

private boolean acquireFromNetwork(IGrid grid, int slot, AEKey what, long amount) {
if (!(what instanceof AEItemKey itemKey)) {
if (!(what instanceof AEItemKey item)) {
return false;
}

var emcStorage = grid.getService(KnowledgeService.class).getStorage();
var energy = grid.getEnergyService();

var itemEmc = BigInteger.valueOf(IEMCProxy.INSTANCE.getValue(itemKey.getItem()));
var totalEmc = itemEmc.multiply(BigInteger.valueOf(amount));
var acquiredItems = 0L;

while (totalEmc.compareTo(BigInteger.ZERO) > 0) {
var toWithdraw = clampedLong(totalEmc);
var canWithdraw = emcStorage.extract(EMCKey.base(), toWithdraw, Actionable.SIMULATE, requestSource);

if (canWithdraw < toWithdraw) {
break;
}

var energyToExpend = PowerMultiplier.CONFIG.multiply(toWithdraw);
var availablePower = energy.extractAEPower(energyToExpend, Actionable.SIMULATE, PowerMultiplier.CONFIG);

if (availablePower < energyToExpend) {
break;
}

energy.extractAEPower(energyToExpend, Actionable.MODULATE, PowerMultiplier.CONFIG);
emcStorage.extract(EMCKey.base(), toWithdraw, Actionable.MODULATE, requestSource);

var withdrawn = BigInteger.valueOf(toWithdraw);
acquiredItems += withdrawn.divide(itemEmc).longValue();
totalEmc = totalEmc.subtract(withdrawn).add(withdrawn.remainder(itemEmc));
}
var acquiredItems = grid.getService(KnowledgeService.class)
.getStorage()
.extractItem(item, amount, Actionable.MODULATE, requestSource);

if (acquiredItems > 0) {
var inserted = storage.insert(slot, what, acquiredItems, Actionable.MODULATE);
Expand Down
58 changes: 57 additions & 1 deletion src/main/java/gripe/_90/appliede/service/EMCStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@
import net.minecraft.network.chat.Component;

import appeng.api.config.Actionable;
import appeng.api.config.PowerMultiplier;
import appeng.api.networking.security.IActionSource;
import appeng.api.stacks.AEItemKey;
import appeng.api.stacks.AEKey;
import appeng.api.stacks.KeyCounter;
import appeng.api.storage.MEStorage;

import gripe._90.appliede.AppliedE;
import gripe._90.appliede.key.EMCKey;

import moze_intel.projecte.api.proxy.IEMCProxy;

public record EMCStorage(KnowledgeService service) implements MEStorage {
@Override
public void getAvailableStacks(KeyCounter out) {
Expand Down Expand Up @@ -58,7 +62,15 @@ public long insert(AEKey what, long amount, Actionable mode, IActionSource sourc

@Override
public long extract(AEKey what, long amount, Actionable mode, IActionSource source) {
if (amount == 0 || !(what instanceof EMCKey emc)) {
if (amount == 0) {
return 0;
}

if (what instanceof AEItemKey item && source.player().isPresent()) {
return extractItem(item, amount, mode, source);
}

if (!(what instanceof EMCKey emc)) {
return 0;
}

Expand Down Expand Up @@ -106,6 +118,50 @@ public long extract(AEKey what, long amount, Actionable mode, IActionSource sour
return extracted;
}

public long extractItem(AEItemKey what, long amount, Actionable mode, IActionSource source) {
var grid = service.getGrid();

if (grid == null) {
return 0;
}

var energy = grid.getEnergyService();
var itemEmc = BigInteger.valueOf(IEMCProxy.INSTANCE.getValue(what.getItem()));
var totalEmc = itemEmc.multiply(BigInteger.valueOf(amount));
var acquiredItems = 0L;

while (totalEmc.compareTo(BigInteger.ZERO) > 0) {
var toWithdraw = AppliedE.clampedLong(totalEmc);
var canWithdraw = extract(EMCKey.base(), toWithdraw, Actionable.SIMULATE, source);

if (canWithdraw < toWithdraw) {
break;
}

var energyToExpend = PowerMultiplier.CONFIG.multiply(toWithdraw);
var availablePower = energy.extractAEPower(energyToExpend, Actionable.SIMULATE, PowerMultiplier.CONFIG);

if (availablePower < energyToExpend) {
break;
}

BigInteger withdrawn;

if (mode == Actionable.MODULATE) {
energy.extractAEPower(energyToExpend, Actionable.MODULATE, PowerMultiplier.CONFIG);
extract(EMCKey.base(), toWithdraw, Actionable.MODULATE, source);
withdrawn = BigInteger.valueOf(toWithdraw);
} else {
withdrawn = BigInteger.valueOf(canWithdraw);
}

acquiredItems += withdrawn.divide(itemEmc).longValue();
totalEmc = totalEmc.subtract(withdrawn).add(withdrawn.remainder(itemEmc));
}

return acquiredItems;
}

@Override
public Component getDescription() {
return AppliedE.EMC_MODULE.get().getDescription();
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/gripe/_90/appliede/service/KnowledgeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import appeng.api.crafting.IPatternDetails;
import appeng.api.features.IPlayerRegistry;
import appeng.api.networking.IGrid;
import appeng.api.networking.IGridNode;
import appeng.api.networking.IGridService;
import appeng.api.networking.IGridServiceProvider;
Expand All @@ -37,9 +38,10 @@
public class KnowledgeService implements IGridService, IGridServiceProvider {
private final List<EMCModulePart> modules = new ArrayList<>();
private final Map<UUID, Supplier<IKnowledgeProvider>> providers = new HashMap<>();
private final MEStorage storage = new EMCStorage(this);
private final EMCStorage storage = new EMCStorage(this);

private MinecraftServer server;
private IGrid grid;

public KnowledgeService() {
MinecraftForge.EVENT_BUS.addListener((PlayerKnowledgeChangeEvent event) -> {
Expand All @@ -55,6 +57,10 @@ public void addNode(IGridNode gridNode, @Nullable CompoundTag savedData) {
server = gridNode.getLevel().getServer();
}

if (grid == null) {
grid = gridNode.getGrid();
}

if (gridNode.getOwner() instanceof EMCModulePart module) {
modules.add(module);
var uuid = gridNode.getOwningPlayerProfileId();
Expand All @@ -81,7 +87,7 @@ Set<Supplier<IKnowledgeProvider>> getProviders() {
return providers.values().stream().collect(Collectors.toUnmodifiableSet());
}

public MEStorage getStorage() {
public EMCStorage getStorage() {
return storage;
}

Expand Down Expand Up @@ -115,6 +121,11 @@ public List<IPatternDetails> getPatterns() {
return patterns;
}

@Nullable
public IGrid getGrid() {
return grid;
}

public BigInteger getEmc() {
return getProviders().stream()
.map(provider -> provider.get().getEmc())
Expand Down

0 comments on commit e3beaaa

Please sign in to comment.