Skip to content

Commit

Permalink
NeoForge 1.20.4 (#122)
Browse files Browse the repository at this point in the history
Port to NeoForge 1.20.4
  • Loading branch information
shartte authored Dec 24, 2023
1 parent 045c925 commit 4b15308
Show file tree
Hide file tree
Showing 14 changed files with 231 additions and 193 deletions.
6 changes: 0 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ repositories {

apply plugin: "me.shedaniel.unified-publishing"

sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17

dependencies {
implementation "net.neoforged:neoforge:${project.neoforge_version}"

Expand All @@ -56,10 +53,8 @@ dependencies {
compileOnly "me.shedaniel:RoughlyEnoughItems-api-neoforge:${project.rei_version}"
compileOnly "me.shedaniel:RoughlyEnoughItems-default-plugin-neoforge:${project.rei_version}"
implementation "me.shedaniel:RoughlyEnoughItems-neoforge:${project.rei_version}"
implementation "dev.architectury:architectury-neoforge:10.0.16"

compileOnly "top.theillusivec4.curios:curios-neoforge:${project.curios_version}:api"
//modRuntimeOnly "top.theillusivec4.curios:curios-forge:${project.curios_version}"
}

base {
Expand All @@ -78,7 +73,6 @@ java {

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

jar {
Expand Down
11 changes: 5 additions & 6 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,16 @@ org.gradle.jvmargs=--add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAME
--add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED \
--add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED

org.gradle.daemon=false
mod_name=Powah
mod_version=5.1.0-alpha
archives_base_name=Powah
maven_group=dev.technici4n

# Common
minecraft_version=1.20.2
cloth_config_version=12.0.111
minecraft_version=1.20.4
cloth_config_version=13.0.121

rei_version=13.0.678
rei_version=14.0.688
emi_version=1.0.5+1.20.1
neoforge_version=20.2.86
curios_version=6.1.0+1.20.2
neoforge_version=20.4.55-beta
curios_version=7.0.0-beta.3+1.20.4
229 changes: 85 additions & 144 deletions src/main/java/owmii/powah/Powah.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,33 @@

import com.google.common.primitives.Ints;
import me.shedaniel.autoconfig.ConfigHolder;
import net.minecraft.core.Direction;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.neoforged.bus.api.Event;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.fml.ModList;
import net.neoforged.fml.common.Mod;
import net.neoforged.fml.event.lifecycle.FMLCommonSetupEvent;
import net.neoforged.fml.javafmlmod.FMLJavaModLoadingContext;
import net.neoforged.fml.loading.FMLEnvironment;
import net.neoforged.neoforge.capabilities.Capabilities;
import net.neoforged.neoforge.capabilities.RegisterCapabilitiesEvent;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.common.capabilities.Capabilities;
import net.neoforged.neoforge.common.capabilities.Capability;
import net.neoforged.neoforge.common.capabilities.ICapabilityProvider;
import net.neoforged.neoforge.common.util.LazyOptional;
import net.neoforged.neoforge.energy.IEnergyStorage;
import net.neoforged.neoforge.event.AttachCapabilitiesEvent;
import net.neoforged.neoforge.event.entity.player.PlayerInteractEvent;
import net.neoforged.neoforge.registries.RegisterEvent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import owmii.powah.api.PowahAPI;
import owmii.powah.block.Blcks;
import owmii.powah.block.Tiles;
import owmii.powah.block.reactor.ReactorPartTile;
import owmii.powah.compat.curios.CuriosCompat;
import owmii.powah.config.v2.PowahConfig;
import owmii.powah.data.DataEvents;
Expand Down Expand Up @@ -69,8 +61,7 @@ public static PowahConfig config() {
return CONFIG.getConfig();
}

public Powah() {
var modEventBus = FMLJavaModLoadingContext.get().getModEventBus();
public Powah(IEventBus modEventBus) {

Blcks.DR.register(modEventBus);
Tiles.DR.register(modEventBus);
Expand All @@ -81,8 +72,8 @@ public Powah() {
Recipes.DR_SERIALIZER.register(modEventBus);
Recipes.DR_TYPE.register(modEventBus);
CreativeTabs.DR.register(modEventBus);
modEventBus.addListener(RegisterCapabilitiesEvent.class, this::registerTransfer);

registerTransfer();
Network.register();

modEventBus.addListener((FMLCommonSetupEvent event) -> {
Expand Down Expand Up @@ -119,150 +110,100 @@ public Powah() {
}
}

private void registerTransfer() {
NeoForge.EVENT_BUS.addGenericListener(BlockEntity.class, (AttachCapabilitiesEvent<BlockEntity> event) -> {
if (event.getObject() instanceof ReactorPartTile reactorPart) {
event.addCapability(Powah.id("reactor_part"), new ICapabilityProvider() {
@NotNull
@Override
public <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap, @Nullable Direction side) {
if (reactorPart.core().isPresent()) {
if (cap != Capabilities.ENERGY || reactorPart.isExtractor()) {
return reactorPart.core().get().getCapability(cap, side);
}
}
return LazyOptional.empty();
}
});
private void registerTransfer(RegisterCapabilitiesEvent event) {
// Special handling, since reactor parts delegate to their core
event.registerBlockEntity(Capabilities.EnergyStorage.BLOCK, Tiles.REACTOR_PART.get(), (reactorPart, unused) -> {
if (reactorPart.isExtractor()) {
return reactorPart.getCoreEnergyStorage();
}
if (event.getObject() instanceof AbstractEnergyStorage<?, ?>energyStorage) {
event.addCapability(Powah.id("energy"), new ICapabilityProvider() {
@NotNull
@Override
public <T> LazyOptional<T> getCapability(@NotNull Capability<T> capability, @Nullable Direction side) {
if (capability == Capabilities.ENERGY) {
if (energyStorage.isEnergyPresent(side)) {
return LazyOptional.of(() -> new IEnergyStorage() {
@Override
public int extractEnergy(int maxExtract, boolean simulate) {
return Util.safeInt(energyStorage.extractEnergy(maxExtract, simulate, side));
}

@Override
public int getEnergyStored() {
return Util.safeInt(energyStorage.getEnergy().getStored());
}

@Override
public int getMaxEnergyStored() {
return Ints.saturatedCast(energyStorage.getEnergy().getMaxEnergyStored());
}
return null;
});
event.registerBlockEntity(Capabilities.ItemHandler.BLOCK, Tiles.REACTOR_PART.get(), (reactorPart, unused) -> {
return reactorPart.getCoreItemHandler();
});
event.registerBlockEntity(Capabilities.FluidHandler.BLOCK, Tiles.REACTOR_PART.get(), (reactorPart, unused) -> {
return reactorPart.getCoreFluidHandler();
});

@Override
public int receiveEnergy(int maxReceive, boolean simulate) {
return Util.safeInt(energyStorage.receiveEnergy(maxReceive, simulate, side));
}
for (var entry : Tiles.DR.getEntries()) {
var be = entry.get().create(BlockPos.ZERO, Blocks.AIR.defaultBlockState());
if (be == null) {
throw new IllegalStateException("Failed to create a dummy BE for " + entry.getId());
}

@Override
public boolean canReceive() {
return energyStorage.canReceiveEnergy(side);
}
registerBlockEntityCapability(event, entry.get(), be.getClass());
}

@Override
public boolean canExtract() {
return energyStorage.canExtractEnergy(side);
}
}).cast();
}
}
return LazyOptional.empty();
for (var entry : Itms.DR.getEntries()) {
if (entry.get() instanceof IEnergyContainingItem eci) {
event.registerItem(Capabilities.EnergyStorage.ITEM, (stack, unused) -> {
var info = eci.getEnergyInfo();
if (info == null) {
return null;
}
});

var energyItem = new Energy.Item(stack, info);
return energyItem.createItemCapability();
}, entry.get());
}
if (event.getObject() instanceof IInventoryHolder holder) {
event.addCapability(Powah.id("inv"), new ICapabilityProvider() {
@NotNull
}
}

private static void registerBlockEntityCapability(RegisterCapabilitiesEvent event, BlockEntityType<?> beType, Class<?> beClass) {
if (AbstractEnergyStorage.class.isAssignableFrom(beClass)) {
event.registerBlockEntity(Capabilities.EnergyStorage.BLOCK, beType, (o, side) -> {
var energyStorage = (AbstractEnergyStorage<?, ?>) o;
if (!energyStorage.isEnergyPresent(side)) {
return null;
}

return new IEnergyStorage() {
@Override
public <T> LazyOptional<T> getCapability(@NotNull Capability<T> capability, @Nullable Direction side) {
if (capability == Capabilities.ITEM_HANDLER) {
var inv = holder.getInventory();
if (!inv.isBlank()) {
return LazyOptional.of(() -> inv).cast();
}
}
return LazyOptional.empty();
public int extractEnergy(int maxExtract, boolean simulate) {
return Util.safeInt(energyStorage.extractEnergy(maxExtract, simulate, side));
}
});
}
if (event.getObject() instanceof ITankHolder holder) {
event.addCapability(Powah.id("tank"), new ICapabilityProvider() {
@NotNull

@Override
public <T> LazyOptional<T> getCapability(@NotNull Capability<T> capability, @Nullable Direction arg) {
if (capability == Capabilities.FLUID_HANDLER) {
return LazyOptional.of(holder::getTank).cast();
}
return LazyOptional.empty();
public int getEnergyStored() {
return Util.safeInt(energyStorage.getEnergy().getStored());
}
});
}
});
NeoForge.EVENT_BUS.addGenericListener(ItemStack.class, (AttachCapabilitiesEvent<ItemStack> event) -> {
if (event.getObject().getItem() instanceof IEnergyContainingItem eci) {
event.addCapability(Powah.id("energy"), new ICapabilityProvider() {
@NotNull
@Override
public <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap, @Nullable Direction arg) {
if (cap == Capabilities.ENERGY) {
var info = eci.getEnergyInfo();
if (info != null) {
return LazyOptional.of(() -> {
var energyItem = new Energy.Item(event.getObject(), info);
return new IEnergyStorage() {
@Override
public int receiveEnergy(int i, boolean bl) {
if (info.capacity() == 0 || !energyItem.canReceive()) {
return 0;
}
return Ints.saturatedCast(energyItem.receiveEnergy(i, bl));
}

@Override
public int extractEnergy(int i, boolean bl) {
if (!energyItem.canExtract()) {
return 0;
}
return Ints.saturatedCast(energyItem.extractEnergy(i, bl));
}

@Override
public int getEnergyStored() {
return Ints.saturatedCast(energyItem.getEnergyStored());
}
@Override
public int getMaxEnergyStored() {
return Ints.saturatedCast(energyStorage.getEnergy().getMaxEnergyStored());
}

@Override
public int getMaxEnergyStored() {
return Ints.saturatedCast(energyItem.getMaxEnergyStored());
}
@Override
public int receiveEnergy(int maxReceive, boolean simulate) {
return Util.safeInt(energyStorage.receiveEnergy(maxReceive, simulate, side));
}

@Override
public boolean canExtract() {
return energyItem.canExtract();
}
@Override
public boolean canReceive() {
return energyStorage.canReceiveEnergy(side);
}

@Override
public boolean canReceive() {
return energyItem.canReceive();
}
};
}).cast();
}
}
return LazyOptional.empty();
@Override
public boolean canExtract() {
return energyStorage.canExtractEnergy(side);
}
});
}
});
};
});
}
if (IInventoryHolder.class.isAssignableFrom(beClass)) {
event.registerBlockEntity(Capabilities.ItemHandler.BLOCK, beType, (o, direction) -> {
var inv = ((IInventoryHolder) o).getInventory();
if (!inv.isBlank()) {
return inv;
}
return null;
});
}
if (ITankHolder.class.isAssignableFrom(beClass)) {
event.registerBlockEntity(Capabilities.FluidHandler.BLOCK, beType, (o, direction) -> {
return ((ITankHolder) o).getTank();
});
}
}

private void setupBlockItems(IEventBus modEventBus) {
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/owmii/powah/block/cable/CableTile.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.neoforged.neoforge.common.capabilities.Capabilities;
import net.neoforged.neoforge.capabilities.Capabilities;
import org.apache.commons.lang3.mutable.MutableBoolean;
import org.jetbrains.annotations.Nullable;
import owmii.powah.block.Tier;
Expand Down Expand Up @@ -187,8 +187,7 @@ private long pushEnergy(long maxReceive, boolean simulate, @Nullable Direction d
}

private long receive(Level level, BlockPos pos, Direction side, long amount, boolean simulate) {
var tile = level.getBlockEntity(pos);
var energy = tile != null ? tile.getCapability(Capabilities.ENERGY, side).orElse(null) : null;
var energy = level.getCapability(Capabilities.EnergyStorage.BLOCK, pos, side);
return energy != null ? energy.receiveEnergy(Ints.saturatedCast(amount), simulate) : 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.CraftingRecipeCodecs;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.item.crafting.RecipeSerializer;
Expand Down Expand Up @@ -117,7 +116,7 @@ public static class Serializer implements RecipeSerializer<EnergizingRecipe> {
.listOf()
.fieldOf("ingredients")
.forGetter(EnergizingRecipe::getIngredients),
CraftingRecipeCodecs.ITEMSTACK_OBJECT_CODEC.fieldOf("result").forGetter(EnergizingRecipe::getResultItem))
ItemStack.ITEM_WITH_COUNT_CODEC.fieldOf("result").forGetter(EnergizingRecipe::getResultItem))
.apply(builder, (energy, ingredients, result) -> {
var nnIngredients = NonNullList.<Ingredient>create();
nnIngredients.addAll(ingredients);
Expand Down
Loading

0 comments on commit 4b15308

Please sign in to comment.