diff --git a/src/main/java/com/teammoeg/frostedheart/FHBlocks.java b/src/main/java/com/teammoeg/frostedheart/FHBlocks.java index 5a9c20953..2a2a1de83 100644 --- a/src/main/java/com/teammoeg/frostedheart/FHBlocks.java +++ b/src/main/java/com/teammoeg/frostedheart/FHBlocks.java @@ -198,6 +198,22 @@ public static RegistryObject register(String name,Supplier< .item() .transform(customItemModel()) .register(); + public static RegistryObject house = register("house",()->new HouseBlock( Block.Properties + .create(Material.WOOD) + .sound(SoundType.WOOD) + .setRequiresTool() + .harvestTool(ToolType.AXE) + .hardnessAndResistance(2, 6) + .notSolid())); + + public static RegistryObject farm = register("farm",()->new FarmBlock( Block.Properties + .create(Material.WOOD) + .sound(SoundType.WOOD) + .setRequiresTool() + .harvestTool(ToolType.AXE) + .hardnessAndResistance(2, 6) + .notSolid())); + public static void init() { Create.registrate().addToSection(steam_core, AllSections.KINETICS); } diff --git a/src/main/java/com/teammoeg/frostedheart/FHTileTypes.java b/src/main/java/com/teammoeg/frostedheart/FHTileTypes.java index aa0edeed8..e0f851ac3 100644 --- a/src/main/java/com/teammoeg/frostedheart/FHTileTypes.java +++ b/src/main/java/com/teammoeg/frostedheart/FHTileTypes.java @@ -42,6 +42,11 @@ import com.teammoeg.frostedheart.content.steamenergy.debug.DebugHeaterTileEntity; import com.teammoeg.frostedheart.content.steamenergy.sauna.SaunaTileEntity; import com.teammoeg.frostedheart.content.steamenergy.steamcore.SteamCoreTileEntity; +import com.teammoeg.frostedheart.research.machines.DrawingDeskTileEntity; +import com.teammoeg.frostedheart.research.machines.MechCalcTileEntity; + +import com.teammoeg.frostedheart.town.Farm.FarmBlockTileEntity; +import com.teammoeg.frostedheart.town.house.HouseTileEntity; import com.teammoeg.frostedheart.content.town.house.HouseTileEntity; import com.teammoeg.frostedheart.content.town.mine.MineBaseTileEntity; import com.teammoeg.frostedheart.content.town.mine.MineTileEntity; @@ -131,6 +136,9 @@ public class FHTileTypes { "mine_base", makeType(MineBaseTileEntity::new, FHBlocks.mine_base) ); + public static final RegistryObject> FARM = REGISTER.register( + "farm_block", makeType(FarmBlockTileEntity::new, FHBlocks.farm) + ); private static Supplier> makeType(Supplier create, Supplier valid) { return makeTypeMultipleBlocks(create, () -> ImmutableSet.of(valid.get())); } diff --git a/src/main/java/com/teammoeg/frostedheart/content/climate/WorldTemperature.java b/src/main/java/com/teammoeg/frostedheart/content/climate/WorldTemperature.java index ad41e7a10..b96fb3909 100644 --- a/src/main/java/com/teammoeg/frostedheart/content/climate/WorldTemperature.java +++ b/src/main/java/com/teammoeg/frostedheart/content/climate/WorldTemperature.java @@ -154,7 +154,7 @@ public static float getClimateTemperature(IWorldReader w) { } public static int getClimateWind(IWorldReader w) { if (w instanceof World) { - return WorldClimate.getWind((World) w); + return (int)WorldClimate.getWind((World) w); } return 0; } diff --git a/src/main/java/com/teammoeg/frostedheart/town/Farm/FarmBlock.java b/src/main/java/com/teammoeg/frostedheart/town/Farm/FarmBlock.java new file mode 100644 index 000000000..ed253a192 --- /dev/null +++ b/src/main/java/com/teammoeg/frostedheart/town/Farm/FarmBlock.java @@ -0,0 +1,71 @@ +package com.teammoeg.frostedheart.town.Farm; + +import blusunrize.immersiveengineering.common.util.Utils; +import com.teammoeg.frostedheart.FHTileTypes; +import com.teammoeg.frostedheart.base.block.FHBaseBlock; +import com.teammoeg.frostedheart.content.steamenergy.steamcore.SteamCoreTileEntity; +import net.minecraft.block.Block; +import net.minecraft.block.BlockState; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.ActionResultType; +import net.minecraft.util.Hand; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.BlockRayTraceResult; +import net.minecraft.world.IBlockReader; +import net.minecraft.world.World; + +import javax.annotation.Nonnull; +import java.util.function.BiFunction; + +public class FarmBlock extends FHBaseBlock { + public FarmBlock(Properties blockProps) { + super(blockProps); + } + + @Override + public TileEntity createTileEntity(@Nonnull BlockState state, @Nonnull IBlockReader world) { + return FHTileTypes.FARM.get().create(); + } + + @Override + public boolean hasTileEntity(BlockState state) { + return true; + } + + @Override + public ActionResultType onBlockActivated(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockRayTraceResult hit) { + ActionResultType superResult = super.onBlockActivated(state, world, pos, player, hand, hit); + if (superResult.isSuccessOrConsume() || player.isSneaking()) + return superResult; + ItemStack item = player.getHeldItem(hand); + TileEntity te = Utils.getExistingTileEntity(world, pos); + if (te instanceof FarmBlockTileEntity) { + return ((FarmBlockTileEntity) te).onClick(player, item); + } + return superResult; + } +} +/* +【管理员】不咕不咕(1905387052) 2024/1/23 17:14:17 +可以先判断是否在能量塔加温区域 + +【管理员】不咕不咕(1905387052) 2024/1/23 17:14:45 +然后可以用那个schedule的东西执行 + +【管理员】不咕不咕(1905387052) 2024/1/23 17:16:08 +对应实体实现IScheduledTaskTE接口 + +【管理员】不咕不咕(1905387052) 2024/1/23 17:16:27 +然后在executeTask写上你的判定代码 + +【管理员】不咕不咕(1905387052) 2024/1/23 17:16:56 +实体初始化的时候或者首次tick在SchedulerQueue里面add + +【管理员】不咕不咕(1905387052) 2024/1/23 17:17:09 +系统就会自动调度了 + + + */ diff --git a/src/main/java/com/teammoeg/frostedheart/town/Farm/FarmBlockTileEntity.java b/src/main/java/com/teammoeg/frostedheart/town/Farm/FarmBlockTileEntity.java new file mode 100644 index 000000000..a5bb07869 --- /dev/null +++ b/src/main/java/com/teammoeg/frostedheart/town/Farm/FarmBlockTileEntity.java @@ -0,0 +1,207 @@ +package com.teammoeg.frostedheart.town.Farm; + +import blusunrize.immersiveengineering.common.blocks.IEBlocks; +import blusunrize.immersiveengineering.common.blocks.plant.HempBlock; +import com.alcatrazescapee.primalwinter.common.ModBlocks; +import com.ibm.icu.impl.Pair; +import com.teammoeg.frostedheart.FHBlocks; +import com.teammoeg.frostedheart.FHTileTypes; +import com.teammoeg.frostedheart.client.util.ClientUtils; +import com.teammoeg.frostedheart.climate.WorldTemperature; +import com.teammoeg.frostedheart.climate.chunkheatdata.ChunkHeatData; +import com.teammoeg.frostedheart.content.agriculture.FHBerryBushBlock; +import com.teammoeg.frostedheart.content.agriculture.FHCropBlock; +import com.teammoeg.frostedheart.content.agriculture.RyeBlock; +import com.teammoeg.frostedheart.content.agriculture.WhiteTurnipBlock; +import com.teammoeg.frostedheart.scheduler.IScheduledTaskTE; +import com.teammoeg.frostedheart.scheduler.SchedulerQueue; +import com.teammoeg.frostedheart.town.ITownBlockTE; +import com.teammoeg.frostedheart.town.TownWorkerType; +import io.netty.handler.codec.sctp.SctpOutboundByteStreamHandler; +import net.minecraft.block.*; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.CompoundNBT; +import net.minecraft.tileentity.ITickableTileEntity; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.ActionResultType; +import net.minecraft.util.math.BlockPos; +import net.minecraftforge.common.Tags; + +import java.util.*; + + +public class FarmBlockTileEntity extends TileEntity implements ITownBlockTE, IScheduledTaskTE, ITickableTileEntity { + private boolean isAdd; + public static int MAX_SIZE = 576; + public static int MIN_SIZE = 4; + public static double WORK_SPEED = 10.0; + public Map> blockTransMap; + public List> blocksForPlant; + public int size;//Size of the farm + public int temperature; + public Map blocks; + public FarmBlockTileEntity() { + super(FHTileTypes.FARM.get()); + this.isAdd = false; + this.blocks = new HashMap<>(); + this.blockTransMap = new HashMap<>(); + this.blocksForPlant = new Stack<>(); + + blockTransMap.put(Blocks.DIRT, Pair.of(Blocks.FARMLAND, 1.0)); + blockTransMap.put(Blocks.COARSE_DIRT, Pair.of(Blocks.DIRT, 1.0)); + blockTransMap.put(ModBlocks.SNOWY_COARSE_DIRT.get(), Pair.of(Blocks.DIRT, 1.0)); + blockTransMap.put(ModBlocks.SNOWY_DIRT.get(), Pair.of(Blocks.DIRT, 1.0)); + + blocksForPlant.add(Pair.of(FHBlocks.white_turnip_block.get(), (float)((FHCropBlock)FHBlocks.white_turnip_block.get()).getGrowTemperature())); + blocksForPlant.add(Pair.of(IEBlocks.Misc.hempPlant, WorldTemperature.HEMP_GROW_TEMPERATURE)); + blocksForPlant.add(Pair.of(FHBlocks.rye_block.get(), (float)((FHCropBlock)FHBlocks.rye_block.get()).getGrowTemperature())); + + blockTransMap.put(Blocks.GOLD_BLOCK, Pair.of(Blocks.DIRT, 1.0)); + blockTransMap.put(Blocks.SANDSTONE, Pair.of(Blocks.DIRT, 1.0)); + } + + @Override + public int getPriority() { + return 0; + } + + @Override + public CompoundNBT getWorkData() { + CompoundNBT data = new CompoundNBT(); + data.putInt("size", size); + data.putInt("temperature", temperature); + return data; + } + + @Override + public TownWorkerType getWorker() { + return TownWorkerType.FARM; + } + + @Override + public boolean isWorkValid() { + return checkFarm(); + } + + @Override + public void setWorkData(CompoundNBT data) { + size = data.getInt("size"); + temperature = data.getInt("temperature"); + } + + private boolean checkFarm(){ + Queue queue = new PriorityQueue<>(); + Map blockMap = new HashMap<>(); + queue.add(this.pos.add(0, -1, 0)); + blockMap.put(this.pos.add(0, -1, 0).toLong(), this.pos.add(0, -1, 0)); + BlockPos tempPos; + List> py = Arrays.asList(Pair.of(1, 0), Pair.of(-1, 0), Pair.of(0, 1), Pair.of(0, -1)); + while (!queue.isEmpty()){ + if(blockMap.size() > MAX_SIZE)break; + tempPos = queue.poll(); + for (Pair p : py) { + if(world.isAirBlock(tempPos.add(p.first, 1, p.second)) || !isUsefulBlock(tempPos.add(p.first, 1, p.second))){ + if(!world.isAirBlock(tempPos.add(p.first, 0, p.second))){ + BlockPos pos = tempPos.add(p.first, 0, p.second); + Long key = pos.toLong(); + if(!blockMap.containsKey(key)){ + queue.add(pos); + blockMap.put(key, pos); + } + } + } + } + } + if(blockMap.size() >= MAX_SIZE || blockMap.size() <= MIN_SIZE){ + blockMap.clear(); + }else{ + this.size = blockMap.size(); + this.blocks.clear(); + this.blocks.putAll(blockMap); + blockMap.clear(); + return true; + } + return false; + } + + private boolean isUsefulBlock(BlockPos pos){ + assert world != null : "Empty world"; + Block block = world.getBlockState(pos).getBlock(); + if(block instanceof FenceBlock || block instanceof WallBlock ||block instanceof FenceGateBlock)return true; + return false; + } + + public ActionResultType onClick(PlayerEntity pe, ItemStack is){ + if(is != null){ + System.out.println("Farm Clicked"); + if(this.checkFarm()){ + System.out.println("Complete"); + }else{ + System.out.println("UnComplete"); + } + } + return ActionResultType.PASS; + } + + @Override + public void executeTask() { + if(checkFarm()){ + if (world != null) { + if(Math.random() < 0.01 * WORK_SPEED){ + if(this.blocks.size() > 0){ + List list = new ArrayList(blocks.values()); + int pc = (int) (list.size() * Math.random()); + doFarm(list.get(pc)); + } + } + } + } + } + + private void doFarm(BlockPos pos){ + assert world != null; + if(blockTransMap.containsKey(world.getBlockState(pos).getBlock())){ + if(Math.random() < blockTransMap.get(world.getBlockState(pos).getBlock()).second){ + world.setBlockState(pos, blockTransMap.get(world.getBlockState(pos).getBlock()).first.getDefaultState()); + } + } + if(world.getBlockState(pos).getBlock() instanceof FarmlandBlock && world.isAirBlock(pos.add(0, 1, 0))){ + Block plt = selectSeedForPlant(); + if(plt != null){ + System.out.println("Plant: " + plt.getTranslatedName()); + world.setBlockState(pos.add(0, 1, 0), plt.getDefaultState()); + } + } + } + + private Block selectSeedForPlant(){ + float temp = ChunkHeatData.getTemperature(world, pos) + 100; + List blist = new Stack<>(); + for(Pair bp : blocksForPlant){ + if(bp.second < temp){ + blist.add(bp.first); + } + } + if(blist.size() <= 0)return null; + return blist.get((int) (Math.random() * blist.size())); + } + + @Override + public boolean isStillValid() { + return false; + } + + @Override + public void onLoad() { + super.onLoad(); + } + + @Override + public void tick() { + if(!isAdd){ + isAdd = true; + SchedulerQueue.add(this); + } + } +} diff --git a/src/main/java/com/teammoeg/frostedheart/town/TownWorkerType.java b/src/main/java/com/teammoeg/frostedheart/town/TownWorkerType.java new file mode 100644 index 000000000..dd19fb7cb --- /dev/null +++ b/src/main/java/com/teammoeg/frostedheart/town/TownWorkerType.java @@ -0,0 +1,104 @@ +/* + * Copyright (c) 2024 TeamMoeg + * + * This file is part of Frosted Heart. + * + * Frosted Heart is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3. + * + * Frosted Heart is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Frosted Heart. If not, see . + * + */ + +package com.teammoeg.frostedheart.town; + +import com.teammoeg.frostedheart.FHBlocks; + +import net.minecraft.block.Block; + +// TODO: Auto-generated Javadoc + +/** + * The Enum TownWorkerType. + */ +public enum TownWorkerType { + + /** + * The dummy. + */ + DUMMY(null, null, -1), + HOUSE(FHBlocks.house.get(), (resource, workData) -> { + double cost = 1; + double actualCost = resource.cost(TownResourceType.PREP_FOOD, cost, false); + return cost == actualCost; + }, 0), + FARM(FHBlocks.farm.get(), (resource, workData) -> { + double cost = 1; + double actualCost = resource.cost(TownResourceType.PREP_FOOD, cost, false); + return cost == actualCost; + }, 0); + + /** + * Town block. + */ + private Block block; + + /** + * The worker. + */ + private TownWorker worker; + + /** + * The priority. + */ + private int priority; + + /** + * Instantiates a new town worker type. + * + * @param workerBlock the worker block + * @param worker the worker + * @param internalPriority the internal priority + */ + private TownWorkerType(Block workerBlock, TownWorker worker, int internalPriority) { + this.block = workerBlock; + this.worker = worker; + this.priority = internalPriority; + } + + /** + * Gets the block. + * + * @return the block + */ + public Block getBlock() { + return block; + } + + /** + * Gets the priority. + * + * @return the priority + */ + public int getPriority() { + return priority; + } + + /** + * Gets the worker. + * + * @return the worker + */ + public TownWorker getWorker() { + return worker; + } + + +}