-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created Templates for resources instead of using strings
- Loading branch information
Showing
25 changed files
with
363 additions
and
72 deletions.
There are no files selected for viewing
41 changes: 36 additions & 5 deletions
41
src/main/java/com/blamejared/contenttweaker/api/resources/WriteableResourceImage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,52 @@ | ||
package com.blamejared.contenttweaker.api.resources; | ||
|
||
import com.blamejared.contenttweaker.*; | ||
import com.blamejared.crafttweaker.impl.util.*; | ||
import it.unimi.dsi.fastutil.bytes.*; | ||
import net.minecraft.util.*; | ||
|
||
import java.io.*; | ||
import java.util.*; | ||
|
||
public class WriteableResourceImage extends WriteableResource { | ||
|
||
private static final byte[] NO_ICON = Base64.getDecoder() | ||
.decode("iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAACgSURBVDhPhZLRCYAwDAWD3/lwCJGuILj/Kg7gAPE10dK0IR4Fo96BLZIwy3nKfUsOBGjMJKUI0U9jNrRSSK7rp+lsyFQfJY238UADEDaTDb4ADE1kgy4ArTmOuiYb+ADg9b5XFQuDt8FCAzjpdX1nDLgdeEOjffe21YWhPwOlC4Zdtv345gvCM4kaDULbmBrKbMM3eklso2v0985twxrmBzQGNQUhq3LZAAAAAElFTkSuQmCC"); | ||
|
||
public WriteableResourceImage(ImageType imageType, MCResourceLocation location) { | ||
super(ResourceType.ASSETS, FileExtension.PNG, location, "textures", imageType.getFolderName()); | ||
this.withContent(NO_ICON); | ||
} | ||
|
||
public WriteableResourceImage(MCResourceLocation location) { | ||
super(ResourceType.ASSETS, FileExtension.PNG, location, "textures"); | ||
this.withContent(NO_ICON); | ||
} | ||
|
||
/** | ||
* Creates the default image (red X on white background) for the given location/type | ||
*/ | ||
public static WriteableResourceImage noImage(ImageType imageType, MCResourceLocation location) { | ||
return new WriteableResourceImage(imageType, location).setImageToCopy(new ResourceLocation(ContentTweaker.MOD_ID, "textures/generic/no_image")); | ||
} | ||
|
||
public WriteableResourceImage setImageToCopy(ResourceLocation location) { | ||
final String format = "/assets/%s/templates/%s.png"; | ||
final String path = String.format(format, location.getNamespace(), location.getPath()); | ||
final InputStream resourceAsStream = WriteableResourceImage.class.getResourceAsStream(path); | ||
if(resourceAsStream == null) { | ||
System.err.println("Invalid Template resource: " + location); | ||
} | ||
|
||
final ByteArrayList out = new ByteArrayList(); | ||
//Try-with resources to make sure stream is closed properly | ||
try (final InputStream stream = resourceAsStream){ | ||
do { | ||
int b = stream.read(); | ||
if(b == -1) { | ||
break; | ||
} | ||
out.add((byte) b); | ||
} while(true); | ||
} catch(IOException ignored) { | ||
//TODO error | ||
} | ||
this.withContent(out.toByteArray()); | ||
return this; | ||
} | ||
} |
16 changes: 9 additions & 7 deletions
16
...main/java/com/blamejared/contenttweaker/api/resources/WriteableResourceLootTableItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,19 @@ | ||
package com.blamejared.contenttweaker.api.resources; | ||
|
||
import com.blamejared.contenttweaker.*; | ||
import com.blamejared.crafttweaker.impl.util.*; | ||
import net.minecraft.util.*; | ||
|
||
public class WriteableResourceLootTableItem extends WriteableResource { | ||
public class WriteableResourceLootTableItem extends WriteableResourceTemplate { | ||
|
||
public WriteableResourceLootTableItem(MCResourceLocation block) { | ||
super(ResourceType.DATA, FileExtension.JSON, block, "loot_tables", "blocks"); | ||
this.withContent("{\n" + " \"type\": \"minecraft:block\",\n" + " \"pools\": [\n" + " {\n" + " \"rolls\": 1,\n" + " \"entries\": [\n" + " {\n" + " \"type\": \"minecraft:item\",\n" + " \"name\": \"%s\"\n" + " }\n" + " ],\n" + " \"conditions\": [\n" + " {\n" + " \"condition\": \"minecraft:survives_explosion\"\n" + " }\n" + " ]\n" + " }\n" + " ]\n" + "}", block | ||
.getInternal() | ||
.toString()); | ||
this(block, "block_basic"); | ||
} | ||
|
||
@Override | ||
public void onWrite() { | ||
public WriteableResourceLootTableItem(MCResourceLocation block, String name) { | ||
super(ResourceType.DATA, block, "loot_tables", "blocks"); | ||
|
||
final ResourceLocation location = new ResourceLocation(ContentTweaker.MOD_ID, "loot_tables/blocks/" + name); | ||
this.withTemplate(ResourceType.DATA, location).setLocationProperty(block); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/com/blamejared/contenttweaker/api/resources/WriteableResourceTemplate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.blamejared.contenttweaker.api.resources; | ||
|
||
import com.blamejared.contenttweaker.file_handling.templates.*; | ||
import com.blamejared.crafttweaker.impl.util.*; | ||
import net.minecraft.util.*; | ||
|
||
public class WriteableResourceTemplate extends WriteableResource { | ||
|
||
private TemplateFile templateFile; | ||
|
||
public WriteableResourceTemplate(ResourceType type, MCResourceLocation location, String... prefixes) { | ||
super(type, FileExtension.JSON, location, prefixes); | ||
this.contentSupplier = this::getContentArray; | ||
} | ||
|
||
public WriteableResourceTemplate(ResourceType type, String namespace, String path, String... prefixes) { | ||
this(type, new MCResourceLocation(namespace, path), prefixes); | ||
} | ||
|
||
private byte[] getContentArray() { | ||
return templateFile.getContentArray(); | ||
} | ||
|
||
public WriteableResourceTemplate withTemplate(ResourceType type, ResourceLocation location) { | ||
this.templateFile = TemplateFile.of(type, location); | ||
return this; | ||
} | ||
|
||
public WriteableResourceTemplate setProperty(String key, String value) { | ||
templateFile.setValue(key, value); | ||
return this; | ||
} | ||
|
||
/** | ||
* Utility method that sets both NAMESPACE and PATH at once | ||
*/ | ||
public WriteableResourceTemplate setLocationProperty(MCResourceLocation location) { | ||
return setProperty("NAMESPACE", location.getNamespace()).setProperty("PATH", location.getPath()); | ||
} | ||
|
||
public WriteableResourceTemplate setLocationProperty(MCResourceLocation location, String suffix) { | ||
final String namespace = location.getNamespace(); | ||
final String path = location.getPath(); | ||
return setProperty("NAMESPACE" + "_" + suffix, namespace).setProperty("PATH" + "_" + suffix, path); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.