This commit is contained in:
petrak@ 2022-11-21 21:59:22 -06:00
parent 79fea68588
commit 274bec11d6
11 changed files with 428 additions and 278 deletions

View file

@ -0,0 +1,84 @@
package at.petrak.hexcasting.api.addldata;
import at.petrak.hexcasting.api.spell.iota.Iota;
import at.petrak.hexcasting.common.entities.EntityWallScroll;
import at.petrak.hexcasting.xplat.IXplatAbstractions;
import com.google.common.base.Suppliers;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.decoration.ItemFrame;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.Nullable;
import java.util.function.Consumer;
import java.util.function.Supplier;
public abstract class ItemDelegatingEntityIotaHolder implements ADIotaHolder {
private final Supplier<ItemStack> stackCacher;
private final Consumer<ItemStack> save;
public ItemDelegatingEntityIotaHolder(Supplier<ItemStack> stackSupplier, Consumer<ItemStack> save) {
this.stackCacher = Suppliers.memoize(stackSupplier::get);
this.save = save;
}
@Override
public @Nullable CompoundTag readIotaTag() {
var delegate = IXplatAbstractions.INSTANCE.findDataHolder(this.stackCacher.get());
return delegate == null ? null : delegate.readIotaTag();
}
@Override
public boolean writeIota(@Nullable Iota datum, boolean simulate) {
var stacc = this.stackCacher.get();
var delegate = IXplatAbstractions.INSTANCE.findDataHolder(stacc);
var success = delegate != null && delegate.writeIota(datum, simulate);
if (success && !simulate) {
this.save.accept(stacc);
}
return success;
}
@Override
public @Nullable Iota readIota(ServerLevel world) {
var delegate = IXplatAbstractions.INSTANCE.findDataHolder(this.stackCacher.get());
return delegate == null ? null : delegate.readIota(world);
}
@Override
public @Nullable Iota emptyIota() {
var delegate = IXplatAbstractions.INSTANCE.findDataHolder(this.stackCacher.get());
return delegate == null ? null : delegate.emptyIota();
}
public static class ToItemEntity extends ItemDelegatingEntityIotaHolder {
public ToItemEntity(ItemEntity entity) {
super(entity::getItem, stack -> {
// https://github.com/VazkiiMods/Botania/blob/e6d095ff5010074b45408d6cce8ee1e328af3383/Xplat/src/main/java/vazkii/botania/common/helper/EntityHelper.java#L16
entity.setItem(ItemStack.EMPTY);
entity.setItem(stack);
entity.setUnlimitedLifetime();
});
}
}
public static class ToItemFrame extends ItemDelegatingEntityIotaHolder {
public ToItemFrame(ItemFrame entity) {
super(entity::getItem, entity::setItem);
}
}
public static class ToWallScroll extends ItemDelegatingEntityIotaHolder {
public ToWallScroll(EntityWallScroll entity) {
super(() -> entity.scroll.copy(), stack -> {
});
}
@Override
public boolean writeIota(@Nullable Iota datum, boolean simulate) {
return false;
}
}
}

View file

