Jump to content

FenrisFox86

Members
  • Posts

    28
  • Joined

  • Last visited

Everything posted by FenrisFox86

  1. So can you think of any other way to find out which hand was used for an attack? The problem here is that I want these effects to also work on vanilla swords or even weapons from other mods. Could I create a mixin that fires an event when Item#hitEntity is called to know which stack was used? I haven't really worked with mixins yet so I don't know if they are a solution in this situation.
  2. So I'm creating a mod that includes a double-wieldable variation of the sworditem. The double wielding itsself works fine. But my mod also adds some enchantments to the game. Right now I have the problem that offhand weapons use the mainhand weapons' enchantment information. Here's my enchantment code: @SubscribeEvent public static void OnHitEntity(LivingAttackEvent event) { Entity entity = event.getSource().getTrueSource(); if(entity instanceof LivingEntity) { LivingEntity living = (LivingEntity) entity; LivingEntity target = event.getEntityLiving(); Iterable<ItemStack> equipment = living.getEquipmentAndArmor(); ItemStack stack = living.getHeldItem(living.getActiveHand()); //Dark Contract level = EnchantmentHelper.getEnchantmentLevel(DARK_CONTRACT.get(), stack); if(level > 0) { target.attackEntityFrom(DamageSource.GENERIC, level*2); living.attackEntityFrom(DamageSource.causeIndirectMagicDamage(living, null), level); } } } As you see, I only apply the effects of the active hand. In my double sword code, I set the active hand to offhand on right click: public ActionResult<ItemStack> onItemRightClick(World worldIn, PlayerEntity playerIn, Hand handIn) { playerIn.setActiveHand(Hand.OFF_HAND); return ActionResult.resultSuccess(playerIn.getHeldItemOffhand()); } @Override public ActionResultType itemInteractionForEntity(ItemStack stack, PlayerEntity playerIn, LivingEntity target, Hand hand) { playerIn.setActiveHand(Hand.OFF_HAND); stack.getItem().hitEntity(stack, target, playerIn); return ActionResultType.SUCCESS; } So even though I set the active hand and use this information to apply the effects, it keeps using the wrong hand's enchantments. Since the hitEntity() method takes an ItemStack as an argument, I started asking myself if there was a way to get the stack that was used from the event. That would make it much easier. Any Ideas are appreciated.
  3. I'm now coding since some time, but im newer to java and modding, so excuse me, if I'm making some weird mistakes. Hello, some time ago, I started making a mod where the player has a new "awesome charge"- value that can affect him in different ways. I've created a capability for that charge which cost me a lot of time, because I didn't find a tutorial for such a thing in 1.15.2. There are some code fragments that I don't understand. Now I'm having a big problem. As long as I'm in the world, the value can be edited via get, set, add and subtract very well, but when I quit and restart the world, The value is reset to default. I think it's most likely, that the values are just not being saved when I'm quittig the world, but I don't know, how to make it save the value. Here's the code: //The java interface package com.FenrisFox86.syringe_mod.world.entityData; public interface IAwesomeCharge { public void subtract(float points); public void add(float points); public void set(float points); public float get(); } //The implementation package com.FenrisFox86.syringe_mod.world.entityData; public class AwesomeCharge implements IAwesomeCharge { private float points = 20.0F; public void subtract(float points) { this.points -= points; if (this.points < 0.0F) { this.points = 0.0F; } } public void add(float points) { this.points += points; if (this.points > 20.0F) { this.points = 20.0F; } } public void set(float points) { this.points = points; if (this.points < 0.0F) { this.points = 0.0F; } if (this.points > 20.0F) { this.points = 20.0F; } } public float get() { return this.points; } } the capability package com.FenrisFox86.syringe_mod.world.entityData; import net.minecraft.nbt.FloatNBT; import net.minecraft.util.Direction; import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.capabilities.CapabilityInject; import net.minecraftforge.common.capabilities.CapabilityManager; import net.minecraftforge.common.capabilities.ICapabilitySerializable; import net.minecraftforge.common.util.LazyOptional; import javax.annotation.Nonnull; import javax.annotation.Nullable; public class ChargeCapability implements ICapabilitySerializable<FloatNBT> { @CapabilityInject(IAwesomeCharge.class) public static final Capability<IAwesomeCharge> CHARGE_CAP = null; private final LazyOptional<IAwesomeCharge> instance = LazyOptional.of(CHARGE_CAP::getDefaultInstance); //method to register the capability in main class without having too much text. public static void register() { CapabilityManager.INSTANCE.register(IAwesomeCharge.class, new ChargeStorage(), AwesomeCharge::new); } @Nonnull @Override public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) { return CHARGE_CAP.orEmpty(cap, instance); } @Override public FloatNBT serializeNBT() { return (FloatNBT) CHARGE_CAP.getStorage().writeNBT( CHARGE_CAP, instance.orElseThrow(() -> new IllegalArgumentException("LazyOptional cannot be empty!")), null); } @Override public void deserializeNBT(FloatNBT nbt) { CHARGE_CAP.getStorage().readNBT( CHARGE_CAP, instance.orElseThrow(() -> new IllegalArgumentException("LazyOptional cannot be empty!")), null, nbt); } } //the storage handler package com.FenrisFox86.syringe_mod.world.entityData; import net.minecraft.nbt.FloatNBT; import net.minecraft.nbt.INBT; import net.minecraft.util.Direction; import net.minecraftforge.common.capabilities.Capability; public class ChargeStorage implements Capability.IStorage<IAwesomeCharge> { @Override public INBT writeNBT(Capability<IAwesomeCharge> capability, IAwesomeCharge instance, Direction side) { return FloatNBT.valueOf(instance.get()); } @Override public void readNBT(Capability<IAwesomeCharge> capability, IAwesomeCharge instance, Direction side, INBT nbt) { instance.set(((FloatNBT) nbt).getFloat()); } } where I'm attaching the capability public static final ResourceLocation CHARGE_CAP = new ResourceLocation(SyringeMod.MOD_ID, "charge"); @SubscribeEvent public void attachCapabilitiesEntity(final AttachCapabilitiesEvent<Entity> event) { if(event.getObject() instanceof PlayerEntity) event.addCapability(CHARGE_CAP, new ChargeCapability()); }
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.