fix boolean property

This commit is contained in:
Tungstend 2022-10-21 22:42:29 +08:00
parent 336aec6c57
commit 4682abfa0c
36 changed files with 550 additions and 44 deletions

View File

@ -3,10 +3,10 @@ package com.tungsten.fclcore.auth;
import com.tungsten.fclcore.auth.yggdrasil.Texture;
import com.tungsten.fclcore.auth.yggdrasil.TextureType;
import com.tungsten.fclcore.util.ToStringBuilder;
import com.tungsten.fclcore.util.fakefx.fx.Bindings;
import com.tungsten.fclcore.util.fakefx.fx.InvalidationListener;
import com.tungsten.fclcore.util.fakefx.fx.ObjectBinding;
import com.tungsten.fclcore.util.fakefx.fx.Observable;
import com.tungsten.fclcore.fakefx.Bindings;
import com.tungsten.fclcore.fakefx.InvalidationListener;
import com.tungsten.fclcore.fakefx.ObjectBinding;
import com.tungsten.fclcore.fakefx.Observable;
import com.tungsten.fclcore.util.fakefx.ObservableHelper;
import java.util.Map;

View File

@ -28,8 +28,8 @@ import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.annotations.JsonAdapter;
import com.tungsten.fclcore.auth.yggdrasil.YggdrasilService;
import com.tungsten.fclcore.util.fakefx.fx.InvalidationListener;
import com.tungsten.fclcore.util.fakefx.fx.Observable;
import com.tungsten.fclcore.fakefx.InvalidationListener;
import com.tungsten.fclcore.fakefx.Observable;
import com.tungsten.fclcore.util.fakefx.ObservableHelper;
@JsonAdapter(AuthlibInjectorServer.Deserializer.class)

View File

