This commit is contained in:
yrsegal@gmail.com 2022-10-01 12:52:37 -04:00
parent 051a334e85
commit 1d6bb87864
13 changed files with 249 additions and 72 deletions

View file

@ -0,0 +1,33 @@
package at.petrak.hexcasting.api.spell.mishaps
import at.petrak.hexcasting.api.misc.FrozenColorizer
import at.petrak.hexcasting.api.spell.SpellDatum
import at.petrak.hexcasting.api.spell.casting.CastingContext
import at.petrak.hexcasting.api.utils.aqua
import at.petrak.hexcasting.api.utils.asTranslatedComponent
import net.minecraft.network.chat.Component
import net.minecraft.world.entity.Entity
import net.minecraft.world.entity.item.ItemEntity
import net.minecraft.world.item.DyeColor
class MishapBadEntity(val entity: Entity, val wanted: Component) : Mishap() {
override fun accentColor(ctx: CastingContext, errorCtx: Context): FrozenColorizer =
dyeColor(DyeColor.BROWN)
override fun execute(ctx: CastingContext, errorCtx: Context, stack: MutableList<SpellDatum<*>>) {
yeetHeldItemsTowards(ctx, entity.position())
}
override fun errorMessage(ctx: CastingContext, errorCtx: Context) =
error("bad_entity", actionName(errorCtx.action), wanted, entity.displayName.plainCopy().aqua)
companion object {
@JvmStatic
fun of(entity: Entity, stub: String): Mishap {
val component = "hexcasting.mishap.bad_item.$stub".asTranslatedComponent
if (entity is ItemEntity)
return MishapBadItem(entity, component)
return MishapBadEntity(entity, component)
}
}
}

View file

@ -3,6 +3,7 @@ package at.petrak.hexcasting.api.spell.mishaps
import at.petrak.hexcasting.api.misc.FrozenColorizer
import at.petrak.hexcasting.api.spell.SpellDatum
import at.petrak.hexcasting.api.spell.casting.CastingContext
import at.petrak.hexcasting.api.utils.aqua
import net.minecraft.world.entity.Entity
import net.minecraft.world.item.DyeColor
@ -15,5 +16,5 @@ class MishapImmuneEntity(val entity: Entity) : Mishap() {
}
override fun errorMessage(ctx: CastingContext, errorCtx: Context) =
error("immune_entity", actionName(errorCtx.action), entity.displayName)
error("immune_entity", actionName(errorCtx.action), entity.displayName.plainCopy().aqua)
}

View file

@ -1,12 +1,12 @@
package at.petrak.hexcasting.common.casting.operators
import at.petrak.hexcasting.api.spell.ConstManaOperator
import at.petrak.hexcasting.api.spell.getChecked
import at.petrak.hexcasting.api.spell.SpellDatum
import at.petrak.hexcasting.api.spell.casting.CastingContext
import at.petrak.hexcasting.api.spell.mishaps.MishapBadItem
import at.petrak.hexcasting.api.spell.getChecked
import at.petrak.hexcasting.api.spell.mishaps.MishapBadEntity
import at.petrak.hexcasting.xplat.IXplatAbstractions
import net.minecraft.world.entity.item.ItemEntity
import net.minecraft.world.entity.Entity
object OpTheCoolerRead : ConstManaOperator {
override val argc = 1
@ -15,17 +15,16 @@ object OpTheCoolerRead : ConstManaOperator {
args: List<SpellDatum<*>>,
ctx: CastingContext
): List<SpellDatum<*>> {
val target = args.getChecked<ItemEntity>(0, argc)
val target = args.getChecked<Entity>(0, argc)
ctx.assertEntityInRange(target)
val stack = target.item
val datumHolder = IXplatAbstractions.INSTANCE.findDataHolder(stack)
?: throw MishapBadItem.of(target, "iota.read")
val datumHolder = IXplatAbstractions.INSTANCE.findDataHolder(target)
?: throw MishapBadEntity.of(target, "iota.read")
val datum = datumHolder.readDatum(ctx.world)
?: datumHolder.emptyDatum()
?: throw MishapBadItem.of(target, "iota.read")
?: throw MishapBadEntity.of(target, "iota.read")
return listOf(datum)
}
}

View file

