Skip to content

Commit

Permalink
Continued to fix #203, Completed EnchantmentRestriction, Updated Loader
Browse files Browse the repository at this point in the history
 - Mechanised Sawblade now mines wood
 - System for prohibiting and permitting enchants in place
 - Updated Fabric Loader to 0.14.19
 - Updated Fabric Version to 0.76.0+1.19.2
  • Loading branch information
chronosacaria committed May 8, 2023
1 parent ad91a1f commit 4e86ee7
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 156 deletions.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ org.gradle.jvmargs=-Xmx1G
# check these on https://fabricmc.net/develop/
minecraft_version=1.19.2
yarn_mappings=1.19.2+build.28
loader_version=0.14.14
loader_version=0.14.19

# Mod Properties
mod_version=5.0.8-1.19
Expand All @@ -14,7 +14,7 @@ org.gradle.jvmargs=-Xmx1G

# Dependencies
# check this on https://fabricmc.net/develop/
fabric_version=0.73.2+1.19.2
fabric_version=0.76.0+1.19.2
reach_entity_attributes_version=2.3.0
cloth_config_version=7.0.72
projectile_damage_version=3.1.0+1.19
1 change: 1 addition & 0 deletions src/main/java/chronosacaria/mcdw/Mcdw.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public void onInitialize() {
McdwEnchantGiverConfig.appendEnchants();
SummonedEntityRegistry.register();
StatusEffectsRegistry.init();
EnchantmentRestrictionsRegistry.init();
CompatRegistry.init();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package chronosacaria.mcdw.api.util;

import com.google.common.collect.ArrayListMultimap;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.item.ItemStack;

import java.util.ArrayList;
import java.util.HashMap;

/**
* This code is used with the permission of <a href = "https://github.com/ZsoltMolnarrr">Daedelus</a>. <br />
Expand All @@ -15,25 +15,57 @@ public final class EnchantmentRestriction {
public interface Condition {
boolean isAcceptableItem(ItemStack itemStack);
}
private static HashMap<Enchantment, ArrayList<Condition>> alleviations = new HashMap<>();

// If alleviating condition is met, the enchantment will be applicable ignoring additional checks
public static void alleviate(Enchantment enchantment, Condition condition) {
var conditions = alleviations.get(enchantment);
if (conditions == null) {
conditions = new ArrayList<>();
public interface TypeCondition {
boolean isAcceptableItem(Enchantment enchantment, ItemStack itemStack);
}
private static ArrayListMultimap<Enchantment, Condition> permissions = ArrayListMultimap.create();
private static ArrayList<TypeCondition> permissibleTargets = new ArrayList<>();
private static ArrayListMultimap<Enchantment, Condition> prohibitions = ArrayListMultimap.create();
private static ArrayList<TypeCondition> prohibitedTargets = new ArrayList<>();


public static void permit(Enchantment enchantment, Condition condition) {
permissions.put(enchantment, condition);
}

public static void permitTarget(TypeCondition typeCondition) {
permissibleTargets.add(typeCondition);
}

public static void prohibit(Enchantment enchantment, Condition condition) {
prohibitions.put(enchantment, condition);
}

public static void prohibitTarget(TypeCondition typeCondition) {
prohibitedTargets.add(typeCondition);
}

public static boolean isPermitted(Enchantment enchantment, ItemStack itemStack) {
var conditions = permissions.get(enchantment);
for(var condition: conditions) {
if (condition.isAcceptableItem(itemStack)) {
return true;
}
}
for(var condition: permissibleTargets) {
if (condition.isAcceptableItem(enchantment, itemStack)) {
return true;
}
}
conditions.add(condition);
alleviations.put(enchantment, conditions);
return false;
}

public static boolean isAlleviated(Enchantment enchantment, ItemStack itemStack) {
var conditions = alleviations.get(enchantment);
if (conditions != null) {
for(var condition: conditions) {
if (condition.isAcceptableItem(itemStack)) {
return true;
}
public static boolean isProhibited(Enchantment enchantment, ItemStack itemStack) {
var conditions = prohibitions.get(enchantment);
for(var condition: conditions) {
if (condition.isAcceptableItem(itemStack)) {
return true;
}
}
for(var condition: prohibitedTargets) {
if (condition.isAcceptableItem(enchantment, itemStack)) {
return true;
}
}
return false;
Expand Down
42 changes: 13 additions & 29 deletions src/main/java/chronosacaria/mcdw/bases/McdwSword.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import chronosacaria.mcdw.enums.SwordsID;
import chronosacaria.mcdw.registries.ItemsRegistry;
import com.google.common.collect.BiMap;
import com.google.common.collect.ImmutableMap;
import net.fabricmc.fabric.mixin.content.registry.AxeItemAccessor;
import net.minecraft.advancement.criterion.Criteria;
import net.minecraft.block.*;
import net.minecraft.client.item.TooltipContext;
Expand All @@ -16,6 +16,7 @@
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.tag.BlockTags;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Formatting;
Expand All @@ -25,12 +26,10 @@

import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;

@SuppressWarnings("rawtypes")
public class McdwSword extends SwordItem {
protected static final Map<Block, Block> STRIPPED_BLOCKS;
String[] repairIngredient;

public McdwSword(ToolMaterial material, int attackDamage, float attackSpeed, String[] repairIngredient) {
Expand Down Expand Up @@ -88,8 +87,18 @@ public ActionResult useOnBlock(ItemUsageContext context) {
return ActionResult.PASS;
}

@Override
public float getMiningSpeedMultiplier(ItemStack stack, BlockState state) {
if (stack.isOf(SwordsID.SWORD_MECHANIZED_SAWBLADE.getItem())) {
if (state.isIn(BlockTags.AXE_MINEABLE))
return 8.0F;
}
return super.getMiningSpeedMultiplier(stack, state);
}

@SuppressWarnings("UnstableApiUsage")
private Optional<BlockState> getStrippedState(BlockState state) {
return Optional.ofNullable(STRIPPED_BLOCKS.get(state.getBlock())).map((block) -> block.getDefaultState().with(PillarBlock.AXIS, state.get(PillarBlock.AXIS)));
return Optional.ofNullable(AxeItemAccessor.getStrippedBlocks().get(state.getBlock())).map((block) -> block.getDefaultState().with(PillarBlock.AXIS, state.get(PillarBlock.AXIS)));
}

@Override
Expand All @@ -110,29 +119,4 @@ public void appendTooltip(ItemStack stack, World world, List<Text> tooltip, Tool
tooltip.add(Text.translatable("tooltip_info_item.mcdw.diamond_sword_3").formatted(Formatting.ITALIC));
}
}

static {
//noinspection unchecked
STRIPPED_BLOCKS = (new ImmutableMap.Builder())
.put(Blocks.OAK_WOOD, Blocks.STRIPPED_OAK_WOOD)
.put(Blocks.OAK_LOG, Blocks.STRIPPED_OAK_LOG)
.put(Blocks.DARK_OAK_WOOD, Blocks.STRIPPED_DARK_OAK_WOOD)
.put(Blocks.DARK_OAK_LOG, Blocks.STRIPPED_DARK_OAK_LOG)
.put(Blocks.ACACIA_WOOD, Blocks.STRIPPED_ACACIA_WOOD)
.put(Blocks.ACACIA_LOG, Blocks.STRIPPED_ACACIA_LOG)
.put(Blocks.BIRCH_WOOD, Blocks.STRIPPED_BIRCH_WOOD)
.put(Blocks.BIRCH_LOG, Blocks.STRIPPED_BIRCH_LOG)
.put(Blocks.JUNGLE_WOOD, Blocks.STRIPPED_JUNGLE_WOOD)
.put(Blocks.JUNGLE_LOG, Blocks.STRIPPED_JUNGLE_LOG)
.put(Blocks.SPRUCE_WOOD, Blocks.STRIPPED_SPRUCE_WOOD)
.put(Blocks.SPRUCE_LOG, Blocks.STRIPPED_SPRUCE_LOG)
.put(Blocks.WARPED_STEM, Blocks.STRIPPED_WARPED_STEM)
.put(Blocks.WARPED_HYPHAE, Blocks.STRIPPED_WARPED_HYPHAE)
.put(Blocks.CRIMSON_STEM, Blocks.STRIPPED_CRIMSON_STEM)
.put(Blocks.CRIMSON_HYPHAE, Blocks.STRIPPED_CRIMSON_HYPHAE)
.put(Blocks.MANGROVE_WOOD, Blocks.STRIPPED_MANGROVE_WOOD)
.put(Blocks.MANGROVE_LOG, Blocks.STRIPPED_MANGROVE_LOG)
.build();
}

}
19 changes: 0 additions & 19 deletions src/main/java/chronosacaria/mcdw/mixin/DamageEnchantmentMixin.java

This file was deleted.

66 changes: 20 additions & 46 deletions src/main/java/chronosacaria/mcdw/mixin/EnchantmentHelperMixin.java
Original file line number Diff line number Diff line change
@@ -1,65 +1,39 @@
package chronosacaria.mcdw.mixin;

import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.enchantment.EnchantmentLevelEntry;
import net.minecraft.enchantment.EnchantmentTarget;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry;
import org.spongepowered.asm.mixin.Mixin;
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.callback.CallbackInfoReturnable;

import java.util.List;

/**
* This code is used with the permission of <a href = "https://github.com/ZsoltMolnarrr">Daedelus</a>. <br />
* The original code is from SpellEngine and can be found <a href = "https://github.com/ZsoltMolnarrr/SpellEngine/blob/1.19.2/common/src/main/java/net/spell_engine/mixin/enchant/EnchantmentHelperMixin.java">here</a>.
*/

@Mixin(EnchantmentHelper.class)
public class EnchantmentHelperMixin {

@Inject(method = "getPossibleEntries", at = @At(value = "RETURN"), cancellable = true)
private static void mcdw$permitInnateEnchantmentsToBeEnchantedButNotWithConflicts(int power, ItemStack stack, boolean treasureAllowed,
CallbackInfoReturnable<List<EnchantmentLevelEntry>> cir) {
List<EnchantmentLevelEntry> list = cir.getReturnValue();
for (NbtElement enchantmentNbt : stack.getEnchantments()) {
NbtCompound nbtCompound = (NbtCompound) enchantmentNbt;
Identifier identifier = EnchantmentHelper.getIdFromNbt(nbtCompound);
Enchantment enchantmentOnStack = Registry.ENCHANTMENT.get(identifier);
// If can find enchants, remove incompatible options from the list of randomly enchant-able enchantments
if (enchantmentOnStack != null)
list.removeIf(enchantmentLevelEntry -> !enchantmentLevelEntry.enchantment.canCombine(enchantmentOnStack));
}
cir.setReturnValue(list);
@Unique
private static Enchantment mixedInEnchantment;

// Add extra entries from those which were rejected by Vanilla logic
@WrapOperation(method = "getPossibleEntries", at = @At(value = "FIELD", target = "Lnet/minecraft/enchantment/Enchantment;type:Lnet/minecraft/enchantment/EnchantmentTarget;"))
private static EnchantmentTarget mcdw$getEnchantmentContext(Enchantment enchantment, Operation<EnchantmentTarget> type) {
mixedInEnchantment = enchantment;
return type.call();
}

// Or should this be used? \/\/
var currentEntries = cir.getReturnValue();
@WrapOperation(method = "getPossibleEntries", at = @At(value = "INVOKE", target = "Lnet/minecraft/enchantment/EnchantmentTarget;isAcceptableItem(Lnet/minecraft/item/Item;)Z"))
private static boolean mcdw$getPossibleEntries(EnchantmentTarget enchantmentTarget, Item item, Operation<Boolean> returnValue, int power, ItemStack stack, boolean treasureAllowed) {

// This logic is mostly copied from EnchantmentHelper.getPossibleEntries
Item item = stack.getItem();
boolean isBook = stack.isOf(Items.BOOK);
block0: for (Enchantment enchantment : Registry.ENCHANTMENT) {
// Don't check things already added
boolean alreadyAdded = currentEntries.stream().anyMatch(entry -> entry.enchantment.equals(enchantment));
if (alreadyAdded) { continue; }
return (!(mixedInEnchantment.isTreasure()
&& !treasureAllowed
|| !mixedInEnchantment.isAvailableForRandomSelection()
|| !mixedInEnchantment.isAcceptableItem(stack) // Custom logic, replacing `!enchantment.type.isAcceptableItem(item)`
&& !isBook));

if (enchantment.isTreasure()
&& !treasureAllowed
|| !enchantment.isAvailableForRandomSelection()
|| !enchantment.isAcceptableItem(stack) // Custom logic, replacing `!enchantment.type.isAcceptableItem(item)`
&& !isBook) continue;
for (int i = enchantment.getMaxLevel(); i > enchantment.getMinLevel() - 1; --i) {
if (power < enchantment.getMinPower(i) || power > enchantment.getMaxPower(i)) continue;
currentEntries.add(new EnchantmentLevelEntry(enchantment, i));
continue block0;
}
}
}
}
39 changes: 11 additions & 28 deletions src/main/java/chronosacaria/mcdw/mixin/EnchantmentMixin.java
Original file line number Diff line number Diff line change
@@ -1,47 +1,30 @@
package chronosacaria.mcdw.mixin;

import chronosacaria.mcdw.bases.McdwAxe;
import chronosacaria.mcdw.bases.McdwCustomWeaponBase;
import chronosacaria.mcdw.bases.McdwDoubleAxe;
import chronosacaria.mcdw.enums.DaggersID;
import chronosacaria.mcdw.registries.EnchantsRegistry;
import chronosacaria.mcdw.api.util.EnchantmentRestriction;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.EnchantmentTarget;
import net.minecraft.enchantment.Enchantments;
import net.minecraft.item.ItemStack;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.Arrays;

@Mixin(Enchantment.class)
public abstract class EnchantmentMixin {
public class EnchantmentMixin {

@Shadow @Final public EnchantmentTarget type;
@Inject(method = "isAcceptableItem", at = @At("HEAD"), cancellable = true)
public void mcdw$isAcceptableItem(ItemStack stack, CallbackInfoReturnable<Boolean> cir) {
var enchantment = (Enchantment)((Object)this);

@Inject(method = "isAcceptableItem", at = @At("RETURN"), cancellable = true)

private void mcdw$isAcceptablePlz(ItemStack stack, CallbackInfoReturnable<Boolean> cir){
if ((stack.getItem() instanceof McdwAxe || stack.getItem() instanceof McdwDoubleAxe)
&& mcdw$isEnchantment(Enchantments.FIRE_ASPECT)) {
cir.setReturnValue(true);
}
if (stack.getItem() instanceof McdwCustomWeaponBase
&& this.type.equals(EnchantmentTarget.WEAPON)){
cir.setReturnValue(true);
if (EnchantmentRestriction.isProhibited(enchantment, stack)) {
cir.setReturnValue(false);
cir.cancel();
}
if (stack.isOf(DaggersID.DAGGER_SWIFT_STRIKER.getItem())
&& mcdw$isEnchantment(EnchantsRegistry.ECHO, EnchantsRegistry.AMBUSH)){

if (EnchantmentRestriction.isPermitted(enchantment, stack)) {
cir.setReturnValue(true);
cir.cancel();
}
}

private boolean mcdw$isEnchantment(Enchantment ...enchantments){
return Arrays.stream(enchantments).toList().contains((Enchantment) (Object) this);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package chronosacaria.mcdw.registries;

import chronosacaria.mcdw.api.util.EnchantmentRestriction;
import chronosacaria.mcdw.bases.McdwAxe;
import chronosacaria.mcdw.bases.McdwDoubleAxe;
import chronosacaria.mcdw.bases.McdwSpear;
import net.minecraft.enchantment.DamageEnchantment;
import net.minecraft.enchantment.Enchantments;

public class EnchantmentRestrictionsRegistry {
public static void init() {
EnchantmentRestriction.permit(Enchantments.FIRE_ASPECT, itemStack -> itemStack.getItem() instanceof McdwAxe || itemStack.getItem() instanceof McdwDoubleAxe);
EnchantmentRestriction.permitTarget((enchantment, itemStack) -> enchantment instanceof DamageEnchantment && itemStack.getItem() instanceof McdwSpear);
}
}
1 change: 0 additions & 1 deletion src/main/resources/mcdw.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"BowItemMixin",
"CreeperEntityAccessor",
"CrossbowItemMixin",
"DamageEnchantmentMixin",
"EnchantmentHelperMixin",
"EnchantmentMixin",
"ExperienceOrbEntityMixin",
Expand Down

0 comments on commit 4e86ee7

Please sign in to comment.