Skip to content

Commit

Permalink
Update to Fabric Transfer API
Browse files Browse the repository at this point in the history
  • Loading branch information
shedaniel committed Mar 3, 2024
1 parent 52a6bf8 commit aebe0f1
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ private static <P> String getSimpleName(Class<? extends P> pluginClass) {
public void detectServerPlugins() {
loadPlugin(REIServerPlugin.class, ((PluginView<REIServerPlugin>) PluginManager.getServerInstance())::registerPlugin);
try {
// TODO 24w09a: PluginView.getServerInstance().registerPlugin((REIServerPlugin) Class.forName("me.shedaniel.rei.impl.common.compat.FabricFluidAPISupportPlugin").getConstructor().newInstance());
PluginView.getServerInstance().registerPlugin((REIServerPlugin) Class.forName("me.shedaniel.rei.impl.common.compat.FabricFluidAPISupportPlugin").getConstructor().newInstance());
} catch (Throwable throwable) {
throwable.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* This file is licensed under the MIT License, part of Roughly Enough Items.
* Copyright (c) 2018, 2019, 2020, 2021, 2022, 2023 shedaniel
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package me.shedaniel.rei.impl.common.compat;

import dev.architectury.event.CompoundEventResult;
import dev.architectury.fluid.FluidStack;
import dev.architectury.hooks.fluid.fabric.FluidStackHooksFabric;
import me.shedaniel.rei.api.common.entry.EntryStack;
import me.shedaniel.rei.api.common.fluid.FluidSupportProvider;
import me.shedaniel.rei.api.common.plugins.REIServerPlugin;
import me.shedaniel.rei.api.common.util.EntryStacks;
import net.fabricmc.fabric.api.transfer.v1.context.ContainerItemContext;
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidStorage;
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant;
import net.fabricmc.fabric.api.transfer.v1.storage.Storage;
import net.minecraft.world.item.ItemStack;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

public class FabricFluidAPISupportPlugin implements REIServerPlugin {
@Override
public void registerFluidSupport(FluidSupportProvider support) {
support.register(entry -> {
ItemStack stack = entry.getValue().copy();
Storage<FluidVariant> storage = FluidStorage.ITEM.find(stack, ContainerItemContext.withConstant(stack));
if (storage != null) {
List<EntryStack<FluidStack>> result = StreamSupport.stream(storage.spliterator(), false)
.filter(view -> !view.isResourceBlank())
.map(view -> EntryStacks.of(FluidStackHooksFabric.fromFabric(view.getResource(), view.getAmount())))
.collect(Collectors.toList());
if (!result.isEmpty()) {
return CompoundEventResult.interruptTrue(result.stream());
}
}
return CompoundEventResult.pass();
});
}
}
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ neoforge_version=20.4.48-beta
fabricloader_version=0.15.7
cloth_config_version=14.0.122
modmenu_version=7.0.0
fabric_api=0.96.5+1.20.5
architectury_version=12.0.14
fabric_api=0.96.6+1.20.5
architectury_version=12.0.16
api_exculde=
#api_include=me.shedaniel.cloth:cloth-events,me.shedaniel.cloth:config-2,me.sargunvohra.mcmods:autoconfig1u,org.jetbrains:annotations,net.fabricmc.fabric-api:fabric
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import me.shedaniel.rei.api.client.gui.widgets.Tooltip;
import me.shedaniel.rei.api.client.gui.widgets.TooltipContext;
import me.shedaniel.rei.api.client.util.SpriteRenderer;
import me.shedaniel.rei.api.common.display.basic.BasicDisplay;
import me.shedaniel.rei.api.common.entry.EntrySerializer;
import me.shedaniel.rei.api.common.entry.EntryStack;
import me.shedaniel.rei.api.common.entry.comparison.ComparisonContext;
Expand All @@ -57,8 +58,11 @@
import net.minecraft.client.renderer.texture.TextureAtlas;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.resources.language.I18n;
import net.minecraft.core.component.DataComponentPatch;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtOps;
import net.minecraft.nbt.Tag;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagKey;
Expand Down Expand Up @@ -127,7 +131,7 @@ public FluidStack copy(EntryStack<FluidStack> entry, FluidStack value) {
public FluidStack normalize(EntryStack<FluidStack> entry, FluidStack value) {
Fluid fluid = value.getFluid();
if (fluid instanceof FlowingFluid flowingFluid) fluid = flowingFluid.getSource();
return FluidStack.create(fluid, FluidStack.bucketAmount(), value.getTag());
return FluidStack.create(fluid, FluidStack.bucketAmount(), value.getPatch());
}

@Override
Expand Down Expand Up @@ -190,12 +194,12 @@ public boolean acceptsNull() {

@Override
public CompoundTag save(EntryStack<FluidStack> entry, FluidStack value) {
return value.write(new CompoundTag());
return (CompoundTag) value.write(BasicDisplay.registryAccess(), new CompoundTag());
}

@Override
public FluidStack read(CompoundTag tag) {
return FluidStack.read(tag);
return FluidStack.read(BasicDisplay.registryAccess(), tag).orElse(FluidStack.empty());
}

@Override
Expand All @@ -214,7 +218,7 @@ public void fillCrashReport(CrashReport report, CrashReportCategory category, En
FluidStack stack = entry.getValue();
category.setDetail("Fluid Type", () -> String.valueOf(BuiltInRegistries.FLUID.getKey(stack.getFluid())));
category.setDetail("Fluid Amount", () -> String.valueOf(stack.getAmount()));
category.setDetail("Fluid NBT", () -> String.valueOf(stack.getTag()));
category.setDetail("Fluid NBT", () -> DataComponentPatch.CODEC.encodeStart(BasicDisplay.registryAccess().createSerializationContext(NbtOps.INSTANCE), stack.getPatch()).result().map(Tag::toString).orElse("Error"));
}

@Environment(EnvType.CLIENT)
Expand Down

0 comments on commit aebe0f1

Please sign in to comment.