Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically pickup items when cutting down trees (1.21.0) #2

Open
wants to merge 2 commits into
base: 1.21.0
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class ConfigHandler extends DuskConfig {
@Entry public static boolean increaseHarvestingTimePerLog = true;
@Entry(min = 0.01, max = 10.0) public static double increasedHarvestingTimePerLogModifier = 0.2;
@Entry(min = 1, max = 16) public static int amountOfLeavesBrokenPerTick = 5;
public static boolean automaticallyPickupItems = false;

public static void initConfig() {
configMetaData.put("mustHoldAxeForTreeHarvest", Arrays.asList(
Expand Down Expand Up @@ -76,6 +77,9 @@ public static void initConfig() {
configMetaData.put("amountOfLeavesBrokenPerTick", Arrays.asList(
"How many leaves should be broken per tick after a tree has been harvested. Increasing this will speed up the fast leaf decay, but costs more processing power per tick."
));
configMetaData.put("automaticallyPickupItems", Arrays.asList(
"If enabled, automatically picks up items that are dropped when a tree is harvested including the leaf drops."
));

DuskConfig.init(Reference.NAME, Reference.MOD_ID, ConfigHandler.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class Variables {
public static HashMap<BlockPos, Integer> highestleaf = new HashMap<BlockPos, Integer>();
public static CopyOnWriteArrayList<Triplet<Date, BlockPos, CopyOnWriteArrayList<BlockPos>>> saplingPositions = new CopyOnWriteArrayList<Triplet<Date, BlockPos, CopyOnWriteArrayList<BlockPos>>>();

public static final HashMap<Level, CopyOnWriteArrayList<BlockPos>> processTickLeaves = new HashMap<Level, CopyOnWriteArrayList<BlockPos>>();
public static final HashMap<Level, CopyOnWriteArrayList<BlockPos>> processBreakLeaves = new HashMap<Level, CopyOnWriteArrayList<BlockPos>>();
public static final HashMap<Pair<Level, Player>, Pair<Date, Integer>> harvestSpeedCache = new HashMap<Pair<Level, Player>, Pair<Date, Integer>>();
public static final HashMap<Level, CopyOnWriteArrayList<BlockPos>> processTickLeaves = new HashMap<>();
public static final HashMap<Level, CopyOnWriteArrayList<Pair<BlockPos, Player>>> processBreakLeaves = new HashMap<>();
public static final HashMap<Pair<Level, Player>, Pair<Date, Integer>> harvestSpeedCache = new HashMap<>();
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
package com.natamus.treeharvester.events;

import com.mojang.datafixers.util.Pair;
import com.natamus.collective.functions.BlockFunctions;
import com.natamus.collective.functions.CompareBlockFunctions;
import com.natamus.collective.functions.HashMapFunctions;
import com.natamus.treeharvester.config.ConfigHandler;
import com.natamus.treeharvester.data.Variables;
import com.natamus.treeharvester.processing.SaplingProcessing;
import com.natamus.treeharvester.util.Util;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.player.Player;
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.Blocks;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;

import java.util.EnumSet;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class LeafEvents {
Expand All @@ -29,11 +35,28 @@ else if (sizeLeft > 1000) {
leavesLeft *=3;
}

for (BlockPos leafPos : Variables.processBreakLeaves.get(level)) {
Variables.processBreakLeaves.get(level).remove(leafPos);
for (Pair<BlockPos, Player> leafPair : Variables.processBreakLeaves.get(level)) {
BlockPos leafPos = leafPair.getFirst();
BlockState leafState = level.getBlockState(leafPos);
BlockEntity blockEntity = level.getBlockEntity(leafPos);
Variables.processBreakLeaves.get(level).remove(leafPair);

if (Util.isTreeLeaf(level.getBlockState(leafPos).getBlock())) {
BlockFunctions.dropBlock(level, leafPos);
if(ConfigHandler.automaticallyPickupItems && leafPair.getSecond() != null) {
List<ItemStack> drops = Block.getDrops(leafState, (ServerLevel)level, leafPos, blockEntity);
for (ItemStack drop : drops) {
if(Util.isSapling(Block.byItem(drop.getItem()))) {
SaplingProcessing.onSaplingItem(level, leafPair.getSecond(), drop, leafPos);
}
if(!leafPair.getSecond().addItem(drop)) {
Block.popResource(level, leafPos, drop);
}
}
level.setBlock(leafPos, Blocks.AIR.defaultBlockState(), 3);
}
else {
BlockFunctions.dropBlock(level, leafPos);
}

leavesLeft--;
if (leavesLeft < 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.natamus.treeharvester.events;

import com.natamus.collective.functions.BlockPosFunctions;
import com.natamus.treeharvester.config.ConfigHandler;
import com.natamus.treeharvester.data.Variables;
import com.natamus.treeharvester.util.Util;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.Entity;
Expand All @@ -12,10 +10,8 @@
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import oshi.util.tuples.Triplet;

import java.util.Date;
import java.util.concurrent.CopyOnWriteArrayList;
import static com.natamus.treeharvester.processing.SaplingProcessing.placeSaplings;

public class SaplingEvents {
public static void onSaplingItem(Level level, Entity entity) {
Expand Down Expand Up @@ -46,31 +42,6 @@ public static void onSaplingItem(Level level, Entity entity) {
BlockPos itemPos = itemEntity.blockPosition();
BlockPos yZeroItemPos = itemPos.atY(0);

Date now = new Date();
for (Triplet<Date, BlockPos, CopyOnWriteArrayList<BlockPos>> triplet : Variables.saplingPositions) {
long ms = (now.getTime()-triplet.getA().getTime());
if (ms > 2000) {
Variables.saplingPositions.remove(triplet);
continue;
}

if (BlockPosFunctions.withinDistance(yZeroItemPos, triplet.getB().atY(0), 6)) {
for (BlockPos lowerLog : triplet.getC()) {
if (itemStack.getCount() > 0) {
level.setBlock(lowerLog, block.defaultBlockState(), 3);
itemStack.shrink(1);
triplet.getC().remove(lowerLog);
}
}

if (triplet.getC().size() == 0) {
Variables.saplingPositions.remove(triplet);
}
}

if (itemStack.getCount() == 0) {
return;
}
}
placeSaplings(level, yZeroItemPos, itemStack, block);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
import com.natamus.treeharvester.processing.TreeProcessing;
import com.natamus.treeharvester.util.Util;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
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.Blocks;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
Expand Down Expand Up @@ -103,7 +105,17 @@ public static boolean onTreeHarvest(Level level, Player player, BlockPos bpos, B
BlockState logstate = level.getBlockState(logpos);
Block log = logstate.getBlock();

BlockFunctions.dropBlock(level, logpos);
if(ConfigHandler.automaticallyPickupItems) {
List<ItemStack> drops = Block.getDrops(logstate, (ServerLevel)level, logpos, blockEntity);
for (ItemStack drop : drops) {
if(!serverPlayer.addItem(drop)) {
Block.popResource(level, logpos, drop);
}
}
level.setBlock(logpos, Blocks.AIR.defaultBlockState(), 3);
} else {
BlockFunctions.dropBlock(level, logpos);
}
//ForgeEventFactory.onEntityDestroyBlock(player, logpos, logstate);

if (!player.isCreative()) {
Expand All @@ -127,7 +139,7 @@ public static boolean onTreeHarvest(Level level, Player player, BlockPos bpos, B
}
}

LeafProcessing.breakTreeLeaves(level, logsToBreak, bpos, highestLogPos);
LeafProcessing.breakTreeLeaves(level, player, logsToBreak, bpos, highestLogPos);

return logsToBreak.size() == 0;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.natamus.treeharvester.processing;

import com.mojang.datafixers.util.Pair;
import com.natamus.collective.functions.BlockPosFunctions;
import com.natamus.treeharvester.data.Variables;
import com.natamus.treeharvester.util.Util;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;

Expand All @@ -13,7 +15,7 @@
import java.util.concurrent.CopyOnWriteArrayList;

public class LeafProcessing {
public static void breakTreeLeaves(Level level, List<BlockPos> logsToBreak, BlockPos lowestCenterLogPos, BlockPos highestLogPos) {
public static void breakTreeLeaves(Level level, Player player, List<BlockPos> logsToBreak, BlockPos lowestCenterLogPos, BlockPos highestLogPos) {
if (level.isClientSide) {
return;
}
Expand All @@ -40,7 +42,7 @@ public static void breakTreeLeaves(Level level, List<BlockPos> logsToBreak, Bloc
}

if (!Variables.processBreakLeaves.containsKey(level)) {
Variables.processBreakLeaves.put(level, new CopyOnWriteArrayList<BlockPos>());
Variables.processBreakLeaves.put(level, new CopyOnWriteArrayList<>());
}

BlockPos highestLeafPos = highestLogPos.above().immutable();
Expand Down Expand Up @@ -89,8 +91,8 @@ public static void breakTreeLeaves(Level level, List<BlockPos> logsToBreak, Bloc
}
}

if (!Variables.processBreakLeaves.get(level).contains(treeBlockPos)) {
Variables.processBreakLeaves.get(level).add(treeBlockPos);
if (!Variables.processBreakLeaves.get(level).contains(new Pair<>(treeBlockPos, player))) {
Variables.processBreakLeaves.get(level).add(new Pair<>(treeBlockPos, player));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.natamus.treeharvester.processing;

import com.natamus.collective.functions.BlockPosFunctions;
import com.natamus.treeharvester.config.ConfigHandler;
import com.natamus.treeharvester.data.Variables;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import oshi.util.tuples.Triplet;

import java.util.Date;
import java.util.concurrent.CopyOnWriteArrayList;

public class SaplingProcessing {
public static void onSaplingItem(Level level, Player player, ItemStack itemStack, BlockPos leafPos) {
if (level.isClientSide) {
return;
}

if (!ConfigHandler.replaceSaplingOnTreeHarvest || !ConfigHandler.automaticallyPickupItems) {
return;
}

Block block = Block.byItem(itemStack.getItem());
BlockPos yZeroPlayerPos = player.getOnPos().atY(0);
// BlockPos yZeroItemPos = leafPos.atY(0);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ricksouth I wish to hear your input on this. Should it search based on the player position or the leaf position?


placeSaplings(level, yZeroPlayerPos, itemStack, block);
// placeSaplings(level, yZeroItemPos, itemStack, block);

if (itemStack.getCount() > 0) {
if(!player.addItem(itemStack)) {
Block.popResource(level, leafPos, itemStack);
}
}
}

public static void placeSaplings(Level level, BlockPos yZeroItemPos, ItemStack itemStack, Block block) {
Date now = new Date();
for (Triplet<Date, BlockPos, CopyOnWriteArrayList<BlockPos>> triplet : Variables.saplingPositions) {
long ms = (now.getTime()-triplet.getA().getTime());
if (ms > 2000) {
Variables.saplingPositions.remove(triplet);
continue;
}

if (BlockPosFunctions.withinDistance(yZeroItemPos, triplet.getB().atY(0), 6)) {
for (BlockPos lowerLog : triplet.getC()) {
if (itemStack.getCount() > 0) {
level.setBlock(lowerLog, block.defaultBlockState(), 3);
itemStack.shrink(1);
triplet.getC().remove(lowerLog);
}
}

if (triplet.getC().size() == 0) {
Variables.saplingPositions.remove(triplet);
}
}

if (itemStack.getCount() == 0) {
return;
}
}
}
}