Switch to Nodetimers, Enable shift+click
[feedlot.git] / fakeplayer.lua
1 -- mostly taken from the digtron mod
2
3 -- Copyright (C) 2017 FaceDeer, licensed under MIT license
4
5 -- The purpose of this class is to have something that can be passed into callbacks that
6 -- demand a "Player" object as a parameter and hopefully prevent the mods that have
7 -- registered with those callbacks from crashing on a nil dereference or bad function
8 -- call. This is not supposed to be a remotely functional thing, it's just supposed
9 -- to provide dummy methods and return values of the correct data type for anything that
10 -- might ignore the false "is_player()" return and go ahead and try to use this thing
11 -- anyway.
12
13 -- I'm trying to patch holes in bad mod programming, essentially. If a mod is so badly
14 -- programmed that it crashes anyway there's not a lot else I can do on my end of things.
15
16
17
18 feedlotFakePlayer = {}
19 feedlotFakePlayer.__index = feedlotFakePlayer
20
21 local function return_value(x)
22 return (function() return x end)
23 end
24
25 local function return_nil()
26 return nil
27 end
28
29 local function return_empty_string()
30 return ""
31 end
32
33 local function return_zero()
34 return 0
35 end
36
37 local function return_empty_table()
38 return {}
39 end
40
41 function feedlotFakePlayer.update(self, pos, player_name)
42 self.is_fake_player = ":digtron " .. player_name
43 self.get_pos = return_value(pos)
44 end
45 local function get_feed (pos)
46 return function ()
47 local meta = minetest.get_meta(pos)
48 local inv = meta:get_inventory()
49 if not inv or not inv:get_list("main") then
50 return ItemStack(nil)
51 end
52 if inv:get_list("bucket") then
53 return inv:get_stack("bucket", 1)
54 end
55 return inv:get_stack("main", 1)
56 end
57 end
58
59 local function get_inv(pos)
60 return function ()
61 local meta = minetest.get_meta(pos)
62 local inv = meta:get_inventory()
63 return inv
64 end
65 end
66
67 local function set_feed (pos)
68 return function (_, stack)
69 local meta = minetest.get_meta(pos)
70 local inv = meta:get_inventory()
71 if inv:get_list("bucket") then
72 inv:set_stack("bucket", 1, stack)
73 else
74 inv:set_stack("main", 1, stack)
75 end
76 return
77 end
78 end
79
80 function feedlotFakePlayer.create(pos, player_name)
81 local self = {}
82 setmetatable(self, feedlotFakePlayer)
83
84 self.is_fake_player = ":digtron " .. player_name
85
86 -- ObjectRef
87 self.get_pos = return_value(pos)
88 self.set_pos = return_nil
89 self.move_to = return_nil
90 self.punch = return_nil
91 self.right_click = return_nil
92 self.get_hp = return_value(10)
93 self.set_hp = return_nil
94 self.get_inventory = get_inv(pos) -- returns an `InvRef`
95 self.get_wield_list = return_empty_string
96 self.get_wield_index = return_value(1)
97 -- self.get_wielded_item = return_value(ItemStack(nil))
98 self.get_wielded_item = get_feed(pos)
99 self.set_wielded_item = set_feed(pos)
100
101 self.set_armor_groups = return_nil
102 self.get_armor_groups = return_empty_table
103 self.set_animation = return_nil
104 self.get_animation = return_nil -- a set of values, maybe important?
105 self.set_attach = return_nil
106 self.get_attach = return_nil
107 self.set_detach = return_nil
108 self.set_bone_position = return_nil
109 self.get_bone_position = return_nil
110 self.set_properties = return_nil
111 self.get_properties = return_empty_table
112
113 self.is_player = return_value(false)
114
115 self.get_nametag_attributes = return_empty_table
116 self.set_nametag_attributes = return_nil
117
118 --LuaEntitySAO
119 self.set_velocity = return_nil
120 self.get_velocity = return_value({x=0,y=0,z=0})
121 self.set_acceleration = return_nil
122 self.get_acceleration = return_value({x=0,y=0,z=0})
123 self.set_yaw = return_nil
124 self.get_yaw = return_zero
125 self.set_texture_mod = return_nil
126 self.get_texture_mod = return_nil -- maybe important?
127 self.set_sprite = return_nil
128 --self.get_entity_name` (**Deprecated**: Will be removed in a future version)
129 self.get_luaentity = return_nil
130
131 -- Player object
132
133 self.get_player_name = return_empty_string
134 self.get_player_velocity = return_nil
135 self.get_look_dir = return_value({x=0,y=1,z=0})
136 self.get_look_horizontal = return_zero
137 self.set_look_horizontal = return_nil
138 self.get_look_vertical = return_zero
139 self.set_look_vertical = return_nil
140
141 --self.get_look_pitch`: pitch in radians - Deprecated as broken. Use `get_look_vertical`
142 --self.get_look_yaw`: yaw in radians - Deprecated as broken. Use `get_look_horizontal`
143 --self.set_look_pitch(radians)`: sets look pitch - Deprecated. Use `set_look_vertical`.
144 --self.set_look_yaw(radians)`: sets look yaw - Deprecated. Use `set_look_horizontal`.
145 self.get_breath = return_value(10)
146 self.set_breath = return_nil
147 self.get_attribute = return_nil
148 self.set_attribute = return_nil
149
150 self.set_inventory_formspec = return_nil
151 self.get_inventory_formspec = return_empty_string
152 self.get_player_control = return_value({jump=false, right=false, left=false, LMB=false, RMB=false, sneak=false, aux1=false, down=false, up=false} )
153 self.get_player_control_bits = return_zero
154
155 self.set_physics_override = return_nil
156 self.get_physics_override = return_value({speed = 1, jump = 1, gravity = 1, sneak = true, sneak_glitch = false, new_move = true,})
157
158
159 self.hud_add = return_nil
160 self.hud_remove = return_nil
161 self.hud_change = return_nil
162 self.hud_get = return_nil -- possibly important return value?
163 self.hud_set_flags = return_nil
164 self.hud_get_flags = return_value({ hotbar=true, healthbar=true, crosshair=true, wielditem=true, breathbar=true, minimap=true })
165 self.hud_set_hotbar_itemcount = return_nil
166 self.hud_get_hotbar_itemcount = return_zero
167 self.hud_set_hotbar_image = return_nil
168 self.hud_get_hotbar_image = return_empty_string
169 self.hud_set_hotbar_selected_image = return_nil
170 self.hud_get_hotbar_selected_image = return_empty_string
171 self.set_sky = return_nil
172 self.get_sky = return_empty_table -- may need members on this table
173
174 self.set_clouds = return_nil
175 self.get_clouds = return_value({density = 0, color = "#fff0f0e5", ambient = "#000000", height = 120, thickness = 16, speed = {x=0, y=-2}})
176
177 self.override_day_night_ratio = return_nil
178 self.get_day_night_ratio = return_nil
179
180 self.set_local_animation = return_nil
181 self.get_local_animation = return_empty_table
182
183 self.set_eye_offset = return_nil
184 self.get_eye_offset = return_value({x=0,y=0,z=0},{x=0,y=0,z=0})
185
186 return self
187 end
188