@ -17,6 +17,7 @@ import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.entity.npc.VillagerProfession;
@ -99,6 +100,9 @@ public interface IXplatAbstractions {
@Nullable
DataHolder findDataHolder(ItemStack stack);
@Nullable
DataHolder findDataHolder(Entity entity);
@Nullable
HexHolder findHexHolder(ItemStack stack);

View file

@ -69,6 +69,7 @@
"item.hexcasting.creative_unlocker.tooltip": "Consume to unlock all %s knowledge.",
"item.hexcasting.creative_unlocker.mod_name": "Hexcasting",
"entity.hexcasting.wall_scroll": "Hanging Scroll",
"block.hexcasting.conjured": "Conjured Block",
"block.hexcasting.slate.blank": "Blank Slate",
@ -437,6 +438,7 @@
"hexcasting.mishap.eval_too_deep": "Recursively evaluated too deep",
"hexcasting.mishap.no_item": "%s needs %s but got nothing",
"hexcasting.mishap.no_item.offhand": "%s needs %s in the other hand but got nothing",
"hexcasting.mishap.bad_entity": "%s needs %s but got %s",
"hexcasting.mishap.bad_item": "%s needs %s but got %dx %s",
"hexcasting.mishap.bad_item.offhand": "%s needs %s in the other hand but got %dx %s",
"hexcasting.mishap.bad_item.iota": "a place to store iotas",

View file

@ -1,66 +1,7 @@
package at.petrak.hexcasting.fabric.cc;
import at.petrak.hexcasting.api.addldata.DataHolder;
import at.petrak.hexcasting.api.item.DataHolderItem;
import at.petrak.hexcasting.api.spell.SpellDatum;
import dev.onyxstudios.cca.api.v3.item.ItemComponent;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.Nullable;
import dev.onyxstudios.cca.api.v3.component.Component;
import java.util.function.Function;
public abstract class CCDataHolder extends ItemComponent implements DataHolder {
public CCDataHolder(ItemStack stack) {
super(stack, HexCardinalComponents.DATA_HOLDER);
}
public static class ItemBased extends CCDataHolder {
private final DataHolderItem dataHolder;
public ItemBased(ItemStack stack) {
super(stack);
if (!(stack.getItem() instanceof DataHolderItem data)) {
throw new IllegalStateException("item is not a data holder: " + stack);
}
this.dataHolder = data;
}
@Override
public @Nullable CompoundTag readRawDatum() {
return this.dataHolder.readDatumTag(this.stack);
}
@Override
public boolean writeDatum(@Nullable SpellDatum<?> datum, boolean simulate) {
var canWrite = this.dataHolder.canWrite(this.stack, datum);
if (!canWrite) {
return false;
}
if (!simulate) {
this.dataHolder.writeDatum(this.stack, datum);
}
return true;
}
}
public static class Static extends CCDataHolder {
private final Function<ItemStack, SpellDatum<?>> provider;
public Static(ItemStack stack, Function<ItemStack, SpellDatum<?>> provider) {
super(stack);
this.provider = provider;
}
@Override
public @Nullable CompoundTag readRawDatum() {
SpellDatum<?> datum = this.provider.apply(this.stack);
return datum == null ? null : datum.serializeToNBT();
}
@Override
public boolean writeDatum(@Nullable SpellDatum<?> datum, boolean simulate) {
return false;
}
}
public interface CCDataHolder extends DataHolder, Component {
}

View file

@ -0,0 +1,82 @@
package at.petrak.hexcasting.fabric.cc;
import at.petrak.hexcasting.api.addldata.DataHolder;
import at.petrak.hexcasting.api.spell.SpellDatum;
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 CCEntityDataHolder implements CCDataHolder {
@Override
public void writeToNbt(@NotNull CompoundTag tag) {
// NO-OP
}
@Override
public void readFromNbt(@NotNull CompoundTag tag) {
// NO-OP
}
public static class ItemDelegating extends CCEntityDataHolder {
private final Supplier<ItemStack> item;
public ItemDelegating(Supplier<ItemStack> stackSupplier) {
this.item = stackSupplier;
}
@Override
public @Nullable CompoundTag readRawDatum() {
DataHolder delegate = IXplatAbstractions.INSTANCE.findDataHolder(item.get());
return delegate == null ? null : delegate.readRawDatum();
}
@Override
public boolean writeDatum(@Nullable SpellDatum<?> datum, boolean simulate) {
DataHolder delegate = IXplatAbstractions.INSTANCE.findDataHolder(item.get());
return delegate != null && delegate.writeDatum(datum, simulate);
}
@Override
public @Nullable SpellDatum<?> readDatum(ServerLevel world) {
DataHolder delegate = IXplatAbstractions.INSTANCE.findDataHolder(item.get());
return delegate == null ? null : delegate.readDatum(world);
}
@Override
public @Nullable SpellDatum<?> emptyDatum() {
DataHolder delegate = IXplatAbstractions.INSTANCE.findDataHolder(item.get());
return delegate == null ? null : delegate.emptyDatum();
}
}
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 writeDatum(@Nullable SpellDatum<?> datum, boolean simulate) {
return false;
}
}
}

View file

@ -0,0 +1,65 @@
package at.petrak.hexcasting.fabric.cc;
import at.petrak.hexcasting.api.item.DataHolderItem;
import at.petrak.hexcasting.api.spell.SpellDatum;
import dev.onyxstudios.cca.api.v3.item.ItemComponent;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.Nullable;
import java.util.function.Function;
public abstract class CCItemDataHolder extends ItemComponent implements CCDataHolder {
public CCItemDataHolder(ItemStack stack) {
super(stack, HexCardinalComponents.DATA_HOLDER);
}
public static class ItemBased extends CCItemDataHolder {
private final DataHolderItem dataHolder;
public ItemBased(ItemStack stack) {
super(stack);
if (!(stack.getItem() instanceof DataHolderItem data)) {
throw new IllegalStateException("item is not a data holder: " + stack);
}
this.dataHolder = data;
}
@Override
public @Nullable CompoundTag readRawDatum() {
return this.dataHolder.readDatumTag(this.stack);
}
@Override
public boolean writeDatum(@Nullable SpellDatum<?> datum, boolean simulate) {
var canWrite = this.dataHolder.canWrite(this.stack, datum);
if (!canWrite) {
return false;
}
if (!simulate) {
this.dataHolder.writeDatum(this.stack, datum);
}
return true;
}
}
public static class Static extends CCItemDataHolder {
private final Function<ItemStack, SpellDatum<?>> provider;
public Static(ItemStack stack, Function<ItemStack, SpellDatum<?>> provider) {
super(stack);
this.provider = provider;
}
@Override
public @Nullable CompoundTag readRawDatum() {
SpellDatum<?> datum = this.provider.apply(this.stack);
return datum == null ? null : datum.serializeToNBT();
}
@Override
public boolean writeDatum(@Nullable SpellDatum<?> datum, boolean simulate) {
return false;
}
}
}

View file

@ -6,6 +6,7 @@ import at.petrak.hexcasting.api.item.HexHolderItem;
import at.petrak.hexcasting.api.item.ManaHolderItem;
import at.petrak.hexcasting.api.mod.HexConfig;
import at.petrak.hexcasting.api.spell.SpellDatum;
import at.petrak.hexcasting.common.entities.EntityWallScroll;
import at.petrak.hexcasting.common.lib.HexItems;
import dev.onyxstudios.cca.api.v3.component.ComponentKey;
import dev.onyxstudios.cca.api.v3.component.ComponentRegistry;
@ -16,6 +17,8 @@ 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.Mob;
import net.minecraft.world.entity.decoration.ItemFrame;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.item.Items;
import static at.petrak.hexcasting.api.HexAPI.modLoc;
@ -53,16 +56,20 @@ public class HexCardinalComponents implements EntityComponentInitializer, ItemCo
registry.registerFor(ServerPlayer.class, FLIGHT, CCFlight::new);
registry.registerFor(ServerPlayer.class, HARNESS, CCHarness::new);
registry.registerFor(ServerPlayer.class, PATTERNS, CCPatterns::new);
registry.registerFor(ItemEntity.class, DATA_HOLDER, CCEntityDataHolder.EntityItemDelegating::new);
registry.registerFor(ItemFrame.class, DATA_HOLDER, CCEntityDataHolder.ItemFrameDelegating::new);
registry.registerFor(EntityWallScroll.class, DATA_HOLDER, CCEntityDataHolder.ScrollDelegating::new);
}
@Override
public void registerItemComponentFactories(ItemComponentFactoryRegistry registry) {
registry.register(i -> i instanceof ColorizerItem, COLORIZER, CCColorizer.ItemBased::new);
registry.register(i -> i instanceof DataHolderItem, DATA_HOLDER, CCDataHolder.ItemBased::new);
registry.register(i -> i instanceof DataHolderItem, DATA_HOLDER, CCItemDataHolder.ItemBased::new);
// oh havoc, you think you're so funny
// the worst part is you're /right/
registry.register(Items.PUMPKIN_PIE, DATA_HOLDER, stack -> new CCDataHolder.Static(stack,
registry.register(Items.PUMPKIN_PIE, DATA_HOLDER, stack -> new CCItemDataHolder.Static(stack,
s -> SpellDatum.make(Math.PI * s.getCount())));
registry.register(i -> i instanceof ManaHolderItem, MANA_HOLDER, CCManaHolder.ItemBased::new);

View file

@ -225,6 +225,13 @@ public class FabricXplatImpl implements IXplatAbstractions {
return cc.orElse(null);
}
@Override
public @Nullable
DataHolder findDataHolder(Entity entity) {
var cc = HexCardinalComponents.DATA_HOLDER.maybeGet(entity);
return cc.orElse(null);
}
@Override
public @Nullable
HexHolder findHexHolder(ItemStack stack) {

View file

@ -216,6 +216,7 @@ public class ForgeHexInitializer {
modBus.addListener(ForgeCapabilityHandler::registerCaps);
evBus.addGenericListener(ItemStack.class, ForgeCapabilityHandler::attachItemCaps);
evBus.addGenericListener(BlockEntity.class, ForgeCapabilityHandler::attachBlockEntityCaps);
evBus.addGenericListener(Entity.class, ForgeCapabilityHandler::attachEntityCaps);
modBus.register(HexForgeDataGenerators.class);
modBus.register(ForgeCapabilityHandler.class);

View file

@ -11,11 +11,15 @@ import at.petrak.hexcasting.api.item.HexHolderItem;
import at.petrak.hexcasting.api.item.ManaHolderItem;
import at.petrak.hexcasting.api.mod.HexConfig;
import at.petrak.hexcasting.api.spell.SpellDatum;
import at.petrak.hexcasting.common.entities.EntityWallScroll;
import at.petrak.hexcasting.common.lib.HexItems;
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;
@ -84,12 +88,37 @@ public class ForgeCapabilityHandler {
() -> new ItemBasedColorizer(colorizer, stack)));
}
public static void attachEntityCaps(AttachCapabilitiesEvent<Entity> evt) {
if (evt.getObject() instanceof ItemEntity item) {
evt.addCapability(DATA_HOLDER_CAPABILITY, delegateTo(item::getItem)); // Delegate to the item
} else if (evt.getObject() instanceof ItemFrame frame) {
evt.addCapability(DATA_HOLDER_CAPABILITY, delegateTo(frame::getItem));
} else if (evt.getObject() instanceof EntityWallScroll scroll) {
evt.addCapability(DATA_HOLDER_CAPABILITY, delegateTo(() -> scroll.scroll));
}
}
public static void attachBlockEntityCaps(AttachCapabilitiesEvent<BlockEntity> evt) {
if (evt.getObject() instanceof BlockEntityAbstractImpetus impetus)
evt.addCapability(IMPETUS_HANDLER, provide(impetus, CapabilityItemHandler.ITEM_HANDLER_CAPABILITY,
() -> new ForgeImpetusCapability(impetus)));
}
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);
}
};
}
private static <CAP> SimpleProvider<CAP> provide(Entity entity, Capability<CAP> capability, NonNullSupplier<CAP> supplier) {
return provide(entity::isRemoved, capability, supplier);
}
private static <CAP> SimpleProvider<CAP> provide(BlockEntity be, Capability<CAP> capability, NonNullSupplier<CAP> supplier) {
return provide(be::isRemoved, capability, supplier);
}

View file

@ -265,6 +265,12 @@ public class ForgeXplatImpl implements IXplatAbstractions {
return maybeCap.orElse(null);
}
@Override
public @Nullable DataHolder findDataHolder(Entity entity) {
var maybeCap = entity.getCapability(HexCapabilities.DATUM).resolve();
return maybeCap.orElse(null);
}
@Override
public @Nullable
HexHolder findHexHolder(ItemStack stack) {