7
7
import net .fabricmc .api .ModInitializer ;
8
8
import net .minecraft .client .Minecraft ;
9
9
import net .minecraft .client .entity .player .EntityPlayerSP ;
10
+ import net .minecraft .client .render .block .model .BlockModelHorizontalRotation ;
11
+ import net .minecraft .client .render .block .model .BlockModelRotatable ;
12
+ import net .minecraft .client .render .stitcher .AtlasStitcher ;
13
+ import net .minecraft .client .render .stitcher .TextureRegistry ;
10
14
import net .minecraft .core .block .Block ;
11
15
import net .minecraft .core .block .entity .TileEntity ;
12
16
import net .minecraft .core .block .tag .BlockTags ;
24
28
import org .slf4j .Logger ;
25
29
import org .slf4j .LoggerFactory ;
26
30
import sunsetsatellite .computers .packets .PacketComputers ;
27
- import turniplabs .halplibe .helper .BlockBuilder ;
28
- import turniplabs .halplibe .helper . EntityHelper ;
29
- import turniplabs .halplibe .helper . ItemHelper ;
31
+ import turniplabs .halplibe .helper .* ;
32
+ import turniplabs .halplibe .util . ClientStartEntrypoint ;
33
+ import turniplabs .halplibe .util . GameStartEntrypoint ;
30
34
import turniplabs .halplibe .util .RecipeEntrypoint ;
31
35
import turniplabs .halplibe .util .TomlConfigHandler ;
32
36
import turniplabs .halplibe .util .toml .Toml ;
33
37
34
38
import java .io .File ;
39
+ import java .io .IOException ;
35
40
import java .lang .reflect .Field ;
36
- import java .util .Arrays ;
37
- import java .util .List ;
41
+ import java .net .URI ;
42
+ import java .net .URISyntaxException ;
43
+ import java .nio .file .*;
44
+ import java .nio .file .FileSystem ;
45
+ import java .util .*;
38
46
import java .util .stream .Collectors ;
47
+ import java .util .stream .Stream ;
39
48
40
49
41
- public class Computers implements ModInitializer , RecipeEntrypoint {
50
+ public class Computers implements ModInitializer , RecipeEntrypoint , ClientStartEntrypoint {
42
51
public static final String MOD_ID = "computers" ;
43
52
public static final Logger LOGGER = LoggerFactory .getLogger (MOD_ID );
44
53
public static final TomlConfigHandler config ;
45
54
private static int availableBlockId = 1300 ;
46
55
private static int availableItemId = 17300 ;
47
56
48
- public static final RecipeNamespace COMPUTERS = new RecipeNamespace () ;
49
- public static final RecipeGroup <RecipeEntryCrafting <?,?>> WORKBENCH = new RecipeGroup <>( new RecipeSymbol ( new ItemStack ( Block . workbench ))) ;
57
+ public static RecipeNamespace COMPUTERS ;
58
+ public static RecipeGroup <RecipeEntryCrafting <?,?>> WORKBENCH ;
50
59
51
60
static {
52
61
Toml configToml = new Toml ("Computers configuration file." );
@@ -87,20 +96,22 @@ public class Computers implements ModInitializer, RecipeEntrypoint {
87
96
public static String luaFolder = "/mods/computers/lua" ;
88
97
89
98
public static final BlockComputer computer = (BlockComputer ) new BlockBuilder (MOD_ID )
90
- .setSideTextures ("computer/1.png " )
91
- .setNorthTexture ("computer/2.png " )
92
- .setTopBottomTexture ( "computer/3.png " )
99
+ .setSideTextures ("computers:block/1_computer " )
100
+ .setNorthTexture ("computers:block/2_computer " )
101
+ .setTopBottomTextures ( "computers:block/3_computer " )
93
102
.setHardness (2 )
94
103
.setResistance (2 )
104
+ .setBlockModel (BlockModelHorizontalRotation ::new )
95
105
.build (new BlockComputer ("computer" ,config .getInt ("BlockIDs.computer" )).withTags (BlockTags .MINEABLE_BY_PICKAXE ));
96
106
public static final BlockDiskDrive diskDrive = (BlockDiskDrive ) new BlockBuilder (MOD_ID )
97
- .setSideTextures ("diskdrive/1.png " )
98
- .setNorthTexture ("diskdrive/0.png " )
99
- .setTopBottomTexture ( "diskdrive/3.png " )
107
+ .setSideTextures ("computers:block/1_diskdrive " )
108
+ .setNorthTexture ("computers:block/0_diskdrive " )
109
+ .setTopBottomTextures ( "computers:block/3_diskdrive " )
100
110
.setHardness (2 )
101
111
.setResistance (2 )
112
+ .setBlockModel (BlockModelHorizontalRotation ::new )
102
113
.build (new BlockDiskDrive ("diskDrive" ,config .getInt ("BlockIDs.diskDrive" )).withTags (BlockTags .MINEABLE_BY_PICKAXE ));
103
- public static final ItemDisk disk = ( ItemDisk ) ItemHelper . createItem ( MOD_ID , new ItemDisk (config .getInt ("ItemIDs.disk" )), "computerDisk" , "floppy.png" );
114
+ public static final ItemDisk disk = new ItemBuilder ( MOD_ID ). setIcon ( "computers:item/floppy" ). build ( new ItemDisk ("computerDisk" , config .getInt ("ItemIDs.disk" )));
104
115
105
116
public static int m_tickCount ;
106
117
@@ -121,8 +132,8 @@ public void onInitialize() {
121
132
ItemToolPickaxe .miningLevels .put (computer ,1 );
122
133
ItemToolPickaxe .miningLevels .put (diskDrive ,1 );
123
134
124
- EntityHelper .Core . createTileEntity (TileEntityComputer .class ,"Computer" );
125
- EntityHelper .Core . createTileEntity (TileEntityDiskDrive .class ,"Disk Drive" );
135
+ EntityHelper .createTileEntity (TileEntityComputer .class ,"Computer" );
136
+ EntityHelper .createTileEntity (TileEntityDiskDrive .class ,"Disk Drive" );
126
137
}
127
138
128
139
public static boolean isMultiplayerClient () {
@@ -199,8 +210,104 @@ public static boolean getCursorBlink() {
199
210
200
211
@ Override
201
212
public void onRecipesReady () {
213
+ RecipeBuilder .Shaped (MOD_ID ,"XXX" ,"XYX" ,"XZX" )
214
+ .addInput ('X' ,"minecraft:stones" )
215
+ .addInput ('Y' ,Item .dustRedstone )
216
+ .addInput ('Z' ,Block .glass )
217
+ .create ("computer" ,computer .getDefaultStack ());
218
+ RecipeBuilder .Shaped (MOD_ID ,"XXX" ,"XYX" ,"XYX" )
219
+ .addInput ('X' ,"minecraft:stones" )
220
+ .addInput ('Y' ,Item .dustRedstone )
221
+ .create ("computer" ,diskDrive .getDefaultStack ());
222
+ RecipeBuilder .Shaped (MOD_ID ,"XXX" ,"XYX" ,"XYX" )
223
+ .addInput ('X' ,Item .paper )
224
+ .addInput ('Y' ,Item .dustRedstone )
225
+ .create ("computer" ,disk .getDefaultStack ());
226
+ }
227
+
228
+ @ Override
229
+ public void initNamespaces () {
230
+ COMPUTERS = new RecipeNamespace ();
231
+ WORKBENCH = new RecipeGroup <>(new RecipeSymbol (new ItemStack (Block .workbench )));
202
232
COMPUTERS .register ("workbench" ,WORKBENCH );
203
233
Registries .RECIPES .register ("computers" ,COMPUTERS );
204
- DataLoader .loadRecipes ("/assets/computers/workbench.json" );
234
+ }
235
+
236
+ //thanks kill05 ;) (and also why do i have to use this again?)
237
+ public void loadTextures (AtlasStitcher stitcher ){
238
+ // This is awful, but required until 7.2-pre2 comes ou-- nuh uh, pre2 is here and this is still needed :abyss:
239
+ String id = TextureRegistry .stitcherMap .entrySet ().stream ().filter ((e )->e .getValue () == stitcher ).map (Map .Entry ::getKey ).collect (Collectors .toSet ()).stream ().findFirst ().orElse (null );
240
+ if (id == null ){
241
+ throw new RuntimeException ("Failed to load textures: invalid atlas provided!" );
242
+ }
243
+ LOGGER .info ("Loading " +id +" textures..." );
244
+ long start = System .currentTimeMillis ();
245
+
246
+ String path = String .format ("%s/%s/%s" , "/assets" , MOD_ID , stitcher .directoryPath );
247
+ URI uri ;
248
+ try {
249
+ uri = DataLoader .class .getResource (path ).toURI ();
250
+ } catch (URISyntaxException e ) {
251
+ throw new RuntimeException (e );
252
+ }
253
+ FileSystem fileSystem = null ;
254
+ Path myPath ;
255
+ if (uri .getScheme ().equals ("jar" )) {
256
+ try {
257
+ fileSystem = FileSystems .newFileSystem (uri , Collections .emptyMap ());
258
+ } catch (IOException e ) {
259
+ throw new RuntimeException (e );
260
+ }
261
+ myPath = fileSystem .getPath (path );
262
+ } else {
263
+ myPath = Paths .get (uri );
264
+ }
265
+
266
+ Stream <Path > walk ;
267
+ try {
268
+ walk = Files .walk (myPath , Integer .MAX_VALUE );
269
+ } catch (IOException e ) {
270
+ throw new RuntimeException (e );
271
+ }
272
+ Iterator <Path > it = walk .iterator ();
273
+
274
+ while (it .hasNext ()) {
275
+ Path file = it .next ();
276
+ String name = file .getFileName ().toString ();
277
+ if (name .endsWith (".png" )) {
278
+ String path1 = file .toString ().replace (file .getFileSystem ().getSeparator (), "/" );
279
+ String cutPath = path1 .split (path )[1 ];
280
+ cutPath = cutPath .substring (0 , cutPath .length () - 4 );
281
+ TextureRegistry .getTexture (MOD_ID + ":" + id + cutPath );
282
+ }
283
+ }
284
+
285
+ walk .close ();
286
+ if (fileSystem != null ) {
287
+ try {
288
+ fileSystem .close ();
289
+ } catch (IOException e ) {
290
+ throw new RuntimeException (e );
291
+ }
292
+ }
293
+
294
+ try {
295
+ TextureRegistry .initializeAllFiles (MOD_ID , stitcher );
296
+ } catch (URISyntaxException | IOException e ) {
297
+ throw new RuntimeException ("Failed to load textures." , e );
298
+ }
299
+ LOGGER .info (String .format ("Loaded " +id +" textures (took %sms)." , System .currentTimeMillis () - start ));
300
+ }
301
+
302
+ @ Override
303
+ public void beforeClientStart () {
304
+
305
+ }
306
+
307
+ @ Override
308
+ public void afterClientStart () {
309
+ loadTextures (TextureRegistry .blockAtlas );
310
+ loadTextures (TextureRegistry .itemAtlas );
311
+ Minecraft .getMinecraft (Minecraft .class ).renderEngine .refreshTextures (new ArrayList <>());
205
312
}
206
313
}
0 commit comments