-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
blaster ADS zoom via entity attr components
- Loading branch information
Showing
14 changed files
with
271 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
|
@@ -8,3 +8,7 @@ loom { | |
} | ||
} | ||
} | ||
|
||
fabricApi { | ||
configureDataGeneration() | ||
} |
26 changes: 26 additions & 0 deletions
26
...ects/pswg_core/src/client/java/dev/pswg/mixin/client/AbstractClientPlayerEntityMixin.java
This file contains 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,26 @@ | ||
package dev.pswg.mixin.client; | ||
|
||
import dev.pswg.attributes.GalaxiesEntityAttributes; | ||
import net.minecraft.client.network.AbstractClientPlayerEntity; | ||
import net.minecraft.entity.player.PlayerEntity; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.ModifyArg; | ||
|
||
@Mixin(AbstractClientPlayerEntity.class) | ||
public abstract class AbstractClientPlayerEntityMixin | ||
{ | ||
/** | ||
* Append our field of view changes to the client player's field of view calculations | ||
* | ||
* @param fieldOfView The client's current field of view | ||
* | ||
* @return The modified value of the client's field of view | ||
*/ | ||
@ModifyArg(method = "Lnet/minecraft/client/network/AbstractClientPlayerEntity;getFovMultiplier(ZF)F", at = @At(value = "INVOKE", target = "Lnet/minecraft/util/math/MathHelper;lerp(FFF)F"), index = 2) | ||
public float getFovMultiplier(float fieldOfView) | ||
{ | ||
var self = (PlayerEntity)(Object)this; | ||
return fieldOfView / (float)self.getAttributeValue(GalaxiesEntityAttributes.FIELD_OF_VIEW_ZOOM); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
projects/pswg_core/src/client/java/dev/pswg/mixin/client/ItemStackMixin.java
This file contains 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,33 @@ | ||
package dev.pswg.mixin.client; | ||
|
||
import dev.pswg.attributes.GalaxiesEntityAttributes; | ||
import net.minecraft.component.type.AttributeModifiersComponent; | ||
import net.minecraft.entity.attribute.EntityAttribute; | ||
import net.minecraft.entity.attribute.EntityAttributeModifier; | ||
import net.minecraft.entity.player.PlayerEntity; | ||
import net.minecraft.item.ItemStack; | ||
import net.minecraft.registry.entry.RegistryEntry; | ||
import net.minecraft.text.Text; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; | ||
|
||
import java.util.function.Consumer; | ||
|
||
@Mixin(ItemStack.class) | ||
public abstract class ItemStackMixin | ||
{ | ||
@Inject(method = "appendAttributeModifierTooltip(Ljava/util/function/Consumer;Lnet/minecraft/entity/player/PlayerEntity;Lnet/minecraft/registry/entry/RegistryEntry;Lnet/minecraft/entity/attribute/EntityAttributeModifier;)V", at = @At("HEAD"), cancellable = true) | ||
public void appendAttributeModifierTooltip(Consumer<Text> textConsumer, PlayerEntity player, RegistryEntry<EntityAttribute> attribute, EntityAttributeModifier modifier, CallbackInfo ci) | ||
{ | ||
if (attribute.matchesId(GalaxiesEntityAttributes.FIELD_OF_VIEW_ZOOM_ID)) | ||
{ | ||
var d = modifier.value(); | ||
|
||
textConsumer.accept(Text.translatable(GalaxiesEntityAttributes.I18N_ATTR_MULTIPLIER, AttributeModifiersComponent.DECIMAL_FORMAT.format(d), Text.translatable(attribute.value().getTranslationKey())).formatted(attribute.value().getFormatting(true))); | ||
|
||
ci.cancel(); | ||
} | ||
} | ||
} |
This file contains 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
4 changes: 4 additions & 0 deletions
4
projects/pswg_core/src/main/generated/assets/pswg/lang/en_us.json
This file contains 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,4 @@ | ||
{ | ||
"pswg.attribute.modifier.multiplier": "%sx %s", | ||
"pswg.attribute.name.field_of_view_zoom": "Zoom" | ||
} |
37 changes: 37 additions & 0 deletions
37
projects/pswg_core/src/main/java/dev/pswg/attributes/AttributeUtil.java
This file contains 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,37 @@ | ||
package dev.pswg.attributes; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import net.minecraft.component.type.AttributeModifiersComponent; | ||
import net.minecraft.entity.attribute.EntityAttribute; | ||
import net.minecraft.entity.attribute.EntityAttributeModifier; | ||
import net.minecraft.registry.entry.RegistryEntry; | ||
|
||
/** | ||
* A collection of utilities related to entity attributes | ||
*/ | ||
public final class AttributeUtil | ||
{ | ||
/** | ||
* Removes a given attribute modifier from the given attribute modifiers component | ||
* | ||
* @param base The component from which the modifier will be removed | ||
* @param attribute The attribute from which the modifier will be removed | ||
* @param modifier The modifier that will be removed | ||
* | ||
* @return The component without the given modifier | ||
*/ | ||
public static AttributeModifiersComponent without(AttributeModifiersComponent base, RegistryEntry<EntityAttribute> attribute, EntityAttributeModifier modifier) | ||
{ | ||
ImmutableList.Builder<AttributeModifiersComponent.Entry> builder = ImmutableList.builderWithExpectedSize(base.modifiers().size() + 1); | ||
|
||
for (AttributeModifiersComponent.Entry entry : base.modifiers()) | ||
{ | ||
if (!entry.matches(attribute, modifier.id())) | ||
{ | ||
builder.add(entry); | ||
} | ||
} | ||
|
||
return new AttributeModifiersComponent(builder.build(), base.showInTooltip()); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
projects/pswg_core/src/main/java/dev/pswg/attributes/GalaxiesEntityAttributes.java
This file contains 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,46 @@ | ||
package dev.pswg.attributes; | ||
|
||
import dev.pswg.Galaxies; | ||
import net.minecraft.entity.attribute.EntityAttribute; | ||
import net.minecraft.registry.Registries; | ||
import net.minecraft.registry.Registry; | ||
import net.minecraft.registry.entry.RegistryEntry; | ||
import net.minecraft.util.Identifier; | ||
|
||
import java.util.function.Function; | ||
|
||
/** | ||
* A collection of common entity attributes used in PSWG modules and addons | ||
*/ | ||
public final class GalaxiesEntityAttributes | ||
{ | ||
public static final String I18N_ATTR_MULTIPLIER = "pswg.attribute.modifier.multiplier"; | ||
|
||
/** | ||
* The ID of the {@link GalaxiesEntityAttributes#FIELD_OF_VIEW_ZOOM} attribute modifier | ||
*/ | ||
public static final Identifier FIELD_OF_VIEW_ZOOM_ID = Galaxies.id("field_of_view_zoom"); | ||
|
||
/** | ||
* An entity attribute that can modify the entity's field of view. Units | ||
* are zoom multipliers (e.g. 2x, 5x, 10x zoom) and not field of view angle | ||
* multipliers. | ||
*/ | ||
public static final RegistryEntry<EntityAttribute> FIELD_OF_VIEW_ZOOM = register( | ||
FIELD_OF_VIEW_ZOOM_ID, | ||
(translationKey) -> new UnclampedEntityAttribute(translationKey, 1).setTracked(true) | ||
); | ||
|
||
/** | ||
* Registers a new entity attribute with a specific ID. | ||
* | ||
* @param id The ID of the entity attribute to register | ||
* @param attributeConstructor A function to construct the entity attribute using a translation key derived from the ID | ||
* | ||
* @return The registered entity attribute entry | ||
*/ | ||
public static RegistryEntry<EntityAttribute> register(Identifier id, Function<String, EntityAttribute> attributeConstructor) | ||
{ | ||
return Registry.registerReference(Registries.ATTRIBUTE, id, attributeConstructor.apply("%s.attribute.name.%s".formatted(id.getNamespace(), id.getPath()))); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
projects/pswg_core/src/main/java/dev/pswg/attributes/UnclampedEntityAttribute.java
This file contains 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,15 @@ | ||
package dev.pswg.attributes; | ||
|
||
import net.minecraft.entity.attribute.EntityAttribute; | ||
|
||
/** | ||
* Represents an attribute for an entity that is not clamped | ||
* to upper and lower bounds | ||
*/ | ||
public class UnclampedEntityAttribute extends EntityAttribute | ||
{ | ||
protected UnclampedEntityAttribute(String translationKey, double fallback) | ||
{ | ||
super(translationKey, fallback); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
projects/pswg_core/src/main/java/dev/pswg/datagen/GalaxiesDataGenerator.java
This file contains 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,44 @@ | ||
package dev.pswg.datagen; | ||
|
||
import dev.pswg.attributes.GalaxiesEntityAttributes; | ||
import net.fabricmc.fabric.api.datagen.v1.DataGeneratorEntrypoint; | ||
import net.fabricmc.fabric.api.datagen.v1.FabricDataGenerator; | ||
import net.fabricmc.fabric.api.datagen.v1.FabricDataOutput; | ||
import net.fabricmc.fabric.api.datagen.v1.provider.FabricLanguageProvider; | ||
import net.minecraft.registry.RegistryWrapper; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
|
||
/** | ||
* The base data generator | ||
*/ | ||
public class GalaxiesDataGenerator implements DataGeneratorEntrypoint | ||
{ | ||
@Override | ||
public void onInitializeDataGenerator(FabricDataGenerator generator) | ||
{ | ||
var pack = generator.createPack(); | ||
|
||
pack.addProvider(LangGenerator::new); | ||
} | ||
|
||
/** | ||
* The base language file generator. All language entries should be | ||
* added through this generator. | ||
*/ | ||
private static class LangGenerator extends FabricLanguageProvider | ||
{ | ||
protected LangGenerator(FabricDataOutput dataOutput, CompletableFuture<RegistryWrapper.WrapperLookup> registryLookup) | ||
{ | ||
super(dataOutput, "en_us", registryLookup); | ||
} | ||
|
||
@Override | ||
public void generateTranslations(RegistryWrapper.WrapperLookup registryLookup, TranslationBuilder translationBuilder) | ||
{ | ||
translationBuilder.add(GalaxiesEntityAttributes.I18N_ATTR_MULTIPLIER, "%sx %s"); | ||
|
||
translationBuilder.add(GalaxiesEntityAttributes.FIELD_OF_VIEW_ZOOM, "Zoom"); | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
projects/pswg_core/src/main/java/dev/pswg/mixin/PlayerEntityMixin.java
This file contains 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,25 @@ | ||
package dev.pswg.mixin; | ||
|
||
import dev.pswg.attributes.GalaxiesEntityAttributes; | ||
import net.minecraft.entity.attribute.DefaultAttributeContainer; | ||
import net.minecraft.entity.player.PlayerEntity; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; | ||
|
||
@Mixin(PlayerEntity.class) | ||
public class PlayerEntityMixin | ||
{ | ||
/** | ||
* Appends our custom attributes to the player's attribute builder | ||
* | ||
* @param cir The builder container | ||
*/ | ||
@Inject(method = "createPlayerAttributes", at = @At("RETURN")) | ||
private static void modifyPlayerAttributes(CallbackInfoReturnable<DefaultAttributeContainer.Builder> cir) | ||
{ | ||
DefaultAttributeContainer.Builder builder = cir.getReturnValue(); | ||
builder.add(GalaxiesEntityAttributes.FIELD_OF_VIEW_ZOOM); | ||
} | ||
} |
This file contains 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
This file contains 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