Update some features

1.click record to rename P2P Tunnel (Help player to build large ae network)
2.shift click record. Well be transport player to P2P Tunnel position (OP only)
This commit is contained in:
zy committed 2022-09-11 22:09:58 +08:00
1 parent 3621407fa8
commit b9aa3a15bc
15 files changed
+252 -22

No files matched your search

@@ -0,0 +1,10 @@
package com.projecturanus.betterp2p.capability
data class P2PTunnelInfo(
var posX: Int = 0,
var posY: Int = 0,
var posZ: Int = 0,
var world: Int = 0,
var side: Int = 0,
var name:String? = null
)
@@ -4,17 +4,21 @@ import appeng.client.gui.widgets.MEGuiTextField
import com.projecturanus.betterp2p.BetterP2P
import com.projecturanus.betterp2p.MODID
import com.projecturanus.betterp2p.capability.MemoryInfo
import com.projecturanus.betterp2p.capability.P2PTunnelInfo
import com.projecturanus.betterp2p.client.ClientCache
import com.projecturanus.betterp2p.client.TextureBound
import com.projecturanus.betterp2p.client.gui.widget.IGuiTextField
import com.projecturanus.betterp2p.client.gui.widget.WidgetP2PDevice
import com.projecturanus.betterp2p.client.gui.widget.WidgetScrollBar
import com.projecturanus.betterp2p.item.BetterMemoryCardModes
import com.projecturanus.betterp2p.network.*
import net.minecraft.client.Minecraft
import net.minecraft.client.gui.GuiButton
import net.minecraft.client.gui.GuiScreen
import net.minecraft.client.renderer.Tessellator
import net.minecraft.client.resources.I18n
import net.minecraft.util.ResourceLocation
import org.lwjgl.input.Keyboard
import org.lwjgl.input.Mouse
import java.util.*
@@ -32,6 +36,7 @@ class GuiAdvancedMemoryCard(msg: S2CListP2P) : GuiScreen(), TextureBound {
private lateinit var scrollBar: WidgetScrollBar
private lateinit var searchBar: MEGuiTextField
private lateinit var renameBar: IGuiTextField
private val infos = msg.infos.map(::InfoWrapper).toMutableList()
@@ -47,7 +52,6 @@ class GuiAdvancedMemoryCard(msg: S2CListP2P) : GuiScreen(), TextureBound {
private var infoOnScreen: List<InfoWrapper>
private val descriptionLines: MutableList<String> = mutableListOf()
private var mode = msg.memoryInfo.mode
private var modeString = getModeString()
private val modeButton by lazy { GuiButton(0, guiLeft + 8, guiTop + 154, 256, 20, modeString) }
@@ -67,6 +71,8 @@ class GuiAdvancedMemoryCard(msg: S2CListP2P) : GuiScreen(), TextureBound {
override fun initGui() {
super.initGui()
checkInfo()
renameBar = IGuiTextField(0,0)
renameBar.setMaxStringLength(50)
scrollBar = WidgetScrollBar()
searchBar = MEGuiTextField(65, 10)
searchBar.setMaxStringLength(25)
@@ -125,7 +131,7 @@ class GuiAdvancedMemoryCard(msg: S2CListP2P) : GuiScreen(), TextureBound {
} }
}
fun refreshInfo(infos: List<P2PInfo>) {
fun refreshInfo(infos: List<P2PInfo>,reGenInfo:Boolean=false) {
for (info in infos) {
val wrapper = InfoWrapper(info)
this.infos[info.index] = wrapper
@@ -133,6 +139,8 @@ class GuiAdvancedMemoryCard(msg: S2CListP2P) : GuiScreen(), TextureBound {
checkInfo()
sortInfo()
refreshOverlay()
if(reGenInfo)
reGenInfoFromText()
}
private fun syncMemoryInfo() {
@@ -149,6 +157,7 @@ class GuiAdvancedMemoryCard(msg: S2CListP2P) : GuiScreen(), TextureBound {
}
override fun onGuiClosed() {
saveP2PChannel()
syncMemoryInfo()
ModNetwork.channel.sendToServer(C2SCloseGui())
}
@@ -158,7 +167,6 @@ class GuiAdvancedMemoryCard(msg: S2CListP2P) : GuiScreen(), TextureBound {
drawBackground()
scrollBar.draw(this)
searchBar.drawTextBox()
for (widget in widgetDevices) {
widget.render(this, mouseX, mouseY, partialTicks)
}
@@ -171,7 +179,8 @@ class GuiAdvancedMemoryCard(msg: S2CListP2P) : GuiScreen(), TextureBound {
descriptionLines.clear()
}
drawInformation()
if(renameBar.isVisible && !renameBar.isFocused) renameBar.setFocus(true)
renameBar.drawTextBox()
super.drawScreen(mouseX, mouseY, partialTicks)
}
@@ -180,7 +189,6 @@ class GuiAdvancedMemoryCard(msg: S2CListP2P) : GuiScreen(), TextureBound {
mode = mode.next()
modeString = getModeString()
modeButton.displayString = modeString
syncMemoryInfo()
}
@@ -213,6 +221,34 @@ class GuiAdvancedMemoryCard(msg: S2CListP2P) : GuiScreen(), TextureBound {
private fun onSelectButtonClicked(info: InfoWrapper) {
selectInfo(info.index)
}
private fun onRenameButtonClicked(info:InfoWrapper,index: Int){
if(isShiftKeyDown()){
return transportPlayer(info)
}
renameBar.isVisible = true
renameBar.y = (this.guiTop + 6) + (index + 1) * 33
renameBar.x = this.guiLeft + 60
renameBar.w = 120
renameBar.h = 12
renameBar.text = info.name
renameBar.setFocus(true,0)
renameBar.info = info
}
private fun saveP2PChannel(){
for (widget in widgetDevices){
widget.renderNameTextField = true
}
if(renameBar.info != null && renameBar.info.name != renameBar.text){
val info:InfoWrapper = renameBar.info
renameBar.text = renameBar.text.trim()
ModNetwork.channel.sendToServer(C2SP2PTunnelInfo(P2PTunnelInfo(info.posX,info.posY,info.posZ,info.world,info.facing.ordinal,renameBar.text)))
}
renameBar.isVisible = false
renameBar.text = ""
renameBar.setFocus(false)
renameBar.info = null
}
private fun onBindButtonClicked(info: InfoWrapper) {
if (selectedIndex == -1) return
@@ -233,19 +269,37 @@ class GuiAdvancedMemoryCard(msg: S2CListP2P) : GuiScreen(), TextureBound {
}
}
private fun transportPlayer(info: InfoWrapper){
Minecraft.getMinecraft().thePlayer.closeScreen()
ModNetwork.channel.sendToServer(C2STransportPlayer(P2PTunnelInfo(info.posX,info.posY,info.posZ,info.world,0)))
}
override fun mouseClicked(mouseX: Int, mouseY: Int, mouseButton: Int) {
for (widget in widgetDevices) {
var clickRenameButton = false
for ((index,widget) in widgetDevices.withIndex()) {
val info = widget.infoSupplier()
if (info?.selectButton?.mousePressed(mc, mouseX, mouseY) == true)
if (info?.selectButton?.mousePressed(mc, mouseX, mouseY) == true) {
onSelectButtonClicked(widget.infoSupplier()!!)
else if (info?.bindButton?.mousePressed(mc, mouseX, mouseY) == true)
}else if (info?.bindButton?.mousePressed(mc, mouseX, mouseY) == true) {
onBindButtonClicked(widget.infoSupplier()!!)
}else if (info?.renameButton?.mousePressed(mc, mouseX,mouseY) == true) {
if(renameBar.info != widget.infoSupplier()!!)
saveP2PChannel()
widget.renderNameTextField = false
onRenameButtonClicked(widget.infoSupplier()!!,index)
clickRenameButton = true
}
}
if(!clickRenameButton && renameBar.isVisible){
saveP2PChannel()
}
if (modeButton.mousePressed(mc, mouseX, mouseY)) {
switchMode()
}
scrollBar.click(mouseX, mouseY)
searchBar.mouseClicked(mouseX, mouseY, mouseButton)
renameBar.mouseClicked(mouseX,mouseY,mouseButton)
if (mouseButton == 1 && searchBar.isMouseIn(mouseX, mouseY)) {
this.searchBar.text = ""
reGenInfoFromText()
@@ -267,6 +321,7 @@ class GuiAdvancedMemoryCard(msg: S2CListP2P) : GuiScreen(), TextureBound {
// this.mouseWheelEvent(x, y, i / Math.abs(i))
} else if (i != 0) {
scrollBar.wheel(i)
saveP2PChannel()
}
}
@@ -295,11 +350,17 @@ class GuiAdvancedMemoryCard(msg: S2CListP2P) : GuiScreen(), TextureBound {
}
override fun keyTyped(char: Char, key: Int) {
if (char == ' ' && searchBar.text.isEmpty()) {
return
if(key == Keyboard.KEY_LSHIFT) return
if(renameBar.isFocused){
if(key == Keyboard.KEY_RETURN){
saveP2PChannel()
}else{
renameBar.textboxKeyTyped(char, key)
}
}else if (searchBar.isFocused && !(char == ' ' && searchBar.text.isEmpty())){
searchBar.textboxKeyTyped(char, key)
reGenInfoFromText()
}
searchBar.textboxKeyTyped(char, key)
reGenInfoFromText()
super.keyTyped(char, key)
return super.keyTyped(char, key)
}
}
@@ -14,16 +14,17 @@ class InfoWrapper(info: P2PInfo) {
val posX: Int = info.posX
val posY: Int = info.posY
val posZ: Int = info.posZ
val world: Int = info.world
val facing: EnumFacing = info.facing
val description: String
val output: Boolean = info.output
val name: String = info.name
var name: String = info.name
var error: Boolean = false
// Widgets
val selectButton = GuiButton(0, 0, 0, 34, 20, I18n.format("gui.advanced_memory_card.select"))
val bindButton = GuiButton(0, 0, 0, 34, 20, I18n.format("gui.advanced_memory_card.bind"))
val renameButton = GuiButton(0, 0, 0, 0, 0,"")
init {
description = buildString {
append("P2P ")
@@ -0,0 +1,33 @@
package com.projecturanus.betterp2p.client.gui.widget;
import appeng.client.gui.widgets.MEGuiTextField;
import com.projecturanus.betterp2p.client.gui.InfoWrapper;
public class IGuiTextField extends MEGuiTextField {
InfoWrapper info = null;
public void setVisible(boolean visible){
this.field.setVisible(visible);
}
public void setFocus(boolean focus,int position){
this.field.setFocused(focus);
this.field.setCursorPosition(position);
}
public IGuiTextField(final int width, final int height, final String tooltip){
super(width,height,tooltip);
}
public IGuiTextField(final int width, final int height)
{
super(width,height,"");
this.setVisible(false);
}
public void setInfo(InfoWrapper _info){
info = _info;
}
public InfoWrapper getInfo(){
return info;
}
public void setFocus(boolean focus) {
this.field.setFocused(focus);
}
}
@@ -15,6 +15,7 @@ class WidgetP2PDevice(private val selectedInfoProperty: KMutableProperty0<InfoWr
private val rowWidth = 254
private val rowHeight = 32
public var renderNameTextField = true
private var selectedInfo: InfoWrapper?
get() = selectedInfoProperty.get()
@@ -43,7 +44,11 @@ class WidgetP2PDevice(private val selectedInfoProperty: KMutableProperty0<InfoWr
fontRenderer.drawString(info.description, x + 24, y + 3, 0)
fontRenderer.drawString(I18n.format("gui.advanced_memory_card.pos", info.posX, info.posY, info.posZ, info.facing.name), x + 24, y + 12, 0)
fontRenderer.drawString(I18n.format("gui.advanced_memory_card.name", info.name), x + 24, y + 21, 0)
if(renderNameTextField){
fontRenderer.drawString(I18n.format("gui.advanced_memory_card.name", info.name), x + 24, y + 21, 0)
}else{
fontRenderer.drawString(I18n.format("gui.advanced_memory_card.name", ""), x + 24, y + 21, 0)
}
if (selectedInfo == null) {
info.bindButton.enabled = false
@@ -82,6 +87,10 @@ class WidgetP2PDevice(private val selectedInfoProperty: KMutableProperty0<InfoWr
}
private fun drawButtons(gui: GuiScreen, info: InfoWrapper, mouseX: Int, mouseY: Int, partialTicks: Float) {
info.renameButton.xPosition = x
info.renameButton.width = rowWidth
info.renameButton.yPosition = y
info.renameButton.height = rowHeight
if (!info.bindButton.enabled && info.selectButton.enabled) {
info.bindButton.enabled = false
info.selectButton.enabled = true
@@ -98,7 +98,7 @@ object ItemAdvancedMemoryCard : Item() {
itemIcon = ri.registerIcon("$MODID:advanced_memory_card")
}
private fun getInfo(stack: ItemStack): MemoryInfo {
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()
@@ -0,0 +1,37 @@
package com.projecturanus.betterp2p.network
import com.projecturanus.betterp2p.capability.P2PTunnelInfo
import cpw.mods.fml.common.network.simpleimpl.IMessage
import io.netty.buffer.ByteBuf
fun readP2PTunnelInfo(buf: ByteBuf): P2PTunnelInfo {
return P2PTunnelInfo(buf.readInt(),buf.readInt(),buf.readInt(),buf.readInt(),buf.readInt(), buf.readBytes(buf.readInt()).toString(Charsets.UTF_8))
}
fun writeP2PTunnelInfo(buf: ByteBuf,info: P2PTunnelInfo) {
buf.writeInt(info.posX)
buf.writeInt(info.posY)
buf.writeInt(info.posZ)
buf.writeInt(info.world)
buf.writeInt(info.side)
if(info.name ==null) {
buf.writeInt(0)
}else{
val arr = info.name?.toByteArray(Charsets.UTF_8)
arr?.let { buf.writeInt(it.size) }
buf.writeBytes(arr)
}
}
class C2SP2PTunnelInfo(var info: P2PTunnelInfo = P2PTunnelInfo()): IMessage {
override fun fromBytes(buf: ByteBuf) {
info = readP2PTunnelInfo(buf)
}
override fun toBytes(buf: ByteBuf) {
writeP2PTunnelInfo(buf,info)
}
}
@@ -0,0 +1,15 @@
package com.projecturanus.betterp2p.network
import com.projecturanus.betterp2p.capability.P2PTunnelInfo
import cpw.mods.fml.common.network.simpleimpl.IMessage
import io.netty.buffer.ByteBuf
class C2STransportPlayer(var info: P2PTunnelInfo = P2PTunnelInfo()):IMessage{
override fun fromBytes(buf: ByteBuf) {
info = readP2PTunnelInfo(buf)
}
override fun toBytes(buf: ByteBuf) {
writeP2PTunnelInfo(buf,info)
}
}
@@ -11,7 +11,12 @@ import net.minecraft.client.Minecraft
class ClientOpenGuiHandler : IMessageHandler<S2CListP2P, IMessage?> {
@SideOnly(Side.CLIENT)
override fun onMessage(message: S2CListP2P, ctx: MessageContext): IMessage? {
Minecraft.getMinecraft().displayGuiScreen(GuiAdvancedMemoryCard(message))
val gui = Minecraft.getMinecraft().currentScreen
if(gui is GuiAdvancedMemoryCard){
gui.refreshInfo(message.infos,true)
}else {
Minecraft.getMinecraft().displayGuiScreen(GuiAdvancedMemoryCard(message))
}
return null
}
}
@@ -11,9 +11,10 @@ object ModNetwork {
fun registerNetwork() {
channel.registerMessage(ClientOpenGuiHandler::class.java, S2CListP2P::class.java, 0, Side.CLIENT)
channel.registerMessage(ClientRefreshInfoHandler::class.java, S2CRefreshInfo::class.java, 1, Side.CLIENT)
channel.registerMessage(ServerLinkP2PHandler::class.java, C2SLinkP2P::class.java, 2, Side.SERVER)
channel.registerMessage(ServerCloseGuiHandler::class.java, C2SCloseGui::class.java, 3, Side.SERVER)
channel.registerMessage(ServerUpdateInfoHandler::class.java, C2SUpdateInfo::class.java, 4, Side.SERVER)
channel.registerMessage(ServerTransportHandler::class.java,C2STransportPlayer::class.java,5,Side.SERVER)
channel.registerMessage(ServerRenameP2PTunnel::class.java,C2SP2PTunnelInfo::class.java,6,Side.SERVER)
}
}
@@ -2,5 +2,5 @@ package com.projecturanus.betterp2p.network
import net.minecraft.util.EnumFacing
class P2PInfo(val index: Int, val frequency: Long, val posX: Int, val posY: Int, val posZ: Int, val facing: EnumFacing, val name: String, val output: Boolean, val hasChannel: Boolean) {
class P2PInfo(val index: Int, val frequency: Long, val posX: Int, val posY: Int, val posZ: Int,val world: Int, val facing: EnumFacing, val name: String, val output: Boolean, val hasChannel: Boolean) {
}
@@ -10,13 +10,14 @@ fun readInfo(buf: ByteBuf): P2PInfo {
val posX = buf.readInt()
val posY = buf.readInt()
val posZ = buf.readInt()
val world = buf.readInt()
val facing = EnumFacing.values()[buf.readInt()]
val nameLength = buf.readShort() - 1
var name = ""
for (i in 0..nameLength) {
name += buf.readChar()
}
return P2PInfo(index, freq, posX, posY, posZ, facing, name, buf.readBoolean(), buf.readBoolean())
return P2PInfo(index, freq, posX, posY, posZ,world, facing, name, buf.readBoolean(), buf.readBoolean())
}
fun writeInfo(buf: ByteBuf, info: P2PInfo) {
@@ -25,6 +26,7 @@ fun writeInfo(buf: ByteBuf, info: P2PInfo) {
buf.writeInt(info.posX)
buf.writeInt(info.posY)
buf.writeInt(info.posZ)
buf.writeInt(info.world)
buf.writeInt(info.facing.ordinal)
buf.writeShort(info.name.length)
for (c in info.name) {
@@ -0,0 +1,31 @@
package com.projecturanus.betterp2p.network
import appeng.api.networking.IGridHost
import appeng.api.parts.IPartHost
import appeng.parts.p2p.PartP2PTunnel
import com.projecturanus.betterp2p.item.ItemAdvancedMemoryCard
import com.projecturanus.betterp2p.util.p2p.P2PStatus
import com.projecturanus.betterp2p.util.p2p.getInfo
import cpw.mods.fml.common.network.simpleimpl.IMessage
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler
import cpw.mods.fml.common.network.simpleimpl.MessageContext
import net.minecraft.tileentity.TileEntity
import net.minecraft.world.World
import net.minecraftforge.common.DimensionManager
import net.minecraftforge.common.util.ForgeDirection
class ServerRenameP2PTunnel : IMessageHandler<C2SP2PTunnelInfo, IMessage?> {
override fun onMessage(message: C2SP2PTunnelInfo, ctx: MessageContext): IMessage? {
val world: World = DimensionManager.getWorld(message.info.world)
val te: TileEntity = world.getTileEntity(message.info.posX,message.info.posY,message.info.posZ)
val player = ctx.serverHandler.playerEntity
if (te is IGridHost && te.getGridNode(ForgeDirection.getOrientation(message.info.side)) != null) {
val p = (te as IPartHost).getPart(ForgeDirection.getOrientation(message.info.side)) as PartP2PTunnel<*>
p.customName = message.info.name
ModNetwork.channel.sendTo(S2CListP2P(
P2PStatus(player, 0, p.gridNode.grid,p).listP2P.mapIndexed { index, p2p -> p2p.getInfo(index) },
ItemAdvancedMemoryCard.getInfo(player.heldItem)
),player)
}
return null
}
}
@@ -0,0 +1,25 @@
package com.projecturanus.betterp2p.network;
import cpw.mods.fml.common.network.simpleimpl.IMessage
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler
import cpw.mods.fml.common.network.simpleimpl.MessageContext
import net.minecraft.entity.player.EntityPlayerMP
import net.minecraft.server.MinecraftServer
import net.minecraft.util.ChatComponentText
class ServerTransportHandler : IMessageHandler<C2STransportPlayer, IMessage?> {
override fun onMessage(message: C2STransportPlayer, ctx: MessageContext): IMessage? {
val player:EntityPlayerMP = ctx.serverHandler.playerEntity
val op = MinecraftServer.getServer().configurationManager.func_152596_g(player.gameProfile)
if(!op){
player.addChatMessage(ChatComponentText("You are not OP"))
}else{
if(player.worldObj.provider.dimensionId!= message.info.world)
player.travelToDimension(message.info.world)
player.setPositionAndUpdate(message.info.posX.toDouble(), message.info.posY.toDouble(), message.info.posZ.toDouble())
}
return null
}
}
@@ -108,4 +108,4 @@ val PartP2PTunnel<*>.hasChannel
get() = isPowered && isActive
fun PartP2PTunnel<*>.getInfo(index: Int)
= P2PInfo(index, frequency, location.x, location.y, location.z, EnumFacing.getFront(side.getEnumIndex()), customName, isOutput, hasChannel)
= P2PInfo(index, frequency, location.x, location.y, location.z,location.dimension,EnumFacing.getFront(side.getEnumIndex()), customName, isOutput, hasChannel)