From af79d450cb1511d98bb82f31caf2d9416982ade6 Mon Sep 17 00:00:00 2001 From: Gugusb <103099665+Gugusb@users.noreply.github.com> Date: Mon, 29 Jan 2024 20:04:43 +0800 Subject: [PATCH 1/7] Add the deep find for Farm --- .../com/teammoeg/frostedheart/FHBlocks.java | 8 ++ .../teammoeg/frostedheart/FHTileTypes.java | 5 +- .../climate/WorldTemperature.java | 2 +- .../frostedheart/town/Farm/FarmBlock.java | 71 ++++++++++ .../town/Farm/FarmBlockTileEntity.java | 133 ++++++++++++++++++ .../frostedheart/town/TownWorkerType.java | 5 + 6 files changed, 222 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/teammoeg/frostedheart/town/Farm/FarmBlock.java create mode 100644 src/main/java/com/teammoeg/frostedheart/town/Farm/FarmBlockTileEntity.java diff --git a/src/main/java/com/teammoeg/frostedheart/FHBlocks.java b/src/main/java/com/teammoeg/frostedheart/FHBlocks.java index 7f4ea7c28..3b16971e8 100644 --- a/src/main/java/com/teammoeg/frostedheart/FHBlocks.java +++ b/src/main/java/com/teammoeg/frostedheart/FHBlocks.java @@ -41,6 +41,7 @@ import com.teammoeg.frostedheart.research.machines.DrawingDeskBlock; import com.teammoeg.frostedheart.research.machines.MechCalcBlock; +import com.teammoeg.frostedheart.town.Farm.FarmBlock; import com.teammoeg.frostedheart.town.house.HouseBlock; import net.minecraft.block.Block; import net.minecraft.block.SoundType; @@ -156,6 +157,13 @@ public class FHBlocks { .harvestTool(ToolType.AXE) .hardnessAndResistance(2, 6) .notSolid(), FHBlockItem::new); + public static Block farm = new FarmBlock("farm_block", Block.Properties + .create(Material.WOOD) + .sound(SoundType.WOOD) + .setRequiresTool() + .harvestTool(ToolType.AXE) + .hardnessAndResistance(2, 6) + .notSolid(), FHBlockItem::new); public static void init() { } } \ No newline at end of file diff --git a/src/main/java/com/teammoeg/frostedheart/FHTileTypes.java b/src/main/java/com/teammoeg/frostedheart/FHTileTypes.java index 4136fafea..1331a14b8 100644 --- a/src/main/java/com/teammoeg/frostedheart/FHTileTypes.java +++ b/src/main/java/com/teammoeg/frostedheart/FHTileTypes.java @@ -39,6 +39,7 @@ 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 net.minecraft.block.Block; import net.minecraft.tileentity.TileEntity; @@ -105,7 +106,9 @@ public class FHTileTypes { public static final RegistryObject> HOUSE = REGISTER.register( "house", makeType(HouseTileEntity::new, () -> FHBlocks.house) ); - + 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/climate/WorldTemperature.java b/src/main/java/com/teammoeg/frostedheart/climate/WorldTemperature.java index d038dfe9b..32582b9e5 100644 --- a/src/main/java/com/teammoeg/frostedheart/climate/WorldTemperature.java +++ b/src/main/java/com/teammoeg/frostedheart/climate/WorldTemperature.java @@ -155,7 +155,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..143abff61 --- /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(String name, Properties blockProps, BiFunction createItemBlock) { + super(name, blockProps, createItemBlock); + } + + @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..699799177 --- /dev/null +++ b/src/main/java/com/teammoeg/frostedheart/town/Farm/FarmBlockTileEntity.java @@ -0,0 +1,133 @@ +package com.teammoeg.frostedheart.town.Farm; + +import com.ibm.icu.impl.Pair; +import com.teammoeg.frostedheart.FHTileTypes; +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.Block; +import net.minecraft.block.Blocks; +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 = 1000; + 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<>(); + } + + @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<>(); + queue.add(this.pos.add(0, -1, 0)); + blocks.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(blocks.size() > MAX_SIZE)break; + tempPos = queue.poll(); + for (Pair p : py) { + if(true){ + if(world.isAirBlock(tempPos.add(p.first, 1, p.second))){ + BlockPos pos = tempPos.add(p.first, 0, p.second); + Long key = pos.toLong(); + if(!blocks.containsKey(key)){ + queue.add(pos); + blocks.put(key, pos); + world.updateBlock(pos, Blocks.STONE); + } + } + } + } + } + if(blocks.size() >= MAX_SIZE){ + blocks.clear(); + }else{ + blocks.clear(); + 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(true){ + //System.out.println("Working"); + } + } + + @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 index 09cbc89c2..906949db4 100644 --- a/src/main/java/com/teammoeg/frostedheart/town/TownWorkerType.java +++ b/src/main/java/com/teammoeg/frostedheart/town/TownWorkerType.java @@ -37,6 +37,11 @@ public enum TownWorkerType { double cost = 1; double actualCost = resource.cost(TownResourceType.PREP_FOOD, cost, false); return cost == actualCost; + }, 0), + FARM(FHBlocks.farm, (resource, workData) -> { + double cost = 1; + double actualCost = resource.cost(TownResourceType.PREP_FOOD, cost, false); + return cost == actualCost; }, 0); /** From 982da39ed87627da414692609547a03b7474eb98 Mon Sep 17 00:00:00 2001 From: Gugusb <103099665+Gugusb@users.noreply.github.com> Date: Wed, 7 Feb 2024 18:40:32 +0800 Subject: [PATCH 2/7] Fix the deep find for Farm --- .../town/Farm/FarmBlockTileEntity.java | 31 +++++++++++-------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/teammoeg/frostedheart/town/Farm/FarmBlockTileEntity.java b/src/main/java/com/teammoeg/frostedheart/town/Farm/FarmBlockTileEntity.java index 699799177..893dca2a9 100644 --- a/src/main/java/com/teammoeg/frostedheart/town/Farm/FarmBlockTileEntity.java +++ b/src/main/java/com/teammoeg/frostedheart/town/Farm/FarmBlockTileEntity.java @@ -2,6 +2,7 @@ import com.ibm.icu.impl.Pair; import com.teammoeg.frostedheart.FHTileTypes; +import com.teammoeg.frostedheart.client.util.ClientUtils; import com.teammoeg.frostedheart.scheduler.IScheduledTaskTE; import com.teammoeg.frostedheart.scheduler.SchedulerQueue; import com.teammoeg.frostedheart.town.ITownBlockTE; @@ -23,7 +24,8 @@ public class FarmBlockTileEntity extends TileEntity implements ITownBlockTE, IScheduledTaskTE, ITickableTileEntity { private boolean isAdd; - public static int MAX_SIZE = 1000; + public static int MAX_SIZE = 576; + public static int MIN_SIZE = 4; public int size;//Size of the farm public int temperature; public Map blocks; @@ -64,31 +66,32 @@ public void setWorkData(CompoundNBT data) { private boolean checkFarm(){ Queue queue = new PriorityQueue<>(); + Map blockMap = new HashMap<>(); queue.add(this.pos.add(0, -1, 0)); - blocks.put(this.pos.add(0, -1, 0).toLong(), 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(blocks.size() > MAX_SIZE)break; + if(blockMap.size() > MAX_SIZE)break; tempPos = queue.poll(); for (Pair p : py) { - if(true){ - if(world.isAirBlock(tempPos.add(p.first, 1, p.second))){ + if(world.isAirBlock(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(!blocks.containsKey(key)){ + if(!blockMap.containsKey(key)){ queue.add(pos); - blocks.put(key, pos); - world.updateBlock(pos, Blocks.STONE); + blockMap.put(key, pos); + world.setBlockState(pos, Blocks.GOLD_BLOCK.getDefaultState()); } } } } } - if(blocks.size() >= MAX_SIZE){ - blocks.clear(); + if(blockMap.size() >= MAX_SIZE && blockMap.size() <= MIN_SIZE){ + blockMap.clear(); }else{ - blocks.clear(); + blockMap.clear(); return true; } return false; @@ -108,8 +111,10 @@ public ActionResultType onClick(PlayerEntity pe, ItemStack is){ @Override public void executeTask() { - if(true){ - //System.out.println("Working"); + if(checkFarm()){ + if (world != null && world.isRemote) { + ClientUtils.spawnSteamParticles(world, pos.add(0, 1, 0)); + } } } From 65011dd93afeff330537d26cd0d232d895292c80 Mon Sep 17 00:00:00 2001 From: Gugusb <103099665+Gugusb@users.noreply.github.com> Date: Mon, 12 Feb 2024 10:36:29 +0800 Subject: [PATCH 3/7] Fix problems from merge --- src/main/java/com/teammoeg/frostedheart/FHBlocks.java | 10 +++++----- .../java/com/teammoeg/frostedheart/FHTileTypes.java | 2 +- .../com/teammoeg/frostedheart/town/Farm/FarmBlock.java | 4 ++-- .../com/teammoeg/frostedheart/town/TownWorkerType.java | 2 +- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/teammoeg/frostedheart/FHBlocks.java b/src/main/java/com/teammoeg/frostedheart/FHBlocks.java index 6dc416da0..aa494e5fe 100644 --- a/src/main/java/com/teammoeg/frostedheart/FHBlocks.java +++ b/src/main/java/com/teammoeg/frostedheart/FHBlocks.java @@ -190,21 +190,21 @@ public static RegistryObject register(String name,Supplier< .transform(customItemModel()) .register(); public static RegistryObject house = register("house",()->new HouseBlock( Block.Properties - .notSolid(), FHBlockItem::new); - public static Block house = new HouseBlock("house", Block.Properties .create(Material.WOOD) .sound(SoundType.WOOD) .setRequiresTool() .harvestTool(ToolType.AXE) .hardnessAndResistance(2, 6) - .notSolid(), FHBlockItem::new); - public static Block farm = new FarmBlock("farm_block", Block.Properties + .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(), FHBlockItem::new); + .notSolid())); + public static void init() { } } \ No newline at end of file diff --git a/src/main/java/com/teammoeg/frostedheart/FHTileTypes.java b/src/main/java/com/teammoeg/frostedheart/FHTileTypes.java index 8570c7d80..ba068ac37 100644 --- a/src/main/java/com/teammoeg/frostedheart/FHTileTypes.java +++ b/src/main/java/com/teammoeg/frostedheart/FHTileTypes.java @@ -118,7 +118,7 @@ public class FHTileTypes { "house", makeType(HouseTileEntity::new, FHBlocks.house) ); public static final RegistryObject> FARM = REGISTER.register( - "farm_block", makeType(FarmBlockTileEntity::new, () -> FHBlocks.farm) + "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/town/Farm/FarmBlock.java b/src/main/java/com/teammoeg/frostedheart/town/Farm/FarmBlock.java index 143abff61..ed253a192 100644 --- a/src/main/java/com/teammoeg/frostedheart/town/Farm/FarmBlock.java +++ b/src/main/java/com/teammoeg/frostedheart/town/Farm/FarmBlock.java @@ -21,8 +21,8 @@ import java.util.function.BiFunction; public class FarmBlock extends FHBaseBlock { - public FarmBlock(String name, Properties blockProps, BiFunction createItemBlock) { - super(name, blockProps, createItemBlock); + public FarmBlock(Properties blockProps) { + super(blockProps); } @Override diff --git a/src/main/java/com/teammoeg/frostedheart/town/TownWorkerType.java b/src/main/java/com/teammoeg/frostedheart/town/TownWorkerType.java index b27013f72..dd19fb7cb 100644 --- a/src/main/java/com/teammoeg/frostedheart/town/TownWorkerType.java +++ b/src/main/java/com/teammoeg/frostedheart/town/TownWorkerType.java @@ -39,7 +39,7 @@ public enum TownWorkerType { double actualCost = resource.cost(TownResourceType.PREP_FOOD, cost, false); return cost == actualCost; }, 0), - FARM(FHBlocks.farm, (resource, workData) -> { + FARM(FHBlocks.farm.get(), (resource, workData) -> { double cost = 1; double actualCost = resource.cost(TownResourceType.PREP_FOOD, cost, false); return cost == actualCost; From e1979741a23b140f8abf114c8c6814ae192426ba Mon Sep 17 00:00:00 2001 From: Gugusb <103099665+Gugusb@users.noreply.github.com> Date: Mon, 12 Feb 2024 11:13:06 +0800 Subject: [PATCH 4/7] add useful block check --- .../frostedheart/town/Farm/FarmBlockTileEntity.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/teammoeg/frostedheart/town/Farm/FarmBlockTileEntity.java b/src/main/java/com/teammoeg/frostedheart/town/Farm/FarmBlockTileEntity.java index 893dca2a9..d85b08316 100644 --- a/src/main/java/com/teammoeg/frostedheart/town/Farm/FarmBlockTileEntity.java +++ b/src/main/java/com/teammoeg/frostedheart/town/Farm/FarmBlockTileEntity.java @@ -75,7 +75,7 @@ private boolean checkFarm(){ if(blockMap.size() > MAX_SIZE)break; tempPos = queue.poll(); for (Pair p : py) { - if(world.isAirBlock(tempPos.add(p.first, 1, p.second))){ + 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(); @@ -88,7 +88,7 @@ private boolean checkFarm(){ } } } - if(blockMap.size() >= MAX_SIZE && blockMap.size() <= MIN_SIZE){ + if(blockMap.size() >= MAX_SIZE || blockMap.size() <= MIN_SIZE){ blockMap.clear(); }else{ blockMap.clear(); @@ -97,6 +97,13 @@ private boolean checkFarm(){ return false; } + private boolean isUsefulBlock(BlockPos pos){ + //fence wall + String name = world.getBlockState(pos).getBlock().getTranslationKey(); + if(name.contains("fence") || name.contains("wall"))return true; + return false; + } + public ActionResultType onClick(PlayerEntity pe, ItemStack is){ if(is != null){ System.out.println("Farm Clicked"); From e424f3d653ff457ae1e604d72c6c653bf94bd8bd Mon Sep 17 00:00:00 2001 From: Gugusb <103099665+Gugusb@users.noreply.github.com> Date: Mon, 12 Feb 2024 17:08:40 +0800 Subject: [PATCH 5/7] farm block can really do farm now --- .../town/Farm/FarmBlockTileEntity.java | 37 +++++++++++++++++-- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/teammoeg/frostedheart/town/Farm/FarmBlockTileEntity.java b/src/main/java/com/teammoeg/frostedheart/town/Farm/FarmBlockTileEntity.java index d85b08316..03e335df8 100644 --- a/src/main/java/com/teammoeg/frostedheart/town/Farm/FarmBlockTileEntity.java +++ b/src/main/java/com/teammoeg/frostedheart/town/Farm/FarmBlockTileEntity.java @@ -1,6 +1,8 @@ package com.teammoeg.frostedheart.town.Farm; +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.scheduler.IScheduledTaskTE; @@ -82,7 +84,7 @@ private boolean checkFarm(){ if(!blockMap.containsKey(key)){ queue.add(pos); blockMap.put(key, pos); - world.setBlockState(pos, Blocks.GOLD_BLOCK.getDefaultState()); + //world.setBlockState(pos, Blocks.GOLD_BLOCK.getDefaultState()); } } } @@ -91,6 +93,9 @@ private boolean checkFarm(){ 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; } @@ -119,8 +124,34 @@ public ActionResultType onClick(PlayerEntity pe, ItemStack is){ @Override public void executeTask() { if(checkFarm()){ - if (world != null && world.isRemote) { - ClientUtils.spawnSteamParticles(world, pos.add(0, 1, 0)); + if (world != null) { + if(Math.random() < 0.05){ + System.out.println("Size:" + this.blocks.size()); + if(this.blocks.size() > 0){ + List list = new ArrayList(blocks.values()); + int pc = (int) (list.size() * Math.random()); + System.out.println("Change block" + pc + "/" + list.size()); + doFarm(list.get(pc)); + } + } + //ClientUtils.spawnSteamParticles(world, pos.add(0, 1, 0)); + } + } + } + + private void doFarm(BlockPos pos){ + Map> blocksMap = new HashMap<>(); + blocksMap.put(Blocks.GOLD_BLOCK, Pair.of(Blocks.DIRT, 1.0)); + blocksMap.put(Blocks.SANDSTONE, Pair.of(Blocks.DIRT, 1.0)); + + blocksMap.put(Blocks.DIRT, Pair.of(Blocks.FARMLAND, 1.0)); + blocksMap.put(Blocks.COARSE_DIRT, Pair.of(Blocks.DIRT, 1.0)); + blocksMap.put(ModBlocks.SNOWY_COARSE_DIRT.get(), Pair.of(Blocks.DIRT, 1.0)); + blocksMap.put(ModBlocks.SNOWY_DIRT.get(), Pair.of(Blocks.DIRT, 1.0)); + + if(blocksMap.containsKey(world.getBlockState(pos).getBlock())){ + if(Math.random() < blocksMap.get(world.getBlockState(pos).getBlock()).second){ + world.setBlockState(pos, blocksMap.get(world.getBlockState(pos).getBlock()).first.getDefaultState()); } } } From b211e29fb8aa17d62c4bf35c831abac940ed4710 Mon Sep 17 00:00:00 2001 From: Gugusb <103099665+Gugusb@users.noreply.github.com> Date: Fri, 16 Feb 2024 11:05:41 +0800 Subject: [PATCH 6/7] farm block can plant crop now --- .../town/Farm/FarmBlockTileEntity.java | 75 +++++++++++++------ 1 file changed, 53 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/teammoeg/frostedheart/town/Farm/FarmBlockTileEntity.java b/src/main/java/com/teammoeg/frostedheart/town/Farm/FarmBlockTileEntity.java index 03e335df8..5da85bc5e 100644 --- a/src/main/java/com/teammoeg/frostedheart/town/Farm/FarmBlockTileEntity.java +++ b/src/main/java/com/teammoeg/frostedheart/town/Farm/FarmBlockTileEntity.java @@ -1,17 +1,24 @@ 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.Block; -import net.minecraft.block.Blocks; +import net.minecraft.block.*; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.ItemStack; import net.minecraft.nbt.CompoundNBT; @@ -28,6 +35,9 @@ public class FarmBlockTileEntity extends TileEntity implements ITownBlockTE, ISc 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; @@ -35,6 +45,20 @@ 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 @@ -84,7 +108,6 @@ private boolean checkFarm(){ if(!blockMap.containsKey(key)){ queue.add(pos); blockMap.put(key, pos); - //world.setBlockState(pos, Blocks.GOLD_BLOCK.getDefaultState()); } } } @@ -103,9 +126,9 @@ private boolean checkFarm(){ } private boolean isUsefulBlock(BlockPos pos){ - //fence wall - String name = world.getBlockState(pos).getBlock().getTranslationKey(); - if(name.contains("fence") || name.contains("wall"))return true; + assert world != null : "Empty world"; + Block block = world.getBlockState(pos).getBlock(); + if(block instanceof FenceBlock || block instanceof WallBlock)return true; return false; } @@ -125,35 +148,43 @@ public ActionResultType onClick(PlayerEntity pe, ItemStack is){ public void executeTask() { if(checkFarm()){ if (world != null) { - if(Math.random() < 0.05){ - System.out.println("Size:" + this.blocks.size()); + 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()); - System.out.println("Change block" + pc + "/" + list.size()); doFarm(list.get(pc)); } } - //ClientUtils.spawnSteamParticles(world, pos.add(0, 1, 0)); } } } private void doFarm(BlockPos pos){ - Map> blocksMap = new HashMap<>(); - blocksMap.put(Blocks.GOLD_BLOCK, Pair.of(Blocks.DIRT, 1.0)); - blocksMap.put(Blocks.SANDSTONE, Pair.of(Blocks.DIRT, 1.0)); - - blocksMap.put(Blocks.DIRT, Pair.of(Blocks.FARMLAND, 1.0)); - blocksMap.put(Blocks.COARSE_DIRT, Pair.of(Blocks.DIRT, 1.0)); - blocksMap.put(ModBlocks.SNOWY_COARSE_DIRT.get(), Pair.of(Blocks.DIRT, 1.0)); - blocksMap.put(ModBlocks.SNOWY_DIRT.get(), Pair.of(Blocks.DIRT, 1.0)); - - if(blocksMap.containsKey(world.getBlockState(pos).getBlock())){ - if(Math.random() < blocksMap.get(world.getBlockState(pos).getBlock()).second){ - world.setBlockState(pos, blocksMap.get(world.getBlockState(pos).getBlock()).first.getDefaultState()); + 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 From 4def4d63a8a24f6b4bf9980ebce68ce71fd11f33 Mon Sep 17 00:00:00 2001 From: Gugusb <103099665+Gugusb@users.noreply.github.com> Date: Thu, 11 Apr 2024 10:46:33 +0800 Subject: [PATCH 7/7] farm block can plant crop now --- .../teammoeg/frostedheart/town/Farm/FarmBlockTileEntity.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/teammoeg/frostedheart/town/Farm/FarmBlockTileEntity.java b/src/main/java/com/teammoeg/frostedheart/town/Farm/FarmBlockTileEntity.java index 5da85bc5e..a5bb07869 100644 --- a/src/main/java/com/teammoeg/frostedheart/town/Farm/FarmBlockTileEntity.java +++ b/src/main/java/com/teammoeg/frostedheart/town/Farm/FarmBlockTileEntity.java @@ -128,7 +128,7 @@ private boolean checkFarm(){ private boolean isUsefulBlock(BlockPos pos){ assert world != null : "Empty world"; Block block = world.getBlockState(pos).getBlock(); - if(block instanceof FenceBlock || block instanceof WallBlock)return true; + if(block instanceof FenceBlock || block instanceof WallBlock ||block instanceof FenceGateBlock)return true; return false; }