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

GT Base classes for tools #3

Open
wants to merge 1 commit into
base: 1.17
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
29 changes: 29 additions & 0 deletions src/main/java/gregtech/api/items/toolitem/MiningToolItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package gregtech.api.items.toolitem;

import gregtech.api.unification.material.Material;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.EnchantmentTarget;
import net.minecraft.tag.Tag;

public class MiningToolItem extends ToolItem {
Tag<Block> effectiveBlocks;

public MiningToolItem(ToolItemSettings settings, ToolItemType toolItemType, Material material,
Tag<Block> effectiveBlocks) {
super(settings, toolItemType, material);
this.effectiveBlocks = effectiveBlocks;
}

@Override
public boolean canApplyEnchantment(Enchantment enchantment) {
return enchantment.type == EnchantmentTarget.DIGGER ||
enchantment.type.isAcceptableItem(this);
}

@Override
protected boolean isCorrectToolForBlock(BlockState state) {
return state.isIn(this.effectiveBlocks);
}
}
30 changes: 19 additions & 11 deletions src/main/java/gregtech/api/items/toolitem/ToolItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ public abstract class ToolItem extends GTItem implements CustomDamageItem, Custo
private final float attackDamageMultiplier;

protected final int damagePerSpecialAction;
private final int damagePerBlockBreak;
protected final int damagePerBlockBreak;
private final int damagePerEntityAttack;
private final int damagePerCraft;

private final long energyPerDurabilityPoint;
protected final long energyPerDurabilityPoint;
private final int itemDamageChance;

private final String translationKey;
Expand Down Expand Up @@ -100,6 +100,10 @@ public Material getMaterial() {
return material;
}

public int getDamagePerBlockBreak(){
return damagePerBlockBreak;
}

