Skip to content

Commit

Permalink
repair all the coral items in the inventory(its a little bit expensive)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan-Khar committed Nov 16, 2024
1 parent e7cb601 commit 5858b39
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions src/main/java/dev/hybridlabs/aquatic/mixin/PlayerEntityMixin.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.hybridlabs.aquatic.mixin;

import com.google.common.collect.ImmutableList;
import dev.hybridlabs.aquatic.access.CustomPlayerEntityData;
import dev.hybridlabs.aquatic.effect.HybridAquaticStatusEffects;
import dev.hybridlabs.aquatic.entity.shark.HybridAquaticSharkEntity;
Expand All @@ -17,6 +18,7 @@
import net.minecraft.item.ToolItem;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.registry.tag.FluidTags;
import net.minecraft.util.collection.DefaultedList;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
Expand All @@ -25,6 +27,9 @@
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.ArrayList;
import java.util.List;

@Mixin(PlayerEntity.class)
public abstract class PlayerEntityMixin implements CustomPlayerEntityData {

Expand Down Expand Up @@ -97,7 +102,7 @@ private void tickDownCustomHurtTime(CallbackInfo ci) {
// Gives Resistance and Slowness if player has Turtle chestplate equipped
updateTurtleChestplate();
// Repairs coral tools in the water
repairCoralTools(10);
repairCoralTools(5);
}

@Unique
Expand Down Expand Up @@ -141,16 +146,27 @@ private void updateTurtleChestplate() {
@Unique
private void repairCoralTools(int tickDelay) {
var player = (PlayerEntity)(Object)this;
var inventory = player.getInventory();

if (player.isSubmergedIn(FluidTags.WATER)) {
if (coralRepairTick > tickDelay) {
player.getItemsEquipped().forEach(itemStack -> {
if (itemStack.getItem() instanceof ToolItem tool &&
tool.getMaterial() == HybridAquaticToolMaterials.CORAL &&
itemStack.isDamaged()) {
itemStack.setDamage(itemStack.getDamage() - 1);
}
});
List<DefaultedList<ItemStack>> combinedInventory = ImmutableList.of(inventory.main, inventory.offHand);
List<ItemStack> coralItems = new ArrayList<>();
for(List<ItemStack> list: combinedInventory) {
for (ItemStack itemStack: list) {
if (itemStack.getItem() instanceof ToolItem tool &&
tool.getMaterial() == HybridAquaticToolMaterials.CORAL &&
itemStack.isDamaged()) {
coralItems.add(itemStack);
}
}
}

if(!coralItems.isEmpty()) {
ItemStack item = coralItems.get(player.getRandom().nextInt(coralItems.size()));
item.setDamage(item.getDamage() - 1);
inventory.markDirty();
}
coralRepairTick = 0;
}

Expand Down

0 comments on commit 5858b39

Please sign in to comment.