Skip to content

Commit

Permalink
Add harvest compatibility for non-crops
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremiahwinsley committed Sep 1, 2023
1 parent fffb993 commit fba5536
Show file tree
Hide file tree
Showing 9 changed files with 178 additions and 3 deletions.
8 changes: 7 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ minecraft {
// Comma-separated list of namespaces to load gametests from. Empty = all namespaces.
property 'forge.enabledGameTestNamespaces', mod_id

// enable mixins for Jade
property 'mixin.env.remapRefMap', 'true'
property 'mixin.env.refMapRemappingFile', "${buildDir}/createSrgToMcp/output.srg"

mods {
"${mod_id}" {
source sourceSets.main
Expand Down Expand Up @@ -145,12 +149,14 @@ dependencies {
// The userdev artifact is a special name and will get all sorts of transformations applied to it.
minecraft "net.minecraftforge:forge:${minecraft_version}-${forge_version}"

runtimeOnly fg.deobf("curse.maven:jei-238222:4574528")
runtimeOnly fg.deobf("curse.maven:jei-238222:4712868")
runtimeOnly fg.deobf("curse.maven:jade-324717:4711195")
compileOnly fg.deobf("curse.maven:ftb-teams-forge-404468:4623116")

compileOnly fg.deobf("maven.modrinth:argonauts:1.0.2")
compileOnly fg.deobf("maven.modrinth:resourceful-lib:2.0.8")

compileOnly fg.deobf("curse.maven:ars-nouveau-401955:4683698")
}

jar {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ minecraft_version=1.20.1
forge_version=47.1.0
group=net.permutated
mod_id=pylons
version=4.1.0
version=4.2.0
6 changes: 5 additions & 1 deletion src/main/java/net/permutated/pylons/Pylons.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.permutated.pylons.compat.harvest.HarvestCompat;
import net.permutated.pylons.compat.teams.TeamCompat;
import net.permutated.pylons.item.MobFilterCard;
import net.permutated.pylons.item.PlayerFilterCard;
Expand Down Expand Up @@ -44,7 +45,10 @@ public Pylons() {
}

public void onCommonSetupEvent(final FMLCommonSetupEvent event) {
event.enqueueWork(() -> ForgeChunkManager.setForcedChunkLoadingCallback(MODID, ChunkManager::validateTickets));
event.enqueueWork(() -> {
ForgeChunkManager.setForcedChunkLoadingCallback(MODID, ChunkManager::validateTickets);
HarvestCompat.init();
});
}

public void onClientSetupEvent(final FMLClientSetupEvent event) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package net.permutated.pylons.compat.harvest;

import net.minecraft.world.level.block.Block;
import net.minecraftforge.fml.ModList;
import net.permutated.pylons.compat.harvest.adapters.ArsNouveauSourceBerryBush;
import net.permutated.pylons.compat.harvest.adapters.MinecraftNetherWart;
import net.permutated.pylons.compat.harvest.adapters.MinecraftSweetBerryBush;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class HarvestCompat {
private static final Map<Block, Harvestable> compat = new ConcurrentHashMap<>();

private HarvestCompat() {
// nothing to do
}

private static void register(Harvestable harvestable) {
compat.put(harvestable.getBlock(), harvestable);
}

public static Harvestable getCompat(Block block) {
return compat.get(block);
}

public static boolean hasCompat(Block block) {
return compat.containsKey(block);
}

// FMLCommonSetupEvent
public static void init() {
register(new MinecraftNetherWart());
register(new MinecraftSweetBerryBush());
if (ModList.get().isLoaded("ars_nouveau")) {
register(new ArsNouveauSourceBerryBush());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package net.permutated.pylons.compat.harvest;

import net.minecraft.core.BlockPos;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;

public interface Harvestable {
Block getBlock();
boolean isHarvestable(BlockState blockState);
ItemStack harvest(Level level, BlockPos blockPos, BlockState blockState);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package net.permutated.pylons.compat.harvest.adapters;

import com.hollingsworth.arsnouveau.common.block.SourceBerryBush;
import com.hollingsworth.arsnouveau.setup.registry.BlockRegistry;
import net.minecraft.core.BlockPos;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.permutated.pylons.compat.harvest.Harvestable;

public class ArsNouveauSourceBerryBush implements Harvestable {
public Block getBlock() {
return BlockRegistry.SOURCEBERRY_BUSH.get();
}

public boolean isHarvestable(BlockState blockState) {
return blockState.getValue(SourceBerryBush.AGE) == 3;
}

public ItemStack harvest(Level level, BlockPos blockPos, BlockState blockState) {
level.setBlock(blockPos, blockState.setValue(SourceBerryBush.AGE, 1), Block.UPDATE_CLIENTS);
return new ItemStack(BlockRegistry.SOURCEBERRY_BUSH, 2);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package net.permutated.pylons.compat.harvest.adapters;

import net.minecraft.core.BlockPos;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.NetherWartBlock;
import net.minecraft.world.level.block.state.BlockState;
import net.permutated.pylons.compat.harvest.Harvestable;

public class MinecraftNetherWart implements Harvestable {
@Override
public Block getBlock() {
return Blocks.NETHER_WART;
}

@Override
public boolean isHarvestable(BlockState blockState) {
return blockState.getValue(NetherWartBlock.AGE) == 3;
}

@Override
public ItemStack harvest(Level level, BlockPos blockPos, BlockState blockState) {
level.setBlock(blockPos, blockState.setValue(NetherWartBlock.AGE, 0), Block.UPDATE_CLIENTS);
return new ItemStack(Items.NETHER_WART, 3);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package net.permutated.pylons.compat.harvest.adapters;

import net.minecraft.core.BlockPos;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.SweetBerryBushBlock;
import net.minecraft.world.level.block.state.BlockState;
import net.permutated.pylons.compat.harvest.Harvestable;

public class MinecraftSweetBerryBush implements Harvestable {
@Override
public Block getBlock() {
return Blocks.SWEET_BERRY_BUSH;
}

@Override
public boolean isHarvestable(BlockState blockState) {
return blockState.getValue(SweetBerryBushBlock.AGE) == 3;
}

@Override
public ItemStack harvest(Level level, BlockPos blockPos, BlockState blockState) {
level.setBlock(blockPos, blockState.setValue(SweetBerryBushBlock.AGE, 1), Block.UPDATE_CLIENTS);
return new ItemStack(Items.SWEET_BERRIES, 2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import net.permutated.pylons.ConfigManager;
import net.permutated.pylons.ModRegistry;
import net.permutated.pylons.Pylons;
import net.permutated.pylons.compat.harvest.HarvestCompat;
import net.permutated.pylons.compat.harvest.Harvestable;
import net.permutated.pylons.machines.base.AbstractPylonTile;

import java.util.List;
Expand Down Expand Up @@ -170,6 +172,34 @@ public void tick() {
return;
}
}
} else if (HarvestCompat.hasCompat(blockState.getBlock())) {
Harvestable harvestable = HarvestCompat.getCompat(blockState.getBlock());
if (harvestable.isHarvestable(blockState)) {
if (requiresTool()) {
if (hoeSlot == -1) {
workStatus = Status.MISSING_TOOL;
return;
} else {
ItemStack replace = itemStackHandler.getStackInSlot(hoeSlot).copy();
if (replace.hurt(1, level.getRandom(), null)) {
replace.shrink(1);
}
itemStackHandler.setStackInSlot(hoeSlot, replace);
}
}

ItemStack stack = harvestable.harvest(level, workPos, blockState);
if (stack.isEmpty()) {
continue;
}

// try to insert as many drops as possible, discard the rest
boolean result = insertItemOrDiscard(itemHandler, stack);
if (!result) {
workStatus = Status.INVENTORY_FULL;
return;
}
}
}
}
}
Expand Down

0 comments on commit fba5536

Please sign in to comment.