Skip to content

Instantly share code, notes, and snippets.

@HurricanKai
Last active September 26, 2018 09:26
Show Gist options
  • Select an option

  • Save HurricanKai/d5d10424cf9c5c52ca92c260d3a5260f to your computer and use it in GitHub Desktop.

Select an option

Save HurricanKai/d5d10424cf9c5c52ca92c260d3a5260f to your computer and use it in GitHub Desktop.
public class EntityStack
{
private Stack<Entity> entities = new Stack<Entity>();
private Queue<Entity> toCleanup = new Queue<Entity>();
private Queue<Entity> unused = new Queue<Entity>();
private int Amount = 0;
private readonly StringBuilder builder = new StringBuilder();
public EntityStack()
{
}
public Entity PushOne(SupportedType type, out string Command)
{
if (!unused.TryDequeue(out var v))
{
// "allocate" a new one
v = new Entity(++Amount, "stack", type);
var tag = $"stack_{Amount}";
Command = Entity.Summon(tag);
}
else
Command = "# Reusing Entity";
entities.Push(v);
return v;
}
public Entity PushOne(string name, out string Command)
{
var v = new Entity(name);
entities.Push(v);
Command = $"summon armor_stand ~ ~ ~ {{CustomName:\"{{\\\"text\\\":\\\"{name}\\\"}}\"}}";
return v;
}
public Entity PopOne()
{
var v = entities.Pop();
toCleanup.Enqueue(v);
v.Clear(builder);
return v;
}
public string Cleanup()
{
var v = builder.ToString();
builder.Clear();
foreach (var v2 in toCleanup.ToArray())
{
unused.Enqueue(v2);
}
toCleanup.Clear();
return v;
}
}
public enum SupportedType
{
String,
Int32,
}
public class Entity
{
private readonly Int32 id;
private readonly String purpose;
private readonly SupportedType type;
public SupportedType Type => type;
public Entity(Int32 id, string purpose, SupportedType type)
{
this.id = id;
this.purpose = purpose;
if (type == SupportedType.String)
throw new Exception("Wrong CTOR");
this.type = type;
Selector = $"@e[tag={purpose}_{id},limit=1]";
}
public Entity(string name)
{
type = SupportedType.String;
Selector = name;
}
public string Selector { get; }
public string GetSelector(params string[] options)
{
string s = $"@e[tag={purpose}_{id},";
foreach (var v in options)
s += v + ",";
s += "limit=1]";
return s;
}
public void Clear(StringBuilder builder)
{
switch(type)
{
case SupportedType.Int32:
builder.AppendLine($"scoreboard players reset {Selector} {Scoreboards.VALUE}");
return;
case SupportedType.String:
builder.AppendLine($"kill {Selector}");
return;
/*case SupportedType.String:
builder.AppendLine($"data remove entity {Selector} CustomName");
return;*/
default:
throw new NotImplementedException();
}
}
public static String Summon(String tag)
=> $"execute unless entity @e[tag={tag}] run summon armor_stand ~ ~ ~ {{Tags:[\"{tag}\"]}}";
// builder.AppendLine($"data merge entity {Selector} {{CustomName:\"\\\"{""}\\\"\"}}
public string SetValue(Int32 val)
{
if (type != SupportedType.Int32)
throw new ArgumentException($"\"Memory Unit\" {purpose}_{id} is of type {type} but you requested to Set the value to Int32");
return $"scoreboard players set {Selector} {Scoreboards.VALUE} {val}";
}
/*public string SetValue(string val)
{
if (type != SupportedType.String)
throw new ArgumentException($"\"Memory Unit\" {purpose}_{id} is of type {type} but you requested to Set the value to String");
return $"data merge entity {Selector} {{CustomName:\"\\\"{val}\\\"\"}}";
}*/
public string SetValue(Entity val)
{
if (this.type != val.type)
throw new ArgumentException($"\"Memory Unit\" {purpose}_{id} and \"Memory Unit\" {val.purpose}_{val.id} are incompatible");
if (this.type == SupportedType.Int32)
return $"scoreboard players operation {Selector} __value = {val.Selector} __value";
/*if (this.type == SupportedType.String)
return $"";*/
else throw new Exception("Ouuuh");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment