Код: Выделить всё
gateClosed). Однако, когда функция вызывается снова на следующем тике, переменная экземпляра остается той же (false). Из-за этого ворота всегда будут считать, что они открыты, и будут закрываться каждый раз, когда условие закрытия ворот истинно, и никогда не откроются, поскольку предполагается, что они уже открыты. Если я присвою переменной значение true
Этот код будет печатать в консоли «закрытие ворот» каждый тик, если условие закрытия истинно, но если условие закрытия ложно, он ничего не будет печатать.
Код: Выделить всё
public class Gate {
private boolean gateClosed = true;
public void update(ServerWorld world) {
boolean shouldBeClosed = /* should be closed */
// If there is a leader nearby and the gate is not closed, close the gate
if (shouldBeClosed && !this.gateClosed) {
if (!world.getPlayers().isEmpty()) {
world.getPlayers().getFirst().sendMessage(Text.literal("closing gate;"));
}
closeGate(world);
this.gateClosed = true; // Update the gate status after closing it
return;
}
// If there is no leader nearby and the gate is closed, open the gate
else if (!shouldBeClosed && this.gateClosed) {
if (!world.getPlayers().isEmpty()) {
world.getPlayers().getFirst().sendMessage(Text.literal("opening gate;"));
}
openGate(world);
this.gateClosed = false; // Update the gate status after opening it
return;
}
}
}
public class Program {
static List Gates = new ArrayList()
public void onInit(){
Gates.add(new Gate())
}
public void onTick(){
for (Gate gate : Gates) {
gate.update()
}
}
Подробнее здесь: https://stackoverflow.com/questions/793 ... assignment