Skip to content

Commit

Permalink
Disable Trivia block glowing once answered
Browse files Browse the repository at this point in the history
  • Loading branch information
Gegy committed Nov 14, 2024
1 parent 37c6fff commit 0422687
Show file tree
Hide file tree
Showing 15 changed files with 93 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"variants": {
"": {
"answered=false": {
"model": "ltminigames:block/trivia_collectable_active"
},
"answered=true": {
"model": "ltminigames:block/trivia_collectable"
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"variants": {
"": {
"answered=false": {
"model": "ltminigames:block/trivia_gate_active"
},
"answered=true": {
"model": "ltminigames:block/trivia_gate"
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"variants": {
"": {
"answered=false": {
"model": "ltminigames:block/trivia_victory_active"
},
"answered=true": {
"model": "ltminigames:block/trivia_victory"
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"parent": "ltminigames:block/cube_glow",
"parent": "minecraft:block/cube_all",
"textures": {
"all": "ltminigames:block/trivia_collectable",
"glow": "ltminigames:block/trivia_glow"
"all": "ltminigames:block/trivia_collectable"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"parent": "ltminigames:block/cube_glow",
"textures": {
"all": "ltminigames:block/trivia_collectable",
"glow": "ltminigames:block/trivia_glow"
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"parent": "ltminigames:block/cube_glow",
"parent": "minecraft:block/cube_all",
"textures": {
"all": "ltminigames:block/trivia_gate",
"glow": "ltminigames:block/trivia_glow"
"all": "ltminigames:block/trivia_gate"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"parent": "ltminigames:block/cube_glow",
"textures": {
"all": "ltminigames:block/trivia_gate",
"glow": "ltminigames:block/trivia_glow"
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"parent": "ltminigames:block/cube_glow",
"parent": "minecraft:block/cube_all",
"textures": {
"all": "ltminigames:block/trivia_victory",
"glow": "ltminigames:block/trivia_glow"
"all": "ltminigames:block/trivia_victory"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"parent": "ltminigames:block/cube_glow",
"textures": {
"all": "ltminigames:block/trivia_victory",
"glow": "ltminigames:block/trivia_glow"
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.lovetropics.minigames.client.render.block;

import com.lovetropics.minigames.LoveTropics;
import com.lovetropics.minigames.common.content.river_race.block.TriviaChestBlock;
import com.lovetropics.minigames.common.content.river_race.block.TriviaChestBlockEntity;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
Expand All @@ -17,6 +18,7 @@
import net.minecraft.core.Direction;
import net.minecraft.util.Mth;
import net.minecraft.world.level.block.ChestBlock;
import net.minecraft.world.level.block.state.BlockState;

public class TriviaChestRenderer implements BlockEntityRenderer<TriviaChestBlockEntity> {
private static final Material MATERIAL = new Material(Sheets.CHEST_SHEET, LoveTropics.location("entity/chest/trivia"));
Expand All @@ -35,8 +37,10 @@ public TriviaChestRenderer(BlockEntityRendererProvider.Context context) {

@Override
public void render(TriviaChestBlockEntity blockEntity, float partialTicks, PoseStack poseStack, MultiBufferSource bufferSource, int packedLight, int packedOverlay) {
BlockState blockState = blockEntity.getBlockState();

poseStack.pushPose();
float rotation = blockEntity.hasLevel() ? blockEntity.getBlockState().getOptionalValue(ChestBlock.FACING).orElse(Direction.NORTH).toYRot() : Direction.SOUTH.toYRot();
float rotation = blockEntity.hasLevel() ? blockState.getOptionalValue(ChestBlock.FACING).orElse(Direction.NORTH).toYRot() : Direction.SOUTH.toYRot();
poseStack.translate(0.5f, 0.5f, 0.5f);
poseStack.mulPose(Axis.YP.rotationDegrees(-rotation));
poseStack.translate(-0.5f, -0.5f, -0.5f);
Expand All @@ -46,7 +50,9 @@ public void render(TriviaChestBlockEntity blockEntity, float partialTicks, PoseS
openness = 1.0f - openness * openness * openness;

render(poseStack, MATERIAL.buffer(bufferSource, RenderType::entityCutout), lid, lock, bottom, openness, packedLight, packedOverlay);
render(poseStack, GLOW_MATERIAL.buffer(bufferSource, RenderType::entityCutout), lid, lock, bottom, openness, LightTexture.FULL_BRIGHT, packedOverlay);
if (!blockState.getValue(TriviaChestBlock.ANSWERED)) {
render(poseStack, GLOW_MATERIAL.buffer(bufferSource, RenderType::entityCutout), lid, lock, bottom, openness, LightTexture.FULL_BRIGHT, packedOverlay);
}

poseStack.popPose();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,20 @@
import com.lovetropics.minigames.common.content.river_race.block.TriviaChestBlockEntity;
import com.lovetropics.minigames.common.util.registry.GameBehaviorEntry;
import com.lovetropics.minigames.common.util.registry.LoveTropicsRegistrate;
import com.tterrag.registrate.providers.DataGenContext;
import com.tterrag.registrate.providers.ProviderType;
import com.tterrag.registrate.providers.RegistrateBlockstateProvider;
import com.tterrag.registrate.util.entry.BlockEntityEntry;
import com.tterrag.registrate.util.entry.BlockEntry;
import net.minecraft.core.component.DataComponentType;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.Unit;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.neoforged.neoforge.client.model.generators.BlockModelBuilder;
import net.neoforged.neoforge.client.model.generators.ConfiguredModel;
import net.neoforged.neoforge.registries.DeferredHolder;
import net.neoforged.neoforge.registries.DeferredRegister;

Expand All @@ -39,39 +44,36 @@ public class RiverRace {
.block("trivia_gate", TriviaBlock.GateTriviaBlock::new)
.initialProperties(() -> Blocks.BEDROCK)
.properties(BlockBehaviour.Properties::noLootTable)
.blockstate((ctx, prov) -> {
BlockModelBuilder model = prov.models().withExistingParent(ctx.getName(), prov.modLoc("block/cube_glow"))
.texture("all", prov.modLoc("block/" + ctx.getName()))
.texture("glow", prov.modLoc("block/trivia_glow"));
prov.simpleBlock(ctx.get(), model);
})
.blockstate(RiverRace::triviaBlockModel)
.simpleItem()
.register();

public static final BlockEntry<TriviaBlock.CollectableTriviaBlock> TRIVIA_COLLECTABLE = REGISTRATE
.block("trivia_collectable", TriviaBlock.CollectableTriviaBlock::new)
.initialProperties(() -> Blocks.BEDROCK)
.properties(BlockBehaviour.Properties::noLootTable)
.blockstate((ctx, prov) -> {
BlockModelBuilder model = prov.models().withExistingParent(ctx.getName(), prov.modLoc("block/cube_glow"))
.texture("all", prov.modLoc("block/" + ctx.getName()))
.texture("glow", prov.modLoc("block/trivia_glow"));
prov.simpleBlock(ctx.get(), model);
})
.blockstate(RiverRace::triviaBlockModel)
.simpleItem()
.register();
public static final BlockEntry<TriviaBlock.VictoryTriviaBlock> TRIVIA_VICTORY = REGISTRATE
.block("trivia_victory", TriviaBlock.VictoryTriviaBlock::new)
.initialProperties(() -> Blocks.BEDROCK)
.properties(BlockBehaviour.Properties::noLootTable)
.blockstate((ctx, prov) -> {
BlockModelBuilder model = prov.models().withExistingParent(ctx.getName(), prov.modLoc("block/cube_glow"))
.texture("all", prov.modLoc("block/" + ctx.getName()))
.texture("glow", prov.modLoc("block/trivia_glow"));
prov.simpleBlock(ctx.get(), model);
})
.blockstate(RiverRace::triviaBlockModel)
.simpleItem()
.register();

private static void triviaBlockModel(DataGenContext<Block, ?> ctx, RegistrateBlockstateProvider prov) {
ResourceLocation baseTexture = prov.modLoc("block/" + ctx.getName());
BlockModelBuilder activeModel = prov.models().withExistingParent(ctx.getName() + "_active", prov.modLoc("block/cube_glow"))
.texture("all", baseTexture)
.texture("glow", prov.modLoc("block/trivia_glow"));
BlockModelBuilder inactiveModel = prov.models().cubeAll(ctx.getName(), baseTexture);
prov.getVariantBuilder(ctx.get())
.partialState().with(TriviaBlock.ANSWERED, false).setModels(new ConfiguredModel(activeModel))
.partialState().with(TriviaBlock.ANSWERED, true).setModels(new ConfiguredModel(inactiveModel));
}

public static final BlockEntry<TriviaChestBlock> TRIVIA_CHEST = REGISTRATE
.block("trivia_chest", TriviaChestBlock::new)
.initialProperties(() -> Blocks.BEDROCK)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import net.minecraft.world.level.block.EntityBlock;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import org.jetbrains.annotations.Nullable;

public class TriviaBlock extends Block implements EntityBlock {
Expand All @@ -32,11 +34,20 @@ public VictoryTriviaBlock(Properties properties) {
}
}

public static final BooleanProperty ANSWERED = BooleanProperty.create("answered");

private final TriviaType type;

public TriviaBlock(Properties properties, TriviaType blockType) {
public TriviaBlock(Properties properties, TriviaType type) {
super(properties);
this.type = blockType;
this.type = type;
registerDefaultState(getStateDefinition().any().setValue(ANSWERED, false));
}

@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
super.createBlockStateDefinition(builder);
builder.add(ANSWERED);
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,9 @@ public record TriviaBlockState(boolean isAnswered, Optional<String> correctAnswe
private static final Logger LOGGER = LogUtils.getLogger();
public static final String TAG_QUESTION = "question";
public static final String TAG_UNLOCKS_AT = "unlocksAt";
public static final String TAG_ANSWERED = "answered";
@Nullable
private TriviaBehaviour.TriviaQuestion question;
private long unlocksAt;
private boolean answered;
private final TriviaType triviaType;

public TriviaBlockEntity(BlockEntityType<?> type, BlockPos pos, BlockState blockState) {
Expand All @@ -61,7 +59,6 @@ protected void saveAdditional(CompoundTag tag, HolderLookup.Provider registries)
if (unlocksAt > 0) {
tag.putLong(TAG_UNLOCKS_AT, unlocksAt);
}
tag.putBoolean(TAG_ANSWERED, answered);
}

@Override
Expand All @@ -75,9 +72,6 @@ public void loadAdditional(CompoundTag tag, HolderLookup.Provider registries) {
if(tag.contains(TAG_UNLOCKS_AT)) {
unlocksAt = tag.getLong(TAG_UNLOCKS_AT);
}
if (tag.contains(TAG_ANSWERED)) {
answered = tag.getBoolean(TAG_ANSWERED);
}
}

@Override
Expand All @@ -86,7 +80,6 @@ public CompoundTag getUpdateTag(HolderLookup.Provider registries) {
if(unlocksAt > 0) {
tag.putLong(TAG_UNLOCKS_AT, unlocksAt);
}
tag.putBoolean(TAG_ANSWERED, answered);
return tag;
}

Expand All @@ -102,9 +95,6 @@ public void handleUpdateTag(CompoundTag tag, HolderLookup.Provider registries) {
if (tag.contains(TAG_UNLOCKS_AT)) {
unlocksAt = tag.getLong(TAG_UNLOCKS_AT);
}
if (tag.contains(TAG_ANSWERED)) {
answered = tag.getBoolean(TAG_ANSWERED);
}
}

@Override
Expand Down Expand Up @@ -147,19 +137,20 @@ public void unlock() {
}

@Override
public boolean markAsCorrect(){
if (answered) {
public boolean markAsCorrect() {
if (getBlockState().getValue(TriviaBlock.ANSWERED)) {
return false;
}
answered = true;
level.setBlockAndUpdate(getBlockPos(), getBlockState().setValue(TriviaBlock.ANSWERED, true));
markUpdated();
return true;
}

@Override
public TriviaBlockState getState(){
public TriviaBlockState getState() {
boolean answered = getBlockState().getValue(TriviaBlock.ANSWERED);
Optional<String> correctAnswer = Optional.empty();
if(answered){
if (answered) {
correctAnswer = Optional.of(question.answers().stream().filter(TriviaBehaviour.TriviaQuestion.TriviaQuestionAnswer::correct).findFirst().get().text());
}
return new TriviaBlockState(answered, correctAnswer, unlocksAt > 0, unlocksAt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@ public class TriviaChestBlock extends AbstractChestBlock<ChestBlockEntity> {
private static final DirectionProperty FACING = HorizontalDirectionalBlock.FACING;
private static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
private static final EnumProperty<ChestType> TYPE = BlockStateProperties.CHEST_TYPE;
public static final BooleanProperty ANSWERED = TriviaBlock.ANSWERED;
private static final VoxelShape AABB = Block.box(1.0, 0.0, 1.0, 15.0, 14.0, 15.0);

public TriviaChestBlock(Properties properties) {
super(properties, RiverRace.TRIVIA_CHEST_BLOCK_ENTITY::get);
registerDefaultState(stateDefinition.any().setValue(FACING, Direction.NORTH).setValue(WATERLOGGED, false).setValue(TYPE, ChestType.SINGLE));
registerDefaultState(stateDefinition.any().setValue(FACING, Direction.NORTH).setValue(WATERLOGGED, false).setValue(ANSWERED, false).setValue(TYPE, ChestType.SINGLE));
}

@Override
Expand Down Expand Up @@ -144,7 +145,7 @@ protected BlockState mirror(BlockState state, Mirror mirror) {

@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
builder.add(FACING, WATERLOGGED, TYPE);
builder.add(FACING, WATERLOGGED, TYPE, ANSWERED);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public class TriviaChestBlockEntity extends ChestBlockEntity implements HasTrivi
@Nullable
private TriviaBehaviour.TriviaQuestion question;
private long unlocksAt;
private boolean answered;

public TriviaChestBlockEntity(BlockEntityType<?> type, BlockPos pos, BlockState blockState) {
super(type, pos, blockState);
Expand Down Expand Up @@ -56,9 +55,6 @@ protected void loadAdditional(CompoundTag tag, HolderLookup.Provider registries)
if(tag.contains(TriviaBlockEntity.TAG_UNLOCKS_AT)) {
unlocksAt = tag.getLong(TriviaBlockEntity.TAG_UNLOCKS_AT);
}
if (tag.contains(TriviaBlockEntity.TAG_ANSWERED)) {
answered = tag.getBoolean(TriviaBlockEntity.TAG_ANSWERED);
}
}

@Override
Expand All @@ -70,7 +66,6 @@ protected void saveAdditional(CompoundTag tag, HolderLookup.Provider registries)
if(unlocksAt > 0){
tag.putLong(TriviaBlockEntity.TAG_UNLOCKS_AT, unlocksAt);
}
tag.putBoolean(TriviaBlockEntity.TAG_ANSWERED, answered);
}

private void markUpdated() {
Expand Down Expand Up @@ -110,18 +105,19 @@ public void unlock() {

@Override
public boolean markAsCorrect() {
if (answered) {
if (getBlockState().getValue(TriviaBlock.ANSWERED)) {
return false;
}
answered = true;
level.setBlockAndUpdate(getBlockPos(), getBlockState().setValue(TriviaBlock.ANSWERED, true));
markUpdated();
return true;
}

@Override
public TriviaBlockEntity.TriviaBlockState getState() {
boolean answered = getBlockState().getValue(TriviaBlock.ANSWERED);
Optional<String> correctAnswer = Optional.empty();
if(answered){
if (answered) {
correctAnswer = Optional.of(question.answers().stream().filter(TriviaBehaviour.TriviaQuestion.TriviaQuestionAnswer::correct).findFirst().get().text());
}
return new TriviaBlockEntity.TriviaBlockState(answered, correctAnswer, unlocksAt > 0, unlocksAt);
Expand Down

0 comments on commit 0422687

Please sign in to comment.