Skip to content

Commit

Permalink
Under the hood improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
kyrptonaught committed Sep 28, 2019
1 parent 9bcb450 commit 4c80585
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
39 changes: 25 additions & 14 deletions src/main/java/net/kyrptonaught/diggusmaximus/Excavate.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
import net.minecraft.block.Block;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.stat.Stats;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.World;
import net.minecraft.world.timer.Timer;
import net.minecraft.world.timer.TimerCallback;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Stack;

class Excavate {
Expand All @@ -17,21 +22,25 @@ class Excavate {
private Block startBlock;
private int mined = 1;
private ItemStack tool;
private World world;

Excavate(BlockPos pos, Block block, PlayerEntity player) {
this.startPos = pos;
this.player = player;
this.tool = player.inventory.getMainHandStack();
this.startBlock = block;
this.world = player.getEntityWorld();
}

private Stack<BlockPos> points = new Stack<>();
private Deque<BlockPos> points = new ArrayDeque<>();

void startExcavate() {
spiral(startPos);
while (!points.empty()) {
spiral(points.remove(0));
points.add(startPos);
while (!points.isEmpty()) {
spiral(points.remove());
}
if (DiggusMaximusMod.getOptions().playerExhaustion)
player.addExhaustion(0.005F * mined);
}

private void spiral(BlockPos pos) {
Expand All @@ -45,18 +54,15 @@ private void spiral(BlockPos pos) {

private void point(BlockPos pos) {
if (player.getEntityWorld().getBlockState(pos).getBlock().equals(startBlock) && canMine(pos)) {
points.push(pos);
points.add(pos);
mined++;
mine(pos);
}
}

private void mine(BlockPos pos) {
World world = player.getEntityWorld();
// world.getLevelProperties().getScheduledEvents().addEvent(pos.toString(), world.getTime()+ 40, (var1, var2, var3) -> System.out.println("yooo" + var3));
if (DiggusMaximusMod.getOptions().toolDuribility)
tool.postMine(world, world.getBlockState(pos), pos, player);
if (DiggusMaximusMod.getOptions().playerExhaustion)
player.addExhaustion(0.005F);
player.incrementStat(Stats.MINED.getOrCreateStat(startBlock));
dropStacks(world, pos);
//startBlock.onBreak(world, pos, world.getBlockState(pos), player);
Expand All @@ -67,19 +73,24 @@ private void mine(BlockPos pos) {
private void dropStacks(World world, BlockPos pos) {
if (player.isCreative()) return;
Block.getDroppedStacks(world.getBlockState(pos), (ServerWorld) world, pos, null, player, tool).forEach((stack) -> {
if (DiggusMaximusMod.getOptions().autoPickup) player.inventory.insertStack(stack);
else Block.dropStack(world, pos, stack);
if (DiggusMaximusMod.getOptions().autoPickup) {
player.inventory.insertStack(stack);
if (stack.getCount() > 0)
Block.dropStack(world, pos, stack);
} else Block.dropStack(world, pos, stack);
});
startBlock.onStacksDropped(world.getBlockState(pos), world, pos, tool);
}

private int maxMined = Math.min(DiggusMaximusMod.getOptions().maxMinedBlocks, 256);

private boolean canMine(BlockPos pos) {
int maxMined = Math.min(DiggusMaximusMod.getOptions().maxMinedBlocks, 256);
return mined < maxMined && isWithinDistance(pos) && toolHasDurability() && (player.isCreative() || player.isUsingEffectiveTool(player.world.getBlockState(pos)));
}

private double distance = Math.min(DiggusMaximusMod.getOptions().maxMineDistance + 1, 256);

private boolean isWithinDistance(BlockPos pos) {
double distance = Math.min(DiggusMaximusMod.getOptions().maxMineDistance + 1, 256);
return pos.isWithinDistance(startPos, distance);
}

Expand All @@ -89,4 +100,4 @@ private boolean toolHasDurability() {
return !DiggusMaximusMod.getOptions().requiresTool;
return player.inventory.getMainHandStack().getDamage() != player.inventory.getMainHandStack().getMaxDamage();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ public class StartExcavatePacket {
static void registerReceivePacket() {
ServerSidePacketRegistry.INSTANCE.register(START_EXCAVATE_PACKET, (packetContext, packetByteBuf) -> {
BlockPos blockPos = packetByteBuf.readBlockPos();
String blockID = new String(packetByteBuf.readByteArray());
Block block = Registry.BLOCK.get(new Identifier(blockID));
int blockID = packetByteBuf.readInt();
Block block = Registry.BLOCK.get(blockID);
packetContext.getTaskQueue().execute(() -> {
if (DiggusMaximusMod.getOptions().enabled && canMine(blockID)) {
if (DiggusMaximusMod.getOptions().enabled && canMine(Registry.BLOCK.getId(block).toString())) {
if (blockPos.isWithinDistance(packetContext.getPlayer().getPos(), 10)) {
Excavate excavate = new Excavate(blockPos, block, packetContext.getPlayer());
excavate.startExcavate();
Expand All @@ -38,10 +38,10 @@ private static boolean canMine(String blockID) {
}

@Environment(EnvType.CLIENT)
public static void sendExcavatePacket(BlockPos blockPos, String blockID) {
public static void sendExcavatePacket(BlockPos blockPos, int blockID) {
PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer());
buf.writeBlockPos(blockPos);
buf.writeByteArray(blockID.getBytes(StandardCharsets.UTF_8));
buf.writeInt(blockID);
MinecraftClient.getInstance().getNetworkHandler().getConnection().send(new CustomPayloadC2SPacket(START_EXCAVATE_PACKET, new PacketByteBuf(buf)));

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public abstract class MixinBlockBreak {
private void DIGGUS$BLOCKBREAK(World world_1, BlockPos blockPos_1, BlockState blockState_1, PlayerEntity playerEntity_1, CallbackInfo ci) {
if (DiggusMaximusMod.getOptions().enabled) {
if (DiggusMaximusMod.isKeybindPressed()) {
StartExcavatePacket.sendExcavatePacket(blockPos_1, Registry.BLOCK.getId(blockState_1.getBlock()).toString());
StartExcavatePacket.sendExcavatePacket(blockPos_1, Registry.BLOCK.getRawId(blockState_1.getBlock()));
ci.cancel();
}
}
Expand Down

0 comments on commit 4c80585

Please sign in to comment.