Client LuaCsForBarotrauma
ConditionallyEditable.cs
1 using System;
2 using System.Linq;
4 
5 namespace Barotrauma;
6 
7 [AttributeUsage(AttributeTargets.Property)]
9 {
10  public ConditionallyEditable(ConditionType conditionType, bool onlyInEditors = true)
11  {
12  this.conditionType = conditionType;
13  this.onlyInEditors = onlyInEditors;
14  }
15  private readonly ConditionType conditionType;
16 
17  private readonly bool onlyInEditors;
18 
19  public enum ConditionType
20  {
21  //These need to exist at compile time, so it is a little awkward
22  //I would love to see a better way to do this
23  AllowLinkingWifiToChat,
24  IsSwappableItem,
25  AllowRotating,
26  Attachable,
27  HasBody,
28  Pickable,
29  OnlyByStatusEffectsAndNetwork,
30  HasIntegratedButtons,
31  IsToggleableController,
32  HasConnectionPanel,
33  DeteriorateUnderStress
34  }
35 
36  public bool IsEditable(ISerializableEntity entity)
37  {
38  if (onlyInEditors && Screen.Selected is { IsEditor: false }) { return false; }
39 
40  return conditionType switch
41  {
42  ConditionType.AllowLinkingWifiToChat
43  => GameMain.NetworkMember is not { ServerSettings.AllowLinkingWifiToChat: false },
44  ConditionType.IsSwappableItem
45  => entity is Item item && item.Prefab.SwappableItem != null,
46  ConditionType.AllowRotating
47  => (entity is Item item && (item.body == null || item.body.BodyType == FarseerPhysics.BodyType.Static) && item.Prefab.AllowRotatingInEditor)
48  || (entity is Structure structure && structure.Prefab.AllowRotatingInEditor),
49  ConditionType.Attachable
50  => GetComponent<Holdable>(entity) is Holdable { Attachable: true },
51  ConditionType.HasBody
52  => entity is Structure { HasBody: true } or Item { body: not null },
53  ConditionType.Pickable
54  => entity is Item item && item.GetComponent<Pickable>() != null,
55  ConditionType.OnlyByStatusEffectsAndNetwork
56  => GameMain.NetworkMember is { IsServer: true },
57  ConditionType.HasIntegratedButtons
58  => GetComponent<Door>(entity) is { HasIntegratedButtons: true },
59  ConditionType.IsToggleableController
60  => GetComponent<Controller>(entity) is Controller { IsToggle: true } controller &&
61  controller.Item.GetComponent<ConnectionPanel>() != null,
62  ConditionType.HasConnectionPanel
63  => GetComponent<ConnectionPanel>(entity) != null,
64  ConditionType.DeteriorateUnderStress
65  => entity is Item repairableItem && repairableItem.Components.Any(c => c is IDeteriorateUnderStress),
66  _
67  => false
68  };
69 
70  static T GetComponent<T>(ISerializableEntity e) where T : ItemComponent
71  {
72  if (e is T t) { return t; }
73  if (e is Item item)
74  {
75  return item.GetComponent<T>();
76  }
77  if (e is ItemComponent ic)
78  {
79  return ic.Item.GetComponent<T>();
80  }
81  return null;
82  }
83  }
84 }
The base class for components holding the different functionalities of the item
bool IsEditable(ISerializableEntity entity)
ConditionallyEditable(ConditionType conditionType, bool onlyInEditors=true)