mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-06-17 14:21:08 +00:00
fabric inventory stuff
This commit is contained in:
+41
@@ -0,0 +1,41 @@
|
||||
package com.dfsek.terra.fabric.inventory;
|
||||
|
||||
import com.dfsek.terra.api.platform.inventory.ItemStack;
|
||||
import com.dfsek.terra.api.platform.inventory.item.Enchantment;
|
||||
import com.dfsek.terra.fabric.world.FabricAdapter;
|
||||
import net.minecraft.util.registry.Registry;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class FabricEnchantment implements Enchantment {
|
||||
private final net.minecraft.enchantment.Enchantment enchantment;
|
||||
|
||||
public FabricEnchantment(net.minecraft.enchantment.Enchantment enchantment) {
|
||||
this.enchantment = enchantment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public net.minecraft.enchantment.Enchantment getHandle() {
|
||||
return enchantment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canEnchantItem(ItemStack itemStack) {
|
||||
return enchantment.isAcceptableItem(FabricAdapter.adapt(itemStack));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getID() {
|
||||
return Objects.requireNonNull(Registry.ENCHANTMENT.getId(enchantment)).toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean conflictsWith(Enchantment other) {
|
||||
return !enchantment.canCombine(FabricAdapter.adapt(other));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMaxLevel() {
|
||||
return enchantment.getMaxLevel();
|
||||
}
|
||||
}
|
||||
+2
-3
@@ -3,13 +3,12 @@ package com.dfsek.terra.fabric.inventory;
|
||||
import com.dfsek.terra.api.platform.inventory.Inventory;
|
||||
import com.dfsek.terra.api.platform.inventory.ItemStack;
|
||||
import com.dfsek.terra.fabric.world.FabricAdapter;
|
||||
import net.minecraft.block.entity.LootableContainerBlockEntity;
|
||||
import net.minecraft.item.Items;
|
||||
|
||||
public class FabricInventory implements Inventory {
|
||||
private final LootableContainerBlockEntity delegate;
|
||||
private final net.minecraft.inventory.Inventory delegate;
|
||||
|
||||
public FabricInventory(LootableContainerBlockEntity delegate) {
|
||||
public FabricInventory(net.minecraft.inventory.Inventory delegate) {
|
||||
this.delegate = delegate;
|
||||
}
|
||||
|
||||
|
||||
+5
-13
@@ -3,6 +3,8 @@ package com.dfsek.terra.fabric.inventory;
|
||||
import com.dfsek.terra.api.platform.inventory.Item;
|
||||
import com.dfsek.terra.api.platform.inventory.ItemStack;
|
||||
import com.dfsek.terra.api.platform.inventory.item.ItemMeta;
|
||||
import com.dfsek.terra.fabric.inventory.meta.FabricDamageable;
|
||||
import com.dfsek.terra.fabric.inventory.meta.FabricItemMeta;
|
||||
|
||||
public class FabricItemStack implements ItemStack {
|
||||
private net.minecraft.item.ItemStack delegate;
|
||||
@@ -26,25 +28,15 @@ public class FabricItemStack implements ItemStack {
|
||||
return new FabricItem(delegate.getItem());
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack clone() {
|
||||
try {
|
||||
FabricItemStack stack = (FabricItemStack) super.clone();
|
||||
stack.delegate = delegate;
|
||||
return stack;
|
||||
} catch(CloneNotSupportedException e) {
|
||||
throw new Error();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemMeta getItemMeta() {
|
||||
return null;
|
||||
if(delegate.isDamageable()) return new FabricDamageable(delegate);
|
||||
return new FabricItemMeta(delegate.copy());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setItemMeta(ItemMeta meta) {
|
||||
|
||||
this.delegate = ((FabricItemMeta) meta).getHandle();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package com.dfsek.terra.fabric.inventory.meta;
|
||||
|
||||
import com.dfsek.terra.api.platform.inventory.item.Damageable;
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
public class FabricDamageable extends FabricItemMeta implements Damageable {
|
||||
public FabricDamageable(ItemStack delegate) {
|
||||
super(delegate);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getDamage() {
|
||||
return delegate.getDamage();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setDamage(int damage) {
|
||||
System.out.println("Setting damage: " + damage);
|
||||
delegate.setDamage(damage);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasDamage() {
|
||||
return delegate.isDamageable();
|
||||
}
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package com.dfsek.terra.fabric.inventory.meta;
|
||||
|
||||
import com.dfsek.terra.api.platform.inventory.item.Enchantment;
|
||||
import com.dfsek.terra.api.platform.inventory.item.ItemMeta;
|
||||
import com.dfsek.terra.fabric.world.FabricAdapter;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class FabricItemMeta implements ItemMeta {
|
||||
protected final ItemStack delegate;
|
||||
|
||||
public FabricItemMeta(ItemStack delegate) {
|
||||
this.delegate = delegate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack getHandle() {
|
||||
return delegate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<Enchantment, Integer> getEnchantments() {
|
||||
if(!delegate.hasEnchantments()) return Collections.emptyMap();
|
||||
Map<Enchantment, Integer> map = new HashMap<>();
|
||||
|
||||
delegate.getEnchantments().forEach(enchantment -> {
|
||||
CompoundTag eTag = (CompoundTag) enchantment;
|
||||
map.put(FabricAdapter.adapt(net.minecraft.enchantment.Enchantment.byRawId(eTag.getInt("id"))), eTag.getInt("lvl"));
|
||||
});
|
||||
return map;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addEnchantment(Enchantment enchantment, int level) {
|
||||
System.out.println("Enchanting: " + enchantment.getHandle() + ", " + level);
|
||||
delegate.addEnchantment(FabricAdapter.adapt(enchantment), level);
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,8 @@ package com.dfsek.terra.fabric.world;
|
||||
import com.dfsek.terra.api.math.vector.Vector3;
|
||||
import com.dfsek.terra.api.platform.block.BlockType;
|
||||
import com.dfsek.terra.api.platform.entity.EntityType;
|
||||
import com.dfsek.terra.api.platform.inventory.item.Enchantment;
|
||||
import com.dfsek.terra.fabric.inventory.FabricEnchantment;
|
||||
import com.dfsek.terra.fabric.inventory.FabricItem;
|
||||
import com.dfsek.terra.fabric.inventory.FabricItemStack;
|
||||
import com.dfsek.terra.fabric.world.block.FabricBlockData;
|
||||
@@ -73,4 +75,12 @@ public final class FabricAdapter {
|
||||
public static com.dfsek.terra.api.platform.inventory.Item adapt(Item item) {
|
||||
return new FabricItem(item);
|
||||
}
|
||||
|
||||
public static Enchantment adapt(net.minecraft.enchantment.Enchantment enchantment) {
|
||||
return new FabricEnchantment(enchantment);
|
||||
}
|
||||
|
||||
public static net.minecraft.enchantment.Enchantment adapt(Enchantment enchantment) {
|
||||
return ((FabricEnchantment) enchantment).getHandle();
|
||||
}
|
||||
}
|
||||
|
||||
+8
-14
@@ -5,12 +5,9 @@ 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 com.dfsek.terra.fabric.world.block.FabricBlockData;
|
||||
import com.dfsek.terra.fabric.world.handles.world.FabricWorldHandle;
|
||||
import net.minecraft.block.AbstractChestBlock;
|
||||
import net.minecraft.block.AbstractSignBlock;
|
||||
import net.minecraft.block.SpawnerBlock;
|
||||
import net.minecraft.block.entity.BlockEntity;
|
||||
import net.minecraft.block.entity.LootableContainerBlockEntity;
|
||||
import net.minecraft.block.entity.MobSpawnerBlockEntity;
|
||||
import net.minecraft.block.entity.SignBlockEntity;
|
||||
import net.minecraft.world.WorldAccess;
|
||||
@@ -25,18 +22,15 @@ public class FabricBlockState implements BlockState {
|
||||
}
|
||||
|
||||
public static FabricBlockState newInstance(Block block) {
|
||||
net.minecraft.block.Block block1 = ((FabricBlockData) block.getBlockData()).getHandle().getBlock();
|
||||
WorldAccess worldAccess = ((FabricWorldHandle) block.getLocation().getWorld()).getWorld();
|
||||
|
||||
if(block1 instanceof AbstractSignBlock) {
|
||||
SignBlockEntity signBlockEntity = (SignBlockEntity) worldAccess.getBlockEntity(FabricAdapter.adapt(block.getLocation().toVector()));
|
||||
return new FabricSign(signBlockEntity, worldAccess);
|
||||
} else if(block1 instanceof SpawnerBlock) {
|
||||
MobSpawnerBlockEntity mobSpawnerBlockEntity = (MobSpawnerBlockEntity) worldAccess.getBlockEntity(FabricAdapter.adapt(block.getLocation().toVector()));
|
||||
return new FabricMobSpawner(mobSpawnerBlockEntity, worldAccess);
|
||||
} else if(block1 instanceof AbstractChestBlock) {
|
||||
BlockEntity abstractChestBlock = worldAccess.getBlockEntity(FabricAdapter.adapt(block.getLocation().toVector()));
|
||||
return new FabricContainer(abstractChestBlock, worldAccess);
|
||||
BlockEntity entity = worldAccess.getBlockEntity(FabricAdapter.adapt(block.getLocation().toVector()));
|
||||
if(entity instanceof SignBlockEntity) {
|
||||
return new FabricSign((SignBlockEntity) entity, worldAccess);
|
||||
} else if(entity instanceof MobSpawnerBlockEntity) {
|
||||
return new FabricMobSpawner((MobSpawnerBlockEntity) entity, worldAccess);
|
||||
} else if(entity instanceof LootableContainerBlockEntity) {
|
||||
return new FabricContainer((LootableContainerBlockEntity) entity, worldAccess);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
+1
-2
@@ -3,12 +3,11 @@ 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.BlockEntity;
|
||||
import net.minecraft.block.entity.LootableContainerBlockEntity;
|
||||
import net.minecraft.world.WorldAccess;
|
||||
|
||||
public class FabricContainer extends FabricBlockState implements Container {
|
||||
public FabricContainer(BlockEntity blockEntity, WorldAccess worldAccess) {
|
||||
public FabricContainer(LootableContainerBlockEntity blockEntity, WorldAccess worldAccess) {
|
||||
super(blockEntity, worldAccess);
|
||||
}
|
||||
|
||||
|
||||
@@ -11,4 +11,7 @@ accessible field net/minecraft/server/world/ServerWorld worldProperties Lnet/min
|
||||
|
||||
accessible method net/minecraft/world/MobSpawnerLogic getEntityId ()Lnet/minecraft/util/Identifier;
|
||||
|
||||
accessible field net/minecraft/state/State PROPERTY_MAP_PRINTER Ljava/util/function/Function;
|
||||
accessible field net/minecraft/state/State PROPERTY_MAP_PRINTER Ljava/util/function/Function;
|
||||
|
||||
accessible method net/minecraft/block/entity/LootableContainerBlockEntity setInvStackList (Lnet/minecraft/util/collection/DefaultedList;)V
|
||||
accessible method net/minecraft/block/entity/LootableContainerBlockEntity getInvStackList ()Lnet/minecraft/util/collection/DefaultedList;
|
||||
Reference in New Issue
Block a user