start work on state mixins

This commit is contained in:
dfsek
2021-05-02 23:00:21 -07:00
parent 2d27e07441
commit 3b9280b19c
5 changed files with 83 additions and 20 deletions

View File

@@ -0,0 +1,57 @@
package com.dfsek.terra.fabric.mixin.block;
import com.dfsek.terra.api.platform.block.Block;
import com.dfsek.terra.api.platform.block.BlockData;
import com.dfsek.terra.api.platform.block.state.BlockState;
import com.dfsek.terra.fabric.world.FabricAdapter;
import com.dfsek.terra.fabric.world.block.FabricBlock;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Implements;
import org.spongepowered.asm.mixin.Interface;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
@Mixin(BlockEntity.class)
@Implements(@Interface(iface = BlockState.class, prefix = "terra$"))
public abstract class BlockEntityMixin {
@Shadow
protected BlockPos pos;
@Shadow
@Nullable
protected World world;
@Shadow
public abstract net.minecraft.block.BlockState getCachedState();
public Object terra$getHandle() {
return this;
}
public Block terra$getBlock() {
return new FabricBlock(pos, world);
}
public int terra$getX() {
return pos.getX();
}
public int terra$getY() {
return pos.getY();
}
public int terra$getZ() {
return pos.getZ();
}
public BlockData terra$getBlockData() {
return FabricAdapter.adapt(getCachedState());
}
public boolean terra$update(boolean applyPhysics) {
world.getChunk(pos).setBlockEntity(pos, (BlockEntity) (Object) this);
return true;
}
}

View File

@@ -0,0 +1,21 @@
package com.dfsek.terra.fabric.mixin.block;
import com.dfsek.terra.api.platform.block.state.Container;
import com.dfsek.terra.api.platform.inventory.Inventory;
import com.dfsek.terra.fabric.inventory.FabricInventory;
import net.minecraft.block.entity.LootableContainerBlockEntity;
import org.spongepowered.asm.mixin.Implements;
import org.spongepowered.asm.mixin.Interface;
import org.spongepowered.asm.mixin.Mixin;
@Mixin(LootableContainerBlockEntity.class)
@Implements(@Interface(iface = Container.class, prefix = "terra$"))
public abstract class LootableContainerBlockEntityMixin extends BlockEntityMixin {
public Inventory terra$getInventory() {
return new FabricInventory(((LootableContainerBlockEntity) (Object) this));
}
public Object terra$getHandle() {
return this;
}
}

View File

@@ -3,6 +3,7 @@ package com.dfsek.terra.fabric.world.block.state;
import com.dfsek.terra.api.platform.block.Block;
import com.dfsek.terra.api.platform.block.BlockData;
import com.dfsek.terra.api.platform.block.state.BlockState;
import com.dfsek.terra.api.platform.block.state.Container;
import com.dfsek.terra.fabric.world.FabricAdapter;
import com.dfsek.terra.fabric.world.block.FabricBlock;
import net.minecraft.block.entity.BlockEntity;
@@ -20,7 +21,7 @@ public class FabricBlockState implements BlockState {
this.worldAccess = worldAccess;
}
public static FabricBlockState newInstance(Block block) {
public static BlockState newInstance(Block block) {
WorldAccess worldAccess = (WorldAccess) block.getLocation().getWorld();
BlockEntity entity = worldAccess.getBlockEntity(FabricAdapter.adapt(block.getLocation().toVector()));
@@ -29,7 +30,7 @@ public class FabricBlockState implements BlockState {
} else if(entity instanceof MobSpawnerBlockEntity) {
return new FabricMobSpawner((MobSpawnerBlockEntity) entity, worldAccess);
} else if(entity instanceof LootableContainerBlockEntity) {
return new FabricContainer((LootableContainerBlockEntity) entity, worldAccess);
return (Container) entity;
}
return null;
}

View File

@@ -1,18 +0,0 @@
package com.dfsek.terra.fabric.world.block.state;
import com.dfsek.terra.api.platform.block.state.Container;
import com.dfsek.terra.api.platform.inventory.Inventory;
import com.dfsek.terra.fabric.inventory.FabricInventory;
import net.minecraft.block.entity.LootableContainerBlockEntity;
import net.minecraft.world.WorldAccess;
public class FabricContainer extends FabricBlockState implements Container {
public FabricContainer(LootableContainerBlockEntity blockEntity, WorldAccess worldAccess) {
super(blockEntity, worldAccess);
}
@Override
public Inventory getInventory() {
return new FabricInventory(((LootableContainerBlockEntity) blockEntity));
}
}

View File

@@ -5,6 +5,8 @@
"compatibilityLevel": "JAVA_8",
"mixins": [
"MixinGeneratorOptions",
"block.BlockEntityMixin",
"block.LootableContainerBlockEntityMixin",
"entity.EntityMixin",
"entity.EntityTypeMixin",
"entity.PlayerEntityMixin",