Skip to content

Commit

Permalink
- Bunny Ears only get damage when jumping or when food is eaten
Browse files Browse the repository at this point in the history
- reduced Bunny Ears default durability from 600 to 450
  • Loading branch information
cech12 committed May 14, 2024
1 parent 2d6efdf commit 49eb6b2
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 21 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Forge Recommended Versioning](https://mcforge.readthedocs.io/en/latest/conventions/versioning/).

## [1.20.4-5.1.2.0] - 2024-05-11
### Changed
- Bunny Ears only get damage when jumping or when food is eaten
- reduced Bunny Ears default durability from 600 to 450

## [1.20.4-5.1.1.1] - 2024-05-11
### Fixed
- Respiration effect of Aquanaut Helmet was removed when getting underwater (all loaders)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import de.cech12.usefulhats.item.IItemFishedListener;
import de.cech12.usefulhats.item.IItemUseListener;
import de.cech12.usefulhats.item.ILivingDropsListener;
import de.cech12.usefulhats.item.ILivingJumpListener;
import de.cech12.usefulhats.item.IMobEntityChanger;
import de.cech12.usefulhats.item.IRightClickListener;
import de.cech12.usefulhats.platform.Services;
Expand Down Expand Up @@ -55,6 +56,12 @@ public static void onLivingDiesBecauseOf(Entity entity) {
}
}

public static void onLivingJump(LivingEntity entity) {
Services.REGISTRY.getEquippedHatItemStacks(entity).stream()
.filter(stack -> stack.getItem() instanceof ILivingJumpListener)
.forEach(stack -> ((ILivingJumpListener) stack.getItem()).onLivingJumpEvent(entity, stack));
}

