Skip to content

Instantly share code, notes, and snippets.

@kindlich
Last active November 11, 2018 19:52
Show Gist options
  • Save kindlich/a5743a1fa8c2b1d9b285b91c4987ed40 to your computer and use it in GitHub Desktop.
Save kindlich/a5743a1fa8c2b1d9b285b91c4987ed40 to your computer and use it in GitHub Desktop.
import crafttweaker.item.IIngredient;
import crafttweaker.item.IItemStack;
import crafttweaker.block.IBlockState;
import crafttweaker.block.IBlockStateMatcher;
import crafttweaker.world.IBlockPos;
import crafttweaker.event.PlayerInteractBlockEvent;
import crafttweaker.player.IPlayer;
zenClass InWorldCraftingBlock {
var blockPos as IBlockPos;
var blockState as IBlockStateMatcher;
var transformedState as IBlockState;
zenConstructor(blockPos as IBlockPos, blockState as IBlockStateMatcher) {
this.blockPos = blockPos;
this.blockState = blockState;
this.transformedState = null;
}
zenConstructor(blockPos as IBlockPos, blockState as IBlockStateMatcher, transformedState as IBlockState) {
this.blockPos = blockPos;
this.blockState = blockState;
this.transformedState = transformedState;
}
function setTransformedState(transformedState as IBlockState) as InWorldCraftingBlock {
this.transformedState = transformedState;
return this;
}
function getTransformedState(stateIn as IBlockState) as IBlockState {
return isNull(transformedState) ? stateIn : transformedState;
}
}
zenClass InWorldCraftingRecipe {
var inputBlock as IBlockStateMatcher;
var outputBlock as IBlockStateMatcher;
var inputItem as IIngredient;
var outputItem as IItemStack;
zenConstructor(inputBlock as IBlockStateMatcher, inputItem as IIngredient, outputItem as IItemStack) {
this.inputBlock = inputBlock;
this.inputItem = inputItem;
this.outputBlock = <blockstate:minecraft:air>;
this.outputItem = outputItem;
}
function hasOutput() as bool {
return !isNull(this.outputItem) || this.inputItem.hasNewTransformers;
}
function matches(inputBlock as IBlockState, inputItem as IItemStack) as bool {
return (!isNull(inputBlock) && (this.inputBlock.matches(inputBlock))
&& ((isNull(this.inputItem) && isNull(inputItem)) || (this.inputItem has inputItem)));
}
function applyTransform(inputItem as IItemStack) as IItemStack{
return this.inputItem.hasNewTransformers ? this.inputItem.applyNewTransform(inputItem) : null;
}
}
zenClass InWorldCrafting {
var craftingBlocks as [InWorldCraftingBlock];
var recipes as [InWorldCraftingRecipe];
zenConstructor() {
this.craftingBlocks = [];
this.recipes = [];
}
function addCraftingBlock(craftingBlock as InWorldCraftingBlock) as InWorldCrafting {
this.craftingBlocks += craftingBlock;
return this;
}
function addCraftingRecipe(craftingRecipe as InWorldCraftingRecipe) as InWorldCrafting {
this.recipes += craftingRecipe;
return this;
}
function canPlayerCraft(player as IPlayer) as bool {
//TODO add player restrictions like GameStages
return true;
}
function addBlockPositions(a as IBlockPos, b as IBlockPos) as IBlockPos{
return IBlockPos.create(a.x + b.x, a.y + b.y, a.z + b.z);
}
function test(player as IPlayer, state as IBlockState, pos as IBlockPos, item as IItemStack) as bool {
val world = player.world;
for craftingBlock in craftingBlocks {
val checkPos = addBlockPositions(pos, craftingBlock.blockPos);
if (! (craftingBlock.blockState.matches(world.getBlockState(checkPos)))) {
return false;
}
}
//Setup correct (blocks correctly placed)
for recipe in recipes {
if(recipe.matches(state, item)) {
val returnedItem = recipe.outputItem;
val transformedItem = recipe.applyTransform(item);
if(!isNull(returnedItem)) player.give(returnedItem);
if(!isNull(transformedItem)) player.give(transformedItem);
for craftingBlock in craftingBlocks {
val checkPos = addBlockPositions(pos, craftingBlock.blockPos);
val originalState = world.getBlockState(checkPos);
val transformedState = craftingBlock.getTransformedState(originalState);
if(!isNull(transformedState) && transformedState != originalState)
world.setBlockState(transformedState, checkPos);
}
return true;
}
}
return false;
}
}
static inWorldRecipes as [InWorldCrafting] = [
InWorldCrafting()
.addCraftingRecipe(InWorldCraftingRecipe(<blockstate:minecraft:planks>, <minecraft:iron_sword>, <minecraft:wooden_sword>)),
InWorldCrafting()
.addCraftingBlock(InWorldCraftingBlock(IBlockPos.create(1, 0, 0), <blockState:minecraft:stone>))
.addCraftingRecipe(InWorldCraftingRecipe(<blockstate:minecraft:stone>, <minecraft:stick>, <minecraft:golden_sword>))
];
events.onPlayerInteractBlock(function(event as PlayerInteractBlockEvent){
var player = event.player;
var heldItem = player.currentItem;
for inWorldRecipe in inWorldRecipes {
if(inWorldRecipe.test(player, event.blockState, event.position, heldItem)) {
player.world.setBlockState(<blockState:minecraft:air>, event.position);
if(!isNull(heldItem))
player.setItemToSlot(mainHand, heldItem.amount > 1 ? heldItem.withAmount(heldItem.amount - 1) : null);
break;
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment