forked from CleanroomMC/ModularUI
-
Notifications
You must be signed in to change notification settings - Fork 21
Added short syncing #104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Added short syncing #104
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
src/main/java/com/cleanroommc/modularui/api/value/IShortValue.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package com.cleanroommc.modularui.api.value; | ||
|
|
||
| public interface IShortValue<T> extends IValue<T> { | ||
|
|
||
| short getShortValue(); | ||
|
|
||
| void setShortValue(short val); | ||
| } |
22 changes: 22 additions & 0 deletions
22
src/main/java/com/cleanroommc/modularui/api/value/sync/IShortSyncValue.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package com.cleanroommc.modularui.api.value.sync; | ||
|
|
||
| import com.cleanroommc.modularui.api.value.IShortValue; | ||
|
|
||
| /** | ||
| * A helper interface for sync values which can be turned into a short. | ||
| * | ||
| * @param <T> value type | ||
| */ | ||
| public interface IShortSyncValue<T> extends IValueSyncHandler<T>, IShortValue<T> { | ||
|
|
||
| @Override | ||
| default void setShortValue(short val) { | ||
| setShortValue(val, true, true); | ||
| } | ||
|
|
||
| default void setShortValue(short val, boolean setSource) { | ||
| setShortValue(val, setSource, true); | ||
| } | ||
|
|
||
| void setShortValue(short value, boolean setSource, boolean sync); | ||
| } |
79 changes: 79 additions & 0 deletions
79
src/main/java/com/cleanroommc/modularui/value/ShortValue.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| package com.cleanroommc.modularui.value; | ||
|
|
||
| import com.cleanroommc.modularui.api.value.IIntValue; | ||
| import com.cleanroommc.modularui.api.value.IShortValue; | ||
|
|
||
| public class ShortValue implements IShortValue<Short>, IIntValue<Short> { | ||
|
|
||
| public static Dynamic wrap(IShortValue<?> val) { | ||
| return new Dynamic(val::getShortValue, val::setShortValue); | ||
| } | ||
|
|
||
| protected short value; | ||
|
|
||
| @Override | ||
| public void setShortValue(short b) { | ||
| value = b; | ||
| } | ||
|
|
||
| @Override | ||
| public short getShortValue() { | ||
| return value; | ||
| } | ||
|
|
||
| @Override | ||
| public Short getValue() { | ||
| return getShortValue(); | ||
| } | ||
|
|
||
| @Override | ||
| public void setValue(Short value) { | ||
| setShortValue(value); | ||
| } | ||
|
|
||
| @Override | ||
| public Class<Short> getValueType() { | ||
| return Short.class; | ||
| } | ||
|
|
||
| @Override | ||
| public int getIntValue() { | ||
| return value; | ||
| } | ||
|
|
||
| @Override | ||
| public void setIntValue(int val) { | ||
| setShortValue((short) val); | ||
| } | ||
|
|
||
| public static class Dynamic extends ShortValue { | ||
|
|
||
| private final Supplier getter; | ||
| private final Consumer setter; | ||
|
|
||
| public Dynamic(Supplier getter, Consumer setter) { | ||
| this.getter = getter; | ||
| this.setter = setter; | ||
| } | ||
|
|
||
| @Override | ||
| public void setShortValue(short b) { | ||
| this.setter.setShort(b); | ||
| } | ||
|
|
||
| @Override | ||
| public short getShortValue() { | ||
| return this.getter.getShort(); | ||
| } | ||
| } | ||
|
|
||
| public interface Supplier { | ||
|
|
||
| short getShort(); | ||
| } | ||
|
|
||
| public interface Consumer { | ||
|
|
||
| void setShort(short b); | ||
| } | ||
| } |
129 changes: 129 additions & 0 deletions
129
src/main/java/com/cleanroommc/modularui/value/sync/ShortSyncValue.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| package com.cleanroommc.modularui.value.sync; | ||
|
|
||
| import com.cleanroommc.modularui.api.value.sync.IIntSyncValue; | ||
| import com.cleanroommc.modularui.api.value.sync.IShortSyncValue; | ||
| import com.cleanroommc.modularui.api.value.sync.IStringSyncValue; | ||
| import com.cleanroommc.modularui.network.NetworkUtils; | ||
|
|
||
| import com.cleanroommc.modularui.value.ShortValue; | ||
|
|
||
| import net.minecraft.network.PacketBuffer; | ||
|
|
||
| import org.jetbrains.annotations.Contract; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.jetbrains.annotations.Nullable; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| public class ShortSyncValue extends ValueSyncHandler<Short> implements IShortSyncValue<Short>, IIntSyncValue<Short>, IStringSyncValue<Short> { | ||
|
|
||
| private short cache; | ||
| private final ShortValue.Supplier getter; | ||
| private final ShortValue.Consumer setter; | ||
|
|
||
| public ShortSyncValue(@NotNull ShortValue.Supplier getter, @Nullable ShortValue.Consumer setter) { | ||
| this.getter = Objects.requireNonNull(getter); | ||
| this.setter = setter; | ||
| this.cache = getter.getShort(); | ||
| } | ||
|
|
||
| public ShortSyncValue(@NotNull ShortValue.Supplier getter) { | ||
| this(getter, (ShortValue.Consumer) null); | ||
| } | ||
|
|
||
| @Contract("null, null -> fail") | ||
| public ShortSyncValue(@Nullable ShortValue.Supplier clientGetter, | ||
| @Nullable ShortValue.Supplier serverGetter) { | ||
| this(clientGetter, null, serverGetter, null); | ||
| } | ||
|
|
||
| @Contract("null, _, null, _ -> fail") | ||
| public ShortSyncValue(@Nullable ShortValue.Supplier clientGetter, @Nullable ShortValue.Consumer clientSetter, | ||
| @Nullable ShortValue.Supplier serverGetter, @Nullable ShortValue.Consumer serverSetter) { | ||
| if (clientGetter == null && serverGetter == null) { | ||
| throw new NullPointerException("Client or server getter must not be null!"); | ||
| } | ||
| if (NetworkUtils.isClient()) { | ||
| this.getter = clientGetter != null ? clientGetter : serverGetter; | ||
| this.setter = clientSetter != null ? clientSetter : serverSetter; | ||
| } else { | ||
| this.getter = serverGetter != null ? serverGetter : clientGetter; | ||
| this.setter = serverSetter != null ? serverSetter : clientSetter; | ||
| } | ||
| this.cache = this.getter.getShort(); | ||
| } | ||
|
|
||
| @Override | ||
| public Short getValue() { | ||
| return this.cache; | ||
| } | ||
|
|
||
| @Override | ||
| public short getShortValue() { | ||
| return this.cache; | ||
| } | ||
|
|
||
| @Override | ||
| public void setValue(Short value, boolean setSource, boolean sync) { | ||
| setShortValue(value, setSource, sync); | ||
| } | ||
|
|
||
| @Override | ||
| public void setShortValue(short value, boolean setSource, boolean sync) { | ||
| this.cache = value; | ||
| if (setSource && this.setter != null) { | ||
| this.setter.setShort(value); | ||
| } | ||
| onValueChanged(); | ||
| if (sync) sync(); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean updateCacheFromSource(boolean isFirstSync) { | ||
| if (isFirstSync || this.getter.getShort() != this.cache) { | ||
| setShortValue(this.getter.getShort(), false, false); | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public void notifyUpdate() { | ||
| setShortValue(this.getter.getShort(), false, true); | ||
| } | ||
|
|
||
| @Override | ||
| public void write(PacketBuffer buffer) { | ||
| buffer.writeShort(this.cache); | ||
| } | ||
|
|
||
| @Override | ||
| public void read(PacketBuffer buffer) { | ||
| setShortValue(buffer.readShort(), true, false); | ||
| } | ||
|
|
||
| @Override | ||
| public void setStringValue(String value, boolean setSource, boolean sync) { | ||
| setShortValue(Short.parseShort(value), setSource, sync); | ||
| } | ||
|
|
||
| @Override | ||
| public String getStringValue() { | ||
| return String.valueOf(this.cache); | ||
| } | ||
|
|
||
| @Override | ||
| public Class<Short> getValueType() { | ||
| return Short.class; | ||
| } | ||
|
|
||
| @Override | ||
| public void setIntValue(int value, boolean setSource, boolean sync) { | ||
| setShortValue((short) value, setSource, sync); | ||
| } | ||
|
|
||
| @Override | ||
| public int getIntValue() { | ||
| return this.cache; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.