Compare commits

..

401 commits
1.17 ... 0.5.3

Author SHA1 Message Date
paulevsGitch
fc3dc71a44 Feature array length fix 2021-11-22 17:15:33 +03:00
paulevsGitch
42aedf2525
Merge pull request #48 from mindy15963/patch-1
Create ko_kr.json
2021-11-22 12:17:13 +03:00
E. Kim
2969680b13
Create ko_kr.json 2021-11-22 13:01:11 +09:00
paulevsGitch
fd0488dd6a Biome by category adding 2021-11-20 20:32:30 +03:00
paulevsGitch
df214cdb24 Biome include lists 2021-11-20 20:24:00 +03:00
paulevsGitch
c0b7ccca72 Cleanup 2021-11-20 19:01:43 +03:00
paulevsGitch
8ea432bc19
Merge pull request #45 from davidalb97/main
Migrated BetterNether's PR BetterNether/pull/437 into BCLib
2021-11-20 18:57:13 +03:00
paulevsGitch
d8c5e84520 Version change 2021-11-20 13:58:35 +03:00
paulevsGitch
51d484f61e Small changes 2021-11-20 13:57:54 +03:00
paulevsGitch
a2dec40b0a Biome config 2021-11-20 13:41:03 +03:00
paulevsGitch
69d11c0e5a Small cleanup 2021-11-19 17:55:34 +03:00
paulevsGitch
b2ba2976dd Merge remote-tracking branch 'origin/main' 2021-11-19 17:52:54 +03:00
paulevsGitch
0e80d66097 Fix for #46 2021-11-19 17:52:41 +03:00
davidalb97
2206674c0d Migrated BetterNether's PR BetterNether/pull/437 into BCLib 2021-11-18 12:22:37 +00:00
Frank
cf8905ab1a Added ComposterAPI (paulevsGitch/BetterNether#442) 2021-11-15 14:41:46 +01:00
Frank
ce4cb8974f Fixed potential crash in AnvilMenu 2021-11-13 10:50:57 +01:00
paulevsGitch
5ad8752e00 Crafting menu fix 2021-11-13 11:33:04 +03:00
paulevsGitch
cae1d932a1 Biome feature adding API 2021-11-12 17:04:18 +03:00
paulevsGitch
e45f1ac064 Biome source options 2021-11-12 09:22:47 +03:00
paulevsGitch
cc33a9138b Biome modification API 2021-11-12 08:50:41 +03:00
Frank
60574d5b75 Fallback to uncompressed when reading player data (paulevsGitch/BetterNether#436) 2021-11-06 21:26:32 +01:00
Frank
07d8c56e18 Mark Mods that do not contain a client entrypoint as Server-Only for Sync 2021-11-06 20:59:06 +01:00
Frank
2cbfbe8047 Add config to exclude mods from syncing/info 2021-11-06 20:58:41 +01:00
Frank
8233174487 Show Mod info sorted by severity and show client-only mods as OK 2021-11-06 20:04:52 +01:00
Frank
a879a82645 Turn off ModInfo on the server by default 2021-11-06 19:41:40 +01:00
Frank
1b683b5564 Fixed crash on server 2021-11-06 19:16:20 +01:00
paulevsGitch
5247a80549 Biome changer mixin & interface 2021-11-06 15:52:26 +03:00
paulevsGitch
c61856cdba Mod blocks in registry, version change 2021-11-06 15:41:20 +03:00
Frank
473029c31f Fixed JavaDoc error 2021-11-06 11:59:13 +01:00
Frank
d04d75222e A ForcedLevelPatch is always executed against level.dat, (no matter what patchLevel is set for a world) 2021-11-06 11:57:15 +01:00
Frank
f648669c95 Removed errors created for debug purpose 2021-11-06 11:48:19 +01:00
Frank
03e8733ba0 Add UI that presents Fixer Errors to the User(paulevsGitch/BetterNether#436) 2021-11-06 11:41:17 +01:00
Frank
03bef36a45 Updated Version 2021-11-04 02:34:41 +01:00
Frank
143653e2b6 Custom parsing of relevant Mod-Info. Should make it compatible with loader 0.11.x and 0.12.x (#34, #36) 2021-11-04 02:18:33 +01:00
Frank
9a237f52f5 increased fabricloader version (#36, #34) 2021-11-04 00:53:10 +01:00
Frank
dfad74f4a0 Remove BCLib Version Warning screen when user clicks NO 2021-11-03 16:35:52 +01:00
Frank
cca21cd727 Bumped Version and reduced loader dependency (#34) 2021-11-03 16:08:58 +01:00
Frank
383346942f Updated fabric dependencies 2021-11-03 15:45:34 +01:00
Frank
b7c83923ea Using latest fabric API 2021-11-03 15:44:25 +01:00
paulevsGitch
3efa71b08a Null pointer translation fix 2021-11-02 15:25:08 +03:00
paulevsGitch
8eba063b40 Import optimisation, carver adding fix 2021-11-01 14:16:17 +03:00
Frank
2518759f7b Fixed name 2021-10-30 15:00:52 +02:00
Frank
cf31d76ee1 added c:immobile for unmovable blocks like obsidian 2021-10-30 15:00:43 +02:00
paulevsGitch
d33cb707fd Biome def sky color 2021-10-28 17:37:58 +03:00
paulevsGitch
b66c8cd5a6 Registry changes (WIP) 2021-10-26 16:12:10 +03:00
paulevsGitch
449d7cc0d0 No-abstract registry 2021-10-26 15:00:09 +03:00
Frank
0c4ac239b8 Merge branch 'main' of github.com-quiqueck:paulevsGitch/BCLib 2021-10-26 10:20:30 +02:00
Frank
8c535be429 Added support for custom nether-portal frames (common Tag c:nether_pframe (TagAPI.BLOCK_NETHER_PORTAL_FRAME) 2021-10-26 10:20:25 +02:00
paulevsGitch
6455cd2182 Merge remote-tracking branch 'origin/main' 2021-10-25 17:37:45 +03:00
paulevsGitch
102f80c62e Fog rendering fixes 2021-10-25 17:37:36 +03:00
Frank
460f4a8e6c Merge branch 'main' of github.com-quiqueck:paulevsGitch/BCLib 2021-10-25 15:32:46 +02:00
Frank
a5bf1ea3e6 Create Ores with custom properties 2021-10-25 15:32:36 +02:00
Frank
7f7e4bfa4c Better Shears Integration 2021-10-25 15:32:17 +02:00
paulevsGitch
9256faf0ba Merge remote-tracking branch 'origin/main' 2021-10-25 16:20:15 +03:00
paulevsGitch
3888998aff Generator config fix 2021-10-25 16:20:06 +03:00
Frank
1195bcf4c3 Merge branch 'main' of github.com-quiqueck:paulevsGitch/BCLib 2021-10-23 19:49:14 +02:00
Frank
d47321c0f7 Prepared state fixer 2021-10-23 19:49:02 +02:00
paulevsGitch
69ae8ed33e Small change 2021-10-22 21:59:56 +03:00
Frank
f88506f654 Revert "Merge remote-tracking branch 'origin/main'"
This reverts commit ff8b366eb9, reversing
changes made to 52146286fe.
2021-10-22 15:28:37 +02:00
paulevsGitch
9b312bf4de Debug removal 2021-10-22 13:53:37 +03:00
paulevsGitch
ff8b366eb9 Merge remote-tracking branch 'origin/main' 2021-10-22 13:50:14 +03:00
paulevsGitch
52146286fe Thick fog rendering fixes 2021-10-22 13:50:07 +03:00
Frank
e37d03fa77 replaced accesswidener with simple reflection
(cherry picked from commit 18a64c267d)
2021-10-22 12:23:44 +02:00
Frank
da0d122166 Added Function that allows developers to manually fix nbts 2021-10-19 09:58:41 +02:00
Frank
bd4e61f14f Add support for hanging saplings 2021-10-18 22:53:53 +02:00
Frank
3683ebd97a Auto-Add Block/Item Tags for BaseLeavesBlock and FeatureSaplingBlock (paulevsGitch/BetterEnd#303) 2021-10-18 20:09:41 +02:00
Frank
c2452ebcc3 Added common leaves Tag (paulevsGitch/BetterEnd#303) 2021-10-18 19:52:32 +02:00
Frank
073b473840 Preparing common Tags (paulevsGitch/BetterEnd#303) 2021-10-18 19:38:31 +02:00
Frank
a52b18ba9b MultiPartBuilder.create returns an Instance not a globa Object (improves thread safety, fixes paulevsGitch/BetterEnd#316) 2021-10-18 19:26:10 +02:00
Frank
06df39445c removed unneeded import 2021-10-18 12:44:45 +02:00
Frank
c0810fd7ff add dev-configuration to build-system 2021-10-18 12:40:33 +02:00
Frank
2674940c91 Added AnvilReceipts from BE 2021-10-18 12:26:35 +02:00
Frank
57e2099650 Added IPNIgnore for compat with *Inventory Profiles Next* 2021-10-18 09:28:43 +02:00
Frank
cbed2868ba German translation 2021-10-07 14:22:38 +02:00
Frank
c39c1673a9 Some german translations 2021-09-16 10:36:02 +02:00
Frank
7e2ea43817 removed debug line 2021-09-15 08:22:06 +02:00
Frank
7d9b56b6e7 Minor additions to BiomeAPI 2021-08-27 03:52:06 +02:00
Frank
329509d5c0 Removed debug code 2021-08-27 01:01:45 +02:00
Frank
9a356dbf0d Mod Warning Fixes 2021-08-26 21:28:31 +02:00
Frank
d473451295 Ignore Files from Mods that are not known on the client 2021-08-26 21:22:28 +02:00
Frank
0673a271ef ProgressScreen can not close on esc 2021-08-26 10:14:02 +02:00
Frank
1d9d752832 Added wait for Progress Stage 2021-08-25 18:02:55 +02:00
Frank
ae9463124f Latest Fabric-API 2021-08-25 17:36:21 +02:00
Frank
117ab3fa3d Fixed prefix 2021-08-25 17:31:06 +02:00
Frank
d1ef3b5807 Added progress Screen for DataFixer 2021-08-25 17:27:00 +02:00
Frank
11926ac63c Option to disable ModInfo-Screen 2021-08-25 15:12:45 +02:00
Frank
4692831007 More options for ModSyncing and Screen to display missmatching mods 2021-08-25 15:10:51 +02:00
Frank
b959e17c18 Offer Mod Info Screen 2021-08-25 14:11:48 +02:00
Frank
9d7fa9f925 Scrollable GridScreen 2021-08-25 13:47:08 +02:00
Frank
f3bdaaac8e Make sure we do not close the jar filesystem when cheking for mods (otherwise resources might fail) 2021-08-24 16:59:59 +02:00
Frank
617573ca09 BCLib Syncing fixes 2021-08-24 14:49:29 +02:00
Frank
59fcaf6ee9 Reenable BCLIb-Version warning 2021-08-24 14:37:56 +02:00
Frank
16f4451803 Inherited GridColumn width 2021-08-24 14:22:14 +02:00
Frank
939fe9ddca Added AccessWidener 2021-08-23 15:26:36 +02:00
Frank
0816fd032d Merge branch 'feature/ChunkedFileTransfer' 2021-08-23 15:24:15 +02:00
Frank
922e5c9f26 Removed testing files 2021-08-23 15:22:56 +02:00
Frank
2344ef9606 Fixed serverside crash due to Screen 2021-08-23 15:22:17 +02:00
Frank
3321d1c90e Use ProgressScreen for larger Data-Transfers 2021-08-23 15:07:02 +02:00
Frank
469e97b790 ProgressScreen 2021-08-23 15:06:34 +02:00
Frank
898668ae96 Disable mod-Sync by default 2021-08-23 10:09:44 +02:00
Frank
5c6d6a4cb0 Finished Chunk Data Transfere if Paylouad size exeeds maximum 2021-08-23 10:09:38 +02:00
Frank
63830a27d6 First (untested) version of File-Chunker 2021-08-22 15:00:29 +02:00
Frank
5e4f4d5b43 Fixed problems with DataFixer calls and Architectury 2021-08-22 12:00:21 +02:00
Frank
670928a604 Fix problem with non semantic versions 2021-08-22 11:59:43 +02:00
Frank
4a4191c80d Fixed JavaDoc Error 2021-08-21 14:33:55 +02:00
Frank
2b8ef4bcff Has-Debug should be off by default 2021-08-21 14:25:44 +02:00
Frank
4d56796244 Some minor Fixes 2021-08-21 14:25:09 +02:00
Frank
4f8840da68 Added serverside config to add additional Mods to get synced 2021-08-21 13:56:18 +02:00
Frank
ffffeb78ee Fixed issue with wron type 2021-08-21 13:54:06 +02:00
Frank
38c01b8c76 Additional Typesafety 2021-08-21 13:43:22 +02:00
Frank
edb6631768 Supporting StringArray in Configs 2021-08-21 13:43:10 +02:00
Frank
8f9ff14fac Added ConfigUI Annotation 2021-08-21 12:30:47 +02:00
Frank
0adc5024f1 Adding enabled predicate 2021-08-21 09:49:15 +02:00
Frank
b369954c05 Save configs from ModMenu-Screen 2021-08-20 16:50:23 +02:00
Frank
092e0a39e8 Auto generate a simple Config Screen 2021-08-20 14:37:40 +02:00
paulevsGitch
d31425623d Small cleanup 2021-08-20 15:28:38 +03:00
paulevsGitch
9c2b7c9188 Merge remote-tracking branch 'origin/main'
# Conflicts:
#	src/main/java/ru/bclib/api/dataexchange/handler/autosync/AutoFileSyncEntry.java
2021-08-20 15:26:29 +03:00
paulevsGitch
98121a30ef Small fix & cleanup 2021-08-20 15:26:13 +03:00
Frank
601b984430 Merge branch 'main' of github.com-quiqueck:paulevsGitch/BCLib 2021-08-20 14:15:33 +02:00
Frank
fe7e1aa28d Prepare Configs that enable us to derive a ConfigScreen 2021-08-20 14:15:21 +02:00
paulevsGitch
529565911f Fixed javadocs, biome API enhancements 2021-08-20 15:11:36 +03:00
Frank
8d6dc18ce2 Merge branch 'main' of github.com-quiqueck:paulevsGitch/BCLib 2021-08-20 13:24:56 +02:00
Frank
d88462e827 Make auto-sync options consistent with UI 2021-08-20 13:24:47 +02:00
paulevsGitch
5ed75012cd
Merge pull request #32 from glisco03/main
Always use the KubeJS workaround for recipe loading to improve mod compatibility
2021-08-20 13:02:38 +03:00
Frank
6e2a539232 Minor refactoring 2021-08-20 03:16:20 +02:00
Frank
290bed1ffe Added missing doc 2021-08-20 03:06:34 +02:00
Frank
0486b56ec3 Refactored to expose the ModMenu Integration to other Mods 2021-08-20 03:05:36 +02:00
Frank
5808bab08e ModMenu Integration 2021-08-20 01:57:27 +02:00
Frank
fa4e086220 close screen to parent 2021-08-20 01:54:20 +02:00
Frank
48c46e2105 Offer ModMenu if installed 2021-08-20 01:53:55 +02:00
Frank
7eb44ebb82 Make sure the server can load client-side mod's for syncing 2021-08-20 01:52:37 +02:00
Frank
14d3c18945 Small Mod Sync fixes 2021-08-19 20:30:43 +02:00
Frank
dfe25a9599 Sync Screen offers user-options 2021-08-19 20:12:01 +02:00
Frank
d46e375501 WIP: SyncScreen allows users to choose which content type to sync 2021-08-19 16:49:43 +02:00
Frank
698ecefba4 Fixed message 2021-08-19 16:39:37 +02:00
Frank
2d8c92d946 Better GridLayout for Screens 2021-08-19 16:39:30 +02:00
Frank
32e1b2edb4 Make config-register message clearer 2021-08-19 09:05:38 +02:00
Frank
ddaf8a0e01 Do not offer unavailable mod dependencies 2021-08-18 14:32:06 +02:00
Frank
3bb8fec4b2 Refactoring of ModUtil members 2021-08-18 14:29:15 +02:00
Frank
71ad055ea5 Implemented ModSync 2021-08-18 14:23:33 +02:00
Frank
8588191556 Adding ModFile Request 2021-08-18 10:05:11 +02:00
Frank
e9fc77ab0d Prepare to sync Mods 2021-08-18 09:57:52 +02:00
Frank
55f00c664c better naming for client/server only handlers 2021-08-17 12:56:37 +02:00
Frank
29fe59b4b9 Fix confi classes 2021-08-17 12:56:09 +02:00
Frank
123a5e2dc4 Refactored AutoSync 2021-08-17 11:57:40 +02:00
Frank
b398e47056 Make sure users have the option to not use AutoSync 2021-08-17 10:10:15 +02:00
Frank
de5e56cc04 Added API to load all ModMetadata from the 'mods' folder (classpath is not included) 2021-08-17 10:03:14 +02:00
Frank
6837c7d081 Make a non-recursive fileWalker 2021-08-17 09:53:04 +02:00
Frank
742ea56e34 Added Step to handler that allows users to stop the message from beeing sent and prepare some data. 2021-08-17 09:52:48 +02:00
Frank
132623419d moved fileWalker 2021-08-16 10:28:37 +02:00
Frank
03bef0ae88 Reject file sync in a Singleplayer World 2021-08-16 09:37:06 +02:00
Frank
0b3e4198cd Disabled testing code 2021-08-16 09:34:49 +02:00
Frank
fcf2d041cb check file-hashes before content comparison 2021-08-16 09:30:01 +02:00
Frank
31ae7a6d3a Reject folder syncs outside of game-dir 2021-08-16 09:04:53 +02:00
Frank
c7aeef43dd Reminder 2021-08-16 01:05:39 +02:00
Frank
fdbde2e0a6 Make sure we reject files that are not children of a sync-path 2021-08-16 01:01:51 +02:00
Frank
0b9d6093a0 [Ready for Testing] Folder-Syncing 2021-08-15 23:54:42 +02:00
glisco03
7f80cad6f4 Merge remote-tracking branch 'origin/main' 2021-08-15 21:37:19 +02:00
glisco03
23b372252b always load recipes later to improve mod compat 2021-08-15 21:34:22 +02:00
Frank
9cd0ef1f04 make diffContent count 2021-08-15 21:02:30 +02:00
Frank
1f239baeb9 *WIP* Prepared Folder Syncing - Filelist exchange 2021-08-15 15:06:36 +02:00
Frank
5df6de1e3a Implemented content based syncing 2021-08-15 13:55:31 +02:00
Frank Bauer
f80b55aa50 Prepared sync pipeline for content-based config sync 2021-08-15 11:30:39 +02:00
paulevsGitch
895b605523 Better Fabric biome API support 2021-08-15 01:42:51 +03:00
paulevsGitch
67be2ce342 Old biome source patcher 2021-08-14 23:06:32 +03:00
paulevsGitch
9e051d9ab9 Datapack biome source fix 2021-08-14 22:55:25 +03:00
paulevsGitch
28228b35e2 Version change, removed old API functions, javadoc fixes 2021-08-14 16:57:51 +03:00
paulevsGitch
1f0403d1b3 Merge remote-tracking branch 'origin/main' 2021-08-14 16:49:09 +03:00
paulevsGitch
410ff87c88 Small javadoc fixes 2021-08-14 16:48:55 +03:00
Frank
25bcbd6369 Using correct ModID 2021-08-14 15:45:51 +02:00
Frank
09395921ce Accept subclasses in equals 2021-08-14 15:43:48 +02:00
Frank
f28c3e0594 Config reload after write and hash-debug 2021-08-14 15:05:41 +02:00
paulevsGitch
7e36ac4159 Crash fixes 2021-08-14 01:32:25 +03:00
paulevsGitch
97ad8b44aa Small enhancements 2021-08-14 00:36:19 +03:00
paulevsGitch
015d60aee1 Generator options init fix 2021-08-14 00:18:21 +03:00
paulevsGitch
c2015848b0 More fixes 2021-08-13 22:08:18 +03:00
paulevsGitch
0acb947795 Init fixes 2021-08-13 21:59:43 +03:00
paulevsGitch
683427c312 Nether biome source (WIP) 2021-08-13 21:55:40 +03:00
paulevsGitch
34ecbb3f14 End biome source (WIP) 2021-08-13 21:23:12 +03:00
paulevsGitch
0e0944ba51 Dimensional biome API (WIP) 2021-08-13 14:53:19 +03:00
paulevsGitch
a9b8c57567 Temporal GUI disabling 2021-08-13 03:19:48 +03:00
paulevsGitch
7845f5beb3 Custom model bakery fix (#31) 2021-08-13 02:50:23 +03:00
paulevsGitch
83d5a4d2bf Temporal model loading fix (#31) 2021-08-12 03:26:53 +03:00
Frank
49e833e147 Removed unused network hooks 2021-08-11 00:04:37 +02:00
Frank
e06543dac6 More debug messages for file-sync 2021-08-11 00:01:36 +02:00
Frank
a21e7e115b Add config options to control autoSync 2021-08-10 23:39:59 +02:00
Frank
ca721368ea Make sure Mods are not registered twice 2021-08-10 23:39:34 +02:00
Frank
911ca85b03 Make sure server does not offer non-existing config files 2021-08-10 23:15:48 +02:00
paulevsGitch
0f3df9b229
Merge pull request #29 from quiqueck/main
Respect AutoSync setting for Config and add a prePatch run for level.dat
2021-08-10 23:17:01 +03:00
Frank
d479d49550 Merge branch 'main' of github.com:quiqueck/BCLib into main 2021-08-10 22:13:06 +02:00
Frank Bauer
b46f9f90d3 Introduce preChecks to the DataFixer 2021-08-10 21:50:43 +02:00
Frank Bauer
28b3669f39 Respect the auto-sync setting 2021-08-10 21:43:22 +02:00
paulevsGitch
49279480ec Version change 2021-08-10 14:44:04 +03:00
paulevsGitch
56592f3034 Javadoc fixes 2021-08-10 11:46:30 +03:00
paulevsGitch
c48c06d2f8
Merge pull request #28 from quiqueck/main
Start Hello process when player joins (not on enter)
2021-08-09 02:32:20 +03:00
Frank
54e1b486a7 Disable BCLibVersion-missmatch screen (as it is currently non functional) 2021-08-08 17:37:05 +02:00
Frank Bauer
819514a699 Start Hello process when player joins (not on enter) 2021-08-08 17:24:29 +02:00
paulevsGitch
990b024fdb
Merge pull request #27 from quiqueck/feature/data_fixer
Added Fixer for WorldDataAPI
2021-08-07 05:27:15 +03:00
Frank
862f3bb6b9 Fix ReceipBook for players 2021-08-07 04:14:35 +02:00
Frank
bc4ec0660f Make sure id-entries for the WorldDataAPI are fixed as well 2021-08-07 03:33:09 +02:00
Frank
dffda00940 Added API to fix id's on paths that can contain Lists/CompundTags 2021-08-07 02:08:17 +02:00
Frank
b232c764e0 Merge remote-tracking branch 'upstream/main' into feature/data_fixer 2021-08-07 01:52:14 +02:00
Frank
add5b65e85 Simplified Inventory handling 2021-08-07 01:51:54 +02:00
paulevsGitch
b2484cfd96
Merge pull request #26 from quiqueck/feature/networking
Auto File Sync
2021-08-07 01:38:43 +03:00
Frank
ace3156fb2 Additional Log output 2021-08-07 00:18:27 +02:00
Frank
5c11fb5424 Invalidate tokens on fail 2021-08-06 23:49:15 +02:00
Frank
5bff609fb6 Invalidate request token 2021-08-06 23:38:40 +02:00
Frank
4d3f7aa34b Api cleanup 2021-08-06 23:29:41 +02:00
Frank
3438c86de2 Refactor 2021-08-06 23:20:39 +02:00
Frank
6883287c72 Finished first draft of File syncing 2021-08-06 23:08:13 +02:00
Frank
c28a566c78 Fixed server crashes 2021-08-06 22:26:56 +02:00
Frank
d4c9695e5c Merge remote-tracking branch 'upstream/main' into feature/networking 2021-08-06 22:11:33 +02:00
Frank
391ca845cf Adding more verbose message 2021-08-06 22:11:08 +02:00
Frank
d01b7923aa Receive requested file contents on client 2021-08-06 21:27:33 +02:00
Frank Bauer
869dc762fa Send file requests 2021-08-06 19:19:49 +02:00
Frank Bauer
ae344c48ac Merge branch 'feature/networking' of github.com-quiqueck:quiqueck/BCLib into feature/networking 2021-08-06 18:12:21 +02:00
Frank
119e94520c Compare files on client/server 2021-08-06 17:40:46 +02:00
Frank
8397ef7cca Added FileHash Object 2021-08-06 09:33:11 +02:00
Frank
feee3514a9 Unpolute public API 2021-08-06 08:23:23 +02:00
Frank
9b92c6cbf2 call sendOnEnter 2021-08-05 19:09:34 +02:00
Frank
16cbba6e76 Preparing Config-Objects to automatically sync from Server to Client 2021-08-05 19:07:14 +02:00
Frank
589151af81 Call DataFixer on WorldData 2021-08-05 18:43:44 +02:00
paulevsGitch
ba38a32b86 Merge remote-tracking branch 'origin/main' 2021-08-01 17:57:31 +03:00
paulevsGitch
ceea648858 Fixed missing models with optifine 2021-08-01 17:56:09 +03:00
paulevsGitch
9541ba528b
Merge pull request #24 from RDKRACZ/main
Decreased icon file size.
2021-08-01 17:46:12 +03:00
paulevsGitch
427ae65856 Fix optifine compat 2021-08-01 17:45:50 +03:00
K0RR
5e9e30d80d
Add files via upload 2021-08-01 16:34:23 +02:00
Frank Bauer
433aa7a1c7 Merge branch 'main' into feature/networking 2021-08-01 14:24:28 +02:00
paulevsGitch
3de67d42b3 Another small fix & optimisation 2021-08-01 14:39:21 +03:00
paulevsGitch
47655f9a01 Another small fix 2021-08-01 14:17:19 +03:00
paulevsGitch
d497f97663 Small fix 2021-08-01 14:12:30 +03:00
paulevsGitch
b05f5a728f Compat with Canvas emissive shaders, fixes 2021-08-01 14:05:41 +03:00
Frank Bauer
2ae840634a Fixed message 2021-08-01 11:05:11 +02:00
paulevsGitch
bcc3b076c2 Some small alpha emission changes 2021-07-31 16:22:52 +03:00
Frank Bauer
70cfdf0dd8 Display DataExchange Dialog 2021-07-31 14:04:14 +02:00
paulevsGitch
90c8362c95 JSON cache 2021-07-31 14:58:01 +03:00
Frank Bauer
7bfe0b01cd Ability to send data when player first enters the level 2021-07-31 13:51:39 +02:00
paulevsGitch
3a5a264436 Model loading fix 2021-07-31 14:46:20 +03:00
Frank Bauer
4fc9c06204 Prepared BCLib Version warning screen 2021-07-31 11:29:13 +02:00
Frank Bauer
46a5603ff7 fixed import 2021-07-31 10:32:48 +02:00
Frank Bauer
114574bff3 Added class to help with simple GridLayouts 2021-07-31 10:32:23 +02:00
Frank Bauer
c621b0525e Hello message initiated on Server 2021-07-31 10:32:09 +02:00
Frank Bauer
165c6e5b22 Fixed crash when server initializes a new world 2021-07-31 09:19:46 +02:00
paulevsGitch
49e93e4309 Remove debug info 2021-07-31 09:22:50 +03:00
paulevsGitch
24ce3fa4e8 CTM compat fix 2021-07-31 09:22:21 +03:00
paulevsGitch
05b89607a5 Item parallel model loading 2021-07-30 21:13:05 +03:00
paulevsGitch
185da209ce Parallel model loading 2021-07-30 21:11:55 +03:00
paulevsGitch
b5ebabd824 Another terrain block small fix 2021-07-30 20:45:56 +03:00
paulevsGitch
6a1ab89e39 Terrain block small fixes 2021-07-30 20:44:34 +03:00
paulevsGitch
694b5c54c3
Merge pull request #23 from quiqueck/feature/ComplexMatStages
ComplexMaterial
2021-07-30 19:11:51 +03:00
Frank Bauer
a53c503c4e List all mod versions from server and client 2021-07-30 17:30:19 +02:00
Frank Bauer
032b4187bd Replaced string with constant value 2021-07-30 15:02:06 +02:00
Frank Bauer
0e2f876cd4 add equals and hashCode to ComplexMaterialEntry 2021-07-30 14:52:33 +02:00
Frank Bauer
37348ccb0e enable replacement of predefined types 2021-07-30 14:49:14 +02:00
Frank Bauer
2e624956aa Reenable FixerAPI 2021-07-30 13:49:12 +02:00
paulevsGitch
fdcb5ad03a Model loading changes 2021-07-30 14:41:04 +03:00
Frank Bauer
2f1a4e9518 seperated initDefault into three stages. Allows easier cosumization of wood materials 2021-07-30 10:18:31 +02:00
Frank Bauer
d33187d204 added receipeGroupPrefix otherwise everything will have the end-group 2021-07-30 10:17:53 +02:00
Frank Bauer
dca77a58ae separated different initStages 2021-07-29 12:10:55 +02:00
Frank Bauer
6b3957d04a Fixed Shulker Recipe 2021-07-29 11:56:19 +02:00
Frank Bauer
56bbc93460 Reverted wrong additions to WoodenMaterial 2021-07-29 11:29:29 +02:00
paulevsGitch
eb90792aca
Merge pull request #22 from quiqueck/feature/networking
Feature/networking
2021-07-29 12:03:45 +03:00
Frank Bauer
b74f680191 Added HelloServer handler 2021-07-29 10:57:58 +02:00
Frank Bauer
2260d547dc changes to run on non dedicated servers as well 2021-07-29 10:01:02 +02:00
Frank Bauer
7632c4e498 Send first test-message 2021-07-28 23:52:18 +02:00
Frank Bauer
8c0dfadc82 Merge branch 'main' of github.com:paulevsGitch/BCLib into feature/networking 2021-07-28 23:29:10 +02:00
Frank Bauer
30b7c8043a First network test 2021-07-28 23:28:42 +02:00
paulevsGitch
b10e505ed3
Merge pull request #21 from quiqueck/main
Initialize new worlds as fully patched
2021-07-27 08:51:55 +03:00
Frank Bauer
eda626fe10 Doc update 2021-07-26 21:36:34 +02:00
Frank Bauer
eb8c87468f Initialize new worlds as fully patched 2021-07-26 21:31:17 +02:00
paulevsGitch
8f680ac5db
Merge pull request #20 from quiqueck/main
Added Screen to confirm Patches
2021-07-26 22:00:31 +03:00
Frank Bauer
c4d7035ef2 Move method to client Environment 2021-07-26 18:52:22 +02:00
Frank Bauer
d3db623de5 Call DataFixer in Server Context 2021-07-26 18:50:15 +02:00
Frank Bauer
a247b17e7f Added Screen to confirm Patches 2021-07-26 18:36:20 +02:00
paulevsGitch
f69e839a82
Merge pull request #19 from quiqueck/main
Added Patch-Function for *level.dat *
2021-07-26 13:42:03 +03:00
Frank Bauer
c93c271bd4 Added Patch-Function for *level.dat * 2021-07-26 12:27:44 +02:00
paulevsGitch
f853ac75d9 Model transform 2021-07-24 21:30:48 +03:00
paulevsGitch
1ea7221f9f Small fix 2021-07-24 21:08:20 +03:00
paulevsGitch
a6acb67428 Multi material fix 2021-07-24 21:01:46 +03:00
paulevsGitch
d350425049 Small fix 2021-07-24 20:03:11 +03:00
paulevsGitch
4a6d618598 OBJ model offset 2021-07-24 20:02:25 +03:00
paulevsGitch
5efab22561 Culling fix & null pointer fix 2021-07-24 18:52:22 +03:00
paulevsGitch
ca9e32ae53 OBJ model enhancements and builder 2021-07-24 18:20:29 +03:00
paulevsGitch
099ecf68b6 OBJ model prototype 2021-07-24 17:05:03 +03:00
paulevsGitch
c8d9d9b252 Wooden material rename, javadoc fix, recipe entries 2021-07-24 00:59:22 +03:00
paulevsGitch
4df19c2193 Readme update 2021-07-23 22:06:37 +03:00
paulevsGitch
863562ac87 Complex material javadocs 2021-07-23 21:56:10 +03:00
paulevsGitch
0e6fa962de Material ID 2021-07-23 19:24:59 +03:00
paulevsGitch
0c73d69a93 Entites fixes 2021-07-23 18:35:13 +03:00
paulevsGitch
ec61f22682 Small fixes 2021-07-23 17:54:42 +03:00
paulevsGitch
65aae74979 Small null fix 2021-07-23 17:34:55 +03:00
paulevsGitch
806992759a Complex material init changes 2021-07-23 17:26:02 +03:00
paulevsGitch
eb18fa63af Complex material fixes 2021-07-23 15:03:16 +03:00
paulevsGitch
b661aedd60 Recipes, fixes 2021-07-23 14:24:02 +03:00
paulevsGitch
ddbdc0207d Wooden material tags 2021-07-23 14:13:36 +03:00
paulevsGitch
edf9e1d004 Material init 2021-07-23 13:41:54 +03:00
paulevsGitch
0eaaae2f99 Complex materials, wood material (WIP) 2021-07-23 13:40:37 +03:00
paulevsGitch
eb4f70b8a2
Merge pull request #18 from quiqueck/main
Datafixer update, WoodMaterial and BuildSystem improvements
2021-07-23 10:25:59 +03:00
Frank
a998c9fbe7 Merge branch 'subbuild' into main 2021-07-22 23:29:21 +02:00
paulevsGitch
8dbce734d5 Small format fixes, javadocs 2021-07-23 00:07:47 +03:00
Frank
6385480e9e Added first draft of WoodenMaterial (from BetterEnd) 2021-07-22 23:05:51 +02:00
Frank
080bd36714 Added first draft of WoodenMaterial (from BetterEnd) 2021-07-22 23:05:37 +02:00
Frank
bd77ea46b9 Merge branch 'main' into subbuild 2021-07-22 23:01:43 +02:00
Frank
3836e1f81f Merge branch 'main' of github.com:paulevsGitch/BCLib into main 2021-07-22 23:01:34 +02:00
paulevsGitch
7be63c814d Small formatting fixes 2021-07-22 23:57:32 +03:00
paulevsGitch
ceeb36fc2a Translation Helper fixes 2021-07-22 23:55:28 +03:00
paulevsGitch
551bf6865c Post init API enhancement, automatic render registry (WIP) 2021-07-22 23:38:32 +03:00
paulevsGitch
50ccbace6d Post init API 2021-07-22 23:23:14 +03:00
paulevsGitch
4e9fda44f8 Ladder block vanilla class extend 2021-07-22 23:16:58 +03:00
Frank
d259931477 Merge commit 'd179bd2ba8' into subbuild 2021-07-22 15:26:43 +02:00
Frank
5a56064e86 Moved DataFixer call 2021-07-22 15:22:14 +02:00
Frank
89d548caf9 Start fixer before the world is loaded (otherwise no changes to level.dat are possible) 2021-07-22 15:22:10 +02:00
Frank
9e42fe7294 Handle Player and Level data in Fixer 2021-07-22 15:22:07 +02:00
Frank
c64c6eca7b Fixing Entity Inventory 2021-07-22 15:21:57 +02:00
Frank Bauer
d179bd2ba8 Support fir BCLib as subproject to BN/BE 2021-07-22 08:52:46 +02:00
paulevsGitch
37b85b4cee Base plant edit 2021-07-22 07:20:09 +03:00
paulevsGitch
a6419d96e2 Less fast fog density transition, render code in separate class 2021-07-21 18:40:46 +03:00
paulevsGitch
9878854f6b Small fix for fix 2021-07-21 17:38:45 +03:00
paulevsGitch
f981527200 Feature randomization (fix MC bug) 2021-07-21 17:31:24 +03:00
paulevsGitch
a48c350aa2 Tag name changes, workbenches tag 2021-07-21 13:32:02 +03:00
paulevsGitch
51bff6b2ca Small fix 2021-07-21 13:28:27 +03:00
paulevsGitch
5fbb2e03b5 Torus SDF 2021-07-21 12:27:00 +03:00
paulevsGitch
2bfc985403 Description update 2021-07-21 02:09:57 +03:00
paulevsGitch
cdc9d85b53 Shader enhancements, disable when Optifine is installed 2021-07-20 23:55:40 +03:00
paulevsGitch
e5e948ef4f Moved TagHelper inside TagAPI 2021-07-20 04:54:41 +03:00
paulevsGitch
2e12635e66
Merge pull request #16 from quiqueck/main
Adopted WorldDataAPI for new DataFixerAPI
2021-07-20 03:43:30 +03:00
paulevsGitch
5455034da3 Small changes 2021-07-20 03:39:58 +03:00
paulevsGitch
1dc2fea7e2 Translations 2021-07-20 03:33:52 +03:00
paulevsGitch
450b7d4205 Small drop fix 2021-07-20 03:23:17 +03:00
paulevsGitch
1a4a9ef0a1 Anvil fixes 2021-07-20 02:57:43 +03:00
Frank Bauer
65b70f57ad Merge branch 'main' of github.com:paulevsGitch/BCLib 2021-07-20 00:44:49 +02:00
Frank Bauer
cfa240892f Removed original DataFixerAPI 2021-07-20 00:42:01 +02:00
Frank Bauer
dcb66ee5e0 Better Debug order 2021-07-20 00:37:16 +02:00
Frank Bauer
66851baf0f fixed bug where "WorldDataAPI" would always restore an empty Tag. 2021-07-20 00:36:59 +02:00
Frank Bauer
4ad3ff8277 Using WorldDataAPI to keep track of pacth-level 2021-07-20 00:23:49 +02:00
Frank Bauer
f595cdbbed Register BCLib in the WorldDataAPI 2021-07-20 00:22:45 +02:00
paulevsGitch
cba8637122 Anvil model fixes 2021-07-20 01:03:43 +03:00
paulevsGitch
e50ad6b6aa Imports optimised 2021-07-20 00:41:25 +03:00
Frank Bauer
fa1c58b832 Refactor 2021-07-19 23:20:08 +02:00
paulevsGitch
adadee7ef7 Version change 2021-07-20 00:19:45 +03:00
paulevsGitch
db8dc41c8f Getters interfaces to Providers rename 2021-07-20 00:16:42 +03:00
Frank Bauer
5e2961c91d renamed default config dir 2021-07-19 23:11:26 +02:00
paulevsGitch
c6afa74529 Code style fix, interface rename, custom item getter 2021-07-20 00:10:00 +03:00
Frank Bauer
5cc430de6f Fixed regex api use 2021-07-19 23:03:43 +02:00
Frank Bauer
9728dc8dc3 Use version String for DataFixer 2021-07-19 22:38:27 +02:00
paulevsGitch
179ada3296 Code style fix & version update 2021-07-19 21:41:07 +03:00
paulevsGitch
6266b30088
Merge pull request #14 from quiqueck/feature/specialBlocksRegistration
Added method to register special blocks to correct registries
2021-07-19 21:33:49 +03:00
paulevsGitch
c677787f91
Merge pull request #15 from quiqueck/feature/PatchTools
Redesigned `DataFixerAPI`-Management
2021-07-19 21:33:42 +03:00
paulevsGitch
c39eca3bd4
Merge pull request #13 from quiqueck/hashFix
Fixed hash-calculation for `ConfigKey`
2021-07-19 21:33:36 +03:00
Frank Bauer
5d2bb2c66c Call new DataFixer-API 2021-07-19 19:54:37 +02:00
Frank Bauer
51235429db Added alternative DataFixerAPI 2021-07-19 19:54:37 +02:00
Frank Bauer
102c9ec0cc Added a SessionConfig class that can generate config files inside a world folder 2021-07-19 19:54:16 +02:00
Frank Bauer
fee7cbc93a Added possibility to store configs at costum locations 2021-07-19 19:54:16 +02:00
Frank Bauer
8d5f235684 Fixed hash-calculation for ConfigKey 2021-07-19 19:54:16 +02:00
Frank Bauer
d63aa5b92e Added method to register special blocks to correct registries 2021-07-19 19:51:36 +02:00
Frank Bauer
280c6d5849 Fixed hash-calculation for ConfigKey 2021-07-19 19:24:34 +02:00
paulevsGitch
42d9f7f887 Fixed item entity lights 2021-07-19 01:38:13 +03:00
paulevsGitch
f8b88909cb Item shaders upgrade 2021-07-18 19:51:59 +03:00
paulevsGitch
5c6c4677b0 Shader update 2021-07-18 19:27:04 +03:00
paulevsGitch
5afaf5f324 Deprecated mipped shaders 2021-07-18 17:56:57 +03:00
paulevsGitch
21f33f1fef Version change 2021-07-18 17:30:24 +03:00
paulevsGitch
71935a746a Cutout emission 2021-07-18 17:29:23 +03:00
paulevsGitch
201df3746a Emissive textures support 2021-07-18 17:00:46 +03:00
paulevsGitch
940dd39e29 Test shaders 2021-07-18 14:28:57 +03:00
paulevsGitch
f10aa2776f
Merge pull request #11 from quiqueck/main
Proposal to augment default Block properties
2021-07-18 00:27:07 +03:00
Frank Bauer
f7955e60f0 Propsal for property augmentation 2021-07-17 15:12:23 +02:00
Frank Bauer
5b9eb304bc Custom Stylesheet 2021-07-17 10:27:21 +02:00
Frank Bauer
aa59cb9844 Merge branch 'main' of github.com:paulevsGitch/BCLib 2021-07-17 10:19:18 +02:00
paulevsGitch
036a94e171 Underwater plants fix 2021-07-16 15:35:01 +03:00
paulevsGitch
1609b28595 Bonemeal API update and unification, sub-biome fix 2021-07-16 15:04:42 +03:00
Frank Bauer
bc4ff96f0b JavaDoc for TagHelper 2021-07-14 13:49:38 +02:00
paulevsGitch
d8a620c589 Mining tags 2021-07-12 10:03:18 +03:00
paulevsGitch
0a2b6d9977 Mining tags 2021-07-12 10:03:13 +03:00
paulevsGitch
f4a88c277b Add value operation 2021-07-11 15:12:53 +03:00
Aleksey
8fca5aab41 Blockstates StringProperty 2021-07-11 14:32:13 +03:00
paulevsGitch
cbff862594 Fog rendering skip 2021-07-11 01:32:07 +03:00
paulevsGitch
992d755801 Post init interface 2021-07-11 01:25:11 +03:00
paulevsGitch
cb51137c87 Removed face bakery mixin 2021-07-11 00:41:29 +03:00
paulevsGitch
f1ccd563b5 Transformations 2021-07-11 00:04:37 +03:00
paulevsGitch
7d923ba04e Small fixes 2021-07-10 19:54:19 +03:00
paulevsGitch
e41b59d506 Added resource loading for JSON 2021-07-10 19:47:33 +03:00
paulevsGitch
f4df53b171 Color provider mixin 2021-07-10 16:18:05 +03:00
paulevsGitch
5a9365e2bb Fixed structure features and code style 2021-07-10 16:07:44 +03:00
paulevsGitch
d431f2555c
Merge pull request #10 from quiqueck/main
Fix for jitpack
2021-07-09 14:52:02 +03:00
Frank Bauer
370add478f Force jitpack to use Java16 2021-07-09 09:51:28 +02:00
Frank Bauer
644ea6a171 Merge remote-tracking branch 'upstream/main' 2021-07-09 09:14:51 +02:00
paulevsGitch
8d22f7a037
Merge pull request #9 from paulevsGitch/1.16.5
Merge pull request #4 from paulevsGitch/main
2021-07-07 21:07:54 +03:00
paulevsGitch
81eca6fe89
Merge pull request #8 from quiqueck/1.17.1
1.17.1
2021-07-07 20:57:53 +03:00
paulevsGitch
c8dd63136c
Merge pull request #7 from quiqueck/main
Minor fixes
2021-07-07 20:57:43 +03:00
Frank Bauer
1a049d05c4 duplicatesStrategy EXCLUDE will not copy the version number to the built jar 2021-07-07 14:00:37 +02:00
Frank Bauer
4ece50ddb1 duplicatesStrategy EXCLUDE will not copy the version number to the built jar 2021-07-07 14:00:16 +02:00
Frank Bauer
1ebb93d7ab blockStateis null if it can not be placed 2021-07-07 13:36:10 +02:00
Frank Bauer
daeca492ec Dependencies updated for 1.17.1 2021-07-07 13:35:41 +02:00
Frank Bauer
c7b49be379 TagLoader fixes 2021-07-07 13:32:23 +02:00
Frank Bauer
558b1db714 Fixed *Chest* rendering 2021-07-07 13:31:02 +02:00
Frank Bauer
abcf90e0bc Fixed *Sign* rendering 2021-07-07 13:30:42 +02:00
Frank Bauer
827018078f Register tick for *Furnace*-Blocks 2021-07-07 13:30:16 +02:00
Frank Bauer
a53114779a blockStateis null if it can not be placed 2021-07-07 13:29:17 +02:00
Frank Bauer
7dfbe7bfaf HEIGHTMAP_SQUARE has index 17 2021-07-07 13:28:52 +02:00
Frank Bauer
24e16a1850 Merge commit 'befdd2c10a' into 1.17.1 2021-07-07 13:27:01 +02:00
paulevsGitch
a4ff0b0721
Merge pull request #4 from paulevsGitch/main
Update 1.16.5 version
2021-06-21 01:29:15 +03:00
351 changed files with 20478 additions and 4640 deletions

1
.gitignore vendored
View file

@ -27,5 +27,6 @@ bin/
# fabric # fabric
run/ run/
run-client/
output/ output/
*.log *.log

105
README.md
View file

@ -1,33 +1,98 @@
[![](https://jitpack.io/v/paulevsGitch/BCLib.svg)](https://jitpack.io/#paulevsGitch/BCLib) [![](https://jitpack.io/v/paulevsGitch/BCLib.svg)](https://jitpack.io/#paulevsGitch/BCLib)
# BCLib # BCLib
BCLib is a library mod for BetterX team mods, developed for Fabric, MC 1.16.5 BCLib is a library mod for BetterX team mods, developed for Fabric, MC 1.17.1
## Features: ## Features:
### Rendering
* Emissive textures (with _e suffix)
* Can be applied to Solid and Transparent blocks;
* Can be changed/added with resourcepacks;
* Incompatible with Sodium and Canvas (just will be not rendered);
* Incompatible with Iris shaders (Iris without shaders works fine).
* Procedural block and item models (from paterns or from code);
* Block render interfaces.
### API: ### API:
* Simple Mod Integration API; * Simple Mod Integration API:
* Structure Features API; * Get mod inner methods, classes and objects on runtime.
* World Data API; * Structure Features API:
* Bonemeal API; * Sructure Features with automatical registration, Helpers and math stuff.
* Features API; * World Data API:
* Biome API; * World fixers for comfortable migration between mod versions when content was removed;
* Tag API. * Support for Block name changes and Tile Entities (WIP).
* Bonemeal API:
* Add custom spreadable blocks;
* Add custom plants grow with weight, biomes and other checks;
* Custom underwater plants.
* Features API:
* Features with automatical registration, Helpers and math.
* Biome API:
* Biome wrapper around MC biomes;
* Custom biome data storage;
* Custom fog density.
* Tag API:
* Pre-builded set of tags;
* Dynamical tag registration with code;
* Adding blocks and items into tags at runtime.
### Libs: ### Libs:
* Spline library (simple); * Spline library (simple):
* Recipe manager; * Helper to create simple splines as set of points;
* Noise library; * Some basic operation with splines;
* Math library; * Converting splines to SDF.
* SDF library. * Recipe manager:
* Register recipes from code with configs and ingredients check.
* Noise library:
* Voronoi noise and Open Simplex Noise.
* Math library:
* Many basic math functions that are missing in MC.
* SDF library:
* Implementation of Signed Distance Functions;
* Different SDF Operations and Primitives;
* Different materials for SDF Primitives;
* Block post-processing;
* Feature generation using SDF.
### Helpers And Utils: ### Helpers And Utils:
* Custom surface builders; * Custom surface builders.
* Translation helper; * Translation helper:
* Weighted list; * Generates translation template.
* Block helper. * Weighted list:
* A list of objects by weight;
* Weighted Tree:
* Fast approach for big weight structures;
* Block helper:
* Some useful functions to operate with blocks;
### Rendering: ### Complex Materials
* Procedural block models (from paterns or from code); * Utility classes used for mass content generation (wooden blocks, stone blocks, etc.);
* Block render layer interface. * Contains a set of defined blocks, items, recipes and tags;
* Can be modified before mods startup (will add new block type for all instances in all mods);
* All inner blocks and items are Patterned (will have auto-generated models with ability to override them with resource packs or mod resources).
### Pre-Defined Blocks and Items:
* Most basic blocks from MC;
* Automatic item & block model generation;
### Configs:
* Custom config system based on Json;
* Hierarchical configs;
* Different entry types;
* Only-changes saves.
### Interfaces:
* BlockModelProvider:
* Allows block to return custom model and blockstate.
* ItemModelProvider:
* Allows block to return custom item model.
* CustomColorProvider:
* Make available to add block and item color provider.
* RenderLayerProvider:
* Determine block render layer (Transparent and Translucent).
* PostInitable:
* Allows block to init something after all mods are loaded.
* CustomItemProvider:
* Allows block to change its registered item (example - signs, water lilies).
## Importing: ## Importing:
* Clone repo * Clone repo

8
bclib-composit.gradle Normal file
View file

@ -0,0 +1,8 @@
plugins {
id 'idea'
id 'eclipse'
id 'fabric-loom'
id 'maven-publish'
}
apply from: "bclib.gradle"

184
bclib.gradle Normal file
View file

@ -0,0 +1,184 @@
buildscript {
dependencies {
classpath 'org.kohsuke:github-api:1.114'
}
repositories {
gradlePluginPortal()
}
}
sourceCompatibility = JavaVersion.VERSION_16
targetCompatibility = JavaVersion.VERSION_16
archivesBaseName = project.archives_base_name
version = project.mod_version
group = project.maven_group
repositories {
maven { url "https://maven.dblsaiko.net/" }
maven { url "https://server.bbkr.space:8081/artifactory/libs-release/" }
maven { url "https://maven.fabricmc.net/" }
maven { url "https://maven.shedaniel.me/" }
maven { url 'https://maven.blamejared.com' }
maven { url 'https://jitpack.io' }
}
minecraft {
accessWidener = file("src/main/resources/bclib.accesswidener")
}
dependencies {
minecraft "com.mojang:minecraft:${project.minecraft_version}"
mappings minecraft.officialMojangMappings()
modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"
//useApi "vazkii.patchouli:Patchouli:1.16.4-${project.patchouli_version}"
}
def useOptional(String dep) {
dependencies.modRuntime (dep) {
exclude group: 'net.fabricmc.fabric-api'
exclude group: 'net.fabricmc'
if (!dep.contains("me.shedaniel")) {
exclude group: 'me.shedaniel.cloth'
exclude group: 'me.shedaniel'
}
}
dependencies.modCompileOnly (dep) {
exclude group: 'net.fabricmc.fabric-api'
exclude group: 'net.fabricmc'
if (!dep.contains("me.shedaniel")) {
exclude group: 'me.shedaniel.cloth'
exclude group: 'me.shedaniel'
}
}
}
def useApi(String dep) {
dependencies.modApi (dep) {
exclude group: 'net.fabricmc.fabric-api'
exclude group: 'net.fabricmc'
if (!dep.contains("me.shedaniel")) {
exclude group: 'me.shedaniel.cloth'
exclude group: 'me.shedaniel'
}
}
}
processResources {
println "Version: ${project.mod_version}"
inputs.property "version", project.mod_version
// duplicatesStrategy = 'WARN'
// from(sourceSets.main.resources.srcDirs) {
// include "fabric.mod.json"
// expand "version": version
// }
// from(sourceSets.main.resources.srcDirs) {
// exclude "fabric.mod.json"
// }
filesMatching("fabric.mod.json") {
expand "version": project.mod_version
}
}
// ensure that the encoding is set to UTF-8, no matter what the system default is
// this fixes some edge cases with special characters not displaying correctly
// see http://yodaconditions.net/blog/fix-for-java-file-encoding-problems-with-gradle.html
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
it.options.release = 16
}
javadoc {
options.tags = [ "reason" ]
options.stylesheetFile = new File(projectDir, "javadoc.css");
}
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
// Loom will automatically attach sourcesJar to a RemapSourcesJar task and to the "build" task
// if it is present.
// If you remove this task, sources will not be generated.
task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
jar {
from "LICENSE"
}
artifacts {
archives sourcesJar
archives javadocJar
}
def env = System.getenv()
import org.kohsuke.github.GHReleaseBuilder
import org.kohsuke.github.GitHub
task release(dependsOn: [remapJar, sourcesJar, javadocJar]) {
onlyIf {
env.GITHUB_TOKEN
}
doLast {
def github = GitHub.connectUsingOAuth(env.GITHUB_TOKEN as String)
def repository = github.getRepository("paulevsGitch/BCLib")
def releaseBuilder = new GHReleaseBuilder(repository, version as String)
releaseBuilder.name("${archivesBaseName}-${version}")
releaseBuilder.body("A changelog can be found at https://github.com/paulevsGitch/BCLib/commits")
releaseBuilder.commitish("main")
def ghRelease = releaseBuilder.create()
ghRelease.uploadAsset(file("${project.buildDir}/libs/${archivesBaseName}-${version}.jar"), "application/java-archive");
ghRelease.uploadAsset(file("${project.buildDir}/libs/${archivesBaseName}-${version}-sources.jar"), "application/java-archive");
ghRelease.uploadAsset(file("${project.buildDir}/libs/${archivesBaseName}-${version}-javadoc.jar"), "application/java-archive");
}
}
// configure the maven publication
publishing {
publications {
gpr(MavenPublication) {
artifactId archivesBaseName
artifact(remapJar) {
builtBy remapJar
}
artifact(sourcesJar) {
builtBy remapSourcesJar
}
}
}
// select the repositories you want to publish to
repositories {
maven {
name = "GitHubPackages"
url = uri("https://maven.pkg.github.com/paulevsgitch/bclib")
credentials {
username = env.GITHUB_USER
password = env.GITHUB_TOKEN
}
}
}
}
configurations {
dev {
canBeResolved = false
canBeConsumed = true
}
}
artifacts {
dev jar
}

View file

@ -1,164 +1,8 @@
buildscript {
dependencies {
classpath 'org.kohsuke:github-api:1.114'
}
}
plugins { plugins {
id 'idea' id 'idea'
id 'eclipse' id 'eclipse'
id 'fabric-loom' version '0.8-SNAPSHOT' id 'fabric-loom' version "${loom_version}"
id 'maven-publish' id 'maven-publish'
} }
sourceCompatibility = JavaVersion.VERSION_16 apply from: "bclib.gradle"
targetCompatibility = JavaVersion.VERSION_16
archivesBaseName = project.archives_base_name
version = project.mod_version
group = project.maven_group
repositories {
maven { url "https://maven.dblsaiko.net/" }
maven { url "https://server.bbkr.space:8081/artifactory/libs-release/" }
maven { url "https://maven.fabricmc.net/" }
maven { url 'https://maven.blamejared.com' }
maven { url "https://maven.shedaniel.me/" }
maven { url 'https://jitpack.io' }
}
dependencies {
minecraft "com.mojang:minecraft:${project.minecraft_version}"
mappings minecraft.officialMojangMappings()
modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"
//useApi "vazkii.patchouli:Patchouli:1.16.4-${project.patchouli_version}"
}
def useOptional(String dep) {
dependencies.modRuntime (dep) {
exclude group: "net.fabricmc.fabric-api"
exclude group: "net.fabricmc"
if (!dep.contains("me.shedaniel")) {
exclude group: "me.shedaniel"
}
}
dependencies.modCompileOnly (dep) {
exclude group: "net.fabricmc.fabric-api"
exclude group: "net.fabricmc"
if (!dep.contains("me.shedaniel")) {
exclude group: "me.shedaniel"
}
}
}
def useApi(String dep) {
dependencies.modApi (dep) {
exclude group: "net.fabricmc.fabric-api"
exclude group: "net.fabricmc"
if (!dep.contains("me.shedaniel")) {
exclude group: "me.shedaniel"
}
}
}
processResources {
inputs.property "version", project.version
duplicatesStrategy = 'EXCLUDE'
from(sourceSets.main.resources.srcDirs) {
include "fabric.mod.json"
expand "version": project.version
}
from(sourceSets.main.resources.srcDirs) {
exclude "fabric.mod.json"
}
}
// ensure that the encoding is set to UTF-8, no matter what the system default is
// this fixes some edge cases with special characters not displaying correctly
// see http://yodaconditions.net/blog/fix-for-java-file-encoding-problems-with-gradle.html
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}
javadoc {
options.tags = [ "reason" ]
}
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
// Loom will automatically attach sourcesJar to a RemapSourcesJar task and to the "build" task
// if it is present.
// If you remove this task, sources will not be generated.
task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
jar {
from "LICENSE"
}
artifacts {
archives sourcesJar
archives javadocJar
}
def env = System.getenv()
import org.kohsuke.github.GHReleaseBuilder
import org.kohsuke.github.GitHub
task release(dependsOn: [remapJar, sourcesJar, javadocJar]) {
onlyIf {
env.GITHUB_TOKEN
}
doLast {
def github = GitHub.connectUsingOAuth(env.GITHUB_TOKEN as String)
def repository = github.getRepository("paulevsGitch/BCLib")
def releaseBuilder = new GHReleaseBuilder(repository, version as String)
releaseBuilder.name("${archivesBaseName}-${version}")
releaseBuilder.body("A changelog can be found at https://github.com/paulevsGitch/BCLib/commits")
releaseBuilder.commitish("main")
def ghRelease = releaseBuilder.create()
ghRelease.uploadAsset(file("${project.buildDir}/libs/${archivesBaseName}-${version}.jar"), "application/java-archive");
ghRelease.uploadAsset(file("${project.buildDir}/libs/${archivesBaseName}-${version}-sources.jar"), "application/java-archive");
ghRelease.uploadAsset(file("${project.buildDir}/libs/${archivesBaseName}-${version}-javadoc.jar"), "application/java-archive");
}
}
// configure the maven publication
publishing {
publications {
gpr(MavenPublication) {
artifactId archivesBaseName
artifact(remapJar) {
builtBy remapJar
}
artifact(sourcesJar) {
builtBy remapSourcesJar
}
}
}
// select the repositories you want to publish to
repositories {
maven {
name = "GitHubPackages"
url = uri("https://maven.pkg.github.com/paulevsgitch/bclib")
credentials {
username = env.GITHUB_USER
password = env.GITHUB_TOKEN
}
}
}
}

View file

@ -1,18 +1,19 @@
# Done to increase the memory available to gradle. # Done to increase the memory available to gradle.
org.gradle.jvmargs=-Xmx2G org.gradle.jvmargs=-Xmx2G
#Loom
loom_version=0.8-SNAPSHOT
# Fabric Properties # Fabric Properties
# check these on https://fabricmc.net/use # check these on https://fabricmc.net/versions.html
minecraft_version= 1.17 minecraft_version= 1.17.1
yarn_mappings= 6 loader_version= 0.12.4
loader_version= 0.11.6 fabric_version = 0.41.3+1.17
# Mod Properties # Mod Properties
mod_version = 0.2.0 mod_version = 0.5.3
maven_group = ru.bclib maven_group = ru.bclib
archives_base_name = bclib archives_base_name = bclib
# Dependencies # Dependencies
# currently not on the main fabric site, check on the maven: https://maven.fabricmc.net/net/fabricmc/fabric-api/fabric-api patchouli_version = 50-FABRIC
patchouli_version = 50-FABRIC
fabric_version = 0.36.0+1.17

903
javadoc.css Normal file
View file

@ -0,0 +1,903 @@
/*
* Javadoc style sheet
*/
@import url("resources/fonts/dejavu.css");
/*
* Styles for individual HTML elements.
*
* These are styles that are specific to individual HTML elements. Changing them affects the style of a particular
* HTML element throughout the page.
*/
body {
background-color: #ffffff;
color: #353833;
font-family: "DejaVu Sans", Arial, Helvetica, sans-serif;
font-size: 14px;
margin: 0;
padding: 0;
height: 100%;
width: 100%;
}
iframe {
margin: 0;
padding: 0;
height: 100%;
width: 100%;
overflow-y: scroll;
border: none;
}
a:link,
a:visited {
text-decoration: none;
color: #4a6782;
}
a[href]:hover,
a[href]:focus {
text-decoration: none;
color: #bb7a2a;
}
a[name] {
color: #353833;
}
pre {
font-family: "DejaVu Sans Mono", monospace;
font-size: 16px;
background-color: #fffadb;
border-radius: 5px;
}
h1 {
font-size: 20px;
}
h2 {
font-size: 18px;
}
h3 {
font-size: 16px;
}
h4 {
font-size: 13px;
}
h5 {
font-size: 12px;
}
h6 {
font-size: 11px;
}
ul {
list-style-type: disc;
}
code,
tt {
font-family: "DejaVu Sans Mono", monospace;
font-size: 14px;
padding-top: 4px;
margin-top: 8px;
line-height: 1.4em;
}
dt code {
font-family: "DejaVu Sans Mono", monospace;
font-size: 14px;
padding-top: 4px;
}
.summary-table dt code {
font-family: "DejaVu Sans Mono", monospace;
font-size: 14px;
vertical-align: top;
padding-top: 4px;
}
sup {
font-size: 8px;
}
button {
font-family: "DejaVu Sans", Arial, Helvetica, sans-serif;
font-size: 14px;
}
/*
* Styles for HTML generated by javadoc.
*
* These are style classes that are used by the standard doclet to generate HTML documentation.
*/
/*
* Styles for document title and copyright.
*/
.clear {
clear: both;
height: 0;
overflow: hidden;
}
.about-language {
float: right;
padding: 0 21px 8px 8px;
font-size: 11px;
margin-top: -9px;
height: 2.9em;
}
.legal-copy {
margin-left: 0.5em;
}
.tab {
background-color: #0066ff;
color: #ffffff;
padding: 8px;
width: 5em;
font-weight: bold;
}
/*
* Styles for navigation bar.
*/
@media screen {
.flex-box {
position: fixed;
display: flex;
flex-direction: column;
height: 100%;
width: 100%;
}
.flex-header {
flex: 0 0 auto;
}
.flex-content {
flex: 1 1 auto;
overflow-y: auto;
}
}
.top-nav {
background-color: #4d7a97;
color: #ffffff;
float: left;
padding: 0;
width: 100%;
clear: right;
min-height: 2.8em;
padding-top: 10px;
overflow: hidden;
font-size: 12px;
}
.sub-nav {
background-color: #dee3e9;
float: left;
width: 100%;
overflow: hidden;
font-size: 12px;
}
.sub-nav div {
clear: left;
float: left;
padding: 0 0 5px 6px;
text-transform: uppercase;
}
.sub-nav .nav-list {
padding-top: 5px;
}
ul.nav-list {
display: block;
margin: 0 25px 0 0;
padding: 0;
}
ul.sub-nav-list {
float: left;
margin: 0 25px 0 0;
padding: 0;
}
ul.nav-list li {
list-style: none;
float: left;
padding: 5px 6px;
text-transform: uppercase;
}
.sub-nav .nav-list-search {
float: right;
margin: 0 0 0 0;
padding: 5px 6px;
clear: none;
}
.nav-list-search label {
position: relative;
right: -16px;
}
ul.sub-nav-list li {
list-style: none;
float: left;
padding-top: 10px;
}
.top-nav a:link,
.top-nav a:active,
.top-nav a:visited {
color: #ffffff;
text-decoration: none;
text-transform: uppercase;
}
.top-nav a:hover {
text-decoration: none;
color: #bb7a2a;
text-transform: uppercase;
}
.nav-bar-cell1-rev {
background-color: #f8981d;
color: #253441;
margin: auto 5px;
}
.skip-nav {
position: absolute;
top: auto;
left: -9999px;
overflow: hidden;
}
/*
* Hide navigation links and search box in print layout
*/
@media print {
ul.nav-list,
div.sub-nav {
display: none;
}
}
/*
* Styles for page header and footer.
*/
.title {
color: #2c4557;
margin: 10px 0;
}
.sub-title {
margin: 5px 0 0 0;
}
.header ul {
margin: 0 0 15px 0;
padding: 0;
}
.header ul li,
.footer ul li {
list-style: none;
font-size: 13px;
}
/*
* Styles for headings.
*/
body.class-declaration-page .summary h2,
body.class-declaration-page .details h2,
body.class-use-page h2,
body.module-declaration-page .block-list h2 {
font-style: italic;
padding: 0;
margin: 15px 0;
}
body.class-declaration-page .summary h3,
body.class-declaration-page .details h3,
body.class-declaration-page .summary .inherited-list h2 {
background-color: #dee3e9;
border: 1px solid #d0d9e0;
margin: 0 0 6px -8px;
padding: 7px 5px;
}
/*
* Styles for page layout containers.
*/
main {
clear: both;
padding: 10px 20px;
position: relative;
}
dl.notes > dt {
font-family: "DejaVu Sans", Arial, Helvetica, sans-serif;
font-size: 12px;
font-weight: bold;
margin: 10px 0 0 0;
color: #4e4e4e;
}
dl.notes > dd {
margin: 5px 0 10px 0;
font-size: 15px;
font-family: "Roboto", "DejaVu Sans", "Helvetica Neue", Arial, Helvetica, sans-serif;
}
dl.name-value > dt {
margin-left: 1px;
font-size: 1.1em;
display: inline;
font-weight: bold;
}
dl.name-value > dd {
margin: 0 0 0 1px;
font-size: 1.1em;
display: inline;
}
/*
* Styles for lists.
*/
li.circle {
list-style: circle;
}
ul.horizontal li {
display: inline;
font-size: 0.9em;
}
div.inheritance {
margin: 0;
padding: 0;
}
div.inheritance div.inheritance {
margin-left: 2em;
}
ul.block-list,
ul.details-list,
ul.member-list,
ul.summary-list {
margin: 10px 0 10px 0;
padding: 0;
}
ul.block-list > li,
ul.details-list > li,
ul.member-list > li,
ul.summary-list > li {
list-style: none;
margin-bottom: 15px;
line-height: 1.4;
}
.summary-table dl,
.summary-table dl dt,
.summary-table dl dd {
margin-top: 0;
margin-bottom: 1px;
}
/*
* Styles for tables.
*/
.summary-table {
width: 100%;
border-spacing: 0;
border-left: 1px solid #eee;
border-right: 1px solid #eee;
border-bottom: 1px solid #eee;
}
.summary-table {
padding: 0;
}
.caption {
position: relative;
text-align: left;
background-repeat: no-repeat;
color: #253441;
font-weight: bold;
clear: none;
overflow: hidden;
padding: 0px;
padding-top: 10px;
padding-left: 1px;
margin: 0px;
white-space: pre;
}
.caption a:link,
.caption a:visited {
color: #1f389c;
}
.caption a:hover,
.caption a:active {
color: #ffffff;
}
.caption span {
white-space: nowrap;
padding-top: 5px;
padding-left: 12px;
padding-right: 12px;
padding-bottom: 7px;
display: inline-block;
float: left;
background-color: #f8981d;
border: none;
height: 16px;
}
div.table-tabs > button {
border: none;
cursor: pointer;
padding: 5px 12px 7px 12px;
font-weight: bold;
margin-right: 3px;
}
div.table-tabs > button.active-table-tab {
background: #f8981d;
color: #253441;
}
div.table-tabs > button.table-tab {
background: #4d7a97;
color: #ffffff;
}
.two-column-summary {
display: grid;
grid-template-columns: minmax(15%, max-content) minmax(15%, auto);
}
.three-column-summary {
display: grid;
grid-template-columns: minmax(10%, max-content) minmax(15%, max-content) minmax(15%, auto);
}
.four-column-summary {
display: grid;
grid-template-columns: minmax(10%, max-content) minmax(10%, max-content) minmax(10%, max-content) minmax(10%, auto);
}
@media screen and (max-width: 600px) {
.two-column-summary {
display: grid;
grid-template-columns: 1fr;
}
}
@media screen and (max-width: 800px) {
.three-column-summary {
display: grid;
grid-template-columns: minmax(10%, max-content) minmax(25%, auto);
}
.three-column-summary .col-last {
grid-column-end: span 2;
}
}
@media screen and (max-width: 1000px) {
.four-column-summary {
display: grid;
grid-template-columns: minmax(15%, max-content) minmax(15%, auto);
}
}
.summary-table > div {
text-align: left;
padding: 8px 3px 3px 7px;
}
.col-first,
.col-second,
.col-last,
.col-constructor-name,
.col-deprecated-item-name {
vertical-align: top;
padding-right: 0;
padding-top: 8px;
padding-bottom: 3px;
}
.table-header {
background: #dee3e9;
font-weight: bold;
}
.col-first,
.col-first {
font-size: 13px;
}
.col-second,
.col-second,
.col-last,
.col-constructor-name,
.col-deprecated-item-name,
.col-last {
font-size: 13px;
}
.col-first,
.col-second,
.col-constructor-name {
vertical-align: top;
overflow: auto;
}
.col-last {
white-space: normal;
}
.col-first a:link,
.col-first a:visited,
.col-second a:link,
.col-second a:visited,
.col-first a:link,
.col-first a:visited,
.col-second a:link,
.col-second a:visited,
.col-constructor-name a:link,
.col-constructor-name a:visited,
.col-deprecated-item-name a:link,
.col-deprecated-item-name a:visited,
.constant-values-container a:link,
.constant-values-container a:visited,
.all-classes-container a:link,
.all-classes-container a:visited,
.all-packages-container a:link,
.all-packages-container a:visited {
font-weight: bold;
}
.table-sub-heading-color {
background-color: #eeeeff;
}
.even-row-color,
.even-row-color .table-header {
background-color: #ffffff;
}
.odd-row-color,
.odd-row-color .table-header {
background-color: #eeeeef;
}
/*
* Styles for contents.
*/
.deprecated-content {
margin: 0;
padding: 10px 0;
}
div.block {
font-size: 15px;
font-family: "Roboto", "DejaVu Sans", "Helvetica Neue", Arial, Helvetica, sans-serif;
}
.col-last div {
padding-top: 0;
}
.col-last a {
padding-bottom: 3px;
}
.module-signature,
.package-signature,
.type-signature,
.member-signature {
font-family: "DejaVu Sans Mono", monospace;
font-size: 14px;
margin: 14px 0;
white-space: pre-wrap;
}
.module-signature,
.package-signature,
.type-signature {
margin-top: 0;
}
.member-signature .type-parameters-long,
.member-signature .parameters,
.member-signature .exceptions {
display: inline-block;
vertical-align: top;
white-space: pre;
}
.member-signature .type-parameters {
white-space: normal;
}
/*
* Styles for formatting effect.
*/
.source-line-no {
color: green;
padding: 0 30px 0 0;
}
h1.hidden {
visibility: hidden;
overflow: hidden;
font-size: 10px;
}
.block {
display: block;
margin: 0 10px 5px 0;
color: #474747;
}
.deprecated-label,
.descfrm-type-label,
.implementation-label,
.member-name-label,
.member-name-link,
.module-label-in-package,
.module-label-in-type,
.override-specify-label,
.package-label-in-type,
.package-hierarchy-label,
.type-name-label,
.type-name-link,
.search-tag-link {
font-weight: bold;
}
.deprecation-comment,
.help-footnote,
.interface-name {
font-style: italic;
}
.deprecation-block {
font-size: 14px;
font-family: "DejaVu Serif", Georgia, "Times New Roman", Times, serif;
border-style: solid;
border-width: thin;
border-radius: 10px;
padding: 10px;
margin-bottom: 10px;
margin-right: 10px;
display: inline-block;
}
div.block div.deprecation-comment,
div.block div.block span.emphasized-phrase,
div.block div.block span.interface-name {
font-style: normal;
}
/*
* Styles specific to HTML5 elements.
*/
main,
nav,
header,
footer,
section {
display: block;
}
/*
* Styles for javadoc search.
*/
.ui-autocomplete-category {
font-weight: bold;
font-size: 15px;
padding: 7px 0 7px 3px;
background-color: #4d7a97;
color: #ffffff;
}
.result-item {
font-size: 13px;
}
.ui-autocomplete {
max-height: 85%;
max-width: 65%;
overflow-y: scroll;
overflow-x: scroll;
white-space: nowrap;
box-shadow: 0 3px 6px rgba(0, 0, 0, 0.16), 0 3px 6px rgba(0, 0, 0, 0.23);
}
ul.ui-autocomplete {
position: fixed;
z-index: 999999;
}
ul.ui-autocomplete li {
float: left;
clear: both;
width: 100%;
}
.result-highlight {
font-weight: bold;
}
#search {
background-image: url("resources/glass.png");
background-size: 13px;
background-repeat: no-repeat;
background-position: 2px 3px;
padding-left: 20px;
position: relative;
right: -18px;
width: 400px;
}
#reset {
background-color: rgb(255, 255, 255);
background-image: url("resources/x.png");
background-position: center;
background-repeat: no-repeat;
background-size: 12px;
border: 0 none;
width: 16px;
height: 16px;
position: relative;
left: -4px;
top: -4px;
font-size: 0px;
}
.watermark {
color: #545454;
}
.search-tag-desc-result {
font-style: italic;
font-size: 11px;
}
.search-tag-holder-result {
font-style: italic;
font-size: 12px;
}
.search-tag-result:target {
background-color: yellow;
}
.module-graph span {
display: none;
position: absolute;
}
.module-graph:hover span {
display: block;
margin: -100px 0 0 100px;
z-index: 1;
}
.inherited-list {
margin: 10px 0 10px 0;
}
section.description {
line-height: 1.4;
}
.summary section[class$="-summary"],
.details section[class$="-details"],
.class-uses .detail,
.serialized-class-details {
padding: 0px 20px 5px 10px;
border: 1px solid #ededed;
background-color: #f8f8f8;
}
.inherited-list,
section[class$="-details"] .detail {
padding: 0 0 5px 8px;
background-color: #ffffff;
border: none;
}
.vertical-separator {
padding: 0 5px;
}
ul.help-section-list {
margin: 0;
}
/*
* Indicator icon for external links.
*/
main a[href*="://"]::after
{
content: "";
display: inline-block;
background-image: url('data:image/svg+xml; utf8, \
<svg xmlns="http://www.w3.org/2000/svg" width="768" height="768">\
<path d="M584 664H104V184h216V80H0v688h688V448H584zM384 0l132 \
132-240 240 120 120 240-240 132 132V0z" fill="%234a6782"/>\
</svg>');
background-size: 100% 100%;
width: 7px;
height: 7px;
margin-left: 2px;
margin-bottom: 4px;
}
main a[href*="://"]:hover::after,
main a[href*="://"]:focus::after
{
background-image: url('data:image/svg+xml; utf8, \
<svg xmlns="http://www.w3.org/2000/svg" width="768" height="768">\
<path d="M584 664H104V184h216V80H0v688h688V448H584zM384 0l132 \
132-240 240 120 120 240-240 132 132V0z" fill="%23bb7a2a"/>\
</svg>');
}
/*
* Styles for user-provided tables.
*
* borderless:
* No borders, vertical margins, styled caption.
* This style is provided for use with existing doc comments.
* In general, borderless tables should not be used for layout purposes.
*
* plain:
* Plain borders around table and cells, vertical margins, styled caption.
* Best for small tables or for complex tables for tables with cells that span
* rows and columns, when the "striped" style does not work well.
*
* striped:
* Borders around the table and vertical borders between cells, striped rows,
* vertical margins, styled caption.
* Best for tables that have a header row, and a body containing a series of simple rows.
*/
table.borderless,
table.plain,
table.striped {
margin-top: 10px;
margin-bottom: 10px;
}
table.borderless > caption,
table.plain > caption,
table.striped > caption {
font-weight: bold;
font-size: smaller;
}
table.borderless th,
table.borderless td,
table.plain th,
table.plain td,
table.striped th,
table.striped td {
padding: 2px 5px;
}
table.borderless,
table.borderless > thead > tr > th,
table.borderless > tbody > tr > th,
table.borderless > tr > th,
table.borderless > thead > tr > td,
table.borderless > tbody > tr > td,
table.borderless > tr > td {
border: none;
}
table.borderless > thead > tr,
table.borderless > tbody > tr,
table.borderless > tr {
background-color: transparent;
}
table.plain {
border-collapse: collapse;
border: 1px solid black;
}
table.plain > thead > tr,
table.plain > tbody tr,
table.plain > tr {
background-color: transparent;
}
table.plain > thead > tr > th,
table.plain > tbody > tr > th,
table.plain > tr > th,
table.plain > thead > tr > td,
table.plain > tbody > tr > td,
table.plain > tr > td {
border: 1px solid black;
}
table.striped {
border-collapse: collapse;
border: 1px solid black;
}
table.striped > thead {
background-color: #e3e3e3;
}
table.striped > thead > tr > th,
table.striped > thead > tr > td {
border: 1px solid black;
}
table.striped > tbody > tr:nth-child(even) {
background-color: #eee;
}
table.striped > tbody > tr:nth-child(odd) {
background-color: #fff;
}
table.striped > tbody > tr > th,
table.striped > tbody > tr > td {
border-left: 1px solid black;
border-right: 1px solid black;
}
table.striped > tbody > tr > th {
font-weight: normal;
}
/**
* Tweak font sizes and paddings for small screens.
*/
@media screen and (max-width: 1050px) {
#search {
width: 300px;
}
}
@media screen and (max-width: 800px) {
#search {
width: 200px;
}
.top-nav,
.bottom-nav {
font-size: 11px;
padding-top: 6px;
}
.sub-nav {
font-size: 11px;
}
.about-language {
padding-right: 16px;
}
ul.nav-list li,
.sub-nav .nav-list-search {
padding: 6px;
}
ul.sub-nav-list li {
padding-top: 5px;
}
main {
padding: 10px;
}
.summary section[class$="-summary"],
.details section[class$="-details"],
.class-uses .detail,
.serialized-class-details {
padding: 0 8px 5px 8px;
}
body {
-webkit-text-size-adjust: none;
}
}
@media screen and (max-width: 500px) {
#search {
width: 150px;
}
.top-nav,
.bottom-nav {
font-size: 10px;
}
.sub-nav {
font-size: 10px;
}
.about-language {
font-size: 10px;
padding-right: 12px;
}
}

6
jitpack.yml Normal file
View file

@ -0,0 +1,6 @@
# From https://github.com/jitpack/jitpack.io/issues/4506#issuecomment-864562270
before_install:
- source "$HOME/.sdkman/bin/sdkman-init.sh"
- sdk update
- sdk install java 16.0.1.hs-adpt
- sdk use java 16.0.1.hs-adpt

View file

@ -0,0 +1,11 @@
package com.terraformersmc.modmenu.util;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
//This File was included from TerraformersMC/ModMenu
// to enable a ModMenu-Integration without having to
// compile against the actual plugin
@Environment(EnvType.CLIENT)
public interface ModMenuApiMarker {}

View file

@ -0,0 +1,11 @@
package org.anti_ad.mc.ipn.api;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
// Included from "Inventory Profiles Next" (https://github.com/blackd/Inventory-Profiles)
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface IPNIgnore {}

View file

@ -5,13 +5,25 @@ import net.fabricmc.api.ModInitializer;
import net.fabricmc.loader.api.FabricLoader; import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import ru.bclib.api.TagAPI; import ru.bclib.api.TagAPI;
import ru.bclib.api.WorldDataAPI;
import ru.bclib.api.dataexchange.DataExchangeAPI;
import ru.bclib.api.dataexchange.handler.autosync.Chunker;
import ru.bclib.api.dataexchange.handler.autosync.HelloClient;
import ru.bclib.api.dataexchange.handler.autosync.HelloServer;
import ru.bclib.api.dataexchange.handler.autosync.RequestFiles;
import ru.bclib.api.dataexchange.handler.autosync.SendFiles;
import ru.bclib.config.Configs; import ru.bclib.config.Configs;
import ru.bclib.recipes.CraftingRecipes; import ru.bclib.recipes.CraftingRecipes;
import ru.bclib.registry.BaseBlockEntities; import ru.bclib.registry.BaseBlockEntities;
import ru.bclib.registry.BaseRegistry; import ru.bclib.registry.BaseRegistry;
import ru.bclib.util.Logger; import ru.bclib.util.Logger;
import ru.bclib.world.generator.BCLibEndBiomeSource;
import ru.bclib.world.generator.BCLibNetherBiomeSource;
import ru.bclib.world.generator.GeneratorOptions;
import ru.bclib.world.surface.BCLSurfaceBuilders; import ru.bclib.world.surface.BCLSurfaceBuilders;
import java.util.List;
public class BCLib implements ModInitializer { public class BCLib implements ModInitializer {
public static final String MOD_ID = "bclib"; public static final String MOD_ID = "bclib";
public static final Logger LOGGER = new Logger(MOD_ID); public static final Logger LOGGER = new Logger(MOD_ID);
@ -19,10 +31,25 @@ public class BCLib implements ModInitializer {
@Override @Override
public void onInitialize() { public void onInitialize() {
BaseRegistry.register(); BaseRegistry.register();
GeneratorOptions.init();
BaseBlockEntities.register(); BaseBlockEntities.register();
BCLSurfaceBuilders.register(); BCLSurfaceBuilders.register();
BCLibEndBiomeSource.register();
BCLibNetherBiomeSource.register();
TagAPI.init(); TagAPI.init();
CraftingRecipes.init(); CraftingRecipes.init();
WorldDataAPI.registerModCache(MOD_ID);
DataExchangeAPI.registerMod(MOD_ID);
DataExchangeAPI.registerDescriptors(List.of(
HelloClient.DESCRIPTOR,
HelloServer.DESCRIPTOR,
RequestFiles.DESCRIPTOR,
SendFiles.DESCRIPTOR,
Chunker.DESCRIPTOR
));
BCLibPatch.register();
Configs.save(); Configs.save();
} }
@ -33,7 +60,7 @@ public class BCLib implements ModInitializer {
public static boolean isClient() { public static boolean isClient() {
return FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT; return FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT;
} }
public static ResourceLocation makeID(String path) { public static ResourceLocation makeID(String path) {
return new ResourceLocation(MOD_ID, path); return new ResourceLocation(MOD_ID, path);
} }

View file

@ -0,0 +1,82 @@
package ru.bclib;
import net.minecraft.nbt.CompoundTag;
import ru.bclib.api.datafixer.DataFixerAPI;
import ru.bclib.api.datafixer.Patch;
import ru.bclib.api.datafixer.PatchFunction;
public final class BCLibPatch {
public static void register(){
DataFixerAPI.registerPatch(BiomeSourcePatch::new);
}
}
final class BiomeSourcePatch extends Patch {
private static final String NETHER_BIOME_SOURCE = "bclib:nether_biome_source";
private static final String END_BIOME_SOURCE = "bclib:end_biome_source";
protected BiomeSourcePatch() {
super(BCLib.MOD_ID, "0.4.0");
}
public PatchFunction<CompoundTag, Boolean> getLevelDatPatcher() {
return (root, profile) -> {
CompoundTag worldGenSettings = root.getCompound("Data").getCompound("WorldGenSettings");
CompoundTag dimensions = worldGenSettings.getCompound("dimensions");
long seed = worldGenSettings.getLong("seed");
boolean result = false;
if (dimensions.contains("minecraft:the_nether")) {
CompoundTag dimRoot = dimensions.getCompound("minecraft:the_nether");
CompoundTag biomeSource = dimRoot.getCompound("generator").getCompound("biome_source");
if (!biomeSource.getString("type").equals(NETHER_BIOME_SOURCE)) {
BCLib.LOGGER.info("Applying Nether biome source patch");
dimRoot.put("generator", makeNetherGenerator(seed));
result = true;
}
}
if (dimensions.contains("minecraft:the_end")) {
CompoundTag dimRoot = dimensions.getCompound("minecraft:the_end");
CompoundTag biomeSource = dimRoot.getCompound("generator").getCompound("biome_source");
if (!biomeSource.getString("type").equals(END_BIOME_SOURCE)) {
BCLib.LOGGER.info("Applying End biome source patch");
dimRoot.put("generator", makeEndGenerator(seed));
result = true;
}
}
return result;
};
}
private CompoundTag makeNetherGenerator(long seed) {
CompoundTag generator = new CompoundTag();
generator.putString("type", "minecraft:noise");
generator.putString("settings", "minecraft:nether");
generator.putLong("seed", seed);
CompoundTag biomeSource = new CompoundTag();
biomeSource.putString("type", NETHER_BIOME_SOURCE);
biomeSource.putLong("seed", seed);
generator.put("biome_source", biomeSource);
return generator;
}
private CompoundTag makeEndGenerator(long seed) {
CompoundTag generator = new CompoundTag();
generator.putString("type", "minecraft:noise");
generator.putString("settings", "minecraft:end");
generator.putLong("seed", seed);
CompoundTag biomeSource = new CompoundTag();
biomeSource.putString("type", END_BIOME_SOURCE);
biomeSource.putLong("seed", seed);
generator.put("biome_source", biomeSource);
return generator;
}
}

View file

@ -1,24 +1,42 @@
package ru.bclib.api; package ru.bclib.api;
import java.util.HashMap; import com.google.common.collect.ImmutableList;
import java.util.Random; import com.google.common.collect.Lists;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import net.fabricmc.api.EnvType; import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment; import net.fabricmc.api.Environment;
import net.fabricmc.fabric.impl.biome.InternalBiomeData; import net.fabricmc.fabric.impl.biome.InternalBiomeData;
import net.fabricmc.fabric.mixin.biome.modification.GenerationSettingsAccessor;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.core.Registry; import net.minecraft.core.Registry;
import net.minecraft.data.BuiltinRegistries; import net.minecraft.data.BuiltinRegistries;
import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer; import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.biome.Biome; import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.biome.Biome.ClimateParameters; import net.minecraft.world.level.biome.Biome.ClimateParameters;
import net.minecraft.world.level.biome.BiomeSource;
import net.minecraft.world.level.biome.Biomes; import net.minecraft.world.level.biome.Biomes;
import net.minecraft.world.level.levelgen.GenerationStep.Decoration;
import net.minecraft.world.level.levelgen.feature.ConfiguredFeature;
import net.minecraft.world.level.levelgen.feature.ConfiguredStructureFeature;
import org.jetbrains.annotations.Nullable;
import ru.bclib.util.MHelper; import ru.bclib.util.MHelper;
import ru.bclib.world.biomes.BCLBiome; import ru.bclib.world.biomes.BCLBiome;
import ru.bclib.world.biomes.FabricBiomesData;
import ru.bclib.world.features.BCLFeature;
import ru.bclib.world.generator.BiomePicker;
import ru.bclib.world.structures.BCLStructureFeature;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Supplier;
public class BiomeAPI { public class BiomeAPI {
/** /**
@ -27,12 +45,33 @@ public class BiomeAPI {
*/ */
public static final BCLBiome EMPTY_BIOME = new BCLBiome(Biomes.THE_VOID.location(), BuiltinRegistries.BIOME.get(Biomes.THE_VOID), 1, 0); public static final BCLBiome EMPTY_BIOME = new BCLBiome(Biomes.THE_VOID.location(), BuiltinRegistries.BIOME.get(Biomes.THE_VOID), 1, 0);
private static final HashMap<ResourceLocation, BCLBiome> ID_MAP = Maps.newHashMap(); public static final BiomePicker NETHER_BIOME_PICKER = new BiomePicker();
private static final HashMap<Biome, BCLBiome> CLIENT = Maps.newHashMap(); public static final BiomePicker END_LAND_BIOME_PICKER = new BiomePicker();
public static final BiomePicker END_VOID_BIOME_PICKER = new BiomePicker();
private static final Map<ResourceLocation, BCLBiome> ID_MAP = Maps.newHashMap();
private static final Map<Biome, BCLBiome> CLIENT = Maps.newHashMap();
private static Registry<Biome> biomeRegistry; private static Registry<Biome> biomeRegistry;
private static final Map<ResourceKey, List<BiConsumer<ResourceLocation, Biome>>> MODIFICATIONS = Maps.newHashMap();
private static final Set<ResourceLocation> MODIFIED_BIOMES = Sets.newHashSet();
public static final BCLBiome NETHER_WASTES_BIOME = registerNetherBiome(getFromRegistry(Biomes.NETHER_WASTES));
public static final BCLBiome CRIMSON_FOREST_BIOME = registerNetherBiome(getFromRegistry(Biomes.CRIMSON_FOREST));
public static final BCLBiome WARPED_FOREST_BIOME = registerNetherBiome(getFromRegistry(Biomes.WARPED_FOREST));
public static final BCLBiome SOUL_SAND_VALLEY_BIOME = registerNetherBiome(getFromRegistry(Biomes.SOUL_SAND_VALLEY));
public static final BCLBiome BASALT_DELTAS_BIOME = registerNetherBiome(getFromRegistry(Biomes.BASALT_DELTAS));
public static final BCLBiome THE_END = registerEndLandBiome(getFromRegistry(Biomes.THE_END));
public static final BCLBiome END_MIDLANDS = registerSubBiome(THE_END, getFromRegistry(Biomes.END_MIDLANDS), 0.5F);
public static final BCLBiome END_HIGHLANDS = registerSubBiome(THE_END, getFromRegistry(Biomes.END_HIGHLANDS), 0.5F);
public static final BCLBiome END_BARRENS = registerEndVoidBiome(getFromRegistry(new ResourceLocation("end_barrens")));
public static final BCLBiome SMALL_END_ISLANDS = registerEndVoidBiome(getFromRegistry(new ResourceLocation("small_end_islands")));
/** /**
* Initialize registry for current server. * Initialize registry for current server.
*
* @param server - {@link MinecraftServer} * @param server - {@link MinecraftServer}
*/ */
public static void initRegistry(MinecraftServer server) { public static void initRegistry(MinecraftServer server) {
@ -40,53 +79,159 @@ public class BiomeAPI {
CLIENT.clear(); CLIENT.clear();
} }
public static void registerBiome(BCLBiome biome) { /**
* Register {@link BCLBiome} instance and its {@link Biome} if necessary.
* @param biome {@link BCLBiome}
* @return {@link BCLBiome}
*/
public static BCLBiome registerBiome(BCLBiome biome) {
if (BuiltinRegistries.BIOME.get(biome.getID()) == null) { if (BuiltinRegistries.BIOME.get(biome.getID()) == null) {
Registry.register(BuiltinRegistries.BIOME, biome.getID(), biome.getBiome()); Registry.register(BuiltinRegistries.BIOME, biome.getID(), biome.getBiome());
} }
ID_MAP.put(biome.getID(), biome); ID_MAP.put(biome.getID(), biome);
return biome;
}
public static BCLBiome registerSubBiome(BCLBiome parent, BCLBiome subBiome) {
registerBiome(subBiome);
parent.addSubBiome(subBiome);
return subBiome;
}
public static BCLBiome registerSubBiome(BCLBiome parent, Biome biome, float chance) {
ResourceKey<Biome> key = BuiltinRegistries.BIOME.getResourceKey(biome).get();
BCLBiome subBiome = new BCLBiome(key.location(), biome, 1, chance);
return registerSubBiome(parent, subBiome);
} }
/** /**
* Adds {@link BCLBiome} to FabricAPI biomes as the Nether biome (with random {@link ClimateParameters}). * Register {@link BCLBiome} instance and its {@link Biome} if necessary.
* @param biome - {@link BCLBiome}. * After that biome will be added to BCLib Nether Biome Generator and into Fabric Biome API.
* @param biome {@link BCLBiome}
* @return {@link BCLBiome}
*/ */
public static void addNetherBiomeToFabricApi(BCLBiome biome) { public static BCLBiome registerNetherBiome(BCLBiome biome) {
ResourceKey<Biome> key = BuiltinRegistries.BIOME.getResourceKey(biome.getBiome()).get(); registerBiome(biome);
Random random = new Random(biome.getID().toString().hashCode()); NETHER_BIOME_PICKER.addBiome(biome);
Random random = new Random(biome.getID().hashCode());
ClimateParameters parameters = new ClimateParameters( ClimateParameters parameters = new ClimateParameters(
MHelper.randRange(-2F, 2F, random), MHelper.randRange(-1.5F, 1.5F, random),
MHelper.randRange(-2F, 2F, random), MHelper.randRange(-1.5F, 1.5F, random),
MHelper.randRange(-2F, 2F, random), MHelper.randRange(-1.5F, 1.5F, random),
MHelper.randRange(-2F, 2F, random), MHelper.randRange(-1.5F, 1.5F, random),
MHelper.randRange(-2F, 2F, random) random.nextFloat()
); );
ResourceKey<Biome> key = BuiltinRegistries.BIOME.getResourceKey(biome.getBiome()).get();
InternalBiomeData.addNetherBiome(key, parameters); InternalBiomeData.addNetherBiome(key, parameters);
return biome;
} }
/** /**
* Adds {@link BCLBiome} to FabricAPI biomes as an End land biome (generating on islands). * Register {@link BCLBiome} instance and its {@link Biome} if necessary.
* @param biome - {@link BCLBiome}. * After that biome will be added to BCLib Nether Biome Generator and into Fabric Biome API.
* @param biome {@link BCLBiome}
* @return {@link BCLBiome}
*/ */
public static void addEndLandBiomeToFabricApi(BCLBiome biome) { public static BCLBiome registerNetherBiome(Biome biome) {
ResourceKey<Biome> key = BuiltinRegistries.BIOME.getResourceKey(biome).get();
BCLBiome bclBiome = new BCLBiome(key.location(), biome, 1, 1);
NETHER_BIOME_PICKER.addBiome(bclBiome);
registerBiome(bclBiome);
return bclBiome;
}
/**
* Register {@link BCLBiome} instance and its {@link Biome} if necessary.
* After that biome will be added to BCLib End Biome Generator and into Fabric Biome API as a land biome (will generate only on islands).
* @param biome {@link BCLBiome}
* @return {@link BCLBiome}
*/
public static BCLBiome registerEndLandBiome(BCLBiome biome) {
registerBiome(biome);
END_LAND_BIOME_PICKER.addBiome(biome);
float weight = biome.getGenChance(); float weight = biome.getGenChance();
ResourceKey<Biome> key = BuiltinRegistries.BIOME.getResourceKey(biome.getBiome()).get(); ResourceKey<Biome> key = BuiltinRegistries.BIOME.getResourceKey(biome.getBiome()).get();
InternalBiomeData.addEndBiomeReplacement(Biomes.END_HIGHLANDS, key, weight); InternalBiomeData.addEndBiomeReplacement(Biomes.END_HIGHLANDS, key, weight);
InternalBiomeData.addEndBiomeReplacement(Biomes.END_MIDLANDS, key, weight); InternalBiomeData.addEndBiomeReplacement(Biomes.END_MIDLANDS, key, weight);
return biome;
} }
/** /**
* Adds {@link BCLBiome} to FabricAPI biomes as an End void biome (generating between islands in the void). * Register {@link BCLBiome} wrapper for {@link Biome}.
* @param biome - {@link BCLBiome}. * After that biome will be added to BCLib End Biome Generator and into Fabric Biome API as a land biome (will generate only on islands).
* @param biome {@link BCLBiome}
* @return {@link BCLBiome}
*/ */
public static void addEndVoidBiomeToFabricApi(BCLBiome biome) { public static BCLBiome registerEndLandBiome(Biome biome) {
ResourceKey<Biome> key = BuiltinRegistries.BIOME.getResourceKey(biome).get();
BCLBiome bclBiome = new BCLBiome(key.location(), biome, 1, 1);
END_LAND_BIOME_PICKER.addBiome(bclBiome);
registerBiome(bclBiome);
return bclBiome;
}
/**
* Register {@link BCLBiome} wrapper for {@link Biome}.
* After that biome will be added to BCLib End Biome Generator and into Fabric Biome API as a land biome (will generate only on islands).
* @param biome {@link BCLBiome};
* @param weight float generation chance.
* @return {@link BCLBiome}
*/
public static BCLBiome registerEndLandBiome(Biome biome, float weight) {
ResourceKey<Biome> key = BuiltinRegistries.BIOME.getResourceKey(biome).get();
BCLBiome bclBiome = new BCLBiome(key.location(), biome, 1, weight);
END_LAND_BIOME_PICKER.addBiome(bclBiome);
registerBiome(bclBiome);
return bclBiome;
}
/**
* Register {@link BCLBiome} instance and its {@link Biome} if necessary.
* After that biome will be added to BCLib End Biome Generator and into Fabric Biome API as a void biome (will generate only in the End void - between islands).
* @param biome {@link BCLBiome}
* @return {@link BCLBiome}
*/
public static BCLBiome registerEndVoidBiome(BCLBiome biome) {
registerBiome(biome);
END_VOID_BIOME_PICKER.addBiome(biome);
float weight = biome.getGenChance(); float weight = biome.getGenChance();
ResourceKey<Biome> key = BuiltinRegistries.BIOME.getResourceKey(biome.getBiome()).get(); ResourceKey<Biome> key = BuiltinRegistries.BIOME.getResourceKey(biome.getBiome()).get();
InternalBiomeData.addEndBiomeReplacement(Biomes.SMALL_END_ISLANDS, key, weight); InternalBiomeData.addEndBiomeReplacement(Biomes.SMALL_END_ISLANDS, key, weight);
return biome;
}
/**
* Register {@link BCLBiome} instance and its {@link Biome} if necessary.
* After that biome will be added to BCLib End Biome Generator and into Fabric Biome API as a void biome (will generate only in the End void - between islands).
* @param biome {@link BCLBiome}
* @return {@link BCLBiome}
*/
public static BCLBiome registerEndVoidBiome(Biome biome) {
ResourceKey<Biome> key = BuiltinRegistries.BIOME.getResourceKey(biome).get();
BCLBiome bclBiome = new BCLBiome(key.location(), biome, 1, 1);
END_VOID_BIOME_PICKER.addBiome(bclBiome);
registerBiome(bclBiome);
return bclBiome;
}
/**
* Register {@link BCLBiome} instance and its {@link Biome} if necessary.
* After that biome will be added to BCLib End Biome Generator and into Fabric Biome API as a void biome (will generate only in the End void - between islands).
* @param biome {@link BCLBiome};
* @param weight float generation chance.
* @return {@link BCLBiome}
*/
public static BCLBiome registerEndVoidBiome(Biome biome, float weight) {
ResourceKey<Biome> key = BuiltinRegistries.BIOME.getResourceKey(biome).get();
BCLBiome bclBiome = new BCLBiome(key.location(), biome, 1, weight);
END_VOID_BIOME_PICKER.addBiome(bclBiome);
registerBiome(bclBiome);
return bclBiome;
} }
/** /**
* Get {@link BCLBiome} from {@link Biome} instance on server. Used to convert world biomes to BCLBiomes. * Get {@link BCLBiome} from {@link Biome} instance on server. Used to convert world biomes to BCLBiomes.
*
* @param biome - {@link Biome} from world. * @param biome - {@link Biome} from world.
* @return {@link BCLBiome} or {@code BiomeAPI.EMPTY_BIOME}. * @return {@link BCLBiome} or {@code BiomeAPI.EMPTY_BIOME}.
*/ */
@ -99,6 +244,7 @@ public class BiomeAPI {
/** /**
* Get {@link BCLBiome} from biome on client. Used in fog rendering. * Get {@link BCLBiome} from biome on client. Used in fog rendering.
*
* @param biome - {@link Biome} from client world. * @param biome - {@link Biome} from client world.
* @return {@link BCLBiome} or {@code BiomeAPI.EMPTY_BIOME}. * @return {@link BCLBiome} or {@code BiomeAPI.EMPTY_BIOME}.
*/ */
@ -116,6 +262,7 @@ public class BiomeAPI {
/** /**
* Get biome {@link ResourceLocation} from given {@link Biome}. * Get biome {@link ResourceLocation} from given {@link Biome}.
*
* @param biome - {@link Biome} from server world. * @param biome - {@link Biome} from server world.
* @return biome {@link ResourceLocation}. * @return biome {@link ResourceLocation}.
*/ */
@ -123,9 +270,10 @@ public class BiomeAPI {
ResourceLocation id = biomeRegistry.getKey(biome); ResourceLocation id = biomeRegistry.getKey(biome);
return id == null ? EMPTY_BIOME.getID() : id; return id == null ? EMPTY_BIOME.getID() : id;
} }
/** /**
* Get {@link BCLBiome} from given {@link ResourceLocation}. * Get {@link BCLBiome} from given {@link ResourceLocation}.
*
* @param biomeID - biome {@link ResourceLocation}. * @param biomeID - biome {@link ResourceLocation}.
* @return {@link BCLBiome} or {@code BiomeAPI.EMPTY_BIOME}. * @return {@link BCLBiome} or {@code BiomeAPI.EMPTY_BIOME}.
*/ */
@ -133,26 +281,207 @@ public class BiomeAPI {
return ID_MAP.getOrDefault(biomeID, EMPTY_BIOME); return ID_MAP.getOrDefault(biomeID, EMPTY_BIOME);
} }
/**
* Get actual {@link Biome} from given {@link BCLBiome}. If it is null it will request it from current {@link Registry}.
* @param biome - {@link BCLBiome}.
* @return {@link Biome}.
*/
public static Biome getActualBiome(BCLBiome biome) {
Biome actual = biome.getActualBiome();
if (actual == null) {
biome.updateActualBiomes(biomeRegistry);
actual = biome.getActualBiome();
}
return actual;
}
/** /**
* Check if biome with {@link ResourceLocation} exists in API registry. * Check if biome with {@link ResourceLocation} exists in API registry.
*
* @param biomeID - biome {@link ResourceLocation}. * @param biomeID - biome {@link ResourceLocation}.
* @return {@code true} if biome exists in API registry and {@code false} if not. * @return {@code true} if biome exists in API registry and {@code false} if not.
*/ */
public static boolean hasBiome(ResourceLocation biomeID) { public static boolean hasBiome(ResourceLocation biomeID) {
return ID_MAP.containsKey(biomeID); return ID_MAP.containsKey(biomeID);
} }
/**
* Load biomes from Fabric API. For internal usage only.
*/
public static void loadFabricAPIBiomes() {
FabricBiomesData.NETHER_BIOMES.forEach((key) -> {
if (!hasBiome(key.location())) {
registerNetherBiome(BuiltinRegistries.BIOME.get(key.location()));
}
});
FabricBiomesData.END_LAND_BIOMES.forEach((key, weight) -> {
if (!hasBiome(key.location())) {
registerEndLandBiome(BuiltinRegistries.BIOME.get(key.location()), weight);
}
});
FabricBiomesData.END_VOID_BIOMES.forEach((key, weight) -> {
if (!hasBiome(key.location())) {
registerEndVoidBiome(BuiltinRegistries.BIOME.get(key.location()), weight);
}
});
}
@Nullable
public static Biome getFromRegistry(ResourceLocation key) {
return BuiltinRegistries.BIOME.get(key);
}
@Nullable
public static Biome getFromRegistry(ResourceKey<Biome> key) {
return BuiltinRegistries.BIOME.get(key);
}
public static boolean isDatapackBiome(ResourceLocation biomeID) {
return getFromRegistry(biomeID) == null;
}
public static boolean isNetherBiome(ResourceLocation biomeID) {
return pickerHasBiome(NETHER_BIOME_PICKER, biomeID);
}
public static boolean isEndBiome(ResourceLocation biomeID) {
return pickerHasBiome(END_LAND_BIOME_PICKER, biomeID) || pickerHasBiome(END_VOID_BIOME_PICKER, biomeID);
}
private static boolean pickerHasBiome(BiomePicker picker, ResourceLocation key) {
return picker.getBiomes().stream().filter(biome -> biome.getID().equals(key)).findFirst().isPresent();
}
/**
* Registers new biome modification for specified dimension. Will work both for mod and datapack biomes.
* @param dimensionID {@link ResourceLocation} dimension ID, example: Level.OVERWORLD or "minecraft:overworld".
* @param modification {@link BiConsumer} with {@link ResourceKey} biome ID and {@link Biome} parameters.
*/
public static void registerBiomeModification(ResourceKey dimensionID, BiConsumer<ResourceLocation, Biome> modification) {
List<BiConsumer<ResourceLocation, Biome>> modifications = MODIFICATIONS.get(dimensionID);
if (modifications == null) {
modifications = Lists.newArrayList();
MODIFICATIONS.put(dimensionID, modifications);
}
modifications.add(modification);
}
/**
* Registers new biome modification for the Overworld. Will work both for mod and datapack biomes.
* @param modification {@link BiConsumer} with {@link ResourceLocation} biome ID and {@link Biome} parameters.
*/
public static void registerOverworldBiomeModification(BiConsumer<ResourceLocation, Biome> modification) {
registerBiomeModification(Level.OVERWORLD, modification);
}
/**
* Registers new biome modification for the Nether. Will work both for mod and datapack biomes.
* @param modification {@link BiConsumer} with {@link ResourceLocation} biome ID and {@link Biome} parameters.
*/
public static void registerNetherBiomeModification(BiConsumer<ResourceLocation, Biome> modification) {
registerBiomeModification(Level.NETHER, modification);
}
/**
* Registers new biome modification for the End. Will work both for mod and datapack biomes.
* @param modification {@link BiConsumer} with {@link ResourceLocation} biome ID and {@link Biome} parameters.
*/
public static void registerEndBiomeModification(BiConsumer<ResourceLocation, Biome> modification) {
registerBiomeModification(Level.END, modification);
}
/**
* Will apply biome modifications to world, internal usage only.
* @param level
*/
public static void applyModifications(ServerLevel level) {
List<BiConsumer<ResourceLocation, Biome>> modifications = MODIFICATIONS.get(level.dimension());
if (modifications == null) {
return;
}
BiomeSource source = level.getChunkSource().getGenerator().getBiomeSource();
List<Biome> biomes = source.possibleBiomes();
biomes.forEach(biome -> {
ResourceLocation biomeID = getBiomeID(biome);
boolean modify = isDatapackBiome(biomeID);
if (!modify && !MODIFIED_BIOMES.contains(biomeID)) {
MODIFIED_BIOMES.add(biomeID);
modify = true;
}
if (modify) {
modifications.forEach(consumer -> {
consumer.accept(biomeID, biome);
});
}
});
}
/**
* Adds new features to existing biome.
* @param biome {@link Biome} to add features in.
* @param feature {@link ConfiguredFeature} to add.
* @param step a {@link Decoration} step for the feature.
*/
public static void addBiomeFeature(Biome biome, ConfiguredFeature feature, Decoration step) {
GenerationSettingsAccessor accessor = (GenerationSettingsAccessor) biome.getGenerationSettings();
List<List<Supplier<ConfiguredFeature<?, ?>>>> biomeFeatures = getMutableList(accessor.fabric_getFeatures());
List<Supplier<ConfiguredFeature<?, ?>>> list = getList(step, biomeFeatures);
list.add(() -> feature);
accessor.fabric_setFeatures(biomeFeatures);
}
/**
* Adds new features to existing biome.
* @param biome {@link Biome} to add features in.
* @param features array of {@link BCLFeature} to add.
*/
public static void addBiomeFeatures(Biome biome, BCLFeature... features) {
GenerationSettingsAccessor accessor = (GenerationSettingsAccessor) biome.getGenerationSettings();
List<List<Supplier<ConfiguredFeature<?, ?>>>> biomeFeatures = getMutableList(accessor.fabric_getFeatures());
for (BCLFeature feature: features) {
List<Supplier<ConfiguredFeature<?, ?>>> list = getList(feature.getFeatureStep(), biomeFeatures);
list.add(feature::getFeatureConfigured);
}
accessor.fabric_setFeatures(biomeFeatures);
}
/**
* Getter for correct feature list from all biome feature list of lists.
* @param step feature {@link Decoration} step.
* @param lists biome accessor lists.
* @return mutable {@link ConfiguredFeature} list.
*/
private static List<Supplier<ConfiguredFeature<?, ?>>> getList(Decoration step, List<List<Supplier<ConfiguredFeature<?, ?>>>> lists) {
int index = step.ordinal();
if (lists.size() <= index) {
for (int i = lists.size(); i <= index; i++) {
lists.add(Lists.newArrayList());
}
}
List<Supplier<ConfiguredFeature<?, ?>>> list = getMutableList(lists.get(index));
lists.set(index, list);
return list;
}
/**
* Adds new structure feature to existing biome.
* @param biome {@link Biome} to add structure feature in.
* @param structure {@link ConfiguredStructureFeature} to add.
*/
public static void addBiomeStructure(Biome biome, ConfiguredStructureFeature structure) {
GenerationSettingsAccessor accessor = (GenerationSettingsAccessor) biome.getGenerationSettings();
List<Supplier<ConfiguredStructureFeature<?, ?>>> biomeStructures = getMutableList(accessor.fabric_getStructureFeatures());
biomeStructures.add(() -> structure);
accessor.fabric_setStructureFeatures(biomeStructures);
}
/**
* Adds new structure features to existing biome.
* @param biome {@link Biome} to add structure features in.
* @param structures array of {@link BCLStructureFeature} to add.
*/
public static void addBiomeStructures(Biome biome, BCLStructureFeature... structures) {
GenerationSettingsAccessor accessor = (GenerationSettingsAccessor) biome.getGenerationSettings();
List<Supplier<ConfiguredStructureFeature<?, ?>>> biomeStructures = getMutableList(accessor.fabric_getStructureFeatures());
for (BCLStructureFeature structure: structures) {
biomeStructures.add(structure::getFeatureConfigured);
}
accessor.fabric_setStructureFeatures(biomeStructures);
}
private static <T extends Object> List<T> getMutableList(List<T> input) {
if (input instanceof ImmutableList) {
return Lists.newArrayList(input);
}
return input;
}
} }

View file

@ -1,53 +1,65 @@
package ru.bclib.api; package ru.bclib.api;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.Block;
import ru.bclib.util.WeightedList;
import java.util.Map; import java.util.Map;
import java.util.Random; import java.util.Random;
import java.util.Set; import java.util.Set;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.Block;
import ru.bclib.util.WeightedList;
public class BonemealAPI { public class BonemealAPI {
private static final Map<ResourceLocation, Map<Block, WeightedList<Block>>> WATER_GRASS_BIOMES = Maps.newHashMap(); private static final Map<ResourceLocation, Map<Block, WeightedList<Block>>> WATER_GRASS_BIOMES = Maps.newHashMap();
private static final Map<ResourceLocation, Map<Block, WeightedList<Block>>> LAND_GRASS_BIOMES = Maps.newHashMap(); private static final Map<ResourceLocation, Map<Block, WeightedList<Block>>> LAND_GRASS_BIOMES = Maps.newHashMap();
private static final Map<Block, WeightedList<Block>> WATER_GRASS_TYPES = Maps.newHashMap(); private static final Map<Block, WeightedList<Block>> WATER_GRASS_TYPES = Maps.newHashMap();
private static final Map<Block, WeightedList<Block>> LAND_GRASS_TYPES = Maps.newHashMap(); private static final Map<Block, WeightedList<Block>> LAND_GRASS_TYPES = Maps.newHashMap();
private static final Set<Block> SPREADABLE_BLOCKS = Sets.newHashSet(); private static final Map<Block, Block> SPREADABLE_BLOCKS = Maps.newHashMap();
private static final Set<Block> TERRAIN_TO_SPREAD = Sets.newHashSet();
private static final Set<Block> TERRAIN = Sets.newHashSet();
public static void addSpreadableBlock(Block block) { public static void addSpreadableBlock(Block spreadableBlock, Block surfaceForSpread) {
SPREADABLE_BLOCKS.add(block); SPREADABLE_BLOCKS.put(spreadableBlock, surfaceForSpread);
TERRAIN_TO_SPREAD.add(surfaceForSpread);
TERRAIN.add(surfaceForSpread);
} }
public static boolean isSpreadable(Block block) { public static boolean isTerrain(Block block) {
return SPREADABLE_BLOCKS.contains(block); return TERRAIN.contains(block);
}
public static boolean isSpreadableTerrain(Block block) {
return TERRAIN_TO_SPREAD.contains(block);
}
public static Block getSpreadable(Block block) {
return SPREADABLE_BLOCKS.get(block);
} }
public static void addLandGrass(Block plant, Block... terrain) { public static void addLandGrass(Block plant, Block... terrain) {
for (Block block: terrain) { for (Block block : terrain) {
addLandGrass(block, plant, 1F); addLandGrass(plant, block, 1F);
} }
} }
public static void addLandGrass(ResourceLocation biome, Block plant, Block... terrain) { public static void addLandGrass(ResourceLocation biome, Block plant, Block... terrain) {
for (Block block: terrain) { for (Block block : terrain) {
addLandGrass(biome, block, plant, 1F); addLandGrass(biome, plant, block, 1F);
} }
} }
public static void addLandGrass(Block terrain, Block plant, float chance) { public static void addLandGrass(Block plant, Block terrain, float chance) {
WeightedList<Block> list = LAND_GRASS_TYPES.get(terrain); WeightedList<Block> list = LAND_GRASS_TYPES.get(terrain);
if (list == null) { if (list == null) {
list = new WeightedList<Block>(); list = new WeightedList<Block>();
LAND_GRASS_TYPES.put(terrain, list); LAND_GRASS_TYPES.put(terrain, list);
} }
TERRAIN.add(terrain);
list.add(plant, chance); list.add(plant, chance);
} }
public static void addLandGrass(ResourceLocation biome, Block terrain, Block plant, float chance) { public static void addLandGrass(ResourceLocation biome, Block plant, Block terrain, float chance) {
Map<Block, WeightedList<Block>> map = LAND_GRASS_BIOMES.get(biome); Map<Block, WeightedList<Block>> map = LAND_GRASS_BIOMES.get(biome);
if (map == null) { if (map == null) {
map = Maps.newHashMap(); map = Maps.newHashMap();
@ -58,31 +70,33 @@ public class BonemealAPI {
list = new WeightedList<Block>(); list = new WeightedList<Block>();
map.put(terrain, list); map.put(terrain, list);
} }
TERRAIN.add(terrain);
list.add(plant, chance); list.add(plant, chance);
} }
public static void addWaterGrass(Block plant, Block... terrain) { public static void addWaterGrass(Block plant, Block... terrain) {
for (Block block: terrain) { for (Block block : terrain) {
addWaterGrass(block, plant, 1F); addWaterGrass(plant, block, 1F);
} }
} }
public static void addWaterGrass(ResourceLocation biome, Block plant, Block... terrain) { public static void addWaterGrass(ResourceLocation biome, Block plant, Block... terrain) {
for (Block block: terrain) { for (Block block : terrain) {
addWaterGrass(biome, block, plant, 1F); addWaterGrass(biome, plant, block, 1F);
} }
} }
public static void addWaterGrass(Block terrain, Block plant, float chance) { public static void addWaterGrass(Block plant, Block terrain, float chance) {
WeightedList<Block> list = WATER_GRASS_TYPES.get(terrain); WeightedList<Block> list = WATER_GRASS_TYPES.get(terrain);
if (list == null) { if (list == null) {
list = new WeightedList<Block>(); list = new WeightedList<Block>();
WATER_GRASS_TYPES.put(terrain, list); WATER_GRASS_TYPES.put(terrain, list);
} }
TERRAIN.add(terrain);
list.add(plant, chance); list.add(plant, chance);
} }
public static void addWaterGrass(ResourceLocation biome, Block terrain, Block plant, float chance) { public static void addWaterGrass(ResourceLocation biome, Block plant, Block terrain, float chance) {
Map<Block, WeightedList<Block>> map = WATER_GRASS_BIOMES.get(biome); Map<Block, WeightedList<Block>> map = WATER_GRASS_BIOMES.get(biome);
if (map == null) { if (map == null) {
map = Maps.newHashMap(); map = Maps.newHashMap();
@ -93,6 +107,7 @@ public class BonemealAPI {
list = new WeightedList<Block>(); list = new WeightedList<Block>();
map.put(terrain, list); map.put(terrain, list);
} }
TERRAIN.add(terrain);
list.add(plant, chance); list.add(plant, chance);
} }
@ -112,16 +127,16 @@ public class BonemealAPI {
} }
public static Block getWaterGrass(ResourceLocation biomeID, Block terrain, Random random) { public static Block getWaterGrass(ResourceLocation biomeID, Block terrain, Random random) {
Map<Block, WeightedList<Block>> map = LAND_GRASS_BIOMES.get(biomeID); Map<Block, WeightedList<Block>> map = WATER_GRASS_BIOMES.get(biomeID);
WeightedList<Block> list = null; WeightedList<Block> list = null;
if (map != null) { if (map != null) {
list = map.get(terrain); list = map.get(terrain);
if (list == null) { if (list == null) {
list = LAND_GRASS_TYPES.get(terrain); list = WATER_GRASS_TYPES.get(terrain);
} }
} }
else { else {
list = LAND_GRASS_TYPES.get(terrain); list = WATER_GRASS_TYPES.get(terrain);
} }
return list == null ? null : list.get(random); return list == null ? null : list.get(random);
} }

View file

@ -0,0 +1,22 @@
package ru.bclib.api;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.block.Block;
import ru.bclib.mixin.common.ComposterBlockAccessor;
public class ComposterAPI {
public static Block allowCompost(float chance, Block block){
if (block != null) {
allowCompost(chance, block.asItem());
}
return block;
}
public static Item allowCompost(float chance, Item item){
if (item != null && item != Items.AIR) {
ComposterBlockAccessor.callAdd(chance, item);
}
return item;
}
}

View file

@ -1,149 +0,0 @@
package ru.bclib.api;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.ModContainer;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.NbtIo;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.chunk.storage.RegionFile;
public class DataFixerAPI {
private static final Map<String, String> REPLACEMENT = Maps.newHashMap();
private static final Map<String, Integer> FIX_VERSIONS = Maps.newHashMap();
public static void fixData(File dir) {
REPLACEMENT.clear(); // API is not finished yet!
if (REPLACEMENT.isEmpty()) {
return;
}
boolean shoudFix = false;
Collection<ModContainer> mods = FabricLoader.getInstance().getAllMods();
for (ModContainer mod: mods) {
String name = mod.getMetadata().getId();
int preVersion = WorldDataAPI.getIntModVersion(name);
int version = getModVersion(mod.getMetadata().getVersion().toString());
if (version > preVersion) {
int fixVersion = FIX_VERSIONS.getOrDefault(name, version);
shoudFix |= fixVersion < version && fixVersion >= preVersion;
}
};
if (!shoudFix) {
return;
}
List<File> regions = getAllRegions(dir, null);
regions.parallelStream().forEach((file) -> {
try {
System.out.println("Fixing " + file);
boolean[] changed = new boolean[1];
RegionFile region = new RegionFile(file, file.getParentFile(), true);
for (int x = 0; x < 32; x++) {
for (int z = 0; z < 32; z++) {
ChunkPos pos = new ChunkPos(x, z);
changed[0] = false;
if (region.hasChunk(pos)) {
DataInputStream input = region.getChunkDataInputStream(pos);
CompoundTag root = NbtIo.read(input);
input.close();
ListTag sections = root.getCompound("Level").getList("Sections", 10);
sections.forEach((tag) -> {
ListTag palette = ((CompoundTag) tag).getList("Palette", 10);
palette.forEach((blockTag) -> {
CompoundTag blockTagCompound = ((CompoundTag) blockTag);
String name = blockTagCompound.getString("Name");
String replace = REPLACEMENT.get(name);
if (replace != null) {
blockTagCompound.putString("Name", replace);
changed[0] = true;
}
});
});
if (changed[0]) {
System.out.println("Write!");
DataOutputStream output = region.getChunkDataOutputStream(pos);
NbtIo.write(root, output);
output.close();
}
}
}
}
region.close();
}
catch (Exception e) {
e.printStackTrace();
}
});
}
/**
* Register block data fix. Fix will be applied on world load if current mod version will be newer than specified one.
* @param modID - {@link String} mod id;
* @param modVersion - {@link String} mod version, should be in format: %d.%d.%d
* @param result - {@link String} new block name;
* @param names - array of {@link String}, old block names to convert.
*/
protected static void addFix(String modID, String modVersion, String result, String... names) {
FIX_VERSIONS.put(modID, getModVersion(modVersion));
for (String name: names) {
REPLACEMENT.put(name, result);
}
}
private static List<File> getAllRegions(File dir, List<File> list) {
if (list == null) {
list = Lists.newArrayList();
}
for (File file: dir.listFiles()) {
if (file.isDirectory()) {
getAllRegions(file, list);
}
else if (file.isFile() && file.getName().endsWith(".mca")) {
list.add(file);
}
}
return list;
}
/**
* Get mod version from string. String should be in format: %d.%d.%d
* @param version - {@link String} mod version.
* @return int mod version.
*/
public static int getModVersion(String version) {
if (version.isEmpty()) {
return 0;
}
try {
String[] values = version.split("\\.");
return Integer.parseInt(values[0]) << 12 | Integer.parseInt(values[1]) << 6 | Integer.parseInt(values[2]);
}
catch (Exception e) {
return 0;
}
}
/**
* Get mod version from integer. String will be in format %d.%d.%d
* @param version - mod version in integer form.
* @return {@link String} mod version.
*/
public static String getModVersion(int version) {
int a = (version >> 12) & 63;
int b = (version >> 6) & 63;
int c = version & 63;
return String.format(Locale.ROOT, "%d.%d.%d", a, b, c);
}
}

View file

@ -1,16 +1,18 @@
package ru.bclib.api; package ru.bclib.api;
import com.google.common.collect.Lists;
import net.fabricmc.loader.api.FabricLoader;
import ru.bclib.integration.ModIntegration;
import java.util.List; import java.util.List;
import com.google.common.collect.Lists;
import ru.bclib.integration.ModIntegration;
public class ModIntegrationAPI { public class ModIntegrationAPI {
private static final List<ModIntegration> INTEGRATIONS = Lists.newArrayList(); private static final List<ModIntegration> INTEGRATIONS = Lists.newArrayList();
private static final boolean HAS_CANVAS = FabricLoader.getInstance().isModLoaded("canvas");
/** /**
* Registers mod integration * Registers mod integration
*
* @param integration * @param integration
* @return * @return
*/ */
@ -21,6 +23,7 @@ public class ModIntegrationAPI {
/** /**
* Get all registered mod integrations. * Get all registered mod integrations.
*
* @return {@link List} of {@link ModIntegration}. * @return {@link List} of {@link ModIntegration}.
*/ */
public static List<ModIntegration> getIntegrations() { public static List<ModIntegration> getIntegrations() {
@ -37,4 +40,8 @@ public class ModIntegrationAPI {
} }
}); });
} }
public static boolean hasCanvas() {
return HAS_CANVAS;
}
} }

View file

@ -0,0 +1,86 @@
package ru.bclib.api;
import com.google.common.collect.Lists;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.blockrenderlayer.v1.BlockRenderLayerMap;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.core.Registry;
import net.minecraft.world.level.block.Block;
import ru.bclib.blocks.BaseBarrelBlock;
import ru.bclib.blocks.BaseChestBlock;
import ru.bclib.blocks.BaseFurnaceBlock;
import ru.bclib.blocks.BaseSignBlock;
import ru.bclib.client.render.BCLRenderLayer;
import ru.bclib.client.render.BaseChestBlockEntityRenderer;
import ru.bclib.client.render.BaseSignBlockEntityRenderer;
import ru.bclib.interfaces.PostInitable;
import ru.bclib.interfaces.RenderLayerProvider;
import ru.bclib.registry.BaseBlockEntities;
import java.util.List;
import java.util.function.Consumer;
public class PostInitAPI {
private static List<Consumer<Boolean>> postInitFunctions = Lists.newArrayList();
/**
* Register a new function which will be called after all mods are initiated. Will be called on both client and server.
* @param function {@link Consumer} with {@code boolean} parameter ({@code true} for client, {@code false} for server).
*/
public static void register(Consumer<Boolean> function) {
postInitFunctions.add(function);
}
/**
* Called in proper BCLib entry points, for internal usage only.
* @param isClient {@code boolean}, {@code true} for client, {@code false} for server.
*/
public static void postInit(boolean isClient) {
if (postInitFunctions == null) {
return;
}
postInitFunctions.forEach(function -> function.accept(isClient));
Registry.BLOCK.forEach(block -> {
processBlockCommon(block);
if (isClient) {
processBlockClient(block);
}
});
postInitFunctions = null;
BiomeAPI.loadFabricAPIBiomes();
}
@Environment(EnvType.CLIENT)
private static void processBlockClient(Block block) {
if (block instanceof RenderLayerProvider) {
BCLRenderLayer layer = ((RenderLayerProvider) block).getRenderLayer();
if (layer == BCLRenderLayer.CUTOUT) BlockRenderLayerMap.INSTANCE.putBlock(block, RenderType.cutout());
else if (layer == BCLRenderLayer.TRANSLUCENT) BlockRenderLayerMap.INSTANCE.putBlock(block, RenderType.translucent());
}
if (block instanceof BaseChestBlock) {
BaseChestBlockEntityRenderer.registerRenderLayer(block);
}
else if (block instanceof BaseSignBlock) {
BaseSignBlockEntityRenderer.registerRenderLayer(block);
}
}
private static void processBlockCommon(Block block) {
if (block instanceof PostInitable) {
((PostInitable) block).postInit();
}
if (block instanceof BaseChestBlock) {
BaseBlockEntities.CHEST.registerBlock(block);
}
else if (block instanceof BaseSignBlock) {
BaseBlockEntities.SIGN.registerBlock(block);
}
else if (block instanceof BaseBarrelBlock) {
BaseBlockEntities.BARREL.registerBlock(block);
}
else if (block instanceof BaseFurnaceBlock) {
BaseBlockEntities.FURNACE.registerBlock(block);
}
}
}

View file

@ -1,9 +1,11 @@
package ru.bclib.api; package ru.bclib.api;
import java.util.function.Supplier; import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import net.fabricmc.fabric.api.tag.TagRegistry; import net.fabricmc.fabric.api.tag.TagRegistry;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.tags.BlockTags; import net.minecraft.tags.BlockTags;
import net.minecraft.tags.ItemTags; import net.minecraft.tags.ItemTags;
import net.minecraft.tags.Tag; import net.minecraft.tags.Tag;
@ -11,45 +13,76 @@ import net.minecraft.tags.Tag.Named;
import net.minecraft.tags.TagCollection; import net.minecraft.tags.TagCollection;
import net.minecraft.world.item.Item; import net.minecraft.world.item.Item;
import net.minecraft.world.item.Items; import net.minecraft.world.item.Items;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.block.Blocks;
import ru.bclib.BCLib; import ru.bclib.BCLib;
import ru.bclib.util.TagHelper;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;
public class TagAPI { public class TagAPI {
private static final Map<ResourceLocation, Set<ResourceLocation>> TAGS_BLOCK = Maps.newConcurrentMap();
private static final Map<ResourceLocation, Set<ResourceLocation>> TAGS_ITEM = Maps.newConcurrentMap();
// Block Tags // Block Tags
public static final Tag.Named<Block> BOOKSHELVES = makeCommonBlockTag("bookshelves"); public static final Tag.Named<Block> BLOCK_BOOKSHELVES = makeCommonBlockTag("bookshelves");
public static final Tag.Named<Block> GEN_TERRAIN = makeBlockTag(BCLib.MOD_ID, "gen_terrain"); public static final Tag.Named<Block> BLOCK_GEN_TERRAIN = makeBlockTag(BCLib.MOD_ID, "gen_terrain");
public static final Tag.Named<Block> NETHER_GROUND = makeBlockTag(BCLib.MOD_ID, "nether_ground"); public static final Tag.Named<Block> BLOCK_NETHER_GROUND = makeBlockTag(BCLib.MOD_ID, "nether_ground");
public static final Tag.Named<Block> END_GROUND = makeBlockTag(BCLib.MOD_ID, "end_ground"); public static final Tag.Named<Block> BLOCK_END_GROUND = makeBlockTag(BCLib.MOD_ID, "end_ground");
public static final Tag.Named<Block> BLOCK_CHEST = makeCommonBlockTag("chest"); public static final Tag.Named<Block> BLOCK_CHEST = makeCommonBlockTag("chest");
public static final Tag.Named<Block> END_STONES = makeCommonBlockTag("end_stones"); public static final Tag.Named<Block> BLOCK_WOODEN_CHEST = makeCommonBlockTag("wooden_chests");
public static final Tag.Named<Block> NETHER_STONES = makeCommonBlockTag("nether_stones"); public static final Tag.Named<Block> BLOCK_BARREL = makeCommonBlockTag("barrel");
public static final Tag.Named<Block> BLOCK_WOODEN_BARREL = makeCommonBlockTag("wooden_barrels");
public static final Tag.Named<Block> BLOCK_END_STONES = makeCommonBlockTag("end_stones");
public static final Tag.Named<Block> BLOCK_NETHER_STONES = makeCommonBlockTag("nether_stones");
public static final Tag.Named<Block> BLOCK_NETHER_PORTAL_FRAME = makeCommonBlockTag("nether_pframe");
public static final Tag.Named<Block> BLOCK_WORKBENCHES = makeCommonBlockTag("workbench");
public static final Tag.Named<Block> BLOCK_SAPLINGS = makeCommonBlockTag("saplings");
public static final Tag.Named<Block> BLOCK_LEAVES = makeCommonBlockTag("leaves");
public static final Tag.Named<Block> BLOCK_IMMOBILE = makeCommonBlockTag("immobile");
public static final Tag.Named<Block> DRAGON_IMMUNE = getMCBlockTag("dragon_immune"); public static final Tag.Named<Block> BLOCK_DRAGON_IMMUNE = getMCBlockTag("dragon_immune");
public static final Tag.Named<Block> MINEABLE_AXE = getMCBlockTag("mineable/axe");
public static final Tag.Named<Block> MINEABLE_PICKAXE = getMCBlockTag("mineable/pickaxe");
public static final Tag.Named<Block> MINEABLE_SHOVEL = getMCBlockTag("mineable/shovel");
public static final Tag.Named<Block> MINEABLE_HOE = getMCBlockTag("mineable/hoe");
// Item Tags // Item Tags
public static final Tag.Named<Item> ITEM_CHEST = makeCommonItemTag("chest"); public static final Tag.Named<Item> ITEM_CHEST = makeCommonItemTag("chest");
public static final Tag.Named<Item> IRON_INGOTS = makeCommonItemTag("iron_ingots"); public static final Tag.Named<Item> ITEM_WOODEN_CHEST = makeCommonItemTag("wooden_chests");
public static final Tag.Named<Item> FURNACES = makeCommonItemTag("furnaces"); public static final Tag.Named<Item> ITEM_BARREL = makeCommonItemTag("barrel");
public final static Tag.Named<Item> HAMMERS = makeItemTag("fabric", "hammers"); public static final Tag.Named<Item> ITEM_WOODEN_BARREL = makeCommonItemTag("wooden_barrels");
public static final Tag.Named<Item> ITEM_IRON_INGOTS = makeCommonItemTag("iron_ingots");
public static final Tag.Named<Item> ITEM_FURNACES = makeCommonItemTag("furnaces");
public static final Tag.Named<Item> ITEM_WORKBENCHES = makeCommonItemTag("workbench");
public final static Tag.Named<Item> ITEM_HAMMERS = makeCommonItemTag("hammers");
public static final Tag.Named<Item> ITEM_SAPLINGS = makeCommonItemTag("saplings");
public static final Tag.Named<Item> ITEM_LEAVES = makeCommonItemTag("leaves");
public static final Tag.Named<Item> ITEM_SHEARS = getMCItemTag("shears");
public static final Tag.Named<Item> ITEM_COMMON_SHEARS = makeCommonItemTag("shears");
/** /**
* Get or create {@link Tag.Named}. * Get or create {@link Tag.Named}.
*
* @param containerSupplier - {@link TagCollection} {@link Supplier} tag collection; * @param containerSupplier - {@link TagCollection} {@link Supplier} tag collection;
* @param id - {@link ResourceLocation} tag id. * @param id - {@link ResourceLocation} tag id.
* @return {@link Tag.Named}. * @return {@link Tag.Named}.
*/ */
public static <T> Tag.Named<T> makeTag(Supplier<TagCollection<T>> containerSupplier, ResourceLocation id) { public static <T> Tag.Named<T> makeTag(Supplier<TagCollection<T>> containerSupplier, ResourceLocation id) {
Tag<T> tag = containerSupplier.get().getTag(id); Tag<T> tag = containerSupplier.get().getTag(id);
return tag == null ? TagRegistry.create(id, containerSupplier) : (Named<T>) tag; return tag == null ? TagRegistry.create(id, containerSupplier) : (Named<T>) tag;
} }
/** /**
* Get or create {@link Block} {@link Tag.Named} with mod namespace. * Get or create {@link Block} {@link Tag.Named} with mod namespace.
*
* @param modID - {@link String} mod namespace (mod id); * @param modID - {@link String} mod namespace (mod id);
* @param name - {@link String} tag name. * @param name - {@link String} tag name.
* @return {@link Block} {@link Tag.Named}. * @return {@link Block} {@link Tag.Named}.
*/ */
public static Tag.Named<Block> makeBlockTag(String modID, String name) { public static Tag.Named<Block> makeBlockTag(String modID, String name) {
@ -58,8 +91,9 @@ public class TagAPI {
/** /**
* Get or create {@link Item} {@link Tag.Named} with mod namespace. * Get or create {@link Item} {@link Tag.Named} with mod namespace.
*
* @param modID - {@link String} mod namespace (mod id); * @param modID - {@link String} mod namespace (mod id);
* @param name - {@link String} tag name. * @param name - {@link String} tag name.
* @return {@link Item} {@link Tag.Named}. * @return {@link Item} {@link Tag.Named}.
*/ */
public static Tag.Named<Item> makeItemTag(String modID, String name) { public static Tag.Named<Item> makeItemTag(String modID, String name) {
@ -68,9 +102,10 @@ public class TagAPI {
/** /**
* Get or create {@link Block} {@link Tag.Named}. * Get or create {@link Block} {@link Tag.Named}.
* @see <a href="https://fabricmc.net/wiki/tutorial:tags">Fabric Wiki (Tags)</a> *
* @param name - {@link String} tag name. * @param name - {@link String} tag name.
* @return {@link Block} {@link Tag.Named}. * @return {@link Block} {@link Tag.Named}.
* @see <a href="https://fabricmc.net/wiki/tutorial:tags">Fabric Wiki (Tags)</a>
*/ */
public static Tag.Named<Block> makeCommonBlockTag(String name) { public static Tag.Named<Block> makeCommonBlockTag(String name) {
return makeTag(BlockTags::getAllTags, new ResourceLocation("c", name)); return makeTag(BlockTags::getAllTags, new ResourceLocation("c", name));
@ -78,9 +113,10 @@ public class TagAPI {
/** /**
* Get or create {@link Item} {@link Tag.Named}. * Get or create {@link Item} {@link Tag.Named}.
* @see <a href="https://fabricmc.net/wiki/tutorial:tags">Fabric Wiki (Tags)</a> *
* @param name - {@link String} tag name. * @param name - {@link String} tag name.
* @return {@link Item} {@link Tag.Named}. * @return {@link Item} {@link Tag.Named}.
* @see <a href="https://fabricmc.net/wiki/tutorial:tags">Fabric Wiki (Tags)</a>
*/ */
public static Tag.Named<Item> makeCommonItemTag(String name) { public static Tag.Named<Item> makeCommonItemTag(String name) {
return makeTag(ItemTags::getAllTags, new ResourceLocation("c", name)); return makeTag(ItemTags::getAllTags, new ResourceLocation("c", name));
@ -88,6 +124,7 @@ public class TagAPI {
/** /**
* Get or create Minecraft {@link Block} {@link Tag.Named}. * Get or create Minecraft {@link Block} {@link Tag.Named}.
*
* @param name - {@link String} tag name. * @param name - {@link String} tag name.
* @return {@link Block} {@link Tag.Named}. * @return {@link Block} {@link Tag.Named}.
*/ */
@ -97,35 +134,165 @@ public class TagAPI {
return tag == null ? (Named<Block>) TagRegistry.block(id) : (Named<Block>) tag; return tag == null ? (Named<Block>) TagRegistry.block(id) : (Named<Block>) tag;
} }
/**
* Get or create Minecraft {@link Item} {@link Tag.Named}.
*
* @param name - {@link String} tag name.
* @return {@link Item} {@link Tag.Named}.
*/
public static Tag.Named<Item> getMCItemTag(String name) {
ResourceLocation id = new ResourceLocation(name);
Tag<Item> tag = ItemTags.getAllTags().getTag(id);
return tag == null ? (Named<Item>) TagRegistry.item(id) : (Named<Item>) tag;
}
/** /**
* Adds {@link Block} to NETHER_GROUND and GEN_TERRAIN tags to process it properly in terrain generators and block logic. * Adds {@link Block} to NETHER_GROUND and GEN_TERRAIN tags to process it properly in terrain generators and block logic.
*
* @param block - {@link Block}. * @param block - {@link Block}.
*/ */
public static void addNetherGround(Block block) { public static void addNetherGround(Block block) {
TagHelper.addTag(NETHER_GROUND, block); addTag(BLOCK_NETHER_GROUND, block);
TagHelper.addTag(GEN_TERRAIN, block); addTag(BLOCK_GEN_TERRAIN, block);
} }
/** /**
* Adds {@link Block} to END_GROUND and GEN_TERRAIN tags to process it properly in terrain generators and block logic. * Adds {@link Block} to END_GROUND and GEN_TERRAIN tags to process it properly in terrain generators and block logic.
*
* @param block - {@link Block}. * @param block - {@link Block}.
*/ */
public static void addEndGround(Block block) { public static void addEndGround(Block block) {
TagHelper.addTag(GEN_TERRAIN, block); addTag(BLOCK_GEN_TERRAIN, block);
TagHelper.addTag(END_GROUND, block); addTag(BLOCK_END_GROUND, block);
} }
/** /**
* Initializes basic tags. Should be called only in BCLib main class. * Initializes basic tags. Should be called only in BCLib main class.
*/ */
public static void init() { public static void init() {
TagHelper.addTag(BOOKSHELVES, Blocks.BOOKSHELF); addTag(BLOCK_BOOKSHELVES, Blocks.BOOKSHELF);
TagHelper.addTag(GEN_TERRAIN, Blocks.END_STONE, Blocks.NETHERRACK, Blocks.SOUL_SAND, Blocks.SOUL_SOIL); addTag(BLOCK_GEN_TERRAIN, Blocks.END_STONE, Blocks.NETHERRACK, Blocks.SOUL_SAND, Blocks.SOUL_SOIL);
TagHelper.addTag(NETHER_GROUND, Blocks.NETHERRACK, Blocks.SOUL_SAND, Blocks.SOUL_SOIL); addTag(BLOCK_NETHER_GROUND, Blocks.NETHERRACK, Blocks.SOUL_SAND, Blocks.SOUL_SOIL);
TagHelper.addTag(END_GROUND, Blocks.END_STONE); addTag(BLOCK_END_GROUND, Blocks.END_STONE);
TagHelper.addTag(BLOCK_CHEST, Blocks.CHEST); addTag(BLOCK_CHEST, Blocks.CHEST);
TagHelper.addTag(ITEM_CHEST, Items.CHEST); addTag(ITEM_CHEST, Items.CHEST);
TagHelper.addTag(IRON_INGOTS, Items.IRON_INGOT); addTag(ITEM_IRON_INGOTS, Items.IRON_INGOT);
TagHelper.addTag(FURNACES, Blocks.FURNACE); addTag(ITEM_FURNACES, Blocks.FURNACE);
}
/**
* Adds one Tag to multiple Blocks.
* <p>
* Example:
* <pre>{@code Tag.Named<Block> DIMENSION_STONE = makeBlockTag("mymod", "dim_stone");
* addTag(DIMENSION_STONE, Blocks.END_STONE, Blocks.NETHERRACK);}</pre>
* <p>
* The call will reserve the Tag. The Tag is added to the blocks once
* {@link #apply(String, Map)} was executed.
*
* @param tag The new Tag
* @param blocks One or more blocks that should receive the Tag.
*/
public static void addTag(Tag.Named<Block> tag, Block... blocks) {
ResourceLocation tagID = tag.getName();
Set<ResourceLocation> set = TAGS_BLOCK.computeIfAbsent(tagID, k -> Sets.newHashSet());
for (Block block : blocks) {
ResourceLocation id = Registry.BLOCK.getKey(block);
if (id != Registry.BLOCK.getDefaultKey()) {
set.add(id);
}
}
}
/**
* Adds one Tag to multiple Items.
* <p>
* Example:
* <pre>{@code Tag.Named<Item> METALS = makeBlockTag("mymod", "metals");
* addTag(METALS, Items.IRON_INGOT, Items.GOLD_INGOT, Items.COPPER_INGOT);}</pre>
* <p>
* The call will reserve the Tag. The Tag is added to the items once
* {@link #apply(String, Map)} was executed.
*
* @param tag The new Tag
* @param items One or more item that should receive the Tag.
*/
public static void addTag(Tag.Named<Item> tag, ItemLike... items) {
ResourceLocation tagID = tag.getName();
Set<ResourceLocation> set = TAGS_ITEM.computeIfAbsent(tagID, k -> Sets.newHashSet());
for (ItemLike item : items) {
ResourceLocation id = Registry.ITEM.getKey(item.asItem());
if (id != Registry.ITEM.getDefaultKey()) {
set.add(id);
}
}
}
/**
* Adds multiple Tags to one Item.
* <p>
* The call will reserve the Tags. The Tags are added to the Item once
* * {@link #apply(String, Map)} was executed.
*
* @param item The Item that will receive all Tags
* @param tags One or more Tags
*/
@SafeVarargs
public static void addTags(ItemLike item, Tag.Named<Item>... tags) {
for (Tag.Named<Item> tag : tags) {
addTag(tag, item);
}
}
/**
* Adds multiple Tags to one Block.
* <p>
* The call will reserve the Tags. The Tags are added to the Block once
* * {@link #apply(String, Map)} was executed.
*
* @param block The Block that will receive all Tags
* @param tags One or more Tags
*/
@SafeVarargs
public static void addTags(Block block, Tag.Named<Block>... tags) {
for (Tag.Named<Block> tag : tags) {
addTag(tag, block);
}
}
/**
* Adds all {@code ids} to the {@code builder}.
*
* @param builder
* @param ids
* @return The Builder passed as {@code builder}.
*/
public static Tag.Builder apply(Tag.Builder builder, Set<ResourceLocation> ids) {
ids.forEach(value -> builder.addElement(value, "Better End Code"));
return builder;
}
/**
* Automatically called in {@link net.minecraft.tags.TagLoader#loadAndBuild(ResourceManager)}.
* <p>
* In most cases there is no need to call this Method manually.
*
* @param directory The name of the Tag-directory. Should be either <i>"tags/blocks"</i> or
* <i>"tags/items"</i>.
* @param tagsMap The map that will hold the registered Tags
* @return The {@code tagsMap} Parameter.
*/
public static Map<ResourceLocation, Tag.Builder> apply(String directory, Map<ResourceLocation, Tag.Builder> tagsMap) {
Map<ResourceLocation, Set<ResourceLocation>> endTags = null;
if ("tags/blocks".equals(directory)) {
endTags = TAGS_BLOCK;
}
else if ("tags/items".equals(directory)) {
endTags = TAGS_ITEM;
}
if (endTags != null) {
endTags.forEach((id, ids) -> apply(tagsMap.computeIfAbsent(id, key -> Tag.Builder.tag()), ids));
}
return tagsMap;
} }
} }

View file

@ -1,20 +1,30 @@
package ru.bclib.api; package ru.bclib.api;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.ModContainer;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtIo;
import net.minecraft.world.level.storage.LevelStorageSource.LevelStorageAccess;
import ru.bclib.BCLib;
import ru.bclib.api.datafixer.DataFixerAPI;
import ru.bclib.util.ModUtil;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.function.Consumer;
import com.google.common.collect.Lists; /**
import com.google.common.collect.Maps; * Mod-specifix data-storage for a world.
* <p>
import net.fabricmc.loader.api.FabricLoader; * This class provides the ability for mod to store persistent data inside a world. The Storage for the world is
import net.fabricmc.loader.api.ModContainer; * currently initialized as part of the {@link DataFixerAPI} in {@link DataFixerAPI#fixData(LevelStorageAccess, boolean, Consumer)}
import net.minecraft.nbt.CompoundTag; * or {@link DataFixerAPI#initializeWorldData(File, boolean)}
import net.minecraft.nbt.NbtIo; */
import ru.bclib.BCLib;
public class WorldDataAPI { public class WorldDataAPI {
private static final Map<String, CompoundTag> TAGS = Maps.newHashMap(); private static final Map<String, CompoundTag> TAGS = Maps.newHashMap();
private static final List<String> MODS = Lists.newArrayList(); private static final List<String> MODS = Lists.newArrayList();
@ -22,36 +32,43 @@ public class WorldDataAPI {
public static void load(File dataDir) { public static void load(File dataDir) {
WorldDataAPI.dataDir = dataDir; WorldDataAPI.dataDir = dataDir;
MODS.stream().parallel().forEach(modID -> { MODS.stream()
File file = new File(dataDir, modID + ".nbt"); .parallel()
CompoundTag root = new CompoundTag(); .forEach(modID -> {
TAGS.put(modID, root); File file = new File(dataDir, modID + ".nbt");
if (file.exists()) { CompoundTag root = new CompoundTag();
try { if (file.exists()) {
root = NbtIo.readCompressed(file); try {
} root = NbtIo.readCompressed(file);
catch (IOException e) {
BCLib.LOGGER.error("World data loading failed", e);
}
}
else {
Optional<ModContainer> optional = FabricLoader.getInstance().getModContainer(modID);
if (optional.isPresent()) {
ModContainer modContainer = optional.get();
if (BCLib.isDevEnvironment()) {
root.putString("version", "63.63.63");
} }
else { catch (IOException e) {
root.putString("version", modContainer.getMetadata().getVersion().toString()); BCLib.LOGGER.error("World data loading failed", e);
} }
saveFile(modID);
} }
} else {
}); Optional<ModContainer> optional = FabricLoader.getInstance()
.getModContainer(modID);
if (optional.isPresent()) {
ModContainer modContainer = optional.get();
if (BCLib.isDevEnvironment()) {
root.putString("version", "255.255.9999");
}
else {
root.putString("version", modContainer.getMetadata()
.getVersion()
.toString());
}
saveFile(modID);
}
}
TAGS.put(modID, root);
});
} }
/** /**
* Register mod cache, world cache is located in world data folder. * Register mod cache, world cache is located in world data folder.
*
* @param modID - {@link String} modID. * @param modID - {@link String} modID.
*/ */
public static void registerModCache(String modID) { public static void registerModCache(String modID) {
@ -60,6 +77,7 @@ public class WorldDataAPI {
/** /**
* Get root {@link CompoundTag} for mod cache in world data folder. * Get root {@link CompoundTag} for mod cache in world data folder.
*
* @param modID - {@link String} modID. * @param modID - {@link String} modID.
* @return {@link CompoundTag} * @return {@link CompoundTag}
*/ */
@ -74,13 +92,14 @@ public class WorldDataAPI {
/** /**
* Get {@link CompoundTag} with specified path from mod cache in world data folder. * Get {@link CompoundTag} with specified path from mod cache in world data folder.
*
* @param modID - {@link String} path to tag, dot-separated. * @param modID - {@link String} path to tag, dot-separated.
* @return {@link CompoundTag} * @return {@link CompoundTag}
*/ */
public static CompoundTag getCompoundTag(String modID, String path) { public static CompoundTag getCompoundTag(String modID, String path) {
String[] parts = path.split("\\."); String[] parts = path.split("\\.");
CompoundTag tag = getRootTag(modID); CompoundTag tag = getRootTag(modID);
for (String part: parts) { for (String part : parts) {
if (tag.contains(part)) { if (tag.contains(part)) {
tag = tag.getCompound(part); tag = tag.getCompound(part);
} }
@ -95,10 +114,14 @@ public class WorldDataAPI {
/** /**
* Forces mod cache file to be saved. * Forces mod cache file to be saved.
*
* @param modID {@link String} mod ID. * @param modID {@link String} mod ID.
*/ */
public static void saveFile(String modID) { public static void saveFile(String modID) {
try { try {
if (!dataDir.exists()) {
dataDir.mkdirs();
}
NbtIo.writeCompressed(getRootTag(modID), new File(dataDir, modID + ".nbt")); NbtIo.writeCompressed(getRootTag(modID), new File(dataDir, modID + ".nbt"));
} }
catch (IOException e) { catch (IOException e) {
@ -108,6 +131,7 @@ public class WorldDataAPI {
/** /**
* Get stored mod version (only for mods with registered cache). * Get stored mod version (only for mods with registered cache).
*
* @return {@link String} mod version. * @return {@link String} mod version.
*/ */
public static String getModVersion(String modID) { public static String getModVersion(String modID) {
@ -116,9 +140,10 @@ public class WorldDataAPI {
/** /**
* Get stored mod version as integer (only for mods with registered cache). * Get stored mod version as integer (only for mods with registered cache).
*
* @return {@code int} mod version. * @return {@code int} mod version.
*/ */
public static int getIntModVersion(String modID) { public static int getIntModVersion(String modID) {
return DataFixerAPI.getModVersion(getModVersion(modID)); return ModUtil.convertModVersion(getModVersion(modID));
} }
} }

View file

@ -0,0 +1,99 @@
package ru.bclib.api.dataexchange;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.networking.v1.PacketSender;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientPacketListener;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.network.ServerGamePacketListenerImpl;
import org.jetbrains.annotations.NotNull;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
public abstract class BaseDataHandler {
private final boolean originatesOnServer;
@NotNull
private final ResourceLocation identifier;
protected BaseDataHandler(ResourceLocation identifier, boolean originatesOnServer) {
this.originatesOnServer = originatesOnServer;
this.identifier = identifier;
}
final public boolean getOriginatesOnServer() {
return originatesOnServer;
}
final public ResourceLocation getIdentifier() {
return identifier;
}
@Environment(EnvType.CLIENT)
abstract void receiveFromServer(Minecraft client, ClientPacketListener handler, FriendlyByteBuf buf, PacketSender responseSender);
private ServerPlayer lastMessageSender;
void receiveFromClient(MinecraftServer server, ServerPlayer player, ServerGamePacketListenerImpl handler, FriendlyByteBuf buf, PacketSender responseSender) {
lastMessageSender = player;
}
final protected boolean reply(BaseDataHandler message, MinecraftServer server) {
if (lastMessageSender == null) return false;
message.sendToClient(server, lastMessageSender);
return true;
}
abstract void sendToClient(MinecraftServer server);
abstract void sendToClient(MinecraftServer server, ServerPlayer player);
@Environment(EnvType.CLIENT)
abstract void sendToServer(Minecraft client);
protected boolean isBlocking() { return false; }
@Override
public String toString() {
return "BasDataHandler{" + "originatesOnServer=" + originatesOnServer + ", identifier=" + identifier + '}';
}
/**
* Write a String to a buffer (Convenience Method)
*
* @param buf The buffer to write to
* @param s The String you want to write
*/
public static void writeString(FriendlyByteBuf buf, String s) {
buf.writeByteArray(s.getBytes(StandardCharsets.UTF_8));
}
/**
* Read a string from a buffer (Convenience Method)
*
* @param buf Thea buffer to read from
* @return The received String
*/
public static String readString(FriendlyByteBuf buf) {
byte[] data = buf.readByteArray();
return new String(data, StandardCharsets.UTF_8);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof BaseDataHandler)) return false;
BaseDataHandler that = (BaseDataHandler) o;
return originatesOnServer == that.originatesOnServer && identifier.equals(that.identifier);
}
@Override
public int hashCode() {
return Objects.hash(originatesOnServer, identifier);
}
}

View file

@ -0,0 +1,18 @@
package ru.bclib.api.dataexchange;
import ru.bclib.api.dataexchange.handler.DataExchange;
import java.util.Set;
abstract class Connector {
protected final DataExchange api;
Connector(DataExchange api) {
this.api = api;
}
public abstract boolean onClient();
protected Set<DataHandlerDescriptor> getDescriptors(){
return api.getDescriptors();
}
}

View file

@ -0,0 +1,70 @@
package ru.bclib.api.dataexchange;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.networking.v1.PacketSender;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientPacketListener;
import net.minecraft.network.FriendlyByteBuf;
import ru.bclib.BCLib;
import ru.bclib.api.dataexchange.handler.DataExchange;
/**
* This is an internal class that handles a Clienetside players Connection to a Server
*/
@Environment(EnvType.CLIENT)
public class ConnectorClientside extends Connector {
private Minecraft client;
ConnectorClientside(DataExchange api) {
super(api);
this.client = null;
}
@Override
public boolean onClient() {
return true;
}
public void onPlayInit(ClientPacketListener handler, Minecraft client){
if (this.client!=null && this.client != client){
BCLib.LOGGER.warning("Client changed!");
}
this.client = client;
for(DataHandlerDescriptor desc : getDescriptors()){
ClientPlayNetworking.registerReceiver(desc.IDENTIFIER, (_client, _handler, _buf, _responseSender)->{
receiveFromServer(desc, _client, _handler, _buf, _responseSender);
});
}
}
public void onPlayReady(ClientPacketListener handler, PacketSender sender, Minecraft client){
for(DataHandlerDescriptor desc : getDescriptors()){
if (desc.sendOnJoin){
BaseDataHandler h = desc.JOIN_INSTANCE.get();
if (!h.getOriginatesOnServer()) {
h.sendToServer(client);
}
}
}
}
public void onPlayDisconnect(ClientPacketListener handler, Minecraft client){
for(DataHandlerDescriptor desc : getDescriptors()) {
ClientPlayNetworking.unregisterReceiver(desc.IDENTIFIER);
}
}
void receiveFromServer(DataHandlerDescriptor desc, Minecraft client, ClientPacketListener handler, FriendlyByteBuf buf, PacketSender responseSender){
BaseDataHandler h = desc.INSTANCE.get();
h.receiveFromServer(client, handler, buf, responseSender);
}
public void sendToServer(BaseDataHandler h){
if (client==null){
throw new RuntimeException("[internal error] Client not initialized yet!");
}
h.sendToServer(this.client);
}
}

View file

@ -0,0 +1,67 @@
package ru.bclib.api.dataexchange;
import net.fabricmc.fabric.api.networking.v1.PacketSender;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.network.ServerGamePacketListenerImpl;
import ru.bclib.BCLib;
import ru.bclib.api.dataexchange.handler.DataExchange;
/**
* This is an internal class that handles a Serverside Connection to a Client-Player
*/
public class ConnectorServerside extends Connector {
private MinecraftServer server;
ConnectorServerside(DataExchange api) {
super(api);
server = null;
}
@Override
public boolean onClient() {
return false;
}
public void onPlayInit(ServerGamePacketListenerImpl handler, MinecraftServer server){
if (this.server!=null && this.server != server){
BCLib.LOGGER.warning("Server changed!");
}
this.server = server;
for(DataHandlerDescriptor desc : getDescriptors()){
ServerPlayNetworking.registerReceiver(handler, desc.IDENTIFIER, (_server, _player, _handler, _buf, _responseSender) -> {
receiveFromClient(desc, _server, _player, _handler, _buf, _responseSender);
});
}
}
public void onPlayReady(ServerGamePacketListenerImpl handler, PacketSender sender, MinecraftServer server){
for(DataHandlerDescriptor desc : getDescriptors()){
if (desc.sendOnJoin){
BaseDataHandler h = desc.JOIN_INSTANCE.get();
if (h.getOriginatesOnServer()) {
h.sendToClient(server, handler.player);
}
}
}
}
public void onPlayDisconnect(ServerGamePacketListenerImpl handler, MinecraftServer server){
for(DataHandlerDescriptor desc : getDescriptors()){
ServerPlayNetworking.unregisterReceiver(handler, desc.IDENTIFIER);
}
}
void receiveFromClient(DataHandlerDescriptor desc, MinecraftServer server, ServerPlayer player, ServerGamePacketListenerImpl handler, FriendlyByteBuf buf, PacketSender responseSender){
BaseDataHandler h = desc.INSTANCE.get();
h.receiveFromClient(server, player, handler, buf, responseSender);
}
public void sendToClient(BaseDataHandler h){
if (server==null){
throw new RuntimeException("[internal error] Server not initialized yet!");
}
h.sendToClient(this.server);
}
}

View file

@ -0,0 +1,211 @@
package ru.bclib.api.dataexchange;
import com.google.common.collect.Lists;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.network.FriendlyByteBuf;
import ru.bclib.BCLib;
import ru.bclib.api.dataexchange.handler.DataExchange;
import ru.bclib.api.dataexchange.handler.autosync.AutoSync;
import ru.bclib.api.dataexchange.handler.autosync.AutoSync.NeedTransferPredicate;
import ru.bclib.api.dataexchange.handler.autosync.AutoSyncID;
import ru.bclib.config.Config;
import ru.bclib.util.ModUtil;
import java.io.File;
import java.util.List;
import java.util.function.BiConsumer;
public class DataExchangeAPI extends DataExchange {
private final static List<String> MODS = Lists.newArrayList();
/**
* You should never need to create a custom instance of this Object.
*/
public DataExchangeAPI() {
super();
}
@Environment(EnvType.CLIENT)
protected ConnectorClientside clientSupplier(DataExchange api) {
return new ConnectorClientside(api);
}
protected ConnectorServerside serverSupplier(DataExchange api) {
return new ConnectorServerside(api);
}
/**
* Register a mod to participate in the DataExchange.
*
* @param modID - {@link String} modID.
*/
public static void registerMod(String modID) {
if (!MODS.contains(modID)) MODS.add(modID);
}
/**
* Register a mod dependency to participate in the DataExchange.
*
* @param modID - {@link String} modID.
*/
public static void registerModDependency(String modID) {
if (ModUtil.getModInfo(modID, false) != null && !"0.0.0".equals(ModUtil.getModVersion(modID))) {
registerMod(modID);
} else {
BCLib.LOGGER.info("Mod Dependency '" + modID + "' not found. This is probably OK.");
}
}
/**
* Returns the IDs of all registered Mods.
*
* @return List of modIDs
*/
public static List<String> registeredMods() {
return MODS;
}
/**
* Add a new Descriptor for a {@link DataHandler}.
*
* @param desc The Descriptor you want to add.
*/
public static void registerDescriptor(DataHandlerDescriptor desc) {
DataExchange api = DataExchange.getInstance();
api.getDescriptors()
.add(desc);
}
/**
* Bulk-Add a Descriptors for your {@link DataHandler}-Objects.
*
* @param desc The Descriptors you want to add.
*/
public static void registerDescriptors(List<DataHandlerDescriptor> desc) {
DataExchange api = DataExchange.getInstance();
api.getDescriptors()
.addAll(desc);
}
/**
* Sends the Handler.
* <p>
* Depending on what the result of {@link DataHandler#getOriginatesOnServer()}, the Data is sent from the server
* to the client (if {@code true}) or the other way around.
* <p>
* The method {@link DataHandler#serializeData(FriendlyByteBuf, boolean)} is called just before the data is sent. You should
* use this method to add the Data you need to the communication.
*
* @param h The Data that you want to send
*/
public static void send(BaseDataHandler h) {
if (h.getOriginatesOnServer()) {
DataExchangeAPI.getInstance().server.sendToClient(h);
}
else {
DataExchangeAPI.getInstance().client.sendToServer(h);
}
}
/**
* Registers a File for automatic client syncing.
*
* @param modID The ID of the calling Mod
* @param fileName The name of the File
*/
public static void addAutoSyncFile(String modID, File fileName) {
AutoSync.addAutoSyncFileData(modID, fileName, false, SyncFileHash.NEED_TRANSFER);
}
/**
* Registers a File for automatic client syncing.
* <p>
* The file is synced of the {@link SyncFileHash} on client and server are not equal. This method will not copy the
* configs content from the client to the server.
*
* @param modID The ID of the calling Mod
* @param uniqueID A unique Identifier for the File. (see {@link SyncFileHash#uniqueID} for
* Details
* @param fileName The name of the File
*/
public static void addAutoSyncFile(String modID, String uniqueID, File fileName) {
AutoSync.addAutoSyncFileData(modID, uniqueID, fileName, false, SyncFileHash.NEED_TRANSFER);
}
/**
* Registers a File for automatic client syncing.
* <p>
* The content of the file is requested for comparison. This will copy the
* entire file from the client to the server.
* <p>
* You should only use this option, if you need to compare parts of the file in order to decide
* if the File needs to be copied. Normally using the {@link SyncFileHash}
* for comparison is sufficient.
*
* @param modID The ID of the calling Mod
* @param fileName The name of the File
* @param needTransfer If the predicate returns true, the file needs to get copied to the server.
*/
public static void addAutoSyncFile(String modID, File fileName, NeedTransferPredicate needTransfer) {
AutoSync.addAutoSyncFileData(modID, fileName, true, needTransfer);
}
/**
* Registers a File for automatic client syncing.
* <p>
* The content of the file is requested for comparison. This will copy the
* entire file from the client to the server.
* <p>
* You should only use this option, if you need to compare parts of the file in order to decide
* if the File needs to be copied. Normally using the {@link SyncFileHash}
* for comparison is sufficient.
*
* @param modID The ID of the calling Mod
* @param uniqueID A unique Identifier for the File. (see {@link SyncFileHash#uniqueID} for
* Details
* @param fileName The name of the File
* @param needTransfer If the predicate returns true, the file needs to get copied to the server.
*/
public static void addAutoSyncFile(String modID, String uniqueID, File fileName, NeedTransferPredicate needTransfer) {
AutoSync.addAutoSyncFileData(modID, uniqueID, fileName, true, needTransfer);
}
/**
* Register a function that is called whenever the client receives a file from the server and replaced toe local
* file with the new content.
* <p>
* This callback is usefull if you need to reload the new content before the game is quit.
*
* @param callback A Function that receives the AutoSyncID as well as the Filename.
*/
public static void addOnWriteCallback(BiConsumer<AutoSyncID, File> callback) {
AutoSync.addOnWriteCallback(callback);
}
/**
* Returns the sync-folder for a given Mod.
* <p>
* BCLib will ensure that the contents of sync-folder on the client is the same as the one on the server.
*
* @param modID ID of the Mod
* @return The path to the sync-folder
*/
public static File getModSyncFolder(String modID) {
File fl = AutoSync.SYNC_FOLDER.localFolder.resolve(modID.replace(".", "-")
.replace(":", "-")
.replace("\\", "-")
.replace("/", "-"))
.normalize()
.toFile();
if (!fl.exists()) {
fl.mkdirs();
}
return fl;
}
static {
addOnWriteCallback(Config::reloadSyncedConfig);
}
}

View file

@ -0,0 +1,274 @@
package ru.bclib.api.dataexchange;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
import net.fabricmc.fabric.api.networking.v1.PacketSender;
import net.fabricmc.fabric.api.networking.v1.PlayerLookup;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientPacketListener;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.network.ServerGamePacketListenerImpl;
import ru.bclib.BCLib;
import ru.bclib.api.dataexchange.handler.autosync.Chunker;
import ru.bclib.api.dataexchange.handler.autosync.Chunker.PacketChunkSender;
import java.util.Collection;
import java.util.List;
public abstract class DataHandler extends BaseDataHandler {
public abstract static class WithoutPayload extends DataHandler {
protected WithoutPayload(ResourceLocation identifier, boolean originatesOnServer) {
super(identifier, originatesOnServer);
}
@Override
protected boolean prepareData(boolean isClient) { return true; }
@Override
protected void serializeData(FriendlyByteBuf buf, boolean isClient) {
}
@Override
protected void deserializeIncomingData(FriendlyByteBuf buf, PacketSender responseSender, boolean isClient) {
}
}
protected DataHandler(ResourceLocation identifier, boolean originatesOnServer) {
super(identifier, originatesOnServer);
}
protected boolean prepareData(boolean isClient) { return true; }
abstract protected void serializeData(FriendlyByteBuf buf, boolean isClient);
abstract protected void deserializeIncomingData(FriendlyByteBuf buf, PacketSender responseSender, boolean isClient);
abstract protected void runOnGameThread(Minecraft client, MinecraftServer server, boolean isClient);
@Environment(EnvType.CLIENT)
@Override
void receiveFromServer(Minecraft client, ClientPacketListener handler, FriendlyByteBuf buf, PacketSender responseSender) {
deserializeIncomingData(buf, responseSender, true);
final Runnable runner = () -> runOnGameThread(client, null, true);
if (isBlocking()) client.executeBlocking(runner);
else client.execute(runner);
}
@Override
void receiveFromClient(MinecraftServer server, ServerPlayer player, ServerGamePacketListenerImpl handler, FriendlyByteBuf buf, PacketSender responseSender) {
super.receiveFromClient(server, player, handler, buf, responseSender);
deserializeIncomingData(buf, responseSender, false);
final Runnable runner = () -> runOnGameThread(null, server, false);
if (isBlocking()) server.executeBlocking(runner);
else server.execute(runner);
}
@Override
void sendToClient(MinecraftServer server) {
if (prepareData(false)) {
FriendlyByteBuf buf = PacketByteBufs.create();
serializeData(buf, false);
_sendToClient(getIdentifier(), server, PlayerLookup.all(server), buf);
}
}
@Override
void sendToClient(MinecraftServer server, ServerPlayer player) {
if (prepareData(false)) {
FriendlyByteBuf buf = PacketByteBufs.create();
serializeData(buf, false);
_sendToClient(getIdentifier(), server, List.of(player), buf);
}
}
public static void _sendToClient(ResourceLocation identifier, MinecraftServer server, Collection<ServerPlayer> players, FriendlyByteBuf buf) {
if (buf.readableBytes()> Chunker.MAX_PACKET_SIZE) {
final PacketChunkSender sender = new PacketChunkSender(buf, identifier);
sender.sendChunks(players);
} else {
for (ServerPlayer player : players) {
ServerPlayNetworking.send(player, identifier, buf);
}
}
}
@Environment(EnvType.CLIENT)
@Override
void sendToServer(Minecraft client) {
if (prepareData(true)) {
FriendlyByteBuf buf = PacketByteBufs.create();
serializeData(buf, true);
ClientPlayNetworking.send(getIdentifier(), buf);
}
}
/**
* A Message that always originates on the Client
*/
public abstract static class FromClient extends BaseDataHandler {
public abstract static class WithoutPayload extends FromClient {
protected WithoutPayload(ResourceLocation identifier) {
super(identifier);
}
@Override
protected boolean prepareDataOnClient() { return true; }
@Override
protected void serializeDataOnClient(FriendlyByteBuf buf) {
}
@Override
protected void deserializeIncomingDataOnServer(FriendlyByteBuf buf, PacketSender responseSender) {
}
}
protected FromClient(ResourceLocation identifier) {
super(identifier, false);
}
@Environment(EnvType.CLIENT)
protected boolean prepareDataOnClient() { return true; }
@Environment(EnvType.CLIENT)
abstract protected void serializeDataOnClient(FriendlyByteBuf buf);
abstract protected void deserializeIncomingDataOnServer(FriendlyByteBuf buf, PacketSender responseSender);
abstract protected void runOnServerGameThread(MinecraftServer server);
@Environment(EnvType.CLIENT)
@Override
void receiveFromServer(Minecraft client, ClientPacketListener handler, FriendlyByteBuf buf, PacketSender responseSender) {
BCLib.LOGGER.error("[Internal Error] The message '" + getIdentifier() + "' must originate from the client!");
}
@Override
void receiveFromClient(MinecraftServer server, ServerPlayer player, ServerGamePacketListenerImpl handler, FriendlyByteBuf buf, PacketSender responseSender) {
super.receiveFromClient(server, player, handler, buf, responseSender);
deserializeIncomingDataOnServer(buf, responseSender);
final Runnable runner = () -> runOnServerGameThread(server);
if (isBlocking()) server.executeBlocking(runner);
else server.execute(runner);
}
@Override
void sendToClient(MinecraftServer server) {
BCLib.LOGGER.error("[Internal Error] The message '" + getIdentifier() + "' must originate from the client!");
}
@Override
void sendToClient(MinecraftServer server, ServerPlayer player) {
BCLib.LOGGER.error("[Internal Error] The message '" + getIdentifier() + "' must originate from the client!");
}
@Environment(EnvType.CLIENT)
@Override
void sendToServer(Minecraft client) {
if (prepareDataOnClient()) {
FriendlyByteBuf buf = PacketByteBufs.create();
serializeDataOnClient(buf);
ClientPlayNetworking.send(getIdentifier(), buf);
}
}
}
/**
* A Message that always originates on the Server
*/
public abstract static class FromServer extends BaseDataHandler {
public abstract static class WithoutPayload extends FromServer {
protected WithoutPayload(ResourceLocation identifier) {
super(identifier);
}
@Override
protected boolean prepareDataOnServer() { return true; }
@Override
protected void serializeDataOnServer(FriendlyByteBuf buf) {
}
@Override
protected void deserializeIncomingDataOnClient(FriendlyByteBuf buf, PacketSender responseSender) {
}
}
protected FromServer(ResourceLocation identifier) {
super(identifier, true);
}
protected boolean prepareDataOnServer() { return true; }
abstract protected void serializeDataOnServer(FriendlyByteBuf buf);
@Environment(EnvType.CLIENT)
abstract protected void deserializeIncomingDataOnClient(FriendlyByteBuf buf, PacketSender responseSender);
@Environment(EnvType.CLIENT)
abstract protected void runOnClientGameThread(Minecraft client);
@Environment(EnvType.CLIENT)
@Override
final void receiveFromServer(Minecraft client, ClientPacketListener handler, FriendlyByteBuf buf, PacketSender responseSender) {
deserializeIncomingDataOnClient(buf, responseSender);
final Runnable runner = () -> runOnClientGameThread(client);
if (isBlocking()) client.executeBlocking(runner);
else client.execute(runner);
}
@Override
final void receiveFromClient(MinecraftServer server, ServerPlayer player, ServerGamePacketListenerImpl handler, FriendlyByteBuf buf, PacketSender responseSender) {
super.receiveFromClient(server, player, handler, buf, responseSender);
BCLib.LOGGER.error("[Internal Error] The message '" + getIdentifier() + "' must originate from the server!");
}
public void receiveFromMemory(FriendlyByteBuf buf){
receiveFromServer(Minecraft.getInstance(), null, buf, null);
}
@Override
final void sendToClient(MinecraftServer server) {
if (prepareDataOnServer()) {
FriendlyByteBuf buf = PacketByteBufs.create();
serializeDataOnServer(buf);
_sendToClient(getIdentifier(), server, PlayerLookup.all(server), buf);
}
}
@Override
final void sendToClient(MinecraftServer server, ServerPlayer player) {
if (prepareDataOnServer()) {
FriendlyByteBuf buf = PacketByteBufs.create();
serializeDataOnServer(buf);
_sendToClient(getIdentifier(), server, List.of(player), buf);
}
}
@Environment(EnvType.CLIENT)
@Override
final void sendToServer(Minecraft client) {
BCLib.LOGGER.error("[Internal Error] The message '" + getIdentifier() + "' must originate from the server!");
}
}
}

View file

@ -0,0 +1,49 @@
package ru.bclib.api.dataexchange;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.NotNull;
import java.util.Objects;
import java.util.function.Supplier;
public class DataHandlerDescriptor {
public DataHandlerDescriptor(@NotNull ResourceLocation identifier, @NotNull Supplier<BaseDataHandler> instancer){
this(identifier, instancer, instancer, false, false);
}
public DataHandlerDescriptor(@NotNull ResourceLocation identifier,@NotNull Supplier<BaseDataHandler> instancer, boolean sendOnJoin, boolean sendBeforeEnter){
this(identifier, instancer, instancer, sendOnJoin, sendBeforeEnter);
}
public DataHandlerDescriptor(@NotNull ResourceLocation identifier, @NotNull Supplier<BaseDataHandler> receiv_instancer, @NotNull Supplier<BaseDataHandler> join_instancer, boolean sendOnJoin, boolean sendBeforeEnter){
this.INSTANCE = receiv_instancer;
this.JOIN_INSTANCE = join_instancer;
this.IDENTIFIER = identifier;
this.sendOnJoin = sendOnJoin;
this.sendBeforeEnter = sendBeforeEnter;
}
public final boolean sendOnJoin;
public final boolean sendBeforeEnter;
@NotNull
public final ResourceLocation IDENTIFIER;
@NotNull
public final Supplier<BaseDataHandler> INSTANCE;
@NotNull
public final Supplier<BaseDataHandler> JOIN_INSTANCE;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o instanceof ResourceLocation){
return o.equals(IDENTIFIER);
}
if (!(o instanceof DataHandlerDescriptor that)) return false;
return IDENTIFIER.equals(that.IDENTIFIER);
}
@Override
public int hashCode() {
return Objects.hash(IDENTIFIER);
}
}

View file

@ -0,0 +1,161 @@
package ru.bclib.api.dataexchange;
import net.minecraft.network.FriendlyByteBuf;
import org.jetbrains.annotations.NotNull;
import ru.bclib.BCLib;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Objects;
public class FileHash {
private static int ERR_DOES_NOT_EXIST = -10;
private static int ERR_IO_ERROR = -20;
/**
* The md5-hash of the file
*/
@NotNull
public final byte[] md5;
/**
* The size (in bytes) of the input.
*/
public final int size;
/**
* a value that is directly calculated from defined byte positions.
*/
public final int value;
FileHash(byte[] md5, int size, int value) {
Objects.nonNull(md5);
this.md5 = md5;
this.size = size;
this.value = value;
}
static FileHash createForEmpty(int errCode) {
return new FileHash(new byte[0], 0, errCode);
}
public boolean noFile() {
return md5.length == 0;
}
/**
* Serializes the Object to a buffer
*
* @param buf The buffer to write to
*/
public void serialize(FriendlyByteBuf buf) {
buf.writeInt(size);
buf.writeInt(value);
buf.writeByteArray(md5);
}
/**
* Deserialize a Buffer to a new {@link SyncFileHash}-Object
*
* @param buf Thea buffer to read from
* @return The received String
*/
public static FileHash deserialize(FriendlyByteBuf buf) {
final int size = buf.readInt();
final int value = buf.readInt();
final byte[] md5 = buf.readByteArray();
return new FileHash(md5, size, value);
}
/**
* Convert the md5-hash to a human readable string
*
* @return The converted String
*/
public String getMd5String() {
return toHexString(md5);
}
/**
* Converts a byte-array to a hex-string representation
*
* @param bytes The source array
* @return The resulting string, or an empty String if the input was {@code null}
*/
public static String toHexString(byte[] bytes) {
if (bytes == null) return "";
StringBuilder sb = new StringBuilder();
for (byte b : bytes) {
sb.append(String.format("%02x", b));
}
return sb.toString();
}
/**
* Create a new {@link FileHash}.
*
* @param file The input file
* @return A new Instance. You can compare instances using {@link #equals(Object)} to determine if two files are
* identical. Will return {@code null} when an error occurs or the File does not exist
*/
public static FileHash create(File file) {
if (!file.exists()) return createForEmpty(ERR_DOES_NOT_EXIST);
final Path path = file.toPath();
int size = 0;
byte[] md5 = new byte[0];
int value = 0;
try {
byte[] data = Files.readAllBytes(path);
size = data.length;
value = size > 0 ? (data[size / 3] | (data[size / 2] << 8) | (data[size / 5] << 16)) : -1;
if (size > 20) value |= data[20] << 24;
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(data);
md5 = md.digest();
return new FileHash(md5, size, value);
}
catch (IOException e) {
BCLib.LOGGER.error("Failed to read file: " + file);
return null;
}
catch (NoSuchAlgorithmException e) {
BCLib.LOGGER.error("Unable to build hash for file: " + file);
}
return createForEmpty(ERR_IO_ERROR);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof FileHash)) return false;
FileHash fileHash = (FileHash) o;
return size == fileHash.size && value == fileHash.value && Arrays.equals(md5, fileHash.md5);
}
@Override
public int hashCode() {
int result = Objects.hash(size, value);
result = 31 * result + Arrays.hashCode(md5);
return result;
}
@Override
public String toString() {
return String.format("%08x", size) + "-" + String.format("%08x", value) + "-" + getMd5String();
}
}

View file

@ -0,0 +1,108 @@
package ru.bclib.api.dataexchange;
import net.minecraft.network.FriendlyByteBuf;
import ru.bclib.api.dataexchange.handler.autosync.AutoSync.NeedTransferPredicate;
import ru.bclib.api.dataexchange.handler.autosync.AutoSyncID;
import java.io.File;
import java.util.Objects;
/**
* Calculates a hash based on the contents of a File.
* <p>
* A File-Hash contains the md5-sum of the File, as well as its size and byte-values from defined positions
* <p>
* You can compare instances using {@link #equals(Object)} to determine if two files are
* identical.
*/
public class SyncFileHash extends AutoSyncID {
public final FileHash hash;
SyncFileHash(String modID, File file, byte[] md5, int size, int value) {
this(modID, file.getName(), md5, size, value);
}
SyncFileHash(String modID, String uniqueID, byte[] md5, int size, int value) {
this(modID, uniqueID, new FileHash(md5, size, value));
}
SyncFileHash(String modID, File file, FileHash hash) {
this(modID, file.getName(), hash);
}
SyncFileHash(String modID, String uniqueID, FileHash hash) {
super(modID, uniqueID);
this.hash = hash;
}
final static NeedTransferPredicate NEED_TRANSFER = (clientHash, serverHash, content)-> !clientHash.equals(serverHash);
@Override
public String toString() {
return super.toString()+": "+hash.toString();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof SyncFileHash)) return false;
if (!super.equals(o)) return false;
SyncFileHash that = (SyncFileHash) o;
return hash.equals(that.hash);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), hash);
}
/**
* Serializes the Object to a buffer
* @param buf The buffer to write to
*/
public void serialize(FriendlyByteBuf buf) {
hash.serialize(buf);
DataHandler.writeString(buf, modID);
DataHandler.writeString(buf, uniqueID);
}
/**
*Deserialize a Buffer to a new {@link SyncFileHash}-Object
* @param buf Thea buffer to read from
* @return The received String
*/
public static SyncFileHash deserialize(FriendlyByteBuf buf){
final FileHash hash = FileHash.deserialize(buf);
final String modID = DataHandler.readString(buf);
final String uniqueID = DataHandler.readString(buf);
return new SyncFileHash(modID, uniqueID, hash);
}
/**
* Create a new {@link SyncFileHash}.
* <p>
* Will call {@link #create(String, File, String)} using the name of the File as {@code uniqueID}.
* @param modID ID of the calling Mod
* @param file The input file
*
* @return A new Instance. You can compare instances using {@link #equals(Object)} to determine if two files are
* identical. Will return {@code null} when an error occurs or the File does not exist
*/
public static SyncFileHash create(String modID, File file){
return create(modID, file, file.getName());
}
/**
* Create a new {@link SyncFileHash}.
* @param modID ID of the calling Mod
* @param file The input file
* @param uniqueID The unique ID that is used for this File (see {@link SyncFileHash#uniqueID} for Details.
* @return A new Instance. You can compare instances using {@link #equals(Object)} to determine if two files are
* identical. Will return {@code null} when an error occurs or the File does not exist
*/
public static SyncFileHash create(String modID, File file, String uniqueID){
return new SyncFileHash(modID, uniqueID, FileHash.create(file));
}
}

View file

@ -0,0 +1,113 @@
package ru.bclib.api.dataexchange.handler;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
import net.minecraft.resources.ResourceLocation;
import ru.bclib.api.dataexchange.BaseDataHandler;
import ru.bclib.api.dataexchange.ConnectorClientside;
import ru.bclib.api.dataexchange.ConnectorServerside;
import ru.bclib.api.dataexchange.DataExchangeAPI;
import ru.bclib.api.dataexchange.DataHandler;
import ru.bclib.api.dataexchange.DataHandlerDescriptor;
import java.util.HashSet;
import java.util.Set;
abstract public class DataExchange {
private static DataExchangeAPI instance;
protected static DataExchangeAPI getInstance() {
if (instance == null) {
instance = new DataExchangeAPI();
}
return instance;
}
protected ConnectorServerside server;
protected ConnectorClientside client;
protected final Set<DataHandlerDescriptor> descriptors;
private boolean didLoadSyncFolder = false;
abstract protected ConnectorClientside clientSupplier(DataExchange api);
abstract protected ConnectorServerside serverSupplier(DataExchange api);
protected DataExchange() {
descriptors = new HashSet<>();
}
public Set<DataHandlerDescriptor> getDescriptors() { return descriptors; }
public static DataHandlerDescriptor getDescriptor(ResourceLocation identifier){
return getInstance().descriptors.stream().filter(d -> d.equals(identifier)).findFirst().orElse(null);
}
@Environment(EnvType.CLIENT)
protected void initClientside() {
if (client != null) return;
client = clientSupplier(this);
ClientPlayConnectionEvents.INIT.register(client::onPlayInit);
ClientPlayConnectionEvents.JOIN.register(client::onPlayReady);
ClientPlayConnectionEvents.DISCONNECT.register(client::onPlayDisconnect);
}
protected void initServerSide() {
if (server != null) return;
server = serverSupplier(this);
ServerPlayConnectionEvents.INIT.register(server::onPlayInit);
ServerPlayConnectionEvents.JOIN.register(server::onPlayReady);
ServerPlayConnectionEvents.DISCONNECT.register(server::onPlayDisconnect);
}
/**
* Initializes all datastructures that need to exist in the client component.
* <p>
* This is automatically called by BCLib. You can register {@link DataHandler}-Objects before this Method is called
*/
@Environment(EnvType.CLIENT)
public static void prepareClientside() {
DataExchange api = DataExchange.getInstance();
api.initClientside();
}
/**
* Initializes all datastructures that need to exist in the server component.
* <p>
* This is automatically called by BCLib. You can register {@link DataHandler}-Objects before this Method is called
*/
public static void prepareServerside() {
DataExchange api = DataExchange.getInstance();
api.initServerSide();
}
/**
* Automatically called before the player enters the world.
* <p>
* This is automatically called by BCLib. It will send all {@link DataHandler}-Objects that have {@link DataHandlerDescriptor#sendBeforeEnter} set to*
* {@code true},
*/
@Environment(EnvType.CLIENT)
public static void sendOnEnter() {
getInstance().descriptors.forEach((desc) -> {
if (desc.sendBeforeEnter) {
BaseDataHandler h = desc.JOIN_INSTANCE.get();
if (!h.getOriginatesOnServer()) {
getInstance().client.sendToServer(h);
}
}
});
}
}

View file

@ -0,0 +1,237 @@
package ru.bclib.api.dataexchange.handler.autosync;
import net.minecraft.network.FriendlyByteBuf;
import ru.bclib.BCLib;
import ru.bclib.api.dataexchange.DataHandler;
import ru.bclib.api.dataexchange.SyncFileHash;
import ru.bclib.api.dataexchange.handler.autosync.AutoSync.NeedTransferPredicate;
import ru.bclib.api.dataexchange.handler.autosync.SyncFolderDescriptor.SubFile;
import ru.bclib.util.ModUtil;
import ru.bclib.util.ModUtil.ModInfo;
import ru.bclib.util.Pair;
import ru.bclib.util.PathUtil;
import ru.bclib.util.Triple;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
class AutoFileSyncEntry extends AutoSyncID {
static class ForDirectFileRequest extends AutoFileSyncEntry {
final File relFile;
ForDirectFileRequest(String syncID, File relFile, File absFile) {
super(AutoSyncID.ForDirectFileRequest.MOD_ID, syncID, absFile, false, (a, b, c) -> false);
this.relFile = relFile;
}
@Override
public int serializeContent(FriendlyByteBuf buf) {
int res = super.serializeContent(buf);
DataHandler.writeString(buf, relFile.toString());
return res;
}
static AutoFileSyncEntry.ForDirectFileRequest finishDeserializeContent(String syncID, FriendlyByteBuf buf) {
final String relFile = DataHandler.readString(buf);
SyncFolderDescriptor desc = AutoSync.getSyncFolderDescriptor(syncID);
if (desc != null) {
//ensures that the file is not above the base-folder
if (desc.acceptChildElements(desc.mapAbsolute(relFile))) {
return new AutoFileSyncEntry.ForDirectFileRequest(syncID, new File(relFile), desc.localFolder.resolve(relFile)
.normalize()
.toFile());
}
}
return null;
}
@Override
public String toString() {
return uniqueID + " - " + relFile;
}
}
static class ForModFileRequest extends AutoFileSyncEntry {
public static File getLocalPathForID(String modID, boolean matchLocalVersion){
ModInfo mi = ModUtil.getModInfo(modID, matchLocalVersion);
if (mi!=null){
return mi.jarPath.toFile();
}
return null;
}
public final String version;
ForModFileRequest(String modID, boolean matchLocalVersion, String version) {
super(modID, AutoSyncID.ForModFileRequest.UNIQUE_ID, getLocalPathForID(modID, matchLocalVersion), false, (a, b, c) -> false);
if (this.fileName == null && matchLocalVersion){
BCLib.LOGGER.error("Unknown mod '"+modID+"'.");
}
if (version==null)
this.version = ModUtil.getModVersion(modID);
else
this.version = version;
}
@Override
public int serializeContent(FriendlyByteBuf buf) {
final int res = super.serializeContent(buf);
buf.writeInt(ModUtil.convertModVersion(version));
return res;
}
static AutoFileSyncEntry.ForModFileRequest finishDeserializeContent(String modID, FriendlyByteBuf buf) {
final String version = ModUtil.convertModVersion(buf.readInt());
return new AutoFileSyncEntry.ForModFileRequest(modID, false, version);
}
@Override
public String toString() {
return "Mod " + modID + " (v" + version + ")";
}
}
public final NeedTransferPredicate needTransfer;
public final File fileName;
public final boolean requestContent;
private SyncFileHash hash;
AutoFileSyncEntry(String modID, File fileName, boolean requestContent, NeedTransferPredicate needTransfer) {
this(modID, fileName.getName(), fileName, requestContent, needTransfer);
}
AutoFileSyncEntry(String modID, String uniqueID, File fileName, boolean requestContent, NeedTransferPredicate needTransfer) {
super(modID, uniqueID);
this.needTransfer = needTransfer;
this.fileName = fileName;
this.requestContent = requestContent;
}
public SyncFileHash getFileHash() {
if (hash == null) {
hash = SyncFileHash.create(modID, fileName, uniqueID);
}
return hash;
}
public byte[] getContent() {
if (!fileName.exists()) return new byte[0];
final Path path = fileName.toPath();
try {
return Files.readAllBytes(path);
}
catch (IOException e) {
}
return new byte[0];
}
public int serializeContent(FriendlyByteBuf buf) {
DataHandler.writeString(buf, modID);
DataHandler.writeString(buf, uniqueID);
return serializeFileContent(buf);
}
public static Triple<AutoFileSyncEntry, byte[], AutoSyncID> deserializeContent(FriendlyByteBuf buf) {
final String modID = DataHandler.readString(buf);
final String uniqueID = DataHandler.readString(buf);
byte[] data = deserializeFileContent(buf);
AutoFileSyncEntry entry;
if (AutoSyncID.ForDirectFileRequest.MOD_ID.equals(modID)) {
entry = AutoFileSyncEntry.ForDirectFileRequest.finishDeserializeContent(uniqueID, buf);
}
else if (AutoSyncID.ForModFileRequest.UNIQUE_ID.equals(uniqueID)) {
entry = AutoFileSyncEntry.ForModFileRequest.finishDeserializeContent(modID, buf);
}
else {
entry = AutoFileSyncEntry.findMatching(modID, uniqueID);
}
return new Triple<>(entry, data, new AutoSyncID(modID, uniqueID));
}
public void serialize(FriendlyByteBuf buf) {
getFileHash().serialize(buf);
buf.writeBoolean(requestContent);
if (requestContent) {
serializeFileContent(buf);
}
}
public static AutoSync.AutoSyncTriple deserializeAndMatch(FriendlyByteBuf buf) {
Pair<SyncFileHash, byte[]> e = deserialize(buf);
AutoFileSyncEntry match = findMatching(e.first);
return new AutoSync.AutoSyncTriple(e.first, e.second, match);
}
public static Pair<SyncFileHash, byte[]> deserialize(FriendlyByteBuf buf) {
SyncFileHash hash = SyncFileHash.deserialize(buf);
boolean withContent = buf.readBoolean();
byte[] data = null;
if (withContent) {
data = deserializeFileContent(buf);
}
return new Pair(hash, data);
}
private int serializeFileContent(FriendlyByteBuf buf) {
if (!PathUtil.isChildOf(PathUtil.GAME_FOLDER, fileName.toPath())){
BCLib.LOGGER.error(fileName + " is not within game folder " + PathUtil.GAME_FOLDER + ". Pretending it does not exist.");
buf.writeInt(0);
return 0;
}
byte[] content = getContent();
buf.writeInt(content.length);
buf.writeByteArray(content);
return content.length;
}
private static byte[] deserializeFileContent(FriendlyByteBuf buf) {
byte[] data;
int size = buf.readInt();
data = buf.readByteArray(size);
return data;
}
public static AutoFileSyncEntry findMatching(SyncFileHash hash) {
return findMatching(hash.modID, hash.uniqueID);
}
public static AutoFileSyncEntry findMatching(AutoSyncID aid) {
if (aid instanceof AutoSyncID.ForDirectFileRequest) {
AutoSyncID.ForDirectFileRequest freq = (AutoSyncID.ForDirectFileRequest) aid;
SyncFolderDescriptor desc = AutoSync.getSyncFolderDescriptor(freq.uniqueID);
if (desc != null) {
SubFile subFile = desc.getLocalSubFile(freq.relFile.toString());
if (subFile != null) {
final File absPath = desc.localFolder.resolve(subFile.relPath)
.normalize()
.toFile();
return new AutoFileSyncEntry.ForDirectFileRequest(freq.uniqueID, new File(subFile.relPath), absPath);
}
}
return null;
} else if (aid instanceof AutoSyncID.ForModFileRequest) {
AutoSyncID.ForModFileRequest mreq = (AutoSyncID.ForModFileRequest) aid;
return new AutoFileSyncEntry.ForModFileRequest(mreq.modID, true, null);
}
return findMatching(aid.modID, aid.uniqueID);
}
public static AutoFileSyncEntry findMatching(String modID, String uniqueID) {
return AutoSync.getAutoSyncFiles()
.stream()
.filter(asf -> asf.modID.equals(modID) && asf.uniqueID.equals(uniqueID))
.findFirst()
.orElse(null);
}
}

View file

@ -0,0 +1,187 @@
package ru.bclib.api.dataexchange.handler.autosync;
import net.fabricmc.loader.api.FabricLoader;
import ru.bclib.BCLib;
import ru.bclib.api.dataexchange.DataExchangeAPI;
import ru.bclib.api.dataexchange.SyncFileHash;
import ru.bclib.config.Configs;
import ru.bclib.config.ServerConfig;
import ru.bclib.util.PathUtil;
import java.io.File;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.BiConsumer;
public class AutoSync {
public static final String SYNC_CATEGORY = "auto_sync";
public final static SyncFolderDescriptor SYNC_FOLDER = new SyncFolderDescriptor("BCLIB-SYNC", FabricLoader.getInstance()
.getGameDir()
.resolve("bclib-sync")
.normalize()
.toAbsolutePath(), true);
@FunctionalInterface
public interface NeedTransferPredicate {
public boolean test(SyncFileHash clientHash, SyncFileHash serverHash, FileContentWrapper content);
}
final static class AutoSyncTriple {
public final SyncFileHash serverHash;
public final byte[] serverContent;
public final AutoFileSyncEntry localMatch;
public AutoSyncTriple(SyncFileHash serverHash, byte[] serverContent, AutoFileSyncEntry localMatch) {
this.serverHash = serverHash;
this.serverContent = serverContent;
this.localMatch = localMatch;
}
@Override
public String toString() {
return serverHash.modID + "." + serverHash.uniqueID;
}
}
// ##### File Syncing
protected final static List<BiConsumer<AutoSyncID, File>> onWriteCallbacks = new ArrayList<>(2);
/**
* Register a function that is called whenever the client receives a file from the server and replaced toe local
* file with the new content.
* <p>
* This callback is usefull if you need to reload the new content before the game is quit.
*
* @param callback A Function that receives the AutoSyncID as well as the Filename.
*/
public static void addOnWriteCallback(BiConsumer<AutoSyncID, File> callback) {
onWriteCallbacks.add(callback);
}
private static final List<AutoFileSyncEntry> autoSyncFiles = new ArrayList<>(4);
public static List<AutoFileSyncEntry> getAutoSyncFiles() {
return autoSyncFiles;
}
/**
* Registers a File for automatic client syncing.
*
* @param modID The ID of the calling Mod
* @param needTransfer If the predicate returns true, the file needs to get copied to the server.
* @param fileName The name of the File
* @param requestContent When {@code true} the content of the file is requested for comparison. This will copy the
* entire file from the client to the server.
* <p>
* You should only use this option, if you need to compare parts of the file in order to decide
* If the File needs to be copied. Normally using the {@link SyncFileHash}
* for comparison is sufficient.
*/
public static void addAutoSyncFileData(String modID, File fileName, boolean requestContent, NeedTransferPredicate needTransfer) {
if (!PathUtil.isChildOf(PathUtil.GAME_FOLDER, fileName.toPath())){
BCLib.LOGGER.error(fileName + " is outside of Game Folder " + PathUtil.GAME_FOLDER);
} else {
autoSyncFiles.add(new AutoFileSyncEntry(modID, fileName, requestContent, needTransfer));
}
}
/**
* Registers a File for automatic client syncing.
*
* @param modID The ID of the calling Mod
* @param uniqueID A unique Identifier for the File. (see {@link SyncFileHash#uniqueID} for
* Details
* @param needTransfer If the predicate returns true, the file needs to get copied to the server.
* @param fileName The name of the File
* @param requestContent When {@code true} the content of the file is requested for comparison. This will copy the
* entire file from the client to the server.
* <p>
* You should only use this option, if you need to compare parts of the file in order to decide
* If the File needs to be copied. Normally using the {@link SyncFileHash}
* for comparison is sufficient.
*/
public static void addAutoSyncFileData(String modID, String uniqueID, File fileName, boolean requestContent, NeedTransferPredicate needTransfer) {
if (!PathUtil.isChildOf(PathUtil.GAME_FOLDER, fileName.toPath())){
BCLib.LOGGER.error(fileName + " is outside of Game Folder " + PathUtil.GAME_FOLDER);
} else {
autoSyncFiles.add(new AutoFileSyncEntry(modID, uniqueID, fileName, requestContent, needTransfer));
}
}
/**
* Called when {@code SendFiles} received a File on the Client and wrote it to the FileSystem.
* <p>
* This is the place where reload Code should go.
*
* @param aid The ID of the received File
* @param file The location of the FIle on the client
*/
static void didReceiveFile(AutoSyncID aid, File file) {
onWriteCallbacks.forEach(fkt -> fkt.accept(aid, file));
}
// ##### Folder Syncing
static final List<SyncFolderDescriptor> syncFolderDescriptions = Arrays.asList(SYNC_FOLDER);
private List<String> syncFolderContent;
protected List<String> getSyncFolderContent() {
if (syncFolderContent == null) {
return new ArrayList<>(0);
}
return syncFolderContent;
}
private static boolean didRegisterAdditionalMods = false;
//we call this from HelloClient on the Server to prepare transfer
protected static void loadSyncFolder() {
if (Configs.SERVER_CONFIG.isOfferingFiles()) {
syncFolderDescriptions.forEach(desc -> desc.loadCache());
}
if (!didRegisterAdditionalMods && Configs.SERVER_CONFIG.isOfferingMods()){
didRegisterAdditionalMods = true;
List<String> modIDs = Configs.SERVER_CONFIG.get(ServerConfig.ADDITIONAL_MODS);
if (modIDs != null){
modIDs.stream().forEach(modID -> DataExchangeAPI.registerModDependency(modID));
}
}
}
protected static SyncFolderDescriptor getSyncFolderDescriptor(String folderID) {
return syncFolderDescriptions.stream()
.filter(d -> d.equals(folderID))
.findFirst()
.orElse(null);
}
protected static Path localBasePathForFolderID(String folderID) {
final SyncFolderDescriptor desc = getSyncFolderDescriptor(folderID);
if (desc != null) {
return desc.localFolder;
}
else {
BCLib.LOGGER.warning("Unknown Sync-Folder ID '" + folderID + "'");
return null;
}
}
public static void registerSyncFolder(String folderID, Path localBaseFolder, boolean removeAdditionalFiles) {
localBaseFolder = localBaseFolder.normalize();
if (PathUtil.isChildOf(PathUtil.GAME_FOLDER, localBaseFolder)) {
final SyncFolderDescriptor desc = new SyncFolderDescriptor(folderID, localBaseFolder, removeAdditionalFiles);
if (syncFolderDescriptions.contains(desc)) {
BCLib.LOGGER.warning("Tried to override Folder Sync '" + folderID + "' again.");
}
else {
syncFolderDescriptions.add(desc);
}
}
else {
BCLib.LOGGER.error(localBaseFolder + " (from " + folderID + ") is outside the game directory " + PathUtil.GAME_FOLDER + ". Sync is not allowed.");
}
}
}

View file

@ -0,0 +1,142 @@
package ru.bclib.api.dataexchange.handler.autosync;
import net.minecraft.network.FriendlyByteBuf;
import org.jetbrains.annotations.NotNull;
import ru.bclib.api.dataexchange.DataHandler;
import ru.bclib.config.Config;
import ru.bclib.util.ModUtil;
import java.io.File;
import java.util.Objects;
public class AutoSyncID {
static class WithContentOverride extends AutoSyncID {
final FileContentWrapper contentWrapper;
final File localFile;
WithContentOverride(String modID, String uniqueID, FileContentWrapper contentWrapper, File localFile) {
super(modID, uniqueID);
this.contentWrapper = contentWrapper;
this.localFile = localFile;
}
@Override
public String toString() {
return super.toString() + " (Content override)";
}
}
static class ForDirectFileRequest extends AutoSyncID {
public final static String MOD_ID = "bclib::FILE";
final File relFile;
ForDirectFileRequest(String syncID, File relFile) {
super(ForDirectFileRequest.MOD_ID, syncID);
this.relFile = relFile;
}
@Override
void serializeData(FriendlyByteBuf buf) {
super.serializeData(buf);
DataHandler.writeString(buf, relFile.toString());
}
static ForDirectFileRequest finishDeserialize(String modID, String uniqueID, FriendlyByteBuf buf){
final File fl = new File(DataHandler.readString(buf));
return new ForDirectFileRequest(uniqueID, fl);
}
@Override
public String toString() {
return super.uniqueID + " (" + this.relFile + ")";
}
}
static class ForModFileRequest extends AutoSyncID {
public final static String UNIQUE_ID = "bclib::MOD";
private final String version;
ForModFileRequest(String modID, String version) {
super(modID, ForModFileRequest.UNIQUE_ID);
this.version = version;
}
@Override
void serializeData(FriendlyByteBuf buf) {
super.serializeData(buf);
buf.writeInt(ModUtil.convertModVersion(version));
}
static ForModFileRequest finishDeserialize(String modID, String uniqueID, FriendlyByteBuf buf){
final String version = ModUtil.convertModVersion(buf.readInt());
return new ForModFileRequest(modID, version);
}
@Override
public String toString() {
return super.modID + " (v" + this.version + ")";
}
}
/**
* A Unique ID for the referenced File.
* <p>
* Files with the same {@link #modID} need to have a unique IDs. Normally the filename from FileHash(String, File, byte[], int, int)
* is used to generated that ID, but you can directly specify one using FileHash(String, String, byte[], int, int).
*/
@NotNull
public final String uniqueID;
/**
* The ID of the Mod that is registering the File
*/
@NotNull
public final String modID;
public AutoSyncID(String modID, String uniqueID) {
Objects.nonNull(modID);
Objects.nonNull(uniqueID);
this.modID = modID;
this.uniqueID = uniqueID;
}
@Override
public String toString() {
return modID + "." + uniqueID;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof AutoSyncID)) return false;
AutoSyncID that = (AutoSyncID) o;
return uniqueID.equals(that.uniqueID) && modID.equals(that.modID);
}
@Override
public int hashCode() {
return Objects.hash(uniqueID, modID);
}
void serializeData(FriendlyByteBuf buf) {
DataHandler.writeString(buf, modID);
DataHandler.writeString(buf, uniqueID);
}
static AutoSyncID deserializeData(FriendlyByteBuf buf){
String modID = DataHandler.readString(buf);
String uID = DataHandler.readString(buf);
if (ForDirectFileRequest.MOD_ID.equals(modID)){
return ForDirectFileRequest.finishDeserialize(modID, uID, buf);
} else if (ForModFileRequest.UNIQUE_ID.equals(uID)){
return ForModFileRequest.finishDeserialize(modID, uID, buf);
} else{
return new AutoSyncID(modID, uID);
}
}
public boolean isConfigFile(){
return this.uniqueID.startsWith(Config.CONFIG_SYNC_PREFIX);
}
}

View file

@ -0,0 +1,270 @@
package ru.bclib.api.dataexchange.handler.autosync;
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
import net.fabricmc.fabric.api.networking.v1.PacketSender;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.client.Minecraft;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.util.ProgressListener;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import ru.bclib.BCLib;
import ru.bclib.api.dataexchange.BaseDataHandler;
import ru.bclib.api.dataexchange.DataHandler;
import ru.bclib.api.dataexchange.DataHandlerDescriptor;
import ru.bclib.api.dataexchange.handler.DataExchange;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
/**
* Used to seperate large data transfers into multiple smaller messages.
* <p>
* {@link DataHandler} will automatically convert larger messages into Chunks on the Server
* and assemble the original message from those chunks on the client.
*/
public class Chunker extends DataHandler.FromServer {
/**
* Responsible for assembling the original ByteBuffer created by {@link PacketChunkSender} on the
* receiving end. Automatically created from the header {@link Chunker}-Message (where the serialNo==-1)
*/
static class PacketChunkReceiver {
@NotNull
public final UUID uuid;
public final int chunkCount;
@NotNull
private final FriendlyByteBuf networkedBuf;
@Nullable
private final DataHandlerDescriptor descriptor;
private static List<PacketChunkReceiver> active = new ArrayList<>(1);
private static PacketChunkReceiver newReceiver(@NotNull UUID uuid, int chunkCount, ResourceLocation origin){
DataHandlerDescriptor desc = DataExchange.getDescriptor(origin);
final PacketChunkReceiver r = new PacketChunkReceiver(uuid, chunkCount, desc);
active.add(r);
return r;
}
private static PacketChunkReceiver getOrCreate(@NotNull UUID uuid, int chunkCount, ResourceLocation origin){
return active.stream().filter(r -> r.uuid.equals(uuid)).findFirst().orElse(newReceiver(uuid, chunkCount, origin));
}
public static PacketChunkReceiver get(@NotNull UUID uuid){
return active.stream().filter(r -> r.uuid.equals(uuid)).findFirst().orElse(null);
}
private PacketChunkReceiver(@NotNull UUID uuid, int chunkCount, @Nullable DataHandlerDescriptor descriptor){
this.uuid = uuid;
this.chunkCount = chunkCount;
networkedBuf = PacketByteBufs.create();
this.descriptor = descriptor;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof PacketChunkReceiver)) return false;
PacketChunkReceiver that = (PacketChunkReceiver) o;
return uuid.equals(that.uuid);
}
@Override
public int hashCode() {
return Objects.hash(uuid);
}
public boolean testFinished(){
ProgressListener listener = ChunkerProgress.getProgressListener();
if (listener!=null){
listener.progressStagePercentage((100*receivedCount)/chunkCount);
}
if (incomingBuffer == null){
return true;
} if (lastReadSerial>=chunkCount-1){
onFinish();
return true;
}
return false;
}
private void addBuffer(FriendlyByteBuf input){
final int size = input.readableBytes();
final int cap = networkedBuf.capacity()-networkedBuf.writerIndex();
if (cap < size){
networkedBuf.capacity(networkedBuf.writerIndex() + size);
}
input.readBytes(networkedBuf, size);
input.clear();
}
protected void onFinish(){
incomingBuffer.clear();
incomingBuffer = null;
final BaseDataHandler baseHandler = descriptor.INSTANCE.get();
if (baseHandler instanceof DataHandler.FromServer handler){
handler.receiveFromMemory(networkedBuf);
}
}
Map<Integer, FriendlyByteBuf> incomingBuffer = new HashMap<>();
int lastReadSerial = -1;
int receivedCount = 0;
public void processReceived(FriendlyByteBuf buf, int serialNo, int size){
receivedCount++;
if (lastReadSerial == serialNo-1){
addBuffer(buf);
lastReadSerial = serialNo;
} else {
//not sure if order is guaranteed by the underlying system!
boolean haveAll = true;
for (int nr = lastReadSerial+1; nr < serialNo-1; nr++){
if (incomingBuffer.get(nr) == null){
haveAll = false;
break;
}
}
if (haveAll){
for (int nr = lastReadSerial+1; nr < serialNo-1; nr++){
addBuffer(incomingBuffer.get(nr));
incomingBuffer.put(nr, null);
}
addBuffer(buf);
lastReadSerial = serialNo;
} else {
incomingBuffer.put(serialNo, buf);
}
}
}
}
/**
* Responsible for splitting an outgoing ByteBuffer into several smaller Chunks and
* send them as seperate messages to the {@link Chunker}-Channel
*/
public static class PacketChunkSender {
private final FriendlyByteBuf networkedBuf;
public final UUID uuid;
public final int chunkCount;
public final int size;
public final ResourceLocation origin;
public PacketChunkSender(FriendlyByteBuf buf, ResourceLocation origin){
networkedBuf = buf;
size = buf.readableBytes();
chunkCount = (int)Math.ceil((double)size / MAX_PAYLOAD_SIZE);
uuid = UUID.randomUUID();
this.origin = origin;
}
public void sendChunks(Collection<ServerPlayer> players){
BCLib.LOGGER.info("Sending Request in " + chunkCount + " Packet-Chunks");
for (int i=-1; i<chunkCount; i++){
Chunker c = new Chunker(i, uuid, networkedBuf, chunkCount, origin);
FriendlyByteBuf buf = PacketByteBufs.create();
c.serializeDataOnServer(buf);
for (ServerPlayer player : players){
ServerPlayNetworking.send(player, DESCRIPTOR.IDENTIFIER, buf);
}
}
}
}
//header = version + UUID + serialNo + size, see serializeDataOnServer
private static final int HEADER_SIZE = 1 + 16 + 4 + 4;
public static final int MAX_PACKET_SIZE = 1024*1024;
private static final int MAX_PAYLOAD_SIZE = MAX_PACKET_SIZE - HEADER_SIZE;
public static DataHandlerDescriptor DESCRIPTOR = new DataHandlerDescriptor(new ResourceLocation(BCLib.MOD_ID, "chunker"), Chunker::new, false, false);
private int serialNo;
private UUID uuid;
private int chunkCount;
private FriendlyByteBuf networkedBuf;
private ResourceLocation origin;
protected Chunker(int serialNo, UUID uuid, FriendlyByteBuf networkedBuf, int chunkCount, ResourceLocation origin) {
super(DESCRIPTOR.IDENTIFIER);
this.serialNo = serialNo;
this.uuid = uuid;
this.networkedBuf = networkedBuf;
this.chunkCount = chunkCount;
this.origin = origin;
}
protected Chunker(){
super(DESCRIPTOR.IDENTIFIER);
}
@Override
protected void serializeDataOnServer(FriendlyByteBuf buf) {
//Sending Header. Make sure to change HEADER_SIZE if you change this!
buf.writeByte(0);
buf.writeLong(uuid.getMostSignificantBits());
buf.writeLong(uuid.getLeastSignificantBits());
buf.writeInt(serialNo);
//sending Payload
if (serialNo == -1){
//this is our header-Chunk that transports status information
buf.writeInt(chunkCount);
writeString(buf, origin.getNamespace());
writeString(buf, origin.getPath());
} else {
//this is an actual payload chunk
buf.capacity(MAX_PACKET_SIZE);
final int size = Math.min(MAX_PAYLOAD_SIZE, networkedBuf.readableBytes());
buf.writeInt(size);
networkedBuf.readBytes(buf, size);
}
}
private PacketChunkReceiver receiver;
@Override
protected void deserializeIncomingDataOnClient(FriendlyByteBuf buf, PacketSender responseSender) {
final int version = buf.readByte();
uuid = new UUID(buf.readLong(), buf.readLong());
serialNo = buf.readInt();
if (serialNo == -1){
chunkCount = buf.readInt();
final String namespace = readString(buf);
final String path = readString(buf);
ResourceLocation ident = new ResourceLocation(namespace, path);
BCLib.LOGGER.info("Receiving " + chunkCount + " + Packet-Chunks for " + ident);
receiver = PacketChunkReceiver.getOrCreate(uuid, chunkCount, ident);
} else {
receiver = PacketChunkReceiver.get(uuid);
if (receiver!=null) {
final int size = buf.readInt();
receiver.processReceived(buf, serialNo, size);
} else {
BCLib.LOGGER.error("Unknown Packet-Chunk Transfer for " + uuid);
}
}
}
@Override
protected void runOnClientGameThread(Minecraft client) {
if (receiver!=null){
receiver.testFinished();
}
}
}

View file

@ -0,0 +1,26 @@
package ru.bclib.api.dataexchange.handler.autosync;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.util.ProgressListener;
import ru.bclib.gui.screens.ProgressScreen;
@Environment(EnvType.CLIENT)
public class ChunkerProgress {
private static ProgressScreen progressScreen;
@Environment(EnvType.CLIENT)
public static void setProgressScreen(ProgressScreen scr){
progressScreen = scr;
}
@Environment(EnvType.CLIENT)
public static ProgressScreen getProgressScreen(){
return progressScreen;
}
@Environment(EnvType.CLIENT)
public static ProgressListener getProgressListener(){
return progressScreen;
}
}

View file

@ -0,0 +1,77 @@
package ru.bclib.api.dataexchange.handler.autosync;
import ru.bclib.BCLib;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public class FileContentWrapper {
private byte[] rawContent;
private ByteArrayOutputStream outputStream;
FileContentWrapper(byte[] content) {
this.rawContent = content;
this.outputStream = null;
}
public byte[] getOriginalContent() {
return rawContent;
}
public byte[] getRawContent() {
if (outputStream != null) {
return outputStream.toByteArray();
}
return rawContent;
}
private void invalidateOutputStream() {
if (this.outputStream != null) {
try {
this.outputStream.close();
}
catch (IOException e) {
BCLib.LOGGER.debug(e);
}
}
this.outputStream = null;
}
public void setRawContent(byte[] rawContent) {
this.rawContent = rawContent;
invalidateOutputStream();
}
public void syncWithOutputStream() {
if (outputStream != null) {
try {
outputStream.flush();
}
catch (IOException e) {
BCLib.LOGGER.error(e.getMessage());
e.printStackTrace();
}
setRawContent(getRawContent());
invalidateOutputStream();
}
}
public ByteArrayInputStream getInputStream() {
if (rawContent == null) return new ByteArrayInputStream(new byte[0]);
return new ByteArrayInputStream(rawContent);
}
public ByteArrayOutputStream getOrCreateOutputStream() {
if (this.outputStream == null) {
return this.getEmptyOutputStream();
}
return this.outputStream;
}
public ByteArrayOutputStream getEmptyOutputStream() {
invalidateOutputStream();
this.outputStream = new ByteArrayOutputStream(this.rawContent.length);
return this.outputStream;
}
}

View file

@ -0,0 +1,497 @@
package ru.bclib.api.dataexchange.handler.autosync;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.networking.v1.PacketSender;
import net.fabricmc.loader.api.metadata.ModEnvironment;
import net.minecraft.client.Minecraft;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.resources.ResourceLocation;
import ru.bclib.BCLib;
import ru.bclib.api.dataexchange.DataExchangeAPI;
import ru.bclib.api.dataexchange.DataHandler;
import ru.bclib.api.dataexchange.DataHandlerDescriptor;
import ru.bclib.api.dataexchange.handler.autosync.AutoSyncID.WithContentOverride;
import ru.bclib.api.dataexchange.handler.autosync.SyncFolderDescriptor.SubFile;
import ru.bclib.config.Configs;
import ru.bclib.config.ServerConfig;
import ru.bclib.gui.screens.ModListScreen;
import ru.bclib.gui.screens.ProgressScreen;
import ru.bclib.gui.screens.SyncFilesScreen;
import ru.bclib.gui.screens.WarnBCLibVersionMismatch;
import ru.bclib.util.ModUtil;
import ru.bclib.util.ModUtil.ModInfo;
import ru.bclib.util.PathUtil;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.stream.Collectors;
/**
* Sent from the Server to the Client.
* <p>
* For Details refer to {@link HelloServer}
*/
public class HelloClient extends DataHandler.FromServer {
public record OfferedModInfo(String version, int size, boolean canDownload) {
}
public interface IServerModMap extends Map<String, OfferedModInfo> {}
public static class ServerModMap extends HashMap<String, OfferedModInfo> implements IServerModMap {}
public static DataHandlerDescriptor DESCRIPTOR = new DataHandlerDescriptor(new ResourceLocation(BCLib.MOD_ID, "hello_client"), HelloClient::new, false, false);
public HelloClient() {
super(DESCRIPTOR.IDENTIFIER);
}
static String getBCLibVersion() {
return ModUtil.getModVersion(BCLib.MOD_ID);
}
@Override
protected boolean prepareDataOnServer() {
if (!Configs.SERVER_CONFIG.isAllowingAutoSync()) {
BCLib.LOGGER.info("Auto-Sync was disabled on the server.");
return false;
}
AutoSync.loadSyncFolder();
return true;
}
@Override
protected void serializeDataOnServer(FriendlyByteBuf buf) {
final String vbclib = getBCLibVersion();
BCLib.LOGGER.info("Sending Hello to Client. (server=" + vbclib + ")");
//write BCLibVersion (=protocol version)
buf.writeInt(ModUtil.convertModVersion(vbclib));
if (Configs.SERVER_CONFIG.isOfferingMods() || Configs.SERVER_CONFIG.isOfferingInfosForMods()) {
List<String> mods = DataExchangeAPI.registeredMods();
final List<String> inmods = mods;
if (Configs.SERVER_CONFIG.isOfferingAllMods() || Configs.SERVER_CONFIG.isOfferingInfosForMods()){
mods = new ArrayList<>(inmods.size());
mods.addAll(inmods);
mods.addAll(ModUtil
.getMods()
.entrySet()
.stream()
.filter(entry -> entry.getValue().metadata.getEnvironment()!= ModEnvironment.SERVER && !inmods.contains(entry.getKey()))
.map(entry -> entry.getKey())
.collect(Collectors.toList())
);
}
mods = mods
.stream()
.filter(entry -> !Configs.SERVER_CONFIG.get(ServerConfig.EXCLUDED_MODS).contains(entry))
.collect(Collectors.toList());
//write Plugin Versions
buf.writeInt(mods.size());
for (String modID : mods) {
final String ver = ModUtil.getModVersion(modID);
int size = 0;
final ModInfo mi = ModUtil.getModInfo(modID);
if (mi != null) {
try {
size = (int) Files.size(mi.jarPath);
} catch (IOException e) {
BCLib.LOGGER.error("Unable to get File Size: " + e.getMessage());
}
}
writeString(buf, modID);
buf.writeInt(ModUtil.convertModVersion(ver));
buf.writeInt(size);
final boolean canDownload = size>0 && Configs.SERVER_CONFIG.isOfferingMods() && (Configs.SERVER_CONFIG.isOfferingAllMods() || inmods.contains(modID));
buf.writeBoolean(canDownload);
BCLib.LOGGER.info(" - Listing Mod " + modID + " v" + ver + " (size: " + PathUtil.humanReadableFileSize(size) + ", download="+canDownload+")");
}
}
else {
BCLib.LOGGER.info("Server will not list Mods.");
buf.writeInt(0);
}
if (Configs.SERVER_CONFIG.isOfferingFiles() || Configs.SERVER_CONFIG.isOfferingConfigs()) {
//do only include files that exist on the server
final List<AutoFileSyncEntry> existingAutoSyncFiles = AutoSync.getAutoSyncFiles()
.stream()
.filter(e -> e.fileName.exists())
.filter(e -> (e.isConfigFile() && Configs.SERVER_CONFIG.isOfferingConfigs()) || (e instanceof AutoFileSyncEntry.ForDirectFileRequest && Configs.SERVER_CONFIG.isOfferingFiles()))
.collect(Collectors.toList());
//send config Data
buf.writeInt(existingAutoSyncFiles.size());
for (AutoFileSyncEntry entry : existingAutoSyncFiles) {
entry.serialize(buf);
BCLib.LOGGER.info(" - Offering " + (entry.isConfigFile() ? "Config " : "File ") + entry);
}
}
else {
BCLib.LOGGER.info("Server will neither offer Files nor Configs.");
buf.writeInt(0);
}
if (Configs.SERVER_CONFIG.isOfferingFiles()) {
buf.writeInt(AutoSync.syncFolderDescriptions.size());
AutoSync.syncFolderDescriptions.forEach(desc -> {
BCLib.LOGGER.info(" - Offering Folder " + desc.localFolder + " (allowDelete=" + desc.removeAdditionalFiles + ")");
desc.serialize(buf);
});
}
else {
BCLib.LOGGER.info("Server will not offer Sync Folders.");
buf.writeInt(0);
}
buf.writeBoolean(Configs.SERVER_CONFIG.isOfferingInfosForMods());
}
String bclibVersion = "0.0.0";
IServerModMap modVersion = new ServerModMap();
List<AutoSync.AutoSyncTriple> autoSyncedFiles = null;
List<SyncFolderDescriptor> autoSynFolders = null;
boolean serverPublishedModInfo = false;
@Environment(EnvType.CLIENT)
@Override
protected void deserializeIncomingDataOnClient(FriendlyByteBuf buf, PacketSender responseSender) {
//read BCLibVersion (=protocol version)
bclibVersion = ModUtil.convertModVersion(buf.readInt());
final boolean protocolVersion_0_4_1 = ModUtil.isLargerOrEqualVersion(bclibVersion, "0.4.1");
//read Plugin Versions
modVersion = new ServerModMap();
int count = buf.readInt();
for (int i = 0; i < count; i++) {
final String id = readString(buf);
final String version = ModUtil.convertModVersion(buf.readInt());
final int size;
final boolean canDownload;
//since v0.4.1 we also send the size of the mod-File
if (protocolVersion_0_4_1) {
size = buf.readInt();
canDownload = buf.readBoolean();
}
else {
size = 0;
canDownload = true;
}
modVersion.put(id, new OfferedModInfo(version, size, canDownload));
}
//read config Data
count = buf.readInt();
autoSyncedFiles = new ArrayList<>(count);
for (int i = 0; i < count; i++) {
//System.out.println("Deserializing ");
AutoSync.AutoSyncTriple t = AutoFileSyncEntry.deserializeAndMatch(buf);
autoSyncedFiles.add(t);
//System.out.println(t.first);
}
autoSynFolders = new ArrayList<>(1);
//since v0.4.1 we also send the sync folders
if (protocolVersion_0_4_1) {
final int folderCount = buf.readInt();
for (int i = 0; i < folderCount; i++) {
SyncFolderDescriptor desc = SyncFolderDescriptor.deserialize(buf);
autoSynFolders.add(desc);
}
serverPublishedModInfo = buf.readBoolean();
}
}
@Environment(EnvType.CLIENT)
private void processAutoSyncFolder(final List<AutoSyncID> filesToRequest, final List<AutoSyncID.ForDirectFileRequest> filesToRemove) {
if (!Configs.CLIENT_CONFIG.isAcceptingFiles()) {
return;
}
if (autoSynFolders.size() > 0) {
BCLib.LOGGER.info("Folders offered by Server:");
}
autoSynFolders.forEach(desc -> {
//desc contains the fileCache sent from the server, load the local version to get hold of the actual file cache on the client
SyncFolderDescriptor localDescriptor = AutoSync.getSyncFolderDescriptor(desc.folderID);
if (localDescriptor != null) {
BCLib.LOGGER.info(" - " + desc.folderID + " (" + desc.localFolder + ", allowRemove=" + desc.removeAdditionalFiles + ")");
localDescriptor.invalidateCache();
desc.relativeFilesStream()
.filter(desc::discardChildElements)
.forEach(subFile -> {
BCLib.LOGGER.warning(" * " + subFile.relPath + " (REJECTED)");
});
if (desc.removeAdditionalFiles) {
List<AutoSyncID.ForDirectFileRequest> additionalFiles = localDescriptor.relativeFilesStream()
.filter(subFile -> !desc.hasRelativeFile(subFile))
.map(desc::mapAbsolute)
.filter(desc::acceptChildElements)
.map(absPath -> new AutoSyncID.ForDirectFileRequest(desc.folderID, absPath.toFile()))
.collect(Collectors.toList());
additionalFiles.forEach(aid -> BCLib.LOGGER.info(" * " + desc.localFolder.relativize(aid.relFile.toPath()) + " (missing on server)"));
filesToRemove.addAll(additionalFiles);
}
desc.relativeFilesStream()
.filter(desc::acceptChildElements)
.forEach(subFile -> {
SubFile localSubFile = localDescriptor.getLocalSubFile(subFile.relPath);
if (localSubFile != null) {
//the file exists locally, check if the hashes match
if (!localSubFile.hash.equals(subFile.hash)) {
BCLib.LOGGER.info(" * " + subFile.relPath + " (changed)");
filesToRequest.add(new AutoSyncID.ForDirectFileRequest(desc.folderID, new File(subFile.relPath)));
}
else {
BCLib.LOGGER.info(" * " + subFile.relPath);
}
}
else {
//the file is missing locally
BCLib.LOGGER.info(" * " + subFile.relPath + " (missing on client)");
filesToRequest.add(new AutoSyncID.ForDirectFileRequest(desc.folderID, new File(subFile.relPath)));
}
});
//free some memory
localDescriptor.invalidateCache();
}
else {
BCLib.LOGGER.info(" - " + desc.folderID + " (Failed to find)");
}
});
}
@Environment(EnvType.CLIENT)
private void processSingleFileSync(final List<AutoSyncID> filesToRequest) {
final boolean debugHashes = Configs.CLIENT_CONFIG.shouldPrintDebugHashes();
if (autoSyncedFiles.size() > 0) {
BCLib.LOGGER.info("Files offered by Server:");
}
//Handle single sync files
//Single files need to be registered for sync on both client and server
//There are no restrictions to the target folder, but the client decides the final
//location.
for (AutoSync.AutoSyncTriple e : autoSyncedFiles) {
String actionString = "";
FileContentWrapper contentWrapper = new FileContentWrapper(e.serverContent);
if (e.localMatch == null) {
actionString = "(unknown source -> omitting)";
//filesToRequest.add(new AutoSyncID(e.serverHash.modID, e.serverHash.uniqueID));
}
else if (e.localMatch.needTransfer.test(e.localMatch.getFileHash(), e.serverHash, contentWrapper)) {
actionString = "(prepare update)";
//we did not yet receive the new content
if (contentWrapper.getRawContent() == null) {
filesToRequest.add(new AutoSyncID(e.serverHash.modID, e.serverHash.uniqueID));
}
else {
filesToRequest.add(new AutoSyncID.WithContentOverride(e.serverHash.modID, e.serverHash.uniqueID, contentWrapper, e.localMatch.fileName));
}
}
BCLib.LOGGER.info(" - " + e + ": " + actionString);
if (debugHashes) {
BCLib.LOGGER.info(" * " + e.serverHash + " (Server)");
BCLib.LOGGER.info(" * " + e.localMatch.getFileHash() + " (Client)");
BCLib.LOGGER.info(" * local Content " + (contentWrapper.getRawContent() == null));
}
}
}
@Environment(EnvType.CLIENT)
private void processModFileSync(final List<AutoSyncID> filesToRequest, final Set<String> mismatchingMods) {
for (Entry<String, OfferedModInfo> e : modVersion.entrySet()) {
final String localVersion = ModUtil.getModVersion(e.getKey());
final OfferedModInfo serverInfo = e.getValue();
final boolean requestMod = !serverInfo.version.equals(localVersion) && serverInfo.size > 0 && serverInfo.canDownload;
BCLib.LOGGER.info(" - " + e.getKey() + " (client=" + localVersion + ", server=" + serverInfo.version + ", size=" + PathUtil.humanReadableFileSize(serverInfo.size) + (requestMod ? ", requesting" : "") + (serverInfo.canDownload ? "" :", not offered")+ ")");
if (requestMod) {
filesToRequest.add(new AutoSyncID.ForModFileRequest(e.getKey(), serverInfo.version));
}
if (!serverInfo.version.equals(localVersion)) {
mismatchingMods.add(e.getKey());
}
}
mismatchingMods.addAll(ModListScreen.localMissing(modVersion));
mismatchingMods.addAll(ModListScreen.serverMissing(modVersion));
}
@Override
protected boolean isBlocking() {
return true;
}
@Environment(EnvType.CLIENT)
@Override
protected void runOnClientGameThread(Minecraft client) {
if (!Configs.CLIENT_CONFIG.isAllowingAutoSync()) {
BCLib.LOGGER.info("Auto-Sync was disabled on the client.");
return;
}
final String localBclibVersion = getBCLibVersion();
BCLib.LOGGER.info("Received Hello from Server. (client=" + localBclibVersion + ", server=" + bclibVersion + ")");
if (ModUtil.convertModVersion(localBclibVersion) != ModUtil.convertModVersion(bclibVersion)){
showBCLibError(client);
return;
}
final List<AutoSyncID> filesToRequest = new ArrayList<>(2);
final List<AutoSyncID.ForDirectFileRequest> filesToRemove = new ArrayList<>(2);
final Set<String> mismatchingMods = new HashSet<>(2);
processModFileSync(filesToRequest, mismatchingMods);
processSingleFileSync(filesToRequest);
processAutoSyncFolder(filesToRequest, filesToRemove);
//Handle folder sync
//Both client and server need to know about the folder you want to sync
//Files can only get placed within that folder
if ((filesToRequest.size() > 0 || filesToRemove.size() > 0) && ( Configs.CLIENT_CONFIG.isAcceptingMods() || Configs.CLIENT_CONFIG.isAcceptingConfigs() || Configs.CLIENT_CONFIG.isAcceptingFiles())) {
showSyncFilesScreen(client, filesToRequest, filesToRemove);
return;
} else if (serverPublishedModInfo && mismatchingMods.size()>0 && Configs.CLIENT_CONFIG.isShowingModInfo()) {
client.setScreen(new ModListScreen(client.screen, new TranslatableComponent("title.bclib.modmissmatch"), new TranslatableComponent("message.bclib.modmissmatch"), CommonComponents.GUI_PROCEED, ModUtil.getMods(), modVersion));
return;
}
}
@Environment(EnvType.CLIENT)
protected void showBCLibError(Minecraft client) {
BCLib.LOGGER.error("BCLib differs on client and server.");
client.setScreen(new WarnBCLibVersionMismatch((download) -> {
if (download) {
requestBCLibDownload();
this.onCloseSyncFilesScreen();
} else {
Minecraft.getInstance()
.setScreen(null);
}
}));
}
@Environment(EnvType.CLIENT)
protected void showSyncFilesScreen(Minecraft client, List<AutoSyncID> files, final List<AutoSyncID.ForDirectFileRequest> filesToRemove) {
int configFiles = 0;
int singleFiles = 0;
int folderFiles = 0;
int modFiles = 0;
for (AutoSyncID aid : files) {
if (aid.isConfigFile()) {
configFiles++;
}
else if (aid instanceof AutoSyncID.ForModFileRequest) {
modFiles++;
}
else if (aid instanceof AutoSyncID.ForDirectFileRequest) {
folderFiles++;
}
else {
singleFiles++;
}
}
client.setScreen(new SyncFilesScreen(modFiles, configFiles, singleFiles, folderFiles, filesToRemove.size(), modVersion, (downloadMods, downloadConfigs, downloadFiles, removeFiles) -> {
if (downloadMods || downloadConfigs || downloadFiles) {
BCLib.LOGGER.info("Updating local Files:");
List<AutoSyncID.WithContentOverride> localChanges = new ArrayList<>(files.toArray().length);
List<AutoSyncID> requestFiles = new ArrayList<>(files.toArray().length);
files.forEach(aid -> {
if (aid.isConfigFile() && downloadConfigs) {
processOfferedFile(requestFiles, aid);
}
else if (aid instanceof AutoSyncID.ForModFileRequest && downloadMods) {
processOfferedFile(requestFiles, aid);
}
else if (downloadFiles) {
processOfferedFile(requestFiles, aid);
}
});
requestFileDownloads(requestFiles);
}
if (removeFiles) {
filesToRemove.forEach(aid -> {
BCLib.LOGGER.info(" - " + aid.relFile + " (removing)");
aid.relFile.delete();
});
}
this.onCloseSyncFilesScreen();
}));
}
@Environment(EnvType.CLIENT)
private void onCloseSyncFilesScreen(){
Minecraft.getInstance()
.setScreen(ChunkerProgress.getProgressScreen());
}
private void processOfferedFile(List<AutoSyncID> requestFiles, AutoSyncID aid) {
if (aid instanceof WithContentOverride) {
final WithContentOverride aidc = (WithContentOverride) aid;
BCLib.LOGGER.info(" - " + aid + " (updating Content)");
SendFiles.writeSyncedFile(aid, aidc.contentWrapper.getRawContent(), aidc.localFile);
}
else {
requestFiles.add(aid);
BCLib.LOGGER.info(" - " + aid + " (requesting)");
}
}
private void requestBCLibDownload() {
BCLib.LOGGER.warning("Starting download of BCLib");
requestFileDownloads(List.of(new AutoSyncID.ForModFileRequest(BCLib.MOD_ID, bclibVersion)));
}
@Environment(EnvType.CLIENT)
private void requestFileDownloads(List<AutoSyncID> files) {
BCLib.LOGGER.info("Starting download of Files:" + files.size());
final ProgressScreen progress = new ProgressScreen(null, new TranslatableComponent("title.bclib.filesync.progress"), new TranslatableComponent("message.bclib.filesync.progress"));
progress.progressStart(new TranslatableComponent("message.bclib.filesync.progress.stage.empty"));
ChunkerProgress.setProgressScreen(progress);
DataExchangeAPI.send(new RequestFiles(files));
}
}

View file

@ -0,0 +1,108 @@
package ru.bclib.api.dataexchange.handler.autosync;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.networking.v1.PacketSender;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
import ru.bclib.BCLib;
import ru.bclib.api.dataexchange.DataExchangeAPI;
import ru.bclib.api.dataexchange.DataHandler;
import ru.bclib.api.dataexchange.DataHandlerDescriptor;
import ru.bclib.config.Configs;
import ru.bclib.util.ModUtil;
import java.io.File;
/**
* This message is sent once a player enters the world. It initiates a sequence of Messages that will sync files between both
* client and server.
* <table>
* <caption>Description</caption>
* <tr>
* <th>Server</th>
* <th></th>
* <th>Client</th>
* <th></th>
* </tr>
* <tr>
* <td colspan="4">Player enters World</td>
* </tr>
* <tr>
* <td></td>
* <td>&lt;--</td>
* <td>{@link HelloServer}</td>
* <td>Sends the current BLib-Version installed on the Client</td>
* </tr>
* <tr>
* <td>{@link HelloClient}</td>
* <td>--&gt;</td>
* <td></td>
* <td>Sends the current BClIb-Version, the Version of all Plugins and data for all AutpoSync-Files
* ({@link DataExchangeAPI#addAutoSyncFile(String, File)} on the Server</td>
* </tr>
* <tr>
* <td></td>
* <td>&lt;--</td>
* <td>{@link RequestFiles}</td>
* <td>Request missing or out of sync Files from the Server</td>
* </tr>
* <tr>
* <td>{@link SendFiles}</td>
* <td>--&gt;</td>
* <td></td>
* <td>Send Files from the Server to the Client</td>
* </tr>
* </table>
*/
public class HelloServer extends DataHandler.FromClient {
public static DataHandlerDescriptor DESCRIPTOR = new DataHandlerDescriptor(new ResourceLocation(BCLib.MOD_ID, "hello_server"), HelloServer::new, true, false);
protected String bclibVersion = "0.0.0";
public HelloServer() {
super(DESCRIPTOR.IDENTIFIER);
}
@Environment(EnvType.CLIENT)
@Override
protected boolean prepareDataOnClient() {
if (! Configs.CLIENT_CONFIG.isAllowingAutoSync()) {
BCLib.LOGGER.info("Auto-Sync was disabled on the client.");
return false;
}
return true;
}
@Environment(EnvType.CLIENT)
@Override
protected void serializeDataOnClient(FriendlyByteBuf buf) {
BCLib.LOGGER.info("Sending hello to server.");
buf.writeInt(ModUtil.convertModVersion(HelloClient.getBCLibVersion()));
}
@Override
protected void deserializeIncomingDataOnServer(FriendlyByteBuf buf, PacketSender responseSender) {
bclibVersion = ModUtil.convertModVersion(buf.readInt());
}
@Override
protected void runOnServerGameThread(MinecraftServer server) {
if (!Configs.SERVER_CONFIG.isAllowingAutoSync()) {
BCLib.LOGGER.info("Auto-Sync was disabled on the server.");
return;
}
String localBclibVersion = HelloClient.getBCLibVersion();
BCLib.LOGGER.info("Received Hello from Client. (server=" + localBclibVersion + ", client=" + bclibVersion + ")");
if (!server.isPublished()) {
BCLib.LOGGER.info("Auto-Sync is disabled for Singleplayer worlds.");
return;
}
reply(new HelloClient(), server);
}
}

View file

@ -0,0 +1,98 @@
package ru.bclib.api.dataexchange.handler.autosync;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.networking.v1.PacketSender;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
import ru.bclib.BCLib;
import ru.bclib.api.dataexchange.DataHandler;
import ru.bclib.api.dataexchange.DataHandlerDescriptor;
import ru.bclib.config.Configs;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
public class RequestFiles extends DataHandler.FromClient {
public static DataHandlerDescriptor DESCRIPTOR = new DataHandlerDescriptor(new ResourceLocation(BCLib.MOD_ID, "request_files"), RequestFiles::new, false, false);
static String currentToken = "";
protected List<AutoSyncID> files;
private RequestFiles() {
this(null);
}
public RequestFiles(List<AutoSyncID> files) {
super(DESCRIPTOR.IDENTIFIER);
this.files = files;
}
@Environment(EnvType.CLIENT)
@Override
protected boolean prepareDataOnClient() {
if (! Configs.CLIENT_CONFIG.isAllowingAutoSync()) {
BCLib.LOGGER.info("Auto-Sync was disabled on the client.");
return false;
}
return true;
}
@Environment(EnvType.CLIENT)
@Override
protected void serializeDataOnClient(FriendlyByteBuf buf) {
newToken();
writeString(buf, currentToken);
buf.writeInt(files.size());
for (AutoSyncID a : files) {
a.serializeData(buf);
}
}
String receivedToken = "";
@Override
protected void deserializeIncomingDataOnServer(FriendlyByteBuf buf, PacketSender responseSender) {
receivedToken = readString(buf);
int size = buf.readInt();
files = new ArrayList<>(size);
BCLib.LOGGER.info("Client requested " + size + " Files:");
for (int i = 0; i < size; i++) {
AutoSyncID asid = AutoSyncID.deserializeData(buf);
files.add(asid);
BCLib.LOGGER.info(" - " + asid);
}
}
@Override
protected void runOnServerGameThread(MinecraftServer server) {
if (!Configs.SERVER_CONFIG.isAllowingAutoSync()) {
BCLib.LOGGER.info("Auto-Sync was disabled on the server.");
return;
}
List<AutoFileSyncEntry> syncEntries = files.stream()
.map(asid -> AutoFileSyncEntry.findMatching(asid))
.filter(e -> e != null)
.collect(Collectors.toList());
reply(new SendFiles(syncEntries, receivedToken), server);
}
public static void newToken() {
currentToken = UUID.randomUUID()
.toString();
}
static {
newToken();
}
}

View file

@ -0,0 +1,216 @@
package ru.bclib.api.dataexchange.handler.autosync;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.networking.v1.PacketSender;
import net.minecraft.client.Minecraft;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import ru.bclib.BCLib;
import ru.bclib.api.dataexchange.DataHandler;
import ru.bclib.api.dataexchange.DataHandlerDescriptor;
import ru.bclib.config.Configs;
import ru.bclib.gui.screens.ConfirmRestartScreen;
import ru.bclib.util.Pair;
import ru.bclib.util.PathUtil;
import ru.bclib.util.Triple;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class SendFiles extends DataHandler.FromServer {
public static DataHandlerDescriptor DESCRIPTOR = new DataHandlerDescriptor(new ResourceLocation(BCLib.MOD_ID, "send_files"), SendFiles::new, false, false);
protected List<AutoFileSyncEntry> files;
private String token;
public SendFiles() {
this(null, "");
}
public SendFiles(List<AutoFileSyncEntry> files, String token) {
super(DESCRIPTOR.IDENTIFIER);
this.files = files;
this.token = token;
}
@Override
protected boolean prepareDataOnServer() {
if (!Configs.SERVER_CONFIG.isAllowingAutoSync()) {
BCLib.LOGGER.info("Auto-Sync was disabled on the server.");
return false;
}
return true;
}
@Override
protected void serializeDataOnServer(FriendlyByteBuf buf) {
List<AutoFileSyncEntry> existingFiles = files.stream()
.filter(e -> e!=null &&e.fileName!=null && e.fileName.exists())
.collect(Collectors.toList());
/*
//this will try to send a file that was not registered or requested by the client
existingFiles.add(new AutoFileSyncEntry("none", new File("D:\\MinecraftPlugins\\BetterNether\\run\\server.properties"),true,(a, b, content) -> {
System.out.println("Got Content:" + content.length);
return true;
}));*/
/*//this will try to send a folder-file that was not registered or requested by the client
existingFiles.add(new AutoFileSyncEntry.ForDirectFileRequest(DataExchange.SYNC_FOLDER.folderID, new File("test.json"), DataExchange.SYNC_FOLDER.mapAbsolute("test.json").toFile()));*/
/*//this will try to send a folder-file that was not registered or requested by the client and is outside the base-folder
existingFiles.add(new AutoFileSyncEntry.ForDirectFileRequest(DataExchange.SYNC_FOLDER.folderID, new File("../breakout.json"), DataExchange.SYNC_FOLDER.mapAbsolute("../breakout.json").toFile()));*/
writeString(buf, token);
buf.writeInt(existingFiles.size());
BCLib.LOGGER.info("Sending " + existingFiles.size() + " Files to Client:");
for (AutoFileSyncEntry entry : existingFiles) {
int length = entry.serializeContent(buf);
BCLib.LOGGER.info(" - " + entry + " (" + PathUtil.humanReadableFileSize(length) + ")");
}
}
private List<Pair<AutoFileSyncEntry, byte[]>> receivedFiles;
@Environment(EnvType.CLIENT)
@Override
protected void deserializeIncomingDataOnClient(FriendlyByteBuf buf, PacketSender responseSender) {
if ( Configs.CLIENT_CONFIG.isAcceptingConfigs() || Configs.CLIENT_CONFIG.isAcceptingFiles() || Configs.CLIENT_CONFIG.isAcceptingMods()) {
token = readString(buf);
if (!token.equals(RequestFiles.currentToken)) {
RequestFiles.newToken();
BCLib.LOGGER.error("Unrequested File Transfer!");
receivedFiles = new ArrayList<>(0);
return;
}
RequestFiles.newToken();
int size = buf.readInt();
receivedFiles = new ArrayList<>(size);
BCLib.LOGGER.info("Server sent " + size + " Files:");
for (int i = 0; i < size; i++) {
Triple<AutoFileSyncEntry, byte[], AutoSyncID> p = AutoFileSyncEntry.deserializeContent(buf);
if (p.first != null) {
final String type;
if (p.first.isConfigFile() && Configs.CLIENT_CONFIG.isAcceptingConfigs()) {
receivedFiles.add(p);
type = "Accepted Config ";
} else if (p.first instanceof AutoFileSyncEntry.ForModFileRequest && Configs.CLIENT_CONFIG.isAcceptingMods()){
receivedFiles.add(p);
type = "Accepted Mod ";
} else if ( Configs.CLIENT_CONFIG.isAcceptingFiles()){
receivedFiles.add(p);
type = "Accepted File ";
} else {
type = "Ignoring ";
}
BCLib.LOGGER.info(" - " + type + p.first + " (" + PathUtil.humanReadableFileSize(p.second.length) + ")");
}
else {
BCLib.LOGGER.error(" - Failed to receive File " + p.third + ", possibly sent from a Mod that is not installed on the client.");
}
}
}
}
@Environment(EnvType.CLIENT)
@Override
protected void runOnClientGameThread(Minecraft client) {
if ( Configs.CLIENT_CONFIG.isAcceptingConfigs() || Configs.CLIENT_CONFIG.isAcceptingFiles() || Configs.CLIENT_CONFIG.isAcceptingMods()) {
BCLib.LOGGER.info("Writing Files:");
//TODO: Reject files that were not in the last RequestFiles.
for (Pair<AutoFileSyncEntry, byte[]> entry : receivedFiles) {
final AutoFileSyncEntry e = entry.first;
final byte[] data = entry.second;
writeSyncedFile(e, data, e.fileName);
}
showConfirmRestart(client);
}
}
@Environment(EnvType.CLIENT)
static void writeSyncedFile(AutoSyncID e, byte[] data, File fileName) {
if (fileName!=null && !PathUtil.isChildOf(PathUtil.GAME_FOLDER, fileName.toPath())){
BCLib.LOGGER.error(fileName + " is not within game folder " + PathUtil.GAME_FOLDER);
return;
}
if (!PathUtil.MOD_BAK_FOLDER.toFile().exists()){
PathUtil.MOD_BAK_FOLDER.toFile().mkdirs();
}
Path path = fileName!=null?fileName.toPath():null;
Path removeAfter = null;
if (e instanceof AutoFileSyncEntry.ForModFileRequest mase){
removeAfter = path;
int count = 0;
final String prefix = "_bclib_synced_";
String name = prefix + mase.modID + "_" + mase.version.replace(".", "_") + ".jar";
do {
if (path != null) {
//move to the same directory as the existing Mod
path = path.getParent()
.resolve(name);
}
else {
//move to the default mode location
path = PathUtil.MOD_FOLDER.resolve(name);
}
count++;
name = prefix + mase.modID + "_" + mase.version.replace(".", "_") + "__" + String.format("%03d", count) + ".jar";
} while (path.toFile().exists());
}
BCLib.LOGGER.info(" - Writing " + path + " (" + PathUtil.humanReadableFileSize(data.length) + ")");
try {
final File parentFile = path.getParent()
.toFile();
if (!parentFile.exists()) {
parentFile.mkdirs();
}
Files.write(path, data);
if (removeAfter != null){
final String bakFileName = removeAfter.toFile().getName();
String collisionFreeName = bakFileName;
Path targetPath;
int count = 0;
do {
targetPath = PathUtil.MOD_BAK_FOLDER.resolve(collisionFreeName);
count++;
collisionFreeName = String.format("%03d", count) + "_" + bakFileName;
} while (targetPath.toFile().exists());
BCLib.LOGGER.info(" - Moving " + removeAfter + " to " +targetPath);
removeAfter.toFile().renameTo(targetPath.toFile());
}
AutoSync.didReceiveFile(e, fileName);
}
catch (IOException ioException) {
BCLib.LOGGER.error(" --> Writing " + fileName + " failed: " + ioException);
}
}
@Environment(EnvType.CLIENT)
protected void showConfirmRestart(Minecraft client) {
client.setScreen(new ConfirmRestartScreen(() -> {
Minecraft.getInstance()
.setScreen(null);
client.stop();
}));
}
}

View file

@ -0,0 +1,206 @@
package ru.bclib.api.dataexchange.handler.autosync;
import net.minecraft.network.FriendlyByteBuf;
import org.jetbrains.annotations.NotNull;
import ru.bclib.BCLib;
import ru.bclib.api.dataexchange.DataHandler;
import ru.bclib.api.dataexchange.FileHash;
import ru.bclib.api.dataexchange.handler.autosync.AutoSyncID.ForDirectFileRequest;
import ru.bclib.config.Configs;
import ru.bclib.util.PathUtil;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
public class SyncFolderDescriptor {
static class SubFile {
public final String relPath;
public final FileHash hash;
SubFile(String relPath, FileHash hash) {
this.relPath = relPath;
this.hash = hash;
}
@Override
public String toString() {
return relPath;
}
public void serialize(FriendlyByteBuf buf) {
DataHandler.writeString(buf, relPath);
hash.serialize(buf);
}
public static SubFile deserialize(FriendlyByteBuf buf) {
final String relPath = DataHandler.readString(buf);
FileHash hash = FileHash.deserialize(buf);
return new SubFile(relPath, hash);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o instanceof String) return relPath.equals(o);
if (!(o instanceof SubFile)) return false;
SubFile subFile = (SubFile) o;
return relPath.equals(subFile.relPath);
}
@Override
public int hashCode() {
return relPath.hashCode();
}
}
@NotNull
public final String folderID;
public final boolean removeAdditionalFiles;
@NotNull
public final Path localFolder;
private List<SubFile> fileCache;
public SyncFolderDescriptor(String folderID, Path localFolder, boolean removeAdditionalFiles) {
this.removeAdditionalFiles = removeAdditionalFiles;
this.folderID = folderID;
this.localFolder = localFolder;
fileCache = null;
}
@Override
public String toString() {
return "SyncFolderDescriptor{" + "folderID='" + folderID + '\'' + ", removeAdditionalFiles=" + removeAdditionalFiles + ", localFolder=" + localFolder + ", files=" + (fileCache == null ? "?" : fileCache.size()) + "}";
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o instanceof String) {
return folderID.equals(o);
}
if (o instanceof ForDirectFileRequest) {
return folderID.equals(((ForDirectFileRequest) o).uniqueID);
}
if (!(o instanceof SyncFolderDescriptor)) return false;
SyncFolderDescriptor that = (SyncFolderDescriptor) o;
return folderID.equals(that.folderID);
}
@Override
public int hashCode() {
return folderID.hashCode();
}
public int fileCount() {
return fileCache == null ? 0 : fileCache.size();
}
public void invalidateCache() {
fileCache = null;
}
public void loadCache() {
if (fileCache == null) {
fileCache = new ArrayList<>(8);
PathUtil.fileWalker(localFolder.toFile(), p -> fileCache.add(new SubFile(localFolder.relativize(p)
.toString(), FileHash.create(p.toFile()))));
/*//this tests if we can trick the system to load files that are not beneath the base-folder
if (!BCLib.isClient()) {
fileCache.add(new SubFile("../breakout.json", FileHash.create(mapAbsolute("../breakout.json").toFile())));
}*/
}
}
public void serialize(FriendlyByteBuf buf) {
final boolean debugHashes = Configs.CLIENT_CONFIG.getBoolean(AutoSync.SYNC_CATEGORY, "debugHashes", false);
loadCache();
DataHandler.writeString(buf, folderID);
buf.writeBoolean(removeAdditionalFiles);
buf.writeInt(fileCache.size());
fileCache.forEach(fl -> {
BCLib.LOGGER.info(" - " + fl.relPath);
if (debugHashes) {
BCLib.LOGGER.info(" " + fl.hash);
}
fl.serialize(buf);
});
}
public static SyncFolderDescriptor deserialize(FriendlyByteBuf buf) {
final String folderID = DataHandler.readString(buf);
final boolean remAddFiles = buf.readBoolean();
final int count = buf.readInt();
SyncFolderDescriptor localDescriptor = AutoSync.getSyncFolderDescriptor(folderID);
final SyncFolderDescriptor desc;
if (localDescriptor != null) {
desc = new SyncFolderDescriptor(folderID, localDescriptor.localFolder, localDescriptor.removeAdditionalFiles && remAddFiles);
desc.fileCache = new ArrayList<>(count);
}
else {
BCLib.LOGGER.warning(BCLib.isClient() ? "Client" : "Server" + " does not know Sync-Folder ID '" + folderID + "'");
desc = null;
}
for (int i = 0; i < count; i++) {
SubFile relPath = SubFile.deserialize(buf);
if (desc != null) desc.fileCache.add(relPath);
}
return desc;
}
//Note: make sure loadCache was called before using this
boolean hasRelativeFile(String relFile) {
return fileCache.stream()
.filter(sf -> sf.equals(relFile))
.findFirst()
.isPresent();
}
//Note: make sure loadCache was called before using this
boolean hasRelativeFile(SubFile subFile) {
return hasRelativeFile(subFile.relPath);
}
//Note: make sure loadCache was called before using this
SubFile getLocalSubFile(String relPath) {
return fileCache.stream()
.filter(sf -> sf.relPath.equals(relPath))
.findFirst()
.orElse(null);
}
Stream<SubFile> relativeFilesStream() {
loadCache();
return fileCache.stream();
}
public Path mapAbsolute(String relPath) {
return this.localFolder.resolve(relPath)
.normalize();
}
public Path mapAbsolute(SubFile subFile) {
return this.localFolder.resolve(subFile.relPath)
.normalize();
}
public boolean acceptChildElements(Path absPath) {
return PathUtil.isChildOf(this.localFolder, absPath);
}
public boolean acceptChildElements(SubFile subFile) {
return acceptChildElements(mapAbsolute(subFile));
}
public boolean discardChildElements(SubFile subFile) {
return !acceptChildElements(subFile);
}
}

View file

@ -0,0 +1,615 @@
package ru.bclib.api.datafixer;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.Util;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.components.toasts.SystemToast;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.gui.screens.worldselection.EditWorldScreen;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.NbtIo;
import net.minecraft.nbt.StringTag;
import net.minecraft.nbt.Tag;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.chunk.storage.RegionFile;
import net.minecraft.world.level.storage.LevelResource;
import net.minecraft.world.level.storage.LevelStorageSource;
import net.minecraft.world.level.storage.LevelStorageSource.LevelStorageAccess;
import org.jetbrains.annotations.NotNull;
import ru.bclib.BCLib;
import ru.bclib.api.WorldDataAPI;
import ru.bclib.config.Configs;
import ru.bclib.gui.screens.AtomicProgressListener;
import ru.bclib.gui.screens.ConfirmFixScreen;
import ru.bclib.gui.screens.LevelFixErrorScreen;
import ru.bclib.gui.screens.LevelFixErrorScreen.Listener;
import ru.bclib.gui.screens.ProgressScreen;
import ru.bclib.util.Logger;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.zip.ZipException;
/**
* API to manage Patches that need to get applied to a world
*/
public class DataFixerAPI {
static final Logger LOGGER = new Logger("DataFixerAPI");
static class State {
public boolean didFail = false;
protected ArrayList<String> errors = new ArrayList<>();
public void addError(String s){
errors.add(s);
}
public boolean hasError(){
return errors.size()>0;
}
public String getErrorMessage(){
return errors.stream().reduce("", (a, b) -> a + " - " + b + "\n");
}
public String[] getErrorMessages(){
String[] res = new String[errors.size()];
return errors.toArray(res);
}
}
@FunctionalInterface
public static interface Callback {
public void call();
}
private static boolean wrapCall(LevelStorageSource levelSource, String levelID, Function<LevelStorageAccess, Boolean> runWithLevel) {
LevelStorageSource.LevelStorageAccess levelStorageAccess;
try {
levelStorageAccess = levelSource.createAccess(levelID);
}
catch (IOException e) {
BCLib.LOGGER.warning("Failed to read level {} data", levelID, e);
SystemToast.onWorldAccessFailure(Minecraft.getInstance(), levelID);
Minecraft.getInstance().setScreen(null);
return true;
}
boolean returnValue = runWithLevel.apply(levelStorageAccess);
try {
levelStorageAccess.close();
}
catch (IOException e) {
BCLib.LOGGER.warning("Failed to unlock access to level {}", levelID, e);
}
return returnValue;
}
/**
* Will apply necessary Patches to the world.
*
* @param levelSource The SourceStorage for this Minecraft instance, You can get this using
* {@code Minecraft.getInstance().getLevelSource()}
* @param levelID The ID of the Level you want to patch
* @param showUI {@code true}, if you want to present the user with a Screen that offers to backup the world
* before applying the patches
* @param onResume When this method retursn {@code true}, this function will be called when the world is ready
* @return {@code true} if the UI was displayed. The UI is only displayed if {@code showUI} was {@code true} and
* patches were enabled in the config and the Guardian did find any patches that need to be applied to the world.
*
*/
public static boolean fixData(LevelStorageSource levelSource, String levelID, boolean showUI, Consumer<Boolean> onResume) {
return wrapCall(levelSource, levelID, (levelStorageAccess) -> fixData(levelStorageAccess, showUI, onResume));
}
/**
* Will apply necessary Patches to the world.
*
* @param levelStorageAccess The access class of the level you want to patch
* @param showUI {@code true}, if you want to present the user with a Screen that offers to backup the world
* before applying the patches
* @param onResume When this method retursn {@code true}, this function will be called when the world is ready
* @return {@code true} if the UI was displayed. The UI is only displayed if {@code showUI} was {@code true} and
* patches were enabled in the config and the Guardian did find any patches that need to be applied to the world.
*
*/
public static boolean fixData(LevelStorageSource.LevelStorageAccess levelStorageAccess, boolean showUI, Consumer<Boolean> onResume){
File levelPath = levelStorageAccess.getLevelPath(LevelResource.ROOT).toFile();
File levelDat = levelStorageAccess.getLevelPath(LevelResource.LEVEL_DATA_FILE).toFile();
boolean newWorld = false;
if (!levelDat.exists()) {
BCLib.LOGGER.info("Creating a new World, no fixes needed");
newWorld = true;
}
initializeWorldData(levelPath, newWorld);
if (newWorld) return false;
return fixData(levelPath, levelStorageAccess.getLevelId(), showUI, onResume);
}
/**
* Initializes the DataStorage for this world. If the world is new, the patch registry is initialized to the
* current versions of the plugins.
* <p>
* This implementation will create a new {@link LevelStorageAccess} and call {@link #initializeWorldData(File, boolean)}
* using the provided root path.
* @param levelSource The SourceStorage for this Minecraft instance, You can get this using
* {@code Minecraft.getInstance().getLevelSource()}
* @param levelID The ID of the Level you want to patch
* @param newWorld {@code true} if this is a fresh world
*/
public static void initializeWorldData(LevelStorageSource levelSource, String levelID, boolean newWorld) {
wrapCall(levelSource, levelID, (levelStorageAccess) -> {
initializeWorldData(levelStorageAccess.getLevelPath(LevelResource.ROOT).toFile(), newWorld);
return true;
});
}
/**
* Initializes the DataStorage for this world. If the world is new, the patch registry is initialized to the
* current versions of the plugins.
* @param levelBaseDir Folder of the world
* @param newWorld {@code true} if this is a fresh world
*
*/
public static void initializeWorldData(File levelBaseDir, boolean newWorld){
WorldDataAPI.load(new File(levelBaseDir, "data"));
if (newWorld){
getMigrationProfile().markApplied();
WorldDataAPI.saveFile(BCLib.MOD_ID);
}
}
@Environment(EnvType.CLIENT)
private static AtomicProgressListener showProgressScreen(){
ProgressScreen ps = new ProgressScreen(Minecraft.getInstance().screen, new TranslatableComponent("title.bclib.datafixer.progress"), new TranslatableComponent("message.bclib.datafixer.progress"));
Minecraft.getInstance().setScreen(ps);
return ps;
}
private static boolean fixData(File dir, String levelID, boolean showUI, Consumer<Boolean> onResume) {
MigrationProfile profile = loadProfileIfNeeded(dir);
BiConsumer<Boolean, Boolean> runFixes = (createBackup, applyFixes) -> {
final AtomicProgressListener progress;
if (applyFixes) {
if (showUI) {
progress = showProgressScreen();
} else {
progress = new AtomicProgressListener() {
private long timeStamp = Util.getMillis();
private AtomicInteger counter = new AtomicInteger(0);
@Override
public void incAtomic(int maxProgress) {
int percentage = (100 * counter.incrementAndGet()) / maxProgress;
if (Util.getMillis() - this.timeStamp >= 1000L) {
this.timeStamp = Util.getMillis();
BCLib.LOGGER.info((String) "Patching... {}%", percentage);
}
}
@Override
public void resetAtomic() {
counter = new AtomicInteger(0);
}
public void stop() {
}
public void progressStage(Component component) {
BCLib.LOGGER.info((String) "Patcher Stage... {}%", component.getString());
}
};
}
} else {
progress = null;
}
Supplier<State> runner = () -> {
if (createBackup) {
progress.progressStage(new TranslatableComponent("message.bclib.datafixer.progress.waitbackup"));
EditWorldScreen.makeBackupAndShowToast(Minecraft.getInstance().getLevelSource(), levelID);
}
if (applyFixes) {
return runDataFixes(dir, profile, progress);
}
return new State();
};
if (showUI) {
Thread fixerThread = new Thread(() -> {
final State state = runner.get();
Minecraft.getInstance()
.execute(() -> {
if (profile != null && showUI) {
//something went wrong, show the user our error
if (state.didFail || state.hasError()){
showLevelFixErrorScreen(state, (markFixed)->{
if (markFixed) {
profile.markApplied();
}
onResume.accept(applyFixes);
});
} else {
onResume.accept(applyFixes);
}
}
});
});
fixerThread.start();
} else {
State state = runner.get();
if (state.hasError()){
LOGGER.error("There were Errors while fixing the Level:");
LOGGER.error(state.getErrorMessage());
}
}
};
//we have some migrations
if (profile != null) {
//display the confirm UI.
if (showUI){
showBackupWarning(levelID, runFixes);
return true;
} else {
BCLib.LOGGER.warning("Applying Fixes on Level");
runFixes.accept(false, true);
}
}
return false;
}
@Environment(EnvType.CLIENT)
private static void showLevelFixErrorScreen(State state, Listener onContinue){
Minecraft.getInstance()
.setScreen(new LevelFixErrorScreen(Minecraft.getInstance().screen, state.getErrorMessages(), onContinue));
}
private static MigrationProfile loadProfileIfNeeded(File levelBaseDir){
if (!Configs.MAIN_CONFIG.getBoolean(Configs.MAIN_PATCH_CATEGORY, "applyPatches", true)) {
LOGGER.info("World Patches are disabled");
return null;
}
MigrationProfile profile = getMigrationProfile();
profile.runPrePatches(levelBaseDir);
if (!profile.hasAnyFixes()) {
LOGGER.info("Everything up to date");
return null;
}
return profile;
}
@NotNull
private static MigrationProfile getMigrationProfile() {
final CompoundTag patchConfig = WorldDataAPI.getCompoundTag(BCLib.MOD_ID, Configs.MAIN_PATCH_CATEGORY);
MigrationProfile profile = Patch.createMigrationData(patchConfig);
return profile;
}
@Environment(EnvType.CLIENT)
static void showBackupWarning(String levelID, BiConsumer<Boolean, Boolean> whenFinished){
Minecraft.getInstance().setScreen(new ConfirmFixScreen((Screen) null, whenFinished::accept));
}
private static State runDataFixes(File dir, MigrationProfile profile, AtomicProgressListener progress) {
State state = new State();
progress.resetAtomic();
progress.progressStage(new TranslatableComponent("message.bclib.datafixer.progress.reading"));
List<File> players = getAllPlayers(dir);
List<File> regions = getAllRegions(dir, null);
final int maxProgress = players.size()+regions.size()+4;
progress.incAtomic(maxProgress);
progress.progressStage(new TranslatableComponent("message.bclib.datafixer.progress.players"));
players.parallelStream().forEach((file) -> {
fixPlayer(profile, state, file);
progress.incAtomic(maxProgress);
});
progress.progressStage(new TranslatableComponent("message.bclib.datafixer.progress.level"));
fixLevel(profile, state, dir);
progress.incAtomic(maxProgress);
progress.progressStage(new TranslatableComponent("message.bclib.datafixer.progress.worlddata"));
try {
profile.patchWorldData();
} catch (PatchDidiFailException e){
state.didFail = true;
state.addError("Failed fixing worldconfig (" + e.getMessage() + ")");
BCLib.LOGGER.error(e.getMessage());
}
progress.incAtomic(maxProgress);
progress.progressStage(new TranslatableComponent("message.bclib.datafixer.progress.regions"));
regions.parallelStream().forEach((file) -> {
fixRegion(profile, state, file);
progress.incAtomic(maxProgress);
});
if (!state.didFail) {
progress.progressStage(new TranslatableComponent("message.bclib.datafixer.progress.saving"));
profile.markApplied();
WorldDataAPI.saveFile(BCLib.MOD_ID);
}
progress.incAtomic(maxProgress);
progress.stop();
return state;
}
private static void fixLevel(MigrationProfile profile, State state, File levelBaseDir) {
try {
LOGGER.info("Inspecting level.dat in " + levelBaseDir);
//load the level (could already contain patches applied by patchLevelDat)
CompoundTag level = profile.getLevelDat(levelBaseDir);
boolean[] changed = { profile.isLevelDatChanged() };
if (profile.getPrePatchException()!=null){
throw profile.getPrePatchException();
}
if (level.contains("Data")) {
CompoundTag dataTag = (CompoundTag)level.get("Data");
if (dataTag.contains("Player")) {
CompoundTag player = (CompoundTag)dataTag.get("Player");
fixPlayerNbt(player, changed, profile);
}
}
if (changed[0]) {
LOGGER.warning("Writing '{}'", profile.getLevelDatFile());
NbtIo.writeCompressed(level, profile.getLevelDatFile());
}
}
catch (Exception e) {
BCLib.LOGGER.error("Failed fixing Level-Data.");
state.addError("Failed fixing Level-Data in level.dat (" + e.getMessage() + ")");
state.didFail = true;
e.printStackTrace();
}
}
private static void fixPlayer(MigrationProfile data, State state, File file) {
try {
LOGGER.info("Inspecting " + file);
CompoundTag player = readNbt(file);
boolean[] changed = { false };
fixPlayerNbt(player, changed, data);
if (changed[0]) {
LOGGER.warning("Writing '{}'", file);
NbtIo.writeCompressed(player, file);
}
}
catch (Exception e) {
BCLib.LOGGER.error("Failed fixing Player-Data.");
state.addError("Failed fixing Player-Data in " + file.getName() + " (" + e.getMessage() + ")");
state.didFail = true;
e.printStackTrace();
}
}
private static void fixPlayerNbt(CompoundTag player, boolean[] changed, MigrationProfile data) {
//Checking Inventory
ListTag inventory = player.getList("Inventory", Tag.TAG_COMPOUND);
fixItemArrayWithID(inventory, changed, data, true);
//Checking EnderChest
ListTag enderitems = player.getList("EnderItems", Tag.TAG_COMPOUND);
fixItemArrayWithID(enderitems, changed, data, true);
//Checking ReceipBook
if (player.contains("recipeBook")) {
CompoundTag recipeBook = player.getCompound("recipeBook");
changed[0] |= fixStringIDList(recipeBook, "recipes", data);
changed[0] |= fixStringIDList(recipeBook, "toBeDisplayed", data);
}
}
static boolean fixStringIDList(CompoundTag root, String name, MigrationProfile data) {
boolean _changed = false;
if (root.contains(name)) {
ListTag items = root.getList(name, Tag.TAG_STRING);
ListTag newItems = new ListTag();
for (Tag tag : items) {
final StringTag str = (StringTag)tag;
final String replace = data.replaceStringFromIDs(str.getAsString());
if (replace!=null) {
_changed = true;
newItems.add(StringTag.valueOf(replace));
} else {
newItems.add(tag);
}
}
if (_changed) {
root.put(name, newItems);
}
}
return _changed;
}
private static void fixRegion(MigrationProfile data, State state, File file) {
try {
LOGGER.info("Inspecting " + file);
boolean[] changed = new boolean[1];
RegionFile region = new RegionFile(file, file.getParentFile(), true);
for (int x = 0; x < 32; x++) {
for (int z = 0; z < 32; z++) {
ChunkPos pos = new ChunkPos(x, z);
changed[0] = false;
if (region.hasChunk(pos) && !state.didFail) {
DataInputStream input = region.getChunkDataInputStream(pos);
CompoundTag root = NbtIo.read(input);
// if ((root.toString().contains("betternether:chest") || root.toString().contains("bclib:chest"))) {
// NbtIo.write(root, new File(file.toString() + "-" + x + "-" + z + ".nbt"));
// }
input.close();
//Checking TileEntities
ListTag tileEntities = root.getCompound("Level")
.getList("TileEntities", Tag.TAG_COMPOUND);
fixItemArrayWithID(tileEntities, changed, data, true);
//Checking Entities
ListTag entities = root.getList("Entities", Tag.TAG_COMPOUND);
fixItemArrayWithID(entities, changed, data, true);
//Checking Block Palette
ListTag sections = root.getCompound("Level")
.getList("Sections", Tag.TAG_COMPOUND);
sections.forEach((tag) -> {
ListTag palette = ((CompoundTag) tag).getList("Palette", Tag.TAG_COMPOUND);
palette.forEach((blockTag) -> {
CompoundTag blockTagCompound = ((CompoundTag) blockTag);
changed[0] |= data.replaceStringFromIDs(blockTagCompound, "Name");
});
try {
changed[0] |= data.patchBlockState(palette, ((CompoundTag) tag).getList("BlockStates", Tag.TAG_LONG));
}
catch (PatchDidiFailException e) {
BCLib.LOGGER.error("Failed fixing BlockState in " + pos);
state.addError("Failed fixing BlockState in " + pos + " (" + e.getMessage() + ")");
state.didFail = true;
changed[0] = false;
e.printStackTrace();
}
});
if (changed[0]) {
LOGGER.warning("Writing '{}': {}/{}", file, x, z);
// NbtIo.write(root, new File(file.toString() + "-" + x + "-" + z + "-changed.nbt"));
DataOutputStream output = region.getChunkDataOutputStream(pos);
NbtIo.write(root, output);
output.close();
}
}
}
}
region.close();
}
catch (Exception e) {
BCLib.LOGGER.error("Failed fixing Region.");
state.addError("Failed fixing Region in " + file.getName() + " (" + e.getMessage() + ")");
state.didFail = true;
e.printStackTrace();
}
}
static CompoundTag patchConfTag = null;
static CompoundTag getPatchData(){
if (patchConfTag==null) {
patchConfTag = WorldDataAPI.getCompoundTag(BCLib.MOD_ID, Configs.MAIN_PATCH_CATEGORY);
}
return patchConfTag;
}
static void fixItemArrayWithID(ListTag items, boolean[] changed, MigrationProfile data, boolean recursive) {
items.forEach(inTag -> {
fixID((CompoundTag) inTag, changed, data, recursive);
});
}
static void fixID(CompoundTag inTag, boolean[] changed, MigrationProfile data, boolean recursive) {
final CompoundTag tag = inTag;
changed[0] |= data.replaceStringFromIDs(tag, "id");
if (tag.contains("Item")) {
CompoundTag item = (CompoundTag)tag.get("Item");
fixID(item, changed, data, recursive);
}
if (recursive && tag.contains("Items")) {
fixItemArrayWithID(tag.getList("Items", Tag.TAG_COMPOUND), changed, data, true);
}
if (recursive && tag.contains("Inventory")) {
ListTag inventory = tag.getList("Inventory", Tag.TAG_COMPOUND);
fixItemArrayWithID(inventory, changed, data, true);
}
if (tag.contains("tag")) {
CompoundTag entityTag = (CompoundTag)tag.get("tag");
if (entityTag.contains("BlockEntityTag")){
CompoundTag blockEntityTag = (CompoundTag)entityTag.get("BlockEntityTag");
fixID(blockEntityTag, changed, data, recursive);
/*ListTag items = blockEntityTag.getList("Items", Tag.TAG_COMPOUND);
fixItemArrayWithID(items, changed, data, recursive);*/
}
}
}
private static List<File> getAllPlayers(File dir) {
List<File> list = new ArrayList<>();
dir = new File(dir, "playerdata");
if (!dir.exists() || !dir.isDirectory()) {
return list;
}
for (File file : dir.listFiles()) {
if (file.isFile() && file.getName().endsWith(".dat")) {
list.add(file);
}
}
return list;
}
private static List<File> getAllRegions(File dir, List<File> list) {
if (list == null) {
list = new ArrayList<>();
}
for (File file : dir.listFiles()) {
if (file.isDirectory()) {
getAllRegions(file, list);
} else if (file.isFile() && file.getName().endsWith(".mca")) {
list.add(file);
}
}
return list;
}
/**
* register a new Patch
*
* @param patch A #Supplier that will instantiate the new Patch Object
*/
public static void registerPatch(Supplier<Patch> patch) {
Patch.getALL().add(patch.get());
}
private static CompoundTag readNbt(File file) throws IOException {
try {
return NbtIo.readCompressed(file);
} catch (ZipException e){
return NbtIo.read(file);
}
}
}

View file

@ -0,0 +1,47 @@
package ru.bclib.api.datafixer;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import org.jetbrains.annotations.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* A Patch for level.dat that is always executed no matter what Patchlevel is set in a world.
*/
public abstract class ForcedLevelPatch extends Patch {
protected ForcedLevelPatch(@NotNull String modID, String version) {
super(modID, version, true);
}
@Override
public final Map<String, String> getIDReplacements() {
return new HashMap<String, String>();
}
@Override
public final PatchFunction<CompoundTag, Boolean> getWorldDataPatcher() { return null; }
@Override
public final PatchBiFunction<ListTag, ListTag, Boolean> getBlockStatePatcher() { return null; }
@Override
public final List<String> getWorldDataIDPaths() {
return null;
}
@Override
public PatchFunction<CompoundTag, Boolean> getLevelDatPatcher() { return this::runLevelDatPatch; }
/**
* Called with the contents of level.dat in {@code root}
* @param root The contents of level.dat
* @param profile The active migration profile
* @return true, if the run did change the contents of root
*/
abstract protected Boolean runLevelDatPatch(CompoundTag root, MigrationProfile profile);
}

View file

@ -0,0 +1,320 @@
package ru.bclib.api.datafixer;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.NbtIo;
import net.minecraft.nbt.Tag;
import org.jetbrains.annotations.NotNull;
import ru.bclib.BCLib;
import ru.bclib.api.WorldDataAPI;
import ru.bclib.util.ModUtil;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
public class MigrationProfile {
final Set<String> mods;
final Map<String, String> idReplacements;
final List<PatchFunction<CompoundTag, Boolean>> levelPatchers;
final List<PatchBiFunction<ListTag, ListTag, Boolean>> statePatchers;
final List<Patch> worldDataPatchers;
final Map<String, List<String>> worldDataIDPaths;
private final CompoundTag config;
private CompoundTag level;
private File levelBaseDir;
private boolean prePatchChangedLevelDat;
private boolean didRunPrePatch;
private Exception prePatchException;
MigrationProfile(CompoundTag config, boolean applyAll) {
this.config = config;
this.mods = Collections.unmodifiableSet(Patch.getALL()
.stream()
.map(p -> p.modID)
.collect(Collectors.toSet()));
HashMap<String, String> replacements = new HashMap<String, String>();
List<PatchFunction<CompoundTag, Boolean>> levelPatches = new LinkedList<>();
List<Patch> worldDataPatches = new LinkedList<>();
List<PatchBiFunction<ListTag, ListTag, Boolean>> statePatches = new LinkedList<>();
HashMap<String, List<String>> worldDataIDPaths = new HashMap<>();
for (String modID : mods) {
Patch.getALL()
.stream()
.filter(p -> p.modID.equals(modID))
.forEach(patch -> {
List<String> paths = patch.getWorldDataIDPaths();
if (paths!=null) worldDataIDPaths.put(modID, paths);
if (applyAll || currentPatchLevel(modID) < patch.level || patch.alwaysApply) {
replacements.putAll(patch.getIDReplacements());
if (patch.getLevelDatPatcher()!=null)
levelPatches.add(patch.getLevelDatPatcher());
if (patch.getWorldDataPatcher()!=null)
worldDataPatches.add(patch);
if (patch.getBlockStatePatcher()!=null)
statePatches.add(patch.getBlockStatePatcher());
DataFixerAPI.LOGGER.info("Applying " + patch);
}
else {
DataFixerAPI.LOGGER.info("Ignoring " + patch);
}
});
}
this.worldDataIDPaths = Collections.unmodifiableMap(worldDataIDPaths);
this.idReplacements = Collections.unmodifiableMap(replacements);
this.levelPatchers = Collections.unmodifiableList(levelPatches);
this.worldDataPatchers = Collections.unmodifiableList(worldDataPatches);
this.statePatchers = Collections.unmodifiableList(statePatches);
}
/**
* This method is supposed to be used by developers to apply id-patches to custom nbt structures. It is only
* available in Developer-Mode
*
*/
public static void fixCustomFolder(File dir){
if (!BCLib.isDevEnvironment()) return;
MigrationProfile profile = Patch.createMigrationData();
List<File> nbts = getAllNbts(dir, null);
nbts.parallelStream().forEach((file) -> {
DataFixerAPI.LOGGER.info("Loading NBT " + file);
try {
CompoundTag root = NbtIo.readCompressed(file);
boolean[] changed = {false};
if (root.contains("palette")){
ListTag items = root.getList("palette", Tag.TAG_COMPOUND);
items.forEach(inTag -> {
CompoundTag tag = (CompoundTag)inTag;
changed[0] |= profile.replaceStringFromIDs(tag, "Name");
});
}
if (changed[0]){
DataFixerAPI.LOGGER.info("Writing NBT " + file);
NbtIo.writeCompressed(root, file);
}
}
catch (IOException e) {
e.printStackTrace();
}
});
}
private static List<File> getAllNbts(File dir, List<File> list) {
if (list == null) {
list = new ArrayList<>();
}
for (File file : dir.listFiles()) {
if (file.isDirectory()) {
getAllNbts(file, list);
} else if (file.isFile() && file.getName().endsWith(".nbt")) {
list.add(file);
}
}
return list;
}
final public CompoundTag getLevelDat(File levelBaseDir){
if (level == null || this.levelBaseDir==null || !this.levelBaseDir.equals(levelBaseDir)){
runPrePatches(levelBaseDir);
}
return level;
}
final public boolean isLevelDatChanged(){
return prePatchChangedLevelDat;
}
final public File getLevelDatFile(){
return new File(levelBaseDir, "level.dat");
}
final public Exception getPrePatchException(){
return prePatchException;
}
final public void runPrePatches(File levelBaseDir){
if (didRunPrePatch){
BCLib.LOGGER.warning("Already did run PrePatches for " + this.levelBaseDir + ".");
}
BCLib.LOGGER.info("Running Pre Patchers on " + levelBaseDir);
this.levelBaseDir = levelBaseDir;
this.level = null;
this.prePatchException = null;
didRunPrePatch = true;
this.prePatchChangedLevelDat = runPreLevelPatches(getLevelDatFile());
}
private boolean runPreLevelPatches(File levelDat){
try {
level = NbtIo.readCompressed(levelDat);
boolean changed = patchLevelDat(level);
return changed;
}
catch (IOException | PatchDidiFailException e) {
prePatchException = e;
return false;
}
}
final public void markApplied() {
for (String modID : mods) {
DataFixerAPI.LOGGER.info("Updating Patch-Level for '{}' from {} to {}", modID, ModUtil.convertModVersion(currentPatchLevel(modID)), ModUtil.convertModVersion(Patch.maxPatchLevel(modID)));
if (config!=null)
config.putString(modID, Patch.maxPatchVersion(modID));
}
}
public String currentPatchVersion(@NotNull String modID) {
if (config==null || !config.contains(modID)) return "0.0.0";
return config.getString(modID);
}
public int currentPatchLevel(@NotNull String modID) {
return ModUtil.convertModVersion(currentPatchVersion(modID));
}
public boolean hasAnyFixes() {
boolean hasLevelDatPatches;
if (didRunPrePatch != false) {
hasLevelDatPatches = prePatchChangedLevelDat;
} else {
hasLevelDatPatches = levelPatchers.size()>0;
}
return idReplacements.size() > 0 || hasLevelDatPatches || worldDataPatchers.size() > 0;
}
public String replaceStringFromIDs(@NotNull String val) {
final String replace = idReplacements.get(val);
return replace;
}
public boolean replaceStringFromIDs(@NotNull CompoundTag tag, @NotNull String key) {
if (!tag.contains(key)) return false;
final String val = tag.getString(key);
final String replace = idReplacements.get(val);
if (replace != null) {
DataFixerAPI.LOGGER.warning("Replacing ID '{}' with '{}'.", val, replace);
tag.putString(key, replace);
return true;
}
return false;
}
private boolean replaceIDatPath(@NotNull ListTag list, @NotNull String[] parts, int level){
boolean[] changed = {false};
if (level == parts.length-1) {
DataFixerAPI.fixItemArrayWithID(list, changed, this, true);
} else {
list.forEach(inTag -> changed[0] |= replaceIDatPath((CompoundTag)inTag, parts, level+1));
}
return changed[0];
}
private boolean replaceIDatPath(@NotNull CompoundTag tag, @NotNull String[] parts, int level){
boolean changed = false;
for (int i=level; i<parts.length-1; i++) {
final String part = parts[i];
if (tag.contains(part)) {
final byte type = tag.getTagType(part);
if (type == Tag.TAG_LIST) {
ListTag list = tag.getList(part, Tag.TAG_COMPOUND);
return replaceIDatPath(list, parts, i);
} else if (type == Tag.TAG_COMPOUND) {
tag = tag.getCompound(part);
}
} else {
return false;
}
}
if (tag!=null && parts.length>0) {
final String key = parts[parts.length-1];
final byte type = tag.getTagType(key);
if (type == Tag.TAG_LIST) {
final ListTag list = tag.getList(key, Tag.TAG_COMPOUND);
final boolean[] _changed = {false};
if (list.size()==0) {
_changed[0] = DataFixerAPI.fixStringIDList(tag, key, this);
} else {
DataFixerAPI.fixItemArrayWithID(list, _changed, this, true);
}
return _changed[0];
} else if (type == Tag.TAG_STRING) {
return replaceStringFromIDs(tag, key);
} else if (type == Tag.TAG_COMPOUND) {
final CompoundTag cTag = tag.getCompound(key);
boolean[] _changed = {false};
DataFixerAPI.fixID(cTag, _changed, this, true);
return _changed[0];
}
}
return false;
}
public boolean replaceIDatPath(@NotNull CompoundTag root, @NotNull String path){
String[] parts = path.split("\\.");
return replaceIDatPath(root, parts, 0);
}
public boolean patchLevelDat(@NotNull CompoundTag level) throws PatchDidiFailException {
boolean changed = false;
for (PatchFunction<CompoundTag, Boolean> f : levelPatchers) {
changed |= f.apply(level, this);
}
return changed;
}
public void patchWorldData() throws PatchDidiFailException {
for (Patch patch : worldDataPatchers) {
CompoundTag root = WorldDataAPI.getRootTag(patch.modID);
boolean changed = patch.getWorldDataPatcher().apply(root, this);
if (changed) {
WorldDataAPI.saveFile(patch.modID);
}
}
for (Map.Entry<String, List<String>> entry : worldDataIDPaths.entrySet()){
CompoundTag root = WorldDataAPI.getRootTag(entry.getKey());
boolean[] changed = {false};
entry.getValue().forEach(path -> {
changed[0] |= replaceIDatPath(root, path);
});
if (changed[0]){
WorldDataAPI.saveFile(entry.getKey());
}
}
}
public boolean patchBlockState(ListTag palette, ListTag states) throws PatchDidiFailException{
boolean changed = false;
for (PatchBiFunction<ListTag, ListTag, Boolean> f : statePatchers) {
changed |= f.apply(palette, states, this);
}
return changed;
}
}

View file

@ -0,0 +1,241 @@
package ru.bclib.api.datafixer;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import org.jetbrains.annotations.NotNull;
import ru.bclib.util.ModUtil;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public abstract class Patch {
private static List<Patch> ALL = new ArrayList<>(10);
/**
* The Patch-Level derived from {@link #version}
*/
public final int level;
/**
* The Patch-Version string
*/
public final String version;
/**
* The Mod-ID that registered this Patch
*/
@NotNull
public final String modID;
/**
* This Mod is tested for each level start
*/
public final boolean alwaysApply;
static List<Patch> getALL() {
return ALL;
}
/**
* Returns the highest Patch-Version that is available for the given mod. If no patches were
* registerd for the mod, this will return 0.0.0
*
* @param modID The ID of the mod you want to query
* @return The highest Patch-Version that was found
*/
public static String maxPatchVersion(@NotNull String modID) {
return ALL.stream()
.filter(p -> p.modID
.equals(modID))
.map(p -> p.version)
.reduce((p, c) -> c)
.orElse("0.0.0");
}
/**
* Returns the highest patch-level that is available for the given mod. If no patches were
* registerd for the mod, this will return 0
*
* @param modID The ID of the mod you want to query
* @return The highest Patch-Level that was found
*/
public static int maxPatchLevel(@NotNull String modID) {
return ALL.stream()
.filter(p -> p.modID
.equals(modID))
.mapToInt(p -> p.level)
.max()
.orElse(0);
}
/**
* Called by inheriting classes.
* <p>
* Performs some sanity checks on the values and might throw a #RuntimeException if any
* inconsistencies are found.
*
* @param modID The ID of the Mod you want to register a patch for. This should be your
* ModID only. The ModID can not be {@code null} or an empty String.
* @param version The mod-version that introduces the patch. This needs Semantic-Version String
* like x.x.x. Developers are responsible for registering their patches in the correct
* order (with increasing versions). You are not allowed to register a new
* Patch with a version lower or equal than
* {@link Patch#maxPatchVersion(String)}
*/
protected Patch(@NotNull String modID, String version) {
this(modID, version, false);
}
/**
* Internal Constructor used to create patches that can allways run (no matter what patchlevel a level has)
* @param modID The ID of the Mod
* @param version The mod-version that introduces the patch. When {@Code runAllways} is set, this version will
* determine the patchlevel that is written to the level
* @param alwaysApply When true, this patch is always active, no matter the patchlevel of the world.
* This should be used sparingly and just for patches that apply to level.dat (as they only take
* effect when changes are detected). Use {@link ForcedLevelPatch} to instatiate.
*/
Patch(@NotNull String modID, String version, boolean alwaysApply) {
//Patchlevels need to be unique and registered in ascending order
if (modID == null || "".equals(modID)) {
throw new RuntimeException("[INTERNAL ERROR] Patches need a valid modID!");
}
if (version == null || "".equals(version)) {
throw new RuntimeException("Invalid Mod-Version");
}
this.version = version;
this.alwaysApply = alwaysApply;
this.level = ModUtil.convertModVersion(version);
if (!ALL.stream()
.filter(p -> p.modID
.equals(modID))
.noneMatch(p -> p.level >= this.level) || this.level <= 0) {
throw new RuntimeException("[INTERNAL ERROR] Patch-levels need to be created in ascending order beginning with 1.");
}
this.modID = modID;
}
@Override
public String toString() {
return "Patch{" + modID + ':' +version + ':' + level + '}';
}
/**
* Return block data fixes. Fixes will be applied on world load if current patch-level for
* the linked mod is lower than the {@link #level}.
* <p>
* The default implementation of this method returns an empty map.
*
* @return The returned Map should contain the replacements. All occurences of the
* {@code KeySet} are replaced with the associated value.
*/
public Map<String, String> getIDReplacements() {
return new HashMap<String, String>();
}
/**
* Return a {@link PatchFunction} that is called with the content of <i>level.dat</i>.
* <p>
* The function needs to return {@code true}, if changes were made to the data.
* If an error occurs, the method should throw a {@link PatchDidiFailException}
*
* The default implementation of this method returns null.
*
* @return {@code true} if changes were applied and we need to save the data
*/
public PatchFunction<CompoundTag, Boolean> getLevelDatPatcher() { return null; }
/**
* Return a {@link PatchFunction} that is called with the content from the
* {@link ru.bclib.api.WorldDataAPI} for this Mod.
* The function needs to return {@code true}, if changes were made to the data.
* If an error occurs, the method should throw a {@link PatchDidiFailException}
*
* The default implementation of this method returns null.
*
* @return {@code true} if changes were applied and we need to save the data
*/
public PatchFunction<CompoundTag, Boolean> getWorldDataPatcher() { return null; }
/**
* Return a {@link PatchBiFunction} that is called with pallette and blockstate of
* each chunk in every region. This method is called AFTER all ID replacements
* from {@link #getIDReplacements()} were applied to the pallete.
*
* The first parameter is the palette and the second is the blockstate.
*
* The function needs to return {@code true}, if changes were made to the data.
* If an error occurs, the method should throw a {@link PatchDidiFailException}
*
* The default implementation of this method returns null.
*
* @return {@code true} if changes were applied and we need to save the data
*/
public PatchBiFunction<ListTag, ListTag, Boolean> getBlockStatePatcher() { return null; }
/**
* Generates ready to use data for all currently registered patches. The list of
* patches is selected by the current patch-level of the world.
* <p>
* A {@link #Patch} with a given {@link #level} is only included if the patch-level of the
* world is less
* @param config The current patch-level configuration*
* @return a new {@link MigrationProfile} Object.
*/
static MigrationProfile createMigrationData(CompoundTag config) {
return new MigrationProfile(config, false);
}
/**
* This method is supposed to be used by developers to apply id-patches to custom nbt structures. It is only
* available in Developer-Mode
*
*/
static MigrationProfile createMigrationData() {
return new MigrationProfile(null, true);
}
/**
* Returns a list of paths where your mod stores IDs in your {@link ru.bclib.api.WorldDataAPI}-File.
* <p>
* {@link DataFixerAPI} will use information from the latest patch that returns a non-null-result. This list is used
* to automatically fix changed IDs from all active patches (see {@link Patch#getIDReplacements()}
* <p>
* The end of the path can either be a {@link net.minecraft.nbt.StringTag}, a {@link net.minecraft.nbt.ListTag} or
* a {@link CompoundTag}. If the Path contains a non-leaf {@link net.minecraft.nbt.ListTag}, all members of that
* list will be processed. For example:
* <pre>
* - global +
* | - key (String)
* | - items (List) +
* | - { id (String) }
* | - { id (String) }
* </pre>
* The path <b>global.items.id</b> will fix all <i>id</i>-entries in the <i>items</i>-list, while the path
* <b>global.key</b> will only fix the <i>key</i>-entry.
* <p>
* if the leaf-entry (= the last part of the path, which would be <i>items</i> in <b>global.items</b>) is a
* {@link CompoundTag}, the system will fix any <i>id</i> entry. If the {@link CompoundTag} contains an <i>item</i>
* or <i>tag.BlockEntityTag</i> entry, the system will recursivley continue with those. If an <i>items</i>
* or <i>inventory</i>-{@link net.minecraft.nbt.ListTag} was found, the system will continue recursivley with
* every item of that list.
* <p>
* if the leaf-entry is a {@link net.minecraft.nbt.ListTag}, it is handle the same as a child <i>items</i> entry
* of a {@link CompoundTag}.
*
* @return {@code null} if nothing changes or a list of Paths in your {@link ru.bclib.api.WorldDataAPI}-File.
* Paths are dot-seperated (see {@link ru.bclib.api.WorldDataAPI#getCompoundTag(String, String)}).
*/
public List<String> getWorldDataIDPaths() {
return null;
}
}

View file

@ -0,0 +1,6 @@
package ru.bclib.api.datafixer;
@FunctionalInterface
public interface PatchBiFunction<U, V, R> {
R apply(U t, V v, MigrationProfile profile) throws PatchDidiFailException;
}

View file

@ -0,0 +1,10 @@
package ru.bclib.api.datafixer;
public class PatchDidiFailException extends Exception {
public PatchDidiFailException(){
super();
}
public PatchDidiFailException(Exception e){
super(e);
}
}

View file

@ -0,0 +1,6 @@
package ru.bclib.api.datafixer;
@FunctionalInterface
public interface PatchFunction<T, R> {
R apply(T t, MigrationProfile profile) throws PatchDidiFailException;
}

View file

@ -26,25 +26,25 @@ import ru.bclib.registry.BaseBlockEntities;
public class BaseBarrelBlockEntity extends RandomizableContainerBlockEntity { public class BaseBarrelBlockEntity extends RandomizableContainerBlockEntity {
private NonNullList<ItemStack> inventory; private NonNullList<ItemStack> inventory;
private int viewerCount; private int viewerCount;
private BaseBarrelBlockEntity(BlockEntityType<?> type, BlockPos blockPos, BlockState blockState) { private BaseBarrelBlockEntity(BlockEntityType<?> type, BlockPos blockPos, BlockState blockState) {
super(type, blockPos, blockState); super(type, blockPos, blockState);
this.inventory = NonNullList.withSize(27, ItemStack.EMPTY); this.inventory = NonNullList.withSize(27, ItemStack.EMPTY);
} }
public BaseBarrelBlockEntity(BlockPos blockPos, BlockState blockState) { public BaseBarrelBlockEntity(BlockPos blockPos, BlockState blockState) {
this(BaseBlockEntities.BARREL, blockPos, blockState); this(BaseBlockEntities.BARREL, blockPos, blockState);
} }
public CompoundTag save(CompoundTag tag) { public CompoundTag save(CompoundTag tag) {
super.save(tag); super.save(tag);
if (!this.trySaveLootTable(tag)) { if (!this.trySaveLootTable(tag)) {
ContainerHelper.saveAllItems(tag, this.inventory); ContainerHelper.saveAllItems(tag, this.inventory);
} }
return tag; return tag;
} }
public void load(CompoundTag tag) { public void load(CompoundTag tag) {
super.load(tag); super.load(tag);
this.inventory = NonNullList.withSize(this.getContainerSize(), ItemStack.EMPTY); this.inventory = NonNullList.withSize(this.getContainerSize(), ItemStack.EMPTY);
@ -52,56 +52,57 @@ public class BaseBarrelBlockEntity extends RandomizableContainerBlockEntity {
ContainerHelper.loadAllItems(tag, this.inventory); ContainerHelper.loadAllItems(tag, this.inventory);
} }
} }
public int getContainerSize() { public int getContainerSize() {
return 27; return 27;
} }
protected NonNullList<ItemStack> getItems() { protected NonNullList<ItemStack> getItems() {
return this.inventory; return this.inventory;
} }
protected void setItems(NonNullList<ItemStack> list) { protected void setItems(NonNullList<ItemStack> list) {
this.inventory = list; this.inventory = list;
} }
protected Component getDefaultName() { protected Component getDefaultName() {
return new TranslatableComponent("container.barrel"); return new TranslatableComponent("container.barrel");
} }
protected AbstractContainerMenu createMenu(int syncId, Inventory playerInventory) { protected AbstractContainerMenu createMenu(int syncId, Inventory playerInventory) {
return ChestMenu.threeRows(syncId, playerInventory, this); return ChestMenu.threeRows(syncId, playerInventory, this);
} }
public void startOpen(Player player) { public void startOpen(Player player) {
if (!player.isSpectator()) { if (!player.isSpectator()) {
if (viewerCount < 0) { if (viewerCount < 0) {
viewerCount = 0; viewerCount = 0;
} }
++viewerCount; ++viewerCount;
BlockState blockState = this.getBlockState(); BlockState blockState = this.getBlockState();
if (!blockState.getValue(BarrelBlock.OPEN)) { if (!blockState.getValue(BarrelBlock.OPEN)) {
playSound(blockState, SoundEvents.BARREL_OPEN); playSound(blockState, SoundEvents.BARREL_OPEN);
setOpen(blockState, true); setOpen(blockState, true);
} }
if (level != null) { if (level != null) {
scheduleUpdate(); scheduleUpdate();
} }
} }
} }
private void scheduleUpdate() { private void scheduleUpdate() {
level.getBlockTicks().scheduleTick(getBlockPos(), getBlockState().getBlock(), 5); level.getBlockTicks().scheduleTick(getBlockPos(), getBlockState().getBlock(), 5);
} }
public void tick() { public void tick() {
if (level != null) { if (level != null) {
viewerCount = ChestBlockEntity.getOpenCount(level, worldPosition); viewerCount = ChestBlockEntity.getOpenCount(level, worldPosition);
if (viewerCount > 0) { if (viewerCount > 0) {
scheduleUpdate(); scheduleUpdate();
} else { }
else {
BlockState blockState = getBlockState(); BlockState blockState = getBlockState();
if (!(blockState.getBlock() instanceof BaseBarrelBlock)) { if (!(blockState.getBlock() instanceof BaseBarrelBlock)) {
setRemoved(); setRemoved();
@ -114,27 +115,35 @@ public class BaseBarrelBlockEntity extends RandomizableContainerBlockEntity {
} }
} }
} }
public void stopOpen(Player player) { public void stopOpen(Player player) {
if (!player.isSpectator()) { if (!player.isSpectator()) {
--this.viewerCount; --this.viewerCount;
} }
} }
private void setOpen(BlockState state, boolean open) { private void setOpen(BlockState state, boolean open) {
if (level != null) { if (level != null) {
level.setBlock(this.getBlockPos(), state.setValue(BarrelBlock.OPEN, open), 3); level.setBlock(this.getBlockPos(), state.setValue(BarrelBlock.OPEN, open), 3);
} }
} }
private void playSound(BlockState blockState, SoundEvent soundEvent) { private void playSound(BlockState blockState, SoundEvent soundEvent) {
if (level != null) { if (level != null) {
Vec3i vec3i = blockState.getValue(BarrelBlock.FACING).getNormal(); Vec3i vec3i = blockState.getValue(BarrelBlock.FACING).getNormal();
double d = (double) this.worldPosition.getX() + 0.5D + (double) vec3i.getX() / 2.0D; double d = (double) this.worldPosition.getX() + 0.5D + (double) vec3i.getX() / 2.0D;
double e = (double) this.worldPosition.getY() + 0.5D + (double) vec3i.getY() / 2.0D; double e = (double) this.worldPosition.getY() + 0.5D + (double) vec3i.getY() / 2.0D;
double f = (double) this.worldPosition.getZ() + 0.5D + (double) vec3i.getZ() / 2.0D; double f = (double) this.worldPosition.getZ() + 0.5D + (double) vec3i.getZ() / 2.0D;
level.playSound(null, d, e, f, soundEvent, SoundSource.BLOCKS, 0.5F, level.playSound(
this.level.random.nextFloat() * 0.1F + 0.9F); null,
d,
e,
f,
soundEvent,
SoundSource.BLOCKS,
0.5F,
this.level.random.nextFloat() * 0.1F + 0.9F
);
} }
} }
} }

View file

@ -15,11 +15,11 @@ public class BaseFurnaceBlockEntity extends AbstractFurnaceBlockEntity {
public BaseFurnaceBlockEntity(BlockPos blockPos, BlockState blockState) { public BaseFurnaceBlockEntity(BlockPos blockPos, BlockState blockState) {
super(BaseBlockEntities.FURNACE, blockPos, blockState, RecipeType.SMELTING); super(BaseBlockEntities.FURNACE, blockPos, blockState, RecipeType.SMELTING);
} }
protected Component getDefaultName() { protected Component getDefaultName() {
return new TranslatableComponent("container.furnace"); return new TranslatableComponent("container.furnace");
} }
protected AbstractContainerMenu createMenu(int syncId, Inventory playerInventory) { protected AbstractContainerMenu createMenu(int syncId, Inventory playerInventory) {
return new FurnaceMenu(syncId, playerInventory, this, this.dataAccess); return new FurnaceMenu(syncId, playerInventory, this, this.dataAccess);
} }

View file

@ -10,7 +10,7 @@ public class BaseSignBlockEntity extends SignBlockEntity {
public BaseSignBlockEntity(BlockPos blockPos, BlockState blockState) { public BaseSignBlockEntity(BlockPos blockPos, BlockState blockState) {
super(blockPos, blockState); super(blockPos, blockState);
} }
@Override @Override
public BlockEntityType<?> getType() { public BlockEntityType<?> getType() {
return BaseBlockEntities.SIGN; return BaseBlockEntities.SIGN;

View file

@ -1,11 +1,6 @@
package ru.bclib.blockentities; package ru.bclib.blockentities;
import java.util.Collections;
import java.util.Set;
import java.util.function.Supplier;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.entity.BlockEntity;
@ -13,33 +8,36 @@ import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
public class DynamicBlockEntityType<T extends BlockEntity> extends BlockEntityType<T> { import java.util.Collections;
import java.util.Set;
public class DynamicBlockEntityType<T extends BlockEntity> extends BlockEntityType<T> {
private final Set<Block> validBlocks = Sets.newHashSet(); private final Set<Block> validBlocks = Sets.newHashSet();
private final BlockEntitySupplier<? extends T> factory; private final BlockEntitySupplier<? extends T> factory;
public DynamicBlockEntityType(BlockEntitySupplier<? extends T> supplier) { public DynamicBlockEntityType(BlockEntitySupplier<? extends T> supplier) {
super(null, Collections.emptySet(), null); super(null, Collections.emptySet(), null);
this.factory = supplier; this.factory = supplier;
} }
@Override @Override
@Nullable public T create(BlockPos blockPos, BlockState blockState) { @Nullable
public T create(BlockPos blockPos, BlockState blockState) {
return factory.create(blockPos, blockState); return factory.create(blockPos, blockState);
} }
@Override @Override
public boolean isValid(BlockState blockState) { public boolean isValid(BlockState blockState) {
return validBlocks.contains(blockState.getBlock()); return validBlocks.contains(blockState.getBlock());
} }
public void registerBlock(Block block) { public void registerBlock(Block block) {
validBlocks.add(block); validBlocks.add(block);
} }
@FunctionalInterface @FunctionalInterface
public public interface BlockEntitySupplier<T extends BlockEntity> {
interface BlockEntitySupplier<T extends BlockEntity> {
T create(BlockPos blockPos, BlockState blockState); T create(BlockPos blockPos, BlockState blockState);
} }
} }

View file

@ -1,22 +1,17 @@
package ru.bclib.blocks; package ru.bclib.blocks;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.jetbrains.annotations.Nullable;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import net.fabricmc.api.EnvType; import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment; import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.item.v1.FabricItemSettings;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.minecraft.client.renderer.block.model.BlockModel; import net.minecraft.client.renderer.block.model.BlockModel;
import net.minecraft.client.resources.model.UnbakedModel; import net.minecraft.client.resources.model.UnbakedModel;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Item; import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.PickaxeItem;
import net.minecraft.world.level.block.AnvilBlock; import net.minecraft.world.level.block.AnvilBlock;
import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.block.Blocks;
@ -25,15 +20,25 @@ import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.IntegerProperty; import net.minecraft.world.level.block.state.properties.IntegerProperty;
import net.minecraft.world.level.material.MaterialColor; import net.minecraft.world.level.material.MaterialColor;
import net.minecraft.world.level.storage.loot.LootContext; import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
import org.jetbrains.annotations.Nullable;
import ru.bclib.client.models.BasePatterns; import ru.bclib.client.models.BasePatterns;
import ru.bclib.client.models.BlockModelProvider;
import ru.bclib.client.models.ModelsHelper; import ru.bclib.client.models.ModelsHelper;
import ru.bclib.client.models.PatternsHelper; import ru.bclib.client.models.PatternsHelper;
import ru.bclib.interfaces.BlockModelProvider;
import ru.bclib.interfaces.CustomItemProvider;
import ru.bclib.items.BaseAnvilItem; import ru.bclib.items.BaseAnvilItem;
public abstract class BaseAnvilBlock extends AnvilBlock implements BlockModelProvider { import java.util.Collections;
public static final IntegerProperty DESTRUCTION = BlockProperties.DESTRUCTION; import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Random;
public abstract class BaseAnvilBlock extends AnvilBlock implements BlockModelProvider, CustomItemProvider {
public static final IntegerProperty DESTRUCTION = BlockProperties.DESTRUCTION;
public IntegerProperty durability;
public BaseAnvilBlock(MaterialColor color) { public BaseAnvilBlock(MaterialColor color) {
super(FabricBlockSettings.copyOf(Blocks.ANVIL).mapColor(color)); super(FabricBlockSettings.copyOf(Blocks.ANVIL).mapColor(color));
} }
@ -41,35 +46,21 @@ public abstract class BaseAnvilBlock extends AnvilBlock implements BlockModelPro
@Override @Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) { protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
super.createBlockStateDefinition(builder); super.createBlockStateDefinition(builder);
builder.add(DESTRUCTION); if (getMaxDurability() != 3) {
} durability = IntegerProperty.create("durability", 0, getMaxDurability());
@Override
@SuppressWarnings("deprecation")
public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) {
ItemStack dropStack = new ItemStack(this);
int destruction = state.getValue(DESTRUCTION);
dropStack.getOrCreateTag().putInt(BaseAnvilItem.DESTRUCTION, destruction);
return Lists.newArrayList(dropStack);
}
protected String getTop(ResourceLocation blockId, String block) {
if (block.contains("item")) {
return blockId.getPath() + "_top_0";
} }
char last = block.charAt(block.length() - 1); else {
return blockId.getPath() + "_top_" + last; durability = BlockProperties.DEFAULT_ANVIL_DURABILITY;
}
builder.add(DESTRUCTION, durability);
} }
@Override
public abstract Item asItem();
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public BlockModel getItemModel(ResourceLocation blockId) { public BlockModel getItemModel(ResourceLocation blockId) {
return getBlockModel(blockId, defaultBlockState()); return getBlockModel(blockId, defaultBlockState());
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public @Nullable BlockModel getBlockModel(ResourceLocation blockId, BlockState blockState) { public @Nullable BlockModel getBlockModel(ResourceLocation blockId, BlockState blockState) {
@ -82,7 +73,7 @@ public abstract class BaseAnvilBlock extends AnvilBlock implements BlockModelPro
Optional<String> pattern = PatternsHelper.createJson(BasePatterns.BLOCK_ANVIL, textures); Optional<String> pattern = PatternsHelper.createJson(BasePatterns.BLOCK_ANVIL, textures);
return ModelsHelper.fromPattern(pattern); return ModelsHelper.fromPattern(pattern);
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public UnbakedModel getModelVariant(ResourceLocation stateId, BlockState blockState, Map<ResourceLocation, UnbakedModel> modelCache) { public UnbakedModel getModelVariant(ResourceLocation stateId, BlockState blockState, Map<ResourceLocation, UnbakedModel> modelCache) {
@ -93,4 +84,47 @@ public abstract class BaseAnvilBlock extends AnvilBlock implements BlockModelPro
registerBlockModel(stateId, modelLocation, blockState, modelCache); registerBlockModel(stateId, modelLocation, blockState, modelCache);
return ModelsHelper.createFacingModel(modelLocation, blockState.getValue(FACING), false, false); return ModelsHelper.createFacingModel(modelLocation, blockState.getValue(FACING), false, false);
} }
@Override
public BlockItem getCustomItem(ResourceLocation blockID, FabricItemSettings settings) {
return new BaseAnvilItem(this, settings);
}
@Override
@SuppressWarnings("deprecation")
public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) {
int destruction = state.getValue(DESTRUCTION);
int durability = state.getValue(getDurabilityProp());
int value = destruction * getMaxDurability() + durability;
ItemStack tool = builder.getParameter(LootContextParams.TOOL);
if (tool != null && tool.getItem() instanceof PickaxeItem) {
ItemStack itemStack = new ItemStack(this);
itemStack.getOrCreateTag().putInt(BaseAnvilItem.DESTRUCTION, value);
return Lists.newArrayList(itemStack);
}
return Collections.emptyList();
}
public IntegerProperty getDurabilityProp() {
return durability;
}
public int getMaxDurability() {
return 3;
}
public BlockState damageAnvilUse(BlockState state, Random random) {
IntegerProperty durability = getDurabilityProp();
int value = state.getValue(durability);
if (value < getMaxDurability() && random.nextInt(8) == 0) {
return state.setValue(durability, value + 1);
}
value = state.getValue(DESTRUCTION);
return value < 2 ? state.setValue(DESTRUCTION, value + 1).setValue(durability, 0) : null;
}
public BlockState damageAnvilFall(BlockState state) {
int destruction = state.getValue(DESTRUCTION);
return destruction < 2 ? state.setValue(DESTRUCTION, destruction + 1) : null;
}
} }

View file

@ -45,14 +45,14 @@ public abstract class BaseAttachedBlock extends BaseBlockNotFull {
} }
return null; return null;
} }
@Override @Override
public boolean canSurvive(BlockState state, LevelReader world, BlockPos pos) { public boolean canSurvive(BlockState state, LevelReader world, BlockPos pos) {
Direction direction = state.getValue(FACING); Direction direction = state.getValue(FACING);
BlockPos blockPos = pos.relative(direction.getOpposite()); BlockPos blockPos = pos.relative(direction.getOpposite());
return canSupportCenter(world, blockPos, direction) || world.getBlockState(blockPos).is(BlockTags.LEAVES); return canSupportCenter(world, blockPos, direction) || world.getBlockState(blockPos).is(BlockTags.LEAVES);
} }
@Override @Override
public BlockState updateShape(BlockState state, Direction facing, BlockState neighborState, LevelAccessor world, BlockPos pos, BlockPos neighborPos) { public BlockState updateShape(BlockState state, Direction facing, BlockState neighborState, LevelAccessor world, BlockPos pos, BlockPos neighborPos) {
if (!canSurvive(state, world, pos)) { if (!canSurvive(state, world, pos)) {
@ -62,13 +62,13 @@ public abstract class BaseAttachedBlock extends BaseBlockNotFull {
return state; return state;
} }
} }
@Override @Override
public BlockState rotate(BlockState state, Rotation rotation) { public BlockState rotate(BlockState state, Rotation rotation) {
return BlocksHelper.rotateHorizontal(state, rotation, FACING); return BlocksHelper.rotateHorizontal(state, rotation, FACING);
} }
@Override @Override
public BlockState mirror(BlockState state, Mirror mirror) { public BlockState mirror(BlockState state, Mirror mirror) {
return BlocksHelper.mirrorHorizontal(state, mirror, FACING); return BlocksHelper.mirrorHorizontal(state, mirror, FACING);

View file

@ -1,23 +1,23 @@
package ru.bclib.blocks; package ru.bclib.blocks;
import java.util.Optional;
import net.minecraft.core.Registry; import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import ru.bclib.client.models.BasePatterns; import ru.bclib.client.models.BasePatterns;
import ru.bclib.client.models.PatternsHelper; import ru.bclib.client.models.PatternsHelper;
import java.util.Optional;
public class BaseBarkBlock extends BaseRotatedPillarBlock { public class BaseBarkBlock extends BaseRotatedPillarBlock {
public BaseBarkBlock(Properties settings) { public BaseBarkBlock(Properties settings) {
super(settings); super(settings);
} }
@Override @Override
protected Optional<String> createBlockPattern(ResourceLocation blockId) { protected Optional<String> createBlockPattern(ResourceLocation blockId) {
blockId = Registry.BLOCK.getKey(this); blockId = Registry.BLOCK.getKey(this);
return PatternsHelper.createJson(BasePatterns.BLOCK_BASE, replacePath(blockId)); return PatternsHelper.createJson(BasePatterns.BLOCK_BASE, replacePath(blockId));
} }
private ResourceLocation replacePath(ResourceLocation blockId) { private ResourceLocation replacePath(ResourceLocation blockId) {
String newPath = blockId.getPath().replace("_bark", "_log_side"); String newPath = blockId.getPath().replace("_bark", "_log_side");
return new ResourceLocation(blockId.getNamespace(), newPath); return new ResourceLocation(blockId.getNamespace(), newPath);

View file

@ -1,12 +1,5 @@
package ru.bclib.blocks; package ru.bclib.blocks;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Random;
import org.jetbrains.annotations.Nullable;
import net.fabricmc.api.EnvType; import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment; import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
@ -24,7 +17,6 @@ import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.monster.piglin.PiglinAi; import net.minecraft.world.entity.monster.piglin.PiglinAi;
import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level; import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.BarrelBlock; import net.minecraft.world.level.block.BarrelBlock;
import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Block;
@ -33,23 +25,29 @@ import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.storage.loot.LootContext; import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.world.phys.BlockHitResult; import net.minecraft.world.phys.BlockHitResult;
import org.jetbrains.annotations.Nullable;
import ru.bclib.blockentities.BaseBarrelBlockEntity; import ru.bclib.blockentities.BaseBarrelBlockEntity;
import ru.bclib.client.models.BasePatterns; import ru.bclib.client.models.BasePatterns;
import ru.bclib.client.models.BlockModelProvider;
import ru.bclib.client.models.ModelsHelper; import ru.bclib.client.models.ModelsHelper;
import ru.bclib.client.models.PatternsHelper; import ru.bclib.client.models.PatternsHelper;
import ru.bclib.interfaces.BlockModelProvider;
import ru.bclib.registry.BaseBlockEntities; import ru.bclib.registry.BaseBlockEntities;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Random;
public class BaseBarrelBlock extends BarrelBlock implements BlockModelProvider { public class BaseBarrelBlock extends BarrelBlock implements BlockModelProvider {
public BaseBarrelBlock(Block source) { public BaseBarrelBlock(Block source) {
super(FabricBlockSettings.copyOf(source).noOcclusion()); super(FabricBlockSettings.copyOf(source).noOcclusion());
} }
@Override @Override
public BlockEntity newBlockEntity(BlockPos blockPos, BlockState blockState) { public BlockEntity newBlockEntity(BlockPos blockPos, BlockState blockState) {
return BaseBlockEntities.BARREL.create(blockPos, blockState); return BaseBlockEntities.BARREL.create(blockPos, blockState);
} }
@Override @Override
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) { public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) {
@ -57,24 +55,24 @@ public class BaseBarrelBlock extends BarrelBlock implements BlockModelProvider {
drop.add(new ItemStack(this.asItem())); drop.add(new ItemStack(this.asItem()));
return drop; return drop;
} }
@Override @Override
public InteractionResult use(BlockState state, Level world, BlockPos pos, Player player, InteractionHand hand, public InteractionResult use(BlockState state, Level world, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
BlockHitResult hit) {
if (world.isClientSide) { if (world.isClientSide) {
return InteractionResult.SUCCESS; return InteractionResult.SUCCESS;
} else { }
else {
BlockEntity blockEntity = world.getBlockEntity(pos); BlockEntity blockEntity = world.getBlockEntity(pos);
if (blockEntity instanceof BaseBarrelBlockEntity) { if (blockEntity instanceof BaseBarrelBlockEntity) {
player.openMenu((BaseBarrelBlockEntity) blockEntity); player.openMenu((BaseBarrelBlockEntity) blockEntity);
player.awardStat(Stats.OPEN_BARREL); player.awardStat(Stats.OPEN_BARREL);
PiglinAi.angerNearbyPiglins(player, true); PiglinAi.angerNearbyPiglins(player, true);
} }
return InteractionResult.CONSUME; return InteractionResult.CONSUME;
} }
} }
@Override @Override
public void tick(BlockState state, ServerLevel world, BlockPos pos, Random random) { public void tick(BlockState state, ServerLevel world, BlockPos pos, Random random) {
BlockEntity blockEntity = world.getBlockEntity(pos); BlockEntity blockEntity = world.getBlockEntity(pos);
@ -82,15 +80,14 @@ public class BaseBarrelBlock extends BarrelBlock implements BlockModelProvider {
((BaseBarrelBlockEntity) blockEntity).tick(); ((BaseBarrelBlockEntity) blockEntity).tick();
} }
} }
@Override @Override
public RenderShape getRenderShape(BlockState state) { public RenderShape getRenderShape(BlockState state) {
return RenderShape.MODEL; return RenderShape.MODEL;
} }
@Override @Override
public void setPlacedBy(Level world, BlockPos pos, BlockState state, LivingEntity placer, public void setPlacedBy(Level world, BlockPos pos, BlockState state, LivingEntity placer, ItemStack itemStack) {
ItemStack itemStack) {
if (itemStack.hasCustomHoverName()) { if (itemStack.hasCustomHoverName()) {
BlockEntity blockEntity = world.getBlockEntity(pos); BlockEntity blockEntity = world.getBlockEntity(pos);
if (blockEntity instanceof BaseBarrelBlockEntity) { if (blockEntity instanceof BaseBarrelBlockEntity) {
@ -98,41 +95,52 @@ public class BaseBarrelBlock extends BarrelBlock implements BlockModelProvider {
} }
} }
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public BlockModel getItemModel(ResourceLocation blockId) { public BlockModel getItemModel(ResourceLocation blockId) {
return getBlockModel(blockId, defaultBlockState()); return getBlockModel(blockId, defaultBlockState());
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public @Nullable BlockModel getBlockModel(ResourceLocation blockId, BlockState blockState) { public @Nullable BlockModel getBlockModel(ResourceLocation blockId, BlockState blockState) {
Optional<String> pattern; Optional<String> pattern;
if (blockState.getValue(OPEN)) { if (blockState.getValue(OPEN)) {
pattern = PatternsHelper.createJson(BasePatterns.BLOCK_BARREL_OPEN, blockId); pattern = PatternsHelper.createJson(BasePatterns.BLOCK_BARREL_OPEN, blockId);
} else { }
else {
pattern = PatternsHelper.createJson(BasePatterns.BLOCK_BOTTOM_TOP, blockId); pattern = PatternsHelper.createJson(BasePatterns.BLOCK_BOTTOM_TOP, blockId);
} }
return ModelsHelper.fromPattern(pattern); return ModelsHelper.fromPattern(pattern);
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public UnbakedModel getModelVariant(ResourceLocation stateId, BlockState blockState, Map<ResourceLocation, UnbakedModel> modelCache) { public UnbakedModel getModelVariant(ResourceLocation stateId, BlockState blockState, Map<ResourceLocation, UnbakedModel> modelCache) {
String open = blockState.getValue(OPEN) ? "_open" : ""; String open = blockState.getValue(OPEN) ? "_open" : "";
ResourceLocation modelId = new ResourceLocation(stateId.getNamespace(), ResourceLocation modelId = new ResourceLocation(stateId.getNamespace(), "block/" + stateId.getPath() + open);
"block/" + stateId.getPath() + open);
registerBlockModel(stateId, modelId, blockState, modelCache); registerBlockModel(stateId, modelId, blockState, modelCache);
Direction facing = blockState.getValue(FACING); Direction facing = blockState.getValue(FACING);
BlockModelRotation rotation = BlockModelRotation.X0_Y0; BlockModelRotation rotation = BlockModelRotation.X0_Y0;
switch (facing) { switch (facing) {
case NORTH: rotation = BlockModelRotation.X90_Y0; break; case NORTH:
case EAST: rotation = BlockModelRotation.X90_Y90; break; rotation = BlockModelRotation.X90_Y0;
case SOUTH: rotation = BlockModelRotation.X90_Y180; break; break;
case WEST: rotation = BlockModelRotation.X90_Y270; break; case EAST:
case DOWN: rotation = BlockModelRotation.X180_Y0; break; rotation = BlockModelRotation.X90_Y90;
default: break; break;
case SOUTH:
rotation = BlockModelRotation.X90_Y180;
break;
case WEST:
rotation = BlockModelRotation.X90_Y270;
break;
case DOWN:
rotation = BlockModelRotation.X180_Y0;
break;
default:
break;
} }
return ModelsHelper.createMultiVariant(modelId, rotation.getRotation(), false); return ModelsHelper.createMultiVariant(modelId, rotation.getRotation(), false);
} }

View file

@ -1,29 +1,73 @@
package ru.bclib.blocks; package ru.bclib.blocks;
import java.util.Collections; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import java.util.List;
import net.minecraft.client.renderer.block.model.BlockModel; import net.minecraft.client.renderer.block.model.BlockModel;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.MaterialColor;
import net.minecraft.world.level.storage.loot.LootContext; import net.minecraft.world.level.storage.loot.LootContext;
import ru.bclib.client.models.BlockModelProvider; import ru.bclib.interfaces.BlockModelProvider;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
/**
* Base class for a default Block.
* <p>
* This Block-Type will:
* <ul>
* <li>Drop itself</li>
* <li>Automatically create an Item-Model from the Block-Model</li>
* </ul>
*/
public class BaseBlock extends Block implements BlockModelProvider { public class BaseBlock extends Block implements BlockModelProvider {
/**
* Creates a new Block with the passed properties
*
* @param settings The properties of the Block.
*/
public BaseBlock(Properties settings) { public BaseBlock(Properties settings) {
super(settings); super(settings);
} }
/**
* {@inheritDoc}
* <p>
* This implementation will drop the Block itself
*/
@Override @Override
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) { public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) {
return Collections.singletonList(new ItemStack(this)); return Collections.singletonList(new ItemStack(this));
} }
/**
* {@inheritDoc}
* <p>
* This implementation will load the Block-Model and return it as the Item-Model
*/
@Override @Override
public BlockModel getItemModel(ResourceLocation blockId) { public BlockModel getItemModel(ResourceLocation blockId) {
return getBlockModel(blockId, defaultBlockState()); return getBlockModel(blockId, defaultBlockState());
} }
/**
* This method is used internally.
* <p>
* It is called from Block-Contructors, to allow the augmentation of the blocks
* preset properties.
* <p>
* For example in {@link BaseLeavesBlock#BaseLeavesBlock(Block, MaterialColor, Consumer)}
*
* @param customizeProperties A {@link Consumer} to call with the preset properties
* @param settings The properties as created by the Block
* @return The reconfigured {@code settings}
*/
static FabricBlockSettings acceptAndReturn(Consumer<FabricBlockSettings> customizeProperties, FabricBlockSettings settings) {
customizeProperties.accept(settings);
return settings;
}
} }

View file

@ -6,19 +6,19 @@ import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
public class BaseBlockNotFull extends BaseBlock { public class BaseBlockNotFull extends BaseBlock {
public BaseBlockNotFull(Properties settings) { public BaseBlockNotFull(Properties settings) {
super(settings); super(settings);
} }
public boolean canSuffocate(BlockState state, BlockGetter view, BlockPos pos) { public boolean canSuffocate(BlockState state, BlockGetter view, BlockPos pos) {
return false; return false;
} }
public boolean isSimpleFullBlock(BlockState state, BlockGetter view, BlockPos pos) { public boolean isSimpleFullBlock(BlockState state, BlockGetter view, BlockPos pos) {
return false; return false;
} }
public boolean allowsSpawning(BlockState state, BlockGetter view, BlockPos pos, EntityType<?> type) { public boolean allowsSpawning(BlockState state, BlockGetter view, BlockPos pos, EntityType<?> type) {
return false; return false;
} }

View file

@ -1,26 +1,25 @@
package ru.bclib.blocks; package ru.bclib.blocks;
import java.util.Collections;
import java.util.List;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.block.BaseEntityBlock; import net.minecraft.world.level.block.BaseEntityBlock;
import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.storage.loot.LootContext; import net.minecraft.world.level.storage.loot.LootContext;
import java.util.Collections;
import java.util.List;
public class BaseBlockWithEntity extends BaseEntityBlock { public class BaseBlockWithEntity extends BaseEntityBlock {
public BaseBlockWithEntity(Properties settings) { public BaseBlockWithEntity(Properties settings) {
super(settings); super(settings);
} }
@Override @Override
public BlockEntity newBlockEntity(BlockPos blockPos, BlockState blockState) { public BlockEntity newBlockEntity(BlockPos blockPos, BlockState blockState) {
return null; return null;
} }
@Override @Override
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) { public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) {

View file

@ -1,11 +1,5 @@
package ru.bclib.blocks; package ru.bclib.blocks;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import org.jetbrains.annotations.Nullable;
import net.fabricmc.api.EnvType; import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment; import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
@ -19,10 +13,15 @@ import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.storage.loot.LootContext; import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.world.level.storage.loot.parameters.LootContextParams; import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
import org.jetbrains.annotations.Nullable;
import ru.bclib.client.models.BasePatterns; import ru.bclib.client.models.BasePatterns;
import ru.bclib.client.models.ModelsHelper; import ru.bclib.client.models.ModelsHelper;
import ru.bclib.client.models.PatternsHelper; import ru.bclib.client.models.PatternsHelper;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
public class BaseBookshelfBlock extends BaseBlock { public class BaseBookshelfBlock extends BaseBlock {
public BaseBookshelfBlock(Block source) { public BaseBookshelfBlock(Block source) {
super(FabricBlockSettings.copyOf(source)); super(FabricBlockSettings.copyOf(source));
@ -39,14 +38,14 @@ public class BaseBookshelfBlock extends BaseBlock {
} }
return Collections.singletonList(new ItemStack(Items.BOOK, 3)); return Collections.singletonList(new ItemStack(Items.BOOK, 3));
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public @Nullable BlockModel getBlockModel(ResourceLocation blockId, BlockState blockState) { public @Nullable BlockModel getBlockModel(ResourceLocation blockId, BlockState blockState) {
Optional<String> pattern = PatternsHelper.createJson(BasePatterns.BLOCK_BOOKSHELF, replacePath(blockId)); Optional<String> pattern = PatternsHelper.createJson(BasePatterns.BLOCK_BOOKSHELF, replacePath(blockId));
return ModelsHelper.fromPattern(pattern); return ModelsHelper.fromPattern(pattern);
} }
private ResourceLocation replacePath(ResourceLocation blockId) { private ResourceLocation replacePath(ResourceLocation blockId) {
String newPath = blockId.getPath().replace("_bookshelf", ""); String newPath = blockId.getPath().replace("_bookshelf", "");
return new ResourceLocation(blockId.getNamespace(), newPath); return new ResourceLocation(blockId.getNamespace(), newPath);

View file

@ -1,12 +1,5 @@
package ru.bclib.blocks; package ru.bclib.blocks;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.jetbrains.annotations.Nullable;
import net.fabricmc.api.EnvType; import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment; import net.fabricmc.api.Environment;
import net.minecraft.client.renderer.block.model.BlockModel; import net.minecraft.client.renderer.block.model.BlockModel;
@ -20,26 +13,32 @@ import net.minecraft.world.level.block.ButtonBlock;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.AttachFace; import net.minecraft.world.level.block.state.properties.AttachFace;
import net.minecraft.world.level.storage.loot.LootContext; import net.minecraft.world.level.storage.loot.LootContext;
import org.jetbrains.annotations.Nullable;
import ru.bclib.client.models.BasePatterns; import ru.bclib.client.models.BasePatterns;
import ru.bclib.client.models.BlockModelProvider;
import ru.bclib.client.models.ModelsHelper; import ru.bclib.client.models.ModelsHelper;
import ru.bclib.client.models.PatternsHelper; import ru.bclib.client.models.PatternsHelper;
import ru.bclib.interfaces.BlockModelProvider;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
public abstract class BaseButtonBlock extends ButtonBlock implements BlockModelProvider { public abstract class BaseButtonBlock extends ButtonBlock implements BlockModelProvider {
private final Block parent; private final Block parent;
protected BaseButtonBlock(Block parent, Properties properties, boolean sensitive) { protected BaseButtonBlock(Block parent, Properties properties, boolean sensitive) {
super(sensitive, properties); super(sensitive, properties);
this.parent = parent; this.parent = parent;
} }
@Override @Override
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) { public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) {
return Collections.singletonList(new ItemStack(this)); return Collections.singletonList(new ItemStack(this));
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public BlockModel getItemModel(ResourceLocation blockId) { public BlockModel getItemModel(ResourceLocation blockId) {
@ -47,38 +46,56 @@ public abstract class BaseButtonBlock extends ButtonBlock implements BlockModelP
Optional<String> pattern = PatternsHelper.createJson(BasePatterns.ITEM_BUTTON, parentId); Optional<String> pattern = PatternsHelper.createJson(BasePatterns.ITEM_BUTTON, parentId);
return ModelsHelper.fromPattern(pattern); return ModelsHelper.fromPattern(pattern);
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public @Nullable BlockModel getBlockModel(ResourceLocation resourceLocation, BlockState blockState) { public @Nullable BlockModel getBlockModel(ResourceLocation resourceLocation, BlockState blockState) {
ResourceLocation parentId = Registry.BLOCK.getKey(parent); ResourceLocation parentId = Registry.BLOCK.getKey(parent);
Optional<String> pattern = blockState.getValue(POWERED) ? Optional<String> pattern = blockState.getValue(POWERED) ? PatternsHelper.createJson(
PatternsHelper.createJson(BasePatterns.BLOCK_BUTTON_PRESSED, parentId) : BasePatterns.BLOCK_BUTTON_PRESSED,
PatternsHelper.createJson(BasePatterns.BLOCK_BUTTON, parentId); parentId
) : PatternsHelper.createJson(BasePatterns.BLOCK_BUTTON, parentId);
return ModelsHelper.fromPattern(pattern); return ModelsHelper.fromPattern(pattern);
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public UnbakedModel getModelVariant(ResourceLocation stateId, BlockState blockState, Map<ResourceLocation, UnbakedModel> modelCache) { public UnbakedModel getModelVariant(ResourceLocation stateId, BlockState blockState, Map<ResourceLocation, UnbakedModel> modelCache) {
String powered = blockState.getValue(POWERED) ? "_powered" : ""; String powered = blockState.getValue(POWERED) ? "_powered" : "";
ResourceLocation modelId = new ResourceLocation(stateId.getNamespace(), ResourceLocation modelId = new ResourceLocation(stateId.getNamespace(), "block/" + stateId.getPath() + powered);
"block/" + stateId.getPath() + powered);
registerBlockModel(stateId, modelId, blockState, modelCache); registerBlockModel(stateId, modelId, blockState, modelCache);
AttachFace face = blockState.getValue(FACE); AttachFace face = blockState.getValue(FACE);
boolean isCeiling = face == AttachFace.CEILING; boolean isCeiling = face == AttachFace.CEILING;
int x = 0, y = 0; int x = 0, y = 0;
switch (face) { switch (face) {
case CEILING: x = 180; break; case CEILING:
case WALL: x = 90; break; x = 180;
default: break; break;
case WALL:
x = 90;
break;
default:
break;
} }
switch (blockState.getValue(FACING)) { switch (blockState.getValue(FACING)) {
case NORTH: if (isCeiling) { y = 180; } break; case NORTH:
case EAST: y = isCeiling ? 270 : 90; break; if (isCeiling) {
case SOUTH: if(!isCeiling) { y = 180; } break; y = 180;
case WEST: y = isCeiling ? 90 : 270; break; }
default: break; break;
case EAST:
y = isCeiling ? 270 : 90;
break;
case SOUTH:
if (!isCeiling) {
y = 180;
}
break;
case WEST:
y = isCeiling ? 90 : 270;
break;
default:
break;
} }
BlockModelRotation rotation = BlockModelRotation.by(x, y); BlockModelRotation rotation = BlockModelRotation.by(x, y);
return ModelsHelper.createMultiVariant(modelId, rotation.getRotation(), face == AttachFace.WALL); return ModelsHelper.createMultiVariant(modelId, rotation.getRotation(), face == AttachFace.WALL);

View file

@ -1,12 +1,5 @@
package ru.bclib.blocks; package ru.bclib.blocks;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.jetbrains.annotations.Nullable;
import net.fabricmc.api.EnvType; import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment; import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
@ -20,47 +13,52 @@ import net.minecraft.world.level.block.ChainBlock;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.MaterialColor; import net.minecraft.world.level.material.MaterialColor;
import net.minecraft.world.level.storage.loot.LootContext; import net.minecraft.world.level.storage.loot.LootContext;
import org.jetbrains.annotations.Nullable;
import ru.bclib.client.models.BasePatterns; import ru.bclib.client.models.BasePatterns;
import ru.bclib.client.models.BlockModelProvider;
import ru.bclib.client.models.ModelsHelper; import ru.bclib.client.models.ModelsHelper;
import ru.bclib.client.models.PatternsHelper; import ru.bclib.client.models.PatternsHelper;
import ru.bclib.client.render.BCLRenderLayer; import ru.bclib.client.render.BCLRenderLayer;
import ru.bclib.interfaces.IRenderTyped; import ru.bclib.interfaces.BlockModelProvider;
import ru.bclib.interfaces.RenderLayerProvider;
public class BaseChainBlock extends ChainBlock implements BlockModelProvider, IRenderTyped { import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
public class BaseChainBlock extends ChainBlock implements BlockModelProvider, RenderLayerProvider {
public BaseChainBlock(MaterialColor color) { public BaseChainBlock(MaterialColor color) {
super(FabricBlockSettings.copyOf(Blocks.CHAIN).mapColor(color)); super(FabricBlockSettings.copyOf(Blocks.CHAIN).mapColor(color));
} }
@Override @Override
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) { public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) {
return Collections.singletonList(new ItemStack(this)); return Collections.singletonList(new ItemStack(this));
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public BlockModel getItemModel(ResourceLocation blockId) { public BlockModel getItemModel(ResourceLocation blockId) {
return ModelsHelper.createItemModel(blockId); return ModelsHelper.createItemModel(blockId);
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public @Nullable BlockModel getBlockModel(ResourceLocation blockId, BlockState blockState) { public @Nullable BlockModel getBlockModel(ResourceLocation blockId, BlockState blockState) {
Optional<String> pattern = PatternsHelper.createJson(BasePatterns.BLOCK_CHAIN, blockId); Optional<String> pattern = PatternsHelper.createJson(BasePatterns.BLOCK_CHAIN, blockId);
return ModelsHelper.fromPattern(pattern); return ModelsHelper.fromPattern(pattern);
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public UnbakedModel getModelVariant(ResourceLocation stateId, BlockState blockState, Map<ResourceLocation, UnbakedModel> modelCache) { public UnbakedModel getModelVariant(ResourceLocation stateId, BlockState blockState, Map<ResourceLocation, UnbakedModel> modelCache) {
Direction.Axis axis = blockState.getValue(AXIS); Direction.Axis axis = blockState.getValue(AXIS);
ResourceLocation modelId = new ResourceLocation(stateId.getNamespace(), ResourceLocation modelId = new ResourceLocation(stateId.getNamespace(), "block/" + stateId.getPath());
"block/" + stateId.getPath());
registerBlockModel(stateId, modelId, blockState, modelCache); registerBlockModel(stateId, modelId, blockState, modelCache);
return ModelsHelper.createRotatedModel(modelId, axis); return ModelsHelper.createRotatedModel(modelId, axis);
} }
@Override @Override
public BCLRenderLayer getRenderLayer() { public BCLRenderLayer getRenderLayer() {
return BCLRenderLayer.CUTOUT; return BCLRenderLayer.CUTOUT;

View file

@ -1,30 +1,28 @@
package ru.bclib.blocks; package ru.bclib.blocks;
import java.util.List;
import java.util.Optional;
import net.minecraft.core.BlockPos;
import org.jetbrains.annotations.Nullable;
import net.fabricmc.api.EnvType; import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment; import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.minecraft.client.renderer.block.model.BlockModel; import net.minecraft.client.renderer.block.model.BlockModel;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Registry; import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.ChestBlock; import net.minecraft.world.level.block.ChestBlock;
import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.storage.loot.LootContext; import net.minecraft.world.level.storage.loot.LootContext;
import org.jetbrains.annotations.Nullable;
import ru.bclib.client.models.BasePatterns; import ru.bclib.client.models.BasePatterns;
import ru.bclib.client.models.BlockModelProvider;
import ru.bclib.client.models.ModelsHelper; import ru.bclib.client.models.ModelsHelper;
import ru.bclib.client.models.PatternsHelper; import ru.bclib.client.models.PatternsHelper;
import ru.bclib.interfaces.BlockModelProvider;
import ru.bclib.registry.BaseBlockEntities; import ru.bclib.registry.BaseBlockEntities;
import java.util.List;
import java.util.Optional;
public class BaseChestBlock extends ChestBlock implements BlockModelProvider { public class BaseChestBlock extends ChestBlock implements BlockModelProvider {
private final Block parent; private final Block parent;
@ -37,23 +35,22 @@ public class BaseChestBlock extends ChestBlock implements BlockModelProvider {
public BlockEntity newBlockEntity(BlockPos blockPos, BlockState blockState) { public BlockEntity newBlockEntity(BlockPos blockPos, BlockState blockState) {
return BaseBlockEntities.CHEST.create(blockPos, blockState); return BaseBlockEntities.CHEST.create(blockPos, blockState);
} }
@Override @Override
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) {
{
List<ItemStack> drop = super.getDrops(state, builder); List<ItemStack> drop = super.getDrops(state, builder);
drop.add(new ItemStack(this.asItem())); drop.add(new ItemStack(this.asItem()));
return drop; return drop;
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public BlockModel getItemModel(ResourceLocation blockId) { public BlockModel getItemModel(ResourceLocation blockId) {
Optional<String> pattern = PatternsHelper.createJson(BasePatterns.ITEM_CHEST, blockId); Optional<String> pattern = PatternsHelper.createJson(BasePatterns.ITEM_CHEST, blockId);
return ModelsHelper.fromPattern(pattern); return ModelsHelper.fromPattern(pattern);
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public @Nullable BlockModel getBlockModel(ResourceLocation resourceLocation, BlockState blockState) { public @Nullable BlockModel getBlockModel(ResourceLocation resourceLocation, BlockState blockState) {

View file

@ -1,12 +1,5 @@
package ru.bclib.blocks; package ru.bclib.blocks;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.jetbrains.annotations.Nullable;
import net.fabricmc.api.EnvType; import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment; import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
@ -18,56 +11,63 @@ import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.ComposterBlock; import net.minecraft.world.level.block.ComposterBlock;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.storage.loot.LootContext; import net.minecraft.world.level.storage.loot.LootContext;
import org.jetbrains.annotations.Nullable;
import ru.bclib.client.models.BasePatterns; import ru.bclib.client.models.BasePatterns;
import ru.bclib.client.models.BlockModelProvider;
import ru.bclib.client.models.ModelsHelper; import ru.bclib.client.models.ModelsHelper;
import ru.bclib.client.models.ModelsHelper.MultiPartBuilder; import ru.bclib.client.models.ModelsHelper.MultiPartBuilder;
import ru.bclib.client.models.PatternsHelper; import ru.bclib.client.models.PatternsHelper;
import ru.bclib.interfaces.BlockModelProvider;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
public class BaseComposterBlock extends ComposterBlock implements BlockModelProvider { public class BaseComposterBlock extends ComposterBlock implements BlockModelProvider {
public BaseComposterBlock(Block source) { public BaseComposterBlock(Block source) {
super(FabricBlockSettings.copyOf(source)); super(FabricBlockSettings.copyOf(source));
} }
@Override @Override
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) { public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) {
return Collections.singletonList(new ItemStack(this.asItem())); return Collections.singletonList(new ItemStack(this.asItem()));
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public BlockModel getItemModel(ResourceLocation resourceLocation) { public BlockModel getItemModel(ResourceLocation resourceLocation) {
return getBlockModel(resourceLocation, defaultBlockState()); return getBlockModel(resourceLocation, defaultBlockState());
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public @Nullable BlockModel getBlockModel(ResourceLocation blockId, BlockState blockState) { public @Nullable BlockModel getBlockModel(ResourceLocation blockId, BlockState blockState) {
Optional<String> pattern = PatternsHelper.createJson(BasePatterns.BLOCK_COMPOSTER, blockId); Optional<String> pattern = PatternsHelper.createJson(BasePatterns.BLOCK_COMPOSTER, blockId);
return ModelsHelper.fromPattern(pattern); return ModelsHelper.fromPattern(pattern);
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public UnbakedModel getModelVariant(ResourceLocation stateId, BlockState blockState, Map<ResourceLocation, UnbakedModel> modelCache) { public UnbakedModel getModelVariant(ResourceLocation stateId, BlockState blockState, Map<ResourceLocation, UnbakedModel> modelCache) {
ResourceLocation modelId = new ResourceLocation(stateId.getNamespace(), "block/" + stateId.getPath()); ResourceLocation modelId = new ResourceLocation(stateId.getNamespace(), "block/" + stateId.getPath());
registerBlockModel(stateId, modelId, blockState, modelCache); registerBlockModel(stateId, modelId, blockState, modelCache);
MultiPartBuilder builder = MultiPartBuilder.create(stateDefinition); MultiPartBuilder builder = MultiPartBuilder.create(stateDefinition);
LEVEL.getPossibleValues().forEach(level -> { LEVEL.getPossibleValues().forEach(level -> {
if (level > 0) { if (level > 0) {
ResourceLocation contentId; ResourceLocation contentId;
if (level > 7) { if (level > 7) {
contentId = new ResourceLocation("block/composter_contents_ready"); contentId = new ResourceLocation("block/composter_contents_ready");
} else { }
else {
contentId = new ResourceLocation("block/composter_contents" + level); contentId = new ResourceLocation("block/composter_contents" + level);
} }
builder.part(contentId).setCondition(state -> state.getValue(LEVEL).equals(level)).add(); builder.part(contentId).setCondition(state -> state.getValue(LEVEL).equals(level)).add();
} }
}); });
builder.part(modelId).add(); builder.part(modelId).add();
return builder.build(); return builder.build();
} }
} }

View file

@ -1,12 +1,5 @@
package ru.bclib.blocks; package ru.bclib.blocks;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;
import org.jetbrains.annotations.Nullable;
import net.fabricmc.api.EnvType; import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment; import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
@ -17,34 +10,41 @@ import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.CraftingTableBlock; import net.minecraft.world.level.block.CraftingTableBlock;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.storage.loot.LootContext; import net.minecraft.world.level.storage.loot.LootContext;
import org.jetbrains.annotations.Nullable;
import ru.bclib.client.models.BasePatterns; import ru.bclib.client.models.BasePatterns;
import ru.bclib.client.models.BlockModelProvider;
import ru.bclib.client.models.ModelsHelper; import ru.bclib.client.models.ModelsHelper;
import ru.bclib.client.models.PatternsHelper; import ru.bclib.client.models.PatternsHelper;
import ru.bclib.interfaces.BlockModelProvider;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;
public class BaseCraftingTableBlock extends CraftingTableBlock implements BlockModelProvider { public class BaseCraftingTableBlock extends CraftingTableBlock implements BlockModelProvider {
public BaseCraftingTableBlock(Block source) { public BaseCraftingTableBlock(Block source) {
super(FabricBlockSettings.copyOf(source)); super(FabricBlockSettings.copyOf(source));
} }
@Override @Override
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) { public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) {
return Collections.singletonList(new ItemStack(this.asItem())); return Collections.singletonList(new ItemStack(this.asItem()));
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public BlockModel getItemModel(ResourceLocation resourceLocation) { public BlockModel getItemModel(ResourceLocation resourceLocation) {
return getBlockModel(resourceLocation, defaultBlockState()); return getBlockModel(resourceLocation, defaultBlockState());
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public @Nullable BlockModel getBlockModel(ResourceLocation blockId, BlockState blockState) { public @Nullable BlockModel getBlockModel(ResourceLocation blockId, BlockState blockState) {
String blockName = blockId.getPath(); String blockName = blockId.getPath();
Optional<String> pattern = PatternsHelper.createJson(BasePatterns.BLOCK_SIDED, new HashMap<String, String>() { Optional<String> pattern = PatternsHelper.createJson(BasePatterns.BLOCK_SIDED, new HashMap<String, String>() {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
{ {
put("%modid%", blockId.getNamespace()); put("%modid%", blockId.getNamespace());
put("%particle%", blockName + "_front"); put("%particle%", blockName + "_front");

View file

@ -1,11 +1,6 @@
package ru.bclib.blocks; package ru.bclib.blocks;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.fabricmc.fabric.api.tool.attribute.v1.FabricToolTags; import net.fabricmc.fabric.api.tool.attribute.v1.FabricToolTags;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
@ -31,6 +26,10 @@ import net.minecraft.world.phys.shapes.VoxelShape;
import ru.bclib.util.BlocksHelper; import ru.bclib.util.BlocksHelper;
import ru.bclib.util.MHelper; import ru.bclib.util.MHelper;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class BaseCropBlock extends BasePlantBlock { public class BaseCropBlock extends BasePlantBlock {
private static final VoxelShape SHAPE = Block.box(2, 0, 2, 14, 14, 14); private static final VoxelShape SHAPE = Block.box(2, 0, 2, 14, 14, 14);
public static final IntegerProperty AGE = IntegerProperty.create("age", 0, 3); public static final IntegerProperty AGE = IntegerProperty.create("age", 0, 3);
@ -40,11 +39,11 @@ public class BaseCropBlock extends BasePlantBlock {
public BaseCropBlock(Item drop, Block... terrain) { public BaseCropBlock(Item drop, Block... terrain) {
super(FabricBlockSettings.of(Material.PLANT) super(FabricBlockSettings.of(Material.PLANT)
.breakByTool(FabricToolTags.HOES) .breakByTool(FabricToolTags.HOES)
.breakByHand(true) .breakByHand(true)
.sound(SoundType.GRASS) .sound(SoundType.GRASS)
.randomTicks() .randomTicks()
.noCollission()); .noCollission());
this.drop = drop; this.drop = drop;
this.terrain = terrain; this.terrain = terrain;
this.registerDefaultState(defaultBlockState().setValue(AGE, 0)); this.registerDefaultState(defaultBlockState().setValue(AGE, 0));
@ -57,7 +56,7 @@ public class BaseCropBlock extends BasePlantBlock {
@Override @Override
protected boolean isTerrain(BlockState state) { protected boolean isTerrain(BlockState state) {
for (Block block: terrain) { for (Block block : terrain) {
if (state.is(block)) { if (state.is(block)) {
return true; return true;
} }
@ -106,7 +105,7 @@ public class BaseCropBlock extends BasePlantBlock {
public boolean isBonemealSuccess(Level world, Random random, BlockPos pos, BlockState state) { public boolean isBonemealSuccess(Level world, Random random, BlockPos pos, BlockState state) {
return state.getValue(AGE) < 3; return state.getValue(AGE) < 3;
} }
@Override @Override
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public void tick(BlockState state, ServerLevel world, BlockPos pos, Random random) { public void tick(BlockState state, ServerLevel world, BlockPos pos, Random random) {

View file

@ -1,12 +1,5 @@
package ru.bclib.blocks; package ru.bclib.blocks;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.jetbrains.annotations.Nullable;
import net.fabricmc.api.EnvType; import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment; import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
@ -23,32 +16,37 @@ import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.DoorHingeSide; import net.minecraft.world.level.block.state.properties.DoorHingeSide;
import net.minecraft.world.level.block.state.properties.DoubleBlockHalf; import net.minecraft.world.level.block.state.properties.DoubleBlockHalf;
import net.minecraft.world.level.storage.loot.LootContext; import net.minecraft.world.level.storage.loot.LootContext;
import org.jetbrains.annotations.Nullable;
import ru.bclib.client.models.BasePatterns; import ru.bclib.client.models.BasePatterns;
import ru.bclib.client.models.BlockModelProvider;
import ru.bclib.client.models.ModelsHelper; import ru.bclib.client.models.ModelsHelper;
import ru.bclib.client.models.PatternsHelper; import ru.bclib.client.models.PatternsHelper;
import ru.bclib.client.render.BCLRenderLayer; import ru.bclib.client.render.BCLRenderLayer;
import ru.bclib.interfaces.IRenderTyped; import ru.bclib.interfaces.BlockModelProvider;
import ru.bclib.interfaces.RenderLayerProvider;
public class BaseDoorBlock extends DoorBlock implements IRenderTyped, BlockModelProvider { import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
public class BaseDoorBlock extends DoorBlock implements RenderLayerProvider, BlockModelProvider {
public BaseDoorBlock(Block source) { public BaseDoorBlock(Block source) {
super(FabricBlockSettings.copyOf(source).strength(3F, 3F).noOcclusion()); super(FabricBlockSettings.copyOf(source).strength(3F, 3F).noOcclusion());
} }
@Override @Override
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) { public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) {
if (state.getValue(HALF) == DoubleBlockHalf.LOWER) if (state.getValue(HALF) == DoubleBlockHalf.LOWER)
return Collections.singletonList(new ItemStack(this.asItem())); return Collections.singletonList(new ItemStack(this.asItem()));
else else return Collections.emptyList();
return Collections.emptyList();
} }
@Override @Override
public BCLRenderLayer getRenderLayer() { public BCLRenderLayer getRenderLayer() {
return BCLRenderLayer.CUTOUT; return BCLRenderLayer.CUTOUT;
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public @Nullable BlockModel getBlockModel(ResourceLocation resourceLocation, BlockState blockState) { public @Nullable BlockModel getBlockModel(ResourceLocation resourceLocation, BlockState blockState) {
@ -64,11 +62,12 @@ public class BaseDoorBlock extends DoorBlock implements IRenderTyped, BlockModel
case TOP: case TOP:
pattern = PatternsHelper.createJson(BasePatterns.BLOCK_DOOR_TOP, resourceLocation); pattern = PatternsHelper.createJson(BasePatterns.BLOCK_DOOR_TOP, resourceLocation);
break; break;
default: break; default:
break;
} }
return ModelsHelper.fromPattern(pattern); return ModelsHelper.fromPattern(pattern);
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public UnbakedModel getModelVariant(ResourceLocation stateId, BlockState blockState, Map<ResourceLocation, UnbakedModel> modelCache) { public UnbakedModel getModelVariant(ResourceLocation stateId, BlockState blockState, Map<ResourceLocation, UnbakedModel> modelCache) {
@ -81,23 +80,27 @@ public class BaseDoorBlock extends DoorBlock implements IRenderTyped, BlockModel
case EAST: case EAST:
if (hinge && open) { if (hinge && open) {
rotation = BlockModelRotation.X0_Y90; rotation = BlockModelRotation.X0_Y90;
} else if (open) { }
else if (open) {
rotation = BlockModelRotation.X0_Y270; rotation = BlockModelRotation.X0_Y270;
} }
break; break;
case SOUTH: case SOUTH:
if (!hinge && !open || hinge && !open) { if (!hinge && !open || hinge && !open) {
rotation = BlockModelRotation.X0_Y90; rotation = BlockModelRotation.X0_Y90;
} else if (hinge) { }
else if (hinge) {
rotation = BlockModelRotation.X0_Y180; rotation = BlockModelRotation.X0_Y180;
} }
break; break;
case WEST: case WEST:
if (!hinge && !open || hinge && !open) { if (!hinge && !open || hinge && !open) {
rotation = BlockModelRotation.X0_Y180; rotation = BlockModelRotation.X0_Y180;
} else if (hinge) { }
else if (hinge) {
rotation = BlockModelRotation.X0_Y270; rotation = BlockModelRotation.X0_Y270;
} else { }
else {
rotation = BlockModelRotation.X0_Y90; rotation = BlockModelRotation.X0_Y90;
} }
break; break;
@ -105,17 +108,20 @@ public class BaseDoorBlock extends DoorBlock implements IRenderTyped, BlockModel
default: default:
if (!hinge && !open || hinge && !open) { if (!hinge && !open || hinge && !open) {
rotation = BlockModelRotation.X0_Y270; rotation = BlockModelRotation.X0_Y270;
} else if (!hinge) { }
else if (!hinge) {
rotation = BlockModelRotation.X0_Y180; rotation = BlockModelRotation.X0_Y180;
} }
break; break;
} }
ResourceLocation modelId = new ResourceLocation(stateId.getNamespace(), ResourceLocation modelId = new ResourceLocation(
"block/" + stateId.getPath() + "_" + doorType); stateId.getNamespace(),
"block/" + stateId.getPath() + "_" + doorType
);
registerBlockModel(stateId, modelId, blockState, modelCache); registerBlockModel(stateId, modelId, blockState, modelCache);
return ModelsHelper.createMultiVariant(modelId, rotation.getRotation(), false); return ModelsHelper.createMultiVariant(modelId, rotation.getRotation(), false);
} }
protected DoorType getDoorType(BlockState blockState) { protected DoorType getDoorType(BlockState blockState) {
boolean isHinge = isHinge(blockState.getValue(HINGE), blockState.getValue(OPEN)); boolean isHinge = isHinge(blockState.getValue(HINGE), blockState.getValue(OPEN));
switch (blockState.getValue(HALF)) { switch (blockState.getValue(HALF)) {
@ -128,34 +134,30 @@ public class BaseDoorBlock extends DoorBlock implements IRenderTyped, BlockModel
} }
return DoorType.BOTTOM; return DoorType.BOTTOM;
} }
private boolean isHinge(DoorHingeSide hingeSide, boolean open) { private boolean isHinge(DoorHingeSide hingeSide, boolean open) {
boolean isHinge = hingeSide == DoorHingeSide.RIGHT; boolean isHinge = hingeSide == DoorHingeSide.RIGHT;
return isHinge && !open || !isHinge && open; return isHinge && !open || !isHinge && open;
} }
protected enum DoorType implements StringRepresentable { protected enum DoorType implements StringRepresentable {
BOTTOM_HINGE("bottom_hinge"), BOTTOM_HINGE("bottom_hinge"), TOP_HINGE("top_hinge"), BOTTOM("bottom"), TOP("top");
TOP_HINGE("top_hinge"),
BOTTOM("bottom"),
TOP("top");
private final String name; private final String name;
DoorType(String name) { DoorType(String name) {
this.name = name; this.name = name;
} }
public boolean isHinge() { public boolean isHinge() {
return this == BOTTOM_HINGE || return this == BOTTOM_HINGE || this == TOP_HINGE;
this == TOP_HINGE;
} }
@Override @Override
public String toString() { public String toString() {
return getSerializedName(); return getSerializedName();
} }
@Override @Override
public String getSerializedName() { public String getSerializedName() {
return name; return name;

View file

@ -1,10 +1,6 @@
package ru.bclib.blocks; package ru.bclib.blocks;
import java.util.List;
import java.util.Random;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.fabricmc.fabric.api.tool.attribute.v1.FabricToolTags; import net.fabricmc.fabric.api.tool.attribute.v1.FabricToolTags;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
@ -35,31 +31,34 @@ import net.minecraft.world.phys.Vec3;
import net.minecraft.world.phys.shapes.CollisionContext; import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape; import net.minecraft.world.phys.shapes.VoxelShape;
import ru.bclib.client.render.BCLRenderLayer; import ru.bclib.client.render.BCLRenderLayer;
import ru.bclib.interfaces.IRenderTyped; import ru.bclib.interfaces.RenderLayerProvider;
import ru.bclib.util.BlocksHelper; import ru.bclib.util.BlocksHelper;
import java.util.List;
import java.util.Random;
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public abstract class BaseDoublePlantBlock extends BaseBlockNotFull implements IRenderTyped, BonemealableBlock { public abstract class BaseDoublePlantBlock extends BaseBlockNotFull implements RenderLayerProvider, BonemealableBlock {
private static final VoxelShape SHAPE = Block.box(4, 2, 4, 12, 16, 12); private static final VoxelShape SHAPE = Block.box(4, 2, 4, 12, 16, 12);
public static final IntegerProperty ROTATION = BlockProperties.ROTATION; public static final IntegerProperty ROTATION = BlockProperties.ROTATION;
public static final BooleanProperty TOP = BooleanProperty.create("top"); public static final BooleanProperty TOP = BooleanProperty.create("top");
public BaseDoublePlantBlock() { public BaseDoublePlantBlock() {
super(FabricBlockSettings.of(Material.PLANT) super(FabricBlockSettings.of(Material.PLANT)
.breakByTool(FabricToolTags.SHEARS) .breakByTool(FabricToolTags.SHEARS)
.breakByHand(true) .breakByHand(true)
.sound(SoundType.WET_GRASS) .sound(SoundType.WET_GRASS)
.noCollission()); .noCollission());
this.registerDefaultState(this.stateDefinition.any().setValue(TOP, false)); this.registerDefaultState(this.stateDefinition.any().setValue(TOP, false));
} }
public BaseDoublePlantBlock(int light) { public BaseDoublePlantBlock(int light) {
super(FabricBlockSettings.of(Material.PLANT) super(FabricBlockSettings.of(Material.PLANT)
.breakByTool(FabricToolTags.SHEARS) .breakByTool(FabricToolTags.SHEARS)
.breakByHand(true) .breakByHand(true)
.sound(SoundType.WET_GRASS) .sound(SoundType.WET_GRASS)
.lightLevel((state) -> state.getValue(TOP) ? light : 0) .lightLevel((state) -> state.getValue(TOP) ? light : 0)
.noCollission()); .noCollission());
this.registerDefaultState(this.stateDefinition.any().setValue(TOP, false)); this.registerDefaultState(this.stateDefinition.any().setValue(TOP, false));
} }
@ -67,18 +66,18 @@ public abstract class BaseDoublePlantBlock extends BaseBlockNotFull implements I
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> stateManager) { protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> stateManager) {
stateManager.add(TOP, ROTATION); stateManager.add(TOP, ROTATION);
} }
@Override @Override
public VoxelShape getShape(BlockState state, BlockGetter view, BlockPos pos, CollisionContext ePos) { public VoxelShape getShape(BlockState state, BlockGetter view, BlockPos pos, CollisionContext ePos) {
Vec3 vec3d = state.getOffset(view, pos); Vec3 vec3d = state.getOffset(view, pos);
return SHAPE.move(vec3d.x, vec3d.y, vec3d.z); return SHAPE.move(vec3d.x, vec3d.y, vec3d.z);
} }
@Override @Override
public BlockBehaviour.OffsetType getOffsetType() { public BlockBehaviour.OffsetType getOffsetType() {
return BlockBehaviour.OffsetType.XZ; return BlockBehaviour.OffsetType.XZ;
} }
@Override @Override
public boolean canSurvive(BlockState state, LevelReader world, BlockPos pos) { public boolean canSurvive(BlockState state, LevelReader world, BlockPos pos) {
BlockState down = world.getBlockState(pos.below()); BlockState down = world.getBlockState(pos.below());
@ -91,7 +90,7 @@ public abstract class BaseDoublePlantBlock extends BaseBlockNotFull implements I
BlockState up = world.getBlockState(pos.above()); BlockState up = world.getBlockState(pos.above());
return state.getValue(TOP) ? down.getBlock() == this : isTerrain(down) && (up.getBlock() == this); return state.getValue(TOP) ? down.getBlock() == this : isTerrain(down) && (up.getBlock() == this);
} }
protected abstract boolean isTerrain(BlockState state); protected abstract boolean isTerrain(BlockState state);
@Override @Override
@ -111,7 +110,10 @@ public abstract class BaseDoublePlantBlock extends BaseBlockNotFull implements I
} }
ItemStack tool = builder.getParameter(LootContextParams.TOOL); ItemStack tool = builder.getParameter(LootContextParams.TOOL);
if (tool != null && FabricToolTags.SHEARS.contains(tool.getItem()) || EnchantmentHelper.getItemEnchantmentLevel(Enchantments.SILK_TOUCH, tool) > 0) { if (tool != null && FabricToolTags.SHEARS.contains(tool.getItem()) || EnchantmentHelper.getItemEnchantmentLevel(
Enchantments.SILK_TOUCH,
tool
) > 0) {
return Lists.newArrayList(new ItemStack(this)); return Lists.newArrayList(new ItemStack(this));
} }
else { else {
@ -123,20 +125,26 @@ public abstract class BaseDoublePlantBlock extends BaseBlockNotFull implements I
public BCLRenderLayer getRenderLayer() { public BCLRenderLayer getRenderLayer() {
return BCLRenderLayer.CUTOUT; return BCLRenderLayer.CUTOUT;
} }
@Override @Override
public boolean isValidBonemealTarget(BlockGetter world, BlockPos pos, BlockState state, boolean isClient) { public boolean isValidBonemealTarget(BlockGetter world, BlockPos pos, BlockState state, boolean isClient) {
return true; return true;
} }
@Override @Override
public boolean isBonemealSuccess(Level world, Random random, BlockPos pos, BlockState state) { public boolean isBonemealSuccess(Level world, Random random, BlockPos pos, BlockState state) {
return true; return true;
} }
@Override @Override
public void performBonemeal(ServerLevel world, Random random, BlockPos pos, BlockState state) { public void performBonemeal(ServerLevel world, Random random, BlockPos pos, BlockState state) {
ItemEntity item = new ItemEntity(world, pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, new ItemStack(this)); ItemEntity item = new ItemEntity(
world,
pos.getX() + 0.5,
pos.getY() + 0.5,
pos.getZ() + 0.5,
new ItemStack(this)
);
world.addFreshEntity(item); world.addFreshEntity(item);
} }

View file

@ -1,12 +1,5 @@
package ru.bclib.blocks; package ru.bclib.blocks;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.jetbrains.annotations.Nullable;
import net.fabricmc.api.EnvType; import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment; import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
@ -20,11 +13,17 @@ import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.FenceBlock; import net.minecraft.world.level.block.FenceBlock;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.storage.loot.LootContext; import net.minecraft.world.level.storage.loot.LootContext;
import org.jetbrains.annotations.Nullable;
import ru.bclib.client.models.BasePatterns; import ru.bclib.client.models.BasePatterns;
import ru.bclib.client.models.BlockModelProvider;
import ru.bclib.client.models.ModelsHelper; import ru.bclib.client.models.ModelsHelper;
import ru.bclib.client.models.ModelsHelper.MultiPartBuilder; import ru.bclib.client.models.ModelsHelper.MultiPartBuilder;
import ru.bclib.client.models.PatternsHelper; import ru.bclib.client.models.PatternsHelper;
import ru.bclib.interfaces.BlockModelProvider;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
public class BaseFenceBlock extends FenceBlock implements BlockModelProvider { public class BaseFenceBlock extends FenceBlock implements BlockModelProvider {
private final Block parent; private final Block parent;
@ -33,13 +32,13 @@ public class BaseFenceBlock extends FenceBlock implements BlockModelProvider {
super(FabricBlockSettings.copyOf(source).noOcclusion()); super(FabricBlockSettings.copyOf(source).noOcclusion());
this.parent = source; this.parent = source;
} }
@Override @Override
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) { public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) {
return Collections.singletonList(new ItemStack(this)); return Collections.singletonList(new ItemStack(this));
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public BlockModel getItemModel(ResourceLocation blockId) { public BlockModel getItemModel(ResourceLocation blockId) {
@ -47,7 +46,7 @@ public class BaseFenceBlock extends FenceBlock implements BlockModelProvider {
Optional<String> pattern = PatternsHelper.createJson(BasePatterns.ITEM_FENCE, parentId); Optional<String> pattern = PatternsHelper.createJson(BasePatterns.ITEM_FENCE, parentId);
return ModelsHelper.fromPattern(pattern); return ModelsHelper.fromPattern(pattern);
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public @Nullable BlockModel getBlockModel(ResourceLocation blockId, BlockState blockState) { public @Nullable BlockModel getBlockModel(ResourceLocation blockId, BlockState blockState) {
@ -62,27 +61,34 @@ public class BaseFenceBlock extends FenceBlock implements BlockModelProvider {
} }
return ModelsHelper.fromPattern(pattern); return ModelsHelper.fromPattern(pattern);
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public UnbakedModel getModelVariant(ResourceLocation stateId, BlockState blockState, Map<ResourceLocation, UnbakedModel> modelCache) { public UnbakedModel getModelVariant(ResourceLocation stateId, BlockState blockState, Map<ResourceLocation, UnbakedModel> modelCache) {
ResourceLocation postId = new ResourceLocation(stateId.getNamespace(), ResourceLocation postId = new ResourceLocation(stateId.getNamespace(), "block/" + stateId.getPath() + "_post");
"block/" + stateId.getPath() + "_post"); ResourceLocation sideId = new ResourceLocation(stateId.getNamespace(), "block/" + stateId.getPath() + "_side");
ResourceLocation sideId = new ResourceLocation(stateId.getNamespace(),
"block/" + stateId.getPath() + "_side");
registerBlockModel(postId, postId, blockState, modelCache); registerBlockModel(postId, postId, blockState, modelCache);
registerBlockModel(sideId, sideId, blockState, modelCache); registerBlockModel(sideId, sideId, blockState, modelCache);
MultiPartBuilder builder = MultiPartBuilder.create(stateDefinition); MultiPartBuilder builder = MultiPartBuilder.create(stateDefinition);
builder.part(sideId).setCondition(state -> state.getValue(NORTH)).setUVLock(true).add(); builder.part(sideId).setCondition(state -> state.getValue(NORTH)).setUVLock(true).add();
builder.part(sideId).setCondition(state -> state.getValue(EAST)) builder.part(sideId)
.setTransformation(BlockModelRotation.X0_Y90.getRotation()).setUVLock(true).add(); .setCondition(state -> state.getValue(EAST))
builder.part(sideId).setCondition(state -> state.getValue(SOUTH)) .setTransformation(BlockModelRotation.X0_Y90.getRotation())
.setTransformation(BlockModelRotation.X0_Y180.getRotation()).setUVLock(true).add(); .setUVLock(true)
builder.part(sideId).setCondition(state -> state.getValue(WEST)) .add();
.setTransformation(BlockModelRotation.X0_Y270.getRotation()).setUVLock(true).add(); builder.part(sideId)
.setCondition(state -> state.getValue(SOUTH))
.setTransformation(BlockModelRotation.X0_Y180.getRotation())
.setUVLock(true)
.add();
builder.part(sideId)
.setCondition(state -> state.getValue(WEST))
.setTransformation(BlockModelRotation.X0_Y270.getRotation())
.setUVLock(true)
.add();
builder.part(postId).add(); builder.part(postId).add();
return builder.build(); return builder.build();
} }
} }

View file

@ -26,22 +26,22 @@ import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import ru.bclib.blockentities.BaseFurnaceBlockEntity; import ru.bclib.blockentities.BaseFurnaceBlockEntity;
import ru.bclib.client.models.BasePatterns; import ru.bclib.client.models.BasePatterns;
import ru.bclib.client.models.BlockModelProvider;
import ru.bclib.client.models.ModelsHelper; import ru.bclib.client.models.ModelsHelper;
import ru.bclib.client.models.PatternsHelper; import ru.bclib.client.models.PatternsHelper;
import ru.bclib.client.render.BCLRenderLayer; import ru.bclib.client.render.BCLRenderLayer;
import ru.bclib.interfaces.IRenderTyped; import ru.bclib.interfaces.BlockModelProvider;
import ru.bclib.interfaces.RenderLayerProvider;
import ru.bclib.registry.BaseBlockEntities; import ru.bclib.registry.BaseBlockEntities;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
public class BaseFurnaceBlock extends FurnaceBlock implements BlockModelProvider, IRenderTyped { public class BaseFurnaceBlock extends FurnaceBlock implements BlockModelProvider, RenderLayerProvider {
public BaseFurnaceBlock(Block source) { public BaseFurnaceBlock(Block source) {
super(FabricBlockSettings.copyOf(source).luminance(state -> state.getValue(LIT) ? 13 : 0)); super(FabricBlockSettings.copyOf(source).luminance(state -> state.getValue(LIT) ? 13 : 0));
} }
@Override @Override
public BlockEntity newBlockEntity(BlockPos blockPos, BlockState blockState) { public BlockEntity newBlockEntity(BlockPos blockPos, BlockState blockState) {
return new BaseFurnaceBlockEntity(blockPos, blockState); return new BaseFurnaceBlockEntity(blockPos, blockState);
@ -55,7 +55,7 @@ public class BaseFurnaceBlock extends FurnaceBlock implements BlockModelProvider
player.awardStat(Stats.INTERACT_WITH_FURNACE); player.awardStat(Stats.INTERACT_WITH_FURNACE);
} }
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public @Nullable BlockModel getBlockModel(ResourceLocation blockId, BlockState blockState) { public @Nullable BlockModel getBlockModel(ResourceLocation blockId, BlockState blockState) {
@ -69,34 +69,34 @@ public class BaseFurnaceBlock extends FurnaceBlock implements BlockModelProvider
textures.put("%front%", blockName + "_front_on"); textures.put("%front%", blockName + "_front_on");
textures.put("%glow%", blockName + "_glow"); textures.put("%glow%", blockName + "_glow");
pattern = PatternsHelper.createJson(BasePatterns.BLOCK_FURNACE_LIT, textures); pattern = PatternsHelper.createJson(BasePatterns.BLOCK_FURNACE_LIT, textures);
} else { }
else {
textures.put("%front%", blockName + "_front"); textures.put("%front%", blockName + "_front");
pattern = PatternsHelper.createJson(BasePatterns.BLOCK_FURNACE, textures); pattern = PatternsHelper.createJson(BasePatterns.BLOCK_FURNACE, textures);
} }
return ModelsHelper.fromPattern(pattern); return ModelsHelper.fromPattern(pattern);
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public BlockModel getItemModel(ResourceLocation resourceLocation) { public BlockModel getItemModel(ResourceLocation resourceLocation) {
return getBlockModel(resourceLocation, defaultBlockState()); return getBlockModel(resourceLocation, defaultBlockState());
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public UnbakedModel getModelVariant(ResourceLocation stateId, BlockState blockState, Map<ResourceLocation, UnbakedModel> modelCache) { public UnbakedModel getModelVariant(ResourceLocation stateId, BlockState blockState, Map<ResourceLocation, UnbakedModel> modelCache) {
String lit = blockState.getValue(LIT) ? "_lit" : ""; String lit = blockState.getValue(LIT) ? "_lit" : "";
ResourceLocation modelId = new ResourceLocation(stateId.getNamespace(), ResourceLocation modelId = new ResourceLocation(stateId.getNamespace(), "block/" + stateId.getPath() + lit);
"block/" + stateId.getPath() + lit);
registerBlockModel(stateId, modelId, blockState, modelCache); registerBlockModel(stateId, modelId, blockState, modelCache);
return ModelsHelper.createFacingModel(modelId, blockState.getValue(FACING), false, true); return ModelsHelper.createFacingModel(modelId, blockState.getValue(FACING), false, true);
} }
@Override @Override
public BCLRenderLayer getRenderLayer() { public BCLRenderLayer getRenderLayer() {
return BCLRenderLayer.CUTOUT; return BCLRenderLayer.CUTOUT;
} }
@Override @Override
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) { public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) {
@ -110,15 +110,19 @@ public class BaseFurnaceBlock extends FurnaceBlock implements BlockModelProvider
} }
return drop; return drop;
} }
@Override @Override
@Nullable @Nullable
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, BlockState blockState, BlockEntityType<T> blockEntityType) { public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, BlockState blockState, BlockEntityType<T> blockEntityType) {
return createFurnaceTicker(level, blockEntityType, BaseBlockEntities.FURNACE); return createFurnaceTicker(level, blockEntityType, BaseBlockEntities.FURNACE);
} }
@Nullable @Nullable
protected static <T extends BlockEntity> BlockEntityTicker<T> createFurnaceTicker(Level level, BlockEntityType<T> blockEntityType, BlockEntityType<? extends AbstractFurnaceBlockEntity> blockEntityType2) { protected static <T extends BlockEntity> BlockEntityTicker<T> createFurnaceTicker(Level level, BlockEntityType<T> blockEntityType, BlockEntityType<? extends AbstractFurnaceBlockEntity> blockEntityType2) {
return level.isClientSide ? null : createTickerHelper(blockEntityType, blockEntityType2, AbstractFurnaceBlockEntity::serverTick); return level.isClientSide ? null : createTickerHelper(
blockEntityType,
blockEntityType2,
AbstractFurnaceBlockEntity::serverTick
);
} }
} }

View file

@ -1,12 +1,5 @@
package ru.bclib.blocks; package ru.bclib.blocks;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.jetbrains.annotations.Nullable;
import net.fabricmc.api.EnvType; import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment; import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
@ -19,10 +12,16 @@ import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.FenceGateBlock; import net.minecraft.world.level.block.FenceGateBlock;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.storage.loot.LootContext; import net.minecraft.world.level.storage.loot.LootContext;
import org.jetbrains.annotations.Nullable;
import ru.bclib.client.models.BasePatterns; import ru.bclib.client.models.BasePatterns;
import ru.bclib.client.models.BlockModelProvider;
import ru.bclib.client.models.ModelsHelper; import ru.bclib.client.models.ModelsHelper;
import ru.bclib.client.models.PatternsHelper; import ru.bclib.client.models.PatternsHelper;
import ru.bclib.interfaces.BlockModelProvider;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
public class BaseGateBlock extends FenceGateBlock implements BlockModelProvider { public class BaseGateBlock extends FenceGateBlock implements BlockModelProvider {
private final Block parent; private final Block parent;
@ -31,19 +30,19 @@ public class BaseGateBlock extends FenceGateBlock implements BlockModelProvider
super(FabricBlockSettings.copyOf(source).noOcclusion()); super(FabricBlockSettings.copyOf(source).noOcclusion());
this.parent = source; this.parent = source;
} }
@Override @Override
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) { public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) {
return Collections.singletonList(new ItemStack(this)); return Collections.singletonList(new ItemStack(this));
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public BlockModel getItemModel(ResourceLocation resourceLocation) { public BlockModel getItemModel(ResourceLocation resourceLocation) {
return getBlockModel(resourceLocation, defaultBlockState()); return getBlockModel(resourceLocation, defaultBlockState());
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public @Nullable BlockModel getBlockModel(ResourceLocation blockId, BlockState blockState) { public @Nullable BlockModel getBlockModel(ResourceLocation blockId, BlockState blockState) {
@ -52,23 +51,27 @@ public class BaseGateBlock extends FenceGateBlock implements BlockModelProvider
ResourceLocation parentId = Registry.BLOCK.getKey(parent); ResourceLocation parentId = Registry.BLOCK.getKey(parent);
Optional<String> pattern; Optional<String> pattern;
if (inWall) { if (inWall) {
pattern = isOpen ? PatternsHelper.createJson(BasePatterns.BLOCK_GATE_OPEN_WALL, parentId) : pattern = isOpen ? PatternsHelper.createJson(
PatternsHelper.createJson(BasePatterns.BLOCK_GATE_CLOSED_WALL, parentId); BasePatterns.BLOCK_GATE_OPEN_WALL,
} else { parentId
pattern = isOpen ? PatternsHelper.createJson(BasePatterns.BLOCK_GATE_OPEN, parentId) : ) : PatternsHelper.createJson(BasePatterns.BLOCK_GATE_CLOSED_WALL, parentId);
PatternsHelper.createJson(BasePatterns.BLOCK_GATE_CLOSED, parentId); }
else {
pattern = isOpen ? PatternsHelper.createJson(
BasePatterns.BLOCK_GATE_OPEN,
parentId
) : PatternsHelper.createJson(BasePatterns.BLOCK_GATE_CLOSED, parentId);
} }
return ModelsHelper.fromPattern(pattern); return ModelsHelper.fromPattern(pattern);
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public UnbakedModel getModelVariant(ResourceLocation stateId, BlockState blockState, Map<ResourceLocation, UnbakedModel> modelCache) { public UnbakedModel getModelVariant(ResourceLocation stateId, BlockState blockState, Map<ResourceLocation, UnbakedModel> modelCache) {
boolean inWall = blockState.getValue(IN_WALL); boolean inWall = blockState.getValue(IN_WALL);
boolean isOpen = blockState.getValue(OPEN); boolean isOpen = blockState.getValue(OPEN);
String state = "" + (inWall ? "_wall" : "") + (isOpen ? "_open" : "_closed"); String state = "" + (inWall ? "_wall" : "") + (isOpen ? "_open" : "_closed");
ResourceLocation modelId = new ResourceLocation(stateId.getNamespace(), ResourceLocation modelId = new ResourceLocation(stateId.getNamespace(), "block/" + stateId.getPath() + state);
"block/" + stateId.getPath() + state);
registerBlockModel(stateId, modelId, blockState, modelCache); registerBlockModel(stateId, modelId, blockState, modelCache);
return ModelsHelper.createFacingModel(modelId, blockState.getValue(FACING), true, false); return ModelsHelper.createFacingModel(modelId, blockState.getValue(FACING), true, false);
} }

View file

@ -1,159 +1,52 @@
package ru.bclib.blocks; package ru.bclib.blocks;
import java.util.Map;
import java.util.Optional;
import org.jetbrains.annotations.Nullable;
import net.fabricmc.api.EnvType; import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment; import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.minecraft.client.renderer.block.model.BlockModel; import net.minecraft.client.renderer.block.model.BlockModel;
import net.minecraft.client.resources.model.UnbakedModel; import net.minecraft.client.resources.model.UnbakedModel;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.context.BlockPlaceContext; import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.block.LadderBlock;
import net.minecraft.world.level.block.HorizontalDirectionalBlock;
import net.minecraft.world.level.block.Mirror;
import net.minecraft.world.level.block.Rotation;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition; import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.world.level.block.state.properties.BlockStateProperties; import org.jetbrains.annotations.Nullable;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.block.state.properties.DirectionProperty;
import net.minecraft.world.level.material.FluidState;
import net.minecraft.world.level.material.Fluids;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;
import ru.bclib.client.models.BasePatterns; import ru.bclib.client.models.BasePatterns;
import ru.bclib.client.models.BlockModelProvider;
import ru.bclib.client.models.ModelsHelper; import ru.bclib.client.models.ModelsHelper;
import ru.bclib.client.models.PatternsHelper; import ru.bclib.client.models.PatternsHelper;
import ru.bclib.client.render.BCLRenderLayer; import ru.bclib.client.render.BCLRenderLayer;
import ru.bclib.interfaces.IRenderTyped; import ru.bclib.interfaces.BlockModelProvider;
import ru.bclib.util.BlocksHelper; import ru.bclib.interfaces.RenderLayerProvider;
@SuppressWarnings("deprecation") import java.util.Collections;
public class BaseLadderBlock extends BaseBlockNotFull implements IRenderTyped, BlockModelProvider { import java.util.List;
public static final DirectionProperty FACING = HorizontalDirectionalBlock.FACING; import java.util.Map;
public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED; import java.util.Optional;
protected static final VoxelShape EAST_SHAPE = Block.box(0.0D, 0.0D, 0.0D, 3.0D, 16.0D, 16.0D);
protected static final VoxelShape WEST_SHAPE = Block.box(13.0D, 0.0D, 0.0D, 16.0D, 16.0D, 16.0D);
protected static final VoxelShape SOUTH_SHAPE = Block.box(0.0D, 0.0D, 0.0D, 16.0D, 16.0D, 3.0D);
protected static final VoxelShape NORTH_SHAPE = Block.box(0.0D, 0.0D, 13.0D, 16.0D, 16.0D, 16.0D);
public class BaseLadderBlock extends LadderBlock implements RenderLayerProvider, BlockModelProvider {
public BaseLadderBlock(Block block) { public BaseLadderBlock(Block block) {
super(FabricBlockSettings.copyOf(block).noOcclusion()); super(FabricBlockSettings.copyOf(block).noOcclusion());
} }
@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> stateManager) {
stateManager.add(FACING);
stateManager.add(WATERLOGGED);
}
@Override
public VoxelShape getShape(BlockState state, BlockGetter view, BlockPos pos, CollisionContext ePos) {
return switch (state.getValue(FACING)) {
case SOUTH -> SOUTH_SHAPE;
case WEST -> WEST_SHAPE;
case EAST -> EAST_SHAPE;
default -> NORTH_SHAPE;
};
}
private boolean canPlaceOn(BlockGetter world, BlockPos pos, Direction side) {
BlockState blockState = world.getBlockState(pos);
return !blockState.isSignalSource() && blockState.isFaceSturdy(world, pos, side);
}
@Override
public boolean canSurvive(BlockState state, LevelReader world, BlockPos pos) {
Direction direction = state.getValue(FACING);
return canPlaceOn(world, pos.relative(direction.getOpposite()), direction);
}
@Override
public BlockState updateShape(BlockState state, Direction facing, BlockState neighborState,
LevelAccessor world, BlockPos pos, BlockPos neighborPos) {
if (facing.getOpposite() == state.getValue(FACING) && !state.canSurvive(world, pos)) {
return Blocks.AIR.defaultBlockState();
} else {
if (state.getValue(WATERLOGGED)) {
world.getLiquidTicks().scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(world));
}
return super.updateShape(state, facing, neighborState, world, pos, neighborPos);
}
}
@Override
public BlockState getStateForPlacement(BlockPlaceContext ctx) {
BlockState blockState;
if (!ctx.replacingClickedOnBlock()) {
blockState = ctx.getLevel().getBlockState(ctx.getClickedPos().relative(ctx.getClickedFace().getOpposite()));
if (blockState.getBlock() == this && blockState.getValue(FACING) == ctx.getClickedFace()) {
return null;
}
}
blockState = defaultBlockState();
LevelReader worldView = ctx.getLevel();
BlockPos blockPos = ctx.getClickedPos();
FluidState fluidState = ctx.getLevel().getFluidState(ctx.getClickedPos());
Direction[] directions = ctx.getNearestLookingDirections();
for (Direction direction : directions) {
if (direction.getAxis().isHorizontal()) {
blockState = blockState.setValue(FACING, direction.getOpposite());
if (blockState.canSurvive(worldView, blockPos)) {
return blockState.setValue(WATERLOGGED, fluidState.getType() == Fluids.WATER);
}
}
}
return null;
}
@Override
public BlockState rotate(BlockState state, Rotation rotation) {
return BlocksHelper.rotateHorizontal(state, rotation, FACING);
}
@Override
public BlockState mirror(BlockState state, Mirror mirror) {
return BlocksHelper.mirrorHorizontal(state, mirror, FACING);
}
@Override
public FluidState getFluidState(BlockState state) {
return state.getValue(WATERLOGGED) ? Fluids.WATER.getSource(false) : super.getFluidState(state);
}
@Override @Override
public BCLRenderLayer getRenderLayer() { public BCLRenderLayer getRenderLayer() {
return BCLRenderLayer.CUTOUT; return BCLRenderLayer.CUTOUT;
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public BlockModel getItemModel(ResourceLocation blockId) { public BlockModel getItemModel(ResourceLocation blockId) {
return ModelsHelper.createBlockItem(blockId); return ModelsHelper.createBlockItem(blockId);
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public @Nullable BlockModel getBlockModel(ResourceLocation blockId, BlockState blockState) { public @Nullable BlockModel getBlockModel(ResourceLocation blockId, BlockState blockState) {
Optional<String> pattern = PatternsHelper.createJson(BasePatterns.BLOCK_LADDER, blockId); Optional<String> pattern = PatternsHelper.createJson(BasePatterns.BLOCK_LADDER, blockId);
return ModelsHelper.fromPattern(pattern); return ModelsHelper.fromPattern(pattern);
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public UnbakedModel getModelVariant(ResourceLocation stateId, BlockState blockState, Map<ResourceLocation, UnbakedModel> modelCache) { public UnbakedModel getModelVariant(ResourceLocation stateId, BlockState blockState, Map<ResourceLocation, UnbakedModel> modelCache) {
@ -161,4 +54,10 @@ public class BaseLadderBlock extends BaseBlockNotFull implements IRenderTyped, B
registerBlockModel(stateId, modelId, blockState, modelCache); registerBlockModel(stateId, modelId, blockState, modelCache);
return ModelsHelper.createFacingModel(modelId, blockState.getValue(FACING), false, true); return ModelsHelper.createFacingModel(modelId, blockState.getValue(FACING), false, true);
} }
@Override
@SuppressWarnings("deprecation")
public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) {
return Collections.singletonList(new ItemStack(this));
}
} }

View file

@ -1,10 +1,6 @@
package ru.bclib.blocks; package ru.bclib.blocks;
import java.util.Collections;
import java.util.List;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.fabricmc.fabric.api.tool.attribute.v1.FabricToolTags; import net.fabricmc.fabric.api.tool.attribute.v1.FabricToolTags;
import net.minecraft.client.renderer.block.model.BlockModel; import net.minecraft.client.renderer.block.model.BlockModel;
@ -19,49 +15,63 @@ import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.MaterialColor; import net.minecraft.world.level.material.MaterialColor;
import net.minecraft.world.level.storage.loot.LootContext; import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.world.level.storage.loot.parameters.LootContextParams; import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
import ru.bclib.client.models.BlockModelProvider;
import ru.bclib.client.render.BCLRenderLayer; import ru.bclib.client.render.BCLRenderLayer;
import ru.bclib.interfaces.IRenderTyped; import ru.bclib.interfaces.BlockModelProvider;
import ru.bclib.interfaces.RenderLayerProvider;
import ru.bclib.util.MHelper; import ru.bclib.util.MHelper;
public class BaseLeavesBlock extends LeavesBlock implements BlockModelProvider, IRenderTyped { import java.util.Collections;
private final Block sapling; import java.util.List;
import java.util.function.Consumer;
public class BaseLeavesBlock extends LeavesBlock implements BlockModelProvider, RenderLayerProvider {
protected final Block sapling;
private static FabricBlockSettings makeLeaves(MaterialColor color) {
return FabricBlockSettings.copyOf(Blocks.OAK_LEAVES)
.mapColor(color)
.breakByTool(FabricToolTags.HOES)
.breakByTool(FabricToolTags.SHEARS)
.breakByHand(true)
.allowsSpawning((state, world, pos, type) -> false)
.suffocates((state, world, pos) -> false)
.blockVision((state, world, pos) -> false);
}
public BaseLeavesBlock(Block sapling, MaterialColor color, Consumer<FabricBlockSettings> customizeProperties) {
super(BaseBlock.acceptAndReturn(customizeProperties, makeLeaves(color)));
this.sapling = sapling;
}
public BaseLeavesBlock(Block sapling, MaterialColor color, int light, Consumer<FabricBlockSettings> customizeProperties) {
super(BaseBlock.acceptAndReturn(customizeProperties, makeLeaves(color).luminance(light)));
this.sapling = sapling;
}
public BaseLeavesBlock(Block sapling, MaterialColor color) { public BaseLeavesBlock(Block sapling, MaterialColor color) {
super(FabricBlockSettings.copyOf(Blocks.OAK_LEAVES) super(makeLeaves(color));
.mapColor(color)
.breakByTool(FabricToolTags.HOES)
.breakByTool(FabricToolTags.SHEARS)
.breakByHand(true)
.isValidSpawn((state, world, pos, type) -> false)
.isSuffocating((state, world, pos) -> false)
.isViewBlocking((state, world, pos) -> false));
this.sapling = sapling; this.sapling = sapling;
} }
public BaseLeavesBlock(Block sapling, MaterialColor color, int light) { public BaseLeavesBlock(Block sapling, MaterialColor color, int light) {
super(FabricBlockSettings.copyOf(Blocks.OAK_LEAVES) super(makeLeaves(color).lightLevel(light));
.mapColor(color)
.luminance(light)
.breakByTool(FabricToolTags.HOES)
.breakByTool(FabricToolTags.SHEARS)
.isValidSpawn((state, world, pos, type) -> false)
.isSuffocating((state, world, pos) -> false)
.isViewBlocking((state, world, pos) -> false));
this.sapling = sapling; this.sapling = sapling;
} }
@Override @Override
public BCLRenderLayer getRenderLayer() { public BCLRenderLayer getRenderLayer() {
return BCLRenderLayer.CUTOUT; return BCLRenderLayer.CUTOUT;
} }
@Override @Override
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) { public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) {
ItemStack tool = builder.getParameter(LootContextParams.TOOL); ItemStack tool = builder.getParameter(LootContextParams.TOOL);
if (tool != null) { if (tool != null) {
if (FabricToolTags.SHEARS.contains(tool.getItem()) || EnchantmentHelper.getItemEnchantmentLevel(Enchantments.SILK_TOUCH, tool) > 0) { if (FabricToolTags.SHEARS.contains(tool.getItem()) || EnchantmentHelper.getItemEnchantmentLevel(
Enchantments.SILK_TOUCH,
tool
) > 0) {
return Collections.singletonList(new ItemStack(this)); return Collections.singletonList(new ItemStack(this));
} }
int fortune = EnchantmentHelper.getItemEnchantmentLevel(Enchantments.BLOCK_FORTUNE, tool); int fortune = EnchantmentHelper.getItemEnchantmentLevel(Enchantments.BLOCK_FORTUNE, tool);
@ -72,7 +82,7 @@ public class BaseLeavesBlock extends LeavesBlock implements BlockModelProvider,
} }
return MHelper.RANDOM.nextInt(16) == 0 ? Lists.newArrayList(new ItemStack(sapling)) : Lists.newArrayList(); return MHelper.RANDOM.nextInt(16) == 0 ? Lists.newArrayList(new ItemStack(sapling)) : Lists.newArrayList();
} }
@Override @Override
public BlockModel getItemModel(ResourceLocation resourceLocation) { public BlockModel getItemModel(ResourceLocation resourceLocation) {
return getBlockModel(resourceLocation, defaultBlockState()); return getBlockModel(resourceLocation, defaultBlockState());

View file

@ -1,12 +1,5 @@
package ru.bclib.blocks; package ru.bclib.blocks;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.jetbrains.annotations.Nullable;
import net.fabricmc.api.EnvType; import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment; import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
@ -21,24 +14,30 @@ import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.IronBarsBlock; import net.minecraft.world.level.block.IronBarsBlock;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.storage.loot.LootContext; import net.minecraft.world.level.storage.loot.LootContext;
import org.jetbrains.annotations.Nullable;
import ru.bclib.client.models.BasePatterns; import ru.bclib.client.models.BasePatterns;
import ru.bclib.client.models.BlockModelProvider;
import ru.bclib.client.models.ModelsHelper; import ru.bclib.client.models.ModelsHelper;
import ru.bclib.client.models.PatternsHelper; import ru.bclib.client.models.PatternsHelper;
import ru.bclib.client.render.BCLRenderLayer; import ru.bclib.client.render.BCLRenderLayer;
import ru.bclib.interfaces.IRenderTyped; import ru.bclib.interfaces.BlockModelProvider;
import ru.bclib.interfaces.RenderLayerProvider;
public class BaseMetalBarsBlock extends IronBarsBlock implements BlockModelProvider, IRenderTyped { import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
public class BaseMetalBarsBlock extends IronBarsBlock implements BlockModelProvider, RenderLayerProvider {
public BaseMetalBarsBlock(Block source) { public BaseMetalBarsBlock(Block source) {
super(FabricBlockSettings.copyOf(source).strength(5.0F, 6.0F).noOcclusion()); super(FabricBlockSettings.copyOf(source).strength(5.0F, 6.0F).noOcclusion());
} }
@Override @Override
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) { public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) {
return Collections.singletonList(new ItemStack(this)); return Collections.singletonList(new ItemStack(this));
} }
public Optional<String> getModelString(String block) { public Optional<String> getModelString(String block) {
ResourceLocation blockId = Registry.BLOCK.getKey(this); ResourceLocation blockId = Registry.BLOCK.getKey(this);
if (block.contains("item")) { if (block.contains("item")) {
@ -51,13 +50,13 @@ public class BaseMetalBarsBlock extends IronBarsBlock implements BlockModelProvi
return PatternsHelper.createJson(BasePatterns.BLOCK_BARS_SIDE, blockId); return PatternsHelper.createJson(BasePatterns.BLOCK_BARS_SIDE, blockId);
} }
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public BlockModel getItemModel(ResourceLocation resourceLocation) { public BlockModel getItemModel(ResourceLocation resourceLocation) {
return ModelsHelper.createBlockItem(resourceLocation); return ModelsHelper.createBlockItem(resourceLocation);
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public @Nullable BlockModel getBlockModel(ResourceLocation blockId, BlockState blockState) { public @Nullable BlockModel getBlockModel(ResourceLocation blockId, BlockState blockState) {
@ -72,32 +71,40 @@ public class BaseMetalBarsBlock extends IronBarsBlock implements BlockModelProvi
} }
return ModelsHelper.fromPattern(pattern); return ModelsHelper.fromPattern(pattern);
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public UnbakedModel getModelVariant(ResourceLocation stateId, BlockState blockState, Map<ResourceLocation, UnbakedModel> modelCache) { public UnbakedModel getModelVariant(ResourceLocation stateId, BlockState blockState, Map<ResourceLocation, UnbakedModel> modelCache) {
ResourceLocation postId = new ResourceLocation(stateId.getNamespace(), ResourceLocation postId = new ResourceLocation(stateId.getNamespace(), "block/" + stateId.getPath() + "_post");
"block/" + stateId.getPath() + "_post"); ResourceLocation sideId = new ResourceLocation(stateId.getNamespace(), "block/" + stateId.getPath() + "_side");
ResourceLocation sideId = new ResourceLocation(stateId.getNamespace(),
"block/" + stateId.getPath() + "_side");
registerBlockModel(postId, postId, blockState, modelCache); registerBlockModel(postId, postId, blockState, modelCache);
registerBlockModel(sideId, sideId, blockState, modelCache); registerBlockModel(sideId, sideId, blockState, modelCache);
ModelsHelper.MultiPartBuilder builder = ModelsHelper.MultiPartBuilder.create(stateDefinition); ModelsHelper.MultiPartBuilder builder = ModelsHelper.MultiPartBuilder.create(stateDefinition);
builder.part(postId).setCondition(state -> builder.part(postId)
!state.getValue(NORTH) && !state.getValue(EAST) && .setCondition(state -> !state.getValue(NORTH) && !state.getValue(EAST) && !state.getValue(SOUTH) && !state
!state.getValue(SOUTH) && !state.getValue(WEST)).add(); .getValue(WEST))
.add();
builder.part(sideId).setCondition(state -> state.getValue(NORTH)).setUVLock(true).add(); builder.part(sideId).setCondition(state -> state.getValue(NORTH)).setUVLock(true).add();
builder.part(sideId).setCondition(state -> state.getValue(EAST)) builder.part(sideId)
.setTransformation(BlockModelRotation.X0_Y90.getRotation()).setUVLock(true).add(); .setCondition(state -> state.getValue(EAST))
builder.part(sideId).setCondition(state -> state.getValue(SOUTH)) .setTransformation(BlockModelRotation.X0_Y90.getRotation())
.setTransformation(BlockModelRotation.X0_Y180.getRotation()).setUVLock(true).add(); .setUVLock(true)
builder.part(sideId).setCondition(state -> state.getValue(WEST)) .add();
.setTransformation(BlockModelRotation.X0_Y270.getRotation()).setUVLock(true).add(); builder.part(sideId)
.setCondition(state -> state.getValue(SOUTH))
.setTransformation(BlockModelRotation.X0_Y180.getRotation())
.setUVLock(true)
.add();
builder.part(sideId)
.setCondition(state -> state.getValue(WEST))
.setTransformation(BlockModelRotation.X0_Y270.getRotation())
.setUVLock(true)
.add();
return builder.build(); return builder.build();
} }
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public boolean skipRendering(BlockState state, BlockState stateFrom, Direction direction) { public boolean skipRendering(BlockState state, BlockState stateFrom, Direction direction) {
if (direction.getAxis().isVertical() && stateFrom.getBlock() == this && !stateFrom.equals(state)) { if (direction.getAxis().isVertical() && stateFrom.getBlock() == this && !stateFrom.equals(state)) {
@ -105,7 +112,7 @@ public class BaseMetalBarsBlock extends IronBarsBlock implements BlockModelProvi
} }
return super.skipRendering(state, stateFrom, direction); return super.skipRendering(state, stateFrom, direction);
} }
@Override @Override
public BCLRenderLayer getRenderLayer() { public BCLRenderLayer getRenderLayer() {
return BCLRenderLayer.CUTOUT; return BCLRenderLayer.CUTOUT;

View file

@ -1,9 +1,5 @@
package ru.bclib.blocks; package ru.bclib.blocks;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.minecraft.client.renderer.block.model.BlockModel; import net.minecraft.client.renderer.block.model.BlockModel;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
@ -13,6 +9,7 @@ import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.enchantment.EnchantmentHelper; import net.minecraft.world.item.enchantment.EnchantmentHelper;
import net.minecraft.world.item.enchantment.Enchantments; import net.minecraft.world.item.enchantment.Enchantments;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.level.block.OreBlock; import net.minecraft.world.level.block.OreBlock;
import net.minecraft.world.level.block.SoundType; import net.minecraft.world.level.block.SoundType;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
@ -20,32 +17,44 @@ import net.minecraft.world.level.material.Material;
import net.minecraft.world.level.material.MaterialColor; import net.minecraft.world.level.material.MaterialColor;
import net.minecraft.world.level.storage.loot.LootContext; import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.world.level.storage.loot.parameters.LootContextParams; import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
import ru.bclib.client.models.BlockModelProvider; import ru.bclib.interfaces.BlockModelProvider;
import ru.bclib.util.MHelper; import ru.bclib.util.MHelper;
import java.util.Collections;
import java.util.List;
public class BaseOreBlock extends OreBlock implements BlockModelProvider { public class BaseOreBlock extends OreBlock implements BlockModelProvider {
private final Item dropItem; private final Item dropItem;
private final int minCount; private final int minCount;
private final int maxCount; private final int maxCount;
public BaseOreBlock(Item drop, int minCount, int maxCount, int experience) { public BaseOreBlock(Item drop, int minCount, int maxCount, int experience) {
super(FabricBlockSettings.of(Material.STONE, MaterialColor.SAND) this(drop, minCount, maxCount, experience, FabricBlockSettings.of(Material.STONE, MaterialColor.SAND)
.hardness(3F) .hardness(3F)
.resistance(9F) .resistance(9F)
.requiresCorrectToolForDrops() .requiresCorrectToolForDrops()
.sound(SoundType.STONE), UniformInt.of(1, experience)); .sound(SoundType.STONE));
}
public BaseOreBlock(Item drop, int minCount, int maxCount, int experience, Properties properties) {
super(properties, UniformInt.of(experience>0?1:0, experience));
this.dropItem = drop; this.dropItem = drop;
this.minCount = minCount; this.minCount = minCount;
this.maxCount = maxCount; this.maxCount = maxCount;
} }
@Override @Override
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) { public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) {
return getDroppedItems(this, dropItem, maxCount, minCount, state, builder);
}
public static List<ItemStack> getDroppedItems(ItemLike block, Item dropItem, int maxCount, int minCount, BlockState state, LootContext.Builder builder) {
ItemStack tool = builder.getParameter(LootContextParams.TOOL); ItemStack tool = builder.getParameter(LootContextParams.TOOL);
if (tool != null && tool.isCorrectToolForDrops(state)) { if (tool != null && tool.isCorrectToolForDrops(state)) {
if (EnchantmentHelper.getItemEnchantmentLevel(Enchantments.SILK_TOUCH, tool) > 0) { if (EnchantmentHelper.getItemEnchantmentLevel(Enchantments.SILK_TOUCH, tool) > 0) {
return Collections.singletonList(new ItemStack(this)); return Collections.singletonList(new ItemStack(block));
} }
int count; int count;
int enchantment = EnchantmentHelper.getItemEnchantmentLevel(Enchantments.BLOCK_FORTUNE, tool); int enchantment = EnchantmentHelper.getItemEnchantmentLevel(Enchantments.BLOCK_FORTUNE, tool);
@ -56,14 +65,15 @@ public class BaseOreBlock extends OreBlock implements BlockModelProvider {
return Collections.singletonList(new ItemStack(dropItem, max)); return Collections.singletonList(new ItemStack(dropItem, max));
} }
count = MHelper.randRange(min, max, MHelper.RANDOM); count = MHelper.randRange(min, max, MHelper.RANDOM);
} else { }
else {
count = MHelper.randRange(minCount, maxCount, MHelper.RANDOM); count = MHelper.randRange(minCount, maxCount, MHelper.RANDOM);
} }
return Collections.singletonList(new ItemStack(dropItem, count)); return Collections.singletonList(new ItemStack(dropItem, count));
} }
return Collections.emptyList(); return Collections.emptyList();
} }
@Override @Override
public BlockModel getItemModel(ResourceLocation resourceLocation) { public BlockModel getItemModel(ResourceLocation resourceLocation) {
return getBlockModel(resourceLocation, defaultBlockState()); return getBlockModel(resourceLocation, defaultBlockState());

View file

@ -1,14 +1,6 @@
package ru.bclib.blocks; package ru.bclib.blocks;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.jetbrains.annotations.Nullable;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import net.fabricmc.api.EnvType; import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment; import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
@ -28,16 +20,22 @@ import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.world.level.storage.loot.parameters.LootContextParams; import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
import net.minecraft.world.phys.shapes.CollisionContext; import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape; import net.minecraft.world.phys.shapes.VoxelShape;
import org.jetbrains.annotations.Nullable;
import ru.bclib.client.models.BasePatterns; import ru.bclib.client.models.BasePatterns;
import ru.bclib.client.models.ModelsHelper; import ru.bclib.client.models.ModelsHelper;
import ru.bclib.client.models.PatternsHelper; import ru.bclib.client.models.PatternsHelper;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public class BasePathBlock extends BaseBlockNotFull { public class BasePathBlock extends BaseBlockNotFull {
private static final VoxelShape SHAPE = Block.box(0, 0, 0, 16, 15, 16); private static final VoxelShape SHAPE = Block.box(0, 0, 0, 16, 15, 16);
private Block baseBlock; private Block baseBlock;
public BasePathBlock(Block source) { public BasePathBlock(Block source) {
super(FabricBlockSettings.copyOf(source).isValidSpawn((state, world, pos, type) -> false)); super(FabricBlockSettings.copyOf(source).isValidSpawn((state, world, pos, type) -> false));
this.baseBlock = Blocks.DIRT; this.baseBlock = Blocks.DIRT;
@ -72,7 +70,7 @@ public class BasePathBlock extends BaseBlockNotFull {
public BlockModel getItemModel(ResourceLocation blockId) { public BlockModel getItemModel(ResourceLocation blockId) {
return getBlockModel(blockId, defaultBlockState()); return getBlockModel(blockId, defaultBlockState());
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public @Nullable BlockModel getBlockModel(ResourceLocation blockId, BlockState blockState) { public @Nullable BlockModel getBlockModel(ResourceLocation blockId, BlockState blockState) {
@ -87,7 +85,7 @@ public class BasePathBlock extends BaseBlockNotFull {
Optional<String> pattern = PatternsHelper.createJson(BasePatterns.BLOCK_PATH, textures); Optional<String> pattern = PatternsHelper.createJson(BasePatterns.BLOCK_PATH, textures);
return ModelsHelper.fromPattern(pattern); return ModelsHelper.fromPattern(pattern);
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public UnbakedModel getModelVariant(ResourceLocation stateId, BlockState blockState, Map<ResourceLocation, UnbakedModel> modelCache) { public UnbakedModel getModelVariant(ResourceLocation stateId, BlockState blockState, Map<ResourceLocation, UnbakedModel> modelCache) {

View file

@ -1,14 +1,14 @@
package ru.bclib.blocks; package ru.bclib.blocks;
import java.util.List;
import java.util.Random;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.fabricmc.fabric.api.tool.attribute.v1.FabricToolTags; import net.fabricmc.fabric.api.tool.attribute.v1.FabricToolTags;
import net.minecraft.client.renderer.block.model.BlockModel;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction; import net.minecraft.core.Direction;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.item.ItemEntity; import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
@ -30,11 +30,19 @@ import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
import net.minecraft.world.phys.Vec3; import net.minecraft.world.phys.Vec3;
import net.minecraft.world.phys.shapes.CollisionContext; import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape; import net.minecraft.world.phys.shapes.VoxelShape;
import org.jetbrains.annotations.Nullable;
import ru.bclib.client.models.BasePatterns;
import ru.bclib.client.models.ModelsHelper;
import ru.bclib.client.models.PatternsHelper;
import ru.bclib.client.render.BCLRenderLayer; import ru.bclib.client.render.BCLRenderLayer;
import ru.bclib.interfaces.IRenderTyped; import ru.bclib.interfaces.RenderLayerProvider;
import java.util.List;
import java.util.Optional;
import java.util.Random;
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public abstract class BasePlantBlock extends BaseBlockNotFull implements IRenderTyped, BonemealableBlock { public abstract class BasePlantBlock extends BaseBlockNotFull implements RenderLayerProvider, BonemealableBlock {
private static final VoxelShape SHAPE = Block.box(4, 0, 4, 12, 14, 12); private static final VoxelShape SHAPE = Block.box(4, 0, 4, 12, 14, 12);
public BasePlantBlock() { public BasePlantBlock() {
@ -47,44 +55,44 @@ public abstract class BasePlantBlock extends BaseBlockNotFull implements IRender
public BasePlantBlock(boolean replaceable) { public BasePlantBlock(boolean replaceable) {
super(FabricBlockSettings.of(replaceable ? Material.REPLACEABLE_PLANT : Material.PLANT) super(FabricBlockSettings.of(replaceable ? Material.REPLACEABLE_PLANT : Material.PLANT)
.breakByTool(FabricToolTags.SHEARS) .breakByTool(FabricToolTags.SHEARS)
.breakByHand(true) .breakByHand(true)
.sound(SoundType.GRASS) .sound(SoundType.GRASS)
.noCollission()); .noCollission());
} }
public BasePlantBlock(boolean replaceable, int light) { public BasePlantBlock(boolean replaceable, int light) {
super(FabricBlockSettings.of(replaceable ? Material.REPLACEABLE_PLANT : Material.PLANT) super(FabricBlockSettings.of(replaceable ? Material.REPLACEABLE_PLANT : Material.PLANT)
.breakByTool(FabricToolTags.SHEARS) .breakByTool(FabricToolTags.SHEARS)
.breakByHand(true) .breakByHand(true)
.luminance(light) .luminance(light)
.sound(SoundType.GRASS) .sound(SoundType.GRASS)
.noCollission()); .noCollission());
} }
public BasePlantBlock(Properties settings) { public BasePlantBlock(Properties settings) {
super(settings); super(settings);
} }
protected abstract boolean isTerrain(BlockState state); protected abstract boolean isTerrain(BlockState state);
@Override @Override
public VoxelShape getShape(BlockState state, BlockGetter view, BlockPos pos, CollisionContext ePos) { public VoxelShape getShape(BlockState state, BlockGetter view, BlockPos pos, CollisionContext ePos) {
Vec3 vec3d = state.getOffset(view, pos); Vec3 vec3d = state.getOffset(view, pos);
return SHAPE.move(vec3d.x, vec3d.y, vec3d.z); return SHAPE.move(vec3d.x, vec3d.y, vec3d.z);
} }
@Override @Override
public BlockBehaviour.OffsetType getOffsetType() { public BlockBehaviour.OffsetType getOffsetType() {
return BlockBehaviour.OffsetType.XZ; return BlockBehaviour.OffsetType.XZ;
} }
@Override @Override
public boolean canSurvive(BlockState state, LevelReader world, BlockPos pos) { public boolean canSurvive(BlockState state, LevelReader world, BlockPos pos) {
BlockState down = world.getBlockState(pos.below()); BlockState down = world.getBlockState(pos.below());
return isTerrain(down); return isTerrain(down);
} }
@Override @Override
public BlockState updateShape(BlockState state, Direction facing, BlockState neighborState, LevelAccessor world, BlockPos pos, BlockPos neighborPos) { public BlockState updateShape(BlockState state, Direction facing, BlockState neighborState, LevelAccessor world, BlockPos pos, BlockPos neighborPos) {
if (!canSurvive(state, world, pos)) { if (!canSurvive(state, world, pos)) {
@ -98,7 +106,10 @@ public abstract class BasePlantBlock extends BaseBlockNotFull implements IRender
@Override @Override
public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) { public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) {
ItemStack tool = builder.getParameter(LootContextParams.TOOL); ItemStack tool = builder.getParameter(LootContextParams.TOOL);
if (tool != null && FabricToolTags.SHEARS.contains(tool.getItem()) || EnchantmentHelper.getItemEnchantmentLevel(Enchantments.SILK_TOUCH, tool) > 0) { if (tool != null && FabricToolTags.SHEARS.contains(tool.getItem()) || EnchantmentHelper.getItemEnchantmentLevel(
Enchantments.SILK_TOUCH,
tool
) > 0) {
return Lists.newArrayList(new ItemStack(this)); return Lists.newArrayList(new ItemStack(this));
} }
else { else {
@ -110,20 +121,40 @@ public abstract class BasePlantBlock extends BaseBlockNotFull implements IRender
public BCLRenderLayer getRenderLayer() { public BCLRenderLayer getRenderLayer() {
return BCLRenderLayer.CUTOUT; return BCLRenderLayer.CUTOUT;
} }
@Override @Override
public boolean isValidBonemealTarget(BlockGetter world, BlockPos pos, BlockState state, boolean isClient) { public boolean isValidBonemealTarget(BlockGetter world, BlockPos pos, BlockState state, boolean isClient) {
return true; return true;
} }
@Override @Override
public boolean isBonemealSuccess(Level world, Random random, BlockPos pos, BlockState state) { public boolean isBonemealSuccess(Level world, Random random, BlockPos pos, BlockState state) {
return true; return true;
} }
@Override @Override
public void performBonemeal(ServerLevel world, Random random, BlockPos pos, BlockState state) { public void performBonemeal(ServerLevel world, Random random, BlockPos pos, BlockState state) {
ItemEntity item = new ItemEntity(world, pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, new ItemStack(this)); ItemEntity item = new ItemEntity(
world,
pos.getX() + 0.5,
pos.getY() + 0.5,
pos.getZ() + 0.5,
new ItemStack(this)
);
world.addFreshEntity(item); world.addFreshEntity(item);
} }
@Override
@Environment(EnvType.CLIENT)
public BlockModel getItemModel(ResourceLocation resourceLocation) {
return ModelsHelper.createBlockItem(resourceLocation);
}
@Override
@Nullable
@Environment(EnvType.CLIENT)
public BlockModel getBlockModel(ResourceLocation resourceLocation, BlockState blockState) {
Optional<String> pattern = PatternsHelper.createJson(BasePatterns.BLOCK_CROSS, resourceLocation);
return ModelsHelper.fromPattern(pattern);
}
} }

View file

@ -1,7 +1,5 @@
package ru.bclib.blocks; package ru.bclib.blocks;
import java.util.Random;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.fabricmc.fabric.api.tool.attribute.v1.FabricToolTags; import net.fabricmc.fabric.api.tool.attribute.v1.FabricToolTags;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
@ -15,16 +13,18 @@ import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.IntegerProperty; import net.minecraft.world.level.block.state.properties.IntegerProperty;
import net.minecraft.world.level.material.Material; import net.minecraft.world.level.material.Material;
import java.util.Random;
public abstract class BasePlantWithAgeBlock extends BasePlantBlock { public abstract class BasePlantWithAgeBlock extends BasePlantBlock {
public static final IntegerProperty AGE = BlockProperties.AGE; public static final IntegerProperty AGE = BlockProperties.AGE;
public BasePlantWithAgeBlock() { public BasePlantWithAgeBlock() {
this(FabricBlockSettings.of(Material.PLANT) this(FabricBlockSettings.of(Material.PLANT)
.breakByTool(FabricToolTags.SHEARS) .breakByTool(FabricToolTags.SHEARS)
.breakByHand(true) .breakByHand(true)
.sound(SoundType.GRASS) .sound(SoundType.GRASS)
.randomTicks() .randomTicks()
.noCollission()); .noCollission());
} }
public BasePlantWithAgeBlock(Properties settings) { public BasePlantWithAgeBlock(Properties settings) {
@ -53,7 +53,7 @@ public abstract class BasePlantWithAgeBlock extends BasePlantBlock {
public boolean isBonemealSuccess(Level world, Random random, BlockPos pos, BlockState state) { public boolean isBonemealSuccess(Level world, Random random, BlockPos pos, BlockState state) {
return true; return true;
} }
@Override @Override
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public void tick(BlockState state, ServerLevel world, BlockPos pos, Random random) { public void tick(BlockState state, ServerLevel world, BlockPos pos, Random random) {

View file

@ -1,12 +1,5 @@
package ru.bclib.blocks; package ru.bclib.blocks;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.jetbrains.annotations.Nullable;
import net.fabricmc.api.EnvType; import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment; import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
@ -19,10 +12,16 @@ import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.PressurePlateBlock; import net.minecraft.world.level.block.PressurePlateBlock;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.storage.loot.LootContext; import net.minecraft.world.level.storage.loot.LootContext;
import org.jetbrains.annotations.Nullable;
import ru.bclib.client.models.BasePatterns; import ru.bclib.client.models.BasePatterns;
import ru.bclib.client.models.BlockModelProvider;
import ru.bclib.client.models.ModelsHelper; import ru.bclib.client.models.ModelsHelper;
import ru.bclib.client.models.PatternsHelper; import ru.bclib.client.models.PatternsHelper;
import ru.bclib.interfaces.BlockModelProvider;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
public class BasePressurePlateBlock extends PressurePlateBlock implements BlockModelProvider { public class BasePressurePlateBlock extends PressurePlateBlock implements BlockModelProvider {
private final Block parent; private final Block parent;
@ -31,19 +30,19 @@ public class BasePressurePlateBlock extends PressurePlateBlock implements BlockM
super(rule, FabricBlockSettings.copyOf(source).noCollission().noOcclusion().strength(0.5F)); super(rule, FabricBlockSettings.copyOf(source).noCollission().noOcclusion().strength(0.5F));
this.parent = source; this.parent = source;
} }
@Override @Override
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) { public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) {
return Collections.singletonList(new ItemStack(this)); return Collections.singletonList(new ItemStack(this));
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public BlockModel getItemModel(ResourceLocation resourceLocation) { public BlockModel getItemModel(ResourceLocation resourceLocation) {
return getBlockModel(resourceLocation, defaultBlockState()); return getBlockModel(resourceLocation, defaultBlockState());
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public @Nullable BlockModel getBlockModel(ResourceLocation resourceLocation, BlockState blockState) { public @Nullable BlockModel getBlockModel(ResourceLocation resourceLocation, BlockState blockState) {
@ -51,18 +50,18 @@ public class BasePressurePlateBlock extends PressurePlateBlock implements BlockM
Optional<String> pattern; Optional<String> pattern;
if (blockState.getValue(POWERED)) { if (blockState.getValue(POWERED)) {
pattern = PatternsHelper.createJson(BasePatterns.BLOCK_PLATE_DOWN, parentId); pattern = PatternsHelper.createJson(BasePatterns.BLOCK_PLATE_DOWN, parentId);
} else { }
else {
pattern = PatternsHelper.createJson(BasePatterns.BLOCK_PLATE_UP, parentId); pattern = PatternsHelper.createJson(BasePatterns.BLOCK_PLATE_UP, parentId);
} }
return ModelsHelper.fromPattern(pattern); return ModelsHelper.fromPattern(pattern);
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public UnbakedModel getModelVariant(ResourceLocation stateId, BlockState blockState, Map<ResourceLocation, UnbakedModel> modelCache) { public UnbakedModel getModelVariant(ResourceLocation stateId, BlockState blockState, Map<ResourceLocation, UnbakedModel> modelCache) {
String state = blockState.getValue(POWERED) ? "_down" : "_up"; String state = blockState.getValue(POWERED) ? "_down" : "_up";
ResourceLocation modelId = new ResourceLocation(stateId.getNamespace(), ResourceLocation modelId = new ResourceLocation(stateId.getNamespace(), "block/" + stateId.getPath() + state);
"block/" + stateId.getPath() + state);
registerBlockModel(stateId, modelId, blockState, modelCache); registerBlockModel(stateId, modelId, blockState, modelCache);
return ModelsHelper.createBlockSimple(modelId); return ModelsHelper.createBlockSimple(modelId);
} }

View file

@ -1,12 +1,5 @@
package ru.bclib.blocks; package ru.bclib.blocks;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.jetbrains.annotations.Nullable;
import net.fabricmc.api.EnvType; import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment; import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
@ -18,9 +11,15 @@ import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.RotatedPillarBlock; import net.minecraft.world.level.block.RotatedPillarBlock;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.storage.loot.LootContext; import net.minecraft.world.level.storage.loot.LootContext;
import ru.bclib.client.models.BlockModelProvider; import org.jetbrains.annotations.Nullable;
import ru.bclib.client.models.ModelsHelper; import ru.bclib.client.models.ModelsHelper;
import ru.bclib.client.models.PatternsHelper; import ru.bclib.client.models.PatternsHelper;
import ru.bclib.interfaces.BlockModelProvider;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
public class BaseRotatedPillarBlock extends RotatedPillarBlock implements BlockModelProvider { public class BaseRotatedPillarBlock extends RotatedPillarBlock implements BlockModelProvider {
public BaseRotatedPillarBlock(Properties settings) { public BaseRotatedPillarBlock(Properties settings) {
@ -30,26 +29,26 @@ public class BaseRotatedPillarBlock extends RotatedPillarBlock implements BlockM
public BaseRotatedPillarBlock(Block block) { public BaseRotatedPillarBlock(Block block) {
super(FabricBlockSettings.copyOf(block)); super(FabricBlockSettings.copyOf(block));
} }
@Override @Override
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) { public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) {
return Collections.singletonList(new ItemStack(this)); return Collections.singletonList(new ItemStack(this));
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public BlockModel getItemModel(ResourceLocation blockId) { public BlockModel getItemModel(ResourceLocation blockId) {
return getBlockModel(blockId, defaultBlockState()); return getBlockModel(blockId, defaultBlockState());
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public @Nullable BlockModel getBlockModel(ResourceLocation blockId, BlockState blockState) { public @Nullable BlockModel getBlockModel(ResourceLocation blockId, BlockState blockState) {
Optional<String> pattern = createBlockPattern(blockId); Optional<String> pattern = createBlockPattern(blockId);
return ModelsHelper.fromPattern(pattern); return ModelsHelper.fromPattern(pattern);
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public UnbakedModel getModelVariant(ResourceLocation stateId, BlockState blockState, Map<ResourceLocation, UnbakedModel> modelCache) { public UnbakedModel getModelVariant(ResourceLocation stateId, BlockState blockState, Map<ResourceLocation, UnbakedModel> modelCache) {
@ -57,7 +56,7 @@ public class BaseRotatedPillarBlock extends RotatedPillarBlock implements BlockM
registerBlockModel(stateId, modelId, blockState, modelCache); registerBlockModel(stateId, modelId, blockState, modelCache);
return ModelsHelper.createRotatedModel(modelId, blockState.getValue(AXIS)); return ModelsHelper.createRotatedModel(modelId, blockState.getValue(AXIS));
} }
protected Optional<String> createBlockPattern(ResourceLocation blockId) { protected Optional<String> createBlockPattern(ResourceLocation blockId) {
return PatternsHelper.createBlockPillar(blockId); return PatternsHelper.createBlockPillar(blockId);
} }

View file

@ -1,12 +1,8 @@
package ru.bclib.blocks; package ru.bclib.blocks;
import java.util.Collections;
import java.util.List;
import org.jetbrains.annotations.Nullable;
import net.fabricmc.api.EnvType; import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment; import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.item.v1.FabricItemSettings;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.minecraft.client.renderer.block.model.BlockModel; import net.minecraft.client.renderer.block.model.BlockModel;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
@ -18,6 +14,7 @@ import net.minecraft.server.level.ServerPlayer;
import net.minecraft.util.Mth; import net.minecraft.util.Mth;
import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.BlockPlaceContext; import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter; import net.minecraft.world.level.BlockGetter;
@ -42,14 +39,18 @@ import net.minecraft.world.level.material.Fluids;
import net.minecraft.world.level.storage.loot.LootContext; import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.world.phys.shapes.CollisionContext; import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape; import net.minecraft.world.phys.shapes.VoxelShape;
import org.jetbrains.annotations.Nullable;
import ru.bclib.blockentities.BaseSignBlockEntity; import ru.bclib.blockentities.BaseSignBlockEntity;
import ru.bclib.client.models.BlockModelProvider;
import ru.bclib.client.models.ModelsHelper; import ru.bclib.client.models.ModelsHelper;
import ru.bclib.interfaces.ISpetialItem; import ru.bclib.interfaces.BlockModelProvider;
import ru.bclib.interfaces.CustomItemProvider;
import ru.bclib.util.BlocksHelper; import ru.bclib.util.BlocksHelper;
import java.util.Collections;
import java.util.List;
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public class BaseSignBlock extends SignBlock implements BlockModelProvider, ISpetialItem { public class BaseSignBlock extends SignBlock implements BlockModelProvider, CustomItemProvider {
public static final IntegerProperty ROTATION = BlockStateProperties.ROTATION_16; public static final IntegerProperty ROTATION = BlockStateProperties.ROTATION_16;
public static final BooleanProperty FLOOR = BooleanProperty.create("floor"); public static final BooleanProperty FLOOR = BooleanProperty.create("floor");
private static final VoxelShape[] WALL_SHAPES = new VoxelShape[] { private static final VoxelShape[] WALL_SHAPES = new VoxelShape[] {
@ -58,25 +59,28 @@ public class BaseSignBlock extends SignBlock implements BlockModelProvider, ISpe
Block.box(0.0D, 4.5D, 0.0D, 16.0D, 12.5D, 2.0D), Block.box(0.0D, 4.5D, 0.0D, 16.0D, 12.5D, 2.0D),
Block.box(14.0D, 4.5D, 0.0D, 16.0D, 12.5D, 16.0D) Block.box(14.0D, 4.5D, 0.0D, 16.0D, 12.5D, 16.0D)
}; };
private final Block parent; private final Block parent;
public BaseSignBlock(Block source) { public BaseSignBlock(Block source) {
super(FabricBlockSettings.copyOf(source).strength(1.0F, 1.0F).noCollission().noOcclusion(), WoodType.OAK); super(FabricBlockSettings.copyOf(source).strength(1.0F, 1.0F).noCollission().noOcclusion(), WoodType.OAK);
this.registerDefaultState(this.stateDefinition.any().setValue(ROTATION, 0).setValue(FLOOR, false).setValue(WATERLOGGED, false)); this.registerDefaultState(this.stateDefinition.any()
.setValue(ROTATION, 0)
.setValue(FLOOR, false)
.setValue(WATERLOGGED, false));
this.parent = source; this.parent = source;
} }
@Override @Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) { protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
builder.add(ROTATION, FLOOR, WATERLOGGED); builder.add(ROTATION, FLOOR, WATERLOGGED);
} }
@Override @Override
public VoxelShape getShape(BlockState state, BlockGetter view, BlockPos pos, CollisionContext ePos) { public VoxelShape getShape(BlockState state, BlockGetter view, BlockPos pos, CollisionContext ePos) {
return state.getValue(FLOOR) ? SHAPE : WALL_SHAPES[state.getValue(ROTATION) >> 2]; return state.getValue(FLOOR) ? SHAPE : WALL_SHAPES[state.getValue(ROTATION) >> 2];
} }
@Override @Override
public BlockEntity newBlockEntity(BlockPos blockPos, BlockState blockState) { public BlockEntity newBlockEntity(BlockPos blockPos, BlockState blockState) {
return new BaseSignBlockEntity(blockPos, blockState); return new BaseSignBlockEntity(blockPos, blockState);
@ -90,24 +94,26 @@ public class BaseSignBlock extends SignBlock implements BlockModelProvider, ISpe
if (!world.isClientSide) { if (!world.isClientSide) {
sign.setAllowedPlayerEditor(placer.getUUID()); sign.setAllowedPlayerEditor(placer.getUUID());
((ServerPlayer) placer).connection.send(new ClientboundOpenSignEditorPacket(pos)); ((ServerPlayer) placer).connection.send(new ClientboundOpenSignEditorPacket(pos));
} else { }
else {
sign.setEditable(true); sign.setEditable(true);
} }
} }
} }
} }
@Override @Override
public BlockState updateShape(BlockState state, Direction facing, BlockState neighborState, LevelAccessor world, BlockPos pos, BlockPos neighborPos) { public BlockState updateShape(BlockState state, Direction facing, BlockState neighborState, LevelAccessor world, BlockPos pos, BlockPos neighborPos) {
if (state.getValue(WATERLOGGED)) { if (state.getValue(WATERLOGGED)) {
world.getLiquidTicks().scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(world)); world.getLiquidTicks().scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(world));
} }
if (!canSurvive(state, world, pos)) { if (!canSurvive(state, world, pos)) {
return state.getValue(WATERLOGGED) ? state.getFluidState().createLegacyBlock() : Blocks.AIR.defaultBlockState(); return state.getValue(WATERLOGGED) ? state.getFluidState()
.createLegacyBlock() : Blocks.AIR.defaultBlockState();
} }
return super.updateShape(state, facing, neighborState, world, pos, neighborPos); return super.updateShape(state, facing, neighborState, world, pos, neighborPos);
} }
@Override @Override
public boolean canSurvive(BlockState state, LevelReader world, BlockPos pos) { public boolean canSurvive(BlockState state, LevelReader world, BlockPos pos) {
if (!state.getValue(FLOOR)) { if (!state.getValue(FLOOR)) {
@ -118,48 +124,51 @@ public class BaseSignBlock extends SignBlock implements BlockModelProvider, ISpe
return world.getBlockState(pos.below()).getMaterial().isSolid(); return world.getBlockState(pos.below()).getMaterial().isSolid();
} }
} }
@Override @Override
public BlockState getStateForPlacement(BlockPlaceContext ctx) { public BlockState getStateForPlacement(BlockPlaceContext ctx) {
if (ctx.getClickedFace() == Direction.UP) { if (ctx.getClickedFace() == Direction.UP) {
FluidState fluidState = ctx.getLevel().getFluidState(ctx.getClickedPos()); FluidState fluidState = ctx.getLevel().getFluidState(ctx.getClickedPos());
return this.defaultBlockState().setValue(FLOOR, true) return this.defaultBlockState()
.setValue(ROTATION, Mth.floor((180.0 + ctx.getRotation() * 16.0 / 360.0) + 0.5 - 12) & 15) .setValue(FLOOR, true)
.setValue(WATERLOGGED, fluidState.getType() == Fluids.WATER); .setValue(ROTATION, Mth.floor((180.0 + ctx.getRotation() * 16.0 / 360.0) + 0.5 - 12) & 15)
} else if (ctx.getClickedFace() != Direction.DOWN) { .setValue(WATERLOGGED, fluidState.getType() == Fluids.WATER);
}
else if (ctx.getClickedFace() != Direction.DOWN) {
BlockState blockState = this.defaultBlockState(); BlockState blockState = this.defaultBlockState();
FluidState fluidState = ctx.getLevel().getFluidState(ctx.getClickedPos()); FluidState fluidState = ctx.getLevel().getFluidState(ctx.getClickedPos());
LevelReader worldView = ctx.getLevel(); LevelReader worldView = ctx.getLevel();
BlockPos blockPos = ctx.getClickedPos(); BlockPos blockPos = ctx.getClickedPos();
Direction[] directions = ctx.getNearestLookingDirections(); Direction[] directions = ctx.getNearestLookingDirections();
for (Direction direction : directions) { for (Direction direction : directions) {
if (direction.getAxis().isHorizontal()) { if (direction.getAxis().isHorizontal()) {
Direction dir = direction.getOpposite(); Direction dir = direction.getOpposite();
int rot = Mth.floor((180.0 + dir.toYRot() * 16.0 / 360.0) + 0.5 + 4) & 15; int rot = Mth.floor((180.0 + dir.toYRot() * 16.0 / 360.0) + 0.5 + 4) & 15;
blockState = blockState.setValue(ROTATION, rot); blockState = blockState.setValue(ROTATION, rot);
if (blockState.canSurvive(worldView, blockPos)) { if (blockState.canSurvive(worldView, blockPos)) {
return blockState.setValue(FLOOR, false).setValue(WATERLOGGED, fluidState.getType() == Fluids.WATER); return blockState.setValue(FLOOR, false)
.setValue(WATERLOGGED, fluidState.getType() == Fluids.WATER);
} }
} }
} }
} }
return null; return null;
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public @Nullable BlockModel getBlockModel(ResourceLocation resourceLocation, BlockState blockState) { public @Nullable BlockModel getBlockModel(ResourceLocation resourceLocation, BlockState blockState) {
ResourceLocation parentId = Registry.BLOCK.getKey(parent); ResourceLocation parentId = Registry.BLOCK.getKey(parent);
return ModelsHelper.createBlockEmpty(parentId); return ModelsHelper.createBlockEmpty(parentId);
} }
@Override @Override
public BlockState rotate(BlockState state, Rotation rotation) { public BlockState rotate(BlockState state, Rotation rotation) {
return state.setValue(ROTATION, rotation.rotate((Integer) state.getValue(ROTATION), 16)); return state.setValue(ROTATION, rotation.rotate((Integer) state.getValue(ROTATION), 16));
} }
@Override @Override
public BlockState mirror(BlockState state, Mirror mirror) { public BlockState mirror(BlockState state, Mirror mirror) {
return state.setValue(ROTATION, mirror.mirror((Integer) state.getValue(ROTATION), 16)); return state.setValue(ROTATION, mirror.mirror((Integer) state.getValue(ROTATION), 16));
@ -169,26 +178,21 @@ public class BaseSignBlock extends SignBlock implements BlockModelProvider, ISpe
public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) { public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) {
return Collections.singletonList(new ItemStack(this)); return Collections.singletonList(new ItemStack(this));
} }
@Override @Override
public boolean canPlaceLiquid(BlockGetter world, BlockPos pos, BlockState state, Fluid fluid) { public boolean canPlaceLiquid(BlockGetter world, BlockPos pos, BlockState state, Fluid fluid) {
// TODO Auto-generated method stub // TODO Auto-generated method stub
return super.canPlaceLiquid(world, pos, state, fluid); return super.canPlaceLiquid(world, pos, state, fluid);
} }
@Override @Override
public boolean placeLiquid(LevelAccessor world, BlockPos pos, BlockState state, FluidState fluidState) { public boolean placeLiquid(LevelAccessor world, BlockPos pos, BlockState state, FluidState fluidState) {
// TODO Auto-generated method stub // TODO Auto-generated method stub
return super.placeLiquid(world, pos, state, fluidState); return super.placeLiquid(world, pos, state, fluidState);
} }
@Override @Override
public int getStackSize() { public BlockItem getCustomItem(ResourceLocation blockID, FabricItemSettings settings) {
return 16; return new BlockItem(this, settings.maxCount(16));
}
@Override
public boolean canPlaceOnWater() {
return false;
} }
} }

View file

@ -1,12 +1,5 @@
package ru.bclib.blocks; package ru.bclib.blocks;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.jetbrains.annotations.Nullable;
import net.fabricmc.api.EnvType; import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment; import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
@ -21,10 +14,16 @@ import net.minecraft.world.level.block.SlabBlock;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.SlabType; import net.minecraft.world.level.block.state.properties.SlabType;
import net.minecraft.world.level.storage.loot.LootContext; import net.minecraft.world.level.storage.loot.LootContext;
import org.jetbrains.annotations.Nullable;
import ru.bclib.client.models.BasePatterns; import ru.bclib.client.models.BasePatterns;
import ru.bclib.client.models.BlockModelProvider;
import ru.bclib.client.models.ModelsHelper; import ru.bclib.client.models.ModelsHelper;
import ru.bclib.client.models.PatternsHelper; import ru.bclib.client.models.PatternsHelper;
import ru.bclib.interfaces.BlockModelProvider;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
public class BaseSlabBlock extends SlabBlock implements BlockModelProvider { public class BaseSlabBlock extends SlabBlock implements BlockModelProvider {
private final Block parent; private final Block parent;
@ -33,19 +32,19 @@ public class BaseSlabBlock extends SlabBlock implements BlockModelProvider {
super(FabricBlockSettings.copyOf(source)); super(FabricBlockSettings.copyOf(source));
this.parent = source; this.parent = source;
} }
@Override @Override
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) { public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) {
return Collections.singletonList(new ItemStack(this)); return Collections.singletonList(new ItemStack(this));
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public BlockModel getItemModel(ResourceLocation resourceLocation) { public BlockModel getItemModel(ResourceLocation resourceLocation) {
return getBlockModel(resourceLocation, defaultBlockState()); return getBlockModel(resourceLocation, defaultBlockState());
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public @Nullable BlockModel getBlockModel(ResourceLocation blockId, BlockState blockState) { public @Nullable BlockModel getBlockModel(ResourceLocation blockId, BlockState blockState) {
@ -53,18 +52,21 @@ public class BaseSlabBlock extends SlabBlock implements BlockModelProvider {
Optional<String> pattern; Optional<String> pattern;
if (blockState.getValue(TYPE) == SlabType.DOUBLE) { if (blockState.getValue(TYPE) == SlabType.DOUBLE) {
pattern = PatternsHelper.createBlockSimple(parentId); pattern = PatternsHelper.createBlockSimple(parentId);
} else { }
else {
pattern = PatternsHelper.createJson(BasePatterns.BLOCK_SLAB, parentId); pattern = PatternsHelper.createJson(BasePatterns.BLOCK_SLAB, parentId);
} }
return ModelsHelper.fromPattern(pattern); return ModelsHelper.fromPattern(pattern);
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public UnbakedModel getModelVariant(ResourceLocation stateId, BlockState blockState, Map<ResourceLocation, UnbakedModel> modelCache) { public UnbakedModel getModelVariant(ResourceLocation stateId, BlockState blockState, Map<ResourceLocation, UnbakedModel> modelCache) {
SlabType type = blockState.getValue(TYPE); SlabType type = blockState.getValue(TYPE);
ResourceLocation modelId = new ResourceLocation(stateId.getNamespace(), ResourceLocation modelId = new ResourceLocation(
"block/" + stateId.getPath() + "_" + type); stateId.getNamespace(),
"block/" + stateId.getPath() + "_" + type
);
registerBlockModel(stateId, modelId, blockState, modelCache); registerBlockModel(stateId, modelId, blockState, modelCache);
if (type == SlabType.TOP) { if (type == SlabType.TOP) {
return ModelsHelper.createMultiVariant(modelId, BlockModelRotation.X180_Y0.getRotation(), true); return ModelsHelper.createMultiVariant(modelId, BlockModelRotation.X180_Y0.getRotation(), true);

View file

@ -1,12 +1,5 @@
package ru.bclib.blocks; package ru.bclib.blocks;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.jetbrains.annotations.Nullable;
import net.fabricmc.api.EnvType; import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment; import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
@ -22,10 +15,16 @@ import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.Half; import net.minecraft.world.level.block.state.properties.Half;
import net.minecraft.world.level.block.state.properties.StairsShape; import net.minecraft.world.level.block.state.properties.StairsShape;
import net.minecraft.world.level.storage.loot.LootContext; import net.minecraft.world.level.storage.loot.LootContext;
import org.jetbrains.annotations.Nullable;
import ru.bclib.client.models.BasePatterns; import ru.bclib.client.models.BasePatterns;
import ru.bclib.client.models.BlockModelProvider;
import ru.bclib.client.models.ModelsHelper; import ru.bclib.client.models.ModelsHelper;
import ru.bclib.client.models.PatternsHelper; import ru.bclib.client.models.PatternsHelper;
import ru.bclib.interfaces.BlockModelProvider;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
public class BaseStairsBlock extends StairBlock implements BlockModelProvider { public class BaseStairsBlock extends StairBlock implements BlockModelProvider {
@ -41,13 +40,13 @@ public class BaseStairsBlock extends StairBlock implements BlockModelProvider {
public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) { public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) {
return Collections.singletonList(new ItemStack(this)); return Collections.singletonList(new ItemStack(this));
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public BlockModel getItemModel(ResourceLocation resourceLocation) { public BlockModel getItemModel(ResourceLocation resourceLocation) {
return getBlockModel(resourceLocation, defaultBlockState()); return getBlockModel(resourceLocation, defaultBlockState());
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public @Nullable BlockModel getBlockModel(ResourceLocation blockId, BlockState blockState) { public @Nullable BlockModel getBlockModel(ResourceLocation blockId, BlockState blockState) {
@ -68,7 +67,7 @@ public class BaseStairsBlock extends StairBlock implements BlockModelProvider {
} }
return ModelsHelper.fromPattern(pattern); return ModelsHelper.fromPattern(pattern);
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public UnbakedModel getModelVariant(ResourceLocation stateId, BlockState blockState, Map<ResourceLocation, UnbakedModel> modelCache) { public UnbakedModel getModelVariant(ResourceLocation stateId, BlockState blockState, Map<ResourceLocation, UnbakedModel> modelCache) {
@ -77,21 +76,21 @@ public class BaseStairsBlock extends StairBlock implements BlockModelProvider {
switch (shape) { switch (shape) {
case INNER_LEFT: case INNER_LEFT:
case INNER_RIGHT: case INNER_RIGHT:
state = "_inner"; break; state = "_inner";
break;
case OUTER_LEFT: case OUTER_LEFT:
case OUTER_RIGHT: case OUTER_RIGHT:
state = "_outer"; break; state = "_outer";
break;
default: default:
state = ""; state = "";
} }
ResourceLocation modelId = new ResourceLocation(stateId.getNamespace(), "block/" + stateId.getPath() + state); ResourceLocation modelId = new ResourceLocation(stateId.getNamespace(), "block/" + stateId.getPath() + state);
registerBlockModel(stateId, modelId, blockState, modelCache); registerBlockModel(stateId, modelId, blockState, modelCache);
boolean isTop = blockState.getValue(HALF) == Half.TOP; boolean isTop = blockState.getValue(HALF) == Half.TOP;
boolean isLeft = shape == StairsShape.INNER_LEFT || boolean isLeft = shape == StairsShape.INNER_LEFT || shape == StairsShape.OUTER_LEFT;
shape == StairsShape.OUTER_LEFT; boolean isRight = shape == StairsShape.INNER_RIGHT || shape == StairsShape.OUTER_RIGHT;
boolean isRight = shape == StairsShape.INNER_RIGHT ||
shape == StairsShape.OUTER_RIGHT;
int y = 0; int y = 0;
int x = isTop ? 180 : 0; int x = isTop ? 180 : 0;
switch (blockState.getValue(FACING)) { switch (blockState.getValue(FACING)) {

View file

@ -6,11 +6,11 @@ import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Block;
public class BaseStoneButtonBlock extends BaseButtonBlock { public class BaseStoneButtonBlock extends BaseButtonBlock {
public BaseStoneButtonBlock(Block source) { public BaseStoneButtonBlock(Block source) {
super(source, FabricBlockSettings.copyOf(source).noOcclusion(), false); super(source, FabricBlockSettings.copyOf(source).noOcclusion(), false);
} }
@Override @Override
protected SoundEvent getSound(boolean clicked) { protected SoundEvent getSound(boolean clicked) {
return clicked ? SoundEvents.STONE_BUTTON_CLICK_ON : SoundEvents.STONE_BUTTON_CLICK_OFF; return clicked ? SoundEvents.STONE_BUTTON_CLICK_ON : SoundEvents.STONE_BUTTON_CLICK_OFF;

View file

@ -30,7 +30,11 @@ public class BaseStripableLogBlock extends BaseRotatedPillarBlock {
if (FabricToolTags.AXES.contains(player.getMainHandItem().getItem())) { if (FabricToolTags.AXES.contains(player.getMainHandItem().getItem())) {
world.playSound(player, pos, SoundEvents.AXE_STRIP, SoundSource.BLOCKS, 1.0F, 1.0F); world.playSound(player, pos, SoundEvents.AXE_STRIP, SoundSource.BLOCKS, 1.0F, 1.0F);
if (!world.isClientSide) { if (!world.isClientSide) {
world.setBlock(pos, striped.defaultBlockState().setValue(RotatedPillarBlock.AXIS, state.getValue(RotatedPillarBlock.AXIS)), 11); world.setBlock(pos,
striped.defaultBlockState()
.setValue(RotatedPillarBlock.AXIS, state.getValue(RotatedPillarBlock.AXIS)),
11
);
if (!player.isCreative()) { if (!player.isCreative()) {
player.getMainHandItem().hurt(1, world.random, (ServerPlayer) player); player.getMainHandItem().hurt(1, world.random, (ServerPlayer) player);
} }

View file

@ -1,15 +1,6 @@
package ru.bclib.blocks; package ru.bclib.blocks;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Random;
import org.jetbrains.annotations.Nullable;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import net.fabricmc.api.EnvType; import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment; import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
@ -41,30 +32,41 @@ import net.minecraft.world.level.material.MaterialColor;
import net.minecraft.world.level.storage.loot.LootContext; import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.world.level.storage.loot.parameters.LootContextParams; import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
import net.minecraft.world.phys.BlockHitResult; import net.minecraft.world.phys.BlockHitResult;
import org.jetbrains.annotations.Nullable;
import ru.bclib.client.models.BasePatterns; import ru.bclib.client.models.BasePatterns;
import ru.bclib.client.models.ModelsHelper; import ru.bclib.client.models.ModelsHelper;
import ru.bclib.client.models.PatternsHelper; import ru.bclib.client.models.PatternsHelper;
import ru.bclib.client.sound.BlockSounds; import ru.bclib.client.sound.BlockSounds;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Random;
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public class BaseTerrainBlock extends BaseBlock { public class BaseTerrainBlock extends BaseBlock {
private final Block baseBlock; private final Block baseBlock;
private Block pathBlock; private Block pathBlock;
public BaseTerrainBlock(Block baseBlock, MaterialColor color) { public BaseTerrainBlock(Block baseBlock, MaterialColor color) {
super(FabricBlockSettings.copyOf(baseBlock).materialColor(color).sound(BlockSounds.TERRAIN_SOUND).randomTicks()); super(FabricBlockSettings
.copyOf(baseBlock)
.materialColor(color)
.sound(BlockSounds.TERRAIN_SOUND)
.randomTicks()
);
this.baseBlock = baseBlock; this.baseBlock = baseBlock;
} }
public void setPathBlock(Block roadBlock) { public void setPathBlock(Block roadBlock) {
this.pathBlock = roadBlock; this.pathBlock = roadBlock;
} }
public Block getBaseBlock() { public Block getBaseBlock() {
return baseBlock; return baseBlock;
} }
@Override @Override
public InteractionResult use(BlockState state, Level world, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) { public InteractionResult use(BlockState state, Level world, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
if (pathBlock != null && FabricToolTags.SHOVELS.contains(player.getMainHandItem().getItem())) { if (pathBlock != null && FabricToolTags.SHOVELS.contains(player.getMainHandItem().getItem())) {
@ -86,16 +88,16 @@ public class BaseTerrainBlock extends BaseBlock {
if (tool != null && EnchantmentHelper.getItemEnchantmentLevel(Enchantments.SILK_TOUCH, tool) > 0) { if (tool != null && EnchantmentHelper.getItemEnchantmentLevel(Enchantments.SILK_TOUCH, tool) > 0) {
return Collections.singletonList(new ItemStack(this)); return Collections.singletonList(new ItemStack(this));
} }
return Collections.singletonList(new ItemStack(Blocks.END_STONE)); return Collections.singletonList(new ItemStack(getBaseBlock()));
} }
@Override @Override
public void randomTick(BlockState state, ServerLevel world, BlockPos pos, Random random) { public void randomTick(BlockState state, ServerLevel world, BlockPos pos, Random random) {
if (random.nextInt(16) == 0 && !canStay(state, world, pos)) { if (random.nextInt(16) == 0 && !canStay(state, world, pos)) {
world.setBlockAndUpdate(pos, Blocks.END_STONE.defaultBlockState()); world.setBlockAndUpdate(pos, getBaseBlock().defaultBlockState());
} }
} }
public boolean canStay(BlockState state, LevelReader worldView, BlockPos pos) { public boolean canStay(BlockState state, LevelReader worldView, BlockPos pos) {
BlockPos blockPos = pos.above(); BlockPos blockPos = pos.above();
BlockState blockState = worldView.getBlockState(blockPos); BlockState blockState = worldView.getBlockState(blockPos);
@ -106,7 +108,15 @@ public class BaseTerrainBlock extends BaseBlock {
return false; return false;
} }
else { else {
int i = LayerLightEngine.getLightBlockInto(worldView, state, pos, blockState, blockPos, Direction.UP, blockState.getLightBlock(worldView, blockPos)); int i = LayerLightEngine.getLightBlockInto(
worldView,
state,
pos,
blockState,
blockPos,
Direction.UP,
blockState.getLightBlock(worldView, blockPos)
);
return i < 5; return i < 5;
} }
} }
@ -116,11 +126,11 @@ public class BaseTerrainBlock extends BaseBlock {
public BlockModel getItemModel(ResourceLocation blockId) { public BlockModel getItemModel(ResourceLocation blockId) {
return getBlockModel(blockId, defaultBlockState()); return getBlockModel(blockId, defaultBlockState());
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public @Nullable BlockModel getBlockModel(ResourceLocation blockId, BlockState blockState) { public @Nullable BlockModel getBlockModel(ResourceLocation blockId, BlockState blockState) {
ResourceLocation baseId = Registry.BLOCK.getKey(baseBlock); ResourceLocation baseId = Registry.BLOCK.getKey(getBaseBlock());
String modId = blockId.getNamespace(); String modId = blockId.getNamespace();
String path = blockId.getPath(); String path = blockId.getPath();
String bottom = baseId.getNamespace() + ":block/" + baseId.getPath(); String bottom = baseId.getNamespace() + ":block/" + baseId.getPath();
@ -131,7 +141,7 @@ public class BaseTerrainBlock extends BaseBlock {
Optional<String> pattern = PatternsHelper.createJson(BasePatterns.BLOCK_TOP_SIDE_BOTTOM, textures); Optional<String> pattern = PatternsHelper.createJson(BasePatterns.BLOCK_TOP_SIDE_BOTTOM, textures);
return ModelsHelper.fromPattern(pattern); return ModelsHelper.fromPattern(pattern);
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public UnbakedModel getModelVariant(ResourceLocation stateId, BlockState blockState, Map<ResourceLocation, UnbakedModel> modelCache) { public UnbakedModel getModelVariant(ResourceLocation stateId, BlockState blockState, Map<ResourceLocation, UnbakedModel> modelCache) {

View file

@ -1,13 +1,5 @@
package ru.bclib.blocks; package ru.bclib.blocks;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.jetbrains.annotations.Nullable;
import net.fabricmc.api.EnvType; import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment; import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
@ -21,18 +13,25 @@ import net.minecraft.world.level.block.TrapDoorBlock;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.Half; import net.minecraft.world.level.block.state.properties.Half;
import net.minecraft.world.level.storage.loot.LootContext; import net.minecraft.world.level.storage.loot.LootContext;
import org.jetbrains.annotations.Nullable;
import ru.bclib.client.models.BasePatterns; import ru.bclib.client.models.BasePatterns;
import ru.bclib.client.models.BlockModelProvider;
import ru.bclib.client.models.ModelsHelper; import ru.bclib.client.models.ModelsHelper;
import ru.bclib.client.models.PatternsHelper; import ru.bclib.client.models.PatternsHelper;
import ru.bclib.client.render.BCLRenderLayer; import ru.bclib.client.render.BCLRenderLayer;
import ru.bclib.interfaces.IRenderTyped; import ru.bclib.interfaces.BlockModelProvider;
import ru.bclib.interfaces.RenderLayerProvider;
public class BaseTrapdoorBlock extends TrapDoorBlock implements IRenderTyped, BlockModelProvider { import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
public class BaseTrapdoorBlock extends TrapDoorBlock implements RenderLayerProvider, BlockModelProvider {
public BaseTrapdoorBlock(Block source) { public BaseTrapdoorBlock(Block source) {
super(FabricBlockSettings.copyOf(source).strength(3.0F, 3.0F).noOcclusion()); super(FabricBlockSettings.copyOf(source).strength(3.0F, 3.0F).noOcclusion());
} }
@Override @Override
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) { public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) {
@ -43,28 +42,32 @@ public class BaseTrapdoorBlock extends TrapDoorBlock implements IRenderTyped, Bl
public BCLRenderLayer getRenderLayer() { public BCLRenderLayer getRenderLayer() {
return BCLRenderLayer.CUTOUT; return BCLRenderLayer.CUTOUT;
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public BlockModel getItemModel(ResourceLocation resourceLocation) { public BlockModel getItemModel(ResourceLocation resourceLocation) {
return getBlockModel(resourceLocation, defaultBlockState()); return getBlockModel(resourceLocation, defaultBlockState());
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public @Nullable BlockModel getBlockModel(ResourceLocation resourceLocation, BlockState blockState) { public @Nullable BlockModel getBlockModel(ResourceLocation resourceLocation, BlockState blockState) {
String name = resourceLocation.getPath(); String name = resourceLocation.getPath();
Optional<String> pattern = PatternsHelper.createJson(BasePatterns.BLOCK_TRAPDOOR, new HashMap<String, String>() { Optional<String> pattern = PatternsHelper.createJson(
private static final long serialVersionUID = 1L; BasePatterns.BLOCK_TRAPDOOR,
{ new HashMap<String, String>() {
put("%modid%", resourceLocation.getNamespace()); private static final long serialVersionUID = 1L;
put("%texture%", name);
put("%side%", name.replace("trapdoor", "door_side")); {
put("%modid%", resourceLocation.getNamespace());
put("%texture%", name);
put("%side%", name.replace("trapdoor", "door_side"));
}
} }
}); );
return ModelsHelper.fromPattern(pattern); return ModelsHelper.fromPattern(pattern);
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public UnbakedModel getModelVariant(ResourceLocation stateId, BlockState blockState, Map<ResourceLocation, UnbakedModel> modelCache) { public UnbakedModel getModelVariant(ResourceLocation stateId, BlockState blockState, Map<ResourceLocation, UnbakedModel> modelCache) {
@ -87,7 +90,8 @@ public class BaseTrapdoorBlock extends TrapDoorBlock implements IRenderTyped, Bl
case WEST: case WEST:
y = (isTop && isOpen) ? 90 : 270; y = (isTop && isOpen) ? 90 : 270;
break; break;
default: break; default:
break;
} }
BlockModelRotation rotation = BlockModelRotation.by(x, y); BlockModelRotation rotation = BlockModelRotation.by(x, y);
return ModelsHelper.createMultiVariant(modelId, rotation.getRotation(), false); return ModelsHelper.createMultiVariant(modelId, rotation.getRotation(), false);

View file

@ -18,19 +18,19 @@ public abstract class BaseUnderwaterWallPlantBlock extends BaseWallPlantBlock im
public BaseUnderwaterWallPlantBlock() { public BaseUnderwaterWallPlantBlock() {
super(FabricBlockSettings.of(Material.WATER_PLANT) super(FabricBlockSettings.of(Material.WATER_PLANT)
.breakByTool(FabricToolTags.SHEARS) .breakByTool(FabricToolTags.SHEARS)
.breakByHand(true) .breakByHand(true)
.sound(SoundType.WET_GRASS) .sound(SoundType.WET_GRASS)
.noCollission()); .noCollission());
} }
public BaseUnderwaterWallPlantBlock(int light) { public BaseUnderwaterWallPlantBlock(int light) {
super(FabricBlockSettings.of(Material.WATER_PLANT) super(FabricBlockSettings.of(Material.WATER_PLANT)
.breakByTool(FabricToolTags.SHEARS) .breakByTool(FabricToolTags.SHEARS)
.breakByHand(true) .breakByHand(true)
.luminance(light) .luminance(light)
.sound(SoundType.WET_GRASS) .sound(SoundType.WET_GRASS)
.noCollission()); .noCollission());
} }
public BaseUnderwaterWallPlantBlock(Properties settings) { public BaseUnderwaterWallPlantBlock(Properties settings) {
@ -41,7 +41,7 @@ public abstract class BaseUnderwaterWallPlantBlock extends BaseWallPlantBlock im
public boolean canPlaceLiquid(BlockGetter world, BlockPos pos, BlockState state, Fluid fluid) { public boolean canPlaceLiquid(BlockGetter world, BlockPos pos, BlockState state, Fluid fluid) {
return false; return false;
} }
@Override @Override
public boolean placeLiquid(LevelAccessor world, BlockPos pos, BlockState state, FluidState fluidState) { public boolean placeLiquid(LevelAccessor world, BlockPos pos, BlockState state, FluidState fluidState) {
return false; return false;

View file

@ -1,10 +1,6 @@
package ru.bclib.blocks; package ru.bclib.blocks;
import java.util.List;
import java.util.Random;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.fabricmc.fabric.api.tool.attribute.v1.FabricToolTags; import net.fabricmc.fabric.api.tool.attribute.v1.FabricToolTags;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
@ -34,11 +30,14 @@ import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape; import net.minecraft.world.phys.shapes.VoxelShape;
import ru.bclib.blocks.BlockProperties.TripleShape; import ru.bclib.blocks.BlockProperties.TripleShape;
import ru.bclib.client.render.BCLRenderLayer; import ru.bclib.client.render.BCLRenderLayer;
import ru.bclib.interfaces.IRenderTyped; import ru.bclib.interfaces.RenderLayerProvider;
import ru.bclib.util.BlocksHelper; import ru.bclib.util.BlocksHelper;
import java.util.List;
import java.util.Random;
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public class BaseVineBlock extends BaseBlockNotFull implements IRenderTyped, BonemealableBlock { public class BaseVineBlock extends BaseBlockNotFull implements RenderLayerProvider, BonemealableBlock {
public static final EnumProperty<TripleShape> SHAPE = BlockProperties.TRIPLE_SHAPE; public static final EnumProperty<TripleShape> SHAPE = BlockProperties.TRIPLE_SHAPE;
private static final VoxelShape VOXEL_SHAPE = Block.box(2, 0, 2, 14, 16, 14); private static final VoxelShape VOXEL_SHAPE = Block.box(2, 0, 2, 14, 16, 14);
@ -52,11 +51,11 @@ public class BaseVineBlock extends BaseBlockNotFull implements IRenderTyped, Bon
public BaseVineBlock(int light, boolean bottomOnly) { public BaseVineBlock(int light, boolean bottomOnly) {
super(FabricBlockSettings.of(Material.PLANT) super(FabricBlockSettings.of(Material.PLANT)
.breakByTool(FabricToolTags.SHEARS) .breakByTool(FabricToolTags.SHEARS)
.breakByHand(true) .breakByHand(true)
.sound(SoundType.GRASS) .sound(SoundType.GRASS)
.lightLevel((state) -> bottomOnly ? state.getValue(SHAPE) == TripleShape.BOTTOM ? light : 0 : light) .lightLevel((state) -> bottomOnly ? state.getValue(SHAPE) == TripleShape.BOTTOM ? light : 0 : light)
.noCollission()); .noCollission());
this.registerDefaultState(this.stateDefinition.any().setValue(SHAPE, TripleShape.BOTTOM)); this.registerDefaultState(this.stateDefinition.any().setValue(SHAPE, TripleShape.BOTTOM));
} }
@ -64,13 +63,13 @@ public class BaseVineBlock extends BaseBlockNotFull implements IRenderTyped, Bon
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> stateManager) { protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> stateManager) {
stateManager.add(SHAPE); stateManager.add(SHAPE);
} }
@Override @Override
public VoxelShape getShape(BlockState state, BlockGetter view, BlockPos pos, CollisionContext ePos) { public VoxelShape getShape(BlockState state, BlockGetter view, BlockPos pos, CollisionContext ePos) {
Vec3 vec3d = state.getOffset(view, pos); Vec3 vec3d = state.getOffset(view, pos);
return VOXEL_SHAPE.move(vec3d.x, vec3d.y, vec3d.z); return VOXEL_SHAPE.move(vec3d.x, vec3d.y, vec3d.z);
} }
@Override @Override
public BlockBehaviour.OffsetType getOffsetType() { public BlockBehaviour.OffsetType getOffsetType() {
return BlockBehaviour.OffsetType.XZ; return BlockBehaviour.OffsetType.XZ;
@ -79,7 +78,7 @@ public class BaseVineBlock extends BaseBlockNotFull implements IRenderTyped, Bon
public boolean canGenerate(BlockState state, LevelReader world, BlockPos pos) { public boolean canGenerate(BlockState state, LevelReader world, BlockPos pos) {
return isSupport(state, world, pos); return isSupport(state, world, pos);
} }
@Override @Override
public boolean canSurvive(BlockState state, LevelReader world, BlockPos pos) { public boolean canSurvive(BlockState state, LevelReader world, BlockPos pos) {
return isSupport(state, world, pos); return isSupport(state, world, pos);
@ -89,17 +88,15 @@ public class BaseVineBlock extends BaseBlockNotFull implements IRenderTyped, Bon
BlockState up = world.getBlockState(pos.above()); BlockState up = world.getBlockState(pos.above());
return up.is(this) || up.is(BlockTags.LEAVES) || canSupportCenter(world, pos.above(), Direction.DOWN); return up.is(this) || up.is(BlockTags.LEAVES) || canSupportCenter(world, pos.above(), Direction.DOWN);
} }
@Override @Override
public BlockState updateShape(BlockState state, Direction facing, BlockState neighborState, LevelAccessor world, BlockPos pos, BlockPos neighborPos) { public BlockState updateShape(BlockState state, Direction facing, BlockState neighborState, LevelAccessor world, BlockPos pos, BlockPos neighborPos) {
if (!canSurvive(state, world, pos)) { if (!canSurvive(state, world, pos)) {
return Blocks.AIR.defaultBlockState(); return Blocks.AIR.defaultBlockState();
} }
else { else {
if (world.getBlockState(pos.below()).getBlock() != this) if (world.getBlockState(pos.below()).getBlock() != this) return state.setValue(SHAPE, TripleShape.BOTTOM);
return state.setValue(SHAPE, TripleShape.BOTTOM); else if (world.getBlockState(pos.above()).getBlock() != this) return state.setValue(SHAPE, TripleShape.TOP);
else if (world.getBlockState(pos.above()).getBlock() != this)
return state.setValue(SHAPE, TripleShape.TOP);
return state.setValue(SHAPE, TripleShape.MIDDLE); return state.setValue(SHAPE, TripleShape.MIDDLE);
} }
} }
@ -107,7 +104,10 @@ public class BaseVineBlock extends BaseBlockNotFull implements IRenderTyped, Bon
@Override @Override
public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) { public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) {
ItemStack tool = builder.getParameter(LootContextParams.TOOL); ItemStack tool = builder.getParameter(LootContextParams.TOOL);
if (tool != null && FabricToolTags.SHEARS.contains(tool.getItem()) || EnchantmentHelper.getItemEnchantmentLevel(Enchantments.SILK_TOUCH, tool) > 0) { if (tool != null && FabricToolTags.SHEARS.contains(tool.getItem()) || EnchantmentHelper.getItemEnchantmentLevel(
Enchantments.SILK_TOUCH,
tool
) > 0) {
return Lists.newArrayList(new ItemStack(this)); return Lists.newArrayList(new ItemStack(this));
} }
else { else {
@ -119,7 +119,7 @@ public class BaseVineBlock extends BaseBlockNotFull implements IRenderTyped, Bon
public BCLRenderLayer getRenderLayer() { public BCLRenderLayer getRenderLayer() {
return BCLRenderLayer.CUTOUT; return BCLRenderLayer.CUTOUT;
} }
@Override @Override
public boolean isValidBonemealTarget(BlockGetter world, BlockPos pos, BlockState state, boolean isClient) { public boolean isValidBonemealTarget(BlockGetter world, BlockPos pos, BlockState state, boolean isClient) {
while (world.getBlockState(pos).getBlock() == this) { while (world.getBlockState(pos).getBlock() == this) {
@ -127,7 +127,7 @@ public class BaseVineBlock extends BaseBlockNotFull implements IRenderTyped, Bon
} }
return world.getBlockState(pos).isAir(); return world.getBlockState(pos).isAir();
} }
@Override @Override
public boolean isBonemealSuccess(Level world, Random random, BlockPos pos, BlockState state) { public boolean isBonemealSuccess(Level world, Random random, BlockPos pos, BlockState state) {
while (world.getBlockState(pos).getBlock() == this) { while (world.getBlockState(pos).getBlock() == this) {
@ -135,7 +135,7 @@ public class BaseVineBlock extends BaseBlockNotFull implements IRenderTyped, Bon
} }
return world.isEmptyBlock(pos); return world.isEmptyBlock(pos);
} }
@Override @Override
public void performBonemeal(ServerLevel world, Random random, BlockPos pos, BlockState state) { public void performBonemeal(ServerLevel world, Random random, BlockPos pos, BlockState state) {
while (world.getBlockState(pos).getBlock() == this) { while (world.getBlockState(pos).getBlock() == this) {

View file

@ -1,12 +1,5 @@
package ru.bclib.blocks; package ru.bclib.blocks;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.jetbrains.annotations.Nullable;
import net.fabricmc.api.EnvType; import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment; import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
@ -21,10 +14,16 @@ import net.minecraft.world.level.block.WallBlock;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.WallSide; import net.minecraft.world.level.block.state.properties.WallSide;
import net.minecraft.world.level.storage.loot.LootContext; import net.minecraft.world.level.storage.loot.LootContext;
import org.jetbrains.annotations.Nullable;
import ru.bclib.client.models.BasePatterns; import ru.bclib.client.models.BasePatterns;
import ru.bclib.client.models.BlockModelProvider;
import ru.bclib.client.models.ModelsHelper; import ru.bclib.client.models.ModelsHelper;
import ru.bclib.client.models.PatternsHelper; import ru.bclib.client.models.PatternsHelper;
import ru.bclib.interfaces.BlockModelProvider;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
public class BaseWallBlock extends WallBlock implements BlockModelProvider { public class BaseWallBlock extends WallBlock implements BlockModelProvider {
@ -34,13 +33,13 @@ public class BaseWallBlock extends WallBlock implements BlockModelProvider {
super(FabricBlockSettings.copyOf(source).noOcclusion()); super(FabricBlockSettings.copyOf(source).noOcclusion());
this.parent = source; this.parent = source;
} }
@Override @Override
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) { public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) {
return Collections.singletonList(new ItemStack(this)); return Collections.singletonList(new ItemStack(this));
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public BlockModel getItemModel(ResourceLocation blockId) { public BlockModel getItemModel(ResourceLocation blockId) {
@ -48,7 +47,7 @@ public class BaseWallBlock extends WallBlock implements BlockModelProvider {
Optional<String> pattern = PatternsHelper.createJson(BasePatterns.ITEM_WALL, parentId); Optional<String> pattern = PatternsHelper.createJson(BasePatterns.ITEM_WALL, parentId);
return ModelsHelper.fromPattern(pattern); return ModelsHelper.fromPattern(pattern);
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public @Nullable BlockModel getBlockModel(ResourceLocation blockId, BlockState blockState) { public @Nullable BlockModel getBlockModel(ResourceLocation blockId, BlockState blockState) {
@ -66,37 +65,58 @@ public class BaseWallBlock extends WallBlock implements BlockModelProvider {
} }
return ModelsHelper.fromPattern(pattern); return ModelsHelper.fromPattern(pattern);
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public UnbakedModel getModelVariant(ResourceLocation stateId, BlockState blockState, Map<ResourceLocation, UnbakedModel> modelCache) { public UnbakedModel getModelVariant(ResourceLocation stateId, BlockState blockState, Map<ResourceLocation, UnbakedModel> modelCache) {
ResourceLocation postId = new ResourceLocation(stateId.getNamespace(), ResourceLocation postId = new ResourceLocation(stateId.getNamespace(), "block/" + stateId.getPath() + "_post");
"block/" + stateId.getPath() + "_post"); ResourceLocation sideId = new ResourceLocation(stateId.getNamespace(), "block/" + stateId.getPath() + "_side");
ResourceLocation sideId = new ResourceLocation(stateId.getNamespace(), ResourceLocation sideTallId = new ResourceLocation(
"block/" + stateId.getPath() + "_side"); stateId.getNamespace(),
ResourceLocation sideTallId = new ResourceLocation(stateId.getNamespace(), "block/" + stateId.getPath() + "_side_tall"
"block/" + stateId.getPath() + "_side_tall"); );
registerBlockModel(postId, postId, blockState, modelCache); registerBlockModel(postId, postId, blockState, modelCache);
registerBlockModel(sideId, sideId, blockState, modelCache); registerBlockModel(sideId, sideId, blockState, modelCache);
registerBlockModel(sideTallId, sideTallId, blockState, modelCache); registerBlockModel(sideTallId, sideTallId, blockState, modelCache);
ModelsHelper.MultiPartBuilder builder = ModelsHelper.MultiPartBuilder.create(stateDefinition); ModelsHelper.MultiPartBuilder builder = ModelsHelper.MultiPartBuilder.create(stateDefinition);
builder.part(sideId).setCondition(state -> state.getValue(NORTH_WALL) == WallSide.LOW).setUVLock(true).add(); builder.part(sideId).setCondition(state -> state.getValue(NORTH_WALL) == WallSide.LOW).setUVLock(true).add();
builder.part(sideId).setCondition(state -> state.getValue(EAST_WALL) == WallSide.LOW) builder.part(sideId)
.setTransformation(BlockModelRotation.X0_Y90.getRotation()).setUVLock(true).add(); .setCondition(state -> state.getValue(EAST_WALL) == WallSide.LOW)
builder.part(sideId).setCondition(state -> state.getValue(SOUTH_WALL) == WallSide.LOW) .setTransformation(BlockModelRotation.X0_Y90.getRotation())
.setTransformation(BlockModelRotation.X0_Y180.getRotation()).setUVLock(true).add(); .setUVLock(true)
builder.part(sideId).setCondition(state -> state.getValue(WEST_WALL) == WallSide.LOW) .add();
.setTransformation(BlockModelRotation.X0_Y270.getRotation()).setUVLock(true).add(); builder.part(sideId)
builder.part(sideTallId).setCondition(state -> state.getValue(NORTH_WALL) == WallSide.TALL).setUVLock(true).add(); .setCondition(state -> state.getValue(SOUTH_WALL) == WallSide.LOW)
builder.part(sideTallId).setCondition(state -> state.getValue(EAST_WALL) == WallSide.TALL) .setTransformation(BlockModelRotation.X0_Y180.getRotation())
.setTransformation(BlockModelRotation.X0_Y90.getRotation()).setUVLock(true).add(); .setUVLock(true)
builder.part(sideTallId).setCondition(state -> state.getValue(SOUTH_WALL) == WallSide.TALL) .add();
.setTransformation(BlockModelRotation.X0_Y180.getRotation()).setUVLock(true).add(); builder.part(sideId)
builder.part(sideTallId).setCondition(state -> state.getValue(WEST_WALL) == WallSide.TALL) .setCondition(state -> state.getValue(WEST_WALL) == WallSide.LOW)
.setTransformation(BlockModelRotation.X0_Y270.getRotation()).setUVLock(true).add(); .setTransformation(BlockModelRotation.X0_Y270.getRotation())
.setUVLock(true)
.add();
builder.part(sideTallId)
.setCondition(state -> state.getValue(NORTH_WALL) == WallSide.TALL)
.setUVLock(true)
.add();
builder.part(sideTallId)
.setCondition(state -> state.getValue(EAST_WALL) == WallSide.TALL)
.setTransformation(BlockModelRotation.X0_Y90.getRotation())
.setUVLock(true)
.add();
builder.part(sideTallId)
.setCondition(state -> state.getValue(SOUTH_WALL) == WallSide.TALL)
.setTransformation(BlockModelRotation.X0_Y180.getRotation())
.setUVLock(true)
.add();
builder.part(sideTallId)
.setCondition(state -> state.getValue(WEST_WALL) == WallSide.TALL)
.setTransformation(BlockModelRotation.X0_Y270.getRotation())
.setUVLock(true)
.add();
builder.part(postId).setCondition(state -> state.getValue(UP)).add(); builder.part(postId).setCondition(state -> state.getValue(UP)).add();
return builder.build(); return builder.build();
} }
} }

View file

@ -1,10 +1,7 @@
package ru.bclib.blocks; package ru.bclib.blocks;
import java.util.EnumMap;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.fabricmc.fabric.api.tool.attribute.v1.FabricToolTags; import net.fabricmc.fabric.api.tool.attribute.v1.FabricToolTags;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
@ -28,29 +25,36 @@ import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape; import net.minecraft.world.phys.shapes.VoxelShape;
import ru.bclib.util.BlocksHelper; import ru.bclib.util.BlocksHelper;
import java.util.EnumMap;
public abstract class BaseWallPlantBlock extends BasePlantBlock { public abstract class BaseWallPlantBlock extends BasePlantBlock {
private static final EnumMap<Direction, VoxelShape> SHAPES = Maps.newEnumMap(ImmutableMap.of( private static final EnumMap<Direction, VoxelShape> SHAPES = Maps.newEnumMap(ImmutableMap.of(
Direction.NORTH, Block.box(1, 1, 8, 15, 15, 16), Direction.NORTH,
Direction.SOUTH, Block.box(1, 1, 0, 15, 15, 8), Block.box(1, 1, 8, 15, 15, 16),
Direction.WEST, Block.box(8, 1, 1, 16, 15, 15), Direction.SOUTH,
Direction.EAST, Block.box(0, 1, 1, 8, 15, 15))); Block.box(1, 1, 0, 15, 15, 8),
Direction.WEST,
Block.box(8, 1, 1, 16, 15, 15),
Direction.EAST,
Block.box(0, 1, 1, 8, 15, 15)
));
public static final DirectionProperty FACING = HorizontalDirectionalBlock.FACING; public static final DirectionProperty FACING = HorizontalDirectionalBlock.FACING;
public BaseWallPlantBlock() { public BaseWallPlantBlock() {
this(FabricBlockSettings.of(Material.PLANT) this(FabricBlockSettings.of(Material.PLANT)
.breakByTool(FabricToolTags.SHEARS) .breakByTool(FabricToolTags.SHEARS)
.breakByHand(true) .breakByHand(true)
.sound(SoundType.GRASS) .sound(SoundType.GRASS)
.noCollission()); .noCollission());
} }
public BaseWallPlantBlock(int light) { public BaseWallPlantBlock(int light) {
this(FabricBlockSettings.of(Material.PLANT) this(FabricBlockSettings.of(Material.PLANT)
.breakByTool(FabricToolTags.SHEARS) .breakByTool(FabricToolTags.SHEARS)
.breakByHand(true) .breakByHand(true)
.luminance(light) .luminance(light)
.sound(SoundType.GRASS) .sound(SoundType.GRASS)
.noCollission()); .noCollission());
} }
public BaseWallPlantBlock(Properties settings) { public BaseWallPlantBlock(Properties settings) {
@ -61,17 +65,17 @@ public abstract class BaseWallPlantBlock extends BasePlantBlock {
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> stateManager) { protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> stateManager) {
stateManager.add(FACING); stateManager.add(FACING);
} }
@Override @Override
public VoxelShape getShape(BlockState state, BlockGetter view, BlockPos pos, CollisionContext ePos) { public VoxelShape getShape(BlockState state, BlockGetter view, BlockPos pos, CollisionContext ePos) {
return SHAPES.get(state.getValue(FACING)); return SHAPES.get(state.getValue(FACING));
} }
@Override @Override
public BlockBehaviour.OffsetType getOffsetType() { public BlockBehaviour.OffsetType getOffsetType() {
return BlockBehaviour.OffsetType.NONE; return BlockBehaviour.OffsetType.NONE;
} }
@Override @Override
public boolean canSurvive(BlockState state, LevelReader world, BlockPos pos) { public boolean canSurvive(BlockState state, LevelReader world, BlockPos pos) {
Direction direction = state.getValue(FACING); Direction direction = state.getValue(FACING);
@ -101,7 +105,7 @@ public abstract class BaseWallPlantBlock extends BasePlantBlock {
} }
return null; return null;
} }
@Override @Override
public BlockState updateShape(BlockState state, Direction facing, BlockState neighborState, LevelAccessor world, BlockPos pos, BlockPos neighborPos) { public BlockState updateShape(BlockState state, Direction facing, BlockState neighborState, LevelAccessor world, BlockPos pos, BlockPos neighborPos) {
if (!canSurvive(state, world, pos)) { if (!canSurvive(state, world, pos)) {
@ -117,7 +121,7 @@ public abstract class BaseWallPlantBlock extends BasePlantBlock {
public BlockState rotate(BlockState state, Rotation rotation) { public BlockState rotate(BlockState state, Rotation rotation) {
return BlocksHelper.rotateHorizontal(state, rotation, FACING); return BlocksHelper.rotateHorizontal(state, rotation, FACING);
} }
@Override @Override
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public BlockState mirror(BlockState state, Mirror mirror) { public BlockState mirror(BlockState state, Mirror mirror) {

View file

@ -1,12 +1,5 @@
package ru.bclib.blocks; package ru.bclib.blocks;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.jetbrains.annotations.Nullable;
import net.fabricmc.api.EnvType; import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment; import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
@ -19,31 +12,40 @@ import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.WeightedPressurePlateBlock; import net.minecraft.world.level.block.WeightedPressurePlateBlock;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.storage.loot.LootContext; import net.minecraft.world.level.storage.loot.LootContext;
import org.jetbrains.annotations.Nullable;
import ru.bclib.client.models.BasePatterns; import ru.bclib.client.models.BasePatterns;
import ru.bclib.client.models.BlockModelProvider;
import ru.bclib.client.models.ModelsHelper; import ru.bclib.client.models.ModelsHelper;
import ru.bclib.client.models.PatternsHelper; import ru.bclib.client.models.PatternsHelper;
import ru.bclib.interfaces.BlockModelProvider;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
public class BaseWeightedPlateBlock extends WeightedPressurePlateBlock implements BlockModelProvider { public class BaseWeightedPlateBlock extends WeightedPressurePlateBlock implements BlockModelProvider {
private final Block parent; private final Block parent;
public BaseWeightedPlateBlock(Block source) { public BaseWeightedPlateBlock(Block source) {
super(15, FabricBlockSettings.copyOf(source).noCollission().noOcclusion().requiresCorrectToolForDrops().strength(0.5F)); super(
15,
FabricBlockSettings.copyOf(source).noCollission().noOcclusion().requiresCorrectToolForDrops().strength(0.5F)
);
this.parent = source; this.parent = source;
} }
@Override @Override
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) { public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) {
return Collections.singletonList(new ItemStack(this)); return Collections.singletonList(new ItemStack(this));
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public BlockModel getItemModel(ResourceLocation resourceLocation) { public BlockModel getItemModel(ResourceLocation resourceLocation) {
return getBlockModel(resourceLocation, defaultBlockState()); return getBlockModel(resourceLocation, defaultBlockState());
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public @Nullable BlockModel getBlockModel(ResourceLocation resourceLocation, BlockState blockState) { public @Nullable BlockModel getBlockModel(ResourceLocation resourceLocation, BlockState blockState) {
@ -51,18 +53,18 @@ public class BaseWeightedPlateBlock extends WeightedPressurePlateBlock implement
Optional<String> pattern; Optional<String> pattern;
if (blockState.getValue(POWER) > 0) { if (blockState.getValue(POWER) > 0) {
pattern = PatternsHelper.createJson(BasePatterns.BLOCK_PLATE_DOWN, parentId); pattern = PatternsHelper.createJson(BasePatterns.BLOCK_PLATE_DOWN, parentId);
} else { }
else {
pattern = PatternsHelper.createJson(BasePatterns.BLOCK_PLATE_UP, parentId); pattern = PatternsHelper.createJson(BasePatterns.BLOCK_PLATE_UP, parentId);
} }
return ModelsHelper.fromPattern(pattern); return ModelsHelper.fromPattern(pattern);
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public UnbakedModel getModelVariant(ResourceLocation stateId, BlockState blockState, Map<ResourceLocation, UnbakedModel> modelCache) { public UnbakedModel getModelVariant(ResourceLocation stateId, BlockState blockState, Map<ResourceLocation, UnbakedModel> modelCache) {
String state = blockState.getValue(POWER) > 0 ? "_down" : "_up"; String state = blockState.getValue(POWER) > 0 ? "_down" : "_up";
ResourceLocation modelId = new ResourceLocation(stateId.getNamespace(), ResourceLocation modelId = new ResourceLocation(stateId.getNamespace(), "block/" + stateId.getPath() + state);
"block/" + stateId.getPath() + state);
registerBlockModel(stateId, modelId, blockState, modelCache); registerBlockModel(stateId, modelId, blockState, modelCache);
return ModelsHelper.createBlockSimple(modelId); return ModelsHelper.createBlockSimple(modelId);
} }

View file

@ -6,11 +6,11 @@ import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Block;
public class BaseWoodenButtonBlock extends BaseButtonBlock { public class BaseWoodenButtonBlock extends BaseButtonBlock {
public BaseWoodenButtonBlock(Block source) { public BaseWoodenButtonBlock(Block source) {
super(source, FabricBlockSettings.copyOf(source).strength(0.5F, 0.5F).noOcclusion(), true); super(source, FabricBlockSettings.copyOf(source).strength(0.5F, 0.5F).noOcclusion(), true);
} }
@Override @Override
protected SoundEvent getSound(boolean clicked) { protected SoundEvent getSound(boolean clicked) {
return clicked ? SoundEvents.WOODEN_BUTTON_CLICK_ON : SoundEvents.WOODEN_BUTTON_CLICK_OFF; return clicked ? SoundEvents.WOODEN_BUTTON_CLICK_ON : SoundEvents.WOODEN_BUTTON_CLICK_OFF;

View file

@ -16,6 +16,7 @@ public class BlockProperties {
public static final BooleanProperty ACTIVE = BooleanProperty.create("active"); public static final BooleanProperty ACTIVE = BooleanProperty.create("active");
public static final BooleanProperty SMALL = BooleanProperty.create("small"); public static final BooleanProperty SMALL = BooleanProperty.create("small");
public static final IntegerProperty DEFAULT_ANVIL_DURABILITY = IntegerProperty.create("durability", 0, 3);
public static final IntegerProperty DESTRUCTION = IntegerProperty.create("destruction", 0, 2); public static final IntegerProperty DESTRUCTION = IntegerProperty.create("destruction", 0, 2);
public static final IntegerProperty ROTATION = IntegerProperty.create("rotation", 0, 3); public static final IntegerProperty ROTATION = IntegerProperty.create("rotation", 0, 3);
public static final IntegerProperty FULLNESS = IntegerProperty.create("fullness", 0, 3); public static final IntegerProperty FULLNESS = IntegerProperty.create("fullness", 0, 3);
@ -24,9 +25,7 @@ public class BlockProperties {
public static final IntegerProperty AGE = IntegerProperty.create("age", 0, 3); public static final IntegerProperty AGE = IntegerProperty.create("age", 0, 3);
public enum TripleShape implements StringRepresentable { public enum TripleShape implements StringRepresentable {
TOP("top", 0), TOP("top", 0), MIDDLE("middle", 1), BOTTOM("bottom", 2);
MIDDLE("middle", 1),
BOTTOM("bottom", 2);
private final String name; private final String name;
private final int index; private final int index;
@ -35,7 +34,7 @@ public class BlockProperties {
this.name = name; this.name = name;
this.index = index; this.index = index;
} }
@Override @Override
public String getSerializedName() { public String getSerializedName() {
return name; return name;
@ -56,18 +55,14 @@ public class BlockProperties {
} }
public enum PentaShape implements StringRepresentable { public enum PentaShape implements StringRepresentable {
BOTTOM("bottom"), BOTTOM("bottom"), PRE_BOTTOM("pre_bottom"), MIDDLE("middle"), PRE_TOP("pre_top"), TOP("top");
PRE_BOTTOM("pre_bottom"),
MIDDLE("middle"),
PRE_TOP("pre_top"),
TOP("top");
private final String name; private final String name;
PentaShape(String name) { PentaShape(String name) {
this.name = name; this.name = name;
} }
@Override @Override
public String getSerializedName() { public String getSerializedName() {
return name; return name;

View file

@ -0,0 +1,32 @@
package ru.bclib.blocks;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;
public abstract class FeatureHangingSaplingBlock extends FeatureSaplingBlockCommon{
private static final VoxelShape SHAPE = Block.box(4, 2, 4, 12, 16, 12);
public FeatureHangingSaplingBlock() {
super();
}
public FeatureHangingSaplingBlock(int light) {
super(light);
}
@Override
public boolean canSurvive(BlockState blockState, LevelReader levelReader, BlockPos blockPos) {
final BlockPos target = blockPos.above();
return this.mayPlaceOn(levelReader.getBlockState(target), levelReader, target);
}
@Override
public VoxelShape getShape(BlockState state, BlockGetter view, BlockPos pos, CollisionContext ePos) {
return SHAPE;
}
}

View file

@ -1,124 +1,26 @@
package ru.bclib.blocks; package ru.bclib.blocks;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.Random;
import net.minecraft.world.level.levelgen.feature.FeaturePlaceContext;
import org.jetbrains.annotations.Nullable;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.minecraft.client.renderer.block.model.BlockModel;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.BlockGetter; import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.SaplingBlock;
import net.minecraft.world.level.block.SoundType;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.levelgen.feature.Feature;
import net.minecraft.world.level.material.Material;
import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.world.phys.shapes.CollisionContext; import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape; import net.minecraft.world.phys.shapes.VoxelShape;
import ru.bclib.client.models.BasePatterns;
import ru.bclib.client.models.BlockModelProvider;
import ru.bclib.client.models.ModelsHelper;
import ru.bclib.client.models.PatternsHelper;
import ru.bclib.client.render.BCLRenderLayer;
import ru.bclib.interfaces.IRenderTyped;
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public abstract class FeatureSaplingBlock extends SaplingBlock implements IRenderTyped, BlockModelProvider { public abstract class FeatureSaplingBlock extends FeatureSaplingBlockCommon {
private static final VoxelShape SHAPE = Block.box(4, 0, 4, 12, 14, 12); private static final VoxelShape SHAPE = Block.box(4, 0, 4, 12, 14, 12);
public FeatureSaplingBlock() {
super(null, FabricBlockSettings.of(Material.PLANT)
.breakByHand(true)
.collidable(false)
.instabreak()
.sound(SoundType.GRASS)
.randomTicks());
}
public FeatureSaplingBlock(int light) {
super(null, FabricBlockSettings.of(Material.PLANT)
.breakByHand(true)
.collidable(false)
.luminance(light)
.instabreak()
.sound(SoundType.GRASS)
.randomTicks());
}
protected abstract Feature<?> getFeature();
@Override public FeatureSaplingBlock() {
public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) { super();
return Collections.singletonList(new ItemStack(this));
} }
public FeatureSaplingBlock(int light) {
super(light);
}
@Override @Override
public VoxelShape getShape(BlockState state, BlockGetter view, BlockPos pos, CollisionContext ePos) { public VoxelShape getShape(BlockState state, BlockGetter view, BlockPos pos, CollisionContext ePos) {
return SHAPE; return SHAPE;
} }
@Override
public BlockState updateShape(BlockState state, Direction facing, BlockState neighborState, LevelAccessor world, BlockPos pos, BlockPos neighborPos) {
if (!canSurvive(state, world, pos))
return Blocks.AIR.defaultBlockState();
else
return state;
}
@Override
public boolean isBonemealSuccess(Level world, Random random, BlockPos pos, BlockState state) {
return random.nextInt(16) == 0;
}
@Override
public void advanceTree(ServerLevel world, BlockPos pos, BlockState blockState, Random random) {
FeaturePlaceContext context = new FeaturePlaceContext(world, world.getChunkSource().getGenerator(), random, pos, null);
getFeature().place(context);
}
@Override
public void randomTick(BlockState state, ServerLevel world, BlockPos pos, Random random) {
this.tick(state, world, pos, random);
}
@Override
public void tick(BlockState state, ServerLevel world, BlockPos pos, Random random) {
super.tick(state, world, pos, random);
if (isBonemealSuccess(world, random, pos, state)) {
performBonemeal(world, random, pos, state);
}
}
@Override
public BCLRenderLayer getRenderLayer() {
return BCLRenderLayer.CUTOUT;
}
@Override
@Environment(EnvType.CLIENT)
public BlockModel getItemModel(ResourceLocation resourceLocation) {
return ModelsHelper.createBlockItem(resourceLocation);
}
@Override
@Environment(EnvType.CLIENT)
public @Nullable BlockModel getBlockModel(ResourceLocation resourceLocation, BlockState blockState) {
Optional<String> pattern = PatternsHelper.createJson(BasePatterns.BLOCK_CROSS, resourceLocation);
return ModelsHelper.fromPattern(pattern);
}
} }

View file

@ -0,0 +1,129 @@
package ru.bclib.blocks;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.minecraft.client.renderer.block.model.BlockModel;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.SaplingBlock;
import net.minecraft.world.level.block.SoundType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.levelgen.feature.Feature;
import net.minecraft.world.level.levelgen.feature.FeaturePlaceContext;
import net.minecraft.world.level.material.Material;
import net.minecraft.world.level.storage.loot.LootContext;
import org.jetbrains.annotations.Nullable;
import ru.bclib.client.models.BasePatterns;
import ru.bclib.client.models.ModelsHelper;
import ru.bclib.client.models.PatternsHelper;
import ru.bclib.client.render.BCLRenderLayer;
import ru.bclib.interfaces.BlockModelProvider;
import ru.bclib.interfaces.RenderLayerProvider;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.Random;
abstract class FeatureSaplingBlockCommon extends SaplingBlock implements RenderLayerProvider, BlockModelProvider {
public FeatureSaplingBlockCommon() {
super(
null,
FabricBlockSettings.of(Material.PLANT)
.breakByHand(true)
.collidable(false)
.instabreak()
.sound(SoundType.GRASS)
.randomTicks()
);
}
public FeatureSaplingBlockCommon(int light) {
super(
null,
FabricBlockSettings.of(Material.PLANT)
.breakByHand(true)
.collidable(false)
.luminance(light)
.instabreak()
.sound(SoundType.GRASS)
.randomTicks()
);
}
@Deprecated
/**
* Override {@link #getFeature(BlockState)} directly. Will be removed in 5.x
*/
protected Feature<?> getFeature() { return null; }
protected Feature<?> getFeature(BlockState state){
return getFeature();
}
@Override
public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) {
return Collections.singletonList(new ItemStack(this));
}
@Override
public BlockState updateShape(BlockState state, Direction facing, BlockState neighborState, LevelAccessor world, BlockPos pos, BlockPos neighborPos) {
if (!canSurvive(state, world, pos)) return Blocks.AIR.defaultBlockState();
else return state;
}
@Override
public boolean isBonemealSuccess(Level world, Random random, BlockPos pos, BlockState state) {
return random.nextInt(16) == 0;
}
@Override
public void advanceTree(ServerLevel world, BlockPos pos, BlockState blockState, Random random) {
FeaturePlaceContext context = new FeaturePlaceContext(
world,
world.getChunkSource().getGenerator(),
random,
pos,
null
);
getFeature(blockState).place(context);
}
@Override
public void randomTick(BlockState state, ServerLevel world, BlockPos pos, Random random) {
this.tick(state, world, pos, random);
}
@Override
public void tick(BlockState state, ServerLevel world, BlockPos pos, Random random) {
super.tick(state, world, pos, random);
if (isBonemealSuccess(world, random, pos, state)) {
performBonemeal(world, random, pos, state);
}
}
@Override
public BCLRenderLayer getRenderLayer() {
return BCLRenderLayer.CUTOUT;
}
@Override
@Environment(EnvType.CLIENT)
public BlockModel getItemModel(ResourceLocation resourceLocation) {
return ModelsHelper.createBlockItem(resourceLocation);
}
@Override
@Environment(EnvType.CLIENT)
public @Nullable BlockModel getBlockModel(ResourceLocation resourceLocation, BlockState blockState) {
Optional<String> pattern = PatternsHelper.createJson(BasePatterns.BLOCK_CROSS, resourceLocation);
return ModelsHelper.fromPattern(pattern);
}
}

View file

@ -0,0 +1,16 @@
package ru.bclib.blocks;
import net.minecraft.world.level.material.MaterialColor;
public class LeveledAnvilBlock extends BaseAnvilBlock{
protected final int level;
public LeveledAnvilBlock(MaterialColor color, int level) {
super(color);
this.level = level;
}
public int getCraftingLevel() {
return level;
}
}

View file

@ -4,33 +4,38 @@ import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.minecraft.world.level.block.SoundType; import net.minecraft.world.level.block.SoundType;
import net.minecraft.world.level.material.Material; import net.minecraft.world.level.material.Material;
import net.minecraft.world.level.material.MaterialColor; import net.minecraft.world.level.material.MaterialColor;
import ru.bclib.api.TagAPI;
import ru.bclib.client.render.BCLRenderLayer; import ru.bclib.client.render.BCLRenderLayer;
import ru.bclib.interfaces.IRenderTyped; import ru.bclib.interfaces.RenderLayerProvider;
public class SimpleLeavesBlock extends BaseBlockNotFull implements IRenderTyped { public class SimpleLeavesBlock extends BaseBlockNotFull implements RenderLayerProvider {
public SimpleLeavesBlock(MaterialColor color) { public SimpleLeavesBlock(MaterialColor color) {
super(FabricBlockSettings.of(Material.LEAVES) super(FabricBlockSettings.of(Material.LEAVES)
.strength(0.2F) .strength(0.2F)
.mapColor(color) .mapColor(color)
.sound(SoundType.GRASS) .sound(SoundType.GRASS)
.noOcclusion() .noOcclusion()
.isValidSpawn((state, world, pos, type) -> false) .isValidSpawn((state, world, pos, type) -> false)
.isSuffocating((state, world, pos) -> false) .isSuffocating((state, world, pos) -> false)
.isViewBlocking((state, world, pos) -> false)); .isViewBlocking((state, world, pos) -> false));
TagAPI.addTags(this, TagAPI.BLOCK_LEAVES);
} }
public SimpleLeavesBlock(MaterialColor color, int light) { public SimpleLeavesBlock(MaterialColor color, int light) {
super(FabricBlockSettings.of(Material.LEAVES) super(FabricBlockSettings.of(Material.LEAVES)
.luminance(light) .luminance(light)
.mapColor(color) .mapColor(color)
.strength(0.2F) .strength(0.2F)
.sound(SoundType.GRASS) .sound(SoundType.GRASS)
.noOcclusion() .noOcclusion()
.isValidSpawn((state, world, pos, type) -> false) .isValidSpawn((state, world, pos, type) -> false)
.isSuffocating((state, world, pos) -> false) .isSuffocating((state, world, pos) -> false)
.isViewBlocking((state, world, pos) -> false)); .isViewBlocking((state, world, pos) -> false));
}
TagAPI.addTags(this, TagAPI.BLOCK_LEAVES);
}
@Override @Override
public BCLRenderLayer getRenderLayer() { public BCLRenderLayer getRenderLayer() {
return BCLRenderLayer.CUTOUT; return BCLRenderLayer.CUTOUT;

View file

@ -1,10 +1,5 @@
package ru.bclib.blocks; package ru.bclib.blocks;
import java.util.Map;
import java.util.Optional;
import org.jetbrains.annotations.Nullable;
import net.fabricmc.api.EnvType; import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment; import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
@ -37,29 +32,36 @@ import net.minecraft.world.level.material.FluidState;
import net.minecraft.world.level.material.Fluids; import net.minecraft.world.level.material.Fluids;
import net.minecraft.world.phys.shapes.CollisionContext; import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape; import net.minecraft.world.phys.shapes.VoxelShape;
import org.jetbrains.annotations.Nullable;
import ru.bclib.client.models.BasePatterns; import ru.bclib.client.models.BasePatterns;
import ru.bclib.client.models.ModelsHelper; import ru.bclib.client.models.ModelsHelper;
import ru.bclib.client.models.PatternsHelper; import ru.bclib.client.models.PatternsHelper;
import ru.bclib.client.render.BCLRenderLayer; import ru.bclib.client.render.BCLRenderLayer;
import ru.bclib.interfaces.IRenderTyped; import ru.bclib.interfaces.RenderLayerProvider;
import java.util.Map;
import java.util.Optional;
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public class StalactiteBlock extends BaseBlockNotFull implements SimpleWaterloggedBlock, LiquidBlockContainer, IRenderTyped { public class StalactiteBlock extends BaseBlockNotFull implements SimpleWaterloggedBlock, LiquidBlockContainer, RenderLayerProvider {
public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED; public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
public static final BooleanProperty IS_FLOOR = BlockProperties.IS_FLOOR; public static final BooleanProperty IS_FLOOR = BlockProperties.IS_FLOOR;
public static final IntegerProperty SIZE = BlockProperties.SIZE; public static final IntegerProperty SIZE = BlockProperties.SIZE;
private static final VoxelShape[] SHAPES; private static final VoxelShape[] SHAPES;
public StalactiteBlock(Block source) { public StalactiteBlock(Block source) {
super(FabricBlockSettings.copy(source).noOcclusion()); super(FabricBlockSettings.copy(source).noOcclusion());
this.registerDefaultState(getStateDefinition().any().setValue(SIZE, 0).setValue(IS_FLOOR, true).setValue(WATERLOGGED, false)); this.registerDefaultState(getStateDefinition().any()
.setValue(SIZE, 0)
.setValue(IS_FLOOR, true)
.setValue(WATERLOGGED, false));
} }
@Override @Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> stateManager) { protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> stateManager) {
stateManager.add(WATERLOGGED, IS_FLOOR, SIZE); stateManager.add(WATERLOGGED, IS_FLOOR, SIZE);
} }
@Override @Override
public VoxelShape getShape(BlockState state, BlockGetter view, BlockPos pos, CollisionContext ePos) { public VoxelShape getShape(BlockState state, BlockGetter view, BlockPos pos, CollisionContext ePos) {
return SHAPES[state.getValue(SIZE)]; return SHAPES[state.getValue(SIZE)];
@ -95,7 +97,7 @@ public class StalactiteBlock extends BaseBlockNotFull implements SimpleWaterlogg
} }
} }
} }
@Override @Override
public void setPlacedBy(Level world, BlockPos pos, BlockState state, LivingEntity placer, ItemStack itemStack) { public void setPlacedBy(Level world, BlockPos pos, BlockState state, LivingEntity placer, ItemStack itemStack) {
boolean hasUp = isThis(world, pos.above()); boolean hasUp = isThis(world, pos.above());
@ -183,7 +185,7 @@ public class StalactiteBlock extends BaseBlockNotFull implements SimpleWaterlogg
} }
return state; return state;
} }
@Override @Override
public boolean canSurvive(BlockState state, LevelReader world, BlockPos pos) { public boolean canSurvive(BlockState state, LevelReader world, BlockPos pos) {
int size = state.getValue(SIZE); int size = state.getValue(SIZE);
@ -208,22 +210,24 @@ public class StalactiteBlock extends BaseBlockNotFull implements SimpleWaterlogg
Optional<String> pattern = PatternsHelper.createJson(BasePatterns.BLOCK_CROSS_SHADED, resourceLocation); Optional<String> pattern = PatternsHelper.createJson(BasePatterns.BLOCK_CROSS_SHADED, resourceLocation);
return ModelsHelper.fromPattern(pattern); return ModelsHelper.fromPattern(pattern);
} }
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public UnbakedModel getModelVariant(ResourceLocation stateId, BlockState blockState, Map<ResourceLocation, UnbakedModel> modelCache) { public UnbakedModel getModelVariant(ResourceLocation stateId, BlockState blockState, Map<ResourceLocation, UnbakedModel> modelCache) {
BlockModelRotation rotation = blockState.getValue(IS_FLOOR) ? BlockModelRotation.X0_Y0 : BlockModelRotation.X180_Y0; BlockModelRotation rotation = blockState.getValue(IS_FLOOR) ? BlockModelRotation.X0_Y0 : BlockModelRotation.X180_Y0;
ResourceLocation modelId = new ResourceLocation(stateId.getNamespace(), ResourceLocation modelId = new ResourceLocation(
stateId.getPath() + "_" + blockState.getValue(SIZE)); stateId.getNamespace(),
stateId.getPath() + "_" + blockState.getValue(SIZE)
);
registerBlockModel(modelId, modelId, blockState, modelCache); registerBlockModel(modelId, modelId, blockState, modelCache);
return ModelsHelper.createMultiVariant(modelId, rotation.getRotation(), false); return ModelsHelper.createMultiVariant(modelId, rotation.getRotation(), false);
} }
@Override @Override
public boolean canPlaceLiquid(BlockGetter world, BlockPos pos, BlockState state, Fluid fluid) { public boolean canPlaceLiquid(BlockGetter world, BlockPos pos, BlockState state, Fluid fluid) {
return false; return false;
} }
@Override @Override
public boolean placeLiquid(LevelAccessor world, BlockPos pos, BlockState state, FluidState fluidState) { public boolean placeLiquid(LevelAccessor world, BlockPos pos, BlockState state, FluidState fluidState) {
return false; return false;
@ -233,12 +237,12 @@ public class StalactiteBlock extends BaseBlockNotFull implements SimpleWaterlogg
public FluidState getFluidState(BlockState state) { public FluidState getFluidState(BlockState state) {
return state.getValue(WATERLOGGED) ? Fluids.WATER.getSource(false) : Fluids.EMPTY.defaultFluidState(); return state.getValue(WATERLOGGED) ? Fluids.WATER.getSource(false) : Fluids.EMPTY.defaultFluidState();
} }
@Override @Override
public BCLRenderLayer getRenderLayer() { public BCLRenderLayer getRenderLayer() {
return BCLRenderLayer.CUTOUT; return BCLRenderLayer.CUTOUT;
} }
static { static {
float end = 2F / 8F; float end = 2F / 8F;
float start = 5F / 8F; float start = 5F / 8F;

Some files were not shown because too many files have changed in this diff Show more