@ -1,6 +1,7 @@
package at.petrak.hexcasting.fabric.cc;
import at.petrak.hexcasting.api.addldata.ADMediaHolder;
import at.petrak.hexcasting.api.addldata.ItemDelegatingEntityIotaHolder;
import at.petrak.hexcasting.api.item.ColorizerItem;
import at.petrak.hexcasting.api.item.HexHolderItem;
import at.petrak.hexcasting.api.item.IotaHolderItem;
@ -10,6 +11,7 @@ import at.petrak.hexcasting.api.spell.iota.DoubleIota;
import at.petrak.hexcasting.common.entities.EntityWallScroll;
import at.petrak.hexcasting.common.lib.HexItems;
import at.petrak.hexcasting.fabric.cc.adimpl.*;
import dev.onyxstudios.cca.api.v3.component.ComponentFactory;
import dev.onyxstudios.cca.api.v3.component.ComponentKey;
import dev.onyxstudios.cca.api.v3.component.ComponentRegistry;
import dev.onyxstudios.cca.api.v3.entity.EntityComponentFactoryRegistry;
@ -18,11 +20,14 @@ import dev.onyxstudios.cca.api.v3.entity.RespawnCopyStrategy;
import dev.onyxstudios.cca.api.v3.item.ItemComponentFactoryRegistry;
import dev.onyxstudios.cca.api.v3.item.ItemComponentInitializer;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.entity.decoration.ItemFrame;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.item.Items;
import java.util.function.Function;
import static at.petrak.hexcasting.api.HexAPI.modLoc;
public class HexCardinalComponents implements EntityComponentInitializer, ItemComponentInitializer {
@ -59,9 +64,12 @@ public class HexCardinalComponents implements EntityComponentInitializer, ItemCo
registry.registerFor(ServerPlayer.class, HARNESS, CCHarness::new);
registry.registerFor(ServerPlayer.class, PATTERNS, CCPatterns::new);
registry.registerFor(ItemEntity.class, IOTA_HOLDER, CCEntityIotaHolder.EntityItemDelegating::new);
registry.registerFor(ItemFrame.class, IOTA_HOLDER, CCEntityIotaHolder.ItemFrameDelegating::new);
registry.registerFor(EntityWallScroll.class, IOTA_HOLDER, CCEntityIotaHolder.ScrollDelegating::new);
registry.registerFor(ItemEntity.class, IOTA_HOLDER, wrapItemEntityDelegate(
ItemDelegatingEntityIotaHolder.ToItemEntity::new));
registry.registerFor(ItemFrame.class, IOTA_HOLDER, wrapItemEntityDelegate(
ItemDelegatingEntityIotaHolder.ToItemFrame::new));
registry.registerFor(EntityWallScroll.class, IOTA_HOLDER,
wrapItemEntityDelegate(ItemDelegatingEntityIotaHolder.ToWallScroll::new));
}
@Override
@ -87,4 +95,9 @@ public class HexCardinalComponents implements EntityComponentInitializer, ItemCo
registry.register(i -> i instanceof HexHolderItem, HEX_HOLDER, CCHexHolder.ItemBased::new);
}
private <E extends Entity> ComponentFactory<E, CCEntityIotaHolder.Wrapper> wrapItemEntityDelegate(Function<E,
ItemDelegatingEntityIotaHolder> make) {
return e -> new CCEntityIotaHolder.Wrapper(make.apply(e));
}
}

View file

