Skip to content

Commit

Permalink
Merge pull request #439 from Anvil-Dev/charge
Browse files Browse the repository at this point in the history
添加两种简易电荷获取方式
  • Loading branch information
DancingSnow0517 authored Apr 28, 2024
2 parents ebb6adb + 531175c commit 25ee830
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package dev.dubhe.anvilcraft.api.chargecollector;

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

import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;

import net.minecraft.core.BlockPos;
import org.joml.Vector3f;

Expand Down Expand Up @@ -34,11 +35,29 @@ public static Collection<Entry<Float, ChargeCollectorBlockEntity>> getNearestCha
Map<Float, ChargeCollectorBlockEntity> distanceMap = new HashMap<>();
for (Map.Entry<BlockPos, ChargeCollectorBlockEntity> entry : CHARGE_COLLECTOR_MAP.entrySet()) {
float 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());
distanceMap.put(distance, entry.getValue());
}
return distanceMap.entrySet().stream()
.sorted(Comparator.comparing(Entry::getKey)).collect(Collectors.toList());
.sorted(Entry.comparingByKey()).collect(Collectors.toList());
}

/**
* 判断是否能被集电器收集
*
* @param blockEntity 集电器方块实体
* @param blockPos 电荷的位置
* @return 是否能被集点器收集
*/
public static boolean canCollect(ChargeCollectorBlockEntity blockEntity, BlockPos blockPos) {
System.out.println(blockEntity.getPos());
System.out.println(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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import dev.dubhe.anvilcraft.api.chargecollector.ChargeCollectorManager;
import dev.dubhe.anvilcraft.api.hammer.IHammerRemovable;
import dev.dubhe.anvilcraft.block.entity.ChargeCollectorBlockEntity;

import java.util.Collection;
import java.util.Iterator;
import java.util.Map.Entry;
import javax.annotation.Nonnull;

import net.minecraft.core.BlockPos;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.block.Block;
Expand Down Expand Up @@ -53,16 +55,10 @@ public void onHitByAnvil(float fallDistance, BlockPos blockPos) {
.getNearestChargeCollect(blockPos);
Iterator<Entry<Float, ChargeCollectorBlockEntity>> iterator = chargeCollectorCollection.iterator();
int surplus = chargeNum;
for (int i = 0; i < chargeCollectorCollection.size(); i++) {
ChargeCollectorBlockEntity chargeCollectorBlockEntity = iterator.next().getValue();
int x = chargeCollectorBlockEntity.getPos().getX();
int y = chargeCollectorBlockEntity.getPos().getY();
int z = chargeCollectorBlockEntity.getPos().getZ();
if (((x - 2) > blockPos.getX() || (x + 2) < blockPos.getX())
|| ((y - 2) > blockPos.getY() || (y + 2) < blockPos.getY())
|| ((z - 2) > blockPos.getZ() || (z + 2) < blockPos.getZ())) {
return;
}
while (iterator.hasNext()) {
Entry<Float, ChargeCollectorBlockEntity> entry = iterator.next();
ChargeCollectorBlockEntity chargeCollectorBlockEntity = entry.getValue();
if (!ChargeCollectorManager.canCollect(chargeCollectorBlockEntity, blockPos)) return;
surplus = chargeCollectorBlockEntity.incomingCharge(surplus);
if (surplus == 0) return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package dev.dubhe.anvilcraft.event;

import dev.dubhe.anvilcraft.AnvilCraft;
import dev.dubhe.anvilcraft.api.chargecollector.ChargeCollectorManager;
import dev.dubhe.anvilcraft.api.event.SubscribeEvent;
import dev.dubhe.anvilcraft.api.event.entity.LightningStrikeEvent;
import dev.dubhe.anvilcraft.block.entity.ChargeCollectorBlockEntity;
import dev.dubhe.anvilcraft.init.ModBlocks;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.NotNull;

import java.util.Collection;
import java.util.Map;

public class LightningEventListener {
/**
* 侦听雷击事件
Expand All @@ -19,6 +24,7 @@ public class LightningEventListener {
public void onLightingStrike(@NotNull LightningStrikeEvent event) {
BlockPos pos = event.getPos();
BlockState state = event.getLevel().getBlockState(pos);
lightningCharge(pos, state);
if (state.is(Blocks.LIGHTNING_ROD)) pos = pos.below();
int depth = AnvilCraft.config.lightningStrikeDepth;
int radius = AnvilCraft.config.lightningStrikeRadius;
Expand All @@ -38,4 +44,19 @@ public void onLightingStrike(@NotNull LightningStrikeEvent event) {
}
}
}

private void lightningCharge(BlockPos pos, BlockState state) {
if (state.is(Blocks.COPPER_BLOCK) || state.is(Blocks.LIGHTNING_ROD)) {
int unCharged = 16;
Collection<Map.Entry<Float, ChargeCollectorBlockEntity>> nearestChargeCollect =
ChargeCollectorManager.getNearestChargeCollect(pos);
for (var floatChargeCollectorBlockEntityEntry : nearestChargeCollect) {
ChargeCollectorBlockEntity blockEntity = floatChargeCollectorBlockEntityEntry.getValue();
if (ChargeCollectorManager.canCollect(blockEntity, pos)) {
unCharged = blockEntity.incomingCharge(unCharged);
if (unCharged <= 0) break;
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package dev.dubhe.anvilcraft.event;

import dev.dubhe.anvilcraft.api.chargecollector.ChargeCollectorManager;
import dev.dubhe.anvilcraft.block.entity.ChargeCollectorBlockEntity;
import dev.dubhe.anvilcraft.init.ModBlocks;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.util.RandomSource;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Blocks;

import java.util.Collection;
import java.util.List;
import java.util.Map;

public class PistonMoveBlockListener {
public static void onPistonMoveBlocks(Level level, List<BlockPos> blocks) {

Check warning on line 17 in common/src/main/java/dev/dubhe/anvilcraft/event/PistonMoveBlockListener.java

View workflow job for this annotation

GitHub Actions / checkstyle

[Checkstyle] reported by reviewdog 🐶 Missing a Javadoc comment. Raw Output: /home/runner/work/AnvilCraft/AnvilCraft/common/src/main/java/dev/dubhe/anvilcraft/event/PistonMoveBlockListener.java:17:5: warning: Missing a Javadoc comment. (com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocMethodCheck)
RandomSource random = level.random;
for (BlockPos pos : blocks) {
if (!level.getBlockState(pos).is(ModBlocks.MAGNET_BLOCK.get())) continue;
boolean b = isNearbyCopperBlock(level, pos);
double r = random.nextDouble();
if (b && r < 0.25) {
Collection<Map.Entry<Float, ChargeCollectorBlockEntity>> nearestChargeCollect =
ChargeCollectorManager.getNearestChargeCollect(pos);
for (var floatChargeCollectorBlockEntityEntry : nearestChargeCollect) {
ChargeCollectorBlockEntity blockEntity = floatChargeCollectorBlockEntityEntry.getValue();
if (ChargeCollectorManager.canCollect(blockEntity, pos)) {
int unCharged = blockEntity.incomingCharge(1);
if (unCharged == 0) {
break;
}

}
}
}
}
}

private static boolean isNearbyCopperBlock(Level level, BlockPos pos) {
for (Direction face : Direction.values()) {
if (level.getBlockState(pos.relative(face)).is(Blocks.COPPER_BLOCK)) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package dev.dubhe.anvilcraft.mixin;

import dev.dubhe.anvilcraft.event.PistonMoveBlockListener;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.piston.PistonStructureResolver;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.ArrayList;
import java.util.List;

@Mixin(PistonStructureResolver.class)
public class PistonStructureResolverMixin {
@Shadow @Final private List<BlockPos> toPush;

@Shadow @Final private Level level;

@Inject(method = "resolve", at = @At("RETURN"))
private void onPistonResolve(CallbackInfoReturnable<Boolean> cir) {
if (level.isClientSide()) {
return;
}
if (!cir.getReturnValue()) return;
List<BlockPos> toPushBlocks = new ArrayList<>(toPush);
PistonMoveBlockListener.onPistonMoveBlocks(level, toPushBlocks);
}
}
3 changes: 2 additions & 1 deletion common/src/main/resources/anvilcraft-common.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"HoeItemMixin",
"ItemEntityMixin",
"LivingEntityMixin",
"PistonStructureResolverMixin",
"PlayerHitEntityMixin",
"SolidBucketItemMixin",
"accessor.BaseSpawnerAccessor",
Expand All @@ -32,5 +33,5 @@
],
"injectors": {
"defaultRequire": 1
}
}
}

0 comments on commit 25ee830

Please sign in to comment.