Common Mistakes - Italian translation added
This commit is contained in:
parent
2b6e929e2f
commit
21f9abe169
@ -1,72 +1,67 @@
|
|||||||
---
|
---
|
||||||
title: Common Mistakes
|
title: Errori comuni
|
||||||
layout: default
|
layout: default
|
||||||
root: ../..
|
root: ../..
|
||||||
idx: 8.1
|
idx: 8.1
|
||||||
redirect_from: /en/chapters/common_mistakes.html
|
redirect_from: /it/chapters/common_mistakes.html
|
||||||
---
|
---
|
||||||
|
|
||||||
## Introduction <!-- omit in toc -->
|
## Introduzione <!-- omit in toc -->
|
||||||
|
|
||||||
This chapter details common mistakes, and how to avoid them.
|
Questo capitolo illustra gli errori più comuni e come evitarli.
|
||||||
|
|
||||||
- [Never Store ObjectRefs (ie: players or entities)](#never-store-objectrefs-ie-players-or-entities)
|
- [Non salvare mai ObjectRef (giocatori ed entità)](#non-salvare-mai-objectref-giocatori-ed-entita)
|
||||||
- [Don't Trust Formspec Submissions](#dont-trust-formspec-submissions)
|
- [Non fidarti dei campi dei formspec](#non-fidarti-dei-campi-dei-formspec)
|
||||||
- [Set ItemStacks After Changing Them](#set-itemstacks-after-changing-them)
|
- [Imposta gli ItemStack dopo averli modificati](#imposta-gli-itemstack-dopo-averli-modificati)
|
||||||
|
|
||||||
## Never Store ObjectRefs (ie: players or entities)
|
## Non salvare mai ObjectRef (giocatori ed entità)
|
||||||
|
|
||||||
If the object an ObjectRef represents is deleted - for example, if the player goes
|
Se l'oggetto rappresentato da un ObjectRef viene rimosso - per esempio quando il giocatore si disconnette o un'entità viene rimossa dalla memoria - chiamare metodi su quell'oggetto causerà la chiusura improvvisa del server (*crash*).
|
||||||
offline or the entity is unloaded - then calling methods on that object
|
|
||||||
will result in a crash.
|
|
||||||
|
|
||||||
For example, don't do this:
|
Sbagliato:
|
||||||
|
|
||||||
```lua
|
```lua
|
||||||
minetest.register_on_joinplayer(function(player)
|
minetest.register_on_joinplayer(function(player)
|
||||||
local function func()
|
local function func()
|
||||||
local pos = player:get_pos() -- BAD!
|
local pos = player:get_pos() -- MALE!
|
||||||
-- `player` is stored then accessed later.
|
-- `player` viene salvato per essere utilizzato dopo.
|
||||||
-- If the player leaves in that second, the server *will* crash.
|
-- Se il giocatore si disconnette, il server crasha
|
||||||
end
|
end
|
||||||
|
|
||||||
minetest.after(1, func)
|
minetest.after(1, func)
|
||||||
|
|
||||||
foobar[player:get_player_name()] = player
|
foobar[player:get_player_name()] = player
|
||||||
-- RISKY
|
-- RISCHIOSO
|
||||||
-- It's not recommended to do this.
|
-- Non è consigliato fare così.
|
||||||
-- Use minetest.get_connected_players() and
|
-- Usa invece minetest.get_connected_players() e minetest.get_player_by_name().
|
||||||
-- minetest.get_player_by_name() instead.
|
|
||||||
end)
|
end)
|
||||||
```
|
```
|
||||||
|
|
||||||
Do this instead:
|
Giusto:
|
||||||
|
|
||||||
```lua
|
```lua
|
||||||
minetest.register_on_joinplayer(function(player)
|
minetest.register_on_joinplayer(function(player)
|
||||||
local function func(name)
|
local function func(name)
|
||||||
-- Attempt to get the ref again
|
-- Tenta di ottenere il riferimento
|
||||||
local player = minetest.get_player_by_name(name)
|
local player = minetest.get_player_by_name(name)
|
||||||
|
|
||||||
-- Check that the player is still online
|
-- Controlla che il giocatore sia online
|
||||||
if player then
|
if player then
|
||||||
-- Yay! This is fine
|
-- è online, procedo
|
||||||
local pos = player:get_pos()
|
local pos = player:get_pos()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Pass the name into the function
|
-- Passa il nome nella funzione
|
||||||
minetest.after(1, func, player:get_player_name())
|
minetest.after(1, func, player:get_player_name())
|
||||||
end)
|
end)
|
||||||
```
|
```
|
||||||
|
|
||||||
## Don't Trust Formspec Submissions
|
## Non fidarti dei campi dei formspec
|
||||||
|
|
||||||
Malicious clients can submit formspecs whenever they like, with
|
Client malevoli possono compilare i campi nei formspec quando vogliono, con qualsiasi contenuto vogliono.
|
||||||
whatever content they like.
|
|
||||||
|
|
||||||
For example, the following code has a vulnerability which allows players to
|
Per esempio, il seguente codice presenta una vulnerabilità che permette ai giocatori di assegnarsi da soli il privilegio di moderatore:
|
||||||
give themselves moderator privileges:
|
|
||||||
|
|
||||||
```lua
|
```lua
|
||||||
local function show_formspec(name)
|
local function show_formspec(name)
|
||||||
@ -76,15 +71,15 @@ local function show_formspec(name)
|
|||||||
|
|
||||||
minetest.show_formspec(name, "modman:modman", [[
|
minetest.show_formspec(name, "modman:modman", [[
|
||||||
size[3,2]
|
size[3,2]
|
||||||
field[0,0;3,1;target;Name;]
|
field[0,0;3,1;target;Nome;]
|
||||||
button_exit[0,1;3,1;sub;Promote]
|
button_exit[0,1;3,1;sub;Promuovi]
|
||||||
]])
|
]])
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
|
|
||||||
minetest.register_on_player_receive_fields(function(player,
|
minetest.register_on_player_receive_fields(function(player,
|
||||||
formname, fields)
|
formname, fields)
|
||||||
-- BAD! Missing privilege check here!
|
-- MALE! Manca il controllo dei privilegi!
|
||||||
|
|
||||||
local privs = minetest.get_player_privs(fields.target)
|
local privs = minetest.get_player_privs(fields.target)
|
||||||
privs.kick = true
|
privs.kick = true
|
||||||
@ -94,7 +89,7 @@ minetest.register_on_player_receive_fields(function(player,
|
|||||||
end)
|
end)
|
||||||
```
|
```
|
||||||
|
|
||||||
Add a privilege check to solve this:
|
Aggiungi un controllo dei privilegi per ovviare:
|
||||||
|
|
||||||
```lua
|
```lua
|
||||||
minetest.register_on_player_receive_fields(function(player,
|
minetest.register_on_player_receive_fields(function(player,
|
||||||
@ -107,61 +102,51 @@ minetest.register_on_player_receive_fields(function(player,
|
|||||||
end)
|
end)
|
||||||
```
|
```
|
||||||
|
|
||||||
## Set ItemStacks After Changing Them
|
## Imposta gli ItemStack dopo averli modificati
|
||||||
|
|
||||||
Have you noticed that it's simply called an `ItemStack` in the API, not an `ItemStackRef`,
|
Se ci si fa caso, nella documentazione si parla di `ItemStack` e non `ItemStackRef`.
|
||||||
similar to `InvRef`? This is because an `ItemStack` isn't a reference - it's a
|
Questo perché gli ItemStack NON sono un riferimento, bensì una copia.
|
||||||
copy. Stacks work on a copy of the data rather than the stack in the inventory.
|
Questo vuol dire che modificando la copia, non si modificherà in automatico anche l'originale.
|
||||||
This means that modifying a stack won't actually modify that stack in the inventory.
|
|
||||||
|
|
||||||
For example, don't do this:
|
Sbagliato:
|
||||||
|
|
||||||
```lua
|
```lua
|
||||||
local inv = player:get_inventory()
|
local inv = player:get_inventory()
|
||||||
local stack = inv:get_stack("main", 1)
|
local pila = inv:get_stack("main", 1) -- lo copio
|
||||||
stack:get_meta():set_string("description", "Partially eaten")
|
pila:get_meta():set_string("description", "Un po' smangiucchiato")
|
||||||
-- BAD! Modification will be lost
|
-- MALE! Le modifiche saranno perse
|
||||||
```
|
```
|
||||||
|
|
||||||
Do this instead:
|
Giusto:
|
||||||
|
|
||||||
```lua
|
```lua
|
||||||
local inv = player:get_inventory()
|
local inv = player:get_inventory()
|
||||||
local stack = inv:get_stack("main", 1)
|
local pila = inv:get_stack("main", 1) -- lo copio
|
||||||
stack:get_meta():set_string("description", "Partially eaten")
|
pila:get_meta():set_string("description", "Un po' smangiucchiato")
|
||||||
inv:set_stack("main", 1, stack)
|
inv:set_stack("main", 1, pila)
|
||||||
-- Correct! Item stack is set
|
-- Corretto! L'ItemStack è stato cambiato con la copia
|
||||||
```
|
```
|
||||||
|
|
||||||
The behaviour of callbacks is slightly more complicated. Modifying an `ItemStack` you
|
Il comportamento dei callback è leggermente più complicato.
|
||||||
are given will change it for the caller too, and any subsequent callbacks. However,
|
|
||||||
it will only be saved in the engine if the callback caller sets it.
|
|
||||||
|
|
||||||
```lua
|
```lua
|
||||||
minetest.register_on_item_eat(function(hp_change, replace_with_item,
|
minetest.register_on_item_eat(function(hp_change, replace_with_item,
|
||||||
itemstack, user, pointed_thing)
|
itemstack, user, pointed_thing)
|
||||||
itemstack:get_meta():set_string("description", "Partially eaten")
|
itemstack:get_meta():set_string("description", "Un po' smangiucchiato")
|
||||||
-- Almost correct! Data will be lost if another
|
-- Quasi corretto! I dati saranno persi se un altro callback annulla questa chiamata
|
||||||
-- callback cancels the behaviour
|
|
||||||
end)
|
end)
|
||||||
```
|
```
|
||||||
|
|
||||||
If no callbacks cancel this, the stack will be set and the description will be updated,
|
Se nessun callback cancella l'operazione, la pila sarà impostata e la descrizione aggiornata; ma se un callback effettivamente cancella l'operazione, l'aggiornamento potrebbe andar perduto.
|
||||||
but if a callback does cancel this, then the update may be lost.
|
|
||||||
|
|
||||||
It's better to do this instead:
|
È meglio quindi fare così:
|
||||||
|
|
||||||
```lua
|
```lua
|
||||||
minetest.register_on_item_eat(function(hp_change, replace_with_item,
|
minetest.register_on_item_eat(function(hp_change, replace_with_item,
|
||||||
itemstack, user, pointed_thing)
|
itemstack, user, pointed_thing)
|
||||||
itemstack:get_meta():set_string("description", "Partially eaten")
|
itemstack:get_meta():set_string("description", "Un po' smangiucchiato")
|
||||||
user:get_inventory():set_stack("main", user:get_wield_index(),
|
user:get_inventory():set_stack("main", user:get_wield_index(),
|
||||||
itemstack)
|
itemstack)
|
||||||
-- Correct, description will always be set!
|
-- Corretto! La descrizione verrà sempre aggiornata
|
||||||
end)
|
end)
|
||||||
```
|
```
|
||||||
|
|
||||||
If the callbacks cancel or the callback runner doesn't set the stack,
|
|
||||||
then the update will still be set.
|
|
||||||
If the callbacks or the callback runner set the stack, then the use of
|
|
||||||
set_stack doesn't matter.
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user