mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-06-18 23:01:03 +00:00
item mixins
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package com.dfsek.terra.api.platform.inventory;
|
package com.dfsek.terra.api.platform.inventory;
|
||||||
|
|
||||||
import com.dfsek.terra.api.platform.Handle;
|
import com.dfsek.terra.api.platform.Handle;
|
||||||
|
import com.dfsek.terra.api.platform.inventory.item.Damageable;
|
||||||
import com.dfsek.terra.api.platform.inventory.item.ItemMeta;
|
import com.dfsek.terra.api.platform.inventory.item.ItemMeta;
|
||||||
|
|
||||||
public interface ItemStack extends Handle {
|
public interface ItemStack extends Handle {
|
||||||
@@ -13,4 +14,8 @@ public interface ItemStack extends Handle {
|
|||||||
ItemMeta getItemMeta();
|
ItemMeta getItemMeta();
|
||||||
|
|
||||||
void setItemMeta(ItemMeta meta);
|
void setItemMeta(ItemMeta meta);
|
||||||
|
|
||||||
|
default boolean isDamageable() {
|
||||||
|
return getItemMeta() instanceof Damageable;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -34,8 +34,8 @@ public class DamageFunction implements LootFunction {
|
|||||||
@Override
|
@Override
|
||||||
public ItemStack apply(ItemStack original, Random r) {
|
public ItemStack apply(ItemStack original, Random r) {
|
||||||
if(original == null) return null;
|
if(original == null) return null;
|
||||||
|
if(!original.isDamageable()) return original;
|
||||||
ItemMeta meta = original.getItemMeta();
|
ItemMeta meta = original.getItemMeta();
|
||||||
if(!(meta instanceof Damageable)) return original;
|
|
||||||
double itemDurability = (r.nextDouble() * (max - min)) + min;
|
double itemDurability = (r.nextDouble() * (max - min)) + min;
|
||||||
Damageable damage = (Damageable) meta;
|
Damageable damage = (Damageable) meta;
|
||||||
damage.setDamage((int) (original.getType().getMaxDurability() - (itemDurability / 100) * original.getType().getMaxDurability()));
|
damage.setDamage((int) (original.getType().getMaxDurability() - (itemDurability / 100) * original.getType().getMaxDurability()));
|
||||||
|
|||||||
+2
-1
@@ -19,9 +19,10 @@ public class FabricEnchantment implements Enchantment {
|
|||||||
return enchantment;
|
return enchantment;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("ConstantConditions")
|
||||||
@Override
|
@Override
|
||||||
public boolean canEnchantItem(ItemStack itemStack) {
|
public boolean canEnchantItem(ItemStack itemStack) {
|
||||||
return enchantment.isAcceptableItem(FabricAdapter.adapt(itemStack));
|
return enchantment.isAcceptableItem((net.minecraft.item.ItemStack) (Object) itemStack);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
+4
-2
@@ -22,14 +22,16 @@ public class FabricInventory implements Inventory {
|
|||||||
return delegate.size();
|
return delegate.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("ConstantConditions")
|
||||||
@Override
|
@Override
|
||||||
public ItemStack getItem(int slot) {
|
public ItemStack getItem(int slot) {
|
||||||
net.minecraft.item.ItemStack itemStack = delegate.getStack(slot);
|
net.minecraft.item.ItemStack itemStack = delegate.getStack(slot);
|
||||||
return itemStack.getItem() == Items.AIR ? null : FabricAdapter.adapt(itemStack);
|
return itemStack.getItem() == Items.AIR ? null : (ItemStack) (Object) itemStack;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("ConstantConditions")
|
||||||
@Override
|
@Override
|
||||||
public void setItem(int slot, ItemStack newStack) {
|
public void setItem(int slot, ItemStack newStack) {
|
||||||
delegate.setStack(slot, FabricAdapter.adapt(newStack));
|
delegate.setStack(slot, (net.minecraft.item.ItemStack) (Object) newStack);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,46 +0,0 @@
|
|||||||
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;
|
|
||||||
|
|
||||||
public FabricItemStack(net.minecraft.item.ItemStack delegate) {
|
|
||||||
this.delegate = delegate;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getAmount() {
|
|
||||||
return delegate.getCount();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setAmount(int i) {
|
|
||||||
delegate.setCount(i);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Item getType() {
|
|
||||||
return (Item) delegate.getItem();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public ItemMeta getItemMeta() {
|
|
||||||
if(delegate.isDamageable()) return new FabricDamageable(delegate.copy());
|
|
||||||
return new FabricItemMeta(delegate.copy());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setItemMeta(ItemMeta meta) {
|
|
||||||
this.delegate = ((FabricItemMeta) meta).getHandle();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public net.minecraft.item.ItemStack getHandle() {
|
|
||||||
return delegate;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-25
@@ -1,25 +0,0 @@
|
|||||||
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) {
|
|
||||||
delegate.setDamage(damage);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean hasDamage() {
|
|
||||||
return delegate.isDamageable();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-42
@@ -1,42 +0,0 @@
|
|||||||
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 net.minecraft.util.registry.Registry;
|
|
||||||
|
|
||||||
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(Registry.ENCHANTMENT.get(eTag.getInt("id"))), eTag.getInt("lvl"));
|
|
||||||
});
|
|
||||||
return map;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void addEnchantment(Enchantment enchantment, int level) {
|
|
||||||
delegate.addEnchantment(FabricAdapter.adapt(enchantment), level);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+2
-2
@@ -1,7 +1,6 @@
|
|||||||
package com.dfsek.terra.fabric.mixin.inventory;
|
package com.dfsek.terra.fabric.mixin.inventory;
|
||||||
|
|
||||||
import com.dfsek.terra.api.platform.inventory.ItemStack;
|
import com.dfsek.terra.api.platform.inventory.ItemStack;
|
||||||
import com.dfsek.terra.fabric.inventory.FabricItemStack;
|
|
||||||
import net.minecraft.item.Item;
|
import net.minecraft.item.Item;
|
||||||
import org.spongepowered.asm.mixin.Implements;
|
import org.spongepowered.asm.mixin.Implements;
|
||||||
import org.spongepowered.asm.mixin.Interface;
|
import org.spongepowered.asm.mixin.Interface;
|
||||||
@@ -18,8 +17,9 @@ public abstract class ItemMixin {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("ConstantConditions")
|
||||||
public ItemStack vw$newItemStack(int amount) {
|
public ItemStack vw$newItemStack(int amount) {
|
||||||
return new FabricItemStack(new net.minecraft.item.ItemStack((Item) (Object) this, amount));
|
return (ItemStack) (Object) new net.minecraft.item.ItemStack((Item) (Object) this, amount);
|
||||||
}
|
}
|
||||||
|
|
||||||
public double vw$getMaxDurability() {
|
public double vw$getMaxDurability() {
|
||||||
|
|||||||
+19
@@ -0,0 +1,19 @@
|
|||||||
|
package com.dfsek.terra.fabric.mixin.inventory;
|
||||||
|
|
||||||
|
import com.dfsek.terra.api.platform.inventory.item.Damageable;
|
||||||
|
import net.minecraft.item.ItemStack;
|
||||||
|
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(ItemStack.class)
|
||||||
|
@Implements(@Interface(iface = Damageable.class, prefix = "vw$"))
|
||||||
|
public abstract class ItemStackDamageableMixin {
|
||||||
|
@Shadow
|
||||||
|
public abstract boolean isDamaged();
|
||||||
|
|
||||||
|
public boolean vw$hasDamage() {
|
||||||
|
return isDamaged();
|
||||||
|
}
|
||||||
|
}
|
||||||
+49
@@ -0,0 +1,49 @@
|
|||||||
|
package com.dfsek.terra.fabric.mixin.inventory;
|
||||||
|
|
||||||
|
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 net.minecraft.nbt.ListTag;
|
||||||
|
import net.minecraft.util.registry.Registry;
|
||||||
|
import org.spongepowered.asm.mixin.Implements;
|
||||||
|
import org.spongepowered.asm.mixin.Interface;
|
||||||
|
import org.spongepowered.asm.mixin.Mixin;
|
||||||
|
import org.spongepowered.asm.mixin.Shadow;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Mixin(ItemStack.class)
|
||||||
|
@Implements(@Interface(iface = ItemMeta.class, prefix = "vw$"))
|
||||||
|
public abstract class ItemStackMetaMixin {
|
||||||
|
@Shadow
|
||||||
|
public abstract boolean hasEnchantments();
|
||||||
|
|
||||||
|
@Shadow
|
||||||
|
public abstract ListTag getEnchantments();
|
||||||
|
|
||||||
|
@Shadow
|
||||||
|
public abstract void addEnchantment(net.minecraft.enchantment.Enchantment enchantment, int level);
|
||||||
|
|
||||||
|
public Object vw$getHandle() {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<Enchantment, Integer> vw$getEnchantments() {
|
||||||
|
if(!hasEnchantments()) return Collections.emptyMap();
|
||||||
|
Map<Enchantment, Integer> map = new HashMap<>();
|
||||||
|
|
||||||
|
getEnchantments().forEach(enchantment -> {
|
||||||
|
CompoundTag eTag = (CompoundTag) enchantment;
|
||||||
|
map.put(FabricAdapter.adapt(Registry.ENCHANTMENT.get(eTag.getInt("id"))), eTag.getInt("lvl"));
|
||||||
|
});
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void vw$addEnchantment(Enchantment enchantment, int level) {
|
||||||
|
addEnchantment(FabricAdapter.adapt(enchantment), level);
|
||||||
|
}
|
||||||
|
}
|
||||||
+52
@@ -0,0 +1,52 @@
|
|||||||
|
package com.dfsek.terra.fabric.mixin.inventory;
|
||||||
|
|
||||||
|
import com.dfsek.terra.api.platform.inventory.Item;
|
||||||
|
import com.dfsek.terra.api.platform.inventory.item.ItemMeta;
|
||||||
|
import net.minecraft.item.ItemStack;
|
||||||
|
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(ItemStack.class)
|
||||||
|
@Implements(@Interface(iface = com.dfsek.terra.api.platform.inventory.ItemStack.class, prefix = "vw$"))
|
||||||
|
public abstract class ItemStackMixin {
|
||||||
|
@Shadow
|
||||||
|
public abstract int getCount();
|
||||||
|
|
||||||
|
@Shadow
|
||||||
|
public abstract void setCount(int count);
|
||||||
|
|
||||||
|
@Shadow
|
||||||
|
public abstract net.minecraft.item.Item getItem();
|
||||||
|
|
||||||
|
@Shadow
|
||||||
|
public abstract boolean isDamageable();
|
||||||
|
|
||||||
|
@Shadow
|
||||||
|
public abstract ItemStack copy();
|
||||||
|
|
||||||
|
public int vw$getAmount() {
|
||||||
|
return getCount();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void vw$setAmount(int i) {
|
||||||
|
setCount(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Item getType() {
|
||||||
|
return (Item) getItem();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ItemMeta vw$getItemMeta() {
|
||||||
|
return (ItemMeta) this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void vw$setItemMeta(ItemMeta meta) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object vw$getHandle() {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,7 +6,6 @@ import com.dfsek.terra.api.platform.block.BlockType;
|
|||||||
import com.dfsek.terra.api.platform.entity.EntityType;
|
import com.dfsek.terra.api.platform.entity.EntityType;
|
||||||
import com.dfsek.terra.api.platform.inventory.item.Enchantment;
|
import com.dfsek.terra.api.platform.inventory.item.Enchantment;
|
||||||
import com.dfsek.terra.fabric.inventory.FabricEnchantment;
|
import com.dfsek.terra.fabric.inventory.FabricEnchantment;
|
||||||
import com.dfsek.terra.fabric.inventory.FabricItemStack;
|
|
||||||
import com.dfsek.terra.fabric.world.block.FabricBlockData;
|
import com.dfsek.terra.fabric.world.block.FabricBlockData;
|
||||||
import com.dfsek.terra.fabric.world.block.FabricBlockType;
|
import com.dfsek.terra.fabric.world.block.FabricBlockType;
|
||||||
import com.dfsek.terra.fabric.world.block.data.FabricDirectional;
|
import com.dfsek.terra.fabric.world.block.data.FabricDirectional;
|
||||||
@@ -21,7 +20,6 @@ import com.dfsek.terra.fabric.world.handles.world.FabricWorldHandle;
|
|||||||
import net.minecraft.block.Block;
|
import net.minecraft.block.Block;
|
||||||
import net.minecraft.block.BlockState;
|
import net.minecraft.block.BlockState;
|
||||||
import net.minecraft.entity.Entity;
|
import net.minecraft.entity.Entity;
|
||||||
import net.minecraft.item.ItemStack;
|
|
||||||
import net.minecraft.state.property.Properties;
|
import net.minecraft.state.property.Properties;
|
||||||
import net.minecraft.util.math.BlockPos;
|
import net.minecraft.util.math.BlockPos;
|
||||||
import net.minecraft.util.math.Direction;
|
import net.minecraft.util.math.Direction;
|
||||||
@@ -89,14 +87,6 @@ public final class FabricAdapter {
|
|||||||
return ((FabricEntityType) entityType).getHandle();
|
return ((FabricEntityType) entityType).getHandle();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ItemStack adapt(com.dfsek.terra.api.platform.inventory.ItemStack itemStack) {
|
|
||||||
return ((FabricItemStack) itemStack).getHandle();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static com.dfsek.terra.api.platform.inventory.ItemStack adapt(ItemStack itemStack) {
|
|
||||||
return new FabricItemStack(itemStack);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Enchantment adapt(net.minecraft.enchantment.Enchantment enchantment) {
|
public static Enchantment adapt(net.minecraft.enchantment.Enchantment enchantment) {
|
||||||
return new FabricEnchantment(enchantment);
|
return new FabricEnchantment(enchantment);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,9 @@
|
|||||||
"entity.PlayerEntityMixin",
|
"entity.PlayerEntityMixin",
|
||||||
"entity.ServerCommandSourceMixin",
|
"entity.ServerCommandSourceMixin",
|
||||||
"inventory.ItemMixin",
|
"inventory.ItemMixin",
|
||||||
|
"inventory.ItemStackDamageableMixin",
|
||||||
|
"inventory.ItemStackMetaMixin",
|
||||||
|
"inventory.ItemStackMixin",
|
||||||
"world.ChunkRegionMixin",
|
"world.ChunkRegionMixin",
|
||||||
"world.ProtoChunkMixin",
|
"world.ProtoChunkMixin",
|
||||||
"world.WorldChunkMixin"
|
"world.WorldChunkMixin"
|
||||||
|
|||||||
Reference in New Issue
Block a user