Files
BetterP2P/src/main/java/com/projecturanus/betterp2p/item/ItemAdvancedMemoryCard.kt
T
firenoo f5a23d1a3b Completely overhaul the Advanced Memory Card (#29)
* Move mode change button to the side.

- Mode change now on the side w/ resize button
- Some code cleanup
- New description (en only for now), texture.

* Update ae2 to beta-204

* Add OC/Pneumaticraft support

* Create a proxy for client/server

Mostly for convenience of getting icons and other GUI elements.

* Large refactor to make way for "refresh" functionality

Features:
- New "refresh" capability (no button available yet) so the
client can have an updated GUI.
- The player can now control which types to be queried/displayed.

A great number of changes were made to allow this to happen to the mod
internally, as started from the last commit.

- P2PCache has been revisited. Instead of being a one-time instantiation,
the cache can now be queried from the client many times.

- Network has changed. Instead of sending an ItemStack (as previously), we
keep that info locally and send a byte only instead, so that should save
some network traffic.

- New packet for sending the refresh signal has been made. Other packets
send a "type" discriminator to make way for the typing feature.

Because I was lazy and didn't separate the commits, here are some other
changes made:
- Cleaned up a bit of the code to make it easier to read. This includes
code I've written before...
- Some name refactors. Some of the names were (mine) were quite frankly,
bad.

Signed-off-by: Firenoo <[email protected]>

* More cleanup + texture for type change

Some more code cleanup (spacing and labels)

* Disable ability to use copy outputs when output selected

Because it doesn't work, and has strange behavior, this is being disabled
for now (possibly forever).

* Remove unused method + remove unused imports

* Add unbinding mode

+ tooltip explanation

* Add type to filter options

* Add enUS localization

* Add refresh button, and prevent excessive packet spam.

Packets are sent only once every 250 ms to avoid DDOSing someone.
Accomplishes this by adding a new scheduled executor.

* Add the ability to change P2P types from the GUI

* tooltip was actually lying, now it's lying less

* Fix CME from resizing GUI, and add patterns to drops when changing P2P interface type

CME caused by iterating over the button list. Clicking on resize button forces
the GUI to reset, so the button list changes and causes the CME.

P2P interfaces now drop their items if changing their type from the GUI.

* Make the refresh icon look a little less like MSPaint

Also minor change to tooltip when changing types.

---------

Signed-off-by: Firenoo <[email protected]>
2023-05-15 17:29:14 +02:00

132 lines
5.7 KiB
Kotlin

package com.projecturanus.betterp2p.item
import appeng.api.networking.IGridHost
import appeng.core.CreativeTab
import appeng.parts.p2p.PartP2PTunnel
import com.projecturanus.betterp2p.MODID
import com.projecturanus.betterp2p.capability.MemoryInfo
import com.projecturanus.betterp2p.capability.TUNNEL_ANY
import com.projecturanus.betterp2p.client.ClientCache
import com.projecturanus.betterp2p.client.gui.widget.GuiScale
import com.projecturanus.betterp2p.network.ModNetwork
import com.projecturanus.betterp2p.network.NONE_SELECTED
import com.projecturanus.betterp2p.network.S2CListP2P
import com.projecturanus.betterp2p.network.hashP2P
import com.projecturanus.betterp2p.util.getPart
import com.projecturanus.betterp2p.util.p2p.P2PCache
import com.projecturanus.betterp2p.util.p2p.P2PStatus
import com.projecturanus.betterp2p.util.p2p.getTypeIndex
import cpw.mods.fml.relauncher.Side
import cpw.mods.fml.relauncher.SideOnly
import net.minecraft.client.renderer.texture.IIconRegister
import net.minecraft.entity.Entity
import net.minecraft.entity.player.EntityPlayer
import net.minecraft.entity.player.EntityPlayerMP
import net.minecraft.item.Item
import net.minecraft.item.ItemStack
import net.minecraft.nbt.NBTTagCompound
import net.minecraft.util.StatCollector
import net.minecraft.world.World
import net.minecraftforge.common.util.Constants
import net.minecraftforge.common.util.ForgeDirection
import java.util.*
object ItemAdvancedMemoryCard : Item() {
init {
maxStackSize = 1
unlocalizedName = "advanced_memory_card"
creativeTab = CreativeTab.instance
}
override fun onUpdate(stack: ItemStack, worldIn: World, entityIn: Entity, itemSlot: Int, isSelected: Boolean) {
super.onUpdate(stack, worldIn, entityIn, itemSlot, isSelected)
}
private fun sendStatus(status: P2PStatus, info: MemoryInfo, player: EntityPlayerMP) {
P2PCache.statusMap[player.uniqueID] = status
ModNetwork.channel.sendTo(S2CListP2P(status.refresh(info.type), info), player)
}
@SideOnly(Side.CLIENT)
override fun addInformation(stack: ItemStack, player: EntityPlayer, tooltip: MutableList<Any?>, boolean: Boolean) {
val info = getInfo(stack)
tooltip += StatCollector.translateToLocal("gui.advanced_memory_card.mode.${info.mode.name.lowercase(Locale.getDefault())}")
}
@SideOnly(Side.CLIENT)
private fun clearClientCache() {
ClientCache.clear()
}
override fun onItemRightClick(itemstack: ItemStack, worldIn: World, playerIn: EntityPlayer): ItemStack {
if (playerIn.isSneaking && worldIn.isRemote) {
clearClientCache()
}
return super.onItemRightClick(itemstack, worldIn, playerIn)
}
override fun onItemUse(itemstack: ItemStack, player: EntityPlayer, w: World, x: Int, y: Int, z: Int, side: Int, hx: Float, hy: Float, hz: Float): Boolean {
if (!w.isRemote) {
val te = w.getTileEntity(x, y, z)
if (te is IGridHost && te.getGridNode(ForgeDirection.getOrientation(side)) != null) {
val part = getPart(w, x, y, z, hx, hy, hz)
val stack = player.heldItem
val info = getInfo(stack)
val type: Int
val status: P2PStatus
if (part is PartP2PTunnel<*>) {
type = part.getTypeIndex()
status = P2PStatus(part.gridNode.grid, player, type)
info.selectedEntry = hashP2P(part)
} else {
type = TUNNEL_ANY
status = if (part != null) {
// If we have a PartP2PTunnelME, it looks at the P2P grid node,
// so we should use the part instead.
P2PStatus(part.gridNode.grid, player, type)
} else {
// Fall back to the tile entity.
P2PStatus(te.getGridNode(ForgeDirection.getOrientation(side)).grid, player, type)
}
info.selectedEntry = NONE_SELECTED
}
info.type = type
writeInfo(stack, info)
sendStatus(status, info, player as EntityPlayerMP)
return true
}
}
return false
}
override fun doesSneakBypassUse(world: World?, x: Int, y: Int, z: Int, player: EntityPlayer?): Boolean {
return true
}
@SideOnly(Side.CLIENT)
override fun registerIcons(ri: IIconRegister) {
itemIcon = ri.registerIcon("$MODID:advanced_memory_card")
}
fun getInfo(stack: ItemStack): MemoryInfo {
if (stack.item != this) throw ClassCastException("Cannot cast ${stack.item.javaClass.name} to ${javaClass.name}")
if (stack.tagCompound == null) stack.tagCompound = NBTTagCompound()
val compound = stack.tagCompound!!
if (!compound.hasKey("gui")) compound.setByte("gui", GuiScale.DYNAMIC.ordinal.toByte())
if (!compound.hasKey("selectedIndex", Constants.NBT.TAG_LONG)) compound.setLong("selectedIndex", NONE_SELECTED)
return MemoryInfo(compound.getLong("selectedIndex"), compound.getLong("frequency"), BetterMemoryCardModes.values()[compound.getInteger("mode")], GuiScale.values()[compound.getByte("gui").toInt()])
}
fun writeInfo(stack: ItemStack, info: MemoryInfo) {
if (stack.item != this) throw ClassCastException("Cannot cast ${stack.item.javaClass.name} to ${javaClass.name}")
if (stack.tagCompound == null) stack.tagCompound = NBTTagCompound()
val compound = stack.tagCompound!!
compound.setLong("selectedIndex", info.selectedEntry)
compound.setLong("frequency", info.frequency)
compound.setInteger("mode", info.mode.ordinal)
compound.setByte("gui", info.gui.ordinal.toByte())
}
}