227 lines
8.7 KiB
Java
227 lines
8.7 KiB
Java
package com.dairymoose.modernlife.tileentities;
|
|
|
|
import com.dairymoose.entity.projectile.ThrownSeedEntity;
|
|
import com.dairymoose.modernlife.core.CustomBlocks;
|
|
import com.mojang.datafixers.types.Type;
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import net.minecraft.core.BlockPos;
|
|
import net.minecraft.core.NonNullList;
|
|
import net.minecraft.nbt.CompoundTag;
|
|
import net.minecraft.resources.ResourceLocation;
|
|
import net.minecraft.tags.ItemTags;
|
|
import net.minecraft.tags.TagKey;
|
|
import net.minecraft.world.Container;
|
|
import net.minecraft.world.ContainerHelper;
|
|
import net.minecraft.world.entity.player.Player;
|
|
import net.minecraft.world.item.Item;
|
|
import net.minecraft.world.item.ItemStack;
|
|
import net.minecraft.world.item.Items;
|
|
import net.minecraft.world.level.Level;
|
|
import net.minecraft.world.level.block.Block;
|
|
import net.minecraft.world.level.block.Blocks;
|
|
import net.minecraft.world.level.block.entity.BlockEntity;
|
|
import net.minecraft.world.level.block.entity.BlockEntityType;
|
|
import net.minecraft.world.level.block.state.BlockState;
|
|
import org.apache.logging.log4j.LogManager;
|
|
import org.apache.logging.log4j.Logger;
|
|
|
|
/* loaded from: outputsrg.jar:com/dairymoose/modernlife/tileentities/SeedSpreaderBlockEntity.class */
|
|
public class SeedSpreaderBlockEntity extends BlockEntity implements Container {
|
|
public static final int SEED_SPREADER_CONTAINER_SIZE = 9;
|
|
private NonNullList<ItemStack> items;
|
|
private TagKey<Item> seedTag;
|
|
final int DIAMETER = 9;
|
|
final int VERTICAL_SPAN = 5;
|
|
public static final double LAUNCHER_HEIGHT = 0.109375d;
|
|
public static final double LAUNCHER_HEIGHT_SAME_LEVEL = 0.75d;
|
|
public static final double LAUNCHER_RADIUS = 0.5d;
|
|
int counter;
|
|
Map<BlockPos, Long> recentLocations;
|
|
public static final double PROJECTILE_GRAVITY = 0.03d;
|
|
public static final BlockEntityType<SeedSpreaderBlockEntity> SEED_SPREADER = BlockEntityType.Builder.of(SeedSpreaderBlockEntity::new, new Block[]{(Block) CustomBlocks.BLOCK_SEED_SPREADER.get()}).build((Type) null);
|
|
private static final Logger LOGGER = LogManager.getLogger();
|
|
public static int RECENT_LOCATIONS_EXPIRY_MS = 5000;
|
|
public static List<Item> extraItems = new ArrayList();
|
|
|
|
static {
|
|
extraItems.add(Items.NETHER_WART);
|
|
extraItems.add(Items.POTATO);
|
|
extraItems.add(Items.CARROT);
|
|
}
|
|
|
|
public SeedSpreaderBlockEntity(BlockPos pos, BlockState state) {
|
|
super(SEED_SPREADER, pos, state);
|
|
this.items = NonNullList.withSize(9, ItemStack.EMPTY);
|
|
this.seedTag = ItemTags.create(new ResourceLocation("forge", "seeds"));
|
|
this.DIAMETER = 9;
|
|
this.VERTICAL_SPAN = 5;
|
|
this.counter = 0;
|
|
this.recentLocations = new HashMap();
|
|
}
|
|
|
|
double freeFallTime(double h, double g) {
|
|
return Math.sqrt((2.0d * h) / g);
|
|
}
|
|
|
|
void launchTo(ThrownSeedEntity thrownSeed, double x, double y, double z, BlockPos pos) {
|
|
this.recentLocations.put(pos, Long.valueOf(System.currentTimeMillis()));
|
|
double xMid = pos.getX() + 0.5d;
|
|
double zMid = pos.getZ() + 0.5d;
|
|
double xDiff = xMid - x;
|
|
double zDiff = zMid - z;
|
|
double launchYaw = Math.atan2(zDiff, xDiff);
|
|
double launchX = (Math.cos(launchYaw) * 0.5d) + x;
|
|
double launchZ = (Math.sin(launchYaw) * 0.5d) + z;
|
|
double xDiffNew = xMid - launchX;
|
|
double zDiffNew = zMid - launchZ;
|
|
double yDiff = y - pos.getY();
|
|
double time = freeFallTime(yDiff, 0.03d) - 3.0d;
|
|
double Vx = xDiffNew / time;
|
|
double Vz = zDiffNew / time;
|
|
thrownSeed.moveTo(launchX, y, launchZ);
|
|
double v = Math.sqrt((Vx * Vx) + (Vz * Vz));
|
|
thrownSeed.m_6686_(Vx, 0.0d, Vz, (float) v, 0.0f);
|
|
}
|
|
|
|
public BlockPos getNextFarmland(Level world, BlockPos start, ItemStack seedStack) {
|
|
List<BlockPos> farmlandSpots = new ArrayList<>();
|
|
for (int x = -4; x <= 4; x++) {
|
|
for (int z = -4; z <= 4; z++) {
|
|
if (x != 0 || z != 0) {
|
|
BlockPos current = new BlockPos(start).east(x).south(z);
|
|
BlockState state = world.getBlockState(current);
|
|
BlockState aboveState = world.getBlockState(current.above());
|
|
if (seedStack.getItem() == Items.NETHER_WART) {
|
|
if (state.is(Blocks.SOUL_SAND) && aboveState.isAir() && !this.recentLocations.containsKey(current)) {
|
|
farmlandSpots.add(current);
|
|
}
|
|
} else if (state.is(Blocks.FARMLAND) && aboveState.isAir() && !this.recentLocations.containsKey(current)) {
|
|
farmlandSpots.add(current);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (farmlandSpots.size() == 0) {
|
|
return null;
|
|
}
|
|
return farmlandSpots.get((int) (Math.random() * farmlandSpots.size()));
|
|
}
|
|
|
|
private void purgeRecentLocations() {
|
|
long currentTime = System.currentTimeMillis();
|
|
List<BlockPos> toPurge = new ArrayList<>();
|
|
for (Map.Entry<BlockPos, Long> entry : this.recentLocations.entrySet()) {
|
|
BlockPos pos = entry.getKey();
|
|
long time = entry.getValue().longValue();
|
|
long timeDiff = currentTime - time;
|
|
if (timeDiff > RECENT_LOCATIONS_EXPIRY_MS) {
|
|
toPurge.add(pos);
|
|
}
|
|
}
|
|
for (BlockPos pos2 : toPurge) {
|
|
this.recentLocations.remove(pos2);
|
|
}
|
|
}
|
|
|
|
public ItemStack getSeedFromInventory() {
|
|
for (int i = 0; i < getContainerSize(); i++) {
|
|
ItemStack itemStack = getItem(i);
|
|
if (itemStack.is(this.seedTag) || extraItems.contains(itemStack.getItem())) {
|
|
return itemStack;
|
|
}
|
|
}
|
|
return ItemStack.EMPTY;
|
|
}
|
|
|
|
public static void tick(Level level, BlockPos pos, BlockState state, SeedSpreaderBlockEntity entity) {
|
|
entity.tick();
|
|
}
|
|
|
|
public void tick() {
|
|
this.counter++;
|
|
if (this.counter % 8 == 0) {
|
|
BlockPos pos = getBlockPos();
|
|
double x = pos.getX() + 0.5d;
|
|
double y = pos.getY() + 0.109375d;
|
|
double z = pos.getZ() + 0.5d;
|
|
ItemStack seedStack = getSeedFromInventory();
|
|
if (seedStack != ItemStack.EMPTY) {
|
|
purgeRecentLocations();
|
|
BlockPos farmlandPos = null;
|
|
for (int yDelta = 0; yDelta < 5; yDelta++) {
|
|
BlockPos toCheck = new BlockPos(pos);
|
|
farmlandPos = getNextFarmland(this.level, toCheck.below(yDelta), seedStack);
|
|
if (farmlandPos != null) {
|
|
break;
|
|
}
|
|
}
|
|
if (farmlandPos != null) {
|
|
ThrownSeedEntity lvt_5_1_ = new ThrownSeedEntity(getLevel(), x, y, z);
|
|
ItemStack seed = seedStack.split(1);
|
|
lvt_5_1_.setItem(seed);
|
|
if (farmlandPos.above().getY() == pos.getY()) {
|
|
y = pos.getY() + 0.75d;
|
|
}
|
|
launchTo(lvt_5_1_, x, y, z, farmlandPos);
|
|
getLevel().m_7967_(lvt_5_1_);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void load(CompoundTag p_230337_2_) {
|
|
super.load(p_230337_2_);
|
|
ContainerHelper.loadAllItems(p_230337_2_, this.items);
|
|
}
|
|
|
|
protected void saveAdditional(CompoundTag p_189515_1_) {
|
|
ContainerHelper.saveAllItems(p_189515_1_, this.items);
|
|
}
|
|
|
|
public void m_6211_() {
|
|
for (int i = 0; i < this.items.size(); i++) {
|
|
this.items.set(i, ItemStack.EMPTY);
|
|
}
|
|
}
|
|
|
|
public int getContainerSize() {
|
|
return 9;
|
|
}
|
|
|
|
public boolean isEmpty() {
|
|
for (int i = 0; i < this.items.size(); i++) {
|
|
if (this.items.get(i) != ItemStack.EMPTY) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public ItemStack getItem(int var1) {
|
|
setChanged();
|
|
return (ItemStack) this.items.get(var1);
|
|
}
|
|
|
|
public ItemStack removeItem(int var1, int var2) {
|
|
setChanged();
|
|
return ContainerHelper.removeItem(this.items, var1, var2);
|
|
}
|
|
|
|
public ItemStack removeItemNoUpdate(int var1) {
|
|
setChanged();
|
|
return ContainerHelper.takeItem(this.items, var1);
|
|
}
|
|
|
|
public void setItem(int var1, ItemStack var2) {
|
|
this.items.set(var1, var2);
|
|
setChanged();
|
|
}
|
|
|
|
public boolean stillValid(Player p_70300_1_) {
|
|
return this.level.getBlockEntity(this.worldPosition) == this && p_70300_1_.distanceToSqr(((double) this.worldPosition.getX()) + 0.5d, ((double) this.worldPosition.getY()) + 0.5d, ((double) this.worldPosition.getZ()) + 0.5d) <= 64.0d;
|
|
}
|
|
}
|