Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -134,35 +134,40 @@ public <T extends BlockEntity> BlockEntityTicker<T> getTicker(
}

@Override

public void neighborChanged(
BlockState state,
Level level,
BlockPos pos,
Block neighborBlock,
BlockPos neighborPos,
boolean movedByPiston) {
if (level.isClientSide) {
return;
}
update(state, level, pos);
}

private static void update(BlockState state, Level level, BlockPos pos) {
if (level.isClientSide) return;
if (state.getValue(HALF) != Vertical4PartHalf.BOTTOM) return;
BlockPos topPos = pos.above(3);
BlockState topState = level.getBlockState(topPos);
if (!topState.is(ModBlocks.REMOTE_TRANSMISSION_POLE.get())) return;
if (topState.getValue(HALF) != Vertical4PartHalf.TOP) return;
IPowerComponent.Switch sw = state.getValue(SWITCH);
boolean bl = sw == IPowerComponent.Switch.ON;
if (bl == level.hasNeighborSignal(pos)) {
if (bl) {
state = state.setValue(SWITCH, IPowerComponent.Switch.OFF);
topState = topState.setValue(SWITCH, IPowerComponent.Switch.OFF);
} else {
state = state.setValue(SWITCH, IPowerComponent.Switch.ON);
topState = topState.setValue(SWITCH, IPowerComponent.Switch.ON);
}
level.setBlockAndUpdate(pos, state);
level.setBlockAndUpdate(topPos, topState);
IPowerComponent.Switch bottom = state.getValue(SWITCH);
IPowerComponent.Switch top = topState.getValue(SWITCH);
if (level.hasNeighborSignal(pos)) {
state = state.setValue(SWITCH, IPowerComponent.Switch.OFF);
topState = topState.setValue(SWITCH, IPowerComponent.Switch.OFF);
} else {
state = state.setValue(SWITCH, IPowerComponent.Switch.ON);
topState = topState.setValue(SWITCH, IPowerComponent.Switch.ON);
}
if ((bottom == state.getValue(SWITCH)) && (top == topState.getValue(SWITCH))) return;
level.setBlockAndUpdate(pos, state);
level.setBlockAndUpdate(topPos, topState);
}

@Override
protected void onPlace(BlockState state, Level level, BlockPos pos, BlockState oldState, boolean movedByPiston) {
update(state, level, pos);
}

@Override
Expand Down
39 changes: 24 additions & 15 deletions src/main/java/dev/dubhe/anvilcraft/block/TransmissionPoleBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ public VoxelShape getShape(
BlockState state,
BlockGetter level,
BlockPos pos,
CollisionContext context) {
CollisionContext context
) {
if (state.getValue(HALF) == Vertical3PartHalf.BOTTOM) return TRANSMISSION_POLE_BASE;
if (state.getValue(HALF) == Vertical3PartHalf.MID) return TRANSMISSION_POLE_MID;
if (state.getValue(HALF) == Vertical3PartHalf.TOP) return TRANSMISSION_POLE_TOP;
Expand Down Expand Up @@ -122,33 +123,41 @@ public <T extends BlockEntity> BlockEntityTicker<T> getTicker(
}

@Override

public void neighborChanged(
BlockState state,
Level level,
BlockPos pos,
Block neighborBlock,
BlockPos neighborPos,
boolean movedByPiston) {
boolean movedByPiston
) {
update(state, level, pos);
}

private static void update(BlockState state, Level level, BlockPos pos) {
if (level.isClientSide) return;
if (state.getValue(HALF) != Vertical3PartHalf.BOTTOM) return;
BlockPos topPos = pos.above(2);
BlockState topState = level.getBlockState(topPos);
if (!topState.is(ModBlocks.TRANSMISSION_POLE.get())) return;
if (topState.getValue(HALF) != Vertical3PartHalf.TOP) return;
IPowerComponent.Switch sw = state.getValue(SWITCH);
boolean bl = sw == IPowerComponent.Switch.ON;
if (bl == level.hasNeighborSignal(pos)) {
if (bl) {
state = state.setValue(SWITCH, IPowerComponent.Switch.OFF);
topState = topState.setValue(SWITCH, IPowerComponent.Switch.OFF);
} else {
state = state.setValue(SWITCH, IPowerComponent.Switch.ON);
topState = topState.setValue(SWITCH, IPowerComponent.Switch.ON);
}
level.setBlockAndUpdate(pos, state);
level.setBlockAndUpdate(topPos, topState);
IPowerComponent.Switch bottom = state.getValue(SWITCH);
IPowerComponent.Switch top = topState.getValue(SWITCH);
if (level.hasNeighborSignal(pos)) {
state = state.setValue(SWITCH, IPowerComponent.Switch.OFF);
topState = topState.setValue(SWITCH, IPowerComponent.Switch.OFF);
} else {
state = state.setValue(SWITCH, IPowerComponent.Switch.ON);
topState = topState.setValue(SWITCH, IPowerComponent.Switch.ON);
}
if ((bottom == state.getValue(SWITCH)) && (top == topState.getValue(SWITCH))) return;
level.setBlockAndUpdate(pos, state);
level.setBlockAndUpdate(topPos, topState);
}

@Override
protected void onPlace(BlockState state, Level level, BlockPos pos, BlockState oldState, boolean movedByPiston) {
update(state, level, pos);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ public class ChargerBlockEntity extends BlockEntity
private int timeLeft = 0;
@Getter
@Setter
private int startupCoolDown = 0;
@Getter
@Setter
private int timeTotalCache = 0;
private int powerValue = 0;
@Getter
Expand Down Expand Up @@ -97,7 +100,7 @@ protected void onContentsChanged(int slot) {

@Getter
private ItemStack displayItemStack = ItemStack.EMPTY;

@Getter
@Setter
private PowerGrid grid;
Expand Down Expand Up @@ -134,7 +137,10 @@ private ChargerChargingRecipe getItemRecipe(ItemStack stack) {
return null;
}

private boolean checkRecipeItemNotValid(@Nullable ChargerChargingRecipe recipe, @SuppressWarnings("unused") ItemStack stack) {
private boolean checkRecipeItemNotValid(
@Nullable ChargerChargingRecipe recipe,
@SuppressWarnings("unused") ItemStack stack
) {
if (recipe != null) {
if (recipe.power == 0) return true;
return recipe.power >= 0; // 充电器只接受power < 0的配方
Expand Down Expand Up @@ -182,7 +188,8 @@ private void syncPacket() {
if (this.getCurrentLevel() == null || !(this.getCurrentLevel() instanceof ServerLevel serverLevel)) return;
PacketDistributor.sendToPlayersTrackingChunk(
serverLevel, serverLevel.getChunk(this.getBlockPos()).getPos(),
new ChargerSyncPacket(this.getPos(), this.timeLeft, this.timeTotalCache, this.isFeCharging));
new ChargerSyncPacket(this.getPos(), this.timeLeft, this.timeTotalCache, this.isFeCharging)
);
}

private void moveItemToTransformedOverSlot() {
Expand Down Expand Up @@ -210,8 +217,8 @@ private void updateDisplayItemStack() {
ItemStack newDisplayStack = getDisplayItemStackForRender();
displayItemStack = newDisplayStack.copy();
PacketDistributor.sendToPlayersTrackingChunk(
(ServerLevel) level,
level.getChunk(getBlockPos()).getPos(),
(ServerLevel) level,
level.getChunk(getBlockPos()).getPos(),
new UpdateDisplayItemPacket(displayItemStack, getPos())
);
}
Expand Down Expand Up @@ -245,6 +252,8 @@ protected void saveAdditional(CompoundTag tag, HolderLookup.Provider provider) {
super.saveAdditional(tag, provider);
tag.putInt("TimeLeft", timeLeft);
tag.putInt("TimeTotalCache", timeTotalCache);
tag.putInt("PowerValue", powerValue);
tag.putInt("StartupCoolDown", startupCoolDown);
tag.put("Depository", itemHandler.serializeNBT(provider));
tag.putBoolean("FeCharging", isFeCharging);
tag.putInt("FeCooldown", feCooldown);
Expand All @@ -255,6 +264,8 @@ public void loadAdditional(CompoundTag tag, HolderLookup.Provider provider) {
super.loadAdditional(tag, provider);
timeLeft = tag.getInt("TimeLeft");
timeTotalCache = tag.getInt("TimeTotalCache");
powerValue = tag.getInt("PowerValue");
startupCoolDown = tag.getInt("StartupCoolDown");
itemHandler.deserializeNBT(provider, tag.getCompound("Depository"));
isFeCharging = tag.getBoolean("FeCharging");
feCooldown = tag.getInt("FeCooldown");
Expand All @@ -280,9 +291,11 @@ public void handleUpdateTag(CompoundTag tag, HolderLookup.Provider provider) {
}

@Override
public void onDataPacket(Connection connection,
ClientboundBlockEntityDataPacket packet,
HolderLookup.Provider provider) {
public void onDataPacket(
Connection connection,
ClientboundBlockEntityDataPacket packet,
HolderLookup.Provider provider
) {
super.onDataPacket(connection, packet, provider);
// 收到方块更新包后重新计算显示物品
if (level != null && level.isClientSide) {
Expand Down Expand Up @@ -382,8 +395,10 @@ private void dropItemStack(ItemStack stack1) {
if (!stack1.isEmpty()) {
if (level != null) {
Vec3 dropPos = getBlockPos().above().getBottomCenter();
ItemEntity itemEntity = new ItemEntity(level, dropPos.x, dropPos.y, dropPos.z,
stack1, 0, 0, 0);
ItemEntity itemEntity = new ItemEntity(
level, dropPos.x, dropPos.y, dropPos.z,
stack1, 0, 0, 0
);
itemEntity.setDefaultPickUpDelay();
}
}
Expand All @@ -397,53 +412,59 @@ public void tick(Level level1, BlockPos blockPos) {
BlockState state = level1.getBlockState(blockPos);
boolean powered = state.getValue(ChargerBlock.POWERED);
if (grid == null) return;
if (powered) return;
if (powered) {
this.setStartupCoolDown(20);
}
int powerValueCache = powerValue;
if (timeLeft == 0) {
moveItemToTransformingSlot();
}
if (timeLeft > 0) {
if (isFeCharging) {
powerValue = -(getFeChargingPowerLevel());
}
if (powerValue < powerValueCache) {
this.setStartupCoolDown(20);
}
if (startupCoolDown > 0) {
this.startupCoolDown--;
return;
}
if (timeLeft > 0 && isGridWorking()) {
if (isFeCharging) {
powerValue = -(getFeChargingPowerLevel());
}
if (isGridWorking()) {
if (isFeCharging) {
ItemStack processingStack = itemHandler.getStackInSlot(1);
if (!processingStack.isEmpty()) {
IEnergyStorage storage = processingStack.getCapability(Capabilities.EnergyStorage.ITEM);
if (storage != null) {
int powerLevel = getFeChargingPowerLevel();
if (powerLevel > 0) {
int countdown = AnvilCraft.CONFIG.powerConverter.powerConverterCountdown;
int efficiency = AnvilCraft.CONFIG.powerConverter.powerConverterEfficiency;
int remainingFE = storage.getMaxEnergyStored() - storage.getEnergyStored();
if (remainingFE <= 0) {
isFeCharging = false;
timeLeft = 0;
timeTotalCache = 0;
ItemStack processingStack = itemHandler.getStackInSlot(1);
if (!processingStack.isEmpty()) {
IEnergyStorage storage = processingStack.getCapability(Capabilities.EnergyStorage.ITEM);
if (storage != null) {
int powerLevel = getFeChargingPowerLevel();
if (powerLevel > 0) {
int countdown = AnvilCraft.CONFIG.powerConverter.powerConverterCountdown;
int efficiency = AnvilCraft.CONFIG.powerConverter.powerConverterEfficiency;
int remainingFE = storage.getMaxEnergyStored() - storage.getEnergyStored();
if (remainingFE <= 0) {
isFeCharging = false;
timeLeft = 0;
timeTotalCache = 0;
} else {
int feChargeRate = powerLevel * efficiency;
if (feCooldown <= 0) {
feCooldown = countdown;
storage.receiveEnergy(feChargeRate * countdown, false);
} else {
int feChargeRate = powerLevel * efficiency;
if (feCooldown <= 0) {
feCooldown = countdown;
storage.receiveEnergy(feChargeRate * countdown, false);
} else {
feCooldown--;
}
timeLeft = remainingFE;
timeTotalCache = storage.getMaxEnergyStored();
feCooldown--;
}
timeLeft = remainingFE;
timeTotalCache = storage.getMaxEnergyStored();
}
}
}
} else {
timeLeft--;
}
} else {
timeLeft--;
}
}
if (timeLeft == 0) {
moveItemToTransformedOverSlot();
if (timeLeft == 0) {
this.timeTotalCache = 0;
}
this.timeTotalCache = 0;
}

int signal = this.getAnalogRedstoneSignal();
Expand All @@ -456,6 +477,7 @@ public void tick(Level level1, BlockPos blockPos) {
if (level2.getGameTime() % 10 != 0) return;
PacketDistributor.sendToPlayersTrackingChunk(
level2, level2.getChunk(this.getBlockPos()).getPos(),
new ChargerSyncPacket(this.getPos(), this.timeLeft, this.timeTotalCache, this.isFeCharging));
new ChargerSyncPacket(this.getPos(), this.timeLeft, this.timeTotalCache, this.isFeCharging)
);
}
}
Loading
Loading