Skip to content

Commit

Permalink
feat: added cooldown handling for prospecting pick
Browse files Browse the repository at this point in the history
  • Loading branch information
D4RKAR117 committed Jun 4, 2024
1 parent 17af9f1 commit d0a6ed6
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 55 deletions.
5 changes: 4 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
id 'eclipse'
id 'idea'
id 'maven-publish'
id 'net.neoforged.gradle.userdev' version '7.0.124'
id 'net.neoforged.gradle.userdev' version '7.0.139'
}

version = mod_version
Expand Down Expand Up @@ -53,6 +53,9 @@ runs {
client {
// Comma-separated list of namespaces to load gametests from. Empty = all namespaces.
systemProperty 'forge.enabledGameTestNamespaces', project.mod_id
devLogin {
enabled true
}
}

server {
Expand Down
10 changes: 7 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ org.gradle.debug=false
#read more on this at https://github.com/neoforged/NeoGradle/blob/NG_7.0/README.md#apply-parchment-mappings
# you can also find the latest versions at: https://parchmentmc.org/docs/getting-started
neogradle.subsystems.parchment.minecraftVersion=1.20.6
neogradle.subsystems.parchment.mappingsVersion=2024.05.01
neogradle.subsystems.parchment.mappingsVersion=2024.06.02

# NeoGradle Configs
neogradle.subsystems.devLogin.enabled=true

# Environment Properties
# You can find the latest versions here: https://projects.neoforged.net/neoforged/neoforge
# The Minecraft version must agree with the Neo version to get a valid artifact
Expand All @@ -16,7 +20,7 @@ minecraft_version=1.20.6
# as they do not follow standard versioning conventions.
minecraft_version_range=[1.20.6,1.21)
# The Neo version must agree with the Minecraft version to get a valid artifact
neo_version=20.6.100-beta
neo_version=20.6.104-beta
# The Neo version range can use any version of Neo as bounds
neo_version_range=[20.6,)
# The loader version range can only use the major version of FML as bounds
Expand All @@ -32,7 +36,7 @@ mod_name=Cog Works Engineering
# The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default.
mod_license=PolyFormPerimeter-1.0
# The mod version. See https://semver.org/
mod_version=0.3.1
mod_version=0.3.2
# The group ID for the mod. It is only important when publishing as an artifact to a Maven repository.
# This should match the base package used for the mod sources.
# See https://maven.apache.org/guides/mini/guide-naming-conventions.html
Expand Down
15 changes: 7 additions & 8 deletions src/main/java/org/darkar/cog_works/Registry.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import net.neoforged.neoforge.registries.DeferredItem;
import net.neoforged.neoforge.registries.DeferredRegister;
import org.darkar.cog_works.item.ProspectingPickItem;
import org.darkar.cog_works.item.component.ToolBehaviour;
import org.darkar.cog_works.item.component.IsDiggingSample;

import static org.darkar.cog_works.CogWorks.LOGGER;
import static org.darkar.cog_works.CogWorks.MOD_ID;
Expand Down Expand Up @@ -225,14 +225,13 @@ public static class DataComponents {
private static final DeferredRegister<DataComponentType<?>> DEFERRED_REGISTRY =
DeferredRegister.create(Registries.DATA_COMPONENT_TYPE, MOD_ID);

public static final DeferredHolder<DataComponentType<?>, DataComponentType<ToolBehaviour>> TOOL_BEHAVIOUR = DEFERRED_REGISTRY.register(
"tool_behaviour",
() -> DataComponentType.<ToolBehaviour>builder()
.persistent(ToolBehaviour.CODEC)
.networkSynchronized(ToolBehaviour.STREAM_CODEC)
.build()
public static final DeferredHolder<DataComponentType<?>, DataComponentType<IsDiggingSample>> IS_DIGGING_SAMPLE =
DEFERRED_REGISTRY.register("is_digging_sample", () -> DataComponentType.<IsDiggingSample>builder()
.persistent(IsDiggingSample.CODEC)
.networkSynchronized(IsDiggingSample.STREAM_CODEC)
.build()
);

);
private static void init(IEventBus bus) {
LOGGER.info("[Cog Works] Registering item data components ...");
DEFERRED_REGISTRY.register(bus);
Expand Down
51 changes: 34 additions & 17 deletions src/main/java/org/darkar/cog_works/item/ProspectingPickItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
import net.minecraft.tags.BlockTags;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.Entity;
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.state.BlockState;
import net.neoforged.neoforge.common.extensions.IItemExtension;
import org.darkar.cog_works.item.component.ToolBehaviour;
import org.darkar.cog_works.item.component.IsDiggingSample;
import org.darkar.cog_works.item.renderer.ProspectingPickItemRenderer;
import software.bernie.geckolib.animatable.GeoItem;
import software.bernie.geckolib.animatable.SingletonGeoAnimatable;
Expand All @@ -27,16 +28,16 @@

import java.util.function.Consumer;

import static org.darkar.cog_works.Registry.Items.DataComponents.TOOL_BEHAVIOUR;
import static org.darkar.cog_works.Registry.Items.DataComponents.IS_DIGGING_SAMPLE;

public class ProspectingPickItem extends Item implements GeoItem, IItemExtension {

private static final RawAnimation DIG_SAMPLE = RawAnimation.begin().thenPlay("animation.prospecting_pick.dig_sample");
private static final RawAnimation COLLECT_SAMPLE = RawAnimation.begin().thenPlay("animation.prospecting_pick.collect_sample");
private static final Properties itemProperties = new Properties().stacksTo(1).durability(128)
.component(TOOL_BEHAVIOUR, new ToolBehaviour(
"digging_sample", false))
.component(TOOL_BEHAVIOUR, new ToolBehaviour("collecting_sample", false));
.component(IS_DIGGING_SAMPLE,
IsDiggingSample.DEFAULT);

private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this);

public ProspectingPickItem() {
Expand Down Expand Up @@ -79,28 +80,44 @@ public InteractionResultHolder<ItemStack> use(Level level, Player player, Intera
return super.use(level, player, hand);
}


@Override
public boolean canAttackBlock(BlockState pState, Level pLevel, BlockPos pPos, Player pPlayer) {
return false;
}

private static final int COOLDOWN_TICKS = 8 * 20;

public void handleLeftClickBlock(Player player, Level level, BlockPos pos) {
if (level instanceof ServerLevel serverLevel) {

BlockState blockState = serverLevel.getBlockState(pos);
ItemStack itemStack = player.getItemInHand(InteractionHand.MAIN_HAND);


IsDiggingSample isDiggingSample = itemStack.getOrDefault(IS_DIGGING_SAMPLE, IsDiggingSample.DEFAULT);
// Check if block is stone or deepslate
if (blockState.is(BlockTags.STONE_ORE_REPLACEABLES) || blockState.is(BlockTags.DEEPSLATE_ORE_REPLACEABLES)) {
ToolBehaviour toolBehaviour = itemStack.getOrDefault(TOOL_BEHAVIOUR, new ToolBehaviour(
"digging_sample", false));
if ((blockState.is(BlockTags.STONE_ORE_REPLACEABLES) || blockState.is(BlockTags.DEEPSLATE_ORE_REPLACEABLES)) && !isDiggingSample.value()) {

if(toolBehaviour.equals(new ToolBehaviour("digging_sample", true))) return;

toolBehaviour = new ToolBehaviour("digging_sample", true);
itemStack.set(TOOL_BEHAVIOUR, toolBehaviour);

player.sendSystemMessage(Component.literal("Digging valid sample..."));
isDiggingSample = new IsDiggingSample(true);
itemStack.set(IS_DIGGING_SAMPLE, isDiggingSample);
player.sendSystemMessage(Component.literal("Is Digging Sample After: " + isDiggingSample.value() ));
triggerAnim(player, GeoItem.getOrAssignId(itemStack, serverLevel), "dig_sample_controller", "dig_sample");
player.getCooldowns().addCooldown(this, COOLDOWN_TICKS);
}

toolBehaviour = new ToolBehaviour("digging_sample", false);
itemStack.set(TOOL_BEHAVIOUR, toolBehaviour);
}
}

private int lastedCooldownTicks = 0;
@Override
public void inventoryTick(ItemStack stack, Level level, Entity entity, int slotId, boolean isSelected) {
if(!level.isClientSide()) {
IsDiggingSample isDiggingSample = stack.getOrDefault(IS_DIGGING_SAMPLE, IsDiggingSample.DEFAULT);
if(isDiggingSample.value()) lastedCooldownTicks++;
boolean shouldResetCooldown = isDiggingSample.value() && lastedCooldownTicks >= COOLDOWN_TICKS;
if(shouldResetCooldown) {
stack.set(IS_DIGGING_SAMPLE, IsDiggingSample.DEFAULT);
lastedCooldownTicks = 0;
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.darkar.cog_works.item.component;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import io.netty.buffer.ByteBuf;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;

public record IsDiggingSample(boolean value) {

public static final IsDiggingSample DEFAULT = new IsDiggingSample(false);

public static final Codec<IsDiggingSample> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codec.BOOL.fieldOf("value").forGetter(IsDiggingSample::value)
).apply(instance, IsDiggingSample::new));


public static final StreamCodec<ByteBuf, IsDiggingSample> STREAM_CODEC = StreamCodec.composite(
ByteBufCodecs.BOOL, IsDiggingSample::value,
IsDiggingSample::new
);

}

This file was deleted.

10 changes: 6 additions & 4 deletions src/main/java/org/darkar/cog_works/item/events/ItemEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,20 @@ public static void onPlayerLeftClickBlock(PlayerInteractEvent.LeftClickBlock eve
Player player = event.getEntity();
BlockPos pos = event.getPos();
LogicalSide side = event.getSide();
PlayerInteractEvent.LeftClickBlock.Action action = event.getAction();


if (itemStack.isEmpty()) return;
if (item instanceof ProspectingPickItem) {
if (!side.isClient()) {
if(!player.isCreative()) {
((ProspectingPickItem) item).handleLeftClickBlock(player, level, pos);
if (side.isServer()) {
if(!player.isCreative() && action == PlayerInteractEvent.LeftClickBlock.Action.START) {
if(!event.isCanceled()) ((ProspectingPickItem) item).handleLeftClickBlock(player, level, pos);
event.setCanceled(true);
}
return;
}

event.setCanceled(true);
if(side.isClient()) event.setCanceled(true);
}
}

Expand Down

0 comments on commit d0a6ed6

Please sign in to comment.