block breaking / active block entity API

This commit is contained in:
gnembon committed 2020-07-21 05:03:19 -03:00
1 parent 786e282889
commit cfc97c5703
5 files changed
+185 -2

No files matched your search

@@ -0,0 +1,12 @@
package carpet.fakes;
import net.minecraft.util.math.BlockPos;
public interface ServerPlayerInteractionManagerInterface
{
BlockPos getCurrentBreakingBlock();
int getCurrentBlockBreakingProgress();
void setBlockBreakingProgress(int progress);
}
@@ -1,5 +1,6 @@
package carpet.mixins;
import carpet.fakes.ServerPlayerInteractionManagerInterface;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
@@ -7,10 +8,12 @@ import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.network.ServerPlayerInteractionManager;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
@@ -24,10 +27,18 @@ import static carpet.script.CarpetEventServer.Event.PLAYER_INTERACTS_WITH_BLOCK;
@Mixin(ServerPlayerInteractionManager.class)
public class ServerPlayerInteractionManager_scarpetEventsMixin
public class ServerPlayerInteractionManager_scarpetEventsMixin implements ServerPlayerInteractionManagerInterface
{
@Shadow public ServerPlayerEntity player;
@Shadow private boolean mining;
@Shadow private BlockPos miningPos;
@Shadow private int blockBreakingProgress;
@Shadow public ServerWorld world;
@Inject(method = "tryBreakBlock", locals = LocalCapture.CAPTURE_FAILHARD, at = @At(
value = "INVOKE",
target = "Lnet/minecraft/block/Block;onBreak(Lnet/minecraft/world/World;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/BlockState;Lnet/minecraft/entity/player/PlayerEntity;)V",
@@ -46,4 +57,25 @@ public class ServerPlayerInteractionManager_scarpetEventsMixin
{
PLAYER_INTERACTS_WITH_BLOCK.onBlockHit(player, hand, blockHitResult);
}
@Override
public BlockPos getCurrentBreakingBlock()
{
if (!mining) return null;
return miningPos;
}
@Override
public int getCurrentBlockBreakingProgress()
{
if (!mining) return -1;
return blockBreakingProgress;
}
@Override
public void setBlockBreakingProgress(int progress)
{
blockBreakingProgress = MathHelper.clamp(progress, -1, 10);
world.setBlockBreakingInfo(-1*this.player.getEntityId(), miningPos, blockBreakingProgress);
}
}
@@ -5,6 +5,7 @@ import carpet.fakes.ItemEntityInterface;
import carpet.fakes.LivingEntityInterface;
import carpet.fakes.MobEntityInterface;
import carpet.fakes.HungerManagerInterface;
import carpet.fakes.ServerPlayerInteractionManagerInterface;
import carpet.helpers.Tracer;
import carpet.patches.EntityPlayerMPFake;
import carpet.script.CarpetContext;
@@ -464,9 +465,32 @@ public class EntityValue extends Value
put("selected_slot", (e, a) -> {
if (e instanceof PlayerEntity)
return new NumericValue(((PlayerEntity) e).inventory.selectedSlot);
return null;
return Value.NULL;
});
put("active_block", (e, a) -> {
if (e instanceof ServerPlayerEntity)
{
ServerPlayerInteractionManagerInterface manager = (ServerPlayerInteractionManagerInterface) (((ServerPlayerEntity) e).interactionManager);
BlockPos pos = manager.getCurrentBreakingBlock();
if (pos == null) return Value.NULL;
return new BlockValue(null, ((ServerPlayerEntity) e).getServerWorld(), pos);
}
return Value.NULL;
});
put("breaking_progress", (e, a) -> {
if (e instanceof ServerPlayerEntity)
{
ServerPlayerInteractionManagerInterface manager = (ServerPlayerInteractionManagerInterface) (((ServerPlayerEntity) e).interactionManager);
int progress = manager.getCurrentBlockBreakingProgress();
if (progress < 0) return Value.NULL;
return new NumericValue(progress);
}
return Value.NULL;
});
put("facing", (e, a) -> {
int index = 0;
if (a != null)
@@ -1033,6 +1057,15 @@ public class EntityValue extends Value
if(e instanceof PlayerEntity) ((PlayerEntity) e).getHungerManager().setSaturationLevelClient((float)NumericValue.asNumber(v).getLong());
});
put("breaking_progress", (e, a) -> {
if (e instanceof ServerPlayerEntity)
{
int progress = (a == null || a == Value.NULL)?-1:NumericValue.asNumber(a).getInt();
ServerPlayerInteractionManagerInterface manager = (ServerPlayerInteractionManagerInterface) (((ServerPlayerEntity) e).interactionManager);
manager.setBlockBreakingProgress(progress);
}
});
put("nbt", (e, v) -> {
if (!(e instanceof PlayerEntity))
{