Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修改用电器方块状态 #314

Merged
merged 1 commit into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package dev.dubhe.anvilcraft.api.power;

import net.minecraft.core.BlockPos;
import net.minecraft.util.StringRepresentable;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.block.state.properties.EnumProperty;
import net.minecraft.world.phys.shapes.Shapes;
import net.minecraft.world.phys.shapes.VoxelShape;
import org.jetbrains.annotations.NotNull;
Expand All @@ -11,6 +16,9 @@
*/
@SuppressWarnings("unused")
public interface IPowerComponent {
BooleanProperty OVERLOAD = BooleanProperty.create("overload");
EnumProperty<Switch> SWITCH = EnumProperty.create("switch", Switch.class);

/**
* @return 元件位置
*/
Expand Down Expand Up @@ -41,4 +49,43 @@ default VoxelShape getRange() {
*/
@NotNull
PowerComponentType getComponentType();

enum Switch implements StringRepresentable {
ON("on"),
OFF("off");
private final String name;

Switch(String name) {
this.name = name;
}

public @NotNull String toString() {
return this.getSerializedName();
}

@Override
public @NotNull String getSerializedName() {
return this.name;
}
}

/**
* @param level 世界
* @param pos 位置
*/
default void flushState(@NotNull Level level, @NotNull BlockPos pos) {
BlockState state = level.getBlockState(pos);
if (!state.hasProperty(OVERLOAD)) return;
if (this.getGrid() == null) {
if (!state.getValue(OVERLOAD)) {
level.setBlockAndUpdate(pos, state.setValue(OVERLOAD, true));
}
return;
}
if (this.getGrid().isWork() && state.getValue(OVERLOAD)) {
level.setBlockAndUpdate(pos, state.setValue(OVERLOAD, false));
} else if (!this.getGrid().isWork() && !state.getValue(OVERLOAD)) {
level.setBlockAndUpdate(pos, state.setValue(OVERLOAD, true));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import dev.dubhe.anvilcraft.api.depository.FilteredItemDepository;
import dev.dubhe.anvilcraft.api.hammer.IHammerChangeableBlock;
import dev.dubhe.anvilcraft.api.hammer.IHammerRemovable;
import dev.dubhe.anvilcraft.api.power.IPowerComponent;
import dev.dubhe.anvilcraft.block.entity.AutoCrafterBlockEntity;
import dev.dubhe.anvilcraft.init.ModBlockEntities;
import dev.dubhe.anvilcraft.init.ModMenuTypes;
Expand All @@ -25,14 +26,14 @@
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.DirectionalBlock;
import net.minecraft.world.level.block.Mirror;
import net.minecraft.world.level.block.RedstoneTorchBlock;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.Rotation;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityTicker;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.block.state.properties.DirectionProperty;
import net.minecraft.world.phys.BlockHitResult;
Expand All @@ -42,11 +43,20 @@

public class AutoCrafterBlock extends BaseEntityBlock implements IHammerChangeableBlock, IHammerRemovable {
public static final DirectionProperty FACING = DirectionalBlock.FACING;
public static final BooleanProperty LIT = RedstoneTorchBlock.LIT;
public static final BooleanProperty LIT = BlockStateProperties.LIT;
public static final BooleanProperty OVERLOAD = IPowerComponent.OVERLOAD;

/**
* @param properties 方块属性
*/
public AutoCrafterBlock(Properties properties) {
super(properties);
this.registerDefaultState(this.stateDefinition.any().setValue(LIT, false).setValue(FACING, Direction.NORTH));
this.registerDefaultState(
this.stateDefinition.any()
.setValue(LIT, false)
.setValue(OVERLOAD, true)
.setValue(FACING, Direction.NORTH)
);
}

@Override
Expand Down Expand Up @@ -145,12 +155,13 @@ public <T extends BlockEntity> BlockEntityTicker<T> getTicker(
public BlockState getStateForPlacement(@NotNull BlockPlaceContext context) {
return this.defaultBlockState()
.setValue(FACING, context.getNearestLookingDirection().getOpposite())
.setValue(LIT, context.getLevel().hasNeighborSignal(context.getClickedPos()));
.setValue(LIT, context.getLevel().hasNeighborSignal(context.getClickedPos()))
.setValue(OVERLOAD, true);
}

@Override
protected void createBlockStateDefinition(@NotNull StateDefinition.Builder<Block, BlockState> builder) {
builder.add(LIT).add(FACING);
builder.add(LIT).add(OVERLOAD).add(FACING);
}

@Override
Expand Down
16 changes: 11 additions & 5 deletions common/src/main/java/dev/dubhe/anvilcraft/block/HeaterBlock.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package dev.dubhe.anvilcraft.block;

import dev.dubhe.anvilcraft.api.hammer.IHammerRemovable;
import dev.dubhe.anvilcraft.api.power.IPowerComponent;
import dev.dubhe.anvilcraft.block.entity.HeaterBlockEntity;
import dev.dubhe.anvilcraft.init.ModBlockEntities;
import net.minecraft.core.BlockPos;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.BaseEntityBlock;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.RedstoneTorchBlock;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityTicker;
Expand All @@ -22,17 +22,23 @@
import javax.annotation.Nonnull;

public class HeaterBlock extends BaseEntityBlock implements IHammerRemovable {
public static final BooleanProperty LIT = RedstoneTorchBlock.LIT;
public static final BooleanProperty OVERLOAD = IPowerComponent.OVERLOAD;

/**
* @param properties 方块属性
*/
public HeaterBlock(Properties properties) {
super(properties);
this.registerDefaultState(this.stateDefinition.any().setValue(LIT, false));
this.registerDefaultState(
this.stateDefinition.any()
.setValue(OVERLOAD, true)
);
}

@Override
@Nullable
public BlockState getStateForPlacement(@NotNull BlockPlaceContext context) {
return this.defaultBlockState().setValue(LIT, false);
return this.defaultBlockState().setValue(OVERLOAD, true);
}

@Nullable
Expand All @@ -43,7 +49,7 @@ public BlockEntity newBlockEntity(@NotNull BlockPos pos, @NotNull BlockState sta

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,12 @@ public static void onBlockEntityRegister(BlockEntityType<AutoCrafterBlockEntity>
throw new AssertionError();
}

/**
* @param level 世界
* @param pos 位置
*/
public void tick(@NotNull Level level, BlockPos pos) {
this.flushState(level, pos);
BlockState state = level.getBlockState(pos);
if (state.getValue(AutoCrafterBlock.LIT)) craft(level);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import dev.dubhe.anvilcraft.api.power.IPowerConsumer;
import dev.dubhe.anvilcraft.api.power.PowerGrid;
import dev.dubhe.anvilcraft.block.HeaterBlock;
import dev.dubhe.anvilcraft.init.ModBlockEntities;
import dev.dubhe.anvilcraft.init.ModBlocks;
import lombok.Getter;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
Expand Down Expand Up @@ -53,18 +51,6 @@ public void setGrid(@Nullable PowerGrid grid) {
* @param pos 位置
*/
public void tick(@NotNull Level level, @NotNull BlockPos pos) {
BlockState state = level.getBlockState(pos);
if (!state.is(ModBlocks.HEATER.get())) return;
if (this.grid == null) {
if (state.getValue(HeaterBlock.LIT)) {
level.setBlockAndUpdate(pos, state.setValue(HeaterBlock.LIT, false));
}
return;
}
if (this.grid.isWork() && !state.getValue(HeaterBlock.LIT)) {
level.setBlockAndUpdate(pos, state.setValue(HeaterBlock.LIT, true));
} else if (!this.grid.isWork() && state.getValue(HeaterBlock.LIT)) {
level.setBlockAndUpdate(pos, state.setValue(HeaterBlock.LIT, false));
}
this.flushState(level, pos);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ protected void renderSlotTooltip(@NotNull GuiGraphics guiGraphics, int x, int y)
if (!((ItemDepositorySlot) this.hoveredSlot).isFilter()) return;
if (!this.isFilterEnabled()) return;
if (!this.isSlotDisabled(this.hoveredSlot.getContainerSlot())) return;
guiGraphics.renderTooltip(this.font, Component.literal("screen.anvilcraft.slot.disable.tooltip"), x, y);
guiGraphics.renderTooltip(this.font, Component.translatable("screen.anvilcraft.slot.disable.tooltip"), x, y);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ protected void renderSlotTooltip(@NotNull GuiGraphics guiGraphics, int x, int y)
if (!((ItemDepositorySlot) this.hoveredSlot).isFilter()) return;
if (!this.isFilterEnabled()) return;
if (!this.isSlotDisabled(this.hoveredSlot.getContainerSlot())) return;
guiGraphics.renderTooltip(this.font, Component.literal("screen.anvilcraft.slot.disable.tooltip"), x, y);
guiGraphics.renderTooltip(this.font, Component.translatable("screen.anvilcraft.slot.disable.tooltip"), x, y);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.tterrag.registrate.providers.RegistrateRecipeProvider;
import dev.dubhe.anvilcraft.AnvilCraft;
import dev.dubhe.anvilcraft.block.HeaterBlock;
import dev.dubhe.anvilcraft.data.generator.AnvilCraftDatagen;
import dev.dubhe.anvilcraft.data.recipe.anvil.AnvilRecipe;
import dev.dubhe.anvilcraft.init.ModBlocks;
Expand All @@ -16,6 +15,8 @@

import java.util.Map;

import static dev.dubhe.anvilcraft.api.power.IPowerComponent.OVERLOAD;

public class SuperHeatingRecipesLoader {
/**
* 初始化配方
Expand All @@ -25,23 +26,23 @@ public class SuperHeatingRecipesLoader {
public static void init(RegistrateRecipeProvider provider) {
AnvilRecipe.Builder.create(RecipeCategory.MISC)
.icon(Items.DIAMOND)
.hasBlock(ModBlocks.HEATER.get(), new Vec3(0.0, -2.0, 0.0), Map.entry(HeaterBlock.LIT, true))
.hasBlock(ModBlocks.HEATER.get(), new Vec3(0.0, -2.0, 0.0), Map.entry(OVERLOAD, false))
.hasBlock(Blocks.CAULDRON)
.hasItemIngredient(new Vec3(0.0, -1.0, 0.0), 16, Items.COAL_BLOCK)
.spawnItem(new Vec3(0.0, -1.0, 0.0), Items.DIAMOND, 1)
.unlockedBy(AnvilCraftDatagen.hasItem(Items.COAL_BLOCK), AnvilCraftDatagen.has(Items.COAL_BLOCK))
.save(provider, AnvilCraft.of("heating/" + BuiltInRegistries.ITEM.getKey(Items.DIAMOND).getPath()));
AnvilRecipe.Builder.create(RecipeCategory.MISC)
.icon(Items.LAVA_BUCKET)
.hasBlock(ModBlocks.HEATER.get(), new Vec3(0.0, -2.0, 0.0), Map.entry(HeaterBlock.LIT, true))
.hasBlock(ModBlocks.HEATER.get(), new Vec3(0.0, -2.0, 0.0), Map.entry(OVERLOAD, false))
.hasBlock(Blocks.CAULDRON)
.hasItemIngredient(new Vec3(0.0, -1.0, 0.0), 1, ModItemTags.STONE)
.setBlock(Blocks.LAVA_CAULDRON)
.unlockedBy(AnvilCraftDatagen.hasItem(ModItemTags.STONE), AnvilCraftDatagen.has(ModItemTags.STONE))
.save(provider, AnvilCraft.of("heating/" + BuiltInRegistries.BLOCK.getKey(Blocks.LAVA).getPath()));
AnvilRecipe.Builder.create(RecipeCategory.MISC)
.icon(ModItems.ROYAL_STEEL_INGOT)
.hasBlock(ModBlocks.HEATER.get(), new Vec3(0.0, -2.0, 0.0), Map.entry(HeaterBlock.LIT, true))
.hasBlock(ModBlocks.HEATER.get(), new Vec3(0.0, -2.0, 0.0), Map.entry(OVERLOAD, false))
.hasBlock(Blocks.CAULDRON)
.hasItemIngredient(new Vec3(0.0, -1.0, 0.0), 3, Items.IRON_INGOT)
.hasItemIngredient(new Vec3(0.0, -1.0, 0.0), 1, Items.DIAMOND)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import dev.dubhe.anvilcraft.api.event.server.ServerEndDataPackReloadEvent;
import dev.dubhe.anvilcraft.api.event.server.ServerStartedEvent;
import dev.dubhe.anvilcraft.api.hammer.HammerManager;
import dev.dubhe.anvilcraft.block.HeaterBlock;
import dev.dubhe.anvilcraft.api.power.IPowerComponent;
import dev.dubhe.anvilcraft.data.recipe.anvil.AnvilRecipe;
import dev.dubhe.anvilcraft.data.recipe.anvil.outcome.SpawnItem;
import dev.dubhe.anvilcraft.data.recipe.anvil.predicate.HasBlock;
Expand Down Expand Up @@ -152,7 +152,7 @@ public static void processRecipes(@NotNull MinecraftServer server) {
.of(ModBlocks.HEATER.get())
.setProperties(
StatePropertiesPredicate.Builder.properties()
.hasProperty(HeaterBlock.LIT, true)
.hasProperty(IPowerComponent.OVERLOAD, false)
.build()
)
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"anvilcraft:heater"
],
"state": {
"lit": "true"
"overload": "false"
}
},
"offset": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"anvilcraft:heater"
],
"state": {
"lit": "true"
"overload": "false"
}
},
"offset": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"anvilcraft:heater"
],
"state": {
"lit": "true"
"overload": "false"
}
},
"offset": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"anvilcraft:heater"
],
"state": {
"lit": "true"
"overload": "false"
}
},
"offset": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"anvilcraft:heater"
],
"state": {
"lit": "true"
"overload": "false"
}
},
"offset": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"anvilcraft:heater"
],
"state": {
"lit": "true"
"overload": "false"
}
},
"offset": [
Expand Down
Loading