Fix redstone/filter modes not syncing to server from advanced hopper GUI

This commit is contained in:
Shadowfacts 2017-07-02 12:39:24 -04:00
parent ce9bcd9556
commit 34e9671eb0
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
5 changed files with 172 additions and 50 deletions

View File

@ -9,6 +9,7 @@ import net.minecraftforge.fml.common.Mod
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent import net.minecraftforge.fml.common.event.FMLPreInitializationEvent
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import net.minecraftforge.fml.common.network.NetworkRegistry import net.minecraftforge.fml.common.network.NetworkRegistry
import net.minecraftforge.fml.common.network.simpleimpl.SimpleNetworkWrapper
import net.minecraftforge.fml.common.registry.GameRegistry import net.minecraftforge.fml.common.registry.GameRegistry
import net.minecraftforge.fml.relauncher.Side import net.minecraftforge.fml.relauncher.Side
import net.minecraftforge.fml.relauncher.SideOnly import net.minecraftforge.fml.relauncher.SideOnly
@ -20,6 +21,8 @@ import net.shadowfacts.extrahoppers.block.inverted.TileEntityInvertedHopper
import net.shadowfacts.extrahoppers.block.wooden.TileEntityWoodenHopper import net.shadowfacts.extrahoppers.block.wooden.TileEntityWoodenHopper
import net.shadowfacts.extrahoppers.block.wooden_fluid.TileEntityWoodenFluidHopper import net.shadowfacts.extrahoppers.block.wooden_fluid.TileEntityWoodenFluidHopper
import net.shadowfacts.extrahoppers.gui.GUIHandler import net.shadowfacts.extrahoppers.gui.GUIHandler
import net.shadowfacts.extrahoppers.network.PacketSetHopperFilterMode
import net.shadowfacts.extrahoppers.network.PacketSetHopperRedstoneMode
/** /**
* @author shadowfacts * @author shadowfacts
@ -27,6 +30,8 @@ import net.shadowfacts.extrahoppers.gui.GUIHandler
@Mod(modid = MOD_ID, name = NAME, version = VERSION, dependencies = "required-after:shadowmc;", modLanguageAdapter = "net.shadowfacts.forgelin.KotlinAdapter", guiFactory = "net.shadowfacts.extrahoppers.gui.EHGUIFactory") @Mod(modid = MOD_ID, name = NAME, version = VERSION, dependencies = "required-after:shadowmc;", modLanguageAdapter = "net.shadowfacts.forgelin.KotlinAdapter", guiFactory = "net.shadowfacts.extrahoppers.gui.EHGUIFactory")
object ExtraHoppers { object ExtraHoppers {
lateinit var network: SimpleNetworkWrapper private set
// Content // Content
val blocks = ModBlocks val blocks = ModBlocks
@ -36,6 +41,9 @@ object ExtraHoppers {
EHConfig.save() EHConfig.save()
NetworkRegistry.INSTANCE.registerGuiHandler(ExtraHoppers, GUIHandler) NetworkRegistry.INSTANCE.registerGuiHandler(ExtraHoppers, GUIHandler)
network = NetworkRegistry.INSTANCE.newSimpleChannel(MOD_ID)
network.registerMessage(PacketSetHopperRedstoneMode.Handler, PacketSetHopperRedstoneMode::class.java, 0, Side.SERVER)
network.registerMessage(PacketSetHopperFilterMode.Handler, PacketSetHopperFilterMode::class.java, 1, Side.SERVER)
} }
@Mod.EventHandler @Mod.EventHandler

View File

@ -3,8 +3,12 @@ package net.shadowfacts.extrahoppers.block.inventory
import net.minecraft.inventory.Container import net.minecraft.inventory.Container
import net.minecraft.inventory.Slot import net.minecraft.inventory.Slot
import net.minecraft.util.ResourceLocation import net.minecraft.util.ResourceLocation
import net.shadowfacts.extrahoppers.ExtraHoppers
import net.shadowfacts.extrahoppers.MOD_ID import net.shadowfacts.extrahoppers.MOD_ID
import net.shadowfacts.extrahoppers.block.advanced.TileEntityAdvancedHopper
import net.shadowfacts.extrahoppers.gui.element.UIFilterButton import net.shadowfacts.extrahoppers.gui.element.UIFilterButton
import net.shadowfacts.extrahoppers.network.PacketSetHopperFilterMode
import net.shadowfacts.extrahoppers.network.PacketSetHopperRedstoneMode
import net.shadowfacts.extrahoppers.util.filter.FilterMode import net.shadowfacts.extrahoppers.util.filter.FilterMode
import net.shadowfacts.extrahoppers.util.filter.ItemFilter import net.shadowfacts.extrahoppers.util.filter.ItemFilter
import net.shadowfacts.shadowmc.ui.element.UIRect import net.shadowfacts.shadowmc.ui.element.UIRect
@ -33,7 +37,7 @@ class GUIInventoryHopper(container: Container, tile: TileEntityInventoryHopper):
add(UIFixedView(176 + 83 * 2, 133, "root").apply { add(UIFixedView(176 + 83 * 2, 133, "root").apply {
add(UIImage(BG, 176, 133, "bg")) add(UIImage(BG, 176, 133, "bg"))
if (tile.advanced) { if (tile.advanced && tile is TileEntityAdvancedHopper) {
add(UIFixedView(176, 60, "top").apply { add(UIFixedView(176, 60, "top").apply {
add(UIFilterButton({ add(UIFilterButton({
// TODO // TODO
@ -41,7 +45,7 @@ class GUIInventoryHopper(container: Container, tile: TileEntityInventoryHopper):
add(UIButtonRedstoneMode(tile.mode, Consumer { add(UIButtonRedstoneMode(tile.mode, Consumer {
tile.mode = it tile.mode = it
tile.sync() ExtraHoppers.network.sendToServer(PacketSetHopperRedstoneMode(tile))
}, "mode")) }, "mode"))
}) })
@ -60,7 +64,7 @@ class GUIInventoryHopper(container: Container, tile: TileEntityInventoryHopper):
add(UIFixedView(83, 34, "filter-bottom").apply { add(UIFixedView(83, 34, "filter-bottom").apply {
add(UIButtonEnum<FilterMode>(tile.filterMode, Function(FilterMode::localize), Consumer { add(UIButtonEnum<FilterMode>(tile.filterMode, Function(FilterMode::localize), Consumer {
tile.filterMode = it.value tile.filterMode = it.value
tile.sync() ExtraHoppers.network.sendToServer(PacketSetHopperFilterMode(tile))
}, "filter-mode")) }, "filter-mode"))
}) })
}) })

View File

@ -72,52 +72,6 @@ abstract class TileEntityInventoryHopper(inverted: Boolean, advanced: Boolean, s
} }
} }
private fun push(): Boolean {
if (!inventory.isEmpty) {
val facing = getHopperFacing()
val tile = world.getTileEntity(pos.offset(facing))
if (tile is ISidedInventory) {
val slots = tile.getSlotsForFace(facing.opposite)
for (i in slots) {
for (slot in 0.until(inventory.slots)) {
if (inventory[slot].isEmpty) continue
val remainder = tile.insert(inventory.extractItem(slot, 1, true), i)
if (remainder.isEmpty) {
inventory.extractItem(slot, 1, false)
return true
}
}
}
} else if (tile is IInventory) {
for (i in 0.until(tile.sizeInventory)) {
for (slot in 0.until(inventory.slots)) {
if (inventory[slot].isEmpty) continue
val remainder = tile.insert(inventory.extractItem(slot, 1, true), i)
if (remainder.isEmpty) {
inventory.extractItem(slot, 1, false)
return true
}
}
}
} else if (tile != null && tile.hasCapability(ITEM_HANDLER_CAPABILITY, facing.opposite)) {
val handler = tile.getCapability(ITEM_HANDLER_CAPABILITY, facing.opposite)!!
for (i in 0.until(handler.slots)) {
for (slot in 0.until(inventory.slots)) {
if (inventory[slot].isEmpty) continue
val remainder = handler.insertItem(i, inventory.extractItem(slot, 1, true), false)
if (remainder.isEmpty) {
inventory.extractItem(slot, 1, false)
return true
}
}
}
}
}
return false
}
private fun pull(): Boolean { private fun pull(): Boolean {
val items = world.getEntitiesWithinAABB(EntityItem::class.java, box) val items = world.getEntitiesWithinAABB(EntityItem::class.java, box)
for (item in items) { for (item in items) {
@ -175,7 +129,7 @@ abstract class TileEntityInventoryHopper(inverted: Boolean, advanced: Boolean, s
for (i in 0.until(handler.slots)) { for (i in 0.until(handler.slots)) {
for (slot in 0.until(inventory.slots)) { for (slot in 0.until(inventory.slots)) {
val remainder = inventory.insertItem(slot, handler.extractItem(i, 1, true), false) val remainder = inventory.insertItem(slot, handler.extractItem(i, 1, true), false)
if (remainder.isEmpty()) { if (remainder.isEmpty) {
handler.extractItem(i, 1, false) handler.extractItem(i, 1, false)
return true return true
} }
@ -186,6 +140,52 @@ abstract class TileEntityInventoryHopper(inverted: Boolean, advanced: Boolean, s
return false return false
} }
private fun push(): Boolean {
if (!inventory.isEmpty) {
val facing = getHopperFacing()
val tile = world.getTileEntity(pos.offset(facing))
if (tile is ISidedInventory) {
val slots = tile.getSlotsForFace(facing.opposite)
for (i in slots) {
for (slot in 0.until(inventory.slots)) {
if (inventory[slot].isEmpty) continue
val remainder = tile.insert(inventory.extractItem(slot, 1, true), i)
if (remainder.isEmpty) {
inventory.extractItem(slot, 1, false)
return true
}
}
}
} else if (tile is IInventory) {
for (i in 0.until(tile.sizeInventory)) {
for (slot in 0.until(inventory.slots)) {
if (inventory[slot].isEmpty) continue
val remainder = tile.insert(inventory.extractItem(slot, 1, true), i)
if (remainder.isEmpty) {
inventory.extractItem(slot, 1, false)
return true
}
}
}
} else if (tile != null && tile.hasCapability(ITEM_HANDLER_CAPABILITY, facing.opposite)) {
val handler = tile.getCapability(ITEM_HANDLER_CAPABILITY, facing.opposite)!!
for (i in 0.until(handler.slots)) {
for (slot in 0.until(inventory.slots)) {
if (inventory[slot].isEmpty) continue
val remainder = handler.insertItem(i, inventory.extractItem(slot, 1, true), false)
if (remainder.isEmpty) {
inventory.extractItem(slot, 1, false)
return true
}
}
}
}
}
return false
}
override fun hasCapability(capability: Capability<*>, facing: EnumFacing?): Boolean { override fun hasCapability(capability: Capability<*>, facing: EnumFacing?): Boolean {
if (capability == ITEM_HANDLER_CAPABILITY) { if (capability == ITEM_HANDLER_CAPABILITY) {
val side = if (inverted) EnumFacing.DOWN else EnumFacing.UP val side = if (inverted) EnumFacing.DOWN else EnumFacing.UP

View File

@ -0,0 +1,55 @@
package net.shadowfacts.extrahoppers.network
import io.netty.buffer.ByteBuf
import net.minecraft.util.math.BlockPos
import net.minecraftforge.fml.common.FMLCommonHandler
import net.minecraftforge.fml.common.network.simpleimpl.IMessage
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext
import net.shadowfacts.extrahoppers.block.advanced.TileEntityAdvancedHopper
import net.shadowfacts.extrahoppers.util.filter.FilterMode
/**
* @author shadowfacts
*/
class PacketSetHopperFilterMode(): IMessage {
var dim: Int = 0
lateinit var pos: BlockPos
lateinit var mode: FilterMode
constructor(dim: Int, pos: BlockPos, mode: FilterMode): this() {
this.dim = dim
this.pos = pos
this.mode = mode
}
constructor(tile: TileEntityAdvancedHopper): this(tile.world.provider.dimension, tile.pos, tile.filterMode)
override fun toBytes(buf: ByteBuf) {
buf.writeInt(dim)
buf.writeLong(pos.toLong())
buf.writeInt(mode.ordinal)
}
override fun fromBytes(buf: ByteBuf) {
dim = buf.readInt()
pos = BlockPos.fromLong(buf.readLong())
mode = FilterMode.values()[buf.readInt()]
}
object Handler: IMessageHandler<PacketSetHopperFilterMode, IMessage> {
override fun onMessage(message: PacketSetHopperFilterMode, ctx: MessageContext): IMessage? {
val world = FMLCommonHandler.instance().minecraftServerInstance.getWorld(message.dim)
val tile = world.getTileEntity(message.pos)
if (tile is TileEntityAdvancedHopper) {
tile.filterMode = message.mode
tile.markDirty()
}
return null
}
}
}

