mirror of
https://github.com/RoBaertschi/EnvironmentTech.git
synced 2025-04-19 23:03:28 +00:00
added texture for env collector
added blockstate and model datagen for env collector implemented most logic for env collector
This commit is contained in:
parent
8fdca464a6
commit
79f3210e23
@ -0,0 +1,3 @@
|
||||
// 1.20.6 2024-06-04T16:17:04.985464 Block States: environmenttech
|
||||
1e105612311484ccb5c962a0d36f211c56dd86df assets/environmenttech/blockstates/env_collector.json
|
||||
42508b5c76d779fdb052a9e72a5377745684f5da assets/environmenttech/models/block/env_collector_main.json
|
@ -0,0 +1,9 @@
|
||||
{
|
||||
"multipart": [
|
||||
{
|
||||
"apply": {
|
||||
"model": "environmenttech:block/env_collector_main"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
{
|
||||
"parent": "minecraft:block/cube",
|
||||
"elements": [
|
||||
{
|
||||
"faces": {
|
||||
"down": {
|
||||
"texture": "#txt"
|
||||
},
|
||||
"east": {
|
||||
"texture": "#txt"
|
||||
},
|
||||
"north": {
|
||||
"texture": "#txt"
|
||||
},
|
||||
"south": {
|
||||
"texture": "#txt"
|
||||
},
|
||||
"up": {
|
||||
"texture": "#txt"
|
||||
},
|
||||
"west": {
|
||||
"texture": "#txt"
|
||||
}
|
||||
},
|
||||
"from": [
|
||||
0,
|
||||
0,
|
||||
0
|
||||
],
|
||||
"to": [
|
||||
16,
|
||||
16,
|
||||
16
|
||||
]
|
||||
}
|
||||
],
|
||||
"render_type": "minecraft:solid",
|
||||
"textures": {
|
||||
"particle": "environmenttech:block/env_collector_main",
|
||||
"txt": "environmenttech:block/env_collector_main"
|
||||
}
|
||||
}
|
@ -12,8 +12,8 @@ import java.util.function.Supplier;
|
||||
public class ETAttachments {
|
||||
private static final DeferredRegister<AttachmentType<?>> ATTACHMENT_TYPES = DeferredRegister.create(NeoForgeRegistries.ATTACHMENT_TYPES, EnvironmentTech.MODID);
|
||||
|
||||
public static final Supplier<AttachmentType<Integer>> ENV = ATTACHMENT_TYPES.register(
|
||||
"env", () -> AttachmentType.builder(() -> 0).serialize(Codec.INT).build()
|
||||
public static final Supplier<AttachmentType<Long>> ENV = ATTACHMENT_TYPES.register(
|
||||
"env", () -> AttachmentType.builder(() -> 0L).serialize(Codec.LONG).build()
|
||||
);
|
||||
|
||||
public static void init(IEventBus eventBus) {
|
||||
|
@ -8,10 +8,10 @@ import robaertschi.environmenttech.EnvironmentTech;
|
||||
import robaertschi.environmenttech.level.block.entity.ETBlockEntities;
|
||||
|
||||
public class ETCapabilities {
|
||||
public static final BlockCapability<IEnvStorage, EnvCapabilityContext> ENV_STORAGE_BLOCK =
|
||||
public static final BlockCapability<IEnvStorage, EnvType> ENV_STORAGE_BLOCK =
|
||||
BlockCapability.create(EnvironmentTech.id("env_storage"),
|
||||
IEnvStorage.class,
|
||||
EnvCapabilityContext.class
|
||||
EnvType.class
|
||||
);
|
||||
|
||||
public static void init(IEventBus iEventBus) {
|
||||
@ -28,7 +28,10 @@ public class ETCapabilities {
|
||||
event.registerBlockEntity(
|
||||
ENV_STORAGE_BLOCK,
|
||||
ETBlockEntities.ENV_COLLECTOR_BLOCK_ENTITY.get(),
|
||||
(object, context) -> object.getEnvStorage()
|
||||
(object, context) -> {
|
||||
if (context == EnvType.Chunk) return object.getEnvStorage();
|
||||
return null;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -18,6 +18,7 @@ import net.minecraft.world.item.crafting.Recipe;
|
||||
import net.minecraft.world.item.crafting.RecipeSerializer;
|
||||
import net.minecraft.world.item.crafting.RecipeType;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.neoforged.neoforge.items.IItemHandler;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@ -30,6 +31,10 @@ public record EnvCollectorRecipe(Ingredient input, ItemStack output, int envUsed
|
||||
return this.input.test(pContainer.getItem(0));
|
||||
}
|
||||
|
||||
public boolean matches(@NotNull IItemHandler iItemHandler, @NotNull Level level) {
|
||||
return this.input.test(iItemHandler.getStackInSlot(0));
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ItemStack assemble(@NotNull Container pCraftingContainer, HolderLookup.@NotNull Provider pRegistries) {
|
||||
return this.output.copy();
|
||||
|
@ -0,0 +1,49 @@
|
||||
package robaertschi.environmenttech.datagen;
|
||||
|
||||
import net.minecraft.data.PackOutput;
|
||||
import net.minecraft.world.level.block.Block;
|
||||
import net.neoforged.neoforge.client.model.generators.BlockModelBuilder;
|
||||
import net.neoforged.neoforge.client.model.generators.BlockStateProvider;
|
||||
import net.neoforged.neoforge.client.model.generators.MultiPartBlockStateBuilder;
|
||||
import net.neoforged.neoforge.common.data.ExistingFileHelper;
|
||||
import robaertschi.environmenttech.level.block.ETBlocks;
|
||||
|
||||
import static robaertschi.environmenttech.EnvironmentTech.MODID;
|
||||
|
||||
public class ETBlockStateProvider extends BlockStateProvider {
|
||||
public ETBlockStateProvider(PackOutput output, ExistingFileHelper exFileHelper) {
|
||||
super(output, MODID, exFileHelper);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void registerStatesAndModels() {
|
||||
registerEnvCollector();
|
||||
}
|
||||
|
||||
private void registerEnvCollector() {
|
||||
var envCollector = ETBlocks.ENV_COLLECTOR_BLOCK;
|
||||
String path = "env_collector";
|
||||
|
||||
BlockModelBuilder base = models().getBuilder("block/" + path + "_main");
|
||||
base.parent(models().getExistingFile(mcLoc("cube")));
|
||||
|
||||
base.element()
|
||||
.from(0f, 0f, 0f)
|
||||
.to(16f, 16f, 16f)
|
||||
.allFaces((direction, faceBuilder) -> faceBuilder.texture("#txt"))
|
||||
.end();
|
||||
|
||||
base.texture("txt", modLoc("block/env_collector_main"));
|
||||
base.texture("particle", modLoc("block/env_collector_main"));
|
||||
|
||||
base.renderType("solid");
|
||||
|
||||
createEnvCollectorModel(envCollector.get(), path, base);
|
||||
}
|
||||
|
||||
private void createEnvCollectorModel(Block block, String path, BlockModelBuilder frame) {
|
||||
MultiPartBlockStateBuilder bld = getMultipartBuilder(block);
|
||||
|
||||
bld.part().modelFile(frame).addModel().end();
|
||||
}
|
||||
}
|
@ -25,5 +25,10 @@ public class ETDatagen {
|
||||
event.includeServer(),
|
||||
new ETRecipeProvider(output, lookupProvider)
|
||||
);
|
||||
|
||||
generator.addProvider(
|
||||
event.includeClient(),
|
||||
new ETBlockStateProvider(output, existingFileHelper)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -19,12 +19,12 @@ public class ETChunkEvents {
|
||||
return;
|
||||
}
|
||||
int random = event.getLevel().getRandom().nextIntBetweenInclusive(Config.minEnvForNewChunk, Config.maxEnvForNewChunk);
|
||||
EnvironmentTech.LOGGER.debug("New Chunk, set random to {}", random);
|
||||
event.getChunk().setData(ETAttachments.ENV, random);
|
||||
EnvironmentTech.LOGGER.debug("New Chunk at pos {}, set random to {}", event.getChunk().getPos(), random);
|
||||
event.getChunk().setData(ETAttachments.ENV, (long)random);
|
||||
} else if (!event.getChunk().hasData(ETAttachments.ENV)) {
|
||||
int random = event.getLevel().getRandom().nextIntBetweenInclusive(Config.minEnvForNewChunk, Config.maxEnvForNewChunk);
|
||||
EnvironmentTech.LOGGER.debug("Chunk without data, set random to {}", random);
|
||||
event.getChunk().setData(ETAttachments.ENV, random);
|
||||
EnvironmentTech.LOGGER.debug("Chunk without data at pos {}, set random to {}",event.getChunk().getPos(), random);
|
||||
event.getChunk().setData(ETAttachments.ENV, (long)random);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,9 @@
|
||||
package robaertschi.environmenttech.level.block;
|
||||
|
||||
import com.mojang.serialization.MapCodec;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.server.level.ServerLevel;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraft.world.level.block.BaseEntityBlock;
|
||||
import net.minecraft.world.level.block.RenderShape;
|
||||
@ -17,6 +19,7 @@ import robaertschi.environmenttech.level.block.entity.EnvCollectorBlockEntity;
|
||||
import javax.annotation.ParametersAreNonnullByDefault;
|
||||
|
||||
@ParametersAreNonnullByDefault()
|
||||
@Slf4j
|
||||
public class EnvCollectorBlock extends BaseEntityBlock {
|
||||
public static final MapCodec<EnvCollectorBlock> CODEC = simpleCodec(EnvCollectorBlock::new);
|
||||
|
||||
@ -44,6 +47,16 @@ public class EnvCollectorBlock extends BaseEntityBlock {
|
||||
@Nullable
|
||||
@Override
|
||||
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level pLevel, BlockState pState, BlockEntityType<T> pBlockEntityType) {
|
||||
return createTickerHelper(pBlockEntityType, ETBlockEntities.ENV_COLLECTOR_BLOCK_ENTITY.get(), EnvCollectorBlockEntity::tick);
|
||||
return createTickerHelper(pBlockEntityType, ETBlockEntities.ENV_COLLECTOR_BLOCK_ENTITY.get(), this::tick);
|
||||
}
|
||||
|
||||
public void tick(Level level, BlockPos blockPos, BlockState blockState, EnvCollectorBlockEntity envCollectorBlockEntity) {
|
||||
if (!level.isClientSide()) {
|
||||
if (level instanceof ServerLevel serverLevel) {
|
||||
envCollectorBlockEntity.serverTick(serverLevel, blockPos, blockState);
|
||||
} else {
|
||||
log.error("level.isClientSide() returned false, but is not a ServerLevel, not ticking EnvCollector");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,11 @@
|
||||
package robaertschi.environmenttech.level.block.entity;
|
||||
|
||||
import net.minecraft.world.Container;
|
||||
import net.neoforged.neoforge.items.IItemHandlerModifiable;
|
||||
import net.neoforged.neoforge.items.wrapper.RecipeWrapper;
|
||||
|
||||
public class ContainerUtils {
|
||||
public static Container itemHandlerToContainer(IItemHandlerModifiable itemHandler) {
|
||||
return new RecipeWrapper(itemHandler);
|
||||
}
|
||||
}
|
@ -3,15 +3,23 @@ package robaertschi.environmenttech.level.block.entity;
|
||||
import lombok.Getter;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.core.HolderLookup;
|
||||
import net.minecraft.core.registries.Registries;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.server.level.ServerLevel;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraft.world.level.block.entity.BlockEntity;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import net.minecraft.world.level.chunk.ChunkAccess;
|
||||
import net.neoforged.neoforge.items.ItemStackHandler;
|
||||
import net.neoforged.neoforge.items.wrapper.RecipeWrapper;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import robaertschi.environmenttech.data.attachments.ETAttachments;
|
||||
import robaertschi.environmenttech.data.capabilities.EnvStorage;
|
||||
import robaertschi.environmenttech.data.capabilities.EnvType;
|
||||
import robaertschi.environmenttech.data.recipes.ETRecipes;
|
||||
import robaertschi.environmenttech.data.recipes.EnvCollectorRecipe;
|
||||
|
||||
import static robaertschi.environmenttech.EnvironmentTech.MODID;
|
||||
|
||||
@ -26,6 +34,9 @@ public class EnvCollectorBlockEntity extends BlockEntity {
|
||||
}
|
||||
};
|
||||
|
||||
@Getter
|
||||
private final RecipeWrapper recipeWrapper = new RecipeWrapper(inventory);
|
||||
|
||||
@Getter
|
||||
private final EnvStorage envStorage = new EnvStorage(EnvType.Chunk, 64, 0, 1) {
|
||||
@Override
|
||||
@ -36,6 +47,10 @@ public class EnvCollectorBlockEntity extends BlockEntity {
|
||||
};
|
||||
|
||||
private int progress = 0;
|
||||
// Every 20 ticks, we take ENV from the current Chunk
|
||||
private int takeEnv = 0;
|
||||
@Nullable
|
||||
private EnvCollectorRecipe currentRecipe = null;
|
||||
|
||||
|
||||
public EnvCollectorBlockEntity(BlockPos pos, BlockState state) {
|
||||
@ -76,7 +91,57 @@ public class EnvCollectorBlockEntity extends BlockEntity {
|
||||
this.inventory.setStackInSlot(1, itemStack);
|
||||
}
|
||||
|
||||
public static void tick(Level level, BlockPos blockPos, BlockState blockState, EnvCollectorBlockEntity envCollectorBlockEntity) {
|
||||
|
||||
public void serverTick(ServerLevel level, BlockPos blockPos, BlockState blockState) {
|
||||
|
||||
if (takeEnv <= 0) {
|
||||
ChunkAccess currentChunk = level.getChunkAt(blockPos);
|
||||
if (currentChunk.hasData(ETAttachments.ENV) && currentChunk.getData(ETAttachments.ENV) > 0) {
|
||||
long received = envStorage.receiveEnv(1, false);
|
||||
currentChunk.setData(ETAttachments.ENV, currentChunk.getData(ETAttachments.ENV) - received);
|
||||
takeEnv = 20;
|
||||
}
|
||||
} else {
|
||||
takeEnv--;
|
||||
}
|
||||
|
||||
if (hasRecipe(level)) {
|
||||
if (progress > 0 && progress < 60) {
|
||||
progress++;
|
||||
} else if (progress > 0) {
|
||||
produce(level);
|
||||
progress = 0;
|
||||
} else {
|
||||
assert currentRecipe != null;
|
||||
if (envStorage.getEnvStored() >= currentRecipe.envUsed()) {
|
||||
envStorage.setEnvStored(envStorage.getEnvStored() - currentRecipe.envUsed());
|
||||
progress = 1;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private void produce(ServerLevel level) {
|
||||
getInputItem().setCount(getInputItem().getCount() - 1);
|
||||
// Is safe, as hasRecipe already checked
|
||||
assert currentRecipe != null;
|
||||
setOutputItem(currentRecipe.assemble(recipeWrapper, level.registryAccess()));
|
||||
}
|
||||
|
||||
private boolean hasRecipe(Level level) {
|
||||
if (currentRecipe != null) {
|
||||
return currentRecipe.matches(recipeWrapper, level);
|
||||
}
|
||||
var recipe = level.getRecipeManager().getRecipeFor(ETRecipes.ENV_COLLECTOR_RECIPE_TYPE.get(), recipeWrapper, level);
|
||||
if (recipe.isEmpty()) {
|
||||
progress = 0;
|
||||
return false;
|
||||
} else {
|
||||
currentRecipe = recipe.get().value();
|
||||
progress = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
Binary file not shown.
After Width: | Height: | Size: 99 B |
Loading…
x
Reference in New Issue
Block a user