2019-03-01 18:27:24 +01:00
package de.ellpeck.naturesaura.compat.patchouli ;
2021-12-16 21:56:27 +01:00
import com.mojang.blaze3d.systems.RenderSystem ;
import de.ellpeck.naturesaura.ModConfig ;
2019-03-01 18:27:24 +01:00
import de.ellpeck.naturesaura.NaturesAura ;
2021-12-16 21:56:27 +01:00
import de.ellpeck.naturesaura.api.multiblock.Matcher ;
2020-01-26 19:26:50 +01:00
import de.ellpeck.naturesaura.compat.ICompat ;
2020-01-29 00:40:28 +01:00
import de.ellpeck.naturesaura.data.ItemTagProvider ;
2021-12-16 21:56:27 +01:00
import de.ellpeck.naturesaura.events.ClientEvents ;
import de.ellpeck.naturesaura.renderers.SupporterFancyHandler ;
import net.minecraft.ChatFormatting ;
2020-04-29 16:38:50 +02:00
import net.minecraft.client.Minecraft ;
2022-06-27 15:24:04 +02:00
import net.minecraft.network.chat.Component ;
2021-12-16 21:56:27 +01:00
import net.minecraft.network.chat.Style ;
2021-12-08 00:31:29 +01:00
import net.minecraft.resources.ResourceLocation ;
2021-12-16 21:56:27 +01:00
import net.minecraft.world.item.crafting.Ingredient ;
2021-12-08 00:31:29 +01:00
import net.minecraft.world.item.crafting.Recipe ;
2021-12-16 21:56:27 +01:00
import net.minecraftforge.api.distmarker.Dist ;
import net.minecraftforge.api.distmarker.OnlyIn ;
2020-01-21 23:02:39 +01:00
import net.minecraftforge.common.MinecraftForge ;
2021-12-16 21:56:27 +01:00
import net.minecraftforge.eventbus.api.SubscribeEvent ;
2021-03-30 16:22:40 +02:00
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent ;
2021-12-16 21:56:27 +01:00
import vazkii.patchouli.api.BookDrawScreenEvent ;
import vazkii.patchouli.api.IMultiblock ;
import vazkii.patchouli.api.IVariable ;
import vazkii.patchouli.api.PatchouliAPI ;
import java.time.LocalDateTime ;
import java.time.Month ;
2023-07-08 12:32:27 +02:00
import java.util.* ;
2021-12-16 21:56:27 +01:00
import java.util.stream.Collectors ;
2019-03-01 20:51:43 +01:00
2020-01-26 19:26:50 +01:00
public class PatchouliCompat implements ICompat {
2019-03-01 18:27:24 +01:00
2019-03-01 20:51:43 +01:00
private static final ResourceLocation BOOK = new ResourceLocation ( NaturesAura . MOD_ID , " book " ) ;
2021-03-30 16:22:40 +02:00
private static final Map < ResourceLocation , IMultiblock > MULTIBLOCKS = new HashMap < > ( ) ;
2019-03-01 18:27:24 +01:00
2020-02-07 15:22:30 +01:00
public static void addPatchouliMultiblock ( ResourceLocation name , String [ ] [ ] pattern , Object . . . rawMatchers ) {
2021-12-16 21:56:27 +01:00
for ( var i = 1 ; i < rawMatchers . length ; i + = 2 ) {
if ( rawMatchers [ i ] instanceof Matcher matcher ) {
var check = matcher . check ( ) ;
2020-02-07 15:22:30 +01:00
if ( check = = null )
2021-03-30 16:22:40 +02:00
rawMatchers [ i ] = PatchouliAPI . get ( ) . anyMatcher ( ) ;
2020-02-07 15:22:30 +01:00
else
2021-12-16 21:56:27 +01:00
rawMatchers [ i ] = PatchouliAPI . get ( ) . predicateMatcher ( matcher . defaultState ( ) ,
2020-02-07 15:22:30 +01:00
state - > check . matches ( null , null , null , null , state , ( char ) 0 ) ) ;
}
}
2022-06-27 15:24:04 +02:00
PatchouliCompat . MULTIBLOCKS . put ( name , PatchouliAPI . get ( ) . makeMultiblock ( pattern , rawMatchers ) ) ;
2020-02-07 15:22:30 +01:00
}
2021-12-08 00:31:29 +01:00
@SuppressWarnings ( " unchecked " )
public static < T extends Recipe < ? > > T getRecipe ( String type , String name ) {
2021-12-15 16:30:22 +01:00
var manager = Minecraft . getInstance ( ) . level . getRecipeManager ( ) ;
var res = new ResourceLocation ( name ) ;
var pre = new ResourceLocation ( res . getNamespace ( ) , type + " / " + res . getPath ( ) ) ;
2021-12-08 00:31:29 +01:00
return ( T ) manager . byKey ( pre ) . orElse ( null ) ;
2020-02-07 15:22:30 +01:00
}
2021-12-16 21:56:27 +01:00
public static IVariable ingredientVariable ( Ingredient ingredient ) {
return IVariable . wrapList ( Arrays . stream ( ingredient . getItems ( ) )
2020-09-22 18:03:56 +02:00
. map ( IVariable : : from ) . collect ( Collectors . toList ( ) ) ) ;
2021-12-16 21:56:27 +01:00
}
2020-09-22 18:03:56 +02:00
2020-01-26 19:26:50 +01:00
@Override
2021-03-30 16:22:40 +02:00
public void setup ( FMLCommonSetupEvent event ) {
2021-12-16 21:56:27 +01:00
event . enqueueWork ( ( ) - > {
2022-06-27 15:24:04 +02:00
for ( var entry : PatchouliCompat . MULTIBLOCKS . entrySet ( ) )
2021-03-30 16:22:40 +02:00
PatchouliAPI . get ( ) . registerMultiblock ( entry . getKey ( ) , entry . getValue ( ) ) ;
PatchouliAPI . get ( ) . setConfigFlag ( NaturesAura . MOD_ID + " :rf_converter " , ModConfig . instance . rfConverter . get ( ) ) ;
PatchouliAPI . get ( ) . setConfigFlag ( NaturesAura . MOD_ID + " :chunk_loader " , ModConfig . instance . chunkLoader . get ( ) ) ;
2021-12-16 21:56:27 +01:00
} ) ;
2019-03-01 18:27:24 +01:00
}
2020-01-26 19:26:50 +01:00
@Override
2020-04-26 20:40:28 +02:00
public void setupClient ( ) {
2020-01-26 19:26:50 +01:00
MinecraftForge . EVENT_BUS . register ( this ) ;
}
@Override
public void addItemTags ( ItemTagProvider provider ) {
}
2021-12-16 21:56:27 +01:00
@SubscribeEvent
2019-10-20 22:30:49 +02:00
@OnlyIn ( Dist . CLIENT )
2019-03-03 23:33:38 +01:00
public void onBookDraw ( BookDrawScreenEvent event ) {
2021-12-16 21:56:27 +01:00
var book = event . getBook ( ) ;
var gui = event . getScreen ( ) ;
2022-06-27 15:24:04 +02:00
if ( book = = null | | ! book . equals ( PatchouliCompat . BOOK ) )
2019-03-03 23:33:38 +01:00
return ;
2021-12-16 21:56:27 +01:00
var now = LocalDateTime . now ( ) ;
2019-03-03 23:33:38 +01:00
if ( now . getMonth ( ) = = Month . MAY & & now . getDayOfMonth ( ) = = 21 ) {
2021-12-16 21:56:27 +01:00
var x = gui . width / 2 + 272 / 2 - 16 ;
var y = gui . height / 2 - 180 / 2 - 26 ;
2023-07-08 12:32:27 +02:00
event . getGraphics ( ) . blit ( ClientEvents . BOOK_GUI , x , y , 469 , 0 , 43 , 42 , 512 , 256 ) ;
2021-12-16 21:56:27 +01:00
if ( event . getMouseX ( ) > = x & & event . getMouseY ( ) > = y & & event . getMouseX ( ) < x + 43 & & event . getMouseY ( ) < y + 42 )
2023-07-08 12:32:27 +02:00
event . getGraphics ( ) . renderTooltip ( Minecraft . getInstance ( ) . font ,
2022-06-27 15:24:04 +02:00
Collections . singletonList ( Component . literal ( " It's the author Ellpeck's birthday! " ) . setStyle ( Style . EMPTY . applyFormat ( ChatFormatting . GOLD ) ) ) ,
2023-07-08 12:32:27 +02:00
Optional . empty ( ) ,
event . getMouseX ( ) , event . getMouseY ( ) ) ;
2020-02-27 12:27:34 +01:00
} else if ( now . getMonth ( ) = = Month . JUNE ) {
2021-12-16 21:56:27 +01:00
var x = gui . width / 2 + 272 / 2 ;
2022-06-01 15:54:05 +02:00
var y = gui . height / 2 + 32 ;
2021-12-16 21:56:27 +01:00
2023-07-08 12:32:27 +02:00
event . getGraphics ( ) . blit ( ClientEvents . BOOK_GUI , x , y , 424 , 0 , 45 , 26 , 512 , 256 ) ;
2021-12-16 21:56:27 +01:00
if ( event . getMouseX ( ) > = x & & event . getMouseY ( ) > = y & & event . getMouseX ( ) < x + 45 & & event . getMouseY ( ) < y + 26 )
2023-07-08 12:32:27 +02:00
//noinspection UnnecessaryUnicodeEscape
event . getGraphics ( ) . renderTooltip ( gui . getMinecraft ( ) . font ,
Collections . singletonList ( Component . literal ( " \ u00A76Happy \ u00A74P \ u00A76r \ u00A7ei \ u00A72d \ u00A79e \ u00A75! " ) ) , Optional . empty ( ) ,
event . getMouseX ( ) , event . getMouseY ( ) ) ;
2019-03-03 23:33:38 +01:00
}
2019-03-01 23:54:38 +01:00
2021-12-16 21:56:27 +01:00
var name = gui . getMinecraft ( ) . player . getName ( ) . getString ( ) ;
var info = SupporterFancyHandler . FANCY_INFOS . get ( name ) ;
2019-03-03 23:33:38 +01:00
if ( info ! = null ) {
2021-12-16 21:56:27 +01:00
var x = gui . width / 2 - 272 / 2 + 20 ;
var y = gui . height / 2 + 180 / 2 ;
2019-03-01 23:54:38 +01:00
2023-07-08 12:32:27 +02:00
event . getGraphics ( ) . blit ( ClientEvents . BOOK_GUI , x , y , 496 , 44 , 16 , 18 , 512 , 256 ) ;
2021-12-16 21:56:27 +01:00
if ( info . tier ( ) = = 1 ) {
2023-07-08 12:32:27 +02:00
event . getGraphics ( ) . blit ( ClientEvents . BOOK_GUI , x , y , 496 - 16 , 44 , 16 , 18 , 512 , 256 ) ;
2019-03-03 23:33:38 +01:00
} else {
2022-06-27 15:24:04 +02:00
var r = ( info . color ( ) > > 16 & 255 ) / 255F ;
var g = ( info . color ( ) > > 8 & 255 ) / 255F ;
2021-12-16 21:56:27 +01:00
var b = ( info . color ( ) & 255 ) / 255F ;
2021-12-19 18:24:54 +01:00
RenderSystem . setShaderColor ( r , g , b , 1 ) ;
2023-07-08 12:32:27 +02:00
event . getGraphics ( ) . blit ( ClientEvents . BOOK_GUI , x , y , 496 - 32 , 44 , 16 , 18 , 512 , 256 ) ;
2019-03-03 23:33:38 +01:00
}
2019-03-01 23:54:38 +01:00
2021-12-16 21:56:27 +01:00
if ( event . getMouseX ( ) > = x & & event . getMouseY ( ) > = y & & event . getMouseX ( ) < x + 16 & & event . getMouseY ( ) < y + 18 )
2023-07-08 12:32:27 +02:00
event . getGraphics ( ) . renderTooltip ( gui . getMinecraft ( ) . font ,
Collections . singletonList ( Component . literal ( " Thanks for your support, " + name + " ! " ) . setStyle ( Style . EMPTY . applyFormat ( ChatFormatting . YELLOW ) ) ) , Optional . empty ( ) ,
event . getMouseX ( ) , event . getMouseY ( ) ) ;
2019-03-01 23:54:38 +01:00
2019-03-01 18:27:24 +01:00
}
2021-12-16 21:56:27 +01:00
}
2019-03-01 18:27:24 +01:00
}