Skip to content

Commit

Permalink
simple harvest event refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
MehVahdJukaar committed Jan 25, 2024
1 parent 522f22a commit 47002b3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

import net.minecraft.core.BlockPos;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.eventbus.api.Cancelable;
import net.minecraftforge.eventbus.api.Event;
import org.jetbrains.annotations.Nullable;
import org.violetmoon.quark.content.tweaks.module.SimpleHarvestModule;

/**
* Used primarily for double crops which need extra checks before they are considered ready.
Expand All @@ -15,62 +19,58 @@
@Cancelable
public class SimpleHarvestEvent extends Event {

public final BlockState blockState;
public final BlockPos pos;
public final InteractionHand hand;
public final Player player;
public final Source type;
private BlockPos newTarget;
private ActionType action;
public final BlockState blockState;
public final BlockPos pos;
public final Level level;
public final @Nullable InteractionHand hand;
public final @Nullable Entity entity;
private BlockPos newTarget;
private ActionType action;

public SimpleHarvestEvent(BlockState blockState, BlockPos pos, InteractionHand hand,
Player player, boolean isHoe, ActionType actionType) {
this.blockState = blockState;
this.pos = pos;
this.hand = hand;
this.player = player;
this.newTarget = pos;
this.type = isHoe ? Source.HOE : Source.RIGHT_CLICK;
this.action = actionType;
}
//Note that entity could be a player or villager
public SimpleHarvestEvent(BlockState blockState, BlockPos pos, Level level, @Nullable InteractionHand hand,
@Nullable Entity entity, ActionType originalActionType) {
this.blockState = blockState;
this.pos = pos;
this.hand = hand;
this.level = level;
this.entity = entity;
this.newTarget = pos;
this.action = originalActionType;
}

/**
* Used for double crops and the like. Pass a new position which should be broken instead
*
* @param pos new target position
*/
public void setTargetPos(BlockPos pos) {
this.newTarget = pos;
}
/**
* Used for double crops and the like. Pass a new position which should be broken instead
*
* @param pos new target position
*/
public void setTargetPos(BlockPos pos) {
this.newTarget = pos;
}

public Source getInteractionSource() {
return type;
}
@Override
public void setCanceled(boolean cancel) {
if (cancel)
action = ActionType.NONE;
super.setCanceled(cancel);
}

@Override
public void setCanceled(boolean cancel) {
if(cancel)
action = ActionType.NONE;
super.setCanceled(cancel);
}
//Click will work just for players!
public enum ActionType {
NONE, CLICK, HARVEST;
}

public enum ActionType {
NONE, CLICK, HARVEST;
}
public ActionType getAction() {
return action;
}

public ActionType getAction() {
return action;
}
public void setAction(ActionType action) {
this.action = action;
}

public void setAction(ActionType action) {
this.action = action;
}
public BlockPos getTargetPos() {
return newTarget;
}

public BlockPos getTargetPos() {
return newTarget;
}

public enum Source {
RIGHT_CLICK, HOE
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,6 @@ private static boolean harvestAndReplant(Level level, BlockPos pos, BlockState i
}else{
// is this needed? isnt enchantent level handled intenrally?
copy = entity.getItemInHand(hand).copy();

//TODO: I'm pretty sure all this is unnecessary and can be removed
int fortune = Quark.ZETA.itemExtensions.get(copy).getEnchantmentLevelZeta(copy, Enchantments.BLOCK_FORTUNE);

Map<Enchantment, Integer> enchMap = EnchantmentHelper.getEnchantments(copy);
enchMap.put(Enchantments.BLOCK_FORTUNE, fortune);
EnchantmentHelper.setEnchantments(enchMap, copy);
}

MutableBoolean hasTaken = new MutableBoolean(false);
Expand Down

0 comments on commit 47002b3

Please sign in to comment.