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

Fix various issues #21

Merged
merged 16 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -51,7 +51,7 @@ public static boolean canBlockBeInteractedWith(ItemStack offhand, int x, int y,
float subY = (float) mop.hitVec.yCoord - y;
float subZ = (float) mop.hitVec.zCoord - z;

Block block = DummyWorld.INSTANCE.copyAndSetBlock(mc.theWorld, x, y, z);
Block block = DummyWorld.INSTANCE.copyAndSetBlock(mc.theWorld, x, y, z, mop);
if (block == null) return false;
ClientFakePlayer.INSTANCE.prepareForInteraction(mc.thePlayer, stack);

Expand Down
50 changes: 46 additions & 4 deletions src/main/java/xonin/backhand/client/world/DummyWorld.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package xonin.backhand.client.world;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import net.minecraft.block.Block;
import net.minecraft.entity.Entity;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.profiler.Profiler;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.util.Vec3;
import net.minecraft.world.EnumSkyBlock;
import net.minecraft.world.World;
import net.minecraft.world.WorldProviderSurface;
Expand All @@ -26,6 +30,9 @@ public class DummyWorld extends World {

public static final DummyWorld INSTANCE = new DummyWorld();

public Block boundBlock;
Lyfts marked this conversation as resolved.
Show resolved Hide resolved
public Vec3 boundBlockPos;

public DummyWorld() {
super(new DummySaveHandler(), "DummyServer", DEFAULT_SETTINGS, new WorldProviderSurface(), new Profiler());
// Guarantee the dimension ID was not reset by the provider
Expand Down Expand Up @@ -70,23 +77,58 @@ public boolean updateLightByType(EnumSkyBlock p_147463_1_, int p_147463_2_, int
return true;
}

public Block copyAndSetBlock(World world, int x, int y, int z) {
@Override
public TileEntity getTileEntity(int x, int y, int z) {
if (boundBlock == null) return super.getTileEntity(x, y, z);
return super.getTileEntity((int) boundBlockPos.xCoord, (int) boundBlockPos.yCoord, (int) boundBlockPos.zCoord);
}

@Override
public Block getBlock(int x, int y, int z) {
if (boundBlock == null) return super.getBlock(x, y, z);
return boundBlock;
}

@Nullable
public Block copyAndSetBlock(World world, int x, int y, int z, MovingObjectPosition mop) {
reset();
setBlockToAir(x, y, z);
Block block = world.getBlock(x, y, z);

if (block == null || block == Blocks.air) return null;

int meta = block.getDamageValue(world, x, y, z);
DummyWorld.INSTANCE.setBlock(x, y, z, block, meta, 3);
ItemStack stack = block.getPickBlock(mop, world, x, y, z, ClientFakePlayer.INSTANCE);

if (stack == null || !stack.getItem()
.onItemUse(stack, ClientFakePlayer.INSTANCE, this, x, y, z, mop.sideHit, x, y, z)) {
setBlock(x, y, z, block, meta, 3);
setBoundBlock(block, x, y, z);
}

TileEntity tile = world.getTileEntity(x, y, z);
if (tile != null) {
NBTTagCompound tag = new NBTTagCompound();
tile.writeToNBT(tag);
TileEntity dummyTile = TileEntity.createAndLoadEntity(tag);
DummyWorld.INSTANCE.setTileEntity(x, y, z, dummyTile);
TileEntity dummyTile = getTileEntity(x, y, z);
if (dummyTile == null) {
dummyTile = TileEntity.createAndLoadEntity(tag);
setTileEntity(x, y, z, dummyTile);
}
dummyTile.readFromNBT(tag);
}

return getBlock(x, y, z);
}

public void setBoundBlock(Block block, int x, int y, int z) {
boundBlock = block;
boundBlockPos = Vec3.createVectorHelper(x, y, z);
Lyfts marked this conversation as resolved.
Show resolved Hide resolved
}

public void reset() {
boundBlock = null;
boundBlockPos = null;
}

}