-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
760b9cd
commit 9bbf6cb
Showing
34 changed files
with
526 additions
and
414 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
32 changes: 32 additions & 0 deletions
32
src/main/java/com/redblueflame/herbocraft/blocks/AbstractProgressBlock.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 com.redblueflame.herbocraft.blocks; | ||
|
||
import com.redblueflame.herbocraft.HerboCraft; | ||
import net.minecraft.block.BlockEntityProvider; | ||
import net.minecraft.block.BlockState; | ||
import net.minecraft.entity.player.PlayerEntity; | ||
import net.minecraft.util.ActionResult; | ||
import net.minecraft.util.Hand; | ||
import net.minecraft.util.hit.BlockHitResult; | ||
import net.minecraft.util.math.BlockPos; | ||
import net.minecraft.world.World; | ||
|
||
@SuppressWarnings("deprecation") | ||
public abstract class AbstractProgressBlock extends AbstractUpgradableBlock implements BlockEntityProvider { | ||
protected AbstractProgressBlock(Settings settings) { | ||
super(settings); | ||
} | ||
|
||
@Override | ||
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { | ||
if (!world.isClient) { | ||
// Sync the server with client for the state of the block. | ||
AbstractProgressBlockEntity blockEntity = (AbstractProgressBlockEntity) world.getBlockEntity(pos); | ||
if (blockEntity == null) { | ||
HerboCraft.LOGGER.error("There were an error while getting the blockentity. Please check your input."); | ||
return ActionResult.PASS; | ||
} | ||
blockEntity.sendBlockProgressPacket(player); | ||
} | ||
return ActionResult.PASS; | ||
} | ||
} |
132 changes: 132 additions & 0 deletions
132
src/main/java/com/redblueflame/herbocraft/blocks/AbstractProgressBlockEntity.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,132 @@ | ||
package com.redblueflame.herbocraft.blocks; | ||
|
||
import com.redblueflame.herbocraft.HerboCraftPackets; | ||
import com.redblueflame.herbocraft.utils.PlayerContainerStream; | ||
import io.netty.buffer.Unpooled; | ||
import net.fabricmc.fabric.api.network.ServerSidePacketRegistry; | ||
import net.minecraft.block.BlockState; | ||
import net.minecraft.block.entity.BlockEntity; | ||
import net.minecraft.block.entity.BlockEntityType; | ||
import net.minecraft.entity.player.PlayerEntity; | ||
import net.minecraft.inventory.Inventory; | ||
import net.minecraft.nbt.CompoundTag; | ||
import net.minecraft.network.PacketByteBuf; | ||
import net.minecraft.util.Tickable; | ||
|
||
public abstract class AbstractProgressBlockEntity extends BlockEntity implements Tickable, Progress { | ||
public boolean isWorking; | ||
public int targetWork; | ||
public int currentWork; | ||
public int state; | ||
|
||
public AbstractProgressBlockEntity(BlockEntityType<?> type) { | ||
super(type); | ||
isWorking = false; | ||
targetWork = 0; | ||
currentWork = 0; | ||
state = 0; | ||
} | ||
|
||
@Override | ||
public void tick() { | ||
if (world != null && !world.isClient) { | ||
// We have to check for the server, is it's on the client, we will have problems. | ||
if (isWorking && targetWork != 0) { | ||
currentWork += 1; | ||
state = (int) (((float)currentWork / targetWork) * 255F); | ||
if (currentWork >= targetWork) { | ||
resetWork(); | ||
finishWork(); | ||
} | ||
} else { | ||
if (getInventory() != null && this.checkWork()) { | ||
targetWork = estimateWork(); | ||
setWorking(); | ||
} | ||
} | ||
} else if (isWorking) { | ||
// On the client, we can add the currentWork for display | ||
currentWork += 1; | ||
state = (int) (((float)currentWork / targetWork) * 255F); | ||
} | ||
} | ||
|
||
@Override | ||
public void fromTag(BlockState state, CompoundTag tag) { | ||
super.fromTag(state, tag); | ||
isWorking = tag.getBoolean("isWorking"); | ||
targetWork = tag.getInt("targetWork"); | ||
currentWork = tag.getInt("currentWork"); | ||
if (targetWork != 0) { | ||
this.state = currentWork/targetWork; | ||
} | ||
} | ||
|
||
@Override | ||
public CompoundTag toTag(CompoundTag tag) { | ||
super.toTag(tag); | ||
tag.putBoolean("isWorking", isWorking); | ||
tag.putInt("targetWork", targetWork); | ||
tag.putInt("currentWork", currentWork); | ||
return tag; | ||
} | ||
|
||
@Override | ||
public void resetWork() { | ||
isWorking = false; | ||
// Send the update to clients | ||
if (world != null && !world.isClient) { | ||
sendStateChangePacket(); | ||
} | ||
targetWork = 0; | ||
currentWork = 0; | ||
state = 0; | ||
} | ||
|
||
private void setWorking() { | ||
isWorking = true; | ||
if (world != null && !world.isClient) { | ||
sendStateChangePacket(); | ||
} | ||
currentWork = 0; | ||
} | ||
|
||
protected void registerEvents() { | ||
if (getInventory() != null) { | ||
getInventory().addListener(this::onInventoryUpdate); | ||
} | ||
} | ||
|
||
private void onInventoryUpdate(Inventory inventory) { | ||
if (inventoryUpdate(inventory)) { | ||
resetWork(); | ||
} | ||
} | ||
// region Packets | ||
private void sendStateChangePacket() { | ||
PacketByteBuf passedData = new PacketByteBuf(Unpooled.buffer()); | ||
passedData.writeBlockPos(pos); | ||
passedData.writeBoolean(isWorking); | ||
passedData.writeInt(targetWork); | ||
PlayerContainerStream.GetPlayerWatchingContainer(world, pos, this.getContainerClass()).forEach(playerEntity -> | ||
ServerSidePacketRegistry.INSTANCE.sendToPlayer(playerEntity, HerboCraftPackets.STATE_CHANGE, passedData)); | ||
} | ||
|
||
protected void sendBlockProgressPacket() { | ||
PacketByteBuf passedData = new PacketByteBuf(Unpooled.buffer()); | ||
passedData.writeBlockPos(pos); | ||
passedData.writeInt(targetWork); | ||
passedData.writeInt(currentWork); | ||
PlayerContainerStream.GetPlayerWatchingContainer(world, pos, this.getContainerClass()).forEach(playerEntity -> | ||
ServerSidePacketRegistry.INSTANCE.sendToPlayer(playerEntity, HerboCraftPackets.SEND_STATUS, passedData)); | ||
} | ||
|
||
protected void sendBlockProgressPacket(PlayerEntity e) { | ||
PacketByteBuf passedData = new PacketByteBuf(Unpooled.buffer()); | ||
passedData.writeBlockPos(pos); | ||
passedData.writeInt(targetWork); | ||
passedData.writeInt(currentWork); | ||
ServerSidePacketRegistry.INSTANCE.sendToPlayer(e, HerboCraftPackets.SEND_STATUS, passedData); | ||
} | ||
// endregion | ||
} |
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
Oops, something went wrong.