public static int onLivingStartsUsingItem(LivingEntity entity, ItemStack usedStack, int actualDuration) {
final int[] newDuration = {actualDuration};
if (entity instanceof Player player) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,20 @@ public int getColor(ItemStack stack) {
* Copy of {@link ItemStack#hurtAndBreak(int, LivingEntity, Consumer)} to enable own damaging of hat items.
* Added config value to disable damage.
*/
protected void damageHatItemByOne(ItemStack stack, Player entity) {
protected void damageHatItemByOne(ItemStack stack, LivingEntity entity) {
if (!this.enabledDamageConfig.get()) return;

if (!entity.level().isClientSide && !entity.getAbilities().instabuild) {
if (this.canBeDepleted()) {
if (stack.hurt(1, entity.getRandom(), entity instanceof ServerPlayer ? (ServerPlayer)entity : null)) {
entity.broadcastBreakEvent(EquipmentSlot.HEAD);
stack.shrink(1);
entity.awardStat(Stats.ITEM_BROKEN.get(this));
stack.setDamageValue(0);
if (!entity.level().isClientSide
&& !(entity instanceof ServerPlayer player && player.getAbilities().instabuild)
&& this.canBeDepleted()
) {
if (stack.hurt(1, entity.getRandom(), entity instanceof ServerPlayer ? (ServerPlayer)entity : null)) {
entity.broadcastBreakEvent(EquipmentSlot.HEAD);
stack.shrink(1);
if (entity instanceof ServerPlayer player) {
player.awardStat(Stats.ITEM_BROKEN.get(this));
}
stack.setDamageValue(0);
}
}
}
Expand Down
21 changes: 11 additions & 10 deletions common/src/main/java/de/cech12/usefulhats/item/BunnyEarsItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import javax.annotation.Nullable;
import java.util.List;

public class BunnyEarsItem extends AbstractHatItem implements IEquipmentChangeListener, IItemUseListener, IUsefulHatModelOwner {
public class BunnyEarsItem extends AbstractHatItem implements IEquipmentChangeListener, IItemUseListener, ILivingJumpListener, IUsefulHatModelOwner {

private static final int JUMP_BOOST_DURATION = 219;

Expand All @@ -36,16 +36,12 @@ public void appendHoverText(@Nonnull ItemStack stack, @Nullable Level worldIn, @

@Override
public void inventoryTick(@Nonnull ItemStack stack, Level level, @Nonnull Entity entity, int slot, boolean selectedIndex) {
if (!level.isClientSide && entity instanceof Player player) {
if (!Services.REGISTRY.getEquippedHatItemStacks(player).contains(stack)) return; //only one worn stack of this item should add its effect
if (!level.isClientSide && entity instanceof LivingEntity livingEntity) {
if (!Services.REGISTRY.getEquippedHatItemStacks(livingEntity).contains(stack)) return; //only one worn stack of this item should add its effect
int amplifier = Services.PLATFORM.getEnchantmentLevel(stack, Enchantments.BLOCK_EFFICIENCY);
if (!this.isEffectCausedByOtherSource(player, MobEffects.JUMP, JUMP_BOOST_DURATION, amplifier)) {
if (player.getEffect(MobEffects.JUMP) == null || player.tickCount % 19 == 0) {
this.addEffect(player, MobEffects.JUMP, JUMP_BOOST_DURATION, amplifier);
}
//calculate damage if effect is caused by this item
if (player.tickCount % 20 == 0) {
this.damageHatItemByOne(stack, player); //TODO only when jumping
if (!this.isEffectCausedByOtherSource(livingEntity, MobEffects.JUMP, JUMP_BOOST_DURATION, amplifier)) {
if (livingEntity.getEffect(MobEffects.JUMP) == null || livingEntity.tickCount % 19 == 0) {
this.addEffect(livingEntity, MobEffects.JUMP, JUMP_BOOST_DURATION, amplifier);
}
}
}
Expand All @@ -67,4 +63,9 @@ public void onUnequippedHatItem(LivingEntity entity, ItemStack oldStack) {
int amplifier = Services.PLATFORM.getEnchantmentLevel(oldStack, Enchantments.BLOCK_EFFICIENCY);
this.removeEffect(entity, MobEffects.JUMP, JUMP_BOOST_DURATION, amplifier);
}

@Override
public void onLivingJumpEvent(LivingEntity jumpingEntity, ItemStack headSlotItemStack) {
this.damageHatItemByOne(headSlotItemStack, jumpingEntity);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package de.cech12.usefulhats.item;

import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.ItemStack;

public interface ILivingJumpListener {

void onLivingJumpEvent(LivingEntity jumpingEntity, ItemStack headSlotItemStack);

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public interface IConfigHelper {
boolean BUNNY_EARS_DAMAGE_ENABLED_DEFAULT = true;

String BUNNY_EARS_DURABILITY_DESCRIPTION = "Bunny Ears durability.";
int BUNNY_EARS_DURABILITY_DEFAULT = 600;
int BUNNY_EARS_DURABILITY_DEFAULT = 450;
int BUNNY_EARS_DURABILITY_MIN = 1;
int BUNNY_EARS_DURABILITY_MAX = 10000;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@

"text.autoconfig.usefulhats.option.BUNNY_EARS.DURABILITY": "Durability",
"text.autoconfig.usefulhats.option.BUNNY_EARS.DURABILITY.@Tooltip[0]": "§6§lDurability",
"text.autoconfig.usefulhats.option.BUNNY_EARS.DURABILITY.@Tooltip[1]": "§7§oDefault: 600",
"text.autoconfig.usefulhats.option.BUNNY_EARS.DURABILITY.@Tooltip[1]": "§7§oDefault: 450",

"text.autoconfig.usefulhats.option.CHOPPING_HAT": "Chopping Hat",
"text.autoconfig.usefulhats.option.CHOPPING_HAT.@Tooltip[0]": "§6§lChopping Hat",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,9 @@ public void equipmentHasChangedProxy(ItemStack fromItemStack, ItemStack toItemSt
}
}

@Inject(method = "jumpFromGround", at = @At("RETURN"))
public void jumpFromGroundProxy(CallbackInfo ci) {
UsefulHatsEventUtils.onLivingJump((LivingEntity) (Object) this);
}

}
8 changes: 8 additions & 0 deletions forge/src/main/java/de/cech12/usefulhats/init/ModItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import net.minecraftforge.event.entity.living.LivingDropsEvent;
import net.minecraftforge.event.entity.living.LivingEntityUseItemEvent;
import net.minecraftforge.event.entity.living.LivingEquipmentChangeEvent;
import net.minecraftforge.event.entity.living.LivingEvent;
import net.minecraftforge.event.entity.player.ItemFishedEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
Expand Down Expand Up @@ -50,6 +51,7 @@ public static void addEventListeners() {
MinecraftForge.EVENT_BUS.addListener(ModItems::onEntityJoinWorldEvent);
MinecraftForge.EVENT_BUS.addListener(ModItems::onItemFishedEvent);
MinecraftForge.EVENT_BUS.addListener(ModItems::onLivingDropsEvent);
MinecraftForge.EVENT_BUS.addListener(ModItems::onLivingJumpEvent);
MinecraftForge.EVENT_BUS.addListener(ModItems::onLivingEquipmentChangeEvent);
MinecraftForge.EVENT_BUS.addListener(ModItems::onLivingChangeTargetEvent);
MinecraftForge.EVENT_BUS.addListener(ModItems::onLivingUseItemEventStart);
Expand Down Expand Up @@ -90,6 +92,12 @@ private static void onLivingDropsEvent(LivingDropsEvent event) {
}
}

private static void onLivingJumpEvent(LivingEvent.LivingJumpEvent event) {
if (!event.isCanceled()) {
UsefulHatsEventUtils.onLivingJump(event.getEntity());
}
}

private static void onLivingUseItemEventStart(LivingEntityUseItemEvent event) {
if (!event.isCanceled() && event instanceof LivingEntityUseItemEvent.Start) {
event.setDuration(UsefulHatsEventUtils.onLivingStartsUsingItem(event.getEntity(), event.getItem(), event.getDuration()));
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# Project
group=de.cech12.usefulhats
mod_version=5.1.1.1
mod_version=5.1.2.0
mod_id=usefulhats
mod_name=Useful Hats
mod_author=Cech12
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import net.neoforged.neoforge.event.entity.living.LivingDropsEvent;
import net.neoforged.neoforge.event.entity.living.LivingEntityUseItemEvent;
import net.neoforged.neoforge.event.entity.living.LivingEquipmentChangeEvent;
import net.neoforged.neoforge.event.entity.living.LivingEvent;
import net.neoforged.neoforge.event.entity.player.ItemFishedEvent;
import net.neoforged.neoforge.event.entity.player.PlayerEvent;
import net.neoforged.neoforge.event.entity.player.PlayerInteractEvent;
Expand Down Expand Up @@ -50,6 +51,7 @@ public static void addEventListeners() {
NeoForge.EVENT_BUS.addListener(ModItems::onEntityJoinWorldEvent);
NeoForge.EVENT_BUS.addListener(ModItems::onItemFishedEvent);
NeoForge.EVENT_BUS.addListener(ModItems::onLivingDropsEvent);
NeoForge.EVENT_BUS.addListener(ModItems::onLivingJumpEvent);
NeoForge.EVENT_BUS.addListener(ModItems::onLivingEquipmentChangeEvent);
NeoForge.EVENT_BUS.addListener(ModItems::onLivingChangeTargetEvent);
NeoForge.EVENT_BUS.addListener(ModItems::onLivingUseItemEventStart);
Expand Down Expand Up @@ -90,6 +92,10 @@ private static void onLivingDropsEvent(LivingDropsEvent event) {
}
}

private static void onLivingJumpEvent(LivingEvent.LivingJumpEvent event) {
UsefulHatsEventUtils.onLivingJump(event.getEntity());
}

private static void onLivingUseItemEventStart(LivingEntityUseItemEvent event) {
if (event instanceof LivingEntityUseItemEvent.Start) {
event.setDuration(UsefulHatsEventUtils.onLivingStartsUsingItem(event.getEntity(), event.getItem(), event.getDuration()));
Expand Down

0 comments on commit 49eb6b2

Please sign in to comment.