Color lens time.

This commit is contained in:
Flanks255 2022-08-17 10:29:31 -05:00
parent 8d62396155
commit 904c408c94
6 changed files with 13 additions and 110 deletions

View file

@ -44,8 +44,6 @@ public final class ActuallyAdditionsAPI {
// public static final List<TreasureChestLoot> TREASURE_CHEST_LOOT = new ArrayList<>(); // public static final List<TreasureChestLoot> TREASURE_CHEST_LOOT = new ArrayList<>();
//public static final List<LensConversionRecipe> RECONSTRUCTOR_LENS_CONVERSION_RECIPES = new ArrayList<>(); //public static final List<LensConversionRecipe> RECONSTRUCTOR_LENS_CONVERSION_RECIPES = new ArrayList<>();
public static final List<EmpowererRecipe> EMPOWERER_RECIPES = new ArrayList<>(); public static final List<EmpowererRecipe> EMPOWERER_RECIPES = new ArrayList<>();
public static final Map<Item, IColorLensChanger> RECONSTRUCTOR_LENS_COLOR_CHANGERS = new HashMap<>();
public static final List<ColorChangeRecipe> COLOR_CHANGE_RECIPES = new ArrayList<>(); public static final List<ColorChangeRecipe> COLOR_CHANGE_RECIPES = new ArrayList<>();
public static final List<SolidFuelRecipe> SOLID_FUEL_RECIPES = new ArrayList<>(); public static final List<SolidFuelRecipe> SOLID_FUEL_RECIPES = new ArrayList<>();
public static final List<LiquidFuelRecipe> LIQUID_FUEL_RECIPES = new ArrayList<>(); public static final List<LiquidFuelRecipe> LIQUID_FUEL_RECIPES = new ArrayList<>();
@ -267,20 +265,6 @@ public final class ActuallyAdditionsAPI {
public static void addReconstructorLensConversionRecipe(Ingredient input, ItemStack output, int energyUse) { public static void addReconstructorLensConversionRecipe(Ingredient input, ItemStack output, int energyUse) {
//addReconstructorLensConversionRecipe(input, output, energyUse, lensDefaultConversion); //addReconstructorLensConversionRecipe(input, output, energyUse, lensDefaultConversion);
} }
/**
* Adds an item and the way it is modified to the Atomic Reconstructor's color lens.
* This also works for blocks, but they have to be in their item form.
* The way it is modified is an instance of IColorLensChanger. When modifying the item,
* its modifyItem() method will be called with a stack containing the item.
*
* @param item The item (or block's item) to add
* @param changer The change mechanism
*/
public static void addReconstructorLensColorChangeItem(Item item, IColorLensChanger changer) {
RECONSTRUCTOR_LENS_COLOR_CHANGERS.put(item, changer);
}
/** /**
* Adds an ingredient to the Coffee Machine ingredient list * Adds an ingredient to the Coffee Machine ingredient list
* *

View file

@ -1,31 +0,0 @@
/*
* This file ("ColorLensChangerByDyeMeta.java") is part of the Actually Additions mod for Minecraft.
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
* http://ellpeck.de/actaddlicense
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
* © 2015-2017 Ellpeck
*/
package de.ellpeck.actuallyadditions.api.recipe;
import de.ellpeck.actuallyadditions.api.internal.IAtomicReconstructor;
import net.minecraft.block.BlockState;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos;
/**
* Changes an item's color by changing its metadata.
* Much like dye and wool, 0 is white and 15 is black and it will cycle around.
*/
public class ColorLensChangerByDyeMeta implements IColorLensChanger {
@Override
public ItemStack modifyItem(ItemStack stack, BlockState hitBlockState, BlockPos hitBlock, IAtomicReconstructor tile) {
ItemStack newStack = stack.copy();
int meta = newStack.getDamageValue();
newStack.setDamageValue((meta + 1) % 16);
return newStack;
}
}

View file

@ -1,41 +0,0 @@
/*
* This file ("IColorLensChanger.java") is part of the Actually Additions mod for Minecraft.
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
* http://ellpeck.de/actaddlicense
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
* © 2015-2017 Ellpeck
*/
package de.ellpeck.actuallyadditions.api.recipe;
import de.ellpeck.actuallyadditions.api.internal.IAtomicReconstructor;
import net.minecraft.block.BlockState;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos;
/**
* Used for the Atomic Reconstructor's Color Lens changing algorythm.
* When registering a new item to be changed, it needs an IColorLensChanger which
* is the method with which the item will be changed.
* <p>
* See ColorLensChangerByDyeMeta for reference.
*/
public interface IColorLensChanger {
/**
* Modifies the given item.
* Will only be called with stacks containing items that are registered with
* this IColorLensChanger.
*
* @param stack the stack to modify
* @param hitBlockState The state of the block that was hit
* @param hitBlock the block that was hit (usually air, or the block that is also in the stack)
* @param tile the Reconstructor doing the color conversion
*
* @return the modified stack. Please make sure to return a modified COPY of the input stack.
*/
ItemStack modifyItem(ItemStack stack, BlockState hitBlockState, BlockPos hitBlock, IAtomicReconstructor tile);
}

View file

@ -1,6 +1,7 @@
package de.ellpeck.actuallyadditions.mod.crafting; package de.ellpeck.actuallyadditions.mod.crafting;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import de.ellpeck.actuallyadditions.api.ActuallyAdditionsAPI;
import net.minecraft.data.IFinishedRecipe; import net.minecraft.data.IFinishedRecipe;
import net.minecraft.inventory.IInventory; import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
@ -13,11 +14,11 @@ import net.minecraft.util.IItemProvider;
import net.minecraft.util.JSONUtils; import net.minecraft.util.JSONUtils;
import net.minecraft.util.ResourceLocation; import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.ForgeRegistryEntry; import net.minecraftforge.registries.ForgeRegistryEntry;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.Optional;
public class ColorChangeRecipe implements IRecipe<IInventory> { public class ColorChangeRecipe implements IRecipe<IInventory> {
public static final String NAME = "color_change"; public static final String NAME = "color_change";
@ -52,6 +53,10 @@ public class ColorChangeRecipe implements IRecipe<IInventory> {
return false; return false;
} }
public static Optional<ColorChangeRecipe> getRecipeForStack(ItemStack stack) {
return ActuallyAdditionsAPI.COLOR_CHANGE_RECIPES.stream().filter(recipe -> recipe.matches(stack)).findFirst();
}
@Nonnull @Nonnull
@Override @Override
public ItemStack getResultItem() { public ItemStack getResultItem() {

View file

@ -10,23 +10,20 @@
package de.ellpeck.actuallyadditions.mod.items.lens; package de.ellpeck.actuallyadditions.mod.items.lens;
import de.ellpeck.actuallyadditions.api.ActuallyAdditionsAPI;
import de.ellpeck.actuallyadditions.api.internal.IAtomicReconstructor; import de.ellpeck.actuallyadditions.api.internal.IAtomicReconstructor;
import de.ellpeck.actuallyadditions.api.lens.Lens; import de.ellpeck.actuallyadditions.api.lens.Lens;
import de.ellpeck.actuallyadditions.api.recipe.IColorLensChanger; import de.ellpeck.actuallyadditions.mod.crafting.ColorChangeRecipe;
import de.ellpeck.actuallyadditions.mod.util.StackUtil; import de.ellpeck.actuallyadditions.mod.util.StackUtil;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.entity.item.ItemEntity; import net.minecraft.entity.item.ItemEntity;
import net.minecraft.item.BlockItem; import net.minecraft.item.BlockItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.util.Direction; import net.minecraft.util.Direction;
import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Random; import java.util.Random;
public class LensColor extends Lens { public class LensColor extends Lens {
@ -56,12 +53,10 @@ public class LensColor extends Lens {
if (tile.getEnergy() >= ENERGY_USE) { if (tile.getEnergy() >= ENERGY_USE) {
BlockState state = tile.getWorldObject().getBlockState(hitBlock); BlockState state = tile.getWorldObject().getBlockState(hitBlock);
Block block = state.getBlock(); Block block = state.getBlock();
// int meta = block.getMetaFromState(state); ItemStack returnStack = this.tryConvert(new ItemStack(block));
ItemStack returnStack = this.tryConvert(new ItemStack(block), hitState, hitBlock, tile); if (!returnStack.isEmpty() && returnStack.getItem() instanceof BlockItem) {
if (returnStack != null && returnStack.getItem() instanceof BlockItem) {
Block toPlace = Block.byItem(returnStack.getItem()); Block toPlace = Block.byItem(returnStack.getItem());
BlockState state2Place = toPlace.defaultBlockState(); BlockState state2Place = toPlace.defaultBlockState();
//getStateForPlacement(tile.getWorldObject(), hitBlock, Direction.UP, 0, 0, 0, FakePlayerFactory.getMinecraft((ServerWorld) tile.getWorldObject()), Hand.MAIN_HAND); //TODO
tile.getWorldObject().setBlock(hitBlock, state2Place, 2); tile.getWorldObject().setBlock(hitBlock, state2Place, 2);
tile.extractEnergy(ENERGY_USE); tile.extractEnergy(ENERGY_USE);
} }
@ -69,8 +64,8 @@ public class LensColor extends Lens {
List<ItemEntity> items = tile.getWorldObject().getEntitiesOfClass(ItemEntity.class, new AxisAlignedBB(hitBlock.getX(), hitBlock.getY(), hitBlock.getZ(), hitBlock.getX() + 1, hitBlock.getY() + 1, hitBlock.getZ() + 1)); List<ItemEntity> items = tile.getWorldObject().getEntitiesOfClass(ItemEntity.class, new AxisAlignedBB(hitBlock.getX(), hitBlock.getY(), hitBlock.getZ(), hitBlock.getX() + 1, hitBlock.getY() + 1, hitBlock.getZ() + 1));
for (ItemEntity item : items) { for (ItemEntity item : items) {
if (item.isAlive() && StackUtil.isValid(item.getItem()) && tile.getEnergy() >= ENERGY_USE) { if (item.isAlive() && !item.getItem().isEmpty() && tile.getEnergy() >= ENERGY_USE) {
ItemStack newStack = this.tryConvert(item.getItem(), hitState, hitBlock, tile); ItemStack newStack = this.tryConvert(item.getItem());
if (StackUtil.isValid(newStack)) { if (StackUtil.isValid(newStack)) {
item.remove(); item.remove();
@ -85,16 +80,8 @@ public class LensColor extends Lens {
return false; return false;
} }
private ItemStack tryConvert(ItemStack stack, BlockState hitState, BlockPos hitBlock, IAtomicReconstructor tile) { private ItemStack tryConvert(ItemStack stack) {
if (StackUtil.isValid(stack)) { return ColorChangeRecipe.getRecipeForStack(stack).map(ColorChangeRecipe::getResultItem).orElse(ItemStack.EMPTY);
Item item = stack.getItem();
for (Map.Entry<Item, IColorLensChanger> changer : ActuallyAdditionsAPI.RECONSTRUCTOR_LENS_COLOR_CHANGERS.entrySet()) {
if (item == changer.getKey()) {
return changer.getValue().modifyItem(stack, hitState, hitBlock, tile);
}
}
}
return ItemStack.EMPTY;
} }
@Override @Override

View file

@ -162,7 +162,6 @@ public class MethodHandler implements IMethodHandler {
ItemStack output = recipe.get().getResultItem().copy(); ItemStack output = recipe.get().getResultItem().copy();
if (!output.isEmpty()) { if (!output.isEmpty()) {
tile.getWorldObject().levelEvent(2001, pos, Block.getId(state)); tile.getWorldObject().levelEvent(2001, pos, Block.getId(state));
//recipe.transformHook(ItemStack.EMPTY, state, pos, tile); //TODO empty method
if (output.getItem() instanceof BlockItem) { if (output.getItem() instanceof BlockItem) {
Block toPlace = Block.byItem(output.getItem()); Block toPlace = Block.byItem(output.getItem());
BlockState state2Place = toPlace.defaultBlockState(); //.getStateForPlacement(tile.getWorldObject(), pos, facing, 0, 0, 0, output.getMetadata(), FakePlayerFactory.getMinecraft((WorldServer) tile.getWorldObject()), Hand.MAIN_HAND); //TODO BlockState state2Place = toPlace.defaultBlockState(); //.getStateForPlacement(tile.getWorldObject(), pos, facing, 0, 0, 0, output.getMetadata(), FakePlayerFactory.getMinecraft((WorldServer) tile.getWorldObject()), Hand.MAIN_HAND); //TODO