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

修复催熟卡爆服务端 #955

Merged
merged 9 commits into from
Jun 24, 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
115 changes: 0 additions & 115 deletions common/src/main/java/dev/dubhe/anvilcraft/api/BonemealManager.java

This file was deleted.

130 changes: 130 additions & 0 deletions common/src/main/java/dev/dubhe/anvilcraft/api/RipeningManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package dev.dubhe.anvilcraft.api;

import com.mojang.logging.LogUtils;
import dev.dubhe.anvilcraft.AnvilCraft;
import dev.dubhe.anvilcraft.block.InductionLightBlock;
import dev.dubhe.anvilcraft.block.state.LightColor;
import net.minecraft.core.BlockPos;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.TickTask;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LightLayer;
import net.minecraft.world.level.block.BonemealableBlock;
import net.minecraft.world.level.block.GrassBlock;
import net.minecraft.world.level.block.NyliumBlock;
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;


public class RipeningManager {
private static final Map<Level, RipeningManager> INSTANCES = new HashMap<>();

private static int cooldown = 0;
private final Level level;
private final Set<BlockPos> lightBlocks = Collections.synchronizedSet(new HashSet<>());

/**
* 获取当前维度催熟实例
*/
public static RipeningManager getInstance(Level level) {
if (!INSTANCES.containsKey(level)) {
INSTANCES.put(level, new RipeningManager(level));
}
return INSTANCES.get(level);
}


public RipeningManager(Level level) {
this.level = level;
}

public static void tickAll() {
INSTANCES.values().forEach(RipeningManager::tick);
}

private static boolean isLit(@NotNull BlockState state) {
return !(state.getValue(InductionLightBlock.POWERED) || state.getValue(InductionLightBlock.OVERLOAD));
}

private static boolean canCropGrow(@NotNull BlockState state) {
return state.getValue(InductionLightBlock.COLOR).equals(LightColor.PINK);
}

private void doRipen(
@NotNull BlockPos pos,
@NotNull HashSet<BlockPos> ripened
) {
int rangeSize = AnvilCraft.config.inductionLightBlockRipeningRange;
for (int dx = -rangeSize / 2; dx <= rangeSize / 2; dx++) {
for (int dy = -rangeSize / 2; dy <= rangeSize / 2; dy++) {
for (int dz = -rangeSize / 2; dz <= rangeSize / 2; dz++) {
BlockPos pos1 = pos.offset(dx, dy, dz);
if (ripened.contains(pos1)) continue;
BlockState state = level.getBlockState(pos1);
if (state.getBlock() instanceof BonemealableBlock growable
&& !(growable instanceof GrassBlock)
&& !(growable instanceof NyliumBlock)
&& growable.isValidBonemealTarget(level, pos1, state, false)
&& level.getBrightness(LightLayer.BLOCK, pos1) >= 10
) {
growable.performBonemeal((ServerLevel) level, level.getRandom(), pos1, state);
level.addParticle(
ParticleTypes.HAPPY_VILLAGER,
pos1.getX() + 0.5,
pos1.getY() + 0.5,
pos1.getZ() + 0.5,
0.0, 0.0, 0.0);
ripened.add(pos1);
}
}
}
}
}

/**
*
*/
private void tick() {
cooldown--;

if (cooldown <= 0 && !lightBlocks.isEmpty()) {
MinecraftServer server = level.getServer();
if (server == null) return;
server.tell(new TickTask(server.getTickCount(), () -> {
HashSet<BlockPos> ripenedBlocks = new HashSet<>();
Iterator<BlockPos> it = lightBlocks.iterator();
while (it.hasNext()) {
BlockPos pos = it.next();
BlockState lightBlockState = level.getBlockState(pos);
if (lightBlockState.getBlock() instanceof InductionLightBlock
&& isLit(lightBlockState)
&& canCropGrow(lightBlockState)
) {
doRipen(pos, ripenedBlocks);
} else {
it.remove();
}
}
}));
cooldown = AnvilCraft.config.inductionLightBlockRipeningCooldown;
}
}

/**
*
*/
public static void addLightBlock(BlockPos pos, Level level) {
RipeningManager inst = RipeningManager.getInstance(level);
inst.lightBlocks.add(pos);
}
}
Original file line number Diff line number Diff line change
@@ -1,47 +1,67 @@
package dev.dubhe.anvilcraft.api.chargecollector;

import dev.dubhe.anvilcraft.block.entity.ChargeCollectorBlockEntity;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import lombok.Getter;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import org.joml.Vector3f;

