Skip to content
Merged
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 @@ -20,6 +20,7 @@

import com.craftingdead.core.client.gui.screen.inventory.CraftingScreen;
import com.craftingdead.core.network.message.play.DamageHandcuffsMessage;
import com.craftingdead.core.network.message.play.RightClickStateMessage;
import com.craftingdead.core.network.message.play.TraumaPacket;
import com.craftingdead.core.trauma.TraumaSeverity;
import com.craftingdead.core.world.action.RemoveMagazineAction;
Expand Down Expand Up @@ -198,6 +199,9 @@ public class ClientDist implements ModDist {
private boolean wasSneaking;
private long lastSneakPressTime;

public int rightClickTicks = 0;
public boolean wasRightClickDown = false;

private float lastPitch;
private float lastYaw;
private float lastRoll;
Expand Down Expand Up @@ -492,6 +496,26 @@ public void handleClientTick(TickEvent.ClientTickEvent event) {
return;
}

if (this.minecraft.player == null || this.minecraft.getConnection() == null) {
return;
}

boolean isRightClickDown = this.minecraft.options.keyUse.isDown();
int ticks = this.minecraft.player.getUseItemRemainingTicks();

if (isRightClickDown || ticks > 0) {
if (!this.wasRightClickDown || ticks == 0) {
this.rightClickTicks = 0;
}

this.rightClickTicks++;
} else {
this.rightClickTicks = 0;
}
this.wasRightClickDown = isRightClickDown;
NetworkChannel.PLAY.getSimpleChannel().sendToServer(new RightClickStateMessage(
isRightClickDown, this.rightClickTicks));

var player = this.getPlayerExtension().orElse(null);
if (player != null) {
if (this.traumaAimTicks > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package com.craftingdead.core.network;

import com.craftingdead.core.CraftingDead;
import com.craftingdead.core.network.message.play.BlockDestroyActionMessage;
import com.craftingdead.core.network.message.play.CancelActionMessage;
import com.craftingdead.core.network.message.play.CrouchMessage;
import com.craftingdead.core.network.message.play.DamageHandcuffsMessage;
Expand All @@ -30,6 +31,7 @@
import com.craftingdead.core.network.message.play.OpenStorageMessage;
import com.craftingdead.core.network.message.play.ParachuteSyncMessage;
import com.craftingdead.core.network.message.play.PerformActionMessage;
import com.craftingdead.core.network.message.play.RightClickStateMessage;
import com.craftingdead.core.network.message.play.SecondaryActionMessage;
import com.craftingdead.core.network.message.play.SetFireModeMessage;
import com.craftingdead.core.network.message.play.SyncGunContainerSlotMessage;
Expand Down Expand Up @@ -188,6 +190,20 @@ public void registerMessages(SimpleChannel simpleChannel) {
.decoder(SyncProtectionConfigMessage::decode)
.consumer(SyncProtectionConfigMessage::handle)
.add();

simpleChannel
.messageBuilder(RightClickStateMessage.class, 0x15, NetworkDirection.PLAY_TO_SERVER)
.encoder(RightClickStateMessage::encode)
.decoder(RightClickStateMessage::decode)
.consumer(RightClickStateMessage::handle)
.add();

simpleChannel
.messageBuilder(BlockDestroyActionMessage.class, 0x16, NetworkDirection.PLAY_TO_SERVER)
.encoder(BlockDestroyActionMessage::encode)
.decoder(BlockDestroyActionMessage::decode)
.consumer(BlockDestroyActionMessage::handle)
.add();
}
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Crafting Dead
* Copyright (C) 2022 NexusNode LTD
*
* This Non-Commercial Software License Agreement (the "Agreement") is made between
* you (the "Licensee") and NEXUSNODE (BRAD HUNTER). (the "Licensor").
* By installing or otherwise using Crafting Dead (the "Software"), you agree to be
* bound by the terms and conditions of this Agreement as may be revised from time
* to time at Licensor's sole discretion.
*
* If you do not agree to the terms and conditions of this Agreement do not download,
* copy, reproduce or otherwise use any of the source code available online at any time.
*
* https://github.com/nexusnode/crafting-dead/blob/1.18.x/LICENSE.txt
*
* https://craftingdead.net/terms.php
*/

package com.craftingdead.core.network.message.play;

import java.util.function.Supplier;
import net.minecraft.core.BlockPos;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraftforge.network.NetworkEvent;

public record BlockDestroyActionMessage(BlockPos pos) {

public static void encode(BlockDestroyActionMessage msg, FriendlyByteBuf buf) {
buf.writeBlockPos(msg.pos());
}

public static BlockDestroyActionMessage decode(FriendlyByteBuf buf) {
return new BlockDestroyActionMessage(buf.readBlockPos());
}

public static void handle(BlockDestroyActionMessage msg, Supplier<NetworkEvent.Context> ctx) {
ctx.get().enqueueWork(() -> {
var player = ctx.get().getSender();
if (player == null) {
return;
}
var state = player.getLevel().getBlockState(msg.pos);
if (!state.isAir()) {
player.getLevel().levelEvent(2001, msg.pos, Block.getId(state));
player.getLevel().setBlock(msg.pos, Blocks.AIR.defaultBlockState(), 3);
}
});
ctx.get().setPacketHandled(true);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Crafting Dead
* Copyright (C) 2022 NexusNode LTD
*
* This Non-Commercial Software License Agreement (the "Agreement") is made between
* you (the "Licensee") and NEXUSNODE (BRAD HUNTER). (the "Licensor").
* By installing or otherwise using Crafting Dead (the "Software"), you agree to be
* bound by the terms and conditions of this Agreement as may be revised from time
* to time at Licensor's sole discretion.
*
* If you do not agree to the terms and conditions of this Agreement do not download,
* copy, reproduce or otherwise use any of the source code available online at any time.
*
* https://github.com/nexusnode/crafting-dead/blob/1.18.x/LICENSE.txt
*
* https://craftingdead.net/terms.php
*/

package com.craftingdead.core.network.message.play;

import com.craftingdead.core.world.entity.extension.PlayerExtension;
import java.util.function.Supplier;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraftforge.network.NetworkEvent.Context;

public record RightClickStateMessage(boolean isDown, int ticks) {

public static void encode(RightClickStateMessage msg, FriendlyByteBuf buf) {
buf.writeBoolean(msg.isDown());
buf.writeInt(msg.ticks());
}

public static RightClickStateMessage decode(FriendlyByteBuf buf) {
return new RightClickStateMessage(buf.readBoolean(), buf.readInt());
}

public static void handle(RightClickStateMessage msg, Supplier<Context> ctx) {
ctx.get().enqueueWork(() -> {
var player = ctx.get().getSender();
if (player == null || !player.isAlive()) {
return;
}
var extension = PlayerExtension.getOrThrow(player);
if (msg.isDown()) {
extension.setHoldingRightClick(true);
int fixedTicks = msg.ticks() > 0 ? msg.ticks() : extension.getRightClickTicks();
extension.setRightClickTicks(fixedTicks);
} else {
extension.setHoldingRightClick(false);
extension.setRightClickTicks(0);
}
});
ctx.get().setPacketHandled(true);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

package com.craftingdead.core.world.action.item;

import com.craftingdead.core.network.NetworkChannel;
import com.craftingdead.core.network.message.play.BlockDestroyActionMessage;
import com.craftingdead.core.world.action.ActionObserver;
import com.craftingdead.core.world.action.ProgressBar;
import com.craftingdead.core.world.entity.extension.LivingExtension;
Expand Down Expand Up @@ -75,12 +77,18 @@ public boolean start(boolean simulate) {
&& this.type.getPredicate().test(this.blockState);
}


@Override
public boolean tick() {
if (!this.performer.level().isClientSide() && !this.isWithinMaxDistance()) {
this.performer.cancelAction(true);
return false;
}
// Makes sure it happens when the action is finished
// this.progress might not reach so >= 0.99F is the safer
if (this.progress >= 0.99F) {
this.destroyBlockAction();
}
return super.tick();
}

Expand All @@ -98,4 +106,11 @@ private boolean isWithinMaxDistance() {
public ItemActionType<?> type() {
return this.type;
}

public void destroyBlockAction() {
var pos = this.context.getClickedPos();
if (this.type.getDestroyPredicate().test(this.performer.level().getBlockState(pos))) {
NetworkChannel.PLAY.getSimpleChannel().sendToServer(new BlockDestroyActionMessage(pos));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,32 @@
import net.minecraft.tags.TagKey;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.Fluid;
import net.minecraft.world.level.material.FluidState;

public class BlockItemActionType extends ItemActionType<BlockItemAction> {

private final Predicate<BlockState> predicate;
private final Predicate<BlockState> destroyPredicate;
private final double maxDistanceSquared;

protected BlockItemActionType(Builder builder) {
super(builder);
this.predicate = builder.predicate;
this.destroyPredicate = builder.destroyPredicate;
this.maxDistanceSquared = builder.maxDistanceSquared;
}

public Predicate<BlockState> getPredicate() {
return this.predicate;
}

public Predicate<BlockState> getDestroyPredicate() {
return this.destroyPredicate;
}

public double getMaxDistanceSquared() {
return this.maxDistanceSquared;
}
Expand Down Expand Up @@ -74,6 +81,7 @@ public static Builder builder() {
public static final class Builder extends ItemActionType.Builder<Builder> {

private Predicate<BlockState> predicate;
private Predicate<BlockState> destroyPredicate;
private double maxDistanceSquared = 4.0D;

public Builder forBlock(Predicate<BlockState> predicate) {
Expand Down Expand Up @@ -102,8 +110,34 @@ public Builder maxDistance(double maxDistance) {
return this;
}

public Builder destroyBlock(Block... blocks) {
Predicate<BlockState> blockPredicate = state -> {
for (Block block : blocks) {
if (state.is(block)) {
return true;
}
}
return false;
};
if (this.destroyPredicate != null) {
this.destroyPredicate = this.destroyPredicate.and(blockPredicate);
} else {
this.destroyPredicate = blockPredicate;
}
if (this.predicate == null) {
this.predicate = this.destroyPredicate;
}
return this;
}

@Override
public BlockItemActionType build() {
if (this.predicate == null) {
this.predicate = state -> false;
}
if (this.destroyPredicate == null) {
this.destroyPredicate = state -> false;
}
return new BlockItemActionType(this);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package com.craftingdead.core.world.action.item;

import com.craftingdead.core.world.entity.extension.PlayerExtension;
import java.util.Optional;
import com.craftingdead.core.world.action.Action;
import com.craftingdead.core.world.entity.extension.LivingExtension;
Expand All @@ -30,6 +31,7 @@ public abstract class ItemAction implements Action {

private final InteractionHand hand;
private ItemStack originalStack;
public float progress;

public ItemAction(InteractionHand hand) {
this.hand = hand;
Expand Down Expand Up @@ -67,6 +69,7 @@ public boolean start(boolean simulate) {

@Override
public void stop(StopReason reason) {
var extension = PlayerExtension.getOrThrow((Player) this.performer().entity());
this.performer().entity().stopUsingItem();
if (!reason.isCompleted()) {
return;
Expand All @@ -78,6 +81,14 @@ public void stop(StopReason reason) {
.map(Item::getDefaultInstance)
.orElse(ItemStack.EMPTY);

if (!this.shouldConsumeItem(this.performer())
&& this.type().getUsageDamage() >= 1) {
if (!heldStack.isEmpty() && heldStack.isDamageableItem()) {
heldStack.hurtAndBreak(this.type().getUsageDamage(), this.performer().entity(),
(p) -> p.broadcastBreakEvent(this.hand));
}
}

if (this.shouldConsumeItem(this.performer())) {
heldStack.shrink(1);
}
Expand All @@ -91,6 +102,7 @@ public void stop(StopReason reason) {
}
}

extension.setRightClickTicks(0);
this.type().getFinishSound().ifPresent(
sound -> this.performer().entity().playSound(sound, 1.0F, 1.0F));
}
Expand All @@ -109,9 +121,11 @@ protected Optional<Item> getResultItem(LivingExtension<?, ?> performer) {

@Override
public boolean tick() {
if (!this.performer().entity().isUsingItem()
var extension = PlayerExtension.getOrThrow((Player) this.performer().entity());
if (!extension.isHoldingRightClick()
|| this.originalStack != this.getItemStack()) {
this.performer().cancelAction(true);
extension.setRightClickTicks(0);
return false;
}

Expand All @@ -126,7 +140,8 @@ public float getProgress(float partialTicks) {
if (!this.performer().entity().isUsingItem()) {
return 0.0F;
} else {
return (float) (this.performer().entity().getTicksUsingItem() + partialTicks)
this.progress = (this.performer().entity().getTicksUsingItem() + partialTicks) / this.type().getDurationTicks();
return (this.performer().entity().getTicksUsingItem() + partialTicks)
/ this.type().getDurationTicks();
}
}
Expand Down
Loading