moved mode-changing packet to Numina and simplified interface

This commit is contained in:
MachineMuse 2013-10-17 21:40:58 -06:00
parent 64df776285
commit b95beaabc8
9 changed files with 52 additions and 153 deletions

View file

@ -4,6 +4,7 @@ import net.machinemuse.api.moduletrigger.IBlockBreakingModule;
import net.machinemuse.api.moduletrigger.IPlayerTickModule; import net.machinemuse.api.moduletrigger.IPlayerTickModule;
import net.machinemuse.api.moduletrigger.IRightClickModule; import net.machinemuse.api.moduletrigger.IRightClickModule;
import net.machinemuse.api.moduletrigger.IToggleableModule; import net.machinemuse.api.moduletrigger.IToggleableModule;
import net.machinemuse.powersuits.item.ModeChangingModularItem;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
@ -141,8 +142,9 @@ public class ModuleManager {
if (module instanceof IRightClickModule) { if (module instanceof IRightClickModule) {
// MuseLogger.logDebug("Module: " + moduleName + " vs Mode: " + // MuseLogger.logDebug("Module: " + moduleName + " vs Mode: " +
// MuseItemUtils.getActiveMode(itemStack)); // MuseItemUtils.getActiveMode(itemStack));
ModeChangingModularItem item = (ModeChangingModularItem) itemStack.getItem();
return moduleName.equals(MuseItemTag.getActiveMode(itemStack)); return moduleName.equals(item.getActiveMode(itemStack));
} else { } else {
return isModuleOnline(MuseItemTag.getMuseItemTag(itemStack), moduleName); return isModuleOnline(MuseItemTag.getMuseItemTag(itemStack), moduleName);
} }

View file

@ -5,6 +5,7 @@ import net.minecraft.item.ItemStack
import java.lang.String import java.lang.String
import net.machinemuse.numina.scala.OptionCast import net.machinemuse.numina.scala.OptionCast
import net.machinemuse.numina.item.ModeChangingItem import net.machinemuse.numina.item.ModeChangingItem
import net.minecraft.entity.player.EntityPlayer
/** /**
* Author: MachineMuse (Claire Semple) * Author: MachineMuse (Claire Semple)
@ -31,12 +32,4 @@ object MuseItemTag {
properties properties
} }
def getActiveMode(stack: ItemStack): String = {
for (
s <- Option(stack);
i <- OptionCast[ModeChangingItem](s.getItem)
) yield i.getActiveMode(stack)
}.getOrElse("")
} }

View file

@ -29,7 +29,7 @@ import net.machinemuse.powersuits.entity.EntityLuxCapacitor;
import net.machinemuse.powersuits.entity.EntityPlasmaBolt; import net.machinemuse.powersuits.entity.EntityPlasmaBolt;
import net.machinemuse.powersuits.entity.EntitySpinningBlade; import net.machinemuse.powersuits.entity.EntitySpinningBlade;
import net.machinemuse.powersuits.event.RenderEventHandler; import net.machinemuse.powersuits.event.RenderEventHandler;
import net.machinemuse.powersuits.network.packets.MusePacketModeChangeRequest; import net.machinemuse.numina.network.MusePacketModeChangeRequest;
import net.machinemuse.powersuits.tick.ClientTickHandler; import net.machinemuse.powersuits.tick.ClientTickHandler;
import net.machinemuse.powersuits.tick.PlayerTickHandler; import net.machinemuse.powersuits.tick.PlayerTickHandler;
import net.machinemuse.powersuits.tick.RenderTickHandler; import net.machinemuse.powersuits.tick.RenderTickHandler;

View file

@ -5,7 +5,7 @@ import cpw.mods.fml.common.TickType;
import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly; import cpw.mods.fml.relauncher.SideOnly;
import net.machinemuse.powersuits.common.ModularPowersuits; import net.machinemuse.powersuits.common.ModularPowersuits;
import net.machinemuse.utils.MuseItemUtils; import net.machinemuse.powersuits.item.ModeChangingModularItem$;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityClientPlayerMP; import net.minecraft.client.entity.EntityClientPlayerMP;
import net.minecraft.client.settings.KeyBinding; import net.minecraft.client.settings.KeyBinding;
@ -59,11 +59,11 @@ public class KeybindKeyHandler extends KeyHandler {
} }
if (kb.equals(cycleToolBackward)) { if (kb.equals(cycleToolBackward)) {
Minecraft.getMinecraft().playerController.updateController(); Minecraft.getMinecraft().playerController.updateController();
MuseItemUtils.cycleMode(player.inventory.getStackInSlot(player.inventory.currentItem), player, 1); ModeChangingModularItem$.MODULE$.cycleModeForItem(player.inventory.getStackInSlot(player.inventory.currentItem), player, 1);
} }
if (kb.equals(cycleToolForward)) { if (kb.equals(cycleToolForward)) {
Minecraft.getMinecraft().playerController.updateController(); Minecraft.getMinecraft().playerController.updateController();
MuseItemUtils.cycleMode(player.inventory.getStackInSlot(player.inventory.currentItem), player, -1); ModeChangingModularItem$.MODULE$.cycleModeForItem(player.inventory.getStackInSlot(player.inventory.currentItem), player, -1);
} }
} }

View file

@ -168,43 +168,41 @@ with ModeChangingModularItem {
* Called when the right click button is released * Called when the right click button is released
*/ */
override def onPlayerStoppedUsing(itemStack: ItemStack, world: World, player: EntityPlayer, par4: Int) { override def onPlayerStoppedUsing(itemStack: ItemStack, world: World, player: EntityPlayer, par4: Int) {
val mode: String = MuseItemTag.getActiveMode(itemStack) val mode: String = getActiveMode(itemStack, player)
val module: IPowerModule = ModuleManager.getModule(mode) val module: IPowerModule = ModuleManager.getModule(mode)
OptionCast[IRightClickModule](module) map { OptionCast[IRightClickModule](module) map {
m => m.onPlayerStoppedUsing(itemStack, world, player, par4) m => m.onPlayerStoppedUsing(itemStack, world, player, par4)
} }
} }
override def shouldPassSneakingClickToBlock(world: World, x: Int, y: Int, z: Int): Boolean = { override def shouldPassSneakingClickToBlock(world: World, x: Int, y: Int, z: Int): Boolean = true
return true
}
override def onItemUseFirst(itemStack: ItemStack, player: EntityPlayer, world: World, x: Int, y: Int, z: Int, side: Int, hitX: Float, hitY: Float, hitZ: Float): Boolean = { override def onItemUseFirst(itemStack: ItemStack, player: EntityPlayer, world: World, x: Int, y: Int, z: Int, side: Int, hitX: Float, hitY: Float, hitZ: Float): Boolean = {
val mode: String = MuseItemTag.getActiveMode(itemStack) val mode: String = getActiveMode(itemStack, player)
val module: IPowerModule = ModuleManager.getModule(mode) val module: IPowerModule = ModuleManager.getModule(mode)
if (module.isInstanceOf[IRightClickModule]) { module match {
return (module.asInstanceOf[IRightClickModule]).onItemUseFirst(itemStack, player, world, x, y, z, side, hitX, hitY, hitZ) case m:IRightClickModule => m.onItemUseFirst(itemStack, player, world, x, y, z, side, hitX, hitY, hitZ)
case _ => false
} }
return false
} }
override def onItemUse(itemStack: ItemStack, player: EntityPlayer, world: World, x: Int, y: Int, z: Int, side: Int, hitX: Float, hitY: Float, hitZ: Float): Boolean = { override def onItemUse(itemStack: ItemStack, player: EntityPlayer, world: World, x: Int, y: Int, z: Int, side: Int, hitX: Float, hitY: Float, hitZ: Float): Boolean = {
val mode: String = MuseItemTag.getActiveMode(itemStack) val mode: String = getActiveMode(itemStack, player)
val module: IPowerModule = ModuleManager.getModule(mode) val module: IPowerModule = ModuleManager.getModule(mode)
if (module.isInstanceOf[IRightClickModule]) { module match {
(module.asInstanceOf[IRightClickModule]).onItemUse(itemStack, player, world, x, y, z, side, hitX, hitY, hitZ) case m:IRightClickModule => m.onItemUse(itemStack, player, world, x, y, z, side, hitX, hitY, hitZ); false
return false case _ => false
} }
return false
} }
def getSaplingModifier(stack: ItemStack, world: World, player: EntityPlayer, x: Int, y: Int, z: Int): Float = { def getSaplingModifier(stack: ItemStack, world: World, player: EntityPlayer, x: Int, y: Int, z: Int): Float = {
if (ModuleManager.itemHasActiveModule(stack, GrafterModule.MODULE_GRAFTER)) { if (ModuleManager.itemHasActiveModule(stack, GrafterModule.MODULE_GRAFTER)) {
ElectricItemUtils.drainPlayerEnergy(player, ModuleManager.computeModularProperty(stack, GrafterModule.GRAFTER_ENERGY_CONSUMPTION)) ElectricItemUtils.drainPlayerEnergy(player, ModuleManager.computeModularProperty(stack, GrafterModule.GRAFTER_ENERGY_CONSUMPTION))
MuseHeatUtils.heatPlayer(player, ModuleManager.computeModularProperty(stack, GrafterModule.GRAFTER_HEAT_GENERATION)) MuseHeatUtils.heatPlayer(player, ModuleManager.computeModularProperty(stack, GrafterModule.GRAFTER_HEAT_GENERATION))
return 100F 100F
} else {
0F
} }
return 0F
} }
def canHarvestBlock(stack: ItemStack, block: Block, meta: Int, player: EntityPlayer): Boolean = { def canHarvestBlock(stack: ItemStack, block: Block, meta: Int, player: EntityPlayer): Boolean = {

View file

@ -1,72 +1,37 @@
package net.machinemuse.powersuits.item package net.machinemuse.powersuits.item
import net.machinemuse.numina.item.ModeChangingItem import net.machinemuse.numina.item.{NuminaItemUtils, ModeChangingItem}
import net.minecraft.item.ItemStack import net.minecraft.item.ItemStack
import net.minecraft.util.Icon import net.minecraft.util.Icon
import net.machinemuse.api.ModuleManager import net.machinemuse.api.ModuleManager
import net.machinemuse.powersuits.common.ModularPowersuits
import net.machinemuse.api.moduletrigger.IRightClickModule import net.machinemuse.api.moduletrigger.IRightClickModule
import net.minecraft.entity.player.EntityPlayer
import net.machinemuse.numina.scala.OptionCast
/** /**
* Author: MachineMuse (Claire Semple) * Author: MachineMuse (Claire Semple)
* Created: 4:52 PM, 9/5/13 * Created: 4:52 PM, 9/5/13
*/ */
object ModeChangingModularItem {
def cycleModeForItem(stack: ItemStack, player: EntityPlayer, dMode: Int) {
for (
s <- Option(stack);
i <- OptionCast[ModeChangingModularItem](stack.getItem)
) {
i.cycleMode(stack, player, dMode)
}
}
}
trait ModeChangingModularItem extends ModeChangingItem { trait ModeChangingModularItem extends ModeChangingItem {
override def getActiveMode(stack: ItemStack): String = { def getModeIcon(mode: String, stack: ItemStack, player: EntityPlayer): Option[Icon] = Option(ModuleManager.getModule(mode)).map(m => m.getIcon(stack))
val mode = super.getActiveMode(stack)
val modes = getModes(stack) def getValidModes(stack: ItemStack, player: EntityPlayer): Seq[String] = {
if (modes.isEmpty) { getValidModes(stack)
""
} else if (mode.isEmpty) {
modes(0)
} else {
mode
}
} }
def getPrevModeIcon(stack: ItemStack): Option[Icon] = Option(ModuleManager.getModule(prevMode(stack))).map(m=>m.getIcon(stack)) def getValidModes(stack: ItemStack): Seq[String] = {
def getCurrentModeIcon(stack: ItemStack): Option[Icon] = Option(ModuleManager.getModule(getActiveMode(stack))).map(m=>m.getIcon(stack))
def getNextModeIcon(stack: ItemStack): Option[ Icon] = Option(ModuleManager.getModule(nextMode(stack))).map(m=>m.getIcon(stack))
def nextMode(stack: ItemStack): String = {
val mode: String = getActiveMode(stack)
val modes = getModes(stack)
if (modes.size > 0) {
val modeIndex: Int = modes.indexOf(mode)
modes(clampMode(modeIndex + 1, modes.size))
} else {
mode
}
}
def prevMode(stack: ItemStack): String = {
val mode: String = getActiveMode(stack)
val modes = getModes(stack)
if (modes.size > 0) {
val modeIndex: Int = modes.indexOf(mode)
modes(clampMode(modeIndex - 1, modes.size))
} else {
mode
}
}
def cycleMode(stack: ItemStack, dMode: Int) {
if (stack != null && stack.getItem.isInstanceOf[ModeChangingItem]) {
val mode: String = getActiveMode(stack)
val modes = getModes(stack)
if (modes.size > 0 && dMode != 0) {
val modeIndex: Int = modes.indexOf(mode)
val newMode: String = modes(clampMode(modeIndex + dMode, modes.size))
this.setActiveMode(stack, newMode)
ModularPowersuits.proxy.sendModeChange(dMode, newMode)
}
}
}
def getModes(stack: ItemStack) = {
import scala.collection.JavaConversions._ import scala.collection.JavaConversions._
for { for {
module <- ModuleManager.getAllModules module <- ModuleManager.getAllModules
@ -78,11 +43,17 @@ trait ModeChangingModularItem extends ModeChangingItem {
} }
} }
private def clampMode(selection: Int, modesSize: Int): Int = { def getActiveMode(stack: ItemStack): String = {
if (selection > 0) { val modeFromNBT = NuminaItemUtils.getTagCompound(stack).getString("mode")
selection % modesSize if (!modeFromNBT.isEmpty) {
modeFromNBT
} else { } else {
(selection + modesSize * (-selection)) % modesSize val validModes = getValidModes(stack)
if (!validModes.isEmpty) {
validModes(0)
} else {
""
}
} }
} }
} }

View file

@ -1,6 +1,6 @@
package net.machinemuse.powersuits.network.packets package net.machinemuse.powersuits.network.packets
import net.machinemuse.numina.network.MusePacketHandler import net.machinemuse.numina.network.{MusePacketModeChangeRequest, MusePacketHandler}
/** /**
* Author: MachineMuse (Claire Semple) * Author: MachineMuse (Claire Semple)
@ -16,7 +16,6 @@ object MPSPacketList {
MusePacketHandler.packagers.put(6, MusePacketPlayerUpdate) MusePacketHandler.packagers.put(6, MusePacketPlayerUpdate)
MusePacketHandler.packagers.put(7, MusePacketToggleRequest) MusePacketHandler.packagers.put(7, MusePacketToggleRequest)
MusePacketHandler.packagers.put(8, MusePacketPlasmaBolt) MusePacketHandler.packagers.put(8, MusePacketPlasmaBolt)
MusePacketHandler.packagers.put(9, MusePacketModeChangeRequest)
MusePacketHandler.packagers.put(10, MusePacketColourInfo) MusePacketHandler.packagers.put(10, MusePacketColourInfo)
MusePacketHandler.packagers.put(11, MusePacketPropertyModifierConfig) MusePacketHandler.packagers.put(11, MusePacketPropertyModifierConfig)
} }

View file

@ -1,53 +0,0 @@
/**
*
*/
package net.machinemuse.powersuits.network.packets
import cpw.mods.fml.common.network.Player
import net.machinemuse.utils.MuseItemUtils
import net.minecraft.entity.player.EntityPlayerMP
import net.minecraft.item.ItemStack
import java.io.DataInputStream
import java.util.List
import scala.Predef._
import net.machinemuse.numina.network.{MusePackager, MusePacket}
import net.machinemuse.numina.item.ModeChangingItem
import net.machinemuse.numina.scala.OptionCast
import net.machinemuse.powersuits.item.ModeChangingModularItem
/**
* Author: MachineMuse (Claire Semple)
* Created: 12:28 PM, 5/6/13
*/
object MusePacketModeChangeRequest extends MusePackager {
def read(d: DataInputStream, p: Player) = {
val slot = readInt(d)
val mode = readString(d)
new MusePacketModeChangeRequest(p, mode, slot)
}
}
class MusePacketModeChangeRequest(player: Player, mode: String, slot: Int) extends MusePacket(player) {
val packager = MusePacketModeChangeRequest
def write {
writeInt(slot)
writeString(mode)
}
override def handleServer(player: EntityPlayerMP) {
var stack: ItemStack = null
if (slot > -1 && slot < 9) {
for {
stack <- Option(player.inventory.mainInventory(slot))
item <- OptionCast[ModeChangingModularItem](stack.getItem)
} {
val modes = item.getModes(stack)
if (modes.contains(mode)) {
OptionCast[ModeChangingItem](stack.getItem).map(i => i.setActiveMode(stack, mode))
}
}
}
}
}

View file

@ -1,17 +1,13 @@
package net.machinemuse.utils; package net.machinemuse.utils;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.machinemuse.api.IModularItem; import net.machinemuse.api.IModularItem;
import net.machinemuse.api.IPowerModule; import net.machinemuse.api.IPowerModule;
import net.machinemuse.api.ModuleManager; import net.machinemuse.api.ModuleManager;
import net.machinemuse.api.MuseItemTag; import net.machinemuse.api.MuseItemTag;
import net.machinemuse.numina.general.MuseLogger; import net.machinemuse.numina.general.MuseLogger;
import net.machinemuse.numina.general.MuseMathUtils; import net.machinemuse.numina.general.MuseMathUtils;
import net.machinemuse.numina.item.ModeChangingItem;
import net.machinemuse.powersuits.client.render.modelspec.DefaultModelSpec; import net.machinemuse.powersuits.client.render.modelspec.DefaultModelSpec;
import net.machinemuse.powersuits.item.ItemComponent; import net.machinemuse.powersuits.item.ItemComponent;
import net.minecraft.client.entity.EntityClientPlayerMP;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.IInventory; import net.minecraft.inventory.IInventory;
@ -24,13 +20,6 @@ import java.util.*;
public class MuseItemUtils { public class MuseItemUtils {
public static final String ONLINE = "Active"; public static final String ONLINE = "Active";
@SideOnly(Side.CLIENT)
public static void cycleMode(ItemStack stack, EntityClientPlayerMP player, int dMode) {
if (stack != null && stack.getItem() instanceof ModeChangingItem) {
((ModeChangingItem)(stack.getItem())).cycleMode(stack, dMode);
}
}
/** /**
* Gets or creates stack.getTagCompound().getTag(NBTPREFIX) * Gets or creates stack.getTagCompound().getTag(NBTPREFIX)
* *