View File

@ -0,0 +1,55 @@
package net.shadowfacts.extrahoppers.network
import io.netty.buffer.ByteBuf
import net.minecraft.util.math.BlockPos
import net.minecraftforge.fml.common.FMLCommonHandler
import net.minecraftforge.fml.common.network.simpleimpl.IMessage
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext
import net.shadowfacts.extrahoppers.block.advanced.TileEntityAdvancedHopper
import net.shadowfacts.shadowmc.util.RedstoneMode
/**
* @author shadowfacts
*/
class PacketSetHopperRedstoneMode(): IMessage {
var dim: Int = 0
lateinit var pos: BlockPos
lateinit var mode: RedstoneMode
constructor(dim: Int, pos: BlockPos, mode: RedstoneMode): this() {
this.dim = dim
this.pos = pos
this.mode = mode
}
constructor(tile: TileEntityAdvancedHopper): this(tile.world.provider.dimension, tile.pos, tile.mode)
override fun toBytes(buf: ByteBuf) {
buf.writeInt(dim)
buf.writeLong(pos.toLong())
buf.writeInt(mode.ordinal)
}
override fun fromBytes(buf: ByteBuf) {
dim = buf.readInt()
pos = BlockPos.fromLong(buf.readLong())
mode = RedstoneMode.values()[buf.readInt()]
}
object Handler: IMessageHandler<PacketSetHopperRedstoneMode, IMessage> {
override fun onMessage(message: PacketSetHopperRedstoneMode, ctx: MessageContext): IMessage? {
val world = FMLCommonHandler.instance().minecraftServerInstance.getWorld(message.dim)
val tile = world.getTileEntity(message.pos)
if (tile is TileEntityAdvancedHopper) {
tile.mode = message.mode
tile.markDirty()
}
return null
}
}
}