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

FarmBlock dev #170

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
16 changes: 16 additions & 0 deletions src/main/java/com/teammoeg/frostedheart/FHBlocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,22 @@ public static <T extends Block> RegistryObject<T> register(String name,Supplier<
.item()
.transform(customItemModel())
.register();
public static RegistryObject<Block> house = register("house",()->new HouseBlock( Block.Properties
.create(Material.WOOD)
.sound(SoundType.WOOD)
.setRequiresTool()
.harvestTool(ToolType.AXE)
.hardnessAndResistance(2, 6)
.notSolid()));

public static RegistryObject<Block> 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);
}
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/teammoeg/frostedheart/FHTileTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -131,6 +136,9 @@ public class FHTileTypes {
"mine_base", makeType(MineBaseTileEntity::new, FHBlocks.mine_base)
);

public static final RegistryObject<TileEntityType<FarmBlockTileEntity>> FARM = REGISTER.register(
"farm_block", makeType(FarmBlockTileEntity::new, FHBlocks.farm)
);
private static <T extends TileEntity> Supplier<TileEntityType<T>> makeType(Supplier<T> create, Supplier<Block> valid) {
return makeTypeMultipleBlocks(create, () -> ImmutableSet.of(valid.get()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
71 changes: 71 additions & 0 deletions src/main/java/com/teammoeg/frostedheart/town/Farm/FarmBlock.java
Original file line number Diff line number Diff line change
@@ -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
系统就会自动调度了


*/
Original file line number Diff line number Diff line change
@@ -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<Block, Pair<Block, Double>> blockTransMap;
public List<Pair<Block, Float>> blocksForPlant;
public int size;//Size of the farm
public int temperature;
public Map<Long, BlockPos> 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<BlockPos> queue = new PriorityQueue<>();
Map<Long, BlockPos> 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<Pair<Integer, Integer>> 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<Integer, Integer> 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<BlockPos> 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<Block> blist = new Stack<>();
for(Pair<Block, Float> 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);
}
}
}
Loading