Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

ND0322

Members
  • Joined

  • Last visited

Everything posted by ND0322

  1. @SubscribeEvent public static void onAttachCapabilitiesPlayer(AttachCapabilitiesEvent<Entity> event){ if (event.getObject() instanceof Player) { if (!event.getObject().getCapability(CharmBagProvider.Handler).isPresent()) { event.addCapability(new ResourceLocation(truffleMod.MOD_ID, "charm_bag_cap"), new CharmBagProvider()); System.out.println("added"); } System.out.println("eventRan"); } } I have put two prints to check that the event is called and both are called
  2. Woops I set it to private. I changed it now https://github.com/ND0322/Truffle-Mod
  3. https://github.com/ND0322/Truffle-Mod/tree/main/main/java/com/ND0322/trufflemod
  4. I also did register the events properly in the main mod class
  5. I am looking to make a container that is opened through an item and has a seperate inventory for each player like an enderchest. I have created a capability to do this however it does not work. Here is the code: ItemStackHandler: public class CharmBagItemStackHandler extends ItemStackHandler implements CharmBagItemHandler { public int size; public CharmBagItemStackHandler(int size) { super(size); } public CharmBagItemStackHandler() { super(9); } public void copyFrom(CharmBagItemStackHandler source) { this.size = source.size; } } Capability provider(the code is a little messy): public class CharmBagProvider implements ICapabilityProvider, INBTSerializable<CompoundTag>{ public static Capability<CharmBagItemStackHandler> Handler = CapabilityManager.get(new CapabilityToken<>(){}); private CharmBagItemStackHandler inventory = new CharmBagItemStackHandler(9); private Player player; public static CharmBagItemStackHandler CHARMBAG = null; private LazyOptional<CharmBagItemHandler> lazyOptional = LazyOptional.of(() -> inventory); @Nonnull private CharmBagItemStackHandler createHandler() { if (CHARMBAG == null) { CHARMBAG = new CharmBagItemStackHandler(); } return CHARMBAG; } @Override public <T> LazyOptional<T> getCapability(Capability<T> cap, Direction side) { if (cap == Handler) { lazyOptional.cast(); } return LazyOptional.empty(); } @Override public CompoundTag serializeNBT() { return inventory.serializeNBT(); } @Override public void deserializeNBT(CompoundTag nbt) { inventory.deserializeNBT(nbt); } } Container: public class CharmsBagContainer extends AbstractContainerMenu{ private final ItemStack charmBag; private final Level level; // Client Constructor public CharmsBagContainer(int id, Inventory playerInv) { this(id, playerInv, playerInv.player.getMainHandItem()); } // Server constructor public CharmsBagContainer(int id, Inventory inventory, ItemStack item) { super(ContainerInit.CHARM_CONTAINER.get(), id); this.level = inventory.player.level; this.charmBag = item; Player player = inventory.player; System.out.println(player.getCapability(CharmBagProvider.Handler).isPresent()); player.getCapability(CharmBagProvider.Handler).ifPresent(handler -> { for(int i = 0; i < 3; ++i) { for(int j = 0; j < 3; ++j) { this.addSlot(new SlotItemHandler(handler, j + i * 3, 44 + j * 18, 16 + i * 18)); } } }); this.addPlayerHotbar(inventory); this.addPlayerInventory(inventory); } private void addPlayerInventory(Inventory inv){ for(int i = 0; i < 3; ++i) { for(int j = 0; j < 9; ++j) { this.addSlot(new Slot(inv, j + i * 9 + 9, 8 + j * 18, 84 + i * 18)); } } } private void addPlayerHotbar(Inventory inv){ for(int k = 0; k < 9; ++k) { this.addSlot(new Slot(inv, k, 8 + k * 18, 142)); } } @Override public ItemStack quickMoveStack(Player pPlayer, int pIndex) { ItemStack toReturn = ItemStack.EMPTY; Slot slot = this.getSlot(pIndex); if(slot != null && slot.hasItem()){ ItemStack stack = slot.getItem(); toReturn = stack.copy(); if(pIndex < 9){ if(!this.moveItemStackTo(stack, 9, this.slots.size(), true)){ return ItemStack.EMPTY; } }else if(!this.moveItemStackTo(stack, 0, 9, false)){ return ItemStack.EMPTY; } if(!stack.isEmpty()){ slot.set(ItemStack.EMPTY); }else{ slot.setChanged(); } } return toReturn; } @Override public boolean stillValid(Player player) { return player.getMainHandItem().is(this.charmBag.getItem()); } EventHandler: @SubscribeEvent public static void onAttachCapabilitiesPlayer(AttachCapabilitiesEvent<Entity> event){ if (event.getObject() instanceof Player) { if (!event.getObject().getCapability(MaxHealthProvider.MAX_HEALTH_CHANGER).isPresent()) { // The player does not already have this capability so we need to add the capability provider here event.addCapability(new ResourceLocation(truffleMod.MOD_ID, "max_health"), new MaxHealthProvider()); } } } @SubscribeEvent public static void onPlayerCloned(PlayerEvent.Clone event) { if (event.isWasDeath()) { // We need to copyFrom the capabilities event.getOriginal().getCapability(MaxHealthProvider.MAX_HEALTH_CHANGER).ifPresent(oldStore -> { event.getPlayer().getCapability(MaxHealthProvider.MAX_HEALTH_CHANGER).ifPresent(newStore -> { newStore.copyFrom(oldStore); }); }); } } @SubscribeEvent public static void onRegisterCapabilities(RegisterCapabilitiesEvent event) { event.register(MaxHealth.class); } } My issue is probably with attaching the capability as I print to show if the player has the capability and it prints false.
  6. I am wondering how you make a healthbar that gets render over every entity. I want to make something like this: https://minecraft.curseforge.com/projects/neat
  7. nvm i figured this out
  8. I would like to disable vanilla critical hits
  9. One more question: how do i run the CriticalHitEvent. It does not let me register it in the main mod class and when I just @SubscribeEvent in an event handler, it does not fire
  10. Thanks! It works great.
  11. I moved the EntityAttributeModification to another class and setup a listener in the main mod class but I get the same error public class EventHandler { @SubscribeEvent public static void onEntityAttributeModificationEvent(EntityAttributeModificationEvent event) { event.add(EntityType.PLAYER, modAttributes.CRIT_CHANCE.get()); } } public truffleMod() { IEventBus bus = FMLJavaModLoadingContext.get().getModEventBus(); IEventBus eventBus = MinecraftForge.EVENT_BUS; ItemInit.ITEMS.register(bus); ModEffects.register(bus); modAttributes.ATTRIBUTES.register(bus); MinecraftForge.EVENT_BUS.register(this); eventBus.addListener(EventHandler::onEntityAttributeModificationEvent); }
  12. I am making custom attributes for players in my mod but when I try to add a modifier to the player it crashes with the message "Cannot invoke "net.minecraft.world.entity.ai.attributes.AttributeInstance.addTransientModifier(net.minecraft.world.entity.ai.attributes.AttributeModifier)" because the return value of "net.minecraft.world.entity.player.Player.getAttribute(net.minecraft.world.entity.ai.attributes.Attribute)" is null". Main Mod Class: public truffleMod() { IEventBus bus = FMLJavaModLoadingContext.get().getModEventBus(); ItemInit.ITEMS.register(bus); ModEffects.register(bus); MinecraftForge.EVENT_BUS.register(this); modAttributes.ATTRIBUTES.register(bus); } Attribute Registrey: package data.trufflemod.attributes; import java.util.UUID; import com.ND0322.trufflemod.truffleMod; import net.minecraft.world.entity.ai.attributes.Attribute; import net.minecraft.world.entity.ai.attributes.RangedAttribute; import net.minecraftforge.registries.DeferredRegister; import net.minecraftforge.registries.ForgeRegistries; import net.minecraftforge.registries.RegistryObject; public class modAttributes { public static final DeferredRegister<Attribute> ATTRIBUTES = DeferredRegister.create(ForgeRegistries.ATTRIBUTES, truffleMod.MOD_ID); //ATTRIBUTES public static final RegistryObject<Attribute> CRIT_CHANCE = ATTRIBUTES.register("crit_chance", () -> (Attribute) new RangedAttribute(truffleMod.MOD_ID + ".playerCritChance", 4, 0, 100).setSyncable(true)); } EventHandler: @SubscribeEvent public static void onEntityAttributeModificationEvent(EntityAttributeModificationEvent event) { event.add(EntityType.PLAYER, modAttributes.CRIT_CHANCE.get()); } @SubscribeEvent public static void onEvent(EntityJoinWorldEvent event) { if ((event.getEntity() instanceof Player)) { Player player = (Player)event.getEntity(); AttributeModifier CRIT_CHANCE = new AttributeModifier(UUID.fromString("fb84a4dd-8592-48f7-8e47-911987651d42"), "damage modifier", stats.getDamageMultiplier(), AttributeModifier.Operation.MULTIPLY_TOTAL); player.getAttribute(modAttributes.CRIT_CHANCE.get()).addTransientModifier(CRIT_CHANCE); } } What might be causing this error?
  13. Ok I moved it inside a method and now I dont crash
  14. I only have one player.addEffect(). Should I move the effect instance to a different class or method inside the file?
  15. Where should I create the MobEffctInstances?
  16. package com.ND0322.trufflemod; import com.ND0322.trufflemod.effects.ModEffects; import com.ND0322.trufflemod.init.ItemInit; import com.ND0322.trufflemod.potions.ModPotions; import net.minecraft.world.item.CreativeModeTab; import net.minecraft.world.item.ItemStack; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.OnlyIn; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.eventbus.api.IEventBus; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; @Mod("trufflemod") public class truffleMod { public static final String MOD_ID = "trufflemod"; public static final CreativeModeTab Truffle_Tab = new CreativeModeTab(MOD_ID) { @Override @OnlyIn(Dist.CLIENT) public ItemStack makeIcon() { return new ItemStack(ItemInit.BRONZE_DOGE_COIN.get()); } }; public truffleMod() { IEventBus bus = FMLJavaModLoadingContext.get().getModEventBus(); ItemInit.ITEMS.register(bus); ModEffects.register(bus); MinecraftForge.EVENT_BUS.register(this); } } This is the main mod class how do I register the deferred register?
  17. The timer and variables are from before and I am currently not trying to use them. My main problem is the crash issue; however, I do not have a deferred register in the main mod class so that may be the issue. I will try to implement this now
  18. Here is the item class package com.ND0322.trufflemod.items; import java.util.Timer; import java.util.TimerTask; import javax.annotation.Nonnull; import com.ND0322.trufflemod.effects.DamageBoostEffect; import com.ND0322.trufflemod.effects.ModEffects; import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.ImmutableMultimap; import com.google.common.collect.ListMultimap; import com.google.common.collect.Multimap; import net.minecraft.world.item.ItemStack; import net.minecraft.client.renderer.EffectInstance; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResultHolder; import net.minecraft.world.effect.MobEffect; import net.minecraft.world.effect.MobEffectInstance; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.EquipmentSlot; import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.ai.attributes.*; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.SwordItem; import net.minecraft.world.item.Tier; import net.minecraft.world.level.Level; public class CopperizedDiamondSword extends SwordItem{ public boolean rightClickReady = true; public boolean rightClickActive = false; MobEffectInstance m = new MobEffectInstance(ModEffects.DAMAGE_BOOST_EFFECT.get(),10,3 ); public CopperizedDiamondSword(Tier pTier, int pAttackDamageModifier, float pAttackSpeedModifier, Properties pProperties) { super(pTier, pAttackDamageModifier, pAttackSpeedModifier, pProperties); } public static<K, V> ListMultimap<K, V> toMutableMultimap(Multimap<K, V> original) { ListMultimap<K, V> copy = ArrayListMultimap.create(); copy.putAll(original); return copy; } public static<K, V> ImmutableMultimap<K, V> toImmutableMultimap(ListMultimap<K, V> original) { return new ImmutableMultimap.Builder<K, V>().putAll(original).build(); } @Override public InteractionResultHolder<ItemStack> use(Level world, Player player, InteractionHand hand) { if (rightClickReady) { player.addEffect(m); } return super.use(world, player, hand); } @Override public Multimap<Attribute, AttributeModifier> getAttributeModifiers(EquipmentSlot slot, ItemStack stack) { Multimap<Attribute, AttributeModifier> multimap = super.getAttributeModifiers(slot, stack); ListMultimap<Attribute, AttributeModifier> mutableMultimap = toMutableMultimap(multimap); if (slot == EquipmentSlot.MAINHAND && rightClickActive) { mutableMultimap.put(Attributes.ATTACK_DAMAGE, new AttributeModifier(BASE_ATTACK_DAMAGE_UUID, "ATTACK_DAMAGE_MODIFIER", Integer.MAX_VALUE, AttributeModifier.Operation.ADDITION)); } return (toImmutableMultimap(mutableMultimap)); } public void rightClickCooldown(int time) { Timer timer = new Timer(); timer.scheduleAtFixedRate(new TimerTask() { int i = time; public void run() { i--; if (i < 0) { System.out.println("cooldown done"); timer.cancel(); rightClickReady = true; System.out.println("cooldown done"); } } },0,1000); } public void buffTimer(int time) { Timer timer = new Timer(); timer.scheduleAtFixedRate(new TimerTask() { int i = time; public void run() { i--; if (i < 0) { System.out.println("timer done"); timer.cancel(); rightClickActive = false; System.out.println(rightClickActive); } } },0,1000); } } The timer stuff is from before I havent removed yet
  19. package com.ND0322.trufflemod.effects; import net.minecraft.world.effect.MobEffect; import net.minecraft.world.effect.MobEffectCategory; import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.ai.attributes.AttributeModifier; import net.minecraft.world.entity.ai.attributes.Attributes; public class DamageBoostEffect extends MobEffect { public DamageBoostEffect(MobEffectCategory mobEffectCategory, int color){ super(mobEffectCategory, color); } @Override public boolean isDurationEffectTick(int pDuration, int pAmplifier) { return true; } } here is the effect class
  20. When I run it I get a crash that says Caught exception during event RegistryEvent.Register<minecraft:item> dispatch for modid trufflemod java.lang.NullPointerException: Registry Object not present: trufflemod:damage_boost. package com.ND0322.trufflemod.effects; import com.ND0322.trufflemod.truffleMod; import net.minecraft.world.effect.AttackDamageMobEffect; import net.minecraft.world.effect.MobEffect; import net.minecraft.world.effect.MobEffectCategory; import net.minecraft.world.entity.ai.attributes.AttributeModifier; import net.minecraft.world.entity.ai.attributes.Attributes; import net.minecraftforge.eventbus.api.IEventBus; import net.minecraftforge.registries.DeferredRegister; import net.minecraftforge.registries.ForgeRegistries; import net.minecraftforge.registries.RegistryObject; public class ModEffects { public static final DeferredRegister<MobEffect> MOB_EFFECTS = DeferredRegister.create(ForgeRegistries.MOB_EFFECTS,truffleMod.MOD_ID); public static final RegistryObject<MobEffect> DAMAGE_BOOST_EFFECT = MOB_EFFECTS.register("damage_boost", () -> new DamageBoostEffect(MobEffectCategory.BENEFICIAL, 3124687).addAttributeModifier(Attributes.ATTACK_DAMAGE, "648D7064-6A60-4F59-8ABE-C2C23A6DD7A9", 100d, AttributeModifier.Operation.ADDITION)); public static void register(IEventBus eventBus) { MOB_EFFECTS.register(eventBus); } } here is the effect registration.
  21. Thank you for the help I think I understand how to do this.
  22. I read online that you can add attribute modifiers to a MobEffectInstance here: https://forge.gemwire.uk/wiki/Mob_Effects. How would I "chain addAttributeModifier" when making the mobEffectInstance?
  23. So if I were to add an effect to the player I would need to first create that effect with attribute modifers. How would I do this effecttively?
  24. The original code has it so when you right click holding the sword it changes a variable that allows the attribute modifer to change. Here is the code : @Override public InteractionResultHolder<ItemStack> use(Level world, Player player, InteractionHand hand) { if (rightClickReady) { rightClickActive = true; rightClickReady = false; buffTimer(10); rightClickCooldown(30); } return super.use(world, player, hand); } @Override public Multimap<Attribute, AttributeModifier> getAttributeModifiers(EquipmentSlot slot, ItemStack stack) { Multimap<Attribute, AttributeModifier> multimap = super.getAttributeModifiers(slot, stack); ListMultimap<Attribute, AttributeModifier> mutableMultimap = toMutableMultimap(multimap); if (slot == EquipmentSlot.MAINHAND && rightClickActive) { mutableMultimap.put(Attributes.ATTACK_DAMAGE, new AttributeModifier(BASE_ATTACK_DAMAGE_UUID, "ATTACK_DAMAGE_MODIFIER", Integer.MAX_VALUE, AttributeModifier.Operation.ADDITION)); } return (toImmutableMultimap(mutableMultimap)); }

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.