public class ChargeCollectorManager {
private static final Map<BlockPos, ChargeCollectorBlockEntity> CHARGE_COLLECTOR_MAP = new HashMap<>();
private final Map<BlockPos, ChargeCollectorBlockEntity> chargeCollectors = new HashMap<>();
private static final Map<Level, ChargeCollectorManager> INSTANCES = new HashMap<>();

private final Level level;

public ChargeCollectorManager(Level level) {
this.level = level;
}

/**
* 获取当前维度的ChargeCollectorManager
*/
public static ChargeCollectorManager getInstance(Level level) {
if (!INSTANCES.containsKey(level)) {
INSTANCES.put(level, new ChargeCollectorManager(level));
}
return INSTANCES.get(level);
}

/**
* 添加新的集电器
*/
public static void addChargeCollector(ChargeCollectorBlockEntity blockEntity) {
CHARGE_COLLECTOR_MAP.put(blockEntity.getBlockPos(), blockEntity);
public void addChargeCollector(ChargeCollectorBlockEntity blockEntity) {
chargeCollectors.put(blockEntity.getBlockPos(), blockEntity);
}

/**
* 删除集电器
*/
public static void removeChargeCollector(ChargeCollectorBlockEntity blockEntity) {
CHARGE_COLLECTOR_MAP.remove(blockEntity.getBlockPos());
public void removeChargeCollector(ChargeCollectorBlockEntity blockEntity) {
chargeCollectors.remove(blockEntity.getBlockPos());
}

/**
* 获取最近的集电器的Collection集合(以从近至远排序)
*/
public static Collection<Entry> getNearestChargeCollect(BlockPos blockPos) {
public Collection<Entry> getNearestChargeCollect(BlockPos blockPos) {
List<Entry> distanceList = new ArrayList<>();
for (Map.Entry<BlockPos, ChargeCollectorBlockEntity> entry : CHARGE_COLLECTOR_MAP.entrySet()) {
for (Map.Entry<BlockPos, ChargeCollectorBlockEntity> entry : chargeCollectors.entrySet()) {
double distance = Vector3f.distance(
entry.getKey().getX(), entry.getKey().getY(), entry.getKey().getZ(),
blockPos.getX(), blockPos.getY(), blockPos.getZ());
entry.getKey().getX(), entry.getKey().getY(), entry.getKey().getZ(),
blockPos.getX(), blockPos.getY(), blockPos.getZ());
distanceList.add(new Entry(distance, entry.getValue()));
}
return distanceList.stream()
.sorted(Comparator.comparing(Entry::getDistance)).collect(Collectors.toList());
.sorted(Comparator.comparing(Entry::getDistance)).collect(Collectors.toList());
}

/**
Expand All @@ -51,21 +71,17 @@ public static Collection<Entry> getNearestChargeCollect(BlockPos blockPos) {
* @param blockPos 电荷的位置
* @return 是否能被集点器收集
*/
public static boolean canCollect(ChargeCollectorBlockEntity blockEntity, BlockPos blockPos) {
public boolean canCollect(ChargeCollectorBlockEntity blockEntity, BlockPos blockPos) {
return blockEntity.getPos().getX() - 2 <= blockPos.getX()
&& blockEntity.getPos().getY() - 2 <= blockPos.getY()
&& blockEntity.getPos().getZ() - 2 <= blockPos.getZ()
&& blockEntity.getPos().getX() + 2 >= blockPos.getX()
&& blockEntity.getPos().getY() + 2 >= blockPos.getY()
&& blockEntity.getPos().getZ() + 2 >= blockPos.getZ();
}

public static void cleanMap() {
CHARGE_COLLECTOR_MAP.clear();
&& blockEntity.getPos().getY() - 2 <= blockPos.getY()
&& blockEntity.getPos().getZ() - 2 <= blockPos.getZ()
&& blockEntity.getPos().getX() + 2 >= blockPos.getX()
&& blockEntity.getPos().getY() + 2 >= blockPos.getY()
&& blockEntity.getPos().getZ() + 2 >= blockPos.getZ();
}

@Getter
public static class Entry {
public class Entry {
public final double distance;
public final ChargeCollectorBlockEntity blockEntity;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,12 @@ private void tickThis() {

private void charge(int chargeNum, BlockPos blockPos) {
Collection<ChargeCollectorManager.Entry> chargeCollectorCollection = ChargeCollectorManager
.getInstance(level)
.getNearestChargeCollect(blockPos);
double surplus = chargeNum;
for (ChargeCollectorManager.Entry entry : chargeCollectorCollection) {
ChargeCollectorBlockEntity chargeCollectorBlockEntity = entry.getBlockEntity();
if (!ChargeCollectorManager.canCollect(chargeCollectorBlockEntity, blockPos)) return;
if (!ChargeCollectorManager.getInstance(level).canCollect(chargeCollectorBlockEntity, blockPos)) return;
surplus = chargeCollectorBlockEntity.incomingCharge(surplus);
if (surplus == 0) return;
}
Expand Down
Loading
Loading