Skip to content

Commit

Permalink
Merged 7.0.1 release
Browse files Browse the repository at this point in the history
  • Loading branch information
Majrusz authored Dec 4, 2023
2 parents b7ebfee + 9bfd89c commit c6a18df
Show file tree
Hide file tree
Showing 15 changed files with 188 additions and 65 deletions.
9 changes: 6 additions & 3 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
- changed mod id from `mlib` to `majruszlibrary` (suggested by @ChiefArug)
- optimized and reworked multiple features
- fixed game crash `java.lang.NullPointerException: Registry Object not present`
- fixed game crash `java.lang.NullPointerException: Registry Object not present` (reported by @reboundrefice)
- fixed bug with trying to load folders instead of mods (reported by @SettingDust)
- fixed bug with weather changes not working properly
- fixed compatibility issue with Sodium (reported by @LonelyFear)
- fixed compatibility issue with Epic Fight (reported by @Orphion_)
- fixed compatibility issue with Marium's Soulslike Weaponry (reported by @mariumbacchus)
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import java.util.function.Consumer;

@Deprecated( since = "7.0.1 use OnItemRenderColorsGet" )
public class OnItemRenderColorGet {
public final ItemStack itemStack;
public final int layerIdx;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.majruszlibrary.events;

import com.majruszlibrary.annotation.Dist;
import com.majruszlibrary.annotation.OnlyIn;
import com.majruszlibrary.events.base.Event;
import com.majruszlibrary.events.base.Events;
import net.minecraft.world.item.ItemStack;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;

public class OnItemRenderColorsGet {
public final ItemStack itemStack;
private final Map< Integer, Integer > colors = new HashMap<>();

public static Event< OnItemRenderColorsGet > listen( Consumer< OnItemRenderColorsGet > consumer ) {
return Events.get( OnItemRenderColorsGet.class ).add( consumer );
}

public OnItemRenderColorsGet( ItemStack itemStack ) {
this.itemStack = itemStack;
}

public void add( int layerIdx, int color ) {
this.colors.put( layerIdx, color );
}

public boolean hasColorsDefined() {
return this.colors.size() > 0;
}

@OnlyIn( Dist.CLIENT )
public ItemColor toItemColor() {
return new ItemColor( this.colors );
}

@OnlyIn( Dist.CLIENT )
public static class ItemColor implements net.minecraft.client.color.item.ItemColor {
private final Map< Integer, Integer > colors;

public ItemColor( Map< Integer, Integer > colors ) {
this.colors = colors;
}

@Override
public int getColor( ItemStack itemStack, int layerIdx ) {
return this.colors.getOrDefault( layerIdx, 0xffffff );
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static Event< OnItemUseTicked > listen( Consumer< OnItemUseTicked > consu
public OnItemUseTicked( LivingEntity entity, ItemStack itemStack, int maxDuration, int duration ) {
this.entity = entity;
this.itemStack = itemStack;
this.maxDuration = maxDuration;
this.maxDuration = maxDuration != 0 ? maxDuration : duration;
this.original = duration;
this.duration = duration;
}
Expand Down
30 changes: 14 additions & 16 deletions common/src/main/java/com/majruszlibrary/level/LevelHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,14 @@ public static void spawnItemEntityFlyingTowardsDirection( ItemStack itemStack, L
}

public static void startRaining( Level level, int ticks, boolean withThunder ) {
if( !( level.getLevelData() instanceof ServerLevelData data ) ) {
return;
}

data.setRaining( true );
data.setRainTime( ticks );
if( withThunder ) {
data.setThundering( true );
data.setThunderTime( ticks );
if( level.getLevelData() instanceof ServerLevelData data ) {
data.setRaining( true );
data.setRainTime( ticks );
if( withThunder ) {
data.setThundering( true );
data.setThunderTime( ticks );
}
data.setClearWeatherTime( 0 );
}
}

Expand All @@ -156,14 +155,13 @@ public static void startRaining( Level level, int ticks ) {
}

public static void setClearWeather( Level level, int ticks ) {
if( !( level.getLevelData() instanceof ServerLevelData data ) ) {
return;
if( level.getLevelData() instanceof ServerLevelData data ) {
data.setRaining( false );
data.setRainTime( 0 );
data.setThundering( false );
data.setThunderTime( 0 );
data.setClearWeatherTime( ticks );
}

data.setRaining( false );
data.setRainTime( ticks );
data.setThundering( false );
data.setThunderTime( ticks );
}

public static < Type extends Number & Comparable< Type > > Optional< BlockPos > findBlockPosOnGround( Level level, Vec3 position, Type yOffset ) {
Expand Down
26 changes: 23 additions & 3 deletions common/src/main/java/com/majruszlibrary/mixin/MixinItemColors.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,42 @@
package com.majruszlibrary.mixin;

import com.majruszlibrary.events.OnItemRenderColorGet;
import com.majruszlibrary.events.OnItemRenderColorsGet;
import com.majruszlibrary.events.base.Events;
import net.minecraft.client.color.item.ItemColor;
import net.minecraft.client.color.item.ItemColors;
import net.minecraft.world.item.ItemStack;
import org.spongepowered.asm.mixin.Dynamic;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin( ItemColors.class )
@Mixin( value = ItemColors.class, priority = 1100 )
public abstract class MixinItemColors {
@Inject(
at = @At( "RETURN" ),
cancellable = true,
method = "getColor (Lnet/minecraft/world/item/ItemStack;I)I"
)
private void getColor( ItemStack itemStack, int layerIdx, CallbackInfoReturnable< Integer > callback ) {
callback.setReturnValue( Events.dispatch( new OnItemRenderColorGet( itemStack, layerIdx, callback.getReturnValue() ) ).getColor() );
OnItemRenderColorsGet data = Events.dispatch( new OnItemRenderColorsGet( itemStack ) );
if( data.hasColorsDefined() ) {
callback.setReturnValue( data.toItemColor().getColor( itemStack, layerIdx ) );
}
}

@Dynamic( "Sodium compatibility" )
@Inject(
at = @At( "RETURN" ),
cancellable = true,
method = "sodium$getColorProvider",
remap = false,
require = 0
)
private void getColorProvider( ItemStack itemStack, CallbackInfoReturnable< ItemColor > callback ) {
OnItemRenderColorsGet data = Events.dispatch( new OnItemRenderColorsGet( itemStack ) );
if( data.hasColorsDefined() ) {
callback.setReturnValue( data.toItemColor() );
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.majruszlibrary.mixin;

import com.majruszlibrary.events.OnClientTicked;
import com.majruszlibrary.events.OnGameInitialized;
import com.majruszlibrary.events.base.Events;
import net.minecraft.client.Minecraft;
import org.spongepowered.asm.mixin.Mixin;
Expand All @@ -11,14 +10,6 @@

@Mixin( Minecraft.class )
public abstract class MixinMinecraft {
@Inject(
at = @At( "HEAD" ),
method = "onGameLoadFinished ()V"
)
private void onGameLoadFinished( CallbackInfo callback ) {
Events.dispatch( new OnGameInitialized() );
}

@Inject(
at = @At( "TAIL" ),
method = "tick ()V"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ public < Type > Type getInstance( Predicate< Class< ? > > predicate ) {

for( File mod : mods.listFiles() ) {
try {
if( mod.isDirectory() ) {
continue;
}

JarFile modJar = new JarFile( mod );
if( modJar.getJarEntry( "com/%s".formatted( this.helper.getModId() ) ) == null ) {
continue;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,56 +1,59 @@
package com.majruszlibrary.mixin.fabric;

import com.majruszlibrary.item.CustomEnchantment;
import net.minecraft.world.item.Item;
import com.majruszlibrary.registry.Registries;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.enchantment.Enchantment;
import net.minecraft.world.item.enchantment.EnchantmentCategory;
import net.minecraft.world.item.enchantment.EnchantmentHelper;
import net.minecraft.world.item.enchantment.EnchantmentInstance;
import org.spongepowered.asm.mixin.Mixin;
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.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.List;

@Mixin( EnchantmentHelper.class )
public abstract class MixinEnchantmentHelper {
private static ItemStack majruszlibrary$LAST_ITEM_STACK = null;
private static Enchantment majruszlibrary$LAST_ENCHANTMENT = null;

@ModifyVariable(
at = @At( "STORE" ),
method = "getAvailableEnchantmentResults (ILnet/minecraft/world/item/ItemStack;Z)Ljava/util/List;"
)
private static Enchantment getEnchantment( Enchantment enchantment ) {
majruszlibrary$LAST_ENCHANTMENT = enchantment;

return enchantment;
}

// TODO: in Fabric 0.15.0+ there will be a native support for MixinExtras that can be used to make it cleaner
@Inject(
at = @At( "HEAD" ),
at = @At( "RETURN" ),
cancellable = true,
method = "getAvailableEnchantmentResults (ILnet/minecraft/world/item/ItemStack;Z)Ljava/util/List;"
)
private static void getAvailableEnchantmentResults( int $$0, ItemStack itemStack, boolean $$2,
private static void getAvailableEnchantmentResults( int level, ItemStack itemStack, boolean isTreasure,
CallbackInfoReturnable< List< EnchantmentInstance > > callback
) {
majruszlibrary$LAST_ITEM_STACK = itemStack;
}
List< EnchantmentInstance > enchantments = callback.getReturnValue();
enchantments.removeIf( enchantment->enchantment.enchantment instanceof CustomEnchantment );
boolean isBook = itemStack.is( Items.BOOK );

@Redirect(
at = @At(
target = "Lnet/minecraft/world/item/enchantment/EnchantmentCategory;canEnchant (Lnet/minecraft/world/item/Item;)Z",
value = "INVOKE"
),
method = "getAvailableEnchantmentResults (ILnet/minecraft/world/item/ItemStack;Z)Ljava/util/List;"
)
private static boolean canEnchantUsingEnchantingTable( EnchantmentCategory category, Item item ) {
return majruszlibrary$LAST_ENCHANTMENT instanceof CustomEnchantment enchantment
? enchantment.canEnchantUsingEnchantingTable( majruszlibrary$LAST_ITEM_STACK )
: category.canEnchant( item );
for( Enchantment enchantment : Registries.ENCHANTMENTS ) {
if( !( enchantment instanceof CustomEnchantment customEnchantment ) ) {
continue;
}

if( enchantment.isTreasureOnly() && !isTreasure ) {
continue;
}

if( !enchantment.isDiscoverable() ) {
continue;
}

if( !customEnchantment.canEnchantUsingEnchantingTable( itemStack ) && !isBook ) {
continue;
}

for( int enchantmentLevel = enchantment.getMaxLevel(); enchantmentLevel > enchantment.getMinLevel() - 1; --enchantmentLevel ) {
if( level >= enchantment.getMinCost( enchantmentLevel ) && level <= enchantment.getMaxCost( enchantmentLevel ) ) {
enchantments.add( new EnchantmentInstance( enchantment, enchantmentLevel ) );
break;
}
}
}

callback.setReturnValue( enchantments );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.majruszlibrary.mixin.fabric;

import com.majruszlibrary.events.OnGameInitialized;
import com.majruszlibrary.events.base.Events;
import net.minecraft.client.Minecraft;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin( Minecraft.class )
public abstract class MixinMinecraft {
@Inject(
at = @At( "HEAD" ),
method = "onGameLoadFinished ()V"
)
private void onGameLoadFinished( CallbackInfo callback ) {
Events.dispatch( new OnGameInitialized() );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"client": [
"MixinAbstractContainerScreen",
"MixinGui",
"MixinMinecraft",
"MixinParticleEngine"
],
"mixins": [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.majruszlibrary.events;

import com.majruszlibrary.events.base.Events;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;

@Mod.EventBusSubscriber( value = Dist.CLIENT, bus = Mod.EventBusSubscriber.Bus.MOD )
public class OnGameInitializedForge {
@SubscribeEvent
public static void initialize( FMLClientSetupEvent event ) {
event.enqueueWork( ()->Events.dispatch( new OnGameInitialized() ) );
}
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ minecraft_version=1.20.1
# Mod
mod_id=majruszlibrary
mod_archives_name=majrusz-library
mod_version=7.0.0
mod_version=7.0.1
mod_display_name=Majrusz Library
mod_description=Library with common code for my other modifications.
mod_authors=Majrusz
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.majruszlibrary.events;

import com.majruszlibrary.events.base.Events;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;

@Mod.EventBusSubscriber( value = Dist.CLIENT, bus = Mod.EventBusSubscriber.Bus.MOD )
public class OnGameInitializedNeoForge {
@SubscribeEvent
public static void initialize( FMLClientSetupEvent event ) {
event.enqueueWork( ()->Events.dispatch( new OnGameInitialized() ) );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.majruszlibrary.modhelper.DataNeoForge

0 comments on commit c6a18df

Please sign in to comment.