@Override
protected String getOrCreateTranslationKey() {
return translationKey;
Expand Down Expand Up @@ -175,8 +179,7 @@ public int getMiningLevel() {

@Override
public final boolean isSuitableFor(BlockState state) {
int miningLevel = getMiningLevel();
return MiningLevelHelper.checkHarvestLevelRequirements(state, miningLevel) &&
return MiningLevelHelper.checkHarvestLevelRequirements(state, getMiningLevel()) &&
isCorrectToolForBlock(state);
}

Expand Down Expand Up @@ -261,15 +264,10 @@ public boolean postMine(ItemStack stack, World world, BlockState state, BlockPos

@Override
public Pair<ItemDamageResult, ItemStack> attemptDamageItem(ItemStack itemStack, int damage, @Nullable LivingEntity entity, Simulation simulate) {
Ref<ItemStack> stackRef = new Ref<>(itemStack.copy());
LimitedConsumer<ItemStack> excess = LimitedConsumer.rejecting();
Random random = new Random();

if (entity instanceof PlayerEntity playerEntity) {
excess = LimitedConsumer.fromConsumer(PlayerInvUtil.createPlayerInsertable(playerEntity));
}

ElectricItem electricItem = GTAttributes.ELECTRIC_ITEM.getFirstOrNull(stackRef, excess);
Ref<ItemStack> stackRef = new Ref<>(itemStack.copy());
ElectricItem electricItem = getElectricItem(stackRef, entity);

if (electricItem != null) {
long energyToUse = energyPerDurabilityPoint * damage;
Expand Down Expand Up @@ -299,4 +297,14 @@ public Pair<ItemDamageResult, ItemStack> attemptDamageItem(ItemStack itemStack,
}
return Pair.of(ItemDamageResult.DAMAGED, stackRef.obj);
}

@Nullable
public ElectricItem getElectricItem(Ref<ItemStack> stackRef, @Nullable LivingEntity entity) {
LimitedConsumer<ItemStack> excess = LimitedConsumer.rejecting();

if (entity instanceof PlayerEntity playerEntity) {
excess = LimitedConsumer.fromConsumer(PlayerInvUtil.createPlayerInsertable(playerEntity));
}
return GTAttributes.ELECTRIC_ITEM.getFirstOrNull(stackRef, excess);
}
}
37 changes: 37 additions & 0 deletions src/main/java/gregtech/api/util/TaskScheduler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package gregtech.api.util;

import gregtech.api.util.function.Task;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerWorldEvents;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.world.World;

import java.util.*;

public class TaskScheduler {
private static Map<World, List<Task>> tasksPerWorld = new HashMap<>();

public static void scheduleTask(World world, Task task) {
if(!world.isClient) {
throw new IllegalArgumentException("Attempt to schedule task on client world!");
}
List<Task> taskList = tasksPerWorld.computeIfAbsent(world, k -> new ArrayList<>());
taskList.add(task);
}

static {
ServerWorldEvents.UNLOAD.register((MinecraftServer server, ServerWorld world)->{
if(world.isClient()) {
tasksPerWorld.remove(world);
}
});

ServerTickEvents.START_WORLD_TICK.register((ServerWorld world) -> {
if(world.isClient()) {
List<Task> taskList = tasksPerWorld.getOrDefault(world, Collections.emptyList());
taskList.removeIf(task -> !task.run());
}
});
}
}
7 changes: 7 additions & 0 deletions src/main/java/gregtech/api/util/function/Task.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package gregtech.api.util.function;

public interface Task {

boolean run();

}
30 changes: 29 additions & 1 deletion src/main/java/gregtech/common/tools/AxeItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,27 @@

import alexiil.mc.lib.attributes.Simulation;
import gregtech.api.capability.item.CustomDamageItem;
import gregtech.api.items.toolitem.MiningToolItem;
import gregtech.api.items.toolitem.ToolItemSettings;
import gregtech.api.items.toolitem.ToolItemType;
import gregtech.api.unification.material.Material;
import gregtech.api.util.TaskScheduler;
import gregtech.mixin.accessor.AxeItemAccessor;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Oxidizable;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.HoneycombItem;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUsageContext;
import net.minecraft.item.Items;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.tag.BlockTags;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.WorldEvents;
Expand All @@ -27,7 +33,7 @@
public class AxeItem extends MiningToolItem {

public AxeItem(ToolItemSettings settings, ToolItemType toolItemType, Material material) {
super(settings, toolItemType, material);
super(settings, toolItemType, material, BlockTags.AXE_MINEABLE);
}

private static Optional<BlockState> tryConvertBlockAndPlayEffects(ItemUsageContext context) {
Expand Down Expand Up @@ -93,4 +99,26 @@ public ActionResult useOnBlock(ItemUsageContext context) {
}
return ActionResult.PASS;
}

public static boolean applyTimberAxe(ItemStack itemStack, World world, BlockState state, BlockPos blockPos, LivingEntity player) {
if(TreeChopTask.isLogBlock(state) == 1) {
if(!world.isClient()) {
ServerPlayerEntity playerMP = (ServerPlayerEntity) player;
if (playerMP.getItemCooldownManager().isCoolingDown(itemStack.getItem()))
return false;
TreeChopTask treeChopTask = new TreeChopTask(blockPos, world, playerMP, itemStack);
TaskScheduler.scheduleTask(world, treeChopTask);
}
return true;
}
return false;
}


@Override
public boolean postMine(ItemStack stack, World world, BlockState state, BlockPos pos, LivingEntity miner) {
return miner.isSneaking() ?
CustomDamageItem.damageItem(miner, Hand.MAIN_HAND, damagePerBlockBreak, Simulation.ACTION) :
applyTimberAxe(stack, world, state, pos, miner);
}
}
14 changes: 14 additions & 0 deletions src/main/java/gregtech/common/tools/BranchCutterItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package gregtech.common.tools;

import gregtech.api.items.toolitem.MiningToolItem;
import gregtech.api.items.toolitem.ToolItemSettings;
import gregtech.api.items.toolitem.ToolItemType;
import gregtech.api.unification.material.Material;
import net.minecraft.tag.BlockTags;

public class BranchCutterItem extends MiningToolItem {
// TODO: Do either 100% sapling drop without forestry, capitator-like leaves cutting or remove entirely
public BranchCutterItem(ToolItemSettings settings, ToolItemType toolItemType, Material material) {
super(settings, toolItemType, material, BlockTags.LEAVES);
}
}
4 changes: 3 additions & 1 deletion src/main/java/gregtech/common/tools/HoeItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import alexiil.mc.lib.attributes.Simulation;
import com.mojang.datafixers.util.Pair;
import gregtech.api.capability.item.CustomDamageItem;
import gregtech.api.items.toolitem.MiningToolItem;
import gregtech.api.items.toolitem.ToolItemSettings;
import gregtech.api.items.toolitem.ToolItemType;
import gregtech.api.unification.material.Material;
Expand All @@ -13,6 +14,7 @@
import net.minecraft.item.Items;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.tag.BlockTags;
import net.minecraft.util.ActionResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
Expand All @@ -23,7 +25,7 @@
public class HoeItem extends MiningToolItem {

public HoeItem(ToolItemSettings settings, ToolItemType toolItemType, Material material) {
super(settings, toolItemType, material);
super(settings, toolItemType, material, BlockTags.HOE_MINEABLE);
}

@Override
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/gregtech/common/tools/PickaxeItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package gregtech.common.tools;
import gregtech.api.items.toolitem.MiningToolItem;
import gregtech.api.items.toolitem.ToolItemSettings;
import gregtech.api.items.toolitem.ToolItemType;
import gregtech.api.unification.material.Material;
import net.minecraft.tag.BlockTags;

public class PickaxeItem extends MiningToolItem {

public PickaxeItem(ToolItemSettings settings, ToolItemType toolItemType, Material material) {
super(settings, toolItemType, material, BlockTags.PICKAXE_MINEABLE);
}
}
4 changes: 3 additions & 1 deletion src/main/java/gregtech/common/tools/ShovelItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import alexiil.mc.lib.attributes.Simulation;
import gregtech.api.capability.item.CustomDamageItem;
import gregtech.api.items.toolitem.MiningToolItem;
import gregtech.api.items.toolitem.ToolItemSettings;
import gregtech.api.items.toolitem.ToolItemType;
import gregtech.api.unification.material.Material;
Expand All @@ -13,6 +14,7 @@
import net.minecraft.item.ItemUsageContext;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.tag.BlockTags;
import net.minecraft.util.ActionResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
Expand All @@ -25,7 +27,7 @@
public class ShovelItem extends MiningToolItem {

public ShovelItem(ToolItemSettings settings, ToolItemType toolItemType, Material material) {
super(settings, toolItemType, material);
super(settings, toolItemType, material, BlockTags.SHOVEL_MINEABLE);
}

private static Optional<BlockState> tryConvertBlockAndPlayEffects(ItemUsageContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@
import gregtech.api.items.toolitem.ToolItemSettings;
import gregtech.api.items.toolitem.ToolItemType;
import gregtech.api.unification.material.Material;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.EnchantmentTarget;

public class MiningToolItem extends ToolItem {

public MiningToolItem(ToolItemSettings settings, ToolItemType toolItemType, Material material) {
public class SwordItem extends ToolItem {
public SwordItem(ToolItemSettings settings, ToolItemType toolItemType, Material material) {
super(settings, toolItemType, material);
}

@Override
public boolean canApplyEnchantment(Enchantment enchantment) {
return enchantment.type == EnchantmentTarget.DIGGER ||
return enchantment.type == EnchantmentTarget.WEAPON ||
enchantment.type.isAcceptableItem(this);
}

@Override
protected boolean isCorrectToolForBlock(BlockState state) {
return state.isOf(Blocks.COBWEB);
}
}
Loading