Skip to content

Commit

Permalink
Use some more syntactic sugar
Browse files Browse the repository at this point in the history
  • Loading branch information
Pyrofab committed Jun 15, 2021
1 parent cf1795d commit 1e6c234
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,8 @@ public <V> void sync(V provider) {
* @throws ClassCastException if <code>provider</code> does not implement {@link ComponentProvider}
*/
public <V> void sync(V provider, ComponentPacketWriter packetWriter) {
C c = this.get(provider);
if (c instanceof AutoSyncedComponent) {
this.sync(provider, packetWriter, (AutoSyncedComponent) c);
if (this.get(provider) instanceof AutoSyncedComponent synced) {
this.sync(provider, packetWriter, synced);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public abstract class MixinWorldChunk {

@Shadow
@Final
private World world;
World world;

@Nullable
@ModifyVariable(method = "updateTicker", at = @At(value = "INVOKE_ASSIGN", target = "Lnet/minecraft/block/BlockState;getBlockEntityTicker(Lnet/minecraft/world/World;Lnet/minecraft/block/entity/BlockEntityType;)Lnet/minecraft/block/entity/BlockEntityTicker;"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,19 @@
package dev.onyxstudios.cca.api.v3.item;

import net.fabricmc.fabric.api.util.NbtType;
import net.minecraft.nbt.*;
import net.minecraft.nbt.NbtByte;
import net.minecraft.nbt.NbtByteArray;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtDouble;
import net.minecraft.nbt.NbtElement;
import net.minecraft.nbt.NbtFloat;
import net.minecraft.nbt.NbtInt;
import net.minecraft.nbt.NbtIntArray;
import net.minecraft.nbt.NbtList;
import net.minecraft.nbt.NbtLong;
import net.minecraft.nbt.NbtLongArray;
import net.minecraft.nbt.NbtShort;
import net.minecraft.nbt.NbtString;

@SuppressWarnings("unused")
public final class CcaNbtType<T extends NbtElement> {
Expand All @@ -41,21 +53,21 @@ public final class CcaNbtType<T extends NbtElement> {
public static final CcaNbtType<NbtLongArray> LONG_ARRAY = new CcaNbtType<>(NbtType.LONG_ARRAY);

public static CcaNbtType<?> byId(int id) {
switch (id) {
case NbtType.BYTE: return BYTE;
case NbtType.SHORT: return SHORT;
case NbtType.INT: return INT;
case NbtType.LONG: return LONG;
case NbtType.FLOAT: return FLOAT;
case NbtType.DOUBLE: return DOUBLE;
case NbtType.BYTE_ARRAY: return BYTE_ARRAY;
case NbtType.STRING: return STRING;
case NbtType.LIST: return LIST;
case NbtType.COMPOUND: return COMPOUND;
case NbtType.INT_ARRAY: return INT_ARRAY;
case NbtType.LONG_ARRAY: return LONG_ARRAY;
default: throw new IllegalArgumentException("Unsupported NBT Type " + id);
}
return switch (id) {
case NbtType.BYTE -> BYTE;
case NbtType.SHORT -> SHORT;
case NbtType.INT -> INT;
case NbtType.LONG -> LONG;
case NbtType.FLOAT -> FLOAT;
case NbtType.DOUBLE -> DOUBLE;
case NbtType.BYTE_ARRAY -> BYTE_ARRAY;
case NbtType.STRING -> STRING;
case NbtType.LIST -> LIST;
case NbtType.COMPOUND -> COMPOUND;
case NbtType.INT_ARRAY -> INT_ARRAY;
case NbtType.LONG_ARRAY -> LONG_ARRAY;
default -> throw new IllegalArgumentException("Unsupported NBT Type " + id);
};
}

private final int type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public interface ScoreboardComponentContainerFactory extends ComponentContainer.

@Deprecated
@Override // just a dumb hack because I'm too lazy to refactor everything rn
default ComponentContainer createContainer(Scoreboard obj) {
default ComponentContainer createContainer(@Nullable Scoreboard obj) {
throw new UnsupportedOperationException("Not supposed to be called :(");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public interface TeamComponentContainerFactory extends ComponentContainer.Factor

@Deprecated
@Override // just a dumb hack because I'm too lazy to refactor everything rn
default ComponentContainer createContainer(Team obj) {
default ComponentContainer createContainer(@Nullable Team obj) {
throw new UnsupportedOperationException("Not supposed to be called :(");
}
}
3 changes: 2 additions & 1 deletion src/test/java/nerdhub/cardinal/components/TestComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import dev.onyxstudios.cca.api.v3.component.Component;
import net.minecraft.nbt.NbtCompound;
import org.jetbrains.annotations.Nullable;

import java.util.Objects;

Expand Down Expand Up @@ -56,7 +57,7 @@ public String toString() {
}

@Override
public boolean equals(Object o) {
public boolean equals(@Nullable Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Impl impl = (Impl) o;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@
import dev.onyxstudios.cca.api.v3.util.GenericComponentInitializer;
import dev.onyxstudios.cca.api.v3.world.WorldComponentFactoryRegistry;
import dev.onyxstudios.cca.api.v3.world.WorldComponentInitializer;
import dev.onyxstudios.componenttest.vita.*;
import dev.onyxstudios.componenttest.vita.AmbientVita;
import dev.onyxstudios.componenttest.vita.BaseVita;
import dev.onyxstudios.componenttest.vita.ItemVita;
import dev.onyxstudios.componenttest.vita.PlayerVita;
import dev.onyxstudios.componenttest.vita.SyncedVita;
import dev.onyxstudios.componenttest.vita.TeamVita;
import dev.onyxstudios.componenttest.vita.Vita;
import net.minecraft.block.entity.EndGatewayBlockEntity;
import net.minecraft.block.entity.EndPortalBlockEntity;
import net.minecraft.entity.LivingEntity;
Expand All @@ -67,7 +73,8 @@ public final class TestComponents implements
public static final Identifier CUSTOM_PROVIDER_2 = new Identifier("componenttest:custom/2");
public static final Identifier CUSTOM_PROVIDER_3 = new Identifier("componenttest:custom/3");

public static final TypeToken<BiFunction<UUID, PlayerEntity, BaseVita>> CUSTOM_FACTORY_TYPE = new TypeToken<BiFunction<UUID, PlayerEntity, BaseVita>>() {};
public static final TypeToken<BiFunction<UUID, PlayerEntity, BaseVita>> CUSTOM_FACTORY_TYPE = new TypeToken<>() {
};

public static final ComponentKey<Vita> VITA = ComponentRegistryV3.INSTANCE.getOrCreate(CardinalComponentsTest.id("vita"), Vita.class);
public static final ComponentKey<Vita> ALT_VITA = ComponentRegistryV3.INSTANCE.getOrCreate(TestStaticComponentInitializer.ALT_VITA_ID, Vita.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public boolean postHit(ItemStack stack, LivingEntity target, LivingEntity holder
AbstractTeam team = holder.getScoreboardTeam();
if (team != null) {
Optional<Vita> vita = TestComponents.VITA.maybeGet(target.getScoreboardTeam());
if (!vita.isPresent()) {
if (vita.isEmpty()) {
vita = TestComponents.VITA.maybeGet(target);
}
vita.ifPresent(v -> v.transferTo(Vita.get(team), 1));
Expand Down

0 comments on commit 1e6c234

Please sign in to comment.