@ -19,7 +19,7 @@ import com.tungsten.fclcore.auth.yggdrasil.Texture;
import com.tungsten.fclcore.auth.yggdrasil.TextureType;
import com.tungsten.fclcore.auth.yggdrasil.YggdrasilService;
import com.tungsten.fclcore.util.fakefx.BindingMapping;
import com.tungsten.fclcore.util.fakefx.fx.ObjectBinding;
import com.tungsten.fclcore.fakefx.ObjectBinding;
public class MicrosoftAccount extends OAuthAccount {

View File

@ -26,7 +26,7 @@ import com.tungsten.fclcore.game.Arguments;
import com.tungsten.fclcore.game.LaunchOptions;
import com.tungsten.fclcore.util.StringUtils;
import com.tungsten.fclcore.util.ToStringBuilder;
import com.tungsten.fclcore.util.fakefx.fx.ObjectBinding;
import com.tungsten.fclcore.fakefx.ObjectBinding;
import com.tungsten.fclcore.util.gson.UUIDTypeAdapter;
public class OfflineAccount extends Account {

View File

@ -17,7 +17,7 @@ import com.tungsten.fclcore.auth.CredentialExpiredException;
import com.tungsten.fclcore.auth.NoCharacterException;
import com.tungsten.fclcore.auth.ServerResponseMalformedException;
import com.tungsten.fclcore.util.fakefx.BindingMapping;
import com.tungsten.fclcore.util.fakefx.fx.ObjectBinding;
import com.tungsten.fclcore.fakefx.ObjectBinding;
import com.tungsten.fclcore.util.gson.UUIDTypeAdapter;
public class YggdrasilAccount extends ClassicAccount {

View File

@ -1,4 +1,4 @@
package com.tungsten.fclcore.util.fakefx.fx;
package com.tungsten.fclcore.fakefx;
public interface Binding<T> extends ObservableValue<T> {

View File

@ -1,4 +1,4 @@
package com.tungsten.fclcore.util.fakefx.fx;
package com.tungsten.fclcore.fakefx;
import java.lang.ref.WeakReference;

View File

@ -1,4 +1,4 @@
package com.tungsten.fclcore.util.fakefx.fx;
package com.tungsten.fclcore.fakefx;
import java.util.concurrent.Callable;

View File

@ -0,0 +1,92 @@
package com.tungsten.fclcore.fakefx;
public abstract class BooleanBinding extends BooleanExpression implements
Binding<Boolean> {
public BooleanBinding() {
}
private boolean value;
private boolean valid = false;
private BindingHelperObserver observer;
private ExpressionHelper<Boolean> helper = null;
@Override
public void addListener(InvalidationListener listener) {
helper = ExpressionHelper.addListener(helper, this, listener);
}
@Override
public void removeListener(InvalidationListener listener) {
helper = ExpressionHelper.removeListener(helper, listener);
}
@Override
public void addListener(ChangeListener<? super Boolean> listener) {
helper = ExpressionHelper.addListener(helper, this, listener);
}
@Override
public void removeListener(ChangeListener<? super Boolean> listener) {
helper = ExpressionHelper.removeListener(helper, listener);
}
protected final void bind(Observable... dependencies) {
if ((dependencies != null) && (dependencies.length > 0)) {
if (observer == null) {
observer = new BindingHelperObserver(this);
}
for (final Observable dep : dependencies) {
dep.addListener(observer);
}
}
}
protected final void unbind(Observable... dependencies) {
if (observer != null) {
for (final Observable dep : dependencies) {
dep.removeListener(observer);
}
observer = null;
}
}
@Override
public void dispose() {
}
@Override
public final boolean get() {
if (!valid) {
value = computeValue();
valid = true;
}
return value;
}
protected void onInvalidating() {
}
@Override
public final void invalidate() {
if (valid) {
valid = false;
onInvalidating();
ExpressionHelper.fireValueChangedEvent(helper);
}
}
@Override
public final boolean isValid() {
return valid;
}
protected abstract boolean computeValue();
@Override
public String toString() {
return valid ? "BooleanBinding [value: " + get() + "]"
: "BooleanBinding [invalid]";
}
}

View File

@ -0,0 +1,58 @@
package com.tungsten.fclcore.fakefx;
public abstract class BooleanExpression implements ObservableBooleanValue {
public BooleanExpression() {
}
@Override
public Boolean getValue() {
return get();
}
public static BooleanExpression booleanExpression(
final ObservableBooleanValue value) {
if (value == null) {
throw new NullPointerException("Value must be specified.");
}
return (value instanceof BooleanExpression) ? (BooleanExpression) value
: new BooleanBinding() {
{
super.bind(value);
}
@Override
public void dispose() {
super.unbind(value);
}
@Override
protected boolean computeValue() {
return value.get();
}
};
}
public static BooleanExpression booleanExpression(final ObservableValue<Boolean> value) {
if (value == null) {
throw new NullPointerException("Value must be specified.");
}
return (value instanceof BooleanExpression) ? (BooleanExpression) value
: new BooleanBinding() {
{
super.bind(value);
}
@Override
public void dispose() {
super.unbind(value);
}
@Override
protected boolean computeValue() {
final Boolean val = value.getValue();
return val == null ? false : val;
}
};
}
}

View File

@ -0,0 +1,43 @@
package com.tungsten.fclcore.fakefx;
public abstract class BooleanProperty extends ReadOnlyBooleanProperty implements
Property<Boolean>, WritableBooleanValue {
public BooleanProperty() {
}
@Override
public void setValue(Boolean v) {
if (v == null) {
set(false);
} else {
set(v.booleanValue());
}
}
@Override
public void bindBidirectional(Property<Boolean> other) {
}
@Override
public void unbindBidirectional(Property<Boolean> other) {
}
@Override
public String toString() {
final Object bean = getBean();
final String name = getName();
final StringBuilder result = new StringBuilder(
"BooleanProperty [");
if (bean != null) {
result.append("bean: ").append(bean).append(", ");
}
if ((name != null) && (!name.equals(""))) {
result.append("name: ").append(name).append(", ");
}
result.append("value: ").append(get()).append("]");
return result.toString();
}
}

View File

@ -0,0 +1,178 @@
package com.tungsten.fclcore.fakefx;
import java.lang.ref.WeakReference;
public abstract class BooleanPropertyBase extends BooleanProperty {
private boolean value;
private ObservableBooleanValue observable = null;
private InvalidationListener listener = null;
private boolean valid = true;
private ExpressionHelper<Boolean> helper = null;
public BooleanPropertyBase() {
}
public BooleanPropertyBase(boolean initialValue) {
this.value = initialValue;
}
@Override
public void addListener(InvalidationListener listener) {
helper = ExpressionHelper.addListener(helper, this, listener);
}
@Override
public void removeListener(InvalidationListener listener) {
helper = ExpressionHelper.removeListener(helper, listener);
}
@Override
public void addListener(ChangeListener<? super Boolean> listener) {
helper = ExpressionHelper.addListener(helper, this, listener);
}
@Override
public void removeListener(ChangeListener<? super Boolean> listener) {
helper = ExpressionHelper.removeListener(helper, listener);
}
protected void fireValueChangedEvent() {
ExpressionHelper.fireValueChangedEvent(helper);
}
private void markInvalid() {
if (valid) {
valid = false;
invalidated();
fireValueChangedEvent();
}
}
protected void invalidated() {
}
@Override
public boolean get() {
valid = true;
return observable == null ? value : observable.get();
}
@Override
public void set(boolean newValue) {
if (isBound()) {
throw new java.lang.RuntimeException((getBean() != null && getName() != null ?
getBean().getClass().getSimpleName() + "." + getName() + " : ": "") + "A bound value cannot be set.");
}
if (value != newValue) {
value = newValue;
markInvalid();
}
}
@Override
public boolean isBound() {
return observable != null;
}
@Override
public void bind(final ObservableValue<? extends Boolean> rawObservable) {
if (rawObservable == null) {
throw new NullPointerException("Cannot bind to null");
}
final ObservableBooleanValue newObservable = (rawObservable instanceof ObservableBooleanValue) ? (ObservableBooleanValue) rawObservable
: new ValueWrapper(rawObservable);
if (!newObservable.equals(observable)) {
unbind();
observable = newObservable;
if (listener == null) {
listener = new Listener(this);
}
observable.addListener(listener);
markInvalid();
}
}
@Override
public void unbind() {
if (observable != null) {
value = observable.get();
observable.removeListener(listener);
if (observable instanceof ValueWrapper) {
((ValueWrapper)observable).dispose();
}
observable = null;
}
}
@Override
public String toString() {
final Object bean = getBean();
final String name = getName();
final StringBuilder result = new StringBuilder("BooleanProperty [");
if (bean != null) {
result.append("bean: ").append(bean).append(", ");
}
if ((name != null) && (!name.equals(""))) {
result.append("name: ").append(name).append(", ");
}
if (isBound()) {
result.append("bound, ");
if (valid) {
result.append("value: ").append(get());
} else {
result.append("invalid");
}
} else {
result.append("value: ").append(get());
}
result.append("]");
return result.toString();
}
private static class Listener implements InvalidationListener, WeakListener {
private final WeakReference<BooleanPropertyBase> wref;
public Listener(BooleanPropertyBase ref) {
this.wref = new WeakReference<>(ref);
}
@Override
public void invalidated(Observable observable) {
BooleanPropertyBase ref = wref.get();
if (ref == null) {
observable.removeListener(this);
} else {
ref.markInvalid();
}
}
@Override
public boolean wasGarbageCollected() {
return wref.get() == null;
}
}
private class ValueWrapper extends BooleanBinding {
private ObservableValue<? extends Boolean> observable;
public ValueWrapper(ObservableValue<? extends Boolean> observable) {
this.observable = observable;
bind(observable);
}
@Override
protected boolean computeValue() {
final Boolean value = observable.getValue();
return (value == null) ? false : value;
}
@Override
public void dispose() {
unbind(observable);
}
}
}

View File

@ -1,4 +1,4 @@
package com.tungsten.fclcore.util.fakefx.fx;
package com.tungsten.fclcore.fakefx;
public interface ChangeListener<T> {

View File

@ -1,4 +1,4 @@
package com.tungsten.fclcore.util.fakefx.fx;
package com.tungsten.fclcore.fakefx;
import java.util.Arrays;

View File

@ -1,4 +1,4 @@
package com.tungsten.fclcore.util.fakefx.fx;
package com.tungsten.fclcore.fakefx;
import java.util.function.Predicate;

View File

@ -1,4 +1,4 @@
package com.tungsten.fclcore.util.fakefx.fx;
package com.tungsten.fclcore.fakefx;
public interface InvalidationListener {

View File

@ -1,4 +1,4 @@
package com.tungsten.fclcore.util.fakefx.fx;
package com.tungsten.fclcore.fakefx;
public abstract class ObjectBinding<T> extends ObjectExpression<T> implements Binding<T> {

View File

@ -1,4 +1,4 @@
package com.tungsten.fclcore.util.fakefx.fx;
package com.tungsten.fclcore.fakefx;
public abstract class ObjectExpression<T> implements ObservableObjectValue<T> {

View File

@ -1,4 +1,4 @@
package com.tungsten.fclcore.util.fakefx.fx;
package com.tungsten.fclcore.fakefx;
public interface Observable {

View File

@ -0,0 +1,6 @@
package com.tungsten.fclcore.fakefx;
public interface ObservableBooleanValue extends ObservableValue<Boolean> {
boolean get();
}

View File

@ -0,0 +1,19 @@
package com.tungsten.fclcore.fakefx;
import java.util.Collection;
import java.util.List;
public interface ObservableList<E> extends List<E>, Observable {
public boolean addAll(E... elements);
public boolean setAll(E... elements);
public boolean setAll(Collection<? extends E> col);
public boolean removeAll(E... elements);
public boolean retainAll(E... elements);
public void remove(int from, int to);
}

View File

@ -1,4 +1,4 @@
package com.tungsten.fclcore.util.fakefx.fx;
package com.tungsten.fclcore.fakefx;
public interface ObservableObjectValue<T> extends ObservableValue<T> {

View File

@ -1,4 +1,4 @@
package com.tungsten.fclcore.util.fakefx.fx;
package com.tungsten.fclcore.fakefx;
public interface ObservableValue<T> extends Observable {

View File

@ -0,0 +1,15 @@
package com.tungsten.fclcore.fakefx;
public interface Property<T> extends ReadOnlyProperty<T>, WritableValue<T> {
void bind(ObservableValue<? extends T> observable);
void unbind();
boolean isBound();
void bindBidirectional(Property<T> other);
void unbindBidirectional(Property<T> other);
}

View File

@ -0,0 +1,25 @@
package com.tungsten.fclcore.fakefx;
public abstract class ReadOnlyBooleanProperty extends BooleanExpression
implements ReadOnlyProperty<Boolean> {
public ReadOnlyBooleanProperty() {
}
@Override
public String toString() {
final Object bean = getBean();
final String name = getName();
final StringBuilder result = new StringBuilder(
"ReadOnlyBooleanProperty [");
if (bean != null) {
result.append("bean: ").append(bean).append(", ");
}
if ((name != null) && !name.equals("")) {
result.append("name: ").append(name).append(", ");
}
result.append("value: ").append(get()).append("]");
return result.toString();
}
}

View File

@ -0,0 +1,9 @@
package com.tungsten.fclcore.fakefx;
public interface ReadOnlyProperty<T> extends ObservableValue<T> {
Object getBean();
String getName();
}

View File

@ -0,0 +1,39 @@
package com.tungsten.fclcore.fakefx;
public class SimpleBooleanProperty extends BooleanPropertyBase {
private static final Object DEFAULT_BEAN = null;
private static final String DEFAULT_NAME = "";
private final Object bean;
private final String name;
@Override
public Object getBean() {
return bean;
}
@Override
public String getName() {
return name;
}
public SimpleBooleanProperty() {
this(DEFAULT_BEAN, DEFAULT_NAME);
}
public SimpleBooleanProperty(boolean initialValue) {
this(DEFAULT_BEAN, DEFAULT_NAME, initialValue);
}
public SimpleBooleanProperty(Object bean, String name) {
this.bean = bean;
this.name = (name == null) ? DEFAULT_NAME : name;
}
public SimpleBooleanProperty(Object bean, String name, boolean initialValue) {
super(initialValue);
this.bean = bean;
this.name = (name == null) ? DEFAULT_NAME : name;
}
}

View File

@ -1,4 +1,4 @@
package com.tungsten.fclcore.util.fakefx.fx;
package com.tungsten.fclcore.fakefx;
public interface WeakListener {
/**

View File

@ -0,0 +1,12 @@
package com.tungsten.fclcore.fakefx;
public interface WritableBooleanValue extends WritableValue<Boolean> {
boolean get();
void set(boolean value);
@Override
void setValue(Boolean value);
}

View File

@ -0,0 +1,9 @@
package com.tungsten.fclcore.fakefx;
public interface WritableValue<T> {
T getValue();
void setValue(T value);
}

View File

@ -2,20 +2,23 @@ package com.tungsten.fclcore.mod;
import com.google.gson.JsonParseException;
import com.tungsten.fclcore.util.Logging;
import com.tungsten.fclcore.util.StringUtils;
import com.tungsten.fclcore.fakefx.BooleanProperty;
import com.tungsten.fclcore.fakefx.SimpleBooleanProperty;
import com.tungsten.fclcore.util.gson.JsonUtils;
import com.tungsten.fclcore.util.io.CompressingUtils;
import com.tungsten.fclcore.util.io.FileUtils;
import com.tungsten.fclcore.util.io.Unzipper;
import java.io.IOException;
import java.nio.file.*;
import java.util.*;
import java.util.logging.Level;
// Todo : fix
public class Datapack {
private boolean isMultiple;
private final Path path;
private final ObservableList<Pack> info = FXCollections.observableArrayList();
private final ArrayList<Pack> info = new ArrayList<>();
public Datapack(Path path) {
this.path = path;
@ -25,7 +28,7 @@ public class Datapack {
return path;
}
public ObservableList<Pack> getInfo() {
public ArrayList<Pack> getInfo() {
return info;
}
@ -49,12 +52,9 @@ public class Datapack {
if (isMultiple) {
new Unzipper(path, worldPath)
.setReplaceExistentFile(true)
.setFilter(new Unzipper.FileFilter() {
@Override
public boolean accept(Path destPath, boolean isDirectory, Path zipEntry, String entryPath) {
.setFilter((destPath, isDirectory, zipEntry, entryPath) -> {
// We will merge resources.zip instead of replacement.
return !entryPath.equals("resources.zip");
}
})
.unzip();
@ -93,7 +93,7 @@ public class Datapack {
else if (Files.isRegularFile(subPath))
Files.delete(subPath);
Platform.runLater(() -> info.removeIf(p -> p.getId().equals(pack.getId())));
info.removeIf(p -> p.getId().equals(pack.getId()));
}
public void loadFromZip() throws IOException {
@ -107,7 +107,7 @@ public class Datapack {
isMultiple = false;
try {
PackMcMeta pack = JsonUtils.fromNonNullJson(FileUtils.readText(mcmeta), PackMcMeta.class);
Platform.runLater(() -> info.add(new Pack(path, FileUtils.getNameWithoutExtension(path), pack.getPackInfo().getDescription(), this)));
info.add(new Pack(path, FileUtils.getNameWithoutExtension(path), pack.getPackInfo().getDescription(), this));
} catch (IOException | JsonParseException e) {
Logging.LOG.log(Level.WARNING, "Failed to read datapack " + path, e);
}
@ -172,7 +172,7 @@ public class Datapack {
}
}
Platform.runLater(() -> this.info.setAll(info));
this.info.addAll(info);
}
public static class Pack {

View File

@ -1,6 +1,8 @@
package com.tungsten.fclcore.mod;
import com.tungsten.fclcore.util.Logging;
import com.tungsten.fclcore.fakefx.BooleanProperty;
import com.tungsten.fclcore.fakefx.SimpleBooleanProperty;
import com.tungsten.fclcore.util.io.FileUtils;
import java.io.IOException;
@ -9,7 +11,6 @@ import java.util.*;
import java.util.logging.Level;
import java.util.stream.Collectors;
// Todo : fix
public final class LocalModFile implements Comparable<LocalModFile> {
private Path file;

View File

@ -9,10 +9,10 @@ import java.util.function.Supplier;
import static java.util.Objects.requireNonNull;
import com.tungsten.fclcore.util.fakefx.fx.Bindings;
import com.tungsten.fclcore.util.fakefx.fx.ObjectBinding;
import com.tungsten.fclcore.util.fakefx.fx.Observable;
import com.tungsten.fclcore.util.fakefx.fx.ObservableValue;
import com.tungsten.fclcore.fakefx.Bindings;
import com.tungsten.fclcore.fakefx.ObjectBinding;
import com.tungsten.fclcore.fakefx.Observable;
import com.tungsten.fclcore.fakefx.ObservableValue;
public abstract class BindingMapping<T, U> extends ObjectBinding<U> {

View File

@ -1,7 +1,7 @@
package com.tungsten.fclcore.util.fakefx;
import com.tungsten.fclcore.util.fakefx.fx.Bindings;
import com.tungsten.fclcore.util.fakefx.fx.ObjectBinding;
import com.tungsten.fclcore.fakefx.Bindings;
import com.tungsten.fclcore.fakefx.ObjectBinding;
import com.tungsten.fclcore.util.function.ExceptionalFunction;
import java.util.HashMap;

View File

@ -1,7 +1,7 @@
package com.tungsten.fclcore.util.fakefx;
import com.tungsten.fclcore.util.fakefx.fx.InvalidationListener;
import com.tungsten.fclcore.util.fakefx.fx.Observable;
import com.tungsten.fclcore.fakefx.InvalidationListener;
import com.tungsten.fclcore.fakefx.Observable;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

View File

@ -1,6 +1,6 @@
package com.tungsten.fclcore.util.fakefx;
import com.tungsten.fclcore.util.fakefx.fx.ObjectBinding;
import com.tungsten.fclcore.fakefx.ObjectBinding;
import com.tungsten.fclcore.util.function.ExceptionalFunction;
import java.util.Optional;