Skip to content
Open
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
@@ -1,10 +1,12 @@
package com.wintercogs.beyonddimensions.api.dimensionnet;

import com.wintercogs.beyonddimensions.api.dimensionnet.helper.UnifiedStorageBeforeExtractHandler;
import com.wintercogs.beyonddimensions.api.dimensionnet.helper.UnifiedStorageBeforeInsertHandler;
import com.wintercogs.beyonddimensions.api.storage.handler.impl.UnorderedStackHandlerRemoveZero;
import com.wintercogs.beyonddimensions.api.storage.key.IStackKey;
import com.wintercogs.beyonddimensions.api.storage.key.KeyAmount;
import com.wintercogs.beyonddimensions.api.storage.key.impl.EmptyStackKey;
import net.minecraft.tags.TagKey;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;
Expand Down Expand Up @@ -92,4 +94,45 @@ public void onChange()
return super.insert(adjusted.key(), adjusted.amount(), simulate);
}

@Override
public @NotNull KeyAmount extract(int slot, long amount, boolean simulate)
{
return handleExtract(getStackBySlot(slot), amount, simulate, false);
}

@Override
public @NotNull KeyAmount extract(IStackKey<?> key, long amount, boolean simulate, boolean fuzzy)
{
return handleExtract(new KeyAmount(Objects.requireNonNullElse(key, EmptyStackKey.INSTANCE), amount), amount, simulate, fuzzy);
}

@Override
public @NotNull KeyAmount extract(TagKey<?> tagKey, long amount, boolean simulate)
{
KeyAmount extracted = super.extract(tagKey, amount, true);
return handleExtract(extracted, amount, simulate, false);
}

private @NotNull KeyAmount handleExtract(KeyAmount requested, long amount, boolean simulate, boolean fuzzy)
{
if (amount <= 0L || requested.isEmpty())
return new KeyAmount(EmptyStackKey.INSTANCE, 0L);

KeyAmount available = super.extract(requested.key(), amount, true, fuzzy);
var info = UnifiedStorageBeforeExtractHandler.onBeforeExtract(available, net);

if (info.cancel())
return new KeyAmount(EmptyStackKey.INSTANCE, 0L);

KeyAmount adjusted = info.beforeExtract();
if (adjusted.isEmpty())
return adjusted;

long extractedAmount = Math.min(available.amount(), adjusted.amount());
if (!simulate)
super.extract(available.key(), extractedAmount, false, false);

return new KeyAmount(adjusted.key(), extractedAmount);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.wintercogs.beyonddimensions.api.dimensionnet.helper;

import com.wintercogs.beyonddimensions.api.dimensionnet.DimensionsNet;
import com.wintercogs.beyonddimensions.api.storage.key.KeyAmount;
import com.wintercogs.beyonddimensions.api.storage.key.impl.EmptyStackKey;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

/**
* 一些辅助工具,让UnifiedStorage可以在提取操作真实执行前对即将离开网络的堆叠进行调整
*/
public final class UnifiedStorageBeforeExtractHandler
{
private static final List<BeforeExtractHandler> handlers = new ArrayList<>();

@FunctionalInterface
public interface BeforeExtractHandler
{
/**
* @param originalExtract 本次提取最原始的堆叠
* @param tryExtract 当前调用链上传递的堆叠
* @param net 网络信息,可为空
*/
@NotNull
BeforeExtractHandlerReturnInfo beforeExtract(
@NotNull KeyAmount originalExtract,
@NotNull KeyAmount tryExtract,
@Nullable DimensionsNet net
);
}

public record BeforeExtractHandlerReturnInfo(@NotNull KeyAmount beforeExtract, boolean cancel)
{
boolean isEmpty()
{
return beforeExtract.isEmpty();
}

boolean isCanceled()
{
return cancel;
}
}

/**
* 调用此函数以添加处理
*/
public static void addHandler(BeforeExtractHandler handler)
{
handlers.add(handler);
}

/**
* @param tryExtract 本次尝试提取的原始堆叠
* @param net 携带的网络信息,可为空
* @return 最终离开维度网络的堆叠
* <p>
* 会对handlers表进行链式调用,每一次处理完的extract会被传递给下一次调用。
* 如果cancel,则网络不会移除或输出任何内容;否则网络最多移除原始可提取数量,
* 并输出最后一次处理得到的堆叠。处理器可以减少输出数量或替换输出类型,但不能增加实际提取数量。
*/
@NotNull
public static BeforeExtractHandlerReturnInfo onBeforeExtract(@Nullable KeyAmount tryExtract, @Nullable DimensionsNet net)
{
final KeyAmount original = (tryExtract == null)
? new KeyAmount(EmptyStackKey.INSTANCE, 0)
: tryExtract;

if (tryExtract == null)
return new BeforeExtractHandlerReturnInfo(original, true);

if (tryExtract.isEmpty())
return new BeforeExtractHandlerReturnInfo(tryExtract, true);

KeyAmount current = tryExtract;

for (var handler : handlers)
{
if (handler == null)
continue;

var ret = handler.beforeExtract(original, current, net);

current = ret.beforeExtract();

if (ret.cancel())
return new BeforeExtractHandlerReturnInfo(current, true);

if (current.isEmpty())
return new BeforeExtractHandlerReturnInfo(current, false);
}

return new BeforeExtractHandlerReturnInfo(current, false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ protected KeyAmount unzipMatterBall(ItemStackKey ballKey, long ballCount, boolea
for (int i = applied.size() - 1; i >= 0; i--)
{
KeyAmount a = applied.get(i);
extract(a.key(), a.amount(), false, false);
extractByKey(a.key(), a.amount(), false);
}
return new KeyAmount(ballKey, ballCount);
}
Expand All @@ -640,7 +640,7 @@ protected KeyAmount unzipMatterBall(ItemStackKey ballKey, long ballCount, boolea
for (int i = applied.size() - 1; i >= 0; i--)
{
KeyAmount a = applied.get(i);
extract(a.key(), a.amount(), false, false);
extractByKey(a.key(), a.amount(), false);
}
return new KeyAmount(ballKey, ballCount);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.wintercogs.beyonddimensions.api.dimensionnet.NetPermissionlevel;
import com.wintercogs.beyonddimensions.api.dimensionnet.PlayerPermissionInfo;
import com.wintercogs.beyonddimensions.api.dimensionnet.UnifiedStorage;
import com.wintercogs.beyonddimensions.api.dimensionnet.helper.UnifiedStorageBeforeExtractHandler;
import com.wintercogs.beyonddimensions.api.dimensionnet.helper.UnifiedStorageBeforeInsertHandler;
import com.wintercogs.beyonddimensions.api.longtype.LongType;
import com.wintercogs.beyonddimensions.api.storage.handler.IStackHandler;
Expand All @@ -24,6 +25,7 @@ public void registerBindings(BindingsEvent bindings)
{
bindings.add("UnifiedStorage", UnifiedStorage.class);
bindings.add("UnifiedStorageBeforeInsertHandler", UnifiedStorageBeforeInsertHandler.class);
bindings.add("UnifiedStorageBeforeExtractHandler", UnifiedStorageBeforeExtractHandler.class);
bindings.add("IStackTypedHandler", IStackHandler.class);
bindings.add("StackTypedHandler", StackHandler.class);
bindings.add("LongType", LongType.class);
Expand Down