Now adds unravled fabric to your inventory and destroys armor durabilty

This commit is contained in:
MalekiRe 2021-06-09 23:43:57 -07:00
parent 198b214d56
commit 2638d1ba5b
2 changed files with 54 additions and 3 deletions

View file

@ -1,8 +1,11 @@
package org.dimdev.dimdoors.mixin;
import net.minecraft.item.ItemStack;
import org.dimdev.dimdoors.DimensionalDoorsInitializer;
import org.dimdev.dimdoors.item.ModItems;
import org.dimdev.dimdoors.mixin.accessor.EntityAccessor;
import org.dimdev.dimdoors.world.ModDimensions;
import org.dimdev.dimdoors.world.level.component.PlayerModifiersComponent;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
@ -18,8 +21,15 @@ import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.Identifier;
import net.minecraft.world.World;
import java.util.Random;
@Mixin(value = PlayerEntity.class, priority = 900)
public abstract class PlayerEntityMixin extends LivingEntity {
private static final int RANDOM_ACTION_BOUND = 200;
private static final int CHANCE_TO_DECREASE_ARMOR_DURABILITY = 10;
private static final int CHANCE_TO_REPLACE_ITEMSLOT_WITH_UNRAVLED_FABRIC = 30;
Random random = new Random();
@Shadow
public abstract void incrementStat(Identifier stat);
@ -27,9 +37,49 @@ public abstract class PlayerEntityMixin extends LivingEntity {
super(entityType, world);
}
@Inject(method = "tick", at = @At("HEAD"), cancellable = true)
public void mobTickMixin(CallbackInfo ci) {
if (PlayerModifiersComponent.getFray(this) >= 125) {
if (random.nextInt(RANDOM_ACTION_BOUND) == 0) {
doRandomFunction(this);
}
}
}
private void doRandomFunction(LivingEntity player) {
switch (random.nextInt(2)) {
case 0:
decreaseArmorDurability((PlayerEntity) player);
break;
case 1:
addRandomUnravledFabric((PlayerEntity) player);
break;
default:
}
}
private void addRandomUnravledFabric(PlayerEntity player) {
if(random.nextInt(CHANCE_TO_REPLACE_ITEMSLOT_WITH_UNRAVLED_FABRIC) == 0) {
int slot = random.nextInt(player.getInventory().main.size());
if(player.getInventory().main.get(slot).isEmpty() || player.getInventory().main.get(slot).getItem() == ModItems.UNRAVELLED_FABRIC) {
if(player.getInventory().main.get(slot).getCount() < 64)
player.getInventory().main.set(slot, new ItemStack(ModItems.UNRAVELLED_FABRIC, 1+player.getInventory().main.get(slot).getCount()));
}
}
}
private void decreaseArmorDurability(PlayerEntity player) {
for (int i = 0; i < player.getInventory().armor.size(); i++)
if (random.nextInt(CHANCE_TO_DECREASE_ARMOR_DURABILITY) == 0)
player.getArmorItems().forEach((itemStack) -> {
itemStack.setDamage(itemStack.getDamage() + 1);
});
}
@Inject(method = "handleFallDamage", at = @At("HEAD"), cancellable = true)
public void handleLimboFallDamage(float fallDistance, float damageMultiplier, DamageSource damageSource, CallbackInfoReturnable<Boolean> cir) {
if(this.world.getDimension().equals(ModDimensions.LIMBO_DIMENSION.getDimension())) {
if (this.world.getDimension().equals(ModDimensions.LIMBO_DIMENSION.getDimension())) {
cir.setReturnValue(false);
}
}

View file

@ -2,6 +2,7 @@ package org.dimdev.dimdoors.world.level.component;
import dev.onyxstudios.cca.api.v3.component.ComponentV3;
import dev.onyxstudios.cca.api.v3.component.sync.AutoSyncedComponent;
import net.minecraft.entity.LivingEntity;
import org.dimdev.dimdoors.DimensionalDoorsComponents;
import org.dimdev.dimdoors.DimensionalDoorsInitializer;
@ -58,8 +59,8 @@ public class PlayerModifiersComponent implements ComponentV3, AutoSyncedComponen
public static int getFray(PlayerEntity player) {
return get(player).getFray();
public static int getFray(LivingEntity player) {
return get((PlayerEntity) player).getFray();
}
public static void sync(PlayerEntity player) {