@ -1,18 +1,12 @@
package at.petrak.hexcasting.fabric.cc.adimpl;
import at.petrak.hexcasting.api.addldata.ItemDelegatingEntityIotaHolder;
import at.petrak.hexcasting.api.spell.iota.Iota;
import at.petrak.hexcasting.common.entities.EntityWallScroll;
import at.petrak.hexcasting.xplat.IXplatAbstractions;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.decoration.ItemFrame;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.function.Supplier;
public abstract class CCEntityIotaHolder implements CCIotaHolder {
@Override
public void writeToNbt(@NotNull CompoundTag tag) {
@ -24,58 +18,32 @@ public abstract class CCEntityIotaHolder implements CCIotaHolder {
// NO-OP
}
public static class ItemDelegating extends CCEntityIotaHolder {
private final Supplier<ItemStack> item;
public static class Wrapper extends CCEntityIotaHolder {
private final ItemDelegatingEntityIotaHolder inner;
public ItemDelegating(Supplier<ItemStack> stackSupplier) {
this.item = stackSupplier;
public Wrapper(ItemDelegatingEntityIotaHolder inner) {
this.inner = inner;
}
@Override
public @Nullable CompoundTag readIotaTag() {
var delegate = IXplatAbstractions.INSTANCE.findDataHolder(item.get());
return delegate == null ? null : delegate.readIotaTag();
return inner.readIotaTag();
}
@Override
public boolean writeIota(@Nullable Iota datum, boolean simulate) {
var delegate = IXplatAbstractions.INSTANCE.findDataHolder(item.get());
return delegate != null && delegate.writeIota(datum, simulate);
public boolean writeIota(@Nullable Iota iota, boolean simulate) {
return inner.writeIota(iota, simulate);
}
@Override
public @Nullable Iota readIota(ServerLevel world) {
var delegate = IXplatAbstractions.INSTANCE.findDataHolder(item.get());
return delegate == null ? null : delegate.readIota(world);
return inner.readIota(world);
}
@Override
public @Nullable Iota emptyIota() {
var delegate = IXplatAbstractions.INSTANCE.findDataHolder(item.get());
return delegate == null ? null : delegate.emptyIota();
}
}
public static class EntityItemDelegating extends ItemDelegating {
public EntityItemDelegating(ItemEntity entity) {
super(entity::getItem);
}
}
public static class ItemFrameDelegating extends ItemDelegating {
public ItemFrameDelegating(ItemFrame entity) {
super(entity::getItem);
}
}
public static class ScrollDelegating extends ItemDelegating {
public ScrollDelegating(EntityWallScroll entity) {
super(() -> entity.scroll);
}
@Override
public boolean writeIota(@Nullable Iota datum, boolean simulate) {
return false;
return inner.emptyIota();
}
}
}

View file

@ -1,9 +1,6 @@
package at.petrak.hexcasting.forge.cap;
import at.petrak.hexcasting.api.addldata.ADColorizer;
import at.petrak.hexcasting.api.addldata.ADHexHolder;
import at.petrak.hexcasting.api.addldata.ADIotaHolder;
import at.petrak.hexcasting.api.addldata.ADMediaHolder;
import at.petrak.hexcasting.api.addldata.*;
import at.petrak.hexcasting.api.block.circle.BlockEntityAbstractImpetus;
import at.petrak.hexcasting.api.item.ColorizerItem;
import at.petrak.hexcasting.api.item.HexHolderItem;
@ -11,21 +8,17 @@ import at.petrak.hexcasting.api.item.IotaHolderItem;
import at.petrak.hexcasting.api.item.MediaHolderItem;
import at.petrak.hexcasting.api.mod.HexConfig;
import at.petrak.hexcasting.api.spell.iota.DoubleIota;
import at.petrak.hexcasting.api.spell.iota.Iota;
import at.petrak.hexcasting.common.entities.EntityWallScroll;
import at.petrak.hexcasting.common.lib.hex.HexIotaTypes;
import at.petrak.hexcasting.common.lib.HexItems;
import at.petrak.hexcasting.forge.cap.adimpl.*;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.decoration.ItemFrame;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ForgeCapabilities;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
@ -36,11 +29,8 @@ import net.minecraftforge.event.AttachCapabilitiesEvent;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.UUID;
import java.util.function.BooleanSupplier;
import java.util.function.Function;
import java.util.function.Supplier;
import static at.petrak.hexcasting.api.HexAPI.modLoc;
@ -84,47 +74,51 @@ public class ForgeCapabilityHandler {
if (stack.getItem() instanceof MediaHolderItem holder) {
evt.addCapability(MEDIA_STORAGE_CAP,
provide(stack, HexCapabilities.MEDIA, () -> new ItemBasedMediaHolder(holder, stack)));
provide(stack, HexCapabilities.MEDIA, () -> new CapItemMediaHolder(holder, stack)));
} else if (stack.is(HexItems.AMETHYST_DUST)) {
evt.addCapability(MEDIA_STATIC_CAP, provide(stack, HexCapabilities.MEDIA, () ->
new StaticMediaHolder(HexConfig.common()::dustMediaAmount, ADMediaHolder.AMETHYST_DUST_PRIORITY,
new CapStaticMediaHolder(HexConfig.common()::dustMediaAmount, ADMediaHolder.AMETHYST_DUST_PRIORITY,
stack)));
} else if (stack.is(Items.AMETHYST_SHARD)) {
evt.addCapability(MEDIA_STATIC_CAP, provide(stack, HexCapabilities.MEDIA, () -> new StaticMediaHolder(
evt.addCapability(MEDIA_STATIC_CAP, provide(stack, HexCapabilities.MEDIA, () -> new CapStaticMediaHolder(
HexConfig.common()::shardMediaAmount, ADMediaHolder.AMETHYST_SHARD_PRIORITY, stack)));
} else if (stack.is(HexItems.CHARGED_AMETHYST)) {
evt.addCapability(MEDIA_STATIC_CAP,
provide(stack, HexCapabilities.MEDIA, () -> new StaticMediaHolder(
provide(stack, HexCapabilities.MEDIA, () -> new CapStaticMediaHolder(
HexConfig.common()::chargedCrystalMediaAmount, ADMediaHolder.CHARGED_AMETHYST_PRIORITY, stack)));
}
if (stack.getItem() instanceof IotaHolderItem holder) {
evt.addCapability(IOTA_STORAGE_CAP,
provide(stack, HexCapabilities.IOTA, () -> new ItemBasedIotaHolder(holder, stack)));
provide(stack, HexCapabilities.IOTA, () -> new CapItemIotaHolder(holder, stack)));
} else if (stack.is(Items.PUMPKIN_PIE)) {
// haha yes
evt.addCapability(IOTA_STATIC_CAP, provide(stack, HexCapabilities.IOTA, () ->
new StaticIotaHolder((s) -> new DoubleIota(Math.PI * s.getCount()), stack)));
new CapStaticIotaHolder((s) -> new DoubleIota(Math.PI * s.getCount()), stack)));
}
if (stack.getItem() instanceof HexHolderItem holder) {
evt.addCapability(HEX_HOLDER_CAP,
provide(stack, HexCapabilities.STORED_HEX, () -> new ItemBasedHexHolder(holder, stack)));
provide(stack, HexCapabilities.STORED_HEX, () -> new CapItemHexHolder(holder, stack)));
}
if (stack.getItem() instanceof ColorizerItem colorizer) {
evt.addCapability(PIGMENT_CAP,
provide(stack, HexCapabilities.COLOR, () -> new ItemBasedColorizer(colorizer, stack)));
provide(stack, HexCapabilities.COLOR, () -> new CapItemColorizer(colorizer, stack)));
}
}
public static void attachEntityCaps(AttachCapabilitiesEvent<Entity> evt) {
if (evt.getObject() instanceof ItemEntity item) {
evt.addCapability(IOTA_STORAGE_CAP, delegateTo(item::getItem)); // Delegate to the item
} else if (evt.getObject() instanceof ItemFrame frame) {
evt.addCapability(IOTA_STORAGE_CAP, delegateTo(frame::getItem));
} else if (evt.getObject() instanceof EntityWallScroll scroll) {
evt.addCapability(IOTA_STORAGE_CAP, delegateTo(() -> scroll.scroll));
var entity = evt.getObject();
if (entity instanceof ItemEntity item) {
evt.addCapability(IOTA_STORAGE_CAP, wrapItemEntityDelegate(item,
ItemDelegatingEntityIotaHolder.ToItemEntity::new));
} else if (entity instanceof ItemFrame frame) {
evt.addCapability(IOTA_STORAGE_CAP, wrapItemEntityDelegate(frame,
ItemDelegatingEntityIotaHolder.ToItemFrame::new));
} else if (entity instanceof EntityWallScroll scroll) {
evt.addCapability(IOTA_STORAGE_CAP, wrapItemEntityDelegate(scroll,
ItemDelegatingEntityIotaHolder.ToWallScroll::new));
}
}
@ -135,34 +129,30 @@ public class ForgeCapabilityHandler {
}
}
private static ICapabilityProvider delegateTo(Supplier<ICapabilityProvider> provider) {
return new ICapabilityProvider() {
@NotNull
@Override
public <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap, @Nullable Direction side) {
var providerInst = provider.get();
return providerInst == null ? LazyOptional.empty() : providerInst.getCapability(cap, side);
}
};
// i do not know why we need super here
private static <E extends Entity> SimpleProvider<? super CapEntityIotaHolder.Wrapper> wrapItemEntityDelegate(E entity,
Function<E, ItemDelegatingEntityIotaHolder> make) {
return provide(entity, HexCapabilities.IOTA,
() -> new CapEntityIotaHolder.Wrapper(make.apply(entity)));
}
private static <CAP> SimpleProvider<CAP> provide(Entity entity, Capability<CAP> capability,
NonNullSupplier<CAP> supplier) {
NonNullSupplier<CAP> supplier) {
return provide(entity::isRemoved, capability, supplier);
}
private static <CAP> SimpleProvider<CAP> provide(BlockEntity be, Capability<CAP> capability,
NonNullSupplier<CAP> supplier) {
NonNullSupplier<CAP> supplier) {
return provide(be::isRemoved, capability, supplier);
}
private static <CAP> SimpleProvider<CAP> provide(ItemStack stack, Capability<CAP> capability,
NonNullSupplier<CAP> supplier) {
NonNullSupplier<CAP> supplier) {
return provide(stack::isEmpty, capability, supplier);
}
private static <CAP> SimpleProvider<CAP> provide(BooleanSupplier invalidated, Capability<CAP> capability,
NonNullSupplier<CAP> supplier) {
NonNullSupplier<CAP> supplier) {
return new SimpleProvider<>(invalidated, capability, LazyOptional.of(supplier));
}
@ -181,197 +171,4 @@ public class ForgeCapabilityHandler {
}
}
private record StaticMediaHolder(Supplier<Integer> baseWorth,
int consumptionPriority,
ItemStack stack) implements ADMediaHolder {
@Override
public int getMedia() {
return baseWorth.get() * stack.getCount();
}
@Override
public int getMaxMedia() {
return getMedia();
}
@Override
public void setMedia(int media) {
// NO-OP
}
@Override
public boolean canRecharge() {
return false;
}
@Override
public boolean canProvide() {
return true;
}
@Override
public int getConsumptionPriority() {
return consumptionPriority;
}
@Override
public boolean canConstructBattery() {
return true;
}
@Override
public int withdrawMedia(int cost, boolean simulate) {
int worth = baseWorth.get();
if (cost < 0) {
cost = worth * stack.getCount();
}
double itemsRequired = cost / (double) worth;
int itemsUsed = Math.min((int) Math.ceil(itemsRequired), stack.getCount());
if (!simulate) {
stack.shrink(itemsUsed);
}
return itemsUsed * worth;
}
}
private record ItemBasedMediaHolder(MediaHolderItem holder,
ItemStack stack) implements ADMediaHolder {
@Override
public int getMedia() {
return holder.getMedia(stack);
}
@Override
public int getMaxMedia() {
return holder.getMaxMedia(stack);
}
@Override
public void setMedia(int media) {
holder.setMedia(stack, media);
}
@Override
public boolean canRecharge() {
return holder.canRecharge(stack);
}
@Override
public boolean canProvide() {
return holder.canProvideMedia(stack);
}
@Override
public int getConsumptionPriority() {
return 40;
}
@Override
public boolean canConstructBattery() {
return false;
}
@Override
public int withdrawMedia(int cost, boolean simulate) {
return holder.withdrawMedia(stack, cost, simulate);
}
@Override
public int insertMedia(int amount, boolean simulate) {
return holder.insertMedia(stack, amount, simulate);
}
}
private record StaticIotaHolder(Function<ItemStack, Iota> provider,
ItemStack stack) implements ADIotaHolder {
@Override
public @Nullable
CompoundTag readIotaTag() {
var iota = provider.apply(stack);
return iota == null ? null : HexIotaTypes.serialize(iota);
}
@Override
public @Nullable
Iota readIota(ServerLevel world) {
return provider.apply(stack);
}
@Override
public boolean writeIota(@Nullable Iota iota, boolean simulate) {
return false;
}
}
private record ItemBasedIotaHolder(IotaHolderItem holder,
ItemStack stack) implements ADIotaHolder {
@Override
public @Nullable
CompoundTag readIotaTag() {
return holder.readIotaTag(stack);
}
@Override
public @Nullable
Iota readIota(ServerLevel world) {
return holder.readIota(stack, world);
}
@Override
public @Nullable
Iota emptyIota() {
return holder.emptyIota(stack);
}
@Override
public boolean writeIota(@Nullable Iota iota, boolean simulate) {
if (!holder.canWrite(stack, iota)) {
return false;
}
if (!simulate) {
holder.writeDatum(stack, iota);
}
return true;
}
}
private record ItemBasedHexHolder(HexHolderItem holder,
ItemStack stack) implements ADHexHolder {
@Override
public boolean canDrawMediaFromInventory() {
return holder.canDrawMediaFromInventory(stack);
}
@Override
public boolean hasHex() {
return holder.hasHex(stack);
}
@Override
public @Nullable List<Iota> getHex(ServerLevel level) {
return holder.getHex(stack, level);
}
@Override
public void writeHex(List<Iota> patterns, int media) {
holder.writeHex(stack, patterns, media);
}
@Override
public void clearHex() {
holder.clearHex(stack);
}
}
private record ItemBasedColorizer(ColorizerItem holder,
ItemStack stack) implements ADColorizer {
@Override
public int color(UUID owner, float time, Vec3 position) {
return holder.color(stack, owner, time, position);
}
}
}

View file

@ -0,0 +1,39 @@
package at.petrak.hexcasting.forge.cap.adimpl;
import at.petrak.hexcasting.api.addldata.ADIotaHolder;
import at.petrak.hexcasting.api.addldata.ItemDelegatingEntityIotaHolder;
import at.petrak.hexcasting.api.spell.iota.Iota;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.level.ServerLevel;
import org.jetbrains.annotations.Nullable;
public abstract class CapEntityIotaHolder implements ADIotaHolder {
public static class Wrapper extends CapEntityIotaHolder {
private final ItemDelegatingEntityIotaHolder inner;
public Wrapper(ItemDelegatingEntityIotaHolder inner) {
this.inner = inner;
}
@Override
public @Nullable CompoundTag readIotaTag() {
return inner.readIotaTag();
}
@Override
public boolean writeIota(@Nullable Iota iota, boolean simulate) {
return inner.writeIota(iota, simulate);
}
@Override
public @Nullable Iota readIota(ServerLevel world) {
return inner.readIota(world);
}
@Override
public @Nullable Iota emptyIota() {
return inner.emptyIota();
}
}
}

View file

@ -0,0 +1,16 @@
package at.petrak.hexcasting.forge.cap.adimpl;
import at.petrak.hexcasting.api.addldata.ADColorizer;
import at.petrak.hexcasting.api.item.ColorizerItem;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.phys.Vec3;
import java.util.UUID;
public record CapItemColorizer(ColorizerItem holder,
ItemStack stack) implements ADColorizer {
@Override
public int color(UUID owner, float time, Vec3 position) {
return holder.color(stack, owner, time, position);
}
}

View file

@ -0,0 +1,39 @@
package at.petrak.hexcasting.forge.cap.adimpl;
import at.petrak.hexcasting.api.addldata.ADHexHolder;
import at.petrak.hexcasting.api.item.HexHolderItem;
import at.petrak.hexcasting.api.spell.iota.Iota;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.Nullable;
import java.util.List;
public record CapItemHexHolder(HexHolderItem holder,
ItemStack stack) implements ADHexHolder {
@Override
public boolean canDrawMediaFromInventory() {
return holder.canDrawMediaFromInventory(stack);
}
@Override
public boolean hasHex() {
return holder.hasHex(stack);
}
@Override
public @Nullable List<Iota> getHex(ServerLevel level) {
return holder.getHex(stack, level);
}
@Override
public void writeHex(List<Iota> patterns, int media) {
holder.writeHex(stack, patterns, media);
}
@Override
public void clearHex() {
holder.clearHex(stack);
}
}

View file

@ -0,0 +1,42 @@
package at.petrak.hexcasting.forge.cap.adimpl;
import at.petrak.hexcasting.api.addldata.ADIotaHolder;
import at.petrak.hexcasting.api.item.IotaHolderItem;
import at.petrak.hexcasting.api.spell.iota.Iota;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.Nullable;
public record CapItemIotaHolder(IotaHolderItem holder,
ItemStack stack) implements ADIotaHolder {
@Override
public @Nullable
CompoundTag readIotaTag() {
return holder.readIotaTag(stack);
}
@Override
public @Nullable
Iota readIota(ServerLevel world) {
return holder.readIota(stack, world);
}
@Override
public @Nullable
Iota emptyIota() {
return holder.emptyIota(stack);
}
@Override
public boolean writeIota(@Nullable Iota iota, boolean simulate) {
if (!holder.canWrite(stack, iota)) {
return false;
}
if (!simulate) {
holder.writeDatum(stack, iota);
}
return true;
}
}

View file

@ -0,0 +1,57 @@
package at.petrak.hexcasting.forge.cap.adimpl;
import at.petrak.hexcasting.api.addldata.ADMediaHolder;
import at.petrak.hexcasting.api.item.MediaHolderItem;
import net.minecraft.world.item.ItemStack;
/**
* Things that read/write media amounts from an itemstack
*/
public record CapItemMediaHolder(MediaHolderItem holder,
ItemStack stack) implements ADMediaHolder {
@Override
public int getMedia() {
return holder.getMedia(stack);
}
@Override
public int getMaxMedia() {
return holder.getMaxMedia(stack);
}
@Override
public void setMedia(int media) {
holder.setMedia(stack, media);
}
@Override
public boolean canRecharge() {
return holder.canRecharge(stack);
}
@Override
public boolean canProvide() {
return holder.canProvideMedia(stack);
}
@Override
public int getConsumptionPriority() {
return 40;
}
@Override
public boolean canConstructBattery() {
return false;
}
@Override
public int withdrawMedia(int cost, boolean simulate) {
return holder.withdrawMedia(stack, cost, simulate);
}
@Override
public int insertMedia(int amount, boolean simulate) {
return holder.insertMedia(stack, amount, simulate);
}
}

View file

@ -0,0 +1,33 @@
package at.petrak.hexcasting.forge.cap.adimpl;
import at.petrak.hexcasting.api.addldata.ADIotaHolder;
import at.petrak.hexcasting.api.spell.iota.Iota;
import at.petrak.hexcasting.common.lib.hex.HexIotaTypes;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.Nullable;
import java.util.function.Function;
public record CapStaticIotaHolder(Function<ItemStack, Iota> provider,
ItemStack stack) implements ADIotaHolder {
@Override
public @Nullable
CompoundTag readIotaTag() {
var iota = provider.apply(stack);
return iota == null ? null : HexIotaTypes.serialize(iota);
}
@Override
public @Nullable
Iota readIota(ServerLevel world) {
return provider.apply(stack);
}
@Override
public boolean writeIota(@Nullable Iota iota, boolean simulate) {
return false;
}
}

View file

@ -0,0 +1,62 @@
package at.petrak.hexcasting.forge.cap.adimpl;
import at.petrak.hexcasting.api.addldata.ADMediaHolder;
import net.minecraft.world.item.ItemStack;
import java.util.function.Supplier;
/**
* Things that always hold a constant amount of media, like amethyst
*/
public record CapStaticMediaHolder(Supplier<Integer> baseWorth,
int consumptionPriority,
ItemStack stack) implements ADMediaHolder {
@Override
public int getMedia() {
return baseWorth.get() * stack.getCount();
}
@Override
public int getMaxMedia() {
return getMedia();
}
@Override
public void setMedia(int media) {
// NO-OP
}
@Override
public boolean canRecharge() {
return false;
}
@Override
public boolean canProvide() {
return true;
}
@Override
public int getConsumptionPriority() {
return consumptionPriority;
}
@Override
public boolean canConstructBattery() {
return true;
}
@Override
public int withdrawMedia(int cost, boolean simulate) {
int worth = baseWorth.get();
if (cost < 0) {
cost = worth * stack.getCount();
}
double itemsRequired = cost / (double) worth;
int itemsUsed = Math.min((int) Math.ceil(itemsRequired), stack.getCount());
if (!simulate) {
stack.shrink(itemsUsed);
}
return itemsUsed * worth;
}
}