Convert Terminal and Interface blocks to layer 3
This commit is contained in:
parent
fc3716153f
commit
c8cdf425cb
|
@ -3,7 +3,7 @@ package net.shadowfacts.phycon.mixin.client;
|
||||||
import net.minecraft.client.gui.screen.ingame.HandledScreen;
|
import net.minecraft.client.gui.screen.ingame.HandledScreen;
|
||||||
import net.minecraft.client.util.math.MatrixStack;
|
import net.minecraft.client.util.math.MatrixStack;
|
||||||
import net.minecraft.screen.slot.Slot;
|
import net.minecraft.screen.slot.Slot;
|
||||||
//import net.shadowfacts.phycon.network.block.terminal.TerminalScreen;
|
import net.shadowfacts.phycon.network.block.terminal.TerminalScreen;
|
||||||
import org.spongepowered.asm.mixin.Mixin;
|
import org.spongepowered.asm.mixin.Mixin;
|
||||||
import org.spongepowered.asm.mixin.injection.At;
|
import org.spongepowered.asm.mixin.injection.At;
|
||||||
import org.spongepowered.asm.mixin.injection.Inject;
|
import org.spongepowered.asm.mixin.injection.Inject;
|
||||||
|
@ -17,10 +17,10 @@ public class MixinHandledScreen {
|
||||||
|
|
||||||
@Inject(method = "drawSlot(Lnet/minecraft/client/util/math/MatrixStack;Lnet/minecraft/screen/slot/Slot;)V", at = @At(value = "INVOKE", target = "enableDepthTest()V"))
|
@Inject(method = "drawSlot(Lnet/minecraft/client/util/math/MatrixStack;Lnet/minecraft/screen/slot/Slot;)V", at = @At(value = "INVOKE", target = "enableDepthTest()V"))
|
||||||
private void drawSlot(MatrixStack matrixStack, Slot slot, CallbackInfo ci) {
|
private void drawSlot(MatrixStack matrixStack, Slot slot, CallbackInfo ci) {
|
||||||
// if ((Object)this instanceof TerminalScreen) {
|
if ((Object)this instanceof TerminalScreen) {
|
||||||
// TerminalScreen self = (TerminalScreen)(Object)this;
|
TerminalScreen self = (TerminalScreen)(Object)this;
|
||||||
// self.drawSlotUnderlay(matrixStack, slot);
|
self.drawSlotUnderlay(matrixStack, slot);
|
||||||
// }
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import net.fabricmc.api.ModInitializer
|
||||||
import net.shadowfacts.phycon.init.PhyBlockEntities
|
import net.shadowfacts.phycon.init.PhyBlockEntities
|
||||||
import net.shadowfacts.phycon.init.PhyBlocks
|
import net.shadowfacts.phycon.init.PhyBlocks
|
||||||
import net.shadowfacts.phycon.init.PhyItems
|
import net.shadowfacts.phycon.init.PhyItems
|
||||||
|
import net.shadowfacts.phycon.init.PhyScreens
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author shadowfacts
|
* @author shadowfacts
|
||||||
|
@ -16,6 +17,7 @@ object PhysicalConnectivity: ModInitializer {
|
||||||
PhyBlocks.init()
|
PhyBlocks.init()
|
||||||
PhyBlockEntities.init()
|
PhyBlockEntities.init()
|
||||||
PhyItems.init()
|
PhyItems.init()
|
||||||
|
PhyScreens.init()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,7 @@ import net.fabricmc.fabric.api.client.screenhandler.v1.ScreenRegistry
|
||||||
import net.minecraft.client.render.RenderLayer
|
import net.minecraft.client.render.RenderLayer
|
||||||
import net.shadowfacts.phycon.init.PhyBlocks
|
import net.shadowfacts.phycon.init.PhyBlocks
|
||||||
import net.shadowfacts.phycon.init.PhyScreens
|
import net.shadowfacts.phycon.init.PhyScreens
|
||||||
//import net.shadowfacts.phycon.network.block.terminal.TerminalScreen
|
import net.shadowfacts.phycon.network.block.terminal.TerminalScreen
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author shadowfacts
|
* @author shadowfacts
|
||||||
|
@ -15,6 +15,6 @@ object PhysicalConnectivityClient: ClientModInitializer {
|
||||||
override fun onInitializeClient() {
|
override fun onInitializeClient() {
|
||||||
BlockRenderLayerMap.INSTANCE.putBlock(PhyBlocks.CABLE, RenderLayer.getTranslucent())
|
BlockRenderLayerMap.INSTANCE.putBlock(PhyBlocks.CABLE, RenderLayer.getTranslucent())
|
||||||
|
|
||||||
// ScreenRegistry.register(PhyScreens.TERMINAL_SCREEN_HANDLER, ::TerminalScreen)
|
ScreenRegistry.register(PhyScreens.TERMINAL_SCREEN_HANDLER, ::TerminalScreen)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,25 +5,24 @@ import net.minecraft.block.entity.BlockEntity
|
||||||
import net.minecraft.block.entity.BlockEntityType
|
import net.minecraft.block.entity.BlockEntityType
|
||||||
import net.minecraft.util.Identifier
|
import net.minecraft.util.Identifier
|
||||||
import net.minecraft.util.registry.Registry
|
import net.minecraft.util.registry.Registry
|
||||||
|
import net.shadowfacts.phycon.network.block.netinterface.InterfaceBlock
|
||||||
|
import net.shadowfacts.phycon.network.block.netinterface.InterfaceBlockEntity
|
||||||
import net.shadowfacts.phycon.network.block.netswitch.SwitchBlock
|
import net.shadowfacts.phycon.network.block.netswitch.SwitchBlock
|
||||||
import net.shadowfacts.phycon.network.block.netswitch.SwitchBlockEntity
|
import net.shadowfacts.phycon.network.block.netswitch.SwitchBlockEntity
|
||||||
|
import net.shadowfacts.phycon.network.block.terminal.TerminalBlock
|
||||||
|
import net.shadowfacts.phycon.network.block.terminal.TerminalBlockEntity
|
||||||
import net.shadowfacts.phycon.network.block.test.DestBlock
|
import net.shadowfacts.phycon.network.block.test.DestBlock
|
||||||
import net.shadowfacts.phycon.network.block.test.DestBlockEntity
|
import net.shadowfacts.phycon.network.block.test.DestBlockEntity
|
||||||
import net.shadowfacts.phycon.network.block.test.SourceBlock
|
import net.shadowfacts.phycon.network.block.test.SourceBlock
|
||||||
import net.shadowfacts.phycon.network.block.test.SourceBlockEntity
|
import net.shadowfacts.phycon.network.block.test.SourceBlockEntity
|
||||||
|
|
||||||
//import net.shadowfacts.phycon.network.block.netinterface.InterfaceBlock
|
|
||||||
//import net.shadowfacts.phycon.network.block.netinterface.InterfaceBlockEntity
|
|
||||||
//import net.shadowfacts.phycon.network.block.terminal.TerminalBlock
|
|
||||||
//import net.shadowfacts.phycon.network.block.terminal.TerminalBlockEntity
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author shadowfacts
|
* @author shadowfacts
|
||||||
*/
|
*/
|
||||||
object PhyBlockEntities {
|
object PhyBlockEntities {
|
||||||
|
|
||||||
// val INTERFACE = create(::InterfaceBlockEntity, PhyBlocks.INTERFACE)
|
val INTERFACE = create(::InterfaceBlockEntity, PhyBlocks.INTERFACE)
|
||||||
// val TERMINAL = create(::TerminalBlockEntity, PhyBlocks.TERMINAL)
|
val TERMINAL = create(::TerminalBlockEntity, PhyBlocks.TERMINAL)
|
||||||
val SWITCH = create(::SwitchBlockEntity, PhyBlocks.SWITCH)
|
val SWITCH = create(::SwitchBlockEntity, PhyBlocks.SWITCH)
|
||||||
val SOURCE = create(::SourceBlockEntity, PhyBlocks.SOURCE)
|
val SOURCE = create(::SourceBlockEntity, PhyBlocks.SOURCE)
|
||||||
val DEST = create(::DestBlockEntity, PhyBlocks.DEST)
|
val DEST = create(::DestBlockEntity, PhyBlocks.DEST)
|
||||||
|
@ -33,8 +32,8 @@ object PhyBlockEntities {
|
||||||
}
|
}
|
||||||
|
|
||||||
fun init() {
|
fun init() {
|
||||||
// register(InterfaceBlock.ID, INTERFACE)
|
register(InterfaceBlock.ID, INTERFACE)
|
||||||
// register(TerminalBlock.ID, TERMINAL)
|
register(TerminalBlock.ID, TERMINAL)
|
||||||
register(SwitchBlock.ID, SWITCH)
|
register(SwitchBlock.ID, SWITCH)
|
||||||
register(SourceBlock.ID, SOURCE)
|
register(SourceBlock.ID, SOURCE)
|
||||||
register(DestBlock.ID, DEST)
|
register(DestBlock.ID, DEST)
|
||||||
|
|
|
@ -4,7 +4,9 @@ import net.minecraft.block.Block
|
||||||
import net.minecraft.util.Identifier
|
import net.minecraft.util.Identifier
|
||||||
import net.minecraft.util.registry.Registry
|
import net.minecraft.util.registry.Registry
|
||||||
import net.shadowfacts.phycon.network.block.cable.CableBlock
|
import net.shadowfacts.phycon.network.block.cable.CableBlock
|
||||||
|
import net.shadowfacts.phycon.network.block.netinterface.InterfaceBlock
|
||||||
import net.shadowfacts.phycon.network.block.netswitch.SwitchBlock
|
import net.shadowfacts.phycon.network.block.netswitch.SwitchBlock
|
||||||
|
import net.shadowfacts.phycon.network.block.terminal.TerminalBlock
|
||||||
import net.shadowfacts.phycon.network.block.test.DestBlock
|
import net.shadowfacts.phycon.network.block.test.DestBlock
|
||||||
import net.shadowfacts.phycon.network.block.test.SourceBlock
|
import net.shadowfacts.phycon.network.block.test.SourceBlock
|
||||||
|
|
||||||
|
@ -13,16 +15,16 @@ import net.shadowfacts.phycon.network.block.test.SourceBlock
|
||||||
*/
|
*/
|
||||||
object PhyBlocks {
|
object PhyBlocks {
|
||||||
|
|
||||||
// val INTERFACE = InterfaceBlock()
|
val INTERFACE = InterfaceBlock()
|
||||||
// val TERMINAL = TerminalBlock()
|
val TERMINAL = TerminalBlock()
|
||||||
val SWITCH = SwitchBlock()
|
val SWITCH = SwitchBlock()
|
||||||
val CABLE = CableBlock()
|
val CABLE = CableBlock()
|
||||||
val SOURCE = SourceBlock()
|
val SOURCE = SourceBlock()
|
||||||
val DEST = DestBlock()
|
val DEST = DestBlock()
|
||||||
|
|
||||||
fun init() {
|
fun init() {
|
||||||
// register(InterfaceBlock.ID, INTERFACE)
|
register(InterfaceBlock.ID, INTERFACE)
|
||||||
// register(TerminalBlock.ID, TERMINAL)
|
register(TerminalBlock.ID, TERMINAL)
|
||||||
register(SwitchBlock.ID, SWITCH)
|
register(SwitchBlock.ID, SWITCH)
|
||||||
register(CableBlock.ID, CABLE)
|
register(CableBlock.ID, CABLE)
|
||||||
register(SourceBlock.ID, SOURCE)
|
register(SourceBlock.ID, SOURCE)
|
||||||
|
|
|
@ -6,20 +6,19 @@ import net.minecraft.util.Identifier
|
||||||
import net.minecraft.util.registry.Registry
|
import net.minecraft.util.registry.Registry
|
||||||
import net.shadowfacts.phycon.item.ScrewdriverItem
|
import net.shadowfacts.phycon.item.ScrewdriverItem
|
||||||
import net.shadowfacts.phycon.network.block.cable.CableBlock
|
import net.shadowfacts.phycon.network.block.cable.CableBlock
|
||||||
|
import net.shadowfacts.phycon.network.block.netinterface.InterfaceBlock
|
||||||
import net.shadowfacts.phycon.network.block.netswitch.SwitchBlock
|
import net.shadowfacts.phycon.network.block.netswitch.SwitchBlock
|
||||||
|
import net.shadowfacts.phycon.network.block.terminal.TerminalBlock
|
||||||
import net.shadowfacts.phycon.network.block.test.DestBlock
|
import net.shadowfacts.phycon.network.block.test.DestBlock
|
||||||
import net.shadowfacts.phycon.network.block.test.SourceBlock
|
import net.shadowfacts.phycon.network.block.test.SourceBlock
|
||||||
|
|
||||||
//import net.shadowfacts.phycon.network.block.netinterface.InterfaceBlock
|
|
||||||
//import net.shadowfacts.phycon.network.block.terminal.TerminalBlock
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author shadowfacts
|
* @author shadowfacts
|
||||||
*/
|
*/
|
||||||
object PhyItems {
|
object PhyItems {
|
||||||
|
|
||||||
// val INTERFACE = BlockItem(PhyBlocks.INTERFACE, Item.Settings())
|
val INTERFACE = BlockItem(PhyBlocks.INTERFACE, Item.Settings())
|
||||||
// val TERMINAL = BlockItem(PhyBlocks.TERMINAL, Item.Settings())
|
val TERMINAL = BlockItem(PhyBlocks.TERMINAL, Item.Settings())
|
||||||
val SWITCH = BlockItem(PhyBlocks.SWITCH, Item.Settings())
|
val SWITCH = BlockItem(PhyBlocks.SWITCH, Item.Settings())
|
||||||
val CABLE = BlockItem(PhyBlocks.CABLE, Item.Settings())
|
val CABLE = BlockItem(PhyBlocks.CABLE, Item.Settings())
|
||||||
val SOURCE = BlockItem(PhyBlocks.SOURCE, Item.Settings())
|
val SOURCE = BlockItem(PhyBlocks.SOURCE, Item.Settings())
|
||||||
|
@ -28,8 +27,8 @@ object PhyItems {
|
||||||
val SCREWDRIVER = ScrewdriverItem()
|
val SCREWDRIVER = ScrewdriverItem()
|
||||||
|
|
||||||
fun init() {
|
fun init() {
|
||||||
// register(InterfaceBlock.ID, INTERFACE)
|
register(InterfaceBlock.ID, INTERFACE)
|
||||||
// register(TerminalBlock.ID, TERMINAL)
|
register(TerminalBlock.ID, TERMINAL)
|
||||||
register(SwitchBlock.ID, SWITCH)
|
register(SwitchBlock.ID, SWITCH)
|
||||||
register(CableBlock.ID, CABLE)
|
register(CableBlock.ID, CABLE)
|
||||||
register(SourceBlock.ID, SOURCE)
|
register(SourceBlock.ID, SOURCE)
|
||||||
|
|
|
@ -1,12 +1,18 @@
|
||||||
package net.shadowfacts.phycon.init
|
package net.shadowfacts.phycon.init
|
||||||
|
|
||||||
import net.fabricmc.fabric.api.screenhandler.v1.ScreenHandlerRegistry
|
import net.fabricmc.fabric.api.screenhandler.v1.ScreenHandlerRegistry
|
||||||
|
import net.minecraft.screen.ScreenHandlerType
|
||||||
import net.minecraft.util.Identifier
|
import net.minecraft.util.Identifier
|
||||||
import net.shadowfacts.phycon.PhysicalConnectivity
|
import net.shadowfacts.phycon.PhysicalConnectivity
|
||||||
//import net.shadowfacts.phycon.network.block.terminal.TerminalScreenHandler
|
import net.shadowfacts.phycon.network.block.terminal.TerminalScreenHandler
|
||||||
|
|
||||||
object PhyScreens {
|
object PhyScreens {
|
||||||
|
|
||||||
// val TERMINAL_SCREEN_HANDLER = ScreenHandlerRegistry.registerExtended(Identifier(PhysicalConnectivity.MODID, "terminal"), ::TerminalScreenHandler)
|
lateinit var TERMINAL_SCREEN_HANDLER: ScreenHandlerType<TerminalScreenHandler>
|
||||||
|
private set
|
||||||
|
|
||||||
|
fun init() {
|
||||||
|
TERMINAL_SCREEN_HANDLER = ScreenHandlerRegistry.registerExtended(Identifier(PhysicalConnectivity.MODID, "terminal"), ::TerminalScreenHandler)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -63,6 +63,7 @@ abstract class DeviceBlockEntity(type: BlockEntityType<*>): BlockEntity(type),
|
||||||
is ARPResponseFrame -> handleARPResponse(frame)
|
is ARPResponseFrame -> handleARPResponse(frame)
|
||||||
is PacketFrame -> {
|
is PacketFrame -> {
|
||||||
if (frame.packet.destination.isBroadcast || frame.packet.destination == ipAddress) {
|
if (frame.packet.destination.isBroadcast || frame.packet.destination == ipAddress) {
|
||||||
|
println("$this ($ipAddress) received packet: ${frame.packet}")
|
||||||
handle(frame.packet)
|
handle(frame.packet)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -93,9 +94,10 @@ abstract class DeviceBlockEntity(type: BlockEntityType<*>): BlockEntity(type),
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun sendPacket(packet: Packet) {
|
override fun sendPacket(packet: Packet) {
|
||||||
val cached = arpTable[packet.destination]
|
if (packet.destination.isBroadcast) {
|
||||||
if (cached != null) {
|
send(BasePacketFrame(packet, macAddress, MACAddress.BROADCAST))
|
||||||
send(BasePacketFrame(packet, macAddress, cached))
|
} else if (arpTable.containsKey(packet.destination)) {
|
||||||
|
send(BasePacketFrame(packet, macAddress, arpTable[packet.destination]!!))
|
||||||
} else {
|
} else {
|
||||||
packetQueue.add(PendingPacket(packet, counter))
|
packetQueue.add(PendingPacket(packet, counter))
|
||||||
|
|
||||||
|
@ -113,9 +115,9 @@ abstract class DeviceBlockEntity(type: BlockEntityType<*>): BlockEntity(type),
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun tick() {
|
override fun tick() {
|
||||||
if (!world!!.isClient) {
|
counter++
|
||||||
counter++
|
|
||||||
|
|
||||||
|
if (!world!!.isClient) {
|
||||||
packetQueue.removeIf { entry ->
|
packetQueue.removeIf { entry ->
|
||||||
val (packet, timestamp) = entry
|
val (packet, timestamp) = entry
|
||||||
if (arpTable.containsKey(packet.destination)) {
|
if (arpTable.containsKey(packet.destination)) {
|
||||||
|
|
|
@ -1,128 +1,131 @@
|
||||||
package net.shadowfacts.phycon.network.block.netinterface
|
package net.shadowfacts.phycon.network.block.netinterface
|
||||||
//
|
|
||||||
//import alexiil.mc.lib.attributes.AttributeList
|
import alexiil.mc.lib.attributes.AttributeList
|
||||||
//import alexiil.mc.lib.attributes.AttributeProvider
|
import alexiil.mc.lib.attributes.AttributeProvider
|
||||||
//import net.minecraft.block.Block
|
import net.minecraft.block.*
|
||||||
//import net.minecraft.block.BlockState
|
import net.minecraft.entity.LivingEntity
|
||||||
//import net.minecraft.block.Material
|
import net.minecraft.item.ItemPlacementContext
|
||||||
//import net.minecraft.block.ShapeContext
|
import net.minecraft.item.ItemStack
|
||||||
//import net.minecraft.entity.LivingEntity
|
import net.minecraft.state.StateManager
|
||||||
//import net.minecraft.item.ItemPlacementContext
|
import net.minecraft.state.property.EnumProperty
|
||||||
//import net.minecraft.item.ItemStack
|
import net.minecraft.state.property.Properties
|
||||||
//import net.minecraft.state.StateManager
|
import net.minecraft.util.Identifier
|
||||||
//import net.minecraft.state.property.EnumProperty
|
import net.minecraft.util.math.BlockPos
|
||||||
//import net.minecraft.state.property.Properties
|
import net.minecraft.util.math.Direction
|
||||||
//import net.minecraft.util.Identifier
|
import net.minecraft.util.shape.VoxelShape
|
||||||
//import net.minecraft.util.math.BlockPos
|
import net.minecraft.util.shape.VoxelShapes
|
||||||
//import net.minecraft.util.math.Direction
|
import net.minecraft.world.BlockView
|
||||||
//import net.minecraft.util.shape.VoxelShape
|
import net.minecraft.world.World
|
||||||
//import net.minecraft.util.shape.VoxelShapes
|
import net.minecraft.world.WorldAccess
|
||||||
//import net.minecraft.world.BlockView
|
import net.shadowfacts.phycon.PhysicalConnectivity
|
||||||
//import net.minecraft.world.World
|
import net.shadowfacts.phycon.api.NetworkComponentBlock
|
||||||
//import net.minecraft.world.WorldAccess
|
import net.shadowfacts.phycon.api.Interface
|
||||||
//import net.shadowfacts.phycon.PhysicalConnectivity
|
import net.shadowfacts.phycon.network.DeviceBlock
|
||||||
//import net.shadowfacts.phycon.api.NetworkComponentBlock
|
import net.shadowfacts.phycon.network.block.cable.CableBlock
|
||||||
//import net.shadowfacts.phycon.api.Interface
|
import java.util.*
|
||||||
//import net.shadowfacts.phycon.block.BlockWithEntity
|
|
||||||
//import net.shadowfacts.phycon.network.block.cable.CableBlock
|
/**
|
||||||
//import java.util.*
|
* @author shadowfacts
|
||||||
//
|
*/
|
||||||
///**
|
class InterfaceBlock: DeviceBlock<InterfaceBlockEntity>(Settings.of(Material.METAL)),
|
||||||
// * @author shadowfacts
|
NetworkComponentBlock,
|
||||||
// */
|
AttributeProvider {
|
||||||
//class InterfaceBlock: BlockWithEntity<InterfaceBlockEntity>(Settings.of(Material.METAL)),
|
|
||||||
// NetworkComponentBlock,
|
companion object {
|
||||||
// AttributeProvider {
|
val ID = Identifier(PhysicalConnectivity.MODID, "network_interface")
|
||||||
//
|
val FACING = Properties.FACING
|
||||||
// companion object {
|
val CABLE_CONNECTION = EnumProperty.of("cable_connection", Direction::class.java)
|
||||||
// val ID = Identifier(PhysicalConnectivity.MODID, "network_interface")
|
private val SIDE_SHAPES = mapOf<Direction, VoxelShape>(
|
||||||
// val FACING = Properties.FACING
|
Direction.DOWN to createCuboidShape(2.0, 0.0, 2.0, 14.0, 2.0, 14.0),
|
||||||
// val CABLE_CONNECTION = EnumProperty.of("cable_connection", Direction::class.java)
|
Direction.UP to createCuboidShape(2.0, 14.0, 2.0, 14.0, 16.0, 14.0),
|
||||||
// private val SIDE_SHAPES = mapOf<Direction, VoxelShape>(
|
Direction.NORTH to createCuboidShape(2.0, 2.0, 0.0, 14.0, 14.0, 2.0),
|
||||||
// Direction.DOWN to createCuboidShape(2.0, 0.0, 2.0, 14.0, 2.0, 14.0),
|
Direction.SOUTH to createCuboidShape(2.0, 2.0, 14.0, 14.0, 14.0, 16.0),
|
||||||
// Direction.UP to createCuboidShape(2.0, 14.0, 2.0, 14.0, 16.0, 14.0),
|
Direction.WEST to createCuboidShape(0.0, 2.0, 2.0, 2.0, 14.0, 14.0),
|
||||||
// Direction.NORTH to createCuboidShape(2.0, 2.0, 0.0, 14.0, 14.0, 2.0),
|
Direction.EAST to createCuboidShape(14.0, 2.0, 2.0, 16.0, 14.0, 14.0)
|
||||||
// Direction.SOUTH to createCuboidShape(2.0, 2.0, 14.0, 14.0, 14.0, 16.0),
|
)
|
||||||
// Direction.WEST to createCuboidShape(0.0, 2.0, 2.0, 2.0, 14.0, 14.0),
|
private val CENTER_SHAPES = mapOf<Direction, VoxelShape>(
|
||||||
// Direction.EAST to createCuboidShape(14.0, 2.0, 2.0, 16.0, 14.0, 14.0)
|
Direction.DOWN to createCuboidShape(6.0, 2.0, 6.0, 10.0, 10.0, 10.0),
|
||||||
// )
|
Direction.UP to createCuboidShape(6.0, 6.0, 6.0, 10.0, 14.0, 10.0),
|
||||||
// private val CENTER_SHAPES = mapOf<Direction, VoxelShape>(
|
Direction.NORTH to createCuboidShape(6.0, 6.0, 2.0, 10.0, 10.0, 10.0),
|
||||||
// Direction.DOWN to createCuboidShape(6.0, 2.0, 6.0, 10.0, 10.0, 10.0),
|
Direction.SOUTH to createCuboidShape(6.0, 6.0, 6.0, 10.0, 10.0, 14.0),
|
||||||
// Direction.UP to createCuboidShape(6.0, 6.0, 6.0, 10.0, 14.0, 10.0),
|
Direction.WEST to createCuboidShape(2.0, 6.0, 6.0, 10.0, 10.0, 10.0),
|
||||||
// Direction.NORTH to createCuboidShape(6.0, 6.0, 2.0, 10.0, 10.0, 10.0),
|
Direction.EAST to createCuboidShape(6.0, 6.0, 6.0, 14.0, 10.0, 10.0)
|
||||||
// Direction.SOUTH to createCuboidShape(6.0, 6.0, 6.0, 10.0, 10.0, 14.0),
|
)
|
||||||
// Direction.WEST to createCuboidShape(2.0, 6.0, 6.0, 10.0, 10.0, 10.0),
|
|
||||||
// Direction.EAST to createCuboidShape(6.0, 6.0, 6.0, 14.0, 10.0, 10.0)
|
private val shapeCache = mutableMapOf<Pair<Direction, Direction>, VoxelShape>()
|
||||||
// )
|
fun getShape(facing: Direction, cableConnection: Direction): VoxelShape {
|
||||||
//
|
return shapeCache.getOrPut(facing to cableConnection) {
|
||||||
// private val shapeCache = mutableMapOf<Pair<Direction, Direction>, VoxelShape>()
|
VoxelShapes.union(
|
||||||
// fun getShape(facing: Direction, cableConnection: Direction): VoxelShape {
|
VoxelShapes.union(SIDE_SHAPES[facing], CENTER_SHAPES[facing]),
|
||||||
// return shapeCache.getOrPut(facing to cableConnection) {
|
CableBlock.SIDE_SHAPES[cableConnection]
|
||||||
// VoxelShapes.union(
|
)
|
||||||
// VoxelShapes.union(SIDE_SHAPES[facing], CENTER_SHAPES[facing]),
|
}
|
||||||
// CableBlock.SIDE_SHAPES[cableConnection]
|
}
|
||||||
// )
|
}
|
||||||
// }
|
|
||||||
// }
|
override fun getNetworkConnectedSides(state: BlockState, world: World, pos: BlockPos): Collection<Direction> {
|
||||||
// }
|
val set = EnumSet.of(state[CABLE_CONNECTION])
|
||||||
//
|
set.remove(state[FACING])
|
||||||
// override fun getNetworkConnectedSides(state: BlockState, world: World, pos: BlockPos): Collection<Direction> {
|
return set
|
||||||
// return EnumSet.of(state[CABLE_CONNECTION])
|
}
|
||||||
// }
|
|
||||||
//
|
override fun getNetworkInterfaceForSide(side: Direction, state: BlockState, world: World, pos: BlockPos): Interface? {
|
||||||
// override fun getNetworkInterfaceForSide(side: Direction, state: BlockState, world: World, pos: BlockPos): Interface? {
|
return if (side == state[FACING]) {
|
||||||
// return getBlockEntity(world, pos)?.deviceInterfaces?.first()
|
null
|
||||||
// }
|
} else {
|
||||||
//
|
getBlockEntity(world, pos)
|
||||||
// override fun appendProperties(builder: StateManager.Builder<Block, BlockState>) {
|
}
|
||||||
// super.appendProperties(builder)
|
}
|
||||||
// builder.add(FACING)
|
|
||||||
// builder.add(CABLE_CONNECTION)
|
override fun appendProperties(builder: StateManager.Builder<Block, BlockState>) {
|
||||||
// }
|
super.appendProperties(builder)
|
||||||
//
|
builder.add(FACING)
|
||||||
// override fun createBlockEntity(world: BlockView) = InterfaceBlockEntity()
|
builder.add(CABLE_CONNECTION)
|
||||||
//
|
}
|
||||||
// override fun getPlacementState(context: ItemPlacementContext): BlockState {
|
|
||||||
// val facing = if (context.player?.isSneaking == true) context.side.opposite else context.playerFacing.opposite
|
override fun createBlockEntity(world: BlockView) = InterfaceBlockEntity()
|
||||||
// val cableConnection = getCableConnectionSide(context.world, context.blockPos) ?: facing.opposite
|
|
||||||
// return defaultState.with(FACING, facing).with(CABLE_CONNECTION, cableConnection)
|
override fun getPlacementState(context: ItemPlacementContext): BlockState {
|
||||||
// }
|
val facing = if (context.player?.isSneaking == true) context.side.opposite else context.playerFacing.opposite
|
||||||
//
|
val cableConnection = getCableConnectionSide(context.world, context.blockPos) ?: facing.opposite
|
||||||
// private fun getCableConnectionSide(world: World, pos: BlockPos): Direction? {
|
return defaultState.with(FACING, facing).with(CABLE_CONNECTION, cableConnection)
|
||||||
// for (side in Direction.values()) {
|
}
|
||||||
// val offsetPos = pos.offset(side)
|
|
||||||
// if (world.getBlockState(offsetPos).block is NetworkComponentBlock) {
|
private fun getCableConnectionSide(world: World, pos: BlockPos): Direction? {
|
||||||
// return side
|
for (side in Direction.values()) {
|
||||||
// }
|
val offsetPos = pos.offset(side)
|
||||||
// }
|
if (world.getBlockState(offsetPos).block is NetworkComponentBlock) {
|
||||||
// return null
|
return side
|
||||||
// }
|
}
|
||||||
//
|
}
|
||||||
// override fun onPlaced(world: World, pos: BlockPos, state: BlockState, placer: LivingEntity?, stack: ItemStack) {
|
return null
|
||||||
// if (!world.isClient) {
|
}
|
||||||
// getBlockEntity(world, pos)!!.updateInventory()
|
|
||||||
// }
|
override fun onPlaced(world: World, pos: BlockPos, state: BlockState, placer: LivingEntity?, stack: ItemStack) {
|
||||||
// }
|
if (!world.isClient) {
|
||||||
//
|
getBlockEntity(world, pos)!!.updateInventory()
|
||||||
// override fun neighborUpdate(state: BlockState, world: World, pos: BlockPos, neighborBlock: Block, neighborPos: BlockPos, boolean_1: Boolean) {
|
}
|
||||||
// if (!world.isClient) {
|
}
|
||||||
// getBlockEntity(world, pos)!!.updateInventory()
|
|
||||||
// }
|
override fun neighborUpdate(state: BlockState, world: World, pos: BlockPos, neighborBlock: Block, neighborPos: BlockPos, boolean_1: Boolean) {
|
||||||
// }
|
if (!world.isClient) {
|
||||||
//
|
getBlockEntity(world, pos)!!.updateInventory()
|
||||||
// override fun getStateForNeighborUpdate(state: BlockState, side: Direction, neighborState: BlockState, world: WorldAccess, pos: BlockPos, neighborPos: BlockPos): BlockState {
|
}
|
||||||
// if (neighborState.block is NetworkComponentBlock && world.getBlockState(pos.offset(state[CABLE_CONNECTION])).block !is NetworkComponentBlock) {
|
}
|
||||||
// return state.with(CABLE_CONNECTION, side)
|
|
||||||
// }
|
override fun getStateForNeighborUpdate(state: BlockState, side: Direction, neighborState: BlockState, world: WorldAccess, pos: BlockPos, neighborPos: BlockPos): BlockState {
|
||||||
// return state
|
if (neighborState.block is NetworkComponentBlock && world.getBlockState(pos.offset(state[CABLE_CONNECTION])).block !is NetworkComponentBlock) {
|
||||||
// }
|
return state.with(CABLE_CONNECTION, side)
|
||||||
//
|
}
|
||||||
// override fun addAllAttributes(world: World, pos: BlockPos, state: BlockState, to: AttributeList<*>) {
|
return state
|
||||||
// to.offer(getBlockEntity(world, pos))
|
}
|
||||||
// }
|
|
||||||
//
|
override fun addAllAttributes(world: World, pos: BlockPos, state: BlockState, to: AttributeList<*>) {
|
||||||
// override fun getOutlineShape(state: BlockState, world: BlockView, pos: BlockPos, context: ShapeContext): VoxelShape {
|
to.offer(getBlockEntity(world, pos))
|
||||||
// return getShape(state[FACING], state[CABLE_CONNECTION])
|
}
|
||||||
// }
|
|
||||||
//
|
override fun getOutlineShape(state: BlockState, world: BlockView, pos: BlockPos, context: ShapeContext): VoxelShape {
|
||||||
//}
|
return getShape(state[FACING], state[CABLE_CONNECTION])
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -1,93 +1,89 @@
|
||||||
package net.shadowfacts.phycon.network.block.netinterface
|
package net.shadowfacts.phycon.network.block.netinterface
|
||||||
//
|
|
||||||
//import alexiil.mc.lib.attributes.SearchOptions
|
import alexiil.mc.lib.attributes.SearchOptions
|
||||||
//import alexiil.mc.lib.attributes.Simulation
|
import alexiil.mc.lib.attributes.Simulation
|
||||||
//import alexiil.mc.lib.attributes.item.GroupedItemInv
|
import alexiil.mc.lib.attributes.item.GroupedItemInv
|
||||||
//import alexiil.mc.lib.attributes.item.ItemAttributes
|
import alexiil.mc.lib.attributes.item.ItemAttributes
|
||||||
//import net.minecraft.item.ItemStack
|
import net.minecraft.item.ItemStack
|
||||||
//import net.minecraft.util.math.Direction
|
import net.minecraft.util.math.Direction
|
||||||
//import net.shadowfacts.phycon.api.Interface
|
import net.shadowfacts.phycon.api.packet.Packet
|
||||||
//import net.shadowfacts.phycon.api.packet.Packet
|
import net.shadowfacts.phycon.init.PhyBlockEntities
|
||||||
//import net.shadowfacts.phycon.init.PhyBlockEntities
|
import net.shadowfacts.phycon.network.DeviceBlockEntity
|
||||||
//import net.shadowfacts.phycon.network.BaseInterface
|
import net.shadowfacts.phycon.network.component.ItemStackPacketHandler
|
||||||
//import net.shadowfacts.phycon.network.DeviceBlockEntity
|
import net.shadowfacts.phycon.network.component.handleItemStack
|
||||||
//import net.shadowfacts.phycon.network.component.ItemStackPacketHandler
|
import net.shadowfacts.phycon.network.packet.*
|
||||||
//import net.shadowfacts.phycon.network.component.handleItemStack
|
|
||||||
//import net.shadowfacts.phycon.network.packet.*
|
/**
|
||||||
//
|
* @author shadowfacts
|
||||||
///**
|
*/
|
||||||
// * @author shadowfacts
|
class InterfaceBlockEntity: DeviceBlockEntity(PhyBlockEntities.INTERFACE), ItemStackPacketHandler {
|
||||||
// */
|
|
||||||
//class InterfaceBlockEntity: DeviceBlockEntity(PhyBlockEntities.INTERFACE), ItemStackPacketHandler {
|
private val facing: Direction
|
||||||
//
|
get() = world!!.getBlockState(pos)[InterfaceBlock.FACING]
|
||||||
// override val interfaces = listOf(BaseInterface(this))
|
|
||||||
//
|
// todo: should this be a weak ref?
|
||||||
// private val facing: Direction
|
private var inventory: GroupedItemInv? = null
|
||||||
// get() = world!!.getBlockState(pos)[InterfaceBlock.FACING]
|
|
||||||
//
|
fun updateInventory() {
|
||||||
// // todo: should this be a weak ref?
|
val offsetPos = pos.offset(facing)
|
||||||
// private var inventory: GroupedItemInv? = null
|
val option = SearchOptions.inDirection(facing)
|
||||||
//
|
inventory = ItemAttributes.GROUPED_INV.getFirstOrNull(world, offsetPos, option)
|
||||||
// fun updateInventory() {
|
}
|
||||||
// val offsetPos = pos.offset(facing)
|
|
||||||
// val option = SearchOptions.inDirection(facing)
|
private fun getInventory(): GroupedItemInv? {
|
||||||
// inventory = ItemAttributes.GROUPED_INV.getFirstOrNull(world, offsetPos, option)
|
// if we don't have an inventory, try to get one
|
||||||
// }
|
// this happens when readAll is called before a neighbor state changes, such as immediately after world load
|
||||||
//
|
if (inventory == null) updateInventory()
|
||||||
// private fun getInventory(): GroupedItemInv? {
|
return inventory
|
||||||
// // if we don't have an inventory, try to get one
|
}
|
||||||
// // this happens when readAll is called before a neighbor state changes, such as immediately after world load
|
|
||||||
// if (inventory == null) updateInventory()
|
override fun handle(packet: Packet) {
|
||||||
// return inventory
|
when (packet) {
|
||||||
// }
|
is RequestInventoryPacket -> handleRequestInventory(packet)
|
||||||
//
|
is LocateStackPacket -> handleLocateStack(packet)
|
||||||
// override fun handle(packet: Packet, itf: Interface) {
|
is ExtractStackPacket -> handleExtractStack(packet)
|
||||||
// when (packet) {
|
is CheckCapacityPacket -> handleCheckCapacity(packet)
|
||||||
// is RequestInventoryPacket -> handleRequestInventory(packet)
|
is ItemStackPacket -> handleItemStack(packet)
|
||||||
// is LocateStackPacket -> handleLocateStack(packet)
|
}
|
||||||
// is ExtractStackPacket -> handleExtractStack(packet)
|
}
|
||||||
// is CheckCapacityPacket -> handleCheckCapacity(packet)
|
|
||||||
// is ItemStackPacket -> handleItemStack(packet)
|
private fun handleRequestInventory(packet: RequestInventoryPacket) {
|
||||||
// }
|
getInventory()?.also { inv ->
|
||||||
// }
|
sendPacket(ReadInventoryPacket(inv, ipAddress, packet.source))
|
||||||
//
|
}
|
||||||
// private fun handleRequestInventory(packet: RequestInventoryPacket) {
|
}
|
||||||
// getInventory()?.also { inv ->
|
|
||||||
// sendPacket(ReadInventoryPacket(inv, ipAddress, packet.source))
|
private fun handleLocateStack(packet: LocateStackPacket) {
|
||||||
// }
|
getInventory()?.also { inv ->
|
||||||
// }
|
val amount = inv.getAmount(packet.stack)
|
||||||
//
|
sendPacket(StackLocationPacket(packet.stack, amount, this, ipAddress, packet.source))
|
||||||
// private fun handleLocateStack(packet: LocateStackPacket) {
|
}
|
||||||
// getInventory()?.also { inv ->
|
}
|
||||||
// val amount = inv.getAmount(packet.stack)
|
|
||||||
// sendPacket(StackLocationPacket(packet.stack, amount, this, ipAddress, packet.source))
|
private fun handleExtractStack(packet: ExtractStackPacket) {
|
||||||
// }
|
getInventory()?.also { inv ->
|
||||||
// }
|
val extracted = inv.extract(packet.stack, packet.amount)
|
||||||
//
|
sendPacket(ItemStackPacket(extracted, ipAddress, packet.source))
|
||||||
// private fun handleExtractStack(packet: ExtractStackPacket) {
|
}
|
||||||
// getInventory()?.also { inv ->
|
}
|
||||||
// val extracted = inv.extract(packet.stack, packet.amount)
|
|
||||||
// sendPacket(ItemStackPacket(extracted, ipAddress, packet.source))
|
private fun handleCheckCapacity(packet: CheckCapacityPacket) {
|
||||||
// }
|
getInventory()?.also { inv ->
|
||||||
// }
|
val remaining = inv.attemptInsertion(packet.stack, Simulation.SIMULATE)
|
||||||
//
|
val couldAccept = packet.stack.count - remaining.count
|
||||||
// private fun handleCheckCapacity(packet: CheckCapacityPacket) {
|
sendPacket(CapacityPacket(packet.stack, couldAccept, this, ipAddress, packet.source))
|
||||||
// getInventory()?.also { inv ->
|
}
|
||||||
// val remaining = inv.attemptInsertion(packet.stack, Simulation.SIMULATE)
|
}
|
||||||
// val couldAccept = packet.stack.count - remaining.count
|
|
||||||
// sendPacket(CapacityPacket(packet.stack, couldAccept, this, ipAddress, packet.source))
|
override fun doHandleItemStack(packet: ItemStackPacket): ItemStack {
|
||||||
// }
|
val inventory = getInventory()
|
||||||
// }
|
if (inventory != null) {
|
||||||
//
|
val remaining = inventory.insert(packet.stack)
|
||||||
// override fun doHandleItemStack(packet: ItemStackPacket): ItemStack {
|
// whatever could not be inserted will be sent back to the packet's source
|
||||||
// val inventory = getInventory()
|
return remaining
|
||||||
// if (inventory != null) {
|
} else {
|
||||||
// val remaining = inventory.insert(packet.stack)
|
return packet.stack
|
||||||
// // whatever could not be inserted will be sent back to the packet's source
|
}
|
||||||
// return remaining
|
}
|
||||||
// } else {
|
|
||||||
// return packet.stack
|
}
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
//}
|
|
||||||
|
|
|
@ -3,14 +3,17 @@ package net.shadowfacts.phycon.network.block.netswitch
|
||||||
import net.fabricmc.fabric.api.block.entity.BlockEntityClientSerializable
|
import net.fabricmc.fabric.api.block.entity.BlockEntityClientSerializable
|
||||||
import net.minecraft.block.BlockState
|
import net.minecraft.block.BlockState
|
||||||
import net.minecraft.block.entity.BlockEntity
|
import net.minecraft.block.entity.BlockEntity
|
||||||
|
import net.minecraft.entity.ItemEntity
|
||||||
import net.minecraft.nbt.CompoundTag
|
import net.minecraft.nbt.CompoundTag
|
||||||
import net.minecraft.util.Tickable
|
import net.minecraft.util.Tickable
|
||||||
import net.minecraft.util.math.Direction
|
import net.minecraft.util.math.Direction
|
||||||
import net.shadowfacts.phycon.api.Interface
|
import net.shadowfacts.phycon.api.Interface
|
||||||
import net.shadowfacts.phycon.api.frame.EthernetFrame
|
import net.shadowfacts.phycon.api.frame.EthernetFrame
|
||||||
|
import net.shadowfacts.phycon.api.frame.PacketFrame
|
||||||
import net.shadowfacts.phycon.api.util.MACAddress
|
import net.shadowfacts.phycon.api.util.MACAddress
|
||||||
import net.shadowfacts.phycon.init.PhyBlockEntities
|
import net.shadowfacts.phycon.init.PhyBlockEntities
|
||||||
import net.shadowfacts.phycon.network.NetworkUtil
|
import net.shadowfacts.phycon.network.NetworkUtil
|
||||||
|
import net.shadowfacts.phycon.network.packet.ItemStackPacket
|
||||||
import java.lang.ref.WeakReference
|
import java.lang.ref.WeakReference
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -21,45 +24,13 @@ class SwitchBlockEntity: BlockEntity(PhyBlockEntities.SWITCH),
|
||||||
Tickable {
|
Tickable {
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
var SWITCHING_CAPACITY = 256
|
var SWITCHING_CAPACITY = 256 // 256 items/tick
|
||||||
}
|
}
|
||||||
|
|
||||||
val interfaces = Direction.values().map { SwitchInterface(it, WeakReference(this), MACAddress.random()) }
|
val interfaces = Direction.values().map { SwitchInterface(it, WeakReference(this), MACAddress.random()) }
|
||||||
|
|
||||||
private val macTable = mutableMapOf<MACAddress, Direction>()
|
private val macTable = mutableMapOf<MACAddress, Direction>()
|
||||||
private var packetsHandledThisTick = 0
|
private var itemsHandledThisTick = 0
|
||||||
|
|
||||||
// override fun handle(packet: Packet, itf: Interface) {
|
|
||||||
// if (packetsHandledThisTick >= SWITCHING_CAPACITY) {
|
|
||||||
// if (packet is ItemStackPacket) {
|
|
||||||
// // todo: calculate entity spawn point by finding non-obstructed location
|
|
||||||
// val entity = ItemEntity(world!!, pos.x.toDouble(), pos.y + 1.0, pos.z.toDouble(), packet.stack)
|
|
||||||
// world!!.spawnEntity(entity)
|
|
||||||
// }
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// packetsHandledThisTick++
|
|
||||||
//
|
|
||||||
// if (packet.destination.isBroadcast) {
|
|
||||||
//// for (other in interfaces) {
|
|
||||||
//// if (other == itf) continue
|
|
||||||
//// sendPacket(packet, other)
|
|
||||||
//// }
|
|
||||||
// flood(packet, itf)
|
|
||||||
// } else {
|
|
||||||
// val direction = ipTable[packet.destination]
|
|
||||||
// if (direction != null) {
|
|
||||||
// sendPacket()
|
|
||||||
//// val dest = findDestination(direction)
|
|
||||||
//// if (dest != null && packet.destination == dest.macAddress) {
|
|
||||||
//// sendPacke(packet, dest)
|
|
||||||
//// return
|
|
||||||
//// }
|
|
||||||
// }
|
|
||||||
// flood(packet, itf)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
fun interfaceForSide(side: Direction): SwitchInterface {
|
fun interfaceForSide(side: Direction): SwitchInterface {
|
||||||
return interfaces.find { it.side == side }!!
|
return interfaces.find { it.side == side }!!
|
||||||
|
@ -69,10 +40,22 @@ class SwitchBlockEntity: BlockEntity(PhyBlockEntities.SWITCH),
|
||||||
val itfSide = (fromItf as SwitchInterface).side
|
val itfSide = (fromItf as SwitchInterface).side
|
||||||
macTable[frame.source] = itfSide
|
macTable[frame.source] = itfSide
|
||||||
|
|
||||||
val knownDir = macTable[frame.destination]
|
if (frame is PacketFrame && frame.packet is ItemStackPacket) {
|
||||||
if (knownDir != null) {
|
val packet = frame.packet as ItemStackPacket
|
||||||
println("$this ($itfSide, ${fromItf.macAddress}) forwarding $frame to side $knownDir")
|
if (itemsHandledThisTick + packet.stack.count > SWITCHING_CAPACITY) {
|
||||||
interfaceForSide(knownDir).send(frame)
|
// todo: calculate entity spawn point by finding non-obstructed location
|
||||||
|
val entity = ItemEntity(world!!, pos.x.toDouble(), pos.y + 1.0, pos.z.toDouble(), packet.stack)
|
||||||
|
world!!.spawnEntity(entity)
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
itemsHandledThisTick += packet.stack.count
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (frame.destination.type != MACAddress.Type.BROADCAST && macTable.containsKey(frame.destination)) {
|
||||||
|
val dir = macTable[frame.destination]!!
|
||||||
|
println("$this ($itfSide, ${fromItf.macAddress}) forwarding $frame to side $dir")
|
||||||
|
interfaceForSide(dir).send(frame)
|
||||||
} else {
|
} else {
|
||||||
println("$this ($itfSide, ${fromItf.macAddress}) flooding $frame")
|
println("$this ($itfSide, ${fromItf.macAddress}) flooding $frame")
|
||||||
flood(frame, fromItf)
|
flood(frame, fromItf)
|
||||||
|
@ -92,7 +75,7 @@ class SwitchBlockEntity: BlockEntity(PhyBlockEntities.SWITCH),
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun tick() {
|
override fun tick() {
|
||||||
packetsHandledThisTick = 0
|
itemsHandledThisTick = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun toTag(tag: CompoundTag): CompoundTag {
|
override fun toTag(tag: CompoundTag): CompoundTag {
|
||||||
|
|
|
@ -1,44 +1,46 @@
|
||||||
package net.shadowfacts.phycon.network.block.terminal
|
package net.shadowfacts.phycon.network.block.terminal
|
||||||
//
|
|
||||||
//import alexiil.mc.lib.attributes.AttributeList
|
import alexiil.mc.lib.attributes.AttributeList
|
||||||
//import alexiil.mc.lib.attributes.AttributeProvider
|
import alexiil.mc.lib.attributes.AttributeProvider
|
||||||
//import net.minecraft.block.BlockState
|
import net.minecraft.block.BlockState
|
||||||
//import net.minecraft.block.Material
|
import net.minecraft.block.Material
|
||||||
//import net.minecraft.entity.player.PlayerEntity
|
import net.minecraft.entity.player.PlayerEntity
|
||||||
//import net.minecraft.util.ActionResult
|
import net.minecraft.util.ActionResult
|
||||||
//import net.minecraft.util.Hand
|
import net.minecraft.util.Hand
|
||||||
//import net.minecraft.util.Identifier
|
import net.minecraft.util.Identifier
|
||||||
//import net.minecraft.util.hit.BlockHitResult
|
import net.minecraft.util.hit.BlockHitResult
|
||||||
//import net.minecraft.util.math.BlockPos
|
import net.minecraft.util.math.BlockPos
|
||||||
//import net.minecraft.util.math.Direction
|
import net.minecraft.util.math.Direction
|
||||||
//import net.minecraft.world.BlockView
|
import net.minecraft.world.BlockView
|
||||||
//import net.minecraft.world.World
|
import net.minecraft.world.World
|
||||||
//import net.shadowfacts.phycon.PhysicalConnectivity
|
import net.shadowfacts.phycon.PhysicalConnectivity
|
||||||
//import net.shadowfacts.phycon.api.NetworkComponentBlock
|
import net.shadowfacts.phycon.api.NetworkComponentBlock
|
||||||
//import net.shadowfacts.phycon.block.BlockWithEntity
|
import net.shadowfacts.phycon.network.DeviceBlock
|
||||||
//import java.util.*
|
import java.util.*
|
||||||
//
|
|
||||||
///**
|
/**
|
||||||
// * @author shadowfacts
|
* @author shadowfacts
|
||||||
// */
|
*/
|
||||||
//class TerminalBlock: BlockWithEntity<TerminalBlockEntity>(Settings.of(Material.METAL)),
|
class TerminalBlock: DeviceBlock<TerminalBlockEntity>(Settings.of(Material.METAL)),
|
||||||
// NetworkComponentBlock, AttributeProvider {
|
NetworkComponentBlock,
|
||||||
// companion object {
|
AttributeProvider {
|
||||||
// val ID = Identifier(PhysicalConnectivity.MODID, "terminal")
|
|
||||||
// }
|
companion object {
|
||||||
//
|
val ID = Identifier(PhysicalConnectivity.MODID, "terminal")
|
||||||
// override fun getNetworkConnectedSides(state: BlockState, world: World, pos: BlockPos): Collection<Direction> {
|
}
|
||||||
// return EnumSet.allOf(Direction::class.java)
|
|
||||||
// }
|
override fun getNetworkConnectedSides(state: BlockState, world: World, pos: BlockPos): Collection<Direction> {
|
||||||
//
|
return EnumSet.allOf(Direction::class.java)
|
||||||
// override fun createBlockEntity(world: BlockView) = TerminalBlockEntity()
|
}
|
||||||
//
|
|
||||||
// override fun onUse(state: BlockState, world: World, pos: BlockPos, player: PlayerEntity, hand: Hand, hitResult: BlockHitResult): ActionResult {
|
override fun createBlockEntity(world: BlockView) = TerminalBlockEntity()
|
||||||
// getBlockEntity(world, pos)!!.onActivate(player)
|
|
||||||
// return ActionResult.SUCCESS
|
override fun onUse(state: BlockState, world: World, pos: BlockPos, player: PlayerEntity, hand: Hand, hitResult: BlockHitResult): ActionResult {
|
||||||
// }
|
getBlockEntity(world, pos)!!.onActivate(player)
|
||||||
//
|
return ActionResult.SUCCESS
|
||||||
// override fun addAllAttributes(world: World, pos: BlockPos, state: BlockState, to: AttributeList<*>) {
|
}
|
||||||
// to.offer(getBlockEntity(world, pos))
|
|
||||||
// }
|
override fun addAllAttributes(world: World, pos: BlockPos, state: BlockState, to: AttributeList<*>) {
|
||||||
//}
|
to.offer(getBlockEntity(world, pos))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,330 +1,349 @@
|
||||||
package net.shadowfacts.phycon.network.block.terminal
|
package net.shadowfacts.phycon.network.block.terminal
|
||||||
//
|
|
||||||
//import alexiil.mc.lib.attributes.item.GroupedItemInv
|
import alexiil.mc.lib.attributes.item.GroupedItemInv
|
||||||
//import alexiil.mc.lib.attributes.item.ItemStackCollections
|
import alexiil.mc.lib.attributes.item.ItemStackCollections
|
||||||
//import alexiil.mc.lib.attributes.item.ItemStackUtil
|
import alexiil.mc.lib.attributes.item.ItemStackUtil
|
||||||
//import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap
|
import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap
|
||||||
//import net.fabricmc.fabric.api.block.entity.BlockEntityClientSerializable
|
import net.fabricmc.fabric.api.block.entity.BlockEntityClientSerializable
|
||||||
//import net.fabricmc.fabric.api.screenhandler.v1.ExtendedScreenHandlerFactory
|
import net.fabricmc.fabric.api.screenhandler.v1.ExtendedScreenHandlerFactory
|
||||||
//import net.minecraft.block.BlockState
|
import net.minecraft.block.BlockState
|
||||||
//import net.minecraft.entity.player.PlayerEntity
|
import net.minecraft.entity.player.PlayerEntity
|
||||||
//import net.minecraft.entity.player.PlayerInventory
|
import net.minecraft.entity.player.PlayerInventory
|
||||||
//import net.minecraft.inventory.Inventory
|
import net.minecraft.inventory.Inventory
|
||||||
//import net.minecraft.inventory.InventoryChangedListener
|
import net.minecraft.inventory.InventoryChangedListener
|
||||||
//import net.minecraft.item.ItemStack
|
import net.minecraft.item.ItemStack
|
||||||
//import net.minecraft.nbt.CompoundTag
|
import net.minecraft.nbt.CompoundTag
|
||||||
//import net.minecraft.nbt.ListTag
|
import net.minecraft.nbt.ListTag
|
||||||
//import net.minecraft.network.PacketByteBuf
|
import net.minecraft.network.PacketByteBuf
|
||||||
//import net.minecraft.screen.ScreenHandler
|
import net.minecraft.screen.ScreenHandler
|
||||||
//import net.minecraft.server.network.ServerPlayerEntity
|
import net.minecraft.server.network.ServerPlayerEntity
|
||||||
//import net.minecraft.text.LiteralText
|
import net.minecraft.text.LiteralText
|
||||||
//import net.minecraft.util.Tickable
|
import net.minecraft.util.Tickable
|
||||||
//import net.shadowfacts.phycon.api.packet.Packet
|
import net.minecraft.util.math.Direction
|
||||||
//import net.shadowfacts.phycon.api.util.MACAddress
|
import net.shadowfacts.phycon.api.Interface
|
||||||
//import net.shadowfacts.phycon.init.PhyBlockEntities
|
import net.shadowfacts.phycon.api.packet.Packet
|
||||||
//import net.shadowfacts.phycon.network.DeviceBlockEntity
|
import net.shadowfacts.phycon.api.util.IPAddress
|
||||||
//import net.shadowfacts.phycon.network.block.netinterface.InterfaceBlockEntity
|
import net.shadowfacts.phycon.init.PhyBlockEntities
|
||||||
//import net.shadowfacts.phycon.network.component.ItemStackPacketHandler
|
import net.shadowfacts.phycon.network.DeviceBlockEntity
|
||||||
//import net.shadowfacts.phycon.network.component.handleItemStack
|
import net.shadowfacts.phycon.network.NetworkUtil
|
||||||
//import net.shadowfacts.phycon.network.packet.*
|
import net.shadowfacts.phycon.network.block.netinterface.InterfaceBlockEntity
|
||||||
//import java.util.*
|
import net.shadowfacts.phycon.network.component.ItemStackPacketHandler
|
||||||
//import kotlin.math.min
|
import net.shadowfacts.phycon.network.component.handleItemStack
|
||||||
//
|
import net.shadowfacts.phycon.network.packet.*
|
||||||
///**
|
import java.util.*
|
||||||
// * @author shadowfacts
|
import kotlin.math.min
|
||||||
// */
|
|
||||||
//class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL), InventoryChangedListener, BlockEntityClientSerializable, Tickable, ItemStackPacketHandler {
|
/**
|
||||||
//
|
* @author shadowfacts
|
||||||
// companion object {
|
*/
|
||||||
// val LOCATE_REQUEST_TIMEOUT = 40 // ticks
|
class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL), InventoryChangedListener, BlockEntityClientSerializable, Tickable, ItemStackPacketHandler {
|
||||||
// val INSERTION_TIMEOUT = 40
|
|
||||||
// }
|
companion object {
|
||||||
//
|
val LOCATE_REQUEST_TIMEOUT = 40 // ticks
|
||||||
// private val inventoryCache = mutableMapOf<MACAddress, GroupedItemInv>()
|
val INSERTION_TIMEOUT = 40
|
||||||
// val internalBuffer = TerminalBufferInventory(18)
|
}
|
||||||
//
|
|
||||||
// private val locateRequestQueue = LinkedList<StackLocateRequest>()
|
private val inventoryCache = mutableMapOf<IPAddress, GroupedItemInv>()
|
||||||
// private val pendingRequests = LinkedList<StackLocateRequest>()
|
val internalBuffer = TerminalBufferInventory(18)
|
||||||
// private val pendingInsertions = Int2ObjectArrayMap<PendingStackInsertion>()
|
|
||||||
//
|
private val locateRequestQueue = LinkedList<StackLocateRequest>()
|
||||||
// private var observers = 0
|
private val pendingRequests = LinkedList<StackLocateRequest>()
|
||||||
// val cachedNetItems = ItemStackCollections.intMap()
|
private val pendingInsertions = Int2ObjectArrayMap<PendingStackInsertion>()
|
||||||
// var cachedSortedNetItems = listOf<ItemStack>()
|
|
||||||
// var counter = 0
|
private var observers = 0
|
||||||
//
|
val cachedNetItems = ItemStackCollections.intMap()
|
||||||
// init {
|
var cachedSortedNetItems = listOf<ItemStack>()
|
||||||
// internalBuffer.addListener(this)
|
|
||||||
// }
|
init {
|
||||||
//
|
internalBuffer.addListener(this)
|
||||||
// override fun handlePacket(packet: Packet) {
|
}
|
||||||
// when (packet) {
|
|
||||||
// is ReadInventoryPacket -> handleReadInventory(packet)
|
override fun findDestination(): Interface? {
|
||||||
// is DeviceRemovedPacket -> handleDeviceRemoved(packet)
|
for (dir in Direction.values()) {
|
||||||
// is StackLocationPacket -> handleStackLocation(packet)
|
val itf = NetworkUtil.findConnectedInterface(world!!, pos, dir)
|
||||||
// is ItemStackPacket -> handleItemStack(packet)
|
if (itf != null) {
|
||||||
// is CapacityPacket -> handleCapacity(packet)
|
return itf
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
//
|
return null
|
||||||
// private fun handleReadInventory(packet: ReadInventoryPacket) {
|
}
|
||||||
// inventoryCache[packet.source] = packet.inventory
|
|
||||||
// updateNetItems()
|
override fun handle(packet: Packet) {
|
||||||
// sync()
|
when (packet) {
|
||||||
// }
|
is ReadInventoryPacket -> handleReadInventory(packet)
|
||||||
//
|
is DeviceRemovedPacket -> handleDeviceRemoved(packet)
|
||||||
// private fun handleDeviceRemoved(packet: DeviceRemovedPacket) {
|
is StackLocationPacket -> handleStackLocation(packet)
|
||||||
// inventoryCache.remove(packet.source)
|
is ItemStackPacket -> handleItemStack(packet)
|
||||||
// updateNetItems()
|
is CapacityPacket -> handleCapacity(packet)
|
||||||
// sync()
|
}
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// private fun handleStackLocation(packet: StackLocationPacket) {
|
private fun handleReadInventory(packet: ReadInventoryPacket) {
|
||||||
// val request = pendingRequests.firstOrNull {
|
inventoryCache[packet.source] = packet.inventory
|
||||||
// ItemStackUtil.areEqualIgnoreAmounts(it.stack, packet.stack)
|
updateNetItems()
|
||||||
// }
|
sync()
|
||||||
// if (request != null) {
|
}
|
||||||
// request.results.add(packet.amount to packet.sourceInterface)
|
|
||||||
// if (request.totalResultAmount >= request.amount || counter - request.timestamp >= LOCATE_REQUEST_TIMEOUT || request.results.size >= inventoryCache.size) {
|
private fun handleDeviceRemoved(packet: DeviceRemovedPacket) {
|
||||||
// stackLocateRequestCompleted(request)
|
inventoryCache.remove(packet.source)
|
||||||
// }
|
updateNetItems()
|
||||||
// }
|
sync()
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// override fun doHandleItemStack(packet: ItemStackPacket): ItemStack {
|
private fun handleStackLocation(packet: StackLocationPacket) {
|
||||||
// val remaining = internalBuffer.insertFromNetwork(packet.stack)
|
val request = pendingRequests.firstOrNull {
|
||||||
//
|
ItemStackUtil.areEqualIgnoreAmounts(it.stack, packet.stack)
|
||||||
// // this happens outside the normal update loop because by receiving the item stack packet
|
}
|
||||||
// // we "know" how much the count in the source inventory has changed
|
if (request != null) {
|
||||||
// updateNetItems()
|
request.results.add(packet.amount to packet.sourceInterface)
|
||||||
// sync()
|
if (request.totalResultAmount >= request.amount || counter - request.timestamp >= LOCATE_REQUEST_TIMEOUT || request.results.size >= inventoryCache.size) {
|
||||||
//
|
stackLocateRequestCompleted(request)
|
||||||
// return remaining
|
}
|
||||||
// }
|
}
|
||||||
//
|
}
|
||||||
// private fun handleCapacity(packet: CapacityPacket) {
|
|
||||||
// val insertion = pendingInsertions.values.firstOrNull {
|
override fun doHandleItemStack(packet: ItemStackPacket): ItemStack {
|
||||||
// ItemStackUtil.areEqualIgnoreAmounts(packet.stack, it.stack)
|
val remaining = internalBuffer.insertFromNetwork(packet.stack)
|
||||||
// }
|
|
||||||
// if (insertion != null) {
|
// this happens outside the normal update loop because by receiving the item stack packet
|
||||||
// insertion.results.add(packet.capacity to packet.receivingInterface)
|
// we "know" how much the count in the source inventory has changed
|
||||||
// if (insertion.isFinishable(counter)) {
|
updateNetItems()
|
||||||
// finishInsertion(insertion)
|
sync()
|
||||||
// }
|
|
||||||
// }
|
return remaining
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// private fun updateNetItems() {
|
private fun handleCapacity(packet: CapacityPacket) {
|
||||||
// cachedNetItems.clear()
|
val insertion = pendingInsertions.values.firstOrNull {
|
||||||
// for (inventory in inventoryCache.values) {
|
ItemStackUtil.areEqualIgnoreAmounts(packet.stack, it.stack)
|
||||||
// for (stack in inventory.storedStacks) {
|
}
|
||||||
// val amount = inventory.getAmount(stack)
|
if (insertion != null) {
|
||||||
// cachedNetItems.mergeInt(stack, amount) { a, b -> a + b }
|
insertion.results.add(packet.capacity to packet.receivingInterface)
|
||||||
// }
|
if (insertion.isFinishable(counter)) {
|
||||||
// }
|
finishInsertion(insertion)
|
||||||
// // todo: is the map necessary or is just the sorted list enough?
|
}
|
||||||
// cachedSortedNetItems = cachedNetItems.object2IntEntrySet().sortedByDescending { it.intValue }.map {
|
}
|
||||||
// val stack = it.key.copy()
|
}
|
||||||
// stack.count = it.intValue
|
|
||||||
// stack
|
private fun updateNetItems() {
|
||||||
// }
|
cachedNetItems.clear()
|
||||||
// }
|
for (inventory in inventoryCache.values) {
|
||||||
//
|
for (stack in inventory.storedStacks) {
|
||||||
// private fun beginInsertions() {
|
val amount = inventory.getAmount(stack)
|
||||||
// if (world!!.isClient) return
|
cachedNetItems.mergeInt(stack, amount) { a, b -> a + b }
|
||||||
//
|
}
|
||||||
// for (slot in 0 until internalBuffer.size()) {
|
}
|
||||||
// if (internalBuffer.getMode(slot) != TerminalBufferInventory.Mode.TO_NETWORK) continue
|
// todo: is the map necessary or is just the sorted list enough?
|
||||||
// if (slot in pendingInsertions) continue
|
cachedSortedNetItems = cachedNetItems.object2IntEntrySet().sortedByDescending { it.intValue }.map {
|
||||||
// val stack = internalBuffer.getStack(slot)
|
val stack = it.key.copy()
|
||||||
// pendingInsertions[slot] = PendingStackInsertion(slot, stack, counter)
|
stack.count = it.intValue
|
||||||
// sendToSingle(CheckCapacityPacket(stack, macAddress, MACAddress.BROADCAST))
|
stack
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// private fun finishPendingInsertions() {
|
private fun beginInsertions() {
|
||||||
// if (world!!.isClient) return
|
if (world!!.isClient) return
|
||||||
//
|
|
||||||
// for (insertion in pendingInsertions.values) {
|
for (slot in 0 until internalBuffer.size()) {
|
||||||
// if (!insertion.isFinishable(counter)) continue
|
if (internalBuffer.getMode(slot) != TerminalBufferInventory.Mode.TO_NETWORK) continue
|
||||||
// finishInsertion(insertion)
|
if (slot in pendingInsertions) continue
|
||||||
// }
|
val stack = internalBuffer.getStack(slot)
|
||||||
// }
|
pendingInsertions[slot] = PendingStackInsertion(slot, stack, counter)
|
||||||
//
|
sendPacket(CheckCapacityPacket(stack, ipAddress, IPAddress.BROADCAST))
|
||||||
// private fun sendEnqueuedLocateRequests() {
|
}
|
||||||
// if (world!!.isClient) return
|
}
|
||||||
//
|
|
||||||
// for (request in locateRequestQueue) {
|
private fun finishPendingInsertions() {
|
||||||
// pendingRequests.add(request)
|
if (world!!.isClient) return
|
||||||
// sendToSingle(LocateStackPacket(request.stack, macAddress))
|
|
||||||
// }
|
for (insertion in pendingInsertions.values) {
|
||||||
// locateRequestQueue.clear()
|
if (!insertion.isFinishable(counter)) continue
|
||||||
// }
|
finishInsertion(insertion)
|
||||||
//
|
}
|
||||||
// private fun finishPendingRequests() {
|
}
|
||||||
// if (world!!.isClient) return
|
|
||||||
//
|
private fun sendEnqueuedLocateRequests() {
|
||||||
// for (request in pendingRequests) {
|
if (world!!.isClient) return
|
||||||
// if (request.isFinishable(counter)) {
|
|
||||||
// stackLocateRequestCompleted(request)
|
for (request in locateRequestQueue) {
|
||||||
// }
|
pendingRequests.add(request)
|
||||||
// }
|
sendPacket(LocateStackPacket(request.stack, ipAddress))
|
||||||
// }
|
}
|
||||||
//
|
locateRequestQueue.clear()
|
||||||
// fun addObserver() {
|
}
|
||||||
// observers++
|
|
||||||
// }
|
private fun finishPendingRequests() {
|
||||||
//
|
if (world!!.isClient) return
|
||||||
// fun removeObserver() {
|
|
||||||
// observers--
|
for (request in pendingRequests) {
|
||||||
// }
|
if (request.isFinishable(counter)) {
|
||||||
//
|
stackLocateRequestCompleted(request)
|
||||||
// override fun tick() {
|
}
|
||||||
// if (observers > 0 && (++counter % 10) == 0) {
|
}
|
||||||
// if (world!!.isClient) {
|
}
|
||||||
// println(cachedNetItems)
|
|
||||||
// } else {
|
fun addObserver() {
|
||||||
// updateNetItems()
|
observers++
|
||||||
// sendEnqueuedLocateRequests()
|
}
|
||||||
// finishPendingRequests()
|
|
||||||
// beginInsertions()
|
fun removeObserver() {
|
||||||
// finishPendingInsertions()
|
observers--
|
||||||
// sync()
|
}
|
||||||
// }
|
|
||||||
// }
|
override fun tick() {
|
||||||
// }
|
super.tick()
|
||||||
//
|
|
||||||
// fun onActivate(player: PlayerEntity) {
|
if (counter % 20 == 0L) {
|
||||||
// if (!world!!.isClient) {
|
if (!world!!.isClient) {
|
||||||
// updateNetItems()
|
sendEnqueuedLocateRequests()
|
||||||
// sync()
|
finishPendingRequests()
|
||||||
//
|
beginInsertions()
|
||||||
// inventoryCache.clear()
|
finishPendingInsertions()
|
||||||
// sendToSingle(RequestInventoryPacket(macAddress))
|
}
|
||||||
// val factory = object: ExtendedScreenHandlerFactory {
|
|
||||||
// override fun createMenu(syncId: Int, playerInv: PlayerInventory, player: PlayerEntity): ScreenHandler? {
|
if (observers > 0) {
|
||||||
// return TerminalScreenHandler(syncId, playerInv, this@TerminalBlockEntity)
|
if (world!!.isClient) {
|
||||||
// }
|
println(cachedNetItems)
|
||||||
//
|
} else {
|
||||||
// override fun getDisplayName() = LiteralText("Terminal")
|
updateNetItems()
|
||||||
//
|
sync()
|
||||||
// override fun writeScreenOpeningData(player: ServerPlayerEntity, buf: PacketByteBuf) {
|
}
|
||||||
// buf.writeBlockPos(this@TerminalBlockEntity.pos)
|
}
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
// player.openHandledScreen(factory)
|
|
||||||
// }
|
fun onActivate(player: PlayerEntity) {
|
||||||
// addObserver()
|
if (!world!!.isClient) {
|
||||||
// }
|
updateNetItems()
|
||||||
//
|
sync()
|
||||||
// fun requestItem(stack: ItemStack, amount: Int = stack.count) {
|
|
||||||
// locateRequestQueue.add(StackLocateRequest(stack, amount, counter))
|
inventoryCache.clear()
|
||||||
// }
|
sendPacket(RequestInventoryPacket(ipAddress))
|
||||||
//
|
val factory = object: ExtendedScreenHandlerFactory {
|
||||||
// private fun stackLocateRequestCompleted(request: StackLocateRequest) {
|
override fun createMenu(syncId: Int, playerInv: PlayerInventory, player: PlayerEntity): ScreenHandler? {
|
||||||
// pendingRequests.remove(request)
|
return TerminalScreenHandler(syncId, playerInv, this@TerminalBlockEntity)
|
||||||
//
|
}
|
||||||
// // todo: also sort results by interface priority
|
|
||||||
// val sortedResults = request.results.sortedByDescending { it.first }.toMutableList()
|
override fun getDisplayName() = LiteralText("Terminal")
|
||||||
// var amountRequested = 0
|
|
||||||
// while (amountRequested < request.amount && sortedResults.isNotEmpty()) {
|
override fun writeScreenOpeningData(player: ServerPlayerEntity, buf: PacketByteBuf) {
|
||||||
// val (sourceAmount, sourceInterface) = sortedResults.removeAt(0)
|
buf.writeBlockPos(this@TerminalBlockEntity.pos)
|
||||||
// val amountToRequest = min(sourceAmount, request.amount - amountRequested)
|
}
|
||||||
// amountRequested += amountToRequest
|
}
|
||||||
// sendToSingle(ExtractStackPacket(request.stack, amountToRequest, macAddress, sourceInterface.macAddress))
|
player.openHandledScreen(factory)
|
||||||
// }
|
}
|
||||||
// }
|
addObserver()
|
||||||
//
|
}
|
||||||
// private fun finishInsertion(insertion: PendingStackInsertion) {
|
|
||||||
// pendingInsertions.remove(insertion.bufferSlot)
|
fun requestItem(stack: ItemStack, amount: Int = stack.count) {
|
||||||
//
|
locateRequestQueue.add(StackLocateRequest(stack, amount, counter))
|
||||||
// // todo: also sort results by interface priority
|
}
|
||||||
// val sortedResults = insertion.results.sortedBy { it.first }.toMutableList()
|
|
||||||
// val remaining = insertion.stack
|
private fun stackLocateRequestCompleted(request: StackLocateRequest) {
|
||||||
// while (!remaining.isEmpty && sortedResults.isNotEmpty()) {
|
pendingRequests.remove(request)
|
||||||
// val (capacity, receivingInterface) = sortedResults.removeAt(0)
|
|
||||||
// if (capacity <= 0) continue
|
// todo: also sort results by interface priority
|
||||||
// sendToSingle(ItemStackPacket(remaining.copy(), macAddress, receivingInterface.macAddress))
|
val sortedResults = request.results.sortedByDescending { it.first }.toMutableList()
|
||||||
// // todo: the interface should confirm how much was actually inserted, in case of race condition
|
var amountRequested = 0
|
||||||
// remaining.count -= capacity
|
while (amountRequested < request.amount && sortedResults.isNotEmpty()) {
|
||||||
// }
|
val (sourceAmount, sourceInterface) = sortedResults.removeAt(0)
|
||||||
// internalBuffer.setStack(insertion.bufferSlot, remaining)
|
val amountToRequest = min(sourceAmount, request.amount - amountRequested)
|
||||||
//
|
amountRequested += amountToRequest
|
||||||
// // as with extracting, we "know" the new amounts and so can update instantly without actually sending out packets
|
sendPacket(ExtractStackPacket(request.stack, amountToRequest, ipAddress, sourceInterface.ipAddress))
|
||||||
// updateNetItems()
|
}
|
||||||
// sync()
|
}
|
||||||
// }
|
|
||||||
//
|
private fun finishInsertion(insertion: PendingStackInsertion) {
|
||||||
// override fun onInventoryChanged(inv: Inventory) {
|
pendingInsertions.remove(insertion.bufferSlot)
|
||||||
// if (inv == internalBuffer && world != null && !world!!.isClient) {
|
|
||||||
// markDirty()
|
// todo: also sort results by interface priority
|
||||||
// sync()
|
val sortedResults = insertion.results.sortedBy { it.first }.toMutableList()
|
||||||
// }
|
val remaining = insertion.stack
|
||||||
// }
|
while (!remaining.isEmpty && sortedResults.isNotEmpty()) {
|
||||||
//
|
val (capacity, receivingInterface) = sortedResults.removeAt(0)
|
||||||
// override fun toTag(tag: CompoundTag): CompoundTag {
|
if (capacity <= 0) continue
|
||||||
// tag.put("InternalBuffer", internalBuffer.toTag())
|
sendPacket(ItemStackPacket(remaining.copy(), ipAddress, receivingInterface.ipAddress))
|
||||||
// return super.toTag(tag)
|
// todo: the interface should confirm how much was actually inserted, in case of race condition
|
||||||
// }
|
remaining.count -= capacity
|
||||||
//
|
}
|
||||||
// override fun fromTag(state: BlockState, tag: CompoundTag) {
|
internalBuffer.setStack(insertion.bufferSlot, remaining)
|
||||||
// super.fromTag(state, tag)
|
|
||||||
// internalBuffer.fromTag(tag.getCompound("InternalBuffer"))
|
// as with extracting, we "know" the new amounts and so can update instantly without actually sending out packets
|
||||||
// }
|
updateNetItems()
|
||||||
//
|
sync()
|
||||||
// override fun toClientTag(tag: CompoundTag): CompoundTag {
|
}
|
||||||
// tag.put("InternalBuffer", internalBuffer.toTag())
|
|
||||||
// val list = ListTag()
|
override fun onInventoryChanged(inv: Inventory) {
|
||||||
// tag.put("CachedNetItems", list)
|
if (inv == internalBuffer && world != null && !world!!.isClient) {
|
||||||
// for ((stack, amount) in cachedNetItems) {
|
markDirty()
|
||||||
// val entryTag = stack.toTag(CompoundTag())
|
sync()
|
||||||
// entryTag.putInt("NetAmount", amount)
|
}
|
||||||
// list.add(entryTag)
|
}
|
||||||
// }
|
|
||||||
// return tag
|
override fun toTag(tag: CompoundTag): CompoundTag {
|
||||||
// }
|
tag.put("InternalBuffer", internalBuffer.toTag())
|
||||||
//
|
return super.toTag(tag)
|
||||||
// override fun fromClientTag(tag: CompoundTag) {
|
}
|
||||||
// internalBuffer.fromTag(tag.getCompound("InternalBuffer"))
|
|
||||||
// val list = tag.getList("CachedNetItems", 10)
|
override fun fromTag(state: BlockState, tag: CompoundTag) {
|
||||||
// cachedNetItems.clear()
|
super.fromTag(state, tag)
|
||||||
// for (entryTag in list) {
|
internalBuffer.fromTag(tag.getCompound("InternalBuffer"))
|
||||||
// val stack = ItemStack.fromTag(entryTag as CompoundTag)
|
}
|
||||||
// val netAmount = entryTag.getInt("NetAmount")
|
|
||||||
// cachedNetItems[stack] = netAmount
|
override fun toClientTag(tag: CompoundTag): CompoundTag {
|
||||||
// }
|
tag.put("InternalBuffer", internalBuffer.toTag())
|
||||||
// cachedSortedNetItems = cachedNetItems.object2IntEntrySet().sortedByDescending { it.intValue }.map {
|
val list = ListTag()
|
||||||
// val stack = it.key.copy()
|
tag.put("CachedNetItems", list)
|
||||||
// stack.count = it.intValue
|
for ((stack, amount) in cachedNetItems) {
|
||||||
// stack
|
val entryTag = stack.toTag(CompoundTag())
|
||||||
// }
|
entryTag.putInt("NetAmount", amount)
|
||||||
// }
|
list.add(entryTag)
|
||||||
//
|
}
|
||||||
//}
|
return tag
|
||||||
//
|
}
|
||||||
//data class StackLocateRequest(
|
|
||||||
// val stack: ItemStack,
|
override fun fromClientTag(tag: CompoundTag) {
|
||||||
// val amount: Int,
|
internalBuffer.fromTag(tag.getCompound("InternalBuffer"))
|
||||||
// val timestamp: Int,
|
val list = tag.getList("CachedNetItems", 10)
|
||||||
// var results: MutableSet<Pair<Int, InterfaceBlockEntity>> = mutableSetOf()
|
cachedNetItems.clear()
|
||||||
//) {
|
for (entryTag in list) {
|
||||||
// val totalResultAmount: Int
|
val stack = ItemStack.fromTag(entryTag as CompoundTag)
|
||||||
// get() = results.fold(0) { acc, (amount, _) -> acc + amount }
|
val netAmount = entryTag.getInt("NetAmount")
|
||||||
//
|
cachedNetItems[stack] = netAmount
|
||||||
// fun isFinishable(currentTimestamp: Int): Boolean {
|
}
|
||||||
// return totalResultAmount > amount || currentTimestamp - timestamp >= TerminalBlockEntity.LOCATE_REQUEST_TIMEOUT
|
cachedSortedNetItems = cachedNetItems.object2IntEntrySet().sortedByDescending { it.intValue }.map {
|
||||||
// }
|
val stack = it.key.copy()
|
||||||
//}
|
stack.count = it.intValue
|
||||||
//
|
stack
|
||||||
//data class PendingStackInsertion(
|
}
|
||||||
// val bufferSlot: Int,
|
}
|
||||||
// val stack: ItemStack,
|
|
||||||
// val timestamp: Int,
|
}
|
||||||
// val results: MutableSet<Pair<Int, InterfaceBlockEntity>> = mutableSetOf(),
|
|
||||||
//) {
|
data class StackLocateRequest(
|
||||||
// val totalCapacity: Int
|
val stack: ItemStack,
|
||||||
// get() = results.fold(0) { acc, (amount, _) -> acc + amount }
|
val amount: Int,
|
||||||
//
|
val timestamp: Long,
|
||||||
// fun isFinishable(currentTimestamp: Int): Boolean {
|
var results: MutableSet<Pair<Int, InterfaceBlockEntity>> = mutableSetOf()
|
||||||
// return totalCapacity > stack.count || currentTimestamp - timestamp >= TerminalBlockEntity.INSERTION_TIMEOUT
|
) {
|
||||||
// }
|
val totalResultAmount: Int
|
||||||
//}
|
get() = results.fold(0) { acc, (amount, _) -> acc + amount }
|
||||||
|
|
||||||
|
fun isFinishable(currentTimestamp: Long): Boolean {
|
||||||
|
return totalResultAmount >= amount || currentTimestamp - timestamp >= TerminalBlockEntity.LOCATE_REQUEST_TIMEOUT
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
data class PendingStackInsertion(
|
||||||
|
val bufferSlot: Int,
|
||||||
|
val stack: ItemStack,
|
||||||
|
val timestamp: Long,
|
||||||
|
val results: MutableSet<Pair<Int, InterfaceBlockEntity>> = mutableSetOf(),
|
||||||
|
) {
|
||||||
|
val totalCapacity: Int
|
||||||
|
get() = results.fold(0) { acc, (amount, _) -> acc + amount }
|
||||||
|
|
||||||
|
fun isFinishable(currentTimestamp: Long): Boolean {
|
||||||
|
return totalCapacity >= stack.count || currentTimestamp - timestamp >= TerminalBlockEntity.INSERTION_TIMEOUT
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,61 +1,61 @@
|
||||||
package net.shadowfacts.phycon.network.block.terminal
|
package net.shadowfacts.phycon.network.block.terminal
|
||||||
|
|
||||||
//import net.minecraft.screen.slot.Slot
|
import net.minecraft.screen.slot.Slot
|
||||||
//import net.minecraft.entity.player.PlayerEntity
|
import net.minecraft.entity.player.PlayerEntity
|
||||||
//import net.minecraft.inventory.Inventory
|
import net.minecraft.inventory.Inventory
|
||||||
//import net.minecraft.item.ItemStack
|
import net.minecraft.item.ItemStack
|
||||||
//
|
|
||||||
///**
|
/**
|
||||||
// * @author shadowfacts
|
* @author shadowfacts
|
||||||
// */
|
*/
|
||||||
//class TerminalFakeSlot(val terminal: TerminalBlockEntity, slot: Int, x: Int, y: Int): Slot(FakeInventory(terminal, slot), slot, x, y) {
|
class TerminalFakeSlot(val terminal: TerminalBlockEntity, slot: Int, x: Int, y: Int): Slot(FakeInventory(terminal, slot), slot, x, y) {
|
||||||
//
|
|
||||||
// override fun canInsert(stack: ItemStack): Boolean {
|
override fun canInsert(stack: ItemStack): Boolean {
|
||||||
// return false
|
return false
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// override fun setStack(stack: ItemStack) {
|
override fun setStack(stack: ItemStack) {
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// override fun canTakeItems(player: PlayerEntity): Boolean {
|
override fun canTakeItems(player: PlayerEntity): Boolean {
|
||||||
// return false
|
return false
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
//}
|
}
|
||||||
//
|
|
||||||
//class FakeInventory(val terminal: TerminalBlockEntity, val slot: Int): Inventory {
|
class FakeInventory(val terminal: TerminalBlockEntity, val slot: Int): Inventory {
|
||||||
// override fun getStack(_slot: Int): ItemStack {
|
override fun getStack(_slot: Int): ItemStack {
|
||||||
// if (slot >= terminal.cachedSortedNetItems.size) return ItemStack.EMPTY
|
if (slot >= terminal.cachedSortedNetItems.size) return ItemStack.EMPTY
|
||||||
// return terminal.cachedSortedNetItems[slot]
|
return terminal.cachedSortedNetItems[slot]
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// override fun markDirty() {
|
override fun markDirty() {
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// override fun clear() {
|
override fun clear() {
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// override fun setStack(p0: Int, p1: ItemStack?) {
|
override fun setStack(p0: Int, p1: ItemStack?) {
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// override fun removeStack(p0: Int): ItemStack {
|
override fun removeStack(p0: Int): ItemStack {
|
||||||
// return ItemStack.EMPTY
|
return ItemStack.EMPTY
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// override fun canPlayerUse(p0: PlayerEntity?): Boolean {
|
override fun canPlayerUse(p0: PlayerEntity?): Boolean {
|
||||||
// return false
|
return false
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// override fun size(): Int {
|
override fun size(): Int {
|
||||||
// return 1
|
return 1
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// override fun removeStack(p0: Int, p1: Int): ItemStack {
|
override fun removeStack(p0: Int, p1: Int): ItemStack {
|
||||||
// return ItemStack.EMPTY
|
return ItemStack.EMPTY
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// override fun isEmpty(): Boolean {
|
override fun isEmpty(): Boolean {
|
||||||
// return false
|
return false
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
//}
|
}
|
||||||
|
|
|
@ -1,58 +1,57 @@
|
||||||
package net.shadowfacts.phycon.network.block.terminal
|
package net.shadowfacts.phycon.network.block.terminal
|
||||||
//
|
|
||||||
//import com.mojang.blaze3d.platform.GlStateManager
|
import com.mojang.blaze3d.platform.GlStateManager
|
||||||
//import net.minecraft.client.gui.DrawableHelper
|
import net.minecraft.client.gui.DrawableHelper
|
||||||
//import net.minecraft.client.gui.screen.ingame.HandledScreen
|
import net.minecraft.client.gui.screen.ingame.HandledScreen
|
||||||
//import net.minecraft.client.util.math.MatrixStack
|
import net.minecraft.client.util.math.MatrixStack
|
||||||
//import net.minecraft.entity.player.PlayerInventory
|
import net.minecraft.entity.player.PlayerInventory
|
||||||
//import net.minecraft.screen.slot.Slot
|
import net.minecraft.screen.slot.Slot
|
||||||
//import net.minecraft.text.LiteralText
|
import net.minecraft.text.Text
|
||||||
//import net.minecraft.text.Text
|
import net.minecraft.util.Identifier
|
||||||
//import net.minecraft.util.Identifier
|
import net.shadowfacts.phycon.PhysicalConnectivity
|
||||||
//import net.shadowfacts.phycon.PhysicalConnectivity
|
|
||||||
//
|
/**
|
||||||
///**
|
* @author shadowfacts
|
||||||
// * @author shadowfacts
|
*/
|
||||||
// */
|
// todo: translate title
|
||||||
//// todo: translate title
|
class TerminalScreen(handler: TerminalScreenHandler, playerInv: PlayerInventory, title: Text): HandledScreen<TerminalScreenHandler>(handler, playerInv, title) {
|
||||||
//class TerminalScreen(handler: TerminalScreenHandler, playerInv: PlayerInventory, title: Text): HandledScreen<TerminalScreenHandler>(handler, playerInv, title) {
|
companion object {
|
||||||
// companion object {
|
val BACKGROUND = Identifier(PhysicalConnectivity.MODID, "textures/gui/terminal.png")
|
||||||
// val BACKGROUND = Identifier(PhysicalConnectivity.MODID, "textures/gui/terminal.png")
|
}
|
||||||
// }
|
|
||||||
//
|
init {
|
||||||
// init {
|
backgroundWidth = 252
|
||||||
// backgroundWidth = 252
|
backgroundHeight = 222
|
||||||
// backgroundHeight = 222
|
}
|
||||||
// }
|
|
||||||
//
|
override fun drawForeground(matrixStack: MatrixStack, mouseX: Int, mouseY: Int) {
|
||||||
// override fun drawForeground(matrixStack: MatrixStack, mouseX: Int, mouseY: Int) {
|
textRenderer.draw(matrixStack, title, 65f, 6f, 0x404040)
|
||||||
// textRenderer.draw(matrixStack, title, 65f, 6f, 0x404040)
|
textRenderer.draw(matrixStack, playerInventory.displayName, 65f, backgroundHeight - 94f, 0x404040)
|
||||||
// textRenderer.draw(matrixStack, playerInventory.displayName, 65f, backgroundHeight - 94f, 0x404040)
|
// todo: translate this
|
||||||
// // todo: translate this
|
textRenderer.draw(matrixStack, "Buffer", 7f, 6f, 0x404040)
|
||||||
// textRenderer.draw(matrixStack, "Buffer", 7f, 6f, 0x404040)
|
}
|
||||||
// }
|
|
||||||
//
|
override fun drawBackground(matrixStack: MatrixStack, delta: Float, mouseX: Int, mouseY: Int) {
|
||||||
// override fun drawBackground(matrixStack: MatrixStack, delta: Float, mouseX: Int, mouseY: Int) {
|
GlStateManager.color4f(1f, 1f, 1f, 1f)
|
||||||
// GlStateManager.color4f(1f, 1f, 1f, 1f)
|
client!!.textureManager.bindTexture(BACKGROUND)
|
||||||
// client!!.textureManager.bindTexture(BACKGROUND)
|
val x = (width - backgroundWidth) / 2
|
||||||
// val x = (width - backgroundWidth) / 2
|
val y = (height - backgroundHeight) / 2
|
||||||
// val y = (height - backgroundHeight) / 2
|
drawTexture(matrixStack, x, y, 0, 0, backgroundWidth, backgroundHeight)
|
||||||
// drawTexture(matrixStack, x, y, 0, 0, backgroundWidth, backgroundHeight)
|
}
|
||||||
// }
|
|
||||||
//
|
@ExperimentalUnsignedTypes
|
||||||
// @ExperimentalUnsignedTypes
|
fun drawSlotUnderlay(matrixStack: MatrixStack, slot: Slot) {
|
||||||
// fun drawSlotUnderlay(matrixStack: MatrixStack, slot: Slot) {
|
if (!handler.isBufferSlot(slot.id)) {
|
||||||
// if (!handler.isBufferSlot(slot.id)) {
|
return
|
||||||
// return
|
}
|
||||||
// }
|
|
||||||
//
|
val mode = handler.terminal.internalBuffer.getMode(slot.id - handler.bufferSlotsStart)
|
||||||
// val mode = handler.terminal.internalBuffer.getMode(slot.id - handler.bufferSlotsStart)
|
val color: UInt = when (mode) {
|
||||||
// val color: UInt = when (mode) {
|
TerminalBufferInventory.Mode.TO_NETWORK -> 0xFFFF0000u
|
||||||
// TerminalBufferInventory.Mode.TO_NETWORK -> 0xFFFF0000u
|
TerminalBufferInventory.Mode.FROM_NETWORK -> 0xFF00FF00u
|
||||||
// TerminalBufferInventory.Mode.FROM_NETWORK -> 0xFF00FF00u
|
else -> return
|
||||||
// else -> return
|
}
|
||||||
// }
|
DrawableHelper.fill(matrixStack, slot.x, slot.y, slot.x + 16, slot.y + 16, color.toInt())
|
||||||
// DrawableHelper.fill(matrixStack, slot.x, slot.y, slot.x + 16, slot.y + 16, color.toInt())
|
}
|
||||||
// }
|
|
||||||
//
|
}
|
||||||
//}
|
|
||||||
|
|
|
@ -1,161 +1,161 @@
|
||||||
package net.shadowfacts.phycon.network.block.terminal
|
package net.shadowfacts.phycon.network.block.terminal
|
||||||
//
|
|
||||||
//import net.minecraft.screen.slot.Slot
|
import net.minecraft.screen.slot.Slot
|
||||||
//import net.minecraft.screen.slot.SlotActionType
|
import net.minecraft.screen.slot.SlotActionType
|
||||||
//import net.minecraft.entity.player.PlayerEntity
|
import net.minecraft.entity.player.PlayerEntity
|
||||||
//import net.minecraft.entity.player.PlayerInventory
|
import net.minecraft.entity.player.PlayerInventory
|
||||||
//import net.minecraft.item.ItemStack
|
import net.minecraft.item.ItemStack
|
||||||
//import net.minecraft.network.PacketByteBuf
|
import net.minecraft.network.PacketByteBuf
|
||||||
//import net.minecraft.screen.ScreenHandler
|
import net.minecraft.screen.ScreenHandler
|
||||||
//import net.minecraft.util.Identifier
|
import net.minecraft.util.Identifier
|
||||||
//import net.shadowfacts.phycon.PhysicalConnectivity
|
import net.shadowfacts.phycon.PhysicalConnectivity
|
||||||
//import net.shadowfacts.phycon.init.PhyBlocks
|
import net.shadowfacts.phycon.init.PhyBlocks
|
||||||
//import net.shadowfacts.phycon.init.PhyScreens
|
import net.shadowfacts.phycon.init.PhyScreens
|
||||||
//import kotlin.math.ceil
|
import kotlin.math.ceil
|
||||||
//import kotlin.math.min
|
import kotlin.math.min
|
||||||
//
|
|
||||||
///**
|
/**
|
||||||
// * @author shadowfacts
|
* @author shadowfacts
|
||||||
// */
|
*/
|
||||||
//class TerminalScreenHandler(syncId: Int, playerInv: PlayerInventory, val terminal: TerminalBlockEntity): ScreenHandler(PhyScreens.TERMINAL_SCREEN_HANDLER, syncId) {
|
class TerminalScreenHandler(syncId: Int, playerInv: PlayerInventory, val terminal: TerminalBlockEntity): ScreenHandler(PhyScreens.TERMINAL_SCREEN_HANDLER, syncId) {
|
||||||
//
|
|
||||||
// companion object {
|
companion object {
|
||||||
// val ID = Identifier(PhysicalConnectivity.MODID, "terminal")
|
val ID = Identifier(PhysicalConnectivity.MODID, "terminal")
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// constructor(syncId: Int, playerInv: PlayerInventory, buf: PacketByteBuf):
|
constructor(syncId: Int, playerInv: PlayerInventory, buf: PacketByteBuf):
|
||||||
// this(syncId, playerInv, PhyBlocks.TERMINAL.getBlockEntity(playerInv.player.world, buf.readBlockPos())!!)
|
this(syncId, playerInv, PhyBlocks.TERMINAL.getBlockEntity(playerInv.player.world, buf.readBlockPos())!!)
|
||||||
//
|
|
||||||
// init {
|
init {
|
||||||
// // network
|
// network
|
||||||
// for (y in 0 until 6) {
|
for (y in 0 until 6) {
|
||||||
// for (x in 0 until 9) {
|
for (x in 0 until 9) {
|
||||||
// addSlot(TerminalFakeSlot(terminal, y * 9 + x, 66 + x * 18, 18 + y * 18))
|
addSlot(TerminalFakeSlot(terminal, y * 9 + x, 66 + x * 18, 18 + y * 18))
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// // internal buffer
|
// internal buffer
|
||||||
// for (y in 0 until 6) {
|
for (y in 0 until 6) {
|
||||||
// for (x in 0 until 3) {
|
for (x in 0 until 3) {
|
||||||
// addSlot(Slot(terminal.internalBuffer, y * 3 + x, 8 + x * 18, 18 + y * 18))
|
addSlot(Slot(terminal.internalBuffer, y * 3 + x, 8 + x * 18, 18 + y * 18))
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// // player inv
|
// player inv
|
||||||
// for (y in 0 until 3) {
|
for (y in 0 until 3) {
|
||||||
// for (x in 0 until 9) {
|
for (x in 0 until 9) {
|
||||||
// addSlot(Slot(playerInv, x + y * 9 + 9, 66 + x * 18, 140 + y * 18))
|
addSlot(Slot(playerInv, x + y * 9 + 9, 66 + x * 18, 140 + y * 18))
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
// // hotbar
|
// hotbar
|
||||||
// for (x in 0 until 9) {
|
for (x in 0 until 9) {
|
||||||
// addSlot(Slot(playerInv, x, 66 + x * 18, 198))
|
addSlot(Slot(playerInv, x, 66 + x * 18, 198))
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// override fun canUse(player: PlayerEntity): Boolean {
|
override fun canUse(player: PlayerEntity): Boolean {
|
||||||
// return true
|
return true
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// override fun close(player: PlayerEntity) {
|
override fun close(player: PlayerEntity) {
|
||||||
// super.close(player)
|
super.close(player)
|
||||||
//
|
|
||||||
// terminal.removeObserver()
|
terminal.removeObserver()
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// override fun onSlotClick(slotId: Int, clickData: Int, actionType: SlotActionType, player: PlayerEntity): ItemStack {
|
override fun onSlotClick(slotId: Int, clickData: Int, actionType: SlotActionType, player: PlayerEntity): ItemStack {
|
||||||
// if (isNetworkSlot(slotId)) {
|
if (isNetworkSlot(slotId)) {
|
||||||
// // the slot clicked was one of the network stacks
|
// the slot clicked was one of the network stacks
|
||||||
// if (actionType == SlotActionType.QUICK_MOVE) {
|
if (actionType == SlotActionType.QUICK_MOVE) {
|
||||||
// val stack = slots[slotId].stack
|
val stack = slots[slotId].stack
|
||||||
// if (!stack.isEmpty && !player.world.isClient) {
|
if (!stack.isEmpty && !player.world.isClient) {
|
||||||
// terminal.requestItem(stack, min(stack.count, stack.maxCount))
|
terminal.requestItem(stack, min(stack.count, stack.maxCount))
|
||||||
// }
|
}
|
||||||
// } else if (actionType == SlotActionType.PICKUP && clickData == 1) {
|
} else if (actionType == SlotActionType.PICKUP && clickData == 1) {
|
||||||
// if (clickData == 1) {
|
if (clickData == 1) {
|
||||||
// // right click, request half stack
|
// right click, request half stack
|
||||||
// val stack = slots[slotId].stack
|
val stack = slots[slotId].stack
|
||||||
// if (!stack.isEmpty && !player.world.isClient) {
|
if (!stack.isEmpty && !player.world.isClient) {
|
||||||
// terminal.requestItem(stack, ceil(min(stack.count, stack.maxCount) / 2f).toInt())
|
terminal.requestItem(stack, ceil(min(stack.count, stack.maxCount) / 2f).toInt())
|
||||||
// }
|
}
|
||||||
// } else {
|
} else {
|
||||||
// // todo: left click, show amount dialog
|
// todo: left click, show amount dialog
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
// return ItemStack.EMPTY
|
return ItemStack.EMPTY
|
||||||
// } else if (isBufferSlot(slotId)) {
|
} else if (isBufferSlot(slotId)) {
|
||||||
// // internal buffer
|
// internal buffer
|
||||||
// // todo: why does this think it's quick_craft sometimes?
|
// todo: why does this think it's quick_craft sometimes?
|
||||||
// if ((actionType == SlotActionType.PICKUP || actionType == SlotActionType.QUICK_CRAFT) && !player.inventory.cursorStack.isEmpty) {
|
if ((actionType == SlotActionType.PICKUP || actionType == SlotActionType.QUICK_CRAFT) && !player.inventory.cursorStack.isEmpty) {
|
||||||
// // placing cursor stack into buffer
|
// placing cursor stack into buffer
|
||||||
// val bufferSlot = slotId - bufferSlotsStart // subtract 54 to convert the handler slot ID to a valid buffer index
|
val bufferSlot = slotId - bufferSlotsStart // subtract 54 to convert the handler slot ID to a valid buffer index
|
||||||
// terminal.internalBuffer.markSlot(bufferSlot, TerminalBufferInventory.Mode.TO_NETWORK)
|
terminal.internalBuffer.markSlot(bufferSlot, TerminalBufferInventory.Mode.TO_NETWORK)
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
// return super.onSlotClick(slotId, clickData, actionType, player)
|
return super.onSlotClick(slotId, clickData, actionType, player)
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// override fun transferSlot(player: PlayerEntity, slotId: Int): ItemStack {
|
override fun transferSlot(player: PlayerEntity, slotId: Int): ItemStack {
|
||||||
// if (isNetworkSlot(slotId)) {
|
if (isNetworkSlot(slotId)) {
|
||||||
// return ItemStack.EMPTY;
|
return ItemStack.EMPTY;
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// val slot = slots[slotId]
|
val slot = slots[slotId]
|
||||||
// if (!slot.hasStack()) {
|
if (!slot.hasStack()) {
|
||||||
// return ItemStack.EMPTY
|
return ItemStack.EMPTY
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// val result = slot.stack.copy()
|
val result = slot.stack.copy()
|
||||||
//
|
|
||||||
// if (isBufferSlot(slotId)) {
|
if (isBufferSlot(slotId)) {
|
||||||
// if (!insertItem(slot.stack, playerSlotsStart, playerSlotsEnd, false)) {
|
if (!insertItem(slot.stack, playerSlotsStart, playerSlotsEnd, false)) {
|
||||||
// return ItemStack.EMPTY
|
return ItemStack.EMPTY
|
||||||
// }
|
}
|
||||||
// if (slot.stack.isEmpty) {
|
if (slot.stack.isEmpty) {
|
||||||
// terminal.internalBuffer.markSlot(slotId - bufferSlotsStart, TerminalBufferInventory.Mode.UNASSIGNED)
|
terminal.internalBuffer.markSlot(slotId - bufferSlotsStart, TerminalBufferInventory.Mode.UNASSIGNED)
|
||||||
// }
|
}
|
||||||
// } else if (isPlayerSlot(slotId)) {
|
} else if (isPlayerSlot(slotId)) {
|
||||||
// val slotsInsertedInto = tryInsertItem(slot.stack, bufferSlotsStart until playerSlotsStart) { terminal.internalBuffer.getMode(it - bufferSlotsStart) != TerminalBufferInventory.Mode.FROM_NETWORK }
|
val slotsInsertedInto = tryInsertItem(slot.stack, bufferSlotsStart until playerSlotsStart) { terminal.internalBuffer.getMode(it - bufferSlotsStart) != TerminalBufferInventory.Mode.FROM_NETWORK }
|
||||||
// slotsInsertedInto.forEach { terminal.internalBuffer.markSlot(it - bufferSlotsStart, TerminalBufferInventory.Mode.TO_NETWORK) }
|
slotsInsertedInto.forEach { terminal.internalBuffer.markSlot(it - bufferSlotsStart, TerminalBufferInventory.Mode.TO_NETWORK) }
|
||||||
// if (slot.stack.isEmpty) {
|
if (slot.stack.isEmpty) {
|
||||||
// return ItemStack.EMPTY
|
return ItemStack.EMPTY
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// return result
|
return result
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// private fun tryInsertItem(stack: ItemStack, slots: IntRange, slotPredicate: (Int) -> Boolean): Collection<Int> {
|
private fun tryInsertItem(stack: ItemStack, slots: IntRange, slotPredicate: (Int) -> Boolean): Collection<Int> {
|
||||||
// val slotsInsertedInto = mutableListOf<Int>()
|
val slotsInsertedInto = mutableListOf<Int>()
|
||||||
// for (index in slots) {
|
for (index in slots) {
|
||||||
// if (stack.isEmpty) break
|
if (stack.isEmpty) break
|
||||||
// if (!slotPredicate(index)) continue
|
if (!slotPredicate(index)) continue
|
||||||
//
|
|
||||||
// val slot = this.slots[index]
|
val slot = this.slots[index]
|
||||||
// val slotStack = slot.stack
|
val slotStack = slot.stack
|
||||||
// if (slotStack.isEmpty) {
|
if (slotStack.isEmpty) {
|
||||||
// slot.stack = stack.copy()
|
slot.stack = stack.copy()
|
||||||
// stack.count = 0
|
stack.count = 0
|
||||||
//
|
|
||||||
// slot.markDirty()
|
slot.markDirty()
|
||||||
// slotsInsertedInto.add(index)
|
slotsInsertedInto.add(index)
|
||||||
// } else if (canStacksCombine(slotStack, stack) && slotStack.count < slotStack.maxCount) {
|
} else if (canStacksCombine(slotStack, stack) && slotStack.count < slotStack.maxCount) {
|
||||||
// val maxToMove = slotStack.maxCount - slotStack.count
|
val maxToMove = slotStack.maxCount - slotStack.count
|
||||||
// val toMove = min(maxToMove, stack.count)
|
val toMove = min(maxToMove, stack.count)
|
||||||
// slotStack.increment(toMove)
|
slotStack.increment(toMove)
|
||||||
// stack.decrement(toMove)
|
stack.decrement(toMove)
|
||||||
//
|
|
||||||
// slot.markDirty()
|
slot.markDirty()
|
||||||
// slotsInsertedInto.add(index)
|
slotsInsertedInto.add(index)
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
// return slotsInsertedInto
|
return slotsInsertedInto
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// val bufferSlotsStart = 54
|
val bufferSlotsStart = 54
|
||||||
// val playerSlotsStart = 72
|
val playerSlotsStart = 72
|
||||||
// val playerSlotsEnd = 72 + 36
|
val playerSlotsEnd = 72 + 36
|
||||||
// fun isNetworkSlot(id: Int) = id in 0 until bufferSlotsStart
|
fun isNetworkSlot(id: Int) = id in 0 until bufferSlotsStart
|
||||||
// fun isBufferSlot(id: Int) = id in bufferSlotsStart until playerSlotsStart
|
fun isBufferSlot(id: Int) = id in bufferSlotsStart until playerSlotsStart
|
||||||
// fun isPlayerSlot(id: Int) = id >= playerSlotsStart
|
fun isPlayerSlot(id: Int) = id >= playerSlotsStart
|
||||||
//}
|
}
|
||||||
|
|
|
@ -2,10 +2,10 @@ package net.shadowfacts.phycon.network.packet
|
||||||
|
|
||||||
import net.minecraft.item.ItemStack
|
import net.minecraft.item.ItemStack
|
||||||
import net.shadowfacts.phycon.api.util.IPAddress
|
import net.shadowfacts.phycon.api.util.IPAddress
|
||||||
//import net.shadowfacts.phycon.network.block.netinterface.InterfaceBlockEntity
|
import net.shadowfacts.phycon.network.block.netinterface.InterfaceBlockEntity
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author shadowfacts
|
* @author shadowfacts
|
||||||
*/
|
*/
|
||||||
//class CapacityPacket(val stack: ItemStack, val capacity: Int, val receivingInterface: InterfaceBlockEntity, source: IPAddress, destination: IPAddress): BasePacket(source, destination) {
|
class CapacityPacket(val stack: ItemStack, val capacity: Int, val receivingInterface: InterfaceBlockEntity, source: IPAddress, destination: IPAddress): BasePacket(source, destination) {
|
||||||
//}
|
}
|
|
@ -2,17 +2,17 @@ package net.shadowfacts.phycon.network.packet
|
||||||
|
|
||||||
import net.minecraft.item.ItemStack
|
import net.minecraft.item.ItemStack
|
||||||
import net.shadowfacts.phycon.api.util.IPAddress
|
import net.shadowfacts.phycon.api.util.IPAddress
|
||||||
//import net.shadowfacts.phycon.network.block.netinterface.InterfaceBlockEntity
|
import net.shadowfacts.phycon.network.block.netinterface.InterfaceBlockEntity
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author shadowfacts
|
* @author shadowfacts
|
||||||
*/
|
*/
|
||||||
// todo: better name with LocateStackPacket
|
// todo: better name with LocateStackPacket
|
||||||
//class StackLocationPacket(
|
class StackLocationPacket(
|
||||||
// val stack: ItemStack,
|
val stack: ItemStack,
|
||||||
// val amount: Int,
|
val amount: Int,
|
||||||
// val sourceInterface: InterfaceBlockEntity,
|
val sourceInterface: InterfaceBlockEntity,
|
||||||
// source: IPAddress,
|
source: IPAddress,
|
||||||
// destination: IPAddress
|
destination: IPAddress
|
||||||
//): BasePacket(source, destination) {
|
): BasePacket(source, destination) {
|
||||||
//}
|
}
|
||||||
|
|
Loading…
Reference in New Issue