Skip to content

Commit

Permalink
dd neoforge
Browse files Browse the repository at this point in the history
  • Loading branch information
cech12 committed Dec 29, 2023
1 parent 3656974 commit 67c0a36
Show file tree
Hide file tree
Showing 67 changed files with 1,642 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public boolean canHoldFluid(Fluid fluid) {
if (bucket instanceof MilkBucketItem && fluid != Services.FLUID.getMilkFluid()) {
return false;
}
if (!(bucket instanceof MilkBucketItem) && (!(bucket instanceof BucketItem) || BucketLibUtil.getFluidOfBucketItem((BucketItem) bucket) != fluid)) {
if (!(bucket instanceof MilkBucketItem) && (!(bucket instanceof BucketItem) || Services.BUCKET.getFluidOfBucketItem((BucketItem) bucket) != fluid)) {
return false;
}
if (this.properties.allowedFluidsTag != null || this.properties.allowedFluids != null) {
Expand Down Expand Up @@ -308,8 +308,8 @@ public InteractionResult interactLivingEntity(@Nonnull ItemStack itemStack, @Non
if (entity instanceof Axolotl axolotl && BucketLibUtil.containsEntityType(itemStack)
&& Arrays.stream(AxolotlAi.getTemptations().getItems()).anyMatch(
stack -> stack.getItem() instanceof MobBucketItem mobBucketItem
&& BucketLibUtil.getFluid(itemStack) == BucketLibUtil.getFluidOfBucketItem(mobBucketItem)
&& BucketLibUtil.getEntityType(itemStack) == BucketLibUtil.getEntityTypeOfMobBucketItem(mobBucketItem)
&& BucketLibUtil.getFluid(itemStack) == Services.BUCKET.getFluidOfBucketItem(mobBucketItem)
&& BucketLibUtil.getEntityType(itemStack) == Services.BUCKET.getEntityTypeOfMobBucketItem(mobBucketItem)
)) {
int age = axolotl.getAge();
if (!axolotl.level().isClientSide && age == 0 && axolotl.canFallInLove()) {
Expand All @@ -336,7 +336,7 @@ public InteractionResult interactLivingEntity(@Nonnull ItemStack itemStack, @Non
private <T extends LivingEntity & Bucketable> InteractionResult pickupEntityWithBucket(Player player, InteractionHand interactionHand, T entity) {
ItemStack itemStack = player.getItemInHand(interactionHand).copy(); //copy to avoid changing the real item stack
Fluid containedFluid = Services.FLUID.getContainedFluid(itemStack);
Fluid entityBucketFluid = BucketLibUtil.getFluidOfBucketItem((BucketItem) entity.getBucketItemStack().getItem());
Fluid entityBucketFluid = Services.BUCKET.getFluidOfBucketItem((BucketItem) entity.getBucketItemStack().getItem());
if (itemStack.getItem() instanceof UniversalBucketItem
&& entity.isAlive()
&& entityBucketFluid == containedFluid) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ private static void getTemptationsProxy(CallbackInfoReturnable<Ingredient> cir)
for (ItemStack stack : cir.getReturnValue().getItems()) {
temptationStacks.add(stack);
if (stack.getItem() instanceof MobBucketItem mobBucketItem) {
Fluid fluid = BucketLibUtil.getFluidOfBucketItem(mobBucketItem);
EntityType<?> entityType = BucketLibUtil.getEntityTypeOfMobBucketItem(mobBucketItem);
Fluid fluid = Services.BUCKET.getFluidOfBucketItem(mobBucketItem);
EntityType<?> entityType = Services.BUCKET.getEntityTypeOfMobBucketItem(mobBucketItem);
for (UniversalBucketItem bucketItem : Services.REGISTRY.getRegisteredBuckets()) {
if (bucketItem.canHoldFluid(fluid) && bucketItem.canHoldEntity(entityType)) {
ItemStack bucket = new ItemStack(bucketItem);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package de.cech12.bucketlib.platform;

import de.cech12.bucketlib.CommonLoader;
import de.cech12.bucketlib.api.BucketLib;
import de.cech12.bucketlib.platform.services.IBucketHelper;
import de.cech12.bucketlib.platform.services.IConfigHelper;
import de.cech12.bucketlib.platform.services.IFluidHelper;
import de.cech12.bucketlib.platform.services.IPlatformHelper;
Expand All @@ -16,6 +16,8 @@
*/
public class Services {

/** bucket helper instance */
public static final IBucketHelper BUCKET = load(IBucketHelper.class);
/** Config helper instance */
public static final IConfigHelper CONFIG = load(IConfigHelper.class);
/** Fluid helper instance */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package de.cech12.bucketlib.platform.services;

import net.minecraft.world.entity.EntityType;
import net.minecraft.world.item.BucketItem;
import net.minecraft.world.item.MobBucketItem;
import net.minecraft.world.level.material.Fluid;

public interface IBucketHelper {

Fluid getFluidOfBucketItem(BucketItem bucket);

EntityType<?> getEntityTypeOfMobBucketItem(MobBucketItem mobBucketItem);

}
22 changes: 0 additions & 22 deletions common/src/main/java/de/cech12/bucketlib/util/BucketLibUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -283,26 +283,4 @@ public static ItemStack removeBlock(ItemStack itemStack) {
return itemStack.copy();
}

public static Fluid getFluidOfBucketItem(BucketItem bucket) {
try {
Field fluidField = BucketItem.class.getDeclaredField("content");
fluidField.setAccessible(true);
return (Fluid) fluidField.get(bucket);
} catch (IllegalAccessException | NoSuchFieldException ex) {
CommonLoader.LOG.error("Could not get entity type of MobBucketItem.", ex);
}
return Fluids.EMPTY;
}

public static EntityType<?> getEntityTypeOfMobBucketItem(MobBucketItem mobBucketItem) {
try {
Field entityTypeField = MobBucketItem.class.getDeclaredField("type");
entityTypeField.setAccessible(true);
return (EntityType<?>) entityTypeField.get(mobBucketItem);
} catch (IllegalAccessException | NoSuchFieldException ex) {
CommonLoader.LOG.error("Could not get entity type of MobBucketItem.", ex);
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ private static void readRegistry() {
bucketBlocks.add(new BucketBlock(bucket.getBlock(), bucket));
}
if (item instanceof MobBucketItem bucket) {
EntityType<?> entityType = BucketLibUtil.getEntityTypeOfMobBucketItem(bucket);
EntityType<?> entityType = Services.BUCKET.getEntityTypeOfMobBucketItem(bucket);
if (entityType != null && level != null && entityType.create(level) instanceof Bucketable) {
bucketEntities.add(new BucketEntity(entityType, BucketLibUtil.getFluidOfBucketItem(bucket), bucket));
bucketEntities.add(new BucketEntity(entityType, Services.BUCKET.getFluidOfBucketItem(bucket), bucket));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package de.cech12.bucketlib.platform;

import de.cech12.bucketlib.CommonLoader;
import de.cech12.bucketlib.platform.services.IBucketHelper;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.item.BucketItem;
import net.minecraft.world.item.MobBucketItem;
import net.minecraft.world.level.material.Fluid;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
* The bucket helper implementation for Forge.
*/
public class ForgeBucketHelper implements IBucketHelper {

@Override
public Fluid getFluidOfBucketItem(BucketItem bucket) {
return bucket.getFluid();
}

@Override
public EntityType<?> getEntityTypeOfMobBucketItem(MobBucketItem mobBucketItem) {
try {
Method entityTypeMethod = MobBucketItem.class.getDeclaredMethod("getFishType");
entityTypeMethod.setAccessible(true);
return (EntityType<?>) entityTypeMethod.invoke(mobBucketItem);
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) {
CommonLoader.LOG.error("Could not get entity type of MobBucketItem.", ex);
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
de.cech12.bucketlib.platform.ForgeBucketHelper
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cech12.bucketlib;
package de.cech12.bucketlib;

import com.mojang.authlib.GameProfile;
import net.minecraft.core.BlockPos;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package cech12.bucketlib;
package de.cech12.bucketlib;

import de.cech12.bucketlib.api.BucketLib;
import de.cech12.bucketlib.api.BucketLibApi;
import de.cech12.bucketlib.api.item.UniversalBucketItem;
import net.minecraft.resources.ResourceLocation;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cech12.bucketlib;
package de.cech12.bucketlib;

import de.cech12.bucketlib.api.BucketLib;
import de.cech12.bucketlib.util.BucketLibUtil;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cech12.bucketlib;
package de.cech12.bucketlib;

import de.cech12.bucketlib.api.BucketLib;
import net.minecraft.core.BlockPos;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cech12.bucketlib;
package de.cech12.bucketlib;

import de.cech12.bucketlib.api.BucketLib;
import net.minecraft.world.level.material.Fluid;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cech12.bucketlib;
package de.cech12.bucketlib;

import net.minecraft.world.InteractionResult;
import net.minecraft.world.InteractionResultHolder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cech12.bucketlib;
package de.cech12.bucketlib;

import de.cech12.bucketlib.api.BucketLib;
import de.cech12.bucketlib.util.BucketLibUtil;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cech12.bucketlib;
package de.cech12.bucketlib;

import de.cech12.bucketlib.api.BucketLib;
import net.minecraft.core.BlockPos;
Expand Down
76 changes: 76 additions & 0 deletions neoforge/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
plugins {
id 'idea'
id 'maven-publish'
id 'net.neoforged.gradle.userdev' version '7.0.41'
id 'java-library'
}
base {
archivesName = "${mod_id}-neoforge-${minecraft_version}"
}

// Automatically enable neoforge AccessTransformers if the file exists
// This location is hardcoded in FML and can not be changed.
// https://github.com/neoforged/FancyModLoader/blob/a952595eaaddd571fbc53f43847680b00894e0c1/loader/src/main/java/net/neoforged/fml/loading/moddiscovery/ModFile.java#L118
if (file('src/main/resources/META-INF/accesstransformer.cfg').exists()) {
minecraft.accessTransformers.file file('src/main/resources/META-INF/accesstransformer.cfg')
}
runs {
configureEach {
modSource project.sourceSets.main
modSource project.sourceSets.test
}
client {
systemProperty 'neoforge.enabledGameTestNamespaces', project.mod_id
}
server {
systemProperty 'neoforge.enabledGameTestNamespaces', project.mod_id
programArgument '--nogui'
}
gameTestServer {
systemProperty 'neoforge.enabledGameTestNamespaces', project.mod_id
}
data {
programArguments.addAll '--mod', project.mod_id, '--all', '--output', file('src/generated/resources/').getAbsolutePath(), '--existing', file('src/main/resources/').getAbsolutePath()
}
}

sourceSets.main.resources { srcDir 'src/generated/resources' }

dependencies {
implementation "net.neoforged:neoforge:${neoforge_version}"
compileOnly project(":common")
testCompileOnly project(":common")
}

// NeoGradle compiles the game, but we don't want to add our common code to the game's code
Spec<Task> notNeoTask = { Task it -> !it.name.startsWith("neo") } as Spec<Task>

tasks.withType(JavaCompile).matching(notNeoTask).configureEach {
source(project(":common").sourceSets.main.allSource)
}

tasks.withType(Javadoc).matching(notNeoTask).configureEach {
source(project(":common").sourceSets.main.allJava)
}

tasks.named("sourcesJar", Jar) {
from(project(":common").sourceSets.main.allSource)
}

tasks.withType(ProcessResources).matching(notNeoTask).configureEach {
from project(":common").sourceSets.main.resources
}

publishing {
publications {
mavenJava(MavenPublication) {
artifactId base.archivesName.get()
from components.java
}
}
repositories {
maven {
url "file://" + System.getenv("local_maven")
}
}
}
Loading

0 comments on commit 67c0a36

Please sign in to comment.