use of net.minecraft.world.phys.Vec2 in project MinecraftForge by MinecraftForge.
the class OBJModel method makeQuad.
private Pair<BakedQuad, Direction> makeQuad(int[][] indices, int tintIndex, Vector4f colorTint, Vector4f ambientColor, TextureAtlasSprite texture, Transformation transform) {
boolean needsNormalRecalculation = false;
for (int[] ints : indices) {
needsNormalRecalculation |= ints.length < 3;
}
Vector3f faceNormal = new Vector3f(0, 0, 0);
if (needsNormalRecalculation) {
Vector3f a = positions.get(indices[0][0]);
Vector3f ab = positions.get(indices[1][0]);
Vector3f ac = positions.get(indices[2][0]);
Vector3f abs = ab.copy();
abs.sub(a);
Vector3f acs = ac.copy();
acs.sub(a);
abs.cross(acs);
abs.normalize();
faceNormal = abs;
}
Vector4f[] pos = new Vector4f[4];
Vector3f[] norm = new Vector3f[4];
BakedQuadBuilder builder = new BakedQuadBuilder(texture);
builder.setQuadTint(tintIndex);
Vec2 uv2 = new Vec2(0, 0);
if (ambientToFullbright) {
int fakeLight = (int) ((ambientColor.x() + ambientColor.y() + ambientColor.z()) * 15 / 3.0f);
uv2 = new Vec2((fakeLight << 4) / 32767.0f, (fakeLight << 4) / 32767.0f);
builder.setApplyDiffuseLighting(fakeLight == 0);
} else {
builder.setApplyDiffuseLighting(diffuseLighting);
}
boolean hasTransform = !transform.isIdentity();
// The incoming transform is referenced on the center of the block, but our coords are referenced on the corner
Transformation transformation = hasTransform ? transform.blockCenterToCorner() : transform;
for (int i = 0; i < 4; i++) {
int[] index = indices[Math.min(i, indices.length - 1)];
Vector3f pos0 = positions.get(index[0]);
Vector4f position = new Vector4f(pos0);
Vec2 texCoord = index.length >= 2 && texCoords.size() > 0 ? texCoords.get(index[1]) : DEFAULT_COORDS[i];
Vector3f norm0 = !needsNormalRecalculation && index.length >= 3 && normals.size() > 0 ? normals.get(index[2]) : faceNormal;
Vector3f normal = norm0;
Vector4f color = index.length >= 4 && colors.size() > 0 ? colors.get(index[3]) : COLOR_WHITE;
if (hasTransform) {
normal = norm0.copy();
transformation.transformPosition(position);
transformation.transformNormal(normal);
}
;
Vector4f tintedColor = new Vector4f(color.x() * colorTint.x(), color.y() * colorTint.y(), color.z() * colorTint.z(), color.w() * colorTint.w());
putVertexData(builder, position, texCoord, normal, tintedColor, uv2, texture);
pos[i] = position;
norm[i] = normal;
}
builder.setQuadOrientation(Direction.getNearest(norm[0].x(), norm[0].y(), norm[0].z()));
Direction cull = null;
if (detectCullableFaces) {
if (// vertex.position.x
Mth.equal(pos[0].x(), 0) && Mth.equal(pos[1].x(), 0) && Mth.equal(pos[2].x(), 0) && Mth.equal(pos[3].x(), 0) && // vertex.normal.x
norm[0].x() < 0) {
cull = Direction.WEST;
} else if (// vertex.position.x
Mth.equal(pos[0].x(), 1) && Mth.equal(pos[1].x(), 1) && Mth.equal(pos[2].x(), 1) && Mth.equal(pos[3].x(), 1) && // vertex.normal.x
norm[0].x() > 0) {
cull = Direction.EAST;
} else if (// vertex.position.z
Mth.equal(pos[0].z(), 0) && Mth.equal(pos[1].z(), 0) && Mth.equal(pos[2].z(), 0) && Mth.equal(pos[3].z(), 0) && // vertex.normal.z
norm[0].z() < 0) {
// can never remember
cull = Direction.NORTH;
} else if (// vertex.position.z
Mth.equal(pos[0].z(), 1) && Mth.equal(pos[1].z(), 1) && Mth.equal(pos[2].z(), 1) && Mth.equal(pos[3].z(), 1) && // vertex.normal.z
norm[0].z() > 0) {
cull = Direction.SOUTH;
} else if (// vertex.position.y
Mth.equal(pos[0].y(), 0) && Mth.equal(pos[1].y(), 0) && Mth.equal(pos[2].y(), 0) && Mth.equal(pos[3].y(), 0) && // vertex.normal.z
norm[0].y() < 0) {
// can never remember
cull = Direction.DOWN;
} else if (// vertex.position.y
Mth.equal(pos[0].y(), 1) && Mth.equal(pos[1].y(), 1) && Mth.equal(pos[2].y(), 1) && Mth.equal(pos[3].y(), 1) && // vertex.normal.y
norm[0].y() > 0) {
cull = Direction.UP;
}
}
return Pair.of(builder.build(), cull);
}
use of net.minecraft.world.phys.Vec2 in project SpongeCommon by SpongePowered.
the class ServerLevelMixin method impl$constructPostEventForEntityAdd.
@Inject(method = "addEntity", at = @At("HEAD"))
private void impl$constructPostEventForEntityAdd(final Entity entity, final CallbackInfoReturnable<Boolean> cir) {
if (!(entity instanceof EntityBridge)) {
return;
}
if (!((EntityBridge) entity).bridge$isConstructing()) {
return;
}
((EntityBridge) entity).bridge$fireConstructors();
final Vec3 position = entity.position();
final ServerLocation location = ServerLocation.of((ServerWorld) this, position.x(), position.y(), position.z());
final Vec2 rotationVector = entity.getRotationVector();
final Vector3d rotation = new Vector3d(rotationVector.x, rotationVector.y, 0);
try (final CauseStackManager.StackFrame frame = PhaseTracker.SERVER.pushCauseFrame()) {
frame.pushCause(entity);
final Event construct = SpongeEventFactory.createConstructEntityEventPost(frame.currentCause(), (org.spongepowered.api.entity.Entity) entity, location, rotation, ((EntityType<?>) entity.getType()));
SpongeCommon.post(construct);
}
}
use of net.minecraft.world.phys.Vec2 in project SpongeCommon by SpongePowered.
the class SpongeCommandCauseFactory method create.
@Override
@NonNull
public CommandCause create() {
try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
final Cause cause = frame.currentCause();
final CommandSource iCommandSource = cause.first(CommandSource.class).orElseGet(() -> SpongeCommon.game().systemSubject());
final CommandSourceStack commandSource;
if (iCommandSource instanceof CommandSourceProviderBridge) {
// We know about this one so we can create it using the factory method on the source.
commandSource = ((CommandSourceProviderBridge) iCommandSource).bridge$getCommandSource(cause);
} else {
// try to create a command cause from the given ICommandSource, but as Mojang did not see fit to
// put any identifying characteristics on the object, we have to go it alone...
final EventContext context = cause.context();
@Nullable final Locatable locatable = iCommandSource instanceof Locatable ? (Locatable) iCommandSource : null;
final Component displayName;
if (iCommandSource instanceof Entity) {
displayName = ((Entity) iCommandSource).get(Keys.DISPLAY_NAME).map(SpongeAdventure::asVanilla).orElseGet(() -> new TextComponent(iCommandSource instanceof Nameable ? ((Nameable) iCommandSource).name() : iCommandSource.getClass().getSimpleName()));
} else {
displayName = new TextComponent(iCommandSource instanceof Nameable ? ((Nameable) iCommandSource).name() : iCommandSource.getClass().getSimpleName());
}
final String name = displayName.getString();
commandSource = new CommandSourceStack(iCommandSource, context.get(EventContextKeys.LOCATION).map(x -> VecHelper.toVanillaVector3d(x.position())).orElseGet(() -> locatable == null ? Vec3.ZERO : VecHelper.toVanillaVector3d(locatable.location().position())), context.get(EventContextKeys.ROTATION).map(rot -> new Vec2((float) rot.x(), (float) rot.y())).orElse(Vec2.ZERO), context.get(EventContextKeys.LOCATION).map(x -> (ServerLevel) x.world()).orElseGet(() -> locatable == null ? SpongeCommon.server().getLevel(Level.OVERWORLD) : (ServerLevel) locatable.serverLocation().world()), 4, name, displayName, SpongeCommon.server(), iCommandSource instanceof Entity ? (net.minecraft.world.entity.Entity) iCommandSource : null);
}
// We don't want the command source to have altered the cause here (unless there is the special case of the
// server), so we reset it back to what it was (in the ctor of CommandSource, it will add the current source
// to the cause - that's for if the source is created elsewhere, not here)
((CommandSourceStackBridge) commandSource).bridge$setCause(frame.currentCause());
return (CommandCause) commandSource;
}
}
use of net.minecraft.world.phys.Vec2 in project SpongeCommon by SpongePowered.
the class SpongeRegistryLoaders method valueParameter.
@SuppressWarnings("ConstantConditions")
public static RegistryLoader<ValueParameter<?>> valueParameter() {
return RegistryLoader.of(l -> {
l.add(ResourceKeyedValueParameters.BIG_DECIMAL, SpongeBigDecimalValueParameter::new);
l.add(ResourceKeyedValueParameters.BIG_INTEGER, SpongeBigIntegerValueParameter::new);
l.add(ResourceKeyedValueParameters.BLOCK_STATE, k -> ClientNativeArgumentParser.createConverter(k, BlockStateArgument.block(), (reader, cause, state) -> (BlockState) state.getState()));
l.add(ResourceKeyedValueParameters.BOOLEAN, k -> ClientNativeArgumentParser.createIdentity(k, BoolArgumentType.bool()));
l.add(ResourceKeyedValueParameters.COLOR, SpongeColorValueParameter::new);
l.add(ResourceKeyedValueParameters.DATA_CONTAINER, SpongeDataContainerValueParameter::new);
l.add(ResourceKeyedValueParameters.DATE_TIME, SpongeDateTimeValueParameter::new);
l.add(ResourceKeyedValueParameters.DOUBLE, k -> ClientNativeArgumentParser.createIdentity(k, DoubleArgumentType.doubleArg()));
l.add(ResourceKeyedValueParameters.DURATION, SpongeDurationValueParameter::new);
l.add(ResourceKeyedValueParameters.ENTITY, k -> ClientNativeArgumentParser.createConverter(k, EntityArgument.entity(), (reader, cause, selector) -> (Entity) selector.findSingleEntity((CommandSourceStack) cause)));
l.add(ResourceKeyedValueParameters.GAME_PROFILE, SpongeGameProfileValueParameter::new);
l.add(ResourceKeyedValueParameters.INTEGER, k -> ClientNativeArgumentParser.createIdentity(k, IntegerArgumentType.integer()));
l.add(ResourceKeyedValueParameters.IP, SpongeIPAddressValueParameter::new);
l.add(ResourceKeyedValueParameters.ITEM_STACK_SNAPSHOT, k -> ClientNativeArgumentParser.createConverter(k, ItemArgument.item(), (reader, cause, converter) -> new SpongeItemStackSnapshot((ItemStack) (Object) converter.createItemStack(1, true))));
l.add(ResourceKeyedValueParameters.LOCATION, SpongeServerLocationValueParameter::new);
l.add(ResourceKeyedValueParameters.LONG, k -> ClientNativeArgumentParser.createIdentity(k, LongArgumentType.longArg()));
l.add(ResourceKeyedValueParameters.MANY_ENTITIES, k -> ClientNativeArgumentParser.createConverter(k, EntityArgument.entities(), (reader, cause, selector) -> selector.findEntities((CommandSourceStack) cause).stream().map(x -> (Entity) x).collect(Collectors.toList())));
l.add(ResourceKeyedValueParameters.MANY_GAME_PROFILES, k -> ClientNativeArgumentParser.createConverter(k, GameProfileArgument.gameProfile(), (reader, cause, converter) -> converter.getNames((CommandSourceStack) cause)));
l.add(ResourceKeyedValueParameters.MANY_PLAYERS, k -> ClientNativeArgumentParser.createConverter(k, EntityArgument.players(), (reader, cause, selector) -> selector.findPlayers((CommandSourceStack) cause)));
l.add(ResourceKeyedValueParameters.NONE, SpongeNoneValueParameter::new);
l.add(ResourceKeyedValueParameters.OPERATOR, SpongeOperatorValueParameter::new);
l.add(ResourceKeyedValueParameters.PLAYER, k -> ClientNativeArgumentParser.createConverter(k, EntityArgument.player(), (reader, cause, selector) -> (Player) selector.findSinglePlayer((CommandSourceStack) cause)));
l.add(ResourceKeyedValueParameters.PLUGIN, SpongePluginContainerValueParameter::new);
l.add(ResourceKeyedValueParameters.REMAINING_JOINED_STRINGS, k -> ClientNativeArgumentParser.createIdentity(k, StringArgumentType.greedyString()));
l.add(ResourceKeyedValueParameters.RESOURCE_KEY, k -> ClientNativeArgumentParser.createConverter(k, ResourceLocationArgument.id(), (reader, cause, resourceLocation) -> (ResourceKey) (Object) resourceLocation));
l.add(ResourceKeyedValueParameters.ROTATION, k -> ClientNativeArgumentParser.createConverter(k, RotationArgument.rotation(), (reader, cause, coords) -> {
final Vec2 rotation = coords.getRotation((CommandSourceStack) cause);
return new Vector3d(rotation.x, rotation.y, 0);
}));
l.add(ResourceKeyedValueParameters.STRING, k -> ClientNativeArgumentParser.createIdentity(k, StringArgumentType.string()));
l.add(ResourceKeyedValueParameters.TARGET_BLOCK, SpongeTargetBlockValueParameter::new);
l.add(ResourceKeyedValueParameters.TARGET_ENTITY, k -> new SpongeTargetEntityValueParameter(k, false));
l.add(ResourceKeyedValueParameters.TARGET_PLAYER, k -> new SpongeTargetEntityValueParameter(k, true));
l.add(ResourceKeyedValueParameters.TEXT_FORMATTING_CODE, k -> ClientNativeArgumentParser.createConverter(k, StringArgumentType.string(), (reader, cause, result) -> LegacyComponentSerializer.legacyAmpersand().deserialize(result)));
l.add(ResourceKeyedValueParameters.TEXT_FORMATTING_CODE_ALL, k -> ClientNativeArgumentParser.createConverter(k, StringArgumentType.greedyString(), (reader, cause, result) -> LegacyComponentSerializer.legacyAmpersand().deserialize(result)));
l.add(ResourceKeyedValueParameters.TEXT_JSON, k -> ClientNativeArgumentParser.createConverter(k, ComponentArgument.textComponent(), (reader, cause, result) -> SpongeAdventure.asAdventure(result)));
l.add(ResourceKeyedValueParameters.TEXT_JSON_ALL, k -> ClientNativeArgumentParser.createConverter(k, StringArgumentType.greedyString(), (reader, cause, result) -> GsonComponentSerializer.gson().deserialize(result)));
l.add(ResourceKeyedValueParameters.URL, k -> ClientNativeArgumentParser.createConverter(k, StringArgumentType.string(), (reader, cause, input) -> {
try {
return new URL(input);
} catch (final MalformedURLException ex) {
throw new SimpleCommandExceptionType(new TextComponent("Could not parse " + input + " as a URL")).createWithContext(reader);
}
}));
l.add(ResourceKeyedValueParameters.USER, SpongeUserValueParameter::new);
l.add(ResourceKeyedValueParameters.UUID, k -> ClientNativeArgumentParser.createIdentity(k, UuidArgument.uuid()));
l.add(ResourceKeyedValueParameters.VECTOR2D, k -> ClientNativeArgumentParser.createConverter(k, Vec2Argument.vec2(), (reader, cause, result) -> {
final net.minecraft.world.phys.Vec3 r = result.getPosition((CommandSourceStack) cause);
return new Vector2d(r.x, r.z);
}));
l.add(ResourceKeyedValueParameters.VECTOR3D, k -> ClientNativeArgumentParser.createConverter(k, Vec3Argument.vec3(false), (reader, cause, result) -> VecHelper.toVector3d(result.getPosition((CommandSourceStack) cause))));
l.add(ResourceKeyedValueParameters.WORLD, k -> ClientNativeArgumentParser.createConverter(k, DimensionArgument.dimension(), (reader, cause, result) -> Sponge.server().worldManager().world((ResourceKey) (Object) result).orElseThrow(() -> DimensionArgumentAccessor.accessor$ERROR_INVALID_VALUE().createWithContext(reader, result))));
});
}
use of net.minecraft.world.phys.Vec2 in project SpongeCommon by SpongePowered.
the class CommandSourceStackMixin method impl$setCauseOnConstruction.
@Inject(method = CommandSourceStackMixin.PROTECTED_CTOR_METHOD, at = @At("RETURN"))
private void impl$setCauseOnConstruction(final CommandSource p_i49553_1_, final Vec3 p_i49553_2_, final Vec2 p_i49553_3_, final ServerLevel p_i49553_4_, final int p_i49553_5_, final String p_i49553_6_, final Component p_i49553_7_, final MinecraftServer p_i49553_8_, @Nullable final Entity p_i49553_9_, final boolean p_i49553_10_, final ResultConsumer<CommandSourceStack> p_i49553_11_, final EntityAnchorArgument.Anchor p_i49553_12_, final CallbackInfo ci) {
this.impl$cause = PhaseTracker.getCauseStackManager().currentCause();
final EventContext context = this.impl$cause.context();
context.get(EventContextKeys.LOCATION).ifPresent(x -> {
this.worldPosition = VecHelper.toVanillaVector3d(x.position());
this.level = (ServerLevel) x.world();
});
context.get(EventContextKeys.ROTATION).ifPresent(x -> this.rotation = new Vec2((float) x.x(), (float) x.y()));
context.get(EventContextKeys.SUBJECT).ifPresent(x -> {
if (x instanceof EntityAccessor) {
this.permissionLevel = ((EntityAccessor) x).invoker$getPermissionLevel();
} else if (x instanceof MinecraftServer && !((MinecraftServer) x).isSingleplayer()) {
this.permissionLevel = 4;
}
});
}
Aggregations