mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2025-07-03 00:15:35 +00:00
fabric inventory stuff
This commit is contained in:
parent
4a47815be7
commit
4ca2f0c08d
@ -3,15 +3,13 @@ package com.dfsek.terra.api.platform.inventory;
|
||||
import com.dfsek.terra.api.platform.Handle;
|
||||
import com.dfsek.terra.api.platform.inventory.item.ItemMeta;
|
||||
|
||||
public interface ItemStack extends Handle, Cloneable {
|
||||
public interface ItemStack extends Handle {
|
||||
int getAmount();
|
||||
|
||||
void setAmount(int i);
|
||||
|
||||
Item getType();
|
||||
|
||||
ItemStack clone();
|
||||
|
||||
ItemMeta getItemMeta();
|
||||
|
||||
void setItemMeta(ItemMeta meta);
|
||||
|
@ -58,7 +58,8 @@ public class LootTable {
|
||||
for(ItemStack stack : loot) {
|
||||
int attempts = 0;
|
||||
while(stack.getAmount() != 0 && attempts < 10) {
|
||||
ItemStack newStack = stack.clone();
|
||||
ItemStack newStack = stack.getType().newItemStack(stack.getAmount());
|
||||
newStack.setItemMeta(stack.getItemMeta());
|
||||
newStack.setAmount(1);
|
||||
int slot = r.nextInt(i.getSize());
|
||||
ItemStack slotItem = i.getItem(slot);
|
||||
@ -66,7 +67,8 @@ public class LootTable {
|
||||
i.setItem(slot, newStack);
|
||||
stack.setAmount(stack.getAmount() - 1);
|
||||
} else if(slotItem.getType().equals(newStack.getType())) {
|
||||
ItemStack dep = newStack.clone();
|
||||
ItemStack dep = newStack.getType().newItemStack(newStack.getAmount());
|
||||
dep.setItemMeta(newStack.getItemMeta());
|
||||
dep.setAmount(newStack.getAmount() + slotItem.getAmount());
|
||||
i.setItem(slot, dep);
|
||||
stack.setAmount(stack.getAmount() - 1);
|
||||
|
@ -33,9 +33,11 @@ public class DamageFunction implements LootFunction {
|
||||
*/
|
||||
@Override
|
||||
public ItemStack apply(ItemStack original, Random r) {
|
||||
if(!(original instanceof Damageable)) return original;
|
||||
if(original == null) return null;
|
||||
ItemMeta meta = original.getItemMeta();
|
||||
if(!(meta instanceof Damageable)) return original;
|
||||
double itemDurability = (r.nextDouble() * (max - min)) + min;
|
||||
Damageable damage = (Damageable) original.getItemMeta();
|
||||
Damageable damage = (Damageable) meta;
|
||||
damage.setDamage((int) (original.getType().getMaxDurability() - (itemDurability / 100) * original.getType().getMaxDurability()));
|
||||
original.setItemMeta((ItemMeta) damage);
|
||||
return original;
|
||||
|
@ -15,4 +15,8 @@ public class BukkitContainer extends BukkitBlockState implements Container {
|
||||
return new BukkitInventory(((org.bukkit.block.Container) getHandle()).getInventory());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean update(boolean applyPhysics) {
|
||||
return false; // This clears the inventory. we don't want that.
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ import com.dfsek.terra.api.platform.inventory.item.ItemMeta;
|
||||
import com.dfsek.terra.bukkit.world.BukkitAdapter;
|
||||
|
||||
public class BukkitItemStack implements ItemStack {
|
||||
private org.bukkit.inventory.ItemStack delegate;
|
||||
private final org.bukkit.inventory.ItemStack delegate;
|
||||
|
||||
public BukkitItemStack(org.bukkit.inventory.ItemStack delegate) {
|
||||
this.delegate = delegate;
|
||||
@ -27,18 +27,6 @@ public class BukkitItemStack implements ItemStack {
|
||||
return BukkitAdapter.adapt(delegate.getType());
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack clone() {
|
||||
BukkitItemStack clone;
|
||||
try {
|
||||
clone = (BukkitItemStack) super.clone();
|
||||
clone.delegate = delegate.clone();
|
||||
} catch(CloneNotSupportedException e) {
|
||||
throw new Error(e);
|
||||
}
|
||||
return clone;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemMeta getItemMeta() {
|
||||
return BukkitItemMeta.newInstance(delegate.getItemMeta());
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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;
|
Loading…
x
Reference in New Issue
Block a user