mirror of
https://github.com/PixelGuys/Cubyz.git
synced 2025-08-03 03:06:55 -04:00
Print modifiers, modifier restrictions and tags in item tooltips.
fixes #1587
This commit is contained in:
parent
df0af7afbc
commit
1794515c92
@ -81,6 +81,24 @@ const Material = struct { // MARK: Material
|
||||
inline else => |field| return @field(self, @tagName(field)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn printTooltip(self: Material, outString: *main.List(u8)) void {
|
||||
if(self.modifiers.len == 0) {
|
||||
outString.appendSlice("§#808080Material\n");
|
||||
}
|
||||
for(self.modifiers) |modifier| {
|
||||
if(modifier.restriction.vTable == modifierRestrictions.get("always") orelse unreachable) {
|
||||
modifier.printTooltip(outString);
|
||||
outString.appendSlice("\n");
|
||||
} else {
|
||||
outString.appendSlice("§#808080if ");
|
||||
modifier.restriction.printTooltip(outString);
|
||||
outString.appendSlice("\n ");
|
||||
modifier.printTooltip(outString);
|
||||
outString.appendSlice("\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
pub const ModifierRestriction = struct {
|
||||
@ -90,6 +108,7 @@ pub const ModifierRestriction = struct {
|
||||
pub const VTable = struct {
|
||||
satisfied: *const fn(data: *anyopaque, tool: *const Tool, x: i32, y: i32) bool,
|
||||
loadFromZon: *const fn(allocator: NeverFailingAllocator, zon: ZonElement) *anyopaque,
|
||||
printTooltip: *const fn(data: *anyopaque, outString: *main.List(u8)) void,
|
||||
};
|
||||
|
||||
pub fn satisfied(self: ModifierRestriction, tool: *const Tool, x: i32, y: i32) bool {
|
||||
@ -107,6 +126,10 @@ pub const ModifierRestriction = struct {
|
||||
.data = vTable.loadFromZon(allocator, zon),
|
||||
};
|
||||
}
|
||||
|
||||
pub fn printTooltip(self: ModifierRestriction, outString: *main.List(u8)) void {
|
||||
self.vTable.printTooltip(self.data, outString);
|
||||
}
|
||||
};
|
||||
|
||||
const Modifier = struct {
|
||||
@ -209,6 +232,7 @@ pub const BaseItem = struct { // MARK: BaseItem
|
||||
id: []const u8,
|
||||
name: []const u8,
|
||||
tags: []const Tag,
|
||||
tooltip: []const u8,
|
||||
|
||||
stackSize: u16,
|
||||
material: ?Material,
|
||||
@ -251,6 +275,25 @@ pub const BaseItem = struct { // MARK: BaseItem
|
||||
};
|
||||
self.texture = null;
|
||||
self.foodValue = zon.get(f32, "food", 0);
|
||||
|
||||
var tooltip: main.List(u8) = .init(allocator);
|
||||
tooltip.appendSlice(self.name);
|
||||
tooltip.append('\n');
|
||||
if(self.material) |mat| {
|
||||
mat.printTooltip(&tooltip);
|
||||
}
|
||||
if(self.tags.len != 0) {
|
||||
tooltip.appendSlice("§#808080");
|
||||
for(self.tags, 0..) |tag, i| {
|
||||
if(i != 0) tooltip.append(' ');
|
||||
tooltip.append('.');
|
||||
tooltip.appendSlice(tag.getName());
|
||||
}
|
||||
}
|
||||
if(tooltip.items[tooltip.items.len - 1] == '\n') {
|
||||
_ = tooltip.swapRemove(tooltip.items.len - 1);
|
||||
}
|
||||
self.tooltip = tooltip.toOwnedSlice();
|
||||
}
|
||||
|
||||
fn hashCode(self: BaseItem) u32 {
|
||||
@ -279,7 +322,7 @@ pub const BaseItem = struct { // MARK: BaseItem
|
||||
}
|
||||
|
||||
fn getTooltip(self: BaseItem) []const u8 {
|
||||
return self.name;
|
||||
return self.tooltip;
|
||||
}
|
||||
|
||||
pub fn hasTag(self: *const BaseItem, tag: Tag) bool {
|
||||
@ -1059,6 +1102,7 @@ pub fn globalInit() void {
|
||||
modifierRestrictions.put(decl.name, &.{
|
||||
.satisfied = comptime main.utils.castFunctionSelfToAnyopaque(ModifierRestrictionStruct.satisfied),
|
||||
.loadFromZon = comptime main.utils.castFunctionReturnToAnyopaque(ModifierRestrictionStruct.loadFromZon),
|
||||
.printTooltip = comptime main.utils.castFunctionSelfToAnyopaque(ModifierRestrictionStruct.printTooltip),
|
||||
}) catch unreachable;
|
||||
}
|
||||
Inventory.Sync.ClientSide.init();
|
||||
|
@ -12,3 +12,7 @@ pub fn satisfied(_: *const anyopaque, _: *const Tool, _: i32, _: i32) bool {
|
||||
pub fn loadFromZon(_: NeverFailingAllocator, _: ZonElement) *const anyopaque {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
pub fn printTooltip(_: *const anyopaque, outString: *main.List(u8)) void {
|
||||
outString.appendSlice("always");
|
||||
}
|
||||
|
@ -26,3 +26,12 @@ pub fn loadFromZon(allocator: NeverFailingAllocator, zon: ZonElement) *const And
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
pub fn printTooltip(self: *const And, outString: *main.List(u8)) void {
|
||||
outString.append('(');
|
||||
for(self.children, 0..) |child, i| {
|
||||
if(i != 0) outString.appendSlice(" and ");
|
||||
child.printTooltip(outString);
|
||||
}
|
||||
outString.append(')');
|
||||
}
|
||||
|
@ -29,3 +29,7 @@ pub fn loadFromZon(allocator: NeverFailingAllocator, zon: ZonElement) *const Enc
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
pub fn printTooltip(self: *const Encased, outString: *main.List(u8)) void {
|
||||
outString.writer().print("encased in {} .{s}", .{self.amount, self.tag.getName()}) catch unreachable;
|
||||
}
|
||||
|
@ -21,3 +21,8 @@ pub fn loadFromZon(allocator: NeverFailingAllocator, zon: ZonElement) *const Not
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
pub fn printTooltip(self: *const Not, outString: *main.List(u8)) void {
|
||||
outString.appendSlice("not ");
|
||||
self.child.printTooltip(outString);
|
||||
}
|
||||
|
@ -26,3 +26,12 @@ pub fn loadFromZon(allocator: NeverFailingAllocator, zon: ZonElement) *const Or
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
pub fn printTooltip(self: *const Or, outString: *main.List(u8)) void {
|
||||
outString.append('(');
|
||||
for(self.children, 0..) |child, i| {
|
||||
if(i != 0) outString.appendSlice(" or ");
|
||||
child.printTooltip(outString);
|
||||
}
|
||||
outString.append(')');
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user