-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #439 from Anvil-Dev/charge
添加两种简易电荷获取方式
- Loading branch information
Showing
6 changed files
with
132 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
common/src/main/java/dev/dubhe/anvilcraft/event/PistonMoveBlockListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 GitHub Actions / checkstyle
|
||
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; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
common/src/main/java/dev/dubhe/anvilcraft/mixin/PistonStructureResolverMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters