Skip to content

Commit

Permalink
Created Templates for resources instead of using strings
Browse files Browse the repository at this point in the history
  • Loading branch information
kindlich committed Nov 16, 2020
1 parent 4155584 commit 3ec7711
Show file tree
Hide file tree
Showing 25 changed files with 363 additions and 72 deletions.
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;
}
}
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);
}
}
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);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.blamejared.contenttweaker.blocks;

import com.blamejared.contenttweaker.*;
import com.blamejared.contenttweaker.api.blocks.*;
import com.blamejared.contenttweaker.api.items.*;
import com.blamejared.contenttweaker.api.resources.*;
import com.blamejared.crafttweaker.impl.util.*;
import net.minecraft.item.*;
import net.minecraft.util.*;

import javax.annotation.*;
import java.util.*;
Expand All @@ -21,9 +23,11 @@ public CoTBlockItem(IIsCoTBlock blockIn, Item.Properties builder) {
public Collection<WriteableResource> getResourcePackResources() {
final MCResourceLocation location = getMCResourceLocation();
final List<WriteableResource> out = new ArrayList<>();
out.add(new WriteableResourceImage(ImageType.ITEM, location));
out.add(new WriteableResource(ResourceType.ASSETS, FileExtension.JSON, location, "models","item").withContent(String
.format("{\n \"parent\" : \"%s:block/%s\"\n}", location.getNamespace(), location.getPath())));
out.add(WriteableResourceImage.noImage(ImageType.ITEM, location));
final WriteableResourceTemplate modelTemplate = new WriteableResourceTemplate(ResourceType.ASSETS, location, "models", "item")
.withTemplate(ResourceType.ASSETS, new ResourceLocation(ContentTweaker.MOD_ID, "models/item/item_block"))
.setLocationProperty(location);
out.add(modelTemplate);
return out;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.blamejared.contenttweaker.blocks.types.basic;

import com.blamejared.contenttweaker.*;
import com.blamejared.contenttweaker.api.blocks.*;
import com.blamejared.contenttweaker.api.items.*;
import com.blamejared.contenttweaker.api.resources.*;
Expand Down Expand Up @@ -34,15 +35,18 @@ public IIsCotItem getItem() {
public Collection<WriteableResource> getResourcePackResources() {
final MCResourceLocation location = getMCResourceLocation();
final Collection<WriteableResource> out = new ArrayList<>();
out.add(new WriteableResourceImage(ImageType.BLOCK, location));
out.add(new WriteableResource(ResourceType.ASSETS, FileExtension.JSON, location, "models", "block")
.withContent("{\n" + " \"parent\": \"block/cube_all\",\n" + " \"textures\": {\n" + " \"all\": \"%s:block/%s\"\n" + " }\n" + "}\n", location
.getNamespace(), location.getPath()));

out.add(new WriteableResource(ResourceType.ASSETS, FileExtension.JSON, location, "blockstates")
.withContent("{\n" + " \"variants\": {\n" + " \"\": {\"model\" : \"%s:block/%s\"}\n" + " }\n" + "}\n", location
.getNamespace(), location.getPath()));
out.add(WriteableResourceImage.noImage(ImageType.BLOCK, location));

final WriteableResourceTemplate modelTemplate = new WriteableResourceTemplate(ResourceType.ASSETS, location, "models", "block")
.withTemplate(ResourceType.ASSETS, new ResourceLocation(ContentTweaker.MOD_ID, "models/block/block_basic"))
.setLocationProperty(location);
out.add(modelTemplate);

final WriteableResourceTemplate blockstateTemplate = new WriteableResourceTemplate(ResourceType.ASSETS, location, "blockstates")
.withTemplate(ResourceType.ASSETS, new ResourceLocation(ContentTweaker.MOD_ID, "blockstates/block_basic"))
.setLocationProperty(location);
out.add(blockstateTemplate);

return out;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.blamejared.contenttweaker.blocks.types.horizontal;

import com.blamejared.contenttweaker.*;
import com.blamejared.contenttweaker.api.blocks.*;
import com.blamejared.contenttweaker.api.items.*;
import com.blamejared.contenttweaker.api.resources.*;
import com.blamejared.contenttweaker.blocks.*;
import com.blamejared.crafttweaker.impl.util.*;
import net.minecraft.block.*;
import net.minecraft.util.*;

import javax.annotation.*;
import java.util.*;
Expand All @@ -19,7 +21,8 @@ final class CoTBlockRotatablePillar extends RotatedPillarBlock implements IIsCoT
public CoTBlockRotatablePillar(BlockBuilderPillarRotatable blockBuilderPillarRotatable, MCResourceLocation location) {
super(blockBuilderPillarRotatable.getBlockBuilder().getBlockProperties());
this.setRegistryName(location.getInternal());
item = new CoTBlockItem(this, blockBuilderPillarRotatable.getBlockBuilder().getItemProperties());
item = new CoTBlockItem(this, blockBuilderPillarRotatable.getBlockBuilder()
.getItemProperties());
end = blockBuilderPillarRotatable.getEnd(location);
sides = blockBuilderPillarRotatable.getSides(location);
}
Expand All @@ -35,19 +38,21 @@ public IIsCotItem getItem() {
public Collection<WriteableResource> getResourcePackResources() {
final MCResourceLocation location = getMCResourceLocation();
final Collection<WriteableResource> out = new ArrayList<>();
out.add(new WriteableResourceImage(ImageType.BLOCK, end));
out.add(WriteableResourceImage.noImage(ImageType.BLOCK, end));
if(!end.equals(sides)) {
out.add(new WriteableResourceImage(ImageType.BLOCK, sides));
out.add(WriteableResourceImage.noImage(ImageType.BLOCK, sides));
}

out.add(new WriteableResource(ResourceType.ASSETS, FileExtension.JSON, location, "models/block")
.withContent("{\n" + " \"parent\": \"block/cube_column\",\n" + " \"textures\": {\n" + " \"end\": \"%s:block/%s\",\n" + " \"side\": \"%s:block/%s\"\n" + " }\n" + "}", end
.getNamespace(), end.getPath(), sides.getNamespace(), sides.getPath()));

out.add(new WriteableResource(ResourceType.ASSETS, FileExtension.JSON, location, "blockstates")
.withContent("{\n" + " \"variants\": {\n" + " \"axis=y\": { \"model\": \"%1$s:block/%2$s\" },\n" + " \"axis=z\": { \"model\": \"%1$s:block/%2$s\", \"x\": 90 },\n" + " \"axis=x\": { \"model\": \"%1$s:block/%2$s\", \"x\": 90, \"y\": 90 }\n" + " }\n" + "}", location
.getNamespace(), location.getPath()));
final WriteableResourceTemplate blockModelTemplate = new WriteableResourceTemplate(ResourceType.ASSETS, location, "models", "block")
.withTemplate(ResourceType.ASSETS, new ResourceLocation(ContentTweaker.MOD_ID, "models/block/block_rotatable_pillar"))
.setLocationProperty(end, "END")
.setLocationProperty(sides, "SIDE");
out.add(blockModelTemplate);

final WriteableResourceTemplate blockStateTemplate = new WriteableResourceTemplate(ResourceType.ASSETS, location, "blockstates")
.withTemplate(ResourceType.ASSETS, new ResourceLocation(ContentTweaker.MOD_ID, "blockstates/block_rotatable_pillar"))
.setLocationProperty(location);
out.add(blockStateTemplate);
return out;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.blamejared.contenttweaker.blocks.types.stairs;

import com.blamejared.contenttweaker.*;
import com.blamejared.contenttweaker.api.blocks.*;
import com.blamejared.contenttweaker.api.items.*;
import com.blamejared.contenttweaker.api.resources.*;
import com.blamejared.contenttweaker.blocks.*;
import com.blamejared.crafttweaker.impl.util.*;
import net.minecraft.block.*;
import net.minecraft.util.*;

import javax.annotation.*;
import java.util.*;
Expand All @@ -16,9 +18,11 @@ final class CoTStairsBlock extends StairsBlock implements IIsCoTBlock {
private final MCResourceLocation top, bottom, sides;

public CoTStairsBlock(BlockBuilderStairs blockBuilderStairs, MCResourceLocation location) {
super(Blocks.AIR::getDefaultState, blockBuilderStairs.getBlockBuilder().getBlockProperties());
super(Blocks.AIR::getDefaultState, blockBuilderStairs.getBlockBuilder()
.getBlockProperties());
this.setRegistryName(location.getInternal());
this.item = new CoTBlockItem(this, blockBuilderStairs.getBlockBuilder().getItemProperties());
this.item = new CoTBlockItem(this, blockBuilderStairs.getBlockBuilder()
.getItemProperties());
this.top = blockBuilderStairs.getTop(location);
this.bottom = blockBuilderStairs.getBottom(location);
this.sides = blockBuilderStairs.getSides(location);
Expand All @@ -36,10 +40,14 @@ public Collection<WriteableResource> getResourcePackResources() {
final MCResourceLocation location = getMCResourceLocation();
final Collection<WriteableResource> out = new ArrayList<>();
for(MCResourceLocation texture : new HashSet<>(Arrays.asList(top, bottom, sides))) {
out.add(new WriteableResourceImage(ImageType.BLOCK, texture));
out.add(WriteableResourceImage.noImage(ImageType.BLOCK, texture));
}

out.add(new WriteableResourceBlockStateStairs(location));
final WriteableResourceTemplate templateBlockState = new WriteableResourceTemplate(ResourceType.ASSETS, location, "blockstates")
.withTemplate(ResourceType.ASSETS, new ResourceLocation(ContentTweaker.MOD_ID, "blockstates/block_stairs"))
.setLocationProperty(location);
out.add(templateBlockState);

out.add(new WriteableResourceModelStairs(location, WriteableResourceModelStairs.ModelType.BASE, top, bottom, sides));
out.add(new WriteableResourceModelStairs(location, WriteableResourceModelStairs.ModelType.INNER, top, bottom, sides));
out.add(new WriteableResourceModelStairs(location, WriteableResourceModelStairs.ModelType.OUTER, top, bottom, sides));
Expand Down
Loading

0 comments on commit 3ec7711

Please sign in to comment.