Skip to content
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 .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jobs:
build:
runs-on: ubuntu-24.04
container:
image: mcr.microsoft.com/openjdk/jdk:21-ubuntu
image: mcr.microsoft.com/openjdk/jdk:25-ubuntu
options: --user root
steps:
- uses: actions/checkout@v4
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ jobs:
build:
runs-on: ubuntu-24.04
container:
image: mcr.microsoft.com/openjdk/jdk:21-ubuntu
image: mcr.microsoft.com/openjdk/jdk:25-ubuntu
options: --user root
steps:
- uses: actions/checkout@v4
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@ public class MyBlockEntity extends BlockEntity {
public final SimpleEnergyStorage energyStorage = new SimpleEnergyStorage(CAPACITY, MAX_INSERT, MAX_EXTRACT) {
@Override
protected void onFinalCommit() {
markDirty();
setChanged();
}
};

// Use the energy internally, for example in tick()
public void tick() {
if (!world.isClient && energyStorage.amount >= 10) {
if (!level.isClientSide() && energyStorage.amount >= 10) {
energyStorage.amount -= 10;
// do something with the 10 energy we just used.
markDirty();
setChanged();
}
}

Expand All @@ -78,10 +78,10 @@ EnergyStorage maybeStorage = EnergyStorage.SIDED.find(world, pos, direction);
Get an adjacent energy storage:
```groovy
// Known things
World world; BlockPos currentPos; Direction adjacentDirection;
Level level; BlockPos currentPos; Direction adjacentDirection;
// Get adjacent energy storage, or null if there is none
@Nullable
EnergyStorage maybeStorage = EnergyStorage.SIDED.find(world, currentPos.offset(adjacentDirection), adjacentDirection.getOpposite());
EnergyStorage maybeStorage = EnergyStorage.SIDED.find(level, currentPos.relative(adjacentDirection), adjacentDirection.getOpposite());
```
Move energy between two storages:
```groovy
Expand Down
26 changes: 10 additions & 16 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import groovy.xml.XmlSlurper

plugins {
id 'fabric-loom' version '1.10-SNAPSHOT'
id 'net.fabricmc.fabric-loom' version '1.14-SNAPSHOT'
id 'java'
id 'maven-publish'
}
Expand All @@ -12,23 +14,15 @@ def ENV = System.getenv()
group = "teamreborn"
version = project.mod_version

loom {
addRemapConfiguration("testModImplementation") {
targetConfigurationName = "test"
onCompileClasspath = true
onRuntimeClasspath = true
}
}

dependencies {
minecraft "com.mojang:minecraft:${project.minecraft_version}"
mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2"
modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"

modApi fabricApi.module("fabric-transfer-api-v1", project.fabric_version)
implementation "net.fabricmc:fabric-loader:${project.loader_version}"

api fabricApi.module("fabric-transfer-api-v1", project.fabric_version)

testImplementation "net.fabricmc:fabric-loader-junit:${project.loader_version}"
testModImplementation fabricApi.module("fabric-registry-sync-v0", project.fabric_version)
testImplementation fabricApi.module("fabric-registry-sync-v0", project.fabric_version)
}

test {
Expand All @@ -45,7 +39,7 @@ processResources {

tasks.withType(JavaCompile).configureEach {
it.options.encoding = "UTF-8"
it.options.release = 21
it.options.release = 25
}

jar {
Expand All @@ -54,8 +48,8 @@ jar {

java {
withSourcesJar()
sourceCompatibility = JavaVersion.VERSION_21
targetCompatibility = JavaVersion.VERSION_21
sourceCompatibility = JavaVersion.VERSION_25
targetCompatibility = JavaVersion.VERSION_25
}

publishing {
Expand Down
7 changes: 3 additions & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ mod_version=4.2.0

# Fabric Properties
# check these on https://fabricmc.net/versions.html
minecraft_version=1.21.5-pre2
yarn_mappings=1.21.5-pre2+build.6
loader_version=0.16.10
minecraft_version=26.1-snapshot-1
loader_version=0.18.4

# Dependencies
fabric_version=0.119.0+1.21.5
fabric_version=0.140.3+26.1
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.13-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-9.2.1-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/team/reborn/energy/api/EnergyStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import net.fabricmc.fabric.api.transfer.v1.context.ContainerItemContext;
import net.fabricmc.fabric.api.transfer.v1.transaction.Transaction;
import net.fabricmc.fabric.api.transfer.v1.transaction.TransactionContext;
import net.minecraft.component.ComponentType;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.Direction;
import net.minecraft.core.Direction;
import net.minecraft.core.component.DataComponentType;
import net.minecraft.resources.Identifier;
import org.jetbrains.annotations.Nullable;
import team.reborn.energy.api.base.DelegatingEnergyStorage;
import team.reborn.energy.api.base.SimpleEnergyItem;
Expand Down Expand Up @@ -56,7 +56,7 @@ public interface EnergyStorage {
* On the client thread (i.e. with a client world), contents of queried EnergyStorages are unreliable and should not be modified.
*/
BlockApiLookup<EnergyStorage, @Nullable Direction> SIDED =
BlockApiLookup.get(Identifier.of("teamreborn", "sided_energy"), EnergyStorage.class, Direction.class);
BlockApiLookup.get(Identifier.fromNamespaceAndPath("teamreborn", "sided_energy"), EnergyStorage.class, Direction.class);

/**
* Item access to energy storages.
Expand All @@ -72,7 +72,7 @@ public interface EnergyStorage {
* Returned APIs should behave the same regardless of the logical side.
*/
ItemApiLookup<EnergyStorage, ContainerItemContext> ITEM =
ItemApiLookup.get(Identifier.of("teamreborn", "energy"), EnergyStorage.class, ContainerItemContext.class);
ItemApiLookup.get(Identifier.fromNamespaceAndPath("teamreborn", "energy"), EnergyStorage.class, ContainerItemContext.class);

/**
* Always empty energy storage.
Expand All @@ -86,7 +86,7 @@ public interface EnergyStorage {
* Otherwise, do not query it or assume it exists.
* Inter-mod energy interactions should happen using {@link #ITEM}.</b>
*/
ComponentType<Long> ENERGY_COMPONENT = Objects.requireNonNull(EnergyImpl.ENERGY_COMPONENT);
DataComponentType<Long> ENERGY_COMPONENT = Objects.requireNonNull(EnergyImpl.ENERGY_COMPONENT);

/**
* Return false if calling {@link #insert} will absolutely always return 0, or true otherwise or in doubt.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import net.fabricmc.fabric.api.transfer.v1.storage.StoragePreconditions;
import net.fabricmc.fabric.api.transfer.v1.transaction.Transaction;
import net.fabricmc.fabric.api.transfer.v1.transaction.TransactionContext;
import net.minecraft.item.ItemStack;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.Nullable;

/**
Expand Down
16 changes: 5 additions & 11 deletions src/main/java/team/reborn/energy/api/base/SimpleEnergyItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import net.fabricmc.fabric.api.transfer.v1.context.ContainerItemContext;
import net.fabricmc.fabric.api.transfer.v1.item.ItemVariant;
import net.minecraft.component.ComponentChanges;
import net.minecraft.item.ItemStack;
import net.minecraft.core.component.DataComponentMap;
import net.minecraft.core.component.DataComponentPatch;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.Nullable;
import team.reborn.energy.api.EnergyStorage;
import team.reborn.energy.impl.SimpleItemEnergyStorageImpl;
Expand Down Expand Up @@ -97,15 +98,8 @@ static long getStoredEnergyUnchecked(ItemVariant variant) {
return getStoredEnergyUnchecked(variant.getComponents());
}

static long getStoredEnergyUnchecked(ComponentChanges components) {
@Nullable Optional<Long> energy = (Optional<Long>) components.get(EnergyStorage.ENERGY_COMPONENT);

//noinspection OptionalAssignedToNull
if (energy != null) {
return energy.orElse(0L);
}

return 0;
static long getStoredEnergyUnchecked(DataComponentMap components) {
return components.getOrDefault(EnergyStorage.ENERGY_COMPONENT, 0L);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import net.fabricmc.fabric.api.transfer.v1.storage.StoragePreconditions;
import net.fabricmc.fabric.api.transfer.v1.transaction.TransactionContext;
import net.fabricmc.fabric.api.transfer.v1.transaction.base.SnapshotParticipant;
import net.minecraft.util.math.Direction;
import net.minecraft.core.Direction;
import org.jetbrains.annotations.Nullable;
import team.reborn.energy.api.EnergyStorage;

Expand All @@ -19,7 +19,7 @@ public abstract class SimpleSidedEnergyContainer extends SnapshotParticipant<Lon

public SimpleSidedEnergyContainer() {
for (int i = 0; i < 7; ++i) {
sideStorages[i] = new SideStorage(i == 6 ? null : Direction.byIndex(i));
sideStorages[i] = new SideStorage(i == 6 ? null : Direction.from3DDataValue(i));
}
}

Expand All @@ -42,7 +42,7 @@ public SimpleSidedEnergyContainer() {
* @return An {@link EnergyStorage} implementation for the passed side.
*/
public EnergyStorage getSideStorage(@Nullable Direction side) {
return sideStorages[side == null ? 6 : side.getIndex()];
return sideStorages[side == null ? 6 : side.get3DDataValue()];
}

@Override
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/team/reborn/energy/impl/EnergyImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@

import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
import net.minecraft.component.ComponentType;
import net.minecraft.network.codec.PacketCodecs;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.util.Identifier;
import net.minecraft.core.Registry;
import net.minecraft.core.component.DataComponentType;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.resources.Identifier;
import org.jetbrains.annotations.ApiStatus;
import team.reborn.energy.api.EnergyStorage;
import team.reborn.energy.api.base.SimpleEnergyItem;

@ApiStatus.Internal
public class EnergyImpl {
public static final ComponentType<Long> ENERGY_COMPONENT = ComponentType.<Long>builder()
.codec(nonNegativeLong())
.packetCodec(PacketCodecs.VAR_LONG)
public static final DataComponentType<Long> ENERGY_COMPONENT = DataComponentType.<Long>builder()
.persistent(nonNegativeLong())
.networkSynchronized(ByteBufCodecs.VAR_LONG)
.build();

public static void init() {
Registry.register(Registries.DATA_COMPONENT_TYPE, Identifier.of("team_reborn_energy", "energy"), ENERGY_COMPONENT);
Registry.register(BuiltInRegistries.DATA_COMPONENT_TYPE, Identifier.fromNamespaceAndPath("team_reborn_energy", "energy"), ENERGY_COMPONENT);
EnergyStorage.ITEM.registerFallback((stack, ctx) -> {
if (stack.getItem() instanceof SimpleEnergyItem energyItem) {
return SimpleEnergyItem.createStorage(ctx, energyItem.getEnergyCapacity(stack), energyItem.getEnergyMaxInput(stack), energyItem.getEnergyMaxOutput(stack));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import net.fabricmc.fabric.api.transfer.v1.storage.StoragePreconditions;
import net.fabricmc.fabric.api.transfer.v1.transaction.Transaction;
import net.fabricmc.fabric.api.transfer.v1.transaction.TransactionContext;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.ApiStatus;
import team.reborn.energy.api.EnergyStorage;
import team.reborn.energy.api.base.DelegatingEnergyStorage;
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
]
},
"depends": {
"java": ">=21",
"minecraft": ">=1.21.5-",
"java": ">=25",
"minecraft": ">=26.1-",
"fabric-transfer-api-v1": ">=5.1.0"
}
}
18 changes: 9 additions & 9 deletions src/test/java/team/reborn/energy/test/EnergyTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
import net.fabricmc.fabric.api.transfer.v1.storage.base.SingleVariantStorage;
import net.fabricmc.fabric.api.transfer.v1.transaction.Transaction;
import net.fabricmc.fabric.api.transfer.v1.transaction.TransactionContext;
import net.minecraft.Bootstrap;
import net.minecraft.SharedConstants;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.util.Identifier;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.Identifier;
import net.minecraft.server.Bootstrap;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import team.reborn.energy.api.EnergyStorage;
Expand All @@ -28,12 +28,12 @@ public class EnergyTests {

@BeforeAll
public static void setup() {
SharedConstants.createGameVersion();
Bootstrap.initialize();
SharedConstants.tryDetectVersion();
Bootstrap.bootStrap();
EnergyImpl.init();

item = new TestBatteryItem(60, 50, 50);
Registry.register(Registries.ITEM, Identifier.of("energy_test", "battery"), item);
Registry.register(BuiltInRegistries.ITEM, Identifier.fromNamespaceAndPath("energy_test", "battery"), item);
}

@Test
Expand Down
12 changes: 6 additions & 6 deletions src/test/java/team/reborn/energy/test/TestBatteryItem.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package team.reborn.energy.test;

import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.util.Identifier;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.Identifier;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import team.reborn.energy.api.base.SimpleEnergyItem;

public class TestBatteryItem extends Item implements SimpleEnergyItem {
private final long capacity, maxInput, maxOutput;

public TestBatteryItem(long capacity, long maxInput, long maxOutput) {
super(new Item.Settings().registryKey(RegistryKey.of(RegistryKeys.ITEM, Identifier.of("energy_test", "battery"))));
super(new Item.Properties().setId(ResourceKey.create(Registries.ITEM, Identifier.fromNamespaceAndPath("energy_test", "battery"))));
this.capacity = capacity;
this.maxInput = maxInput;
this.maxOutput = maxOutput;
Expand Down