Skip to content

Instantly share code, notes, and snippets.

@Lanse505
Last active July 12, 2021 17:02
Show Gist options
  • Save Lanse505/9b3ecc1b27e18f94cfcf342a1bc4c9a0 to your computer and use it in GitHub Desktop.
Save Lanse505/9b3ecc1b27e18f94cfcf342a1bc4c9a0 to your computer and use it in GitHub Desktop.
package com.teamacronymcoders.epos.mixin;
import com.teamacronymcoders.epos.api.event.EposUnbreakingEvent;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.MinecraftForge;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyVariable;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;
import java.lang.ref.WeakReference;
import java.util.Random;
@Mixin(ItemStack.class)
public abstract class ItemStackMixin {
@Shadow public abstract ItemStack copy();
@Unique
private WeakReference<ServerPlayerEntity> playerRef = new WeakReference<>(null);
@Inject(method = "hurt(ILjava/util/Random;Lnet/minecraft/entity/player/ServerPlayerEntity;)Z",
locals = LocalCapture.CAPTURE_FAILEXCEPTION,
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/enchantment/EnchantmentHelper;getItemEnchantmentLevel(Lnet/minecraft/enchantment/Enchantment;Lnet/minecraft/item/ItemStack;)I"
)
)
private void modifiedHurt(int damage, Random random, ServerPlayerEntity playerEntity, CallbackInfoReturnable<Boolean> cir) {
playerRef = new WeakReference<>(playerEntity);
}
@ModifyVariable(
method = "hurt(ILjava/util/Random;Lnet/minecraft/entity/player/ServerPlayerEntity;)Z",
ordinal = 1,
at = @At(
value = "INVOKE_ASSIGN",
target = "Lnet/minecraft/enchantment/EnchantmentHelper;getItemEnchantmentLevel(Lnet/minecraft/enchantment/Enchantment;Lnet/minecraft/item/ItemStack;)I",
shift = At.Shift.AFTER
)
)
private int modifiedHurt(int level) {
int modifiedLevel = level;
ServerPlayerEntity playerEntity = playerRef.get();
if (playerEntity != null) {
modifiedLevel += this.getUnbreakingModifier(level);
return modifiedLevel;
}
return modifiedLevel;
}
@Unique
private int getUnbreakingModifier(int originalLevel) {
ServerPlayerEntity playerEntity = playerRef.get();
ItemStack stack = copy();
if (playerEntity != null && (stack != null || stack.isEmpty())) {
EposUnbreakingEvent event = new EposUnbreakingEvent(originalLevel, playerEntity, stack);
MinecraftForge.EVENT_BUS.post(event);
return event.getModifiedLevel();
}
return originalLevel;
}
}
package com.teamacronymcoders.epos.impl.feat.generic;
import com.hrznstudio.titanium.event.handler.EventManager;
import com.teamacronymcoders.epos.api.character.ICharacterSheet;
import com.teamacronymcoders.epos.api.event.EposUnbreakingEvent;
import com.teamacronymcoders.epos.impl.feat.EposFeatIds;
import com.teamacronymcoders.epos.util.EposCharacterUtil;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.common.ToolType;
import java.util.Arrays;
import java.util.Set;
public class Specialized {
public static final EventManager.ISubscribe featManager = EventManager.create(EposUnbreakingEvent.class, EventManager.Bus.FORGE)
.filter(event -> {
ServerPlayerEntity playerEntity = event.getServerPlayer();
Set<ToolType> toolTypes = event.getStack().getToolTypes();
boolean hasMatchingSpecialization = false;
for (ToolType type : toolTypes) {
String name = type.getName();
if (name.equals("axe")) {
hasMatchingSpecialization = EposCharacterUtil.hasFeats(playerEntity, EposFeatIds.AXE_SPECIALIZATION_NOVICE, EposFeatIds.AXE_SPECIALIZATION_INTERMEDIATE, EposFeatIds.AXE_SPECIALIZATION_ADVANCED);
} else if (name.equals("hoe")) {
hasMatchingSpecialization = EposCharacterUtil.hasFeats(playerEntity, EposFeatIds.HOE_SPECIALIZATION_NOVICE, EposFeatIds.HOE_SPECIALIZATION_INTERMEDIATE, EposFeatIds.HOE_SPECIALIZATION_ADVANCED);
} else if (name.equals("pickaxe")) {
hasMatchingSpecialization = EposCharacterUtil.hasFeats(playerEntity, EposFeatIds.PICKAXE_SPECIALIZATION_NOVICE, EposFeatIds.PICKAXE_SPECIALIZATION_INTERMEDIATE, EposFeatIds.PICKAXE_SPECIALIZATION_ADVANCED);
} else if (name.equals("shovel")) {
hasMatchingSpecialization = EposCharacterUtil.hasFeats(playerEntity, EposFeatIds.SHOVEL_SPECIALIZATION_NOVICE, EposFeatIds.SHOVEL_SPECIALIZATION_INTERMEDIATE, EposFeatIds.SHOVEL_SPECIALIZATION_ADVANCED);
}
}
return playerEntity != null && hasMatchingSpecialization;
})
.process(event -> {
ServerPlayerEntity playerEntity = event.getServerPlayer();
Set<ToolType> toolTypes = event.getStack().getToolTypes();
int unbreakingModifier = 0;
for (ToolType type : toolTypes) {
String name = type.getName();
if (name.equals("axe")) {
unbreakingModifier += getModifierAmount(playerEntity, EposFeatIds.AXE_SPECIALIZATION_NOVICE, EposFeatIds.AXE_SPECIALIZATION_INTERMEDIATE, EposFeatIds.AXE_SPECIALIZATION_ADVANCED);
} else if (name.equals("hoe")) {
unbreakingModifier += getModifierAmount(playerEntity, EposFeatIds.HOE_SPECIALIZATION_NOVICE, EposFeatIds.HOE_SPECIALIZATION_INTERMEDIATE, EposFeatIds.HOE_SPECIALIZATION_ADVANCED);
} else if (name.equals("pickaxe")) {
unbreakingModifier += getModifierAmount(playerEntity, EposFeatIds.PICKAXE_SPECIALIZATION_NOVICE, EposFeatIds.PICKAXE_SPECIALIZATION_INTERMEDIATE, EposFeatIds.PICKAXE_SPECIALIZATION_ADVANCED);
} else if (name.equals("shovel")) {
unbreakingModifier += getModifierAmount(playerEntity, EposFeatIds.SHOVEL_SPECIALIZATION_NOVICE, EposFeatIds.SHOVEL_SPECIALIZATION_INTERMEDIATE, EposFeatIds.SHOVEL_SPECIALIZATION_ADVANCED);
}
}
int original = event.getOriginalLevel();
event.setModifiedLevel(original + unbreakingModifier);
});
private static int getModifierAmount(ServerPlayerEntity character, ResourceLocation... featIds) {
ICharacterSheet sheet = EposCharacterUtil.getCharacterSheet(character);
if (sheet != null) {
int unbreakingAmount = 0;
unbreakingAmount += Arrays.stream(featIds).filter(id -> sheet.getFeats().getOrCreate(id).isUnlocked()).count();
return unbreakingAmount;
}
return 0;
}
}
package com.teamacronymcoders.epos.impl.feat.monk;
import com.hrznstudio.titanium.event.handler.EventManager;
import com.teamacronymcoders.epos.impl.feat.EposFeatIds;
import com.teamacronymcoders.epos.util.EposCharacterUtil;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.nbt.CompoundNBT;
import net.minecraftforge.event.TickEvent;
public class EmbraceOfTheLotus {
private static final String REMAINING_TIME = "eotl_remaining";
public static final EventManager.ISubscribe featManager = EventManager.create(TickEvent.PlayerTickEvent.class, EventManager.Bus.FORGE)
.filter(event -> {
if (event.player != null) return EposCharacterUtil.hasFeat(event.player, EposFeatIds.EMBRACE_OF_THE_LOTUS);
return false;
})
.process(event -> {
PlayerEntity player = event.player;
CompoundNBT persistent = player.getPersistentData();
float absorptionAmount = player.getAbsorptionAmount();
if (persistent.contains(REMAINING_TIME)) {
int remainingTime = persistent.getInt(REMAINING_TIME);
if (remainingTime <= 0 && absorptionAmount < 5f) {
player.setAbsorptionAmount(player.getAbsorptionAmount() + 0.5f);
persistent.putInt(REMAINING_TIME, 20);
} else {
if (absorptionAmount < 5f) {
remainingTime--;
persistent.putInt(REMAINING_TIME, remainingTime);
}
}
} else {
if (absorptionAmount < 5f) {
player.setAbsorptionAmount(player.getAbsorptionAmount() + 0.5f);
}
persistent.putInt(REMAINING_TIME, 20);
}
});
}
package com.teamacronymcoders.epos.impl.feat.ranger;
import com.hrznstudio.titanium.event.handler.EventManager;
import com.teamacronymcoders.epos.impl.feat.EposFeatIds;
import com.teamacronymcoders.epos.util.EposCharacterUtil;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.passive.WolfEntity;
import net.minecraft.nbt.CompoundNBT;
import net.minecraftforge.event.entity.living.LivingEvent;
public class MansBestFriend {
private static final String REMAINING = "mbf_remaining";
public static final EventManager.ISubscribe featManager = EventManager.create(LivingEvent.LivingUpdateEvent.class, EventManager.Bus.FORGE)
.filter(event -> {
if (event.getEntityLiving() != null && event.getEntityLiving() instanceof WolfEntity) {
WolfEntity wolf = (WolfEntity) event.getEntityLiving();
if (wolf.isTame()) {
LivingEntity owner = wolf.getOwner();
return owner != null && EposCharacterUtil.hasFeat(owner, EposFeatIds.MANS_BEST_FRIEND);
}
}
return false;
})
.process(event -> {
WolfEntity wolf = (WolfEntity) event.getEntityLiving();
LivingEntity owner = wolf.getOwner();
CompoundNBT persistent = wolf.getPersistentData();
boolean isCloseToOwner = wolf.blockPosition().closerThan(owner.blockPosition(), 10d);
boolean isMissingHealth = wolf.getHealth() < wolf.getMaxHealth();
if (persistent.contains(REMAINING)) {
int remainingTime = persistent.getInt(REMAINING);
if (remainingTime <= 0 && isMissingHealth) {
if (isCloseToOwner) {
wolf.heal(0.5f);
}
} else {
if (isMissingHealth) {
remainingTime--;
persistent.putInt(REMAINING, remainingTime);
}
}
} else {
if (isCloseToOwner && isMissingHealth) {
wolf.heal(0.5f);
}
persistent.putInt(REMAINING, 10);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment